You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GCExecutor.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright 2012 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.lang.reflect.Field;
  18. import java.text.MessageFormat;
  19. import java.util.Calendar;
  20. import java.util.Date;
  21. import java.util.Map;
  22. import java.util.Properties;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.concurrent.atomic.AtomicBoolean;
  25. import java.util.concurrent.atomic.AtomicInteger;
  26. import org.eclipse.jgit.api.GarbageCollectCommand;
  27. import org.eclipse.jgit.api.Git;
  28. import org.eclipse.jgit.lib.Repository;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import com.gitblit.manager.IRepositoryManager;
  32. import com.gitblit.models.RepositoryModel;
  33. import com.gitblit.utils.FileUtils;
  34. /**
  35. * The GC executor handles periodic garbage collection in repositories.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public class GCExecutor implements Runnable {
  41. public static enum GCStatus {
  42. READY, COLLECTING;
  43. public boolean exceeds(GCStatus s) {
  44. return ordinal() > s.ordinal();
  45. }
  46. }
  47. private final Logger logger = LoggerFactory.getLogger(GCExecutor.class);
  48. private final IStoredSettings settings;
  49. private AtomicBoolean running = new AtomicBoolean(false);
  50. private AtomicBoolean forceClose = new AtomicBoolean(false);
  51. private final Map<String, GCStatus> gcCache = new ConcurrentHashMap<String, GCStatus>();
  52. public GCExecutor(IStoredSettings settings) {
  53. this.settings = settings;
  54. }
  55. /**
  56. * Indicates if the GC executor is ready to process repositories.
  57. *
  58. * @return true if the GC executor is ready to process repositories
  59. */
  60. public boolean isReady() {
  61. return settings.getBoolean(Keys.git.enableGarbageCollection, false);
  62. }
  63. public boolean isRunning() {
  64. return running.get();
  65. }
  66. public boolean lock(String repositoryName) {
  67. return setGCStatus(repositoryName, GCStatus.COLLECTING);
  68. }
  69. /**
  70. * Tries to set a GCStatus for the specified repository.
  71. *
  72. * @param repositoryName
  73. * @return true if the status has been set
  74. */
  75. private boolean setGCStatus(String repositoryName, GCStatus status) {
  76. String key = repositoryName.toLowerCase();
  77. if (gcCache.containsKey(key)) {
  78. if (gcCache.get(key).exceeds(GCStatus.READY)) {
  79. // already collecting or blocked
  80. return false;
  81. }
  82. }
  83. gcCache.put(key, status);
  84. return true;
  85. }
  86. /**
  87. * Returns true if Gitblit is actively collecting garbage in this repository.
  88. *
  89. * @param repositoryName
  90. * @return true if actively collecting garbage
  91. */
  92. public boolean isCollectingGarbage(String repositoryName) {
  93. String key = repositoryName.toLowerCase();
  94. return gcCache.containsKey(key) && GCStatus.COLLECTING.equals(gcCache.get(key));
  95. }
  96. /**
  97. * Resets the GC status to ready.
  98. *
  99. * @param repositoryName
  100. */
  101. public void releaseLock(String repositoryName) {
  102. gcCache.put(repositoryName.toLowerCase(), GCStatus.READY);
  103. }
  104. public void close() {
  105. forceClose.set(true);
  106. }
  107. @Override
  108. public void run() {
  109. if (!isReady()) {
  110. return;
  111. }
  112. running.set(true);
  113. Date now = new Date();
  114. IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class);
  115. for (String repositoryName : repositoryManager.getRepositoryList()) {
  116. if (forceClose.get()) {
  117. break;
  118. }
  119. if (isCollectingGarbage(repositoryName)) {
  120. logger.warn(MessageFormat.format("Already collecting garbage from {0}?!?", repositoryName));
  121. continue;
  122. }
  123. boolean garbageCollected = false;
  124. RepositoryModel model = null;
  125. Repository repository = null;
  126. try {
  127. model = repositoryManager.getRepositoryModel(repositoryName);
  128. repository = repositoryManager.getRepository(repositoryName);
  129. if (repository == null) {
  130. logger.warn(MessageFormat.format("GCExecutor is missing repository {0}?!?", repositoryName));
  131. continue;
  132. }
  133. if (!isRepositoryIdle(repository)) {
  134. logger.debug(MessageFormat.format("GCExecutor is skipping {0} because it is not idle", repositoryName));
  135. continue;
  136. }
  137. // By setting the GCStatus to COLLECTING we are
  138. // disabling *all* access to this repository from Gitblit.
  139. // Think of this as a clutch in a manual transmission vehicle.
  140. if (!setGCStatus(repositoryName, GCStatus.COLLECTING)) {
  141. logger.warn(MessageFormat.format("Can not acquire GC lock for {0}, skipping", repositoryName));
  142. continue;
  143. }
  144. logger.debug(MessageFormat.format("GCExecutor locked idle repository {0}", repositoryName));
  145. Git git = new Git(repository);
  146. GarbageCollectCommand gc = git.gc();
  147. Properties stats = gc.getStatistics();
  148. // determine if this is a scheduled GC
  149. Calendar cal = Calendar.getInstance();
  150. cal.setTime(model.lastGC);
  151. cal.set(Calendar.HOUR_OF_DAY, 0);
  152. cal.set(Calendar.MINUTE, 0);
  153. cal.set(Calendar.SECOND, 0);
  154. cal.set(Calendar.MILLISECOND, 0);
  155. cal.add(Calendar.DATE, model.gcPeriod);
  156. Date gcDate = cal.getTime();
  157. boolean shouldCollectGarbage = now.after(gcDate);
  158. // determine if filesize triggered GC
  159. long gcThreshold = FileUtils.convertSizeToLong(model.gcThreshold, 500*1024L);
  160. long sizeOfLooseObjects = (Long) stats.get("sizeOfLooseObjects");
  161. boolean hasEnoughGarbage = sizeOfLooseObjects >= gcThreshold;
  162. // if we satisfy one of the requirements, GC
  163. boolean hasGarbage = sizeOfLooseObjects > 0;
  164. if (hasGarbage && (hasEnoughGarbage || shouldCollectGarbage)) {
  165. long looseKB = sizeOfLooseObjects/1024L;
  166. logger.info(MessageFormat.format("Collecting {1} KB of loose objects from {0}", repositoryName, looseKB));
  167. // do the deed
  168. gc.call();
  169. garbageCollected = true;
  170. }
  171. } catch (Exception e) {
  172. logger.error("Error collecting garbage in " + repositoryName, e);
  173. } finally {
  174. // cleanup
  175. if (repository != null) {
  176. if (garbageCollected) {
  177. // update the last GC date
  178. model.lastGC = new Date();
  179. repositoryManager.updateConfiguration(repository, model);
  180. }
  181. repository.close();
  182. }
  183. // reset the GC lock
  184. releaseLock(repositoryName);
  185. logger.debug(MessageFormat.format("GCExecutor released GC lock for {0}", repositoryName));
  186. }
  187. }
  188. running.set(false);
  189. }
  190. private boolean isRepositoryIdle(Repository repository) {
  191. try {
  192. // Read the use count.
  193. // An idle use count is 2:
  194. // +1 for being in the cache
  195. // +1 for the repository parameter in this method
  196. Field useCnt = Repository.class.getDeclaredField("useCnt");
  197. useCnt.setAccessible(true);
  198. int useCount = ((AtomicInteger) useCnt.get(repository)).get();
  199. return useCount == 2;
  200. } catch (Exception e) {
  201. logger.warn(MessageFormat
  202. .format("Failed to reflectively determine use count for repository {0}",
  203. repository.getDirectory().getPath()), e);
  204. }
  205. return false;
  206. }
  207. }