--- /dev/null
+package org.eclipse.jgit.internal.storage.dfs;
+
+import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheConfig.DEFAULT_NAME;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+import org.junit.Test;
+
+public class ClockBlockCacheTableTest {
+ private static final String NAME = "name";
+
+ @Test
+ public void getName_nameNotConfigured_returnsDefaultName() {
+ ClockBlockCacheTable cacheTable = new ClockBlockCacheTable(
+ createBlockCacheConfig());
+
+ assertThat(cacheTable.getName(), equalTo(DEFAULT_NAME));
+ }
+
+ @Test
+ public void getName_nameConfigured_returnsConfiguredName() {
+ ClockBlockCacheTable cacheTable = new ClockBlockCacheTable(
+ createBlockCacheConfig().setName(NAME));
+
+ assertThat(cacheTable.getName(), equalTo(NAME));
+ }
+
+ @Test
+ public void getBlockCacheStats_nameNotConfigured_returnsBlockCacheStatsWithDefaultName() {
+ ClockBlockCacheTable cacheTable = new ClockBlockCacheTable(
+ createBlockCacheConfig());
+
+ assertThat(cacheTable.getBlockCacheStats().getName(),
+ equalTo(DEFAULT_NAME));
+ }
+
+ @Test
+ public void getBlockCacheStats_nameConfigured_returnsBlockCacheStatsWithConfiguredName() {
+ ClockBlockCacheTable cacheTable = new ClockBlockCacheTable(
+ createBlockCacheConfig().setName(NAME));
+
+ assertThat(cacheTable.getBlockCacheStats().getName(), equalTo(NAME));
+ }
+
+ private static DfsBlockCacheConfig createBlockCacheConfig() {
+ return new DfsBlockCacheConfig().setBlockSize(512)
+ .setConcurrencyLevel(4).setBlockLimit(1024);
+ }
+}
\ No newline at end of file
package org.eclipse.jgit.internal.storage.dfs;
+import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheConfig.DEFAULT_NAME;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_CACHE_PREFIX;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
+import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;
is(indexConfig));
}
+ @Test
+ public void fromConfigs_baseConfigOnly_nameSetFromConfigDfsSubSection() {
+ Config config = new Config();
+
+ DfsBlockCacheConfig blockCacheConfig = new DfsBlockCacheConfig()
+ .fromConfig(config);
+ assertThat(blockCacheConfig.getName(), equalTo(DEFAULT_NAME));
+ }
+
+ @Test
+ public void fromConfigs_namesSetFromConfigDfsCachePrefixSubSections() {
+ Config config = new Config();
+ config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
+ CONFIG_KEY_STREAM_RATIO, "0.5");
+ config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "name1",
+ CONFIG_KEY_PACK_EXTENSIONS, PackExt.PACK.name());
+ config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "name2",
+ CONFIG_KEY_PACK_EXTENSIONS, PackExt.BITMAP_INDEX.name());
+
+ DfsBlockCacheConfig blockCacheConfig = new DfsBlockCacheConfig()
+ .fromConfig(config);
+ assertThat(blockCacheConfig.getName(), equalTo("dfs"));
+ assertThat(
+ blockCacheConfig.getPackExtCacheConfigurations().get(0)
+ .getPackExtCacheConfiguration().getName(),
+ equalTo("dfs.name1"));
+ assertThat(
+ blockCacheConfig.getPackExtCacheConfigurations().get(1)
+ .getPackExtCacheConfiguration().getName(),
+ equalTo("dfs.name2"));
+ }
+
@Test
public void fromConfigs_dfsBlockCachePackExtConfigWithDuplicateExtensions_throws() {
Config config = new Config();
package org.eclipse.jgit.internal.storage.dfs;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
@SuppressWarnings({ "boxing", "unchecked" })
public class PackExtBlockCacheTableTest {
+ private static final String CACHE_NAME = "CacheName";
+
@Test
public void fromBlockCacheConfigs_createsDfsPackExtBlockCacheTables() {
DfsBlockCacheConfig cacheConfig = new DfsBlockCacheConfig();
assertThat(tables.get(dfsStreamKey, 0), sameInstance(ref));
}
+ @Test
+ public void getName() {
+ DfsBlockCacheStats packStats = new DfsBlockCacheStats();
+ PackExtBlockCacheTable tables = PackExtBlockCacheTable.fromCacheTables(
+ cacheTableWithStats(/* name= */ "defaultName", packStats),
+ Map.of(PackExt.PACK, cacheTableWithStats(/* name= */ "packName",
+ packStats)));
+
+ assertThat(tables.getName(), equalTo("defaultName,packName"));
+ }
+
+ @Test
+ public void getBlockCacheStats_getName_returnsPackExtCacheTableName() {
+ DfsBlockCacheStats packStats = new DfsBlockCacheStats();
+ PackExtBlockCacheTable tables = PackExtBlockCacheTable.fromCacheTables(
+ cacheTableWithStats(/* name= */ "defaultName", packStats),
+ Map.of(PackExt.PACK, cacheTableWithStats(/* name= */ "packName",
+ packStats)));
+
+ assertThat(tables.getBlockCacheStats().getName(),
+ equalTo("defaultName,packName"));
+ }
+
@Test
public void getBlockCacheStats_getCurrentSize_consolidatesAllTableCurrentSizes() {
long[] currentSizes = createEmptyStatsArray();
private static DfsBlockCacheTable cacheTableWithStats(
DfsBlockCacheStats dfsBlockCacheStats) {
+ return cacheTableWithStats(CACHE_NAME, dfsBlockCacheStats);
+ }
+
+ private static DfsBlockCacheTable cacheTableWithStats(String name,
+ DfsBlockCacheStats dfsBlockCacheStats) {
DfsBlockCacheTable cacheTable = mock(DfsBlockCacheTable.class);
+ when(cacheTable.getName()).thenReturn(name);
when(cacheTable.getBlockCacheStats()).thenReturn(dfsBlockCacheStats);
return cacheTable;
}
* invocations is also fixed in size.
*/
final class ClockBlockCacheTable implements DfsBlockCacheTable {
+ /**
+ * Table name.
+ */
+ private final String name;
+
/** Number of entries in {@link #table}. */
private final int tableSize;
-1, 0, null);
clockHand.next = clockHand;
- this.dfsBlockCacheStats = new DfsBlockCacheStats();
+ this.name = cfg.getName();
+ this.dfsBlockCacheStats = new DfsBlockCacheStats(this.name);
this.refLockWaitTime = cfg.getRefLockWaitTimeConsumer();
this.indexEventConsumer = cfg.getIndexEventConsumer();
}
return dfsBlockCacheStats;
}
+ @Override
+ public String getName() {
+ return name;
+ }
+
@Override
public boolean hasBlock0(DfsStreamKey key) {
HashEntry e1 = table.get(slot(key, 0));
/** Default number of max cache hits. */
public static final int DEFAULT_CACHE_HOT_MAX = 1;
+ static final String DEFAULT_NAME = "<default>";
+
+ private String name;
+
private long blockLimit;
private int blockSize;
* Create a default configuration.
*/
public DfsBlockCacheConfig() {
+ name = DEFAULT_NAME;
setBlockLimit(32 * MB);
setBlockSize(64 * KB);
setStreamRatio(0.30);
packExtCacheConfigurations = Collections.emptyList();
}
+ /**
+ * Get the name for the block cache configured by this cache config.
+ *
+ * @return the name for the block cache configured by this cache config.
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Set the name for the block cache configured by this cache config.
+ * <p>
+ * Made visible for testing.
+ *
+ * @param name
+ * the name for the block cache configured by this cache config.
+ * @return {@code this}
+ */
+ DfsBlockCacheConfig setName(String name) {
+ this.name = name;
+ return this;
+ }
+
/**
* Get maximum number bytes of heap memory to dedicate to caching pack file
* data.
Long.valueOf(cfgBlockLimit), Long.valueOf(cfgBlockSize)));
}
+ // Set name only if `core dfs` is configured, otherwise fall back to the
+ // default.
+ if (rc.getSubsections(section).contains(subSection)) {
+ this.name = subSection;
+ }
setBlockLimit(cfgBlockLimit);
setBlockSize(cfgBlockSize);
*/
BlockCacheStats getBlockCacheStats();
+ /**
+ * Get the name of the table.
+ *
+ * @return this table's name.
+ */
+ String getName();
+
/**
* Provides methods used with Block Cache statistics.
*/
interface BlockCacheStats {
+ String getName();
+
/**
* Get total number of bytes in the cache, per pack file extension.
*
* Keeps track of stats for a Block Cache table.
*/
class DfsBlockCacheStats implements BlockCacheStats {
+ private final String name;
+
/**
* Number of times a block was found in the cache, per pack file
* extension.
private final AtomicReference<AtomicLong[]> liveBytes;
DfsBlockCacheStats() {
+ this("");
+ }
+
+ DfsBlockCacheStats(String name) {
+ this.name = name;
statHit = new AtomicReference<>(newCounters());
statMiss = new AtomicReference<>(newCounters());
statEvict = new AtomicReference<>(newCounters());
liveBytes = new AtomicReference<>(newCounters());
}
+ @Override
+ public String getName() {
+ return name;
+ }
+
/**
* Increment the {@code statHit} count.
*
* type.
*/
class PackExtBlockCacheTable implements DfsBlockCacheTable {
+ /**
+ * Table name.
+ */
+ private final String name;
+
private final DfsBlockCacheTable defaultBlockCacheTable;
// Holds the unique tables backing the extBlockCacheTables values.
Set<DfsBlockCacheTable> blockCacheTables = new HashSet<>();
blockCacheTables.add(defaultBlockCacheTable);
blockCacheTables.addAll(packExtBlockCacheTables.values());
- return new PackExtBlockCacheTable(defaultBlockCacheTable,
+ String name = defaultBlockCacheTable.getName() + ","
+ + packExtBlockCacheTables.values().stream()
+ .map(DfsBlockCacheTable::getName).sorted()
+ .collect(Collectors.joining(","));
+ return new PackExtBlockCacheTable(name, defaultBlockCacheTable,
List.copyOf(blockCacheTables), packExtBlockCacheTables);
}
- private PackExtBlockCacheTable(DfsBlockCacheTable defaultBlockCacheTable,
+ private PackExtBlockCacheTable(String name,
+ DfsBlockCacheTable defaultBlockCacheTable,
List<DfsBlockCacheTable> blockCacheTableList,
Map<PackExt, DfsBlockCacheTable> extBlockCacheTables) {
+ this.name = name;
this.defaultBlockCacheTable = defaultBlockCacheTable;
this.blockCacheTableList = blockCacheTableList;
this.extBlockCacheTables = extBlockCacheTables;
@Override
public BlockCacheStats getBlockCacheStats() {
- return new CacheStats(blockCacheTableList.stream()
- .map(DfsBlockCacheTable::getBlockCacheStats)
- .collect(Collectors.toList()));
+ return new CacheStats(name,
+ blockCacheTableList.stream()
+ .map(DfsBlockCacheTable::getBlockCacheStats)
+ .collect(Collectors.toList()));
+ }
+
+ @Override
+ public String getName() {
+ return name;
}
private DfsBlockCacheTable getTable(PackExt packExt) {
}
private static class CacheStats implements BlockCacheStats {
+ private final String name;
+
private final List<BlockCacheStats> blockCacheStats;
- private CacheStats(List<BlockCacheStats> blockCacheStats) {
+ private CacheStats(String name, List<BlockCacheStats> blockCacheStats) {
+ this.name = name;
this.blockCacheStats = blockCacheStats;
}
+ @Override
+ public String getName() {
+ return name;
+ }
+
@Override
public long[] getCurrentSize() {
long[] sums = emptyPackStats();