Skip to content

Commit 9c30e0e

Browse files
tihom88reschke
authored andcommitted
OAK-11750: Add flag to enable/disable inference stats collection (#2322)
* OAK-11750: Update inference stats name * OAK-11750: Update inference stats name * OAK-11750: Added a flag to disable collection of inference stats * OAK-11750: Added a flag to disable collection of inference stats * OAK-11750: test updated * OAK-11750: test updated
1 parent 35a6c84 commit 9c30e0e

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class ElasticIndexProviderService {
7575
protected static final String PROP_ELASTIC_API_KEY_SECRET = "elasticsearch.apiKeySecret";
7676
protected static final String PROP_LOCAL_TEXT_EXTRACTION_DIR = "localTextExtractionDir";
7777
private static final boolean DEFAULT_IS_INFERENCE_ENABLED = false;
78+
private static final String ENV_VAR_OAK_INFERENCE_STATISTICS_DISABLED = "OAK_INFERENCE_STATISTICS_DISABLED";
7879

7980
@ObjectClassDefinition(name = "ElasticIndexProviderService", description = "Apache Jackrabbit Oak ElasticIndexProvider")
8081
public @interface Config {
@@ -190,7 +191,12 @@ private void activate(BundleContext bundleContext, Config config) {
190191
} else {
191192
this.isInferenceEnabled = config.isInferenceEnabled();
192193
}
193-
InferenceConfig.reInitialize(nodeStore, statisticsProvider, config.inferenceConfigPath(), isInferenceEnabled);
194+
195+
if (isInferenceStatisticsDisabled()) {
196+
InferenceConfig.reInitialize(nodeStore, config.inferenceConfigPath(), isInferenceEnabled);
197+
} else {
198+
InferenceConfig.reInitialize(nodeStore, statisticsProvider, config.inferenceConfigPath(), isInferenceEnabled);
199+
}
194200

195201
//initializeTextExtractionDir(bundleContext, config);
196202
//initializeExtractedTextCache(config, statisticsProvider);
@@ -297,4 +303,12 @@ private ElasticConnection getElasticConnection(Config contextConfig) {
297303
public InferenceConfig getInferenceConfig() {
298304
return InferenceConfig.getInstance();
299305
}
306+
307+
/**
308+
* Checks if inference statistics are disabled via environment variable
309+
* @return true if the environment variable is set to true
310+
*/
311+
protected boolean isInferenceStatisticsDisabled() {
312+
return Boolean.parseBoolean(System.getenv(ENV_VAR_OAK_INFERENCE_STATISTICS_DISABLED));
313+
}
300314
}

oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public static InferenceService getInstance(@NotNull String url, String model) {
5454

5555
public static InferenceService getInstance(InferenceModelConfig inferenceModelConfig) {
5656
//TODO we should use hash here, as hash takes care of all properties in model config.
57-
String key = inferenceModelConfig.getEmbeddingServiceUrl()
58-
+ "|" + inferenceModelConfig.getInferenceModelConfigName()
57+
String key = inferenceModelConfig.getInferenceModelConfigName()
5958
+ "|" + inferenceModelConfig.getModel();
6059

6160
if (SERVICES.size() >= MAX_CACHED_SERVICES) {

oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ public void logMetricsSummary(long intervalMillis) {
202202
}
203203

204204
private MeterStats getMeter(String name) {
205-
return statisticsProvider.getMeter(getMetricName(this.metricsServiceKey + ";" + name), StatsOptions.DEFAULT);
205+
return statisticsProvider.getMeter(getMetricName(this.metricsServiceKey + "-" + name), StatsOptions.DEFAULT);
206206
}
207207

208208
private TimerStats getTimer(String name) {
209-
return statisticsProvider.getTimer(this.metricsServiceKey + ";" + getMetricName(name), StatsOptions.DEFAULT);
209+
return statisticsProvider.getTimer(this.metricsServiceKey + "-" + getMetricName(name), StatsOptions.DEFAULT);
210210
}
211211

212212
/**

oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard;
2020
import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService;
2121
import org.apache.jackrabbit.oak.plugins.index.IndexEditorProvider;
22+
import org.apache.jackrabbit.oak.plugins.index.elastic.query.inference.InferenceConfig;
2223
import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
2324
import org.apache.jackrabbit.oak.query.QueryEngineSettings;
2425
import org.apache.jackrabbit.oak.spi.mount.MountInfoProvider;
@@ -154,6 +155,28 @@ public void disabled() {
154155
MockOsgi.deactivate(service, context.bundleContext());
155156
}
156157

158+
@Test
159+
public void testDisabledInferenceStatistics() throws Exception {
160+
// Create a spy of the service to be able to override just the environment check method
161+
ElasticIndexProviderService serviceSpy = spy(service);
162+
when(serviceSpy.isInferenceStatisticsDisabled()).thenReturn(true);
163+
164+
// Setup the rest as normal
165+
MockOsgi.injectServices(serviceSpy, context.bundleContext());
166+
167+
// Activate the service with inference enabled
168+
Map<String, Object> props = new HashMap<>(getElasticConfig());
169+
props.put("isInferenceEnabled", true);
170+
MockOsgi.activate(serviceSpy, context.bundleContext(), props);
171+
172+
// Verify that InferenceConfig.reInitialize was called with nodeStore and path but not statisticsProvider
173+
InferenceConfig inferenceConfig = serviceSpy.getInferenceConfig();
174+
assertNotNull(inferenceConfig);
175+
assertEquals(StatisticsProvider.NOOP.getClass(), inferenceConfig.getStatisticsProvider().getClass());
176+
177+
MockOsgi.deactivate(serviceSpy, context.bundleContext());
178+
}
179+
157180
private HashMap<String, Object> getElasticConfig() {
158181
HashMap<String, Object> config = new HashMap<>();
159182
config.put(PROP_INDEX_PREFIX, "elastic");

oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetricsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,20 +237,20 @@ public TestInferenceServiceMetrics(String testPrefix) {
237237

238238
@Override
239239
protected String getMetricName(String baseName) {
240-
// This method is called with metricsServiceKey + ";" + name, so we need to preserve that format
240+
// This method is called with metricsServiceKey + "-" + name, so we need to preserve that format
241241
// but still add our test prefix for uniqueness
242242
return testPrefix + "_" + baseName;
243243
}
244244

245245
// Methods to directly access the stats for verification
246246
public CounterStats getDirectCounter(String name) {
247247
// Format the name the same way it's done in the parent class
248-
return statisticsProvider.getCounterStats(getMetricName(TEST_SERVICE_KEY + ";" + name), StatsOptions.DEFAULT);
248+
return statisticsProvider.getCounterStats(getMetricName(TEST_SERVICE_KEY + "-" + name), StatsOptions.DEFAULT);
249249
}
250250

251251
public MeterStats getDirectMeter(String name) {
252252
// Format the name the same way it's done in the parent getMeter() method
253-
return statisticsProvider.getMeter(getMetricName(TEST_SERVICE_KEY + ";" + name), StatsOptions.DEFAULT);
253+
return statisticsProvider.getMeter(getMetricName(TEST_SERVICE_KEY + "-" + name), StatsOptions.DEFAULT);
254254
}
255255
}
256256
}

0 commit comments

Comments
 (0)