You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by hx...@apache.org on 2021/06/04 17:04:38 UTC

[iotdb] branch rel/0.12 updated: [To rel/0.12] Fix upgrade NPE and DeadLock (#3329)

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

hxd pushed a commit to branch rel/0.12
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rel/0.12 by this push:
     new 7469cc4  [To rel/0.12] Fix upgrade NPE and DeadLock (#3329)
7469cc4 is described below

commit 7469cc4c52e00480185786c738bd3e39065ceb37
Author: Haonan <hh...@outlook.com>
AuthorDate: Sat Jun 5 01:04:01 2021 +0800

    [To rel/0.12] Fix upgrade NPE and DeadLock (#3329)
    
    * Fix upgrade NPE and DeadLock
    
    * Fix get ungrade file num deadlock
---
 .../iotdb/db/engine/upgrade/UpgradeTask.java       | 15 +++++---------
 .../org/apache/iotdb/db/service/UpgradeSevice.java | 24 +++++-----------------
 .../iotdb/tsfile/write/chunk/ChunkWriterImpl.java  |  3 ++-
 3 files changed, 12 insertions(+), 30 deletions(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/engine/upgrade/UpgradeTask.java b/server/src/main/java/org/apache/iotdb/db/engine/upgrade/UpgradeTask.java
index 3d4aa76..76b1bac 100644
--- a/server/src/main/java/org/apache/iotdb/db/engine/upgrade/UpgradeTask.java
+++ b/server/src/main/java/org/apache/iotdb/db/engine/upgrade/UpgradeTask.java
@@ -61,19 +61,14 @@ public class UpgradeTask extends WrappedRunnable {
         logger.info("find upgraded file for {}", upgradeResource.getTsFile());
         upgradedResources = findUpgradedFiles();
       }
-      upgradeResource.writeLock();
-      try {
-        upgradeResource.setUpgradedResources(upgradedResources);
-        upgradeResource.getUpgradeTsFileResourceCallBack().call(upgradeResource);
-      } finally {
-        upgradeResource.writeUnlock();
-      }
-      UpgradeSevice.setCntUpgradeFileNum(UpgradeSevice.getCntUpgradeFileNum() - 1);
+      upgradeResource.setUpgradedResources(upgradedResources);
+      upgradeResource.getUpgradeTsFileResourceCallBack().call(upgradeResource);
+      UpgradeSevice.getTotalUpgradeFileNum().getAndAdd(-1);
       logger.info(
           "Upgrade completes, file path:{} , the remaining upgraded file num: {}",
           oldTsfilePath,
-          UpgradeSevice.getCntUpgradeFileNum());
-      if (UpgradeSevice.getCntUpgradeFileNum() == 0) {
+          UpgradeSevice.getTotalUpgradeFileNum().get());
+      if (UpgradeSevice.getTotalUpgradeFileNum().get() == 0) {
         logger.info("Start delete empty tmp folders");
         clearTmpFolders(DirectoryManager.getInstance().getAllSequenceFileFolders());
         clearTmpFolders(DirectoryManager.getInstance().getAllUnSequenceFileFolders());
diff --git a/server/src/main/java/org/apache/iotdb/db/service/UpgradeSevice.java b/server/src/main/java/org/apache/iotdb/db/service/UpgradeSevice.java
index df1c394..c48cb5a 100644
--- a/server/src/main/java/org/apache/iotdb/db/service/UpgradeSevice.java
+++ b/server/src/main/java/org/apache/iotdb/db/service/UpgradeSevice.java
@@ -39,7 +39,7 @@ public class UpgradeSevice implements IService {
   private static final UpgradeSevice INSTANCE = new UpgradeSevice();
   private ExecutorService upgradeThreadPool;
   private AtomicInteger threadCnt = new AtomicInteger();
-  private static int cntUpgradeFileNum;
+  private static AtomicInteger cntUpgradeFileNum = new AtomicInteger();
 
   private UpgradeSevice() {}
 
@@ -58,7 +58,7 @@ public class UpgradeSevice implements IService {
             updateThreadNum, r -> new Thread(r, "UpgradeThread-" + threadCnt.getAndIncrement()));
     UpgradeLog.createUpgradeLog();
     countUpgradeFiles();
-    if (cntUpgradeFileNum == 0) {
+    if (cntUpgradeFileNum.get() == 0) {
       stop();
       return;
     }
@@ -82,22 +82,8 @@ public class UpgradeSevice implements IService {
     return ServiceType.UPGRADE_SERVICE;
   }
 
-  public static void setCntUpgradeFileNum(int cntUpgradeFileNum) {
-    UpgradeUtils.getCntUpgradeFileLock().writeLock().lock();
-    try {
-      UpgradeSevice.cntUpgradeFileNum = cntUpgradeFileNum;
-    } finally {
-      UpgradeUtils.getCntUpgradeFileLock().writeLock().unlock();
-    }
-  }
-
-  public static int getCntUpgradeFileNum() {
-    UpgradeUtils.getCntUpgradeFileLock().readLock().lock();
-    try {
-      return cntUpgradeFileNum;
-    } finally {
-      UpgradeUtils.getCntUpgradeFileLock().readLock().unlock();
-    }
+  public static AtomicInteger getTotalUpgradeFileNum() {
+    return cntUpgradeFileNum;
   }
 
   public void submitUpgradeTask(UpgradeTask upgradeTask) {
@@ -105,7 +91,7 @@ public class UpgradeSevice implements IService {
   }
 
   private static void countUpgradeFiles() {
-    cntUpgradeFileNum = StorageEngine.getInstance().countUpgradeFiles();
+    cntUpgradeFileNum.addAndGet(StorageEngine.getInstance().countUpgradeFiles());
     logger.info("finish counting upgrading files, total num:{}", cntUpgradeFileNum);
   }
 
diff --git a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java
index 2c00f4a..39e5f79 100644
--- a/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java
+++ b/tsfile/src/main/java/org/apache/iotdb/tsfile/write/chunk/ChunkWriterImpl.java
@@ -294,7 +294,8 @@ public class ChunkWriterImpl implements IChunkWriter {
       if (numOfPages == 0) { // record the firstPageStatistics
         this.firstPageStatistics = pageWriter.getStatistics();
         this.sizeWithoutStatistic = pageWriter.writePageHeaderAndDataIntoBuff(pageBuffer, true);
-      } else if (numOfPages == 1) { // put the firstPageStatistics into pageBuffer
+      } else if (numOfPages == 1
+          && firstPageStatistics != null) { // put the firstPageStatistics into pageBuffer
         byte[] b = pageBuffer.toByteArray();
         pageBuffer.reset();
         pageBuffer.write(b, 0, this.sizeWithoutStatistic);