-
Notifications
You must be signed in to change notification settings - Fork 596
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
base: master
Are you sure you want to change the base?
Changes from all commits
f24f3fa
c72f2f4
38d8a66
d5d7ce0
2df723b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,40 @@ 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); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| protected boolean gcIfNeeded() { | ||||||||||||||||||||||||||||||||||||||||||||
| if (GC_RATE_LIMITER.tryAcquire(1)) { | ||||||||||||||||||||||||||||||||||||||||||||
| System.gc(); | ||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| protected boolean allowRejectLog() { | ||||||||||||||||||||||||||||||||||||||||||||
| return REJECT_LOG_RATE_LIMITER.tryAcquire(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| protected void logRejectWarning(String message, Object... args) { | ||||||||||||||||||||||||||||||||||||||||||||
|
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.
高负载路径使用封装的 // 高负载路径
this.logRejectWarning("Rejected request due to high worker load...");
// 低内存路径
boolean shouldLog = this.allowRejectLog();
if (shouldLog) { LOG.warn(...); }两条路径的日志方式不一致,降低了可读性。建议统一为同一种模式。另外 |
||||||||||||||||||||||||||||||||||||||||||||
| if (this.allowRejectLog()) { | ||||||||||||||||||||||||||||||||||||||||||||
| LOG.warn(message, args); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||
| public void filter(ContainerRequestContext context) { | ||||||||||||||||||||||||||||||||||||||||||||
| if (LoadDetectFilter.isWhiteAPI(context)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -70,7 +103,12 @@ 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) { | ||||||||||||||||||||||||||||||||||||||||||||
| this.logRejectWarning("Rejected request due to high worker load, method={}, path={}, " + | ||||||||||||||||||||||||||||||||||||||||||||
| "currentLoad={}, maxWorkerThreads={}", | ||||||||||||||||||||||||||||||||||||||||||||
| context.getMethod(), context.getUriInfo().getPath(), | ||||||||||||||||||||||||||||||||||||||||||||
| 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 +121,20 @@ public void filter(ContainerRequestContext context) { | |||||||||||||||||||||||||||||||||||||||||||
| long presumableFreeMem = (Runtime.getRuntime().maxMemory() - | ||||||||||||||||||||||||||||||||||||||||||||
| allocatedMem) / Bytes.MB; | ||||||||||||||||||||||||||||||||||||||||||||
| if (presumableFreeMem < minFreeMemory) { | ||||||||||||||||||||||||||||||||||||||||||||
| gcIfNeeded(); | ||||||||||||||||||||||||||||||||||||||||||||
| boolean shouldLog = this.allowRejectLog(); | ||||||||||||||||||||||||||||||||||||||||||||
| boolean gcTriggered = this.gcIfNeeded(); | ||||||||||||||||||||||||||||||||||||||||||||
| if (shouldLog) { | ||||||||||||||||||||||||||||||||||||||||||||
| long allocatedMemAfterCheck = Runtime.getRuntime().totalMemory() - | ||||||||||||||||||||||||||||||||||||||||||||
|
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. 🧹 当 建议只在 |
||||||||||||||||||||||||||||||||||||||||||||
| Runtime.getRuntime().freeMemory(); | ||||||||||||||||||||||||||||||||||||||||||||
| long recheckedFreeMem = (Runtime.getRuntime().maxMemory() - | ||||||||||||||||||||||||||||||||||||||||||||
| allocatedMemAfterCheck) / Bytes.MB; | ||||||||||||||||||||||||||||||||||||||||||||
| LOG.warn("Rejected request due to low free memory, method={}, path={}, " + | ||||||||||||||||||||||||||||||||||||||||||||
| "presumableFreeMemMB={}, recheckedFreeMemMB={}, gcTriggered={}, " + | ||||||||||||||||||||||||||||||||||||||||||||
| "minFreeMemoryMB={}", | ||||||||||||||||||||||||||||||||||||||||||||
| context.getMethod(), context.getUriInfo().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. 当前逻辑在 GC 后直接抛出异常,并没有重新检查内存是否已恢复到阈值以上。既然已经在这里做了 当前行为:检测到内存不足 → 尝试 GC → 记录 GC 后的内存 → 仍然抛异常
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| presumableFreeMem, recheckedFreeMem, gcTriggered, | ||||||||||||||||||||||||||||||||||||||||||||
| minFreeMemory); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| 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 +143,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(); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
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: