-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CatchupServerIT.java
356 lines (306 loc) · 14.4 KB
/
CatchupServerIT.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* 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.catchup.storecopy;
import org.eclipse.collections.api.iterator.LongIterator;
import org.eclipse.collections.api.set.primitive.LongSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.neo4j.causalclustering.catchup.CatchUpClient;
import org.neo4j.causalclustering.catchup.CatchupClientBuilder;
import org.neo4j.causalclustering.identity.StoreId;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.NeoStoreDataSource;
import org.neo4j.kernel.impl.store.StoreType;
import org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer;
import org.neo4j.kernel.impl.transaction.state.NeoStoreFileListing;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.storageengine.api.StoreFileMetadata;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.io.fs.FileUtils.relativePath;
public class CatchupServerIT
{
private static final String EXISTING_FILE_NAME = "neostore.nodestore.db";
private static final StoreId WRONG_STORE_ID = new StoreId( 123, 221, 3131, 45678 );
private static final LogProvider LOG_PROVIDER = NullLogProvider.getInstance();
private static final String PROP_NAME = "name";
private static final String PROP = "prop";
public static final Label LABEL = label( "MyLabel" );
private static final String INDEX = "index";
private GraphDatabaseAPI graphDb;
private TestCatchupServer catchupServer;
private File temporaryDirectory;
private PageCache pageCache;
@Rule
public DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();
@Rule
public TestDirectory testDirectory = TestDirectory.testDirectory( fileSystemRule );
private CatchUpClient catchupClient;
private DefaultFileSystemAbstraction fsa = fileSystemRule.get();
@Before
public void startDb() throws Throwable
{
temporaryDirectory = testDirectory.directory( "temp" );
graphDb = (GraphDatabaseAPI) new TestGraphDatabaseFactory().setFileSystem( fsa ).newEmbeddedDatabase( testDirectory.directory() );
createLegacyIndex();
createPropertyIndex();
addData( graphDb );
catchupServer = new TestCatchupServer( fsa, graphDb );
catchupServer.start();
catchupClient = new CatchupClientBuilder().build();
catchupClient.start();
pageCache = graphDb.getDependencyResolver().resolveDependency( PageCache.class );
}
@After
public void stopDb() throws Throwable
{
pageCache.flushAndForce();
if ( graphDb != null )
{
graphDb.shutdown();
}
if ( catchupClient != null )
{
catchupClient.stop();
}
if ( catchupServer != null )
{
catchupServer.stop();
}
}
@Test
public void shouldListExpectedFilesCorrectly() throws Exception
{
// given (setup) required runtime subject dependencies
NeoStoreDataSource neoStoreDataSource = getNeoStoreDataSource( graphDb );
SimpleCatchupClient simpleCatchupClient = new SimpleCatchupClient( graphDb, fsa, catchupClient, catchupServer, temporaryDirectory, LOG_PROVIDER );
// when
PrepareStoreCopyResponse prepareStoreCopyResponse = simpleCatchupClient.requestListOfFilesFromServer();
simpleCatchupClient.close();
// then
listOfDownloadedFilesMatchesServer( neoStoreDataSource, prepareStoreCopyResponse.getFiles() );
// and downloaded files are identical to source
List<File> expectedCountStoreFiles = listServerExpectedNonReplayableFiles( neoStoreDataSource );
for ( File storeFileSnapshot : expectedCountStoreFiles )
{
fileContentEquals( databaseFileToClientFile( storeFileSnapshot ), storeFileSnapshot );
}
// and
assertTransactionIdMatches( prepareStoreCopyResponse.lastTransactionId() );
//and
assertTrue( "Expected an empty set of ids. Found size " + prepareStoreCopyResponse.getIndexIds().size(),
prepareStoreCopyResponse.getIndexIds().isEmpty() );
}
@Test
public void shouldCommunicateErrorIfStoreIdDoesNotMatchRequest() throws Exception
{
// given (setup) required runtime subject dependencies
addData( graphDb );
SimpleCatchupClient simpleCatchupClient = new SimpleCatchupClient( graphDb, fsa, catchupClient, catchupServer, temporaryDirectory, LOG_PROVIDER );
// when the list of files are requested from the server with the wrong storeId
PrepareStoreCopyResponse prepareStoreCopyResponse = simpleCatchupClient.requestListOfFilesFromServer( WRONG_STORE_ID );
simpleCatchupClient.close();
// then the response is not a list of files but an error
assertEquals( PrepareStoreCopyResponse.Status.E_STORE_ID_MISMATCH, prepareStoreCopyResponse.status() );
// and the list of files is empty because the request should have failed
File[] remoteFiles = prepareStoreCopyResponse.getFiles();
assertArrayEquals( new File[]{}, remoteFiles );
}
@Test
public void individualFileCopyWorks() throws Exception
{
// given a file exists on the server
addData( graphDb );
File existingFile = new File( temporaryDirectory, EXISTING_FILE_NAME );
// and
SimpleCatchupClient simpleCatchupClient = new SimpleCatchupClient( graphDb, fsa, catchupClient, catchupServer, temporaryDirectory, LOG_PROVIDER );
// when we copy that file
pageCache.flushAndForce();
StoreCopyFinishedResponse storeCopyFinishedResponse = simpleCatchupClient.requestIndividualFile( existingFile );
simpleCatchupClient.close();
// then the response is successful
assertEquals( StoreCopyFinishedResponse.Status.SUCCESS, storeCopyFinishedResponse.status() );
// then the contents matches
fileContentEquals( clientFileToDatabaseFile( existingFile ), existingFile );
}
@Test
public void individualIndexSnapshotCopyWorks() throws Exception
{
// given
NeoStoreDataSource neoStoreDataSource = getNeoStoreDataSource( graphDb );
List<File> expectingFiles = neoStoreDataSource.getNeoStoreFileListing().builder().excludeAll().includeSchemaIndexStoreFiles().build().stream().map(
StoreFileMetadata::file ).collect( toList() );
SimpleCatchupClient simpleCatchupClient = new SimpleCatchupClient( graphDb, fsa, catchupClient, catchupServer, temporaryDirectory, LOG_PROVIDER );
// and
LongIterator indexIds = getExpectedIndexIds( neoStoreDataSource ).longIterator();
// when
while ( indexIds.hasNext() )
{
long indexId = indexIds.next();
StoreCopyFinishedResponse response = simpleCatchupClient.requestIndexSnapshot( indexId );
simpleCatchupClient.close();
assertEquals( StoreCopyFinishedResponse.Status.SUCCESS, response.status() );
}
// then
fileContentEquals( expectingFiles );
}
@Test
public void individualFileCopyFailsIfStoreIdMismatch() throws Exception
{
// given a file exists on the server
addData( graphDb );
File expectedExistingFile = new File( graphDb.getStoreDir(), EXISTING_FILE_NAME );
// and
SimpleCatchupClient simpleCatchupClient = new SimpleCatchupClient( graphDb, fsa, catchupClient, catchupServer, temporaryDirectory, LOG_PROVIDER );
// when we copy that file using a different storeId
StoreCopyFinishedResponse storeCopyFinishedResponse = simpleCatchupClient.requestIndividualFile( expectedExistingFile, WRONG_STORE_ID );
simpleCatchupClient.close();
// then the response from the server should be an error message that describes a store ID mismatch
assertEquals( StoreCopyFinishedResponse.Status.E_STORE_ID_MISMATCH, storeCopyFinishedResponse.status() );
}
private void assertTransactionIdMatches( long lastTxId )
{
long expectedTransactionId = getCheckPointer( graphDb ).lastCheckPointedTransactionId();
assertEquals( expectedTransactionId, lastTxId);
}
private void fileContentEquals( Collection<File> countStore ) throws IOException
{
for ( File file : countStore )
{
fileContentEquals( databaseFileToClientFile( file ), file );
}
}
private File databaseFileToClientFile( File file ) throws IOException
{
String relativePathToDatabaseDir = relativePath( testDirectory.graphDbDir(), file );
return new File( temporaryDirectory, relativePathToDatabaseDir );
}
private File clientFileToDatabaseFile( File file ) throws IOException
{
String relativePathToDatabaseDir = relativePath( temporaryDirectory, file );
return new File( testDirectory.graphDbDir(), relativePathToDatabaseDir );
}
private void fileContentEquals( File fileA, File fileB ) throws IOException
{
assertNotEquals( fileA.getPath(), fileB.getPath() );
String message = String.format( "Expected file: %s\ndoes not match actual file: %s", fileA, fileB );
assertEquals( message, StoreCopyClientIT.fileContent( fileA, fsa ), StoreCopyClientIT.fileContent( fileB, fsa ) );
}
private void listOfDownloadedFilesMatchesServer( NeoStoreDataSource neoStoreDataSource, File[] files )
throws IOException
{
List<String> expectedStoreFiles = getExpectedStoreFiles( neoStoreDataSource );
List<String> givenFile = Arrays.stream( files ).map( File::getName ).collect( toList() );
assertThat( givenFile, containsInAnyOrder( expectedStoreFiles.toArray( new String[givenFile.size()] ) ) );
}
private static LongSet getExpectedIndexIds( NeoStoreDataSource neoStoreDataSource )
{
return neoStoreDataSource.getNeoStoreFileListing().getNeoStoreFileIndexListing().getIndexIds();
}
private static List<File> listServerExpectedNonReplayableFiles( NeoStoreDataSource neoStoreDataSource ) throws IOException
{
try ( Stream<StoreFileMetadata> countStoreStream = neoStoreDataSource.getNeoStoreFileListing().builder().excludeAll()
.includeNeoStoreFiles().build().stream();
Stream<StoreFileMetadata> explicitIndexStream = neoStoreDataSource.getNeoStoreFileListing().builder().excludeAll()
.includeExplicitIndexStoreStoreFiles().build().stream() )
{
return Stream.concat( countStoreStream.filter( isCountFile() ), explicitIndexStream ).map( StoreFileMetadata::file ).collect( toList() );
}
}
private List<String> getExpectedStoreFiles( NeoStoreDataSource neoStoreDataSource ) throws IOException
{
NeoStoreFileListing.StoreFileListingBuilder builder = neoStoreDataSource.getNeoStoreFileListing().builder();
builder.excludeLogFiles().excludeExplicitIndexStoreFiles().excludeSchemaIndexStoreFiles().excludeAdditionalProviders();
try ( Stream<StoreFileMetadata> stream = builder.build().stream() )
{
return stream.filter( isCountFile().negate() ).map( sfm -> sfm.file().getName() ).collect( toList() );
}
}
private static Predicate<StoreFileMetadata> isCountFile()
{
return storeFileMetadata -> StoreType.typeOf( storeFileMetadata.file().getName() ).filter( f -> f == StoreType.COUNTS ).isPresent();
}
private static void addData( GraphDatabaseAPI graphDb )
{
try ( Transaction tx = graphDb.beginTx() )
{
Node node = graphDb.createNode();
node.addLabel( LABEL );
node.setProperty( PROP_NAME, "Neo" );
node.setProperty( PROP, Math.random() * 10000 );
graphDb.createNode().createRelationshipTo( node, RelationshipType.withName( "KNOWS" ) );
tx.success();
}
}
private void createPropertyIndex()
{
try ( Transaction tx = graphDb.beginTx() )
{
graphDb.schema().indexFor( LABEL ).on( PROP_NAME ).create();
tx.success();
}
}
private void createLegacyIndex()
{
try ( Transaction tx = graphDb.beginTx() )
{
Index<Node> nodeIndex = graphDb.index().forNodes( INDEX );
nodeIndex.add( graphDb.createNode(), "some-key", "som-value" );
tx.success();
}
}
private static CheckPointer getCheckPointer( GraphDatabaseAPI graphDb )
{
return graphDb.getDependencyResolver().resolveDependency( CheckPointer.class );
}
private static NeoStoreDataSource getNeoStoreDataSource( GraphDatabaseAPI graphDb )
{
return graphDb.getDependencyResolver().resolveDependency( NeoStoreDataSource.class );
}
}