This is an automated email from the ASF dual-hosted git repository.

mkataria pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new e2a1539407 OAK-11750: Add flag to enable/disable inference stats 
collection (#2322)
e2a1539407 is described below

commit e2a153940736f638fc42811be06aa81b87fc3883
Author: Mohit Kataria <[email protected]>
AuthorDate: Thu Jun 5 13:31:45 2025 +0530

    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
---
 .../index/elastic/ElasticIndexProviderService.java | 16 ++++++++++++++-
 .../query/inference/InferenceServiceManager.java   |  3 +--
 .../query/inference/InferenceServiceMetrics.java   |  4 ++--
 .../elastic/ElasticIndexProviderServiceTest.java   | 23 ++++++++++++++++++++++
 .../inference/InferenceServiceMetricsTest.java     |  6 +++---
 5 files changed, 44 insertions(+), 8 deletions(-)

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 f591d1e56a..7750a04c56 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 @@ public class ElasticIndexProviderService {
         } 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 @@ public class ElasticIndexProviderService {
     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 adf971e90e..72799af0b7 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 class InferenceServiceManager {
 
     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 92cccbe085..e93470b418 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 class InferenceServiceMetrics {
     }
 
     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 010e7f442d..59ad14aea7 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 @@ package org.apache.jackrabbit.oak.plugins.index.elastic;
 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 class ElasticIndexProviderServiceTest {
         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<String, Object> 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<String, Object> getElasticConfig() {
         HashMap<String, Object> 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 9cb0d46449..2ecf579b0e 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 class InferenceServiceMetricsTest {
 
         @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 @@ public class InferenceServiceMetricsTest {
         // 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

Reply via email to