LCOV - code coverage report
Current view: top level - server/plugins - PluginCleanerTask.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 38 68.4 %
Date: 2022-11-19 15:00:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2012 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.plugins;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.server.git.WorkQueue;
      19             : import com.google.gerrit.server.util.time.TimeUtil;
      20             : import com.google.inject.Inject;
      21             : import com.google.inject.Singleton;
      22             : import java.util.concurrent.Future;
      23             : import java.util.concurrent.TimeUnit;
      24             : 
      25             : @Singleton
      26             : class PluginCleanerTask implements Runnable {
      27         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      28             : 
      29             :   private final WorkQueue workQueue;
      30             :   private final PluginLoader loader;
      31             :   private volatile int pending;
      32             :   private Future<?> self;
      33             :   private int attempts;
      34             :   private long start;
      35             : 
      36             :   @Inject
      37         138 :   PluginCleanerTask(WorkQueue workQueue, PluginLoader loader) {
      38         138 :     this.workQueue = workQueue;
      39         138 :     this.loader = loader;
      40         138 :   }
      41             : 
      42             :   @Override
      43             :   public void run() {
      44             :     try {
      45           2 :       for (int t = 0; t < 2 * (attempts + 1); t++) {
      46           2 :         System.gc();
      47           2 :         Thread.sleep(50);
      48             :       }
      49           0 :     } catch (InterruptedException e) {
      50             :       // Ignored
      51           2 :     }
      52             : 
      53           2 :     int left = loader.processPendingCleanups();
      54           2 :     synchronized (this) {
      55           2 :       pending = left;
      56           2 :       self = null;
      57             : 
      58           2 :       if (0 < left) {
      59           0 :         long waiting = TimeUtil.nowMs() - start;
      60           0 :         logger.atWarning().log(
      61             :             "%d plugins still waiting to be reclaimed after %d minutes",
      62           0 :             pending, TimeUnit.MILLISECONDS.toMinutes(waiting));
      63           0 :         attempts = Math.min(attempts + 1, 15);
      64           0 :         ensureScheduled();
      65           0 :       } else {
      66           2 :         attempts = 0;
      67             :       }
      68           2 :     }
      69           2 :   }
      70             : 
      71             :   @Override
      72             :   public String toString() {
      73           0 :     int p = pending;
      74           0 :     if (0 < p) {
      75           0 :       return String.format("Plugin Cleaner (waiting for %d plugins)", p);
      76             :     }
      77           0 :     return "Plugin Cleaner";
      78             :   }
      79             : 
      80             :   synchronized void clean(int expect) {
      81           2 :     if (self == null && pending == 0) {
      82           2 :       start = TimeUtil.nowMs();
      83             :     }
      84           2 :     pending = expect;
      85           2 :     ensureScheduled();
      86           2 :   }
      87             : 
      88             :   private void ensureScheduled() {
      89           2 :     if (self == null && 0 < pending) {
      90           2 :       if (attempts == 1) {
      91           0 :         self = workQueue.getDefaultQueue().schedule(this, 30, TimeUnit.SECONDS);
      92             :       } else {
      93           2 :         self = workQueue.getDefaultQueue().schedule(this, attempts + 1, TimeUnit.MINUTES);
      94             :       }
      95             :     }
      96           2 :   }
      97             : }

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