Copilot commented on code in PR #2972:
URL: https://github.com/apache/hugegraph/pull/2972#discussion_r2973031366


##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/LoadDetectFilter.java:
##########
@@ -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={}, " +
+                     "presumableFreeMemMB={}, recheckedFreeMemMB={}, 
gcTriggered={}, " +
+                     "minFreeMemoryMB={}",
+                     context.getMethod(), context.getUriInfo().getPath(),
+                     presumableFreeMem, recheckedFreeMem, gcTriggered,
+                     minFreeMemory);

Review Comment:
   The low-memory rejection path logs a WARN for every rejected request (no 
sampling/RateLimiter), which can still create a log storm under memory pressure 
and further degrade the server. Consider applying the same rate limiting as the 
busy-worker rejection (either reuse `REJECT_LOG_RATE_LIMITER` here or introduce 
a dedicated limiter for low-memory logs) so rejection logging remains bounded.
   ```suggestion
               if (REJECT_LOG_RATE_LIMITER.tryAcquire()) {
                   LOG.warn("Rejected request due to low free memory, 
method={}, path={}, " +
                            "presumableFreeMemMB={}, recheckedFreeMemMB={}, 
gcTriggered={}, " +
                            "minFreeMemoryMB={}",
                            context.getMethod(), context.getUriInfo().getPath(),
                            presumableFreeMem, recheckedFreeMem, gcTriggered,
                            minFreeMemory);
               }
   ```



##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/filter/LoadDetectFilterTest.java:
##########
@@ -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());
+    }
+
+    @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(
+                ServiceUnavailableException.class,
+                () -> this.loadDetectFilter.filter(this.requestContext));
+

Review Comment:
   `testFilter_RejectsWhenFreeMemoryIsTooLow()` drives the code path that (on 
first hit) will typically call `System.gc()` via `gcIfNeeded()`. Triggering a 
real GC in unit tests can slow down or introduce variability in CI runs; 
consider disabling GC for this test by temporarily overriding the 
`GC_RATE_LIMITER` (via reflection/Whitebox) so `tryAcquire()` can’t succeed, or 
by refactoring `gcIfNeeded()` to be injectable/mocked in tests.



-- 
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]

Reply via email to