[case30] talk about servicecomb-saga’s alpha-server

  saga

Order

This article mainly studies the alpha-server of servicecomb-saga.

Start alpha-server

Alpha-server is the distributed transaction coordination center of servicecomb-saga. It is developed by spring boot and can be started directly from jar package. It needs to rely on mysql or pg database and initialize data at the same time. The startup command is as follows:

java -jar -Dspring.profiles.active=prd \
 alpha-server-0.2.0-exec.jar \
 --spring.datasource.url=jdbc:postgresql://localhost:5432/postgres \
 --spring.datasource.username=postgres \
 --spring.datasource.password=postgres

AlphaConfig

@EntityScan(basePackages = "org.apache.servicecomb.saga.alpha")
@Configuration
class AlphaConfig {
  private final BlockingQueue<Runnable> pendingCompensations = new LinkedBlockingQueue<>();
  private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

  @Value("${alpha.compensation.retry.delay:3000}")
  private int delay;

  @Bean
  Map<String, Map<String, OmegaCallback>> omegaCallbacks() {
    return new ConcurrentHashMap<>();
  }

  @Bean
  OmegaCallback omegaCallback(Map<String, Map<String, OmegaCallback>> callbacks) {
    return new PushBackOmegaCallback(pendingCompensations, new CompositeOmegaCallback(callbacks));
  }
  
  @Bean
  TxEventRepository springTxEventRepository(TxEventEnvelopeRepository eventRepo) {
    return new SpringTxEventRepository(eventRepo);
  }

  @Bean
  CommandRepository springCommandRepository(TxEventEnvelopeRepository eventRepo, CommandEntityRepository commandRepository) {
    return new SpringCommandRepository(eventRepo, commandRepository);
  }

  @Bean
  TxTimeoutRepository springTxTimeoutRepository(TxTimeoutEntityRepository timeoutRepo) {
    return new SpringTxTimeoutRepository(timeoutRepo);
  }

  @Bean
  ScheduledExecutorService compensationScheduler() {
    return scheduler;
  }

  @Bean
  GrpcServerConfig grpcServerConfig() { return new GrpcServerConfig(); }

  @Bean
  TxConsistentService txConsistentService(
      @Value("${alpha.event.pollingInterval:500}") int eventPollingInterval,
      GrpcServerConfig serverConfig,
      ScheduledExecutorService scheduler,
      TxEventRepository eventRepository,
      CommandRepository commandRepository,
      TxTimeoutRepository timeoutRepository,
      OmegaCallback omegaCallback,
      Map<String, Map<String, OmegaCallback>> omegaCallbacks) {

    new EventScanner(scheduler,
        eventRepository, commandRepository, timeoutRepository,
        omegaCallback, eventPollingInterval).run();

    TxConsistentService consistentService = new TxConsistentService(eventRepository);

    ServerStartable startable = buildGrpc(serverConfig, consistentService, omegaCallbacks);
    new Thread(startable::start).start();

    return consistentService;
  }

  private ServerStartable buildGrpc(GrpcServerConfig serverConfig, TxConsistentService txConsistentService,
      Map<String, Map<String, OmegaCallback>> omegaCallbacks) {
    return new GrpcStartable(serverConfig,
        new GrpcTxEventEndpointImpl(txConsistentService, omegaCallbacks));
  }

  @PostConstruct
  void init() {
    new PendingTaskRunner(pendingCompensations, delay).run();
  }

  @PreDestroy
  void shutdown() {
    scheduler.shutdownNow();
  }
}
  • This is mainly to start grpc server.
  • The RESPONSIBILITY section includes SpringCommAndre, SpringCommandRepository, SpringTXTimeOutpost

Summary

  • Judging from the whole alpha engineering structure, it is relatively rough. config, repository and other classes are not classified by package name.
  • In addition, the whole project has not been made into spring-boot-starter, which is inconvenient to use jar package embedding for expansion and needs improvement.

doc