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

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


The following commit(s) were added to refs/heads/master by this push:
     new 64ae88f01 RATIS-2097 Add a conf to enable/disable Metadata entries. 
(#1101)
64ae88f01 is described below

commit 64ae88f017cb0fd5649bd465027bd524296ada11
Author: Potato <[email protected]>
AuthorDate: Fri May 24 16:25:18 2024 +0800

    RATIS-2097 Add a conf to enable/disable Metadata entries. (#1101)
---
 .../apache/ratis/server/RaftServerConfigKeys.java  | 12 ++++
 .../apache/ratis/server/impl/LeaderStateImpl.java  |  6 +-
 .../apache/ratis/server/impl/LogMetadataTests.java | 80 ++++++++++++++++++++++
 .../ratis/grpc/TestLogMetadataTestsWithGrpc.java   | 27 ++++++++
 4 files changed, 124 insertions(+), 1 deletion(-)

diff --git 
a/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java
 
b/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java
index f6dcbaa66..8e1d92b75 100644
--- 
a/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java
+++ 
b/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java
@@ -475,6 +475,18 @@ public interface RaftServerConfigKeys {
       setBoolean(properties::setBoolean, ASYNC_FLUSH_ENABLED_KEY, asyncFlush);
     }
 
+    /** Log metadata can guarantee that a server can recover commit index and 
state machine
+     * even if a majority of servers are dead by consuming a little extra 
space. */
+    String LOG_METADATA_ENABLED_KEY = PREFIX + ".log-metadata.enabled";
+    boolean LOG_METADATA_ENABLED_DEFAULT = true;
+    static boolean logMetadataEnabled(RaftProperties properties) {
+      return getBoolean(properties::getBoolean,
+          LOG_METADATA_ENABLED_KEY, LOG_METADATA_ENABLED_DEFAULT, 
getDefaultLog());
+    }
+    static void setLogMetadataEnabled(RaftProperties properties, boolean 
logMetadata) {
+      setBoolean(properties::setBoolean, LOG_METADATA_ENABLED_KEY, 
logMetadata);
+    }
+
     /** The policy to handle corrupted raft log. */
     enum CorruptionPolicy {
       /** Rethrow the exception. */
diff --git 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java
index 28f2350c7..cea8b585e 100644
--- 
a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java
+++ 
b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java
@@ -352,6 +352,7 @@ class LeaderStateImpl implements LeaderState {
   private final MemoizedSupplier<StartupLogEntry> startupLogEntry = 
MemoizedSupplier.valueOf(StartupLogEntry::new);
   private final AtomicBoolean isStopped = new AtomicBoolean();
 
+  private final boolean logMetadataEnabled;
   private final int stagingCatchupGap;
   private final RaftServerMetricsImpl raftServerMetrics;
   private final LogAppenderMetrics logAppenderMetrics;
@@ -382,6 +383,7 @@ class LeaderStateImpl implements LeaderState {
     this.pendingStepDown = new PendingStepDown(this);
     this.readIndexHeartbeats = new ReadIndexHeartbeats();
     this.lease = new LeaderLease(properties);
+    this.logMetadataEnabled = 
RaftServerConfigKeys.Log.logMetadataEnabled(properties);
     long maxPendingRequests = 
RaftServerConfigKeys.Write.elementLimit(properties);
     double followerGapRatioMax = 
RaftServerConfigKeys.Write.followerGapRatioMax(properties);
 
@@ -946,7 +948,9 @@ class LeaderStateImpl implements LeaderState {
 
   private void updateCommit(LogEntryHeader[] entriesToCommit) {
     final long newCommitIndex = raftLog.getLastCommittedIndex();
-    logMetadata(newCommitIndex);
+    if (logMetadataEnabled) {
+      logMetadata(newCommitIndex);
+    }
     commitIndexChanged();
 
     boolean hasConfiguration = false;
diff --git 
a/ratis-server/src/test/java/org/apache/ratis/server/impl/LogMetadataTests.java 
b/ratis-server/src/test/java/org/apache/ratis/server/impl/LogMetadataTests.java
new file mode 100644
index 000000000..a834e2714
--- /dev/null
+++ 
b/ratis-server/src/test/java/org/apache/ratis/server/impl/LogMetadataTests.java
@@ -0,0 +1,80 @@
+/*
+ * 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.ratis.server.impl;
+
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import org.apache.ratis.BaseTest;
+import org.apache.ratis.RaftTestUtil;
+import org.apache.ratis.conf.RaftProperties;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.server.RaftServer;
+import org.apache.ratis.server.RaftServerConfigKeys;
+import org.apache.ratis.server.raftlog.RaftLog;
+import org.junit.Assert;
+import org.junit.Test;
+
+public abstract class LogMetadataTests<CLUSTER extends MiniRaftCluster>
+    extends BaseTest
+    implements MiniRaftCluster.Factory.Get<CLUSTER> {
+
+  @Test
+  public void testLogMetadataEnabled() throws Exception {
+    testLogMetadataBasicTest(true, x -> x > RaftLog.INVALID_LOG_INDEX);
+  }
+
+  @Test
+  public void testLogMetadataDisabled() throws Exception {
+    testLogMetadataBasicTest(false, x -> x == RaftLog.INVALID_LOG_INDEX);
+  }
+
+  public void testLogMetadataBasicTest(boolean logMetadata, Predicate<Long> 
checker)
+      throws Exception {
+    final RaftProperties prop = getProperties();
+    RaftServerConfigKeys.Log.setLogMetadataEnabled(prop, logMetadata);
+
+    final MiniRaftCluster cluster = newCluster(3);
+    try {
+      cluster.start();
+      RaftTestUtil.waitForLeader(cluster);
+      final RaftServer.Division leader = cluster.getLeader();
+      RaftPeerId leaderId = leader.getId();
+
+      cluster.getLeaderAndSendFirstMessage(true);
+
+      // kill majority servers
+      for (RaftPeerId id : 
cluster.getGroup().getPeers().stream().map(RaftPeer::getId)
+          .filter(x -> !x.equals(leaderId)).collect(Collectors.toList())) {
+        cluster.killServer(id);
+      }
+
+      // only restart one server
+      cluster.restartServer(leaderId, false);
+
+      long commitIndex = 
cluster.getServer(leaderId).getDivision(cluster.getGroupId()).getRaftLog()
+          .getLastCommittedIndex();
+
+      Assert.assertTrue(checker.test(commitIndex));
+    } finally {
+      cluster.shutdown();
+    }
+  }
+}
diff --git 
a/ratis-test/src/test/java/org/apache/ratis/grpc/TestLogMetadataTestsWithGrpc.java
 
b/ratis-test/src/test/java/org/apache/ratis/grpc/TestLogMetadataTestsWithGrpc.java
new file mode 100644
index 000000000..1c69c42bb
--- /dev/null
+++ 
b/ratis-test/src/test/java/org/apache/ratis/grpc/TestLogMetadataTestsWithGrpc.java
@@ -0,0 +1,27 @@
+/*
+ * 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.ratis.grpc;
+
+import org.apache.ratis.server.impl.LogMetadataTests;
+
+public class TestLogMetadataTestsWithGrpc extends 
LogMetadataTests<MiniRaftClusterWithGrpc>
+    implements MiniRaftClusterWithGrpc.FactoryGet{
+
+}

Reply via email to