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.

GcBasicPackingTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 java.io.File;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.Collection;
  49. import java.util.Date;
  50. import java.util.List;
  51. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  52. import org.eclipse.jgit.revwalk.RevCommit;
  53. import org.eclipse.jgit.storage.pack.PackConfig;
  54. import org.junit.Test;
  55. import org.junit.experimental.theories.DataPoints;
  56. import org.junit.experimental.theories.Theories;
  57. import org.junit.experimental.theories.Theory;
  58. import org.junit.runner.RunWith;
  59. @RunWith(Theories.class)
  60. public class GcBasicPackingTest extends GcTestCase {
  61. @DataPoints
  62. public static boolean[] aggressiveValues = { true, false };
  63. @Theory
  64. public void repackEmptyRepo_noPackCreated(boolean aggressive)
  65. throws IOException {
  66. configureGc(gc, aggressive);
  67. gc.repack();
  68. assertEquals(0, repo.getObjectDatabase().getPacks().size());
  69. }
  70. @Theory
  71. public void testPackRepoWithNoRefs(boolean aggressive) throws Exception {
  72. tr.commit().add("A", "A").add("B", "B").create();
  73. stats = gc.getStatistics();
  74. assertEquals(4, stats.numberOfLooseObjects);
  75. assertEquals(0, stats.numberOfPackedObjects);
  76. configureGc(gc, aggressive);
  77. gc.gc();
  78. stats = gc.getStatistics();
  79. assertEquals(4, stats.numberOfLooseObjects);
  80. assertEquals(0, stats.numberOfPackedObjects);
  81. assertEquals(0, stats.numberOfPackFiles);
  82. assertEquals(0, stats.numberOfBitmaps);
  83. }
  84. @Theory
  85. public void testPack2Commits(boolean aggressive) throws Exception {
  86. BranchBuilder bb = tr.branch("refs/heads/master");
  87. bb.commit().add("A", "A").add("B", "B").create();
  88. bb.commit().add("A", "A2").add("B", "B2").create();
  89. stats = gc.getStatistics();
  90. assertEquals(8, stats.numberOfLooseObjects);
  91. assertEquals(0, stats.numberOfPackedObjects);
  92. configureGc(gc, aggressive);
  93. gc.gc();
  94. stats = gc.getStatistics();
  95. assertEquals(0, stats.numberOfLooseObjects);
  96. assertEquals(8, stats.numberOfPackedObjects);
  97. assertEquals(1, stats.numberOfPackFiles);
  98. assertEquals(2, stats.numberOfBitmaps);
  99. }
  100. @Theory
  101. public void testPackAllObjectsInOnePack(boolean aggressive)
  102. throws Exception {
  103. tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
  104. .create();
  105. stats = gc.getStatistics();
  106. assertEquals(4, stats.numberOfLooseObjects);
  107. assertEquals(0, stats.numberOfPackedObjects);
  108. configureGc(gc, aggressive);
  109. gc.gc();
  110. stats = gc.getStatistics();
  111. assertEquals(0, stats.numberOfLooseObjects);
  112. assertEquals(4, stats.numberOfPackedObjects);
  113. assertEquals(1, stats.numberOfPackFiles);
  114. assertEquals(1, stats.numberOfBitmaps);
  115. // Do the gc again and check that it hasn't changed anything
  116. gc.gc();
  117. stats = gc.getStatistics();
  118. assertEquals(0, stats.numberOfLooseObjects);
  119. assertEquals(4, stats.numberOfPackedObjects);
  120. assertEquals(1, stats.numberOfPackFiles);
  121. assertEquals(1, stats.numberOfBitmaps);
  122. }
  123. @Theory
  124. public void testPackCommitsAndLooseOne(boolean aggressive)
  125. throws Exception {
  126. BranchBuilder bb = tr.branch("refs/heads/master");
  127. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  128. bb.commit().add("A", "A2").add("B", "B2").create();
  129. tr.update("refs/heads/master", first);
  130. stats = gc.getStatistics();
  131. assertEquals(8, stats.numberOfLooseObjects);
  132. assertEquals(0, stats.numberOfPackedObjects);
  133. configureGc(gc, aggressive);
  134. gc.gc();
  135. stats = gc.getStatistics();
  136. assertEquals(0, stats.numberOfLooseObjects);
  137. assertEquals(8, stats.numberOfPackedObjects);
  138. assertEquals(2, stats.numberOfPackFiles);
  139. assertEquals(1, stats.numberOfBitmaps);
  140. }
  141. @Theory
  142. public void testNotPackTwice(boolean aggressive) throws Exception {
  143. BranchBuilder bb = tr.branch("refs/heads/master");
  144. RevCommit first = bb.commit().message("M").add("M", "M").create();
  145. bb.commit().message("B").add("B", "Q").create();
  146. bb.commit().message("A").add("A", "A").create();
  147. RevCommit second = tr.commit().parent(first).message("R").add("R", "Q")
  148. .create();
  149. tr.update("refs/tags/t1", second);
  150. Collection<PackFile> oldPacks = tr.getRepository().getObjectDatabase()
  151. .getPacks();
  152. assertEquals(0, oldPacks.size());
  153. stats = gc.getStatistics();
  154. assertEquals(11, stats.numberOfLooseObjects);
  155. assertEquals(0, stats.numberOfPackedObjects);
  156. gc.setExpireAgeMillis(0);
  157. fsTick();
  158. configureGc(gc, aggressive);
  159. gc.gc();
  160. stats = gc.getStatistics();
  161. assertEquals(0, stats.numberOfLooseObjects);
  162. List<PackFile> packs = new ArrayList<>(
  163. repo.getObjectDatabase().getPacks());
  164. assertEquals(11, packs.get(0).getObjectCount());
  165. }
  166. @Test
  167. public void testDonePruneTooYoungPacks() throws Exception {
  168. BranchBuilder bb = tr.branch("refs/heads/master");
  169. bb.commit().message("M").add("M", "M").create();
  170. gc.setExpireAgeMillis(0);
  171. gc.gc();
  172. stats = gc.getStatistics();
  173. assertEquals(0, stats.numberOfLooseObjects);
  174. assertEquals(3, stats.numberOfPackedObjects);
  175. assertEquals(1, stats.numberOfPackFiles);
  176. File oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
  177. .iterator().next().getPackFile();
  178. fsTick();
  179. bb.commit().message("B").add("B", "Q").create();
  180. // The old packfile is too young to be deleted. We should end up with
  181. // two pack files
  182. gc.setExpire(new Date(oldPackfile.lastModified() - 1));
  183. gc.gc();
  184. stats = gc.getStatistics();
  185. assertEquals(0, stats.numberOfLooseObjects);
  186. // if objects exist in multiple packFiles then they are counted multiple
  187. // times
  188. assertEquals(9, stats.numberOfPackedObjects);
  189. assertEquals(2, stats.numberOfPackFiles);
  190. // repack again but now without a grace period for loose objects. Since
  191. // we don't have loose objects anymore this shouldn't change anything
  192. gc.setExpireAgeMillis(0);
  193. gc.gc();
  194. stats = gc.getStatistics();
  195. assertEquals(0, stats.numberOfLooseObjects);
  196. // if objects exist in multiple packFiles then they are counted multiple
  197. // times
  198. assertEquals(9, stats.numberOfPackedObjects);
  199. assertEquals(2, stats.numberOfPackFiles);
  200. // repack again but now without a grace period for packfiles. We should
  201. // end up with one packfile
  202. gc.setPackExpireAgeMillis(0);
  203. gc.gc();
  204. stats = gc.getStatistics();
  205. assertEquals(0, stats.numberOfLooseObjects);
  206. // if objects exist in multiple packFiles then they are counted multiple
  207. // times
  208. assertEquals(6, stats.numberOfPackedObjects);
  209. assertEquals(1, stats.numberOfPackFiles);
  210. }
  211. private void configureGc(GC myGc, boolean aggressive) {
  212. PackConfig pconfig = new PackConfig(repo);
  213. if (aggressive) {
  214. pconfig.setDeltaSearchWindowSize(250);
  215. pconfig.setMaxDeltaDepth(250);
  216. pconfig.setReuseObjects(false);
  217. } else
  218. pconfig = new PackConfig(repo);
  219. myGc.setPackConfig(pconfig);
  220. }
  221. }