Search in sources :

Example 1 with LoggingContextAwareExecutorService

use of com.google.gerrit.server.logging.LoggingContextAwareExecutorService in project gerrit by GerritCodeReview.

the class ProjectCacheWarmer method start.

@Override
public void start() {
    int cpus = Runtime.getRuntime().availableProcessors();
    if (config.getBoolean("cache", "projects", "loadOnStartup", false)) {
        ExecutorService pool = new LoggingContextAwareExecutorService(new ScheduledThreadPoolExecutor(config.getInt("cache", "projects", "loadThreads", cpus), new ThreadFactoryBuilder().setNameFormat("ProjectCacheLoader-%d").build()));
        Thread scheduler = new Thread(() -> {
            for (Project.NameKey name : cache.all()) {
                pool.execute(() -> {
                    Optional<ProjectState> project = cache.get(name);
                    if (!project.isPresent()) {
                        throw new IllegalStateException("race while traversing projects. got " + name + " when loading all projects, but can't load it now");
                    }
                });
            }
            pool.shutdown();
            try {
                pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
                logger.atInfo().log("Finished loading project cache");
            } catch (InterruptedException e) {
                logger.atWarning().log("Interrupted while waiting for project cache to load");
            }
        });
        scheduler.setName("ProjectCacheWarmer");
        scheduler.setDaemon(true);
        logger.atInfo().log("Loading project cache");
        scheduler.start();
    }
}
Also used : Project(com.google.gerrit.entities.Project) LoggingContextAwareExecutorService(com.google.gerrit.server.logging.LoggingContextAwareExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) LoggingContextAwareExecutorService(com.google.gerrit.server.logging.LoggingContextAwareExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Project (com.google.gerrit.entities.Project)1 LoggingContextAwareExecutorService (com.google.gerrit.server.logging.LoggingContextAwareExecutorService)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1