33
33
import org .elasticsearch .threadpool .ThreadPool ;
34
34
import org .elasticsearch .transport .TcpTransport ;
35
35
import org .elasticsearch .transport .Transports ;
36
- import org .elasticsearch .transport .nio .channel .ChannelFactory ;
37
36
import org .elasticsearch .transport .nio .channel .NioChannel ;
38
37
import org .elasticsearch .transport .nio .channel .NioServerSocketChannel ;
39
38
import 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 ;
40
42
import org .elasticsearch .transport .nio .channel .TcpReadContext ;
41
43
import org .elasticsearch .transport .nio .channel .TcpWriteContext ;
42
44
@@ -65,12 +67,12 @@ public class NioTransport extends TcpTransport {
65
67
public static final Setting <Integer > NIO_ACCEPTOR_COUNT =
66
68
intSetting ("transport.nio.acceptor_count" , 1 , 1 , Setting .Property .NodeScope );
67
69
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 ();
70
72
private final ArrayList <AcceptingSelector > acceptors = new ArrayList <>();
71
73
private final ArrayList <SocketSelector > socketSelectors = new ArrayList <>();
72
74
private RoundRobinSelectorSupplier clientSelectorSupplier ;
73
- private ChannelFactory clientChannelFactory ;
75
+ private TcpChannelFactory clientChannelFactory ;
74
76
private int acceptorNumber ;
75
77
76
78
public NioTransport (Settings settings , ThreadPool threadPool , NetworkService networkService , BigArrays bigArrays ,
@@ -84,17 +86,21 @@ public long getNumOpenServerConnections() {
84
86
}
85
87
86
88
@ 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 );
89
91
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 ;
91
96
}
92
97
93
98
@ Override
94
- protected NioChannel initiateChannel (DiscoveryNode node , TimeValue connectTimeout , ActionListener <Void > connectListener )
99
+ protected TcpNioSocketChannel initiateChannel (DiscoveryNode node , TimeValue connectTimeout , ActionListener <Void > connectListener )
95
100
throws IOException {
96
- NioSocketChannel channel = clientChannelFactory .openNioChannel (node .getAddress ().address (), clientSelectorSupplier .get ());
101
+ TcpNioSocketChannel channel = clientChannelFactory .openNioChannel (node .getAddress ().address (), clientSelectorSupplier .get ());
97
102
openChannels .clientChannelOpened (channel );
103
+ channel .addCloseListener (ActionListener .wrap (() -> openChannels .channelClosed (channel )));
98
104
channel .addConnectListener (connectListener );
99
105
return channel ;
100
106
}
@@ -119,14 +125,14 @@ protected void doStart() {
119
125
120
126
Consumer <NioSocketChannel > clientContextSetter = getContextSetter ("client-socket" );
121
127
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 ());
123
130
124
131
if (NetworkService .NETWORK_SERVER .get (settings )) {
125
132
int acceptorCount = NioTransport .NIO_ACCEPTOR_COUNT .get (settings );
126
133
for (int i = 0 ; i < acceptorCount ; ++i ) {
127
134
Supplier <SocketSelector > selectorSupplier = new RoundRobinSelectorSupplier (socketSelectors );
128
- AcceptorEventHandler eventHandler = new AcceptorEventHandler (logger , openChannels , selectorSupplier ,
129
- this ::serverAcceptedChannel );
135
+ AcceptorEventHandler eventHandler = new AcceptorEventHandler (logger , selectorSupplier );
130
136
AcceptingSelector acceptor = new AcceptingSelector (eventHandler );
131
137
acceptors .add (acceptor );
132
138
}
@@ -143,7 +149,8 @@ protected void doStart() {
143
149
for (ProfileSettings profileSettings : profileSettings ) {
144
150
String profileName = profileSettings .profileName ;
145
151
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 );
147
154
bindServer (profileSettings );
148
155
}
149
156
}
@@ -169,14 +176,27 @@ protected void stopInternal() {
169
176
}
170
177
171
178
protected SocketEventHandler getSocketEventHandler () {
172
- return new SocketEventHandler (logger , this :: exceptionCaught , openChannels );
179
+ return new SocketEventHandler (logger );
173
180
}
174
181
175
182
final void exceptionCaught (NioSocketChannel channel , Exception exception ) {
176
- onException (channel , exception );
183
+ onException (( TcpNioSocketChannel ) channel , exception );
177
184
}
178
185
179
186
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 );
181
201
}
182
202
}
0 commit comments