OAK-10265 | Refresh the index def after lane revert in oak-run out of… (#958)

* OAK-10265 | Refresh the index def after lane revert in oak-run out of the band reindex import

* OAK-10265 | Added test assertions

---------

Co-authored-by: Nitin Gupta <nitigup@adobe.com>
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexConstants.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexConstants.java
index 58b99d8..fe8abdb 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexConstants.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/IndexConstants.java
@@ -36,6 +36,11 @@
 
     String REINDEX_PROPERTY_NAME = "reindex";
 
+    /**
+     * Boolean property which signals to refresh the stored index definition
+     */
+    String REFRESH_PROPERTY_NAME = "refresh";
+
     String REINDEX_COUNT = "reindexCount";
 
     String REINDEX_ASYNC_PROPERTY_NAME = "reindex-async";
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/importer/AsyncLaneSwitcher.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/importer/AsyncLaneSwitcher.java
index cb3c2fc..8dde3c3 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/importer/AsyncLaneSwitcher.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/importer/AsyncLaneSwitcher.java
@@ -95,6 +95,8 @@
             idxBuilder.setProperty(clone(IndexConstants.ASYNC_PROPERTY_NAME, previousAsync));
         }
         idxBuilder.removeProperty(ASYNC_PREVIOUS);
+        // Set the refresh flag to true here otherwise the lane changes won't reflect in the storedIndexDefinition.
+        idxBuilder.setProperty(IndexConstants.REFRESH_PROPERTY_NAME, true);
     }
 
     public static boolean isNone(PropertyState previousAsync) {
diff --git a/oak-run/src/test/java/org/apache/jackrabbit/oak/index/ReindexIT.java b/oak-run/src/test/java/org/apache/jackrabbit/oak/index/ReindexIT.java
index 0e7456a..fea417e 100644
--- a/oak-run/src/test/java/org/apache/jackrabbit/oak/index/ReindexIT.java
+++ b/oak-run/src/test/java/org/apache/jackrabbit/oak/index/ReindexIT.java
@@ -25,6 +25,7 @@
 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.json.JsopDiff;
 import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
 import org.apache.jackrabbit.oak.plugins.index.IndexPathService;
 import org.apache.jackrabbit.oak.plugins.index.IndexPathServiceImpl;
@@ -184,7 +185,6 @@
         String explain = getQueryPlan(fixture2, "select * from [nt:base] where [bar] is not null");
         assertThat(explain, containsString("traverse"));
         assertThat(explain, not(containsString(TEST_INDEX_PATH)));
-
         int foo2Count = getFooCount(fixture2, "foo");
         assertEquals(fooCount + 100, foo2Count);
         assertNotNull(fixture2.getNodeStore().retrieve(checkpoint));
@@ -213,7 +213,6 @@
 
         IndexRepositoryFixture fixture4 = new LuceneRepositoryFixture(storeDir);
         int foo4Count = getFooCount(fixture4, "foo");
-
         //new count should be same as previous
         assertEquals(foo2Count, foo4Count);
 
@@ -227,6 +226,20 @@
         //Updates to the index definition should have got picked up
         String explain4 = getQueryPlan(fixture4, "select * from [nt:base] where [bar] is not null");
         assertThat(explain4, containsString(TEST_INDEX_PATH));
+
+        // Run the async index update
+        // This is needed for the refresh of the stored index def to take place.
+        fixture4.getAsyncIndexUpdate("async").run();
+
+        // check if the stored index def has the correct async property set
+        NodeState index = fixture4.getNodeStore().getRoot().getChildNode("oak:index").getChildNode("fooIndex");
+        NodeState storedDef = index.getChildNode(":index-definition");
+
+        // This assertion checks that the diff b/w index def and stored index def should not have async property in it.
+        assertFalse(JsopDiff.diffToJsop(index, storedDef).contains("async"));
+
+        // This checks that the stored index def has the proper async value set.
+        assertTrue(storedDef.toString().contains("async = async"));
         fixture4.close();
     }