Skip to content

Commit

Permalink
Ensure that mock transport does not swallow req
Browse files Browse the repository at this point in the history
Currently it is possible for the MockTransportService distrupt behavior
to swallow requests if either the connection is already closed (in which
case response pruning has already occurred) or if the behavior is added
after the clear callback has been triggered.
  • Loading branch information
Tim-Brooks committed May 15, 2024
1 parent 920290a commit f33f236
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.elasticsearch.transport.ClusterConnectionManager;
import org.elasticsearch.transport.ConnectTransportException;
import org.elasticsearch.transport.ConnectionProfile;
import org.elasticsearch.transport.NodeNotConnectedException;
import org.elasticsearch.transport.RequestHandlerRegistry;
import org.elasticsearch.transport.TcpTransport;
import org.elasticsearch.transport.Transport;
Expand All @@ -72,6 +73,7 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -428,6 +430,8 @@ public void addUnresponsiveRule(TransportAddress transportAddress) {
);

transport().addSendBehavior(transportAddress, new StubbableTransport.SendRequestBehavior() {

private final Semaphore sendSemaphore = new Semaphore(Integer.MAX_VALUE);
private final Set<Transport.Connection> toClose = ConcurrentHashMap.newKeySet();

@Override
Expand All @@ -437,18 +441,26 @@ public void sendRequest(
String action,
TransportRequest request,
TransportRequestOptions options
) {
// don't send anything, the receiving node is unresponsive
toClose.add(connection);
) throws IOException {
if (connection.isClosed()) {
throw new NodeNotConnectedException(connection.getNode(), "connection already closed");
} else if (sendSemaphore.tryAcquire()) {
// don't send anything, the receiving node is unresponsive
toClose.add(connection);
sendSemaphore.release();
} else {
connection.sendRequest(requestId, action, request, options);
}
}

@Override
public void clearCallback() {
// close to simulate that tcp-ip eventually times out and closes connection (necessary to ensure transport eventually
// responds).
try {
sendSemaphore.acquire(Integer.MAX_VALUE);
IOUtils.close(toClose);
} catch (IOException e) {
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
Expand Down

0 comments on commit f33f236

Please sign in to comment.