You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by va...@apache.org on 2023/03/09 02:02:45 UTC

[incubator-hugegraph] branch zy_dev created (now e68140a58)

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

vaughn pushed a change to branch zy_dev
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git


      at e68140a58 optimize: remove lock

This branch includes the following new commits:

     new e68140a58 optimize: remove lock

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-hugegraph] 01/01: optimize: remove lock

Posted by va...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

vaughn pushed a commit to branch zy_dev
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git

commit e68140a58a317036e4616d75ce2db35b4e6e2b5c
Author: vaughn <va...@apache.org>
AuthorDate: Thu Mar 9 09:59:59 2023 +0800

    optimize: remove lock
---
 .../hugegraph/api/filter/RedirectFilter.java       | 19 ++++++-----
 .../hugegraph/masterelection/GlobalMasterInfo.java | 37 +++++++++++++++-------
 .../StandardStateMachineCallback.java              |  4 +--
 3 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java
index 23e5f0735..84c851723 100644
--- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java
+++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java
@@ -76,23 +76,22 @@ public class RedirectFilter implements ContainerRequestFilter {
         E.checkState(handle != null, "Context GraphManager is absent");
         GraphManager manager = handle.getService();
         E.checkState(manager != null, "Context GraphManager is absent");
-        GlobalMasterInfo globalMasterInfo = manager.globalMasterInfo();
-        if (globalMasterInfo == null || !globalMasterInfo.isFeatureSupport()) {
-            return;
-        }
 
         String redirectTag = context.getHeaderString(X_HG_REDIRECT);
         if (StringUtils.isNotEmpty(redirectTag)) {
             return;
         }
 
-        String url;
-        synchronized (globalMasterInfo) {
-            if (globalMasterInfo.isMaster() || StringUtils.isEmpty(globalMasterInfo.url())) {
-                return;
-            }
-            url = globalMasterInfo.url();
+        GlobalMasterInfo globalMasterInfo = manager.globalMasterInfo();
+        if (globalMasterInfo == null || !globalMasterInfo.isFeatureSupport()) {
+            return;
+        }
+        GlobalMasterInfo.Info masterInfo = globalMasterInfo.info();
+        if (masterInfo == null || masterInfo.isMaster() ||
+            StringUtils.isEmpty(masterInfo.url())) {
+            return;
         }
+        String url = masterInfo.url();
 
         URI redirectUri;
         try {
diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/GlobalMasterInfo.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/GlobalMasterInfo.java
index 9d8f85a9b..a62280347 100644
--- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/GlobalMasterInfo.java
+++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/GlobalMasterInfo.java
@@ -19,26 +19,20 @@ package org.apache.hugegraph.masterelection;
 
 public class GlobalMasterInfo {
 
-    private boolean isMaster;
-    private String url;
-
+    private volatile Info info;
     private volatile boolean featureSupport;
 
     public GlobalMasterInfo() {
         this.featureSupport = false;
+        this.info = new Info(false, "");
     }
 
-    public synchronized void set(boolean isMaster, String url) {
-        this.isMaster = isMaster;
-        this.url = url;
-    }
-
-    public synchronized boolean isMaster() {
-        return this.isMaster;
+    public void info(boolean isMaster, String url) {
+        this.info = new Info(isMaster, url);
     }
 
-    public synchronized String url() {
-        return this.url;
+    public Info info() {
+        return this.info;
     }
 
     public void isFeatureSupport(boolean featureSupport) {
@@ -48,4 +42,23 @@ public class GlobalMasterInfo {
     public boolean isFeatureSupport() {
         return this.featureSupport;
     }
+
+    public static class Info {
+
+        private boolean isMaster;
+        private String url;
+
+        public Info(boolean isMaster, String url) {
+            this.isMaster = isMaster;
+            this.url = url;
+        }
+
+        public boolean isMaster() {
+            return this.isMaster;
+        }
+
+        public String url() {
+            return this.url;
+        }
+    }
 }
diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java
index 11a989ecd..fb6b5c9dc 100644
--- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java
+++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java
@@ -94,12 +94,12 @@ public class StandardStateMachineCallback implements StateMachineCallback {
     public void initGlobalMasterInfo(StateMachineContext context) {
         StateMachineContext.MasterServerInfo master = context.master();
         if (master == null) {
-            this.globalMasterInfo.set(false, null);
+            this.globalMasterInfo.info(false, null);
             return;
         }
 
         boolean isMaster = Objects.equals(context.node(), master.node());
         String url = master.url();
-        this.globalMasterInfo.set(isMaster, url);
+        this.globalMasterInfo.info(isMaster, url);
     }
 }