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.

FileReftableStackTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2019 Google LLC and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertThrows;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.File;
  16. import java.io.FileNotFoundException;
  17. import java.io.IOException;
  18. import java.nio.file.Files;
  19. import java.nio.file.Path;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.stream.Collectors;
  23. import org.eclipse.jgit.internal.storage.file.FileReftableStack.Segment;
  24. import org.eclipse.jgit.internal.storage.reftable.MergedReftable;
  25. import org.eclipse.jgit.internal.storage.reftable.RefCursor;
  26. import org.eclipse.jgit.lib.Config;
  27. import org.eclipse.jgit.lib.ObjectId;
  28. import org.eclipse.jgit.lib.ObjectIdRef;
  29. import org.eclipse.jgit.lib.Ref;
  30. import org.eclipse.jgit.util.FileUtils;
  31. import org.junit.After;
  32. import org.junit.Before;
  33. import org.junit.Test;
  34. public class FileReftableStackTest {
  35. private static Ref newRef(String name, ObjectId id) {
  36. return new ObjectIdRef.PeeledNonTag(PACKED, name, id);
  37. }
  38. private File reftableDir;
  39. @Before
  40. public void setup() throws Exception {
  41. reftableDir = FileUtils.createTempDir("rtstack", "", null);
  42. }
  43. @After
  44. public void tearDown() throws Exception {
  45. if (reftableDir != null) {
  46. FileUtils.delete(reftableDir, FileUtils.RECURSIVE);
  47. }
  48. }
  49. void writeBranches(FileReftableStack stack, String template, int start,
  50. int N) throws IOException {
  51. for (int i = 0; i < N; i++) {
  52. while (true) {
  53. final long next = stack.getMergedReftable().maxUpdateIndex()
  54. + 1;
  55. String name = String.format(template,
  56. Integer.valueOf(start + i));
  57. Ref r = newRef(name, ObjectId.zeroId());
  58. boolean ok = stack.addReftable(rw -> {
  59. rw.setMinUpdateIndex(next).setMaxUpdateIndex(next).begin()
  60. .writeRef(r);
  61. });
  62. if (ok) {
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. public void testCompaction(int N) throws Exception {
  69. try (FileReftableStack stack = new FileReftableStack(
  70. new File(reftableDir, "refs"), reftableDir, null,
  71. () -> new Config())) {
  72. writeBranches(stack, "refs/heads/branch%d", 0, N);
  73. MergedReftable table = stack.getMergedReftable();
  74. for (int i = 1; i < N; i++) {
  75. String name = String.format("refs/heads/branch%d",
  76. Integer.valueOf(i));
  77. RefCursor c = table.seekRef(name);
  78. assertTrue(c.next());
  79. assertEquals(ObjectId.zeroId(), c.getRef().getObjectId());
  80. }
  81. List<String> files = Files.list(reftableDir.toPath())
  82. .map(Path::getFileName).map(Path::toString)
  83. .collect(Collectors.toList());
  84. Collections.sort(files);
  85. assertTrue(files.size() < 20);
  86. FileReftableStack.CompactionStats stats = stack.getStats();
  87. assertEquals(0, stats.failed);
  88. assertTrue(stats.attempted < N);
  89. assertTrue(stats.refCount < FileReftableStack.log(N) * N);
  90. }
  91. }
  92. @Test
  93. public void testCompaction9() throws Exception {
  94. testCompaction(9);
  95. }
  96. @Test
  97. public void testCompaction1024() throws Exception {
  98. testCompaction(1024);
  99. }
  100. @SuppressWarnings({ "resource", "unused" })
  101. @Test
  102. public void missingReftable() throws Exception {
  103. try (FileReftableStack stack = new FileReftableStack(
  104. new File(reftableDir, "refs"), reftableDir, null,
  105. () -> new Config())) {
  106. outer: for (int i = 0; i < 10; i++) {
  107. final long next = stack.getMergedReftable().maxUpdateIndex()
  108. + 1;
  109. String name = String.format("branch%d", Integer.valueOf(i));
  110. Ref r = newRef(name, ObjectId.zeroId());
  111. boolean ok = stack.addReftable(rw -> {
  112. rw.setMinUpdateIndex(next).setMaxUpdateIndex(next).begin()
  113. .writeRef(r);
  114. });
  115. assertTrue(ok);
  116. List<Path> files = Files.list(reftableDir.toPath())
  117. .collect(Collectors.toList());
  118. for (int j = 0; j < files.size(); j++) {
  119. Path f = files.get(j);
  120. Path fileName = f.getFileName();
  121. if (fileName != null
  122. && fileName.toString().endsWith(".ref")) {
  123. Files.delete(f);
  124. break outer;
  125. }
  126. }
  127. }
  128. }
  129. assertThrows(FileNotFoundException.class,
  130. () -> new FileReftableStack(new File(reftableDir, "refs"),
  131. reftableDir, null, () -> new Config()));
  132. }
  133. @Test
  134. public void testSegments() {
  135. long in[] = { 1024, 1024, 1536, 100, 64, 50, 25, 24 };
  136. List<Segment> got = FileReftableStack.segmentSizes(in);
  137. Segment want[] = { new Segment(0, 3, 10, 3584),
  138. new Segment(3, 5, 6, 164), new Segment(5, 6, 5, 50),
  139. new Segment(6, 8, 4, 49), };
  140. assertEquals(got.size(), want.length);
  141. for (int i = 0; i < want.length; i++) {
  142. assertTrue(want[i].equals(got.get(i)));
  143. }
  144. }
  145. @Test
  146. public void testLog2() throws Exception {
  147. assertEquals(10, FileReftableStack.log(1024));
  148. assertEquals(10, FileReftableStack.log(1025));
  149. assertEquals(10, FileReftableStack.log(2047));
  150. }
  151. }