Skip to content

Commit

Permalink
Include state of protocol negotiation in handshake exceptions.
Browse files Browse the repository at this point in the history
These are logged.
  • Loading branch information
andrewkerr9000 committed Apr 13, 2018
1 parent abdf645 commit b1c0c56
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
Expand Up @@ -19,10 +19,25 @@
*/ */
package org.neo4j.causalclustering.protocol.handshake; package org.neo4j.causalclustering.protocol.handshake;


import java.util.List;
import java.util.Optional;

import javax.annotation.Nullable;

import org.neo4j.causalclustering.protocol.Protocol;
import org.neo4j.helpers.collection.Pair;

public class ClientHandshakeException extends Exception public class ClientHandshakeException extends Exception
{ {
public ClientHandshakeException( String message ) public ClientHandshakeException( String message )
{ {
super( message ); super( message );
} }

public ClientHandshakeException( String message, @Nullable Protocol.ApplicationProtocol negotiatedApplicationProtocol,
List<Pair<String,Optional<Protocol.ModifierProtocol>>> negotiatedModifierProtocols )
{
super( message + " Negotiated application protocol: " + negotiatedApplicationProtocol +
" Negotiated modifier protocols: " + negotiatedModifierProtocols );
}
} }
Expand Up @@ -41,7 +41,7 @@ public class HandshakeClient implements ClientMessageHandler
private ApplicationSupportedProtocols supportedApplicationProtocol; private ApplicationSupportedProtocols supportedApplicationProtocol;
private ModifierProtocolRepository modifierProtocolRepository; private ModifierProtocolRepository modifierProtocolRepository;
private Collection<ModifierSupportedProtocols> supportedModifierProtocols; private Collection<ModifierSupportedProtocols> supportedModifierProtocols;
private ApplicationProtocol applicationProtocol; private ApplicationProtocol negotiatedApplicationProtocol;
private List<Pair<String,Optional<ModifierProtocol>>> negotiatedModifierProtocols; private List<Pair<String,Optional<ModifierProtocol>>> negotiatedModifierProtocols;
private ProtocolStack protocolStack; private ProtocolStack protocolStack;
private CompletableFuture<ProtocolStack> future = new CompletableFuture<>(); private CompletableFuture<ProtocolStack> future = new CompletableFuture<>();
Expand Down Expand Up @@ -127,7 +127,7 @@ public void handle( ApplicationProtocolResponse applicationProtocolResponse )
} }
else else
{ {
applicationProtocol = protocol.get(); negotiatedApplicationProtocol = protocol.get();


sendSwitchOverRequestIfReady(); sendSwitchOverRequestIfReady();
} }
Expand All @@ -153,22 +153,26 @@ public void handle( ModifierProtocolResponse modifierProtocolResponse )


private void sendSwitchOverRequestIfReady() private void sendSwitchOverRequestIfReady()
{ {
if ( applicationProtocol != null && negotiatedModifierProtocols.size() == supportedModifierProtocols.size() ) if ( negotiatedApplicationProtocol != null && negotiatedModifierProtocols.size() == supportedModifierProtocols.size() )
{ {
List<ModifierProtocol> agreedModifierProtocols = negotiatedModifierProtocols List<ModifierProtocol> agreedModifierProtocols = negotiatedModifierProtocols
.stream() .stream()
.map( Pair::other ) .map( Pair::other )
.flatMap( Streams::ofOptional ) .flatMap( Streams::ofOptional )
.collect( Collectors.toList() ); .collect( Collectors.toList() );


protocolStack = new ProtocolStack( applicationProtocol, agreedModifierProtocols ); protocolStack = new ProtocolStack( negotiatedApplicationProtocol, agreedModifierProtocols );
List<Pair<String,String>> switchOverModifierProtocols = List<Pair<String,String>> switchOverModifierProtocols =
agreedModifierProtocols agreedModifierProtocols
.stream() .stream()
.map( protocol -> Pair.of( protocol.category(), protocol.implementation() ) ) .map( protocol -> Pair.of( protocol.category(), protocol.implementation() ) )
.collect( Collectors.toList() ); .collect( Collectors.toList() );


channel.writeAndFlush( new SwitchOverRequest( applicationProtocol.category(), applicationProtocol.implementation(), switchOverModifierProtocols ) ); channel.writeAndFlush(
new SwitchOverRequest(
negotiatedApplicationProtocol.category(),
negotiatedApplicationProtocol.implementation(),
switchOverModifierProtocols ) );
} }
} }


Expand Down Expand Up @@ -202,6 +206,6 @@ boolean failIfNotDone( String message )


private void decline( String message ) private void decline( String message )
{ {
future.completeExceptionally( new ClientHandshakeException( message ) ); future.completeExceptionally( new ClientHandshakeException( message, negotiatedApplicationProtocol, negotiatedModifierProtocols ) );
} }
} }
Expand Up @@ -200,7 +200,7 @@ private Set<Integer> supportedVersionsFor( ApplicationProtocolRequest request )
private void decline( String message ) private void decline( String message )
{ {
channel.dispose(); channel.dispose();
protocolStackFuture.completeExceptionally( new ServerHandshakeException( message ) ); protocolStackFuture.completeExceptionally( new ServerHandshakeException( message, protocolStackBuilder ) );
} }


CompletableFuture<ProtocolStack> protocolStackFuture() CompletableFuture<ProtocolStack> protocolStackFuture()
Expand Down
Expand Up @@ -105,5 +105,11 @@ ProtocolStack build()
{ {
return new ProtocolStack( applicationProtocol, modifierProtocols ); return new ProtocolStack( applicationProtocol, modifierProtocols );
} }

@Override
public String toString()
{
return "Builder{" + "applicationProtocol=" + applicationProtocol + ", modifierProtocols=" + modifierProtocols + '}';
}
} }
} }
Expand Up @@ -21,8 +21,8 @@


public class ServerHandshakeException extends Exception public class ServerHandshakeException extends Exception
{ {
public ServerHandshakeException( String message ) public ServerHandshakeException( String message, ProtocolStack.Builder protocolStackBuilder )
{ {
super( message ); super( message + " Negotiated protocols: " + protocolStackBuilder );
} }
} }

0 comments on commit b1c0c56

Please sign in to comment.