From d6a32da3599a9e10ad96ae703ef91229c62205a8 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Fri, 29 Jan 2021 15:50:54 +0800 Subject: [PATCH 01/20] Add multi-node auth information sharing function --- hugegraph-api/pom.xml | 23 +++++ .../hugegraph/auth/HugeGraphAuthProxy.java | 8 +- .../hugegraph/auth/StandardAuthenticator.java | 8 ++ .../baidu/hugegraph/config/ServerOptions.java | 65 +++++++++++++ .../baidu/hugegraph/core/GraphManager.java | 14 +++ .../hugegraph/server/ApplicationConfig.java | 6 ++ .../hugegraph/sofarpc/RpcClientProvider.java | 43 +++++++++ .../hugegraph/sofarpc/RpcConsumerConfig.java | 57 ++++++++++++ .../hugegraph/sofarpc/RpcProviderConfig.java | 48 ++++++++++ .../hugegraph/sofarpc/RpcServerProvider.java | 52 +++++++++++ .../hugegraph/sofarpc/SofaRpcClient.java | 43 +++++++++ .../hugegraph/sofarpc/SofaRpcServer.java | 92 +++++++++++++++++++ .../java/com/baidu/hugegraph/HugeGraph.java | 1 + .../baidu/hugegraph/StandardHugeGraph.java | 7 +- .../com/baidu/hugegraph/auth/HugeUser.java | 1 - .../baidu/hugegraph/auth/SchemaDefine.java | 4 +- hugegraph-core/src/main/resources/log4j2.xml | 23 +++++ .../static/conf/rest-server.properties | 9 ++ 18 files changed, 499 insertions(+), 5 deletions(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java create mode 100644 hugegraph-core/src/main/resources/log4j2.xml diff --git a/hugegraph-api/pom.xml b/hugegraph-api/pom.xml index b963c6bfca..7eafd3b250 100644 --- a/hugegraph-api/pom.xml +++ b/hugegraph-api/pom.xml @@ -87,6 +87,29 @@ grizzly-http-servlet 2.4.4 + + + + com.lmax + disruptor + 3.4.2 + + + com.alipay.sofa + sofa-rpc-all + 5.7.6 + + + io.netty + netty-all + + + + + io.netty + netty-all + 4.1.56.Final + diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java index f58cc35f44..e7f239e8ba 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java @@ -101,7 +101,7 @@ public final class HugeGraphAuthProxy implements HugeGraph { private final HugeGraph hugegraph; private final TaskScheduler taskScheduler; - private final UserManager userManager; + private UserManager userManager; public HugeGraphAuthProxy(HugeGraph hugegraph) { LOG.info("Wrap graph '{}' with HugeGraphAuthProxy", hugegraph.name()); @@ -652,6 +652,12 @@ public UserManager userManager() { return this.userManager; } + @Override + public void swichUserManager(UserManager userManager) { + verifyAdminPermission(); + this.hugegraph.swichUserManager(userManager); + } + @Override public RaftGroupManager raftGroupManager(String group) { this.verifyAdminPermission(); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java index 0e0d215c05..9ba1304ef0 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java @@ -24,11 +24,13 @@ import java.util.Scanner; import org.apache.commons.lang.NotImplementedException; +import org.apache.commons.lang.StringUtils; import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; +import com.baidu.hugegraph.sofarpc.RpcClientProvider; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.StringEncoding; @@ -78,6 +80,12 @@ public void setup(HugeConfig config) { E.checkArgument(graphPath != null, "Invalid graph name '%s'", graphName); this.graph = (HugeGraph) GraphFactory.open(graphPath); + + String remoteUrl = config.get(ServerOptions.AUTH_REMOTE_URL); + if (StringUtils.isNotEmpty(remoteUrl)) { + this.graph.swichUserManager(new RpcClientProvider(config). + getUserManagerService()); + } } /** diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index f5a5e3c289..c06661688c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -222,6 +222,71 @@ public static synchronized ServerOptions instance() { "hugegraph:9fd95c9c-711b-415b-b85f-d4df46ba5c31" ); + public static final ConfigOption AUTH_REMOTE_URL = + new ConfigOption<>( + "auth.remote_url", + "Auth server address", + null, + "" + ); + + public static final ConfigOption RPC_SERVER_PORT = + new ConfigOption<>( + "rpc.server.port", + "Rpc server port", + rangeInt(-1, Integer.MAX_VALUE), + 8099 + ); + + public static final ConfigOption RPC_SERVER_HOST = + new ConfigOption<>( + "rpc.server.host", + "Rpc server host", + disallowEmpty(), + "0.0.0.0" + ); + + public static final ConfigOption RPC_SERVER_TIMEOUT = + new ConfigOption<>( + "rpc.server.timeout", + "Rpc server connction timeout", + rangeInt(-1, Integer.MAX_VALUE), + 8000 + ); + + public static final ConfigOption RPC_SERVER_RETRIES = + new ConfigOption<>( + "rpc.server.retries", + "Rpc server failed and retries times", + rangeInt(-1, Integer.MAX_VALUE), + 3 + ); + + public static final ConfigOption RPC_CONFIG_ORDER = + new ConfigOption<>( + "rpc.config.order", + "Sofa rpc loading order, the larger the more after " + + "loading", + rangeInt(-1, Integer.MAX_VALUE), + 999 + ); + + public static final ConfigOption RPC_LOGGER_IMPL = + new ConfigOption<>( + "rpc.logger.impl", + "Sofa rpc log implementation class", + disallowEmpty(), + "com.alipay.sofa.rpc.log.SLF4JLoggerImpl" + ); + + public static final ConfigOption RPC_PROTOCOL = + new ConfigOption<>( + "rpc.protocol", + "Rpc communication protocol", + disallowEmpty(), + "bolt" + ); + public static final ConfigOption SSL_KEYSTORE_FILE = new ConfigOption<>( "ssl.keystore_file", diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 82269a28b0..d02a332e53 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -54,6 +54,7 @@ import com.baidu.hugegraph.serializer.JsonSerializer; import com.baidu.hugegraph.serializer.Serializer; import com.baidu.hugegraph.server.RestServer; +import com.baidu.hugegraph.sofarpc.RpcServerProvider; import com.baidu.hugegraph.task.TaskManager; import com.baidu.hugegraph.type.define.NodeRole; import com.baidu.hugegraph.util.E; @@ -65,6 +66,7 @@ public final class GraphManager { private final Map graphs; private final HugeAuthenticator authenticator; + private RpcServerProvider rpcServerProvider; public GraphManager(HugeConfig conf) { this.graphs = new ConcurrentHashMap<>(); @@ -75,6 +77,7 @@ public GraphManager(HugeConfig conf) { // Raft will load snapshot firstly then launch election and replay log this.waitGraphsStarted(); this.checkBackendVersionOrExit(); + this.rpcServerStart(conf); this.serverStarted(conf); this.addMetrics(conf); } @@ -159,6 +162,17 @@ public UserManager userManager() { return this.authenticator().userManager(); } + private void rpcServerStart(HugeConfig conf) { + if (this.authenticator != null) { + this.rpcServerProvider = new RpcServerProvider(conf, + this.authenticator.userManager()); + } + } + + public void rpcDestory() { + this.rpcServerProvider.destroy(); + } + private HugeAuthenticator authenticator() { E.checkState(this.authenticator != null, "Unconfigured authenticator"); return this.authenticator; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java index f8cf2fb2ca..cde518f523 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java @@ -102,10 +102,16 @@ public GraphManagerFactory(HugeConfig conf) { register(new ApplicationEventListener() { private final ApplicationEvent.Type EVENT_INITED = ApplicationEvent.Type.INITIALIZATION_FINISHED; + private final ApplicationEvent.Type EVENT_DESTROY = + ApplicationEvent.Type.DESTROY_FINISHED; @Override public void onEvent(ApplicationEvent event) { if (event.getType() == this.EVENT_INITED) { manager = new GraphManager(conf); + } else if(event.getType() == this.EVENT_DESTROY) { + if (manager != null) { + manager.rpcDestory(); + } } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java new file mode 100644 index 0000000000..17c3c9153e --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java @@ -0,0 +1,43 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc; + +import com.baidu.hugegraph.auth.UserManager; +import com.baidu.hugegraph.config.HugeConfig; + +public class RpcClientProvider { + + public final SofaRpcClient rpcClient; + public final RpcConsumerConfig rpcConsumerConfig; + + public RpcClientProvider(HugeConfig conf) { + this.rpcConsumerConfig = new RpcConsumerConfig(UserManager.class, conf); + this.rpcClient = SofaRpcClient.getInstance(); + } + + public UserManager getUserManagerService() { + return (UserManager)this.rpcClient.getServer(UserManager.class.getName(), + this.rpcConsumerConfig); + } + + public Object getServer(String serviceName) { + return this.rpcClient.getServer(serviceName,this.rpcConsumerConfig); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java new file mode 100644 index 0000000000..0102bf3fe9 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc; + +import java.util.Map; + +import com.alipay.sofa.rpc.config.ConsumerConfig; +import com.alipay.sofa.rpc.core.exception.RpcErrorType; +import com.alipay.sofa.rpc.core.exception.SofaRpcException; +import com.baidu.hugegraph.config.HugeConfig; +import com.baidu.hugegraph.config.ServerOptions; +import com.google.common.collect.Maps; + +public class RpcConsumerConfig { + + private final Map CONSUMER_CONFIG = + Maps.newHashMap(); + + public RpcConsumerConfig(Class clazz, HugeConfig conf) { + this.buildConsumerConfig(clazz, conf); + } + + public void buildConsumerConfig(Class clazz, HugeConfig conf) { + ConsumerConfig consumerConfig = new ConsumerConfig() + .setInterfaceId(clazz.getName()) + .setDirectUrl(conf.get(ServerOptions.AUTH_REMOTE_URL)) + .setTimeout(conf.get(ServerOptions.RPC_SERVER_TIMEOUT)) + .setRetries(conf.get(ServerOptions.RPC_SERVER_RETRIES)); + CONSUMER_CONFIG.put(clazz.getName(), consumerConfig); + } + + public ConsumerConfig getConsumerConfig(String serverName) { + if (!CONSUMER_CONFIG.containsKey(serverName)) { + throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, + String.format("Invalid server name " + + "%s", serverName)); + } + return CONSUMER_CONFIG.get(serverName); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java new file mode 100644 index 0000000000..d7d8f3d778 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java @@ -0,0 +1,48 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc; + +import java.util.Map; + +import com.alipay.sofa.rpc.config.ProviderConfig; +import com.google.common.collect.Maps; + +public class RpcProviderConfig { + + private final Map PROVIDER_CONFIG = + Maps.newHashMap(); + + public RpcProviderConfig(Class clazz, + E serviceImpl) { + this.buildProviderConfig(clazz, serviceImpl); + } + + public void buildProviderConfig(Class clazz, + E serviceImpl) { + ProviderConfig providerConfig = new ProviderConfig() + .setInterfaceId(clazz.getName()) + .setRef(serviceImpl); + PROVIDER_CONFIG.put(clazz.getName(), providerConfig); + } + + public Map getProviderConfigMap() { + return PROVIDER_CONFIG; + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java new file mode 100644 index 0000000000..d0dee2a87a --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java @@ -0,0 +1,52 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc; + +import org.slf4j.Logger; + +import com.baidu.hugegraph.auth.UserManager; +import com.baidu.hugegraph.config.HugeConfig; +import com.baidu.hugegraph.config.ServerOptions; +import com.baidu.hugegraph.util.Log; + +public class RpcServerProvider { + + private static final Logger LOG = Log.logger(RpcServerProvider.class); + + public final SofaRpcServer rpcServer; + + public RpcServerProvider(HugeConfig conf, UserManager userManager) { + LOG.info("rpcServer start {}", conf.get(ServerOptions.RPC_SERVER_PORT)); + RpcProviderConfig rpcProviderConfig = + new RpcProviderConfig(UserManager.class, userManager); + this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); + this.rpcServer.exportAll(); + LOG.info("rpcServer start success, bind port is {}", + this.rpcServer.getBindPort()); + } + + public void unExport(String serviceName) { + this.rpcServer.unExport(serviceName); + } + + public void destroy() { + this.rpcServer.destroy(); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java new file mode 100644 index 0000000000..85efd6eb22 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java @@ -0,0 +1,43 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc; + +import com.alipay.sofa.rpc.config.ConsumerConfig; + +public class SofaRpcClient { + + private SofaRpcClient() { + } + + private static class SofaRpcClientHolder { + private final static SofaRpcClient INSTANCE = new SofaRpcClient(); + } + + public static SofaRpcClient getInstance() { + return SofaRpcClientHolder.INSTANCE; + } + + public Object getServer(String serverName, + RpcConsumerConfig rpcConsumerConfig) { + ConsumerConfig consumerConfig = rpcConsumerConfig.getConsumerConfig + (serverName); + return consumerConfig.refer(); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java new file mode 100644 index 0000000000..1c475d75d6 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java @@ -0,0 +1,92 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc; + +import java.util.Map; + +import org.apache.commons.collections.MapUtils; + +import com.alipay.sofa.rpc.common.RpcConfigs; +import com.alipay.sofa.rpc.common.RpcOptions; +import com.alipay.sofa.rpc.config.ProviderConfig; +import com.alipay.sofa.rpc.config.ServerConfig; +import com.alipay.sofa.rpc.context.RpcRuntimeContext; +import com.alipay.sofa.rpc.core.exception.RpcErrorType; +import com.alipay.sofa.rpc.core.exception.SofaRpcException; +import com.baidu.hugegraph.config.HugeConfig; +import com.baidu.hugegraph.config.ServerOptions; + +public class SofaRpcServer { + + private Map providerConfigMap; + private ServerConfig serverConfig; + + static { + if (RpcConfigs.getOrDefaultValue(RpcOptions.JVM_SHUTDOWN_HOOK, true)) { + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + RpcRuntimeContext.destroy(); + } + }, "SOFA-RPC-ShutdownHook")); + } + } + + public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { + RpcConfigs.putValue("rpc.config.order", + conf.get(ServerOptions.RPC_CONFIG_ORDER)); + RpcConfigs.putValue("logger.impl", + conf.get(ServerOptions.RPC_LOGGER_IMPL)); + this.serverConfig = new ServerConfig() + .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) + .setPort(conf.get(ServerOptions.RPC_SERVER_PORT)) + .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) + .setDaemon(false); + this.providerConfigMap = providerConfig.getProviderConfigMap(); + } + + public void exportAll() { + if (MapUtils.isEmpty(this.providerConfigMap)) { + throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, + "Provider config map is empty"); + } + for (ProviderConfig providerConfig : this.providerConfigMap.values()) { + providerConfig.setServer(this.serverConfig); + providerConfig.export(); + } + } + + public int getBindPort() { + return this.serverConfig.getPort(); + } + + public void unExport(String serviceName) { + if (!this.providerConfigMap.containsKey(serviceName)) { + throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, + "Service name is not exist, please " + + "change others"); + } + this.providerConfigMap.get(serviceName).unExport(); + } + + public void destroy() { + this.serverConfig.destroy(); + } +} diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index e3d8f44be2..f1fc85def0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -155,6 +155,7 @@ public interface HugeGraph extends Graph { public HugeFeatures features(); public UserManager userManager(); + public void swichUserManager(UserManager userManager); public TaskScheduler taskScheduler(); public RaftGroupManager raftGroupManager(String group); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java index dbb4ac055b..8d2cc9f9d6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java @@ -140,7 +140,7 @@ public class StandardHugeGraph implements HugeGraph { private final RateLimiter writeRateLimiter; private final RateLimiter readRateLimiter; private final TaskManager taskManager; - private final UserManager userManager; + private UserManager userManager; private final HugeFeatures features; @@ -883,6 +883,11 @@ public UserManager userManager() { return this.userManager; } + @Override + public void swichUserManager(UserManager userManager) { + this.userManager = userManager; + } + @Override public RaftGroupManager raftGroupManager(String group) { if (!(this.storeProvider instanceof RaftBackendStoreProvider)) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeUser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeUser.java index d47385deed..78d376fd49 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeUser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/HugeUser.java @@ -29,7 +29,6 @@ import org.apache.tinkerpop.gremlin.structure.Vertex; import com.baidu.hugegraph.HugeGraphParams; -import com.baidu.hugegraph.auth.ResourceType; import com.baidu.hugegraph.auth.SchemaDefine.Entity; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.schema.VertexLabel; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/SchemaDefine.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/SchemaDefine.java index e38da0c29d..57bf0e4fd0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/SchemaDefine.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/auth/SchemaDefine.java @@ -19,6 +19,7 @@ package com.baidu.hugegraph.auth; +import java.io.Serializable; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -32,7 +33,6 @@ import com.baidu.hugegraph.HugeGraphParams; import com.baidu.hugegraph.auth.HugeTarget.P; -import com.baidu.hugegraph.auth.ResourceType; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.schema.IndexLabel; import com.baidu.hugegraph.schema.PropertyKey; @@ -116,7 +116,7 @@ protected static String unhideField(String label, String key) { return Hidden.unHide(label) + "_" + key; } - public static abstract class UserElement { + public static abstract class UserElement implements Serializable { protected static final String CREATE = "create"; protected static final String UPDATE = "update"; diff --git a/hugegraph-core/src/main/resources/log4j2.xml b/hugegraph-core/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..601b29afd5 --- /dev/null +++ b/hugegraph-core/src/main/resources/log4j2.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 71e7daa4da..02b8eec27e 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -13,3 +13,12 @@ graphs=[hugegraph:conf/hugegraph.properties] server.id=server-1 server.role=master + +#auth.remote_url=10.103.168.25:8899 +rpc.server.port=8899 +rpc.server.host=172.24.174.30 +rpc.server.timeout=8000 +rpc.server.retries=3 +rpc.protocol=bolt +rpc.config.order=999 +rpc.logger.impl=com.alipay.sofa.rpc.log.SLF4JLoggerImpl From a4d7b2e62f3b780610760d58f159cd7b80300f26 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 1 Feb 2021 14:49:38 +0800 Subject: [PATCH 02/20] Optimize and update some code --- .../hugegraph/auth/HugeGraphAuthProxy.java | 4 +- .../hugegraph/auth/StandardAuthenticator.java | 4 +- .../baidu/hugegraph/config/ServerOptions.java | 22 +++++++--- .../baidu/hugegraph/core/GraphManager.java | 10 +++-- .../hugegraph/server/ApplicationConfig.java | 2 +- .../hugegraph/sofarpc/RpcClientProvider.java | 13 +++--- .../hugegraph/sofarpc/RpcConsumerConfig.java | 15 ++++--- .../hugegraph/sofarpc/RpcProviderConfig.java | 6 +-- .../hugegraph/sofarpc/RpcServerProvider.java | 2 +- .../hugegraph/sofarpc/SofaRpcClient.java | 43 ------------------- .../hugegraph/sofarpc/SofaRpcServer.java | 12 +++--- .../static/conf/rest-server.properties | 9 ++-- 12 files changed, 56 insertions(+), 86 deletions(-) delete mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java index e7f239e8ba..45af701408 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java @@ -101,7 +101,7 @@ public final class HugeGraphAuthProxy implements HugeGraph { private final HugeGraph hugegraph; private final TaskScheduler taskScheduler; - private UserManager userManager; + private final UserManager userManager; public HugeGraphAuthProxy(HugeGraph hugegraph) { LOG.info("Wrap graph '{}' with HugeGraphAuthProxy", hugegraph.name()); @@ -654,7 +654,7 @@ public UserManager userManager() { @Override public void swichUserManager(UserManager userManager) { - verifyAdminPermission(); + this.verifyAdminPermission(); this.hugegraph.swichUserManager(userManager); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java index 9ba1304ef0..f17d637933 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java @@ -83,8 +83,8 @@ public void setup(HugeConfig config) { String remoteUrl = config.get(ServerOptions.AUTH_REMOTE_URL); if (StringUtils.isNotEmpty(remoteUrl)) { - this.graph.swichUserManager(new RpcClientProvider(config). - getUserManagerService()); + RpcClientProvider provider = new RpcClientProvider(config); + this.graph.swichUserManager(provider.userManager()); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index c06661688c..9679871b9c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -232,7 +232,7 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_SERVER_PORT = new ConfigOption<>( - "rpc.server.port", + "rpcserver.port", "Rpc server port", rangeInt(-1, Integer.MAX_VALUE), 8099 @@ -240,23 +240,31 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_SERVER_HOST = new ConfigOption<>( - "rpc.server.host", + "rpcserver.host", "Rpc server host", disallowEmpty(), "0.0.0.0" ); - public static final ConfigOption RPC_SERVER_TIMEOUT = + public static final ConfigOption RPC_CONNECTION_TIMEOUT = new ConfigOption<>( - "rpc.server.timeout", - "Rpc server connction timeout", + "rpc.connection_timeout", + "Rpc connection timeout", rangeInt(-1, Integer.MAX_VALUE), 8000 ); - public static final ConfigOption RPC_SERVER_RETRIES = + public static final ConfigOption RPC_READ_TIMEOUT = new ConfigOption<>( - "rpc.server.retries", + "rpc.read_timeout", + "Rpc read timeout", + rangeInt(-1, Integer.MAX_VALUE), + 8000 + ); + + public static final ConfigOption RPC_RETRIES = + new ConfigOption<>( + "rpc.retries", "Rpc server failed and retries times", rangeInt(-1, Integer.MAX_VALUE), 3 diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index d02a332e53..90abb6a051 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -77,7 +77,7 @@ public GraphManager(HugeConfig conf) { // Raft will load snapshot firstly then launch election and replay log this.waitGraphsStarted(); this.checkBackendVersionOrExit(); - this.rpcServerStart(conf); + this.startRpcServer(conf); this.serverStarted(conf); this.addMetrics(conf); } @@ -162,17 +162,21 @@ public UserManager userManager() { return this.authenticator().userManager(); } - private void rpcServerStart(HugeConfig conf) { + private void startRpcServer(HugeConfig conf) { if (this.authenticator != null) { this.rpcServerProvider = new RpcServerProvider(conf, this.authenticator.userManager()); } } - public void rpcDestory() { + private void destoryRpcServer() { this.rpcServerProvider.destroy(); } + public void close() { + this.destoryRpcServer(); + } + private HugeAuthenticator authenticator() { E.checkState(this.authenticator != null, "Unconfigured authenticator"); return this.authenticator; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java index cde518f523..fe3f15b9df 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java @@ -110,7 +110,7 @@ public void onEvent(ApplicationEvent event) { manager = new GraphManager(conf); } else if(event.getType() == this.EVENT_DESTROY) { if (manager != null) { - manager.rpcDestory(); + manager.close(); } } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java index 17c3c9153e..7f14272302 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java @@ -19,25 +19,24 @@ package com.baidu.hugegraph.sofarpc; +import com.alipay.sofa.rpc.config.ConsumerConfig; import com.baidu.hugegraph.auth.UserManager; import com.baidu.hugegraph.config.HugeConfig; public class RpcClientProvider { - public final SofaRpcClient rpcClient; public final RpcConsumerConfig rpcConsumerConfig; public RpcClientProvider(HugeConfig conf) { this.rpcConsumerConfig = new RpcConsumerConfig(UserManager.class, conf); - this.rpcClient = SofaRpcClient.getInstance(); } - public UserManager getUserManagerService() { - return (UserManager)this.rpcClient.getServer(UserManager.class.getName(), - this.rpcConsumerConfig); + public UserManager userManager() { + return (UserManager) this.serviceProxy(UserManager.class.getName()); } - public Object getServer(String serviceName) { - return this.rpcClient.getServer(serviceName,this.rpcConsumerConfig); + public Object serviceProxy(String serviceName) { + ConsumerConfig config = rpcConsumerConfig.consumerConfig(serviceName); + return config.refer(); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java index 0102bf3fe9..450b02c5c2 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java @@ -30,7 +30,7 @@ public class RpcConsumerConfig { - private final Map CONSUMER_CONFIG = + private final Map CONFIG = Maps.newHashMap(); public RpcConsumerConfig(Class clazz, HugeConfig conf) { @@ -41,17 +41,18 @@ public void buildConsumerConfig(Class clazz, HugeConfig conf) { ConsumerConfig consumerConfig = new ConsumerConfig() .setInterfaceId(clazz.getName()) .setDirectUrl(conf.get(ServerOptions.AUTH_REMOTE_URL)) - .setTimeout(conf.get(ServerOptions.RPC_SERVER_TIMEOUT)) - .setRetries(conf.get(ServerOptions.RPC_SERVER_RETRIES)); - CONSUMER_CONFIG.put(clazz.getName(), consumerConfig); + .setTimeout(conf.get(ServerOptions.RPC_READ_TIMEOUT)) + .setConnectTimeout(conf.get(ServerOptions.RPC_CONNECTION_TIMEOUT)) + .setRetries(conf.get(ServerOptions.RPC_RETRIES)); + CONFIG.put(clazz.getName(), consumerConfig); } - public ConsumerConfig getConsumerConfig(String serverName) { - if (!CONSUMER_CONFIG.containsKey(serverName)) { + public ConsumerConfig consumerConfig(String serverName) { + if (!CONFIG.containsKey(serverName)) { throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, String.format("Invalid server name " + "%s", serverName)); } - return CONSUMER_CONFIG.get(serverName); + return CONFIG.get(serverName); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java index d7d8f3d778..a0f2ed9c1c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java @@ -37,12 +37,12 @@ public RpcProviderConfig(Class clazz, public void buildProviderConfig(Class clazz, E serviceImpl) { ProviderConfig providerConfig = new ProviderConfig() - .setInterfaceId(clazz.getName()) - .setRef(serviceImpl); + .setInterfaceId(clazz.getName()) + .setRef(serviceImpl); PROVIDER_CONFIG.put(clazz.getName(), providerConfig); } - public Map getProviderConfigMap() { + public Map providerConfigMap() { return PROVIDER_CONFIG; } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java index d0dee2a87a..76d7718ed4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java @@ -39,7 +39,7 @@ public RpcServerProvider(HugeConfig conf, UserManager userManager) { this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); this.rpcServer.exportAll(); LOG.info("rpcServer start success, bind port is {}", - this.rpcServer.getBindPort()); + this.rpcServer.port()); } public void unExport(String serviceName) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java deleted file mode 100644 index 85efd6eb22..0000000000 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcClient.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2017 HugeGraph Authors - * - * 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 com.baidu.hugegraph.sofarpc; - -import com.alipay.sofa.rpc.config.ConsumerConfig; - -public class SofaRpcClient { - - private SofaRpcClient() { - } - - private static class SofaRpcClientHolder { - private final static SofaRpcClient INSTANCE = new SofaRpcClient(); - } - - public static SofaRpcClient getInstance() { - return SofaRpcClientHolder.INSTANCE; - } - - public Object getServer(String serverName, - RpcConsumerConfig rpcConsumerConfig) { - ConsumerConfig consumerConfig = rpcConsumerConfig.getConsumerConfig - (serverName); - return consumerConfig.refer(); - } -} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java index 1c475d75d6..1be20243fe 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java @@ -55,11 +55,11 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { RpcConfigs.putValue("logger.impl", conf.get(ServerOptions.RPC_LOGGER_IMPL)); this.serverConfig = new ServerConfig() - .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) - .setPort(conf.get(ServerOptions.RPC_SERVER_PORT)) - .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) - .setDaemon(false); - this.providerConfigMap = providerConfig.getProviderConfigMap(); + .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) + .setPort(conf.get(ServerOptions.RPC_SERVER_PORT)) + .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) + .setDaemon(false); + this.providerConfigMap = providerConfig.providerConfigMap(); } public void exportAll() { @@ -73,7 +73,7 @@ public void exportAll() { } } - public int getBindPort() { + public int port() { return this.serverConfig.getPort(); } diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 02b8eec27e..0d0139ffc8 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -15,10 +15,11 @@ server.id=server-1 server.role=master #auth.remote_url=10.103.168.25:8899 -rpc.server.port=8899 -rpc.server.host=172.24.174.30 -rpc.server.timeout=8000 -rpc.server.retries=3 +rpcserver.port=8899 +rpcserver.host=172.24.174.30 +rpc.connection_timeout=8000 +rpc.read_timeout=8000 +rpc.retries=3 rpc.protocol=bolt rpc.config.order=999 rpc.logger.impl=com.alipay.sofa.rpc.log.SLF4JLoggerImpl From b43b6998756cfdc331f875c3161325e7b8e0b7fd Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 1 Feb 2021 16:46:05 +0800 Subject: [PATCH 03/20] Optimize and update some code --- .../baidu/hugegraph/config/ServerOptions.java | 66 ++++++++++--------- .../baidu/hugegraph/core/GraphManager.java | 8 +-- .../hugegraph/sofarpc/RpcConsumerConfig.java | 23 ++++--- .../hugegraph/sofarpc/RpcServerProvider.java | 9 +++ .../hugegraph/sofarpc/SofaRpcServer.java | 15 ++--- .../sofarpc/exception/RpcException.java | 39 +++++++++++ .../static/conf/rest-server.properties | 12 ++-- 7 files changed, 107 insertions(+), 65 deletions(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/exception/RpcException.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 9679871b9c..707c8a688c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -225,76 +225,80 @@ public static synchronized ServerOptions instance() { public static final ConfigOption AUTH_REMOTE_URL = new ConfigOption<>( "auth.remote_url", - "Auth server address", + "Auth server address. If the address is empty, it is " + + "only auth Service, otherwise it is auth server and auth " + + "client.", null, "" ); public static final ConfigOption RPC_SERVER_PORT = new ConfigOption<>( - "rpcserver.port", - "Rpc server port", - rangeInt(-1, Integer.MAX_VALUE), + "rpc.server_port", + "The port of rpc server for rpc client call.", + rangeInt(1, Integer.MAX_VALUE), 8099 ); public static final ConfigOption RPC_SERVER_HOST = new ConfigOption<>( - "rpcserver.host", - "Rpc server host", + "rpc.server_host", + "The specified host of rpc server for rpc client " + + "call.", disallowEmpty(), "0.0.0.0" ); - public static final ConfigOption RPC_CONNECTION_TIMEOUT = + public static final ConfigOption RPC_CLIENT_CONNECTION_TIMEOUT = new ConfigOption<>( - "rpc.connection_timeout", - "Rpc connection timeout", - rangeInt(-1, Integer.MAX_VALUE), + "rpc.client_connection_timeout", + "The rpc client connect rpc server timeout(seconds).", + rangeInt(1, Integer.MAX_VALUE), 8000 ); - public static final ConfigOption RPC_READ_TIMEOUT = + public static final ConfigOption RPC_CLIENT_READ_TIMEOUT = new ConfigOption<>( - "rpc.read_timeout", - "Rpc read timeout", - rangeInt(-1, Integer.MAX_VALUE), + "rpc.client_read_timeout", + "The rpc client read from rpc server timeout(seconds).", + rangeInt(1, Integer.MAX_VALUE), 8000 ); - public static final ConfigOption RPC_RETRIES = + public static final ConfigOption RPC_CLIENT_RETRIES = new ConfigOption<>( - "rpc.retries", - "Rpc server failed and retries times", - rangeInt(-1, Integer.MAX_VALUE), + "rpc.client_retries", + "Failed retry number of rpc client calls to rpc Server.", + rangeInt(1, Integer.MAX_VALUE), 3 ); + public static final ConfigOption RPC_PROTOCOL = + new ConfigOption<>( + "rpc.protocol", + "Rpc communication protocol, client and server need to " + + "be specified at the same time, and can match.", + disallowEmpty(), + "bolt" + ); + public static final ConfigOption RPC_CONFIG_ORDER = new ConfigOption<>( - "rpc.config.order", - "Sofa rpc loading order, the larger the more after " + + "rpc.config_order", + "Sofa rpc loading order, the larger the more after." + "loading", - rangeInt(-1, Integer.MAX_VALUE), + rangeInt(1, Integer.MAX_VALUE), 999 ); public static final ConfigOption RPC_LOGGER_IMPL = new ConfigOption<>( - "rpc.logger.impl", - "Sofa rpc log implementation class", + "rpc.logger_impl", + "Sofa rpc log implementation class.", disallowEmpty(), "com.alipay.sofa.rpc.log.SLF4JLoggerImpl" ); - public static final ConfigOption RPC_PROTOCOL = - new ConfigOption<>( - "rpc.protocol", - "Rpc communication protocol", - disallowEmpty(), - "bolt" - ); - public static final ConfigOption SSL_KEYSTORE_FILE = new ConfigOption<>( "ssl.keystore_file", diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 90abb6a051..1edb91278a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -162,6 +162,10 @@ public UserManager userManager() { return this.authenticator().userManager(); } + public void close() { + this.destoryRpcServer(); + } + private void startRpcServer(HugeConfig conf) { if (this.authenticator != null) { this.rpcServerProvider = new RpcServerProvider(conf, @@ -173,10 +177,6 @@ private void destoryRpcServer() { this.rpcServerProvider.destroy(); } - public void close() { - this.destoryRpcServer(); - } - private HugeAuthenticator authenticator() { E.checkState(this.authenticator != null, "Unconfigured authenticator"); return this.authenticator; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java index 450b02c5c2..ed0eea5800 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java @@ -22,15 +22,14 @@ import java.util.Map; import com.alipay.sofa.rpc.config.ConsumerConfig; -import com.alipay.sofa.rpc.core.exception.RpcErrorType; -import com.alipay.sofa.rpc.core.exception.SofaRpcException; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; +import com.baidu.hugegraph.sofarpc.exception.RpcException; import com.google.common.collect.Maps; public class RpcConsumerConfig { - private final Map CONFIG = + private final Map config = Maps.newHashMap(); public RpcConsumerConfig(Class clazz, HugeConfig conf) { @@ -40,19 +39,19 @@ public RpcConsumerConfig(Class clazz, HugeConfig conf) { public void buildConsumerConfig(Class clazz, HugeConfig conf) { ConsumerConfig consumerConfig = new ConsumerConfig() .setInterfaceId(clazz.getName()) + .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) .setDirectUrl(conf.get(ServerOptions.AUTH_REMOTE_URL)) - .setTimeout(conf.get(ServerOptions.RPC_READ_TIMEOUT)) - .setConnectTimeout(conf.get(ServerOptions.RPC_CONNECTION_TIMEOUT)) - .setRetries(conf.get(ServerOptions.RPC_RETRIES)); - CONFIG.put(clazz.getName(), consumerConfig); + .setTimeout(conf.get(ServerOptions.RPC_CLIENT_READ_TIMEOUT)) + .setConnectTimeout(conf.get( + ServerOptions.RPC_CLIENT_CONNECTION_TIMEOUT)) + .setRetries(conf.get(ServerOptions.RPC_CLIENT_RETRIES)); + config.put(clazz.getName(), consumerConfig); } public ConsumerConfig consumerConfig(String serverName) { - if (!CONFIG.containsKey(serverName)) { - throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, - String.format("Invalid server name " + - "%s", serverName)); + if (!config.containsKey(serverName)) { + throw new RpcException("Invalid server name '%s'", serverName); } - return CONFIG.get(serverName); + return config.get(serverName); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java index 76d7718ed4..59e5bf08b4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java @@ -21,6 +21,7 @@ import org.slf4j.Logger; +import com.alipay.sofa.rpc.common.RpcConfigs; import com.baidu.hugegraph.auth.UserManager; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; @@ -34,6 +35,7 @@ public class RpcServerProvider { public RpcServerProvider(HugeConfig conf, UserManager userManager) { LOG.info("rpcServer start {}", conf.get(ServerOptions.RPC_SERVER_PORT)); + this.initRpcConfigs(conf); RpcProviderConfig rpcProviderConfig = new RpcProviderConfig(UserManager.class, userManager); this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); @@ -42,6 +44,13 @@ public RpcServerProvider(HugeConfig conf, UserManager userManager) { this.rpcServer.port()); } + private void initRpcConfigs(HugeConfig conf) { + RpcConfigs.putValue("rpc.config.order", + conf.get(ServerOptions.RPC_CONFIG_ORDER)); + RpcConfigs.putValue("logger.impl", + conf.get(ServerOptions.RPC_LOGGER_IMPL)); + } + public void unExport(String serviceName) { this.rpcServer.unExport(serviceName); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java index 1be20243fe..857f0b89ae 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java @@ -28,10 +28,9 @@ import com.alipay.sofa.rpc.config.ProviderConfig; import com.alipay.sofa.rpc.config.ServerConfig; import com.alipay.sofa.rpc.context.RpcRuntimeContext; -import com.alipay.sofa.rpc.core.exception.RpcErrorType; -import com.alipay.sofa.rpc.core.exception.SofaRpcException; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; +import com.baidu.hugegraph.sofarpc.exception.RpcException; public class SofaRpcServer { @@ -50,10 +49,6 @@ public void run() { } public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { - RpcConfigs.putValue("rpc.config.order", - conf.get(ServerOptions.RPC_CONFIG_ORDER)); - RpcConfigs.putValue("logger.impl", - conf.get(ServerOptions.RPC_LOGGER_IMPL)); this.serverConfig = new ServerConfig() .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) .setPort(conf.get(ServerOptions.RPC_SERVER_PORT)) @@ -64,8 +59,7 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { public void exportAll() { if (MapUtils.isEmpty(this.providerConfigMap)) { - throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, - "Provider config map is empty"); + throw new RpcException("Server provider config map is empty"); } for (ProviderConfig providerConfig : this.providerConfigMap.values()) { providerConfig.setServer(this.serverConfig); @@ -79,9 +73,8 @@ public int port() { public void unExport(String serviceName) { if (!this.providerConfigMap.containsKey(serviceName)) { - throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, - "Service name is not exist, please " + - "change others"); + throw new RpcException("Service name '%s' is not exist, please " + + "change others", serviceName); } this.providerConfigMap.get(serviceName).unExport(); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/exception/RpcException.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/exception/RpcException.java new file mode 100644 index 0000000000..1cff61d5d0 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/exception/RpcException.java @@ -0,0 +1,39 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc.exception; + +public class RpcException extends RuntimeException { + + public RpcException(String message) { + super(message); + } + + public RpcException(String message, Throwable cause) { + super(message, cause); + } + + public RpcException(String message, Object... args) { + super(String.format(message, args)); + } + + public RpcException(String message, Throwable cause, Object... args) { + super(String.format(message, args), cause); + } +} diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 0d0139ffc8..9432ea53fe 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -15,11 +15,9 @@ server.id=server-1 server.role=master #auth.remote_url=10.103.168.25:8899 -rpcserver.port=8899 -rpcserver.host=172.24.174.30 -rpc.connection_timeout=8000 -rpc.read_timeout=8000 -rpc.retries=3 +rpc.server_port=8899 +rpc.server_host=172.24.174.30 +rpc.client_connection_timeout=8000 +rpc.client_read_timeout=8000 +rpc.client_retries=3 rpc.protocol=bolt -rpc.config.order=999 -rpc.logger.impl=com.alipay.sofa.rpc.log.SLF4JLoggerImpl From 431f5bce8ced616131051c39d9cdf681bf144ad6 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 2 Feb 2021 14:30:18 +0800 Subject: [PATCH 04/20] Optimize and update some code --- .../baidu/hugegraph/config/ServerOptions.java | 14 +++--- .../hugegraph/sofarpc/RpcClientProvider.java | 1 + .../hugegraph/sofarpc/RpcCommonConfig.java | 46 +++++++++++++++++++ .../hugegraph/sofarpc/RpcServerProvider.java | 12 +---- 4 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcCommonConfig.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 707c8a688c..dde6eb5807 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -225,9 +225,9 @@ public static synchronized ServerOptions instance() { public static final ConfigOption AUTH_REMOTE_URL = new ConfigOption<>( "auth.remote_url", - "Auth server address. If the address is empty, it is " + - "only auth Service, otherwise it is auth server and auth " + - "client.", + "If the address is empty, it provide auth service, " + + "otherwise it is auth client and also provide auth service " + + "through rpc forwarding.", null, "" ); @@ -235,7 +235,7 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_SERVER_PORT = new ConfigOption<>( "rpc.server_port", - "The port of rpc server for rpc client call.", + "The port of rpc server for rpc client calling.", rangeInt(1, Integer.MAX_VALUE), 8099 ); @@ -252,7 +252,8 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_CLIENT_CONNECTION_TIMEOUT = new ConfigOption<>( "rpc.client_connection_timeout", - "The rpc client connect rpc server timeout(seconds).", + "The timeout(in seconds) of rpc client connect to rpc " + + "server.", rangeInt(1, Integer.MAX_VALUE), 8000 ); @@ -260,7 +261,8 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_CLIENT_READ_TIMEOUT = new ConfigOption<>( "rpc.client_read_timeout", - "The rpc client read from rpc server timeout(seconds).", + "The timeout(in seconds) of rpc client read from rpc " + + "server.", rangeInt(1, Integer.MAX_VALUE), 8000 ); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java index 7f14272302..c09ce5050b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java @@ -28,6 +28,7 @@ public class RpcClientProvider { public final RpcConsumerConfig rpcConsumerConfig; public RpcClientProvider(HugeConfig conf) { + RpcCommonConfig.initRpcConfigs(conf); this.rpcConsumerConfig = new RpcConsumerConfig(UserManager.class, conf); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcCommonConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcCommonConfig.java new file mode 100644 index 0000000000..e72e62ef9e --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcCommonConfig.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * 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 com.baidu.hugegraph.sofarpc; + +import java.util.Map; + +import com.alipay.sofa.rpc.common.RpcConfigs; +import com.baidu.hugegraph.config.HugeConfig; +import com.baidu.hugegraph.config.ServerOptions; + +public class RpcCommonConfig { + + public static void initRpcConfigs(HugeConfig conf) { + RpcConfigs.putValue("rpc.config.order", + conf.get(ServerOptions.RPC_CONFIG_ORDER)); + RpcConfigs.putValue("logger.impl", + conf.get(ServerOptions.RPC_LOGGER_IMPL)); + } + + public static void initRpcConfigs(String key, Object value) { + RpcConfigs.putValue(key, value); + } + + public static void initRpcConfigs(Map conf) { + for(Map.Entry entry : conf.entrySet()) { + RpcConfigs.putValue(entry.getKey(), entry.getValue()); + } + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java index 59e5bf08b4..ac6494df57 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java @@ -21,7 +21,6 @@ import org.slf4j.Logger; -import com.alipay.sofa.rpc.common.RpcConfigs; import com.baidu.hugegraph.auth.UserManager; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; @@ -31,11 +30,11 @@ public class RpcServerProvider { private static final Logger LOG = Log.logger(RpcServerProvider.class); - public final SofaRpcServer rpcServer; + private final SofaRpcServer rpcServer; public RpcServerProvider(HugeConfig conf, UserManager userManager) { LOG.info("rpcServer start {}", conf.get(ServerOptions.RPC_SERVER_PORT)); - this.initRpcConfigs(conf); + RpcCommonConfig.initRpcConfigs(conf); RpcProviderConfig rpcProviderConfig = new RpcProviderConfig(UserManager.class, userManager); this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); @@ -44,13 +43,6 @@ public RpcServerProvider(HugeConfig conf, UserManager userManager) { this.rpcServer.port()); } - private void initRpcConfigs(HugeConfig conf) { - RpcConfigs.putValue("rpc.config.order", - conf.get(ServerOptions.RPC_CONFIG_ORDER)); - RpcConfigs.putValue("logger.impl", - conf.get(ServerOptions.RPC_LOGGER_IMPL)); - } - public void unExport(String serviceName) { this.rpcServer.unExport(serviceName); } From 28363927341f972254339d218405b211f278c782 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 2 Feb 2021 18:27:29 +0800 Subject: [PATCH 05/20] Optimize and update some code --- hugegraph-api/pom.xml | 4 ++++ .../baidu/hugegraph/auth/StandardAuthenticator.java | 2 +- .../java/com/baidu/hugegraph/core/GraphManager.java | 12 ++++++------ .../{sofarpc => rpc}/RpcClientProvider.java | 2 +- .../hugegraph/{sofarpc => rpc}/RpcCommonConfig.java | 2 +- .../{sofarpc => rpc}/RpcConsumerConfig.java | 3 +-- .../{sofarpc/exception => rpc}/RpcException.java | 2 +- .../{sofarpc => rpc}/RpcProviderConfig.java | 2 +- .../{sofarpc => rpc}/RpcServerProvider.java | 6 +++--- .../hugegraph/{sofarpc => rpc}/SofaRpcServer.java | 3 +-- 10 files changed, 20 insertions(+), 18 deletions(-) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{sofarpc => rpc}/RpcClientProvider.java (97%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{sofarpc => rpc}/RpcCommonConfig.java (97%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{sofarpc => rpc}/RpcConsumerConfig.java (95%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{sofarpc/exception => rpc}/RpcException.java (96%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{sofarpc => rpc}/RpcProviderConfig.java (97%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{sofarpc => rpc}/RpcServerProvider.java (91%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{sofarpc => rpc}/SofaRpcServer.java (96%) diff --git a/hugegraph-api/pom.xml b/hugegraph-api/pom.xml index 7eafd3b250..269f5d7f64 100644 --- a/hugegraph-api/pom.xml +++ b/hugegraph-api/pom.xml @@ -103,6 +103,10 @@ io.netty netty-all + + org.jboss.resteasy + resteasy-client + diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java index f17d637933..63a27cb3d7 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java @@ -30,7 +30,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; -import com.baidu.hugegraph.sofarpc.RpcClientProvider; +import com.baidu.hugegraph.rpc.RpcClientProvider; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.StringEncoding; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 1edb91278a..72fcf7ca49 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -54,7 +54,7 @@ import com.baidu.hugegraph.serializer.JsonSerializer; import com.baidu.hugegraph.serializer.Serializer; import com.baidu.hugegraph.server.RestServer; -import com.baidu.hugegraph.sofarpc.RpcServerProvider; +import com.baidu.hugegraph.rpc.RpcServerProvider; import com.baidu.hugegraph.task.TaskManager; import com.baidu.hugegraph.type.define.NodeRole; import com.baidu.hugegraph.util.E; @@ -66,7 +66,7 @@ public final class GraphManager { private final Map graphs; private final HugeAuthenticator authenticator; - private RpcServerProvider rpcServerProvider; + private final RpcServerProvider rpcServerProvider; public GraphManager(HugeConfig conf) { this.graphs = new ConcurrentHashMap<>(); @@ -77,7 +77,7 @@ public GraphManager(HugeConfig conf) { // Raft will load snapshot firstly then launch election and replay log this.waitGraphsStarted(); this.checkBackendVersionOrExit(); - this.startRpcServer(conf); + this.rpcServerProvider = startRpcServer(conf); this.serverStarted(conf); this.addMetrics(conf); } @@ -166,11 +166,11 @@ public void close() { this.destoryRpcServer(); } - private void startRpcServer(HugeConfig conf) { + private RpcServerProvider startRpcServer(HugeConfig conf) { if (this.authenticator != null) { - this.rpcServerProvider = new RpcServerProvider(conf, - this.authenticator.userManager()); + return new RpcServerProvider(conf, this.authenticator.userManager()); } + return null; } private void destoryRpcServer() { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java similarity index 97% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java index c09ce5050b..1acbd46589 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcClientProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.sofarpc; +package com.baidu.hugegraph.rpc; import com.alipay.sofa.rpc.config.ConsumerConfig; import com.baidu.hugegraph.auth.UserManager; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcCommonConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcCommonConfig.java similarity index 97% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcCommonConfig.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcCommonConfig.java index e72e62ef9e..d3ad769d10 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcCommonConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcCommonConfig.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.sofarpc; +package com.baidu.hugegraph.rpc; import java.util.Map; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java similarity index 95% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index ed0eea5800..1be45a5f9b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -17,14 +17,13 @@ * under the License. */ -package com.baidu.hugegraph.sofarpc; +package com.baidu.hugegraph.rpc; import java.util.Map; import com.alipay.sofa.rpc.config.ConsumerConfig; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; -import com.baidu.hugegraph.sofarpc.exception.RpcException; import com.google.common.collect.Maps; public class RpcConsumerConfig { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/exception/RpcException.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java similarity index 96% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/exception/RpcException.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java index 1cff61d5d0..c5d47764a4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/exception/RpcException.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.sofarpc.exception; +package com.baidu.hugegraph.rpc; public class RpcException extends RuntimeException { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java similarity index 97% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java index a0f2ed9c1c..1de2caf41c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcProviderConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.sofarpc; +package com.baidu.hugegraph.rpc; import java.util.Map; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java similarity index 91% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java index ac6494df57..420d098f3f 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/RpcServerProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.sofarpc; +package com.baidu.hugegraph.rpc; import org.slf4j.Logger; @@ -33,13 +33,13 @@ public class RpcServerProvider { private final SofaRpcServer rpcServer; public RpcServerProvider(HugeConfig conf, UserManager userManager) { - LOG.info("rpcServer start {}", conf.get(ServerOptions.RPC_SERVER_PORT)); + LOG.info("RpcServer start {}", conf.get(ServerOptions.RPC_SERVER_PORT)); RpcCommonConfig.initRpcConfigs(conf); RpcProviderConfig rpcProviderConfig = new RpcProviderConfig(UserManager.class, userManager); this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); this.rpcServer.exportAll(); - LOG.info("rpcServer start success, bind port is {}", + LOG.info("RpcServer start success, bind port is {}", this.rpcServer.port()); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java similarity index 96% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 857f0b89ae..94da3f00b9 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/sofarpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.sofarpc; +package com.baidu.hugegraph.rpc; import java.util.Map; @@ -30,7 +30,6 @@ import com.alipay.sofa.rpc.context.RpcRuntimeContext; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; -import com.baidu.hugegraph.sofarpc.exception.RpcException; public class SofaRpcServer { From bba8e75da653368b268c9d2fde9f27f735e4b0cd Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 2 Feb 2021 20:14:11 +0800 Subject: [PATCH 06/20] Optimize and update some code --- .../baidu/hugegraph/config/ServerOptions.java | 2 +- .../com/baidu/hugegraph/core/GraphManager.java | 2 +- .../baidu/hugegraph/rpc/RpcClientProvider.java | 3 ++- .../baidu/hugegraph/rpc/RpcConsumerConfig.java | 15 +++++---------- .../com/baidu/hugegraph/rpc/RpcException.java | 6 +++++- .../baidu/hugegraph/rpc/RpcProviderConfig.java | 18 ++++++------------ .../baidu/hugegraph/rpc/RpcServerProvider.java | 11 ++++++----- .../com/baidu/hugegraph/rpc/SofaRpcServer.java | 2 +- 8 files changed, 27 insertions(+), 32 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index dde6eb5807..8d6d63b9d4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -280,7 +280,7 @@ public static synchronized ServerOptions instance() { "rpc.protocol", "Rpc communication protocol, client and server need to " + "be specified at the same time, and can match.", - disallowEmpty(), + allowValues("bolt", "rest", "dubbo", "h2c", "http"), "bolt" ); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 72fcf7ca49..8f3bf75d50 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -77,7 +77,7 @@ public GraphManager(HugeConfig conf) { // Raft will load snapshot firstly then launch election and replay log this.waitGraphsStarted(); this.checkBackendVersionOrExit(); - this.rpcServerProvider = startRpcServer(conf); + this.rpcServerProvider = this.startRpcServer(conf); this.serverStarted(conf); this.addMetrics(conf); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java index 1acbd46589..ce6516ef7f 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java @@ -29,7 +29,8 @@ public class RpcClientProvider { public RpcClientProvider(HugeConfig conf) { RpcCommonConfig.initRpcConfigs(conf); - this.rpcConsumerConfig = new RpcConsumerConfig(UserManager.class, conf); + this.rpcConsumerConfig = new RpcConsumerConfig(); + this.rpcConsumerConfig.addConsumerConfig(UserManager.class, conf); } public UserManager userManager() { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index 1be45a5f9b..49c23e500b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -28,14 +28,9 @@ public class RpcConsumerConfig { - private final Map config = - Maps.newHashMap(); + private final Map configs = Maps.newHashMap(); - public RpcConsumerConfig(Class clazz, HugeConfig conf) { - this.buildConsumerConfig(clazz, conf); - } - - public void buildConsumerConfig(Class clazz, HugeConfig conf) { + public void addConsumerConfig(Class clazz, HugeConfig conf) { ConsumerConfig consumerConfig = new ConsumerConfig() .setInterfaceId(clazz.getName()) .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) @@ -44,13 +39,13 @@ public void buildConsumerConfig(Class clazz, HugeConfig conf) { .setConnectTimeout(conf.get( ServerOptions.RPC_CLIENT_CONNECTION_TIMEOUT)) .setRetries(conf.get(ServerOptions.RPC_CLIENT_RETRIES)); - config.put(clazz.getName(), consumerConfig); + configs.put(clazz.getName(), consumerConfig); } public ConsumerConfig consumerConfig(String serverName) { - if (!config.containsKey(serverName)) { + if (!configs.containsKey(serverName)) { throw new RpcException("Invalid server name '%s'", serverName); } - return config.get(serverName); + return configs.get(serverName); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java index c5d47764a4..012606d1e9 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java @@ -19,7 +19,11 @@ package com.baidu.hugegraph.rpc; -public class RpcException extends RuntimeException { +import com.baidu.hugegraph.HugeException; + +public class RpcException extends HugeException { + + private static final long serialVersionUID = -6067652498161184537L; public RpcException(String message) { super(message); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java index 1de2caf41c..73ad91adc3 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java @@ -26,23 +26,17 @@ public class RpcProviderConfig { - private final Map PROVIDER_CONFIG = - Maps.newHashMap(); + private final Map configs = Maps.newHashMap(); - public RpcProviderConfig(Class clazz, - E serviceImpl) { - this.buildProviderConfig(clazz, serviceImpl); - } - - public void buildProviderConfig(Class clazz, - E serviceImpl) { + public void addProviderConfig(Class clazz, + E serviceImpl) { ProviderConfig providerConfig = new ProviderConfig() .setInterfaceId(clazz.getName()) .setRef(serviceImpl); - PROVIDER_CONFIG.put(clazz.getName(), providerConfig); + configs.put(clazz.getName(), providerConfig); } - public Map providerConfigMap() { - return PROVIDER_CONFIG; + public Map providerConfigs() { + return configs; } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java index 420d098f3f..3eacff689b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java @@ -33,14 +33,15 @@ public class RpcServerProvider { private final SofaRpcServer rpcServer; public RpcServerProvider(HugeConfig conf, UserManager userManager) { - LOG.info("RpcServer start {}", conf.get(ServerOptions.RPC_SERVER_PORT)); + LOG.debug("RpcServer starting on port {}", + conf.get(ServerOptions.RPC_SERVER_PORT)); RpcCommonConfig.initRpcConfigs(conf); - RpcProviderConfig rpcProviderConfig = - new RpcProviderConfig(UserManager.class, userManager); + RpcProviderConfig rpcProviderConfig = new RpcProviderConfig(); + rpcProviderConfig.addProviderConfig(UserManager.class, userManager); this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); this.rpcServer.exportAll(); - LOG.info("RpcServer start success, bind port is {}", - this.rpcServer.port()); + LOG.debug("RpcServer started success on port {}", + this.rpcServer.port()); } public void unExport(String serviceName) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 94da3f00b9..c5206f2860 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -53,7 +53,7 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { .setPort(conf.get(ServerOptions.RPC_SERVER_PORT)) .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) .setDaemon(false); - this.providerConfigMap = providerConfig.providerConfigMap(); + this.providerConfigMap = providerConfig.providerConfigs(); } public void exportAll() { From f2cc09c6511e675a57c8a2db881252b134e46ad4 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 3 Feb 2021 08:59:42 +0800 Subject: [PATCH 07/20] Optimize and update some code --- .../main/java/com/baidu/hugegraph/rpc/RpcException.java | 4 ++-- .../java/com/baidu/hugegraph/rpc/RpcServerProvider.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java index 012606d1e9..4b03f6943a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcException.java @@ -34,10 +34,10 @@ public RpcException(String message, Throwable cause) { } public RpcException(String message, Object... args) { - super(String.format(message, args)); + super(message, args); } public RpcException(String message, Throwable cause, Object... args) { - super(String.format(message, args), cause); + super(message, cause, args); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java index 3eacff689b..0ba4d7b96b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java @@ -33,15 +33,15 @@ public class RpcServerProvider { private final SofaRpcServer rpcServer; public RpcServerProvider(HugeConfig conf, UserManager userManager) { - LOG.debug("RpcServer starting on port {}", - conf.get(ServerOptions.RPC_SERVER_PORT)); + LOG.info("RpcServer starting on port {}", + conf.get(ServerOptions.RPC_SERVER_PORT)); RpcCommonConfig.initRpcConfigs(conf); RpcProviderConfig rpcProviderConfig = new RpcProviderConfig(); rpcProviderConfig.addProviderConfig(UserManager.class, userManager); this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); this.rpcServer.exportAll(); - LOG.debug("RpcServer started success on port {}", - this.rpcServer.port()); + LOG.info("RpcServer started success on port {}", + this.rpcServer.port()); } public void unExport(String serviceName) { From 2b953b9b3352552db4d03408d4d27ff21fb36eda Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 3 Feb 2021 18:40:27 +0800 Subject: [PATCH 08/20] Optimize and update some code --- .../baidu/hugegraph/config/ServerOptions.java | 21 ++++++++++++++----- .../hugegraph/rpc/RpcConsumerConfig.java | 4 +++- .../hugegraph/rpc/RpcServerProvider.java | 4 ++-- .../baidu/hugegraph/rpc/SofaRpcServer.java | 20 +++++++++--------- .../static/conf/rest-server.properties | 5 +++-- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 8d6d63b9d4..802a4e1e3d 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -235,7 +235,7 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_SERVER_PORT = new ConfigOption<>( "rpc.server_port", - "The port of rpc server for rpc client calling.", + "The port bound by rpc server to provide services.", rangeInt(1, Integer.MAX_VALUE), 8099 ); @@ -243,8 +243,8 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_SERVER_HOST = new ConfigOption<>( "rpc.server_host", - "The specified host of rpc server for rpc client " + - "call.", + "The multiple hosts/ips bound by rpc server to provide " + + "services. Multiple hosts/ips link them with ',' together.", disallowEmpty(), "0.0.0.0" ); @@ -275,6 +275,17 @@ public static synchronized ServerOptions instance() { 3 ); + public static final ConfigOption RPC_CLIENT_LOAD_BALANCER = + new ConfigOption<>( + "rpc.client_load_balancer", + "The rpc client uses a load-balancing algorithm to " + + "configure the addresses of multiple rpc servers. Default " + + "value is 'consistentHash', means forwording by parameters.", + allowValues("random", "localPref", "roundRobin", + "consistentHash", "weightRoundRobin"), + "consistentHash" + ); + public static final ConfigOption RPC_PROTOCOL = new ConfigOption<>( "rpc.protocol", @@ -287,8 +298,8 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_CONFIG_ORDER = new ConfigOption<>( "rpc.config_order", - "Sofa rpc loading order, the larger the more after." + - "loading", + "Sofa rpc configuration file loading order, the larger " + + "the more later loading", rangeInt(1, Integer.MAX_VALUE), 999 ); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index 49c23e500b..fc6da41627 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -38,7 +38,9 @@ public void addConsumerConfig(Class clazz, HugeConfig conf) { .setTimeout(conf.get(ServerOptions.RPC_CLIENT_READ_TIMEOUT)) .setConnectTimeout(conf.get( ServerOptions.RPC_CLIENT_CONNECTION_TIMEOUT)) - .setRetries(conf.get(ServerOptions.RPC_CLIENT_RETRIES)); + .setRetries(conf.get(ServerOptions.RPC_CLIENT_RETRIES)) + .setLoadBalancer(conf.get( + ServerOptions.RPC_CLIENT_LOAD_BALANCER)); configs.put(clazz.getName(), consumerConfig); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java index 0ba4d7b96b..7b473cf036 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java @@ -33,8 +33,8 @@ public class RpcServerProvider { private final SofaRpcServer rpcServer; public RpcServerProvider(HugeConfig conf, UserManager userManager) { - LOG.info("RpcServer starting on port {}", - conf.get(ServerOptions.RPC_SERVER_PORT)); + LOG.debug("RpcServer starting on port {}", + conf.get(ServerOptions.RPC_SERVER_PORT)); RpcCommonConfig.initRpcConfigs(conf); RpcProviderConfig rpcProviderConfig = new RpcProviderConfig(); rpcProviderConfig.addProviderConfig(UserManager.class, userManager); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index c5206f2860..7fa5712708 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -33,7 +33,7 @@ public class SofaRpcServer { - private Map providerConfigMap; + private Map providerConfigs; private ServerConfig serverConfig; static { @@ -53,29 +53,29 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { .setPort(conf.get(ServerOptions.RPC_SERVER_PORT)) .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) .setDaemon(false); - this.providerConfigMap = providerConfig.providerConfigs(); + this.providerConfigs = providerConfig.providerConfigs(); } public void exportAll() { - if (MapUtils.isEmpty(this.providerConfigMap)) { + if (MapUtils.isEmpty(this.providerConfigs)) { throw new RpcException("Server provider config map is empty"); } - for (ProviderConfig providerConfig : this.providerConfigMap.values()) { + for (ProviderConfig providerConfig : this.providerConfigs.values()) { providerConfig.setServer(this.serverConfig); providerConfig.export(); } } - public int port() { - return this.serverConfig.getPort(); - } - public void unExport(String serviceName) { - if (!this.providerConfigMap.containsKey(serviceName)) { + if (!this.providerConfigs.containsKey(serviceName)) { throw new RpcException("Service name '%s' is not exist, please " + "change others", serviceName); } - this.providerConfigMap.get(serviceName).unExport(); + this.providerConfigs.get(serviceName).unExport(); + } + + public int port() { + return this.serverConfig.getPort(); } public void destroy() { diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 9432ea53fe..ebe074041a 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -14,10 +14,11 @@ graphs=[hugegraph:conf/hugegraph.properties] server.id=server-1 server.role=master -#auth.remote_url=10.103.168.25:8899 +#auth.remote_url=10.103.168.25:8899,10.103.168.25:8898,10.103.168.25:8897 rpc.server_port=8899 -rpc.server_host=172.24.174.30 +rpc.server_host=172.24.174.28 rpc.client_connection_timeout=8000 rpc.client_read_timeout=8000 rpc.client_retries=3 +rpc.client_load_balancer=consistentHash rpc.protocol=bolt From b10030b04528da66c1c76d62aa93acad4da44711 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Thu, 4 Feb 2021 09:11:44 +0800 Subject: [PATCH 09/20] Optimize and update some code --- .../java/com/baidu/hugegraph/config/ServerOptions.java | 10 ++++++---- .../src/assembly/static/conf/rest-server.properties | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 802a4e1e3d..0ba34e8f5a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -227,7 +227,8 @@ public static synchronized ServerOptions instance() { "auth.remote_url", "If the address is empty, it provide auth service, " + "otherwise it is auth client and also provide auth service " + - "through rpc forwarding.", + "through rpc forwarding. The remote url can set multiple " + + "addresses, which are linked by ','.", null, "" ); @@ -243,8 +244,8 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_SERVER_HOST = new ConfigOption<>( "rpc.server_host", - "The multiple hosts/ips bound by rpc server to provide " + - "services. Multiple hosts/ips link them with ',' together.", + "The hosts/ips bound by rpc server to provide " + + "services.", disallowEmpty(), "0.0.0.0" ); @@ -280,7 +281,8 @@ public static synchronized ServerOptions instance() { "rpc.client_load_balancer", "The rpc client uses a load-balancing algorithm to " + "configure the addresses of multiple rpc servers. Default " + - "value is 'consistentHash', means forwording by parameters.", + "value is 'consistentHash', means forwording by request " + + "parameters.", allowValues("random", "localPref", "roundRobin", "consistentHash", "weightRoundRobin"), "consistentHash" diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index ebe074041a..f028082ea4 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -16,7 +16,7 @@ server.role=master #auth.remote_url=10.103.168.25:8899,10.103.168.25:8898,10.103.168.25:8897 rpc.server_port=8899 -rpc.server_host=172.24.174.28 +rpc.server_host=127.0.0.1 rpc.client_connection_timeout=8000 rpc.client_read_timeout=8000 rpc.client_retries=3 From 7fdbbd54637cce258171f24f9285e4310042af3d Mon Sep 17 00:00:00 2001 From: xuliguo Date: Thu, 4 Feb 2021 17:16:10 +0800 Subject: [PATCH 10/20] Optimize and update some code --- .../java/com/baidu/hugegraph/rpc/RpcClientProvider.java | 2 +- .../java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java | 6 +++--- .../java/com/baidu/hugegraph/rpc/RpcProviderConfig.java | 4 ++-- .../main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java | 5 +++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java index ce6516ef7f..d028ce8104 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcClientProvider.java @@ -38,7 +38,7 @@ public UserManager userManager() { } public Object serviceProxy(String serviceName) { - ConsumerConfig config = rpcConsumerConfig.consumerConfig(serviceName); + ConsumerConfig config = this.rpcConsumerConfig.consumerConfig(serviceName); return config.refer(); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index fc6da41627..b32c38d8f8 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -41,13 +41,13 @@ public void addConsumerConfig(Class clazz, HugeConfig conf) { .setRetries(conf.get(ServerOptions.RPC_CLIENT_RETRIES)) .setLoadBalancer(conf.get( ServerOptions.RPC_CLIENT_LOAD_BALANCER)); - configs.put(clazz.getName(), consumerConfig); + this.configs.put(clazz.getName(), consumerConfig); } public ConsumerConfig consumerConfig(String serverName) { - if (!configs.containsKey(serverName)) { + if (!this.configs.containsKey(serverName)) { throw new RpcException("Invalid server name '%s'", serverName); } - return configs.get(serverName); + return this.configs.get(serverName); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java index 73ad91adc3..ca8c336e95 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java @@ -33,10 +33,10 @@ public void addProviderConfig(Class clazz, ProviderConfig providerConfig = new ProviderConfig() .setInterfaceId(clazz.getName()) .setRef(serviceImpl); - configs.put(clazz.getName(), providerConfig); + this.configs.put(clazz.getName(), providerConfig); } public Map providerConfigs() { - return configs; + return this.configs; } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 7fa5712708..56ec856b63 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -58,7 +58,8 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { public void exportAll() { if (MapUtils.isEmpty(this.providerConfigs)) { - throw new RpcException("Server provider config map is empty"); + throw new RpcException("The server provider config map can't be " + + "empty"); } for (ProviderConfig providerConfig : this.providerConfigs.values()) { providerConfig.setServer(this.serverConfig); @@ -68,7 +69,7 @@ public void exportAll() { public void unExport(String serviceName) { if (!this.providerConfigs.containsKey(serviceName)) { - throw new RpcException("Service name '%s' is not exist, please " + + throw new RpcException("The service name '%s' doesn't exist, please " + "change others", serviceName); } this.providerConfigs.get(serviceName).unExport(); From c354121f19abb5a7895d9061a27696b9c0bcaddd Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 8 Feb 2021 12:53:30 +0800 Subject: [PATCH 11/20] Optimize and update some code --- .../com/baidu/hugegraph/api/metrics/MetricsAPI.java | 2 +- .../baidu/hugegraph/auth/HugeGraphAuthProxy.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java index 27c4caed73..2c94c7b6dd 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java @@ -76,7 +76,7 @@ public String system() { @Timed @Path("backend") @Produces(APPLICATION_JSON_WITH_CHARSET) - @RolesAllowed({"admin", "$owner= $action=metrics_read"}) +// @RolesAllowed({"admin", "$owner= $action=metrics_read"}) public String backend(@Context GraphManager manager) { Map> results = InsertionOrderUtil.newMap(); for (String graph : manager.graphs()) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java index 45af701408..1a2fe8176d 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java @@ -100,8 +100,8 @@ public final class HugeGraphAuthProxy implements HugeGraph { private static final Logger LOG = Log.logger(HugeGraphAuthProxy.class); private final HugeGraph hugegraph; - private final TaskScheduler taskScheduler; - private final UserManager userManager; + private final TaskSchedulerProxy taskScheduler; + private final UserManagerProxy userManager; public HugeGraphAuthProxy(HugeGraph hugegraph) { LOG.info("Wrap graph '{}' with HugeGraphAuthProxy", hugegraph.name()); @@ -655,7 +655,7 @@ public UserManager userManager() { @Override public void swichUserManager(UserManager userManager) { this.verifyAdminPermission(); - this.hugegraph.swichUserManager(userManager); + this.userManager.swichUserManager(userManager); } @Override @@ -1050,7 +1050,7 @@ private boolean hasTaskPermission(HugeTask task) { class UserManagerProxy implements UserManager { - private final UserManager userManager; + private UserManager userManager; public UserManagerProxy(UserManager origin) { this.userManager = origin; @@ -1351,6 +1351,11 @@ public RolePermission loginUser(String username, String password) { setContext(context); } } + + private void swichUserManager(UserManager userManager) { + this.userManager = userManager; + hugegraph.swichUserManager(userManager); + } } class VariablesProxy implements Variables { From c1dc32b72783eba3bf705f4fa37d6dbdc64457d9 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 8 Feb 2021 16:51:23 +0800 Subject: [PATCH 12/20] Optimize and update some code --- .../java/com/baidu/hugegraph/api/metrics/MetricsAPI.java | 2 +- .../main/java/com/baidu/hugegraph/config/ServerOptions.java | 6 +++--- .../main/java/com/baidu/hugegraph/core/GraphManager.java | 4 +++- .../main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java index 2c94c7b6dd..35519cc45e 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java @@ -76,7 +76,7 @@ public String system() { @Timed @Path("backend") @Produces(APPLICATION_JSON_WITH_CHARSET) -// @RolesAllowed({"admin", "$owner= $action=metrics_read"}) +// @RolesAllowed({"admin", "$owner= $action=metrics_read"}) public String backend(@Context GraphManager manager) { Map> results = InsertionOrderUtil.newMap(); for (String graph : manager.graphs()) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 0ba34e8f5a..a384e24d7c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -227,8 +227,8 @@ public static synchronized ServerOptions instance() { "auth.remote_url", "If the address is empty, it provide auth service, " + "otherwise it is auth client and also provide auth service " + - "through rpc forwarding. The remote url can set multiple " + - "addresses, which are linked by ','.", + "through rpc forwarding. The remote url can be set to " + + "multiple addresses, which are linked by ','.", null, "" ); @@ -280,7 +280,7 @@ public static synchronized ServerOptions instance() { new ConfigOption<>( "rpc.client_load_balancer", "The rpc client uses a load-balancing algorithm to " + - "configure the addresses of multiple rpc servers. Default " + + "access multiple rpc servers in one cluster. Default " + "value is 'consistentHash', means forwording by request " + "parameters.", allowValues("random", "localPref", "roundRobin", diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 8f3bf75d50..a94f0a788e 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -174,7 +174,9 @@ private RpcServerProvider startRpcServer(HugeConfig conf) { } private void destoryRpcServer() { - this.rpcServerProvider.destroy(); + if (this.rpcServerProvider != null) { + this.rpcServerProvider.destroy(); + } } private HugeAuthenticator authenticator() { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 56ec856b63..0496ff8a5d 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -58,8 +58,8 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { public void exportAll() { if (MapUtils.isEmpty(this.providerConfigs)) { - throw new RpcException("The server provider config map can't be " + - "empty"); + throw new RpcException( + "The server provider config map can't be empty"); } for (ProviderConfig providerConfig : this.providerConfigs.values()) { providerConfig.setServer(this.serverConfig); From eec6ae2f1294109bdb97f87503bd6267d28d5bc9 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 8 Feb 2021 21:26:12 +0800 Subject: [PATCH 13/20] Optimize and update some code --- .../baidu/hugegraph/config/ServerOptions.java | 25 ++++++++++++++++--- .../hugegraph/rpc/RpcConsumerConfig.java | 6 +++-- .../baidu/hugegraph/rpc/SofaRpcServer.java | 3 +++ .../static/conf/rest-server.properties | 6 +++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index a384e24d7c..8050e399f3 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -250,22 +250,39 @@ public static synchronized ServerOptions instance() { "0.0.0.0" ); - public static final ConfigOption RPC_CLIENT_CONNECTION_TIMEOUT = + public static final ConfigOption RPC_SERVER_TIMEOUT = + new ConfigOption<>( + "rpc.server_timeout", + "The timeout(in seconds) of rpc server execution.", + rangeInt(1, Integer.MAX_VALUE), + 30 + ); + + public static final ConfigOption CLIENT_CONNECTION_TIMEOUT = new ConfigOption<>( "rpc.client_connection_timeout", "The timeout(in seconds) of rpc client connect to rpc " + "server.", rangeInt(1, Integer.MAX_VALUE), - 8000 + 20 + ); + + public static final ConfigOption CLIENT_RECONNECTION_TIMEOUT = + new ConfigOption<>( + "rpc.client_reconnection_timeout", + "The timeout(in seconds) of rpc client reconnect to rpc " + + "server.", + rangeInt(1, Integer.MAX_VALUE), + 20 ); - public static final ConfigOption RPC_CLIENT_READ_TIMEOUT = + public static final ConfigOption CLIENT_READ_TIMEOUT = new ConfigOption<>( "rpc.client_read_timeout", "The timeout(in seconds) of rpc client read from rpc " + "server.", rangeInt(1, Integer.MAX_VALUE), - 8000 + 40 ); public static final ConfigOption RPC_CLIENT_RETRIES = diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index b32c38d8f8..91c0573b26 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -35,9 +35,11 @@ public void addConsumerConfig(Class clazz, HugeConfig conf) { .setInterfaceId(clazz.getName()) .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) .setDirectUrl(conf.get(ServerOptions.AUTH_REMOTE_URL)) - .setTimeout(conf.get(ServerOptions.RPC_CLIENT_READ_TIMEOUT)) + .setTimeout(conf.get(ServerOptions.CLIENT_READ_TIMEOUT)*1000) .setConnectTimeout(conf.get( - ServerOptions.RPC_CLIENT_CONNECTION_TIMEOUT)) + ServerOptions.CLIENT_CONNECTION_TIMEOUT)*1000) + .setReconnectPeriod(conf.get( + ServerOptions.CLIENT_RECONNECTION_TIMEOUT) *1000) .setRetries(conf.get(ServerOptions.RPC_CLIENT_RETRIES)) .setLoadBalancer(conf.get( ServerOptions.RPC_CLIENT_LOAD_BALANCER)); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 0496ff8a5d..7fbc43bfce 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -35,6 +35,7 @@ public class SofaRpcServer { private Map providerConfigs; private ServerConfig serverConfig; + private final int rpcServerTimeout; static { if (RpcConfigs.getOrDefaultValue(RpcOptions.JVM_SHUTDOWN_HOOK, true)) { @@ -54,6 +55,7 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) .setDaemon(false); this.providerConfigs = providerConfig.providerConfigs(); + this.rpcServerTimeout = conf.get(ServerOptions.RPC_SERVER_TIMEOUT)*1000; } public void exportAll() { @@ -63,6 +65,7 @@ public void exportAll() { } for (ProviderConfig providerConfig : this.providerConfigs.values()) { providerConfig.setServer(this.serverConfig); + providerConfig.setTimeout(this.rpcServerTimeout); providerConfig.export(); } } diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index f028082ea4..b0b5a018c0 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -17,8 +17,10 @@ server.role=master #auth.remote_url=10.103.168.25:8899,10.103.168.25:8898,10.103.168.25:8897 rpc.server_port=8899 rpc.server_host=127.0.0.1 -rpc.client_connection_timeout=8000 -rpc.client_read_timeout=8000 +rpc.server_timeout=30 +rpc.client_connection_timeout=20 +rpc.client_reconnection_timeout=20 +rpc.client_read_timeout=40 rpc.client_retries=3 rpc.client_load_balancer=consistentHash rpc.protocol=bolt From 14a7e6d622eb42a5029a8e90dcbc5a1f2d24a4b8 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 9 Feb 2021 15:01:29 +0800 Subject: [PATCH 14/20] Exclude sofa rpc and other projects with the same jar package, and Optimize some code. --- hugegraph-api/pom.xml | 31 +++++++++++++++---- .../baidu/hugegraph/config/ServerOptions.java | 8 ++--- .../hugegraph/rpc/RpcConsumerConfig.java | 26 ++++++++++------ .../baidu/hugegraph/rpc/SofaRpcServer.java | 6 ++-- .../static/conf/rest-server.properties | 4 +-- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/hugegraph-api/pom.xml b/hugegraph-api/pom.xml index 269f5d7f64..b687b64ac2 100644 --- a/hugegraph-api/pom.xml +++ b/hugegraph-api/pom.xml @@ -92,7 +92,7 @@ com.lmax disruptor - 3.4.2 + 3.3.7 com.alipay.sofa @@ -107,13 +107,32 @@ org.jboss.resteasy resteasy-client + + com.alipay.sofa + bolt + + + org.apache.httpcomponents + httpcore + + + org.apache.httpcomponents + httpmime + + + com.alipay.sofa.common + sofa-common-tools + + + org.jboss.logging + jboss-logging + + + com.alipay.sofa + hessian + - - io.netty - netty-all - 4.1.56.Final - diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 8050e399f3..89b80ae9b3 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -258,18 +258,18 @@ public static synchronized ServerOptions instance() { 30 ); - public static final ConfigOption CLIENT_CONNECTION_TIMEOUT = + public static final ConfigOption CLIENT_CONNECT_TIMEOUT = new ConfigOption<>( - "rpc.client_connection_timeout", + "rpc.client_connect_timeout", "The timeout(in seconds) of rpc client connect to rpc " + "server.", rangeInt(1, Integer.MAX_VALUE), 20 ); - public static final ConfigOption CLIENT_RECONNECTION_TIMEOUT = + public static final ConfigOption CLIENT_RECONNECT_TIMEOUT = new ConfigOption<>( - "rpc.client_reconnection_timeout", + "rpc.client_reconnect_timeout", "The timeout(in seconds) of rpc client reconnect to rpc " + "server.", rangeInt(1, Integer.MAX_VALUE), diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index 91c0573b26..aed06fd67e 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -31,18 +31,24 @@ public class RpcConsumerConfig { private final Map configs = Maps.newHashMap(); public void addConsumerConfig(Class clazz, HugeConfig conf) { + String protocol = conf.get(ServerOptions.RPC_PROTOCOL); + String directUrl = conf.get(ServerOptions.AUTH_REMOTE_URL); + int timeout = conf.get(ServerOptions.CLIENT_READ_TIMEOUT) * 1000; + int connectTimeout = conf.get(ServerOptions + .CLIENT_CONNECT_TIMEOUT) * 1000; + int reconnectPeriod = conf.get(ServerOptions + .CLIENT_RECONNECT_TIMEOUT) * 1000; + int retries = conf.get(ServerOptions.RPC_CLIENT_RETRIES); + String loadBalancer = conf.get(ServerOptions.RPC_CLIENT_LOAD_BALANCER); ConsumerConfig consumerConfig = new ConsumerConfig() .setInterfaceId(clazz.getName()) - .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) - .setDirectUrl(conf.get(ServerOptions.AUTH_REMOTE_URL)) - .setTimeout(conf.get(ServerOptions.CLIENT_READ_TIMEOUT)*1000) - .setConnectTimeout(conf.get( - ServerOptions.CLIENT_CONNECTION_TIMEOUT)*1000) - .setReconnectPeriod(conf.get( - ServerOptions.CLIENT_RECONNECTION_TIMEOUT) *1000) - .setRetries(conf.get(ServerOptions.RPC_CLIENT_RETRIES)) - .setLoadBalancer(conf.get( - ServerOptions.RPC_CLIENT_LOAD_BALANCER)); + .setProtocol(protocol) + .setDirectUrl(directUrl) + .setTimeout(timeout) + .setConnectTimeout(connectTimeout) + .setReconnectPeriod(reconnectPeriod) + .setRetries(retries) + .setLoadBalancer(loadBalancer); this.configs.put(clazz.getName(), consumerConfig); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 7fbc43bfce..4cb02e9027 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -33,8 +33,8 @@ public class SofaRpcServer { - private Map providerConfigs; - private ServerConfig serverConfig; + private final Map providerConfigs; + private final ServerConfig serverConfig; private final int rpcServerTimeout; static { @@ -55,7 +55,7 @@ public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) .setDaemon(false); this.providerConfigs = providerConfig.providerConfigs(); - this.rpcServerTimeout = conf.get(ServerOptions.RPC_SERVER_TIMEOUT)*1000; + this.rpcServerTimeout = conf.get(ServerOptions.RPC_SERVER_TIMEOUT) * 1000; } public void exportAll() { diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index b0b5a018c0..66f12d20b5 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -18,8 +18,8 @@ server.role=master rpc.server_port=8899 rpc.server_host=127.0.0.1 rpc.server_timeout=30 -rpc.client_connection_timeout=20 -rpc.client_reconnection_timeout=20 +rpc.client_connect_timeout=20 +rpc.client_reconnect_timeout=20 rpc.client_read_timeout=40 rpc.client_retries=3 rpc.client_load_balancer=consistentHash From 3c78da113585706ed21b6cff13924b150fe80ad5 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 9 Feb 2021 15:49:30 +0800 Subject: [PATCH 15/20] Optimize and update some code --- .../main/java/com/baidu/hugegraph/config/ServerOptions.java | 4 ++-- .../main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 89b80ae9b3..ed4da0e375 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -258,7 +258,7 @@ public static synchronized ServerOptions instance() { 30 ); - public static final ConfigOption CLIENT_CONNECT_TIMEOUT = + public static final ConfigOption RPC_CLIENT_CONNECT_TIMEOUT = new ConfigOption<>( "rpc.client_connect_timeout", "The timeout(in seconds) of rpc client connect to rpc " + @@ -267,7 +267,7 @@ public static synchronized ServerOptions instance() { 20 ); - public static final ConfigOption CLIENT_RECONNECT_TIMEOUT = + public static final ConfigOption CLIENT_RECONNECT_PERIOD = new ConfigOption<>( "rpc.client_reconnect_timeout", "The timeout(in seconds) of rpc client reconnect to rpc " + diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index aed06fd67e..5a1a744d61 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -35,9 +35,9 @@ public void addConsumerConfig(Class clazz, HugeConfig conf) { String directUrl = conf.get(ServerOptions.AUTH_REMOTE_URL); int timeout = conf.get(ServerOptions.CLIENT_READ_TIMEOUT) * 1000; int connectTimeout = conf.get(ServerOptions - .CLIENT_CONNECT_TIMEOUT) * 1000; + .RPC_CLIENT_CONNECT_TIMEOUT) * 1000; int reconnectPeriod = conf.get(ServerOptions - .CLIENT_RECONNECT_TIMEOUT) * 1000; + .CLIENT_RECONNECT_PERIOD) * 1000; int retries = conf.get(ServerOptions.RPC_CLIENT_RETRIES); String loadBalancer = conf.get(ServerOptions.RPC_CLIENT_LOAD_BALANCER); ConsumerConfig consumerConfig = new ConsumerConfig() From ce81669d5d9e6fdd431aed59fdb5c82c5dd04322 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 9 Feb 2021 16:46:52 +0800 Subject: [PATCH 16/20] Optimize and update some code --- .../main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java | 2 +- .../main/java/com/baidu/hugegraph/config/ServerOptions.java | 4 ++-- .../main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java | 4 ++-- .../java/com/baidu/hugegraph/server/ApplicationConfig.java | 2 +- .../src/assembly/static/conf/rest-server.properties | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java index 35519cc45e..27c4caed73 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java @@ -76,7 +76,7 @@ public String system() { @Timed @Path("backend") @Produces(APPLICATION_JSON_WITH_CHARSET) -// @RolesAllowed({"admin", "$owner= $action=metrics_read"}) + @RolesAllowed({"admin", "$owner= $action=metrics_read"}) public String backend(@Context GraphManager manager) { Map> results = InsertionOrderUtil.newMap(); for (String graph : manager.graphs()) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index ed4da0e375..44df6e894b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -267,7 +267,7 @@ public static synchronized ServerOptions instance() { 20 ); - public static final ConfigOption CLIENT_RECONNECT_PERIOD = + public static final ConfigOption RPC_CLIENT_RECONNECT_PERIOD = new ConfigOption<>( "rpc.client_reconnect_timeout", "The timeout(in seconds) of rpc client reconnect to rpc " + @@ -276,7 +276,7 @@ public static synchronized ServerOptions instance() { 20 ); - public static final ConfigOption CLIENT_READ_TIMEOUT = + public static final ConfigOption RPC_CLIENT_READ_TIMEOUT = new ConfigOption<>( "rpc.client_read_timeout", "The timeout(in seconds) of rpc client read from rpc " + diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java index 5a1a744d61..ec92cc259b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcConsumerConfig.java @@ -33,11 +33,11 @@ public class RpcConsumerConfig { public void addConsumerConfig(Class clazz, HugeConfig conf) { String protocol = conf.get(ServerOptions.RPC_PROTOCOL); String directUrl = conf.get(ServerOptions.AUTH_REMOTE_URL); - int timeout = conf.get(ServerOptions.CLIENT_READ_TIMEOUT) * 1000; + int timeout = conf.get(ServerOptions.RPC_CLIENT_READ_TIMEOUT) * 1000; int connectTimeout = conf.get(ServerOptions .RPC_CLIENT_CONNECT_TIMEOUT) * 1000; int reconnectPeriod = conf.get(ServerOptions - .CLIENT_RECONNECT_PERIOD) * 1000; + .RPC_CLIENT_RECONNECT_PERIOD) * 1000; int retries = conf.get(ServerOptions.RPC_CLIENT_RETRIES); String loadBalancer = conf.get(ServerOptions.RPC_CLIENT_LOAD_BALANCER); ConsumerConfig consumerConfig = new ConsumerConfig() diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java index fe3f15b9df..b5134c8007 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java @@ -108,7 +108,7 @@ public GraphManagerFactory(HugeConfig conf) { public void onEvent(ApplicationEvent event) { if (event.getType() == this.EVENT_INITED) { manager = new GraphManager(conf); - } else if(event.getType() == this.EVENT_DESTROY) { + } else if (event.getType() == this.EVENT_DESTROY) { if (manager != null) { manager.close(); } diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 66f12d20b5..1400f70427 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -14,9 +14,9 @@ graphs=[hugegraph:conf/hugegraph.properties] server.id=server-1 server.role=master -#auth.remote_url=10.103.168.25:8899,10.103.168.25:8898,10.103.168.25:8897 -rpc.server_port=8899 +#auth.remote_url=127.0.0.1:8899,127.0.0.1:8898,127.0.0.1:8897 rpc.server_host=127.0.0.1 +rpc.server_port=8893 rpc.server_timeout=30 rpc.client_connect_timeout=20 rpc.client_reconnect_timeout=20 From fd508d66822070c603adec5d4744eb5520acf331 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 10 Feb 2021 11:52:58 +0800 Subject: [PATCH 17/20] Optimize and update some code --- .../baidu/hugegraph/config/ServerOptions.java | 14 +++++------ hugegraph-core/src/main/resources/log4j2.xml | 23 ------------------- .../static/conf/rest-server.properties | 2 +- 3 files changed, 8 insertions(+), 31 deletions(-) delete mode 100644 hugegraph-core/src/main/resources/log4j2.xml diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 44df6e894b..a46572285a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -269,11 +269,11 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_CLIENT_RECONNECT_PERIOD = new ConfigOption<>( - "rpc.client_reconnect_timeout", - "The timeout(in seconds) of rpc client reconnect to rpc " + + "rpc.client_reconnect_period", + "The period(in seconds) of rpc client reconnect to rpc " + "server.", rangeInt(1, Integer.MAX_VALUE), - 20 + 10 ); public static final ConfigOption RPC_CLIENT_READ_TIMEOUT = @@ -288,8 +288,8 @@ public static synchronized ServerOptions instance() { public static final ConfigOption RPC_CLIENT_RETRIES = new ConfigOption<>( "rpc.client_retries", - "Failed retry number of rpc client calls to rpc Server.", - rangeInt(1, Integer.MAX_VALUE), + "Failed retry number of rpc client calls to rpc server.", + rangeInt(0, Integer.MAX_VALUE), 3 ); @@ -309,7 +309,7 @@ public static synchronized ServerOptions instance() { new ConfigOption<>( "rpc.protocol", "Rpc communication protocol, client and server need to " + - "be specified at the same time, and can match.", + "be specified the same value, and can match.", allowValues("bolt", "rest", "dubbo", "h2c", "http"), "bolt" ); @@ -318,7 +318,7 @@ public static synchronized ServerOptions instance() { new ConfigOption<>( "rpc.config_order", "Sofa rpc configuration file loading order, the larger " + - "the more later loading", + "the more later loading.", rangeInt(1, Integer.MAX_VALUE), 999 ); diff --git a/hugegraph-core/src/main/resources/log4j2.xml b/hugegraph-core/src/main/resources/log4j2.xml deleted file mode 100644 index 601b29afd5..0000000000 --- a/hugegraph-core/src/main/resources/log4j2.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 1400f70427..1ae2a65f84 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -19,7 +19,7 @@ rpc.server_host=127.0.0.1 rpc.server_port=8893 rpc.server_timeout=30 rpc.client_connect_timeout=20 -rpc.client_reconnect_timeout=20 +rpc.client_reconnect_period=10 rpc.client_read_timeout=40 rpc.client_retries=3 rpc.client_load_balancer=consistentHash From b1660b715cd129fd8266fec7217b7d0e9cf0b039 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 10 Feb 2021 14:23:33 +0800 Subject: [PATCH 18/20] Optimize and update some code --- .../main/java/com/baidu/hugegraph/config/ServerOptions.java | 4 ++-- .../src/assembly/static/conf/rest-server.properties | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index a46572285a..dcce7c7493 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -247,7 +247,7 @@ public static synchronized ServerOptions instance() { "The hosts/ips bound by rpc server to provide " + "services.", disallowEmpty(), - "0.0.0.0" + "127.0.0.1" ); public static final ConfigOption RPC_SERVER_TIMEOUT = @@ -309,7 +309,7 @@ public static synchronized ServerOptions instance() { new ConfigOption<>( "rpc.protocol", "Rpc communication protocol, client and server need to " + - "be specified the same value, and can match.", + "be specified the same value.", allowValues("bolt", "rest", "dubbo", "h2c", "http"), "bolt" ); diff --git a/hugegraph-dist/src/assembly/static/conf/rest-server.properties b/hugegraph-dist/src/assembly/static/conf/rest-server.properties index 1ae2a65f84..4287e13566 100644 --- a/hugegraph-dist/src/assembly/static/conf/rest-server.properties +++ b/hugegraph-dist/src/assembly/static/conf/rest-server.properties @@ -10,11 +10,11 @@ graphs=[hugegraph:conf/hugegraph.properties] #auth.authenticator= #auth.admin_token= #auth.user_tokens=[] +#auth.remote_url=127.0.0.1:8899,127.0.0.1:8898,127.0.0.1:8897 server.id=server-1 server.role=master -#auth.remote_url=127.0.0.1:8899,127.0.0.1:8898,127.0.0.1:8897 rpc.server_host=127.0.0.1 rpc.server_port=8893 rpc.server_timeout=30 From d2747a652f7c1c367493fc313581403a274bb1a1 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 10 Feb 2021 15:50:04 +0800 Subject: [PATCH 19/20] Optimize and update some code --- .../hugegraph/auth/HugeGraphAuthProxy.java | 8 +-- .../hugegraph/auth/StandardAuthenticator.java | 2 +- .../baidu/hugegraph/core/GraphManager.java | 20 +++---- .../hugegraph/rpc/RpcProviderConfig.java | 5 +- .../hugegraph/rpc/RpcServerProvider.java | 54 ------------------- .../baidu/hugegraph/rpc/SofaRpcServer.java | 42 ++++++++++----- .../java/com/baidu/hugegraph/HugeGraph.java | 2 +- .../baidu/hugegraph/StandardHugeGraph.java | 2 +- 8 files changed, 48 insertions(+), 87 deletions(-) delete mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java index 1a2fe8176d..d06ebceb18 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java @@ -653,9 +653,9 @@ public UserManager userManager() { } @Override - public void swichUserManager(UserManager userManager) { + public void switchUserManager(UserManager userManager) { this.verifyAdminPermission(); - this.userManager.swichUserManager(userManager); + this.userManager.switchUserManager(userManager); } @Override @@ -1352,9 +1352,9 @@ public RolePermission loginUser(String username, String password) { } } - private void swichUserManager(UserManager userManager) { + private void switchUserManager(UserManager userManager) { this.userManager = userManager; - hugegraph.swichUserManager(userManager); + hugegraph.switchUserManager(userManager); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java index 63a27cb3d7..08c8a98ed6 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java @@ -84,7 +84,7 @@ public void setup(HugeConfig config) { String remoteUrl = config.get(ServerOptions.AUTH_REMOTE_URL); if (StringUtils.isNotEmpty(remoteUrl)) { RpcClientProvider provider = new RpcClientProvider(config); - this.graph.swichUserManager(provider.userManager()); + this.graph.switchUserManager(provider.userManager()); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index a94f0a788e..727f6ea034 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -51,10 +51,11 @@ import com.baidu.hugegraph.license.LicenseVerifier; import com.baidu.hugegraph.metrics.MetricsUtil; import com.baidu.hugegraph.metrics.ServerReporter; +import com.baidu.hugegraph.rpc.RpcProviderConfig; +import com.baidu.hugegraph.rpc.SofaRpcServer; import com.baidu.hugegraph.serializer.JsonSerializer; import com.baidu.hugegraph.serializer.Serializer; import com.baidu.hugegraph.server.RestServer; -import com.baidu.hugegraph.rpc.RpcServerProvider; import com.baidu.hugegraph.task.TaskManager; import com.baidu.hugegraph.type.define.NodeRole; import com.baidu.hugegraph.util.E; @@ -66,18 +67,19 @@ public final class GraphManager { private final Map graphs; private final HugeAuthenticator authenticator; - private final RpcServerProvider rpcServerProvider; + private final SofaRpcServer rpcServer; public GraphManager(HugeConfig conf) { this.graphs = new ConcurrentHashMap<>(); this.authenticator = HugeAuthenticator.loadAuthenticator(conf); + this.rpcServer = new SofaRpcServer(conf); this.loadGraphs(conf.getMap(ServerOptions.GRAPHS)); // this.installLicense(conf, ""); // Raft will load snapshot firstly then launch election and replay log this.waitGraphsStarted(); this.checkBackendVersionOrExit(); - this.rpcServerProvider = this.startRpcServer(conf); + this.startRpcServer(); this.serverStarted(conf); this.addMetrics(conf); } @@ -166,17 +168,17 @@ public void close() { this.destoryRpcServer(); } - private RpcServerProvider startRpcServer(HugeConfig conf) { + private void startRpcServer() { + RpcProviderConfig config = this.rpcServer.config(); if (this.authenticator != null) { - return new RpcServerProvider(conf, this.authenticator.userManager()); + config.addService(UserManager.class, + this.authenticator.userManager()); } - return null; + this.rpcServer.exportAll(); } private void destoryRpcServer() { - if (this.rpcServerProvider != null) { - this.rpcServerProvider.destroy(); - } + this.rpcServer.destroy(); } private HugeAuthenticator authenticator() { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java index ca8c336e95..096d339ca4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcProviderConfig.java @@ -28,15 +28,14 @@ public class RpcProviderConfig { private final Map configs = Maps.newHashMap(); - public void addProviderConfig(Class clazz, - E serviceImpl) { + public void addService(Class clazz, E serviceImpl) { ProviderConfig providerConfig = new ProviderConfig() .setInterfaceId(clazz.getName()) .setRef(serviceImpl); this.configs.put(clazz.getName(), providerConfig); } - public Map providerConfigs() { + public Map configs() { return this.configs; } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java deleted file mode 100644 index 7b473cf036..0000000000 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/RpcServerProvider.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2017 HugeGraph Authors - * - * 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 com.baidu.hugegraph.rpc; - -import org.slf4j.Logger; - -import com.baidu.hugegraph.auth.UserManager; -import com.baidu.hugegraph.config.HugeConfig; -import com.baidu.hugegraph.config.ServerOptions; -import com.baidu.hugegraph.util.Log; - -public class RpcServerProvider { - - private static final Logger LOG = Log.logger(RpcServerProvider.class); - - private final SofaRpcServer rpcServer; - - public RpcServerProvider(HugeConfig conf, UserManager userManager) { - LOG.debug("RpcServer starting on port {}", - conf.get(ServerOptions.RPC_SERVER_PORT)); - RpcCommonConfig.initRpcConfigs(conf); - RpcProviderConfig rpcProviderConfig = new RpcProviderConfig(); - rpcProviderConfig.addProviderConfig(UserManager.class, userManager); - this.rpcServer = new SofaRpcServer(conf, rpcProviderConfig); - this.rpcServer.exportAll(); - LOG.info("RpcServer started success on port {}", - this.rpcServer.port()); - } - - public void unExport(String serviceName) { - this.rpcServer.unExport(serviceName); - } - - public void destroy() { - this.rpcServer.destroy(); - } -} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 4cb02e9027..1c8355e916 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -22,6 +22,7 @@ import java.util.Map; import org.apache.commons.collections.MapUtils; +import org.slf4j.Logger; import com.alipay.sofa.rpc.common.RpcConfigs; import com.alipay.sofa.rpc.common.RpcOptions; @@ -30,12 +31,15 @@ import com.alipay.sofa.rpc.context.RpcRuntimeContext; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; +import com.baidu.hugegraph.util.Log; public class SofaRpcServer { - private final Map providerConfigs; + private static final Logger LOG = Log.logger(SofaRpcServer.class); + + private final HugeConfig conf; + private final RpcProviderConfig configs; private final ServerConfig serverConfig; - private final int rpcServerTimeout; static { if (RpcConfigs.getOrDefaultValue(RpcOptions.JVM_SHUTDOWN_HOOK, true)) { @@ -48,34 +52,44 @@ public void run() { } } - public SofaRpcServer(HugeConfig conf, RpcProviderConfig providerConfig) { + public SofaRpcServer(HugeConfig conf) { + RpcCommonConfig.initRpcConfigs(conf); + this.conf = conf; this.serverConfig = new ServerConfig() .setProtocol(conf.get(ServerOptions.RPC_PROTOCOL)) .setPort(conf.get(ServerOptions.RPC_SERVER_PORT)) .setHost(conf.get(ServerOptions.RPC_SERVER_HOST)) .setDaemon(false); - this.providerConfigs = providerConfig.providerConfigs(); - this.rpcServerTimeout = conf.get(ServerOptions.RPC_SERVER_TIMEOUT) * 1000; + this.configs = new RpcProviderConfig(); + } + + public RpcProviderConfig config() { + return this.configs; } public void exportAll() { - if (MapUtils.isEmpty(this.providerConfigs)) { - throw new RpcException( - "The server provider config map can't be empty"); + LOG.debug("RpcServer starting on port {}", this.port()); + Map configs = this.configs.configs(); + if (MapUtils.isEmpty(configs)) { + LOG.info("RpcServer configs is empty, skip RpcServer starting"); + return; } - for (ProviderConfig providerConfig : this.providerConfigs.values()) { + int timeout = this.conf.get(ServerOptions.RPC_SERVER_TIMEOUT) * 1000; + for (ProviderConfig providerConfig : configs.values()) { providerConfig.setServer(this.serverConfig); - providerConfig.setTimeout(this.rpcServerTimeout); + providerConfig.setTimeout(timeout); providerConfig.export(); } + LOG.info("RpcServer started success on port {}", this.port()); } public void unExport(String serviceName) { - if (!this.providerConfigs.containsKey(serviceName)) { - throw new RpcException("The service name '%s' doesn't exist, please " + - "change others", serviceName); + Map configs = this.configs.configs(); + if (!configs.containsKey(serviceName)) { + throw new RpcException("The service name '%s' doesn't exist, " + + "please change others", serviceName); } - this.providerConfigs.get(serviceName).unExport(); + configs.get(serviceName).unExport(); } public int port() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index f1fc85def0..82ea83a729 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -155,7 +155,7 @@ public interface HugeGraph extends Graph { public HugeFeatures features(); public UserManager userManager(); - public void swichUserManager(UserManager userManager); + public void switchUserManager(UserManager userManager); public TaskScheduler taskScheduler(); public RaftGroupManager raftGroupManager(String group); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java index 8d2cc9f9d6..38a8d57eb7 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java @@ -884,7 +884,7 @@ public UserManager userManager() { } @Override - public void swichUserManager(UserManager userManager) { + public void switchUserManager(UserManager userManager) { this.userManager = userManager; } From e2a3bad978934327c27001fc166982f38ba2c76d Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 10 Feb 2021 16:31:10 +0800 Subject: [PATCH 20/20] Optimize and update some code --- .../com/baidu/hugegraph/rpc/SofaRpcServer.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java index 1c8355e916..a92401b518 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/rpc/SofaRpcServer.java @@ -43,11 +43,8 @@ public class SofaRpcServer { static { if (RpcConfigs.getOrDefaultValue(RpcOptions.JVM_SHUTDOWN_HOOK, true)) { - Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { - @Override - public void run() { - RpcRuntimeContext.destroy(); - } + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + RpcRuntimeContext.destroy(); }, "SOFA-RPC-ShutdownHook")); } } @@ -71,7 +68,7 @@ public void exportAll() { LOG.debug("RpcServer starting on port {}", this.port()); Map configs = this.configs.configs(); if (MapUtils.isEmpty(configs)) { - LOG.info("RpcServer configs is empty, skip RpcServer starting"); + LOG.info("RpcServer config is empty, skip starting RpcServer"); return; } int timeout = this.conf.get(ServerOptions.RPC_SERVER_TIMEOUT) * 1000; @@ -86,8 +83,8 @@ public void exportAll() { public void unExport(String serviceName) { Map configs = this.configs.configs(); if (!configs.containsKey(serviceName)) { - throw new RpcException("The service name '%s' doesn't exist, " + - "please change others", serviceName); + throw new RpcException("The service name '%s' doesn't exist", + serviceName); } configs.get(serviceName).unExport(); } @@ -97,6 +94,7 @@ public int port() { } public void destroy() { + LOG.info("RpcServer stop on port {}", this.port()); this.serverConfig.destroy(); } }