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.

GcBasicPackingTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.Date;
  19. import java.util.List;
  20. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  21. import org.eclipse.jgit.lib.ConfigConstants;
  22. import org.eclipse.jgit.lib.ObjectId;
  23. import org.eclipse.jgit.lib.RefUpdate;
  24. import org.eclipse.jgit.revwalk.RevCommit;
  25. import org.eclipse.jgit.storage.file.FileBasedConfig;
  26. import org.eclipse.jgit.storage.pack.PackConfig;
  27. import org.junit.Test;
  28. import org.junit.experimental.theories.DataPoints;
  29. import org.junit.experimental.theories.Theories;
  30. import org.junit.experimental.theories.Theory;
  31. import org.junit.runner.RunWith;
  32. @RunWith(Theories.class)
  33. public class GcBasicPackingTest extends GcTestCase {
  34. @DataPoints
  35. public static boolean[] aggressiveValues = { true, false };
  36. @Theory
  37. public void repackEmptyRepo_noPackCreated(boolean aggressive)
  38. throws IOException {
  39. configureGc(gc, aggressive);
  40. gc.repack();
  41. assertEquals(0, repo.getObjectDatabase().getPacks().size());
  42. }
  43. @Theory
  44. public void testPackRepoWithNoRefs(boolean aggressive) throws Exception {
  45. tr.commit().add("A", "A").add("B", "B").create();
  46. stats = gc.getStatistics();
  47. assertEquals(4, stats.numberOfLooseObjects);
  48. assertEquals(0, stats.numberOfPackedObjects);
  49. configureGc(gc, aggressive);
  50. gc.gc();
  51. stats = gc.getStatistics();
  52. assertEquals(4, stats.numberOfLooseObjects);
  53. assertEquals(0, stats.numberOfPackedObjects);
  54. assertEquals(0, stats.numberOfPackFiles);
  55. assertEquals(0, stats.numberOfBitmaps);
  56. }
  57. @Theory
  58. public void testPack2Commits(boolean aggressive) throws Exception {
  59. BranchBuilder bb = tr.branch("refs/heads/master");
  60. bb.commit().add("A", "A").add("B", "B").create();
  61. bb.commit().add("A", "A2").add("B", "B2").create();
  62. stats = gc.getStatistics();
  63. assertEquals(8, stats.numberOfLooseObjects);
  64. assertEquals(0, stats.numberOfPackedObjects);
  65. configureGc(gc, aggressive);
  66. gc.gc();
  67. stats = gc.getStatistics();
  68. assertEquals(0, stats.numberOfLooseObjects);
  69. assertEquals(8, stats.numberOfPackedObjects);
  70. assertEquals(1, stats.numberOfPackFiles);
  71. assertEquals(2, stats.numberOfBitmaps);
  72. }
  73. @Theory
  74. public void testPack2Commits_noPackFolder(boolean aggressive) throws Exception {
  75. File packDir = repo.getObjectDatabase().getPackDirectory();
  76. assertTrue(packDir.delete());
  77. BranchBuilder bb = tr.branch("refs/heads/master");
  78. bb.commit().add("A", "A").add("B", "B").create();
  79. bb.commit().add("A", "A2").add("B", "B2").create();
  80. stats = gc.getStatistics();
  81. assertEquals(8, stats.numberOfLooseObjects);
  82. assertEquals(0, stats.numberOfPackedObjects);
  83. configureGc(gc, aggressive);
  84. gc.gc();
  85. stats = gc.getStatistics();
  86. assertEquals(0, stats.numberOfLooseObjects);
  87. assertEquals(8, stats.numberOfPackedObjects);
  88. assertEquals(1, stats.numberOfPackFiles);
  89. assertEquals(2, stats.numberOfBitmaps);
  90. assertTrue(packDir.exists());
  91. }
  92. @Theory
  93. public void testPackAllObjectsInOnePack(boolean aggressive)
  94. throws Exception {
  95. tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
  96. .create();
  97. stats = gc.getStatistics();
  98. assertEquals(4, stats.numberOfLooseObjects);
  99. assertEquals(0, stats.numberOfPackedObjects);
  100. configureGc(gc, aggressive);
  101. gc.gc();
  102. stats = gc.getStatistics();
  103. assertEquals(0, stats.numberOfLooseObjects);
  104. assertEquals(4, stats.numberOfPackedObjects);
  105. assertEquals(1, stats.numberOfPackFiles);
  106. assertEquals(1, stats.numberOfBitmaps);
  107. // Do the gc again and check that it hasn't changed anything
  108. gc.gc();
  109. stats = gc.getStatistics();
  110. assertEquals(0, stats.numberOfLooseObjects);
  111. assertEquals(4, stats.numberOfPackedObjects);
  112. assertEquals(1, stats.numberOfPackFiles);
  113. assertEquals(1, stats.numberOfBitmaps);
  114. }
  115. @Theory
  116. public void testPackCommitsAndLooseOne(boolean aggressive)
  117. throws Exception {
  118. BranchBuilder bb = tr.branch("refs/heads/master");
  119. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  120. bb.commit().add("A", "A2").add("B", "B2").create();
  121. tr.update("refs/heads/master", first);
  122. stats = gc.getStatistics();
  123. assertEquals(8, stats.numberOfLooseObjects);
  124. assertEquals(0, stats.numberOfPackedObjects);
  125. configureGc(gc, aggressive);
  126. gc.gc();
  127. stats = gc.getStatistics();
  128. assertEquals(0, stats.numberOfLooseObjects);
  129. assertEquals(8, stats.numberOfPackedObjects);
  130. assertEquals(2, stats.numberOfPackFiles);
  131. assertEquals(1, stats.numberOfBitmaps);
  132. }
  133. @Theory
  134. public void testNotPackTwice(boolean aggressive) throws Exception {
  135. BranchBuilder bb = tr.branch("refs/heads/master");
  136. RevCommit first = bb.commit().message("M").add("M", "M").create();
  137. bb.commit().message("B").add("B", "Q").create();
  138. bb.commit().message("A").add("A", "A").create();
  139. RevCommit second = tr.commit().parent(first).message("R").add("R", "Q")
  140. .create();
  141. tr.update("refs/tags/t1", second);
  142. Collection<Pack> oldPacks = tr.getRepository().getObjectDatabase()
  143. .getPacks();
  144. assertEquals(0, oldPacks.size());
  145. stats = gc.getStatistics();
  146. assertEquals(11, stats.numberOfLooseObjects);
  147. assertEquals(0, stats.numberOfPackedObjects);
  148. gc.setExpireAgeMillis(0);
  149. fsTick();
  150. configureGc(gc, aggressive);
  151. gc.gc();
  152. stats = gc.getStatistics();
  153. assertEquals(0, stats.numberOfLooseObjects);
  154. List<Pack> packs = new ArrayList<>(
  155. repo.getObjectDatabase().getPacks());
  156. assertEquals(11, packs.get(0).getObjectCount());
  157. }
  158. @Test
  159. public void testDonePruneTooYoungPacks() throws Exception {
  160. BranchBuilder bb = tr.branch("refs/heads/master");
  161. bb.commit().message("M").add("M", "M").create();
  162. String tempRef = "refs/heads/soon-to-be-unreferenced";
  163. BranchBuilder bb2 = tr.branch(tempRef);
  164. bb2.commit().message("M").add("M", "M").create();
  165. gc.setExpireAgeMillis(0);
  166. gc.gc();
  167. stats = gc.getStatistics();
  168. assertEquals(0, stats.numberOfLooseObjects);
  169. assertEquals(4, stats.numberOfPackedObjects);
  170. assertEquals(1, stats.numberOfPackFiles);
  171. File oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
  172. .iterator().next().getPackFile();
  173. fsTick();
  174. // delete the temp ref, orphaning its commit
  175. RefUpdate update = tr.getRepository().getRefDatabase().newUpdate(tempRef, false);
  176. update.setForceUpdate(true);
  177. update.delete();
  178. bb.commit().message("B").add("B", "Q").create();
  179. // The old packfile is too young to be deleted. We should end up with
  180. // two pack files
  181. gc.setExpire(new Date(oldPackfile.lastModified() - 1));
  182. gc.gc();
  183. stats = gc.getStatistics();
  184. assertEquals(0, stats.numberOfLooseObjects);
  185. // if objects exist in multiple packFiles then they are counted multiple
  186. // times
  187. assertEquals(10, stats.numberOfPackedObjects);
  188. assertEquals(2, stats.numberOfPackFiles);
  189. // repack again but now without a grace period for loose objects. Since
  190. // we don't have loose objects anymore this shouldn't change anything
  191. gc.setExpireAgeMillis(0);
  192. gc.gc();
  193. stats = gc.getStatistics();
  194. assertEquals(0, stats.numberOfLooseObjects);
  195. // if objects exist in multiple packFiles then they are counted multiple
  196. // times
  197. assertEquals(10, stats.numberOfPackedObjects);
  198. assertEquals(2, stats.numberOfPackFiles);
  199. // repack again but now without a grace period for packfiles. We should
  200. // end up with one packfile
  201. gc.setPackExpireAgeMillis(0);
  202. // we want to keep newly-loosened objects though
  203. gc.setExpireAgeMillis(-1);
  204. gc.gc();
  205. stats = gc.getStatistics();
  206. assertEquals(1, stats.numberOfLooseObjects);
  207. // if objects exist in multiple packFiles then they are counted multiple
  208. // times
  209. assertEquals(6, stats.numberOfPackedObjects);
  210. assertEquals(1, stats.numberOfPackFiles);
  211. }
  212. @Test
  213. public void testImmediatePruning() throws Exception {
  214. BranchBuilder bb = tr.branch("refs/heads/master");
  215. bb.commit().message("M").add("M", "M").create();
  216. String tempRef = "refs/heads/soon-to-be-unreferenced";
  217. BranchBuilder bb2 = tr.branch(tempRef);
  218. bb2.commit().message("M").add("M", "M").create();
  219. gc.setExpireAgeMillis(0);
  220. gc.gc();
  221. stats = gc.getStatistics();
  222. fsTick();
  223. // delete the temp ref, orphaning its commit
  224. RefUpdate update = tr.getRepository().getRefDatabase().newUpdate(tempRef, false);
  225. update.setForceUpdate(true);
  226. update.delete();
  227. bb.commit().message("B").add("B", "Q").create();
  228. // We want to immediately prune deleted objects
  229. FileBasedConfig config = repo.getConfig();
  230. config.setString(ConfigConstants.CONFIG_GC_SECTION, null,
  231. ConfigConstants.CONFIG_KEY_PRUNEEXPIRE, "now");
  232. config.save();
  233. //And we don't want to keep packs full of dead objects
  234. gc.setPackExpireAgeMillis(0);
  235. gc.gc();
  236. stats = gc.getStatistics();
  237. assertEquals(0, stats.numberOfLooseObjects);
  238. assertEquals(6, stats.numberOfPackedObjects);
  239. assertEquals(1, stats.numberOfPackFiles);
  240. }
  241. @Test
  242. public void testPreserveAndPruneOldPacks() throws Exception {
  243. testPreserveOldPacks();
  244. configureGc(gc, false).setPrunePreserved(true);
  245. gc.gc();
  246. assertFalse(repo.getObjectDatabase().getPreservedDirectory().exists());
  247. }
  248. private void testPreserveOldPacks() throws Exception {
  249. BranchBuilder bb = tr.branch("refs/heads/master");
  250. bb.commit().message("P").add("P", "P").create();
  251. // pack loose object into packfile
  252. gc.setExpireAgeMillis(0);
  253. gc.gc();
  254. PackFile oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
  255. .iterator().next().getPackFile();
  256. assertTrue(oldPackfile.exists());
  257. fsTick();
  258. bb.commit().message("B").add("B", "Q").create();
  259. // repack again but now without a grace period for packfiles. We should
  260. // end up with a new packfile and the old one should be placed in the
  261. // preserved directory
  262. gc.setPackExpireAgeMillis(0);
  263. configureGc(gc, false).setPreserveOldPacks(true);
  264. gc.gc();
  265. File preservedPackFile = oldPackfile.createPreservedForDirectory(
  266. repo.getObjectDatabase().getPreservedDirectory());
  267. assertTrue(preservedPackFile.exists());
  268. }
  269. @Test
  270. public void testPruneAndRestoreOldPacks() throws Exception {
  271. String tempRef = "refs/heads/soon-to-be-unreferenced";
  272. BranchBuilder bb = tr.branch(tempRef);
  273. bb.commit().add("A", "A").add("B", "B").create();
  274. // Verify setup conditions
  275. stats = gc.getStatistics();
  276. assertEquals(4, stats.numberOfLooseObjects);
  277. assertEquals(0, stats.numberOfPackedObjects);
  278. // Force all referenced objects into packs (to avoid having loose objects)
  279. configureGc(gc, false);
  280. gc.setExpireAgeMillis(0);
  281. gc.setPackExpireAgeMillis(0);
  282. gc.gc();
  283. stats = gc.getStatistics();
  284. assertEquals(0, stats.numberOfLooseObjects);
  285. assertEquals(4, stats.numberOfPackedObjects);
  286. assertEquals(1, stats.numberOfPackFiles);
  287. // Delete the temp ref, orphaning its commit
  288. RefUpdate update = tr.getRepository().getRefDatabase().newUpdate(tempRef, false);
  289. update.setForceUpdate(true);
  290. ObjectId objectId = update.getOldObjectId(); // remember it so we can restore it!
  291. RefUpdate.Result result = update.delete();
  292. assertEquals(RefUpdate.Result.FORCED, result);
  293. fsTick();
  294. // Repack with only orphaned commit, so packfile will be pruned
  295. configureGc(gc, false).setPreserveOldPacks(true);
  296. gc.gc();
  297. stats = gc.getStatistics();
  298. assertEquals(0, stats.numberOfLooseObjects);
  299. assertEquals(0, stats.numberOfPackedObjects);
  300. assertEquals(0, stats.numberOfPackFiles);
  301. // Restore the temp ref to the deleted commit, should restore old-packs!
  302. update = tr.getRepository().getRefDatabase().newUpdate(tempRef, false);
  303. update.setNewObjectId(objectId);
  304. update.setExpectedOldObjectId(null);
  305. result = update.update();
  306. assertEquals(RefUpdate.Result.NEW, result);
  307. stats = gc.getStatistics();
  308. assertEquals(4, stats.numberOfPackedObjects);
  309. assertEquals(1, stats.numberOfPackFiles);
  310. }
  311. private PackConfig configureGc(GC myGc, boolean aggressive) {
  312. PackConfig pconfig = new PackConfig(repo);
  313. if (aggressive) {
  314. pconfig.setDeltaSearchWindowSize(250);
  315. pconfig.setMaxDeltaDepth(250);
  316. pconfig.setReuseObjects(false);
  317. } else
  318. pconfig = new PackConfig(repo);
  319. myGc.setPackConfig(pconfig);
  320. return pconfig;
  321. }
  322. }