From ce2110588610c63bb308d5d8e52ff455a34cffa4 Mon Sep 17 00:00:00 2001 From: Aph-CUG <2582215315@qq> Date: Wed, 23 Oct 2024 08:50:12 -0700 Subject: [PATCH 1/4] tx enhancer --- .../hugegraph/api/profile/GraphsAPI.java | 4 + .../apache/hugegraph/core/GraphManager.java | 45 ++++++++- .../java/org/apache/hugegraph/HugeGraph.java | 1 + .../apache/hugegraph/StandardHugeGraph.java | 40 ++++++-- .../apache/hugegraph/variables/CheckList.java | 93 +++++++++++++++++++ .../java/org/apache/hugegraph/TxScanner.java | 79 ++++++++++++++++ 6 files changed, 254 insertions(+), 8 deletions(-) create mode 100644 hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java create mode 100644 hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java index f45c228baf..7fd17c65d9 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java @@ -29,6 +29,8 @@ import org.apache.hugegraph.auth.HugePermission; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; import org.apache.hugegraph.type.define.GraphMode; import org.apache.hugegraph.type.define.GraphReadMode; import org.apache.hugegraph.util.E; @@ -66,6 +68,8 @@ public class GraphsAPI extends API { private static final String CONFIRM_CLEAR = "I'm sure to delete all data"; private static final String CONFIRM_DROP = "I'm sure to drop the graph"; + private KvClient client; + @GET @Timed @Produces(APPLICATION_JSON_WITH_CHARSET) diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java index 5cba0a0f0f..e4c3988ae9 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java @@ -29,6 +29,7 @@ import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.lang3.StringUtils; +import org.apache.hugegraph.variables.CheckList; import org.apache.hugegraph.HugeFactory; import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.auth.AuthManager; @@ -53,6 +54,10 @@ import org.apache.hugegraph.masterelection.StandardRoleListener; import org.apache.hugegraph.metrics.MetricsUtil; import org.apache.hugegraph.metrics.ServerReporter; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.client.PDConfig; +import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; import org.apache.hugegraph.rpc.RpcClientProvider; import org.apache.hugegraph.rpc.RpcConsumerConfig; import org.apache.hugegraph.rpc.RpcProviderConfig; @@ -66,12 +71,14 @@ import org.apache.hugegraph.util.ConfigUtil; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Events; +import org.apache.hugegraph.util.JsonUtil; import org.apache.hugegraph.util.Log; import org.apache.tinkerpop.gremlin.server.auth.AuthenticationException; import org.apache.tinkerpop.gremlin.server.util.MetricManager; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.structure.Transaction; import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; +import org.junit.Before; import org.slf4j.Logger; import com.alipay.sofa.rpc.config.ServerConfig; @@ -94,6 +101,15 @@ public final class GraphManager { private final HugeConfig conf; private final EventHub eventHub; + private final String preFix = "graph_creat_tx"; + + private KvClient client; + + @Before + public void setUp() { + this.client = new KvClient<>(PDConfig.of("localhost:8686")); + } + public GraphManager(HugeConfig conf, EventHub hub) { this.graphsDir = conf.get(ServerOptions.GRAPHS); this.graphs = new ConcurrentHashMap<>(); @@ -172,6 +188,8 @@ public HugeGraph cloneGraph(String name, String newName, String configText) { } public HugeGraph createGraph(String name, String configText) { + + E.checkArgument(this.conf.get(ServerOptions.ENABLE_DYNAMIC_CREATE_DROP), "Not allowed to create graph '%s' dynamically, " + "please set `enable_dynamic_create_drop` to true.", @@ -181,9 +199,34 @@ public HugeGraph createGraph(String name, String configText) { E.checkArgument(!this.graphs().contains(name), "The graph name '%s' has existed", name); + CheckList checkList = new CheckList(name, configText); + + PropertiesConfiguration propConfig = ConfigUtil.buildConfig(configText); HugeConfig config = new HugeConfig(propConfig); this.checkOptions(config); + + checkList.setConfig(config); + checkList.setStage("config"); + + String json = JsonUtil.toJson(checkList); + + try { + client.put(preFix + name, json); + } + + catch (PDException e) { + throw new RuntimeException(e); + } + + checkList.setStage("finish"); + try { + client.put(preFix + name, json); + } + + catch (PDException e) { + throw new RuntimeException(e); + } return this.createGraph(config, name); } @@ -581,7 +624,7 @@ private void notifyAndWaitEvent(String event, HugeGraph graph) { } } - private HugeGraph createGraph(HugeConfig config, String name) { + private HugeGraph createGraph(HugeConfig config, String name){ HugeGraph graph = null; try { // Create graph instance diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java index ab460d495f..2bc42ac5e7 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java @@ -32,6 +32,7 @@ import org.apache.hugegraph.config.TypedOption; import org.apache.hugegraph.masterelection.GlobalMasterInfo; import org.apache.hugegraph.masterelection.RoleElectionStateMachine; +import org.apache.hugegraph.pd.common.PDException; import org.apache.hugegraph.rpc.RpcServiceConfig4Client; import org.apache.hugegraph.rpc.RpcServiceConfig4Server; import org.apache.hugegraph.schema.EdgeLabel; diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java index 4e9263a488..223a82bc28 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java @@ -71,6 +71,10 @@ import org.apache.hugegraph.masterelection.StandardClusterRoleStore; import org.apache.hugegraph.masterelection.StandardRoleElectionStateMachine; import org.apache.hugegraph.meta.MetaManager; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.pd.grpc.kv.KResponse; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; import org.apache.hugegraph.perf.PerfUtil.Watched; import org.apache.hugegraph.rpc.RpcServiceConfig4Client; import org.apache.hugegraph.rpc.RpcServiceConfig4Server; @@ -97,8 +101,10 @@ import org.apache.hugegraph.util.DateUtil; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Events; +import org.apache.hugegraph.util.JsonUtil; import org.apache.hugegraph.util.LockUtil; import org.apache.hugegraph.util.Log; +import org.apache.hugegraph.variables.CheckList; import org.apache.hugegraph.variables.HugeVariables; import org.apache.tinkerpop.gremlin.process.computer.GraphComputer; import org.apache.tinkerpop.gremlin.structure.Edge; @@ -180,6 +186,8 @@ public class StandardHugeGraph implements HugeGraph { private final String schedulerType; + private KvClient client; + public StandardHugeGraph(HugeConfig config) { this.params = new StandardHugeGraphParams(); this.configuration = config; @@ -1003,14 +1011,32 @@ public synchronized void close() throws Exception { } @Override - public void create(String configPath, GlobalMasterInfo nodeInfo) { - this.initBackend(); - this.serverStarted(nodeInfo); + public void create(String configPath, GlobalMasterInfo nodeInfo){ + //CheckList checkList = new CheckList(); + KResponse result = null; + try { + result = client.get(this.name); + String json = result.getValue(); + CheckList checkList = JsonUtil.fromJson(json, CheckList.class); + + this.initBackend(); + checkList.setInitBackended(true); + checkList.setStage("initBackend"); + client.put(name, JsonUtil.toJson(checkList)); + this.serverStarted(nodeInfo); + checkList.setServerStarted(true); + checkList.setStage("setServerStarted"); + client.put(name, JsonUtil.toJson(checkList)); + + + // Write config to disk file + String confPath = ConfigUtil.writeToFile(configPath, this.name(), + this.configuration()); + this.configuration.file(confPath); + } catch (Exception e) { + + } - // Write config to disk file - String confPath = ConfigUtil.writeToFile(configPath, this.name(), - this.configuration()); - this.configuration.file(confPath); } @Override diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java new file mode 100644 index 0000000000..ce85780af9 --- /dev/null +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java @@ -0,0 +1,93 @@ +package org.apache.hugegraph.variables; + +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.masterelection.GlobalMasterInfo; + +import java.io.Serializable; + +public class CheckList implements Serializable { + + private String name; + private String configText; + + private HugeConfig config; + + private boolean initBackended; + + private boolean serverStarted; + + private String stage; + + private String configPath; + + private GlobalMasterInfo nodeInfo; + + boolean toCheck; + String context; + private boolean isBuild; + + public void setBuild(boolean build) { + isBuild = build; + } + + public CheckList(String name, String context) { + this.name = name; + this.context = context; + } + + public HugeConfig getConfig() { + return config; + } + + public void setConfig(HugeConfig config) { + this.config = config; + } + + public boolean isInitBackended() { + return initBackended; + } + + public void setInitBackended(boolean initBackended) { + this.initBackended = initBackended; + } + + public boolean isServerStarted() { + return serverStarted; + } + + public void setServerStarted(boolean serverStarted) { + this.serverStarted = serverStarted; + } + + public String getStage() { + return stage; + } + + public void setStage(String stage) { + this.stage = stage; + } + + public String getConfigPath() { + return configPath; + } + + public void setConfigPath(String configPath) { + this.configPath = configPath; + } + + public GlobalMasterInfo getNodeInfo() { + return nodeInfo; + } + + public void setNodeInfo(GlobalMasterInfo nodeInfo) { + this.nodeInfo = nodeInfo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java b/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java new file mode 100644 index 0000000000..2ceb5f0713 --- /dev/null +++ b/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java @@ -0,0 +1,79 @@ +package org.apache.hugegraph; + +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.masterelection.GlobalMasterInfo; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; +import org.apache.hugegraph.util.ConfigUtil; +import org.apache.hugegraph.util.Events; +import org.apache.hugegraph.util.JsonUtil; +import org.apache.hugegraph.variables.CheckList; +import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; + + + +public class TxScanner { + private final String prefix = "graph_creat_tx"; + + private KvClient client; + + public TxScanner(KvClient client) { + } + + + public void scan() { + try { + ScanPrefixResponse response = this.client.scanPrefix(prefix); + for(String key : response.getKvsMap().keySet()) { + String value = response.getKvsMap().get(key); + CheckList checkList = JsonUtil.fromJson(value, CheckList.class); + switch (checkList.getStage()) { + case "config": { + configContinue(checkList); + } + case "initBackend" : { + HugeConfig config = checkList.getConfig(); + HugeGraph graph = (HugeGraph) GraphFactory.open(config); + GlobalMasterInfo globalMasterInfo = checkList.getNodeInfo(); + graph.serverStarted(globalMasterInfo); + // Write config to disk file + String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), + (HugeConfig)graph.configuration()); + } + case "setServerStarted" : { + HugeConfig config = checkList.getConfig(); + HugeGraph graph = (HugeGraph) GraphFactory.open(config); + String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), + (HugeConfig)graph.configuration()); + } + case "finish" : { + client.delete(prefix + checkList.getName()); + } + } + } + } catch (PDException e) { + throw new RuntimeException(e); + } + + } + + private void configContinue(CheckList checkList) { + HugeConfig config = checkList.getConfig(); + HugeGraph graph = (HugeGraph) GraphFactory.open(config); + try { + // Create graph instance + graph = (HugeGraph) GraphFactory.open(config); + String configPath = checkList.getConfigPath(); + GlobalMasterInfo globalMasterInfo = checkList.getNodeInfo(); + // Init graph and start it + graph.create(configPath, globalMasterInfo); + } catch (Throwable e) { + throw e; + } + + } + +} From 0c79c2c6c715ff15ee02bbb989ba5066148485d8 Mon Sep 17 00:00:00 2001 From: Aph-CUG <2582215315@qq.com> Date: Fri, 25 Oct 2024 16:54:39 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BA=8B=E5=8A=A1=E6=80=A7=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\256\241\346\226\207\346\241\243.md" | 239 ++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 "\350\256\276\350\256\241\346\226\207\346\241\243.md" diff --git "a/\350\256\276\350\256\241\346\226\207\346\241\243.md" "b/\350\256\276\350\256\241\346\226\207\346\241\243.md" new file mode 100644 index 0000000000..4dac72d30f --- /dev/null +++ "b/\350\256\276\350\256\241\346\226\207\346\241\243.md" @@ -0,0 +1,239 @@ +## 事务日志——checklist + +使用checklist来执行事务日志的记录,checklist以类的形式保存,记录检查点,每执行完一个节点需要做对应的记录。 + +checklist的结构如下: + +```java +package org.apache.hugegraph.variables; + +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.masterelection.GlobalMasterInfo; + +import java.io.Serializable; + +public class CheckList implements Serializable { + + private String name; + private String configText; + + private HugeConfig config; + + private boolean initBackended; + + private boolean serverStarted; + + private String stage; + + private String configPath; + + private GlobalMasterInfo nodeInfo; + + boolean toCheck; + String context; + private boolean isBuild; + + public void setBuild(boolean build) { + isBuild = build; + } + + public CheckList(String name, String context) { + this.name = name; + this.context = context; + } + + public HugeConfig getConfig() { + return config; + } + + public void setConfig(HugeConfig config) { + this.config = config; + } + + public boolean isInitBackended() { + return initBackended; + } + + public void setInitBackended(boolean initBackended) { + this.initBackended = initBackended; + } + + public boolean isServerStarted() { + return serverStarted; + } + + public void setServerStarted(boolean serverStarted) { + this.serverStarted = serverStarted; + } + + public String getStage() { + return stage; + } + + public void setStage(String stage) { + this.stage = stage; + } + + public String getConfigPath() { + return configPath; + } + + public void setConfigPath(String configPath) { + this.configPath = configPath; + } + + public GlobalMasterInfo getNodeInfo() { + return nodeInfo; + } + + public void setNodeInfo(GlobalMasterInfo nodeInfo) { + this.nodeInfo = nodeInfo; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + +``` + + + +主要涉及到的函数有GraphsAPI类中的create、drop两个函数,以create为例,在原来的流程之上,在检查点处保存log信息: + +```java + public void create(String configPath, GlobalMasterInfo nodeInfo){ + //CheckList checkList = new CheckList(); + KResponse result = null; + try { + result = client.get(this.name); + String json = result.getValue(); + CheckList checkList = JsonUtil.fromJson(json, CheckList.class); + + this.initBackend(); + checkList.setInitBackended(true); + checkList.setStage("initBackend"); + client.put(name, JsonUtil.toJson(checkList)); + this.serverStarted(nodeInfo); + checkList.setServerStarted(true); + checkList.setStage("setServerStarted"); + client.put(name, JsonUtil.toJson(checkList)); + + + // Write config to disk file + String confPath = ConfigUtil.writeToFile(configPath, this.name(), + this.configuration()); + this.configuration.file(confPath); + } +``` + + + +除此之外,还有一些上下文信息需要保存,便于重新恢复: + +```java +checkList.setConfig(config); +``` + + + +序列化与日志存放,分别借用了hugegraph中的Jsonutil与pd中的kvclient: + +```java +private KvClient client; +CheckList checkList = JsonUtil.fromJson(json, CheckList.class); +client.put(name, JsonUtil.toJson(checkList)); +``` + + + +恢复机制:在HugeGraphServer中,启动时添加对事务的扫描,扫描的机制为,使用KvClient的前缀扫描,遇到没有执行完的,进行恢复执行。 + +```java +package org.apache.hugegraph; + +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.masterelection.GlobalMasterInfo; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; +import org.apache.hugegraph.util.ConfigUtil; +import org.apache.hugegraph.util.Events; +import org.apache.hugegraph.util.JsonUtil; +import org.apache.hugegraph.variables.CheckList; +import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; + + + +public class TxScanner { + private final String prefix = "graph_creat_tx"; + + private KvClient client; + + public TxScanner(KvClient client) { + } + + + public void scan() { + try { + ScanPrefixResponse response = this.client.scanPrefix(prefix); + for(String key : response.getKvsMap().keySet()) { + String value = response.getKvsMap().get(key); + CheckList checkList = JsonUtil.fromJson(value, CheckList.class); + switch (checkList.getStage()) { + case "config": { + configContinue(checkList); + } + case "initBackend" : { + HugeConfig config = checkList.getConfig(); + HugeGraph graph = (HugeGraph) GraphFactory.open(config); + GlobalMasterInfo globalMasterInfo = checkList.getNodeInfo(); + graph.serverStarted(globalMasterInfo); + // Write config to disk file + String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), + (HugeConfig)graph.configuration()); + } + case "setServerStarted" : { + HugeConfig config = checkList.getConfig(); + HugeGraph graph = (HugeGraph) GraphFactory.open(config); + String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), + (HugeConfig)graph.configuration()); + } + case "finish" : { + client.delete(prefix + checkList.getName()); + } + } + } + } catch (PDException e) { + throw new RuntimeException(e); + } + + } + + private void configContinue(CheckList checkList) { + HugeConfig config = checkList.getConfig(); + HugeGraph graph = (HugeGraph) GraphFactory.open(config); + try { + // Create graph instance + graph = (HugeGraph) GraphFactory.open(config); + String configPath = checkList.getConfigPath(); + GlobalMasterInfo globalMasterInfo = checkList.getNodeInfo(); + // Init graph and start it + graph.create(configPath, globalMasterInfo); + } catch (Throwable e) { + throw e; + } + + } + +} +``` + + + From e19bf223fe7fd722cc280a2ea96bb344788e941f Mon Sep 17 00:00:00 2001 From: Aph-CUG <2582215315@qq> Date: Tue, 29 Oct 2024 20:21:31 -0700 Subject: [PATCH 3/4] update --- .../src/main/java/org/apache/hugegraph/TxScanner.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java b/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java index 2ceb5f0713..79c96afd80 100644 --- a/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java +++ b/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java @@ -33,6 +33,7 @@ public void scan() { switch (checkList.getStage()) { case "config": { configContinue(checkList); + break; } case "initBackend" : { HugeConfig config = checkList.getConfig(); @@ -42,15 +43,18 @@ public void scan() { // Write config to disk file String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), (HugeConfig)graph.configuration()); + break; } case "setServerStarted" : { HugeConfig config = checkList.getConfig(); HugeGraph graph = (HugeGraph) GraphFactory.open(config); String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), (HugeConfig)graph.configuration()); + break; } case "finish" : { client.delete(prefix + checkList.getName()); + break; } } } From 3062fa323f73d2c178e651e81375f2d77d04fd9f Mon Sep 17 00:00:00 2001 From: Aph-CUG <2582215315@qq> Date: Fri, 1 Nov 2024 06:32:02 -0700 Subject: [PATCH 4/4] update --- .../apache/hugegraph/core/GraphManager.java | 37 +------- .../apache/hugegraph/StandardHugeGraph.java | 47 +++++----- .../apache/hugegraph/variables/CheckList.java | 93 ++++++++++++------- .../java/org/apache/hugegraph/TxScanner.java | 49 ++-------- 4 files changed, 90 insertions(+), 136 deletions(-) diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java index e4c3988ae9..b7d9998351 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java @@ -101,14 +101,6 @@ public final class GraphManager { private final HugeConfig conf; private final EventHub eventHub; - private final String preFix = "graph_creat_tx"; - - private KvClient client; - - @Before - public void setUp() { - this.client = new KvClient<>(PDConfig.of("localhost:8686")); - } public GraphManager(HugeConfig conf, EventHub hub) { this.graphsDir = conf.get(ServerOptions.GRAPHS); @@ -198,36 +190,13 @@ public HugeGraph createGraph(String name, String configText) { "The graph name can't be null or empty"); E.checkArgument(!this.graphs().contains(name), "The graph name '%s' has existed", name); - - CheckList checkList = new CheckList(name, configText); - - PropertiesConfiguration propConfig = ConfigUtil.buildConfig(configText); HugeConfig config = new HugeConfig(propConfig); this.checkOptions(config); - - checkList.setConfig(config); - checkList.setStage("config"); - - String json = JsonUtil.toJson(checkList); - - try { - client.put(preFix + name, json); - } - - catch (PDException e) { - throw new RuntimeException(e); - } - - checkList.setStage("finish"); - try { - client.put(preFix + name, json); - } - - catch (PDException e) { - throw new RuntimeException(e); - } + return this.createGraph(config, name); + } + public HugeGraph createGraphRecover(HugeConfig config, String name) { return this.createGraph(config, name); } diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java index 223a82bc28..ac67e862dc 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java @@ -72,6 +72,7 @@ import org.apache.hugegraph.masterelection.StandardRoleElectionStateMachine; import org.apache.hugegraph.meta.MetaManager; import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.client.PDConfig; import org.apache.hugegraph.pd.common.PDException; import org.apache.hugegraph.pd.grpc.kv.KResponse; import org.apache.hugegraph.pd.grpc.kv.WatchResponse; @@ -186,8 +187,8 @@ public class StandardHugeGraph implements HugeGraph { private final String schedulerType; - private KvClient client; - + private KvClient client = new KvClient<>(PDConfig.of("localhost:8686")); + private CheckList checkList; public StandardHugeGraph(HugeConfig config) { this.params = new StandardHugeGraphParams(); this.configuration = config; @@ -222,6 +223,7 @@ public StandardHugeGraph(HugeConfig config) { this.mode = GraphMode.NONE; this.readMode = GraphReadMode.OLTP_ONLY; this.schedulerType = config.get(CoreOptions.SCHEDULER_TYPE); + this.checkList = new CheckList(this.name, this.configuration); LockUtil.init(this.name); @@ -293,8 +295,10 @@ public void serverStarted(GlobalMasterInfo nodeInfo) { LOG.info("Init server info [{}-{}] for graph '{}'...", nodeInfo.nodeId(), nodeInfo.nodeRole(), this.name); - this.serverInfoManager().initServerInfo(nodeInfo); - + if(!this.checkList.isInitServerInfo()) { + this.serverInfoManager().initServerInfo(nodeInfo); + this.checkList.setInitServerInfo(); + } this.initRoleStateMachine(nodeInfo.nodeId()); // TODO: check necessary? @@ -435,9 +439,18 @@ public void truncateBackend() { @Override public void initSystemInfo() { try { - this.taskScheduler().init(); - this.serverInfoManager().init(); - this.authManager().init(); + if(!this.checkList.isTaskSchedulerInit()) { + this.taskScheduler().init(); + this.checkList.setTaskSchedulerInit(); + } + if(!this.checkList.isServerInfoManagerInit()) { + this.serverInfoManager().init(); + this.checkList.setTaskSchedulerInit(); + } + if(!this.checkList.isAuthManagerInit()) { + this.authManager().init(); + this.checkList.setAuthManagerInit(); + } } finally { this.closeTx(); } @@ -1012,31 +1025,15 @@ public synchronized void close() throws Exception { @Override public void create(String configPath, GlobalMasterInfo nodeInfo){ - //CheckList checkList = new CheckList(); - KResponse result = null; - try { - result = client.get(this.name); - String json = result.getValue(); - CheckList checkList = JsonUtil.fromJson(json, CheckList.class); - + this.checkList.setConfigPath(configPath); + this.checkList.setNodeInfo(nodeInfo); this.initBackend(); - checkList.setInitBackended(true); - checkList.setStage("initBackend"); - client.put(name, JsonUtil.toJson(checkList)); this.serverStarted(nodeInfo); - checkList.setServerStarted(true); - checkList.setStage("setServerStarted"); - client.put(name, JsonUtil.toJson(checkList)); - // Write config to disk file String confPath = ConfigUtil.writeToFile(configPath, this.name(), this.configuration()); this.configuration.file(confPath); - } catch (Exception e) { - - } - } @Override diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java index ce85780af9..cb580f59e6 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/variables/CheckList.java @@ -2,69 +2,98 @@ import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.masterelection.GlobalMasterInfo; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.client.PDConfig; +import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; +import org.apache.hugegraph.util.JsonUtil; +import org.junit.Before; import java.io.Serializable; public class CheckList implements Serializable { private String name; - private String configText; - + private HugeConfig config; - private boolean initBackended; + private boolean taskSchedulerInit; + + private boolean serverInfoManagerInit; - private boolean serverStarted; + private boolean authManagerInit; - private String stage; + private boolean initServerInfo; private String configPath; private GlobalMasterInfo nodeInfo; - boolean toCheck; - String context; - private boolean isBuild; + private KvClient client; + - public void setBuild(boolean build) { - isBuild = build; + private final String preFix = "graph_creat_tx_"; + + @Before + public void setUp() { + this.client = new KvClient<>(PDConfig.of("localhost:8686")); } - public CheckList(String name, String context) { + public CheckList(String name, HugeConfig config) { this.name = name; - this.context = context; + this.config = config; } - public HugeConfig getConfig() { - return config; + public String getName() { + return name; } - public void setConfig(HugeConfig config) { - this.config = config; + public boolean isTaskSchedulerInit() { + return taskSchedulerInit; } - public boolean isInitBackended() { - return initBackended; + public void setTaskSchedulerInit() { + this.taskSchedulerInit = true; + String json = JsonUtil.toJson(this); + tryPut(json); } - public void setInitBackended(boolean initBackended) { - this.initBackended = initBackended; + public boolean isServerInfoManagerInit() { + return serverInfoManagerInit; } - public boolean isServerStarted() { - return serverStarted; + public void setServerInfoManagerInit() { + this.serverInfoManagerInit = true; + String json = JsonUtil.toJson(this); + tryPut(json); } - public void setServerStarted(boolean serverStarted) { - this.serverStarted = serverStarted; + public boolean isAuthManagerInit() { + return authManagerInit; } - public String getStage() { - return stage; + public void setAuthManagerInit() { + this.authManagerInit = true; + String json = JsonUtil.toJson(this); + tryPut(json); } - public void setStage(String stage) { - this.stage = stage; + public boolean isInitServerInfo() { + return initServerInfo; + } + + public void setInitServerInfo() { + this.initServerInfo = true; + String json = JsonUtil.toJson(this); + tryPut(json); + } + + private void tryPut(String json) { + try { + client.put(preFix + name, json); + } catch (PDException e) { + throw new RuntimeException(e); + } } public String getConfigPath() { @@ -83,11 +112,7 @@ public void setNodeInfo(GlobalMasterInfo nodeInfo) { this.nodeInfo = nodeInfo; } - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; + public HugeConfig getConfig() { + return config; } } diff --git a/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java b/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java index 2ceb5f0713..7ec47f359d 100644 --- a/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java +++ b/hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/TxScanner.java @@ -1,5 +1,6 @@ package org.apache.hugegraph; +import jakarta.ws.rs.core.Context; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.masterelection.GlobalMasterInfo; @@ -16,64 +17,26 @@ public class TxScanner { - private final String prefix = "graph_creat_tx"; + private final String prefix = "graph_creat_tx_"; + + @Context + GraphManager manager; private KvClient client; public TxScanner(KvClient client) { } - public void scan() { try { ScanPrefixResponse response = this.client.scanPrefix(prefix); for(String key : response.getKvsMap().keySet()) { String value = response.getKvsMap().get(key); CheckList checkList = JsonUtil.fromJson(value, CheckList.class); - switch (checkList.getStage()) { - case "config": { - configContinue(checkList); - } - case "initBackend" : { - HugeConfig config = checkList.getConfig(); - HugeGraph graph = (HugeGraph) GraphFactory.open(config); - GlobalMasterInfo globalMasterInfo = checkList.getNodeInfo(); - graph.serverStarted(globalMasterInfo); - // Write config to disk file - String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), - (HugeConfig)graph.configuration()); - } - case "setServerStarted" : { - HugeConfig config = checkList.getConfig(); - HugeGraph graph = (HugeGraph) GraphFactory.open(config); - String confPath = ConfigUtil.writeToFile(checkList.getConfigPath(), graph.name(), - (HugeConfig)graph.configuration()); - } - case "finish" : { - client.delete(prefix + checkList.getName()); - } - } + HugeGraph graph = manager.createGraphRecover(checkList.getConfig(), checkList.getName()); } } catch (PDException e) { throw new RuntimeException(e); } - } - - private void configContinue(CheckList checkList) { - HugeConfig config = checkList.getConfig(); - HugeGraph graph = (HugeGraph) GraphFactory.open(config); - try { - // Create graph instance - graph = (HugeGraph) GraphFactory.open(config); - String configPath = checkList.getConfigPath(); - GlobalMasterInfo globalMasterInfo = checkList.getNodeInfo(); - // Init graph and start it - graph.create(configPath, globalMasterInfo); - } catch (Throwable e) { - throw e; - } - - } - }