All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.util.groupcommit.GroupCommitMonitor Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

The newest version!
package com.scalar.db.util.groupcommit;

import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.common.util.concurrent.Uninterruptibles;
import java.io.Closeable;
import java.time.Instant;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class GroupCommitMonitor implements Closeable {
  private static final Logger logger = LoggerFactory.getLogger(GroupCommitMonitor.class);
  private final ExecutorService executorService;

  GroupCommitMonitor(String label, Supplier metricsSupplier) {
    executorService =
        Executors.newSingleThreadExecutor(
            new ThreadFactoryBuilder()
                .setDaemon(true)
                .setNameFormat(label + "-group-commit-monitor-%d")
                .build());
    startExecutorService(metricsSupplier);
  }

  private void startExecutorService(Supplier metricsSupplier) {
    Runnable print =
        () ->
            logger.info(
                "Timestamp={}, GroupCommitMetrics={}", Instant.now(), metricsSupplier.get());

    executorService.execute(
        () -> {
          while (!executorService.isShutdown()) {
            print.run();
            Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
          }
          print.run();
        });
  }

  @Override
  public void close() {
    MoreExecutors.shutdownAndAwaitTermination(executorService, 10, TimeUnit.SECONDS);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy