-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ParallelNativeIndexPopulatorTest.java
360 lines (324 loc) · 14.6 KB
/
ParallelNativeIndexPopulatorTest.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
357
358
359
360
/*
* 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.kernel.impl.index.schema;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.neo4j.function.ThrowingConsumer;
import org.neo4j.gis.spatial.index.curves.SpaceFillingCurveConfiguration;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.impl.SingleFilePageSwapperFactory;
import org.neo4j.io.pagecache.impl.muninn.MuninnPageCache;
import org.neo4j.io.pagecache.tracing.PageCacheTracer;
import org.neo4j.io.pagecache.tracing.cursor.PageCursorTracerSupplier;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.NodePropertyAccessor;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.index.schema.config.ConfiguredSpaceFillingCurveSettingsCache;
import org.neo4j.kernel.impl.index.schema.config.IndexSpecificSpaceFillingCurveSettingsCache;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.storageengine.api.schema.StoreIndexDescriptor;
import org.neo4j.test.extension.Inject;
import org.neo4j.test.extension.TestDirectoryExtension;
import org.neo4j.test.rule.TestDirectory;
import static java.util.Arrays.asList;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.io.pagecache.tracing.cursor.context.EmptyVersionContextSupplier.EMPTY;
import static org.neo4j.kernel.api.index.IndexDirectoryStructure.directoriesByProvider;
import static org.neo4j.kernel.api.index.IndexEntryUpdate.add;
import static org.neo4j.kernel.api.schema.SchemaDescriptorFactory.forLabel;
import static org.neo4j.kernel.configuration.Config.defaults;
import static org.neo4j.kernel.impl.scheduler.JobSchedulerFactory.createInitialisedScheduler;
import static org.neo4j.storageengine.api.schema.IndexDescriptorFactory.forSchema;
import static org.neo4j.values.storable.Values.longValue;
@ExtendWith( TestDirectoryExtension.class )
class ParallelNativeIndexPopulatorTest
{
private static final int THREADS = 4;
private static final StoreIndexDescriptor DESCRIPTOR = forSchema( forLabel( 1, 1 ), GenericNativeIndexProvider.DESCRIPTOR ).withId( 1 );
@Inject
TestDirectory directory;
private final Config defaults = defaults();
private final ConfiguredSpaceFillingCurveSettingsCache settingsCache = new ConfiguredSpaceFillingCurveSettingsCache( defaults );
private final IndexSpecificSpaceFillingCurveSettingsCache spatialSettings =
new IndexSpecificSpaceFillingCurveSettingsCache( settingsCache, new HashMap<>() );
private final GenericLayout layout = new GenericLayout( 1, spatialSettings );
private final AtomicLong next = new AtomicLong();
private ExecutorService executorService;
private File baseIndexFile;
@BeforeEach
void startExecutor()
{
executorService = Executors.newFixedThreadPool( THREADS );
baseIndexFile = directory.file( "index" );
}
@AfterEach
void stopExecutor()
{
executorService.shutdown();
}
@Test
void shouldCreateThreadLocalParts() throws ExecutionException, InterruptedException, IndexEntryConflictException
{
// given
Thread mainThread = Thread.currentThread();
ConcurrentMap<Thread,NativeIndexPopulator> partPopulators = new ConcurrentHashMap<>();
ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator =
new ParallelNativeIndexPopulator<>( baseIndexFile, layout, mockPartSupplier( partPopulators, this::mockNativeIndexPopulator ) );
// when
int batchCountPerThread = 10;
applyBatchesInParallel( populator, batchCountPerThread );
// then
assertEquals( THREADS, partPopulators.size() );
for ( Thread thread : partPopulators.keySet() )
{
if ( thread != mainThread )
{
NativeIndexPopulator partPopulator = partPopulators.get( thread );
verify( partPopulator, times( batchCountPerThread ) ).add( anyCollection() );
}
}
}
@Test
void shouldApplyUpdatesOnEachPart() throws ExecutionException, InterruptedException, IndexEntryConflictException
{
// given
Thread mainThread = Thread.currentThread();
ConcurrentMap<Thread,NativeIndexPopulator> partPopulators = new ConcurrentHashMap<>();
ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator =
new ParallelNativeIndexPopulator<>( baseIndexFile, layout, mockPartSupplier( partPopulators, this::mockNativeIndexPopulator ) );
int batchCountPerThread = 10;
// when
applyBatchesInParallel( populator, batchCountPerThread );
applyUpdates( populator, next );
applyBatchesInParallel( populator, batchCountPerThread );
applyUpdates( populator, next );
applyBatchesInParallel( populator, batchCountPerThread );
// then
assertEquals( THREADS, partPopulators.size() );
for ( Thread thread : partPopulators.keySet() )
{
if ( thread != mainThread )
{
NativeIndexPopulator partPopulator = partPopulators.get( thread );
verify( partPopulator, times( batchCountPerThread * 3 ) ).add( anyCollection() );
CountingIndexUpdater updater = (CountingIndexUpdater) partPopulator.newPopulatingUpdater();
assertEquals( 10, updater.count );
}
}
}
@Test
void shouldDropAllPartsOnClose() throws ExecutionException, InterruptedException
{
// Only test false since merging on these mocked populators will fail
shouldDropAllParts( populator -> populator.close( false ) );
}
@Test
void shouldDropAllPartsOnDrop() throws ExecutionException, InterruptedException
{
shouldDropAllParts( ParallelNativeIndexPopulator::drop );
}
private void shouldDropAllParts( Consumer<ParallelNativeIndexPopulator<GenericKey,NativeIndexValue>> method )
throws ExecutionException, InterruptedException
{
// given
ConcurrentMap<Thread,NativeIndexPopulator> partPopulators = new ConcurrentHashMap<>();
ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator =
new ParallelNativeIndexPopulator<>( baseIndexFile, layout, mockPartSupplier( partPopulators, this::failOnDropNativeIndexPopulator ) );
populator.create();
applyBatchesInParallel( populator, 1 );
// when
try
{
method.accept( populator );
fail( "Should have failed" );
}
catch ( CustomFailure e )
{
// then good
}
// then
for ( NativeIndexPopulator part : partPopulators.values() )
{
verify( part ).drop();
}
}
@Test
void shouldMergePartsOnAccessingVerifyDeferredConstraintsAfterPopulation() throws Exception
{
shouldMergePartsOnAccessingFirstCompleteMethodAfterPopulation( populator -> populator.verifyDeferredConstraints( mock( NodePropertyAccessor.class ) ) );
}
@Test
void shouldMergePartsOnAccessingSampleResultAfterPopulation() throws Exception
{
shouldMergePartsOnAccessingFirstCompleteMethodAfterPopulation( ParallelNativeIndexPopulator::sampleResult );
}
private void shouldMergePartsOnAccessingFirstCompleteMethodAfterPopulation(
ThrowingConsumer<ParallelNativeIndexPopulator<GenericKey,NativeIndexValue>,IndexEntryConflictException> method ) throws Exception
{
try ( EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction(); JobScheduler jobScheduler = createInitialisedScheduler() )
{
SingleFilePageSwapperFactory swapper = new SingleFilePageSwapperFactory();
swapper.open( fs, defaults );
try ( PageCache pageCache = new MuninnPageCache( swapper, 1_000, PageCacheTracer.NULL, PageCursorTracerSupplier.NULL, EMPTY, jobScheduler ) )
{
// given
NativeIndexPopulatorPartSupplier<GenericKey,NativeIndexValue> partSupplier =
file -> new GenericNativeIndexPopulator( pageCache, (FileSystemAbstraction) fs, file, layout, IndexProvider.Monitor.EMPTY, DESCRIPTOR,
spatialSettings, directoriesByProvider( directory.directory() ).forProvider( GenericNativeIndexProvider.DESCRIPTOR ),
mock( SpaceFillingCurveConfiguration.class ), false, !file.equals( baseIndexFile ) );
ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator = new ParallelNativeIndexPopulator<>( baseIndexFile, layout, partSupplier );
try
{
populator.create();
applyBatchesInParallel( populator, 100 );
// when
method.accept( populator );
// then
NodeValueIterator results = new NodeValueIterator();
try ( NativeIndexReader<GenericKey,NativeIndexValue> reader = populator.newReader() )
{
reader.query( results, IndexOrder.NONE, true, IndexQuery.exists( 1 ) );
long nextExpectedId = 0;
while ( results.hasNext() )
{
long id = results.next();
assertEquals( nextExpectedId++, id );
}
assertEquals( nextExpectedId, next.get() );
}
}
finally
{
populator.close( true );
}
}
}
}
private NativeIndexPopulatorPartSupplier<GenericKey,NativeIndexValue> mockPartSupplier( ConcurrentMap<Thread,NativeIndexPopulator> partPopulators,
Supplier<NativeIndexPopulator<GenericKey,NativeIndexValue>> partSupplier )
{
return file ->
{
NativeIndexPopulator<GenericKey,NativeIndexValue> part = partSupplier.get();
if ( !file.equals( baseIndexFile ) )
{
assertNull( partPopulators.put( Thread.currentThread(), part ) );
}
return part;
};
}
private NativeIndexPopulator<GenericKey,NativeIndexValue> mockNativeIndexPopulator()
{
NativeIndexPopulator<GenericKey,NativeIndexValue> populator = mock( NativeIndexPopulator.class );
when( populator.newPopulatingUpdater() ).thenReturn( new CountingIndexUpdater() );
return populator;
}
private NativeIndexPopulator<GenericKey,NativeIndexValue> failOnDropNativeIndexPopulator()
{
NativeIndexPopulator<GenericKey,NativeIndexValue> populator = mockNativeIndexPopulator();
doThrow( CustomFailure.class ).when( populator ).drop();
return populator;
}
private void applyUpdates( ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator, AtomicLong next ) throws IndexEntryConflictException
{
try ( IndexUpdater updater = populator.newPopulatingUpdater( mock( NodePropertyAccessor.class ) ) )
{
for ( int i = 0; i < 5; i++ )
{
updater.process( update( next.incrementAndGet() ) );
}
}
}
private void applyBatchesInParallel( ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator, int batchCountPerThread )
throws ExecutionException, InterruptedException
{
CountDownLatch startSignal = new CountDownLatch( THREADS );
List<Future<Void>> futures = new ArrayList<>();
for ( int i = 0; i < THREADS; i++ )
{
futures.add( executorService.submit( () ->
{
// Wait for all to get into pole position, this is because we want to make sure all threads are used.
startSignal.countDown();
startSignal.await();
for ( int j = 0; j < batchCountPerThread; j++ )
{
populator.add( asList( update( next.getAndIncrement() ) ) );
}
return null;
} ) );
}
for ( Future<Void> future : futures )
{
future.get();
}
}
private IndexEntryUpdate<?> update( long id )
{
return add( id, DESCRIPTOR, longValue( id ) );
}
private static class CustomFailure extends RuntimeException
{
}
private static class CountingIndexUpdater implements IndexUpdater
{
private int count;
@Override
public void process( IndexEntryUpdate<?> update )
{
count++;
}
@Override
public void close()
{
}
}
}