View Javadoc
1   /*
2    * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com>
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.file;
45  
46  import static org.junit.Assert.assertEquals;
47  
48  import java.io.File;
49  import java.io.IOException;
50  import java.util.Collection;
51  import java.util.Date;
52  import java.util.Iterator;
53  
54  import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
55  import org.eclipse.jgit.revwalk.RevCommit;
56  import org.eclipse.jgit.storage.pack.PackConfig;
57  import org.junit.Test;
58  import org.junit.experimental.theories.DataPoints;
59  import org.junit.experimental.theories.Theories;
60  import org.junit.experimental.theories.Theory;
61  import org.junit.runner.RunWith;
62  
63  @RunWith(Theories.class)
64  public class GcBasicPackingTest extends GcTestCase {
65  	@DataPoints
66  	public static boolean[] aggressiveValues = { true, false };
67  
68  	@Theory
69  	public void repackEmptyRepo_noPackCreated(boolean aggressive)
70  			throws IOException {
71  		configureGc(gc, aggressive);
72  		gc.repack();
73  		assertEquals(0, repo.getObjectDatabase().getPacks().size());
74  	}
75  
76  	@Theory
77  	public void testPackRepoWithNoRefs(boolean aggressive) throws Exception {
78  		tr.commit().add("A", "A").add("B", "B").create();
79  		stats = gc.getStatistics();
80  		assertEquals(4, stats.numberOfLooseObjects);
81  		assertEquals(0, stats.numberOfPackedObjects);
82  		configureGc(gc, aggressive);
83  		gc.gc();
84  		stats = gc.getStatistics();
85  		assertEquals(4, stats.numberOfLooseObjects);
86  		assertEquals(0, stats.numberOfPackedObjects);
87  		assertEquals(0, stats.numberOfPackFiles);
88  	}
89  
90  	@Theory
91  	public void testPack2Commits(boolean aggressive) throws Exception {
92  		BranchBuilder bb = tr.branch("refs/heads/master");
93  		bb.commit().add("A", "A").add("B", "B").create();
94  		bb.commit().add("A", "A2").add("B", "B2").create();
95  
96  		stats = gc.getStatistics();
97  		assertEquals(8, stats.numberOfLooseObjects);
98  		assertEquals(0, stats.numberOfPackedObjects);
99  		configureGc(gc, aggressive);
100 		gc.gc();
101 		stats = gc.getStatistics();
102 		assertEquals(0, stats.numberOfLooseObjects);
103 		assertEquals(8, stats.numberOfPackedObjects);
104 		assertEquals(1, stats.numberOfPackFiles);
105 	}
106 
107 	@Theory
108 	public void testPackAllObjectsInOnePack(boolean aggressive)
109 			throws Exception {
110 		tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
111 				.create();
112 		stats = gc.getStatistics();
113 		assertEquals(4, stats.numberOfLooseObjects);
114 		assertEquals(0, stats.numberOfPackedObjects);
115 		configureGc(gc, aggressive);
116 		gc.gc();
117 		stats = gc.getStatistics();
118 		assertEquals(0, stats.numberOfLooseObjects);
119 		assertEquals(4, stats.numberOfPackedObjects);
120 		assertEquals(1, stats.numberOfPackFiles);
121 
122 		// Do the gc again and check that it hasn't changed anything
123 		gc.gc();
124 		stats = gc.getStatistics();
125 		assertEquals(0, stats.numberOfLooseObjects);
126 		assertEquals(4, stats.numberOfPackedObjects);
127 		assertEquals(1, stats.numberOfPackFiles);
128 	}
129 
130 	@Theory
131 	public void testPackCommitsAndLooseOne(boolean aggressive) throws Exception {
132 		BranchBuilder bb = tr.branch("refs/heads/master");
133 		RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
134 		bb.commit().add("A", "A2").add("B", "B2").create();
135 		tr.update("refs/heads/master", first);
136 
137 		stats = gc.getStatistics();
138 		assertEquals(8, stats.numberOfLooseObjects);
139 		assertEquals(0, stats.numberOfPackedObjects);
140 		configureGc(gc, aggressive);
141 		gc.gc();
142 		stats = gc.getStatistics();
143 		assertEquals(0, stats.numberOfLooseObjects);
144 		assertEquals(8, stats.numberOfPackedObjects);
145 		assertEquals(2, stats.numberOfPackFiles);
146 	}
147 
148 	@Theory
149 	public void testNotPackTwice(boolean aggressive) throws Exception {
150 		BranchBuilder bb = tr.branch("refs/heads/master");
151 		RevCommit first = bb.commit().message("M").add("M", "M").create();
152 		bb.commit().message("B").add("B", "Q").create();
153 		bb.commit().message("A").add("A", "A").create();
154 		RevCommit second = tr.commit().parent(first).message("R").add("R", "Q")
155 				.create();
156 		tr.update("refs/tags/t1", second);
157 
158 		Collection<PackFile> oldPacks = tr.getRepository().getObjectDatabase()
159 				.getPacks();
160 		assertEquals(0, oldPacks.size());
161 		stats = gc.getStatistics();
162 		assertEquals(11, stats.numberOfLooseObjects);
163 		assertEquals(0, stats.numberOfPackedObjects);
164 
165 		gc.setExpireAgeMillis(0);
166 		fsTick();
167 		configureGc(gc, aggressive);
168 		gc.gc();
169 		stats = gc.getStatistics();
170 		assertEquals(0, stats.numberOfLooseObjects);
171 
172 		Iterator<PackFile> pIt = repo.getObjectDatabase().getPacks().iterator();
173 		long c = pIt.next().getObjectCount();
174 		if (c == 9)
175 			assertEquals(2, pIt.next().getObjectCount());
176 		else {
177 			assertEquals(2, c);
178 			assertEquals(9, pIt.next().getObjectCount());
179 		}
180 	}
181 
182 	@Test
183 	public void testDonePruneTooYoungPacks() throws Exception {
184 		BranchBuilder bb = tr.branch("refs/heads/master");
185 		bb.commit().message("M").add("M", "M").create();
186 
187 		gc.setExpireAgeMillis(0);
188 		gc.gc();
189 		stats = gc.getStatistics();
190 		assertEquals(0, stats.numberOfLooseObjects);
191 		assertEquals(3, stats.numberOfPackedObjects);
192 		assertEquals(1, stats.numberOfPackFiles);
193 		File oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
194 				.iterator().next().getPackFile();
195 
196 		fsTick();
197 		bb.commit().message("B").add("B", "Q").create();
198 
199 		// The old packfile is too young to be deleted. We should end up with
200 		// two pack files
201 		gc.setExpire(new Date(oldPackfile.lastModified() - 1));
202 		gc.gc();
203 		stats = gc.getStatistics();
204 		assertEquals(0, stats.numberOfLooseObjects);
205 		// if objects exist in multiple packFiles then they are counted multiple
206 		// times
207 		assertEquals(9, stats.numberOfPackedObjects);
208 		assertEquals(2, stats.numberOfPackFiles);
209 
210 		// repack again but now without a grace period for packfiles. We should
211 		// end up with one packfile
212 		gc.setExpireAgeMillis(0);
213 		gc.gc();
214 		stats = gc.getStatistics();
215 		assertEquals(0, stats.numberOfLooseObjects);
216 		// if objects exist in multiple packFiles then they are counted multiple
217 		// times
218 		assertEquals(6, stats.numberOfPackedObjects);
219 		assertEquals(1, stats.numberOfPackFiles);
220 
221 	}
222 
223 	private void configureGc(GC myGc, boolean aggressive) {
224 		PackConfig pconfig = new PackConfig(repo);
225 		if (aggressive) {
226 			pconfig.setDeltaSearchWindowSize(250);
227 			pconfig.setMaxDeltaDepth(250);
228 			pconfig.setReuseObjects(false);
229 		} else
230 			pconfig = new PackConfig(repo);
231 		myGc.setPackConfig(pconfig);
232 	}
233 }