From e68140a58a317036e4616d75ce2db35b4e6e2b5c Mon Sep 17 00:00:00 2001 From: vaughn Date: Thu, 9 Mar 2023 09:59:59 +0800 Subject: [PATCH 1/2] optimize: remove lock --- .../hugegraph/api/filter/RedirectFilter.java | 19 +++++----- .../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 23e5f0735c..84c8517237 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 void filter(ContainerRequestContext context) throws IOException { 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 9d8f85a9b2..a622803471 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 @@ 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 void isFeatureSupport(boolean featureSupport) { 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 11a989ecdb..fb6b5c9dc4 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 void error(StateMachineContext context, Throwable e) { 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); } } From 5361aeb3bd17ea93428a7d66994979379395256c Mon Sep 17 00:00:00 2001 From: vaughn Date: Mon, 13 Mar 2023 11:01:49 +0800 Subject: [PATCH 2/2] chore: improve --- .../hugegraph/api/filter/RedirectFilter.java | 8 +++---- .../masterelection/GlobalMasterInfo.java | 22 ++++++++++--------- .../StandardStateMachineCallback.java | 4 ++-- 3 files changed, 18 insertions(+), 16 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 84c8517237..e675dd9554 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 @@ -86,12 +86,12 @@ public void filter(ContainerRequestContext context) throws IOException { if (globalMasterInfo == null || !globalMasterInfo.isFeatureSupport()) { return; } - GlobalMasterInfo.Info masterInfo = globalMasterInfo.info(); - if (masterInfo == null || masterInfo.isMaster() || - StringUtils.isEmpty(masterInfo.url())) { + GlobalMasterInfo.NodeInfo masterNodeInfo = globalMasterInfo.nodeInfo(); + if (masterNodeInfo == null || masterNodeInfo.isMaster() || + StringUtils.isEmpty(masterNodeInfo.url())) { return; } - String url = masterInfo.url(); + String url = masterNodeInfo.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 a622803471..d7dd127af5 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,20 +19,22 @@ public class GlobalMasterInfo { - private volatile Info info; + private NodeInfo nodeInfo; private volatile boolean featureSupport; public GlobalMasterInfo() { this.featureSupport = false; - this.info = new Info(false, ""); + this.nodeInfo = new NodeInfo(false, ""); } - public void info(boolean isMaster, String url) { - this.info = new Info(isMaster, url); + public final void nodeInfo(boolean isMaster, String url) { + // final can avoid instruction rearrangement, visibility can be ignored + final NodeInfo tmp = new NodeInfo(isMaster, url); + this.nodeInfo = tmp; } - public Info info() { - return this.info; + public final NodeInfo nodeInfo() { + return this.nodeInfo; } public void isFeatureSupport(boolean featureSupport) { @@ -43,12 +45,12 @@ public boolean isFeatureSupport() { return this.featureSupport; } - public static class Info { + public static class NodeInfo { - private boolean isMaster; - private String url; + private final boolean isMaster; + private final String url; - public Info(boolean isMaster, String url) { + public NodeInfo(boolean isMaster, String url) { this.isMaster = isMaster; this.url = 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 fb6b5c9dc4..88bec95b31 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 void error(StateMachineContext context, Throwable e) { public void initGlobalMasterInfo(StateMachineContext context) { StateMachineContext.MasterServerInfo master = context.master(); if (master == null) { - this.globalMasterInfo.info(false, null); + this.globalMasterInfo.nodeInfo(false, ""); return; } boolean isMaster = Objects.equals(context.node(), master.node()); String url = master.url(); - this.globalMasterInfo.info(isMaster, url); + this.globalMasterInfo.nodeInfo(isMaster, url); } }