2121import org .apache .lucene .util .IOUtils ;
2222import org .elasticsearch .Version ;
2323import org .elasticsearch .action .ActionListener ;
24- import org .elasticsearch .cluster .node .DiscoveryNode ;
2524import org .elasticsearch .common .bytes .BytesReference ;
2625import org .elasticsearch .common .io .stream .BytesStreamOutput ;
2726import org .elasticsearch .common .io .stream .InputStreamStreamInput ;
3029import org .elasticsearch .common .network .NetworkService ;
3130import org .elasticsearch .common .settings .Settings ;
3231import org .elasticsearch .common .unit .ByteSizeValue ;
33- import org .elasticsearch .common .unit .TimeValue ;
3432import org .elasticsearch .common .util .BigArrays ;
3533import org .elasticsearch .common .util .CancellableThreads ;
3634import org .elasticsearch .common .util .concurrent .AbstractRunnable ;
4947import java .net .ServerSocket ;
5048import java .net .Socket ;
5149import java .net .SocketException ;
52- import java .net .SocketTimeoutException ;
5350import java .util .Collections ;
5451import java .util .HashSet ;
5552import java .util .Set ;
6158import java .util .concurrent .Executors ;
6259import java .util .concurrent .TimeUnit ;
6360import java .util .concurrent .atomic .AtomicBoolean ;
64- import java .util .function .Consumer ;
6561
6662/**
6763 * This is a socket based blocking TcpTransport implementation that is used for tests
@@ -164,28 +160,32 @@ private void readMessage(MockChannel mockChannel, StreamInput input) throws IOEx
164160 }
165161
166162 @ Override
167- protected MockChannel initiateChannel (DiscoveryNode node , TimeValue connectTimeout , ActionListener <Void > connectListener )
168- throws IOException {
169- InetSocketAddress address = node .getAddress ().address ();
163+ protected MockChannel initiateChannel (InetSocketAddress address , ActionListener <Void > connectListener ) throws IOException {
170164 final MockSocket socket = new MockSocket ();
165+ final MockChannel channel = new MockChannel (socket , address , "none" );
166+
171167 boolean success = false ;
172168 try {
173169 configureSocket (socket );
174- try {
175- socket .connect (address , Math .toIntExact (connectTimeout .millis ()));
176- } catch (SocketTimeoutException ex ) {
177- throw new ConnectTransportException (node , "connect_timeout[" + connectTimeout + "]" , ex );
178- }
179- MockChannel channel = new MockChannel (socket , address , "none" , (c ) -> {});
180- channel .loopRead (executor );
181170 success = true ;
182- connectListener .onResponse (null );
183- return channel ;
184171 } finally {
185172 if (success == false ) {
186173 IOUtils .close (socket );
187174 }
175+
188176 }
177+
178+ executor .submit (() -> {
179+ try {
180+ socket .connect (address );
181+ channel .loopRead (executor );
182+ connectListener .onResponse (null );
183+ } catch (Exception ex ) {
184+ connectListener .onFailure (ex );
185+ }
186+ });
187+
188+ return channel ;
189189 }
190190
191191 @ Override
@@ -218,7 +218,6 @@ public final class MockChannel implements Closeable, TcpChannel {
218218 private final Socket activeChannel ;
219219 private final String profile ;
220220 private final CancellableThreads cancellableThreads = new CancellableThreads ();
221- private final Closeable onClose ;
222221 private final CompletableFuture <Void > closeFuture = new CompletableFuture <>();
223222
224223 /**
@@ -227,14 +226,12 @@ public final class MockChannel implements Closeable, TcpChannel {
227226 * @param socket The client socket. Mut not be null.
228227 * @param localAddress Address associated with the corresponding local server socket. Must not be null.
229228 * @param profile The associated profile name.
230- * @param onClose Callback to execute when this channel is closed.
231229 */
232- public MockChannel (Socket socket , InetSocketAddress localAddress , String profile , Consumer < MockChannel > onClose ) {
230+ public MockChannel (Socket socket , InetSocketAddress localAddress , String profile ) {
233231 this .localAddress = localAddress ;
234232 this .activeChannel = socket ;
235233 this .serverSocket = null ;
236234 this .profile = profile ;
237- this .onClose = () -> onClose .accept (this );
238235 synchronized (openChannels ) {
239236 openChannels .add (this );
240237 }
@@ -246,12 +243,11 @@ public MockChannel(Socket socket, InetSocketAddress localAddress, String profile
246243 * @param serverSocket The associated server socket. Must not be null.
247244 * @param profile The associated profile name.
248245 */
249- public MockChannel (ServerSocket serverSocket , String profile ) {
246+ MockChannel (ServerSocket serverSocket , String profile ) {
250247 this .localAddress = (InetSocketAddress ) serverSocket .getLocalSocketAddress ();
251248 this .serverSocket = serverSocket ;
252249 this .profile = profile ;
253250 this .activeChannel = null ;
254- this .onClose = null ;
255251 synchronized (openChannels ) {
256252 openChannels .add (this );
257253 }
@@ -266,8 +262,19 @@ public void accept(Executor executor) throws IOException {
266262 synchronized (this ) {
267263 if (isOpen .get ()) {
268264 incomingChannel = new MockChannel (incomingSocket ,
269- new InetSocketAddress (incomingSocket .getLocalAddress (), incomingSocket .getPort ()), profile ,
270- workerChannels ::remove );
265+ new InetSocketAddress (incomingSocket .getLocalAddress (), incomingSocket .getPort ()), profile );
266+ MockChannel finalIncomingChannel = incomingChannel ;
267+ incomingChannel .addCloseListener (new ActionListener <Void >() {
268+ @ Override
269+ public void onResponse (Void aVoid ) {
270+ workerChannels .remove (finalIncomingChannel );
271+ }
272+
273+ @ Override
274+ public void onFailure (Exception e ) {
275+ workerChannels .remove (finalIncomingChannel );
276+ }
277+ });
271278 serverAcceptedChannel (incomingChannel );
272279 //establish a happens-before edge between closing and accepting a new connection
273280 workerChannels .add (incomingChannel );
@@ -287,7 +294,7 @@ public void accept(Executor executor) throws IOException {
287294 }
288295 }
289296
290- public void loopRead (Executor executor ) {
297+ void loopRead (Executor executor ) {
291298 executor .execute (new AbstractRunnable () {
292299 @ Override
293300 public void onFailure (Exception e ) {
@@ -312,7 +319,7 @@ protected void doRun() throws Exception {
312319 });
313320 }
314321
315- public synchronized void close0 () throws IOException {
322+ synchronized void close0 () throws IOException {
316323 // establish a happens-before edge between closing and accepting a new connection
317324 // we have to sync this entire block to ensure that our openChannels checks work correctly.
318325 // The close block below will close all worker channels but if one of the worker channels runs into an exception
@@ -325,7 +332,7 @@ public synchronized void close0() throws IOException {
325332 removedChannel = openChannels .remove (this );
326333 }
327334 IOUtils .close (serverSocket , activeChannel , () -> IOUtils .close (workerChannels ),
328- () -> cancellableThreads .cancel ("channel closed" ), onClose );
335+ () -> cancellableThreads .cancel ("channel closed" ));
329336 assert removedChannel : "Channel was not removed or removed twice?" ;
330337 }
331338 }
0 commit comments