From 651e1743c593bd084451521c315ee14a8e31e783 Mon Sep 17 00:00:00 2001 From: Felix Li Date: Tue, 31 Oct 2023 13:56:35 +0800 Subject: [PATCH] [Enhancement] Add logs for slow interfaces (#33908) Signed-off-by: Astralidea (cherry picked from commit 6488726958751765dd4164af137d9c63993e9ec9) # Conflicts: # fe/fe-core/src/main/java/com/starrocks/http/rest/RestBaseAction.java # fe/fe-core/src/test/java/com/starrocks/http/ShowDataActionTest.java --- .../java/com/starrocks/common/Config.java | 6 ++ .../starrocks/http/rest/RestBaseAction.java | 11 ++++ .../starrocks/http/ShowDataActionTest.java | 64 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 fe/fe-core/src/test/java/com/starrocks/http/ShowDataActionTest.java diff --git a/fe/fe-core/src/main/java/com/starrocks/common/Config.java b/fe/fe-core/src/main/java/com/starrocks/common/Config.java index f4bcd8111268d..256272c30348e 100644 --- a/fe/fe-core/src/main/java/com/starrocks/common/Config.java +++ b/fe/fe-core/src/main/java/com/starrocks/common/Config.java @@ -509,6 +509,12 @@ public class Config extends ConfigBase { @ConfField public static int http_max_chunk_size = 8192; + /** + * If a request takes longer than the configured time, a log will be generated to trace it. + */ + @ConfField(mutable = true) + public static int http_slow_request_threshold_ms = 5000; + /** * When obtaining hardware information, some sensitive commands will be executed indirectly through * the oshi library, such as: getent passwd diff --git a/fe/fe-core/src/main/java/com/starrocks/http/rest/RestBaseAction.java b/fe/fe-core/src/main/java/com/starrocks/http/rest/RestBaseAction.java index bab12f91d61d9..f707bd515be50 100644 --- a/fe/fe-core/src/main/java/com/starrocks/http/rest/RestBaseAction.java +++ b/fe/fe-core/src/main/java/com/starrocks/http/rest/RestBaseAction.java @@ -22,7 +22,11 @@ package com.starrocks.http.rest; import com.fasterxml.jackson.databind.ObjectMapper; +<<<<<<< HEAD import com.starrocks.analysis.UserIdentity; +======= +import com.starrocks.common.Config; +>>>>>>> 6488726958 ([Enhancement] Add logs for slow interfaces (#33908)) import com.starrocks.common.DdlException; import com.starrocks.common.Pair; import com.starrocks.common.util.UUIDUtil; @@ -55,6 +59,7 @@ public RestBaseAction(ActionController controller) { @Override public void handleRequest(BaseRequest request) throws Exception { LOG.info("receive http request. url={}", request.getRequest().uri()); + long startTime = System.currentTimeMillis(); BaseResponse response = new BaseResponse(); try { execute(request, response); @@ -68,6 +73,12 @@ public void handleRequest(BaseRequest request) throws Exception { sendResult(request, response, new RestBaseResult(e.getMessage())); } } + long endTime = System.currentTimeMillis(); + long elapsedTime = endTime - startTime; + if (elapsedTime > Config.http_slow_request_threshold_ms) { + LOG.warn("Execution uri={} time exceeded {} ms and took {} ms.", request.getRequest().uri(), + Config.http_slow_request_threshold_ms, elapsedTime); + } } @Override diff --git a/fe/fe-core/src/test/java/com/starrocks/http/ShowDataActionTest.java b/fe/fe-core/src/test/java/com/starrocks/http/ShowDataActionTest.java new file mode 100644 index 0000000000000..e7a67fde5f1a6 --- /dev/null +++ b/fe/fe-core/src/test/java/com/starrocks/http/ShowDataActionTest.java @@ -0,0 +1,64 @@ +// Copyright 2021-present StarRocks, Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.starrocks.http; + +import com.starrocks.catalog.Database; +import com.starrocks.catalog.OlapTable; +import com.starrocks.common.Config; +import com.starrocks.server.GlobalStateMgr; +import okhttp3.Request; +import okhttp3.Response; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.concurrent.ConcurrentHashMap; + +import static org.junit.Assert.assertTrue; + +public class ShowDataActionTest extends StarRocksHttpTestCase { + + private static final String SHOW_DATA_URI = "/show_data"; + private static final String SHOW_DATA_DB_NAME = "showdatadb"; + private long expectedSize = 0; + + @Override + public void doSetUp() { + Database db = new Database(1000 + testDbId, SHOW_DATA_DB_NAME); + OlapTable table = newTable("ShowDataTable"); + db.registerTableUnlocked(table); + expectedSize = table.getDataSize(); + + // inject our test db + ConcurrentHashMap fullNameToDb = GlobalStateMgr.getCurrentState().getFullNameToDb(); + fullNameToDb.put(SHOW_DATA_DB_NAME, db); + } + + @Test + public void testGetShowData() throws IOException { + Config.http_slow_request_threshold_ms = 0; + Request request = new Request.Builder() + .get() + .addHeader("Authorization", rootAuth) + .url(BASE_URL + "/api" + SHOW_DATA_URI + "?db=" + SHOW_DATA_DB_NAME) + .build(); + Response response = networkClient.newCall(request).execute(); + assertTrue(response.isSuccessful()); + Assert.assertNotNull(response.body()); + String respStr = response.body().string(); + Assert.assertNotNull(respStr); + Assert.assertEquals(String.valueOf(expectedSize), respStr); + } +}