diff --git a/proxy/src/main/java/org/apache/rocketmq/proxy/remoting/RemotingProtocolServer.java b/proxy/src/main/java/org/apache/rocketmq/proxy/remoting/RemotingProtocolServer.java index c26f6bc2ef4..36cf859535e 100644 --- a/proxy/src/main/java/org/apache/rocketmq/proxy/remoting/RemotingProtocolServer.java +++ b/proxy/src/main/java/org/apache/rocketmq/proxy/remoting/RemotingProtocolServer.java @@ -387,11 +387,26 @@ protected void cleanExpiredRequestInQueue(ThreadPoolExecutor threadPoolExecutor, } else { break; } - } catch (Throwable ignored) { + } catch (Throwable t) { + log.warn("clean expired remoting request failed. queueSize:{}, maxWaitTimeMillsInQueue:{}", + safeQueueSize(threadPoolExecutor), maxWaitTimeMillsInQueue, t); + break; } } } + private int safeQueueSize(ThreadPoolExecutor threadPoolExecutor) { + try { + BlockingQueue queue = threadPoolExecutor == null ? null : threadPoolExecutor.getQueue(); + if (queue == null) { + return -1; + } + return queue.size(); + } catch (Throwable ignored) { + return -1; + } + } + private RequestTask castRunnable(final Runnable runnable) { try { if (runnable instanceof FutureTaskExt) { diff --git a/proxy/src/test/java/org/apache/rocketmq/proxy/remoting/RemotingProtocolServerTest.java b/proxy/src/test/java/org/apache/rocketmq/proxy/remoting/RemotingProtocolServerTest.java new file mode 100644 index 00000000000..2c5e307df7a --- /dev/null +++ b/proxy/src/test/java/org/apache/rocketmq/proxy/remoting/RemotingProtocolServerTest.java @@ -0,0 +1,36 @@ +/* + * 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.rocketmq.proxy.remoting; + +import java.util.concurrent.ThreadPoolExecutor; +import org.junit.Test; +import org.mockito.Mockito; + +public class RemotingProtocolServerTest { + + @Test(timeout = 1000) + public void testCleanExpiredRequestInQueueBreaksWhenQueueAccessFails() { + RemotingProtocolServer server = Mockito.mock(RemotingProtocolServer.class, Mockito.CALLS_REAL_METHODS); + ThreadPoolExecutor executor = Mockito.mock(ThreadPoolExecutor.class); + Mockito.when(executor.getQueue()).thenThrow(new RuntimeException("queue unavailable")); + + server.cleanExpiredRequestInQueue(executor, 1); + + Mockito.verify(executor, Mockito.atMost(2)).getQueue(); + } +}