Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions server/src/main/java/io/druid/client/cache/CacheMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,48 @@
import com.metamx.emitter.service.ServiceMetricEvent;
import com.metamx.metrics.AbstractMonitor;

/**
*/
public class CacheMonitor extends AbstractMonitor
{
private final Cache cache;
// package private for tests
volatile Cache cache;

private volatile CacheStats prevCacheStats = null;

@Inject
public CacheMonitor()
{
}

public CacheMonitor(
Cache cache
)
{
this.cache = cache;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this constructor still needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if someone uses it directly, yes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a big deal but,
i meant, is anything actually using this constructor anymore. If not, then we can remove it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not within Druid at least. Fine to remove, unless we want to preserve binary compatibility for extensions that might bind it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if they are binding via guice then this shouldn't break anything.
I would remove it.
It is up to you though, I'm fine either ways :)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 agent-workflow — Working on it

This is a valid concern. The cached review_decision field is only refreshed during the auto-detect block when WORKFLOW_RESULT is empty, but after COMPLETE is set, the cached value can become stale. A fix is in progress to refresh the review decision before the BLOCKED check in the staleness guard.


Automated reply — not a human reviewer.


// make it possible to enable CacheMonitor even if cache is not bound
// (e.g. some index tasks may have a cache, others may not)
@Inject(optional = true)
public void setCache(Cache cache)
{
this.cache = cache;
}

@Override
public boolean doMonitor(ServiceEmitter emitter)
{
final CacheStats currCacheStats = cache.getStats();
final CacheStats deltaCacheStats = currCacheStats.delta(prevCacheStats);
if (cache != null) {
final CacheStats currCacheStats = cache.getStats();
final CacheStats deltaCacheStats = currCacheStats.delta(prevCacheStats);

final ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder();
emitStats(emitter, "query/cache/delta", deltaCacheStats, builder);
emitStats(emitter, "query/cache/total", currCacheStats, builder);
final ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder();
emitStats(emitter, "query/cache/delta", deltaCacheStats, builder);
emitStats(emitter, "query/cache/total", currCacheStats, builder);

prevCacheStats = currCacheStats;
prevCacheStats = currCacheStats;

// Any custom cache statistics that need monitoring
cache.doMonitor(emitter);
// Any custom cache statistics that need monitoring
cache.doMonitor(emitter);
}
return true;
}

Expand All @@ -62,14 +74,16 @@ private void emitStats(
ServiceMetricEvent.Builder builder
)
{
emitter.emit(builder.build(String.format("%s/numEntries", metricPrefix), cacheStats.getNumEntries()));
emitter.emit(builder.build(String.format("%s/sizeBytes", metricPrefix), cacheStats.getSizeInBytes()));
emitter.emit(builder.build(String.format("%s/hits", metricPrefix), cacheStats.getNumHits()));
emitter.emit(builder.build(String.format("%s/misses", metricPrefix), cacheStats.getNumMisses()));
emitter.emit(builder.build(String.format("%s/evictions", metricPrefix), cacheStats.getNumEvictions()));
emitter.emit(builder.build(String.format("%s/hitRate", metricPrefix), cacheStats.hitRate()));
emitter.emit(builder.build(String.format("%s/averageBytes", metricPrefix), cacheStats.averageBytes()));
emitter.emit(builder.build(String.format("%s/timeouts", metricPrefix), cacheStats.getNumTimeouts()));
emitter.emit(builder.build(String.format("%s/errors", metricPrefix), cacheStats.getNumErrors()));
if (cache != null) {
emitter.emit(builder.build(String.format("%s/numEntries", metricPrefix), cacheStats.getNumEntries()));
emitter.emit(builder.build(String.format("%s/sizeBytes", metricPrefix), cacheStats.getSizeInBytes()));
emitter.emit(builder.build(String.format("%s/hits", metricPrefix), cacheStats.getNumHits()));
emitter.emit(builder.build(String.format("%s/misses", metricPrefix), cacheStats.getNumMisses()));
emitter.emit(builder.build(String.format("%s/evictions", metricPrefix), cacheStats.getNumEvictions()));
emitter.emit(builder.build(String.format("%s/hitRate", metricPrefix), cacheStats.hitRate()));
emitter.emit(builder.build(String.format("%s/averageBytes", metricPrefix), cacheStats.averageBytes()));
emitter.emit(builder.build(String.format("%s/timeouts", metricPrefix), cacheStats.getNumTimeouts()));
emitter.emit(builder.build(String.format("%s/errors", metricPrefix), cacheStats.getNumErrors()));
}
}
}
75 changes: 75 additions & 0 deletions server/src/test/java/io/druid/client/cache/CacheMonitorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.client.cache;

import com.google.common.collect.ImmutableList;
import com.google.inject.Binder;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import io.druid.guice.GuiceInjectors;
import io.druid.guice.JsonConfigProvider;
import io.druid.guice.annotations.Self;
import io.druid.initialization.Initialization;
import io.druid.server.DruidNode;
import org.junit.Assert;
import org.junit.Test;

public class CacheMonitorTest
{
@Test
public void testOptionalInject() throws Exception
{
Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(
new Module() {
@Override
public void configure(Binder binder)
{
JsonConfigProvider.bindInstance(
binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null)
);
}
}
));

CacheMonitor monitor = injector.getInstance(CacheMonitor.class);
Assert.assertNull(monitor.cache);
}

@Test
public void testInject() throws Exception
{
Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(
new Module() {
@Override
public void configure(Binder binder)
{
JsonConfigProvider.bindInstance(
binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null)
);
binder.bind(Cache.class).toInstance(MapCache.create(0));
}
}
));

CacheMonitor monitor = injector.getInstance(CacheMonitor.class);
Assert.assertNotNull(monitor.cache);
}
}