-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FlippableIndexProxy.java
473 lines (443 loc) · 14.3 KB
/
FlippableIndexProxy.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
/*
* Copyright (c) 2002-2017 "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.File;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.kernel.api.exceptions.index.ExceptionDuringFlipKernelException;
import org.neo4j.kernel.api.exceptions.index.FlipFailedKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexActivationFailedKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexProxyAlreadyClosedKernelException;
import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.api.index.updater.DelegatingIndexUpdater;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.storageengine.api.schema.PopulationProgress;
public class FlippableIndexProxy implements IndexProxy
{
private volatile boolean closed;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock( true );
private volatile IndexProxyFactory flipTarget;
// This variable below is volatile because it can be changed in flip or flipTo
// and even though it may look like acquiring the read lock, when using this variable
// for various things, execution flow would go through a memory barrier of some sort.
// But it turns out that that may not be the case. F.ex. ReentrantReadWriteLock
// code uses unsafe compareAndSwap that sort of circumvents an equivalent of a volatile read.
private volatile IndexProxy delegate;
private boolean started;
public FlippableIndexProxy()
{
this( null );
}
public FlippableIndexProxy( IndexProxy originalDelegate )
{
this.delegate = originalDelegate;
}
@Override
public void start() throws IOException
{
lock.readLock().lock();
try
{
delegate.start();
started = true;
}
finally
{
lock.readLock().unlock();
}
}
@Override
public IndexUpdater newUpdater( IndexUpdateMode mode )
{
// Making use of reentrant locks to ensure that the delegate's constructor is called under lock protection
// while still retaining the lock until a call to close on the returned IndexUpdater
lock.readLock().lock();
try
{
return new LockingIndexUpdater( delegate.newUpdater( mode ) );
}
finally
{
lock.readLock().unlock();
}
}
@Override
public Future<Void> drop() throws IOException
{
lock.readLock().lock();
try
{
closed = true;
return delegate.drop();
}
finally
{
lock.readLock().unlock();
}
}
/**
* The {@code force()}-method is called during log rotation. At this time we do not want to wait for locks held by
* {@link LockingIndexUpdater}. Waiting on such locks would cause a serious risk of deadlocks, since very likely
* the reader we would be waiting on would be waiting on the log rotation lock held by the thread calling this
* method. The reason we would wait for a read lock while trying to acquire a read lock is if there is a third
* thread waiting on the write lock, probably an index populator wanting to
* {@linkplain #flip(Callable, FailedIndexProxyFactory) flip the index into active state}.
* <p/>
* We avoid this deadlock situation by "barging" on the read lock, i.e. acquire it in an <i>unfair</i> way, where
* we don't care about waiting threads, only about whether the exclusive lock is held or not.
*/
@Override
public void force() throws IOException
{
barge( lock.readLock() ); // see javadoc of this method (above) for rationale on why we use barge(...) here
try
{
delegate.force();
}
finally
{
lock.readLock().unlock();
}
}
/**
* Acquire the {@code ReadLock} in an <i>unfair</i> way, without waiting for queued up writers.
* <p/>
* The {@link ReentrantReadWriteLock.ReadLock#tryLock() tryLock}-method of the {@code ReadLock} implementation of
* {@code ReentrantReadWriteLock} implements a <i>barging</i> behaviour, where if an exclusive lock is not held,
* the shared lock will be acquired, even if there are other threads waiting for the lock. This behaviour is
* regardless of whether the lock is fair or not.
* <p/>
* This allows us to avoid deadlocks where readers would wait for writers that wait for readers in critical
* methods.
* <p/>
* The naive way to implement this method would be:
* <pre><code>
* if ( !lock.tryLock() ) // try to barge
* lock.lock(); // fall back to normal blocking lock call
* </code></pre>
* This would however not implement the appropriate barging behaviour in a scenario like the following: Say the
* exclusive lock is held, and there is a queue waiting containing first a reader and then a writer, in this case
* the {@code tryLock()} method will return false. If the writer then finishes between the naive implementation
* exiting {@code tryLock()} and before entering {@code lock()} the {@code barge(...)} method would now block in
* the exact way we don't want it to block, with a read lock held and a writer waiting.<br/>
* In order to get around this situation, the implementation of this method uses a
* {@linkplain Lock#tryLock(long, TimeUnit) timed wait} in a retry-loop in order to ensure that we make another
* attempt to barge the lock at a later point.
* <p/>
* This method is written to be compatible with the signature of {@link Lock#lock()}, which is not interruptible,
* but implemented based on the interruptible {@link Lock#tryLock(long, TimeUnit)}, so the implementation needs to
* remember being interrupted, and reset the flag before exiting, so that later invocations of interruptible
* methods detect the interruption.
*
* @param lock a {@link java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock}
*/
private static void barge( ReentrantReadWriteLock.ReadLock lock )
{
boolean interrupted = false;
// exponential retry back-off, no more than 1 second
for ( long timeout = 10; !lock.tryLock(); timeout = Math.min( 1000, timeout * 2 ) )
{
try
{
if ( lock.tryLock( timeout, TimeUnit.MILLISECONDS ) )
{
return;
}
}
// the barge()-method is uninterruptable, but implemented based on the interruptible tryLock()-method
catch ( InterruptedException e )
{
Thread.interrupted(); // ensure the interrupt flag is cleared
interrupted = true; // remember to set interrupt flag before we exit
}
}
if ( interrupted )
{
Thread.currentThread().interrupt(); // reset the interrupt flag
}
}
@Override
public IndexDescriptor getDescriptor()
{
lock.readLock().lock();
try
{
return delegate.getDescriptor();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public LabelSchemaDescriptor schema()
{
lock.readLock().lock();
try
{
return delegate.schema();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public SchemaIndexProvider.Descriptor getProviderDescriptor()
{
lock.readLock().lock();
try
{
return delegate.getProviderDescriptor();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public InternalIndexState getState()
{
lock.readLock().lock();
try
{
return delegate.getState();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public Future<Void> close() throws IOException
{
lock.readLock().lock();
try
{
closed = true;
return delegate.close();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public IndexReader newReader() throws IndexNotFoundKernelException
{
lock.readLock().lock();
try
{
return delegate.newReader();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public boolean awaitStoreScanCompleted() throws IndexPopulationFailedKernelException, InterruptedException
{
IndexProxy proxy;
do
{
lock.readLock().lock();
proxy = delegate;
lock.readLock().unlock();
} while ( proxy.awaitStoreScanCompleted() );
return true;
}
@Override
public void activate() throws IndexActivationFailedKernelException
{
// use write lock, since activate() might call flip*() which acquires a write lock itself.
lock.writeLock().lock();
try
{
delegate.activate();
}
finally
{
lock.writeLock().unlock();
}
}
@Override
public void validate() throws IndexPopulationFailedKernelException, UniquePropertyValueValidationException
{
lock.readLock().lock();
try
{
delegate.validate();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public ResourceIterator<File> snapshotFiles() throws IOException
{
lock.readLock().lock();
try
{
return delegate.snapshotFiles();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public IndexPopulationFailure getPopulationFailure() throws IllegalStateException
{
lock.readLock().lock();
try
{
return delegate.getPopulationFailure();
}
finally
{
lock.readLock().unlock();
}
}
@Override
public PopulationProgress getIndexPopulationProgress()
{
lock.readLock().lock();
try
{
return delegate.getIndexPopulationProgress();
}
finally
{
lock.readLock().unlock();
}
}
public void setFlipTarget( IndexProxyFactory flipTarget )
{
lock.writeLock().lock();
try
{
this.flipTarget = flipTarget;
}
finally
{
lock.writeLock().unlock();
}
}
public void flipTo( IndexProxy targetDelegate )
{
lock.writeLock().lock();
try
{
this.delegate = targetDelegate;
}
finally
{
lock.writeLock().unlock();
}
}
public void flip( Callable<Void> actionDuringFlip, FailedIndexProxyFactory failureDelegate )
throws FlipFailedKernelException
{
lock.writeLock().lock();
try
{
assertOpen();
try
{
actionDuringFlip.call();
this.delegate = flipTarget.create();
if ( started )
{
this.delegate.start();
}
}
catch ( Exception e )
{
this.delegate = failureDelegate.create( e );
throw new ExceptionDuringFlipKernelException( e );
}
}
finally
{
lock.writeLock().unlock();
}
}
@Override
public String toString()
{
return getClass().getSimpleName() + " -> " + delegate + "[target:" + flipTarget + "]";
}
private void assertOpen() throws IndexProxyAlreadyClosedKernelException
{
if ( closed )
{
throw new IndexProxyAlreadyClosedKernelException( this.getClass() );
}
}
@Override
public void verifyDeferredConstraints( PropertyAccessor accessor ) throws IndexEntryConflictException, IOException
{
lock.readLock().lock();
try
{
delegate.verifyDeferredConstraints( accessor );
}
finally
{
lock.readLock().unlock();
}
}
private class LockingIndexUpdater extends DelegatingIndexUpdater
{
private LockingIndexUpdater( IndexUpdater delegate )
{
super( delegate );
lock.readLock().lock();
}
@Override
public void close() throws IOException, IndexEntryConflictException
{
try
{
delegate.close();
}
finally
{
lock.readLock().unlock();
}
}
}
}