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.

GcCommitSelectionTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2015, Google Inc.
  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.pack;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.Arrays;
  49. import java.util.Collections;
  50. import java.util.List;
  51. import java.util.Set;
  52. import org.eclipse.jgit.internal.storage.file.GcTestCase;
  53. import org.eclipse.jgit.internal.storage.file.PackBitmapIndexBuilder;
  54. import org.eclipse.jgit.internal.storage.pack.PackWriterBitmapPreparer.BitmapCommit;
  55. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  56. import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.NullProgressMonitor;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.revwalk.RevCommit;
  61. import org.eclipse.jgit.storage.pack.PackConfig;
  62. import org.junit.Test;
  63. public class GcCommitSelectionTest extends GcTestCase {
  64. @Test
  65. public void testBitmapSpansNoMerges() throws Exception {
  66. /*
  67. * Commit counts -> expected bitmap counts for history without merges.
  68. * The top 100 contiguous commits should always have bitmaps, and the
  69. * "recent" bitmaps beyond that are spaced out every 100-200 commits.
  70. * (Starting at 100, the next 100 commits are searched for a merge
  71. * commit. Since one is not found, the spacing between commits is 200.
  72. */
  73. int[][] bitmapCounts = { //
  74. { 1, 1 }, { 50, 50 }, { 99, 99 }, { 100, 100 }, { 101, 100 },
  75. { 200, 100 }, { 201, 100 }, { 299, 100 }, { 300, 101 },
  76. { 301, 101 }, { 401, 101 }, { 499, 101 }, { 500, 102 }, };
  77. int currentCommits = 0;
  78. BranchBuilder bb = tr.branch("refs/heads/main");
  79. for (int[] counts : bitmapCounts) {
  80. int nextCommitCount = counts[0];
  81. int expectedBitmapCount = counts[1];
  82. assertTrue(nextCommitCount > currentCommits); // programming error
  83. for (int i = currentCommits; i < nextCommitCount; i++) {
  84. String str = "A" + i;
  85. bb.commit().message(str).add(str, str).create();
  86. }
  87. currentCommits = nextCommitCount;
  88. gc.setPackExpireAgeMillis(0); // immediately delete old packs
  89. gc.setExpireAgeMillis(0);
  90. gc.gc();
  91. assertEquals(currentCommits * 3, // commit/tree/object
  92. gc.getStatistics().numberOfPackedObjects);
  93. assertEquals(currentCommits + " commits: ", expectedBitmapCount,
  94. gc.getStatistics().numberOfBitmaps);
  95. }
  96. }
  97. @Test
  98. public void testBitmapSpansWithMerges() throws Exception {
  99. /*
  100. * Commits that are merged. Since 55 is in the oldest history it is
  101. * never considered. Searching goes from oldest to newest so 115 is the
  102. * first merge commit found. After that the range 116-216 is ignored so
  103. * 175 is never considered.
  104. */
  105. List<Integer> merges = Arrays.asList(Integer.valueOf(55),
  106. Integer.valueOf(115), Integer.valueOf(175),
  107. Integer.valueOf(235));
  108. /*
  109. * Commit counts -> expected bitmap counts for history with merges. The
  110. * top 100 contiguous commits should always have bitmaps, and the
  111. * "recent" bitmaps beyond that are spaced out every 100-200 commits.
  112. * Merges in the < 100 range have no effect and merges in the > 100
  113. * range will only be considered for commit counts > 200.
  114. */
  115. int[][] bitmapCounts = { //
  116. { 1, 1 }, { 55, 55 }, { 56, 57 }, // +1 bitmap from branch A55
  117. { 99, 100 }, // still +1 branch @55
  118. { 100, 100 }, // 101 commits, only 100 newest
  119. { 116, 100 }, // @55 still in 100 newest bitmaps
  120. { 176, 101 }, // @55 branch tip is not in 100 newest
  121. { 213, 101 }, // 216 commits, @115&@175 in 100 newest
  122. { 214, 102 }, // @55 branch tip, merge @115, @177 in newest
  123. { 236, 102 }, // all 4 merge points in history
  124. { 273, 102 }, // 277 commits, @175&@235 in newest
  125. { 274, 103 }, // @55, @115, merge @175, @235 in newest
  126. { 334, 103 }, // @55,@115,@175, @235 in newest
  127. { 335, 104 }, // @55,@115,@175, merge @235
  128. { 435, 104 }, // @55,@115,@175,@235 tips
  129. { 436, 104 }, // force @236
  130. };
  131. int currentCommits = 0;
  132. BranchBuilder bb = tr.branch("refs/heads/main");
  133. for (int[] counts : bitmapCounts) {
  134. int nextCommitCount = counts[0];
  135. int expectedBitmapCount = counts[1];
  136. assertTrue(nextCommitCount > currentCommits); // programming error
  137. for (int i = currentCommits; i < nextCommitCount; i++) {
  138. String str = "A" + i;
  139. if (!merges.contains(Integer.valueOf(i))) {
  140. bb.commit().message(str).add(str, str).create();
  141. } else {
  142. BranchBuilder bbN = tr.branch("refs/heads/A" + i);
  143. bb.commit().message(str).add(str, str)
  144. .parent(bbN.commit().create()).create();
  145. }
  146. }
  147. currentCommits = nextCommitCount;
  148. gc.setPackExpireAgeMillis(0); // immediately delete old packs
  149. gc.setExpireAgeMillis(0);
  150. gc.gc();
  151. assertEquals(currentCommits + " commits: ", expectedBitmapCount,
  152. gc.getStatistics().numberOfBitmaps);
  153. }
  154. }
  155. @Test
  156. public void testBitmapsForExcessiveBranches() throws Exception {
  157. int oneDayInSeconds = 60 * 60 * 24;
  158. // All of branch A is committed on day1
  159. BranchBuilder bbA = tr.branch("refs/heads/A");
  160. for (int i = 0; i < 1001; i++) {
  161. String msg = "A" + i;
  162. bbA.commit().message(msg).add(msg, msg).create();
  163. }
  164. // All of in branch B is committed on day91
  165. tr.tick(oneDayInSeconds * 90);
  166. BranchBuilder bbB = tr.branch("refs/heads/B");
  167. for (int i = 0; i < 1001; i++) {
  168. String msg = "B" + i;
  169. bbB.commit().message(msg).add(msg, msg).create();
  170. }
  171. // Create 100 other branches with a single commit
  172. for (int i = 0; i < 100; i++) {
  173. BranchBuilder bb = tr.branch("refs/heads/N" + i);
  174. String msg = "singlecommit" + i;
  175. bb.commit().message(msg).add(msg, msg).create();
  176. }
  177. // now is day92
  178. tr.tick(oneDayInSeconds);
  179. // Since there are no merges, commits in recent history are selected
  180. // every 200 commits.
  181. final int commitsForSparseBranch = 1 + (1001 / 200);
  182. final int commitsForFullBranch = 100 + (901 / 200);
  183. final int commitsForShallowBranches = 100;
  184. // Excessive branch history pruning, one old branch.
  185. gc.setPackExpireAgeMillis(0); // immediately delete old packs
  186. gc.setExpireAgeMillis(0);
  187. gc.gc();
  188. assertEquals(
  189. commitsForSparseBranch + commitsForFullBranch
  190. + commitsForShallowBranches,
  191. gc.getStatistics().numberOfBitmaps);
  192. }
  193. @Test
  194. public void testSelectionOrderingWithChains() throws Exception {
  195. /*-
  196. * Create a history like this, where 'N' is the number of seconds from
  197. * the first commit in the branch:
  198. *
  199. * ---o---o---o commits b3,b5,b7
  200. * / \
  201. * o--o--o---o---o---o--o commits m0,m1,m2,m4,m6,m8,m9
  202. */
  203. BranchBuilder bb = tr.branch("refs/heads/main");
  204. RevCommit m0 = addCommit(bb, "m0");
  205. RevCommit m1 = addCommit(bb, "m1", m0);
  206. RevCommit m2 = addCommit(bb, "m2", m1);
  207. RevCommit b3 = addCommit(bb, "b3", m1);
  208. RevCommit m4 = addCommit(bb, "m4", m2);
  209. RevCommit b5 = addCommit(bb, "m5", b3);
  210. RevCommit m6 = addCommit(bb, "m6", m4);
  211. RevCommit b7 = addCommit(bb, "m7", b5);
  212. RevCommit m8 = addCommit(bb, "m8", m6, b7);
  213. RevCommit m9 = addCommit(bb, "m9", m8);
  214. List<RevCommit> commits = Arrays.asList(m0, m1, m2, b3, m4, b5, m6, b7,
  215. m8, m9);
  216. PackWriterBitmapPreparer preparer = newPeparer(m9, commits);
  217. List<BitmapCommit> selection = new ArrayList<>(
  218. preparer.selectCommits(commits.size()));
  219. // Verify that the output is ordered by the separate "chains"
  220. String[] expected = { m0.name(), m1.name(), m2.name(), m4.name(),
  221. m6.name(), m8.name(), m9.name(), b3.name(), b5.name(),
  222. b7.name() };
  223. assertEquals(expected.length, selection.size());
  224. for (int i = 0; i < expected.length; i++) {
  225. assertEquals("Entry " + i, expected[i], selection.get(i).getName());
  226. }
  227. }
  228. private RevCommit addCommit(BranchBuilder bb, String msg,
  229. RevCommit... parents) throws Exception {
  230. CommitBuilder commit = bb.commit().message(msg).add(msg, msg).tick(1)
  231. .noParents();
  232. for (RevCommit parent : parents) {
  233. commit.parent(parent);
  234. }
  235. return commit.create();
  236. }
  237. private PackWriterBitmapPreparer newPeparer(RevCommit want,
  238. List<RevCommit> commits)
  239. throws IOException {
  240. List<ObjectToPack> objects = new ArrayList<>(commits.size());
  241. for (RevCommit commit : commits) {
  242. objects.add(new ObjectToPack(commit, Constants.OBJ_COMMIT));
  243. }
  244. Set<ObjectId> wants = Collections.singleton((ObjectId) want);
  245. PackConfig config = new PackConfig();
  246. PackBitmapIndexBuilder builder = new PackBitmapIndexBuilder(objects);
  247. return new PackWriterBitmapPreparer(
  248. tr.getRepository().newObjectReader(), builder,
  249. NullProgressMonitor.INSTANCE, wants, config);
  250. }
  251. }