Skip to content

Commit

Permalink
chore: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxxoo committed Mar 5, 2023
1 parent c5db1cf commit 68e186e
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.election.GlobalMasterInfo;
import org.apache.hugegraph.masterelection.GlobalMasterInfo;
import org.apache.hugegraph.util.E;
import org.apache.hugegraph.util.Log;
import org.glassfish.hk2.api.IterableProvider;
Expand All @@ -53,7 +53,7 @@ public class RedirectFilter implements ContainerRequestFilter {

private static final Logger LOG = Log.logger(RedirectFilter.class);

public static final String X_HG_REDIRECT = "x-hg-redirect";
private static final String X_HG_REDIRECT = "x-hg-redirect";

private static volatile Client client = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import javax.security.sasl.AuthenticationException;

import org.apache.commons.configuration2.Configuration;
import org.apache.hugegraph.election.RoleElectionStateMachine;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.tinkerpop.gremlin.process.computer.GraphComputer;
import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
import org.apache.tinkerpop.gremlin.process.traversal.Bytecode.Instruction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void setup(HugeConfig config) {
String raftGroupPeers = config.get(ServerOptions.RAFT_GROUP_PEERS);
graphConfig.addProperty(ServerOptions.RAFT_GROUP_PEERS.name(),
raftGroupPeers);
this.addRoleWorkerConfig(graphConfig, config);
this.transferRoleWorkerConfig(graphConfig, config);

this.graph = (HugeGraph) GraphFactory.open(graphConfig);

Expand All @@ -138,19 +138,19 @@ public void setup(HugeConfig config) {
}
}

private void addRoleWorkerConfig(HugeConfig graphConfig, HugeConfig config) {
private void transferRoleWorkerConfig(HugeConfig graphConfig, HugeConfig config) {
graphConfig.addProperty(CoreOptions.NODE_EXTERNAL_URL.name(),
config.get(ServerOptions.REST_SERVER_URL));
config.get(ServerOptions.REST_SERVER_URL));
graphConfig.addProperty(CoreOptions.BASE_TIMEOUT_MILLISECOND.name(),
config.get(CoreOptions.BASE_TIMEOUT_MILLISECOND));
config.get(CoreOptions.BASE_TIMEOUT_MILLISECOND));
graphConfig.addProperty(CoreOptions.EXCEEDS_FAIL_COUNT.name(),
config.get(CoreOptions.EXCEEDS_FAIL_COUNT));
config.get(CoreOptions.EXCEEDS_FAIL_COUNT));
graphConfig.addProperty(CoreOptions.RANDOM_TIMEOUT_MILLISECOND.name(),
config.get(CoreOptions.RANDOM_TIMEOUT_MILLISECOND));
config.get(CoreOptions.RANDOM_TIMEOUT_MILLISECOND));
graphConfig.addProperty(CoreOptions.HEARTBEAT_INTERVAL_SECOUND.name(),
config.get(CoreOptions.HEARTBEAT_INTERVAL_SECOUND));
config.get(CoreOptions.HEARTBEAT_INTERVAL_SECOUND));
graphConfig.addProperty(CoreOptions.EXCEEDS_WORKER_COUNT.name(),
config.get(CoreOptions.EXCEEDS_WORKER_COUNT));
config.get(CoreOptions.EXCEEDS_WORKER_COUNT));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.apache.hugegraph.auth.StandardAuthenticator;
import org.apache.hugegraph.election.GlobalMasterInfo;
import org.apache.hugegraph.election.StandardStateMachineCallback;
import org.apache.hugegraph.election.RoleElectionStateMachine;
import org.apache.hugegraph.masterelection.GlobalMasterInfo;
import org.apache.hugegraph.masterelection.StandardStateMachineCallback;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.tinkerpop.gremlin.server.auth.AuthenticationException;
import org.apache.tinkerpop.gremlin.server.util.MetricManager;
import org.apache.tinkerpop.gremlin.structure.Graph;
Expand Down Expand Up @@ -371,7 +371,7 @@ private void loadGraph(String name, String graphConfPath) {
String raftGroupPeers = this.conf.get(ServerOptions.RAFT_GROUP_PEERS);
config.addProperty(ServerOptions.RAFT_GROUP_PEERS.name(),
raftGroupPeers);
this.addRoleWorkerConfig(config);
this.transferRoleWorkerConfig(config);

Graph graph = GraphFactory.open(config);
this.graphs.put(name, graph);
Expand All @@ -389,7 +389,7 @@ private void loadGraph(String name, String graphConfPath) {
}
}

private void addRoleWorkerConfig(HugeConfig config) {
private void transferRoleWorkerConfig(HugeConfig config) {
config.addProperty(CoreOptions.NODE_EXTERNAL_URL.name(),
this.conf.get(ServerOptions.REST_SERVER_URL));
config.addProperty(CoreOptions.BASE_TIMEOUT_MILLISECOND.name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.hugegraph.backend.store.BackendFeatures;
import org.apache.hugegraph.backend.store.BackendStoreInfo;
import org.apache.hugegraph.backend.store.raft.RaftGroupManager;
import org.apache.hugegraph.election.RoleElectionStateMachine;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.hugegraph.rpc.RpcServiceConfig4Client;
import org.apache.hugegraph.rpc.RpcServiceConfig4Server;
import org.apache.hugegraph.task.TaskScheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
import org.apache.hugegraph.backend.tx.GraphTransaction;
import org.apache.hugegraph.backend.tx.SchemaTransaction;
import org.apache.hugegraph.config.CoreOptions;
import org.apache.hugegraph.election.ClusterRoleStore;
import org.apache.hugegraph.election.Config;
import org.apache.hugegraph.election.HugeRoleStateMachineConfig;
import org.apache.hugegraph.election.RoleElectionStateMachine;
import org.apache.hugegraph.election.StandardRoleElectionStateMachine;
import org.apache.hugegraph.election.StandardClusterRoleStore;
import org.apache.hugegraph.masterelection.ClusterRoleStore;
import org.apache.hugegraph.masterelection.Config;
import org.apache.hugegraph.masterelection.HugeRoleStateMachineConfig;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.hugegraph.masterelection.StandardRoleElectionStateMachine;
import org.apache.hugegraph.masterelection.StandardClusterRoleStore;
import org.apache.hugegraph.io.HugeGraphIoRegistry;
import org.apache.hugegraph.rpc.RpcServiceConfig4Client;
import org.apache.hugegraph.rpc.RpcServiceConfig4Server;
Expand Down Expand Up @@ -294,8 +294,8 @@ private void initRoleStateWorker(Id serverId) {
this.configuration.get(CoreOptions.HEARTBEAT_INTERVAL_SECOUND),
this.configuration.get(CoreOptions.EXCEEDS_WORKER_COUNT),
this.configuration.get(CoreOptions.BASE_TIMEOUT_MILLISECOND));
ClusterRoleStore adapter = new StandardClusterRoleStore(this.params);
this.roleElectionStateMachine = new StandardRoleElectionStateMachine(roleStateMachineConfig, adapter);
ClusterRoleStore clusterRoleStore = new StandardClusterRoleStore(this.params);
this.roleElectionStateMachine = new StandardRoleElectionStateMachine(roleStateMachineConfig, clusterRoleStore);
}

@Override
Expand Down Expand Up @@ -1090,7 +1090,6 @@ public String toString() {
return StringFactory.graphString(this, this.name());
}


@Override
public final void proxy(HugeGraph graph) {
this.params.graph(graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

import java.util.Objects;

Expand All @@ -24,7 +24,6 @@ public class ClusterRole {
private String node;
private long clock;
private int epoch;

private String url;

public ClusterRole(String node, String url, int epoch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

public interface Config {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

public class GlobalMasterInfo {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

public class HugeRoleStateMachineConfig implements Config {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

public interface RoleElectionStateMachine {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -43,7 +43,7 @@
public class StandardClusterRoleStore implements ClusterRoleStore {

private static final Logger LOG = Log.logger(StandardClusterRoleStore.class);
public static final int RETRY_QUERY_TIMEOUT = 200;
private static final int RETRY_QUERY_TIMEOUT = 200;

private final HugeGraphParams graph;
private final Schema schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

import java.security.SecureRandom;
import java.util.Optional;
Expand All @@ -27,16 +27,16 @@

public class StandardRoleElectionStateMachine implements RoleElectionStateMachine {

private static final Logger LOG = Log.logger(StandardRoleElectionStateMachine.class);

private volatile boolean shutdown;
private final Config config;
private volatile RoleState state;
private final ClusterRoleStore clusterRoleStore;

private static final Logger LOG = Log.logger(StandardRoleElectionStateMachine.class);

public StandardRoleElectionStateMachine(Config config, ClusterRoleStore adapter) {
public StandardRoleElectionStateMachine(Config config, ClusterRoleStore clusterRoleStore) {
this.config = config;
this.clusterRoleStore = adapter;
this.clusterRoleStore = clusterRoleStore;
this.state = new UnknownState(null);
this.shutdown = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

import org.apache.hugegraph.task.TaskManager;
import org.apache.hugegraph.util.Log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

public interface StateMachineCallback {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* under the License.
*/

package org.apache.hugegraph.election;
package org.apache.hugegraph.masterelection;

public interface StateMachineContext {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;

import org.apache.hugegraph.election.ClusterRoleStore;
import org.apache.hugegraph.election.Config;
import org.apache.hugegraph.election.RoleElectionStateMachine;
import org.apache.hugegraph.election.StandardRoleElectionStateMachine;
import org.apache.hugegraph.election.ClusterRole;
import org.apache.hugegraph.election.StateMachineCallback;
import org.apache.hugegraph.election.StateMachineContext;
import org.apache.hugegraph.masterelection.ClusterRoleStore;
import org.apache.hugegraph.masterelection.Config;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.hugegraph.masterelection.StandardRoleElectionStateMachine;
import org.apache.hugegraph.masterelection.ClusterRole;
import org.apache.hugegraph.masterelection.StateMachineCallback;
import org.apache.hugegraph.masterelection.StateMachineContext;
import org.apache.hugegraph.testutil.Assert;
import org.apache.hugegraph.testutil.Utils;
import org.junit.Test;
Expand Down Expand Up @@ -206,7 +206,7 @@ public void error(StateMachineContext context, Throwable e) {
};

final List<ClusterRole> clusterRoleLogs = Collections.synchronizedList(new ArrayList<>(100));
final ClusterRoleStore adapter = new ClusterRoleStore() {
final ClusterRoleStore clusterRoleStore = new ClusterRoleStore() {

volatile int epoch = 0;

Expand Down Expand Up @@ -263,7 +263,7 @@ public Optional<ClusterRole> query() {
Thread node1 = new Thread(() -> {
Config config = new TestConfig("1");
RoleElectionStateMachine stateMachine =
new StandardRoleElectionStateMachine(config, adapter);
new StandardRoleElectionStateMachine(config, clusterRoleStore);
machines[1] = stateMachine;
stateMachine.apply(callback);
stop.countDown();
Expand All @@ -272,7 +272,7 @@ public Optional<ClusterRole> query() {
Thread node2 = new Thread(() -> {
Config config = new TestConfig("2");
RoleElectionStateMachine stateMachine =
new StandardRoleElectionStateMachine(config, adapter);
new StandardRoleElectionStateMachine(config, clusterRoleStore);
machines[2] = stateMachine;
stateMachine.apply(callback);
stop.countDown();
Expand All @@ -281,7 +281,7 @@ public Optional<ClusterRole> query() {
Thread node3 = new Thread(() -> {
Config config = new TestConfig("3");
RoleElectionStateMachine stateMachine =
new StandardRoleElectionStateMachine(config, adapter);
new StandardRoleElectionStateMachine(config, clusterRoleStore);
machines[3] = stateMachine;
stateMachine.apply(callback);
stop.countDown();
Expand Down

0 comments on commit 68e186e

Please sign in to comment.