diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java index f591d1e56af..7750a04c566 100644 --- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java +++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java @@ -75,6 +75,7 @@ public class ElasticIndexProviderService { protected static final String PROP_ELASTIC_API_KEY_SECRET = "elasticsearch.apiKeySecret"; protected static final String PROP_LOCAL_TEXT_EXTRACTION_DIR = "localTextExtractionDir"; private static final boolean DEFAULT_IS_INFERENCE_ENABLED = false; + private static final String ENV_VAR_OAK_INFERENCE_STATISTICS_DISABLED = "OAK_INFERENCE_STATISTICS_DISABLED"; @ObjectClassDefinition(name = "ElasticIndexProviderService", description = "Apache Jackrabbit Oak ElasticIndexProvider") public @interface Config { @@ -190,7 +191,12 @@ private void activate(BundleContext bundleContext, Config config) { } else { this.isInferenceEnabled = config.isInferenceEnabled(); } - InferenceConfig.reInitialize(nodeStore, statisticsProvider, config.inferenceConfigPath(), isInferenceEnabled); + + if (isInferenceStatisticsDisabled()) { + InferenceConfig.reInitialize(nodeStore, config.inferenceConfigPath(), isInferenceEnabled); + } else { + InferenceConfig.reInitialize(nodeStore, statisticsProvider, config.inferenceConfigPath(), isInferenceEnabled); + } //initializeTextExtractionDir(bundleContext, config); //initializeExtractedTextCache(config, statisticsProvider); @@ -297,4 +303,12 @@ private ElasticConnection getElasticConnection(Config contextConfig) { public InferenceConfig getInferenceConfig() { return InferenceConfig.getInstance(); } + + /** + * Checks if inference statistics are disabled via environment variable + * @return true if the environment variable is set to true + */ + protected boolean isInferenceStatisticsDisabled() { + return Boolean.parseBoolean(System.getenv(ENV_VAR_OAK_INFERENCE_STATISTICS_DISABLED)); + } } diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceManager.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceManager.java index adf971e90e5..72799af0b78 100644 --- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceManager.java +++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceManager.java @@ -54,8 +54,7 @@ public static InferenceService getInstance(@NotNull String url, String model) { public static InferenceService getInstance(InferenceModelConfig inferenceModelConfig) { //TODO we should use hash here, as hash takes care of all properties in model config. - String key = inferenceModelConfig.getEmbeddingServiceUrl() - + "|" + inferenceModelConfig.getInferenceModelConfigName() + String key = inferenceModelConfig.getInferenceModelConfigName() + "|" + inferenceModelConfig.getModel(); if (SERVICES.size() >= MAX_CACHED_SERVICES) { diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetrics.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetrics.java index 92cccbe085b..e93470b418b 100644 --- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetrics.java +++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetrics.java @@ -202,11 +202,11 @@ public void logMetricsSummary(long intervalMillis) { } private MeterStats getMeter(String name) { - return statisticsProvider.getMeter(getMetricName(this.metricsServiceKey + ";" + name), StatsOptions.DEFAULT); + return statisticsProvider.getMeter(getMetricName(this.metricsServiceKey + "-" + name), StatsOptions.DEFAULT); } private TimerStats getTimer(String name) { - return statisticsProvider.getTimer(this.metricsServiceKey + ";" + getMetricName(name), StatsOptions.DEFAULT); + return statisticsProvider.getTimer(this.metricsServiceKey + "-" + getMetricName(name), StatsOptions.DEFAULT); } /** diff --git a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java index 010e7f442de..59ad14aea7e 100644 --- a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java +++ b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java @@ -19,6 +19,7 @@ import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard; import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService; import org.apache.jackrabbit.oak.plugins.index.IndexEditorProvider; +import org.apache.jackrabbit.oak.plugins.index.elastic.query.inference.InferenceConfig; import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore; import org.apache.jackrabbit.oak.query.QueryEngineSettings; import org.apache.jackrabbit.oak.spi.mount.MountInfoProvider; @@ -154,6 +155,28 @@ public void disabled() { MockOsgi.deactivate(service, context.bundleContext()); } + @Test + public void testDisabledInferenceStatistics() throws Exception { + // Create a spy of the service to be able to override just the environment check method + ElasticIndexProviderService serviceSpy = spy(service); + when(serviceSpy.isInferenceStatisticsDisabled()).thenReturn(true); + + // Setup the rest as normal + MockOsgi.injectServices(serviceSpy, context.bundleContext()); + + // Activate the service with inference enabled + Map props = new HashMap<>(getElasticConfig()); + props.put("isInferenceEnabled", true); + MockOsgi.activate(serviceSpy, context.bundleContext(), props); + + // Verify that InferenceConfig.reInitialize was called with nodeStore and path but not statisticsProvider + InferenceConfig inferenceConfig = serviceSpy.getInferenceConfig(); + assertNotNull(inferenceConfig); + assertEquals(StatisticsProvider.NOOP.getClass(), inferenceConfig.getStatisticsProvider().getClass()); + + MockOsgi.deactivate(serviceSpy, context.bundleContext()); + } + private HashMap getElasticConfig() { HashMap config = new HashMap<>(); config.put(PROP_INDEX_PREFIX, "elastic"); diff --git a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetricsTest.java b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetricsTest.java index 9cb0d464492..2ecf579b0e6 100644 --- a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetricsTest.java +++ b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/inference/InferenceServiceMetricsTest.java @@ -237,7 +237,7 @@ public TestInferenceServiceMetrics(String testPrefix) { @Override protected String getMetricName(String baseName) { - // This method is called with metricsServiceKey + ";" + name, so we need to preserve that format + // This method is called with metricsServiceKey + "-" + name, so we need to preserve that format // but still add our test prefix for uniqueness return testPrefix + "_" + baseName; } @@ -245,12 +245,12 @@ protected String getMetricName(String baseName) { // Methods to directly access the stats for verification public CounterStats getDirectCounter(String name) { // Format the name the same way it's done in the parent class - return statisticsProvider.getCounterStats(getMetricName(TEST_SERVICE_KEY + ";" + name), StatsOptions.DEFAULT); + return statisticsProvider.getCounterStats(getMetricName(TEST_SERVICE_KEY + "-" + name), StatsOptions.DEFAULT); } public MeterStats getDirectMeter(String name) { // Format the name the same way it's done in the parent getMeter() method - return statisticsProvider.getMeter(getMetricName(TEST_SERVICE_KEY + ";" + name), StatsOptions.DEFAULT); + return statisticsProvider.getMeter(getMetricName(TEST_SERVICE_KEY + "-" + name), StatsOptions.DEFAULT); } } } \ No newline at end of file