-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MultipleIndexPopulator.java
550 lines (492 loc) · 20.3 KB
/
MultipleIndexPopulator.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* 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 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.api.index;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.IntPredicate;
import org.neo4j.collection.primitive.PrimitiveLongSet;
import org.neo4j.function.ThrowingConsumer;
import org.neo4j.helpers.collection.Visitor;
import org.neo4j.kernel.api.exceptions.index.FlipFailedKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException;
import org.neo4j.kernel.api.index.IndexConfiguration;
import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.NodePropertyUpdate;
import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.api.index.SchemaIndexProvider.Descriptor;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.unsafe.impl.internal.dragons.FeatureToggles;
import static java.lang.String.format;
import static org.neo4j.collection.primitive.PrimitiveIntCollections.contains;
import static org.neo4j.kernel.impl.api.index.IndexPopulationFailure.failure;
/**
* {@link IndexPopulator} that allow population of multiple indexes during one iteration.
* Performs operations by calling corresponding operations of particular index populators.
*
* There are two ways data is fed to this multi-populator:
* <ul>
* <li>{@link #indexAllNodes()}, which is a blocking call and will scan the entire store and
* and generate updates that are fed into the {@link IndexPopulator populators}. Only a single call to this
* method should be made during the life time of a {@link MultipleIndexPopulator} and should be called by the
* same thread instantiating this instance.</li>
* <li>{@link #queue(NodePropertyUpdate)} which queues updates which will be read by the thread currently executing
* {@link #indexAllNodes()} and incorporated into that data stream. Calls to this method may come from any number
* of concurrent threads.</li>
* </ul>
*
* Usage of this class should be something like:
* <ol>
* <li>Instantiation.</li>
* <li>One or more calls to {@link #addPopulator(IndexPopulator, IndexDescriptor, Descriptor, IndexConfiguration,
* FlippableIndexProxy, FailedIndexProxyFactory, String)}.</li>
* <li>Call to {@link #create()} to create data structures and files to start accepting updates.</li>
* <li>Call to {@link #indexAllNodes()} (blocking call).</li>
* <li>While all nodes are being indexed, calls to {@link #queue(NodePropertyUpdate)} are accepted.</li>
* <li>Call to {@link #flipAfterPopulation()} after successful population, or {@link #fail(Throwable)} if not</li>
* </ol>
*/
public class MultipleIndexPopulator implements IndexPopulator
{
public static final String QUEUE_THRESHOLD_NAME = "queue_threshold";
private final int QUEUE_THRESHOLD = FeatureToggles.getInteger( getClass(), QUEUE_THRESHOLD_NAME, 20_000 );
// Concurrency queue since multiple concurrent threads may enqueue updates into it. It is important for this queue
// to have fast #size() method since it might be drained in batches
protected final Queue<NodePropertyUpdate> queue = new LinkedBlockingQueue<>();
// Populators are added into this list. The same thread adding populators will later call #indexAllNodes.
// Multiple concurrent threads might fail individual populations.
// Failed populations are removed from this list while iterating over it.
private final List<IndexPopulation> populations = new CopyOnWriteArrayList<>();
private final IndexStoreView storeView;
private final LogProvider logProvider;
protected final Log log;
private StoreScan<IndexPopulationFailedKernelException> storeScan;
public MultipleIndexPopulator( IndexStoreView storeView, LogProvider logProvider )
{
this.storeView = storeView;
this.logProvider = logProvider;
this.log = logProvider.getLog( IndexPopulationJob.class );
}
public IndexPopulation addPopulator(
IndexPopulator populator,
IndexDescriptor descriptor,
Descriptor providerDescriptor, IndexConfiguration config,
FlippableIndexProxy flipper,
FailedIndexProxyFactory failedIndexProxyFactory,
String indexUserDescription )
{
IndexPopulation population = createPopulation( populator, descriptor, config, providerDescriptor, flipper,
failedIndexProxyFactory, indexUserDescription );
populations.add( population );
return population;
}
protected IndexPopulation createPopulation( IndexPopulator populator, IndexDescriptor descriptor,
IndexConfiguration config, Descriptor providerDescriptor, FlippableIndexProxy flipper,
FailedIndexProxyFactory failedIndexProxyFactory, String indexUserDescription )
{
return new IndexPopulation( populator, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory,
indexUserDescription );
}
public boolean hasPopulators()
{
return !populations.isEmpty();
}
@Override
public void create()
{
forEachPopulation( population -> {
log.info( "Index population started: [%s]", population.indexUserDescription );
population.populator.create();
} );
}
@Override
public void drop() throws IOException
{
throw new UnsupportedOperationException( "Can't drop indexes from this populator implementation" );
}
@Override
public void add( Collection<NodePropertyUpdate> updates )
{
throw new UnsupportedOperationException( "Can't populate directly using this populator implementation. " );
}
public StoreScan<IndexPopulationFailedKernelException> indexAllNodes()
{
int[] labelIds = labelIds();
int[] propertyKeyIds = propertyKeyIds();
IntPredicate propertyKeyIdFilter = (propertyKeyId) -> contains( propertyKeyIds, propertyKeyId );
storeScan = storeView.visitNodes( labelIds, propertyKeyIdFilter, new NodePopulationVisitor(), null );
storeScan.configure(populations);
return storeScan;
}
/**
* Queues an update to be fed into the index populators. These updates come from changes being made
* to storage while a concurrent scan is happening to keep populators up to date with all latest changes.
*
* @param update {@link NodePropertyUpdate} to queue.
*/
public void queue( NodePropertyUpdate update )
{
queue.add( update );
}
/**
* Called if forced failure from the outside
*/
public void fail( Throwable failure )
{
for ( IndexPopulation population : populations )
{
fail( population, failure );
}
}
protected void fail( IndexPopulation population, Throwable failure )
{
boolean removed = populations.remove( population );
if ( !removed )
{
return;
}
// If the cause of index population failure is a conflict in a (unique) index, the conflict is the
// failure
// TODO do we need this?
if ( failure instanceof IndexPopulationFailedKernelException )
{
Throwable cause = failure.getCause();
if ( cause instanceof IndexEntryConflictException )
{
failure = cause;
}
}
// Index conflicts are expected (for unique indexes) so we don't need to log them.
if ( !(failure instanceof IndexEntryConflictException) /*TODO: && this is a unique index...*/ )
{
log.error( format( "Failed to populate index: [%s]", population.indexUserDescription ), failure );
}
// The flipper will have already flipped to a failed index context here, but
// it will not include the cause of failure, so we do another flip to a failed
// context that does.
// The reason for having the flipper transition to the failed index context in the first
// place is that we would otherwise introduce a race condition where updates could come
// in to the old context, if something failed in the job we send to the flipper.
population.flipToFailed( failure );
try
{
population.populator.markAsFailed( failure( failure ).asString() );
population.populator.close( false );
}
catch ( Throwable e )
{
log.error( format( "Unable to close failed populator for index: [%s]",
population.indexUserDescription ), e );
}
}
@Override
public void verifyDeferredConstraints( PropertyAccessor accessor ) throws IndexEntryConflictException, IOException
{
throw new UnsupportedOperationException( "Should not be called directly" );
}
public void verifyAllDeferredConstraints( PropertyAccessor accessor )
{
forEachPopulation( population -> population.populator.verifyDeferredConstraints( accessor ) );
}
@Override
public MultipleIndexUpdater newPopulatingUpdater( PropertyAccessor accessor )
{
Map<IndexPopulation,IndexUpdater> populationsWithUpdaters = new HashMap<>();
forEachPopulation( population -> {
IndexUpdater updater = population.populator.newPopulatingUpdater( accessor );
populationsWithUpdaters.put( population, updater );
} );
return new MultipleIndexUpdater( this, populationsWithUpdaters, logProvider );
}
@Override
public void close( boolean populationCompletedSuccessfully )
{
forEachPopulation( population -> population.populator.close( populationCompletedSuccessfully ) );
}
@Override
public void markAsFailed( String failure ) throws IOException
{
throw new UnsupportedOperationException( "Multiple index populator can't be marked as failed." );
}
@Override
public void includeSample( NodePropertyUpdate update )
{
throw new UnsupportedOperationException( "Multiple index populator can't perform index sampling." );
}
@Override
public void configureSampling( boolean onlineSampling )
{
throw new UnsupportedOperationException( "Multiple index populator can't be configured." );
}
@Override
public IndexSample sampleResult()
{
throw new UnsupportedOperationException( "Multiple index populator can't perform index sampling." );
}
public void replaceIndexCounts( long uniqueElements, long maxUniqueElements, long indexSize )
{
forEachPopulation( population ->
storeView.replaceIndexCounts( population.descriptor, uniqueElements, maxUniqueElements, indexSize ) );
}
public void flipAfterPopulation()
{
for ( IndexPopulation population : populations )
{
try
{
population.flip();
populations.remove( population );
}
catch ( Throwable t )
{
fail( population, t );
}
}
}
private int[] propertyKeyIds()
{
return populations.stream().mapToInt( population -> population.descriptor.getPropertyKeyId() ).toArray();
}
private int[] labelIds()
{
return populations.stream().mapToInt( population -> population.descriptor.getLabelId() ).toArray();
}
public void cancel()
{
replaceIndexCounts( 0, 0, 0 );
close( false );
}
void populateFromQueueBatched( long currentlyIndexedNodeId )
{
if ( isQueueThresholdReached() )
{
populateFromQueue( currentlyIndexedNodeId );
}
}
private boolean isQueueThresholdReached()
{
return queue.size() >= QUEUE_THRESHOLD;
}
protected void populateFromQueue( long currentlyIndexedNodeId )
{
populateFromQueueIfAvailable( currentlyIndexedNodeId );
}
private void populateFromQueueIfAvailable( long currentlyIndexedNodeId )
{
if ( !queue.isEmpty() )
{
try ( MultipleIndexUpdater updater = newPopulatingUpdater( storeView ) )
{
do
{
// no need to check for null as nobody else is emptying this queue
NodePropertyUpdate update = queue.poll();
storeScan.acceptUpdate( updater, update, currentlyIndexedNodeId );
}
while ( !queue.isEmpty() );
}
}
}
private void forEachPopulation( ThrowingConsumer<IndexPopulation,Exception> action )
{
for ( IndexPopulation population : populations )
{
try
{
action.accept( population );
}
catch ( Throwable failure )
{
fail( population, failure );
}
}
}
public static class MultipleIndexUpdater implements IndexUpdater
{
private final Map<IndexPopulation,IndexUpdater> populationsWithUpdaters;
private final MultipleIndexPopulator multipleIndexPopulator;
private final Log log;
MultipleIndexUpdater( MultipleIndexPopulator multipleIndexPopulator,
Map<IndexPopulation,IndexUpdater> populationsWithUpdaters, LogProvider logProvider )
{
this.multipleIndexPopulator = multipleIndexPopulator;
this.populationsWithUpdaters = populationsWithUpdaters;
this.log = logProvider.getLog( getClass() );
}
@Override
public void remove( PrimitiveLongSet nodeIds )
{
throw new UnsupportedOperationException( "Index populators don't do removal" );
}
@Override
public void process( NodePropertyUpdate update )
{
Iterator<Map.Entry<IndexPopulation,IndexUpdater>> iterator = populationsWithUpdaters.entrySet().iterator();
while ( iterator.hasNext() )
{
Map.Entry<IndexPopulation,IndexUpdater> entry = iterator.next();
IndexPopulation population = entry.getKey();
IndexUpdater updater = entry.getValue();
if ( population.isApplicable( update ) )
{
try
{
updater.process( update );
}
catch ( Throwable t )
{
try
{
updater.close();
}
catch ( Throwable ce )
{
log.error( format( "Failed to close index updater: [%s]", updater ), ce );
}
iterator.remove();
multipleIndexPopulator.fail( population, t );
}
}
}
}
@Override
public void close()
{
Iterator<Map.Entry<IndexPopulation,IndexUpdater>> iterator = populationsWithUpdaters.entrySet().iterator();
while ( iterator.hasNext() )
{
Map.Entry<IndexPopulation,IndexUpdater> entry = iterator.next();
IndexPopulation population = entry.getKey();
IndexUpdater updater = entry.getValue();
try
{
updater.close();
}
catch ( Throwable t )
{
iterator.remove();
multipleIndexPopulator.fail( population, t );
}
}
}
}
public class IndexPopulation
{
public final IndexPopulator populator;
final IndexDescriptor descriptor;
final IndexConfiguration config;
final SchemaIndexProvider.Descriptor providerDescriptor;
final IndexCountsRemover indexCountsRemover;
final FlippableIndexProxy flipper;
final FailedIndexProxyFactory failedIndexProxyFactory;
final String indexUserDescription;
private Collection<NodePropertyUpdate> applicableUpdates = new ArrayList<>();
IndexPopulation(
IndexPopulator populator,
IndexDescriptor descriptor,
IndexConfiguration config,
Descriptor providerDescriptor,
FlippableIndexProxy flipper,
FailedIndexProxyFactory failedIndexProxyFactory,
String indexUserDescription )
{
this.populator = populator;
this.descriptor = descriptor;
this.config = config;
this.providerDescriptor = providerDescriptor;
this.flipper = flipper;
this.failedIndexProxyFactory = failedIndexProxyFactory;
this.indexUserDescription = indexUserDescription;
this.indexCountsRemover = new IndexCountsRemover( storeView, descriptor );
}
private void flipToFailed( Throwable t )
{
flipper.flipTo( new FailedIndexProxy( descriptor, config, providerDescriptor, indexUserDescription,
populator, failure( t ), indexCountsRemover, logProvider ) );
}
private void addAll( Collection<NodePropertyUpdate> updates )
throws IndexEntryConflictException, IOException
{
for ( NodePropertyUpdate update : updates )
{
if ( isApplicable( update ) )
{
populator.includeSample( update );
applicableUpdates.add( update );
}
}
if ( !applicableUpdates.isEmpty() )
{
addApplicable( applicableUpdates );
applicableUpdates.clear();
}
}
void addApplicable( Collection<NodePropertyUpdate> updates )
throws IOException, IndexEntryConflictException
{
populator.add( updates );
}
private boolean isApplicable( NodePropertyUpdate update )
{
return update.forLabel( descriptor.getLabelId() ) &&
update.getPropertyKeyId() == descriptor.getPropertyKeyId();
}
private void flip() throws FlipFailedKernelException
{
flipper.flip( () -> {
populateFromQueueIfAvailable( Long.MAX_VALUE );
IndexSample sample = populator.sampleResult();
storeView.replaceIndexCounts( descriptor, sample.uniqueValues(), sample.sampleSize(),
sample.indexSize() );
populator.close( true );
return null;
}, failedIndexProxyFactory );
log.info( "Index population completed. Index is now online: [%s]", indexUserDescription );
}
}
private class NodePopulationVisitor implements Visitor<NodePropertyUpdates,
IndexPopulationFailedKernelException>
{
@Override
public boolean visit( NodePropertyUpdates updates ) throws IndexPopulationFailedKernelException
{
add( updates );
populateFromQueueBatched( updates.getNodeId() );
return false;
}
private void add( NodePropertyUpdates updates )
{
forEachPopulation( population -> population.addAll( updates.getPropertyUpdates() ) );
}
}
}