-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AbstractKeyValueStore.java
479 lines (425 loc) · 16.2 KB
/
AbstractKeyValueStore.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
/*
* 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.store.kvstore;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.layout.DatabaseLayout;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.impl.FileIsNotMappedException;
import org.neo4j.io.pagecache.tracing.cursor.context.VersionContextSupplier;
import org.neo4j.kernel.impl.locking.LockWrapper;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.util.FeatureToggles;
import static org.neo4j.kernel.impl.locking.LockWrapper.readLock;
import static org.neo4j.kernel.impl.locking.LockWrapper.writeLock;
/**
* The base for building a key value store based on rotating immutable
* {@linkplain KeyValueStoreFile key/value store files}
*
* @param <Key> a base type for the keys stored in this store.
*/
@State( State.Strategy.CONCURRENT_HASH_MAP )
public abstract class AbstractKeyValueStore<Key> extends LifecycleAdapter
{
static final long MAX_LOOKUP_RETRY_COUNT = FeatureToggles.getLong( AbstractKeyValueStore.class, "maxLookupRetryCount", 1024 );
private final ReadWriteLock updateLock = new ReentrantReadWriteLock( /*fair=*/true );
private final Format format;
final RotationStrategy rotationStrategy;
private final RotationTimerFactory rotationTimerFactory;
volatile ProgressiveState<Key> state;
private DataInitializer<EntryUpdater<Key>> stateInitializer;
private final FileSystemAbstraction fs;
final int keySize;
final int valueSize;
private volatile boolean stopped;
public AbstractKeyValueStore( FileSystemAbstraction fs, PageCache pages, DatabaseLayout databaseLayout, RotationMonitor monitor,
RotationTimerFactory timerFactory, VersionContextSupplier versionContextSupplier, int keySize,
int valueSize, HeaderField<?>... headerFields )
{
this.fs = fs;
this.keySize = keySize;
this.valueSize = valueSize;
Rotation rotation = getClass().getAnnotation( Rotation.class );
if ( monitor == null )
{
monitor = RotationMonitor.NONE;
}
this.format = new Format( headerFields );
this.rotationStrategy = rotation.value().create( fs, pages, format, monitor, databaseLayout );
this.rotationTimerFactory = timerFactory;
this.state = new DeadState.Stopped<>( format, getClass().getAnnotation( State.class ).value(),
versionContextSupplier );
}
protected final void setEntryUpdaterInitializer( DataInitializer<EntryUpdater<Key>> stateInitializer )
{
this.stateInitializer = stateInitializer;
}
@Override
public String toString()
{
return String.format( "%s[state=%s, hasChanges=%s]", getClass().getSimpleName(), state, state.hasChanges() );
}
protected final <Value> Value lookup( Key key, Reader<Value> reader ) throws IOException
{
ValueLookup<Value> lookup = new ValueLookup<>( reader );
long retriesLeft = MAX_LOOKUP_RETRY_COUNT;
while ( retriesLeft > 0 )
{
ProgressiveState<Key> originalState = this.state;
try
{
return lookup.value( !originalState.lookup( key, lookup ) );
}
catch ( FileIsNotMappedException e )
{
// if the state has changed we think the exception is caused by a rotation event. In this
// case we simply retry the lookup on the rotated state. Otherwise we rethrow.
if ( originalState == this.state )
{
throw e;
}
}
retriesLeft--;
}
throw new IOException( String.format( "Failed to lookup `%s` in key value store, after %d retries", key, MAX_LOOKUP_RETRY_COUNT ) );
}
/** Introspective feature, not thread safe. */
protected final void visitAll( Visitor visitor ) throws IOException
{
ProgressiveState<Key> state = this.state;
if ( visitor instanceof MetadataVisitor )
{
((MetadataVisitor) visitor).visitMetadata( state.file(), headers(), state.storedEntryCount() );
}
try ( DataProvider provider = state.dataProvider() )
{
transfer( provider, visitor );
}
}
protected final void visitFile( File path, Visitor visitor ) throws IOException
{
try ( KeyValueStoreFile file = rotationStrategy.openStoreFile( path ) )
{
if ( visitor instanceof MetadataVisitor )
{
((MetadataVisitor) visitor).visitMetadata( path, file.headers(), file.entryCount() );
}
try ( DataProvider provider = file.dataProvider() )
{
transfer( provider, visitor );
}
}
}
protected abstract Key readKey( ReadableBuffer key ) throws UnknownKey;
protected abstract void writeKey( Key key, WritableBuffer buffer );
protected abstract void writeFormatSpecifier( WritableBuffer formatSpecifier );
protected abstract Headers initialHeaders( long version );
protected abstract int compareHeaders( Headers lhs, Headers rhs );
protected boolean include( Key key, ReadableBuffer value )
{
return true;
}
protected final Headers headers()
{
return state.headers();
}
public int totalEntriesStored()
{
return state.storedEntryCount();
}
public final File currentFile()
{
return state.file();
}
@Override
public final void init() throws IOException
{
try ( LockWrapper ignored = writeLock( updateLock ) )
{
state = state.initialize( rotationStrategy );
}
}
@Override
public final void start() throws IOException
{
try ( LockWrapper ignored = writeLock( updateLock ) )
{
state = state.start( stateInitializer );
}
}
protected final Optional<EntryUpdater<Key>> updater( final long version )
{
try ( LockWrapper lock = readLock( updateLock ) )
{
return state.optionalUpdater( version, lock.get() );
}
}
protected final EntryUpdater<Key> updater()
{
try ( LockWrapper lock = readLock( updateLock ) )
{
return state.unsafeUpdater( lock.get() );
}
}
protected final EntryUpdater<Key> resetter( long version )
{
try ( LockWrapper lock = writeLock( updateLock ) )
{
ProgressiveState<Key> current = state;
return current.resetter( lock.get(), new RotationTask( version ) );
}
}
/**
* Prepare for rotation. Sets up the internal structures to ensure that all changes up to and including the changes
* of the specified version are applied before rotation takes place. This method does not block, however if all
* required changes have not been applied {@linkplain PreparedRotation#rotate() the rotate method} will block
* waiting for all changes to be applied. Invoking {@linkplain PreparedRotation#rotate() the rotate method} some
* time after all requested transactions have been applied is ok, since setting the store up for rotation does
* not block updates, it just sorts them into updates that apply before rotation and updates that apply after.
*
* @param version the smallest version to include in the rotation. Note that the actual rotated version might be a
* later version than this version. The actual rotated version is returned by
* {@link PreparedRotation#rotate()}.
*/
protected final PreparedRotation prepareRotation( final long version )
{
try ( LockWrapper ignored = writeLock( updateLock ) )
{
ProgressiveState<Key> prior = state;
if ( prior.storedVersion() == version && !prior.hasChanges() )
{
return () -> version;
}
return new RotationTask( version );
}
}
protected abstract void updateHeaders( Headers.Builder headers, long version );
@Override
public final void shutdown() throws IOException
{
try ( LockWrapper ignored = writeLock( updateLock ) )
{
stopped = true;
state = state.stop();
}
}
private boolean transfer( EntryVisitor<WritableBuffer> producer, EntryVisitor<ReadableBuffer> consumer )
throws IOException
{
BigEndianByteArrayBuffer key = new BigEndianByteArrayBuffer( keySize );
BigEndianByteArrayBuffer value = new BigEndianByteArrayBuffer( valueSize );
while ( producer.visit( key, value ) )
{
if ( !consumer.visit( key, value ) )
{
return false;
}
}
return true;
}
public Iterable<File> allFiles()
{
return StreamSupport.stream( rotationStrategy.candidateFiles().spliterator(), false )
.filter( fs::fileExists )
.collect( Collectors.toList() );
}
private class RotationTask implements PreparedRotation, Runnable
{
private final RotationState<Key> rotation;
RotationTask( long version )
{
state = this.rotation = state.prepareRotation( version );
}
@Override
public long rotate() throws IOException
{
return rotate( false );
}
@Override
public void run()
{
try ( LockWrapper ignored = writeLock( updateLock ) )
{
rotate( true );
}
catch ( IOException e )
{
throw new UnderlyingStorageException( e );
}
}
private long rotate( boolean force ) throws IOException
{
try ( RotationState<Key> rotation = this.rotation )
{
try
{
final long version = rotation.rotationVersion();
ProgressiveState<Key> next = rotation.rotate( force, rotationStrategy, rotationTimerFactory,
value -> updateHeaders( value, version ) );
try ( LockWrapper ignored = writeLock( updateLock ) )
{
state = next;
}
return version;
}
catch ( Throwable t )
{
// Rotation failed. Here we assume that rotation state remembers this so that closing it
// won't close the state as it was before rotation began, which we're reverting to right here.
try ( LockWrapper ignored = writeLock( updateLock ) )
{
// Only mark as failed if we're still running.
// If shutdown has been called while being in rotation state then shutdown will fail
// without closing the store. This means that rotation takes over that responsibility.
// Therefore avoid marking rotation state as failed in this case and let the store
// be naturally closed before leaving this method.
if ( !stopped )
{
state = rotation.markAsFailed();
}
}
throw t;
}
}
}
}
public abstract static class Reader<Value>
{
protected abstract Value parseValue( ReadableBuffer value );
protected Value defaultValue()
{
return null;
}
}
public abstract class Visitor implements KeyValueVisitor
{
@Override
public boolean visit( ReadableBuffer key, ReadableBuffer value )
{
try
{
return visitKeyValuePair( readKey( key ), value );
}
catch ( UnknownKey e )
{
return visitUnknownKey( e, key, value );
}
}
protected boolean visitUnknownKey( UnknownKey exception, ReadableBuffer key, ReadableBuffer value )
{
throw new IllegalArgumentException( exception.getMessage(), exception );
}
protected abstract boolean visitKeyValuePair( Key key, ReadableBuffer value );
}
private HeaderField<?>[] headerFieldsForFormat( ReadableBuffer formatSpecifier )
{
return format.defaultHeaderFieldsForFormat( formatSpecifier );
}
protected abstract long version( Headers headers );
private final class Format extends ProgressiveFormat implements KeyFormat<Key>
{
Format( HeaderField<?>... headerFields )
{
super( 512, headerFields );
}
@Override
protected void writeFormatSpecifier( WritableBuffer formatSpecifier )
{
AbstractKeyValueStore.this.writeFormatSpecifier( formatSpecifier );
}
@Override
protected HeaderField<?>[] headerFieldsForFormat( ReadableBuffer formatSpecifier )
{
return AbstractKeyValueStore.this.headerFieldsForFormat( formatSpecifier );
}
HeaderField<?>[] defaultHeaderFieldsForFormat( ReadableBuffer formatSpecifier )
{
return super.headerFieldsForFormat( formatSpecifier );
}
@Override
public void writeKey( Key key, WritableBuffer buffer )
{
AbstractKeyValueStore.this.writeKey( key, buffer );
}
@Override
public int compareHeaders( Headers lhs, Headers rhs )
{
return AbstractKeyValueStore.this.compareHeaders( lhs, rhs );
}
@Override
public Headers initialHeaders( long version )
{
return AbstractKeyValueStore.this.initialHeaders( version );
}
@Override
public int keySize()
{
return AbstractKeyValueStore.this.keySize;
}
@Override
public long version( Headers headers )
{
return AbstractKeyValueStore.this.version( headers );
}
@Override
public DataProvider filter( final DataProvider provider )
{
return new DataProvider()
{
@Override
public boolean visit( WritableBuffer key, WritableBuffer value ) throws IOException
{
while ( provider.visit( key, value ) )
{
try
{
if ( include( readKey( key ), value ) )
{
return true;
}
}
catch ( UnknownKey e )
{
throw new IllegalArgumentException( e.getMessage(), e );
}
}
return false;
}
@Override
public void close() throws IOException
{
provider.close();
}
};
}
@Override
public int valueSize()
{
return AbstractKeyValueStore.this.valueSize;
}
}
}