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

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