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 4.8KB

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