From 5523064b96c80a9cce735e39b85767adbc664bbc Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 7 Mar 2023 19:43:48 +0800 Subject: [PATCH] optimize all imports & tiny improve --- .../hugegraph/api/filter/RedirectFilter.java | 28 +++---- .../filter/RedirectFilterDynamicFeature.java | 3 +- .../hugegraph/api/job/AlgorithmAPI.java | 10 +-- .../apache/hugegraph/api/job/ComputerAPI.java | 28 +++---- .../apache/hugegraph/api/job/GremlinAPI.java | 32 ++++---- .../apache/hugegraph/api/job/RebuildAPI.java | 24 +++--- .../org/apache/hugegraph/api/job/TaskAPI.java | 34 ++++----- .../hugegraph/api/schema/EdgeLabelAPI.java | 55 +++++++------- .../hugegraph/api/schema/IndexLabelAPI.java | 41 +++++----- .../hugegraph/api/schema/PropertyKeyAPI.java | 44 +++++------ .../hugegraph/api/schema/SchemaAPI.java | 18 ++--- .../hugegraph/api/schema/VertexLabelAPI.java | 49 ++++++------ .../hugegraph/auth/ConfigAuthenticator.java | 22 ++---- .../hugegraph/auth/HugeAuthenticator.java | 13 ++-- .../hugegraph/auth/HugeFactoryAuthProxy.java | 14 ++-- .../hugegraph/auth/HugeGraphAuthProxy.java | 54 +++++++------- .../hugegraph/auth/StandardAuthenticator.java | 13 ++-- .../apache/hugegraph/core/GraphManager.java | 66 +++++++++-------- .../hugegraph/server/ApplicationConfig.java | 41 +++++----- .../java/org/apache/hugegraph/HugeGraph.java | 33 ++++----- .../apache/hugegraph/StandardHugeGraph.java | 74 ++++++++++--------- .../hugegraph/masterelection/ClusterRole.java | 6 +- .../masterelection/RoleElectionConfig.java | 14 ++-- .../masterelection/RoleElectionOptions.java | 8 +- .../StandardClusterRoleStore.java | 33 +++++---- .../StandardRoleElectionStateMachine.java | 10 +-- .../StandardStateMachineCallback.java | 4 +- .../masterelection/StateMachineContext.java | 1 + .../hugegraph/task/ServerInfoManager.java | 27 ++++--- .../hugegraph/task/StandardTaskScheduler.java | 32 ++++---- .../apache/hugegraph/dist/RegisterUtil.java | 32 ++++---- .../core/RoleElectionStateMachineTest.java | 15 ++-- 32 files changed, 431 insertions(+), 447 deletions(-) diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java index 66a9f43c41..23e5f0735c 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java @@ -28,16 +28,6 @@ import java.util.Map; import java.util.Set; -import jakarta.ws.rs.NameBinding; -import jakarta.ws.rs.client.Client; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Entity; -import jakarta.ws.rs.client.Invocation; -import jakarta.ws.rs.container.ContainerRequestContext; -import jakarta.ws.rs.container.ContainerRequestFilter; -import jakarta.ws.rs.core.Context; -import jakarta.ws.rs.core.MultivaluedMap; -import jakarta.ws.rs.core.Response; import org.apache.commons.lang3.StringUtils; import org.apache.http.client.utils.URIBuilder; import org.apache.hugegraph.core.GraphManager; @@ -49,6 +39,17 @@ import org.glassfish.jersey.message.internal.HeaderUtils; import org.slf4j.Logger; +import jakarta.ws.rs.NameBinding; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.container.ContainerRequestContext; +import jakarta.ws.rs.container.ContainerRequestFilter; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.MultivaluedMap; +import jakarta.ws.rs.core.Response; + public class RedirectFilter implements ContainerRequestFilter { private static final Logger LOG = Log.logger(RedirectFilter.class); @@ -85,7 +86,7 @@ public void filter(ContainerRequestContext context) throws IOException { return; } - String url = ""; + String url; synchronized (globalMasterInfo) { if (globalMasterInfo.isMaster() || StringUtils.isEmpty(globalMasterInfo.url())) { return; @@ -93,7 +94,7 @@ public void filter(ContainerRequestContext context) throws IOException { url = globalMasterInfo.url(); } - URI redirectUri = null; + URI redirectUri; try { URIBuilder redirectURIBuilder = new URIBuilder(context.getUriInfo().getRequestUri()); URI masterURI = URI.create(url); @@ -125,7 +126,7 @@ private Response forwardRequest(ContainerRequestContext requestContext, URI redi Invocation.Builder builder = client.target(redirectUri) .request() .headers(newHeaders); - Response response = null; + Response response; if (MUST_BE_NULL.contains(requestContext.getMethod())) { response = builder.method(requestContext.getMethod()); } else { @@ -144,7 +145,6 @@ private void initClientIfNeeded() { if (client != null) { return; } - client = ClientBuilder.newClient(); } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilterDynamicFeature.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilterDynamicFeature.java index 0b62693583..547661d5da 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilterDynamicFeature.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilterDynamicFeature.java @@ -27,7 +27,8 @@ public class RedirectFilterDynamicFeature implements DynamicFeature { @Override public void configure(ResourceInfo resourceInfo, FeatureContext context) { - if (resourceInfo.getResourceMethod().isAnnotationPresent(RedirectFilter.RedirectMasterRole.class)) { + if (resourceInfo.getResourceMethod() + .isAnnotationPresent(RedirectFilter.RedirectMasterRole.class)) { context.register(RedirectFilter.class); } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/AlgorithmAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/AlgorithmAPI.java index ebb172a9e9..8e0e7d10c3 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/AlgorithmAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/AlgorithmAPI.java @@ -19,20 +19,20 @@ import java.util.Map; -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.core.GraphManager; -import org.apache.hugegraph.server.RestServer; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.job.AlgorithmJob; import org.apache.hugegraph.job.JobBuilder; +import org.apache.hugegraph.server.RestServer; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.JsonUtil; import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + import com.codahale.metrics.annotation.Timed; import com.google.common.collect.ImmutableMap; diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/ComputerAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/ComputerAPI.java index 88b229bdf4..351eec303b 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/ComputerAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/ComputerAPI.java @@ -19,33 +19,33 @@ import java.util.Map; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.inject.Singleton; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.NotFoundException; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.PathParam; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.core.Context; - -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.core.GraphManager; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.job.ComputerJob; import org.apache.hugegraph.job.JobBuilder; import org.apache.hugegraph.task.HugeTask; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.JsonUtil; import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + import com.codahale.metrics.annotation.Timed; import com.google.common.collect.ImmutableMap; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.inject.Singleton; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.NotFoundException; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.Context; + @Path("graphs/{graph}/jobs/computer") @Singleton @Tag(name = "ComputerAPI") diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java index aacfc2bb93..8e45cbf11e 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java @@ -25,37 +25,37 @@ import java.util.HashMap; import java.util.Map; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.security.RolesAllowed; -import jakarta.inject.Singleton; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.PathParam; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.core.Context; - -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.core.GraphManager; -import org.apache.hugegraph.define.Checkable; -import org.apache.hugegraph.metrics.MetricsUtil; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeException; import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.define.Checkable; import org.apache.hugegraph.job.GremlinJob; import org.apache.hugegraph.job.JobBuilder; +import org.apache.hugegraph.metrics.MetricsUtil; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.JsonUtil; import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + import com.codahale.metrics.Histogram; import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableMap; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.security.RolesAllowed; +import jakarta.inject.Singleton; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.Context; + @Path("graphs/{graph}/jobs/gremlin") @Singleton @Tag(name = "GremlinAPI") diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java index 7d00d63f09..f3a94e76bf 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java @@ -19,6 +19,18 @@ import java.util.Map; +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; +import org.apache.hugegraph.api.filter.StatusFilter.Status; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + +import com.codahale.metrics.annotation.Timed; +import com.google.common.collect.ImmutableMap; + import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.security.RolesAllowed; import jakarta.inject.Singleton; @@ -28,18 +40,6 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.Context; -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.api.filter.StatusFilter.Status; -import org.apache.hugegraph.core.GraphManager; -import org.slf4j.Logger; - -import org.apache.hugegraph.HugeGraph; -import org.apache.hugegraph.api.API; -import org.apache.hugegraph.backend.id.Id; -import org.apache.hugegraph.util.Log; -import com.codahale.metrics.annotation.Timed; -import com.google.common.collect.ImmutableMap; - @Path("graphs/{graph}/jobs/rebuild") @Singleton @Tag(name = "RebuildAPI") diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/TaskAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/TaskAPI.java index 30f8d3bdd3..c2dedf32a6 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/TaskAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/TaskAPI.java @@ -24,6 +24,23 @@ import java.util.Map; import java.util.stream.Collectors; +import org.apache.groovy.util.Maps; +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; +import org.apache.hugegraph.api.filter.StatusFilter.Status; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.id.IdGenerator; +import org.apache.hugegraph.backend.page.PageInfo; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.task.HugeTask; +import org.apache.hugegraph.task.TaskScheduler; +import org.apache.hugegraph.task.TaskStatus; +import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + +import com.codahale.metrics.annotation.Timed; + import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.inject.Singleton; import jakarta.ws.rs.BadRequestException; @@ -38,23 +55,6 @@ import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; -import org.apache.groovy.util.Maps; -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.api.filter.StatusFilter.Status; -import org.apache.hugegraph.core.GraphManager; -import org.slf4j.Logger; - -import org.apache.hugegraph.api.API; -import org.apache.hugegraph.backend.id.Id; -import org.apache.hugegraph.backend.id.IdGenerator; -import org.apache.hugegraph.backend.page.PageInfo; -import org.apache.hugegraph.task.HugeTask; -import org.apache.hugegraph.task.TaskScheduler; -import org.apache.hugegraph.task.TaskStatus; -import org.apache.hugegraph.util.E; -import org.apache.hugegraph.util.Log; -import com.codahale.metrics.annotation.Timed; - @Path("graphs/{graph}/tasks") @Singleton @Tag(name = "TaskAPI") diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java index 5ec2785e2a..ebf9a49a8a 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/EdgeLabelAPI.java @@ -18,44 +18,45 @@ package org.apache.hugegraph.api.schema; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.security.RolesAllowed; -import jakarta.inject.Singleton; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.DELETE; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.PathParam; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.QueryParam; -import jakarta.ws.rs.core.Context; - import org.apache.commons.collections.CollectionUtils; -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.core.GraphManager; -import org.apache.hugegraph.define.Checkable; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.define.Checkable; import org.apache.hugegraph.schema.EdgeLabel; import org.apache.hugegraph.schema.Userdata; import org.apache.hugegraph.type.define.Frequency; import org.apache.hugegraph.type.define.GraphMode; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableMap; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.security.RolesAllowed; +import jakarta.inject.Singleton; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.core.Context; + @Path("graphs/{graph}/schema/edgelabels") @Singleton @Tag(name = "EdgeLabelAPI") @@ -207,8 +208,7 @@ private static class JsonEdgeLabel implements Checkable { @Override public void checkCreate(boolean isBatch) { - E.checkArgumentNotNull(this.name, - "The name of edge label can't be null"); + E.checkArgumentNotNull(this.name, "The name of edge label can't be null"); } private EdgeLabel.Builder convert2Builder(HugeGraph g) { @@ -265,12 +265,13 @@ private EdgeLabel.Builder convert2Builder(HugeGraph g) { @Override public String toString() { return String.format("JsonEdgeLabel{" + - "name=%s, sourceLabel=%s, targetLabel=%s, frequency=%s, " + - "sortKeys=%s, nullableKeys=%s, properties=%s, ttl=%s, " + - "ttlStartTime=%s}", - this.name, this.sourceLabel, this.targetLabel, - this.frequency, this.sortKeys, this.nullableKeys, - this.properties, this.ttl, this.ttlStartTime); + "name=%s, sourceLabel=%s, targetLabel=%s, frequency=%s, " + + "sortKeys=%s, nullableKeys=%s, properties=%s, ttl=%s, " + + "ttlStartTime=%s}", + this.name, this.sourceLabel, this.targetLabel, + this.frequency, Arrays.toString(this.sortKeys), + Arrays.toString(this.nullableKeys), + Arrays.toString(this.properties), this.ttl, this.ttlStartTime); } } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java index 16be06e513..aa192fc1a6 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/IndexLabelAPI.java @@ -18,33 +18,18 @@ package org.apache.hugegraph.api.schema; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.security.RolesAllowed; -import jakarta.inject.Singleton; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.DELETE; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.PathParam; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.QueryParam; -import jakarta.ws.rs.core.Context; - import org.apache.commons.collections.CollectionUtils; -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.core.GraphManager; -import org.apache.hugegraph.define.Checkable; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.define.Checkable; import org.apache.hugegraph.schema.IndexLabel; import org.apache.hugegraph.schema.SchemaElement; import org.apache.hugegraph.schema.Userdata; @@ -53,11 +38,27 @@ import org.apache.hugegraph.type.define.IndexType; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableMap; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.security.RolesAllowed; +import jakarta.inject.Singleton; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.core.Context; + @Path("graphs/{graph}/schema/indexlabels") @Singleton @Tag(name = "IndexLabelAPI") @@ -290,7 +291,7 @@ public String toString() { return String.format("JsonIndexLabel{name=%s, baseType=%s," + "baseValue=%s, indexType=%s, fields=%s}", this.name, this.baseType, this.baseValue, - this.indexType, this.fields); + this.indexType, Arrays.toString(this.fields)); } } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java index 893dcd9c69..8bfbcc599a 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/PropertyKeyAPI.java @@ -18,34 +18,19 @@ package org.apache.hugegraph.api.schema; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.security.RolesAllowed; -import jakarta.inject.Singleton; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.DELETE; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.PathParam; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.QueryParam; -import jakarta.ws.rs.core.Context; - import org.apache.commons.collections.CollectionUtils; -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.core.GraphManager; -import org.apache.hugegraph.define.Checkable; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.backend.id.Id; import org.apache.hugegraph.backend.id.IdGenerator; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.define.Checkable; import org.apache.hugegraph.schema.PropertyKey; import org.apache.hugegraph.schema.SchemaElement; import org.apache.hugegraph.schema.Userdata; @@ -56,11 +41,27 @@ import org.apache.hugegraph.type.define.WriteType; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableMap; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.security.RolesAllowed; +import jakarta.inject.Singleton; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.core.Context; + @Path("graphs/{graph}/schema/propertykeys") @Singleton @Tag(name = "PropertyKeyAPI") @@ -78,8 +79,7 @@ public class PropertyKeyAPI extends API { public String create(@Context GraphManager manager, @PathParam("graph") String graph, JsonPropertyKey jsonPropertyKey) { - LOG.debug("Graph [{}] create property key: {}", - graph, jsonPropertyKey); + LOG.debug("Graph [{}] create property key: {}", graph, jsonPropertyKey); checkCreatingBody(jsonPropertyKey); HugeGraph g = graph(manager, graph); @@ -270,7 +270,7 @@ public String toString() { "writeType=%s, properties=%s}", this.name, this.cardinality, this.dataType, this.aggregateType, - this.writeType, this.properties); + this.writeType, Arrays.toString(this.properties)); } } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java index 7ebb32b5b6..203ed2cbba 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/SchemaAPI.java @@ -21,6 +21,15 @@ import java.util.List; import java.util.Map; +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.api.API; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.schema.SchemaManager; +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + +import com.codahale.metrics.annotation.Timed; + import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.security.RolesAllowed; import jakarta.inject.Singleton; @@ -30,15 +39,6 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.Context; -import org.apache.hugegraph.core.GraphManager; -import org.slf4j.Logger; - -import org.apache.hugegraph.HugeGraph; -import org.apache.hugegraph.api.API; -import org.apache.hugegraph.schema.SchemaManager; -import org.apache.hugegraph.util.Log; -import com.codahale.metrics.annotation.Timed; - @Path("graphs/{graph}/schema") @Singleton @Tag(name = "SchemaAPI") diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java index 0ad5a5d8e3..1542903523 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/schema/VertexLabelAPI.java @@ -18,44 +18,45 @@ package org.apache.hugegraph.api.schema; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.security.RolesAllowed; -import jakarta.inject.Singleton; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.DELETE; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.POST; -import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; -import jakarta.ws.rs.PathParam; -import jakarta.ws.rs.Produces; -import jakarta.ws.rs.QueryParam; -import jakarta.ws.rs.core.Context; - import org.apache.commons.collections.CollectionUtils; -import org.apache.hugegraph.api.filter.RedirectFilter; -import org.apache.hugegraph.core.GraphManager; -import org.apache.hugegraph.define.Checkable; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.api.API; +import org.apache.hugegraph.api.filter.RedirectFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.define.Checkable; import org.apache.hugegraph.schema.Userdata; import org.apache.hugegraph.schema.VertexLabel; import org.apache.hugegraph.type.define.GraphMode; import org.apache.hugegraph.type.define.IdStrategy; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + import com.codahale.metrics.annotation.Timed; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableMap; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.security.RolesAllowed; +import jakarta.inject.Singleton; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.core.Context; + @Path("graphs/{graph}/schema/vertexlabels") @Singleton @Tag(name = "VertexLabelAPI") @@ -258,11 +259,11 @@ private VertexLabel.Builder convert2Builder(HugeGraph g) { @Override public String toString() { return String.format("JsonVertexLabel{" + - "name=%s, idStrategy=%s, primaryKeys=%s, nullableKeys=%s, " + - "properties=%s, ttl=%s, ttlStartTime=%s}", - this.name, this.idStrategy, this.primaryKeys, - this.nullableKeys, this.properties, this.ttl, - this.ttlStartTime); + "name=%s, idStrategy=%s, primaryKeys=%s, nullableKeys=%s, " + + "properties=%s, ttl=%s, ttlStartTime=%s}", + this.name, this.idStrategy, Arrays.toString(this.primaryKeys), + Arrays.toString(this.nullableKeys), + Arrays.toString(this.properties), this.ttl, this.ttlStartTime); } } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/ConfigAuthenticator.java b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/ConfigAuthenticator.java index c063fc0764..95d048ede5 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/ConfigAuthenticator.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/ConfigAuthenticator.java @@ -24,19 +24,16 @@ import org.apache.commons.lang.NotImplementedException; import org.apache.hugegraph.HugeGraph; -import org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraphTokens; - import org.apache.hugegraph.backend.id.IdGenerator; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.config.ServerOptions; import org.apache.hugegraph.util.E; +import org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraphTokens; public class ConfigAuthenticator implements HugeAuthenticator { - public static final String KEY_USERNAME = - CredentialGraphTokens.PROPERTY_USERNAME; - public static final String KEY_PASSWORD = - CredentialGraphTokens.PROPERTY_PASSWORD; + public static final String KEY_USERNAME = CredentialGraphTokens.PROPERTY_USERNAME; + public static final String KEY_PASSWORD = CredentialGraphTokens.PROPERTY_PASSWORD; private final Map tokens; @@ -72,7 +69,7 @@ public UserWithRole authenticate(final String username, if (username.equals(USER_ADMIN)) { role = ROLE_ADMIN; } else { - // Return role with all permission, set user name as owner graph + // Return role with all permission, set username as owner graph role = RolePermission.all(username); } } else { @@ -84,18 +81,16 @@ public UserWithRole authenticate(final String username, @Override public AuthManager authManager() { - throw new NotImplementedException( - "AuthManager is unsupported by ConfigAuthenticator"); + throw new NotImplementedException("AuthManager is unsupported by ConfigAuthenticator"); } @Override public HugeGraph graph() { - throw new NotImplementedException( - "graph() is unsupported by ConfigAuthenticator"); + throw new NotImplementedException("graph() is unsupported by ConfigAuthenticator"); } @Override - public void initAdminUser(String password) throws Exception { + public void initAdminUser(String password) { String adminToken = this.tokens.get(USER_ADMIN); E.checkArgument(Objects.equals(adminToken, password), "The password can't be changed for " + @@ -104,7 +99,6 @@ public void initAdminUser(String password) throws Exception { @Override public SaslNegotiator newSaslNegotiator(InetAddress remoteAddress) { - throw new NotImplementedException( - "SaslNegotiator is unsupported by ConfigAuthenticator"); + throw new NotImplementedException("SaslNegotiator is unsupported by ConfigAuthenticator"); } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeAuthenticator.java b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeAuthenticator.java index 186f2605cd..b22229e9bc 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeAuthenticator.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeAuthenticator.java @@ -21,14 +21,8 @@ import java.util.List; import java.util.Map; -import org.apache.hugegraph.HugeGraph; -import org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraphTokens; -import org.apache.tinkerpop.gremlin.server.auth.AuthenticatedUser; -import org.apache.tinkerpop.gremlin.server.auth.AuthenticationException; -import org.apache.tinkerpop.gremlin.server.auth.Authenticator; -import org.apache.tinkerpop.shaded.jackson.annotation.JsonProperty; - import org.apache.hugegraph.HugeException; +import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.auth.HugeGraphAuthProxy.Context; import org.apache.hugegraph.auth.SchemaDefine.AuthElement; import org.apache.hugegraph.backend.id.Id; @@ -39,6 +33,11 @@ import org.apache.hugegraph.type.Nameable; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.JsonUtil; +import org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraphTokens; +import org.apache.tinkerpop.gremlin.server.auth.AuthenticatedUser; +import org.apache.tinkerpop.gremlin.server.auth.AuthenticationException; +import org.apache.tinkerpop.gremlin.server.auth.Authenticator; +import org.apache.tinkerpop.shaded.jackson.annotation.JsonProperty; public interface HugeAuthenticator extends Authenticator { diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeFactoryAuthProxy.java b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeFactoryAuthProxy.java index e2b9ccfcc3..e9291fe529 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeFactoryAuthProxy.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeFactoryAuthProxy.java @@ -26,24 +26,27 @@ import java.util.Map; import java.util.Set; +import org.apache.commons.configuration2.Configuration; import org.apache.hugegraph.HugeException; import org.apache.hugegraph.HugeFactory; import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.StandardHugeGraph; import org.apache.hugegraph.backend.cache.CacheManager; import org.apache.hugegraph.backend.tx.AbstractTransaction; +import org.apache.hugegraph.backend.tx.GraphTransaction; +import org.apache.hugegraph.backend.tx.IndexableTransaction; import org.apache.hugegraph.concurrent.LockManager; import org.apache.hugegraph.metrics.ServerReporter; import org.apache.hugegraph.schema.SchemaElement; import org.apache.hugegraph.schema.SchemaManager; -import org.apache.hugegraph.backend.tx.GraphTransaction; -import org.apache.hugegraph.backend.tx.IndexableTransaction; +import org.apache.hugegraph.schema.builder.AbstractBuilder; import org.apache.hugegraph.schema.builder.EdgeLabelBuilder; import org.apache.hugegraph.schema.builder.IndexLabelBuilder; import org.apache.hugegraph.schema.builder.PropertyKeyBuilder; import org.apache.hugegraph.schema.builder.VertexLabelBuilder; import org.apache.hugegraph.serializer.JsonSerializer; import org.apache.hugegraph.structure.HugeEdge; +import org.apache.hugegraph.structure.HugeElement; import org.apache.hugegraph.structure.HugeProperty; import org.apache.hugegraph.structure.HugeVertex; import org.apache.hugegraph.task.HugeTask; @@ -51,23 +54,20 @@ import org.apache.hugegraph.task.TaskCallable; import org.apache.hugegraph.task.TaskCallable.SysTaskCallable; import org.apache.hugegraph.task.TaskManager; -import org.apache.hugegraph.schema.builder.AbstractBuilder; -import org.apache.hugegraph.structure.HugeElement; import org.apache.hugegraph.traversal.optimize.HugeCountStepStrategy; import org.apache.hugegraph.traversal.optimize.HugeGraphStepStrategy; import org.apache.hugegraph.traversal.optimize.HugeVertexStepStrategy; import org.apache.hugegraph.util.Reflection; import org.apache.hugegraph.variables.HugeVariables; + import com.google.common.collect.ImmutableSet; -import org.apache.commons.configuration2.Configuration; public final class HugeFactoryAuthProxy { public static final String GRAPH_FACTORY = "gremlin.graph=org.apache.hugegraph.auth.HugeFactoryAuthProxy"; - private static final Set PROTECT_METHODS = ImmutableSet.of( - "instance"); + private static final Set PROTECT_METHODS = ImmutableSet.of("instance"); private static final Map GRAPHS = new HashMap<>(); diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java index 88b09bc7cb..04cfac30d7 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java @@ -37,27 +37,6 @@ import javax.security.sasl.AuthenticationException; import org.apache.commons.configuration2.Configuration; -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; -import org.apache.tinkerpop.gremlin.process.traversal.Script; -import org.apache.tinkerpop.gremlin.process.traversal.Traversal; -import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies; -import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy; -import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; -import org.apache.tinkerpop.gremlin.process.traversal.translator.GroovyTranslator; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Element; -import org.apache.tinkerpop.gremlin.structure.Graph; -import org.apache.tinkerpop.gremlin.structure.Property; -import org.apache.tinkerpop.gremlin.structure.Transaction; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.apache.tinkerpop.gremlin.structure.VertexProperty; -import org.apache.tinkerpop.gremlin.structure.io.Io; -import org.slf4j.Logger; - -import com.alipay.remoting.rpc.RpcServer; import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.auth.HugeAuthenticator.RolePerm; import org.apache.hugegraph.auth.HugeAuthenticator.User; @@ -76,6 +55,7 @@ import org.apache.hugegraph.exception.NotSupportException; import org.apache.hugegraph.iterator.FilterIterator; import org.apache.hugegraph.iterator.MapperIterator; +import org.apache.hugegraph.masterelection.RoleElectionStateMachine; import org.apache.hugegraph.rpc.RpcServiceConfig4Client; import org.apache.hugegraph.rpc.RpcServiceConfig4Server; import org.apache.hugegraph.schema.EdgeLabel; @@ -102,6 +82,26 @@ import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Log; import org.apache.hugegraph.util.RateLimiter; +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; +import org.apache.tinkerpop.gremlin.process.traversal.Script; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies; +import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.translator.GroovyTranslator; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Element; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Property; +import org.apache.tinkerpop.gremlin.structure.Transaction; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.VertexProperty; +import org.apache.tinkerpop.gremlin.structure.io.Io; +import org.slf4j.Logger; + +import com.alipay.remoting.rpc.RpcServer; import jakarta.ws.rs.ForbiddenException; import jakarta.ws.rs.NotAuthorizedException; @@ -1664,10 +1664,7 @@ public Iterator> iterator() { return Collections.emptyIterator(); } - return new MapperIterator, TraversalStrategy>( - this.strategies.iterator(), (strategy) -> { - return new TraversalStrategyProxy<>(strategy); - }); + return new MapperIterator<>(this.strategies.iterator(), TraversalStrategyProxy::new); } @Override @@ -1675,7 +1672,7 @@ public TraversalStrategies addStrategies(TraversalStrategy... strategies) { return this.strategies.addStrategies(strategies); } - @SuppressWarnings({ "rawtypes", "unchecked" }) + @SuppressWarnings({"unchecked"}) @Override public TraversalStrategies removeStrategies( Class... strategyClasses) { @@ -1773,8 +1770,7 @@ public Configuration getConfiguration() { } @Override - public int compareTo(@SuppressWarnings("rawtypes") - Class other) { + public int compareTo(Class other) { return this.origin.compareTo(other); } @@ -1876,7 +1872,7 @@ public static class ContextThreadPoolExecutor extends ThreadPoolExecutor { public ContextThreadPoolExecutor(int corePoolSize, int maxPoolSize, ThreadFactory threadFactory) { super(corePoolSize, maxPoolSize, 0L, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue(), threadFactory); + new LinkedBlockingQueue<>(), threadFactory); } @Override diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/StandardAuthenticator.java index ac97c26f67..244bfd8485 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/auth/StandardAuthenticator.java @@ -24,17 +24,16 @@ import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.StringUtils; -import org.apache.hugegraph.masterelection.RoleElectionOptions; -import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; - import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.config.CoreOptions; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.config.ServerOptions; +import org.apache.hugegraph.masterelection.RoleElectionOptions; import org.apache.hugegraph.rpc.RpcClientProviderWithAuth; import org.apache.hugegraph.util.ConfigUtil; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.StringEncoding; +import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; public class StandardAuthenticator implements HugeAuthenticator { @@ -57,7 +56,7 @@ public HugeGraph graph() { @Override public void initAdminUser(String password) { - // Not allowed to call by non main thread + // Not allowed to call by non-main thread String caller = Thread.currentThread().getName(); E.checkState("main".equals(caller), "Invalid caller '%s'", caller); @@ -90,7 +89,7 @@ private String inputPassword() { // CHECKSTYLE:OFF System.out.println(inputPrompt); // CHECKSTYLE:ON - @SuppressWarnings("resource") // just wrapper of System.in + // just wrapper of System.in Scanner scanner = new Scanner(System.in); password = scanner.nextLine(); } @@ -148,8 +147,8 @@ private void transferRoleWorkerConfig(HugeConfig graphConfig, HugeConfig config) config.get(RoleElectionOptions.EXCEEDS_FAIL_COUNT)); graphConfig.addProperty(RoleElectionOptions.RANDOM_TIMEOUT_MILLISECOND.name(), config.get(RoleElectionOptions.RANDOM_TIMEOUT_MILLISECOND)); - graphConfig.addProperty(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOUND.name(), - config.get(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOUND)); + graphConfig.addProperty(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOND.name(), + config.get(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOND)); graphConfig.addProperty(RoleElectionOptions.MASTER_DEAD_TIMES.name(), config.get(RoleElectionOptions.MASTER_DEAD_TIMES)); } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java index 3a21704863..b203c10470 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; @@ -30,31 +31,13 @@ import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.lang3.StringUtils; -import org.apache.hugegraph.auth.StandardAuthenticator; -import org.apache.hugegraph.masterelection.GlobalMasterInfo; -import org.apache.hugegraph.masterelection.RoleElectionOptions; -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; -import org.apache.tinkerpop.gremlin.structure.Transaction; -import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; -import org.apache.hugegraph.auth.HugeFactoryAuthProxy; -import org.apache.hugegraph.auth.HugeGraphAuthProxy; -import org.apache.hugegraph.config.ServerOptions; -import org.apache.hugegraph.metrics.MetricsUtil; -import org.apache.hugegraph.metrics.ServerReporter; -import org.apache.hugegraph.serializer.JsonSerializer; -import org.apache.hugegraph.serializer.Serializer; -import org.apache.hugegraph.server.RestServer; -import org.slf4j.Logger; - -import com.alipay.sofa.rpc.config.ServerConfig; import org.apache.hugegraph.HugeFactory; import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.auth.AuthManager; import org.apache.hugegraph.auth.HugeAuthenticator; +import org.apache.hugegraph.auth.HugeFactoryAuthProxy; +import org.apache.hugegraph.auth.HugeGraphAuthProxy; +import org.apache.hugegraph.auth.StandardAuthenticator; import org.apache.hugegraph.backend.BackendException; import org.apache.hugegraph.backend.cache.Cache; import org.apache.hugegraph.backend.cache.CacheManager; @@ -63,13 +46,23 @@ import org.apache.hugegraph.backend.store.BackendStoreInfo; import org.apache.hugegraph.config.CoreOptions; import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.config.ServerOptions; import org.apache.hugegraph.config.TypedOption; import org.apache.hugegraph.event.EventHub; import org.apache.hugegraph.exception.NotSupportException; +import org.apache.hugegraph.masterelection.GlobalMasterInfo; +import org.apache.hugegraph.masterelection.RoleElectionOptions; +import org.apache.hugegraph.masterelection.RoleElectionStateMachine; +import org.apache.hugegraph.masterelection.StandardStateMachineCallback; +import org.apache.hugegraph.metrics.MetricsUtil; +import org.apache.hugegraph.metrics.ServerReporter; import org.apache.hugegraph.rpc.RpcClientProvider; import org.apache.hugegraph.rpc.RpcConsumerConfig; import org.apache.hugegraph.rpc.RpcProviderConfig; import org.apache.hugegraph.rpc.RpcServer; +import org.apache.hugegraph.serializer.JsonSerializer; +import org.apache.hugegraph.serializer.Serializer; +import org.apache.hugegraph.server.RestServer; import org.apache.hugegraph.task.TaskManager; import org.apache.hugegraph.testutil.Whitebox; import org.apache.hugegraph.type.define.NodeRole; @@ -77,6 +70,14 @@ import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Events; import org.apache.hugegraph.util.Log; +import org.apache.tinkerpop.gremlin.server.auth.AuthenticationException; +import org.apache.tinkerpop.gremlin.server.util.MetricManager; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Transaction; +import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; +import org.slf4j.Logger; + +import com.alipay.sofa.rpc.config.ServerConfig; public final class GraphManager { @@ -91,7 +92,6 @@ public final class GraphManager { private RoleElectionStateMachine roleStateWorker; private GlobalMasterInfo globalMasterInfo; - private Executor applyThread; private Id server; private NodeRole role; @@ -378,7 +378,7 @@ private void loadGraph(String name, String graphConfPath) { this.graphs.put(name, graph); HugeConfig graphConfig = (HugeConfig) graph.configuration(); - assert graphConfPath.equals(graphConfig.file().getPath()); + assert graphConfPath.equals(Objects.requireNonNull(graphConfig.file()).getPath()); LOG.info("Graph '{}' was successfully configured via '{}'", name, graphConfPath); @@ -399,8 +399,8 @@ private void transferRoleWorkerConfig(HugeConfig config) { this.conf.get(RoleElectionOptions.EXCEEDS_FAIL_COUNT)); config.addProperty(RoleElectionOptions.RANDOM_TIMEOUT_MILLISECOND.name(), this.conf.get(RoleElectionOptions.RANDOM_TIMEOUT_MILLISECOND)); - config.addProperty(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOUND.name(), - this.conf.get(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOUND)); + config.addProperty(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOND.name(), + this.conf.get(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOND)); config.addProperty(RoleElectionOptions.MASTER_DEAD_TIMES.name(), this.conf.get(RoleElectionOptions.MASTER_DEAD_TIMES)); } @@ -423,6 +423,7 @@ private void checkBackendVersionOrExit(HugeConfig config) { for (String graph : this.graphs()) { // TODO: close tx from main thread HugeGraph hugegraph = this.graph(graph); + assert hugegraph != null; if (!hugegraph.backendStoreFeatures().supportsPersistence()) { hugegraph.initBackend(); if (this.requireAuthentication()) { @@ -477,13 +478,13 @@ private void serverStarted(HugeConfig config) { private void initRoleStateWorker() { E.checkArgument(this.roleStateWorker == null, "Repetition init"); - this.applyThread = Executors.newSingleThreadExecutor(); + Executor applyThread = Executors.newSingleThreadExecutor(); this.roleStateWorker = this.authenticator().graph().roleElectionStateMachine(); this.globalMasterInfo = new GlobalMasterInfo(); StandardStateMachineCallback stateMachineCallback = new StandardStateMachineCallback( TaskManager.instance(), this.globalMasterInfo); - this.applyThread.execute(() -> { + applyThread.execute(() -> { this.roleStateWorker.apply(stateMachineCallback); }); } @@ -627,6 +628,7 @@ private void checkOptionUnique(HugeConfig config, Object incomingValue = config.get(option); for (String graphName : this.graphs.keySet()) { HugeGraph graph = this.graph(graphName); + assert graph != null; Object existedValue = graph.option(option); E.checkArgument(!incomingValue.equals(existedValue), "The value '%s' of option '%s' conflicts with " + @@ -651,11 +653,11 @@ private static void registerCacheMetrics(Map> caches) { continue; } - MetricsUtil.registerGauge(Cache.class, hits, () -> cache.hits()); - MetricsUtil.registerGauge(Cache.class, miss, () -> cache.miss()); - MetricsUtil.registerGauge(Cache.class, exp, () -> cache.expire()); - MetricsUtil.registerGauge(Cache.class, size, () -> cache.size()); - MetricsUtil.registerGauge(Cache.class, cap, () -> cache.capacity()); + MetricsUtil.registerGauge(Cache.class, hits, cache::hits); + MetricsUtil.registerGauge(Cache.class, miss, cache::miss); + MetricsUtil.registerGauge(Cache.class, exp, cache::expire); + MetricsUtil.registerGauge(Cache.class, size, cache::size); + MetricsUtil.registerGauge(Cache.class, cap, cache::capacity); } } } diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java b/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java index 838e4b8105..d70e6e0b29 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java @@ -17,7 +17,14 @@ package org.apache.hugegraph.server; +import org.apache.hugegraph.HugeException; import org.apache.hugegraph.api.filter.RedirectFilterDynamicFeature; +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.define.WorkLoad; +import org.apache.hugegraph.event.EventHub; +import org.apache.hugegraph.util.E; +import org.apache.hugegraph.version.CoreVersion; import org.apache.tinkerpop.gremlin.server.util.MetricManager; import org.glassfish.hk2.api.Factory; import org.glassfish.hk2.api.MultiException; @@ -30,13 +37,6 @@ import org.glassfish.jersey.server.monitoring.RequestEvent; import org.glassfish.jersey.server.monitoring.RequestEventListener; -import org.apache.hugegraph.HugeException; -import org.apache.hugegraph.config.HugeConfig; -import org.apache.hugegraph.core.GraphManager; -import org.apache.hugegraph.define.WorkLoad; -import org.apache.hugegraph.event.EventHub; -import org.apache.hugegraph.util.E; -import org.apache.hugegraph.version.CoreVersion; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.jersey3.InstrumentedResourceMethodApplicationListener; @@ -48,15 +48,13 @@ @ApplicationPath("/") @OpenAPIDefinition( - info = - @Info( - title = "HugeGraph RESTful API", - version = CoreVersion.DEFAULT_VERSION, - description = "All management API for HugeGraph", - contact = - @Contact( - url = "https://github.com/apache/hugegraph", - name = "HugeGraph"))) + info = @Info( + title = "HugeGraph RESTful API", + version = CoreVersion.DEFAULT_VERSION, + description = "All management API for HugeGraph", + contact = @Contact(url = "https://github.com/apache/hugegraph", name = "HugeGraph") + ) +) public class ApplicationConfig extends ResourceConfig { public ApplicationConfig(HugeConfig conf, EventHub hub) { @@ -87,10 +85,9 @@ public ApplicationConfig(HugeConfig conf, EventHub hub) { register(OpenApiResource.class); } - private class ConfFactory extends AbstractBinder - implements Factory { + private class ConfFactory extends AbstractBinder implements Factory { - private HugeConfig conf = null; + private final HugeConfig conf; public ConfFactory(HugeConfig conf) { E.checkNotNull(conf, "configuration"); @@ -113,8 +110,7 @@ public void dispose(HugeConfig conf) { } } - private class GraphManagerFactory extends AbstractBinder - implements Factory { + private class GraphManagerFactory extends AbstractBinder implements Factory { private GraphManager manager = null; @@ -170,8 +166,7 @@ public void dispose(GraphManager manager) { } } - private class WorkLoadFactory extends AbstractBinder - implements Factory { + private class WorkLoadFactory extends AbstractBinder implements Factory { private final WorkLoad load; diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java index 9c4614f459..4a62708646 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/HugeGraph.java @@ -28,10 +28,23 @@ 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.config.HugeConfig; +import org.apache.hugegraph.config.TypedOption; import org.apache.hugegraph.masterelection.RoleElectionStateMachine; import org.apache.hugegraph.rpc.RpcServiceConfig4Client; import org.apache.hugegraph.rpc.RpcServiceConfig4Server; +import org.apache.hugegraph.schema.EdgeLabel; +import org.apache.hugegraph.schema.IndexLabel; +import org.apache.hugegraph.schema.PropertyKey; +import org.apache.hugegraph.schema.SchemaElement; +import org.apache.hugegraph.schema.SchemaLabel; +import org.apache.hugegraph.schema.SchemaManager; +import org.apache.hugegraph.schema.VertexLabel; +import org.apache.hugegraph.structure.HugeFeatures; import org.apache.hugegraph.task.TaskScheduler; +import org.apache.hugegraph.traversal.optimize.HugeCountStepStrategy; +import org.apache.hugegraph.traversal.optimize.HugeGraphStepStrategy; +import org.apache.hugegraph.traversal.optimize.HugeVertexStepStrategy; import org.apache.hugegraph.type.HugeType; import org.apache.hugegraph.type.define.GraphMode; import org.apache.hugegraph.type.define.GraphReadMode; @@ -44,19 +57,6 @@ import org.apache.tinkerpop.gremlin.structure.VertexProperty; import com.alipay.remoting.rpc.RpcServer; -import org.apache.hugegraph.config.HugeConfig; -import org.apache.hugegraph.config.TypedOption; -import org.apache.hugegraph.schema.EdgeLabel; -import org.apache.hugegraph.schema.IndexLabel; -import org.apache.hugegraph.schema.PropertyKey; -import org.apache.hugegraph.schema.SchemaElement; -import org.apache.hugegraph.schema.SchemaLabel; -import org.apache.hugegraph.schema.SchemaManager; -import org.apache.hugegraph.schema.VertexLabel; -import org.apache.hugegraph.structure.HugeFeatures; -import org.apache.hugegraph.traversal.optimize.HugeCountStepStrategy; -import org.apache.hugegraph.traversal.optimize.HugeGraphStepStrategy; -import org.apache.hugegraph.traversal.optimize.HugeVertexStepStrategy; /** * Graph interface for Gremlin operations @@ -314,10 +314,9 @@ default Id[] mapVlName2Id(String[] vertexLabels) { } static void registerTraversalStrategies(Class clazz) { - TraversalStrategies strategies = null; - strategies = TraversalStrategies.GlobalCache - .getStrategies(Graph.class) - .clone(); + TraversalStrategies strategies = TraversalStrategies.GlobalCache + .getStrategies(Graph.class) + .clone(); strategies.addStrategies(HugeVertexStepStrategy.instance(), HugeGraphStepStrategy.instance(), HugeCountStepStrategy.instance()); diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java b/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java index 160c92fdd9..c1d0108f7c 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java @@ -25,6 +25,8 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.hugegraph.analyzer.Analyzer; +import org.apache.hugegraph.analyzer.AnalyzerFactory; import org.apache.hugegraph.auth.AuthManager; import org.apache.hugegraph.auth.StandardAuthManager; import org.apache.hugegraph.backend.BackendException; @@ -39,22 +41,47 @@ import org.apache.hugegraph.backend.id.IdGenerator; import org.apache.hugegraph.backend.id.SnowflakeIdGenerator; import org.apache.hugegraph.backend.query.Query; +import org.apache.hugegraph.backend.serializer.AbstractSerializer; +import org.apache.hugegraph.backend.serializer.SerializerFactory; +import org.apache.hugegraph.backend.store.BackendFeatures; +import org.apache.hugegraph.backend.store.BackendProviderFactory; +import org.apache.hugegraph.backend.store.BackendStore; +import org.apache.hugegraph.backend.store.BackendStoreInfo; +import org.apache.hugegraph.backend.store.BackendStoreProvider; import org.apache.hugegraph.backend.store.raft.RaftBackendStoreProvider; import org.apache.hugegraph.backend.store.raft.RaftGroupManager; import org.apache.hugegraph.backend.store.ram.RamTable; import org.apache.hugegraph.backend.tx.GraphTransaction; import org.apache.hugegraph.backend.tx.SchemaTransaction; import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.config.TypedOption; +import org.apache.hugegraph.event.EventHub; +import org.apache.hugegraph.event.EventListener; +import org.apache.hugegraph.exception.NotAllowException; +import org.apache.hugegraph.io.HugeGraphIoRegistry; import org.apache.hugegraph.masterelection.ClusterRoleStore; import org.apache.hugegraph.masterelection.Config; import org.apache.hugegraph.masterelection.RoleElectionConfig; import org.apache.hugegraph.masterelection.RoleElectionOptions; 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.masterelection.StandardRoleElectionStateMachine; +import org.apache.hugegraph.perf.PerfUtil.Watched; import org.apache.hugegraph.rpc.RpcServiceConfig4Client; import org.apache.hugegraph.rpc.RpcServiceConfig4Server; +import org.apache.hugegraph.schema.EdgeLabel; +import org.apache.hugegraph.schema.IndexLabel; +import org.apache.hugegraph.schema.PropertyKey; +import org.apache.hugegraph.schema.SchemaElement; +import org.apache.hugegraph.schema.SchemaLabel; +import org.apache.hugegraph.schema.SchemaManager; +import org.apache.hugegraph.schema.VertexLabel; +import org.apache.hugegraph.structure.HugeEdge; +import org.apache.hugegraph.structure.HugeEdgeProperty; +import org.apache.hugegraph.structure.HugeFeatures; +import org.apache.hugegraph.structure.HugeVertex; +import org.apache.hugegraph.structure.HugeVertexProperty; import org.apache.hugegraph.task.ServerInfoManager; import org.apache.hugegraph.task.TaskManager; import org.apache.hugegraph.task.TaskScheduler; @@ -62,7 +89,13 @@ import org.apache.hugegraph.type.define.GraphMode; import org.apache.hugegraph.type.define.GraphReadMode; import org.apache.hugegraph.type.define.NodeRole; -import org.apache.hugegraph.util.*; +import org.apache.hugegraph.util.ConfigUtil; +import org.apache.hugegraph.util.DateUtil; +import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.Events; +import org.apache.hugegraph.util.LockUtil; +import org.apache.hugegraph.util.Log; +import org.apache.hugegraph.variables.HugeVariables; import org.apache.tinkerpop.gremlin.process.computer.GraphComputer; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Graph; @@ -73,40 +106,9 @@ import org.apache.tinkerpop.gremlin.structure.io.Io; import org.apache.tinkerpop.gremlin.structure.util.AbstractThreadLocalTransaction; import org.apache.tinkerpop.gremlin.structure.util.StringFactory; -import org.apache.hugegraph.util.ConfigUtil; -import org.apache.hugegraph.util.Events; -import org.apache.hugegraph.util.LockUtil; import org.slf4j.Logger; import com.alipay.remoting.rpc.RpcServer; -import org.apache.hugegraph.analyzer.Analyzer; -import org.apache.hugegraph.analyzer.AnalyzerFactory; -import org.apache.hugegraph.backend.serializer.AbstractSerializer; -import org.apache.hugegraph.backend.serializer.SerializerFactory; -import org.apache.hugegraph.backend.store.BackendFeatures; -import org.apache.hugegraph.backend.store.BackendProviderFactory; -import org.apache.hugegraph.backend.store.BackendStore; -import org.apache.hugegraph.backend.store.BackendStoreInfo; -import org.apache.hugegraph.backend.store.BackendStoreProvider; -import org.apache.hugegraph.config.HugeConfig; -import org.apache.hugegraph.config.TypedOption; -import org.apache.hugegraph.event.EventHub; -import org.apache.hugegraph.event.EventListener; -import org.apache.hugegraph.exception.NotAllowException; -import org.apache.hugegraph.perf.PerfUtil.Watched; -import org.apache.hugegraph.schema.EdgeLabel; -import org.apache.hugegraph.schema.IndexLabel; -import org.apache.hugegraph.schema.PropertyKey; -import org.apache.hugegraph.schema.SchemaElement; -import org.apache.hugegraph.schema.SchemaLabel; -import org.apache.hugegraph.schema.SchemaManager; -import org.apache.hugegraph.schema.VertexLabel; -import org.apache.hugegraph.structure.HugeEdge; -import org.apache.hugegraph.structure.HugeEdgeProperty; -import org.apache.hugegraph.structure.HugeFeatures; -import org.apache.hugegraph.structure.HugeVertex; -import org.apache.hugegraph.structure.HugeVertexProperty; -import org.apache.hugegraph.variables.HugeVariables; import com.google.common.collect.ImmutableSet; import com.google.common.util.concurrent.RateLimiter; @@ -292,7 +294,7 @@ private void initRoleStateWorker(Id serverId) { this.configuration.get(RoleElectionOptions.NODE_EXTERNAL_URL), this.configuration.get(RoleElectionOptions.EXCEEDS_FAIL_COUNT), this.configuration.get(RoleElectionOptions.RANDOM_TIMEOUT_MILLISECOND), - this.configuration.get(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOUND), + this.configuration.get(RoleElectionOptions.HEARTBEAT_INTERVAL_SECOND), this.configuration.get(RoleElectionOptions.MASTER_DEAD_TIMES), this.configuration.get(RoleElectionOptions.BASE_TIMEOUT_MILLISECOND)); ClusterRoleStore clusterRoleStore = new StandardClusterRoleStore(this.params); @@ -394,7 +396,7 @@ public void truncateBackend() { LockUtil.lock(this.name, LockUtil.GRAPH_LOCK); try { this.storeProvider.truncate(); - // TOOD: remove this after serverinfo saved in etcd + // TODO: remove this after serverinfo saved in etcd this.serverStarted(this.serverInfoManager().selfServerId(), this.serverInfoManager().selfServerRole()); } finally { diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/ClusterRole.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/ClusterRole.java index c22020a83a..d9b95a9b92 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/ClusterRole.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/ClusterRole.java @@ -21,10 +21,10 @@ public class ClusterRole { - private String node; + private final String node; private long clock; - private int epoch; - private String url; + private final int epoch; + private final String url; public ClusterRole(String node, String url, int epoch) { this(node, url, epoch, 1); diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionConfig.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionConfig.java index 0a915283b4..c810b28e08 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionConfig.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionConfig.java @@ -19,13 +19,13 @@ public class RoleElectionConfig implements Config { - private String node; - private String url; - private int exceedsFailCount; - private long randomTimeoutMillisecond; - private long heartBeatIntervalSecond; - private int masterDeadTimes; - private long baseTimeoutMillisecond; + private final String node; + private final String url; + private final int exceedsFailCount; + private final long randomTimeoutMillisecond; + private final long heartBeatIntervalSecond; + private final int masterDeadTimes; + private final long baseTimeoutMillisecond; public RoleElectionConfig(String node, String url, int exceedsFailCount, long randomTimeoutMillisecond, long heartBeatIntervalSecond, diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionOptions.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionOptions.java index 97017f2732..1c8498c08d 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionOptions.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/RoleElectionOptions.java @@ -17,12 +17,12 @@ package org.apache.hugegraph.masterelection; -import org.apache.hugegraph.config.ConfigOption; -import org.apache.hugegraph.config.OptionHolder; - import static org.apache.hugegraph.config.OptionChecker.disallowEmpty; import static org.apache.hugegraph.config.OptionChecker.rangeInt; +import org.apache.hugegraph.config.ConfigOption; +import org.apache.hugegraph.config.OptionHolder; + public class RoleElectionOptions extends OptionHolder { private RoleElectionOptions() { @@ -67,7 +67,7 @@ public static synchronized RoleElectionOptions instance() { 1000 ); - public static final ConfigOption HEARTBEAT_INTERVAL_SECOUND = + public static final ConfigOption HEARTBEAT_INTERVAL_SECOND = new ConfigOption<>( "server.role.heartbeat_interval", "The role state machine heartbeat interval second time.", diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardClusterRoleStore.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardClusterRoleStore.java index cbb6e9bfc9..f0f69319d6 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardClusterRoleStore.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardClusterRoleStore.java @@ -46,14 +46,13 @@ public class StandardClusterRoleStore implements ClusterRoleStore { private static final int RETRY_QUERY_TIMEOUT = 200; private final HugeGraphParams graph; - private final Schema schema; private boolean firstTime; public StandardClusterRoleStore(HugeGraphParams graph) { this.graph = graph; - this.schema = new Schema(graph); - this.schema.initSchemaIfNeeded(); + Schema schema = new Schema(graph); + schema.initSchemaIfNeeded(); this.firstTime = true; } @@ -73,19 +72,21 @@ public boolean updateIfNodePresent(ClusterRole clusterRole) { return false; } LOG.trace("Server {} epoch {} begin remove data old epoch {}, ", - clusterRole.node(), clusterRole.epoch(), oldClusterRole.epoch()); + clusterRole.node(), clusterRole.epoch(), oldClusterRole.epoch()); this.graph.systemTransaction().removeVertex((HugeVertex) oldClusterRoleOpt.get()); this.graph.systemTransaction().commitOrRollback(); LOG.trace("Server {} epoch {} success remove data old epoch {}, ", - clusterRole.node(), clusterRole.epoch(), oldClusterRole.epoch()); + clusterRole.node(), clusterRole.epoch(), oldClusterRole.epoch()); } try { GraphTransaction tx = this.graph.systemTransaction(); tx.doUpdateIfAbsent(this.constructEntry(clusterRole)); tx.commitOrRollback(); - LOG.trace("Server {} epoch {} success update data", clusterRole.node(), clusterRole.epoch()); - } catch (Throwable ignore){ - LOG.trace("Server {} epoch {} fail update data", clusterRole.node(), clusterRole.epoch()); + LOG.trace("Server {} epoch {} success update data", + clusterRole.node(), clusterRole.epoch()); + } catch (Throwable ignore) { + LOG.trace("Server {} epoch {} fail update data", + clusterRole.node(), clusterRole.epoch()); return false; } @@ -140,8 +141,7 @@ private ClusterRole from(Vertex vertex) { Long clock = (Long) vertex.property(P.CLOCK).value(); Integer epoch = (Integer) vertex.property(P.EPOCH).value(); - ClusterRole roleTypeData = new ClusterRole(node, url, epoch, clock); - return roleTypeData; + return new ClusterRole(node, url, epoch, clock); } private Optional queryVertex() { @@ -191,12 +191,13 @@ public void initSchemaIfNeeded() { String[] properties = this.initProperties(); - VertexLabel label = this.schema().vertexLabel(this.label) - .enableLabelIndex(true) - .usePrimaryKeyId() - .primaryKeys(P.TYPE) - .properties(properties) - .build(); + VertexLabel label = this.schema() + .vertexLabel(this.label) + .enableLabelIndex(true) + .usePrimaryKeyId() + .primaryKeys(P.TYPE) + .properties(properties) + .build(); this.graph.schemaTransaction().addVertexLabel(label); } diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardRoleElectionStateMachine.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardRoleElectionStateMachine.java index be151deb10..aa284def60 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardRoleElectionStateMachine.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardRoleElectionStateMachine.java @@ -56,8 +56,8 @@ public void apply(StateMachineCallback stateMachineCallback) { RoleState pre = this.state; this.state = state.transform(context); LOG.trace("server {} epoch {} role state change {} to {}", - context.node(), context.epoch(), pre.getClass().getSimpleName(), - this.state.getClass().getSimpleName()); + context.node(), context.epoch(), pre.getClass().getSimpleName(), + this.state.getClass().getSimpleName()); Callback runnable = this.state.callback(stateMachineCallback); runnable.call(context); failCount = 0; @@ -128,8 +128,7 @@ public RoleState transform(StateMachineContext context) { } context.epoch(clusterRole.epoch()); - context.master(new MasterServerInfoImpl( - clusterRole.node(), clusterRole.url())); + context.master(new MasterServerInfoImpl(clusterRole.node(), clusterRole.url())); if (clusterRole.isMaster(context.node())) { return new MasterState(clusterRole); } else { @@ -257,8 +256,7 @@ public RoleState transform(StateMachineContext context) { // The master failover completed context.epoch(clusterRole.epoch()); if (context.adapter().updateIfNodePresent(clusterRole)) { - context.master(new MasterServerInfoImpl( - clusterRole.node(), clusterRole.url())); + context.master(new MasterServerInfoImpl(clusterRole.node(), clusterRole.url())); return new MasterState(clusterRole); } else { return new UnknownState(epoch).transform(context); diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java index 1713919f42..11a989ecdb 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StandardStateMachineCallback.java @@ -17,12 +17,12 @@ package org.apache.hugegraph.masterelection; +import java.util.Objects; + import org.apache.hugegraph.task.TaskManager; import org.apache.hugegraph.util.Log; import org.slf4j.Logger; -import java.util.Objects; - public class StandardStateMachineCallback implements StateMachineCallback { private static final Logger LOG = Log.logger(StandardStateMachineCallback.class); diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StateMachineContext.java b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StateMachineContext.java index b4ffa717a2..a3eaf89626 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StateMachineContext.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/masterelection/StateMachineContext.java @@ -40,6 +40,7 @@ public interface StateMachineContext { interface MasterServerInfo { String url(); + String node(); } } diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java b/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java index 3adaf6e802..e8cccf88e3 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java @@ -25,30 +25,30 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; +import org.apache.hugegraph.HugeException; +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.HugeGraphParams; import org.apache.hugegraph.backend.id.Id; import org.apache.hugegraph.backend.page.PageInfo; import org.apache.hugegraph.backend.query.Condition; import org.apache.hugegraph.backend.query.ConditionQuery; import org.apache.hugegraph.backend.query.QueryResults; import org.apache.hugegraph.backend.tx.GraphTransaction; +import org.apache.hugegraph.exception.ConnectionException; +import org.apache.hugegraph.iterator.ListIterator; +import org.apache.hugegraph.iterator.MapperIterator; import org.apache.hugegraph.schema.PropertyKey; import org.apache.hugegraph.schema.VertexLabel; +import org.apache.hugegraph.structure.HugeVertex; import org.apache.hugegraph.type.HugeType; import org.apache.hugegraph.type.define.HugeKeys; import org.apache.hugegraph.type.define.NodeRole; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.slf4j.Logger; - -import org.apache.hugegraph.HugeException; -import org.apache.hugegraph.HugeGraph; -import org.apache.hugegraph.HugeGraphParams; -import org.apache.hugegraph.exception.ConnectionException; -import org.apache.hugegraph.iterator.ListIterator; -import org.apache.hugegraph.iterator.MapperIterator; -import org.apache.hugegraph.structure.HugeVertex; import org.apache.hugegraph.util.DateUtil; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Log; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.slf4j.Logger; + import com.google.common.collect.ImmutableMap; public class ServerInfoManager { @@ -192,9 +192,8 @@ protected boolean graphReady() { return !this.closed && this.graph.started() && this.graph.initialized(); } - protected synchronized HugeServerInfo pickWorkerNode( - Collection servers, - HugeTask task) { + protected synchronized HugeServerInfo pickWorkerNode(Collection servers, + HugeTask task) { HugeServerInfo master = null; HugeServerInfo serverWithMinLoad = null; int minLoad = Integer.MAX_VALUE; @@ -271,7 +270,7 @@ private Id save(HugeServerInfo serverInfo) { private int save(Collection serverInfos) { return this.call(() -> { if (serverInfos.isEmpty()) { - return serverInfos.size(); + return 0; } HugeServerInfo.Schema schema = HugeServerInfo.schema(this.graph); if (!schema.existVertexLabel(HugeServerInfo.P.SERVER)) { diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java b/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java index a8aa673570..9eda3f6b02 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java @@ -29,6 +29,9 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; +import org.apache.hugegraph.HugeException; +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.HugeGraphParams; import org.apache.hugegraph.backend.id.Id; import org.apache.hugegraph.backend.page.PageInfo; import org.apache.hugegraph.backend.query.Condition; @@ -37,32 +40,29 @@ import org.apache.hugegraph.backend.store.BackendStore; import org.apache.hugegraph.backend.tx.GraphTransaction; import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.exception.ConnectionException; +import org.apache.hugegraph.exception.NotFoundException; +import org.apache.hugegraph.iterator.ExtendableIterator; +import org.apache.hugegraph.iterator.MapperIterator; +import org.apache.hugegraph.job.EphemeralJob; import org.apache.hugegraph.schema.IndexLabel; import org.apache.hugegraph.schema.PropertyKey; import org.apache.hugegraph.schema.SchemaManager; import org.apache.hugegraph.schema.VertexLabel; +import org.apache.hugegraph.structure.HugeVertex; +import org.apache.hugegraph.task.HugeTask.P; +import org.apache.hugegraph.task.TaskCallable.SysTaskCallable; +import org.apache.hugegraph.task.TaskManager.ContextCallable; import org.apache.hugegraph.type.HugeType; import org.apache.hugegraph.type.define.Cardinality; import org.apache.hugegraph.type.define.DataType; import org.apache.hugegraph.type.define.HugeKeys; +import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.Log; import org.apache.tinkerpop.gremlin.structure.Graph.Hidden; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.slf4j.Logger; -import org.apache.hugegraph.HugeException; -import org.apache.hugegraph.HugeGraph; -import org.apache.hugegraph.HugeGraphParams; -import org.apache.hugegraph.exception.ConnectionException; -import org.apache.hugegraph.exception.NotFoundException; -import org.apache.hugegraph.iterator.ExtendableIterator; -import org.apache.hugegraph.iterator.MapperIterator; -import org.apache.hugegraph.job.EphemeralJob; -import org.apache.hugegraph.structure.HugeVertex; -import org.apache.hugegraph.task.HugeTask.P; -import org.apache.hugegraph.task.TaskCallable.SysTaskCallable; -import org.apache.hugegraph.task.TaskManager.ContextCallable; -import org.apache.hugegraph.util.E; -import org.apache.hugegraph.util.Log; import com.google.common.collect.ImmutableMap; public class StandardTaskScheduler implements TaskScheduler { @@ -123,7 +123,7 @@ private TaskTransaction tx() { * NOTE: don't synchronized(this) due to scheduler thread hold * this lock through scheduleTasks(), then query tasks and wait * for db-worker thread after call(), the tx may not be initialized - * but can't catch this lock, then cause dead lock. + * but can't catch this lock, then cause deadlock. * We just use this.serverManager as a monitor here */ synchronized (this.serverManager) { @@ -636,7 +636,7 @@ private HugeTask waitUntilTaskCompleted(Id id, long seconds, public void waitUntilAllTasksCompleted(long seconds) throws TimeoutException { long passes = seconds * 1000 / QUERY_INTERVAL; - int taskSize = 0; + int taskSize; for (long pass = 0;; pass++) { taskSize = this.pendingTasks(); if (taskSize == 0) { diff --git a/hugegraph-dist/src/main/java/org/apache/hugegraph/dist/RegisterUtil.java b/hugegraph-dist/src/main/java/org/apache/hugegraph/dist/RegisterUtil.java index e1c9a3b046..34e5ea3cf6 100644 --- a/hugegraph-dist/src/main/java/org/apache/hugegraph/dist/RegisterUtil.java +++ b/hugegraph-dist/src/main/java/org/apache/hugegraph/dist/RegisterUtil.java @@ -24,20 +24,19 @@ import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.configuration2.builder.fluent.Configurations; import org.apache.commons.configuration2.ex.ConfigurationException; -import org.apache.hugegraph.masterelection.RoleElectionOptions; -import org.slf4j.Logger; - import org.apache.hugegraph.HugeException; import org.apache.hugegraph.backend.serializer.SerializerFactory; import org.apache.hugegraph.backend.store.BackendProviderFactory; import org.apache.hugegraph.config.CoreOptions; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.config.OptionSpace; +import org.apache.hugegraph.masterelection.RoleElectionOptions; import org.apache.hugegraph.plugin.HugeGraphPlugin; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.Log; import org.apache.hugegraph.util.VersionUtil; import org.apache.hugegraph.version.CoreVersion; +import org.slf4j.Logger; public class RegisterUtil { @@ -55,7 +54,7 @@ public static void registerBackends() { E.checkState(input != null, "Can't read file '%s' as stream", confFile); - PropertiesConfiguration props = null; + PropertiesConfiguration props; try { props = new Configurations().properties(input); } catch (ConfigurationException e) { @@ -93,15 +92,14 @@ private static void registerBackend(String backend) { registerPostgresql(); break; default: - throw new HugeException("Unsupported backend type '%s'", - backend); + throw new HugeException("Unsupported backend type '%s'", backend); } } public static void registerCassandra() { // Register config OptionSpace.register("cassandra", - "org.apache.hugegraph.backend.store.cassandra.CassandraOptions"); + "org.apache.hugegraph.backend.store.cassandra.CassandraOptions"); // Register serializer SerializerFactory.register("cassandra", "org.apache.hugegraph.backend.store.cassandra.CassandraSerializer"); @@ -113,7 +111,7 @@ public static void registerCassandra() { public static void registerScyllaDB() { // Register config OptionSpace.register("scylladb", - "org.apache.hugegraph.backend.store.cassandra.CassandraOptions"); + "org.apache.hugegraph.backend.store.cassandra.CassandraOptions"); // Register serializer SerializerFactory.register("scylladb", "org.apache.hugegraph.backend.store.cassandra.CassandraSerializer"); @@ -125,10 +123,10 @@ public static void registerScyllaDB() { public static void registerHBase() { // Register config OptionSpace.register("hbase", - "org.apache.hugegraph.backend.store.hbase.HbaseOptions"); + "org.apache.hugegraph.backend.store.hbase.HbaseOptions"); // Register serializer SerializerFactory.register("hbase", - "org.apache.hugegraph.backend.store.hbase.HbaseSerializer"); + "org.apache.hugegraph.backend.store.hbase.HbaseSerializer"); // Register backend BackendProviderFactory.register("hbase", "org.apache.hugegraph.backend.store.hbase.HbaseStoreProvider"); @@ -137,7 +135,7 @@ public static void registerHBase() { public static void registerRocksDB() { // Register config OptionSpace.register("rocksdb", - "org.apache.hugegraph.backend.store.rocksdb.RocksDBOptions"); + "org.apache.hugegraph.backend.store.rocksdb.RocksDBOptions"); // Register backend BackendProviderFactory.register("rocksdb", "org.apache.hugegraph.backend.store.rocksdb.RocksDBStoreProvider"); @@ -148,10 +146,10 @@ public static void registerRocksDB() { public static void registerMysql() { // Register config OptionSpace.register("mysql", - "org.apache.hugegraph.backend.store.mysql.MysqlOptions"); + "org.apache.hugegraph.backend.store.mysql.MysqlOptions"); // Register serializer SerializerFactory.register("mysql", - "org.apache.hugegraph.backend.store.mysql.MysqlSerializer"); + "org.apache.hugegraph.backend.store.mysql.MysqlSerializer"); // Register backend BackendProviderFactory.register("mysql", "org.apache.hugegraph.backend.store.mysql.MysqlStoreProvider"); @@ -160,7 +158,7 @@ public static void registerMysql() { public static void registerPalo() { // Register config OptionSpace.register("palo", - "org.apache.hugegraph.backend.store.palo.PaloOptions"); + "org.apache.hugegraph.backend.store.palo.PaloOptions"); // Register serializer SerializerFactory.register("palo", "org.apache.hugegraph.backend.store.palo.PaloSerializer"); @@ -194,8 +192,7 @@ public static void registerServer() { * Scan the jars in plugins directory and load them */ public static void registerPlugins() { - ServiceLoader plugins = ServiceLoader.load( - HugeGraphPlugin.class); + ServiceLoader plugins = ServiceLoader.load(HugeGraphPlugin.class); for (HugeGraphPlugin plugin : plugins) { LOG.info("Loading plugin {}({})", plugin.name(), plugin.getClass().getCanonicalName()); @@ -214,8 +211,7 @@ public static void registerPlugins() { plugin.register(); LOG.info("Loaded plugin '{}'", plugin.name()); } catch (Exception e) { - throw new HugeException("Failed to load plugin '%s'", - plugin.name(), e); + throw new HugeException("Failed to load plugin '%s'", plugin.name(), e); } } } diff --git a/hugegraph-test/src/main/java/org/apache/hugegraph/core/RoleElectionStateMachineTest.java b/hugegraph-test/src/main/java/org/apache/hugegraph/core/RoleElectionStateMachineTest.java index a2af1bd193..9fbbb5e628 100644 --- a/hugegraph-test/src/main/java/org/apache/hugegraph/core/RoleElectionStateMachineTest.java +++ b/hugegraph-test/src/main/java/org/apache/hugegraph/core/RoleElectionStateMachineTest.java @@ -30,11 +30,11 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.LockSupport; +import org.apache.hugegraph.masterelection.ClusterRole; 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; @@ -86,10 +86,10 @@ public int hashCode() { @Override public String toString() { return "LogEntry{" + - "epoch=" + epoch + - ", node='" + node + '\'' + - ", role=" + role + - '}'; + "epoch=" + epoch + + ", node='" + node + '\'' + + ", role=" + role + + '}'; } } @@ -254,8 +254,7 @@ public boolean updateIfNodePresent(ClusterRole clusterRole) { @Override public Optional query() { - return Optional.ofNullable( - this.copy(this.data.get(this.epoch))); + return Optional.ofNullable(this.copy(this.data.get(this.epoch))); } }; @@ -315,7 +314,7 @@ public Optional query() { Assert.assertGt(0, logRecords.size()); Map masters = new HashMap<>(); - for (LogEntry entry: logRecords) { + for (LogEntry entry : logRecords) { if (entry.role == LogEntry.Role.master) { String lastNode = masters.putIfAbsent(entry.epoch, entry.node); if (lastNode != null) {