ASTERIXDB-1338: Prevent Metadata Datasets Eviction

- Exclude metadata datasets from eviction policy.
- Fix updating used memory when a dataset is closed.

Change-Id: I56734bfe0eb7d166786cacb8d76f12f8d1a21798
Reviewed-on: https://asterix-gerrit.ics.uci.edu/707
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: abdullah alamoudi <bamousaa@gmail.com>
diff --git a/asterix-common/src/main/java/org/apache/asterix/common/context/DatasetLifecycleManager.java b/asterix-common/src/main/java/org/apache/asterix/common/context/DatasetLifecycleManager.java
index 0cd88d6..eb03015 100644
--- a/asterix-common/src/main/java/org/apache/asterix/common/context/DatasetLifecycleManager.java
+++ b/asterix-common/src/main/java/org/apache/asterix/common/context/DatasetLifecycleManager.java
@@ -214,16 +214,18 @@
     }
 
     private boolean evictCandidateDataset() throws HyracksDataException {
-        // We will take a dataset that has no active transactions, it is open (a dataset consuming memory),
-        // that is not being used (refcount == 0) and has been least recently used. The sort order defined
-        // for DatasetInfo maintains this. See DatasetInfo.compareTo().
+        /**
+         * We will take a dataset that has no active transactions, it is open (a dataset consuming memory),
+         * that is not being used (refcount == 0) and has been least recently used, excluding metadata datasets.
+         * The sort order defined for DatasetInfo maintains this. See DatasetInfo.compareTo().
+         */
         List<DatasetInfo> datasetInfosList = new ArrayList<DatasetInfo>(datasetInfos.values());
         Collections.sort(datasetInfosList);
         for (DatasetInfo dsInfo : datasetInfosList) {
             PrimaryIndexOperationTracker opTracker = (PrimaryIndexOperationTracker) datasetOpTrackers
                     .get(dsInfo.datasetID);
             if (opTracker != null && opTracker.getNumActiveOperations() == 0 && dsInfo.referenceCount == 0
-                    && dsInfo.isOpen) {
+                    && dsInfo.isOpen && dsInfo.datasetID >= firstAvilableUserDatasetID) {
                 closeDataset(dsInfo);
                 return true;
             }
@@ -607,8 +609,8 @@
             }
             assert iInfo.referenceCount == 0;
         }
-        dsInfo.isOpen = false;
         removeDatasetFromCache(dsInfo.datasetID);
+        dsInfo.isOpen = false;
     }
 
     @Override