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 8.6KB

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