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 al...@apache.org on 2015/10/14 14:04:25 UTC

svn commit: r1708592 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java

Author: alexparvulescu
Date: Wed Oct 14 12:04:25 2015
New Revision: 1708592

URL: http://svn.apache.org/viewvc?rev=1708592&view=rev
Log:
OAK-3505 Provide an optionally stricter policy for missing synchronous index editor providers


Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java?rev=1708592&r1=1708591&r2=1708592&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdate.java Wed Oct 14 12:04:25 2015
@@ -67,6 +67,34 @@ public class IndexUpdate implements Edit
 
     private static final Logger log = LoggerFactory.getLogger(IndexUpdate.class);
 
+    /**
+     * <p>
+     * The value of this flag determines the behavior of the IndexUpdate when
+     * dealing with {@code reindex} flags.
+     * </p>
+     * <p>
+     * If {@code false} (default value), the indexer will start reindexing
+     * immediately in the current thread, blocking a commit until this operation
+     * is done.
+     * </p>
+     * <p>
+     * If {@code true}, the indexer will ignore the flag, therefore ignoring any
+     * reindex requests.
+     * </p>
+     * <p>
+     * This is only provided as a support tool (see OAK-3505) so it should be
+     * used with extreme caution!
+     * </p>
+     */
+    private static final boolean IGNORE_REINDEX_FLAGS = Boolean
+            .getBoolean("oak.indexUpdate.ignoreReindexFlags");
+
+    static {
+        if (IGNORE_REINDEX_FLAGS) {
+            log.warn("Reindexing is disabled by configuration. This value is configurable via the 'oak.indexUpdate.ignoreReindexFlags' system property.");
+        }
+    }
+
     private final IndexUpdateRootState rootState;
 
     private final NodeBuilder builder;
@@ -145,7 +173,7 @@ public class IndexUpdate implements Edit
             String name) {
         PropertyState ps = definition.getProperty(REINDEX_PROPERTY_NAME);
         if (ps != null && ps.getValue(BOOLEAN)) {
-            return true;
+            return !IGNORE_REINDEX_FLAGS;
         }
         // reindex in the case this is a new node, even though the reindex flag
         // might be set to 'false' (possible via content import)
@@ -316,6 +344,16 @@ public class IndexUpdate implements Edit
 
     public static class MissingIndexProviderStrategy {
 
+        /**
+         * The value of this flag determines the behavior of
+         * {@link #onMissingIndex(String, NodeBuilder, String)}. If
+         * {@code false} (default value), the method will set the
+         * {@code reindex} flag to true and log a warning. if {@code true}, the
+         * method will throw a {@link CommitFailedException} failing the commit.
+         */
+        private boolean failOnMissingIndexProvider = Boolean
+                .getBoolean("oak.indexUpdate.failOnMissingIndexProvider");
+
         private final Set<String> ignore = newHashSet("disabled");
 
         public void onMissingIndex(String type, NodeBuilder definition, String indexPath)
@@ -329,15 +367,26 @@ public class IndexUpdate implements Edit
                 // already true, skip the update
                 return;
             }
-            log.warn(
-                    "Missing index provider of type [{}], requesting reindex on [{}]",
-                    type, indexPath);
-            definition.setProperty(REINDEX_PROPERTY_NAME, true);
+
+            if (failOnMissingIndexProvider) {
+                throw new CommitFailedException("IndexUpdate", 1,
+                        "Missing index provider detected for type [" + type
+                                + "] on index [" + indexPath + "]");
+            } else {
+                log.warn(
+                        "Missing index provider of type [{}], requesting reindex on [{}]",
+                        type, indexPath);
+                definition.setProperty(REINDEX_PROPERTY_NAME, true);
+            }
         }
 
         boolean isDisabled(String type) {
             return ignore.contains(type);
         }
+
+        void setFailOnMissingIndexProvider(boolean failOnMissingIndexProvider) {
+            this.failOnMissingIndexProvider = failOnMissingIndexProvider;
+        }
     }
 
     public IndexUpdate withMissingProviderStrategy(

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java?rev=1708592&r1=1708591&r2=1708592&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/IndexUpdateTest.java Wed Oct 14 12:04:25 2015
@@ -32,6 +32,7 @@ import static org.junit.Assert.assertFal
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.Set;
 
@@ -40,6 +41,7 @@ import javax.annotation.Nonnull;
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.plugins.index.IndexUpdate.MissingIndexProviderStrategy;
 import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
 import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexLookup;
 import org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState;
@@ -50,6 +52,7 @@ import org.apache.jackrabbit.oak.query.i
 import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
 import org.apache.jackrabbit.oak.spi.commit.Editor;
 import org.apache.jackrabbit.oak.spi.commit.EditorHook;
+import org.apache.jackrabbit.oak.spi.commit.EditorProvider;
 import org.apache.jackrabbit.oak.spi.query.Filter;
 import org.apache.jackrabbit.oak.spi.query.PropertyValues;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
@@ -332,8 +335,7 @@ public class IndexUpdateTest {
                 .getValue(Type.BOOLEAN));
         assertTrue(ns1.getProperty(REINDEX_ASYNC_PROPERTY_NAME).getValue(
                 Type.BOOLEAN));
-        assertEquals(ASYNC_REINDEX_VALUE, ns1.getProperty(ASYNC_PROPERTY_NAME)
-                .getValue(Type.STRING));
+        assertEquals(ASYNC_REINDEX_VALUE, ns1.getString(ASYNC_PROPERTY_NAME));
 
         AsyncIndexUpdate async = new AsyncIndexUpdate(ASYNC_REINDEX_VALUE,
                 store, provider, true);
@@ -390,6 +392,47 @@ public class IndexUpdateTest {
                 azerty.getProperty(REINDEX_PROPERTY_NAME));
     }
 
+    /**
+     * OAK-3505 Provide an optionally stricter policy for missing synchronous
+     * index editor providers
+     */
+    @Test
+    public void testMissingProviderFailsCommit() throws Exception {
+
+        final IndexUpdateCallback noop = new IndexUpdateCallback() {
+            @Override
+            public void indexUpdate() {
+            }
+        };
+        final MissingIndexProviderStrategy mips = new MissingIndexProviderStrategy();
+        mips.setFailOnMissingIndexProvider(true);
+
+        EditorHook hook = new EditorHook(new EditorProvider() {
+            @Override
+            public Editor getRootEditor(NodeState before, NodeState after,
+                    NodeBuilder builder, CommitInfo info)
+                    throws CommitFailedException {
+                return new IndexUpdate(emptyProvider(), null, after, builder,
+                        noop).withMissingProviderStrategy(mips);
+            }
+        });
+
+        NodeState before = builder.getNodeState();
+
+        createIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME),
+                "rootIndex", true, false, ImmutableSet.of("foo"), null);
+        builder.child(INDEX_DEFINITIONS_NAME).child("azerty");
+        builder.child("testRoot").setProperty("foo", "abc");
+        NodeState after = builder.getNodeState();
+
+        try {
+            hook.processCommit(before, after, CommitInfo.EMPTY);
+            fail("commit should fail on missing index provider");
+        } catch (CommitFailedException ex) {
+            // expected
+        }
+    }
+
     @Test
     public void testReindexCount() throws Exception{
         builder.child("testRoot").setProperty("foo", "abc");