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.

PackInserterTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (C) 2017, 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.file;
  44. import static java.util.Comparator.comparing;
  45. import static java.util.stream.Collectors.toList;
  46. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  47. import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
  48. import static org.hamcrest.Matchers.greaterThan;
  49. import static org.hamcrest.Matchers.lessThan;
  50. import static org.junit.Assert.assertArrayEquals;
  51. import static org.junit.Assert.assertEquals;
  52. import static org.junit.Assert.assertNotEquals;
  53. import static org.junit.Assert.assertThat;
  54. import static org.junit.Assert.fail;
  55. import java.io.ByteArrayInputStream;
  56. import java.io.File;
  57. import java.io.IOException;
  58. import java.nio.file.FileVisitResult;
  59. import java.nio.file.Files;
  60. import java.nio.file.Path;
  61. import java.nio.file.SimpleFileVisitor;
  62. import java.nio.file.attribute.BasicFileAttributes;
  63. import java.util.ArrayList;
  64. import java.util.Collection;
  65. import java.util.List;
  66. import java.util.Random;
  67. import java.util.function.Predicate;
  68. import java.util.regex.Matcher;
  69. import java.util.regex.Pattern;
  70. import org.eclipse.jgit.dircache.DirCache;
  71. import org.eclipse.jgit.dircache.DirCacheBuilder;
  72. import org.eclipse.jgit.dircache.DirCacheEntry;
  73. import org.eclipse.jgit.errors.MissingObjectException;
  74. import org.eclipse.jgit.junit.RepositoryTestCase;
  75. import org.eclipse.jgit.lib.CommitBuilder;
  76. import org.eclipse.jgit.lib.Constants;
  77. import org.eclipse.jgit.lib.FileMode;
  78. import org.eclipse.jgit.lib.ObjectId;
  79. import org.eclipse.jgit.lib.ObjectLoader;
  80. import org.eclipse.jgit.lib.ObjectReader;
  81. import org.eclipse.jgit.lib.ObjectStream;
  82. import org.eclipse.jgit.storage.file.WindowCacheConfig;
  83. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  84. import org.eclipse.jgit.util.IO;
  85. import org.junit.After;
  86. import org.junit.Before;
  87. import org.junit.Test;
  88. @SuppressWarnings("boxing")
  89. public class PackInserterTest extends RepositoryTestCase {
  90. private WindowCacheConfig origWindowCacheConfig;
  91. @Before
  92. public void setWindowCacheConfig() {
  93. origWindowCacheConfig = new WindowCacheConfig();
  94. origWindowCacheConfig.install();
  95. }
  96. @After
  97. public void resetWindowCacheConfig() {
  98. origWindowCacheConfig.install();
  99. }
  100. @Before
  101. public void emptyAtSetUp() throws Exception {
  102. assertEquals(0, listPacks().size());
  103. assertNoObjects();
  104. }
  105. @Test
  106. public void noFlush() throws Exception {
  107. try (PackInserter ins = newInserter()) {
  108. ins.insert(OBJ_BLOB, Constants.encode("foo contents"));
  109. // No flush.
  110. }
  111. assertNoObjects();
  112. }
  113. @Test
  114. public void flushEmptyPack() throws Exception {
  115. try (PackInserter ins = newInserter()) {
  116. ins.flush();
  117. }
  118. assertNoObjects();
  119. }
  120. @Test
  121. public void singlePack() throws Exception {
  122. ObjectId blobId;
  123. byte[] blob = Constants.encode("foo contents");
  124. ObjectId treeId;
  125. ObjectId commitId;
  126. byte[] commit;
  127. try (PackInserter ins = newInserter()) {
  128. blobId = ins.insert(OBJ_BLOB, blob);
  129. DirCache dc = DirCache.newInCore();
  130. DirCacheBuilder b = dc.builder();
  131. DirCacheEntry dce = new DirCacheEntry("foo");
  132. dce.setFileMode(FileMode.REGULAR_FILE);
  133. dce.setObjectId(blobId);
  134. b.add(dce);
  135. b.finish();
  136. treeId = dc.writeTree(ins);
  137. CommitBuilder cb = new CommitBuilder();
  138. cb.setTreeId(treeId);
  139. cb.setAuthor(author);
  140. cb.setCommitter(committer);
  141. cb.setMessage("Commit message");
  142. commit = cb.toByteArray();
  143. commitId = ins.insert(cb);
  144. ins.flush();
  145. }
  146. assertPacksOnly();
  147. List<PackFile> packs = listPacks();
  148. assertEquals(1, packs.size());
  149. assertEquals(3, packs.get(0).getObjectCount());
  150. try (ObjectReader reader = db.newObjectReader()) {
  151. assertBlob(reader, blobId, blob);
  152. CanonicalTreeParser treeParser =
  153. new CanonicalTreeParser(null, reader, treeId);
  154. assertEquals("foo", treeParser.getEntryPathString());
  155. assertEquals(blobId, treeParser.getEntryObjectId());
  156. ObjectLoader commitLoader = reader.open(commitId);
  157. assertEquals(OBJ_COMMIT, commitLoader.getType());
  158. assertArrayEquals(commit, commitLoader.getBytes());
  159. }
  160. }
  161. @Test
  162. public void multiplePacks() throws Exception {
  163. ObjectId blobId1;
  164. ObjectId blobId2;
  165. byte[] blob1 = Constants.encode("blob1");
  166. byte[] blob2 = Constants.encode("blob2");
  167. try (PackInserter ins = newInserter()) {
  168. blobId1 = ins.insert(OBJ_BLOB, blob1);
  169. ins.flush();
  170. blobId2 = ins.insert(OBJ_BLOB, blob2);
  171. ins.flush();
  172. }
  173. assertPacksOnly();
  174. List<PackFile> packs = listPacks();
  175. assertEquals(2, packs.size());
  176. assertEquals(1, packs.get(0).getObjectCount());
  177. assertEquals(1, packs.get(1).getObjectCount());
  178. try (ObjectReader reader = db.newObjectReader()) {
  179. assertBlob(reader, blobId1, blob1);
  180. assertBlob(reader, blobId2, blob2);
  181. }
  182. }
  183. @Test
  184. public void largeBlob() throws Exception {
  185. ObjectId blobId;
  186. byte[] blob = newLargeBlob();
  187. try (PackInserter ins = newInserter()) {
  188. assertThat(blob.length, greaterThan(ins.getBufferSize()));
  189. blobId =
  190. ins.insert(OBJ_BLOB, blob.length, new ByteArrayInputStream(blob));
  191. ins.flush();
  192. }
  193. assertPacksOnly();
  194. Collection<PackFile> packs = listPacks();
  195. assertEquals(1, packs.size());
  196. PackFile p = packs.iterator().next();
  197. assertEquals(1, p.getObjectCount());
  198. try (ObjectReader reader = db.newObjectReader()) {
  199. assertBlob(reader, blobId, blob);
  200. }
  201. }
  202. @Test
  203. public void overwriteExistingPack() throws Exception {
  204. ObjectId blobId;
  205. byte[] blob = Constants.encode("foo contents");
  206. try (PackInserter ins = newInserter()) {
  207. blobId = ins.insert(OBJ_BLOB, blob);
  208. ins.flush();
  209. }
  210. assertPacksOnly();
  211. List<PackFile> packs = listPacks();
  212. assertEquals(1, packs.size());
  213. PackFile pack = packs.get(0);
  214. assertEquals(1, pack.getObjectCount());
  215. String inode = getInode(pack.getPackFile());
  216. try (PackInserter ins = newInserter()) {
  217. ins.checkExisting(false);
  218. assertEquals(blobId, ins.insert(OBJ_BLOB, blob));
  219. ins.flush();
  220. }
  221. assertPacksOnly();
  222. packs = listPacks();
  223. assertEquals(1, packs.size());
  224. pack = packs.get(0);
  225. assertEquals(1, pack.getObjectCount());
  226. if (inode != null) {
  227. // Old file was overwritten with new file, although objects were
  228. // equivalent.
  229. assertNotEquals(inode, getInode(pack.getPackFile()));
  230. }
  231. }
  232. @Test
  233. public void checkExisting() throws Exception {
  234. ObjectId blobId;
  235. byte[] blob = Constants.encode("foo contents");
  236. try (PackInserter ins = newInserter()) {
  237. blobId = ins.insert(OBJ_BLOB, blob);
  238. ins.insert(OBJ_BLOB, Constants.encode("another blob"));
  239. ins.flush();
  240. }
  241. assertPacksOnly();
  242. assertEquals(1, listPacks().size());
  243. try (PackInserter ins = newInserter()) {
  244. assertEquals(blobId, ins.insert(OBJ_BLOB, blob));
  245. ins.flush();
  246. }
  247. assertPacksOnly();
  248. assertEquals(1, listPacks().size());
  249. try (PackInserter ins = newInserter()) {
  250. ins.checkExisting(false);
  251. assertEquals(blobId, ins.insert(OBJ_BLOB, blob));
  252. ins.flush();
  253. }
  254. assertPacksOnly();
  255. assertEquals(2, listPacks().size());
  256. try (ObjectReader reader = db.newObjectReader()) {
  257. assertBlob(reader, blobId, blob);
  258. }
  259. }
  260. @Test
  261. public void insertSmallInputStreamRespectsCheckExisting() throws Exception {
  262. ObjectId blobId;
  263. byte[] blob = Constants.encode("foo contents");
  264. try (PackInserter ins = newInserter()) {
  265. assertThat(blob.length, lessThan(ins.getBufferSize()));
  266. blobId = ins.insert(OBJ_BLOB, blob);
  267. ins.insert(OBJ_BLOB, Constants.encode("another blob"));
  268. ins.flush();
  269. }
  270. assertPacksOnly();
  271. assertEquals(1, listPacks().size());
  272. try (PackInserter ins = newInserter()) {
  273. assertEquals(blobId,
  274. ins.insert(OBJ_BLOB, blob.length, new ByteArrayInputStream(blob)));
  275. ins.flush();
  276. }
  277. assertPacksOnly();
  278. assertEquals(1, listPacks().size());
  279. }
  280. @Test
  281. public void insertLargeInputStreamBypassesCheckExisting() throws Exception {
  282. ObjectId blobId;
  283. byte[] blob = newLargeBlob();
  284. try (PackInserter ins = newInserter()) {
  285. assertThat(blob.length, greaterThan(ins.getBufferSize()));
  286. blobId = ins.insert(OBJ_BLOB, blob);
  287. ins.insert(OBJ_BLOB, Constants.encode("another blob"));
  288. ins.flush();
  289. }
  290. assertPacksOnly();
  291. assertEquals(1, listPacks().size());
  292. try (PackInserter ins = newInserter()) {
  293. assertEquals(blobId,
  294. ins.insert(OBJ_BLOB, blob.length, new ByteArrayInputStream(blob)));
  295. ins.flush();
  296. }
  297. assertPacksOnly();
  298. assertEquals(2, listPacks().size());
  299. }
  300. @Test
  301. public void readBackSmallFiles() throws Exception {
  302. ObjectId blobId1;
  303. ObjectId blobId2;
  304. ObjectId blobId3;
  305. byte[] blob1 = Constants.encode("blob1");
  306. byte[] blob2 = Constants.encode("blob2");
  307. byte[] blob3 = Constants.encode("blob3");
  308. try (PackInserter ins = newInserter()) {
  309. assertThat(blob1.length, lessThan(ins.getBufferSize()));
  310. blobId1 = ins.insert(OBJ_BLOB, blob1);
  311. try (ObjectReader reader = ins.newReader()) {
  312. assertBlob(reader, blobId1, blob1);
  313. }
  314. // Read-back should not mess up the file pointer.
  315. blobId2 = ins.insert(OBJ_BLOB, blob2);
  316. ins.flush();
  317. blobId3 = ins.insert(OBJ_BLOB, blob3);
  318. }
  319. assertPacksOnly();
  320. List<PackFile> packs = listPacks();
  321. assertEquals(1, packs.size());
  322. assertEquals(2, packs.get(0).getObjectCount());
  323. try (ObjectReader reader = db.newObjectReader()) {
  324. assertBlob(reader, blobId1, blob1);
  325. assertBlob(reader, blobId2, blob2);
  326. try {
  327. reader.open(blobId3);
  328. fail("Expected MissingObjectException");
  329. } catch (MissingObjectException expected) {
  330. // Expected.
  331. }
  332. }
  333. }
  334. @Test
  335. public void readBackLargeFile() throws Exception {
  336. ObjectId blobId;
  337. byte[] blob = newLargeBlob();
  338. WindowCacheConfig wcc = new WindowCacheConfig();
  339. wcc.setStreamFileThreshold(1024);
  340. wcc.install();
  341. try (ObjectReader reader = db.newObjectReader()) {
  342. assertThat(blob.length, greaterThan(reader.getStreamFileThreshold()));
  343. }
  344. try (PackInserter ins = newInserter()) {
  345. blobId = ins.insert(OBJ_BLOB, blob);
  346. try (ObjectReader reader = ins.newReader()) {
  347. // Double-check threshold is propagated.
  348. assertThat(blob.length, greaterThan(reader.getStreamFileThreshold()));
  349. assertBlob(reader, blobId, blob);
  350. }
  351. }
  352. assertPacksOnly();
  353. // Pack was streamed out to disk and read back from the temp file, but
  354. // ultimately rolled back and deleted.
  355. assertEquals(0, listPacks().size());
  356. try (ObjectReader reader = db.newObjectReader()) {
  357. try {
  358. reader.open(blobId);
  359. fail("Expected MissingObjectException");
  360. } catch (MissingObjectException expected) {
  361. // Expected.
  362. }
  363. }
  364. }
  365. @Test
  366. public void readBackFallsBackToRepo() throws Exception {
  367. ObjectId blobId;
  368. byte[] blob = Constants.encode("foo contents");
  369. try (PackInserter ins = newInserter()) {
  370. assertThat(blob.length, lessThan(ins.getBufferSize()));
  371. blobId = ins.insert(OBJ_BLOB, blob);
  372. ins.flush();
  373. }
  374. try (PackInserter ins = newInserter();
  375. ObjectReader reader = ins.newReader()) {
  376. assertBlob(reader, blobId, blob);
  377. }
  378. }
  379. @Test
  380. public void readBackSmallObjectBeforeLargeObject() throws Exception {
  381. WindowCacheConfig wcc = new WindowCacheConfig();
  382. wcc.setStreamFileThreshold(1024);
  383. wcc.install();
  384. ObjectId blobId1;
  385. ObjectId blobId2;
  386. ObjectId largeId;
  387. byte[] blob1 = Constants.encode("blob1");
  388. byte[] blob2 = Constants.encode("blob2");
  389. byte[] largeBlob = newLargeBlob();
  390. try (PackInserter ins = newInserter()) {
  391. assertThat(blob1.length, lessThan(ins.getBufferSize()));
  392. assertThat(largeBlob.length, greaterThan(ins.getBufferSize()));
  393. blobId1 = ins.insert(OBJ_BLOB, blob1);
  394. largeId = ins.insert(OBJ_BLOB, largeBlob);
  395. try (ObjectReader reader = ins.newReader()) {
  396. // A previous bug did not reset the file pointer to EOF after reading
  397. // back. We need to seek to something further back than a full buffer,
  398. // since the read-back code eagerly reads a full buffer's worth of data
  399. // from the file to pass to the inflater. If we seeked back just a small
  400. // amount, this step would consume the rest of the file, so the file
  401. // pointer would coincidentally end up back at EOF, hiding the bug.
  402. assertBlob(reader, blobId1, blob1);
  403. }
  404. blobId2 = ins.insert(OBJ_BLOB, blob2);
  405. try (ObjectReader reader = ins.newReader()) {
  406. assertBlob(reader, blobId1, blob1);
  407. assertBlob(reader, blobId2, blob2);
  408. assertBlob(reader, largeId, largeBlob);
  409. }
  410. ins.flush();
  411. }
  412. try (ObjectReader reader = db.newObjectReader()) {
  413. assertBlob(reader, blobId1, blob1);
  414. assertBlob(reader, blobId2, blob2);
  415. assertBlob(reader, largeId, largeBlob);
  416. }
  417. }
  418. private List<PackFile> listPacks() throws Exception {
  419. List<PackFile> fromOpenDb = listPacks(db);
  420. List<PackFile> reopened;
  421. try (FileRepository db2 = new FileRepository(db.getDirectory())) {
  422. reopened = listPacks(db2);
  423. }
  424. assertEquals(fromOpenDb.size(), reopened.size());
  425. for (int i = 0 ; i < fromOpenDb.size(); i++) {
  426. PackFile a = fromOpenDb.get(i);
  427. PackFile b = reopened.get(i);
  428. assertEquals(a.getPackName(), b.getPackName());
  429. assertEquals(
  430. a.getPackFile().getAbsolutePath(), b.getPackFile().getAbsolutePath());
  431. assertEquals(a.getObjectCount(), b.getObjectCount());
  432. a.getObjectCount();
  433. }
  434. return fromOpenDb;
  435. }
  436. private static List<PackFile> listPacks(FileRepository db) throws Exception {
  437. return db.getObjectDatabase().getPacks().stream()
  438. .sorted(comparing(PackFile::getPackName)).collect(toList());
  439. }
  440. private PackInserter newInserter() {
  441. return db.getObjectDatabase().newPackInserter();
  442. }
  443. private static byte[] newLargeBlob() {
  444. byte[] blob = new byte[10240];
  445. new Random(0).nextBytes(blob);
  446. return blob;
  447. }
  448. private static String getInode(File f) throws Exception {
  449. BasicFileAttributes attrs = Files.readAttributes(
  450. f.toPath(), BasicFileAttributes.class);
  451. Object k = attrs.fileKey();
  452. if (k == null) {
  453. return null;
  454. }
  455. Pattern p = Pattern.compile("^\\(dev=[^,]*,ino=(\\d+)\\)$");
  456. Matcher m = p.matcher(k.toString());
  457. return m.matches() ? m.group(1) : null;
  458. }
  459. private static void assertBlob(ObjectReader reader, ObjectId id,
  460. byte[] expected) throws Exception {
  461. ObjectLoader loader = reader.open(id);
  462. assertEquals(OBJ_BLOB, loader.getType());
  463. assertEquals(expected.length, loader.getSize());
  464. try (ObjectStream s = loader.openStream()) {
  465. int n = (int) s.getSize();
  466. byte[] actual = new byte[n];
  467. assertEquals(n, IO.readFully(s, actual, 0));
  468. assertArrayEquals(expected, actual);
  469. }
  470. }
  471. private void assertPacksOnly() throws Exception {
  472. new BadFileCollector(f -> !f.endsWith(".pack") && !f.endsWith(".idx"))
  473. .assertNoBadFiles(db.getObjectDatabase().getDirectory());
  474. }
  475. private void assertNoObjects() throws Exception {
  476. new BadFileCollector(f -> true)
  477. .assertNoBadFiles(db.getObjectDatabase().getDirectory());
  478. }
  479. private static class BadFileCollector extends SimpleFileVisitor<Path> {
  480. private final Predicate<String> badName;
  481. private List<String> bad;
  482. BadFileCollector(Predicate<String> badName) {
  483. this.badName = badName;
  484. }
  485. void assertNoBadFiles(File f) throws IOException {
  486. bad = new ArrayList<>();
  487. Files.walkFileTree(f.toPath(), this);
  488. if (!bad.isEmpty()) {
  489. fail("unexpected files in object directory: " + bad);
  490. }
  491. }
  492. @Override
  493. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
  494. String name = file.getFileName().toString();
  495. if (!attrs.isDirectory() && badName.test(name)) {
  496. bad.add(name);
  497. }
  498. return FileVisitResult.CONTINUE;
  499. }
  500. }
  501. }