-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NettyInstalledProtocolsIT.java
337 lines (294 loc) · 14.7 KB
/
NettyInstalledProtocolsIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.causalclustering.protocol.handshake;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.neo4j.causalclustering.core.CausalClusteringSettings;
import org.neo4j.causalclustering.core.consensus.RaftMessages;
import org.neo4j.causalclustering.core.consensus.protocol.v1.RaftProtocolClientInstallerV1;
import org.neo4j.causalclustering.core.consensus.protocol.v1.RaftProtocolServerInstallerV1;
import org.neo4j.causalclustering.core.consensus.protocol.v2.RaftProtocolClientInstallerV2;
import org.neo4j.causalclustering.core.consensus.protocol.v2.RaftProtocolServerInstallerV2;
import org.neo4j.causalclustering.handlers.VoidPipelineWrapperFactory;
import org.neo4j.causalclustering.identity.ClusterId;
import org.neo4j.causalclustering.identity.MemberId;
import org.neo4j.causalclustering.protocol.ModifierProtocolInstaller;
import org.neo4j.causalclustering.protocol.NettyPipelineBuilderFactory;
import org.neo4j.causalclustering.protocol.Protocol;
import org.neo4j.causalclustering.protocol.Protocol.ModifierProtocol;
import org.neo4j.causalclustering.protocol.Protocol.ModifierProtocols;
import org.neo4j.causalclustering.protocol.ProtocolInstaller;
import org.neo4j.causalclustering.protocol.ProtocolInstallerRepository;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.logging.LogProvider;
import org.neo4j.ports.allocation.PortAuthority;
import org.neo4j.stream.Streams;
import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.Matchers.contains;
import static org.neo4j.causalclustering.protocol.Protocol.ApplicationProtocolCategory.RAFT;
import static org.neo4j.causalclustering.protocol.Protocol.ApplicationProtocols.RAFT_1;
import static org.neo4j.causalclustering.protocol.Protocol.ApplicationProtocols.RAFT_2;
import static org.neo4j.causalclustering.protocol.Protocol.ModifierProtocolCategory.COMPRESSION;
import static org.neo4j.test.assertion.Assert.assertEventually;
@RunWith( Parameterized.class )
public class NettyInstalledProtocolsIT
{
private static final int TIMEOUT_SECONDS = 10;
private Parameters parameters;
private AssertableLogProvider logProvider;
public NettyInstalledProtocolsIT( Parameters parameters )
{
this.parameters = parameters;
}
@Parameterized.Parameters( name = "{0}" )
public static Collection<Parameters> data()
{
Stream<Optional<ModifierProtocol>> noModifierProtocols = Stream.of( Optional.empty() );
Stream<Optional<ModifierProtocol>> individualModifierProtocols = Stream.of( ModifierProtocols.values() ).map( Optional::of );
return Stream
.concat( noModifierProtocols, individualModifierProtocols )
.flatMap( protocol -> Stream.of( raft1WithCompressionModifier( protocol ), raft2WithCompressionModifiers( protocol ) ) )
.collect( Collectors.toList() );
}
@SuppressWarnings( "OptionalUsedAsFieldOrParameterType" )
private static Parameters raft1WithCompressionModifier( Optional<ModifierProtocol> protocol )
{
List<String> versions = Streams.ofOptional( protocol ).map( Protocol::implementation ).collect( Collectors.toList() );
return new Parameters( "Raft 1, modifiers: " + protocol, new ApplicationSupportedProtocols( RAFT, singletonList( RAFT_1.implementation() ) ),
singletonList( new ModifierSupportedProtocols( COMPRESSION, versions ) ) );
}
@SuppressWarnings( "OptionalUsedAsFieldOrParameterType" )
private static Parameters raft2WithCompressionModifiers( Optional<ModifierProtocol> protocol )
{
List<String> versions = Streams.ofOptional( protocol ).map( Protocol::implementation ).collect( Collectors.toList() );
return new Parameters( "Raft 2, modifiers: " + protocol, new ApplicationSupportedProtocols( RAFT, singletonList( RAFT_2.implementation() ) ),
singletonList( new ModifierSupportedProtocols( COMPRESSION, versions ) ) );
}
@Test
public void shouldSuccessfullySendAndReceiveAMessage() throws Throwable
{
// given
RaftMessages.Heartbeat raftMessage = new RaftMessages.Heartbeat( new MemberId( UUID.randomUUID() ), 1, 2, 3 );
RaftMessages.ClusterIdAwareMessage<RaftMessages.Heartbeat> networkMessage =
RaftMessages.ClusterIdAwareMessage.of( new ClusterId( UUID.randomUUID() ), raftMessage );
// when
client.send( networkMessage ).syncUninterruptibly();
// then
assertEventually(
messages -> String.format( "Received messages %s should contain message decorating %s", messages, raftMessage ),
() -> server.received(),
contains( messageMatches( networkMessage ) ), TIMEOUT_SECONDS, SECONDS );
}
private Server server;
private Client client;
@Before
public void setUp()
{
logProvider = new AssertableLogProvider( true );
ApplicationProtocolRepository applicationProtocolRepository =
new ApplicationProtocolRepository( Protocol.ApplicationProtocols.values(), parameters.applicationSupportedProtocol );
ModifierProtocolRepository modifierProtocolRepository =
new ModifierProtocolRepository( ModifierProtocols.values(), parameters.modifierSupportedProtocols );
NettyPipelineBuilderFactory serverPipelineBuilderFactory = new NettyPipelineBuilderFactory( VoidPipelineWrapperFactory.VOID_WRAPPER );
NettyPipelineBuilderFactory clientPipelineBuilderFactory = new NettyPipelineBuilderFactory( VoidPipelineWrapperFactory.VOID_WRAPPER );
server = new Server( serverPipelineBuilderFactory );
server.start( applicationProtocolRepository, modifierProtocolRepository, logProvider );
Config config = Config.builder().withSetting( CausalClusteringSettings.handshake_timeout, TIMEOUT_SECONDS + "s" ).build();
client = new Client( applicationProtocolRepository, modifierProtocolRepository, clientPipelineBuilderFactory, config, logProvider );
client.connect( server.port() );
}
@After
public void tearDown()
{
client.disconnect();
server.stop();
logProvider.clear();
}
private static class Parameters
{
final String name;
final ApplicationSupportedProtocols applicationSupportedProtocol;
final Collection<ModifierSupportedProtocols> modifierSupportedProtocols;
Parameters( String name, ApplicationSupportedProtocols applicationSupportedProtocol,
Collection<ModifierSupportedProtocols> modifierSupportedProtocols )
{
this.name = name;
this.applicationSupportedProtocol = applicationSupportedProtocol;
this.modifierSupportedProtocols = modifierSupportedProtocols;
}
@Override
public String toString()
{
return name;
}
}
static class Server
{
private Channel channel;
private NioEventLoopGroup eventLoopGroup;
private final List<Object> received = new CopyOnWriteArrayList<>();
private NettyPipelineBuilderFactory pipelineBuilderFactory;
ChannelInboundHandler nettyHandler = new SimpleChannelInboundHandler<Object>()
{
@Override
protected void channelRead0( ChannelHandlerContext ctx, Object msg )
{
received.add( msg );
}
};
Server( NettyPipelineBuilderFactory pipelineBuilderFactory )
{
this.pipelineBuilderFactory = pipelineBuilderFactory;
}
void start( final ApplicationProtocolRepository applicationProtocolRepository, final ModifierProtocolRepository modifierProtocolRepository,
LogProvider logProvider )
{
RaftProtocolServerInstallerV2.Factory raftFactoryV2 =
new RaftProtocolServerInstallerV2.Factory( nettyHandler, pipelineBuilderFactory, logProvider );
RaftProtocolServerInstallerV1.Factory raftFactoryV1 =
new RaftProtocolServerInstallerV1.Factory( nettyHandler, pipelineBuilderFactory,
logProvider );
ProtocolInstallerRepository<ProtocolInstaller.Orientation.Server> protocolInstallerRepository =
new ProtocolInstallerRepository<>( Arrays.asList( raftFactoryV1, raftFactoryV2 ), ModifierProtocolInstaller.allServerInstallers );
eventLoopGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap().group( eventLoopGroup )
.channel( NioServerSocketChannel.class )
.option( ChannelOption.SO_REUSEADDR, true )
.localAddress( PortAuthority.allocatePort() )
.childHandler( new HandshakeServerInitializer( applicationProtocolRepository, modifierProtocolRepository,
protocolInstallerRepository, pipelineBuilderFactory, logProvider ).asChannelInitializer() );
channel = bootstrap.bind().syncUninterruptibly().channel();
}
void stop()
{
channel.close().syncUninterruptibly();
eventLoopGroup.shutdownGracefully( 0, TIMEOUT_SECONDS, SECONDS );
}
int port()
{
return ((InetSocketAddress) channel.localAddress()).getPort();
}
public Collection<Object> received()
{
return received;
}
}
static class Client
{
private Bootstrap bootstrap;
private NioEventLoopGroup eventLoopGroup;
private Channel channel;
private HandshakeClientInitializer handshakeClientInitializer;
Client( ApplicationProtocolRepository applicationProtocolRepository, ModifierProtocolRepository modifierProtocolRepository,
NettyPipelineBuilderFactory pipelineBuilderFactory, Config config, LogProvider logProvider )
{
RaftProtocolClientInstallerV2.Factory raftFactoryV2 = new RaftProtocolClientInstallerV2.Factory( pipelineBuilderFactory, logProvider );
RaftProtocolClientInstallerV1.Factory raftFactoryV1 =
new RaftProtocolClientInstallerV1.Factory( pipelineBuilderFactory, logProvider );
ProtocolInstallerRepository<ProtocolInstaller.Orientation.Client> protocolInstallerRepository =
new ProtocolInstallerRepository<>( Arrays.asList( raftFactoryV1, raftFactoryV2 ), ModifierProtocolInstaller.allClientInstallers );
eventLoopGroup = new NioEventLoopGroup();
Duration handshakeTimeout = config.get( CausalClusteringSettings.handshake_timeout );
handshakeClientInitializer = new HandshakeClientInitializer( applicationProtocolRepository, modifierProtocolRepository,
protocolInstallerRepository, pipelineBuilderFactory, handshakeTimeout, logProvider, logProvider );
bootstrap = new Bootstrap().group( eventLoopGroup ).channel( NioSocketChannel.class ).handler( handshakeClientInitializer );
}
@SuppressWarnings( "SameParameterValue" )
void connect( int port )
{
ChannelFuture channelFuture = bootstrap.connect( "localhost", port ).syncUninterruptibly();
channel = channelFuture.channel();
}
void disconnect()
{
if ( channel != null )
{
channel.close().syncUninterruptibly();
eventLoopGroup.shutdownGracefully( 0, TIMEOUT_SECONDS, SECONDS ).syncUninterruptibly();
}
}
ChannelFuture send( Object message )
{
return channel.writeAndFlush( message );
}
}
private Matcher<Object> messageMatches( RaftMessages.ClusterIdAwareMessage<? extends RaftMessages.RaftMessage> expected )
{
return new MessageMatcher( expected );
}
class MessageMatcher extends BaseMatcher<Object>
{
private final RaftMessages.ClusterIdAwareMessage<? extends RaftMessages.RaftMessage> expected;
MessageMatcher( RaftMessages.ClusterIdAwareMessage<? extends RaftMessages.RaftMessage> expected )
{
this.expected = expected;
}
@Override
public boolean matches( Object item )
{
if ( item instanceof RaftMessages.ClusterIdAwareMessage<?> )
{
RaftMessages.ClusterIdAwareMessage<?> message = (RaftMessages.ClusterIdAwareMessage<?>) item;
return message.clusterId().equals( expected.clusterId() ) && message.message().equals( expected.message() );
}
else
{
return false;
}
}
@Override
public void describeTo( Description description )
{
description.appendText( "Cluster ID " ).appendValue( expected.clusterId() ).appendText( " message " ).appendValue( expected.message() );
}
}
}