Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ConcurrentRepackTest.java 9.4KB

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