From d85ee50d5feeb580683f98f78ff1d16b823c1dec Mon Sep 17 00:00:00 2001 From: imbajin <17706099+imbajin@users.noreply.github.com> Date: Wed, 20 Apr 2022 22:41:05 +0800 Subject: [PATCH 1/5] chore: replace mysql actions by official docker image (#1834) * chore: replace mysql actions by official docker image * update codecov v3 * move traivis file --- .travis.yml => .github/outdated/.travis.yml | 0 .../outdated/APACHE_PROPOSAL.md | 0 .github/{workflows => outdated}/cla.yml | 0 .github/workflows/ci.yml | 26 +++++++++---------- .../travis/install-mysql-via-docker.sh | 4 +-- 5 files changed, 14 insertions(+), 16 deletions(-) rename .travis.yml => .github/outdated/.travis.yml (100%) rename APACHE_PROPOSAL.md => .github/outdated/APACHE_PROPOSAL.md (100%) rename .github/{workflows => outdated}/cla.yml (100%) diff --git a/.travis.yml b/.github/outdated/.travis.yml similarity index 100% rename from .travis.yml rename to .github/outdated/.travis.yml diff --git a/APACHE_PROPOSAL.md b/.github/outdated/APACHE_PROPOSAL.md similarity index 100% rename from APACHE_PROPOSAL.md rename to .github/outdated/APACHE_PROPOSAL.md diff --git a/.github/workflows/cla.yml b/.github/outdated/cla.yml similarity index 100% rename from .github/workflows/cla.yml rename to .github/outdated/cla.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dad6487eb6..b0a1169752 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,19 +56,17 @@ jobs: run: | $TRAVIS_DIR/install-backend.sh $BACKEND - - name: Init MySQL Env - if: ${{ env.BACKEND == 'mysql' }} - uses: mirromutth/mysql-action@v1.1 - with: - host port: 3306 # Optional, default value is 3306. The port of host - container port: 3306 # Optional, default value is 3306. The port of container - character set server: 'utf8mb4' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld - collation server: 'utf8mb4_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld - mysql version: '5.7' # Optional, default value is "latest". The version of the MySQL - mysql database: 'test' # Optional, default value is "test". The specified database which will be create - mysql root password: "******" # Required if "mysql user" is empty, default is empty. The root superuser password - #mysql user: 'root' # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too - #mysql password: ${{ secrets.DatabasePassword }} # Required if "mysql user" exists. The password for the "mysql user" +# - name: Init MySQL Env +# if: ${{ env.BACKEND == 'mysql' }} +# uses: mirromutth/mysql-action@v1.1 +# with: +# host port: 3306 # Optional, default value is 3306. The port of host +# container port: 3306 # Optional, default value is 3306. The port of container +# character set server: 'utf8mb4' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld +# collation server: 'utf8mb4_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld +# mysql version: '5.7' # Optional, default value is "latest". The version of the MySQL +# mysql database: 'test' # Optional, default value is "test". The specified database which will be create +# mysql root password: "******" # Required if "mysql user" is empty, default is empty. The root superuser password - name: Run unit test run: | @@ -93,6 +91,6 @@ jobs: $TRAVIS_DIR/run-tinkerpop-test.sh $BACKEND tinkerpop - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3.0.0 with: file: ${{ env.REPORT_DIR }}/*.xml diff --git a/hugegraph-dist/src/assembly/travis/install-mysql-via-docker.sh b/hugegraph-dist/src/assembly/travis/install-mysql-via-docker.sh index d2e3a8e12f..25cdf9f679 100755 --- a/hugegraph-dist/src/assembly/travis/install-mysql-via-docker.sh +++ b/hugegraph-dist/src/assembly/travis/install-mysql-via-docker.sh @@ -15,5 +15,5 @@ sed -i "s/jdbc.reconnect_interval=.*/jdbc.reconnect_interval=5/" $CONF # Keep for upgrade in future -#docker pull mysql:5.7 -#docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD="******" -d mysql:5.7 +docker pull mysql:5.7 +docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD="******" -d mysql:5.7 From 28ed873360c5c3c8fd6e638c6c31417ae3d982a8 Mon Sep 17 00:00:00 2001 From: imbajin <17706099+imbajin@users.noreply.github.com> Date: Wed, 20 Apr 2022 23:45:26 +0800 Subject: [PATCH 2/5] doc: add repository urls for other modules (#1831) --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 1f59fd1fa2..9ca13208c3 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,12 @@ HugeGraph is a fast-speed and highly-scalable [graph database](https://en.wikipe The project [homepage](https://hugegraph.github.io/hugegraph-doc/) contains more information on HugeGraph and provides links to **documentation**, getting-started guides and release downloads. +And here are links of other repositories: +1. [hugegraph-toolchain](https://github.com/apache/incubator-hugegraph-toolchain) (include loader/dashboard/tool/client) +2. [hugegraph-computer](https://github.com/apache/incubator-hugegraph-computer) (graph computing system) +3. [hugegraph-commons](https://github.com/apache/incubator-hugegraph-commons) (include common & rpc module) +4. [hugegraph-website](https://github.com/apache/incubator-hugegraph-doc) (include doc & website code) + ## Contributing Welcome to contribute to HugeGraph, please see [`How to Contribute`](CONTRIBUTING.md) for more information. From 8d52abb2a0fe76b24830f50e1ffb00e44f38002d Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Thu, 21 Apr 2022 14:15:46 +0800 Subject: [PATCH 3/5] HugeGraph-622: Bump up to version 0.13.0 (#1740) --- hugegraph-api/pom.xml | 2 +- .../src/main/java/com/baidu/hugegraph/version/ApiVersion.java | 2 +- hugegraph-cassandra/pom.xml | 2 +- hugegraph-core/pom.xml | 4 ++-- .../main/java/com/baidu/hugegraph/version/CoreVersion.java | 2 +- hugegraph-dist/pom.xml | 2 +- hugegraph-example/pom.xml | 2 +- hugegraph-hbase/pom.xml | 2 +- hugegraph-mysql/pom.xml | 2 +- hugegraph-palo/pom.xml | 2 +- hugegraph-postgresql/pom.xml | 2 +- hugegraph-rocksdb/pom.xml | 2 +- hugegraph-scylladb/pom.xml | 2 +- hugegraph-test/pom.xml | 2 +- pom.xml | 2 +- 15 files changed, 16 insertions(+), 16 deletions(-) diff --git a/hugegraph-api/pom.xml b/hugegraph-api/pom.xml index e35a3e8000..3632e75d37 100644 --- a/hugegraph-api/pom.xml +++ b/hugegraph-api/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java index d9fb63d0fd..14e2eba7d8 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java @@ -125,6 +125,6 @@ public final class ApiVersion { public static void check() { // Check version of hugegraph-core. Firstly do check from version 0.3 - VersionUtil.check(CoreVersion.VERSION, "0.12", "0.13", CoreVersion.NAME); + VersionUtil.check(CoreVersion.VERSION, "0.13", "0.14", CoreVersion.NAME); } } diff --git a/hugegraph-cassandra/pom.xml b/hugegraph-cassandra/pom.xml index ade3a1b61b..0ff2924688 100644 --- a/hugegraph-cassandra/pom.xml +++ b/hugegraph-cassandra/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-core/pom.xml b/hugegraph-core/pom.xml index 1ca2e0413c..4351d29290 100644 --- a/hugegraph-core/pom.xml +++ b/hugegraph-core/pom.xml @@ -5,7 +5,7 @@ com.baidu.hugegraph hugegraph - 0.12.0 + 0.13.0 ../pom.xml hugegraph-core @@ -243,7 +243,7 @@ - 0.12.0.0 + 0.13.0.0 diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java index d8e845d7ae..4ff12ab021 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java @@ -33,7 +33,7 @@ public class CoreVersion { // The second parameter of Version.of() is for IDE running without JAR public static final Version VERSION = Version.of(CoreVersion.class, - "0.12.0"); + "0.13.0"); public static final String GREMLIN_VERSION = "3.4.3"; diff --git a/hugegraph-dist/pom.xml b/hugegraph-dist/pom.xml index 60e770b3fa..f37cc24759 100644 --- a/hugegraph-dist/pom.xml +++ b/hugegraph-dist/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 hugegraph-dist diff --git a/hugegraph-example/pom.xml b/hugegraph-example/pom.xml index 7ddfc44d50..c22169b55e 100644 --- a/hugegraph-example/pom.xml +++ b/hugegraph-example/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-hbase/pom.xml b/hugegraph-hbase/pom.xml index 9467c7e7b6..93a523d2ef 100644 --- a/hugegraph-hbase/pom.xml +++ b/hugegraph-hbase/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-mysql/pom.xml b/hugegraph-mysql/pom.xml index 2e6f663f5e..ba74b4e831 100644 --- a/hugegraph-mysql/pom.xml +++ b/hugegraph-mysql/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-palo/pom.xml b/hugegraph-palo/pom.xml index 561f5a9ee7..a001a47986 100644 --- a/hugegraph-palo/pom.xml +++ b/hugegraph-palo/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-postgresql/pom.xml b/hugegraph-postgresql/pom.xml index c5012d7abe..0c7f4e6417 100644 --- a/hugegraph-postgresql/pom.xml +++ b/hugegraph-postgresql/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-rocksdb/pom.xml b/hugegraph-rocksdb/pom.xml index ebd88cd1e9..207dd7c34d 100644 --- a/hugegraph-rocksdb/pom.xml +++ b/hugegraph-rocksdb/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-scylladb/pom.xml b/hugegraph-scylladb/pom.xml index 6ba5b8c87f..4897d3b5be 100644 --- a/hugegraph-scylladb/pom.xml +++ b/hugegraph-scylladb/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/hugegraph-test/pom.xml b/hugegraph-test/pom.xml index 0177e38bb3..31190185b7 100644 --- a/hugegraph-test/pom.xml +++ b/hugegraph-test/pom.xml @@ -5,7 +5,7 @@ hugegraph com.baidu.hugegraph - 0.12.0 + 0.13.0 4.0.0 diff --git a/pom.xml b/pom.xml index b66f7988e3..e7b03f1bde 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.baidu.hugegraph hugegraph - 0.12.0 + 0.13.0 pom 3.3.9 From 7d46524b1c7bbb9967c08d46eb51a336b2da635b Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Thu, 21 Apr 2022 18:41:44 +0800 Subject: [PATCH 4/5] improve raft module (#1802) --- .../backend/store/raft/RaftBackendStore.java | 4 +- .../backend/store/raft/RaftClosure.java | 6 +- .../backend/store/raft/RaftNode.java | 83 ++++++++++--------- .../backend/store/raft/RaftResult.java | 4 + .../backend/store/raft/RaftSharedContext.java | 4 +- .../backend/store/raft/rpc/RpcForwarder.java | 23 ++--- 6 files changed, 71 insertions(+), 53 deletions(-) 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 5f9dfb2d8a..c21e3f58e7 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 @@ -56,7 +56,7 @@ public RaftBackendStore(BackendStore store, RaftSharedContext context) { this.store = store; this.context = context; this.mutationBatch = new ThreadLocal<>(); - this.isSafeRead = this.context.isSafeRead(); + this.isSafeRead = this.context.safeRead(); } public BackendStore originStore() { @@ -228,7 +228,7 @@ public void run(Status status, long index, byte[] reqCtx) { } } }; - this.node().node().readIndex(BytesUtil.EMPTY_BYTES, readIndexClosure); + this.node().readIndex(BytesUtil.EMPTY_BYTES, readIndexClosure); try { return future.waitFinished(); } catch (Throwable e) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftClosure.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftClosure.java index eb337c72cc..1808bc85b6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftClosure.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftClosure.java @@ -68,6 +68,10 @@ private RaftResult get() { } } + public void complete(Status status) { + this.future.complete(new RaftResult<>(status)); + } + public void complete(Status status, Supplier callback) { this.future.complete(new RaftResult<>(status, callback)); } @@ -79,7 +83,7 @@ public void failure(Status status, Throwable exception) { @Override public void run(Status status) { if (status.isOk()) { - this.complete(status, () -> null); + this.complete(status); } else { LOG.error("Failed to apply command: {}", status); String msg = "Failed to apply command in raft node with error: " + diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftNode.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftNode.java index d3dd65b0ab..078b9382fc 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftNode.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftNode.java @@ -67,11 +67,11 @@ public RaftNode(RaftSharedContext context) { this.busyCounter = new AtomicInteger(); } - public RaftSharedContext context() { + protected RaftSharedContext context() { return this.context; } - public Node node() { + protected Node node() { assert this.node != null; return this.node; } @@ -111,62 +111,55 @@ public void snapshot() { } } - private Node initRaftNode() throws IOException { - NodeOptions nodeOptions = this.context.nodeOptions(); - nodeOptions.setFsm(this.stateMachine); - // TODO: When support sharding, groupId needs to be bound to shard Id - String groupId = this.context.group(); - PeerId endpoint = this.context.endpoint(); - /* - * Start raft node with shared rpc server: - * return new RaftGroupService(groupId, endpoint, nodeOptions, - * this.context.rpcServer(), true) - * .start(false) - */ - return RaftServiceFactory.createAndInitRaftNode(groupId, endpoint, - nodeOptions); + public void readIndex(byte[] reqCtx, ReadIndexClosure done) { + this.node.readIndex(reqCtx, done); } - private void submitCommand(StoreCommand command, RaftStoreClosure closure) { + public Object submitAndWait(StoreCommand command, RaftStoreClosure future) { + // Submit command to raft node + this.submitCommand(command, future); + + try { + /* + * Here wait for the command to complete: + * 1.If on the leader, wait for the logs has been committed. + * 2.If on the follower, request command will be forwarded to the + * leader, actually it has waited in forwardToLeader(). + */ + return future.waitFinished(); + } catch (Throwable e) { + throw new BackendException("Failed to wait store command %s", + e, command); + } + } + + private void submitCommand(StoreCommand command, RaftStoreClosure future) { // Wait leader elected LeaderInfo leaderInfo = this.waitLeaderElected( RaftSharedContext.NO_TIMEOUT); + // If myself is not leader, forward to the leader if (!leaderInfo.selfIsLeader) { this.context.rpcForwarder().forwardToLeader(leaderInfo.leaderId, - command, closure); + command, future); return; } + // Sleep a while when raft node is busy this.waitIfBusy(); Task task = new Task(); - task.setDone(closure); - // compress return BytesBuffer + // Compress data, note compress() will return a BytesBuffer ByteBuffer buffer = LZ4Util.compress(command.data(), RaftSharedContext.BLOCK_SIZE) .forReadWritten() .asByteBuffer(); - LOG.debug("The bytes size of command(compressed) {} is {}", - command.action(), buffer.limit()); + LOG.debug("Submit to raft node '{}', the compressed bytes of command " + + "{} is {}", this.node, command.action(), buffer.limit()); task.setData(buffer); - LOG.debug("submit to raft node {}", this.node); + task.setDone(future); this.node.apply(task); } - public Object submitAndWait(StoreCommand command, RaftStoreClosure future) { - this.submitCommand(command, future); - try { - /* - * Here will wait future complete, actually the follower has waited - * in forwardToLeader, written like this to simplify the code - */ - return future.waitFinished(); - } catch (Throwable e) { - throw new BackendException("Failed to wait store command %s", - e, command); - } - } - protected LeaderInfo waitLeaderElected(int timeout) { String group = this.context.group(); LeaderInfo leaderInfo = this.leaderInfo.get(); @@ -250,6 +243,22 @@ private void waitIfBusy() { } } + private Node initRaftNode() throws IOException { + NodeOptions nodeOptions = this.context.nodeOptions(); + nodeOptions.setFsm(this.stateMachine); + // TODO: When support sharding, groupId needs to be bound to shard Id + String groupId = this.context.group(); + PeerId endpoint = this.context.endpoint(); + /* + * Start raft node with shared rpc server: + * return new RaftGroupService(groupId, endpoint, nodeOptions, + * this.context.rpcServer(), true) + * .start(false) + */ + return RaftServiceFactory.createAndInitRaftNode(groupId, endpoint, + nodeOptions); + } + @Override public String toString() { return String.format("[%s-%s]", this.context.group(), this.nodeId()); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftResult.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftResult.java index 5bee760edb..5f26993ffa 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftResult.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/RaftResult.java @@ -30,6 +30,10 @@ public final class RaftResult { private final Supplier callback; private final Throwable exception; + public RaftResult(Status status) { + this(status, () -> null, null); + } + public RaftResult(Status status, Supplier callback) { this(status, callback, null); } 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 8342d08a0e..3176cafc8d 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 @@ -136,7 +136,7 @@ public RaftSharedContext(HugeGraphParams params) { public void initRaftNode() { this.raftNode = new RaftNode(this); - this.rpcForwarder = new RpcForwarder(this.raftNode); + this.rpcForwarder = new RpcForwarder(this.raftNode.node()); this.raftGroupManager = new RaftGroupManagerImpl(this); } @@ -337,7 +337,7 @@ public PeerId endpoint() { return endpoint; } - public boolean isSafeRead() { + public boolean safeRead() { return this.config().get(CoreOptions.RAFT_SAFE_READ); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/rpc/RpcForwarder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/rpc/RpcForwarder.java index 0151f5040d..2c93d113ee 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/rpc/RpcForwarder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/raft/rpc/RpcForwarder.java @@ -25,6 +25,7 @@ import org.slf4j.Logger; +import com.alipay.sofa.jraft.Node; import com.alipay.sofa.jraft.Status; import com.alipay.sofa.jraft.core.NodeImpl; import com.alipay.sofa.jraft.entity.PeerId; @@ -34,7 +35,6 @@ import com.alipay.sofa.jraft.util.Endpoint; import com.baidu.hugegraph.backend.BackendException; import com.baidu.hugegraph.backend.store.raft.RaftClosure; -import com.baidu.hugegraph.backend.store.raft.RaftNode; import com.baidu.hugegraph.backend.store.raft.RaftStoreClosure; import com.baidu.hugegraph.backend.store.raft.StoreCommand; import com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse; @@ -53,14 +53,14 @@ public class RpcForwarder { private final PeerId nodeId; private final RaftClientService rpcClient; - public RpcForwarder(RaftNode node) { - this.nodeId = node.node().getNodeId().getPeerId(); - this.rpcClient = ((NodeImpl) node.node()).getRpcService(); + public RpcForwarder(Node node) { + this.nodeId = node.getNodeId().getPeerId(); + this.rpcClient = ((NodeImpl) node).getRpcService(); E.checkNotNull(this.rpcClient, "rpc client"); } public void forwardToLeader(PeerId leaderId, StoreCommand command, - RaftStoreClosure closure) { + RaftStoreClosure future) { E.checkNotNull(leaderId, "leader id"); E.checkState(!leaderId.equals(this.nodeId), "Invalid state: current node is the leader, there is " + @@ -80,7 +80,7 @@ public void forwardToLeader(PeerId leaderId, StoreCommand command, public void setResponse(StoreCommandResponse response) { if (response.getStatus()) { LOG.debug("StoreCommandResponse status ok"); - closure.complete(Status.OK(), () -> null); + future.complete(Status.OK(), () -> null); } else { LOG.debug("StoreCommandResponse status error"); Status status = new Status(RaftError.UNKNOWN, @@ -90,13 +90,13 @@ public void setResponse(StoreCommandResponse response) { "is [%s], failed to forward request " + "to leader: %s", leaderId, response.getMessage()); - closure.failure(status, e); + future.failure(status, e); } } @Override public void run(Status status) { - closure.run(status); + future.run(status); } }; this.waitRpc(leaderId.getEndpoint(), request, responseClosure); @@ -112,7 +112,7 @@ public RaftClosure forwardToLeader(PeerId leaderId, this.nodeId, leaderId); RaftClosure future = new RaftClosure<>(); - RpcResponseClosure responseClosure = new RpcResponseClosure() { + RpcResponseClosure responseDone = new RpcResponseClosure() { @Override public void setResponse(T response) { FieldDescriptor fd = response.getDescriptorForType() @@ -142,7 +142,7 @@ public void run(Status status) { future.run(status); } }; - this.waitRpc(leaderId.getEndpoint(), request, responseClosure); + this.waitRpc(leaderId.getEndpoint(), request, responseDone); return future; } @@ -151,7 +151,8 @@ private void waitRpc(Endpoint endpoint, Message request, E.checkNotNull(endpoint, "leader endpoint"); try { this.rpcClient.invokeWithDone(endpoint, request, done, - WAIT_RPC_TIMEOUT).get(); + WAIT_RPC_TIMEOUT) + .get(); } catch (InterruptedException e) { throw new BackendException("Invoke rpc request was interrupted, " + "please try again later", e); From 9a8259ef3f22abf9781513b823a9f1096152d494 Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Thu, 21 Apr 2022 18:59:56 +0800 Subject: [PATCH 5/5] fix schema label: addIndexLabel/removeIndexLabel race condition (#1807) --- .../backend/serializer/BinarySerializer.java | 4 +-- .../backend/serializer/TableSerializer.java | 4 +-- .../backend/serializer/TextSerializer.java | 4 +-- .../backend/tx/SchemaTransaction.java | 36 +++++++++++++++++-- .../job/schema/IndexLabelRemoveJob.java | 5 +-- .../baidu/hugegraph/job/schema/SchemaJob.java | 22 ------------ .../baidu/hugegraph/schema/IndexLabel.java | 9 ++--- .../baidu/hugegraph/schema/SchemaLabel.java | 4 +-- .../schema/builder/IndexLabelBuilder.java | 22 ++++++------ .../baidu/hugegraph/structure/HugeIndex.java | 2 +- 10 files changed, 62 insertions(+), 50 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java index a07515db98..bdfb6d2e88 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java @@ -1051,7 +1051,7 @@ public VertexLabel readVertexLabel(HugeGraph graph, vertexLabel.properties(readIds(HugeKeys.PROPERTIES)); vertexLabel.primaryKeys(readIds(HugeKeys.PRIMARY_KEYS)); vertexLabel.nullableKeys(readIds(HugeKeys.NULLABLE_KEYS)); - vertexLabel.indexLabels(readIds(HugeKeys.INDEX_LABELS)); + vertexLabel.addIndexLabels(readIds(HugeKeys.INDEX_LABELS)); vertexLabel.enableLabelIndex(readBool(HugeKeys.ENABLE_LABEL_INDEX)); vertexLabel.status(readEnum(HugeKeys.STATUS, SchemaStatus.class)); vertexLabel.ttl(readLong(HugeKeys.TTL)); @@ -1092,7 +1092,7 @@ public EdgeLabel readEdgeLabel(HugeGraph graph, edgeLabel.properties(readIds(HugeKeys.PROPERTIES)); edgeLabel.sortKeys(readIds(HugeKeys.SORT_KEYS)); edgeLabel.nullableKeys(readIds(HugeKeys.NULLABLE_KEYS)); - edgeLabel.indexLabels(readIds(HugeKeys.INDEX_LABELS)); + edgeLabel.addIndexLabels(readIds(HugeKeys.INDEX_LABELS)); edgeLabel.enableLabelIndex(readBool(HugeKeys.ENABLE_LABEL_INDEX)); edgeLabel.status(readEnum(HugeKeys.STATUS, SchemaStatus.class)); edgeLabel.ttl(readLong(HugeKeys.TTL)); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java index 0079ba5191..7a56fe50a6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java @@ -496,7 +496,7 @@ public VertexLabel readVertexLabel(HugeGraph graph, vertexLabel.properties(this.toIdArray(properties)); vertexLabel.primaryKeys(this.toIdArray(primaryKeys)); vertexLabel.nullableKeys(this.toIdArray(nullableKeys)); - vertexLabel.indexLabels(this.toIdArray(indexLabels)); + vertexLabel.addIndexLabels(this.toIdArray(indexLabels)); vertexLabel.status(status); vertexLabel.ttl(ttl.longValue()); vertexLabel.ttlStartTime(this.toId(ttlStartTime)); @@ -535,7 +535,7 @@ public EdgeLabel readEdgeLabel(HugeGraph graph, BackendEntry backendEntry) { edgeLabel.properties(this.toIdArray(properties)); edgeLabel.sortKeys(this.toIdArray(sortKeys)); edgeLabel.nullableKeys(this.toIdArray(nullableKeys)); - edgeLabel.indexLabels(this.toIdArray(indexLabels)); + edgeLabel.addIndexLabels(this.toIdArray(indexLabels)); edgeLabel.status(status); edgeLabel.ttl(ttl.longValue()); edgeLabel.ttlStartTime(this.toId(ttlStartTime)); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java index b90646b6fd..68f130a9aa 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java @@ -586,7 +586,7 @@ public VertexLabel readVertexLabel(HugeGraph graph, vertexLabel.properties(readIds(properties)); vertexLabel.primaryKeys(readIds(primaryKeys)); vertexLabel.nullableKeys(readIds(nullableKeys)); - vertexLabel.indexLabels(readIds(indexLabels)); + vertexLabel.addIndexLabels(readIds(indexLabels)); vertexLabel.enableLabelIndex(JsonUtil.fromJson(enableLabelIndex, Boolean.class)); readUserdata(vertexLabel, entry); @@ -648,7 +648,7 @@ public EdgeLabel readEdgeLabel(HugeGraph graph, edgeLabel.properties(readIds(properties)); edgeLabel.sortKeys(readIds(sortKeys)); edgeLabel.nullableKeys(readIds(nullablekeys)); - edgeLabel.indexLabels(readIds(indexLabels)); + edgeLabel.addIndexLabels(readIds(indexLabels)); edgeLabel.enableLabelIndex(JsonUtil.fromJson(enableLabelIndex, Boolean.class)); readUserdata(edgeLabel, entry); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java index af567b384f..7563dbad04 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java @@ -242,8 +242,40 @@ public void addIndexLabel(SchemaLabel schemaLabel, IndexLabel indexLabel) { if (schemaLabel.equals(VertexLabel.OLAP_VL)) { return; } - schemaLabel.indexLabel(indexLabel.id()); - this.updateSchema(schemaLabel); + + // FIXME: move schemaLabel update into updateSchema() lock block instead + synchronized (schemaLabel) { + schemaLabel.addIndexLabel(indexLabel.id()); + this.updateSchema(schemaLabel); + } + } + + @Watched(prefix = "schema") + public void removeIndexLabelFromBaseLabel(IndexLabel indexLabel) { + HugeType baseType = indexLabel.baseType(); + Id baseValue = indexLabel.baseValue(); + SchemaLabel schemaLabel; + if (baseType == HugeType.VERTEX_LABEL) { + schemaLabel = this.getVertexLabel(baseValue); + } else { + assert baseType == HugeType.EDGE_LABEL; + schemaLabel = this.getEdgeLabel(baseValue); + } + + if (schemaLabel == null) { + LOG.info("The base label '{}' of index label '{}' " + + "may be deleted before", baseValue, indexLabel); + return; + } + if (schemaLabel.equals(VertexLabel.OLAP_VL)) { + return; + } + + // FIXME: move schemaLabel update into updateSchema() lock block instead + synchronized (schemaLabel) { + schemaLabel.removeIndexLabel(indexLabel.id()); + this.updateSchema(schemaLabel); + } } @Watched(prefix = "schema") diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/IndexLabelRemoveJob.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/IndexLabelRemoveJob.java index d81e46022b..085b72aa1b 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/IndexLabelRemoveJob.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/IndexLabelRemoveJob.java @@ -60,8 +60,9 @@ protected static void removeIndexLabel(HugeGraphParams graph, Id id) { // Set index label to "deleting" status schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.DELETING); try { - // Remove label from indexLabels of vertex or edge label - removeIndexLabelFromBaseLabel(schemaTx, indexLabel); + // Remove indexLabel from indexLabels of vertex/edge label + schemaTx.removeIndexLabelFromBaseLabel(indexLabel); + // Remove index data // TODO: use event to replace direct call graphTx.removeIndex(indexLabel); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java index ed76824335..cb37841335 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java @@ -9,10 +9,7 @@ import com.baidu.hugegraph.backend.id.IdGenerator; import com.baidu.hugegraph.backend.tx.SchemaTransaction; import com.baidu.hugegraph.job.SysJob; -import com.baidu.hugegraph.schema.IndexLabel; import com.baidu.hugegraph.schema.SchemaElement; -import com.baidu.hugegraph.schema.SchemaLabel; -import com.baidu.hugegraph.schema.VertexLabel; import com.baidu.hugegraph.type.HugeType; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; @@ -65,25 +62,6 @@ public static String formatTaskName(HugeType type, Id id, String name) { return String.join(SPLITOR, type.toString(), id.asString(), name); } - protected static void removeIndexLabelFromBaseLabel(SchemaTransaction tx, - IndexLabel label) { - HugeType baseType = label.baseType(); - Id baseValue = label.baseValue(); - SchemaLabel schemaLabel; - if (baseType == HugeType.VERTEX_LABEL) { - if (VertexLabel.OLAP_VL.id().equals(baseValue)) { - return; - } - schemaLabel = tx.getVertexLabel(baseValue); - } else { - assert baseType == HugeType.EDGE_LABEL; - schemaLabel = tx.getEdgeLabel(baseValue); - } - assert schemaLabel != null; - schemaLabel.removeIndexLabel(label.id()); - updateSchema(tx, schemaLabel); - } - /** * Use reflection to call SchemaTransaction.removeSchema(), * which is protected diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java index 6bf2457470..64c9723e20 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/IndexLabel.java @@ -118,8 +118,8 @@ public Id indexField() { return this.indexFields.get(0); } - public SchemaLabel baseElement() { - return getElement(this.graph, this.baseType, this.baseValue); + public SchemaLabel baseLabel() { + return getBaseLabel(this.graph, this.baseType, this.baseValue); } public boolean hasSameContent(IndexLabel other) { @@ -210,8 +210,9 @@ public static IndexLabel label(HugeGraph graph, Id id) { return graph.indexLabel(id); } - public static SchemaLabel getElement(HugeGraph graph, - HugeType baseType, Object baseValue) { + public static SchemaLabel getBaseLabel(HugeGraph graph, + HugeType baseType, + Object baseValue) { E.checkNotNull(baseType, "base type", "index label"); E.checkNotNull(baseValue, "base value", "index label"); E.checkArgument(baseValue instanceof String || baseValue instanceof Id, diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaLabel.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaLabel.java index 354fb88606..ed8c1c1a86 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaLabel.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaLabel.java @@ -93,11 +93,11 @@ public Set indexLabels() { return Collections.unmodifiableSet(this.indexLabels); } - public void indexLabel(Id id) { + public void addIndexLabel(Id id) { this.indexLabels.add(id); } - public void indexLabels(Id... ids) { + public void addIndexLabels(Id... ids) { this.indexLabels.addAll(Arrays.asList(ids)); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java index 2521b1b1a7..44b21f4bec 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java @@ -86,8 +86,9 @@ public IndexLabelBuilder(SchemaTransaction transaction, super(transaction, graph); E.checkNotNull(copy, "copy"); // Get base element from self graph - SchemaLabel schemaLabel = IndexLabel.getElement(graph, copy.baseType(), - copy.baseValue()); + SchemaLabel schemaLabel = IndexLabel.getBaseLabel(graph, + copy.baseType(), + copy.baseValue()); this.id = null; this.name = copy.name(); this.baseType = copy.baseType(); @@ -110,7 +111,7 @@ public IndexLabel build() { this.checkFields4Range(); IndexLabel indexLabel = new IndexLabel(graph, id, this.name); indexLabel.baseType(this.baseType); - SchemaLabel schemaLabel = this.loadElement(); + SchemaLabel schemaLabel = this.loadBaseLabel(); indexLabel.baseValue(schemaLabel.id()); indexLabel.indexType(this.indexType); for (String field : this.indexFields) { @@ -137,7 +138,7 @@ private boolean hasSameProperties(IndexLabel existedIndexLabel) { return false; } - SchemaLabel schemaLabel = this.loadElement(); + SchemaLabel schemaLabel = this.loadBaseLabel(); if (!schemaLabel.id().equals(existedIndexLabel.baseValue())) { return false; } @@ -200,7 +201,7 @@ public SchemaElement.TaskWithSchema createWithTask() { IdGenerator.ZERO); } - SchemaLabel schemaLabel = this.loadElement(); + SchemaLabel schemaLabel = this.loadBaseLabel(); /* * If new index label is prefix of existed index label, or has @@ -286,7 +287,7 @@ public IndexLabel append() { this.checkStableVars(); Userdata.check(this.userdata, Action.APPEND); indexLabel.userdata(this.userdata); - SchemaLabel schemaLabel = indexLabel.baseElement(); + SchemaLabel schemaLabel = indexLabel.baseLabel(); this.graph().addIndexLabel(schemaLabel, indexLabel); return indexLabel; } @@ -302,7 +303,7 @@ public IndexLabel eliminate() { Userdata.check(this.userdata, Action.ELIMINATE); indexLabel.removeUserdata(this.userdata); - SchemaLabel schemaLabel = indexLabel.baseElement(); + SchemaLabel schemaLabel = indexLabel.baseLabel(); this.graph().addIndexLabel(schemaLabel, indexLabel); return indexLabel; } @@ -454,9 +455,9 @@ private void checkIndexType() { } } - private SchemaLabel loadElement() { - return IndexLabel.getElement(this.graph(), - this.baseType, this.baseValue); + private SchemaLabel loadBaseLabel() { + return IndexLabel.getBaseLabel(this.graph(), + this.baseType, this.baseValue); } private void checkFields(Set propertyIds) { @@ -619,7 +620,6 @@ private Set removeSubIndex(SchemaLabel schemaLabel) { } Set tasks = InsertionOrderUtil.newSet(); for (Id id : overrideIndexLabelIds) { - schemaLabel.removeIndexLabel(id); Id task = this.graph().removeIndexLabel(id); E.checkNotNull(task, "remove sub index label task"); tasks.add(task); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeIndex.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeIndex.java index 9dedfe9245..fec8a0a946 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeIndex.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeIndex.java @@ -151,7 +151,7 @@ public boolean hasTtl() { if (this.indexLabel.system()) { return false; } - return this.indexLabel.baseElement().ttl() > 0L; + return this.indexLabel.baseLabel().ttl() > 0L; } public long ttl() {