Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dispatcher that delegates to the correct decoder in CatchupServer
This will avoid unnecessary buffer copies.
- Loading branch information
1 parent
739e95a
commit 39a10da
Showing
10 changed files
with
169 additions
and
94 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
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
64 changes: 64 additions & 0 deletions
64
enterprise/core-edge/src/main/java/org/neo4j/coreedge/catchup/RequestDecoderDispatcher.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,64 @@ | |||
/* | |||
* Copyright (c) 2002-2016 "Neo Technology," | |||
* Network Engine for Objects in Lund AB [http://neotechnology.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 Affero 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 Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
package org.neo4j.coreedge.catchup; | |||
|
|||
import io.netty.channel.ChannelHandlerContext; | |||
import io.netty.channel.ChannelInboundHandler; | |||
import io.netty.channel.ChannelInboundHandlerAdapter; | |||
|
|||
import java.util.HashMap; | |||
import java.util.Map; | |||
|
|||
import org.neo4j.coreedge.catchup.CatchupServerProtocol.NextMessage; | |||
import org.neo4j.logging.Log; | |||
import org.neo4j.logging.LogProvider; | |||
|
|||
public class RequestDecoderDispatcher extends ChannelInboundHandlerAdapter | |||
{ | |||
private final Map<NextMessage, ChannelInboundHandler> decoders = new HashMap<>(); | |||
private final CatchupServerProtocol protocol; | |||
private final Log log; | |||
|
|||
public RequestDecoderDispatcher( CatchupServerProtocol protocol, LogProvider logProvider ) | |||
{ | |||
this.protocol = protocol; | |||
this.log = logProvider.getLog( getClass() ); | |||
} | |||
|
|||
@Override | |||
public void channelRead( ChannelHandlerContext ctx, Object msg ) throws Exception | |||
{ | |||
NextMessage expecting = protocol.expecting(); | |||
ChannelInboundHandler delegate = decoders.get( expecting ); | |||
if ( delegate == null ) | |||
{ | |||
log.warn( "Unknown message %s", expecting ); | |||
return; | |||
} | |||
|
|||
delegate.channelRead( ctx, msg ); | |||
} | |||
|
|||
public void register( NextMessage type, ChannelInboundHandler decoder ) | |||
{ | |||
assert !decoders.containsKey( type ) : "registering twice a decoder for the same type?"; | |||
decoders.put( type, decoder ); | |||
} | |||
} |
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
75 changes: 75 additions & 0 deletions
75
...rise/core-edge/src/test/java/org/neo4j/coreedge/catchup/RequestDecoderDispatcherTest.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,75 @@ | |||
/* | |||
* Copyright (c) 2002-2016 "Neo Technology," | |||
* Network Engine for Objects in Lund AB [http://neotechnology.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 Affero 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 Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
package org.neo4j.coreedge.catchup; | |||
|
|||
import io.netty.channel.ChannelHandlerContext; | |||
import io.netty.channel.ChannelInboundHandler; | |||
import org.junit.Test; | |||
|
|||
import org.neo4j.logging.AssertableLogProvider; | |||
|
|||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.verify; | |||
import static org.mockito.Mockito.verifyNoMoreInteractions; | |||
import static org.neo4j.coreedge.catchup.CatchupServerProtocol.NextMessage.TX_PULL; | |||
import static org.neo4j.logging.AssertableLogProvider.inLog; | |||
|
|||
public class RequestDecoderDispatcherTest | |||
{ | |||
private final CatchupServerProtocol protocol = new CatchupServerProtocol(); | |||
private final AssertableLogProvider logProvider = new AssertableLogProvider(); | |||
|
|||
@Test | |||
public void shouldDispatchToRegisteredDecoder() throws Exception | |||
{ | |||
// given | |||
RequestDecoderDispatcher dispatcher = new RequestDecoderDispatcher( protocol, logProvider ); | |||
protocol.expect( TX_PULL ); | |||
ChannelInboundHandler delegate = mock( ChannelInboundHandler.class ); | |||
dispatcher.register( TX_PULL, delegate ); | |||
|
|||
ChannelHandlerContext ctx = mock( ChannelHandlerContext.class ); | |||
Object msg = new Object(); | |||
|
|||
// when | |||
dispatcher.channelRead( ctx, msg ); | |||
|
|||
// then | |||
verify( delegate ).channelRead( ctx, msg ); | |||
verifyNoMoreInteractions( delegate ); | |||
} | |||
|
|||
@Test | |||
public void shouldLogAWarningIfThereIsNoDecoderForTheMessageType() throws Exception | |||
{ | |||
// given | |||
RequestDecoderDispatcher dispatcher = new RequestDecoderDispatcher( protocol, logProvider ); | |||
protocol.expect( TX_PULL ); | |||
|
|||
// when | |||
dispatcher.channelRead( mock( ChannelHandlerContext.class ), new Object() ); | |||
|
|||
// then | |||
AssertableLogProvider.LogMatcher matcher = | |||
inLog( RequestDecoderDispatcher.class ).warn( "Unknown message %s", TX_PULL ); | |||
|
|||
logProvider.assertExactly( matcher ); | |||
} | |||
} |
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