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.6KB

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