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