Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

GcConcurrentTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import static java.lang.Integer.valueOf;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertNotEquals;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.IOException;
  51. import java.util.Collection;
  52. import java.util.Collections;
  53. import java.util.concurrent.BrokenBarrierException;
  54. import java.util.concurrent.Callable;
  55. import java.util.concurrent.CountDownLatch;
  56. import java.util.concurrent.CyclicBarrier;
  57. import java.util.concurrent.ExecutionException;
  58. import java.util.concurrent.ExecutorService;
  59. import java.util.concurrent.Executors;
  60. import java.util.concurrent.Future;
  61. import java.util.concurrent.TimeUnit;
  62. import org.eclipse.jgit.errors.CancelledException;
  63. import org.eclipse.jgit.internal.JGitText;
  64. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  65. import org.eclipse.jgit.junit.TestRepository;
  66. import org.eclipse.jgit.lib.ConfigConstants;
  67. import org.eclipse.jgit.lib.EmptyProgressMonitor;
  68. import org.eclipse.jgit.lib.NullProgressMonitor;
  69. import org.eclipse.jgit.lib.ObjectId;
  70. import org.eclipse.jgit.lib.Sets;
  71. import org.eclipse.jgit.revwalk.RevBlob;
  72. import org.eclipse.jgit.revwalk.RevCommit;
  73. import org.eclipse.jgit.storage.file.FileBasedConfig;
  74. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  75. import org.junit.Test;
  76. public class GcConcurrentTest extends GcTestCase {
  77. @Test
  78. public void concurrentRepack() throws Exception {
  79. final CyclicBarrier syncPoint = new CyclicBarrier(2);
  80. class DoRepack extends EmptyProgressMonitor implements
  81. Callable<Integer> {
  82. @Override
  83. public void beginTask(String title, int totalWork) {
  84. if (title.equals(JGitText.get().writingObjects)) {
  85. try {
  86. syncPoint.await();
  87. } catch (InterruptedException e) {
  88. Thread.currentThread().interrupt();
  89. } catch (BrokenBarrierException ignored) {
  90. //
  91. }
  92. }
  93. }
  94. /** @return 0 for success, 1 in case of error when writing pack */
  95. @Override
  96. public Integer call() throws Exception {
  97. try {
  98. gc.setProgressMonitor(this);
  99. gc.repack();
  100. return valueOf(0);
  101. } catch (IOException e) {
  102. // leave the syncPoint in broken state so any awaiting
  103. // threads and any threads that call await in the future get
  104. // the BrokenBarrierException
  105. Thread.currentThread().interrupt();
  106. try {
  107. syncPoint.await();
  108. } catch (InterruptedException ignored) {
  109. //
  110. }
  111. return valueOf(1);
  112. }
  113. }
  114. }
  115. RevBlob a = tr.blob("a");
  116. tr.lightweightTag("t", a);
  117. ExecutorService pool = Executors.newFixedThreadPool(2);
  118. try {
  119. DoRepack repack1 = new DoRepack();
  120. DoRepack repack2 = new DoRepack();
  121. Future<Integer> result1 = pool.submit(repack1);
  122. Future<Integer> result2 = pool.submit(repack2);
  123. assertEquals(0, result1.get().intValue() + result2.get().intValue());
  124. } finally {
  125. pool.shutdown();
  126. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  127. }
  128. }
  129. @Test
  130. public void repackAndGetStats() throws Exception {
  131. TestRepository<FileRepository>.BranchBuilder test = tr.branch("test");
  132. test.commit().add("a", "a").create();
  133. GC gc1 = new GC(tr.getRepository());
  134. gc1.setPackExpireAgeMillis(0);
  135. gc1.gc();
  136. test.commit().add("b", "b").create();
  137. // Create a new Repository instance and trigger a gc
  138. // from that instance. Reusing the existing repo instance
  139. // tr.getRepository() would not show the problem.
  140. FileRepository r2 = new FileRepository(
  141. tr.getRepository().getDirectory());
  142. GC gc2 = new GC(r2);
  143. gc2.setPackExpireAgeMillis(0);
  144. gc2.gc();
  145. new GC(tr.getRepository()).getStatistics();
  146. }
  147. @Test
  148. public void repackAndUploadPack() throws Exception {
  149. TestRepository<FileRepository>.BranchBuilder test = tr.branch("test");
  150. // RevCommit a = test.commit().add("a", "a").create();
  151. test.commit().add("a", "a").create();
  152. GC gc1 = new GC(tr.getRepository());
  153. gc1.setPackExpireAgeMillis(0);
  154. gc1.gc();
  155. RevCommit b = test.commit().add("b", "b").create();
  156. FileRepository r2 = new FileRepository(
  157. tr.getRepository().getDirectory());
  158. GC gc2 = new GC(r2);
  159. gc2.setPackExpireAgeMillis(0);
  160. gc2.gc();
  161. // Simulate parts of an UploadPack. This is the situation on
  162. // server side (e.g. gerrit) when when clients are
  163. // cloning/fetching while the server side repo's
  164. // are gc'ed by an external process (e.g. scheduled
  165. // native git gc)
  166. try (PackWriter pw = new PackWriter(tr.getRepository())) {
  167. pw.setUseBitmaps(true);
  168. pw.preparePack(NullProgressMonitor.INSTANCE, Sets.of(b),
  169. Collections.<ObjectId> emptySet());
  170. new GC(tr.getRepository()).getStatistics();
  171. }
  172. }
  173. PackFile getSinglePack(FileRepository r) {
  174. Collection<PackFile> packs = r.getObjectDatabase().getPacks();
  175. assertEquals(1, packs.size());
  176. return packs.iterator().next();
  177. }
  178. @Test
  179. public void repackAndCheckBitmapUsage() throws Exception {
  180. // create a test repository with one commit and pack all objects. After
  181. // packing create loose objects to trigger creation of a new packfile on
  182. // the next gc
  183. TestRepository<FileRepository>.BranchBuilder test = tr.branch("test");
  184. test.commit().add("a", "a").create();
  185. FileRepository repository = tr.getRepository();
  186. GC gc1 = new GC(repository);
  187. gc1.setPackExpireAgeMillis(0);
  188. gc1.gc();
  189. String oldPackName = getSinglePack(repository).getPackName();
  190. RevCommit b = test.commit().add("b", "b").create();
  191. // start the garbage collection on a new repository instance,
  192. FileRepository repository2 = new FileRepository(repository.getDirectory());
  193. GC gc2 = new GC(repository2);
  194. gc2.setPackExpireAgeMillis(0);
  195. gc2.gc();
  196. String newPackName = getSinglePack(repository2).getPackName();
  197. // make sure gc() has caused creation of a new packfile
  198. assertNotEquals(oldPackName, newPackName);
  199. // Even when asking again for the set of packfiles outdated data
  200. // will be returned. As long as the repository can work on cached data
  201. // it will do so and not detect that a new packfile exists.
  202. assertNotEquals(getSinglePack(repository).getPackName(), newPackName);
  203. // Only when accessing object content it is required to rescan the pack
  204. // directory and the new packfile will be detected.
  205. repository.getObjectDatabase().open(b).getSize();
  206. assertEquals(getSinglePack(repository).getPackName(), newPackName);
  207. assertNotNull(getSinglePack(repository).getBitmapIndex());
  208. }
  209. @Test
  210. public void testInterruptGc() throws Exception {
  211. FileBasedConfig c = repo.getConfig();
  212. c.setInt(ConfigConstants.CONFIG_GC_SECTION, null,
  213. ConfigConstants.CONFIG_KEY_AUTOPACKLIMIT, 1);
  214. c.save();
  215. SampleDataRepositoryTestCase.copyCGitTestPacks(repo);
  216. ExecutorService executor = Executors.newSingleThreadExecutor();
  217. final CountDownLatch latch = new CountDownLatch(1);
  218. Future<Collection<PackFile>> result = executor
  219. .submit(new Callable<Collection<PackFile>>() {
  220. @Override
  221. public Collection<PackFile> call() throws Exception {
  222. long start = System.currentTimeMillis();
  223. System.out.println("starting gc");
  224. latch.countDown();
  225. Collection<PackFile> r = gc.gc();
  226. System.out.println("gc took "
  227. + (System.currentTimeMillis() - start) + " ms");
  228. return r;
  229. }
  230. });
  231. try {
  232. latch.await();
  233. Thread.sleep(5);
  234. executor.shutdownNow();
  235. result.get();
  236. fail("thread wasn't interrupted");
  237. } catch (ExecutionException e) {
  238. Throwable cause = e.getCause();
  239. if (cause instanceof CancelledException) {
  240. assertEquals(JGitText.get().operationCanceled,
  241. cause.getMessage());
  242. } else if (cause instanceof IOException) {
  243. Throwable cause2 = cause.getCause();
  244. assertTrue(cause2 instanceof InterruptedException
  245. || cause2 instanceof ExecutionException);
  246. } else {
  247. fail("unexpected exception " + e);
  248. }
  249. }
  250. }
  251. }