-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RandomPageCacheTestHarness.java
544 lines (491 loc) · 18.2 KB
/
RandomPageCacheTestHarness.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
/*
* 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.randomharness;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.neo4j.adversaries.RandomAdversary;
import org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction;
import org.neo4j.graphdb.config.Configuration;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageSwapperFactory;
import org.neo4j.io.pagecache.PagedFile;
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.DefaultPageCursorTracerSupplier;
import org.neo4j.io.pagecache.tracing.cursor.PageCursorTracerSupplier;
import org.neo4j.io.pagecache.tracing.cursor.context.EmptyVersionContextSupplier;
import org.neo4j.io.pagecache.tracing.linear.LinearHistoryPageCacheTracerTest;
import org.neo4j.io.pagecache.tracing.linear.LinearTracers;
/**
* The RandomPageCacheTestHarness can plan and run random page cache tests, repeatably if necessary, and verify that
* the behaviour of the page cache is correct to some degree. For instance, it can verify that records don't end up
* overlapping each other in the mapped files, that records end up at the correct locations in the files, and that
* records don't end up in the wrong files. The harness can also execute separate preparation and verification steps,
* before and after executing the planned test respectively, and it can integrate with the adversarial file system
* for fault injection, and arbitrary PageCacheTracers.
*
* See {@link LinearHistoryPageCacheTracerTest} for an example of how to configure and use the harness.
*/
public class RandomPageCacheTestHarness implements Closeable
{
private double mischiefRate;
private double failureRate;
private double errorRate;
private int concurrencyLevel;
private int initialMappedFiles;
private int cachePageCount;
private int cachePageSize;
private int filePageCount;
private int filePageSize;
private PageCacheTracer tracer;
private PageCursorTracerSupplier cursorTracerSupplier;
private int commandCount;
private double[] commandProbabilityFactors;
private long randomSeed;
private boolean fixedRandomSeed;
private FileSystemAbstraction fs;
private boolean useAdversarialIO;
private Plan plan;
private Phase preparation;
private Phase verification;
private RecordFormat recordFormat;
public RandomPageCacheTestHarness()
{
mischiefRate = 0.1;
failureRate = 0.1;
errorRate = 0.0;
concurrencyLevel = 1;
initialMappedFiles = 2;
cachePageCount = 20;
cachePageSize = 8192;
filePageCount = cachePageCount * 10;
filePageSize = cachePageSize;
tracer = PageCacheTracer.NULL;
cursorTracerSupplier = DefaultPageCursorTracerSupplier.INSTANCE;
commandCount = 1000;
Command[] commands = Command.values();
commandProbabilityFactors = new double[commands.length];
for ( Command command : commands )
{
commandProbabilityFactors[command.ordinal()] = command.getDefaultProbabilityFactor();
}
fs = new EphemeralFileSystemAbstraction();
useAdversarialIO = true;
recordFormat = new StandardRecordFormat();
}
/**
* Disable all of the given commands, by setting their probability factors to zero.
*/
public void disableCommands( Command... commands )
{
for ( Command command : commands )
{
setCommandProbabilityFactor( command, 0 );
}
}
/**
* Set the probability factor of the given command. The default value is given by
* {@link Command#getDefaultProbabilityFactor()}. The effective probability is computed from the relative
* difference in probability factors between all the commands.
*
* Setting the probability factor to zero will disable that command.
*/
public void setCommandProbabilityFactor( Command command, double probabilityFactor )
{
assert 0.0 <= probabilityFactor : "Probability factor cannot be negative";
commandProbabilityFactors[command.ordinal()] = probabilityFactor;
}
/**
* Set to "true" to execute the plans with fault injection from the {@link AdversarialFileSystemAbstraction}, or
* set to "false" to disable this feature.
*
* The default is "true".
*/
public void setUseAdversarialIO( boolean useAdversarialIO )
{
this.useAdversarialIO = useAdversarialIO;
}
/**
* Set the PageCacheTracer that the page cache under test should be configured with.
*/
public void setTracer( PageCacheTracer tracer )
{
this.tracer = tracer;
}
/**
* Set the page cursor tracers supplier.
*/
public void setCursorTracerSupplier( PageCursorTracerSupplier cursorTracerSupplier )
{
this.cursorTracerSupplier = cursorTracerSupplier;
}
/**
* Set the mischief rate for the adversarial file system.
*/
public void setMischiefRate( double rate )
{
mischiefRate = rate;
}
/**
* Set the failure rate for the adversarial file system.
*/
public void setFailureRate( double rate )
{
failureRate = rate;
}
/**
* Set the error rate for the adversarial file system.
*/
public void setErrorRate( double rate )
{
errorRate = rate;
}
/**
* Set the number of threads that will execute commands from the plan. If this number is greater than 1, then the
* plan will execute non-deterministically. The description of the iteration that
* {@link #describePreviousRun(PrintStream)} prints will include which thread performed which command.
*/
public void setConcurrencyLevel( int concurrencyLevel )
{
this.concurrencyLevel = concurrencyLevel;
}
/**
* Set the number of files that should be mapped from the start of the plan. If you have set the probability of
* the {@link Command#MapFile} command to zero, then you must have a positive number of initial mapped files.
* Otherwise there will be no files to plan any work for.
*
* The default value is 2.
*/
public void setInitialMappedFiles( int initialMappedFiles )
{
this.initialMappedFiles = initialMappedFiles;
}
public void setCachePageCount( int count )
{
this.cachePageCount = count;
}
public void setCachePageSize( int size )
{
this.cachePageSize = size;
}
public void setFilePageCount( int count )
{
this.filePageCount = count;
}
public void setFilePageSize( int size )
{
this.filePageSize = size;
}
/**
* Set the number of commands to plan in each iteration.
*/
public void setCommandCount( int commandCount )
{
this.commandCount = commandCount;
}
/**
* Set the preparation phase to use. This phase is executed before all the planned commands. It can be used to
* prepare some file contents, or reset some external state, such as the
* {@link LinearTracers}.
*
* The preparation phase is executed before each iteration.
*/
public void setPreparation( Phase preparation )
{
this.preparation = preparation;
}
/**
* Set the verification phase to use. This phase is executed after all the planned commands have executed
* completely, and can be used to verify the consistency of the data, or some other invariant.
*
* The verification phase is executed after each iteration.
*/
public void setVerification( Phase verification )
{
this.verification = verification;
}
/**
* Set the record format to use. The record format is used to read, write and verify file contents.
*/
public void setRecordFormat( RecordFormat recordFormat )
{
this.recordFormat = recordFormat;
}
/**
* Set and fix the random seed to the given value. All iterations run through this harness will then use that seed.
*
* If the random seed has not been configured, then each iteration will use a new seed.
*/
public void setRandomSeed( long randomSeed )
{
this.randomSeed = randomSeed;
this.fixedRandomSeed = true;
}
public void setFileSystem( FileSystemAbstraction fileSystem )
{
this.fs = fileSystem;
}
/**
* Write out a textual description of the last run iteration, including the exact plan and what thread
* executed which command, and the random seed that can be used to recreate that plan for improved repeatability.
*/
public void describePreviousRun( PrintStream out )
{
out.println( "randomSeed = " + randomSeed );
out.println( "commandCount = " + commandCount );
out.println( "concurrencyLevel (number of worker threads) = " + concurrencyLevel );
out.println( "initialMappedFiles = " + initialMappedFiles );
out.println( "cachePageCount = " + cachePageCount );
out.println( "cachePageSize = " + cachePageSize );
out.println( "tracer = " + tracer );
out.println( "useAdversarialIO = " + useAdversarialIO );
out.println( "mischeifRate = " + mischiefRate );
out.println( "failureRate = " + failureRate );
out.println( "errorRate = " + errorRate );
out.println( "Command probability factors:" );
Command[] commands = Command.values();
for ( int i = 0; i < commands.length; i++ )
{
out.print( " " );
out.print( commands[i] );
out.print( " = " );
out.println( commandProbabilityFactors[i] );
}
if ( plan != null )
{
plan.print( out );
}
}
/**
* Run a single iteration with the current harness configuration.
*
* This will either complete within the given timeout, or throw an exception.
*
* If the run fails, then a description will be printed to System.err.
*/
public void run( long iterationTimeout, TimeUnit unit ) throws Exception
{
run( 1, iterationTimeout, unit );
}
/**
* Run the given number of iterations with the given harness configuration.
*
* If the random seed has been set to a specific value, then all iterations will use that seed. Otherwise each
* iteration will use a new seed.
*
* The given timeout applies to the individual iteration, not to their combined run. This is effectively similar
* to calling {@link #run(long, TimeUnit)} the given number of times.
*
* The run will stop at the first failure, if any, and print a description of it to System.err.
*/
public void run( int iterations, long iterationTimeout, TimeUnit unit ) throws Exception
{
try
{
for ( int i = 0; i < iterations; i++ )
{
runIteration( iterationTimeout, unit );
}
}
catch ( Exception e )
{
describePreviousRun( System.err );
throw e;
}
}
@Override
public void close() throws IOException
{
fs.close();
}
@SuppressWarnings( "unchecked" )
private void runIteration( long timeout, TimeUnit unit ) throws Exception
{
assert filePageSize % recordFormat.getRecordSize() == 0 :
"File page size must be a multiple of the record size";
if ( !fixedRandomSeed )
{
randomSeed = ThreadLocalRandom.current().nextLong();
}
FileSystemAbstraction fs = this.fs;
File[] files = buildFileNames();
RandomAdversary adversary = new RandomAdversary( mischiefRate, failureRate, errorRate );
adversary.setProbabilityFactor( 0.0 );
if ( useAdversarialIO )
{
adversary.setSeed( randomSeed );
fs = new AdversarialFileSystemAbstraction( adversary, fs );
}
PageSwapperFactory swapperFactory = new SingleFilePageSwapperFactory();
swapperFactory.open( fs, Configuration.EMPTY );
MuninnPageCache cache = new MuninnPageCache( swapperFactory, cachePageCount, cachePageSize, tracer,
cursorTracerSupplier, EmptyVersionContextSupplier.EMPTY );
cache.setPrintExceptionsOnClose( false );
Map<File,PagedFile> fileMap = new HashMap<>( files.length );
for ( int i = 0; i < Math.min( files.length, initialMappedFiles ); i++ )
{
File file = files[i];
fileMap.put( file, cache.map( file, filePageSize ) );
}
plan = plan( cache, files, fileMap );
Callable<Void> planRunner = new PlanRunner( plan );
Future<Void>[] futures = new Future[concurrencyLevel];
ExecutorService executor = Executors.newFixedThreadPool( concurrencyLevel );
for ( int i = 0; i < concurrencyLevel; i++ )
{
futures[i] = executor.submit( planRunner );
}
if ( preparation != null )
{
preparation.run( cache, this.fs, plan.getFilesTouched() );
}
adversary.setProbabilityFactor( 1.0 );
plan.start();
long deadlineMillis = System.currentTimeMillis() + unit.toMillis( timeout );
long now;
try
{
for ( Future<Void> future : futures )
{
now = System.currentTimeMillis();
if ( deadlineMillis < now )
{
throw new TimeoutException();
}
future.get( deadlineMillis - now, TimeUnit.MILLISECONDS );
}
adversary.setProbabilityFactor( 0.0 );
runVerificationPhase( cache );
}
finally
{
adversary.setProbabilityFactor( 0.0 );
for ( Future<Void> future : futures )
{
future.cancel( true );
}
executor.shutdown();
now = System.currentTimeMillis();
executor.awaitTermination( deadlineMillis - now, TimeUnit.MILLISECONDS );
plan.close();
cache.close();
if ( this.fs instanceof EphemeralFileSystemAbstraction )
{
this.fs.close();
this.fs = new EphemeralFileSystemAbstraction();
}
else
{
for ( File file : files )
{
file.delete();
}
}
}
}
private void runVerificationPhase( MuninnPageCache cache ) throws Exception
{
if ( verification != null )
{
cache.flushAndForce(); // Clears any stray evictor exceptions
verification.run( cache, this.fs, plan.getFilesTouched() );
}
}
private File[] buildFileNames() throws IOException
{
String s = "abcdefghijklmnopqrstuvwxyz";
File[] files = new File[s.length()];
for ( int i = 0; i < s.length(); i++ )
{
files[i] = new File( s.substring( i, i + 1 ) ).getCanonicalFile();
fs.mkdirs( files[i].getParentFile() );
fs.open( files[i], "rw" ).close();
}
return files;
}
private Plan plan( MuninnPageCache cache, File[] files, Map<File,PagedFile> fileMap )
{
Action[] plan = new Action[commandCount];
int[] commandWeights = computeCommandWeights();
int commandWeightSum = sum( commandWeights );
Random rng = new Random( randomSeed );
CommandPrimer primer = new CommandPrimer(
rng, cache, files, fileMap, filePageCount, filePageSize, recordFormat );
for ( int i = 0; i < plan.length; i++ )
{
Command command = pickCommand( rng.nextInt( commandWeightSum ), commandWeights );
Action action = primer.prime( command );
plan[i] = action;
if ( action == null )
{
i--;
}
}
return new Plan( plan, fileMap, primer.getMappedFiles(), primer.getFilesTouched() );
}
private int[] computeCommandWeights()
{
Command[] commands = Command.values();
int[] weights = new int[commands.length];
int base = 100_000_000;
for ( int i = 0; i < commands.length; i++ )
{
weights[i] = (int) (base * commandProbabilityFactors[i]);
}
return weights;
}
private int sum( int[] xs )
{
int sum = 0;
for ( int x : xs )
{
sum += x;
}
return sum;
}
private Command pickCommand( int randomPick, int[] commandWeights )
{
for ( int i = 0; i < commandWeights.length; i++ )
{
randomPick -= commandWeights[i];
if ( randomPick < 0 )
{
return Command.values()[i];
}
}
throw new AssertionError(
"Tried to pick randomPick = " + randomPick + " from weights = " + Arrays.toString( commandWeights ) );
}
}