LCOV - code coverage report
Current view: top level - server/project - ProjectCacheWarmer.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 9 31 29.0 %
Date: 2022-11-19 15:00:39 Functions: 4 6 66.7 %

          Line data    Source code
       1             : // Copyright (C) 2014 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.project;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.common.util.concurrent.ThreadFactoryBuilder;
      19             : import com.google.gerrit.entities.Project;
      20             : import com.google.gerrit.extensions.events.LifecycleListener;
      21             : import com.google.gerrit.server.config.GerritServerConfig;
      22             : import com.google.gerrit.server.logging.LoggingContextAwareExecutorService;
      23             : import com.google.inject.Inject;
      24             : import com.google.inject.Singleton;
      25             : import java.util.Optional;
      26             : import java.util.concurrent.ExecutorService;
      27             : import java.util.concurrent.ScheduledThreadPoolExecutor;
      28             : import java.util.concurrent.TimeUnit;
      29             : import org.eclipse.jgit.lib.Config;
      30             : 
      31             : @Singleton
      32             : public class ProjectCacheWarmer implements LifecycleListener {
      33         151 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      34             : 
      35             :   private final Config config;
      36             :   private final ProjectCache cache;
      37             : 
      38             :   @Inject
      39         151 :   ProjectCacheWarmer(@GerritServerConfig Config config, ProjectCache cache) {
      40         151 :     this.config = config;
      41         151 :     this.cache = cache;
      42         151 :   }
      43             : 
      44             :   @Override
      45             :   public void start() {
      46         151 :     int cpus = Runtime.getRuntime().availableProcessors();
      47         151 :     if (config.getBoolean("cache", "projects", "loadOnStartup", false)) {
      48           0 :       ExecutorService pool =
      49             :           new LoggingContextAwareExecutorService(
      50             :               new ScheduledThreadPoolExecutor(
      51           0 :                   config.getInt("cache", "projects", "loadThreads", cpus),
      52           0 :                   new ThreadFactoryBuilder().setNameFormat("ProjectCacheLoader-%d").build()));
      53           0 :       Thread scheduler =
      54             :           new Thread(
      55             :               () -> {
      56           0 :                 for (Project.NameKey name : cache.all()) {
      57           0 :                   pool.execute(
      58             :                       () -> {
      59           0 :                         Optional<ProjectState> project = cache.get(name);
      60           0 :                         if (!project.isPresent()) {
      61           0 :                           throw new IllegalStateException(
      62             :                               "race while traversing projects. got "
      63             :                                   + name
      64             :                                   + " when loading all projects, but can't load it now");
      65             :                         }
      66           0 :                       });
      67           0 :                 }
      68           0 :                 pool.shutdown();
      69             :                 try {
      70           0 :                   pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
      71           0 :                   logger.atInfo().log("Finished loading project cache");
      72           0 :                 } catch (InterruptedException e) {
      73           0 :                   logger.atWarning().log("Interrupted while waiting for project cache to load");
      74           0 :                 }
      75           0 :               });
      76           0 :       scheduler.setName("ProjectCacheWarmer");
      77           0 :       scheduler.setDaemon(true);
      78             : 
      79           0 :       logger.atInfo().log("Loading project cache");
      80           0 :       scheduler.start();
      81             :     }
      82         151 :   }
      83             : 
      84             :   @Override
      85         151 :   public void stop() {}
      86             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750