Skip to content

Commit bfbced6

Browse files
Keep writeCount as is and introduce a miss count.
1 parent bd33e89 commit bfbced6

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/BlobCacheMetrics.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class BlobCacheMetrics {
4040
private final LongCounter cachePopulationBytes;
4141
private final LongCounter cachePopulationTime;
4242

43-
private final LongAdder writeCount = new LongAdder();
43+
private final LongAdder missCount = new LongAdder();
4444
private final LongAdder readCount = new LongAdder();
4545

4646
public enum CachePopulationReason {
@@ -114,7 +114,7 @@ public BlobCacheMetrics(MeterRegistry meterRegistry) {
114114
"es.blob_cache.miss.total",
115115
"The number of cache misses (warming not included)",
116116
"count",
117-
() -> new LongWithAttributes(writeCount.longValue())
117+
() -> new LongWithAttributes(missCount.longValue())
118118
);
119119
// adding this helps search for high or low miss ratio. It will be since boot of the node though. More advanced queries can use
120120
// deltas of the totals to see miss ratio over time.
@@ -123,7 +123,7 @@ public BlobCacheMetrics(MeterRegistry meterRegistry) {
123123
"The fraction of cache reads that missed data (warming not included)",
124124
"fraction",
125125
// read misses before reads on purpose
126-
() -> new DoubleWithAttributes(Math.min((double) writeCount.longValue() / Math.max(readCount.longValue(), 1L), 1.0d))
126+
() -> new DoubleWithAttributes(Math.min((double) missCount.longValue() / Math.max(readCount.longValue(), 1L), 1.0d))
127127
);
128128
}
129129

@@ -205,17 +205,8 @@ public void recordRead() {
205205
readCount.increment();
206206
}
207207

208-
public void recordWrite() {
209-
writeCount.increment();
210-
}
211-
212-
public void recordWrites(long count) {
213-
assert count > 0;
214-
writeCount.add(count);
215-
}
216-
217-
public long writeCount() {
218-
return writeCount.sum();
208+
public void recordMiss() {
209+
missCount.increment();
219210
}
220211

221212
public long readCount() {

x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/shared/SharedBlobCacheService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ private CacheEntry(T chunk) {
325325

326326
private final ConcurrentHashMap<SharedBytes.IO, CacheFileRegion<KeyType>> regionOwners; // to assert exclusive access of regions
327327

328+
private final LongAdder writeCount = new LongAdder();
328329
private final LongAdder writeBytes = new LongAdder();
329330

330331
private final LongAdder readBytes = new LongAdder();
@@ -737,7 +738,7 @@ public Stats getStats() {
737738
cacheSize,
738739
regionSize,
739740
evictCount.sum(),
740-
blobCacheMetrics.writeCount(),
741+
writeCount.sum(),
741742
writeBytes.sum(),
742743
blobCacheMetrics.readCount(),
743744
readBytes.sum()
@@ -1170,6 +1171,7 @@ private Runnable fillGapRunnable(
11701171
l.<Void>map(unused -> {
11711172
assert blobCacheService.regionOwners.get(ioRef) == CacheFileRegion.this;
11721173
assert CacheFileRegion.this.hasReferences() : CacheFileRegion.this;
1174+
blobCacheService.writeCount.increment();
11731175
gap.onCompletion();
11741176
return null;
11751177
}).delegateResponse((delegate, e) -> failGapAndListener(gap, delegate, e))
@@ -1242,7 +1244,7 @@ public boolean tryRead(ByteBuffer buf, long offset) throws IOException {
12421244
lastAccessedRegion = res ? fileRegion : null;
12431245
if (res && incrementReads) {
12441246
blobCacheMetrics.recordRead();
1245-
readBytes.add(end - offset);
1247+
// todo: should we add to readBytes? readBytes.add(end - offset);
12461248
}
12471249
return res;
12481250
}
@@ -1425,7 +1427,7 @@ private RangeMissingHandler metricRecordingWriter(RangeMissingHandler writer) {
14251427
return new DelegatingRangeMissingHandler(writer) {
14261428
@Override
14271429
public SourceInputStreamFactory sharedInputStreamFactory(List<SparseFileTracker.Gap> gaps) {
1428-
blobCacheMetrics.recordWrite();
1430+
blobCacheMetrics.recordMiss();
14291431
return super.sharedInputStreamFactory(gaps);
14301432
}
14311433
};

x-pack/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/BlobCacheMetricsTests.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,17 @@ public void testRecordCachePopulationMetricsRecordsThroughput() {
7171
// let us check for 0, avoid div by 0.
7272
checkReadsAndMisses(0, 0, 1);
7373
int reads = between(1, 100);
74-
int writes = between(1, reads);
75-
metrics.recordWrites(writes);
76-
checkReadsAndMisses(0, writes, writes);
74+
int misses = between(1, reads);
75+
recordMisses(metrics, misses);
76+
checkReadsAndMisses(0, misses, misses);
7777
IntStream.range(0, reads).forEach(i -> metrics.recordRead());
78-
checkReadsAndMisses(reads, writes, reads);
79-
metrics.recordWrites(reads);
80-
checkReadsAndMisses(reads, writes + reads, writes + reads);
78+
checkReadsAndMisses(reads, misses, reads);
79+
recordMisses(metrics, reads);
80+
checkReadsAndMisses(reads, misses + reads, misses + reads);
81+
}
82+
83+
private void recordMisses(BlobCacheMetrics metrics, int misses) {
84+
IntStream.range(0, misses).forEach(i -> metrics.recordMiss());
8185
}
8286

8387
private void checkReadsAndMisses(int reads, int writes, int readsForRatio) {

0 commit comments

Comments
 (0)