You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by fo...@apache.org on 2021/03/31 12:48:18 UTC

svn commit: r1888249 - in /jackrabbit/oak/trunk/oak-search-elastic/src: main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ test/java/org/apache/jackrabbit/oak/plugins/index/elastic/

Author: fortino
Date: Wed Mar 31 12:48:18 2021
New Revision: 1888249

URL: http://svn.apache.org/viewvc?rev=1888249&view=rev
Log:
OAK-9379: disable ElasticIndexProviderService via a system property

Modified:
    jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticConnection.java
    jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java
    jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java

Modified: jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticConnection.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticConnection.java?rev=1888249&r1=1888248&r2=1888249&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticConnection.java (original)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticConnection.java Wed Mar 31 12:48:18 2021
@@ -24,6 +24,8 @@ import org.elasticsearch.client.RestClie
 import org.elasticsearch.client.RestClientBuilder;
 import org.elasticsearch.client.RestHighLevelClient;
 import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.Closeable;
 import java.io.IOException;
@@ -43,15 +45,12 @@ import java.util.concurrent.atomic.Atomi
  */
 public class ElasticConnection implements Closeable {
 
-    protected static final String SCHEME_PROP = "elasticsearch.scheme";
+    private static final Logger LOG = LoggerFactory.getLogger(ElasticConnection.class);
+
     protected static final String DEFAULT_SCHEME = "http";
-    protected static final String HOST_PROP = "elasticsearch.host";
     protected static final String DEFAULT_HOST = "127.0.0.1";
-    protected static final String PORT_PROP = "elasticsearch.port";
     protected static final int DEFAULT_PORT = 9200;
-    protected static final String API_KEY_ID_PROP = "elasticsearch.apiKeyId";
     protected static final String DEFAULT_API_KEY_ID = "";
-    protected static final String API_KEY_SECRET_PROP = "elasticsearch.apiKeySecret";
     protected static final String DEFAULT_API_KEY_SECRET = "";
 
     private final String indexPrefix;
@@ -123,10 +122,15 @@ public class ElasticConnection implement
     /**
      * Checks if elastic server is available for connection.
      * @return true if available, false otherwise.
-     * @throws IOException if some connection exception occurs.
      */
-    public boolean isAvailable() throws IOException {
-        return this.getClient().ping(RequestOptions.DEFAULT);
+    public boolean isAvailable() {
+        try {
+            return this.getClient().ping(RequestOptions.DEFAULT);
+        } catch (IOException e) {
+            LOG.warn("Error checking connection for {}, message: {}", this, e.getMessage());
+            LOG.debug("", e);
+            return false;
+        }
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java?rev=1888249&r1=1888248&r2=1888249&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java (original)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java Wed Mar 31 12:48:18 2021
@@ -49,7 +49,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.Hashtable;
@@ -63,16 +62,23 @@ import static org.apache.jackrabbit.oak.
 @Designate(ocd = ElasticIndexProviderService.Config.class)
 public class ElasticIndexProviderService {
 
+    protected static final String OAK_ELASTIC_PREFIX = "oak.elastic.";
+
+    protected static final String PROP_DISABLED = "disabled";
     protected static final String PROP_INDEX_PREFIX = "indexPrefix";
-    protected static final String PROP_ELASTIC_SCHEME = ElasticConnection.SCHEME_PROP;
-    protected static final String PROP_ELASTIC_HOST = ElasticConnection.HOST_PROP;
-    protected static final String PROP_ELASTIC_PORT = ElasticConnection.PORT_PROP;
-    protected static final String PROP_ELASTIC_API_KEY_ID = ElasticConnection.API_KEY_ID_PROP;
-    protected static final String PROP_ELASTIC_API_KEY_SECRET = ElasticConnection.API_KEY_SECRET_PROP;
+    protected static final String PROP_ELASTIC_SCHEME = "elasticsearch.scheme";
+    protected static final String PROP_ELASTIC_HOST = "elasticsearch.host";
+    protected static final String PROP_ELASTIC_PORT = "elasticsearch.port";
+    protected static final String PROP_ELASTIC_API_KEY_ID = "elasticsearch.apiKeyId";
+    protected static final String PROP_ELASTIC_API_KEY_SECRET = "elasticsearch.apiKeySecret";
     protected static final String PROP_LOCAL_TEXT_EXTRACTION_DIR = "localTextExtractionDir";
 
     @ObjectClassDefinition(name = "ElasticIndexProviderService", description = "Apache Jackrabbit Oak ElasticIndexProvider")
     public @interface Config {
+        @AttributeDefinition(name = "Disable the OAK Elastic service",
+                description = "If true, does not start the Elastic component")
+        boolean disabled() default false;
+
         @AttributeDefinition(name = "Extracted text cache size (MB)",
                 description = "Cache size in MB for caching extracted text for some time. When set to 0 then " +
                         "cache would be disabled")
@@ -145,11 +151,15 @@ public class ElasticIndexProviderService
     private File textExtractionDir;
 
     private ElasticConnection elasticConnection;
-    private ElasticIndexProvider indexProvider;
-    private String indexPrefix;
 
     @Activate
-    private void activate(BundleContext bundleContext, Config config) throws IOException {
+    private void activate(BundleContext bundleContext, Config config) {
+        boolean disabled = Boolean.parseBoolean(System.getProperty(OAK_ELASTIC_PREFIX + PROP_DISABLED, Boolean.toString(config.disabled())));
+        if (disabled) {
+            LOG.info("Component disabled by configuration");
+            return;
+        }
+
         whiteboard = new OsgiWhiteboard(bundleContext);
 
         //initializeTextExtractionDir(bundleContext, config);
@@ -181,10 +191,10 @@ public class ElasticIndexProviderService
         }
     }
 
-    private void registerIndexCleaner(Config contextConfig) throws IOException {
-        boolean reachable = elasticConnection.isAvailable();
-        if (!reachable) {
-            throw new IllegalArgumentException("Elastic server is not available - " + elasticConnection.toString());
+    private void registerIndexCleaner(Config contextConfig) {
+        if (!elasticConnection.isAvailable()) {
+            LOG.warn("The Elastic cluster at {} is not reachable. The index cleaner job has not been enabled", elasticConnection);
+            return;
         }
         if (contextConfig.remoteIndexCleanupFrequency() == -1) {
             return;
@@ -194,7 +204,7 @@ public class ElasticIndexProviderService
     }
 
     private void registerIndexProvider(BundleContext bundleContext) {
-        indexProvider = new ElasticIndexProvider(elasticConnection, new ElasticMetricHandler(statisticsProvider));
+        ElasticIndexProvider indexProvider = new ElasticIndexProvider(elasticConnection, new ElasticMetricHandler(statisticsProvider));
 
         // register observer needed for index tracking
         regs.add(bundleContext.registerService(Observer.class.getName(), indexProvider, null));
@@ -271,7 +281,7 @@ public class ElasticIndexProviderService
 
     private ElasticConnection getElasticConnection(Config contextConfig) {
         // system properties have priority, get mandatory params first
-        indexPrefix = System.getProperty(PROP_INDEX_PREFIX, contextConfig.indexPrefix());
+        String indexPrefix = System.getProperty(PROP_INDEX_PREFIX, contextConfig.indexPrefix());
         final String scheme = System.getProperty(PROP_ELASTIC_SCHEME, contextConfig.elasticsearch_scheme());
         final String host = System.getProperty(PROP_ELASTIC_HOST, contextConfig.elasticsearch_host());
         final String portString = System.getProperty(PROP_ELASTIC_PORT, contextConfig.elasticsearch_port());

Modified: jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java?rev=1888249&r1=1888248&r2=1888249&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java (original)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java Wed Mar 31 12:48:18 2021
@@ -35,13 +35,18 @@ import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
 import java.io.File;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import static org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexProviderService.PROP_DISABLED;
 import static org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexProviderService.PROP_ELASTIC_HOST;
 import static org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexProviderService.PROP_ELASTIC_PORT;
 import static org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexProviderService.PROP_INDEX_PREFIX;
+import static org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexProviderService.PROP_LOCAL_TEXT_EXTRACTION_DIR;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 
 public class ElasticIndexProviderServiceTest {
 
@@ -72,9 +77,23 @@ public class ElasticIndexProviderService
     }
 
     @Test
-    public void defaultSetup() throws Exception {
+    public void defaultSetup() {
+        MockOsgi.activate(service, context.bundleContext());
+
+        assertNotNull(context.getService(QueryIndexProvider.class));
+        assertNotNull(context.getService(IndexEditorProvider.class));
+
+        // With the default setup and no elastic cluster available at localhost:9200 the index cleaner will not be registered.
+        // This check can potentially fail if a local cluster is up and running.
+        assertEquals(0, WhiteboardUtils.getServices(wb, Runnable.class).size());
+
+        MockOsgi.deactivate(service, context.bundleContext());
+    }
+
+    @Test
+    public void withElasticSetup() throws Exception {
         Map<String, Object> props = new HashMap<>();
-        props.put("localTextExtractionDir", folder.newFolder("localTextExtractionDir").getAbsolutePath());
+        props.put(PROP_LOCAL_TEXT_EXTRACTION_DIR, folder.newFolder("localTextExtractionDir").getAbsolutePath());
         props.put(PROP_INDEX_PREFIX, "elastic");
         props.put(PROP_ELASTIC_HOST, "localhost");
         props.put(PROP_ELASTIC_PORT, elasticRule.elastic.getFirstMappedPort());
@@ -83,7 +102,19 @@ public class ElasticIndexProviderService
         assertNotNull(context.getService(QueryIndexProvider.class));
         assertNotNull(context.getService(IndexEditorProvider.class));
 
-        assertNotNull(WhiteboardUtils.getServices(wb, Runnable.class));
+        assertEquals(1, WhiteboardUtils.getServices(wb, Runnable.class).size());
+
+        MockOsgi.deactivate(service, context.bundleContext());
+    }
+
+    @Test
+    public void disabled() {
+        MockOsgi.activate(service, context.bundleContext(), Collections.singletonMap(PROP_DISABLED, true));
+
+        assertNull(context.getService(QueryIndexProvider.class));
+        assertNull(context.getService(IndexEditorProvider.class));
+
+        assertEquals(0, WhiteboardUtils.getServices(wb, Runnable.class).size());
 
         MockOsgi.deactivate(service, context.bundleContext());
     }