Notify waiting operation on change of component state

The change fixes a bug in the lsm harness
that could lead to an accessor being blocked on an empty
component because a flush request used to set the state
of all components to unwritable. when the component
state was fixed, no notification was sent to the
waiting operation.

Change-Id: I6fe0aa0323a70a2702a9310339abc68ab5cf14c7
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1733
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
BAD: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/PrimaryIndexOperationTracker.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/PrimaryIndexOperationTracker.java
index 7fca039..feeb578 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/PrimaryIndexOperationTracker.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/PrimaryIndexOperationTracker.java
@@ -35,9 +35,9 @@
 import org.apache.hyracks.storage.am.lsm.common.api.ILSMComponent.ComponentState;
 import org.apache.hyracks.storage.am.lsm.common.api.ILSMIndex;
 import org.apache.hyracks.storage.am.lsm.common.api.ILSMIndexAccessor;
+import org.apache.hyracks.storage.am.lsm.common.api.ILSMMemoryComponent;
 import org.apache.hyracks.storage.am.lsm.common.api.ILSMOperationTracker;
 import org.apache.hyracks.storage.am.lsm.common.api.LSMOperationType;
-import org.apache.hyracks.storage.am.lsm.common.impls.AbstractLSMIndex;
 
 public class PrimaryIndexOperationTracker extends BaseOperationTracker {
 
@@ -110,11 +110,11 @@
         if (needsFlush || flushOnExit) {
             //Make the current mutable components READABLE_UNWRITABLE to stop coming modify operations from entering them until the current flush is scheduled.
             for (ILSMIndex lsmIndex : indexes) {
-                AbstractLSMIndex abstractLSMIndex = ((AbstractLSMIndex) lsmIndex);
-                ILSMOperationTracker opTracker = abstractLSMIndex.getOperationTracker();
+                ILSMOperationTracker opTracker = lsmIndex.getOperationTracker();
                 synchronized (opTracker) {
-                    if (abstractLSMIndex.getCurrentMutableComponentState() == ComponentState.READABLE_WRITABLE) {
-                        abstractLSMIndex.setCurrentMutableComponentState(ComponentState.READABLE_UNWRITABLE);
+                    ILSMMemoryComponent memComponent = lsmIndex.getCurrentMemoryComponent();
+                    if (memComponent.getState() == ComponentState.READABLE_WRITABLE && memComponent.isModified()) {
+                        memComponent.setState(ComponentState.READABLE_UNWRITABLE);
                     }
                 }
             }
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/LSMHarness.java b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/LSMHarness.java
index 494ba27..682e323 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/LSMHarness.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/LSMHarness.java
@@ -88,6 +88,7 @@
                                     .getCurrentMutableComponentState() == ComponentState.READABLE_UNWRITABLE) {
                                 ((AbstractLSMIndex) lsmIndex)
                                         .setCurrentMutableComponentState(ComponentState.READABLE_WRITABLE);
+                                opTracker.notifyAll();
                             }
                             return false;
                         }
@@ -367,7 +368,9 @@
         }
         getAndEnterComponents(ctx, LSMOperationType.MODIFICATION, false);
         try {
-            lsmIndex.getCurrentMemoryComponent().getMetadata().put(key, value);
+            AbstractLSMMemoryComponent c = (AbstractLSMMemoryComponent) ctx.getComponentHolder().get(0);
+            c.getMetadata().put(key, value);
+            c.setModified();
         } finally {
             exitAndComplete(ctx, LSMOperationType.MODIFICATION);
         }
@@ -389,7 +392,9 @@
         }
         getAndEnterComponents(ctx, LSMOperationType.FORCE_MODIFICATION, false);
         try {
-            lsmIndex.getCurrentMemoryComponent().getMetadata().put(key, value);
+            AbstractLSMMemoryComponent c = (AbstractLSMMemoryComponent) ctx.getComponentHolder().get(0);
+            c.getMetadata().put(key, value);
+            c.setModified();
         } finally {
             exitAndComplete(ctx, LSMOperationType.FORCE_MODIFICATION);
         }