-
Notifications
You must be signed in to change notification settings - Fork 597
optimize(api): add warning logs for load-based request rejection #2972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
imbajin
merged 6 commits into
apache:master
from
contrueCT:fix/issue-2747-load-detect-logs
Apr 25, 2026
+477
−20
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f24f3fa
fix(api): add load rejection warning logs
contrueCT c72f2f4
fix(api): style check
contrueCT 38d8a66
fix(filter): enhance load detection logging and memory management
contrueCT d5d7ce0
fix(api): harden load rejection logging
contrueCT 2df723b
test(api): stabilize load rejection log coverage
contrueCT 0463c68
fix(api): refine low-memory rejection handling
contrueCT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| import org.apache.hugegraph.define.WorkLoad; | ||
| import org.apache.hugegraph.util.Bytes; | ||
| import org.apache.hugegraph.util.E; | ||
| import org.apache.hugegraph.util.Log; | ||
| import org.slf4j.Logger; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import com.google.common.util.concurrent.RateLimiter; | ||
|
|
@@ -43,6 +45,8 @@ | |
| @PreMatching | ||
| public class LoadDetectFilter implements ContainerRequestFilter { | ||
|
|
||
| private static final Logger LOG = Log.logger(LoadDetectFilter.class); | ||
|
|
||
| private static final Set<String> WHITE_API_LIST = ImmutableSet.of( | ||
| "", | ||
| "apis", | ||
|
|
@@ -54,11 +58,30 @@ public class LoadDetectFilter implements ContainerRequestFilter { | |
| private static final RateLimiter GC_RATE_LIMITER = | ||
| RateLimiter.create(1.0 / 30); | ||
|
|
||
| // Log at most 1 request per second to avoid too many logs when server is under heavy load | ||
| private static final RateLimiter REJECT_LOG_RATE_LIMITER = RateLimiter.create(1.0); | ||
|
|
||
| @Context | ||
| private jakarta.inject.Provider<HugeConfig> configProvider; | ||
| @Context | ||
| private jakarta.inject.Provider<WorkLoad> loadProvider; | ||
|
|
||
| public static boolean isWhiteAPI(ContainerRequestContext context) { | ||
| List<PathSegment> segments = context.getUriInfo().getPathSegments(); | ||
| E.checkArgument(!segments.isEmpty(), "Invalid request uri '%s'", | ||
| context.getUriInfo().getPath()); | ||
| String rootPath = segments.get(0).getPath(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
原方法是
建议保持
同样的建议也适用于 |
||
| return WHITE_API_LIST.contains(rootPath); | ||
| } | ||
|
|
||
| private static boolean gcIfNeeded() { | ||
| if (GC_RATE_LIMITER.tryAcquire(1)) { | ||
| System.gc(); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void filter(ContainerRequestContext context) { | ||
| if (LoadDetectFilter.isWhiteAPI(context)) { | ||
|
|
@@ -70,7 +93,14 @@ public void filter(ContainerRequestContext context) { | |
| int maxWorkerThreads = config.get(ServerOptions.MAX_WORKER_THREADS); | ||
| WorkLoad load = this.loadProvider.get(); | ||
| // There will be a thread doesn't work, dedicated to statistics | ||
| if (load.incrementAndGet() >= maxWorkerThreads) { | ||
| int currentLoad = load.incrementAndGet(); | ||
| if (currentLoad >= maxWorkerThreads) { | ||
| if (REJECT_LOG_RATE_LIMITER.tryAcquire()) { | ||
| LOG.warn("Rejected request due to high worker load, method={}, path={}, " + | ||
| "currentLoad={}, maxWorkerThreads={}", | ||
| context.getMethod(), context.getUriInfo().getPath(), | ||
|
contrueCT marked this conversation as resolved.
|
||
| currentLoad, maxWorkerThreads); | ||
| } | ||
| throw new ServiceUnavailableException(String.format( | ||
| "The server is too busy to process the request, " + | ||
| "you can config %s to adjust it or try again later", | ||
|
|
@@ -83,7 +113,17 @@ public void filter(ContainerRequestContext context) { | |
| long presumableFreeMem = (Runtime.getRuntime().maxMemory() - | ||
| allocatedMem) / Bytes.MB; | ||
| if (presumableFreeMem < minFreeMemory) { | ||
| gcIfNeeded(); | ||
| boolean gcTriggered = gcIfNeeded(); | ||
| long allocatedMemAfterCheck = Runtime.getRuntime().totalMemory() - | ||
| Runtime.getRuntime().freeMemory(); | ||
| long recheckedFreeMem = (Runtime.getRuntime().maxMemory() - | ||
| allocatedMemAfterCheck) / Bytes.MB; | ||
| LOG.warn("Rejected request due to low free memory, method={}, path={}, " + | ||
|
contrueCT marked this conversation as resolved.
Outdated
contrueCT marked this conversation as resolved.
Outdated
|
||
| "presumableFreeMemMB={}, recheckedFreeMemMB={}, gcTriggered={}, " + | ||
| "minFreeMemoryMB={}", | ||
| context.getMethod(), context.getUriInfo().getPath(), | ||
| presumableFreeMem, recheckedFreeMem, gcTriggered, | ||
| minFreeMemory); | ||
|
contrueCT marked this conversation as resolved.
Outdated
|
||
| throw new ServiceUnavailableException(String.format( | ||
| "The server available memory %s(MB) is below than " + | ||
| "threshold %s(MB) and can't process the request, " + | ||
|
|
@@ -92,18 +132,4 @@ public void filter(ContainerRequestContext context) { | |
| ServerOptions.MIN_FREE_MEMORY.name())); | ||
| } | ||
| } | ||
|
|
||
| public static boolean isWhiteAPI(ContainerRequestContext context) { | ||
| List<PathSegment> segments = context.getUriInfo().getPathSegments(); | ||
| E.checkArgument(!segments.isEmpty(), "Invalid request uri '%s'", | ||
| context.getUriInfo().getPath()); | ||
| String rootPath = segments.get(0).getPath(); | ||
| return WHITE_API_LIST.contains(rootPath); | ||
| } | ||
|
|
||
| private static void gcIfNeeded() { | ||
| if (GC_RATE_LIMITER.tryAcquire(1)) { | ||
| System.gc(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...gegraph-test/src/main/java/org/apache/hugegraph/unit/api/filter/LoadDetectFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.hugegraph.unit.api.filter; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.commons.configuration2.Configuration; | ||
| import org.apache.commons.configuration2.PropertiesConfiguration; | ||
| import org.apache.hugegraph.api.filter.LoadDetectFilter; | ||
| import org.apache.hugegraph.config.HugeConfig; | ||
| import org.apache.hugegraph.config.ServerOptions; | ||
| import org.apache.hugegraph.define.WorkLoad; | ||
| import org.apache.hugegraph.testutil.Assert; | ||
| import org.apache.hugegraph.testutil.Whitebox; | ||
| import org.apache.hugegraph.unit.BaseUnitTest; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import jakarta.inject.Provider; | ||
| import jakarta.ws.rs.ServiceUnavailableException; | ||
| import jakarta.ws.rs.container.ContainerRequestContext; | ||
| import jakarta.ws.rs.core.PathSegment; | ||
| import jakarta.ws.rs.core.UriInfo; | ||
|
|
||
| public class LoadDetectFilterTest extends BaseUnitTest { | ||
|
|
||
| private LoadDetectFilter loadDetectFilter; | ||
| private ContainerRequestContext requestContext; | ||
| private UriInfo uriInfo; | ||
| private WorkLoad workLoad; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| this.requestContext = Mockito.mock(ContainerRequestContext.class); | ||
| this.uriInfo = Mockito.mock(UriInfo.class); | ||
| this.workLoad = new WorkLoad(); | ||
|
|
||
| Mockito.when(this.requestContext.getUriInfo()).thenReturn(this.uriInfo); | ||
| Mockito.when(this.requestContext.getMethod()).thenReturn("GET"); | ||
|
|
||
| this.loadDetectFilter = new LoadDetectFilter(); | ||
| this.setLoadProvider(this.workLoad); | ||
| this.setConfigProvider(createConfig(8, 0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFilter_WhiteListPathIgnored() { | ||
| setupPath("", List.of("")); | ||
| this.workLoad.incrementAndGet(); | ||
|
|
||
| this.loadDetectFilter.filter(this.requestContext); | ||
|
|
||
| Assert.assertEquals(1, this.workLoad.get().get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFilter_RejectsWhenWorkerLoadIsTooHigh() { | ||
| setupPath("graphs/hugegraph/vertices", | ||
| List.of("graphs", "hugegraph", "vertices")); | ||
| this.setConfigProvider(createConfig(2, 0)); | ||
| this.workLoad.incrementAndGet(); | ||
|
|
||
| ServiceUnavailableException exception = (ServiceUnavailableException) Assert.assertThrows( | ||
| ServiceUnavailableException.class, | ||
| () -> this.loadDetectFilter.filter(this.requestContext)); | ||
|
|
||
| Assert.assertContains("The server is too busy to process the request", | ||
| exception.getMessage()); | ||
| Assert.assertContains(ServerOptions.MAX_WORKER_THREADS.name(), | ||
| exception.getMessage()); | ||
| } | ||
|
contrueCT marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| public void testFilter_RejectsWhenFreeMemoryIsTooLow() { | ||
| setupPath("graphs/hugegraph/vertices", | ||
| List.of("graphs", "hugegraph", "vertices")); | ||
| this.setConfigProvider(createConfig(8, Integer.MAX_VALUE)); | ||
|
|
||
| ServiceUnavailableException exception = (ServiceUnavailableException) Assert.assertThrows( | ||
|
contrueCT marked this conversation as resolved.
|
||
| ServiceUnavailableException.class, | ||
| () -> this.loadDetectFilter.filter(this.requestContext)); | ||
|
|
||
|
contrueCT marked this conversation as resolved.
|
||
| Assert.assertContains("The server available memory", | ||
| exception.getMessage()); | ||
| Assert.assertContains(ServerOptions.MIN_FREE_MEMORY.name(), | ||
| exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFilter_AllowsRequestWhenLoadAndMemoryAreHealthy() { | ||
| setupPath("graphs/hugegraph/vertices", | ||
| List.of("graphs", "hugegraph", "vertices")); | ||
| this.setConfigProvider(createConfig(8, 0)); | ||
|
|
||
| this.loadDetectFilter.filter(this.requestContext); | ||
|
|
||
| Assert.assertEquals(1, this.workLoad.get().get()); | ||
| } | ||
|
|
||
| private HugeConfig createConfig(int maxWorkerThreads, int minFreeMemory) { | ||
| Configuration conf = new PropertiesConfiguration(); | ||
| conf.setProperty(ServerOptions.MAX_WORKER_THREADS.name(), maxWorkerThreads); | ||
| conf.setProperty(ServerOptions.MIN_FREE_MEMORY.name(), minFreeMemory); | ||
| return new HugeConfig(conf); | ||
| } | ||
|
|
||
| private void setupPath(String path, List<String> segments) { | ||
| List<PathSegment> pathSegments = segments.stream() | ||
| .map(this::createPathSegment) | ||
| .collect(Collectors.toList()); | ||
| Mockito.when(this.uriInfo.getPath()).thenReturn(path); | ||
| Mockito.when(this.uriInfo.getPathSegments()).thenReturn(pathSegments); | ||
| } | ||
|
|
||
| private PathSegment createPathSegment(String path) { | ||
| PathSegment segment = Mockito.mock(PathSegment.class); | ||
| Mockito.when(segment.getPath()).thenReturn(path); | ||
| return segment; | ||
| } | ||
|
|
||
| private void setLoadProvider(WorkLoad workLoad) { | ||
| Whitebox.setInternalState(this.loadDetectFilter, "loadProvider", | ||
| (Provider<WorkLoad>) () -> workLoad); | ||
| } | ||
|
|
||
| private void setConfigProvider(HugeConfig config) { | ||
| Whitebox.setInternalState(this.loadDetectFilter, "configProvider", | ||
| (Provider<HugeConfig>) () -> config); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REJECT_LOG_RATE_LIMITER高负载拒绝和低内存拒绝共享同一个 RateLimiter(每秒 1 个 permit)。这意味着:
建议为两种拒绝原因使用独立的 RateLimiter: