View Javadoc
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  
44  package org.eclipse.jgit.internal.storage.pack;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.IOException;
50  import java.util.ArrayList;
51  import java.util.Arrays;
52  import java.util.Collections;
53  import java.util.HashSet;
54  import java.util.List;
55  import java.util.Set;
56  
57  import org.eclipse.jgit.internal.storage.file.GcTestCase;
58  import org.eclipse.jgit.internal.storage.file.PackBitmapIndexBuilder;
59  import org.eclipse.jgit.internal.storage.pack.PackWriterBitmapPreparer.BitmapCommit;
60  import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
61  import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
62  import org.eclipse.jgit.lib.Constants;
63  import org.eclipse.jgit.lib.NullProgressMonitor;
64  import org.eclipse.jgit.lib.ObjectId;
65  import org.eclipse.jgit.revwalk.RevCommit;
66  import org.eclipse.jgit.storage.pack.PackConfig;
67  import org.junit.Test;
68  
69  public class GcCommitSelectionTest extends GcTestCase {
70  
71  	@Test
72  	public void testBitmapSpansNoMerges() throws Exception {
73  		testBitmapSpansNoMerges(false);
74  	}
75  
76  	@Test
77  	public void testBitmapSpansNoMergesWithTags() throws Exception {
78  		testBitmapSpansNoMerges(true);
79  	}
80  
81  	private void testBitmapSpansNoMerges(boolean withTags) throws Exception {
82  		/*
83  		 * Commit counts -> expected bitmap counts for history without merges.
84  		 * The top 100 contiguous commits should always have bitmaps, and the
85  		 * "recent" bitmaps beyond that are spaced out every 100-200 commits.
86  		 * (Starting at 100, the next 100 commits are searched for a merge
87  		 * commit. Since one is not found, the spacing between commits is 200.
88  		 */
89  		int[][] bitmapCounts = { //
90  				{ 1, 1 }, { 50, 50 }, { 99, 99 }, { 100, 100 }, { 101, 100 },
91  				{ 200, 100 }, { 201, 100 }, { 299, 100 }, { 300, 101 },
92  				{ 301, 101 }, { 401, 101 }, { 499, 101 }, { 500, 102 }, };
93  		int currentCommits = 0;
94  		BranchBuilder bb = tr.branch("refs/heads/main");
95  
96  		for (int[] counts : bitmapCounts) {
97  			int nextCommitCount = counts[0];
98  			int expectedBitmapCount = counts[1];
99  			assertTrue(nextCommitCount > currentCommits); // programming error
100 			for (int i = currentCommits; i < nextCommitCount; i++) {
101 				String str = "A" + i;
102 				RevCommit rc = bb.commit().message(str).add(str, str).create();
103 				if (withTags) {
104 					tr.lightweightTag(str, rc);
105 				}
106 			}
107 			currentCommits = nextCommitCount;
108 
109 			gc.setPackExpireAgeMillis(0); // immediately delete old packs
110 			gc.setExpireAgeMillis(0);
111 			gc.gc();
112 			assertEquals(currentCommits * 3, // commit/tree/object
113 					gc.getStatistics().numberOfPackedObjects);
114 			assertEquals(currentCommits + " commits: ", expectedBitmapCount,
115 					gc.getStatistics().numberOfBitmaps);
116 		}
117 	}
118 
119 	@Test
120 	public void testBitmapSpansWithMerges() throws Exception {
121 		/*
122 		 * Commits that are merged. Since 55 is in the oldest history it is
123 		 * never considered. Searching goes from oldest to newest so 115 is the
124 		 * first merge commit found. After that the range 116-216 is ignored so
125 		 * 175 is never considered.
126 		 */
127 		List<Integer> merges = Arrays.asList(Integer.valueOf(55),
128 				Integer.valueOf(115), Integer.valueOf(175),
129 				Integer.valueOf(235));
130 		/*
131 		 * Commit counts -> expected bitmap counts for history with merges. The
132 		 * top 100 contiguous commits should always have bitmaps, and the
133 		 * "recent" bitmaps beyond that are spaced out every 100-200 commits.
134 		 * Merges in the < 100 range have no effect and merges in the > 100
135 		 * range will only be considered for commit counts > 200.
136 		 */
137 		int[][] bitmapCounts = { //
138 				{ 1, 1 }, { 55, 55 }, { 56, 57 }, // +1 bitmap from branch A55
139 				{ 99, 100 }, // still +1 branch @55
140 				{ 100, 100 }, // 101 commits, only 100 newest
141 				{ 116, 100 }, // @55 still in 100 newest bitmaps
142 				{ 176, 101 }, // @55 branch tip is not in 100 newest
143 				{ 213, 101 }, // 216 commits, @115&@175 in 100 newest
144 				{ 214, 102 }, // @55 branch tip, merge @115, @177 in newest
145 				{ 236, 102 }, // all 4 merge points in history
146 				{ 273, 102 }, // 277 commits, @175&@235 in newest
147 				{ 274, 103 }, // @55, @115, merge @175, @235 in newest
148 				{ 334, 103 }, // @55,@115,@175, @235 in newest
149 				{ 335, 104 }, // @55,@115,@175, merge @235
150 				{ 435, 104 }, // @55,@115,@175,@235 tips
151 				{ 436, 104 }, // force @236
152 		};
153 
154 		int currentCommits = 0;
155 		BranchBuilder bb = tr.branch("refs/heads/main");
156 
157 		for (int[] counts : bitmapCounts) {
158 			int nextCommitCount = counts[0];
159 			int expectedBitmapCount = counts[1];
160 			assertTrue(nextCommitCount > currentCommits); // programming error
161 			for (int i = currentCommits; i < nextCommitCount; i++) {
162 				String str = "A" + i;
163 				if (!merges.contains(Integer.valueOf(i))) {
164 					bb.commit().message(str).add(str, str).create();
165 				} else {
166 					BranchBuilder bbN = tr.branch("refs/heads/A" + i);
167 					bb.commit().message(str).add(str, str)
168 							.parent(bbN.commit().create()).create();
169 				}
170 			}
171 			currentCommits = nextCommitCount;
172 
173 			gc.setPackExpireAgeMillis(0); // immediately delete old packs
174 			gc.setExpireAgeMillis(0);
175 			gc.gc();
176 			assertEquals(currentCommits + " commits: ", expectedBitmapCount,
177 					gc.getStatistics().numberOfBitmaps);
178 		}
179 	}
180 
181 	@Test
182 	public void testBitmapsForExcessiveBranches() throws Exception {
183 		int oneDayInSeconds = 60 * 60 * 24;
184 
185 		// All of branch A is committed on day1
186 		BranchBuilder bbA = tr.branch("refs/heads/A");
187 		for (int i = 0; i < 1001; i++) {
188 			String msg = "A" + i;
189 			bbA.commit().message(msg).add(msg, msg).create();
190 		}
191 		// All of in branch B is committed on day91
192 		tr.tick(oneDayInSeconds * 90);
193 		BranchBuilder bbB = tr.branch("refs/heads/B");
194 		for (int i = 0; i < 1001; i++) {
195 			String msg = "B" + i;
196 			bbB.commit().message(msg).add(msg, msg).create();
197 		}
198 		// Create 100 other branches with a single commit
199 		for (int i = 0; i < 100; i++) {
200 			BranchBuilder bb = tr.branch("refs/heads/N" + i);
201 			String msg = "singlecommit" + i;
202 			bb.commit().message(msg).add(msg, msg).create();
203 		}
204 		// now is day92
205 		tr.tick(oneDayInSeconds);
206 
207 		// Since there are no merges, commits in recent history are selected
208 		// every 200 commits.
209 		final int commitsForSparseBranch = 1 + (1001 / 200);
210 		final int commitsForFullBranch = 100 + (901 / 200);
211 		final int commitsForShallowBranches = 100;
212 
213 		// Excessive branch history pruning, one old branch.
214 		gc.setPackExpireAgeMillis(0); // immediately delete old packs
215 		gc.setExpireAgeMillis(0);
216 		gc.gc();
217 		assertEquals(
218 				commitsForSparseBranch + commitsForFullBranch
219 						+ commitsForShallowBranches,
220 				gc.getStatistics().numberOfBitmaps);
221 	}
222 
223 	@Test
224 	public void testSelectionOrderingWithChains() throws Exception {
225 		/*-
226 		 * Create a history like this, where 'N' is the number of seconds from
227 		 * the first commit in the branch:
228 		 *
229 		 *      ---o---o---o        commits b3,b5,b7
230 		 *     /            \
231 		 * o--o--o---o---o---o--o   commits m0,m1,m2,m4,m6,m8,m9
232 		 */
233 		BranchBuilder bb = tr.branch("refs/heads/main");
234 		RevCommit m0 = addCommit(bb, "m0");
235 		RevCommit m1 = addCommit(bb, "m1", m0);
236 		RevCommit m2 = addCommit(bb, "m2", m1);
237 		RevCommit b3 = addCommit(bb, "b3", m1);
238 		RevCommit m4 = addCommit(bb, "m4", m2);
239 		RevCommit b5 = addCommit(bb, "m5", b3);
240 		RevCommit m6 = addCommit(bb, "m6", m4);
241 		RevCommit b7 = addCommit(bb, "m7", b5);
242 		RevCommit m8 = addCommit(bb, "m8", m6, b7);
243 		RevCommit m9 = addCommit(bb, "m9", m8);
244 
245 		List<RevCommit> commits = Arrays.asList(m0, m1, m2, b3, m4, b5, m6, b7,
246 				m8, m9);
247 		PackWriterBitmapPreparer preparer = newPreparer(
248 				Collections.singleton(m9), commits, new PackConfig());
249 		List<BitmapCommit> selection = new ArrayList<>(
250 				preparer.selectCommits(commits.size(), PackWriter.NONE));
251 
252 		// Verify that the output is ordered by the separate "chains"
253 		String[] expected = { m0.name(), m1.name(), m2.name(), m4.name(),
254 				m6.name(), m8.name(), m9.name(), b3.name(), b5.name(),
255 				b7.name() };
256 		assertEquals(expected.length, selection.size());
257 		for (int i = 0; i < expected.length; i++) {
258 			assertEquals("Entry " + i, expected[i], selection.get(i).getName());
259 		}
260 	}
261 
262 	private RevCommit addCommit(BranchBuilder bb, String msg,
263 			RevCommit... parents) throws Exception {
264 		CommitBuilder commit = bb.commit().message(msg).add(msg, msg).tick(1)
265 				.noParents();
266 		for (RevCommit parent : parents) {
267 			commit.parent(parent);
268 		}
269 		return commit.create();
270 	}
271 
272 	@Test
273 	public void testDistributionOnMultipleBranches() throws Exception {
274 		BranchBuilder[] branches = { tr.branch("refs/heads/main"),
275 				tr.branch("refs/heads/a"), tr.branch("refs/heads/b"),
276 				tr.branch("refs/heads/c") };
277 		RevCommit[] tips = new RevCommit[branches.length];
278 		List<RevCommit> commits = createHistory(branches, tips);
279 		PackConfig config = new PackConfig();
280 		config.setBitmapContiguousCommitCount(1);
281 		config.setBitmapRecentCommitSpan(5);
282 		config.setBitmapDistantCommitSpan(20);
283 		config.setBitmapRecentCommitCount(100);
284 		Set<RevCommit> wants = new HashSet<>(Arrays.asList(tips));
285 		PackWriterBitmapPreparer preparer = newPreparer(wants, commits, config);
286 		List<BitmapCommit> selection = new ArrayList<>(
287 				preparer.selectCommits(commits.size(), PackWriter.NONE));
288 		Set<ObjectId> selected = new HashSet<>();
289 		for (BitmapCommit c : selection) {
290 			selected.add(c.toObjectId());
291 		}
292 
293 		// Verify that each branch has uniform bitmap selection coverage
294 		for (RevCommit c : wants) {
295 			assertTrue(selected.contains(c.toObjectId()));
296 			int count = 1;
297 			int selectedCount = 1;
298 			RevCommit parent = c;
299 			while (parent.getParentCount() != 0) {
300 				parent = parent.getParent(0);
301 				count++;
302 				if (selected.contains(parent.toObjectId())) {
303 					selectedCount++;
304 				}
305 			}
306 			// The selection algorithm prefers merges and will look in the
307 			// current range plus the recent commit span before selecting a
308 			// commit. Since this history has no merges, we expect the recent
309 			// span should have 100/10=10 and distant commit spans should have
310 			// 100/25=4 per 100 commit range.
311 			int expectedCount = 10 + (count - 100 - 24) / 25;
312 			assertTrue(expectedCount <= selectedCount);
313 		}
314 	}
315 
316 	private List<RevCommit> createHistory(BranchBuilder[] branches,
317 			RevCommit[] tips) throws Exception {
318 		/*-
319 		 * Create a history like this, where branches a, b and c branch off of the main branch
320 		 * at commits 100, 200 and 300, and where commit times move forward alternating between
321 		 * branches.
322 		 *
323 		 * o...o...o...o...o      commits root,m0,m1,...,m399
324 		 *      \   \   \
325 		 *       \   \   o...     commits branch_c,c300,c301,...,c399
326 		 *        \   \
327 		 *         \   o...o...   commits branch_b,b200,b201,...,b399
328 		 *          \
329 		 *           o...o...o... commits branch_a,b100,b101,...,a399
330 		 */
331 		List<RevCommit> commits = new ArrayList<>();
332 		String[] prefixes = { "m", "a", "b", "c" };
333 		int branchCount = branches.length;
334 		tips[0] = addCommit(commits, branches[0], "root");
335 		int counter = 0;
336 
337 		for (int b = 0; b < branchCount; b++) {
338 			for (int i = 0; i < 100; i++, counter++) {
339 				for (int j = 0; j <= b; j++) {
340 					tips[j] = addCommit(commits, branches[j],
341 							prefixes[j] + counter);
342 				}
343 			}
344 			// Create a new branch from current value of the master branch
345 			if (b < branchCount - 1) {
346 				tips[b + 1] = addCommit(branches[b + 1],
347 						"branch_" + prefixes[b + 1], tips[0]);
348 			}
349 		}
350 		return commits;
351 	}
352 
353 	private RevCommit addCommit(List<RevCommit> commits, BranchBuilder bb,
354 			String msg, RevCommit... parents) throws Exception {
355 		CommitBuilder commit = bb.commit().message(msg).add(msg, msg).tick(1);
356 		if (parents.length > 0) {
357 			commit.noParents();
358 			for (RevCommit parent : parents) {
359 				commit.parent(parent);
360 			}
361 		}
362 		RevCommit c = commit.create();
363 		commits.add(c);
364 		return c;
365 	}
366 
367 	private PackWriterBitmapPreparer newPreparer(Set<RevCommit> wants,
368 			List<RevCommit> commits, PackConfig config) throws IOException {
369 		List<ObjectToPack> objects = new ArrayList<>(commits.size());
370 		for (RevCommit commit : commits) {
371 			objects.add(new ObjectToPack(commit, Constants.OBJ_COMMIT));
372 		}
373 		PackBitmapIndexBuilder builder = new PackBitmapIndexBuilder(objects);
374 		return new PackWriterBitmapPreparer(
375 				tr.getRepository().newObjectReader(), builder,
376 				NullProgressMonitor.INSTANCE, wants, config);
377 	}
378 }