Skip to content

Commit

Permalink
sequential port allocation _outside_ the ephemeral port range. becaus…
Browse files Browse the repository at this point in the history
…e OS might grab ephemeral ports
  • Loading branch information
lassewesth committed Jun 29, 2017
1 parent e6d8ee7 commit 59c5414
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Expand Up @@ -49,7 +49,7 @@ public class PortAuthority
{ {
Path directory = Paths.get( portAuthorityDirectory ); Path directory = Paths.get( portAuthorityDirectory );
Files.createDirectories( directory ); Files.createDirectories( directory );
PortRepository portRepository = new PortRepository( directory, EphemeralPortMinimum ); PortRepository portRepository = new PortRepository( directory, 20000 );
PortProbe portProbe = new DefaultPortProbe(); PortProbe portProbe = new DefaultPortProbe();
portProvider = new CoordinatingPortProvider( portRepository, portProbe ); portProvider = new CoordinatingPortProvider( portRepository, portProbe );
} }
Expand Down
Expand Up @@ -19,36 +19,33 @@
*/ */
package org.neo4j.causalclustering; package org.neo4j.causalclustering;


import static org.neo4j.causalclustering.PortConstants.EphemeralPortMaximum;


import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Random;


public class PortRepository public class PortRepository
{ {
private static final Random RANDOM = new Random();

private final Path directory; private final Path directory;
private final int lowestPort;


public PortRepository( Path directory, int lowestPort ) private int currentPort;

public PortRepository( Path directory, int initialPort )
{ {
this.directory = directory; this.directory = directory;
this.lowestPort = lowestPort;
this.currentPort = initialPort;
} }


// synchronize between threads in this JVM // synchronize between threads in this JVM
public synchronized int reserveNextPort( String trace ) public synchronized int reserveNextPort( String trace )
{ {
// if we do not find one after trying 100(!) times, bail out while ( currentPort <= EphemeralPortMaximum )
for ( int i = 0; i < 100; i++ )
{ {
int port = lowestPort + RANDOM.nextInt( PortConstants.EphemeralPortMaximum - lowestPort ); Path portFilePath = directory.resolve( "port" + currentPort );

Path portFilePath = directory.resolve( "port" + port );


try try
{ {
Expand All @@ -60,11 +57,11 @@ public synchronized int reserveNextPort( String trace )
fileOutputStream.flush(); fileOutputStream.flush();
} }


return port; return currentPort++;
} }
catch ( FileAlreadyExistsException e ) catch ( FileAlreadyExistsException e )
{ {
// try again currentPort++;
} }
catch ( IOException e ) catch ( IOException e )
{ {
Expand Down
Expand Up @@ -88,7 +88,6 @@ public void shouldNotInterfereWithOtherRepositories() throws Exception
} }


@Test @Test
@Ignore( "This was for when ports were allocated sequentially" )
public void shouldNotOverrun() throws Exception public void shouldNotOverrun() throws Exception
{ {
Path directory = testDirectory.cleanDirectory( "port-repository" ).toPath(); Path directory = testDirectory.cleanDirectory( "port-repository" ).toPath();
Expand Down

0 comments on commit 59c5414

Please sign in to comment.