Pengzna commented on code in PR #2476:
URL: 
https://github.com/apache/incubator-hugegraph/pull/2476#discussion_r1537004761


##########
hugegraph-store/hg-store-client/src/main/java/org/apache/hugegraph/store/client/NodeTxExecutor.java:
##########
@@ -0,0 +1,434 @@
+/*
+ * 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.hugegraph.store.client;
+
+import static 
org.apache.hugegraph.store.client.util.HgStoreClientConst.EMPTY_LIST;
+import static 
org.apache.hugegraph.store.client.util.HgStoreClientConst.NODE_MAX_RETRYING_TIMES;
+import static 
org.apache.hugegraph.store.client.util.HgStoreClientConst.TX_SESSIONS_MAP_CAPACITY;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import org.apache.hugegraph.store.HgOwnerKey;
+import org.apache.hugegraph.store.HgStoreSession;
+import org.apache.hugegraph.store.client.type.HgStoreClientException;
+import org.apache.hugegraph.store.client.util.HgAssert;
+import org.apache.hugegraph.store.client.util.HgStoreClientConst;
+import org.apache.hugegraph.store.term.HgPair;
+import org.apache.hugegraph.store.term.HgTriple;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 2021/11/18
+ */
+@Slf4j
+@NotThreadSafe
+final class NodeTxExecutor {
+
+    private static final String maxTryMsg =
+            "the number of retries reached the upper limit : " + 
NODE_MAX_RETRYING_TIMES +
+            ",caused by:";
+    private static final String msg =
+            "Not all tx-data delivered to real-node-session successfully.";
+
+    static {
+        
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism",
+                           
String.valueOf(Runtime.getRuntime().availableProcessors() * 2));
+    }
+
+    private final String graphName;
+    NodeTxSessionProxy proxy;
+    Collector<NodeTkv, ?, Map<Long, List<HgOwnerKey>>> collector = 
Collectors.groupingBy(
+            nkv -> nkv.getNodeId(), Collectors.mapping(NodeTkv::getKey, 
Collectors.toList()));
+    private Map<Long, HgStoreSession> sessions = new 
HashMap<>(TX_SESSIONS_MAP_CAPACITY, 1);
+    private boolean isTx;
+    private List<HgPair<HgTriple<String, HgOwnerKey, Object>,
+            Function<NodeTkv, Boolean>>> entries = new LinkedList<>();
+
+    private NodeTxExecutor(String graphName, NodeTxSessionProxy proxy) {
+        this.graphName = graphName;
+        this.proxy = proxy;
+    }
+
+    static NodeTxExecutor graphOf(String graphName, NodeTxSessionProxy proxy) {
+        return new NodeTxExecutor(graphName, proxy);
+    }
+
+    public boolean isTx() {
+        return isTx;
+    }
+
+    void setTx(boolean tx) {
+        isTx = tx;
+    }
+
+    void commitTx() {
+        if (!this.isTx) {
+            throw new IllegalStateException("It's not in tx state");
+        }
+
+        this.doCommit();
+    }
+
+    void rollbackTx() {
+        if (!this.isTx) {
+            return;
+        }
+        try {
+            this.sessions.values().stream().filter(HgStoreSession::isTx)
+                         .forEach(HgStoreSession::rollback);
+        } catch (Throwable t) {
+            throw t;
+        } finally {
+            this.isTx = false;
+            this.sessions.clear();
+        }
+    }
+
+    void doCommit() {
+        try {
+            this.retryingInvoke(() -> {
+                if (this.entries.isEmpty()) {
+                    return true;
+                }
+                AtomicBoolean allSuccess = new AtomicBoolean(true);
+                for (HgPair<HgTriple<String, HgOwnerKey, Object>, 
Function<NodeTkv, Boolean>> e :
+                        this.entries) {
+                    doAction(e.getKey(), e.getValue());
+                }
+                if (!allSuccess.get()) {
+                    throw HgStoreClientException.of(msg);
+                }
+                AtomicReference<Throwable> throwable = new AtomicReference<>();
+                Collection<HgStoreSession> sessions = this.sessions.values();
+                sessions.parallelStream().forEach(e -> {
+                    if (e.isTx()) {

Review Comment:
   sure, we will do that in the next code style related PR



##########
hugegraph-store/hg-store-client/src/main/java/org/apache/hugegraph/store/client/NodeTxExecutor.java:
##########
@@ -0,0 +1,434 @@
+/*
+ * 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.hugegraph.store.client;
+
+import static 
org.apache.hugegraph.store.client.util.HgStoreClientConst.EMPTY_LIST;
+import static 
org.apache.hugegraph.store.client.util.HgStoreClientConst.NODE_MAX_RETRYING_TIMES;
+import static 
org.apache.hugegraph.store.client.util.HgStoreClientConst.TX_SESSIONS_MAP_CAPACITY;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import org.apache.hugegraph.store.HgOwnerKey;
+import org.apache.hugegraph.store.HgStoreSession;
+import org.apache.hugegraph.store.client.type.HgStoreClientException;
+import org.apache.hugegraph.store.client.util.HgAssert;
+import org.apache.hugegraph.store.client.util.HgStoreClientConst;
+import org.apache.hugegraph.store.term.HgPair;
+import org.apache.hugegraph.store.term.HgTriple;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 2021/11/18
+ */
+@Slf4j
+@NotThreadSafe
+final class NodeTxExecutor {
+
+    private static final String maxTryMsg =
+            "the number of retries reached the upper limit : " + 
NODE_MAX_RETRYING_TIMES +
+            ",caused by:";
+    private static final String msg =
+            "Not all tx-data delivered to real-node-session successfully.";
+
+    static {
+        
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism",
+                           
String.valueOf(Runtime.getRuntime().availableProcessors() * 2));
+    }
+
+    private final String graphName;
+    NodeTxSessionProxy proxy;
+    Collector<NodeTkv, ?, Map<Long, List<HgOwnerKey>>> collector = 
Collectors.groupingBy(
+            nkv -> nkv.getNodeId(), Collectors.mapping(NodeTkv::getKey, 
Collectors.toList()));
+    private Map<Long, HgStoreSession> sessions = new 
HashMap<>(TX_SESSIONS_MAP_CAPACITY, 1);
+    private boolean isTx;
+    private List<HgPair<HgTriple<String, HgOwnerKey, Object>,
+            Function<NodeTkv, Boolean>>> entries = new LinkedList<>();
+
+    private NodeTxExecutor(String graphName, NodeTxSessionProxy proxy) {
+        this.graphName = graphName;
+        this.proxy = proxy;
+    }
+
+    static NodeTxExecutor graphOf(String graphName, NodeTxSessionProxy proxy) {
+        return new NodeTxExecutor(graphName, proxy);
+    }
+
+    public boolean isTx() {
+        return isTx;
+    }
+
+    void setTx(boolean tx) {
+        isTx = tx;
+    }
+
+    void commitTx() {
+        if (!this.isTx) {
+            throw new IllegalStateException("It's not in tx state");
+        }
+
+        this.doCommit();
+    }
+
+    void rollbackTx() {
+        if (!this.isTx) {
+            return;
+        }
+        try {
+            this.sessions.values().stream().filter(HgStoreSession::isTx)
+                         .forEach(HgStoreSession::rollback);
+        } catch (Throwable t) {
+            throw t;
+        } finally {
+            this.isTx = false;
+            this.sessions.clear();
+        }
+    }
+
+    void doCommit() {
+        try {
+            this.retryingInvoke(() -> {
+                if (this.entries.isEmpty()) {
+                    return true;
+                }
+                AtomicBoolean allSuccess = new AtomicBoolean(true);
+                for (HgPair<HgTriple<String, HgOwnerKey, Object>, 
Function<NodeTkv, Boolean>> e :
+                        this.entries) {
+                    doAction(e.getKey(), e.getValue());
+                }
+                if (!allSuccess.get()) {
+                    throw HgStoreClientException.of(msg);
+                }
+                AtomicReference<Throwable> throwable = new AtomicReference<>();
+                Collection<HgStoreSession> sessions = this.sessions.values();
+                sessions.parallelStream().forEach(e -> {
+                    if (e.isTx()) {
+                        try {
+                            e.commit();
+                        } catch (Throwable t) {

Review Comment:
   ditto



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to