Skip to content

Commit 6b62b71

Browse files
committed
Decouple ChannelFactory from Tcp classes
This is related to elastic#27260. Currently `ChannelFactory` is tightly coupled to classes related to the elasticsearch Tcp binary protocol. This commit modifies the factory to be able to construct http or other protocol channels.
1 parent 4b7b1e2 commit 6b62b71

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

test/framework/src/main/java/org/elasticsearch/transport/nio/NioTransport.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.elasticsearch.transport.nio.channel.NioChannel;
4242
import org.elasticsearch.transport.nio.channel.NioServerSocketChannel;
4343
import org.elasticsearch.transport.nio.channel.NioSocketChannel;
44+
import org.elasticsearch.transport.nio.channel.TcpReadContext;
45+
import org.elasticsearch.transport.nio.channel.TcpWriteContext;
4446

4547
import java.io.IOException;
4648
import java.net.InetSocketAddress;
@@ -68,7 +70,7 @@ public class NioTransport extends TcpTransport<NioChannel> {
6870
public static final Setting<Integer> NIO_ACCEPTOR_COUNT =
6971
intSetting("transport.nio.acceptor_count", 1, 1, Setting.Property.NodeScope);
7072

71-
private final TcpReadHandler tcpReadHandler = new TcpReadHandler(this);
73+
private final Consumer<NioSocketChannel> contextSetter;
7274
private final ConcurrentMap<String, ChannelFactory> profileToChannelFactory = newConcurrentMap();
7375
private final OpenChannels openChannels = new OpenChannels(logger);
7476
private final ArrayList<AcceptingSelector> acceptors = new ArrayList<>();
@@ -79,6 +81,7 @@ public class NioTransport extends TcpTransport<NioChannel> {
7981
public NioTransport(Settings settings, ThreadPool threadPool, NetworkService networkService, BigArrays bigArrays,
8082
NamedWriteableRegistry namedWriteableRegistry, CircuitBreakerService circuitBreakerService) {
8183
super("nio", settings, threadPool, bigArrays, circuitBreakerService, namedWriteableRegistry, networkService);
84+
contextSetter = (c) -> c.setContexts(new TcpReadContext(c, new TcpReadHandler(this)), new TcpWriteContext(c));
8285
}
8386

8487
@Override
@@ -206,7 +209,7 @@ protected void doStart() {
206209

207210
// loop through all profiles and start them up, special handling for default one
208211
for (ProfileSettings profileSettings : profileSettings) {
209-
profileToChannelFactory.putIfAbsent(profileSettings.profileName, new ChannelFactory(profileSettings, tcpReadHandler));
212+
profileToChannelFactory.putIfAbsent(profileSettings.profileName, new ChannelFactory(profileSettings, contextSetter));
210213
bindServer(profileSettings);
211214
}
212215
}
@@ -243,7 +246,7 @@ final void exceptionCaught(NioSocketChannel channel, Throwable cause) {
243246

244247
private NioClient createClient() {
245248
Supplier<SocketSelector> selectorSupplier = new RoundRobinSelectorSupplier(socketSelectors);
246-
ChannelFactory channelFactory = new ChannelFactory(new ProfileSettings(settings, "default"), tcpReadHandler);
249+
ChannelFactory channelFactory = new ChannelFactory(new ProfileSettings(settings, "default"), contextSetter);
247250
return new NioClient(logger, openChannels, selectorSupplier, defaultConnectionProfile.getConnectTimeout(), channelFactory);
248251
}
249252

test/framework/src/main/java/org/elasticsearch/transport/nio/channel/ChannelFactory.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.transport.TcpTransport;
2727
import org.elasticsearch.transport.nio.AcceptingSelector;
2828
import org.elasticsearch.transport.nio.SocketSelector;
29-
import org.elasticsearch.transport.nio.TcpReadHandler;
3029

3130
import java.io.Closeable;
3231
import java.io.IOException;
@@ -39,23 +38,27 @@
3938

4039
public class ChannelFactory {
4140

42-
private final TcpReadHandler handler;
41+
private final Consumer<NioSocketChannel> contextSetter;
4342
private final RawChannelFactory rawChannelFactory;
4443

45-
public ChannelFactory(TcpTransport.ProfileSettings profileSettings, TcpReadHandler handler) {
46-
this(new RawChannelFactory(profileSettings), handler);
44+
public ChannelFactory(TcpTransport.ProfileSettings profileSettings, Consumer<NioSocketChannel> contextSetter) {
45+
this(new RawChannelFactory(profileSettings.tcpNoDelay,
46+
profileSettings.tcpKeepAlive,
47+
profileSettings.reuseAddress,
48+
Math.toIntExact(profileSettings.sendBufferSize.getBytes()),
49+
Math.toIntExact(profileSettings.receiveBufferSize.getBytes())), contextSetter);
4750
}
4851

49-
ChannelFactory(RawChannelFactory rawChannelFactory, TcpReadHandler handler) {
50-
this.handler = handler;
52+
ChannelFactory(RawChannelFactory rawChannelFactory, Consumer<NioSocketChannel> contextSetter) {
53+
this.contextSetter = contextSetter;
5154
this.rawChannelFactory = rawChannelFactory;
5255
}
5356

5457
public NioSocketChannel openNioChannel(InetSocketAddress remoteAddress, SocketSelector selector,
5558
Consumer<NioChannel> closeListener) throws IOException {
5659
SocketChannel rawChannel = rawChannelFactory.openNioChannel(remoteAddress);
5760
NioSocketChannel channel = new NioSocketChannel(NioChannel.CLIENT, rawChannel, selector);
58-
channel.setContexts(new TcpReadContext(channel, handler), new TcpWriteContext(channel));
61+
contextSetter.accept(channel);
5962
channel.getCloseFuture().addListener(ActionListener.wrap(closeListener::accept, (e) -> closeListener.accept(channel)));
6063
scheduleChannel(channel, selector);
6164
return channel;
@@ -65,7 +68,7 @@ public NioSocketChannel acceptNioChannel(NioServerSocketChannel serverChannel, S
6568
Consumer<NioChannel> closeListener) throws IOException {
6669
SocketChannel rawChannel = rawChannelFactory.acceptNioChannel(serverChannel);
6770
NioSocketChannel channel = new NioSocketChannel(serverChannel.getProfile(), rawChannel, selector);
68-
channel.setContexts(new TcpReadContext(channel, handler), new TcpWriteContext(channel));
71+
contextSetter.accept(channel);
6972
channel.getCloseFuture().addListener(ActionListener.wrap(closeListener::accept, (e) -> closeListener.accept(channel)));
7073
scheduleChannel(channel, selector);
7174
return channel;
@@ -105,12 +108,13 @@ static class RawChannelFactory {
105108
private final int tcpSendBufferSize;
106109
private final int tcpReceiveBufferSize;
107110

108-
RawChannelFactory(TcpTransport.ProfileSettings profileSettings) {
109-
tcpNoDelay = profileSettings.tcpNoDelay;
110-
tcpKeepAlive = profileSettings.tcpKeepAlive;
111-
tcpReusedAddress = profileSettings.reuseAddress;
112-
tcpSendBufferSize = Math.toIntExact(profileSettings.sendBufferSize.getBytes());
113-
tcpReceiveBufferSize = Math.toIntExact(profileSettings.receiveBufferSize.getBytes());
111+
RawChannelFactory(boolean tcpNoDelay, boolean tcpKeepAlive, boolean tcpReusedAddress, int tcpSendBufferSize,
112+
int tcpReceiveBufferSize) {
113+
this.tcpNoDelay = tcpNoDelay;
114+
this.tcpKeepAlive = tcpKeepAlive;
115+
this.tcpReusedAddress = tcpReusedAddress;
116+
this.tcpSendBufferSize = tcpSendBufferSize;
117+
this.tcpReceiveBufferSize = tcpReceiveBufferSize;
114118
}
115119

116120
SocketChannel openNioChannel(InetSocketAddress remoteAddress) throws IOException {

test/framework/src/test/java/org/elasticsearch/transport/nio/channel/ChannelFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class ChannelFactoryTests extends ESTestCase {
5555
@SuppressWarnings("unchecked")
5656
public void setupFactory() throws IOException {
5757
rawChannelFactory = mock(ChannelFactory.RawChannelFactory.class);
58-
channelFactory = new ChannelFactory(rawChannelFactory, mock(TcpReadHandler.class));
58+
channelFactory = new ChannelFactory(rawChannelFactory, mock(Consumer.class));
5959
listener = mock(Consumer.class);
6060
socketSelector = mock(SocketSelector.class);
6161
acceptingSelector = mock(AcceptingSelector.class);

0 commit comments

Comments
 (0)