View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.internal.storage.file;
46  
47  import static org.junit.Assert.assertArrayEquals;
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertFalse;
50  import static org.junit.Assert.assertNotNull;
51  import static org.junit.Assert.assertNotSame;
52  import static org.junit.Assert.fail;
53  
54  import java.io.BufferedOutputStream;
55  import java.io.File;
56  import java.io.FileOutputStream;
57  import java.io.IOException;
58  import java.io.OutputStream;
59  
60  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
61  import org.eclipse.jgit.errors.MissingObjectException;
62  import org.eclipse.jgit.internal.storage.pack.PackWriter;
63  import org.eclipse.jgit.junit.RepositoryTestCase;
64  import org.eclipse.jgit.lib.AnyObjectId;
65  import org.eclipse.jgit.lib.Constants;
66  import org.eclipse.jgit.lib.NullProgressMonitor;
67  import org.eclipse.jgit.lib.ObjectId;
68  import org.eclipse.jgit.lib.ObjectInserter;
69  import org.eclipse.jgit.lib.ObjectLoader;
70  import org.eclipse.jgit.lib.Repository;
71  import org.eclipse.jgit.revwalk.RevObject;
72  import org.eclipse.jgit.revwalk.RevWalk;
73  import org.eclipse.jgit.storage.file.WindowCacheConfig;
74  import org.eclipse.jgit.util.FileUtils;
75  import org.junit.After;
76  import org.junit.Before;
77  import org.junit.Test;
78  
79  public class ConcurrentRepackTest extends RepositoryTestCase {
80  	@Override
81  	@Before
82  	public void setUp() throws Exception {
83  		WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
84  		windowCacheConfig.setPackedGitOpenFiles(1);
85  		windowCacheConfig.install();
86  		super.setUp();
87  	}
88  
89  	@Override
90  	@After
91  	public void tearDown() throws Exception {
92  		super.tearDown();
93  		new WindowCacheConfig().install();
94  	}
95  
96  	@Test
97  	public void testObjectInNewPack() throws IncorrectObjectTypeException,
98  			IOException {
99  		// Create a new object in a new pack, and test that it is present.
100 		//
101 		final Repository eden = createBareRepository();
102 		final RevObject o1 = writeBlob(eden, "o1");
103 		pack(eden, o1);
104 		assertEquals(o1.name(), parse(o1).name());
105 	}
106 
107 	@Test
108 	public void testObjectMovedToNewPack1()
109 			throws IncorrectObjectTypeException, IOException {
110 		// Create an object and pack it. Then remove that pack and put the
111 		// object into a different pack file, with some other object. We
112 		// still should be able to access the objects.
113 		//
114 		final Repository eden = createBareRepository();
115 		final RevObject o1 = writeBlob(eden, "o1");
116 		final File[] out1 = pack(eden, o1);
117 		assertEquals(o1.name(), parse(o1).name());
118 
119 		final RevObject o2 = writeBlob(eden, "o2");
120 		pack(eden, o2, o1);
121 
122 		// Force close, and then delete, the old pack.
123 		//
124 		whackCache();
125 		delete(out1);
126 
127 		// Now here is the interesting thing. Will git figure the new
128 		// object exists in the new pack, and not the old one.
129 		//
130 		assertEquals(o2.name(), parse(o2).name());
131 		assertEquals(o1.name(), parse(o1).name());
132 	}
133 
134 	@Test
135 	public void testObjectMovedWithinPack()
136 			throws IncorrectObjectTypeException, IOException {
137 		// Create an object and pack it.
138 		//
139 		final Repository eden = createBareRepository();
140 		final RevObject o1 = writeBlob(eden, "o1");
141 		final File[] out1 = pack(eden, o1);
142 		assertEquals(o1.name(), parse(o1).name());
143 
144 		// Force close the old pack.
145 		//
146 		whackCache();
147 
148 		// Now overwrite the old pack in place. This method of creating a
149 		// different pack under the same file name is partially broken. We
150 		// should also have a different file name because the list of objects
151 		// within the pack has been modified.
152 		//
153 		final RevObject o2 = writeBlob(eden, "o2");
154 		try (PackWriter pw = new PackWriter(eden)) {
155 			pw.addObject(o2);
156 			pw.addObject(o1);
157 			write(out1, pw);
158 		}
159 
160 		// Try the old name, then the new name. The old name should cause the
161 		// pack to reload when it opens and the index and pack mismatch.
162 		//
163 		assertEquals(o1.name(), parse(o1).name());
164 		assertEquals(o2.name(), parse(o2).name());
165 	}
166 
167 	@Test
168 	public void testObjectMovedToNewPack2()
169 			throws IncorrectObjectTypeException, IOException {
170 		// Create an object and pack it. Then remove that pack and put the
171 		// object into a different pack file, with some other object. We
172 		// still should be able to access the objects.
173 		//
174 		final Repository eden = createBareRepository();
175 		final RevObject o1 = writeBlob(eden, "o1");
176 		final File[] out1 = pack(eden, o1);
177 		assertEquals(o1.name(), parse(o1).name());
178 
179 		final ObjectLoader load1 = db.open(o1, Constants.OBJ_BLOB);
180 		assertNotNull(load1);
181 
182 		final RevObject o2 = writeBlob(eden, "o2");
183 		pack(eden, o2, o1);
184 
185 		// Force close, and then delete, the old pack.
186 		//
187 		whackCache();
188 		delete(out1);
189 
190 		// Now here is the interesting thing... can the loader we made
191 		// earlier still resolve the object, even though its underlying
192 		// pack is gone, but the object still exists.
193 		//
194 		final ObjectLoader load2 = db.open(o1, Constants.OBJ_BLOB);
195 		assertNotNull(load2);
196 		assertNotSame(load1, load2);
197 
198 		final byte[] data2 = load2.getCachedBytes();
199 		final byte[] data1 = load1.getCachedBytes();
200 		assertNotNull(data2);
201 		assertNotNull(data1);
202 		assertNotSame(data1, data2); // cache should be per-pack, not per object
203 		assertArrayEquals(data1, data2);
204 		assertEquals(load2.getType(), load1.getType());
205 	}
206 
207 	private static void whackCache() {
208 		final WindowCacheConfig config = new WindowCacheConfig();
209 		config.setPackedGitOpenFiles(1);
210 		config.install();
211 	}
212 
213 	private RevObject parse(final AnyObjectId id)
214 			throws MissingObjectException, IOException {
215 		try (RevWalk rw = new RevWalk(db)) {
216 			return rw.parseAny(id);
217 		}
218 	}
219 
220 	private File[] pack(final Repository src, final RevObject... list)
221 			throws IOException {
222 		try (PackWriter pw = new PackWriter(src)) {
223 			for (final RevObject o : list) {
224 				pw.addObject(o);
225 			}
226 
227 			final ObjectId name = pw.computeName();
228 			final File packFile = fullPackFileName(name, ".pack");
229 			final File idxFile = fullPackFileName(name, ".idx");
230 			final File[] files = new File[] { packFile, idxFile };
231 			write(files, pw);
232 			return files;
233 		}
234 	}
235 
236 	private static void write(final File[] files, final PackWriter pw)
237 			throws IOException {
238 		final long begin = files[0].getParentFile().lastModified();
239 		NullProgressMonitor m = NullProgressMonitor.INSTANCE;
240 
241 		try (OutputStream out = new BufferedOutputStream(
242 				new FileOutputStream(files[0]))) {
243 			pw.writePack(m, m, out);
244 		}
245 
246 		try (OutputStream out = new BufferedOutputStream(
247 				new FileOutputStream(files[1]))) {
248 			pw.writeIndex(out);
249 		}
250 
251 		touch(begin, files[0].getParentFile());
252 	}
253 
254 	private static void delete(final File[] list) throws IOException {
255 		final long begin = list[0].getParentFile().lastModified();
256 		for (final File f : list) {
257 			FileUtils.delete(f);
258 			assertFalse(f + " was removed", f.exists());
259 		}
260 		touch(begin, list[0].getParentFile());
261 	}
262 
263 	private static void touch(final long begin, final File dir) {
264 		while (begin >= dir.lastModified()) {
265 			try {
266 				Thread.sleep(25);
267 			} catch (InterruptedException ie) {
268 				//
269 			}
270 			dir.setLastModified(System.currentTimeMillis());
271 		}
272 	}
273 
274 	private File fullPackFileName(final ObjectId name, final String suffix) {
275 		final File packdir = new File(db.getObjectDatabase().getDirectory(), "pack");
276 		return new File(packdir, "pack-" + name.name() + suffix);
277 	}
278 
279 	private RevObject writeBlob(final Repository repo, final String data)
280 			throws IOException {
281 		final byte[] bytes = Constants.encode(data);
282 		final ObjectId id;
283 		try (ObjectInserter inserter = repo.newObjectInserter()) {
284 			id = inserter.insert(Constants.OBJ_BLOB, bytes);
285 			inserter.flush();
286 		}
287 		try {
288 			parse(id);
289 			fail("Object " + id.name() + " should not exist in test repository");
290 		} catch (MissingObjectException e) {
291 			// Ok
292 		}
293 		try (RevWalk revWalk = new RevWalk(repo)) {
294 			return revWalk.lookupBlob(id);
295 		}
296 	}
297 }