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.

ObjectDirectoryTest.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2012, Roberto Tyley <roberto.tyley@gmail.com>
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.internal.storage.file;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import java.io.File;
  46. import java.io.FilenameFilter;
  47. import java.io.IOException;
  48. import java.io.PrintWriter;
  49. import java.text.MessageFormat;
  50. import java.util.Collection;
  51. import java.util.Collections;
  52. import java.util.Set;
  53. import java.util.concurrent.Callable;
  54. import java.util.concurrent.ExecutorService;
  55. import java.util.concurrent.Executors;
  56. import java.util.concurrent.Future;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.lib.ConfigConstants;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.ObjectId;
  62. import org.eclipse.jgit.revwalk.RevCommit;
  63. import org.eclipse.jgit.storage.file.FileBasedConfig;
  64. import org.junit.Assume;
  65. import org.junit.Rule;
  66. import org.junit.Test;
  67. import org.junit.rules.ExpectedException;
  68. public class ObjectDirectoryTest extends RepositoryTestCase {
  69. @Rule
  70. public ExpectedException expectedEx = ExpectedException.none();
  71. @Test
  72. public void testConcurrentInsertionOfBlobsToTheSameNewFanOutDirectory()
  73. throws Exception {
  74. ExecutorService e = Executors.newCachedThreadPool();
  75. for (int i=0; i < 100; ++i) {
  76. ObjectDirectory dir = createBareRepository().getObjectDatabase();
  77. for (Future f : e.invokeAll(blobInsertersForTheSameFanOutDir(dir))) {
  78. f.get();
  79. }
  80. }
  81. }
  82. /**
  83. * Test packfile scanning while a gc is done from the outside (different
  84. * process or different Repository instance). This situation occurs e.g. if
  85. * a gerrit server is serving fetch requests while native git is doing a
  86. * garbage collection. The test shows that when core.trustfolderstat==true
  87. * jgit may miss to detect that a new packfile was created. This situation
  88. * is persistent until a new full rescan of the pack directory is triggered.
  89. *
  90. * The test works with two Repository instances working on the same disk
  91. * location. One (db) for all write operations (creating commits, doing gc)
  92. * and another one (receivingDB) which just reads and which in the end shows
  93. * the bug
  94. *
  95. * @throws Exception
  96. */
  97. @Test
  98. public void testScanningForPackfiles() throws Exception {
  99. ObjectId unknownID = ObjectId
  100. .fromString("c0ffee09d0b63d694bf49bc1e6847473f42d4a8c");
  101. GC gc = new GC(db);
  102. gc.setExpireAgeMillis(0);
  103. gc.setPackExpireAgeMillis(0);
  104. // the default repo db is used to create the objects. The receivingDB
  105. // repo is used to trigger gc's
  106. try (FileRepository receivingDB = new FileRepository(
  107. db.getDirectory())) {
  108. // set trustfolderstat to true. If set to false the test always
  109. // succeeds.
  110. FileBasedConfig cfg = receivingDB.getConfig();
  111. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  112. ConfigConstants.CONFIG_KEY_TRUSTFOLDERSTAT, true);
  113. cfg.save();
  114. // setup a repo which has at least one pack file and trigger
  115. // scanning of the packs directory
  116. ObjectId id = commitFile("file.txt", "test", "master").getId();
  117. gc.gc();
  118. assertFalse(receivingDB.hasObject(unknownID));
  119. assertTrue(receivingDB.getObjectDatabase().hasPackedObject(id));
  120. // preparations
  121. File packsFolder = new File(receivingDB.getObjectsDirectory(),
  122. "pack");
  123. // prepare creation of a temporary file in the pack folder. This
  124. // simulates that a native git gc is happening starting to write
  125. // temporary files but has not yet finished
  126. File tmpFile = new File(packsFolder, "1.tmp");
  127. RevCommit id2 = commitFile("file.txt", "test2", "master");
  128. // wait until filesystem timer ticks. This raises probability that
  129. // the next statements are executed in the same tick as the
  130. // filesystem timer
  131. fsTick(null);
  132. // create a Temp file in the packs folder and trigger a rescan of
  133. // the packs folder. This lets receivingDB think it has scanned the
  134. // packs folder at the current fs timestamp t1. The following gc
  135. // will create new files which have the same timestamp t1 but this
  136. // will not update the mtime of the packs folder. Because of that
  137. // JGit will not rescan the packs folder later on and fails to see
  138. // the pack file created during gc.
  139. assertTrue(tmpFile.createNewFile());
  140. assertFalse(receivingDB.hasObject(unknownID));
  141. // trigger a gc. This will create packfiles which have likely the
  142. // same mtime than the packfolder
  143. gc.gc();
  144. // To deal with racy-git situations JGit's Filesnapshot class will
  145. // report a file/folder potentially dirty if
  146. // cachedLastReadTime-cachedLastModificationTime < 2500ms. This
  147. // causes JGit to always rescan a file after modification. But:
  148. // this was true only if the difference between current system time
  149. // and cachedLastModification time was less than 2500ms. If the
  150. // modification is more than 2500ms ago we may have reported a
  151. // file/folder to be clean although it has not been rescanned. A
  152. // Bug. To show the bug we sleep for more than 2500ms
  153. Thread.sleep(2600);
  154. File[] ret = packsFolder.listFiles(new FilenameFilter() {
  155. @Override
  156. public boolean accept(File dir, String name) {
  157. return name.endsWith(".pack");
  158. }
  159. });
  160. assertTrue(ret != null && ret.length == 1);
  161. Assume.assumeTrue(tmpFile.lastModified() == ret[0].lastModified());
  162. // all objects are in a new packfile but we will not detect it
  163. assertFalse(receivingDB.hasObject(unknownID));
  164. assertTrue(receivingDB.hasObject(id2));
  165. }
  166. }
  167. @Test
  168. public void testShallowFile()
  169. throws Exception {
  170. FileRepository repository = createBareRepository();
  171. ObjectDirectory dir = repository.getObjectDatabase();
  172. String commit = "d3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
  173. try (PrintWriter writer = new PrintWriter(
  174. new File(repository.getDirectory(), Constants.SHALLOW))) {
  175. writer.println(commit);
  176. }
  177. Set<ObjectId> shallowCommits = dir.getShallowCommits();
  178. assertTrue(shallowCommits.remove(ObjectId.fromString(commit)));
  179. assertTrue(shallowCommits.isEmpty());
  180. }
  181. @Test
  182. public void testShallowFileCorrupt()
  183. throws Exception {
  184. FileRepository repository = createBareRepository();
  185. ObjectDirectory dir = repository.getObjectDatabase();
  186. String commit = "X3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
  187. try (PrintWriter writer = new PrintWriter(
  188. new File(repository.getDirectory(), Constants.SHALLOW))) {
  189. writer.println(commit);
  190. }
  191. expectedEx.expect(IOException.class);
  192. expectedEx.expectMessage(MessageFormat
  193. .format(JGitText.get().badShallowLine, commit));
  194. dir.getShallowCommits();
  195. }
  196. private Collection<Callable<ObjectId>> blobInsertersForTheSameFanOutDir(
  197. final ObjectDirectory dir) {
  198. Callable<ObjectId> callable = new Callable<ObjectId>() {
  199. @Override
  200. public ObjectId call() throws Exception {
  201. return dir.newInserter().insert(Constants.OBJ_BLOB, new byte[0]);
  202. }
  203. };
  204. return Collections.nCopies(4, callable);
  205. }
  206. }