Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

GcCommitSelectionTest.java 11KB

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