Caideyipi commented on code in PR #17609:
URL: https://github.com/apache/iotdb/pull/17609#discussion_r4309969142


##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/consensus/statemachine/ConfigRegionStateMachine.java:
##########
@@ -579,60 +602,84 @@ public boolean isReadOnly() {
     return CommonDescriptor.getInstance().getConfig().isReadOnly();
   }
 
-  private void writeLogForSimpleConsensus(ConfigPhysicalPlan plan) {
-    if (simpleLogFile.length() > LOG_FILE_MAX_SIZE) {
-      try {
-        simpleLogWriter.force();
-        File completedFilePath = new File(FILE_PATH + startIndex + "_" + 
endIndex);
-        Files.move(
-            simpleLogFile.toPath(), completedFilePath.toPath(), 
StandardCopyOption.ATOMIC_MOVE);
-      } catch (IOException e) {
-        LOGGER.error(
-            
ConfigNodeMessages.CAN_T_FORCE_LOGWRITER_FOR_CONFIGNODE_SIMPLECONSENSUS_MODE, 
e);
+  private SimpleConsensusPersistResult 
persistPlanForSimpleConsensus(ConfigPhysicalPlan plan) {
+    File persistedLogFile = null;
+    long logFileSizeBeforeWrite = 0;
+    int endIndexBeforeWrite = endIndex;
+    try {
+      if (simpleLogWriter == null || simpleLogFile == null) {
+        throw new 
IOException(ConfigNodeMessages.SIMPLECONSENSUS_LOG_WRITER_IS_NOT_INITIALIZED);
       }
-      for (int retry = 0; retry < 5; retry++) {
-        try {
-          simpleLogWriter.close();
-        } catch (IOException e) {
-          LOGGER.warn(
-              ConfigNodeMessages
-                  
.LOG_CONFIGREGION_UPDATE_FAILED_TO_CLOSE_THE_CONFIGNODE_SIMPLECONSENSUS_LOG_FILEPATH_ARG_RETRY_ARG_188A702E,
-              simpleLogFile.getAbsolutePath(),
-              retry);
-          try {
-            // Sleep 1s and retry
-            TimeUnit.SECONDS.sleep(1);
-          } catch (InterruptedException e2) {
-            Thread.currentThread().interrupt();
-            LOGGER.warn(
-                
ConfigNodeMessages.UNEXPECTED_INTERRUPTION_DURING_THE_CLOSE_METHOD_OF_LOGWRITER);
-          }
-          continue;
-        }
-        break;
+
+      if (simpleLogFile.length() > LOG_FILE_MAX_SIZE) {
+        rollSimpleConsensusLogFile();
       }
-      startIndex = endIndex + 1;
-      createLogFile(startIndex);
-    }
 
-    try {
+      persistedLogFile = simpleLogFile;
+      logFileSizeBeforeWrite = persistedLogFile.length();
+      endIndexBeforeWrite = endIndex;
+
       ByteBuffer buffer = plan.serializeToByteBuffer();
       buffer.position(buffer.limit());
       simpleLogWriter.write(buffer);
+      simpleLogWriter.force();
 
       endIndex = endIndex + 1;
     } catch (Exception e) {
       LOGGER.error(
           ConfigNodeMessages
-              
.CAN_T_SERIALIZE_CURRENT_CONFIGPHYSICALPLAN_FOR_CONFIGNODE_SIMPLECONSENSUS_MODE,
+              
.PERSIST_CURRENT_CONFIGPHYSICALPLAN_FOR_CONFIGNODE_SIMPLECONSENSUS_MODE_FAILED,
           e);
+      return SimpleConsensusPersistResult.failure(

Review Comment:
   Fixed in 
[1e1ffe04c8d](https://github.com/apache/iotdb/commit/1e1ffe04c8d035f46815ac91334a0d0183e9a913).
 Append and `force()` failures now restore the saved pre-write WAL offset and 
force-sync the truncation before another write is accepted. If cleanup cannot 
complete, further SimpleConsensus writes are rejected until restart, with an 
explicit status and operator log.
   
   Regression tests cover a complete appended record followed by a force 
failure, a partial append failure, and cleanup failure, in addition to the 
existing executor-failure test. The replay checks preserve the preceding 
accepted plan, exclude the failed plan, and verify that a subsequent write 
succeeds after successful cleanup.



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/node/NodeManager.java:
##########
@@ -1385,6 +1412,144 @@ public TDataNodeLocation 
getLowestLoadDataNode(Set<Integer> nodes) {
     return getRegisteredDataNode(dataNodeId).getLocation();
   }
 
+  private TSStatus writeConfigPhysicalPlan(ConfigPhysicalPlan plan) {
+    try {
+      return getConsensusManager().write(plan);
+    } catch (ConsensusException e) {
+      LOGGER.warn(CONSENSUS_WRITE_ERROR, e);
+      return new TSStatus(TSStatusCode.EXECUTE_STATEMENT_ERROR.getStatusCode())
+          .setMessage(e.getMessage());
+    }
+  }
+
+  private boolean isConsensusWriteSuccessful(TSStatus status) {
+    return status != null && status.getCode() == 
TSStatusCode.SUCCESS_STATUS.getStatusCode();
+  }
+
+  private TSStatus rollbackDataNodeRegistration(
+      TDataNodeLocation dataNodeLocation, TSStatus versionUpdateStatus) {
+    final TSStatus rollbackStatus =
+        writeConfigPhysicalPlan(
+            new 
RemoveDataNodePlan(Collections.singletonList(dataNodeLocation)));
+    final String failureMessage =
+        String.format(
+            "Failed to persist version info for DataNode %d: %s",
+            dataNodeLocation.getDataNodeId(), 
describeStatus(versionUpdateStatus));
+    if (isConsensusWriteSuccessful(rollbackStatus)) {
+      return buildStatus(
+          versionUpdateStatus.getCode(),
+          failureMessage,
+          "The registration has been rolled back. Please retry the 
registration.");
+    }
+
+    LOGGER.error(
+        "Failed to roll back DataNode registration {} after version info 
persistence failure. "
+            + ROLLBACK_STATUS_LOG_FORMAT,
+        dataNodeLocation,
+        versionUpdateStatus,
+        rollbackStatus);
+    return buildStatus(
+        rollbackStatus.getCode(),
+        failureMessage,
+        String.format(REGISTRATION_ROLLBACK_FAILED_FORMAT, 
describeStatus(rollbackStatus)),
+        MANUAL_REGISTRATION_CLEANUP_MESSAGE);
+  }
+
+  private TSStatus rollbackAiNodeRegistration(
+      TAINodeLocation aiNodeLocation, TSStatus versionUpdateStatus) {
+    final TSStatus rollbackStatus = writeConfigPhysicalPlan(new 
RemoveAINodePlan(aiNodeLocation));
+    final String failureMessage =
+        String.format(
+            "Failed to persist version info for AINode %d: %s",
+            aiNodeLocation.getAiNodeId(), describeStatus(versionUpdateStatus));
+    if (isConsensusWriteSuccessful(rollbackStatus)) {
+      return buildStatus(
+          versionUpdateStatus.getCode(),
+          failureMessage,
+          "The registration has been rolled back. Please retry the 
registration.");
+    }
+
+    LOGGER.error(
+        "Failed to roll back AINode registration {} after version info 
persistence failure. "
+            + ROLLBACK_STATUS_LOG_FORMAT,
+        aiNodeLocation,
+        versionUpdateStatus,
+        rollbackStatus);
+    return buildStatus(
+        rollbackStatus.getCode(),
+        failureMessage,
+        String.format(REGISTRATION_ROLLBACK_FAILED_FORMAT, 
describeStatus(rollbackStatus)),
+        MANUAL_REGISTRATION_CLEANUP_MESSAGE);
+  }
+
+  private TSStatus rollbackConfigNodeRegistration(
+      TConfigNodeLocation configNodeLocation, TSStatus versionUpdateStatus) {
+    final TSStatus rollbackStatus =
+        writeConfigPhysicalPlan(new RemoveConfigNodePlan(configNodeLocation));

Review Comment:
   Fixed in 
[1e1ffe04c8d](https://github.com/apache/iotdb/commit/1e1ffe04c8d035f46815ac91334a0d0183e9a913).
 A failed ConfigNode version write no longer submits `RemoveConfigNodePlan`. 
Since `ADD_PEER` has already joined the node to Raft, its membership is 
retained and the failure is propagated so `REGISTER_SUCCESS` can retry the 
idempotent registration and version plans. Heartbeat-cache creation remains 
after both writes succeed.
   
   The regression test applies the plans through a real `NodeInfo` as the 
joining node. It verifies that the node remains Running after the version-write 
failure and after a successful retry, registration contains one member, the 
version is updated, and no removal plan is written.



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/node/NodeManager.java:
##########
@@ -1385,6 +1412,144 @@ public TDataNodeLocation 
getLowestLoadDataNode(Set<Integer> nodes) {
     return getRegisteredDataNode(dataNodeId).getLocation();
   }
 
+  private TSStatus writeConfigPhysicalPlan(ConfigPhysicalPlan plan) {
+    try {
+      return getConsensusManager().write(plan);
+    } catch (ConsensusException e) {
+      LOGGER.warn(CONSENSUS_WRITE_ERROR, e);
+      return new TSStatus(TSStatusCode.EXECUTE_STATEMENT_ERROR.getStatusCode())
+          .setMessage(e.getMessage());
+    }
+  }
+
+  private boolean isConsensusWriteSuccessful(TSStatus status) {
+    return status != null && status.getCode() == 
TSStatusCode.SUCCESS_STATUS.getStatusCode();
+  }
+
+  private TSStatus rollbackDataNodeRegistration(
+      TDataNodeLocation dataNodeLocation, TSStatus versionUpdateStatus) {
+    final TSStatus rollbackStatus =
+        writeConfigPhysicalPlan(
+            new 
RemoveDataNodePlan(Collections.singletonList(dataNodeLocation)));
+    final String failureMessage =
+        String.format(
+            "Failed to persist version info for DataNode %d: %s",
+            dataNodeLocation.getDataNodeId(), 
describeStatus(versionUpdateStatus));

Review Comment:
   Fixed in 
[1e1ffe04c8d](https://github.com/apache/iotdb/commit/1e1ffe04c8d035f46815ac91334a0d0183e9a913).
 The registration failure statuses, rollback logs, exception templates, and 
fallback status descriptions now use matching English and Chinese 
`ConfigNodeMessages` constants. Each outcome uses a complete format template, 
and the raw English fragment constants were removed. The new WAL failure 
messages are also localized.
   
   Full-reactor test compilation passed for both locales; all 431 ConfigNode 
message keys and placeholder counts match. A scan found no added raw English 
literals in main Java, and all 12 focused WAL/registration tests passed in both 
locales.



##########
iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/partition/DatabasePartitionTable.java:
##########
@@ -243,7 +243,7 @@ public int getRegionGroupCount(TConsensusGroupType type) {
                 result.getAndIncrement();
               }
             });
-    return result.getAndIncrement();
+    return result.get();

Review Comment:
   Correction to my earlier reply: `result` is a local counter, and 
`getAndIncrement()` returns its value before incrementing it. Replacing it with 
`get()` leaves the returned count unchanged; this removes an unused increment 
rather than fixing an off-by-one bug. I have corrected item 4 in the PR 
description accordingly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to