Copilot commented on code in PR #2972:
URL: https://github.com/apache/hugegraph/pull/2972#discussion_r2978779898
##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/LoadDetectFilter.java:
##########
@@ -83,7 +121,17 @@ public void filter(ContainerRequestContext context) {
long presumableFreeMem = (Runtime.getRuntime().maxMemory() -
allocatedMem) / Bytes.MB;
if (presumableFreeMem < minFreeMemory) {
- gcIfNeeded();
+ boolean gcTriggered = this.gcIfNeeded();
+ long allocatedMemAfterCheck = Runtime.getRuntime().totalMemory() -
+ Runtime.getRuntime().freeMemory();
+ long recheckedFreeMem = (Runtime.getRuntime().maxMemory() -
+ allocatedMemAfterCheck) / Bytes.MB;
+ this.logRejectWarning("Rejected request due to low free memory,
method={}, path={}, " +
+ "presumableFreeMemMB={},
recheckedFreeMemMB={}, gcTriggered={}, " +
+ "minFreeMemoryMB={}",
+ context.getMethod(),
context.getUriInfo().getPath(),
+ presumableFreeMem, recheckedFreeMem,
gcTriggered,
+ minFreeMemory);
Review Comment:
In the low-free-memory rejection path, `allocatedMemAfterCheck` /
`recheckedFreeMem` are computed on every rejected request, even when the reject
log is suppressed by `REJECT_LOG_RATE_LIMITER` (since `allowRejectLog()` is
only checked inside `logRejectWarning()`). To keep the rate-limiting effective
under overload/low-memory conditions, consider checking `allowRejectLog()`
first (or having `logRejectWarning()` return a boolean) and only doing the
recheck computations when a log will actually be emitted.
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/filter/LoadDetectFilterTest.java:
##########
@@ -0,0 +1,272 @@
+/*
+ * 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.io.Serializable;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+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.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.After;
+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 static final Logger TEST_LOGGER =
+ (Logger) LogManager.getLogger(LoadDetectFilter.class);
+
Review Comment:
`TEST_LOGGER` is obtained by casting
`LogManager.getLogger(LoadDetectFilter.class)` to
`org.apache.logging.log4j.core.Logger`. With the test `log4j2.xml` using
`<AsyncLogger name="org.apache.hugegraph" ...>`, this logger instance can be an
`AsyncLogger`, so this cast (and `addAppender/removeAppender`) can fail with
`ClassCastException` or not attach the appender as intended. Consider attaching
the `TestAppender` via `LoggerContext`/`Configuration`/`LoggerConfig` (e.g.,
`config.getLoggerConfig(loggerName).addAppender(...)` + `ctx.updateLoggers()`),
and remove it the same way in teardown.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]