From 034cca6555ed99e3903b8e618f25b33eeb5a9dd9 Mon Sep 17 00:00:00 2001 From: imbajin Date: Fri, 10 Nov 2023 14:42:09 +0800 Subject: [PATCH 1/4] fix(api): refactor/downgrade record logic for slow log add some TODOs & assign @SunnyBoy-WYH to address it --- .../apache/hugegraph/api/auth/LoginAPI.java | 56 ++++++------- .../hugegraph/api/filter/AccessLogFilter.java | 83 ++++++++++--------- .../hugegraph/api/filter/PathFilter.java | 24 ++---- .../hugegraph/metrics/SlowQueryLog.java | 21 +++-- 4 files changed, 92 insertions(+), 92 deletions(-) diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/LoginAPI.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/LoginAPI.java index a227fd0b50..0982e01c1e 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/LoginAPI.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/LoginAPI.java @@ -17,39 +17,40 @@ package org.apache.hugegraph.api.auth; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.inject.Singleton; import javax.security.sasl.AuthenticationException; -import jakarta.ws.rs.BadRequestException; -import jakarta.ws.rs.Consumes; -import jakarta.ws.rs.DELETE; -import jakarta.ws.rs.GET; -import jakarta.ws.rs.HeaderParam; -import jakarta.ws.rs.NotAuthorizedException; -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 jakarta.ws.rs.core.HttpHeaders; import org.apache.commons.lang3.StringUtils; -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.AuthenticationFilter; import org.apache.hugegraph.api.filter.StatusFilter.Status; import org.apache.hugegraph.auth.AuthConstant; import org.apache.hugegraph.auth.UserWithRole; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.define.Checkable; 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.JsonProperty; import com.google.common.collect.ImmutableMap; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.inject.Singleton; +import jakarta.ws.rs.BadRequestException; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.HeaderParam; +import jakarta.ws.rs.NotAuthorizedException; +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 jakarta.ws.rs.core.HttpHeaders; + @Path("graphs/{graph}/auth") @Singleton @Tag(name = "LoginAPI") @@ -63,8 +64,7 @@ public class LoginAPI extends API { @Status(Status.OK) @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON_WITH_CHARSET) - public String login(@Context GraphManager manager, - @PathParam("graph") String graph, + public String login(@Context GraphManager manager, @PathParam("graph") String graph, JsonLogin jsonLogin) { LOG.debug("Graph [{}] user login: {}", graph, jsonLogin); checkCreatingBody(jsonLogin); @@ -94,13 +94,10 @@ public void logout(@Context GraphManager manager, LOG.debug("Graph [{}] user logout: {}", graph, auth); if (!auth.startsWith(AuthenticationFilter.BEARER_TOKEN_PREFIX)) { - throw new BadRequestException( - "Only HTTP Bearer authentication is supported"); + throw new BadRequestException("Only HTTP Bearer authentication is supported"); } - String token = auth.substring(AuthenticationFilter.BEARER_TOKEN_PREFIX - .length()); - + String token = auth.substring(AuthenticationFilter.BEARER_TOKEN_PREFIX.length()); manager.authManager().logoutUser(token); } @@ -119,12 +116,10 @@ public String verifyToken(@Context GraphManager manager, LOG.debug("Graph [{}] get user: {}", graph, token); if (!token.startsWith(AuthenticationFilter.BEARER_TOKEN_PREFIX)) { - throw new BadRequestException( - "Only HTTP Bearer authentication is supported"); + throw new BadRequestException("Only HTTP Bearer authentication is supported"); } - token = token.substring(AuthenticationFilter.BEARER_TOKEN_PREFIX - .length()); + token = token.substring(AuthenticationFilter.BEARER_TOKEN_PREFIX.length()); UserWithRole userWithRole = manager.authManager().validateUser(token); HugeGraph g = graph(manager, graph); @@ -144,8 +139,7 @@ private static class JsonLogin implements Checkable { @Override public void checkCreate(boolean isBatch) { - E.checkArgument(!StringUtils.isEmpty(this.name), - "The name of user can't be null"); + E.checkArgument(!StringUtils.isEmpty(this.name), "The name of user can't be null"); E.checkArgument(!StringUtils.isEmpty(this.password), "The password of user can't be null"); } diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java index 3b529cf0a3..e57a88ce50 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java @@ -17,7 +17,6 @@ package org.apache.hugegraph.api.filter; -import static org.apache.hugegraph.api.filter.PathFilter.REQUEST_PARAMS_JSON; import static org.apache.hugegraph.api.filter.PathFilter.REQUEST_TIME; import static org.apache.hugegraph.metrics.MetricsUtil.METRICS_PATH_FAILED_COUNTER; import static org.apache.hugegraph.metrics.MetricsUtil.METRICS_PATH_RESPONSE_TIME_HISTOGRAM; @@ -25,12 +24,11 @@ import static org.apache.hugegraph.metrics.MetricsUtil.METRICS_PATH_TOTAL_COUNTER; import java.io.IOException; +import java.net.URI; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.config.ServerOptions; import org.apache.hugegraph.metrics.MetricsUtil; -import org.apache.hugegraph.metrics.SlowQueryLog; -import org.apache.hugegraph.util.JsonUtil; import org.apache.hugegraph.util.Log; import org.slf4j.Logger; @@ -42,12 +40,12 @@ import jakarta.ws.rs.core.Context; import jakarta.ws.rs.ext.Provider; - +// TODO: should add test for this class @Provider @Singleton public class AccessLogFilter implements ContainerResponseFilter { - private static final String DELIMETER = "/"; + private static final String DELIMITER = "/"; private static final String GRAPHS = "graphs"; private static final String GREMLIN = "gremlin"; private static final String CYPHER = "cypher"; @@ -57,6 +55,25 @@ public class AccessLogFilter implements ContainerResponseFilter { @Context private jakarta.inject.Provider configProvider; + public static boolean needRecord(ContainerRequestContext context) { + // TODO: add test for 'path' result ('/gremlin' or 'gremlin') + String path = context.getUriInfo().getPath(); + + // GraphsAPI/CypherAPI/Job GremlinAPI + if (path.startsWith(GRAPHS)) { + if (HttpMethod.GET.equals(context.getMethod()) || + path.endsWith(CYPHER) || path.endsWith(GREMLIN)) { + return true; + } + } + // Raw GremlinAPI + return path.startsWith(GREMLIN); + } + + private String join(String path1, String path2) { + return String.join(DELIMITER, path1, path2); + } + /** * Use filter to log request info * @@ -64,10 +81,12 @@ public class AccessLogFilter implements ContainerResponseFilter { * @param responseContext responseContext */ @Override - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { + public void filter(ContainerRequestContext requestContext, + ContainerResponseContext responseContext) throws IOException { // Grab corresponding request / response info from context; - String method = requestContext.getRequest().getMethod(); - String path = requestContext.getUriInfo().getPath(); + URI uri = requestContext.getUriInfo().getRequestUri(); + String path = uri.getRawPath(); + String method = requestContext.getMethod(); String metricsName = join(path, method); MetricsUtil.registerCounter(join(metricsName, METRICS_PATH_TOTAL_COUNTER)).inc(); @@ -77,48 +96,30 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont MetricsUtil.registerCounter(join(metricsName, METRICS_PATH_FAILED_COUNTER)).inc(); } - // get responseTime Object requestTime = requestContext.getProperty(REQUEST_TIME); - if(requestTime != null){ + if (requestTime != null) { long now = System.currentTimeMillis(); long start = (Long) requestTime; - long responseTime = now - start; + long executeTime = now - start; - MetricsUtil.registerHistogram( - join(metricsName, METRICS_PATH_RESPONSE_TIME_HISTOGRAM)) - .update(responseTime); + MetricsUtil.registerHistogram(join(metricsName, METRICS_PATH_RESPONSE_TIME_HISTOGRAM)) + .update(executeTime); - HugeConfig config = configProvider.get(); - long timeThreshold = config.get(ServerOptions.SLOW_QUERY_LOG_TIME_THRESHOLD); + if (needRecord(requestContext)) { + HugeConfig config = configProvider.get(); + long timeThreshold = config.get(ServerOptions.SLOW_QUERY_LOG_TIME_THRESHOLD); - // record slow query log - if (timeThreshold > 0 && isSlowQueryLogWhiteAPI(requestContext) && responseTime > timeThreshold) { - SlowQueryLog log = new SlowQueryLog(responseTime, start, (String) requestContext.getProperty(REQUEST_PARAMS_JSON), - method, timeThreshold, path); - LOG.info("Slow query: {}", JsonUtil.toJson(log)); + // Record slow query if meet needs + if (timeThreshold > 0 && executeTime > timeThreshold) { + // TODO: set RequsetBody null, handle it later & should record "client IP" + LOG.info("[Slow Query] execTime={}ms, body={}, method={}, path={}, query={}", + executeTime, null, method, path, uri.getRawQuery()); + } } } } - private String join(String path1, String path2) { - return String.join(DELIMETER, path1, path2); - } - - private boolean statusOk(int status){ - return status == 200 || status == 201 || status == 202; - } - - public static boolean isSlowQueryLogWhiteAPI(ContainerRequestContext context) { - String path = context.getUriInfo().getPath(); - String method = context.getRequest().getMethod(); - - // GraphsAPI/CypherAPI/Job GremlinAPI - if (path.startsWith(GRAPHS)) { - if (method.equals(HttpMethod.GET) || path.endsWith(CYPHER) || path.endsWith(GREMLIN) ){ - return true; - } - } - // Raw GremlinAPI - return path.startsWith(GREMLIN); + private boolean statusOk(int status) { + return status >= 200 && status < 300; } } diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java index e1e449ef26..3ae9c35bc8 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java @@ -17,20 +17,12 @@ package org.apache.hugegraph.api.filter; -import static org.apache.hugegraph.api.API.CHARSET; - import java.io.IOException; -import java.io.InputStream; - -import org.apache.commons.io.Charsets; -import org.apache.commons.io.IOUtils; import jakarta.inject.Singleton; -import jakarta.ws.rs.HttpMethod; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerRequestFilter; import jakarta.ws.rs.container.PreMatching; -import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.ext.Provider; @Provider @@ -42,23 +34,25 @@ public class PathFilter implements ContainerRequestFilter { public static final String REQUEST_PARAMS_JSON = "request_params_json"; @Override - public void filter(ContainerRequestContext context) - throws IOException { + public void filter(ContainerRequestContext context) throws IOException { context.setProperty(REQUEST_TIME, System.currentTimeMillis()); - // record the request json + // TODO: comment it to fix loader bug, handle it later + /*// record the request json String method = context.getMethod(); String requestParamsJson = ""; if (method.equals(HttpMethod.POST)) { - requestParamsJson = IOUtils.toString(context.getEntityStream(), Charsets.toCharset(CHARSET)); + requestParamsJson = IOUtils.toString(context.getEntityStream(), + Charsets.toCharset(CHARSET)); // replace input stream because we have already read it InputStream in = IOUtils.toInputStream(requestParamsJson, Charsets.toCharset(CHARSET)); context.setEntityStream(in); - } else if(method.equals(HttpMethod.GET)){ - MultivaluedMap pathParameters = context.getUriInfo().getPathParameters(); + } else if (method.equals(HttpMethod.GET)) { + MultivaluedMap pathParameters = context.getUriInfo() + .getPathParameters(); requestParamsJson = pathParameters.toString(); } - context.setProperty(REQUEST_PARAMS_JSON, requestParamsJson); + context.setProperty(REQUEST_PARAMS_JSON, requestParamsJson);*/ } } diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java index cb3f1c7125..af49e5fdf7 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java @@ -19,20 +19,20 @@ public class SlowQueryLog { - public Long executeTime; + public long executeTime; - public Long startTime; + public long startTime; public String rawQuery; public String method; - public Long threshold; + public long threshold; public String path; - public SlowQueryLog(Long executeTime, Long startTime, String rawQuery, String method, Long threshold, - String path) { + public SlowQueryLog(long executeTime, long startTime, String rawQuery, String method, + long threshold, String path) { this.executeTime = executeTime; this.startTime = startTime; this.rawQuery = rawQuery; @@ -40,4 +40,15 @@ public SlowQueryLog(Long executeTime, Long startTime, String rawQuery, String me this.threshold = threshold; this.path = path; } + + @Override + public String toString() { + return "SlowQueryLog{executeTime=" + executeTime + + ", startTime=" + startTime + + ", rawQuery='" + rawQuery + '\'' + + ", method='" + method + '\'' + + ", threshold=" + threshold + + ", path='" + path + '\'' + + '}'; + } } From e3237842d9a8eae14154281ea16c7b6b6aef26ed Mon Sep 17 00:00:00 2001 From: imbajin Date: Fri, 10 Nov 2023 15:16:07 +0800 Subject: [PATCH 2/4] fix typo --- .../hugegraph/api/filter/AccessLogFilter.java | 2 +- .../hugegraph/metrics/SlowQueryLog.java | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java index e57a88ce50..435a9be351 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java @@ -111,7 +111,7 @@ public void filter(ContainerRequestContext requestContext, // Record slow query if meet needs if (timeThreshold > 0 && executeTime > timeThreshold) { - // TODO: set RequsetBody null, handle it later & should record "client IP" + // TODO: set RequestBody null, handle it later & should record "client IP" LOG.info("[Slow Query] execTime={}ms, body={}, method={}, path={}, query={}", executeTime, null, method, path, uri.getRawQuery()); } diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java index af49e5fdf7..f4707800cc 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java @@ -19,26 +19,26 @@ public class SlowQueryLog { - public long executeTime; - - public long startTime; - public String rawQuery; public String method; - public long threshold; - public String path; - public SlowQueryLog(long executeTime, long startTime, String rawQuery, String method, - long threshold, String path) { - this.executeTime = executeTime; - this.startTime = startTime; + public long executeTime; + + public long startTime; + + public long threshold; + + public SlowQueryLog(String rawQuery, String method, String path, + long executeTime, long startTime, long threshold) { this.rawQuery = rawQuery; this.method = method; - this.threshold = threshold; this.path = path; + this.executeTime = executeTime; + this.startTime = startTime; + this.threshold = threshold; } @Override From 42ae00ab1822ee75f4e72ad477ffc999a5e2faa2 Mon Sep 17 00:00:00 2001 From: imbajin Date: Sun, 12 Nov 2023 19:30:02 +0800 Subject: [PATCH 3/4] enhance the perf --- .../hugegraph/api/filter/AccessLogFilter.java | 31 +++++++-------- .../hugegraph/api/filter/PathFilter.java | 2 +- .../apache/hugegraph/api/ArthasApiTest.java | 5 ++- .../org/apache/hugegraph/api/BaseApiTest.java | 3 +- .../apache/hugegraph/api/LoginApiTest.java | 33 +++++++--------- .../apache/hugegraph/api/MetricsApiTest.java | 39 ++++++++----------- .../apache/hugegraph/api/ProjectApiTest.java | 20 +++++----- .../org/apache/hugegraph/api/UserApiTest.java | 26 ++++++------- .../apache/hugegraph/unit/UnitTestSuite.java | 31 +++++++-------- 9 files changed, 86 insertions(+), 104 deletions(-) diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java index 435a9be351..dd16eb3291 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java @@ -45,29 +45,28 @@ @Singleton public class AccessLogFilter implements ContainerResponseFilter { + private static final Logger LOG = Log.logger(AccessLogFilter.class); + private static final String DELIMITER = "/"; private static final String GRAPHS = "graphs"; private static final String GREMLIN = "gremlin"; private static final String CYPHER = "cypher"; - private static final Logger LOG = Log.logger(AccessLogFilter.class); - @Context private jakarta.inject.Provider configProvider; - public static boolean needRecord(ContainerRequestContext context) { + public static boolean needRecordLog(ContainerRequestContext context) { // TODO: add test for 'path' result ('/gremlin' or 'gremlin') String path = context.getUriInfo().getPath(); // GraphsAPI/CypherAPI/Job GremlinAPI if (path.startsWith(GRAPHS)) { - if (HttpMethod.GET.equals(context.getMethod()) || - path.endsWith(CYPHER) || path.endsWith(GREMLIN)) { + if (HttpMethod.GET.equals(context.getMethod()) || path.endsWith(CYPHER)) { return true; } } - // Raw GremlinAPI - return path.startsWith(GREMLIN); + // Direct GremlinAPI + return path.endsWith(GREMLIN); } private String join(String path1, String path2) { @@ -105,16 +104,14 @@ public void filter(ContainerRequestContext requestContext, MetricsUtil.registerHistogram(join(metricsName, METRICS_PATH_RESPONSE_TIME_HISTOGRAM)) .update(executeTime); - if (needRecord(requestContext)) { - HugeConfig config = configProvider.get(); - long timeThreshold = config.get(ServerOptions.SLOW_QUERY_LOG_TIME_THRESHOLD); - - // Record slow query if meet needs - if (timeThreshold > 0 && executeTime > timeThreshold) { - // TODO: set RequestBody null, handle it later & should record "client IP" - LOG.info("[Slow Query] execTime={}ms, body={}, method={}, path={}, query={}", - executeTime, null, method, path, uri.getRawQuery()); - } + HugeConfig config = configProvider.get(); + long timeThreshold = config.get(ServerOptions.SLOW_QUERY_LOG_TIME_THRESHOLD); + // Record slow query if meet needs, watch out the perf + if (timeThreshold > 0 && timeThreshold < executeTime && + needRecordLog(requestContext)) { + // TODO: set RequestBody null, handle it later & should record "client IP" + LOG.info("[Slow Query] execTime={}ms, body={}, method={}, path={}, query={}", + executeTime, null, method, path, uri.getQuery()); } } } diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java index 3ae9c35bc8..e7520f84fb 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java @@ -37,7 +37,7 @@ public class PathFilter implements ContainerRequestFilter { public void filter(ContainerRequestContext context) throws IOException { context.setProperty(REQUEST_TIME, System.currentTimeMillis()); - // TODO: comment it to fix loader bug, handle it later + // TODO: temporarily comment it to fix loader bug, handle it later /*// record the request json String method = context.getMethod(); String requestParamsJson = ""; diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ArthasApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ArthasApiTest.java index 174b665fea..c73276b38d 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ArthasApiTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ArthasApiTest.java @@ -46,8 +46,9 @@ public void testArthasApi() { " \"execTimeout\": \"10000\"\n" + "}"; RestClient arthasApiClient = new RestClient(ARTHAS_API_BASE_URL, false); - // If request header contains basic auth, and if we are not set auth when arthas attach hg, - // arthas will auth it and return 401. ref:https://arthas.aliyun.com/en/doc/auth.html#configure-username-and-password + // If the request header contains basic auth, + // and if we are not set auth when arthas attach hg, arthas will auth it and return 401. + // ref:https://arthas.aliyun.com/en/doc/auth.html#configure-username-and-password Response r = arthasApiClient.post(ARTHAS_API_PATH, body); String result = assertResponseStatus(200, r); assertJsonContains(result, "state"); diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java index 24b19ba1ea..a964a7236d 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/BaseApiTest.java @@ -139,8 +139,7 @@ public Response get(String path, String id) { return this.target.path(path).path(id).request().get(); } - public Response get(String path, - MultivaluedMap headers) { + public Response get(String path, MultivaluedMap headers) { return this.target.path(path).request().headers(headers).get(); } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/LoginApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/LoginApiTest.java index 40c83d5115..30775cb729 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/LoginApiTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/LoginApiTest.java @@ -20,18 +20,18 @@ import java.nio.file.Paths; import java.util.Map; -import jakarta.ws.rs.core.GenericType; -import jakarta.ws.rs.core.HttpHeaders; -import jakarta.ws.rs.core.MultivaluedHashMap; -import jakarta.ws.rs.core.MultivaluedMap; -import jakarta.ws.rs.core.Response; +import org.apache.hugegraph.testutil.Assert; +import org.apache.hugegraph.util.JsonUtil; import org.apache.tinkerpop.shaded.jackson.core.type.TypeReference; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.apache.hugegraph.testutil.Assert; -import org.apache.hugegraph.util.JsonUtil; +import jakarta.ws.rs.core.GenericType; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MultivaluedHashMap; +import jakarta.ws.rs.core.MultivaluedMap; +import jakarta.ws.rs.core.Response; public class LoginApiTest extends BaseApiTest { @@ -42,8 +42,7 @@ public class LoginApiTest extends BaseApiTest { @Before public void setup() { Response r = this.createUser("test", "test"); - Map user = r.readEntity( - new GenericType>(){}); + Map user = r.readEntity(new GenericType>(){}); this.userId4Test = (String) user.get("id"); } @@ -115,9 +114,8 @@ public void testVerify() { assertJsonContains(result, "user_id"); assertJsonContains(result, "user_name"); - Map user = JsonUtil.fromJson( - result, - new TypeReference>(){}); + Map user = JsonUtil.fromJson(result, + new TypeReference>(){}); Assert.assertEquals(this.userId4Test, user.get("user_id")); Assert.assertEquals("test", user.get("user_name")); @@ -142,8 +140,7 @@ private Response createUser(String name, String password) { "\",\"user_email\":\"user1@baidu.com\"," + "\"user_phone\":\"123456789\",\"user_avatar\":\"image1" + ".jpg\"}"; - return this.client().post(USER_PATH, - String.format(user, name, password)); + return this.client().post(USER_PATH, String.format(user, name, password)); } private Response deleteUser(String id) { @@ -155,14 +152,12 @@ private Response login(String name, String password) { String loginUser = "{\"user_name\":\"%s\"," + "\"user_password\":\"%s\"}"; - return client().post(login, String.format(loginUser, - name, password)); + return client().post(login, String.format(loginUser, name, password)); } private String tokenFromResponse(String content) { - Map data = JsonUtil.fromJson( - content, - new TypeReference>(){}); + Map data = JsonUtil.fromJson(content, + new TypeReference>(){}); return (String) data.get("token"); } } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/MetricsApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/MetricsApiTest.java index cce5af30cc..86759483c2 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/MetricsApiTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/MetricsApiTest.java @@ -27,14 +27,14 @@ public class MetricsApiTest extends BaseApiTest { - private static final String path = "/metrics"; - private static final String statisticsPath = path + "/statistics"; + private static final String PATH = "/metrics"; + private static final String STATISTICS_PATH = PATH + "/statistics"; @Test public void testBaseMetricsAll() { Map params = new HashMap<>(); params.put("type", "json"); - Response r = client().get(path, params); + Response r = client().get(PATH, params); String result = assertResponseStatus(200, r); assertJsonContains(result, "gauges"); assertJsonContains(result, "counters"); @@ -45,7 +45,7 @@ public void testBaseMetricsAll() { @Test public void testBaseMetricsPromAll() { - Response r = client().get(path); + Response r = client().get(PATH); assertResponseStatus(200, r); } @@ -53,19 +53,19 @@ public void testBaseMetricsPromAll() { public void testStatisticsMetricsAll() { Map params = new HashMap<>(); params.put("type", "json"); - Response r = client().get(path); + Response r = client().get(PATH); assertResponseStatus(200, r); } @Test public void testStatisticsMetricsPromAll() { - Response r = client().get(statisticsPath); + Response r = client().get(STATISTICS_PATH); assertResponseStatus(200, r); } @Test public void testMetricsSystem() { - Response r = client().get(path, "system"); + Response r = client().get(PATH, "system"); String result = assertResponseStatus(200, r); assertJsonContains(result, "basic"); assertJsonContains(result, "heap"); @@ -77,7 +77,7 @@ public void testMetricsSystem() { @Test public void testMetricsBackend() { - Response r = client().get(path, "backend"); + Response r = client().get(PATH, "backend"); String result = assertResponseStatus(200, r); Object value = assertJsonContains(result, "hugegraph"); @@ -125,10 +125,8 @@ public void testMetricsBackend() { for (Map.Entry e : servers.entrySet()) { String key = (String) e.getKey(); value = e.getValue(); - Assert.assertTrue(String.format( - "Expect map value for key %s but got %s", - key, value), - value instanceof Map); + Assert.assertTrue(String.format("Expect map value for key %s but got %s", + key, value), value instanceof Map); host = (Map) value; assertMapContains(host, "mem_max"); assertMapContains(host, "mem_committed"); @@ -188,10 +186,8 @@ public void testMetricsBackend() { for (Map.Entry e : servers.entrySet()) { String key = (String) e.getKey(); value = e.getValue(); - Assert.assertTrue(String.format( - "Expect map value for key %s but got %s", - key, value), - value instanceof Map); + Assert.assertTrue(String.format("Expect map value for key %s but got %s", + key, value), value instanceof Map); host = (Map) value; assertMapContains(host, "mem_max"); assertMapContains(host, "mem_committed"); @@ -257,10 +253,8 @@ public void testMetricsBackend() { for (Map.Entry e : servers.entrySet()) { String key = (String) e.getKey(); value = e.getValue(); - Assert.assertTrue(String.format( - "Expect map value for key %s but got %s", - key, value), - value instanceof Map); + Assert.assertTrue(String.format("Expect map value for key %s but got %s", + key, value), value instanceof Map); Map regionServer = (Map) value; assertMapContains(regionServer, "mem_max"); assertMapContains(regionServer, "mem_used"); @@ -275,8 +269,7 @@ public void testMetricsBackend() { assertMapContains(regionServer, "request_count_per_second"); assertMapContains(regionServer, "coprocessor_names"); - Map regions = assertMapContains(regionServer, - "regions"); + Map regions = assertMapContains(regionServer, "regions"); Assert.assertGte(1, regions.size()); for (Map.Entry e2 : regions.entrySet()) { Map region = (Map) e2.getValue(); @@ -307,7 +300,7 @@ public void testMetricsBackend() { } break; default: - Assert.assertTrue("Unexpected backend " + backend, false); + Assert.fail("Unexpected backend " + backend); break; } } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ProjectApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ProjectApiTest.java index 3f4239dfc6..1d372f0c5f 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ProjectApiTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/ProjectApiTest.java @@ -20,17 +20,18 @@ import java.util.List; import java.util.Map; -import jakarta.ws.rs.client.Entity; -import jakarta.ws.rs.core.Response; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; +import org.apache.hugegraph.util.JsonUtil; import org.junit.After; import org.junit.Assert; import org.junit.Test; -import org.apache.hugegraph.util.JsonUtil; import com.google.common.collect.ImmutableMap; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.core.Response; + public class ProjectApiTest extends BaseApiTest { private static final String PATH = "graphs/hugegraph/auth/projects"; @@ -45,9 +46,9 @@ public void teardown() throws Exception { @SuppressWarnings("unchecked") Map projectMap = ((Map) project); String projectId = (String) projectMap.get("id"); - // remove graphs from project if needed + // remove graphs from a project if needed List projectGraphs = (List) projectMap.get("project_graphs"); - if (projectGraphs != null && projectGraphs.size() > 0) { + if (projectGraphs != null && !projectGraphs.isEmpty()) { Map graphs = ImmutableMap.of("project_graphs", projectGraphs); resp = client().target() @@ -70,9 +71,9 @@ public void teardown() throws Exception { @Test public void testCreate() { - String project = String.format("{\"project_name\": \"test_project\"," + - "\"project_description\": " + - "\"this is a good project\"}"); + String project = "{\"project_name\": \"test_project\"," + + "\"project_description\": " + + "\"this is a good project\"}"; Response resp = client().post(PATH, project); String respBody = assertResponseStatus(201, resp); String projectName = assertJsonContains(respBody, "project_name"); @@ -256,7 +257,6 @@ private String getProject(String projectId) { .path(projectId) .request() .get(); - String respBody = assertResponseStatus(200, resp); - return respBody; + return assertResponseStatus(200, resp); } } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/UserApiTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/UserApiTest.java index e2759b6368..ff457242b0 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/UserApiTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/api/UserApiTest.java @@ -20,16 +20,17 @@ import java.util.List; import java.util.Map; -import jakarta.ws.rs.core.Response; +import org.apache.hugegraph.util.JsonUtil; import org.apache.tinkerpop.shaded.jackson.core.type.TypeReference; import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.Assert; import org.junit.Test; -import org.apache.hugegraph.util.JsonUtil; import com.google.common.collect.ImmutableMap; +import jakarta.ws.rs.core.Response; + public class UserApiTest extends BaseApiTest { private static final String PATH = "graphs/hugegraph/auth/users"; @@ -39,16 +40,14 @@ public class UserApiTest extends BaseApiTest { @After public void teardown() throws Exception { super.teardown(); - Response r = this.client().get(PATH, - ImmutableMap.of("limit", NO_LIMIT)); + Response r = this.client().get(PATH, ImmutableMap.of("limit", NO_LIMIT)); String result = r.readEntity(String.class); Map>> resultMap = - JsonUtil.fromJson(result, - new TypeReference>>>() {}); + JsonUtil.fromJson(result, + new TypeReference>>>() {}); List> users = resultMap.get("users"); for (Map user : users) { - if (user.get("user_name").equals("admin")) { + if ("admin".equals(user.get("user_name"))) { continue; } this.client().delete(PATH, (String) user.get("id")); @@ -124,7 +123,7 @@ public void testUpdate() { createUser("test2"); List> users = listUsers(); for (Map user : users) { - if (user.get("user_name").equals("admin")) { + if ("admin".equals(user.get("user_name"))) { continue; } String user1 = "{\"user_password\":\"p1\"," + @@ -146,7 +145,7 @@ public void testDelete() { List> users = listUsers(); for (Map user : users) { - if (user.get("user_name").equals("admin")) { + if ("admin".equals(user.get("user_name"))) { continue; } Response r = client().delete(PATH, (String) user.get("id")); @@ -169,13 +168,12 @@ protected void createUser(String name) { } protected List> listUsers() { - Response r = this.client().get(PATH, ImmutableMap.of("limit", - NO_LIMIT)); + Response r = this.client().get(PATH, ImmutableMap.of("limit", NO_LIMIT)); String result = assertResponseStatus(200, r); Map>> resultMap = - JsonUtil.fromJson(result, new TypeReference>>>() {}); + JsonUtil.fromJson(result, + new TypeReference>>>() {}); return resultMap.get("users"); } } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java index d72269a4f5..eb1fb6ad3b 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java @@ -17,27 +17,12 @@ package org.apache.hugegraph.unit; -import org.apache.hugegraph.unit.cassandra.CassandraTest; -import org.apache.hugegraph.unit.id.EdgeIdTest; -import org.apache.hugegraph.unit.id.IdTest; -import org.apache.hugegraph.unit.id.IdUtilTest; -import org.apache.hugegraph.unit.id.SplicingIdGeneratorTest; -import org.apache.hugegraph.unit.mysql.MysqlUtilTest; -import org.apache.hugegraph.unit.mysql.WhereBuilderTest; -import org.apache.hugegraph.unit.rocksdb.RocksDBCountersTest; -import org.apache.hugegraph.unit.rocksdb.RocksDBSessionTest; -import org.apache.hugegraph.unit.rocksdb.RocksDBSessionsTest; -import org.apache.hugegraph.unit.store.RamIntObjectMapTest; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -import org.apache.hugegraph.unit.core.SystemSchemaStoreTest; -import org.apache.hugegraph.unit.util.RateLimiterTest; import org.apache.hugegraph.unit.cache.CacheManagerTest; import org.apache.hugegraph.unit.cache.CacheTest; import org.apache.hugegraph.unit.cache.CachedGraphTransactionTest; import org.apache.hugegraph.unit.cache.CachedSchemaTransactionTest; import org.apache.hugegraph.unit.cache.RamTableTest; +import org.apache.hugegraph.unit.cassandra.CassandraTest; import org.apache.hugegraph.unit.core.AnalyzerTest; import org.apache.hugegraph.unit.core.BackendMutationTest; import org.apache.hugegraph.unit.core.BackendStoreInfoTest; @@ -54,7 +39,17 @@ import org.apache.hugegraph.unit.core.RowLockTest; import org.apache.hugegraph.unit.core.SecurityManagerTest; import org.apache.hugegraph.unit.core.SerialEnumTest; +import org.apache.hugegraph.unit.core.SystemSchemaStoreTest; import org.apache.hugegraph.unit.core.TraversalUtilTest; +import org.apache.hugegraph.unit.id.EdgeIdTest; +import org.apache.hugegraph.unit.id.IdTest; +import org.apache.hugegraph.unit.id.IdUtilTest; +import org.apache.hugegraph.unit.id.SplicingIdGeneratorTest; +import org.apache.hugegraph.unit.mysql.MysqlUtilTest; +import org.apache.hugegraph.unit.mysql.WhereBuilderTest; +import org.apache.hugegraph.unit.rocksdb.RocksDBCountersTest; +import org.apache.hugegraph.unit.rocksdb.RocksDBSessionTest; +import org.apache.hugegraph.unit.rocksdb.RocksDBSessionsTest; import org.apache.hugegraph.unit.serializer.BinaryBackendEntryTest; import org.apache.hugegraph.unit.serializer.BinaryScatterSerializerTest; import org.apache.hugegraph.unit.serializer.BinarySerializerTest; @@ -63,8 +58,10 @@ import org.apache.hugegraph.unit.serializer.StoreSerializerTest; import org.apache.hugegraph.unit.serializer.TableBackendEntryTest; import org.apache.hugegraph.unit.serializer.TextBackendEntryTest; +import org.apache.hugegraph.unit.store.RamIntObjectMapTest; import org.apache.hugegraph.unit.util.CompressUtilTest; import org.apache.hugegraph.unit.util.JsonUtilTest; +import org.apache.hugegraph.unit.util.RateLimiterTest; import org.apache.hugegraph.unit.util.StringEncodingTest; import org.apache.hugegraph.unit.util.VersionTest; import org.apache.hugegraph.unit.util.collection.CollectionFactoryTest; @@ -73,6 +70,8 @@ import org.apache.hugegraph.unit.util.collection.IntMapTest; import org.apache.hugegraph.unit.util.collection.IntSetTest; import org.apache.hugegraph.unit.util.collection.ObjectIntMappingTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ From c0d56b1ec98c60a38b88a1821e4171b2550f0768 Mon Sep 17 00:00:00 2001 From: imbajin Date: Mon, 13 Nov 2023 12:54:48 +0800 Subject: [PATCH 4/4] tiny fix --- .../org/apache/hugegraph/api/filter/AccessLogFilter.java | 2 +- .../java/org/apache/hugegraph/metrics/SlowQueryLog.java | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java index dd16eb3291..0b864e4b22 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java @@ -107,7 +107,7 @@ public void filter(ContainerRequestContext requestContext, HugeConfig config = configProvider.get(); long timeThreshold = config.get(ServerOptions.SLOW_QUERY_LOG_TIME_THRESHOLD); // Record slow query if meet needs, watch out the perf - if (timeThreshold > 0 && timeThreshold < executeTime && + if (timeThreshold > 0 && executeTime > timeThreshold && needRecordLog(requestContext)) { // TODO: set RequestBody null, handle it later & should record "client IP" LOG.info("[Slow Query] execTime={}ms, body={}, method={}, path={}, query={}", diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java index f4707800cc..e55316c39e 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/metrics/SlowQueryLog.java @@ -16,7 +16,6 @@ */ package org.apache.hugegraph.metrics; - public class SlowQueryLog { public String rawQuery; @@ -29,16 +28,16 @@ public class SlowQueryLog { public long startTime; - public long threshold; + public long thresholdTime; public SlowQueryLog(String rawQuery, String method, String path, - long executeTime, long startTime, long threshold) { + long executeTime, long startTime, long thresholdTime) { this.rawQuery = rawQuery; this.method = method; this.path = path; this.executeTime = executeTime; this.startTime = startTime; - this.threshold = threshold; + this.thresholdTime = thresholdTime; } @Override @@ -47,7 +46,7 @@ public String toString() { ", startTime=" + startTime + ", rawQuery='" + rawQuery + '\'' + ", method='" + method + '\'' + - ", threshold=" + threshold + + ", threshold=" + thresholdTime + ", path='" + path + '\'' + '}'; }