From 7512587747fa02dc4475ddc1b380c32775936eea Mon Sep 17 00:00:00 2001 From: zyxxoo <1318247699@qq.com> Date: Fri, 15 Oct 2021 12:23:25 +0800 Subject: [PATCH] Fix: quorum read if safe read (#1618) Co-authored-by: vaughn.zhang --- .../hugegraph/backend/store/raft/RaftBackendStore.java | 2 +- .../hugegraph/backend/store/raft/RaftSharedContext.java | 4 ++++ .../main/java/com/baidu/hugegraph/config/CoreOptions.java | 8 ++++++++ .../src/assembly/static/conf/hugegraph.properties | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftBackendStore.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftBackendStore.java index 259b6ef576..17f55b192e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftBackendStore.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftBackendStore.java @@ -200,7 +200,7 @@ private Object submitAndWait(StoreCommand command) { } private Object queryByRaft(Object query, Function func) { - if (this.node().selfIsLeader() || !this.context.isSafeRead()) { + if (!this.context.isSafeRead()) { return func.apply(query); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftSharedContext.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftSharedContext.java index 7a609a6e00..75a512da41 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftSharedContext.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftSharedContext.java @@ -30,6 +30,7 @@ import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; +import com.alipay.sofa.jraft.option.ReadOnlyOption; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; @@ -258,6 +259,9 @@ public NodeOptions nodeOptions() throws IOException { raftOptions.setReplicatorPipeline( config.get(CoreOptions.RAFT_REPLICATOR_PIPELINE)); raftOptions.setOpenStatistics(false); + raftOptions.setReadOnlyOptions( + ReadOnlyOption.valueOf( + config.get(CoreOptions.RAFT_READ_STRATEGY))); return nodeOptions; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java index 501d311ecf..7e6a2891a6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java @@ -195,6 +195,14 @@ public static synchronized CoreOptions instance() { 8 ); + public static final ConfigOption RAFT_READ_STRATEGY = + new ConfigOption<>( + "raft.read_strategy", + "The linearizability of read strategy.", + allowValues("ReadOnlyLeaseBased", "ReadOnlySafe"), + "ReadOnlyLeaseBased" + ); + public static final ConfigOption RAFT_APPLY_BATCH = new ConfigOption<>( "raft.apply_batch", diff --git a/hugegraph-dist/src/assembly/static/conf/hugegraph.properties b/hugegraph-dist/src/assembly/static/conf/hugegraph.properties index 24ce42e64c..80b23e2e6a 100644 --- a/hugegraph-dist/src/assembly/static/conf/hugegraph.properties +++ b/hugegraph-dist/src/assembly/static/conf/hugegraph.properties @@ -33,6 +33,7 @@ raft.election_timeout=10000 raft.snapshot_interval=3600 raft.backend_threads=48 raft.read_index_threads=8 +raft.read_strategy=ReadOnlyLeaseBased raft.queue_size=16384 raft.queue_publish_timeout=60 raft.apply_batch=1