Add WARN Logging if Mock Network Accepts Huge Number of Connections (#44169) (#44182)

* Add WARN Logging if Mock Network Accepts Huge Number of Connections

* As discussed, added warn logging to rule out endless accept loops for #43387
* Had to handle it by the relatively awkward override in the mock nio
because we don't have logging in the NIO module where
(`ServerChannelContext` lives)
This commit is contained in:
Armin Braun 2019-07-10 22:08:36 +02:00 committed by GitHub
parent c6efb9be2a
commit d6f09fdb97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -67,6 +67,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap;
@ -228,12 +229,25 @@ public class MockNioTransport extends TcpTransport {
}
@Override
public MockServerChannel createServerChannel(NioSelector selector, ServerSocketChannel channel) throws IOException {
public MockServerChannel createServerChannel(NioSelector selector, ServerSocketChannel channel) {
MockServerChannel nioServerChannel = new MockServerChannel(profileName, channel);
Consumer<Exception> exceptionHandler = (e) -> logger.error(() ->
new ParameterizedMessage("exception from server channel caught on transport layer [{}]", channel), e);
ServerChannelContext context = new ServerChannelContext(nioServerChannel, this, selector, MockNioTransport.this::acceptChannel,
exceptionHandler);
ServerChannelContext context = new ServerChannelContext(nioServerChannel, null, selector, null,
exceptionHandler) {
@Override
public void acceptChannels(Supplier<NioSelector> selectorSupplier) throws IOException {
int acceptCount = 0;
NioSocketChannel acceptedChannel;
while ((acceptedChannel = MockTcpChannelFactory.this.acceptNioChannel(this, selectorSupplier)) != null) {
acceptChannel(acceptedChannel);
++acceptCount;
if (acceptCount % 100 == 0) {
logger.warn("Accepted [{}] connections in a single select loop iteration on [{}]", acceptCount, channel);
}
}
}
};
nioServerChannel.setContext(context);
return nioServerChannel;
}