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.

DfsGarbageCollector.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  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.dfs;
  44. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
  45. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.GC;
  46. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.GC_REST;
  47. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.GC_TXN;
  48. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.INSERT;
  49. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.RECEIVE;
  50. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.UNREACHABLE_GARBAGE;
  51. import static org.eclipse.jgit.internal.storage.dfs.DfsPackCompactor.configureReftable;
  52. import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
  53. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  54. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  55. import static org.eclipse.jgit.internal.storage.pack.PackExt.REFTABLE;
  56. import static org.eclipse.jgit.internal.storage.pack.PackWriter.NONE;
  57. import java.io.IOException;
  58. import java.util.ArrayList;
  59. import java.util.Arrays;
  60. import java.util.Calendar;
  61. import java.util.Collection;
  62. import java.util.EnumSet;
  63. import java.util.GregorianCalendar;
  64. import java.util.HashSet;
  65. import java.util.List;
  66. import java.util.Set;
  67. import java.util.concurrent.TimeUnit;
  68. import org.eclipse.jgit.internal.JGitText;
  69. import org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource;
  70. import org.eclipse.jgit.internal.storage.file.PackIndex;
  71. import org.eclipse.jgit.internal.storage.file.PackReverseIndex;
  72. import org.eclipse.jgit.internal.storage.pack.PackExt;
  73. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  74. import org.eclipse.jgit.internal.storage.reftable.ReftableCompactor;
  75. import org.eclipse.jgit.internal.storage.reftable.ReftableConfig;
  76. import org.eclipse.jgit.internal.storage.reftable.ReftableWriter;
  77. import org.eclipse.jgit.internal.storage.reftree.RefTreeNames;
  78. import org.eclipse.jgit.lib.AnyObjectId;
  79. import org.eclipse.jgit.lib.Constants;
  80. import org.eclipse.jgit.lib.NullProgressMonitor;
  81. import org.eclipse.jgit.lib.ObjectId;
  82. import org.eclipse.jgit.lib.ObjectIdSet;
  83. import org.eclipse.jgit.lib.ProgressMonitor;
  84. import org.eclipse.jgit.lib.Ref;
  85. import org.eclipse.jgit.lib.RefDatabase;
  86. import org.eclipse.jgit.revwalk.RevWalk;
  87. import org.eclipse.jgit.storage.pack.PackConfig;
  88. import org.eclipse.jgit.storage.pack.PackStatistics;
  89. import org.eclipse.jgit.util.SystemReader;
  90. import org.eclipse.jgit.util.io.CountingOutputStream;
  91. /**
  92. * Repack and garbage collect a repository.
  93. */
  94. public class DfsGarbageCollector {
  95. private final DfsRepository repo;
  96. private final RefDatabase refdb;
  97. private final DfsObjDatabase objdb;
  98. private final List<DfsPackDescription> newPackDesc;
  99. private final List<PackStatistics> newPackStats;
  100. private final List<ObjectIdSet> newPackObj;
  101. private DfsReader ctx;
  102. private PackConfig packConfig;
  103. private ReftableConfig reftableConfig;
  104. private boolean convertToReftable = true;
  105. private boolean includeDeletes;
  106. private long reftableInitialMinUpdateIndex = 1;
  107. private long reftableInitialMaxUpdateIndex = 1;
  108. // See packIsCoalesceableGarbage(), below, for how these two variables
  109. // interact.
  110. private long coalesceGarbageLimit = 50 << 20;
  111. private long garbageTtlMillis = TimeUnit.DAYS.toMillis(1);
  112. private long startTimeMillis;
  113. private List<DfsPackFile> packsBefore;
  114. private List<DfsReftable> reftablesBefore;
  115. private List<DfsPackFile> expiredGarbagePacks;
  116. private Collection<Ref> refsBefore;
  117. private Set<ObjectId> allHeadsAndTags;
  118. private Set<ObjectId> allTags;
  119. private Set<ObjectId> nonHeads;
  120. private Set<ObjectId> txnHeads;
  121. private Set<ObjectId> tagTargets;
  122. /**
  123. * Initialize a garbage collector.
  124. *
  125. * @param repository
  126. * repository objects to be packed will be read from.
  127. */
  128. public DfsGarbageCollector(DfsRepository repository) {
  129. repo = repository;
  130. refdb = repo.getRefDatabase();
  131. objdb = repo.getObjectDatabase();
  132. newPackDesc = new ArrayList<>(4);
  133. newPackStats = new ArrayList<>(4);
  134. newPackObj = new ArrayList<>(4);
  135. packConfig = new PackConfig(repo);
  136. packConfig.setIndexVersion(2);
  137. }
  138. /**
  139. * Get configuration used to generate the new pack file.
  140. *
  141. * @return configuration used to generate the new pack file.
  142. */
  143. public PackConfig getPackConfig() {
  144. return packConfig;
  145. }
  146. /**
  147. * Set the new configuration to use when creating the pack file.
  148. *
  149. * @param newConfig
  150. * the new configuration to use when creating the pack file.
  151. * @return {@code this}
  152. */
  153. public DfsGarbageCollector setPackConfig(PackConfig newConfig) {
  154. packConfig = newConfig;
  155. return this;
  156. }
  157. /**
  158. * Set configuration to write a reftable.
  159. *
  160. * @param cfg
  161. * configuration to write a reftable. Reftable writing is
  162. * disabled (default) when {@code cfg} is {@code null}.
  163. * @return {@code this}
  164. */
  165. public DfsGarbageCollector setReftableConfig(ReftableConfig cfg) {
  166. reftableConfig = cfg;
  167. return this;
  168. }
  169. /**
  170. * Whether the garbage collector should convert references to reftable.
  171. *
  172. * @param convert
  173. * if {@code true}, {@link #setReftableConfig(ReftableConfig)}
  174. * has been set non-null, and a GC reftable doesn't yet exist,
  175. * the garbage collector will make one by scanning the existing
  176. * references, and writing a new reftable. Default is
  177. * {@code true}.
  178. * @return {@code this}
  179. */
  180. public DfsGarbageCollector setConvertToReftable(boolean convert) {
  181. convertToReftable = convert;
  182. return this;
  183. }
  184. /**
  185. * Whether the garbage collector will include tombstones for deleted
  186. * references in the reftable.
  187. *
  188. * @param include
  189. * if {@code true}, the garbage collector will include tombstones
  190. * for deleted references in the reftable. Default is
  191. * {@code false}.
  192. * @return {@code this}
  193. */
  194. public DfsGarbageCollector setIncludeDeletes(boolean include) {
  195. includeDeletes = include;
  196. return this;
  197. }
  198. /**
  199. * Set minUpdateIndex for the initial reftable created during conversion.
  200. *
  201. * @param u
  202. * minUpdateIndex for the initial reftable created by scanning
  203. * {@link org.eclipse.jgit.internal.storage.dfs.DfsRefDatabase#getRefs(String)}.
  204. * Ignored unless caller has also set
  205. * {@link #setReftableConfig(ReftableConfig)}. Defaults to
  206. * {@code 1}. Must be {@code u >= 0}.
  207. * @return {@code this}
  208. */
  209. public DfsGarbageCollector setReftableInitialMinUpdateIndex(long u) {
  210. reftableInitialMinUpdateIndex = Math.max(u, 0);
  211. return this;
  212. }
  213. /**
  214. * Set maxUpdateIndex for the initial reftable created during conversion.
  215. *
  216. * @param u
  217. * maxUpdateIndex for the initial reftable created by scanning
  218. * {@link org.eclipse.jgit.internal.storage.dfs.DfsRefDatabase#getRefs(String)}.
  219. * Ignored unless caller has also set
  220. * {@link #setReftableConfig(ReftableConfig)}. Defaults to
  221. * {@code 1}. Must be {@code u >= 0}.
  222. * @return {@code this}
  223. */
  224. public DfsGarbageCollector setReftableInitialMaxUpdateIndex(long u) {
  225. reftableInitialMaxUpdateIndex = Math.max(0, u);
  226. return this;
  227. }
  228. /**
  229. * Get coalesce garbage limit
  230. *
  231. * @return coalesce garbage limit, packs smaller than this size will be
  232. * repacked.
  233. */
  234. public long getCoalesceGarbageLimit() {
  235. return coalesceGarbageLimit;
  236. }
  237. /**
  238. * Set the byte size limit for garbage packs to be repacked.
  239. * <p>
  240. * Any UNREACHABLE_GARBAGE pack smaller than this limit will be repacked at
  241. * the end of the run. This allows the garbage collector to coalesce
  242. * unreachable objects into a single file.
  243. * <p>
  244. * If an UNREACHABLE_GARBAGE pack is already larger than this limit it will
  245. * be left alone by the garbage collector. This avoids unnecessary disk IO
  246. * reading and copying the objects.
  247. * <p>
  248. * If limit is set to 0 the UNREACHABLE_GARBAGE coalesce is disabled.<br>
  249. * If limit is set to {@link java.lang.Long#MAX_VALUE}, everything is
  250. * coalesced.
  251. * <p>
  252. * Keeping unreachable garbage prevents race conditions with repository
  253. * changes that may suddenly need an object whose only copy was stored in
  254. * the UNREACHABLE_GARBAGE pack.
  255. *
  256. * @param limit
  257. * size in bytes.
  258. * @return {@code this}
  259. */
  260. public DfsGarbageCollector setCoalesceGarbageLimit(long limit) {
  261. coalesceGarbageLimit = limit;
  262. return this;
  263. }
  264. /**
  265. * Get time to live for garbage packs.
  266. *
  267. * @return garbage packs older than this limit (in milliseconds) will be
  268. * pruned as part of the garbage collection process if the value is
  269. * &gt; 0, otherwise garbage packs are retained.
  270. */
  271. public long getGarbageTtlMillis() {
  272. return garbageTtlMillis;
  273. }
  274. /**
  275. * Set the time to live for garbage objects.
  276. * <p>
  277. * Any UNREACHABLE_GARBAGE older than this limit will be pruned at the end
  278. * of the run.
  279. * <p>
  280. * If timeToLiveMillis is set to 0, UNREACHABLE_GARBAGE purging is disabled.
  281. *
  282. * @param ttl
  283. * Time to live whatever unit is specified.
  284. * @param unit
  285. * The specified time unit.
  286. * @return {@code this}
  287. */
  288. public DfsGarbageCollector setGarbageTtl(long ttl, TimeUnit unit) {
  289. garbageTtlMillis = unit.toMillis(ttl);
  290. return this;
  291. }
  292. /**
  293. * Create a single new pack file containing all of the live objects.
  294. * <p>
  295. * This method safely decides which packs can be expired after the new pack
  296. * is created by validating the references have not been modified in an
  297. * incompatible way.
  298. *
  299. * @param pm
  300. * progress monitor to receive updates on as packing may take a
  301. * while, depending on the size of the repository.
  302. * @return true if the repack was successful without race conditions. False
  303. * if a race condition was detected and the repack should be run
  304. * again later.
  305. * @throws java.io.IOException
  306. * a new pack cannot be created.
  307. */
  308. public boolean pack(ProgressMonitor pm) throws IOException {
  309. if (pm == null)
  310. pm = NullProgressMonitor.INSTANCE;
  311. if (packConfig.getIndexVersion() != 2)
  312. throw new IllegalStateException(
  313. JGitText.get().supportOnlyPackIndexVersion2);
  314. startTimeMillis = SystemReader.getInstance().getCurrentTime();
  315. ctx = objdb.newReader();
  316. try {
  317. refdb.refresh();
  318. objdb.clearCache();
  319. refsBefore = getAllRefs();
  320. readPacksBefore();
  321. readReftablesBefore();
  322. Set<ObjectId> allHeads = new HashSet<>();
  323. allHeadsAndTags = new HashSet<>();
  324. allTags = new HashSet<>();
  325. nonHeads = new HashSet<>();
  326. txnHeads = new HashSet<>();
  327. tagTargets = new HashSet<>();
  328. for (Ref ref : refsBefore) {
  329. if (ref.isSymbolic() || ref.getObjectId() == null) {
  330. continue;
  331. }
  332. if (isHead(ref)) {
  333. allHeads.add(ref.getObjectId());
  334. } else if (isTag(ref)) {
  335. allTags.add(ref.getObjectId());
  336. } else if (RefTreeNames.isRefTree(refdb, ref.getName())) {
  337. txnHeads.add(ref.getObjectId());
  338. } else {
  339. nonHeads.add(ref.getObjectId());
  340. }
  341. if (ref.getPeeledObjectId() != null) {
  342. tagTargets.add(ref.getPeeledObjectId());
  343. }
  344. }
  345. // Don't exclude tags that are also branch tips.
  346. allTags.removeAll(allHeads);
  347. allHeadsAndTags.addAll(allHeads);
  348. allHeadsAndTags.addAll(allTags);
  349. // Hoist all branch tips and tags earlier in the pack file
  350. tagTargets.addAll(allHeadsAndTags);
  351. // Combine the GC_REST objects into the GC pack if requested
  352. if (packConfig.getSinglePack()) {
  353. allHeadsAndTags.addAll(nonHeads);
  354. nonHeads.clear();
  355. }
  356. boolean rollback = true;
  357. try {
  358. packHeads(pm);
  359. packRest(pm);
  360. packRefTreeGraph(pm);
  361. packGarbage(pm);
  362. objdb.commitPack(newPackDesc, toPrune());
  363. rollback = false;
  364. return true;
  365. } finally {
  366. if (rollback)
  367. objdb.rollbackPack(newPackDesc);
  368. }
  369. } finally {
  370. ctx.close();
  371. }
  372. }
  373. private Collection<Ref> getAllRefs() throws IOException {
  374. Collection<Ref> refs = refdb.getRefs();
  375. List<Ref> addl = refdb.getAdditionalRefs();
  376. if (!addl.isEmpty()) {
  377. List<Ref> all = new ArrayList<>(refs.size() + addl.size());
  378. all.addAll(refs);
  379. // add additional refs which start with refs/
  380. for (Ref r : addl) {
  381. if (r.getName().startsWith(Constants.R_REFS)) {
  382. all.add(r);
  383. }
  384. }
  385. return all;
  386. }
  387. return refs;
  388. }
  389. private void readPacksBefore() throws IOException {
  390. DfsPackFile[] packs = objdb.getPacks();
  391. packsBefore = new ArrayList<>(packs.length);
  392. expiredGarbagePacks = new ArrayList<>(packs.length);
  393. long now = SystemReader.getInstance().getCurrentTime();
  394. for (DfsPackFile p : packs) {
  395. DfsPackDescription d = p.getPackDescription();
  396. if (d.getPackSource() != UNREACHABLE_GARBAGE) {
  397. packsBefore.add(p);
  398. } else if (packIsExpiredGarbage(d, now)) {
  399. expiredGarbagePacks.add(p);
  400. } else if (packIsCoalesceableGarbage(d, now)) {
  401. packsBefore.add(p);
  402. }
  403. }
  404. }
  405. private void readReftablesBefore() throws IOException {
  406. DfsReftable[] tables = objdb.getReftables();
  407. reftablesBefore = new ArrayList<>(Arrays.asList(tables));
  408. }
  409. private boolean packIsExpiredGarbage(DfsPackDescription d, long now) {
  410. // Consider the garbage pack as expired when it's older than
  411. // garbagePackTtl. This check gives concurrent inserter threads
  412. // sufficient time to identify an object is not in the graph and should
  413. // have a new copy written, rather than relying on something from an
  414. // UNREACHABLE_GARBAGE pack.
  415. return d.getPackSource() == UNREACHABLE_GARBAGE
  416. && garbageTtlMillis > 0
  417. && now - d.getLastModified() >= garbageTtlMillis;
  418. }
  419. private boolean packIsCoalesceableGarbage(DfsPackDescription d, long now) {
  420. // An UNREACHABLE_GARBAGE pack can be coalesced if its size is less than
  421. // the coalesceGarbageLimit and either garbageTtl is zero or if the pack
  422. // is created in a close time interval (on a single calendar day when
  423. // the garbageTtl is more than one day or one third of the garbageTtl).
  424. //
  425. // When the garbageTtl is more than 24 hours, garbage packs that are
  426. // created within a single calendar day are coalesced together. This
  427. // would make the effective ttl of the garbage pack as garbageTtl+23:59
  428. // and limit the number of garbage to a maximum number of
  429. // garbageTtl_in_days + 1 (assuming all of them are less than the size
  430. // of coalesceGarbageLimit).
  431. //
  432. // When the garbageTtl is less than or equal to 24 hours, garbage packs
  433. // that are created within a one third of garbageTtl are coalesced
  434. // together. This would make the effective ttl of the garbage packs as
  435. // garbageTtl + (garbageTtl / 3) and would limit the number of garbage
  436. // packs to a maximum number of 4 (assuming all of them are less than
  437. // the size of coalesceGarbageLimit).
  438. if (d.getPackSource() != UNREACHABLE_GARBAGE
  439. || d.getFileSize(PackExt.PACK) >= coalesceGarbageLimit) {
  440. return false;
  441. }
  442. if (garbageTtlMillis == 0) {
  443. return true;
  444. }
  445. long lastModified = d.getLastModified();
  446. long dayStartLastModified = dayStartInMillis(lastModified);
  447. long dayStartToday = dayStartInMillis(now);
  448. if (dayStartLastModified != dayStartToday) {
  449. return false; // this pack is not created today.
  450. }
  451. if (garbageTtlMillis > TimeUnit.DAYS.toMillis(1)) {
  452. return true; // ttl is more than one day and pack is created today.
  453. }
  454. long timeInterval = garbageTtlMillis / 3;
  455. if (timeInterval == 0) {
  456. return false; // ttl is too small, don't try to coalesce.
  457. }
  458. long modifiedTimeSlot = (lastModified - dayStartLastModified) / timeInterval;
  459. long presentTimeSlot = (now - dayStartToday) / timeInterval;
  460. return modifiedTimeSlot == presentTimeSlot;
  461. }
  462. private static long dayStartInMillis(long timeInMillis) {
  463. Calendar cal = new GregorianCalendar(
  464. SystemReader.getInstance().getTimeZone());
  465. cal.setTimeInMillis(timeInMillis);
  466. cal.set(Calendar.HOUR_OF_DAY, 0);
  467. cal.set(Calendar.MINUTE, 0);
  468. cal.set(Calendar.SECOND, 0);
  469. cal.set(Calendar.MILLISECOND, 0);
  470. return cal.getTimeInMillis();
  471. }
  472. /**
  473. * Get all of the source packs that fed into this compaction.
  474. *
  475. * @return all of the source packs that fed into this compaction.
  476. */
  477. public Set<DfsPackDescription> getSourcePacks() {
  478. return toPrune();
  479. }
  480. /**
  481. * Get new packs created by this compaction.
  482. *
  483. * @return new packs created by this compaction.
  484. */
  485. public List<DfsPackDescription> getNewPacks() {
  486. return newPackDesc;
  487. }
  488. /**
  489. * Get statistics corresponding to the {@link #getNewPacks()}.
  490. * <p>
  491. * The elements can be null if the stat is not available for the pack file.
  492. *
  493. * @return statistics corresponding to the {@link #getNewPacks()}.
  494. */
  495. public List<PackStatistics> getNewPackStatistics() {
  496. return newPackStats;
  497. }
  498. private Set<DfsPackDescription> toPrune() {
  499. Set<DfsPackDescription> toPrune = new HashSet<>();
  500. for (DfsPackFile pack : packsBefore) {
  501. toPrune.add(pack.getPackDescription());
  502. }
  503. if (reftableConfig != null) {
  504. for (DfsReftable table : reftablesBefore) {
  505. toPrune.add(table.getPackDescription());
  506. }
  507. }
  508. for (DfsPackFile pack : expiredGarbagePacks) {
  509. toPrune.add(pack.getPackDescription());
  510. }
  511. return toPrune;
  512. }
  513. private void packHeads(ProgressMonitor pm) throws IOException {
  514. if (allHeadsAndTags.isEmpty()) {
  515. writeReftable();
  516. return;
  517. }
  518. try (PackWriter pw = newPackWriter()) {
  519. pw.setTagTargets(tagTargets);
  520. pw.preparePack(pm, allHeadsAndTags, NONE, NONE, allTags);
  521. if (0 < pw.getObjectCount()) {
  522. long estSize = estimateGcPackSize(INSERT, RECEIVE, COMPACT, GC);
  523. writePack(GC, pw, pm, estSize);
  524. } else {
  525. writeReftable();
  526. }
  527. }
  528. }
  529. private void packRest(ProgressMonitor pm) throws IOException {
  530. if (nonHeads.isEmpty())
  531. return;
  532. try (PackWriter pw = newPackWriter()) {
  533. for (ObjectIdSet packedObjs : newPackObj)
  534. pw.excludeObjects(packedObjs);
  535. pw.preparePack(pm, nonHeads, allHeadsAndTags);
  536. if (0 < pw.getObjectCount())
  537. writePack(GC_REST, pw, pm,
  538. estimateGcPackSize(INSERT, RECEIVE, COMPACT, GC_REST));
  539. }
  540. }
  541. private void packRefTreeGraph(ProgressMonitor pm) throws IOException {
  542. if (txnHeads.isEmpty())
  543. return;
  544. try (PackWriter pw = newPackWriter()) {
  545. for (ObjectIdSet packedObjs : newPackObj)
  546. pw.excludeObjects(packedObjs);
  547. pw.preparePack(pm, txnHeads, NONE);
  548. if (0 < pw.getObjectCount())
  549. writePack(GC_TXN, pw, pm, 0 /* unknown pack size */);
  550. }
  551. }
  552. private void packGarbage(ProgressMonitor pm) throws IOException {
  553. PackConfig cfg = new PackConfig(packConfig);
  554. cfg.setReuseDeltas(true);
  555. cfg.setReuseObjects(true);
  556. cfg.setDeltaCompress(false);
  557. cfg.setBuildBitmaps(false);
  558. try (PackWriter pw = new PackWriter(cfg, ctx);
  559. RevWalk pool = new RevWalk(ctx)) {
  560. pw.setDeltaBaseAsOffset(true);
  561. pw.setReuseDeltaCommits(true);
  562. pm.beginTask(JGitText.get().findingGarbage, objectsBefore());
  563. long estimatedPackSize = 12 + 20; // header and trailer sizes.
  564. for (DfsPackFile oldPack : packsBefore) {
  565. PackIndex oldIdx = oldPack.getPackIndex(ctx);
  566. PackReverseIndex oldRevIdx = oldPack.getReverseIdx(ctx);
  567. long maxOffset = oldPack.getPackDescription().getFileSize(PACK)
  568. - 20; // pack size - trailer size.
  569. for (PackIndex.MutableEntry ent : oldIdx) {
  570. pm.update(1);
  571. ObjectId id = ent.toObjectId();
  572. if (pool.lookupOrNull(id) != null || anyPackHas(id))
  573. continue;
  574. long offset = ent.getOffset();
  575. int type = oldPack.getObjectType(ctx, offset);
  576. pw.addObject(pool.lookupAny(id, type));
  577. long objSize = oldRevIdx.findNextOffset(offset, maxOffset)
  578. - offset;
  579. estimatedPackSize += objSize;
  580. }
  581. }
  582. pm.endTask();
  583. if (0 < pw.getObjectCount())
  584. writePack(UNREACHABLE_GARBAGE, pw, pm, estimatedPackSize);
  585. }
  586. }
  587. private boolean anyPackHas(AnyObjectId id) {
  588. for (ObjectIdSet packedObjs : newPackObj)
  589. if (packedObjs.contains(id))
  590. return true;
  591. return false;
  592. }
  593. private static boolean isHead(Ref ref) {
  594. return ref.getName().startsWith(Constants.R_HEADS);
  595. }
  596. private static boolean isTag(Ref ref) {
  597. return ref.getName().startsWith(Constants.R_TAGS);
  598. }
  599. private int objectsBefore() {
  600. int cnt = 0;
  601. for (DfsPackFile p : packsBefore)
  602. cnt += (int) p.getPackDescription().getObjectCount();
  603. return cnt;
  604. }
  605. private PackWriter newPackWriter() {
  606. PackWriter pw = new PackWriter(packConfig, ctx);
  607. pw.setDeltaBaseAsOffset(true);
  608. pw.setReuseDeltaCommits(false);
  609. return pw;
  610. }
  611. private long estimateGcPackSize(PackSource first, PackSource... rest) {
  612. EnumSet<PackSource> sourceSet = EnumSet.of(first, rest);
  613. // Every pack file contains 12 bytes of header and 20 bytes of trailer.
  614. // Include the final pack file header and trailer size here and ignore
  615. // the same from individual pack files.
  616. long size = 32;
  617. for (DfsPackDescription pack : getSourcePacks()) {
  618. if (sourceSet.contains(pack.getPackSource())) {
  619. size += pack.getFileSize(PACK) - 32;
  620. }
  621. }
  622. return size;
  623. }
  624. private DfsPackDescription writePack(PackSource source, PackWriter pw,
  625. ProgressMonitor pm, long estimatedPackSize) throws IOException {
  626. DfsPackDescription pack = repo.getObjectDatabase().newPack(source,
  627. estimatedPackSize);
  628. if (source == GC && reftableConfig != null) {
  629. writeReftable(pack);
  630. }
  631. try (DfsOutputStream out = objdb.writeFile(pack, PACK)) {
  632. pw.writePack(pm, pm, out);
  633. pack.addFileExt(PACK);
  634. pack.setBlockSize(PACK, out.blockSize());
  635. }
  636. try (DfsOutputStream out = objdb.writeFile(pack, INDEX)) {
  637. CountingOutputStream cnt = new CountingOutputStream(out);
  638. pw.writeIndex(cnt);
  639. pack.addFileExt(INDEX);
  640. pack.setFileSize(INDEX, cnt.getCount());
  641. pack.setBlockSize(INDEX, out.blockSize());
  642. pack.setIndexVersion(pw.getIndexVersion());
  643. }
  644. if (pw.prepareBitmapIndex(pm)) {
  645. try (DfsOutputStream out = objdb.writeFile(pack, BITMAP_INDEX)) {
  646. CountingOutputStream cnt = new CountingOutputStream(out);
  647. pw.writeBitmapIndex(cnt);
  648. pack.addFileExt(BITMAP_INDEX);
  649. pack.setFileSize(BITMAP_INDEX, cnt.getCount());
  650. pack.setBlockSize(BITMAP_INDEX, out.blockSize());
  651. }
  652. }
  653. PackStatistics stats = pw.getStatistics();
  654. pack.setPackStats(stats);
  655. pack.setLastModified(startTimeMillis);
  656. newPackDesc.add(pack);
  657. newPackStats.add(stats);
  658. newPackObj.add(pw.getObjectSet());
  659. return pack;
  660. }
  661. private void writeReftable() throws IOException {
  662. if (reftableConfig != null) {
  663. DfsPackDescription pack = objdb.newPack(GC);
  664. newPackDesc.add(pack);
  665. newPackStats.add(null);
  666. writeReftable(pack);
  667. }
  668. }
  669. private void writeReftable(DfsPackDescription pack) throws IOException {
  670. if (convertToReftable && !hasGcReftable()) {
  671. writeReftable(pack, refsBefore);
  672. return;
  673. }
  674. try (ReftableStack stack = ReftableStack.open(ctx, reftablesBefore)) {
  675. ReftableCompactor compact = new ReftableCompactor();
  676. compact.addAll(stack.readers());
  677. compact.setIncludeDeletes(includeDeletes);
  678. compactReftable(pack, compact);
  679. }
  680. }
  681. private boolean hasGcReftable() {
  682. for (DfsReftable table : reftablesBefore) {
  683. if (table.getPackDescription().getPackSource() == GC) {
  684. return true;
  685. }
  686. }
  687. return false;
  688. }
  689. private void writeReftable(DfsPackDescription pack, Collection<Ref> refs)
  690. throws IOException {
  691. try (DfsOutputStream out = objdb.writeFile(pack, REFTABLE)) {
  692. ReftableConfig cfg = configureReftable(reftableConfig, out);
  693. ReftableWriter writer = new ReftableWriter(cfg)
  694. .setMinUpdateIndex(reftableInitialMinUpdateIndex)
  695. .setMaxUpdateIndex(reftableInitialMaxUpdateIndex)
  696. .begin(out)
  697. .sortAndWriteRefs(refs)
  698. .finish();
  699. pack.addFileExt(REFTABLE);
  700. pack.setReftableStats(writer.getStats());
  701. }
  702. }
  703. private void compactReftable(DfsPackDescription pack,
  704. ReftableCompactor compact) throws IOException {
  705. try (DfsOutputStream out = objdb.writeFile(pack, REFTABLE)) {
  706. compact.setConfig(configureReftable(reftableConfig, out));
  707. compact.compact(out);
  708. pack.addFileExt(REFTABLE);
  709. pack.setReftableStats(compact.getStats());
  710. }
  711. }
  712. }