[NO ISSUE][STO] Allocate Memory Component Index Lazily

- user model changes: no
- storage format changes: no
- interface changes: no

Details:

- When a memory component is flushed, delay the allocation of
  its index until the next activation.
- When the global virtual buffer cache is full, notify the
  flush thread to check if any indexes can be flushed.

Change-Id: I2e07d4e857989fb742e1fc0c5e620a7293fcc3e4
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/9463
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java
index f772038..a51e13c 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/context/GlobalVirtualBufferCache.java
@@ -199,7 +199,11 @@
 
     @Override
     public boolean isFull() {
-        return vbc.isFull();
+        boolean full = vbc.isFull();
+        if (full) {
+            checkAndNotifyFlushThread();
+        }
+        return full;
     }
 
     @Override
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMMemoryComponent.java b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMMemoryComponent.java
index 3b6667e..8d37d97 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMMemoryComponent.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMMemoryComponent.java
@@ -35,6 +35,7 @@
 public abstract class AbstractLSMMemoryComponent extends AbstractLSMComponent implements ILSMMemoryComponent {
 
     private static final Logger LOGGER = LogManager.getLogger();
+    protected final AtomicBoolean allocated;
     private final IVirtualBufferCache vbc;
     private final AtomicBoolean isModified;
     private int writerCount;
@@ -48,6 +49,7 @@
         writerCount = 0;
         state = ComponentState.INACTIVE;
         isModified = new AtomicBoolean();
+        allocated = new AtomicBoolean();
         metadata = new MemoryComponentMetadata();
     }
 
@@ -80,6 +82,9 @@
 
     private void activate() throws HyracksDataException {
         if (state == ComponentState.INACTIVE) {
+            if (!allocated.get()) {
+                doAllocate();
+            }
             state = ComponentState.READABLE_WRITABLE;
             lsmIndex.getIOOperationCallback().recycled(this);
         }
@@ -247,10 +252,11 @@
 
     @Override
     public void cleanup() throws HyracksDataException {
-        getIndex().deactivate();
-        getIndex().destroy();
-        getIndex().create();
-        getIndex().activate();
+        if (allocated.get()) {
+            getIndex().deactivate();
+            getIndex().destroy();
+            allocated.set(false);
+        }
     }
 
     @Override
@@ -286,6 +292,7 @@
             created = true;
             getIndex().activate();
             activated = true;
+            allocated.set(true);
         } finally {
             if (created && !activated) {
                 getIndex().destroy();
@@ -305,8 +312,11 @@
     }
 
     protected void doDeallocate() throws HyracksDataException {
-        getIndex().deactivate();
-        getIndex().destroy();
+        if (allocated.get()) {
+            getIndex().deactivate();
+            getIndex().destroy();
+            allocated.set(false);
+        }
         componentId = null;
     }
 
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMWithBuddyMemoryComponent.java b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMWithBuddyMemoryComponent.java
index 882b4bd..5ebaf26 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMWithBuddyMemoryComponent.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/AbstractLSMWithBuddyMemoryComponent.java
@@ -34,11 +34,11 @@
 
     @Override
     public void cleanup() throws HyracksDataException {
-        super.cleanup();
-        getBuddyIndex().deactivate();
-        getBuddyIndex().destroy();
-        getBuddyIndex().create();
-        getBuddyIndex().activate();
+        if (allocated.get()) {
+            super.cleanup();
+            getBuddyIndex().deactivate();
+            getBuddyIndex().destroy();
+        }
     }
 
     @Override
@@ -50,9 +50,11 @@
 
     @Override
     public void doDeallocate() throws HyracksDataException {
-        super.doDeallocate();
-        getBuddyIndex().deactivate();
-        getBuddyIndex().destroy();
+        if (allocated.get()) {
+            super.doDeallocate();
+            getBuddyIndex().deactivate();
+            getBuddyIndex().destroy();
+        }
     }
 
     @Override