Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.rocketmq.proxy.config.ConfigurationManager;
import org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;

public class InvocationChannel extends SimpleChannel {
Expand Down Expand Up @@ -68,6 +69,7 @@ public void clearExpireContext() {
Map.Entry<Integer, InvocationContextInterface> entry = iterator.next();
if (entry.getValue().expired(ConfigurationManager.getProxyConfig().getChannelExpiredInSeconds())) {
iterator.remove();
entry.getValue().expire(new RemotingTimeoutException("Invocation context expired. opaque=" + entry.getKey()));
count++;
log.debug("An expired request is found, request: {}", entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public CompletableFuture<RemotingCommand> getResponse() {
public void handle(RemotingCommand remotingCommand) {
response.complete(remotingCommand);
}

@Override
public void expire(Throwable throwable) {
response.completeExceptionally(throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ public interface InvocationContextInterface {
void handle(RemotingCommand remotingCommand);

boolean expired(long expiredTimeSec);

default void expire(Throwable throwable) {
}
Comment on lines 25 to +28
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,24 @@

package org.apache.rocketmq.proxy.service.channel;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.rocketmq.proxy.config.ConfigurationManager;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class InvocationChannelTest {

@BeforeClass
public static void setUp() throws Exception {
ConfigurationManager.initEnv();
ConfigurationManager.initConfig();
}

@Test
public void testWriteAndFlushShouldNotRemoveReRegisteredContext() {
InvocationChannel channel = new InvocationChannel("127.0.0.1:8080", "127.0.0.1:8081");
Expand Down Expand Up @@ -64,4 +72,22 @@ public boolean expired(long expiredTimeSec) {
assertTrue(nextContextHandled.get());
assertFalse(channel.isWritable());
}

@Test
public void testClearExpireContextShouldCompleteResponseFutureExceptionally() {
int originalExpiredInSeconds = ConfigurationManager.getProxyConfig().getChannelExpiredInSeconds();
ConfigurationManager.getProxyConfig().setChannelExpiredInSeconds(0);
try {
InvocationChannel channel = new InvocationChannel("127.0.0.1:8080", "127.0.0.1:8081");
CompletableFuture<RemotingCommand> responseFuture = new CompletableFuture<>();
channel.registerInvocationContext(1, new InvocationContext(responseFuture));

channel.clearExpireContext();

assertFalse(channel.isWritable());
assertTrue(responseFuture.isCompletedExceptionally());
} finally {
ConfigurationManager.getProxyConfig().setChannelExpiredInSeconds(originalExpiredInSeconds);
}
}
}
Loading