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

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