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