-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MuninnPageCache.java
1064 lines (968 loc) · 39.3 KB
/
MuninnPageCache.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002-2018 "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.io.pagecache.impl.muninn;
import java.io.File;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.Stream;
import org.neo4j.io.pagecache.FileHandle;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PageCacheOpenOptions;
import org.neo4j.io.pagecache.PageSwapperFactory;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.io.pagecache.impl.FileIsMappedException;
import org.neo4j.io.pagecache.tracing.EvictionEvent;
import org.neo4j.io.pagecache.tracing.EvictionRunEvent;
import org.neo4j.io.pagecache.tracing.FlushEventOpportunity;
import org.neo4j.io.pagecache.tracing.MajorFlushEvent;
import org.neo4j.io.pagecache.tracing.PageCacheTracer;
import org.neo4j.io.pagecache.tracing.PageFaultEvent;
import org.neo4j.io.pagecache.tracing.cursor.PageCursorTracerSupplier;
import org.neo4j.io.pagecache.tracing.cursor.context.VersionContextSupplier;
import org.neo4j.unsafe.impl.internal.dragons.MemoryManager;
import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil;
import static org.neo4j.unsafe.impl.internal.dragons.FeatureToggles.flag;
import static org.neo4j.unsafe.impl.internal.dragons.FeatureToggles.getInteger;
/**
* The Muninn {@link org.neo4j.io.pagecache.PageCache page cache} implementation.
* <pre>
* ....
* .;okKNWMUWN0ko,
* O'er Mithgarth Hugin and Munin both ;0WMUNINNMUNINNMUNOdc:.
* Each day set forth to fly; .OWMUNINNMUNI 00WMUNINNXko;.
* For Hugin I fear lest he come not home, .KMUNINNMUNINNMWKKWMUNINNMUNIN0l.
* But for Munin my care is more. .KMUNINNMUNINNMUNINNWKkdlc:::::::'
* .lXMUNINNMUNINNMUNINXo'
* .,lONMUNINNMUNINNMUNINNk'
* .,cox0NMUNINNMUNINNMUNINNMUNI:
* .;dONMUNINNMUNINNMUNINNMUNINNMUNIN'
* .';okKWMUNINNMUNINNMUNINNMUNINNMUNINNMUx
* .:dkKNWMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNN'
* .';lONMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNWl
* .:okXWMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNM0'
* .,oONMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNo
* .';lx0NMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUN0'
* ;kKWMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMWx'
* .,kWMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMXd'
* .;lkKNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMUNINNMNx;'
* .oNMUNINNMWNKOxoc;'';:cdkKNWMUNINNMUNINNMUNINNMUNINNMUNINWKx;'
* lkOkkdl:'´ `':lkWMUNINNMUNINNMUNINN0kdoc;'
* c0WMUNINNMUNINNMUWx'
* .;ccllllxNMUNIXo'
* lWMUWkK; .
* OMUNK.dNdc,....
* cWMUNlkWWWO:cl;.
* ;kWO,....',,,.
* cNd
* :Nk.
* cWK,
* .,ccxXWd.
* dWNkxkOdc::;.
* cNNo:ldo:.
* 'xo. ..
* </pre>
* <p>
* In Norse mythology, Huginn (from Old Norse "thought") and Muninn (Old Norse
* "memory" or "mind") are a pair of ravens that fly all over the world, Midgard,
* and bring information to the god Odin.
* </p>
* <p>
* This implementation of {@link org.neo4j.io.pagecache.PageCache} is optimised for
* configurations with large memory capacities and large stores, and uses sequence
* locks to make uncontended reads and writes fast.
* </p>
*/
public class MuninnPageCache implements PageCache
{
public static final byte ZERO_BYTE =
(byte) (flag( MuninnPageCache.class, "brandedZeroByte", false ) ? 0x0f : 0);
// Keep this many pages free and ready for use in faulting.
// This will be truncated to be no more than half of the number of pages
// in the cache.
private static final int pagesToKeepFree = getInteger(
MuninnPageCache.class, "pagesToKeepFree", 30 );
// This is how many times that, during cooperative eviction, we'll iterate through the entire set of pages looking
// for a page to evict, before we give up and throw CacheLiveLockException. This MUST be greater than 1.
private static final int cooperativeEvictionLiveLockThreshold = getInteger(
MuninnPageCache.class, "cooperativeEvictionLiveLockThreshold", 100 );
// This is a pre-allocated constant, so we can throw it without allocating any objects:
@SuppressWarnings( "ThrowableInstanceNeverThrown" )
private static final IOException oomException = new IOException(
"OutOfMemoryError encountered in the page cache background eviction thread" );
// The field offset to unsafely access the freelist field.
private static final long freelistOffset =
UnsafeUtil.getFieldOffset( MuninnPageCache.class, "freelist" );
// This is used as a poison-pill signal in the freelist, to inform any
// page faulting thread that it is now no longer possible to queue up and
// wait for more pages to be evicted, because the page cache has been shut
// down.
private static final FreePage shutdownSignal = new FreePage( null );
// A counter used to identify which background threads belong to which page cache.
private static final AtomicInteger pageCacheIdCounter = new AtomicInteger();
// This Executor runs all the background threads for all page cache instances. It allows us to reuse threads
// between multiple page cache instances, which is of no consequence in normal usage, but is quite useful for the
// many, many tests that create and close page caches all the time. We DO NOT want to take an Executor in through
// the constructor of the PageCache, because the Executors have too many configuration options, many of which are
// highly troublesome for our use case; caller-runs, bounded submission queues, bounded thread count, non-daemon
// thread factories, etc.
private static final Executor backgroundThreadExecutor = BackgroundThreadExecutor.INSTANCE;
private static final List<OpenOption> ignoredOpenOptions = Arrays.asList( (OpenOption) StandardOpenOption.APPEND,
StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.SPARSE );
private final int pageCacheId;
private final PageSwapperFactory swapperFactory;
private final int cachePageSize;
private final int keepFree;
private final PageCacheTracer pageCacheTracer;
private final MuninnPage[] pages;
// All PageCursors are initialised with their pointers pointing to the victim page. This way, we can do branch-free
// bounds checking of page accesses without fear of segfaulting newly allocated cursors.
final long victimPage;
// The freelist is a thread-safe linked-list of 2 types of objects. A link
// can either be a MuninnPage or a FreePage.
// Initially, most of the links are MuninnPages that are ready for the
// taking. Then towards the end, we have the last bunch of pages linked
// through FreePage objects. We make this transition because, once a
// MuninnPage has been removed from the list, it cannot be added back. The
// reason is that the MuninnPages are reused, and adding them back into the
// freelist would expose us to the ABA-problem, which can cause cycles to
// form. The FreePage objects, however, are single-use such that they don't
// exhibit the ABA-problem. In other words, eviction will never add
// MuninnPages to the freelist; it will only add free pages through a new
// FreePage object.
@SuppressWarnings( "unused" ) // This field is accessed via Unsafe.
private volatile Object freelist;
// Linked list of mappings - guarded by synchronized(this)
private volatile FileMapping mappedFiles;
// The thread that runs the eviction algorithm. We unpark this when we've run out of
// free pages to grab.
private volatile Thread evictionThread;
// True if the eviction thread is currently parked, without someone having
// signalled it to wake up. This is used as a weak guard for unparking the
// eviction thread, because calling unpark too much (from many page
// faulting threads) can cause contention on the locks protecting that
// threads scheduling meta-data in the OS kernel.
private volatile boolean evictorParked;
private volatile IOException evictorException;
// Flag for when page cache is closed - writes guarded by synchronized(this), reads can be unsynchronized
private volatile boolean closed;
// Only used by ensureThreadsInitialised while holding the monitor lock on this MuninnPageCache instance.
private boolean threadsInitialised;
// 'true' (the default) if we should print any exceptions we get when unmapping a file.
private boolean printExceptionsOnClose;
private final PageCursorTracerSupplier pageCursorTracerSupplier;
private final VersionContextSupplier versionContextSupplier;
/**
* Create page cache
* @param swapperFactory page cache swapper factory
* @param maxPages maximum number of pages
* @param cachePageSize page cache size
* @param pageCacheTracer global page cache tracer
* @param pageCursorTracerSupplier supplier of thread local (transaction local) page cursor tracer that will provide
* thread local page cache statistics
* @param versionContextSupplier supplier of thread local (transaction local) version context that will provide
* access to thread local version context
*/
public MuninnPageCache( PageSwapperFactory swapperFactory, int maxPages, int cachePageSize, PageCacheTracer pageCacheTracer,
PageCursorTracerSupplier pageCursorTracerSupplier, VersionContextSupplier versionContextSupplier )
{
verifyHacks();
verifyCachePageSizeIsPowerOfTwo( cachePageSize );
verifyMinimumPageCount( maxPages, cachePageSize );
this.pageCacheId = pageCacheIdCounter.incrementAndGet();
this.swapperFactory = swapperFactory;
this.cachePageSize = cachePageSize;
this.keepFree = Math.min( pagesToKeepFree, maxPages / 2 );
this.pageCacheTracer = pageCacheTracer;
this.pageCursorTracerSupplier = pageCursorTracerSupplier;
this.versionContextSupplier = versionContextSupplier;
this.pages = new MuninnPage[maxPages];
this.printExceptionsOnClose = true;
long alignment = swapperFactory.getRequiredBufferAlignment();
long expectedMaxMemory = ((long) maxPages) * cachePageSize; // cast to long prevents overflow
MemoryManager memoryManager = new MemoryManager( expectedMaxMemory, alignment );
this.victimPage = VictimPageReference.getVictimPage( cachePageSize );
Object pageList = null;
int pageIndex = maxPages;
while ( pageIndex-- > 0 )
{
MuninnPage page = new MuninnPage( cachePageSize, memoryManager );
page.tryExclusiveLock(); // All pages in the free-list are exclusively locked, and unlocked by page fault.
pages[pageIndex] = page;
if ( pageList == null )
{
FreePage freePage = new FreePage( page );
freePage.setNext( null );
pageList = freePage;
}
else if ( pageList instanceof FreePage
&& ((FreePage) pageList).count < keepFree )
{
FreePage freePage = new FreePage( page );
freePage.setNext( (FreePage) pageList );
pageList = freePage;
}
else
{
page.nextFree = pageList;
pageList = page;
}
}
UnsafeUtil.putObjectVolatile( this, freelistOffset, pageList );
}
private static void verifyHacks()
{
// Make sure that we have access to theUnsafe.
UnsafeUtil.assertHasUnsafe();
}
private static void verifyCachePageSizeIsPowerOfTwo( int cachePageSize )
{
int exponent = 31 - Integer.numberOfLeadingZeros( cachePageSize );
if ( 1 << exponent != cachePageSize )
{
throw new IllegalArgumentException(
"Cache page size must be a power of two, but was " + cachePageSize );
}
}
private static void verifyMinimumPageCount( int maxPages, int cachePageSize )
{
int minimumPageCount = 2;
if ( maxPages < minimumPageCount )
{
throw new IllegalArgumentException( String.format(
"Page cache must have at least %s pages (%s bytes of memory), but was given %s pages.",
minimumPageCount, minimumPageCount * cachePageSize, maxPages ) );
}
}
@Override
public synchronized PagedFile map( File file, int filePageSize, OpenOption... openOptions ) throws IOException
{
assertHealthy();
ensureThreadsInitialised();
if ( filePageSize > cachePageSize )
{
throw new IllegalArgumentException(
"Cannot map files with a filePageSize (" + filePageSize + ") that is greater than the " +
"cachePageSize (" + cachePageSize + ")" );
}
file = file.getCanonicalFile();
boolean createIfNotExists = false;
boolean truncateExisting = false;
boolean deleteOnClose = false;
boolean anyPageSize = false;
for ( OpenOption option : openOptions )
{
if ( option.equals( StandardOpenOption.CREATE ) )
{
createIfNotExists = true;
}
else if ( option.equals( StandardOpenOption.TRUNCATE_EXISTING ) )
{
truncateExisting = true;
}
else if ( option.equals( StandardOpenOption.DELETE_ON_CLOSE ) )
{
deleteOnClose = true;
}
else if ( option.equals( PageCacheOpenOptions.ANY_PAGE_SIZE ) )
{
anyPageSize = true;
}
else if ( !ignoredOpenOptions.contains( option ) )
{
throw new UnsupportedOperationException( "Unsupported OpenOption: " + option );
}
}
FileMapping current = mappedFiles;
// find an existing mapping
while ( current != null )
{
if ( current.file.equals( file ) )
{
MuninnPagedFile pagedFile = current.pagedFile;
if ( pagedFile.pageSize() != filePageSize && !anyPageSize )
{
String msg = "Cannot map file " + file + " with " +
"filePageSize " + filePageSize + " bytes, " +
"because it has already been mapped with a " +
"filePageSize of " + pagedFile.pageSize() +
" bytes.";
throw new IllegalArgumentException( msg );
}
if ( truncateExisting )
{
throw new UnsupportedOperationException( "Cannot truncate a file that is already mapped" );
}
pagedFile.incrementRefCount();
pagedFile.markDeleteOnClose( deleteOnClose );
return pagedFile;
}
current = current.next;
}
if ( filePageSize < Long.BYTES )
{
throw new IllegalArgumentException(
"Cannot map files with a filePageSize (" + filePageSize + ") that is less than " +
Long.BYTES + " bytes" );
}
// there was no existing mapping
MuninnPagedFile pagedFile = new MuninnPagedFile(
file,
this,
filePageSize,
swapperFactory, pageCacheTracer, pageCursorTracerSupplier, versionContextSupplier,
createIfNotExists,
truncateExisting );
pagedFile.incrementRefCount();
pagedFile.markDeleteOnClose( deleteOnClose );
current = new FileMapping( file, pagedFile );
current.next = mappedFiles;
mappedFiles = current;
pageCacheTracer.mappedFile( file );
return pagedFile;
}
@Override
public synchronized Optional<PagedFile> getExistingMapping( File file ) throws IOException
{
assertHealthy();
ensureThreadsInitialised();
file = file.getCanonicalFile();
MuninnPagedFile pagedFile = tryGetMappingOrNull( file );
if ( pagedFile != null )
{
pagedFile.incrementRefCount();
return Optional.of( pagedFile );
}
return Optional.empty();
}
private MuninnPagedFile tryGetMappingOrNull( File file ) throws IOException
{
FileMapping current = mappedFiles;
// find an existing mapping
while ( current != null )
{
if ( current.file.equals( file ) )
{
return current.pagedFile;
}
current = current.next;
}
// no mapping exists
return null;
}
@Override
public Stream<FileHandle> streamFilesRecursive( File directory ) throws IOException
{
return swapperFactory.streamFilesRecursive( directory.getCanonicalFile() ).map( this::checkingFileHandle );
}
private FileHandle checkingFileHandle( FileHandle fileHandle )
{
return new FileHandle()
{
@Override
public File getFile()
{
return fileHandle.getFile();
}
@Override
public File getRelativeFile()
{
return fileHandle.getRelativeFile();
}
@Override
public void rename( File targetFile, CopyOption... options ) throws IOException
{
synchronized ( MuninnPageCache.this )
{
File sourceFile = getFile();
sourceFile = sourceFile.getCanonicalFile();
targetFile = targetFile.getCanonicalFile();
assertNotMapped( sourceFile, FileIsMappedException.Operation.RENAME );
assertNotMapped( targetFile, FileIsMappedException.Operation.RENAME );
fileHandle.rename( targetFile, options );
}
}
@Override
public String toString()
{
return fileHandle.toString();
}
@Override
public void delete() throws IOException
{
synchronized ( MuninnPageCache.this )
{
assertNotMapped( getFile(), FileIsMappedException.Operation.DELETE );
fileHandle.delete();
}
}
};
}
private void assertNotMapped( File file, FileIsMappedException.Operation operation ) throws IOException
{
if ( tryGetMappingOrNull( file ) != null )
{
throw new FileIsMappedException( file, operation );
}
}
@Override
public synchronized List<PagedFile> listExistingMappings() throws IOException
{
assertHealthy();
ensureThreadsInitialised();
List<PagedFile> list = new ArrayList<>();
FileMapping current = mappedFiles;
while ( current != null )
{
MuninnPagedFile pagedFile = current.pagedFile;
pagedFile.incrementRefCount();
list.add( pagedFile );
current = current.next;
}
return list;
}
/**
* Note: Must be called while synchronizing on the MuninnPageCache instance.
*/
private void ensureThreadsInitialised() throws IOException
{
if ( threadsInitialised )
{
return;
}
threadsInitialised = true;
try
{
backgroundThreadExecutor.execute( new EvictionTask( this ) );
}
catch ( Exception e )
{
IOException exception = new IOException( e );
try
{
close();
}
catch ( Exception closeException )
{
exception.addSuppressed( closeException );
}
throw exception;
}
}
synchronized void unmap( MuninnPagedFile file )
{
if ( file.decrementRefCount() )
{
// This was the last reference!
// Find and remove the existing mapping:
FileMapping prev = null;
FileMapping current = mappedFiles;
while ( current != null )
{
if ( current.pagedFile == file )
{
if ( prev == null )
{
mappedFiles = current.next;
}
else
{
prev.next = current.next;
}
pageCacheTracer.unmappedFile( current.file );
flushAndCloseWithoutFail( file );
break;
}
prev = current;
current = current.next;
}
}
}
private void flushAndCloseWithoutFail( MuninnPagedFile file )
{
boolean flushedAndClosed = false;
boolean printedFirstException = false;
do
{
try
{
file.flushAndForceForClose();
file.closeSwapper();
flushedAndClosed = true;
}
catch ( IOException e )
{
if ( printExceptionsOnClose && !printedFirstException )
{
printedFirstException = true;
try
{
e.printStackTrace();
}
catch ( Exception ignore )
{
}
}
}
}
while ( !flushedAndClosed );
}
public void setPrintExceptionsOnClose( boolean enabled )
{
this.printExceptionsOnClose = enabled;
}
@Override
public void flushAndForce() throws IOException
{
flushAndForce( IOLimiter.unlimited() );
}
@Override
public synchronized void flushAndForce( IOLimiter limiter ) throws IOException
{
if ( limiter == null )
{
throw new IllegalArgumentException( "IOPSLimiter cannot be null" );
}
assertNotClosed();
flushAllPages( limiter );
clearEvictorException();
}
private void flushAllPages( IOLimiter limiter ) throws IOException
{
try ( MajorFlushEvent cacheFlush = pageCacheTracer.beginCacheFlush() )
{
FileMapping fileMapping = mappedFiles;
while ( fileMapping != null )
{
try ( MajorFlushEvent fileFlush = pageCacheTracer.beginFileFlush( fileMapping.pagedFile.swapper ) )
{
FlushEventOpportunity flushOpportunity = fileFlush.flushEventOpportunity();
fileMapping.pagedFile.flushAndForceInternal( flushOpportunity, false, limiter );
}
fileMapping = fileMapping.next;
}
syncDevice();
}
}
void syncDevice() throws IOException
{
swapperFactory.syncDevice();
}
@Override
public synchronized void close()
{
if ( closed )
{
return;
}
FileMapping files = mappedFiles;
if ( files != null )
{
StringBuilder msg = new StringBuilder(
"Cannot close the PageCache while files are still mapped:" );
while ( files != null )
{
int refCount = files.pagedFile.getRefCount();
msg.append( "\n\t" );
msg.append( files.file );
msg.append( " (" ).append( refCount );
msg.append( refCount == 1 ? " mapping)" : " mappings)" );
files = files.next;
}
throw new IllegalStateException( msg.toString() );
}
closed = true;
for ( int i = 0; i < pages.length; i++ )
{
pages[i] = null;
}
interrupt( evictionThread );
evictionThread = null;
// Close the page swapper factory last. If this fails then we will still consider ourselves closed.
swapperFactory.close();
}
private void interrupt( Thread thread )
{
if ( thread != null )
{
thread.interrupt();
}
}
@Override
protected void finalize() throws Throwable
{
close();
super.finalize();
}
private void assertHealthy() throws IOException
{
assertNotClosed();
IOException exception = evictorException;
if ( exception != null )
{
throw new IOException( "Exception in the page eviction thread", exception );
}
}
private void assertNotClosed()
{
if ( closed )
{
throw new IllegalStateException( "The PageCache has been shut down" );
}
}
@Override
public int pageSize()
{
return cachePageSize;
}
@Override
public int maxCachedPages()
{
return pages.length;
}
int getPageCacheId()
{
return pageCacheId;
}
MuninnPage grabFreeAndExclusivelyLockedPage( PageFaultEvent faultEvent ) throws IOException
{
// Review the comment on the freelist field before making changes to
// this part of the code.
// Whatever the case, we're going to the head-pointer of the freelist,
// and in doing so, we can discover a number of things.
// We can discover a MuninnPage object, in which case we can try to
// CAS the freelist pointer to the value of the MuninnPage.nextFree
// pointer, and if this succeeds then we've grabbed that page.
// We can discover a FreePage object, in which case we'll do a similar
// dance by attempting to CAS the freelist to the FreePage objects next
// pointer, and again, if we succeed then we've grabbed the MuninnPage
// given by the FreePage object.
// We can discover a null-pointer, in which case the freelist has just
// been emptied for whatever it contained before. New FreePage objects
// are eventually going to be added to the freelist, but we are not
// going to wait around for that to happen. If the freelist is empty,
// then we do our own eviction to get a free page.
// If we find a FreePage object on the freelist, then it is important
// to check and see if it is the shutdownSignal instance. If that's the
// case, then the page cache has been shut down, and we should throw an
// exception from our page fault routine.
Object current;
for (;;)
{
assertHealthy();
current = getFreelistHead();
if ( current == null )
{
unparkEvictor();
MuninnPage page = cooperativelyEvict( faultEvent );
if ( page != null )
{
return page;
}
}
else if ( current instanceof MuninnPage )
{
MuninnPage page = (MuninnPage) current;
if ( compareAndSetFreelistHead( page, page.nextFree ) )
{
return page;
}
}
else if ( current instanceof FreePage )
{
FreePage freePage = (FreePage) current;
if ( freePage == shutdownSignal )
{
throw new IllegalStateException( "The PageCache has been shut down." );
}
if ( compareAndSetFreelistHead( freePage, freePage.next ) )
{
return freePage.page;
}
}
}
}
private MuninnPage cooperativelyEvict( PageFaultEvent faultEvent ) throws IOException
{
int iterations = 0;
int clockArm = ThreadLocalRandom.current().nextInt( pages.length );
MuninnPage page;
boolean evicted = false;
do
{
assertHealthy();
if ( getFreelistHead() != null )
{
return null;
}
if ( clockArm == pages.length )
{
if ( iterations == cooperativeEvictionLiveLockThreshold )
{
throw cooperativeEvictionLiveLock();
}
iterations++;
clockArm = 0;
}
page = pages[clockArm];
if ( page == null )
{
throw new IllegalStateException(
"The PageCache has been shut down" );
}
if ( page.isLoaded() && page.decrementUsage() )
{
if ( page.tryExclusiveLock() )
{
// We got the write lock. Time to evict the page!
try ( EvictionEvent evictionEvent = faultEvent.beginEviction() )
{
evicted = page.isLoaded() && evictPage( page, evictionEvent );
}
finally
{
if ( !evicted )
{
page.unlockExclusive();
}
}
}
}
clockArm++;
}
while ( !evicted );
return page;
}
private CacheLiveLockException cooperativeEvictionLiveLock()
{
return new CacheLiveLockException(
"Live-lock encountered when trying to cooperatively evict a page during page fault. " +
"This happens when we want to access a page that is not in memory, so it has to be faulted in, but " +
"there are no free memory pages available to accept the page fault, so we have to evict an existing " +
"page, but all the in-memory pages are currently locked by other accesses. If those other access are " +
"waiting for our page fault to make progress, then we have a live-lock, and the only way we can get " +
"out of it is by throwing this exception. This should be extremely rare, but can happen if the page " +
"cache size is tiny and the number of concurrently running transactions is very high. You should be " +
"able to get around this problem by increasing the amount of memory allocated to the page cache " +
"with the `dbms.memory.pagecache.size` setting. Please contact Neo4j support if you need help tuning " +
"your database." );
}
private void unparkEvictor()
{
if ( evictorParked )
{
evictorParked = false;
LockSupport.unpark( evictionThread );
}
}
private void parkEvictor( long parkNanos )
{
// Only called from the background eviction thread!
evictorParked = true;
LockSupport.parkNanos( this, parkNanos );
evictorParked = false;
}
private Object getFreelistHead()
{
return UnsafeUtil.getObjectVolatile( this, freelistOffset );
}
private boolean compareAndSetFreelistHead( Object expected, Object update )
{
return UnsafeUtil.compareAndSwapObject(
this, freelistOffset, expected, update );
}
private Object getAndSetFreelistHead( Object newFreelistHead )
{
return UnsafeUtil.getAndSetObject(
this, freelistOffset, newFreelistHead );
}
/**
* Scan through all the pages, one by one, and decrement their usage stamps.
* If a usage reaches zero, we try-write-locking it, and if we get that lock,
* we evict the page. If we don't, we move on to the next page.
* Once we have enough free pages, we park our thread. Page-faulting will
* unpark our thread as needed.
*/
void continuouslySweepPages()
{
evictionThread = Thread.currentThread();
int clockArm = 0;
while ( !closed )
{
int pageCountToEvict = parkUntilEvictionRequired( keepFree );
try ( EvictionRunEvent evictionRunEvent = pageCacheTracer.beginPageEvictions( pageCountToEvict ) )
{
clockArm = evictPages( pageCountToEvict, clockArm, evictionRunEvent );
}
}
// The last thing we do, is signalling the shutdown of the cache via
// the freelist. This signal is looked out for in grabFreePage.
getAndSetFreelistHead( shutdownSignal );
}
private int parkUntilEvictionRequired( int keepFree )
{
// Park until we're either interrupted, or the number of free pages drops
// bellow keepFree.
long parkNanos = TimeUnit.MILLISECONDS.toNanos( 10 );
for (;;)
{
parkEvictor( parkNanos );
if ( Thread.interrupted() || closed )
{
return 0;
}
Object freelistHead = getFreelistHead();
if ( freelistHead == null )
{
return keepFree;
}
else if ( freelistHead.getClass() == FreePage.class )
{
int availablePages = ((FreePage) freelistHead).count;
if ( availablePages < keepFree )
{
return keepFree - availablePages;
}
}
}
}
int evictPages( int pageCountToEvict, int clockArm, EvictionRunEvent evictionRunEvent )
{
while ( pageCountToEvict > 0 && !closed )
{
if ( clockArm == pages.length )
{
clockArm = 0;
}
MuninnPage page = pages[clockArm];
if ( page == null )
{
// The page cache has been shut down.
return 0;
}
if ( page.isLoaded() && page.decrementUsage() )
{
if ( page.tryExclusiveLock() )
{
// We got the lock.
// Assume that the eviction is going to succeed, so that we
// always make some kind of progress. This means that, if
// we have a temporary outage of the storage system, for
// instance if the drive is full, then we won't spin in
// this forever. Instead, we'll eventually make our way
// back out to the main loop, where we have a chance to
// sleep for a little while in `parkUntilEvictionRequired`.
// This reduces the CPU load and power usage in such a
// scenario.
pageCountToEvict--;
boolean pageEvicted = false;
try ( EvictionEvent evictionEvent = evictionRunEvent.beginEviction() )
{
pageEvicted = page.isLoaded() && evictPage( page, evictionEvent );
if ( pageEvicted )
{
Object current;
FreePage freePage = new FreePage( page );
do
{
current = getFreelistHead();
freePage.setNext( (FreePage) current );
}
while ( !compareAndSetFreelistHead(
current, freePage ) );
}
}
finally
{
if ( !pageEvicted )
{
// Pages we put into the free-list remain exclusively locked until a page fault unlocks
// them. If we somehow failed to evict the page, then we need to make sure that we release
// the exclusive lock.