Skip to content

Commit

Permalink
Added metrics to DeviceSchemaCache's memory and entries num (#14826)
Browse files Browse the repository at this point in the history
* Partial

* partial

* put

* partial
  • Loading branch information
Caideyipi authored Feb 13, 2025
1 parent 29b9ead commit eed0a46
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ public interface IDualKeyCacheStats {

/** Return current memory usage of dual key cache. */
long memoryUsage();

/** Return capacity of dual key cache. */
long capacity();

/** Return entries num of dual key cache */
long entriesCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CacheStats implements IDualKeyCacheStats {
private final long memoryThreshold;

private final AtomicLong memoryUsage = new AtomicLong(0);
private final AtomicLong entriesCount = new AtomicLong(0);

private final AtomicLong requestCount = new AtomicLong(0);
private final AtomicLong hitCount = new AtomicLong(0);
Expand Down Expand Up @@ -68,6 +69,14 @@ void recordMiss(int num) {
requestCount.getAndAdd(num);
}

void increaseEntryCount() {
entriesCount.incrementAndGet();
}

void decreaseEntryCount() {
entriesCount.decrementAndGet();
}

@Override
public long requestCount() {
return requestCount.get();
Expand Down Expand Up @@ -96,13 +105,24 @@ public long memoryUsage() {
return memoryUsage.get();
}

@Override
public long capacity() {
return memoryThreshold;
}

@Override
public long entriesCount() {
return entriesCount.get();
}

void reset() {
resetMemoryUsage();
resetMemoryUsageAndEntriesCount();
hitCount.set(0);
requestCount.set(0);
}

void resetMemoryUsage() {
void resetMemoryUsageAndEntriesCount() {
memoryUsage.set(0);
entriesCount.set(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public void put(final FK firstKey, final SK secondKey, final V value) {
cacheEntry =
cacheEntryManager.createCacheEntry(secondKey, value, finalCacheEntryGroup);
cacheEntryManager.put(cacheEntry);
cacheStats.increaseEntryCount();
usedMemorySize.getAndAdd(sizeComputer.computeSecondKeySize(sk));
} else {
final V existingValue = cacheEntry.getValue();
Expand Down Expand Up @@ -208,6 +209,7 @@ public void update(
cacheEntryManager.createCacheEntry(
secondKey, value, finalCacheEntryGroup);
cacheEntryManager.put(entry);
cacheStats.increaseEntryCount();
usedMemorySize.getAndAdd(
sizeComputer.computeSecondKeySize(sk)
+ sizeComputer.computeValueSize(entry.getValue()));
Expand Down Expand Up @@ -304,6 +306,7 @@ private int evictOneCacheEntry() {
final ICacheEntryGroup<FK, SK, V, T> belongedGroup = evictCacheEntry.getBelongedGroup();
evictCacheEntry.setBelongedGroup(null);
belongedGroup.removeCacheEntry(evictCacheEntry.getSecondKey());
cacheStats.decreaseEntryCount();
evictedSize.getAndAdd(sizeComputer.computeSecondKeySize(evictCacheEntry.getSecondKey()));

if (belongedGroup.isEmpty()) {
Expand Down Expand Up @@ -334,7 +337,7 @@ public void invalidateAll() {
private void executeInvalidateAll() {
firstKeyMap.clear();
cacheEntryManager.cleanUp();
cacheStats.resetMemoryUsage();
cacheStats.resetMemoryUsageAndEntriesCount();
}

@Override
Expand Down Expand Up @@ -364,6 +367,7 @@ public void invalidate(final FK firstKey) {
it.hasNext(); ) {
final Map.Entry<SK, T> entry = it.next();
if (cacheEntryManager.invalidate(entry.getValue())) {
cacheStats.decreaseEntryCount();
estimateSize +=
sizeComputer.computeSecondKeySize(entry.getKey())
+ sizeComputer.computeValueSize(entry.getValue().getValue());
Expand All @@ -387,6 +391,7 @@ public void invalidate(final FK firstKey, final SK secondKey) {

final T entry = cacheEntryGroup.getCacheEntry(secondKey);
if (Objects.nonNull(entry) && cacheEntryManager.invalidate(entry)) {
cacheStats.decreaseEntryCount();
usedMemorySize.getAndAdd(
sizeComputer.computeSecondKeySize(entry.getSecondKey())
+ sizeComputer.computeValueSize(entry.getValue()));
Expand Down Expand Up @@ -418,6 +423,7 @@ public void invalidate(final FK firstKey, final Predicate<SK> secondKeyChecker)
it.hasNext(); ) {
final Map.Entry<SK, T> entry = it.next();
if (cacheEntryManager.invalidate(entry.getValue())) {
cacheStats.decreaseEntryCount();
cacheEntryGroup.removeCacheEntry(entry.getKey());
estimateSize.addAndGet(
sizeComputer.computeSecondKeySize(entry.getKey())
Expand Down Expand Up @@ -452,6 +458,7 @@ public void invalidate(
}

if (cacheEntryManager.invalidate(entry.getValue())) {
cacheStats.decreaseEntryCount();
entryGroup.removeCacheEntry(entry.getKey());
estimateSize.addAndGet(
sizeComputer.computeSecondKeySize(entry.getKey())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,18 @@ long getRequestCount() {
return dualKeyCache.stats().requestCount();
}

long getMemoryUsage() {
return dualKeyCache.stats().memoryUsage();
}

long capacity() {
return dualKeyCache.stats().capacity();
}

long entriesCount() {
return dualKeyCache.stats().entriesCount();
}

void invalidateLastCache(final @Nonnull String database) {
readWriteLock.writeLock().lock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import java.util.Objects;

public class TableDeviceSchemaCacheMetrics implements IMetricSet {

private static final String SCHEMA_CACHE_TOTAL_USAGE = "schema_cache_total_usage";
private static final String SCHEMA_CACHE_MEM_CAPACITY = "schema_cache_mem_capacity";

private final TableDeviceSchemaCache tableDeviceSchemaCache;

public TableDeviceSchemaCacheMetrics(final TableDeviceSchemaCache dataNodeSchemaCache) {
Expand All @@ -55,6 +59,25 @@ public void bindTo(final AbstractMetricService metricService) {
"SchemaCache",
Tag.TYPE.toString(),
"all");
metricService.createAutoGauge(
Metric.MEM.toString(),
MetricLevel.IMPORTANT,
tableDeviceSchemaCache,
TableDeviceSchemaCache::getMemoryUsage,
Tag.NAME.toString(),
SCHEMA_CACHE_TOTAL_USAGE);
metricService.createAutoGauge(
Metric.MEM.toString(),
MetricLevel.IMPORTANT,
tableDeviceSchemaCache,
TableDeviceSchemaCache::capacity,
Tag.NAME.toString(),
SCHEMA_CACHE_MEM_CAPACITY);
metricService.createAutoGauge(
Metric.CACHE_ENTRIES_NUM.toString(),
MetricLevel.IMPORTANT,
tableDeviceSchemaCache,
TableDeviceSchemaCache::entriesCount);
}

@Override
Expand All @@ -73,6 +96,17 @@ public void unbindFrom(final AbstractMetricService metricService) {
"SchemaCache",
Tag.TYPE.toString(),
"all");
metricService.remove(
MetricType.AUTO_GAUGE,
Metric.MEM.toString(),
Tag.NAME.toString(),
SCHEMA_CACHE_TOTAL_USAGE);
metricService.remove(
MetricType.AUTO_GAUGE,
Metric.MEM.toString(),
Tag.NAME.toString(),
SCHEMA_CACHE_MEM_CAPACITY);
metricService.remove(MetricType.AUTO_GAUGE, Metric.CACHE_ENTRIES_NUM.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public enum Metric {
MEM("mem"),
CACHE("cache"),
CACHE_HIT_RATE("cache_hit"),
CACHE_ENTRIES_NUM("cache_entries_num"),
QUANTITY("quantity"),
LEADER_QUANTITY("leader_quantity"),
SCHEMA_REGION("schema_region"),
Expand Down

0 comments on commit eed0a46

Please sign in to comment.