-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MasterClientTest.java
279 lines (242 loc) · 11.5 KB
/
MasterClientTest.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
/*
* Copyright (c) 2002-2018 "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.ha.upgrade;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Matchers;
import org.neo4j.cluster.ClusterSettings;
import org.neo4j.com.RequestContext;
import org.neo4j.com.ResourceReleaser;
import org.neo4j.com.Response;
import org.neo4j.com.Responses;
import org.neo4j.com.Server;
import org.neo4j.com.StoreIdTestFactory;
import org.neo4j.com.TransactionStream;
import org.neo4j.com.TransactionStreamResponse;
import org.neo4j.com.TxChecksumVerifier;
import org.neo4j.com.monitor.RequestMonitor;
import org.neo4j.com.storecopy.ResponseUnpacker;
import org.neo4j.com.storecopy.TransactionCommittingResponseUnpacker;
import org.neo4j.com.storecopy.TransactionCommittingResponseUnpacker.Dependencies;
import org.neo4j.helpers.HostnamePort;
import org.neo4j.helpers.collection.Visitor;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.ha.MasterClient320;
import org.neo4j.kernel.ha.com.master.ConversationManager;
import org.neo4j.kernel.ha.com.master.HandshakeResult;
import org.neo4j.kernel.ha.com.master.MasterImpl;
import org.neo4j.kernel.ha.com.master.MasterImpl.Monitor;
import org.neo4j.kernel.ha.com.master.MasterImplTest;
import org.neo4j.kernel.ha.com.master.MasterServer;
import org.neo4j.kernel.ha.com.slave.MasterClient;
import org.neo4j.kernel.impl.api.KernelTransactions;
import org.neo4j.kernel.impl.api.TransactionCommitProcess;
import org.neo4j.kernel.impl.api.TransactionToApply;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.kernel.impl.store.MismatchingStoreIdException;
import org.neo4j.kernel.impl.store.StoreId;
import org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation;
import org.neo4j.kernel.impl.transaction.command.Commands;
import org.neo4j.kernel.impl.transaction.log.LogPosition;
import org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel;
import org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader;
import org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart;
import org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit;
import org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader;
import org.neo4j.kernel.impl.transaction.tracing.CommitEvent;
import org.neo4j.kernel.lifecycle.LifeRule;
import org.neo4j.kernel.monitoring.ByteCounterMonitor;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.storageengine.api.TransactionApplicationMode;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.com.storecopy.ResponseUnpacker.NO_OP_RESPONSE_UNPACKER;
import static org.neo4j.com.storecopy.ResponseUnpacker.TxHandler;
import static org.neo4j.com.storecopy.TransactionCommittingResponseUnpacker.DEFAULT_BATCH_SIZE;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.kernel.impl.transaction.command.Commands.createNode;
public class MasterClientTest
{
private static final String MASTER_SERVER_HOST = "localhost";
private static final int MASTER_SERVER_PORT = 9191;
private static final int CHUNK_SIZE = 1024;
private static final int TIMEOUT = 2000;
private static final int TX_LOG_COUNT = 10;
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Rule
public final LifeRule life = new LifeRule( true );
private final Monitors monitors = new Monitors();
private final LogEntryReader<ReadableClosablePositionAwareChannel> logEntryReader =
new VersionAwareLogEntryReader<>();
@Test( expected = MismatchingStoreIdException.class )
public void newClientsShouldNotIgnoreStoreIdDifferences() throws Throwable
{
// Given
MasterImpl.SPI masterImplSPI =
MasterImplTest.mockedSpi( StoreIdTestFactory.newStoreIdForCurrentVersion( 1, 2, 3, 4 ) );
when( masterImplSPI.getTransactionChecksum( anyLong() ) ).thenReturn( 5L );
newMasterServer( masterImplSPI );
StoreId storeId = StoreIdTestFactory.newStoreIdForCurrentVersion( 5, 6, 7, 8 );
MasterClient masterClient = newMasterClient320( storeId );
// When
masterClient.handshake( 1, storeId );
}
@Test
public void clientShouldReadAndApplyTransactionLogsOnNewLockSessionRequest() throws Throwable
{
// Given
MasterImpl master = spy( newMasterImpl( mockMasterImplSpiWith( StoreId.DEFAULT ) ) );
doReturn( voidResponseWithTransactionLogs() ).when( master ).newLockSession( any( RequestContext.class ) );
newMasterServer( master );
Dependencies deps = mock( Dependencies.class );
TransactionCommitProcess commitProcess = mock( TransactionCommitProcess.class );
when( deps.commitProcess() ).thenReturn( commitProcess );
when( deps.logService() ).thenReturn( NullLogService.getInstance() );
when( deps.kernelTransactions() ).thenReturn( mock( KernelTransactions.class ) );
ResponseUnpacker unpacker = life.add(
new TransactionCommittingResponseUnpacker( deps, DEFAULT_BATCH_SIZE, 0 ) );
MasterClient masterClient = newMasterClient320( StoreId.DEFAULT, unpacker );
// When
masterClient.newLockSession( new RequestContext( 1, 2, 3, 4, 5 ) );
// Then
verify( commitProcess ).commit( any( TransactionToApply.class ),
any( CommitEvent.class ), any( TransactionApplicationMode.class ) );
}
@Test
public void endLockSessionDoesNotUnpackResponse() throws Throwable
{
StoreId storeId = new StoreId( 1, 2, 3, 4, 5 );
long txChecksum = 123;
long lastAppliedTx = 5;
ResponseUnpacker responseUnpacker = mock( ResponseUnpacker.class );
MasterImpl.SPI masterImplSPI = MasterImplTest.mockedSpi( storeId );
when( masterImplSPI.packTransactionObligationResponse( any( RequestContext.class ), Matchers.anyObject() ) )
.thenReturn( Responses.empty() );
when( masterImplSPI.getTransactionChecksum( anyLong() ) ).thenReturn( txChecksum );
newMasterServer( masterImplSPI );
MasterClient client = newMasterClient320( storeId, responseUnpacker );
HandshakeResult handshakeResult;
try ( Response<HandshakeResult> handshakeResponse = client.handshake( 1, storeId ) )
{
handshakeResult = handshakeResponse.response();
}
verify( responseUnpacker ).unpackResponse( any( Response.class ), any( TxHandler.class ) );
reset( responseUnpacker );
RequestContext context = new RequestContext( handshakeResult.epoch(), 1, 1, lastAppliedTx, txChecksum );
client.endLockSession( context, false );
verify( responseUnpacker, never() ).unpackResponse( any( Response.class ), any( TxHandler.class ) );
}
private static MasterImpl.SPI mockMasterImplSpiWith( StoreId storeId )
{
return when( mock( MasterImpl.SPI.class ).storeId() ).thenReturn( storeId ).getMock();
}
private MasterServer newMasterServer( MasterImpl.SPI masterImplSPI ) throws Throwable
{
MasterImpl masterImpl = new MasterImpl( masterImplSPI, mock(
ConversationManager.class ), mock( Monitor.class ), masterConfig() );
return newMasterServer( masterImpl );
}
private static MasterImpl newMasterImpl( MasterImpl.SPI masterImplSPI )
{
return new MasterImpl( masterImplSPI, mock(
ConversationManager.class ), mock( Monitor.class ), masterConfig() );
}
private MasterServer newMasterServer( MasterImpl masterImpl ) throws Throwable
{
return life.add( new MasterServer( masterImpl, NullLogProvider.getInstance(),
masterServerConfiguration(),
mock( TxChecksumVerifier.class ),
monitors.newMonitor( ByteCounterMonitor.class, MasterClient.class ),
monitors.newMonitor( RequestMonitor.class, MasterClient.class ), mock(
ConversationManager.class ), logEntryReader ) );
}
private MasterClient newMasterClient320( StoreId storeId ) throws Throwable
{
return newMasterClient320( storeId, NO_OP_RESPONSE_UNPACKER );
}
private MasterClient newMasterClient320( StoreId storeId, ResponseUnpacker responseUnpacker ) throws Throwable
{
return life.add( new MasterClient320( MASTER_SERVER_HOST, MASTER_SERVER_PORT, null, NullLogProvider.getInstance(),
storeId, TIMEOUT, TIMEOUT, 1, CHUNK_SIZE, responseUnpacker,
monitors.newMonitor( ByteCounterMonitor.class, MasterClient320.class ),
monitors.newMonitor( RequestMonitor.class, MasterClient320.class ), logEntryReader ) );
}
private static Response<Void> voidResponseWithTransactionLogs()
{
return new TransactionStreamResponse<>( null, StoreId.DEFAULT, new TransactionStream()
{
@Override
public void accept( Visitor<CommittedTransactionRepresentation,Exception> visitor ) throws Exception
{
for ( int i = 1; i <= TX_LOG_COUNT; i++ )
{
visitor.visit( committedTransactionRepresentation( i ) );
}
}
}, ResourceReleaser.NO_OP );
}
private static CommittedTransactionRepresentation committedTransactionRepresentation( int id )
{
return new CommittedTransactionRepresentation(
new LogEntryStart( id, id, id, id - 1, new byte[]{}, LogPosition.UNSPECIFIED ),
Commands.transactionRepresentation( createNode( 0 ) ),
new OnePhaseCommit( id, id ) );
}
private static Config masterConfig()
{
return Config.embeddedDefaults( stringMap( ClusterSettings.server_id.name(), "1" ) );
}
private static Server.Configuration masterServerConfiguration()
{
return new Server.Configuration()
{
@Override
public long getOldChannelThreshold()
{
return -1;
}
@Override
public int getMaxConcurrentTransactions()
{
return 1;
}
@Override
public int getChunkSize()
{
return CHUNK_SIZE;
}
@Override
public HostnamePort getServerAddress()
{
return new HostnamePort( MASTER_SERVER_HOST, MASTER_SERVER_PORT );
}
};
}
}