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 6365fcddce [IOTDB-4137] Reject write when Peer in ReadOnly state in 
RatisConsensus (#7216)
6365fcddce is described below

commit 6365fcddce7e0255f82130b4cbbc0c40dfefb92c
Author: William Song <[email protected]>
AuthorDate: Sat Sep 3 12:26:34 2022 +0800

    [IOTDB-4137] Reject write when Peer in ReadOnly state in RatisConsensus 
(#7216)
---
 .../consensus/exception/NodeReadOnlyException.java | 30 +++++++++++++++++++
 .../iotdb/consensus/ratis/RatisConsensus.java      | 34 +++++++++++++++++++---
 2 files changed, 60 insertions(+), 4 deletions(-)

diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/exception/NodeReadOnlyException.java
 
b/consensus/src/main/java/org/apache/iotdb/consensus/exception/NodeReadOnlyException.java
new file mode 100644
index 0000000000..cd733bc85c
--- /dev/null
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/exception/NodeReadOnlyException.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.consensus.exception;
+
+import org.apache.ratis.protocol.RaftPeer;
+
+public class NodeReadOnlyException extends ConsensusException {
+  public NodeReadOnlyException(RaftPeer peer) {
+    super(
+        String.format(
+            "Current Peer %s in Address %s is in Read Only State",
+            peer.getId(), peer.getAddress()));
+  }
+}
diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
index 0b57fdcc87..fcab610918 100644
--- 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
@@ -26,6 +26,7 @@ import org.apache.iotdb.commons.client.ClientManager;
 import org.apache.iotdb.commons.client.ClientPoolProperty;
 import org.apache.iotdb.commons.client.IClientManager;
 import org.apache.iotdb.commons.client.IClientPoolFactory;
+import org.apache.iotdb.commons.conf.CommonDescriptor;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
 import org.apache.iotdb.commons.utils.TestOnly;
 import org.apache.iotdb.consensus.IConsensus;
@@ -39,6 +40,7 @@ import 
org.apache.iotdb.consensus.common.response.ConsensusWriteResponse;
 import org.apache.iotdb.consensus.config.ConsensusConfig;
 import org.apache.iotdb.consensus.exception.ConsensusException;
 import org.apache.iotdb.consensus.exception.ConsensusGroupNotExistException;
+import org.apache.iotdb.consensus.exception.NodeReadOnlyException;
 import 
org.apache.iotdb.consensus.exception.PeerAlreadyInConsensusGroupException;
 import org.apache.iotdb.consensus.exception.PeerNotInConsensusGroupException;
 import org.apache.iotdb.consensus.exception.RatisRequestFailedException;
@@ -161,6 +163,16 @@ class RatisConsensus implements IConsensus {
       return failedWrite(new 
ConsensusGroupNotExistException(consensusGroupId));
     }
 
+    // current Peer is group leader and in ReadOnly State
+    if (isLeader(consensusGroupId) && 
CommonDescriptor.getInstance().getConfig().isReadOnly()) {
+      try {
+        forceStepDownLeader(raftGroup);
+      } catch (IOException e) {
+        logger.warn("leader {} read only, force step down failed due to {}", 
myself, e);
+      }
+      return failedWrite(new NodeReadOnlyException(myself));
+    }
+
     // serialize request into Message
     Message message = new RequestMessage(IConsensusRequest);
 
@@ -456,10 +468,8 @@ class RatisConsensus implements IConsensus {
       if (!configChangeReply.isSuccess()) {
         return failed(new 
RatisRequestFailedException(configChangeReply.getException()));
       }
-      // TODO tuning for timeoutMs
-      // when newLeaderPeerId == null, ratis forces current leader to step 
down and raise new
-      // election
-      reply = client.getRaftClient().admin().transferLeadership(null, 5000);
+
+      reply = forceStepDownLeader(raftGroup);
       if (!reply.isSuccess()) {
         return failed(new RatisRequestFailedException(reply.getException()));
       }
@@ -473,6 +483,22 @@ class RatisConsensus implements IConsensus {
     return 
ConsensusGenericResponse.newBuilder().setSuccess(reply.isSuccess()).build();
   }
 
+  // TODO when Ratis implements read leader transfer mechanism, change this 
implementation
+  private RaftClientReply forceStepDownLeader(RaftGroup group) throws 
IOException {
+    RatisClient client = null;
+    try {
+      client = getRaftClient(group);
+      // TODO tuning for timeoutMs
+      // when newLeaderPeerId == null, ratis forces current leader to step 
down and raise new
+      // election
+      return client.getRaftClient().admin().transferLeadership(null, 5000);
+    } finally {
+      if (client != null) {
+        client.returnSelf();
+      }
+    }
+  }
+
   @Override
   public boolean isLeader(ConsensusGroupId groupId) {
     RaftGroupId raftGroupId = Utils.fromConsensusGroupIdToRaftGroupId(groupId);

Reply via email to