Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GcBasicPackingTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.ArrayList;
  50. import java.util.Collection;
  51. import java.util.Date;
  52. import java.util.List;
  53. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  54. import org.eclipse.jgit.lib.ConfigConstants;
  55. import org.eclipse.jgit.lib.RefUpdate;
  56. import org.eclipse.jgit.revwalk.RevCommit;
  57. import org.eclipse.jgit.storage.file.FileBasedConfig;
  58. import org.eclipse.jgit.storage.pack.PackConfig;
  59. import org.junit.Test;
  60. import org.junit.experimental.theories.DataPoints;
  61. import org.junit.experimental.theories.Theories;
  62. import org.junit.experimental.theories.Theory;
  63. import org.junit.runner.RunWith;
  64. @RunWith(Theories.class)
  65. public class GcBasicPackingTest extends GcTestCase {
  66. @DataPoints
  67. public static boolean[] aggressiveValues = { true, false };
  68. @Theory
  69. public void repackEmptyRepo_noPackCreated(boolean aggressive)
  70. throws IOException {
  71. configureGc(gc, aggressive);
  72. gc.repack();
  73. assertEquals(0, repo.getObjectDatabase().getPacks().size());
  74. }
  75. @Theory
  76. public void testPackRepoWithNoRefs(boolean aggressive) throws Exception {
  77. tr.commit().add("A", "A").add("B", "B").create();
  78. stats = gc.getStatistics();
  79. assertEquals(4, stats.numberOfLooseObjects);
  80. assertEquals(0, stats.numberOfPackedObjects);
  81. configureGc(gc, aggressive);
  82. gc.gc();
  83. stats = gc.getStatistics();
  84. assertEquals(4, stats.numberOfLooseObjects);
  85. assertEquals(0, stats.numberOfPackedObjects);
  86. assertEquals(0, stats.numberOfPackFiles);
  87. assertEquals(0, stats.numberOfBitmaps);
  88. }
  89. @Theory
  90. public void testPack2Commits(boolean aggressive) throws Exception {
  91. BranchBuilder bb = tr.branch("refs/heads/master");
  92. bb.commit().add("A", "A").add("B", "B").create();
  93. bb.commit().add("A", "A2").add("B", "B2").create();
  94. stats = gc.getStatistics();
  95. assertEquals(8, stats.numberOfLooseObjects);
  96. assertEquals(0, stats.numberOfPackedObjects);
  97. configureGc(gc, aggressive);
  98. gc.gc();
  99. stats = gc.getStatistics();
  100. assertEquals(0, stats.numberOfLooseObjects);
  101. assertEquals(8, stats.numberOfPackedObjects);
  102. assertEquals(1, stats.numberOfPackFiles);
  103. assertEquals(2, stats.numberOfBitmaps);
  104. }
  105. @Theory
  106. public void testPackAllObjectsInOnePack(boolean aggressive)
  107. throws Exception {
  108. tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
  109. .create();
  110. stats = gc.getStatistics();
  111. assertEquals(4, stats.numberOfLooseObjects);
  112. assertEquals(0, stats.numberOfPackedObjects);
  113. configureGc(gc, aggressive);
  114. gc.gc();
  115. stats = gc.getStatistics();
  116. assertEquals(0, stats.numberOfLooseObjects);
  117. assertEquals(4, stats.numberOfPackedObjects);
  118. assertEquals(1, stats.numberOfPackFiles);
  119. assertEquals(1, stats.numberOfBitmaps);
  120. // Do the gc again and check that it hasn't changed anything
  121. gc.gc();
  122. stats = gc.getStatistics();
  123. assertEquals(0, stats.numberOfLooseObjects);
  124. assertEquals(4, stats.numberOfPackedObjects);
  125. assertEquals(1, stats.numberOfPackFiles);
  126. assertEquals(1, stats.numberOfBitmaps);
  127. }
  128. @Theory
  129. public void testPackCommitsAndLooseOne(boolean aggressive)
  130. throws Exception {
  131. BranchBuilder bb = tr.branch("refs/heads/master");
  132. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  133. bb.commit().add("A", "A2").add("B", "B2").create();
  134. tr.update("refs/heads/master", first);
  135. stats = gc.getStatistics();
  136. assertEquals(8, stats.numberOfLooseObjects);
  137. assertEquals(0, stats.numberOfPackedObjects);
  138. configureGc(gc, aggressive);
  139. gc.gc();
  140. stats = gc.getStatistics();
  141. assertEquals(0, stats.numberOfLooseObjects);
  142. assertEquals(8, stats.numberOfPackedObjects);
  143. assertEquals(2, stats.numberOfPackFiles);
  144. assertEquals(1, stats.numberOfBitmaps);
  145. }
  146. @Theory
  147. public void testNotPackTwice(boolean aggressive) throws Exception {
  148. BranchBuilder bb = tr.branch("refs/heads/master");
  149. RevCommit first = bb.commit().message("M").add("M", "M").create();
  150. bb.commit().message("B").add("B", "Q").create();
  151. bb.commit().message("A").add("A", "A").create();
  152. RevCommit second = tr.commit().parent(first).message("R").add("R", "Q")
  153. .create();
  154. tr.update("refs/tags/t1", second);
  155. Collection<PackFile> oldPacks = tr.getRepository().getObjectDatabase()
  156. .getPacks();
  157. assertEquals(0, oldPacks.size());
  158. stats = gc.getStatistics();
  159. assertEquals(11, stats.numberOfLooseObjects);
  160. assertEquals(0, stats.numberOfPackedObjects);
  161. gc.setExpireAgeMillis(0);
  162. fsTick();
  163. configureGc(gc, aggressive);
  164. gc.gc();
  165. stats = gc.getStatistics();
  166. assertEquals(0, stats.numberOfLooseObjects);
  167. List<PackFile> packs = new ArrayList<>(
  168. repo.getObjectDatabase().getPacks());
  169. assertEquals(11, packs.get(0).getObjectCount());
  170. }
  171. @Test
  172. public void testDonePruneTooYoungPacks() throws Exception {
  173. BranchBuilder bb = tr.branch("refs/heads/master");
  174. bb.commit().message("M").add("M", "M").create();
  175. String tempRef = "refs/heads/soon-to-be-unreferenced";
  176. BranchBuilder bb2 = tr.branch(tempRef);
  177. bb2.commit().message("M").add("M", "M").create();
  178. gc.setExpireAgeMillis(0);
  179. gc.gc();
  180. stats = gc.getStatistics();
  181. assertEquals(0, stats.numberOfLooseObjects);
  182. assertEquals(4, stats.numberOfPackedObjects);
  183. assertEquals(1, stats.numberOfPackFiles);
  184. File oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
  185. .iterator().next().getPackFile();
  186. fsTick();
  187. // delete the temp ref, orphaning its commit
  188. RefUpdate update = tr.getRepository().getRefDatabase().newUpdate(tempRef, false);
  189. update.setForceUpdate(true);
  190. update.delete();
  191. bb.commit().message("B").add("B", "Q").create();
  192. // The old packfile is too young to be deleted. We should end up with
  193. // two pack files
  194. gc.setExpire(new Date(oldPackfile.lastModified() - 1));
  195. gc.gc();
  196. stats = gc.getStatistics();
  197. assertEquals(0, stats.numberOfLooseObjects);
  198. // if objects exist in multiple packFiles then they are counted multiple
  199. // times
  200. assertEquals(10, stats.numberOfPackedObjects);
  201. assertEquals(2, stats.numberOfPackFiles);
  202. // repack again but now without a grace period for loose objects. Since
  203. // we don't have loose objects anymore this shouldn't change anything
  204. gc.setExpireAgeMillis(0);
  205. gc.gc();
  206. stats = gc.getStatistics();
  207. assertEquals(0, stats.numberOfLooseObjects);
  208. // if objects exist in multiple packFiles then they are counted multiple
  209. // times
  210. assertEquals(10, stats.numberOfPackedObjects);
  211. assertEquals(2, stats.numberOfPackFiles);
  212. // repack again but now without a grace period for packfiles. We should
  213. // end up with one packfile
  214. gc.setPackExpireAgeMillis(0);
  215. // we want to keep newly-loosened objects though
  216. gc.setExpireAgeMillis(-1);
  217. gc.gc();
  218. stats = gc.getStatistics();
  219. assertEquals(1, stats.numberOfLooseObjects);
  220. // if objects exist in multiple packFiles then they are counted multiple
  221. // times
  222. assertEquals(6, stats.numberOfPackedObjects);
  223. assertEquals(1, stats.numberOfPackFiles);
  224. }
  225. @Test
  226. public void testImmediatePruning() throws Exception {
  227. BranchBuilder bb = tr.branch("refs/heads/master");
  228. bb.commit().message("M").add("M", "M").create();
  229. String tempRef = "refs/heads/soon-to-be-unreferenced";
  230. BranchBuilder bb2 = tr.branch(tempRef);
  231. bb2.commit().message("M").add("M", "M").create();
  232. gc.setExpireAgeMillis(0);
  233. gc.gc();
  234. stats = gc.getStatistics();
  235. fsTick();
  236. // delete the temp ref, orphaning its commit
  237. RefUpdate update = tr.getRepository().getRefDatabase().newUpdate(tempRef, false);
  238. update.setForceUpdate(true);
  239. update.delete();
  240. bb.commit().message("B").add("B", "Q").create();
  241. // We want to immediately prune deleted objects
  242. FileBasedConfig config = repo.getConfig();
  243. config.setString(ConfigConstants.CONFIG_GC_SECTION, null,
  244. ConfigConstants.CONFIG_KEY_PRUNEEXPIRE, "now");
  245. config.save();
  246. //And we don't want to keep packs full of dead objects
  247. gc.setPackExpireAgeMillis(0);
  248. gc.gc();
  249. stats = gc.getStatistics();
  250. assertEquals(0, stats.numberOfLooseObjects);
  251. assertEquals(6, stats.numberOfPackedObjects);
  252. assertEquals(1, stats.numberOfPackFiles);
  253. }
  254. @Test
  255. public void testPreserveAndPruneOldPacks() throws Exception {
  256. testPreserveOldPacks();
  257. configureGc(gc, false).setPrunePreserved(true);
  258. gc.gc();
  259. assertFalse(repo.getObjectDatabase().getPreservedDirectory().exists());
  260. }
  261. private void testPreserveOldPacks() throws Exception {
  262. BranchBuilder bb = tr.branch("refs/heads/master");
  263. bb.commit().message("P").add("P", "P").create();
  264. // pack loose object into packfile
  265. gc.setExpireAgeMillis(0);
  266. gc.gc();
  267. File oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
  268. .iterator().next().getPackFile();
  269. assertTrue(oldPackfile.exists());
  270. fsTick();
  271. bb.commit().message("B").add("B", "Q").create();
  272. // repack again but now without a grace period for packfiles. We should
  273. // end up with a new packfile and the old one should be placed in the
  274. // preserved directory
  275. gc.setPackExpireAgeMillis(0);
  276. configureGc(gc, false).setPreserveOldPacks(true);
  277. gc.gc();
  278. File oldPackDir = repo.getObjectDatabase().getPreservedDirectory();
  279. String oldPackFileName = oldPackfile.getName();
  280. String oldPackName = oldPackFileName.substring(0,
  281. oldPackFileName.lastIndexOf('.')) + ".old-pack"; //$NON-NLS-1$
  282. File preservePackFile = new File(oldPackDir, oldPackName);
  283. assertTrue(preservePackFile.exists());
  284. }
  285. private PackConfig configureGc(GC myGc, boolean aggressive) {
  286. PackConfig pconfig = new PackConfig(repo);
  287. if (aggressive) {
  288. pconfig.setDeltaSearchWindowSize(250);
  289. pconfig.setMaxDeltaDepth(250);
  290. pconfig.setReuseObjects(false);
  291. } else
  292. pconfig = new PackConfig(repo);
  293. myGc.setPackConfig(pconfig);
  294. return pconfig;
  295. }
  296. }