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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.util.Arrays;
  49. import java.util.Collection;
  50. import java.util.Date;
  51. import java.util.Iterator;
  52. import java.util.List;
  53. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.storage.pack.PackConfig;
  56. import org.junit.Test;
  57. import org.junit.experimental.theories.DataPoints;
  58. import org.junit.experimental.theories.Theories;
  59. import org.junit.experimental.theories.Theory;
  60. import org.junit.runner.RunWith;
  61. @RunWith(Theories.class)
  62. public class GcBasicPackingTest extends GcTestCase {
  63. @DataPoints
  64. public static boolean[] aggressiveValues = { true, false };
  65. @Theory
  66. public void repackEmptyRepo_noPackCreated(boolean aggressive)
  67. throws IOException {
  68. configureGc(gc, aggressive);
  69. gc.repack();
  70. assertEquals(0, repo.getObjectDatabase().getPacks().size());
  71. }
  72. @Theory
  73. public void testPackRepoWithNoRefs(boolean aggressive) throws Exception {
  74. tr.commit().add("A", "A").add("B", "B").create();
  75. stats = gc.getStatistics();
  76. assertEquals(4, stats.numberOfLooseObjects);
  77. assertEquals(0, stats.numberOfPackedObjects);
  78. configureGc(gc, aggressive);
  79. gc.gc();
  80. stats = gc.getStatistics();
  81. assertEquals(4, stats.numberOfLooseObjects);
  82. assertEquals(0, stats.numberOfPackedObjects);
  83. assertEquals(0, stats.numberOfPackFiles);
  84. assertEquals(0, stats.numberOfBitmaps);
  85. }
  86. @Theory
  87. public void testPack2Commits(boolean aggressive) throws Exception {
  88. BranchBuilder bb = tr.branch("refs/heads/master");
  89. bb.commit().add("A", "A").add("B", "B").create();
  90. bb.commit().add("A", "A2").add("B", "B2").create();
  91. stats = gc.getStatistics();
  92. assertEquals(8, stats.numberOfLooseObjects);
  93. assertEquals(0, stats.numberOfPackedObjects);
  94. configureGc(gc, aggressive);
  95. gc.gc();
  96. stats = gc.getStatistics();
  97. assertEquals(0, stats.numberOfLooseObjects);
  98. assertEquals(8, stats.numberOfPackedObjects);
  99. assertEquals(1, stats.numberOfPackFiles);
  100. assertEquals(2, stats.numberOfBitmaps);
  101. }
  102. @Theory
  103. public void testPackAllObjectsInOnePack(boolean aggressive)
  104. throws Exception {
  105. tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
  106. .create();
  107. stats = gc.getStatistics();
  108. assertEquals(4, stats.numberOfLooseObjects);
  109. assertEquals(0, stats.numberOfPackedObjects);
  110. configureGc(gc, aggressive);
  111. gc.gc();
  112. stats = gc.getStatistics();
  113. assertEquals(0, stats.numberOfLooseObjects);
  114. assertEquals(4, stats.numberOfPackedObjects);
  115. assertEquals(1, stats.numberOfPackFiles);
  116. assertEquals(1, stats.numberOfBitmaps);
  117. // Do the gc again and check that it hasn't changed anything
  118. gc.gc();
  119. stats = gc.getStatistics();
  120. assertEquals(0, stats.numberOfLooseObjects);
  121. assertEquals(4, stats.numberOfPackedObjects);
  122. assertEquals(1, stats.numberOfPackFiles);
  123. assertEquals(1, stats.numberOfBitmaps);
  124. }
  125. @Theory
  126. public void testPackCommitsAndLooseOne(boolean aggressive)
  127. throws Exception {
  128. BranchBuilder bb = tr.branch("refs/heads/master");
  129. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  130. bb.commit().add("A", "A2").add("B", "B2").create();
  131. tr.update("refs/heads/master", first);
  132. stats = gc.getStatistics();
  133. assertEquals(8, stats.numberOfLooseObjects);
  134. assertEquals(0, stats.numberOfPackedObjects);
  135. configureGc(gc, aggressive);
  136. gc.gc();
  137. stats = gc.getStatistics();
  138. assertEquals(0, stats.numberOfLooseObjects);
  139. assertEquals(8, stats.numberOfPackedObjects);
  140. assertEquals(2, stats.numberOfPackFiles);
  141. assertEquals(1, stats.numberOfBitmaps);
  142. }
  143. @Theory
  144. public void testNotPackTwice(boolean aggressive) throws Exception {
  145. BranchBuilder bb = tr.branch("refs/heads/master");
  146. RevCommit first = bb.commit().message("M").add("M", "M").create();
  147. bb.commit().message("B").add("B", "Q").create();
  148. bb.commit().message("A").add("A", "A").create();
  149. RevCommit second = tr.commit().parent(first).message("R").add("R", "Q")
  150. .create();
  151. tr.update("refs/tags/t1", second);
  152. Collection<PackFile> oldPacks = tr.getRepository().getObjectDatabase()
  153. .getPacks();
  154. assertEquals(0, oldPacks.size());
  155. stats = gc.getStatistics();
  156. assertEquals(11, stats.numberOfLooseObjects);
  157. assertEquals(0, stats.numberOfPackedObjects);
  158. gc.setExpireAgeMillis(0);
  159. fsTick();
  160. configureGc(gc, aggressive);
  161. gc.gc();
  162. stats = gc.getStatistics();
  163. assertEquals(0, stats.numberOfLooseObjects);
  164. Iterator<PackFile> pIt = repo.getObjectDatabase().getPacks().iterator();
  165. long c = pIt.next().getObjectCount();
  166. if (c == 9)
  167. assertEquals(2, pIt.next().getObjectCount());
  168. else {
  169. assertEquals(2, c);
  170. assertEquals(9, pIt.next().getObjectCount());
  171. }
  172. }
  173. @Test
  174. public void testDonePruneTooYoungPacks() throws Exception {
  175. BranchBuilder bb = tr.branch("refs/heads/master");
  176. bb.commit().message("M").add("M", "M").create();
  177. gc.setExpireAgeMillis(0);
  178. gc.gc();
  179. stats = gc.getStatistics();
  180. assertEquals(0, stats.numberOfLooseObjects);
  181. assertEquals(3, stats.numberOfPackedObjects);
  182. assertEquals(1, stats.numberOfPackFiles);
  183. File oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
  184. .iterator().next().getPackFile();
  185. fsTick();
  186. bb.commit().message("B").add("B", "Q").create();
  187. // The old packfile is too young to be deleted. We should end up with
  188. // two pack files
  189. gc.setExpire(new Date(oldPackfile.lastModified() - 1));
  190. gc.gc();
  191. stats = gc.getStatistics();
  192. assertEquals(0, stats.numberOfLooseObjects);
  193. // if objects exist in multiple packFiles then they are counted multiple
  194. // times
  195. assertEquals(9, stats.numberOfPackedObjects);
  196. assertEquals(2, stats.numberOfPackFiles);
  197. // repack again but now without a grace period for packfiles. We should
  198. // end up with one packfile
  199. gc.setExpireAgeMillis(0);
  200. gc.gc();
  201. stats = gc.getStatistics();
  202. assertEquals(0, stats.numberOfLooseObjects);
  203. // if objects exist in multiple packFiles then they are counted multiple
  204. // times
  205. assertEquals(6, stats.numberOfPackedObjects);
  206. assertEquals(1, stats.numberOfPackFiles);
  207. }
  208. @Test
  209. public void testBitmapSpansNoMerges() throws Exception {
  210. /*
  211. * Commit counts -> expected bitmap counts for history without merges.
  212. * The top 100 contiguous commits should always have bitmaps, and the
  213. * "recent" bitmaps beyond that are spaced out every 100-200 commits.
  214. * (Starting at 100, the next 100 commits are searched for a merge
  215. * commit. Since one is not found, the spacing between commits is 200.
  216. */
  217. int[][] bitmapCounts = { //
  218. { 1, 1 }, { 50, 50 }, { 99, 99 }, { 100, 100 }, { 101, 100 },
  219. { 200, 100 }, { 201, 100 }, { 299, 100 }, { 300, 101 },
  220. { 301, 101 }, { 401, 101 }, { 499, 101 }, { 500, 102 }, };
  221. int currentCommits = 0;
  222. BranchBuilder bb = tr.branch("refs/heads/main");
  223. for (int[] counts : bitmapCounts) {
  224. int nextCommitCount = counts[0];
  225. int expectedBitmapCount = counts[1];
  226. assertTrue(nextCommitCount > currentCommits); // programming error
  227. for (int i = currentCommits; i < nextCommitCount; i++) {
  228. String str = "A" + i;
  229. bb.commit().message(str).add(str, str).create();
  230. }
  231. currentCommits = nextCommitCount;
  232. gc.setExpireAgeMillis(0); // immediately delete old packs
  233. gc.gc();
  234. assertEquals(currentCommits * 3, // commit/tree/object
  235. gc.getStatistics().numberOfPackedObjects);
  236. assertEquals(currentCommits + " commits: ", expectedBitmapCount,
  237. gc.getStatistics().numberOfBitmaps);
  238. }
  239. }
  240. @Test
  241. public void testBitmapSpansWithMerges() throws Exception {
  242. /*
  243. * Commits that are merged. Since 55 is in the oldest history it is
  244. * never considered. Searching goes from oldest to newest so 115 is the
  245. * first merge commit found. After that the range 116-216 is ignored so
  246. * 175 is never considered.
  247. */
  248. List<Integer> merges = Arrays.asList(Integer.valueOf(55),
  249. Integer.valueOf(115), Integer.valueOf(175),
  250. Integer.valueOf(235));
  251. /*
  252. * Commit counts -> expected bitmap counts for history with merges. The
  253. * top 100 contiguous commits should always have bitmaps, and the
  254. * "recent" bitmaps beyond that are spaced out every 100-200 commits.
  255. * Merges in the < 100 range have no effect and merges in the > 100
  256. * range will only be considered for commit counts > 200.
  257. */
  258. int[][] bitmapCounts = { //
  259. { 1, 1 }, { 55, 55 }, { 56, 57 }, // +1 bitmap from branch A55
  260. { 99, 100 }, // still +1 branch @55
  261. { 100, 100 }, // 101 commits, only 100 newest
  262. { 116, 100 }, // @55 still in 100 newest bitmaps
  263. { 176, 101 }, // @55 branch tip is not in 100 newest
  264. { 213, 101 }, // 216 commits, @115&@175 in 100 newest
  265. { 214, 102 }, // @55 branch tip, merge @115, @177 in newest
  266. { 236, 102 }, // all 4 merge points in history
  267. { 273, 102 }, // 277 commits, @175&@235 in newest
  268. { 274, 103 }, // @55, @115, merge @175, @235 in newest
  269. { 334, 103 }, // @55,@115,@175, @235 in newest
  270. { 335, 104 }, // @55,@115,@175, merge @235
  271. { 435, 104 }, // @55,@115,@175,@235 tips
  272. { 436, 104 }, // force @236
  273. };
  274. int currentCommits = 0;
  275. BranchBuilder bb = tr.branch("refs/heads/main");
  276. for (int[] counts : bitmapCounts) {
  277. int nextCommitCount = counts[0];
  278. int expectedBitmapCount = counts[1];
  279. assertTrue(nextCommitCount > currentCommits); // programming error
  280. for (int i = currentCommits; i < nextCommitCount; i++) {
  281. String str = "A" + i;
  282. if (!merges.contains(Integer.valueOf(i))) {
  283. bb.commit().message(str).add(str, str).create();
  284. } else {
  285. BranchBuilder bbN = tr.branch("refs/heads/A" + i);
  286. bb.commit().message(str).add(str, str)
  287. .parent(bbN.commit().create()).create();
  288. }
  289. }
  290. currentCommits = nextCommitCount;
  291. gc.setExpireAgeMillis(0); // immediately delete old packs
  292. gc.gc();
  293. assertEquals(currentCommits + " commits: ", expectedBitmapCount,
  294. gc.getStatistics().numberOfBitmaps);
  295. }
  296. }
  297. @Test
  298. public void testBitmapsForExcessiveBranches() throws Exception {
  299. int oneDayInSeconds = 60 * 60 * 24;
  300. // All of branch A is committed on day1
  301. BranchBuilder bbA = tr.branch("refs/heads/A");
  302. for (int i = 0; i < 1001; i++) {
  303. String msg = "A" + i;
  304. bbA.commit().message(msg).add(msg, msg).create();
  305. }
  306. // All of in branch B is committed on day91
  307. tr.tick(oneDayInSeconds * 90);
  308. BranchBuilder bbB = tr.branch("refs/heads/B");
  309. for (int i = 0; i < 1001; i++) {
  310. String msg = "B" + i;
  311. bbB.commit().message(msg).add(msg, msg).create();
  312. }
  313. // Create 100 other branches with a single commit
  314. for (int i = 0; i < 100; i++) {
  315. BranchBuilder bb = tr.branch("refs/heads/N" + i);
  316. String msg = "singlecommit" + i;
  317. bb.commit().message(msg).add(msg, msg).create();
  318. }
  319. // now is day92
  320. tr.tick(oneDayInSeconds);
  321. // Since there are no merges, commits in recent history are selected
  322. // every 200 commits.
  323. final int commitsForSparseBranch = 1 + (1001 / 200);
  324. final int commitsForFullBranch = 100 + (901 / 200);
  325. final int commitsForShallowBranches = 100;
  326. // Excessive branch history pruning, one old branch.
  327. gc.gc();
  328. assertEquals(
  329. commitsForSparseBranch + commitsForFullBranch
  330. + commitsForShallowBranches,
  331. gc.getStatistics().numberOfBitmaps);
  332. }
  333. private void configureGc(GC myGc, boolean aggressive) {
  334. PackConfig pconfig = new PackConfig(repo);
  335. if (aggressive) {
  336. pconfig.setDeltaSearchWindowSize(250);
  337. pconfig.setMaxDeltaDepth(250);
  338. pconfig.setReuseObjects(false);
  339. } else
  340. pconfig = new PackConfig(repo);
  341. myGc.setPackConfig(pconfig);
  342. }
  343. }