3333import org .elasticsearch .threadpool .ThreadPool ;
3434import org .elasticsearch .transport .TcpTransport ;
3535import org .elasticsearch .transport .Transports ;
36- import org .elasticsearch .transport .nio .channel .ChannelFactory ;
3736import org .elasticsearch .transport .nio .channel .NioChannel ;
3837import org .elasticsearch .transport .nio .channel .NioServerSocketChannel ;
3938import org .elasticsearch .transport .nio .channel .NioSocketChannel ;
39+ import org .elasticsearch .transport .nio .channel .TcpChannelFactory ;
40+ import org .elasticsearch .transport .nio .channel .TcpNioServerSocketChannel ;
41+ import org .elasticsearch .transport .nio .channel .TcpNioSocketChannel ;
4042import org .elasticsearch .transport .nio .channel .TcpReadContext ;
4143import org .elasticsearch .transport .nio .channel .TcpWriteContext ;
4244
@@ -65,12 +67,12 @@ public class NioTransport extends TcpTransport {
6567 public static final Setting <Integer > NIO_ACCEPTOR_COUNT =
6668 intSetting ("transport.nio.acceptor_count" , 1 , 1 , Setting .Property .NodeScope );
6769
68- protected final OpenChannels openChannels = new OpenChannels (logger );
69- private final ConcurrentMap <String , ChannelFactory > profileToChannelFactory = newConcurrentMap ();
70+ private final OpenChannels openChannels = new OpenChannels (logger );
71+ private final ConcurrentMap <String , TcpChannelFactory > profileToChannelFactory = newConcurrentMap ();
7072 private final ArrayList <AcceptingSelector > acceptors = new ArrayList <>();
7173 private final ArrayList <SocketSelector > socketSelectors = new ArrayList <>();
7274 private RoundRobinSelectorSupplier clientSelectorSupplier ;
73- private ChannelFactory clientChannelFactory ;
75+ private TcpChannelFactory clientChannelFactory ;
7476 private int acceptorNumber ;
7577
7678 public NioTransport (Settings settings , ThreadPool threadPool , NetworkService networkService , BigArrays bigArrays ,
@@ -84,17 +86,21 @@ public long getNumOpenServerConnections() {
8486 }
8587
8688 @ Override
87- protected NioServerSocketChannel bind (String name , InetSocketAddress address ) throws IOException {
88- ChannelFactory channelFactory = this .profileToChannelFactory .get (name );
89+ protected TcpNioServerSocketChannel bind (String name , InetSocketAddress address ) throws IOException {
90+ TcpChannelFactory channelFactory = this .profileToChannelFactory .get (name );
8991 AcceptingSelector selector = acceptors .get (++acceptorNumber % NioTransport .NIO_ACCEPTOR_COUNT .get (settings ));
90- return channelFactory .openNioServerSocketChannel (address , selector );
92+ TcpNioServerSocketChannel serverChannel = channelFactory .openNioServerSocketChannel (address , selector );
93+ openChannels .serverChannelOpened (serverChannel );
94+ serverChannel .addCloseListener (ActionListener .wrap (() -> openChannels .channelClosed (serverChannel )));
95+ return serverChannel ;
9196 }
9297
9398 @ Override
94- protected NioChannel initiateChannel (DiscoveryNode node , TimeValue connectTimeout , ActionListener <Void > connectListener )
99+ protected TcpNioSocketChannel initiateChannel (DiscoveryNode node , TimeValue connectTimeout , ActionListener <Void > connectListener )
95100 throws IOException {
96- NioSocketChannel channel = clientChannelFactory .openNioChannel (node .getAddress ().address (), clientSelectorSupplier .get ());
101+ TcpNioSocketChannel channel = clientChannelFactory .openNioChannel (node .getAddress ().address (), clientSelectorSupplier .get ());
97102 openChannels .clientChannelOpened (channel );
103+ channel .addCloseListener (ActionListener .wrap (() -> openChannels .channelClosed (channel )));
98104 channel .addConnectListener (connectListener );
99105 return channel ;
100106 }
@@ -119,14 +125,14 @@ protected void doStart() {
119125
120126 Consumer <NioSocketChannel > clientContextSetter = getContextSetter ("client-socket" );
121127 clientSelectorSupplier = new RoundRobinSelectorSupplier (socketSelectors );
122- clientChannelFactory = new ChannelFactory (new ProfileSettings (settings , "default" ), clientContextSetter );
128+ ProfileSettings clientProfileSettings = new ProfileSettings (settings , "default" );
129+ clientChannelFactory = new TcpChannelFactory (clientProfileSettings , clientContextSetter , getServerContextSetter ());
123130
124131 if (NetworkService .NETWORK_SERVER .get (settings )) {
125132 int acceptorCount = NioTransport .NIO_ACCEPTOR_COUNT .get (settings );
126133 for (int i = 0 ; i < acceptorCount ; ++i ) {
127134 Supplier <SocketSelector > selectorSupplier = new RoundRobinSelectorSupplier (socketSelectors );
128- AcceptorEventHandler eventHandler = new AcceptorEventHandler (logger , openChannels , selectorSupplier ,
129- this ::serverAcceptedChannel );
135+ AcceptorEventHandler eventHandler = new AcceptorEventHandler (logger , selectorSupplier );
130136 AcceptingSelector acceptor = new AcceptingSelector (eventHandler );
131137 acceptors .add (acceptor );
132138 }
@@ -143,7 +149,8 @@ protected void doStart() {
143149 for (ProfileSettings profileSettings : profileSettings ) {
144150 String profileName = profileSettings .profileName ;
145151 Consumer <NioSocketChannel > contextSetter = getContextSetter (profileName );
146- profileToChannelFactory .putIfAbsent (profileName , new ChannelFactory (profileSettings , contextSetter ));
152+ TcpChannelFactory factory = new TcpChannelFactory (profileSettings , contextSetter , getServerContextSetter ());
153+ profileToChannelFactory .putIfAbsent (profileName , factory );
147154 bindServer (profileSettings );
148155 }
149156 }
@@ -169,14 +176,27 @@ protected void stopInternal() {
169176 }
170177
171178 protected SocketEventHandler getSocketEventHandler () {
172- return new SocketEventHandler (logger , this :: exceptionCaught , openChannels );
179+ return new SocketEventHandler (logger );
173180 }
174181
175182 final void exceptionCaught (NioSocketChannel channel , Exception exception ) {
176- onException (channel , exception );
183+ onException (( TcpNioSocketChannel ) channel , exception );
177184 }
178185
179186 private Consumer <NioSocketChannel > getContextSetter (String profileName ) {
180- return (c ) -> c .setContexts (new TcpReadContext (c , new TcpReadHandler (profileName ,this )), new TcpWriteContext (c ));
187+ return (c ) -> c .setContexts (new TcpReadContext (c , new TcpReadHandler (profileName ,this )), new TcpWriteContext (c ),
188+ this ::exceptionCaught );
189+ }
190+
191+ private void acceptChannel (NioSocketChannel channel ) {
192+ TcpNioSocketChannel tcpChannel = (TcpNioSocketChannel ) channel ;
193+ openChannels .acceptedChannelOpened (tcpChannel );
194+ tcpChannel .addCloseListener (ActionListener .wrap (() -> openChannels .channelClosed (channel )));
195+ serverAcceptedChannel (tcpChannel );
196+
197+ }
198+
199+ private Consumer <NioServerSocketChannel > getServerContextSetter () {
200+ return (c ) -> c .setAcceptContext (this ::acceptChannel );
181201 }
182202}
0 commit comments