You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConcurrentRepackTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. package org.eclipse.jgit.internal.storage.file;
  45. import static org.junit.Assert.assertArrayEquals;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertNotNull;
  49. import static org.junit.Assert.assertNotSame;
  50. import static org.junit.Assert.fail;
  51. import java.io.File;
  52. import java.io.FileOutputStream;
  53. import java.io.IOException;
  54. import java.io.OutputStream;
  55. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  56. import org.eclipse.jgit.errors.MissingObjectException;
  57. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.lib.AnyObjectId;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.NullProgressMonitor;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ObjectInserter;
  64. import org.eclipse.jgit.lib.ObjectLoader;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.eclipse.jgit.revwalk.RevObject;
  67. import org.eclipse.jgit.revwalk.RevWalk;
  68. import org.eclipse.jgit.storage.file.WindowCacheConfig;
  69. import org.eclipse.jgit.util.FileUtils;
  70. import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
  71. import org.junit.After;
  72. import org.junit.Before;
  73. import org.junit.Test;
  74. public class ConcurrentRepackTest extends RepositoryTestCase {
  75. @Before
  76. public void setUp() throws Exception {
  77. WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
  78. windowCacheConfig.setPackedGitOpenFiles(1);
  79. WindowCache.reconfigure(windowCacheConfig);
  80. super.setUp();
  81. }
  82. @After
  83. public void tearDown() throws Exception {
  84. super.tearDown();
  85. WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
  86. WindowCache.reconfigure(windowCacheConfig);
  87. }
  88. @Test
  89. public void testObjectInNewPack() throws IncorrectObjectTypeException,
  90. IOException {
  91. // Create a new object in a new pack, and test that it is present.
  92. //
  93. final Repository eden = createBareRepository();
  94. final RevObject o1 = writeBlob(eden, "o1");
  95. pack(eden, o1);
  96. assertEquals(o1.name(), parse(o1).name());
  97. }
  98. @Test
  99. public void testObjectMovedToNewPack1()
  100. throws IncorrectObjectTypeException, IOException {
  101. // Create an object and pack it. Then remove that pack and put the
  102. // object into a different pack file, with some other object. We
  103. // still should be able to access the objects.
  104. //
  105. final Repository eden = createBareRepository();
  106. final RevObject o1 = writeBlob(eden, "o1");
  107. final File[] out1 = pack(eden, o1);
  108. assertEquals(o1.name(), parse(o1).name());
  109. final RevObject o2 = writeBlob(eden, "o2");
  110. pack(eden, o2, o1);
  111. // Force close, and then delete, the old pack.
  112. //
  113. whackCache();
  114. delete(out1);
  115. // Now here is the interesting thing. Will git figure the new
  116. // object exists in the new pack, and not the old one.
  117. //
  118. assertEquals(o2.name(), parse(o2).name());
  119. assertEquals(o1.name(), parse(o1).name());
  120. }
  121. @Test
  122. public void testObjectMovedWithinPack()
  123. throws IncorrectObjectTypeException, IOException {
  124. // Create an object and pack it.
  125. //
  126. final Repository eden = createBareRepository();
  127. final RevObject o1 = writeBlob(eden, "o1");
  128. final File[] out1 = pack(eden, o1);
  129. assertEquals(o1.name(), parse(o1).name());
  130. // Force close the old pack.
  131. //
  132. whackCache();
  133. // Now overwrite the old pack in place. This method of creating a
  134. // different pack under the same file name is partially broken. We
  135. // should also have a different file name because the list of objects
  136. // within the pack has been modified.
  137. //
  138. final RevObject o2 = writeBlob(eden, "o2");
  139. try (PackWriter pw = new PackWriter(eden)) {
  140. pw.addObject(o2);
  141. pw.addObject(o1);
  142. write(out1, pw);
  143. }
  144. // Try the old name, then the new name. The old name should cause the
  145. // pack to reload when it opens and the index and pack mismatch.
  146. //
  147. assertEquals(o1.name(), parse(o1).name());
  148. assertEquals(o2.name(), parse(o2).name());
  149. }
  150. @Test
  151. public void testObjectMovedToNewPack2()
  152. throws IncorrectObjectTypeException, IOException {
  153. // Create an object and pack it. Then remove that pack and put the
  154. // object into a different pack file, with some other object. We
  155. // still should be able to access the objects.
  156. //
  157. final Repository eden = createBareRepository();
  158. final RevObject o1 = writeBlob(eden, "o1");
  159. final File[] out1 = pack(eden, o1);
  160. assertEquals(o1.name(), parse(o1).name());
  161. final ObjectLoader load1 = db.open(o1, Constants.OBJ_BLOB);
  162. assertNotNull(load1);
  163. final RevObject o2 = writeBlob(eden, "o2");
  164. pack(eden, o2, o1);
  165. // Force close, and then delete, the old pack.
  166. //
  167. whackCache();
  168. delete(out1);
  169. // Now here is the interesting thing... can the loader we made
  170. // earlier still resolve the object, even though its underlying
  171. // pack is gone, but the object still exists.
  172. //
  173. final ObjectLoader load2 = db.open(o1, Constants.OBJ_BLOB);
  174. assertNotNull(load2);
  175. assertNotSame(load1, load2);
  176. final byte[] data2 = load2.getCachedBytes();
  177. final byte[] data1 = load1.getCachedBytes();
  178. assertNotNull(data2);
  179. assertNotNull(data1);
  180. assertNotSame(data1, data2); // cache should be per-pack, not per object
  181. assertArrayEquals(data1, data2);
  182. assertEquals(load2.getType(), load1.getType());
  183. }
  184. private static void whackCache() {
  185. final WindowCacheConfig config = new WindowCacheConfig();
  186. config.setPackedGitOpenFiles(1);
  187. WindowCache.reconfigure(config);
  188. }
  189. private RevObject parse(final AnyObjectId id)
  190. throws MissingObjectException, IOException {
  191. return new RevWalk(db).parseAny(id);
  192. }
  193. private File[] pack(final Repository src, final RevObject... list)
  194. throws IOException {
  195. try (PackWriter pw = new PackWriter(src)) {
  196. for (final RevObject o : list) {
  197. pw.addObject(o);
  198. }
  199. final ObjectId name = pw.computeName();
  200. final File packFile = fullPackFileName(name, ".pack");
  201. final File idxFile = fullPackFileName(name, ".idx");
  202. final File[] files = new File[] { packFile, idxFile };
  203. write(files, pw);
  204. return files;
  205. }
  206. }
  207. private static void write(final File[] files, final PackWriter pw)
  208. throws IOException {
  209. final long begin = files[0].getParentFile().lastModified();
  210. NullProgressMonitor m = NullProgressMonitor.INSTANCE;
  211. OutputStream out;
  212. out = new SafeBufferedOutputStream(new FileOutputStream(files[0]));
  213. try {
  214. pw.writePack(m, m, out);
  215. } finally {
  216. out.close();
  217. }
  218. out = new SafeBufferedOutputStream(new FileOutputStream(files[1]));
  219. try {
  220. pw.writeIndex(out);
  221. } finally {
  222. out.close();
  223. }
  224. touch(begin, files[0].getParentFile());
  225. }
  226. private static void delete(final File[] list) throws IOException {
  227. final long begin = list[0].getParentFile().lastModified();
  228. for (final File f : list) {
  229. FileUtils.delete(f);
  230. assertFalse(f + " was removed", f.exists());
  231. }
  232. touch(begin, list[0].getParentFile());
  233. }
  234. private static void touch(final long begin, final File dir) {
  235. while (begin >= dir.lastModified()) {
  236. try {
  237. Thread.sleep(25);
  238. } catch (InterruptedException ie) {
  239. //
  240. }
  241. dir.setLastModified(System.currentTimeMillis());
  242. }
  243. }
  244. private File fullPackFileName(final ObjectId name, final String suffix) {
  245. final File packdir = new File(db.getObjectDatabase().getDirectory(), "pack");
  246. return new File(packdir, "pack-" + name.name() + suffix);
  247. }
  248. private RevObject writeBlob(final Repository repo, final String data)
  249. throws IOException {
  250. final RevWalk revWalk = new RevWalk(repo);
  251. final byte[] bytes = Constants.encode(data);
  252. final ObjectId id;
  253. try (ObjectInserter inserter = repo.newObjectInserter()) {
  254. id = inserter.insert(Constants.OBJ_BLOB, bytes);
  255. inserter.flush();
  256. }
  257. try {
  258. parse(id);
  259. fail("Object " + id.name() + " should not exist in test repository");
  260. } catch (MissingObjectException e) {
  261. // Ok
  262. }
  263. return revWalk.lookupBlob(id);
  264. }
  265. }