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