From b2e22587ce4faa50cb3ce60171932657226e68e4 Mon Sep 17 00:00:00 2001 From: Lasse Westh-Nielsen Date: Fri, 5 May 2017 14:30:14 +0200 Subject: [PATCH] trying picking ports at random instead of in ascending order --- .../causalclustering/DefaultPortProbe.java | 30 ------------------- .../causalclustering/PortRepository.java | 22 ++++++++------ 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/DefaultPortProbe.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/DefaultPortProbe.java index e349f75deb47c..dbc347a610598 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/DefaultPortProbe.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/DefaultPortProbe.java @@ -51,36 +51,6 @@ public boolean isOccupied( int port ) return true; } -// // poke at the port and as a side-effect, waste some time -// try ( Socket ignored = new Socket( "0.0.0.0", port ) ) -// { -// -// } -// catch ( IOException e ) -// { -// return true; -// } -// -// // poke at the port and as a side-effect, waste some time -// try ( Socket ignored = new Socket( InetAddress.getLoopbackAddress(), port ) ) -// { -// -// } -// catch ( IOException e ) -// { -// return true; -// } - // or not because that ate all ports very quickly... - - try - { - Thread.sleep( 2000 ); - } - catch ( InterruptedException e ) - { - // best effort - } - return false; } } diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java index aefcd82c40e87..0f7b0c5538797 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java @@ -26,26 +26,30 @@ import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Random; public class PortRepository { - private final Path directory; + private static final Random RANDOM = new Random(); - private int currentPort; + private final Path directory; + private final int lowestPort; - public PortRepository( Path directory, int initialPort ) + public PortRepository( Path directory, int lowestPort ) { this.directory = directory; - - this.currentPort = initialPort; + this.lowestPort = lowestPort; } // synchronize between threads in this JVM public synchronized int reserveNextPort( String trace ) { - while ( currentPort <= EphemeralPortMaximum ) + // if we do not find one after trying 100 times, bail out + for ( int i = 0; i < 100; i++ ) { - Path portFilePath = directory.resolve( "port" + currentPort ); + int port = lowestPort + RANDOM.nextInt( PortConstants.EphemeralPortMaximum - lowestPort ); + + Path portFilePath = directory.resolve( "port" + port ); try { @@ -57,11 +61,11 @@ public synchronized int reserveNextPort( String trace ) fileOutputStream.flush(); } - return currentPort++; + return port; } catch ( FileAlreadyExistsException e ) { - currentPort++; + // try again } catch ( IOException e ) {