Line data Source code
1 : // Copyright (C) 2022 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 static java.util.concurrent.TimeUnit.MILLISECONDS; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.extensions.events.LifecycleListener; 21 : import com.google.gerrit.server.config.GerritServerConfig; 22 : import com.google.gerrit.server.config.ScheduleConfig; 23 : import com.google.gerrit.server.git.WorkQueue; 24 : import com.google.inject.Inject; 25 : import java.time.Duration; 26 : import org.eclipse.jgit.lib.Config; 27 : 28 : public class PeriodicProjectListCacheWarmer implements Runnable { 29 151 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 30 : 31 : public static class LifeCycle implements LifecycleListener { 32 : protected final Config config; 33 : protected final WorkQueue queue; 34 : protected final PeriodicProjectListCacheWarmer runner; 35 : 36 : @Inject 37 : LifeCycle( 38 151 : @GerritServerConfig Config config, WorkQueue queue, PeriodicProjectListCacheWarmer runner) { 39 151 : this.config = config; 40 151 : this.queue = queue; 41 151 : this.runner = runner; 42 151 : } 43 : 44 : @Override 45 : public void start() { 46 151 : long interval = -1L; 47 151 : String intervalString = config.getString("cache", ProjectCacheImpl.CACHE_LIST, "interval"); 48 151 : if (!"-1".equals(intervalString)) { 49 151 : long maxAge = 50 151 : config.getTimeUnit("cache", ProjectCacheImpl.CACHE_LIST, "maxAge", -1L, MILLISECONDS); 51 151 : interval = 52 151 : config.getTimeUnit( 53 : "cache", 54 : ProjectCacheImpl.CACHE_LIST, 55 : "interval", 56 151 : getHalfDuration(maxAge), 57 : MILLISECONDS); 58 : } 59 : 60 151 : if (interval == -1L) { 61 151 : logger.atWarning().log("project_list cache warmer is disabled"); 62 151 : return; 63 : } 64 : 65 0 : String startTime = config.getString("cache", ProjectCacheImpl.CACHE_LIST, "startTime"); 66 0 : if (startTime == null) { 67 0 : startTime = "00:00"; 68 : } 69 : 70 0 : runner.run(); 71 0 : queue.scheduleAtFixedRate(runner, ScheduleConfig.Schedule.createOrFail(interval, startTime)); 72 0 : } 73 : 74 : @Override 75 : public void stop() { 76 : // handled by WorkQueue.stop() already 77 151 : } 78 : 79 : private long getHalfDuration(long duration) { 80 151 : if (duration < 0) { 81 151 : return duration; 82 : } 83 0 : return Duration.ofMillis(duration).dividedBy(2L).toMillis(); 84 : } 85 : } 86 : 87 : protected final ProjectCache cache; 88 : 89 : @Inject 90 151 : PeriodicProjectListCacheWarmer(ProjectCache cache) { 91 151 : this.cache = cache; 92 151 : } 93 : 94 : @Override 95 : @SuppressWarnings("CheckReturnValue") 96 : public void run() { 97 0 : logger.atFine().log("Loading project_list cache"); 98 0 : cache.refreshProjectList(); 99 0 : logger.atFine().log("Finished loading project_list cache"); 100 0 : } 101 : }