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

haonan 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 aa5f219a82 [IOTDB-3665] Optimization of configuring consensus protocol 
type on data node (#6466)
aa5f219a82 is described below

commit aa5f219a82bac888944e830373a0392870fd858a
Author: Mrquan <[email protected]>
AuthorDate: Mon Jun 27 21:37:21 2022 +0800

    [IOTDB-3665] Optimization of configuring consensus protocol type on data 
node (#6466)
---
 .../org/apache/iotdb/db/conf/IoTDBDescriptor.java  |  3 --
 .../org/apache/iotdb/db/conf/IoTDBStartCheck.java  | 56 +++++++++++++++++++++-
 .../java/org/apache/iotdb/db/service/DataNode.java | 21 ++++++++
 3 files changed, 76 insertions(+), 4 deletions(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java 
b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
index ce117a7f89..66ed7c4e9d 100644
--- a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
+++ b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
@@ -1712,9 +1712,6 @@ public class IoTDBDescriptor {
   // These configurations are received from config node when registering
   public void loadGlobalConfig(TGlobalConfig globalConfig) {
     
conf.setSeriesPartitionExecutorClass(globalConfig.getSeriesPartitionExecutorClass());
-    
conf.setDataRegionConsensusProtocolClass(globalConfig.getDataRegionConsensusProtocolClass());
-    conf.setSchemaRegionConsensusProtocolClass(
-        globalConfig.getSchemaRegionConsensusProtocolClass());
     conf.setSeriesPartitionSlotNum(globalConfig.getSeriesPartitionSlotNum());
     conf.setPartitionInterval(globalConfig.timePartitionInterval);
   }
diff --git a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBStartCheck.java 
b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBStartCheck.java
index c7e9be67e3..ec23692ef3 100644
--- a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBStartCheck.java
+++ b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBStartCheck.java
@@ -18,6 +18,7 @@
  */
 package org.apache.iotdb.db.conf;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.commons.conf.IoTDBConstant;
 import org.apache.iotdb.commons.exception.ConfigurationException;
 import org.apache.iotdb.commons.file.SystemFileFactory;
@@ -108,6 +109,9 @@ public class IoTDBStartCheck {
 
   private static final String DATA_NODE_ID = "data_node_id";
 
+  private static final String SCHEMA_REGION_CONSENSUS_PROTOCOL = 
"schema_region_consensus_protocol";
+
+  private static final String DATA_REGION_CONSENSUS_PROTOCOL = 
"data_region_consensus_protocol";
   private static final String IOTDB_VERSION_STRING = "iotdb_version";
 
   public static IoTDBStartCheck getInstance() {
@@ -377,10 +381,20 @@ public class IoTDBStartCheck {
       throwException(SCHEMA_ENGINE_MODE, schemaEngineMode);
     }
 
-    // properties contain DATA_NODE_ID only when start as Data node
+    // load configuration from system properties only when start as Data node
     if (properties.containsKey(DATA_NODE_ID)) {
       
config.setDataNodeId(Integer.parseInt(properties.getProperty(DATA_NODE_ID)));
     }
+
+    if (properties.containsKey(SCHEMA_REGION_CONSENSUS_PROTOCOL)) {
+      config.setSchemaRegionConsensusProtocolClass(
+          properties.getProperty(SCHEMA_REGION_CONSENSUS_PROTOCOL));
+    }
+
+    if (properties.containsKey(DATA_REGION_CONSENSUS_PROTOCOL)) {
+      config.setDataRegionConsensusProtocolClass(
+          properties.getProperty(DATA_REGION_CONSENSUS_PROTOCOL));
+    }
   }
 
   private void throwException(String parameter, Object badValue) throws 
ConfigurationException {
@@ -420,4 +434,44 @@ public class IoTDBStartCheck {
     // rename system.properties.tmp to system.properties
     FileUtils.moveFile(tmpPropertiesFile, propertiesFile);
   }
+
+  /** call this method to serialize consensus protocol */
+  public void serializeConsensusProtocol(String regionConsensusProtocol, 
TConsensusGroupType type)
+      throws IOException {
+    // create an empty tmpPropertiesFile
+    if (tmpPropertiesFile.createNewFile()) {
+      logger.info("Create system.properties.tmp {}.", tmpPropertiesFile);
+    } else {
+      logger.error("Create system.properties.tmp {} failed.", 
tmpPropertiesFile);
+      System.exit(-1);
+    }
+
+    reloadProperties();
+
+    try (FileOutputStream tmpFOS = new 
FileOutputStream(tmpPropertiesFile.toString())) {
+      if (type == TConsensusGroupType.DataRegion) {
+        properties.setProperty(DATA_REGION_CONSENSUS_PROTOCOL, 
regionConsensusProtocol);
+      } else if (type == TConsensusGroupType.SchemaRegion) {
+        properties.setProperty(SCHEMA_REGION_CONSENSUS_PROTOCOL, 
regionConsensusProtocol);
+      }
+      properties.store(tmpFOS, SYSTEM_PROPERTIES_STRING);
+      // serialize finished, delete old system.properties file
+      if (propertiesFile.exists()) {
+        Files.delete(propertiesFile.toPath());
+      }
+    }
+    // rename system.properties.tmp to system.properties
+    FileUtils.moveFile(tmpPropertiesFile, propertiesFile);
+  }
+
+  public boolean checkConsensusProtocolExists(TConsensusGroupType type) {
+    if (type == TConsensusGroupType.DataRegion) {
+      return properties.containsKey(DATA_REGION_CONSENSUS_PROTOCOL);
+    } else if (type == TConsensusGroupType.SchemaRegion) {
+      return properties.containsKey(SCHEMA_REGION_CONSENSUS_PROTOCOL);
+    }
+
+    logger.error("Unexpected consensus group type");
+    return false;
+  }
 }
diff --git a/server/src/main/java/org/apache/iotdb/db/service/DataNode.java 
b/server/src/main/java/org/apache/iotdb/db/service/DataNode.java
index be7d75dc3a..cb48d576aa 100644
--- a/server/src/main/java/org/apache/iotdb/db/service/DataNode.java
+++ b/server/src/main/java/org/apache/iotdb/db/service/DataNode.java
@@ -19,6 +19,7 @@
 package org.apache.iotdb.db.service;
 
 import org.apache.iotdb.common.rpc.thrift.TConfigNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TDataNodeInfo;
 import org.apache.iotdb.common.rpc.thrift.TDataNodeLocation;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
@@ -215,6 +216,26 @@ public class DataNode implements DataNodeMBean {
             config.setDataNodeId(dataNodeID);
           }
           
IoTDBDescriptor.getInstance().loadGlobalConfig(dataNodeRegisterResp.globalConfig);
+
+          if (!IoTDBStartCheck.getInstance()
+              .checkConsensusProtocolExists(TConsensusGroupType.DataRegion)) {
+            config.setDataRegionConsensusProtocolClass(
+                
dataNodeRegisterResp.globalConfig.getDataRegionConsensusProtocolClass());
+            IoTDBStartCheck.getInstance()
+                .serializeConsensusProtocol(
+                    
dataNodeRegisterResp.globalConfig.getDataRegionConsensusProtocolClass(),
+                    TConsensusGroupType.DataRegion);
+          }
+
+          if (!IoTDBStartCheck.getInstance()
+              .checkConsensusProtocolExists(TConsensusGroupType.SchemaRegion)) 
{
+            config.setSchemaRegionConsensusProtocolClass(
+                
dataNodeRegisterResp.globalConfig.getSchemaRegionConsensusProtocolClass());
+            IoTDBStartCheck.getInstance()
+                .serializeConsensusProtocol(
+                    
dataNodeRegisterResp.globalConfig.getSchemaRegionConsensusProtocolClass(),
+                    TConsensusGroupType.SchemaRegion);
+          }
           logger.info("Register to the cluster successfully");
           return;
         }

Reply via email to