You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ji...@apache.org on 2019/04/19 04:54:16 UTC

[incubator-iotdb] branch refactor_mem_controller updated: change MemControl policies to close

This is an automated email from the ASF dual-hosted git repository.

jiangtian pushed a commit to branch refactor_mem_controller
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/refactor_mem_controller by this push:
     new deb1bd3  change MemControl policies to close
deb1bd3 is described below

commit deb1bd3b3451670b33fe55adc19433d70a572487
Author: 江天 <jt...@163.com>
AuthorDate: Fri Apr 19 12:52:55 2019 +0800

    change MemControl policies to close
---
 .../org/apache/iotdb/db/concurrent/ThreadName.java |  2 +-
 .../iotdb/db/engine/filenode/FileNodeManager.java  | 57 ++++++++--------------
 .../db/engine/memcontrol/ClosePartialPolicy.java   |  8 +--
 .../db/engine/memcontrol/ForceCloseAllPolicy.java  | 14 ++++--
 .../db/engine/memcontrol/MemMonitorThread.java     |  6 +--
 .../db/engine/modification/DeletionQueryTest.java  |  4 +-
 6 files changed, 39 insertions(+), 52 deletions(-)

diff --git a/iotdb/src/main/java/org/apache/iotdb/db/concurrent/ThreadName.java b/iotdb/src/main/java/org/apache/iotdb/db/concurrent/ThreadName.java
index 6053da6..2828c37 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/concurrent/ThreadName.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/concurrent/ThreadName.java
@@ -28,7 +28,7 @@ public enum ThreadName {
   MERGE_DAEMON("Merge-Daemon-Thread"),
   MEMORY_MONITOR("IoTDB-MemMonitor-Thread"),
   MEMORY_STATISTICS("IoTDB-MemStatistic-Thread"),
-  FLUSH_PARTIAL_POLICY("IoTDB-FlushPartialPolicy-Thread"),
+  FLUSH_PARTIAL_POLICY("IoTDB-ClosePartialPolicy-Thread"),
   FORCE_FLUSH_ALL_POLICY("IoTDB-ForceFlushAllPolicy-Thread"),
   STAT_MONITOR("StatMonitor-ServerServiceImpl"),
   FLUSH_SERVICE("Flush-ServerServiceImpl"),
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/engine/filenode/FileNodeManager.java b/iotdb/src/main/java/org/apache/iotdb/db/engine/filenode/FileNodeManager.java
index baf9fa6..d33713f 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/engine/filenode/FileNodeManager.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/engine/filenode/FileNodeManager.java
@@ -427,15 +427,14 @@ public class FileNodeManager implements IStatistic, IService {
     fileNodeProcessor.setIntervalFileNodeStartTime(deviceId);
     fileNodeProcessor.setLastUpdateTime(deviceId, timestamp);
     try {
-      if(!bufferWriteProcessor.write(tsRecord)) {
-        // undo time update
-        fileNodeProcessor.setIntervalFileNodeStartTime(deviceId, prevStartTime);
-        fileNodeProcessor.setLastUpdateTime(deviceId, prevUpdateTime);
-      }
+      bufferWriteProcessor.write(tsRecord);
     } catch (BufferWriteProcessorException e) {
       if (!isMonitor) {
         updateStatHashMapWhenFail(tsRecord);
       }
+      // undo time update
+      fileNodeProcessor.setIntervalFileNodeStartTime(deviceId, prevStartTime);
+      fileNodeProcessor.setLastUpdateTime(deviceId, prevUpdateTime);
       throw new FileNodeManagerException(e);
     }
 
@@ -1068,55 +1067,44 @@ public class FileNodeManager implements IStatistic, IService {
   }
 
   /**
-   * force flush to control memory usage.
+   * force close to control memory usage.
    */
-  public void forceFlush(BasicMemController.UsageLevel level) {
-    // you may add some delicate process like below
-    // or you could provide multiple methods for different urgency
+  public void forceClose(BasicMemController.UsageLevel level) {
     switch (level) {
       // only select the most urgent (most active or biggest in size)
-      // processors to flush
-      // only select top 10% active memory user to flush
+      // processors to close
+      // only select top 10% active memory user to close
       case WARNING:
         try {
-          flushTop(0.1f);
-        } catch (IOException e) {
-          LOGGER.error("force flush memory data error: {}", e);
+          closeTop(0.1f);
+        } catch (FileNodeProcessorException e) {
+          LOGGER.error("force close error: {}", e);
         }
         break;
       // force all processors to flush
       case DANGEROUS:
         try {
-          flushAll();
-        } catch (IOException e) {
-          LOGGER.error("force flush memory data error: {}", e);
+          closeAllFile();
+        } catch (FileNodeProcessorException e) {
+          LOGGER.error("force close error: {}", e);
         }
         break;
       // if the flush thread pool is not full ( or half full), start a new
       // flush task
       case SAFE:
-        if (FlushManager.getInstance().getActiveCnt() < 0.5 * FlushManager.getInstance()
-            .getThreadCnt()) {
-          try {
-            flushTop(0.01f);
-          } catch (IOException e) {
-            LOGGER.error("force flush memory data error: ", e);
-          }
-        }
         break;
       default:
     }
   }
 
-  private void flushAll() throws IOException {
+  private void closeAllFile() throws FileNodeProcessorException {
     for (FileNodeProcessor processor : processorMap.values()) {
       if (!processor.tryLock(true)) {
         continue;
       }
       try {
-        boolean isMerge = processor.flush().isHasOverflowFlushTask();
-        if (isMerge) {
-          processor.submitToMerge();
+        if (processor.canBeClosed()) {
+          processor.close();
         }
       } finally {
         processor.unlock(true);
@@ -1124,7 +1112,7 @@ public class FileNodeManager implements IStatistic, IService {
     }
   }
 
-  private void flushTop(float percentage) throws IOException {
+  private void closeTop(float percentage) throws FileNodeProcessorException {
     List<FileNodeProcessor> tempProcessors = new ArrayList<>(processorMap.values());
     // sort the tempProcessors as descending order
     tempProcessors.sort((o1, o2) -> (int) (o2.memoryUsage() - o1.memoryUsage()));
@@ -1134,15 +1122,10 @@ public class FileNodeManager implements IStatistic, IService {
             : 1;
     for (int i = 0; i < flushNum && i < tempProcessors.size(); i++) {
       FileNodeProcessor processor = tempProcessors.get(i);
-      // 64M
-      if (processor.memoryUsage() <= TSFileConfig.groupSizeInByte / 2) {
-        continue;
-      }
       processor.writeLock();
       try {
-        boolean isMerge = processor.flush().isHasOverflowFlushTask();
-        if (isMerge) {
-          processor.submitToMerge();
+        if (processor.canBeClosed()) {
+          processor.close();
         }
       } finally {
         processor.writeUnlock();
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ClosePartialPolicy.java b/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ClosePartialPolicy.java
index 9be76dd..f70b401 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ClosePartialPolicy.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ClosePartialPolicy.java
@@ -29,16 +29,16 @@ import org.slf4j.LoggerFactory;
 /**
  * This class only gives a hint to FilenodeManager that it may flush some data to avoid rush hour.
  */
-public class FlushPartialPolicy implements Policy {
+public class ClosePartialPolicy implements Policy {
 
-  private static final Logger LOGGER = LoggerFactory.getLogger(FlushPartialPolicy.class);
+  private static final Logger LOGGER = LoggerFactory.getLogger(ClosePartialPolicy.class);
   private Thread workerThread;
   private long sleepInterval = IoTDBDescriptor.getInstance().getConfig().getSmallFlushInterval();
 
   @Override
   public void execute() {
     if (LOGGER.isDebugEnabled()) {
-      LOGGER.debug("Memory reaches {}, current memory size is {}, JVM memory is {}, flushing.",
+      LOGGER.debug("Memory reaches {}, current memory size is {}, JVM memory is {}, closing.",
               BasicMemController.getInstance().getCurrLevel(),
               MemUtils.bytesCntToStr(BasicMemController.getInstance().getTotalUsage()),
               MemUtils.bytesCntToStr(Runtime.getRuntime().totalMemory()
@@ -51,7 +51,7 @@ public class FlushPartialPolicy implements Policy {
       workerThread.start();
     } else {
       if (workerThread.isAlive()) {
-        LOGGER.debug("Last flush is ongoing...");
+        LOGGER.debug("Last close is ongoing...");
       } else {
         workerThread = createWorkerThread();
         workerThread.start();
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ForceCloseAllPolicy.java b/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ForceCloseAllPolicy.java
index 53533d9..ec9f142 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ForceCloseAllPolicy.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/ForceCloseAllPolicy.java
@@ -24,15 +24,19 @@ import org.apache.iotdb.db.utils.MemUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class ForceFLushAllPolicy implements Policy {
+/**
+ * ForceCloseAllPolicy is executed when the memory usage reaches a dangerous level.
+ * In this case, all FileNodeProcessor should be closed to reduce in-memory data and metadata.
+ */
+public class ForceCloseAllPolicy implements Policy {
 
-  private static final Logger logger = LoggerFactory.getLogger(ForceFLushAllPolicy.class);
+  private static final Logger logger = LoggerFactory.getLogger(ForceCloseAllPolicy.class);
   private Thread workerThread;
 
   @Override
   public void execute() {
     if (logger.isInfoEnabled()) {
-      logger.info("Memory reaches {}, current memory size is {}, JVM memory is {}, flushing.",
+      logger.info("Memory reaches {}, current memory size is {}, JVM memory is {}, closing.",
               BasicMemController.getInstance().getCurrLevel(),
               MemUtils.bytesCntToStr(BasicMemController.getInstance().getTotalUsage()),
               MemUtils.bytesCntToStr(Runtime.getRuntime().totalMemory()
@@ -45,7 +49,7 @@ public class ForceFLushAllPolicy implements Policy {
       workerThread.start();
     } else {
       if (workerThread.isAlive()) {
-        logger.info("Last flush is ongoing...");
+        logger.info("Last close is ongoing...");
       } else {
         workerThread = createWorkerThread();
         workerThread.start();
@@ -55,7 +59,7 @@ public class ForceFLushAllPolicy implements Policy {
 
   private Thread createWorkerThread() {
     return new Thread(() ->
-            FileNodeManager.getInstance().forceFlush(BasicMemController.UsageLevel.DANGEROUS),
+            FileNodeManager.getInstance().forceClose(BasicMemController.UsageLevel.DANGEROUS),
             ThreadName.FORCE_FLUSH_ALL_POLICY.getName());
   }
 }
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/MemMonitorThread.java b/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/MemMonitorThread.java
index 05226aa..7a4de75 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/MemMonitorThread.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/engine/memcontrol/MemMonitorThread.java
@@ -41,12 +41,12 @@ public class MemMonitorThread extends Thread {
     long confInterval = config.getMemMonitorInterval();
     this.checkInterval = confInterval > 0 ? confInterval : this.checkInterval;
     if (config.isEnableSmallFlush()) {
-      this.safePolicy = new FlushPartialPolicy();
+      this.safePolicy = new ClosePartialPolicy();
     } else {
       this.safePolicy = new NoActPolicy();
     }
-    this.warningPolicy = new ForceFLushAllPolicy();
-    this.dangerousPolicy = new ForceFLushAllPolicy();
+    this.warningPolicy = new ClosePartialPolicy();
+    this.dangerousPolicy = new ForceCloseAllPolicy();
   }
 
   public void setCheckInterval(long checkInterval) {
diff --git a/iotdb/src/test/java/org/apache/iotdb/db/engine/modification/DeletionQueryTest.java b/iotdb/src/test/java/org/apache/iotdb/db/engine/modification/DeletionQueryTest.java
index 3e9bb99..afb9741 100644
--- a/iotdb/src/test/java/org/apache/iotdb/db/engine/modification/DeletionQueryTest.java
+++ b/iotdb/src/test/java/org/apache/iotdb/db/engine/modification/DeletionQueryTest.java
@@ -248,7 +248,7 @@ public class DeletionQueryTest {
     FileNodeManager.getInstance().delete(processorName, measurements[5], 30);
     FileNodeManager.getInstance().delete(processorName, measurements[5], 50);
 
-    FileNodeManager.getInstance().forceFlush(UsageLevel.DANGEROUS);
+    FileNodeManager.getInstance().forceClose(UsageLevel.DANGEROUS);
 
     for (int i = 101; i <= 200; i++) {
       TSRecord record = new TSRecord(i, processorName);
@@ -263,7 +263,7 @@ public class DeletionQueryTest {
     FileNodeManager.getInstance().delete(processorName, measurements[5], 230);
     FileNodeManager.getInstance().delete(processorName, measurements[5], 250);
 
-    FileNodeManager.getInstance().forceFlush(UsageLevel.DANGEROUS);
+    FileNodeManager.getInstance().forceClose(UsageLevel.DANGEROUS);
 
     for (int i = 201; i <= 300; i++) {
       TSRecord record = new TSRecord(i, processorName);