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

jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new d4193fbf5b3 Fix upgrade and add fysnc
d4193fbf5b3 is described below

commit d4193fbf5b31e5094ed02a99bbcb46c59f5be95f
Author: Colin Li <[email protected]>
AuthorDate: Tue Jan 16 08:49:14 2024 +0800

    Fix upgrade and add fysnc
---
 .../iotdb/confignode/persistence/AuthorInfo.java   |  5 -----
 .../commons/auth/authorizer/BasicAuthorizer.java   |  1 -
 .../iotdb/commons/auth/role/BasicRoleManager.java  |  6 ++++++
 .../commons/auth/role/LocalFileRoleAccessor.java   | 12 +++++------
 .../commons/auth/role/LocalFileRoleManager.java    |  6 ++++++
 .../iotdb/commons/auth/user/BasicUserManager.java  |  1 +
 .../commons/auth/user/LocalFileUserAccessor.java   | 23 ++++++++++------------
 .../commons/auth/user/LocalFileUserManager.java    |  5 +++++
 .../org/apache/iotdb/commons/utils/FileUtils.java  |  8 +++++++-
 9 files changed, 41 insertions(+), 26 deletions(-)

diff --git 
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/AuthorInfo.java
 
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/AuthorInfo.java
index d5c6b0ab60a..7b4c9ca02f3 100644
--- 
a/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/AuthorInfo.java
+++ 
b/iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/AuthorInfo.java
@@ -545,11 +545,6 @@ public class AuthorInfo implements SnapshotProcessor {
   @Override
   public void processLoadSnapshot(File snapshotDir) throws TException, 
IOException {
     authorizer.processLoadSnapshot(snapshotDir);
-    try {
-      authorizer.reset();
-    } catch (AuthException e) {
-      throw new IOException("Error when load role and user: %s", e);
-    }
   }
 
   @TestOnly
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/BasicAuthorizer.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/BasicAuthorizer.java
index cfb58e808e3..e61982ff65b 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/BasicAuthorizer.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/authorizer/BasicAuthorizer.java
@@ -72,7 +72,6 @@ public abstract class BasicAuthorizer implements IAuthorizer, 
IService {
   BasicAuthorizer(IUserManager userManager, IRoleManager roleManager) throws 
AuthException {
     this.userManager = userManager;
     this.roleManager = roleManager;
-    init();
   }
 
   protected void init() throws AuthException {
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/BasicRoleManager.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/BasicRoleManager.java
index f0994200c56..244bf323668 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/BasicRoleManager.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/BasicRoleManager.java
@@ -27,6 +27,9 @@ import org.apache.iotdb.commons.utils.AuthUtils;
 import org.apache.iotdb.commons.utils.TestOnly;
 import org.apache.iotdb.rpc.TSStatusCode;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -47,6 +50,8 @@ public abstract class BasicRoleManager implements 
IRoleManager {
   protected HashLock lock;
   private boolean preVersion = false;
 
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(BasicRoleManager.class);
+
   BasicRoleManager(LocalFileRoleAccessor accessor) {
     this.roleMap = new HashMap<>();
     this.accessor = accessor;
@@ -172,6 +177,7 @@ public abstract class BasicRoleManager implements 
IRoleManager {
       try {
         roleMap.put(roleName, accessor.loadRole(roleName));
       } catch (IOException e) {
+        LOGGER.warn("Get exception when load role {}", roleName);
         throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
       }
     }
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
index 8f84e80ba78..2606db69aa4 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java
@@ -154,9 +154,8 @@ public class LocalFileRoleAccessor implements IRoleAccessor 
{
       LOGGER.error("Failed to create role dir {}", roleDirPath);
     }
 
-    FileOutputStream fileOutputStream = new FileOutputStream(roleProfile);
-    BufferedOutputStream outputStream = new 
BufferedOutputStream(fileOutputStream);
-    try {
+    try (FileOutputStream fileOutputStream = new FileOutputStream(roleProfile);
+        BufferedOutputStream outputStream = new 
BufferedOutputStream(fileOutputStream)) {
       byte[] strBuffer = role.getName().getBytes(STRING_ENCODING);
       IOUtils.writeInt(outputStream, -1 * strBuffer.length, 
encodingBufferLocal);
       outputStream.write(strBuffer);
@@ -167,12 +166,13 @@ public class LocalFileRoleAccessor implements 
IRoleAccessor {
         IOUtils.writePathPrivilege(
             outputStream, pathPrivilege, STRING_ENCODING, encodingBufferLocal);
       }
+      // handle outputstream's exception in saveRole locally.
+      outputStream.flush();
+      fileOutputStream.getFD().sync();
     } catch (Exception e) {
+      LOGGER.warn("meet error when save role: {}", role.getName());
       throw new IOException(e);
     } finally {
-      outputStream.flush();
-      fileOutputStream.getFD().sync();
-      outputStream.close();
       encodingBufferLocal.remove();
     }
 
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleManager.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleManager.java
index e5ca7d59695..a466f902989 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleManager.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleManager.java
@@ -19,6 +19,7 @@
 
 package org.apache.iotdb.commons.auth.role;
 
+import org.apache.iotdb.commons.auth.AuthException;
 import org.apache.iotdb.commons.auth.entity.Role;
 
 import org.apache.thrift.TException;
@@ -45,5 +46,10 @@ public class LocalFileRoleManager extends BasicRoleManager {
   @Override
   public void processLoadSnapshot(File snapshotDir) throws TException, 
IOException {
     accessor.processLoadSnapshot(snapshotDir);
+    try {
+      super.reset();
+    } catch (AuthException e) {
+      throw new IOException(e);
+    }
   }
 }
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/BasicUserManager.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/BasicUserManager.java
index 04edf749bd2..b40417cb534 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/BasicUserManager.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/BasicUserManager.java
@@ -319,6 +319,7 @@ public abstract class BasicUserManager implements 
IUserManager {
       try {
         userMap.put(name, accessor.loadUser(name));
       } catch (IOException e) {
+        LOGGER.warn("Get exception when load user {}", name);
         throw new AuthException(TSStatusCode.AUTH_IO_EXCEPTION, e);
       }
     }
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
index acee3beeeb1..d2030692df2 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserAccessor.java
@@ -216,9 +216,8 @@ public class LocalFileUserAccessor implements IUserAccessor 
{
                 + IoTDBConstant.PROFILE_SUFFIX
                 + TEMP_SUFFIX);
 
-    FileOutputStream fileOutputStream = new FileOutputStream(userProfile);
-    BufferedOutputStream outputStream = new 
BufferedOutputStream(fileOutputStream);
-    try {
+    try (FileOutputStream fileOutputStream = new FileOutputStream(userProfile);
+        BufferedOutputStream outputStream = new 
BufferedOutputStream(fileOutputStream)) {
       // for IOTDB 1.2, the username's length will be stored as a negative 
number.
       byte[] strBuffer = user.getName().getBytes(STRING_ENCODING);
       IOUtils.writeInt(outputStream, -1 * strBuffer.length, 
encodingBufferLocal);
@@ -232,13 +231,12 @@ public class LocalFileUserAccessor implements 
IUserAccessor {
         IOUtils.writePathPrivilege(
             outputStream, pathPrivilege, STRING_ENCODING, encodingBufferLocal);
       }
-
+      outputStream.flush();
+      fileOutputStream.getFD().sync();
     } catch (Exception e) {
+      LOGGER.warn("Get exception when save user {}'s privileges", 
user.getName(), e);
       throw new IOException(e);
     } finally {
-      outputStream.flush();
-      fileOutputStream.getFD().sync();
-      outputStream.close();
       encodingBufferLocal.remove();
     }
 
@@ -250,20 +248,19 @@ public class LocalFileUserAccessor implements 
IUserAccessor {
                 + ROLE_SUFFIX
                 + IoTDBConstant.PROFILE_SUFFIX
                 + TEMP_SUFFIX);
-    fileOutputStream = new FileOutputStream(roleProfile);
-    outputStream = new BufferedOutputStream(fileOutputStream);
-    try {
+    try (FileOutputStream fileOutputStream = new FileOutputStream(roleProfile);
+        BufferedOutputStream outputStream = new 
BufferedOutputStream(fileOutputStream)) {
       int userNum = user.getRoleList().size();
       for (int i = 0; i < userNum; i++) {
         IOUtils.writeString(
             outputStream, user.getRoleList().get(i), STRING_ENCODING, 
encodingBufferLocal);
       }
+      outputStream.flush();
+      fileOutputStream.getFD().sync();
     } catch (Exception e) {
+      LOGGER.warn("Get Exception when save user {}'s roles", user.getName(), 
e);
       throw new IOException(e);
     } finally {
-      outputStream.flush();
-      fileOutputStream.getFD().sync();
-      outputStream.close();
       encodingBufferLocal.remove();
     }
 
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserManager.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserManager.java
index 2017a06ec8b..fa6536d572b 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserManager.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/user/LocalFileUserManager.java
@@ -45,5 +45,10 @@ public class LocalFileUserManager extends BasicUserManager {
   @Override
   public void processLoadSnapshot(File snapshotDir) throws TException, 
IOException {
     accessor.processLoadSnapshot(snapshotDir);
+    try {
+      super.reset();
+    } catch (AuthException e) {
+      throw new IOException(e);
+    }
   }
 }
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
index 584fba2ab5f..015621b0bfe 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java
@@ -113,12 +113,18 @@ public class FileUtils {
       } else {
         // copy file
         try (BufferedInputStream in = new BufferedInputStream(new 
FileInputStream(file));
-            BufferedOutputStream out = new BufferedOutputStream(new 
FileOutputStream(targetFile))) {
+            FileOutputStream fileOutputStream = new 
FileOutputStream(targetFile);
+            BufferedOutputStream out = new 
BufferedOutputStream(fileOutputStream)) {
           byte[] bytes = new byte[BUFFER_SIZE];
           int size = 0;
           while ((size = in.read(bytes)) > 0) {
             out.write(bytes, 0, size);
           }
+          out.flush();
+          fileOutputStream.getFD().sync(); // after try block, stream will 
call close automatically
+        } catch (IOException e) {
+          LOGGER.warn("get ioexception on file {}", file.getAbsolutePath(), e);
+          throw e;
         }
       }
     }

Reply via email to