Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow easier Bolt message versioning
Previously, decoding of inbound Bolt messages and their routing to the state machine was very protocol V1 specific and not extensible. This commit makes it possible to register protocol version specific message deserializers. It also makes code always use same inbound message classes. Two different implementations existed before - one for production code and one for tests. Central component is `BoltRequestMessageReader` which holds a configurable set of message deserializers. Correct deserializer is looked up by a message signature.
- Loading branch information
Showing
108 changed files
with
2,063 additions
and
1,626 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
community/bolt/src/main/java/org/neo4j/bolt/messaging/BoltRequestMessageReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,101 @@ | |||
/* | |||
* Copyright (c) 2002-2018 "Neo4j," | |||
* Neo4j Sweden AB [http://neo4j.com] | |||
* | |||
* This file is part of Neo4j. | |||
* | |||
* Neo4j is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
package org.neo4j.bolt.messaging; | |||
|
|||
import java.io.IOException; | |||
import java.util.List; | |||
import java.util.Map; | |||
|
|||
import org.neo4j.bolt.logging.BoltMessageLogger; | |||
import org.neo4j.bolt.runtime.BoltConnection; | |||
import org.neo4j.bolt.runtime.BoltResponseHandler; | |||
import org.neo4j.bolt.runtime.Neo4jError; | |||
import org.neo4j.bolt.v1.packstream.PackStream; | |||
import org.neo4j.kernel.api.exceptions.Status; | |||
|
|||
import static java.util.function.Function.identity; | |||
import static java.util.stream.Collectors.toMap; | |||
|
|||
/** | |||
* Reader for Bolt request messages made available via a {@link Neo4jPack.Unpacker}. | |||
*/ | |||
public abstract class BoltRequestMessageReader | |||
{ | |||
private final BoltConnection connection; | |||
private final BoltResponseHandler externalErrorResponseHandler; | |||
private final BoltMessageLogger messageLogger; | |||
private final Map<Integer,RequestMessageDecoder> decoders; | |||
|
|||
protected BoltRequestMessageReader( BoltConnection connection, BoltResponseHandler externalErrorResponseHandler, | |||
List<RequestMessageDecoder> decoders, BoltMessageLogger messageLogger ) | |||
{ | |||
this.connection = connection; | |||
this.externalErrorResponseHandler = externalErrorResponseHandler; | |||
this.messageLogger = messageLogger; | |||
this.decoders = decoders.stream().collect( toMap( RequestMessageDecoder::signature, identity() ) ); | |||
} | |||
|
|||
public void read( Neo4jPack.Unpacker unpacker ) throws IOException | |||
{ | |||
try | |||
{ | |||
doRead( unpacker ); | |||
} | |||
catch ( BoltIOException e ) | |||
{ | |||
if ( e.causesFailureMessage() ) | |||
{ | |||
Neo4jError error = Neo4jError.from( e ); | |||
messageLogger.clientEvent( "ERROR", error::message ); | |||
connection.enqueue( stateMachine -> stateMachine.handleExternalFailure( error, externalErrorResponseHandler ) ); | |||
} | |||
else | |||
{ | |||
throw e; | |||
} | |||
} | |||
} | |||
|
|||
private void doRead( Neo4jPack.Unpacker unpacker ) throws IOException | |||
{ | |||
try | |||
{ | |||
unpacker.unpackStructHeader(); | |||
int signature = unpacker.unpackStructSignature(); | |||
|
|||
RequestMessageDecoder decoder = decoders.get( signature ); | |||
if ( decoder == null ) | |||
{ | |||
throw new BoltIOException( Status.Request.InvalidFormat, | |||
String.format( "Message 0x%s is not a valid message signature.", Integer.toHexString( signature ) ) ); | |||
} | |||
|
|||
RequestMessage message = decoder.decode( unpacker ); | |||
BoltResponseHandler resonseHandler = decoder.resonseHandler(); | |||
|
|||
connection.enqueue( stateMachine -> stateMachine.process( message, resonseHandler ) ); | |||
} | |||
catch ( PackStream.PackStreamException e ) | |||
{ | |||
throw new BoltIOException( Status.Request.InvalidFormat, | |||
String.format( "Unable to read message type. Error was: %s.", e.getMessage() ), e ); | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
community/bolt/src/main/java/org/neo4j/bolt/runtime/StateMachineMessage.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.