/
ParallelComputerBuilder.java
executable file
·729 lines (638 loc) · 24.5 KB
/
ParallelComputerBuilder.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
package org.apache.maven.surefire.junitcore.pc;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import org.apache.maven.surefire.junitcore.JUnitCoreParameters;
import org.apache.maven.surefire.api.report.ConsoleStream;
import org.apache.maven.surefire.api.testset.TestSetFailedException;
import org.apache.maven.surefire.api.util.internal.DaemonThreadFactory;
import org.junit.internal.runners.ErrorReportingRunner;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.ParentRunner;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import static org.apache.maven.surefire.junitcore.pc.ParallelComputerUtil.resolveConcurrency;
import static org.apache.maven.surefire.junitcore.pc.SchedulingStrategies.createParallelStrategy;
import static org.apache.maven.surefire.junitcore.pc.SchedulingStrategies.createParallelStrategyUnbounded;
import static org.apache.maven.surefire.junitcore.pc.Type.CLASSES;
import static org.apache.maven.surefire.junitcore.pc.Type.METHODS;
import static org.apache.maven.surefire.junitcore.pc.Type.SUITES;
@SuppressWarnings( { "javadoc", "checkstyle:javadoctype" } )
/*
* Executing suites, classes and methods with defined concurrency. In this example the threads which completed
* the suites and classes can be reused in parallel methods.
* <pre>
* JUnitCoreParameters parameters = ...;
* ParallelComputerBuilder builder = new ParallelComputerBuilder(parameters);
* builder.useOnePool(8).parallelSuites(2).parallelClasses(4).parallelMethods();
* ParallelComputerBuilder.ParallelComputer computer = builder.buildComputer();
* Class<?>[] tests = {...};
* new JUnitCore().run(computer, tests);
* </pre>
* Note that the type has always at least one thread even if unspecified. The capacity in
* {@link ParallelComputerBuilder#useOnePool(int)} must be greater than the number of concurrent suites and classes
* altogether.
* <br>
* The Computer can be stopped in a separate thread. Pending tests will be interrupted if the argument is
* {@code true}.
* <pre>
* computer.describeStopped(true);
* </pre>
*
* @author Tibor Digana (tibor17)
* @since 2.16
*/
public final class ParallelComputerBuilder
{
private static final ThreadFactory DAEMON_THREAD_FACTORY = DaemonThreadFactory.newDaemonThreadFactory();
private static final Class<? extends Annotation> JCIP_NOT_THREAD_SAFE = loadNotThreadSafeAnnotations();
private static final Set<Runner> NULL_SINGLETON = Collections.singleton( null );
static final int TOTAL_POOL_SIZE_UNDEFINED = 0;
private final Map<Type, Integer> parallelGroups = new EnumMap<>( Type.class );
private final ConsoleStream logger;
private boolean useSeparatePools;
private int totalPoolSize;
private JUnitCoreParameters parameters;
private boolean optimize;
private boolean runningInTests;
/**
* Calling {@link #useSeparatePools()}.
* Can be used only in unit tests.
* Do NOT call this constructor in production.
*/
ParallelComputerBuilder( ConsoleStream logger )
{
this.logger = logger;
runningInTests = true;
useSeparatePools();
parallelGroups.put( SUITES, 0 );
parallelGroups.put( CLASSES, 0 );
parallelGroups.put( METHODS, 0 );
}
public ParallelComputerBuilder( ConsoleStream logger, JUnitCoreParameters parameters )
{
this( logger );
runningInTests = false;
this.parameters = parameters;
}
public ParallelComputer buildComputer()
{
return new PC();
}
ParallelComputerBuilder useSeparatePools()
{
totalPoolSize = TOTAL_POOL_SIZE_UNDEFINED;
useSeparatePools = true;
return this;
}
ParallelComputerBuilder useOnePool()
{
totalPoolSize = TOTAL_POOL_SIZE_UNDEFINED;
useSeparatePools = false;
return this;
}
/**
* @param totalPoolSize Pool size where suites, classes and methods are executed in parallel.
* If the <code>totalPoolSize</code> is {@link Integer#MAX_VALUE}, the pool capacity is not
* limited.
* @throws IllegalArgumentException If <code>totalPoolSize</code> is < 1.
*/
ParallelComputerBuilder useOnePool( int totalPoolSize )
{
if ( totalPoolSize < 1 )
{
throw new IllegalArgumentException( "Size of common pool is less than 1." );
}
this.totalPoolSize = totalPoolSize;
useSeparatePools = false;
return this;
}
boolean isOptimized()
{
return optimize;
}
ParallelComputerBuilder optimize( boolean optimize )
{
this.optimize = optimize;
return this;
}
ParallelComputerBuilder parallelSuites()
{
return parallel( SUITES );
}
ParallelComputerBuilder parallelSuites( int nThreads )
{
return parallel( nThreads, SUITES );
}
ParallelComputerBuilder parallelClasses()
{
return parallel( CLASSES );
}
ParallelComputerBuilder parallelClasses( int nThreads )
{
return parallel( nThreads, CLASSES );
}
ParallelComputerBuilder parallelMethods()
{
return parallel( METHODS );
}
ParallelComputerBuilder parallelMethods( int nThreads )
{
return parallel( nThreads, METHODS );
}
private ParallelComputerBuilder parallel( int nThreads, Type parallelType )
{
if ( nThreads < 0 )
{
throw new IllegalArgumentException( "negative nThreads " + nThreads );
}
if ( parallelType == null )
{
throw new IllegalArgumentException( "null parallelType" );
}
parallelGroups.put( parallelType, nThreads );
return this;
}
private ParallelComputerBuilder parallel( Type parallelType )
{
return parallel( Integer.MAX_VALUE, parallelType );
}
private double parallelTestsTimeoutInSeconds()
{
return parameters == null ? 0d : parameters.getParallelTestsTimeoutInSeconds();
}
private double parallelTestsTimeoutForcedInSeconds()
{
return parameters == null ? 0d : parameters.getParallelTestsTimeoutForcedInSeconds();
}
@SuppressWarnings( "unchecked" )
private static Class<? extends Annotation> loadNotThreadSafeAnnotations()
{
try
{
Class c = Class.forName( "net.jcip.annotations.NotThreadSafe" );
return c.isAnnotation() ? (Class<? extends Annotation>) c : null;
}
catch ( ClassNotFoundException e )
{
return null;
}
}
final class PC
extends ParallelComputer
{
private final SingleThreadScheduler notThreadSafeTests =
new SingleThreadScheduler( ParallelComputerBuilder.this.logger );
private final Collection<ParentRunner> suites = new LinkedHashSet<>();
private final Collection<ParentRunner> nestedSuites = new LinkedHashSet<>();
private final Collection<ParentRunner> classes = new LinkedHashSet<>();
private final Collection<ParentRunner> nestedClasses = new LinkedHashSet<>();
private final Collection<Runner> notParallelRunners = new LinkedHashSet<>();
private int poolCapacity;
private boolean splitPool;
private final Map<Type, Integer> allGroups;
private long nestedClassesChildren;
private volatile Scheduler master;
private PC()
{
super( parallelTestsTimeoutInSeconds(), parallelTestsTimeoutForcedInSeconds() );
allGroups = new EnumMap<>( ParallelComputerBuilder.this.parallelGroups );
poolCapacity = ParallelComputerBuilder.this.totalPoolSize;
splitPool = ParallelComputerBuilder.this.useSeparatePools;
}
Collection<ParentRunner> getSuites()
{
return suites;
}
Collection<ParentRunner> getNestedSuites()
{
return nestedSuites;
}
Collection<ParentRunner> getClasses()
{
return classes;
}
Collection<ParentRunner> getNestedClasses()
{
return nestedClasses;
}
Collection<Runner> getNotParallelRunners()
{
return notParallelRunners;
}
int getPoolCapacity()
{
return poolCapacity;
}
boolean isSplitPool()
{
return splitPool;
}
@Override
protected ShutdownResult describeStopped( boolean shutdownNow )
{
ShutdownResult shutdownResult = notThreadSafeTests.describeStopped( shutdownNow );
final Scheduler m = master;
if ( m != null )
{
ShutdownResult shutdownResultOfMaster = m.describeStopped( shutdownNow );
shutdownResult.getTriggeredTests().addAll( shutdownResultOfMaster.getTriggeredTests() );
shutdownResult.getIncompleteTests().addAll( shutdownResultOfMaster.getIncompleteTests() );
}
return shutdownResult;
}
@Override
protected boolean shutdownThreadPoolsAwaitingKilled()
{
boolean notInterrupted = notThreadSafeTests.shutdownThreadPoolsAwaitingKilled();
final Scheduler m = master;
if ( m != null )
{
notInterrupted &= m.shutdownThreadPoolsAwaitingKilled();
}
return notInterrupted;
}
@Override
public Runner getSuite( RunnerBuilder builder, Class<?>[] cls )
throws InitializationError
{
try
{
super.getSuite( builder, cls );
populateChildrenFromSuites();
WrappedRunners suiteSuites = wrapRunners( suites );
WrappedRunners suiteClasses = wrapRunners( classes );
long suitesCount = suites.size();
long classesCount = classes.size() + nestedClasses.size();
long methodsCount = suiteClasses.embeddedChildrenCount + nestedClassesChildren;
if ( !ParallelComputerBuilder.this.runningInTests )
{
determineThreadCounts( suitesCount, classesCount, methodsCount );
}
return setSchedulers( suiteSuites.wrappingSuite, suiteClasses.wrappingSuite );
}
catch ( TestSetFailedException e )
{
throw new InitializationError( Collections.<Throwable>singletonList( e ) );
}
}
@Override
protected Runner getRunner( RunnerBuilder builder, Class<?> testClass )
throws Throwable
{
Runner runner = super.getRunner( builder, testClass );
if ( canSchedule( runner ) )
{
if ( !isThreadSafe( runner ) )
{
( ( ParentRunner ) runner ).setScheduler( notThreadSafeTests.newRunnerScheduler() );
notParallelRunners.add( runner );
}
else if ( runner instanceof Suite )
{
suites.add( (Suite) runner );
}
else
{
classes.add( (ParentRunner) runner );
}
}
else
{
notParallelRunners.add( runner );
}
return runner;
}
private void determineThreadCounts( long suites, long classes, long methods )
throws TestSetFailedException
{
RunnerCounter counts =
ParallelComputerBuilder.this.optimize ? new RunnerCounter( suites, classes, methods ) : null;
Concurrency concurrency =
resolveConcurrency( ParallelComputerBuilder.this.parameters, counts );
allGroups.put( SUITES, concurrency.suites );
allGroups.put( CLASSES, concurrency.classes );
allGroups.put( METHODS, concurrency.methods );
poolCapacity = concurrency.capacity;
splitPool &= concurrency.capacity <= 0; // fault if negative; should not happen
}
private <T extends Runner> WrappedRunners wrapRunners( Collection<T> runners )
throws InitializationError
{
// Do NOT use allGroups here.
long childrenCounter = 0;
ArrayList<Runner> runs = new ArrayList<>();
for ( T runner : runners )
{
if ( runner != null )
{
int children = countChildren( runner );
childrenCounter += children;
runs.add( runner );
}
}
return runs.isEmpty() ? new WrappedRunners() : new WrappedRunners( createSuite( runs ), childrenCounter );
}
private int countChildren( Runner runner )
{
Description description = runner.getDescription();
Collection children = description == null ? null : description.getChildren();
return children == null ? 0 : children.size();
}
private ExecutorService createPool( int poolSize )
{
return poolSize < Integer.MAX_VALUE
? Executors.newFixedThreadPool( poolSize, DAEMON_THREAD_FACTORY )
: Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY );
}
private Scheduler createMaster( ExecutorService pool, int poolSize )
{
// can be 0, 1, 2 or 3
final int finalRunnersCounter = countFinalRunners();
final SchedulingStrategy strategy;
if ( finalRunnersCounter <= 1 || poolSize <= 1 )
{
strategy = new InvokerStrategy( ParallelComputerBuilder.this.logger );
}
else if ( pool != null && poolSize == Integer.MAX_VALUE )
{
strategy = new SharedThreadPoolStrategy( ParallelComputerBuilder.this.logger, pool );
}
else
{
strategy = createParallelStrategy( ParallelComputerBuilder.this.logger, finalRunnersCounter );
}
return new Scheduler( ParallelComputerBuilder.this.logger, null, strategy );
}
private int countFinalRunners()
{
int counter = notParallelRunners.isEmpty() ? 0 : 1;
if ( !suites.isEmpty() && allGroups.get( SUITES ) > 0 )
{
++counter;
}
if ( !classes.isEmpty() && allGroups.get( CLASSES ) > 0 )
{
++counter;
}
return counter;
}
private void populateChildrenFromSuites()
{
// Do NOT use allGroups here.
Filter filter = new SuiteFilter();
for ( Iterator<ParentRunner> it = suites.iterator(); it.hasNext(); )
{
ParentRunner suite = it.next();
try
{
suite.filter( filter );
}
catch ( NoTestsRemainException e )
{
it.remove();
}
}
}
private int totalPoolSize()
{
if ( poolCapacity == TOTAL_POOL_SIZE_UNDEFINED )
{
int total = 0;
for ( int nThreads : allGroups.values() )
{
total += nThreads;
if ( total < 0 )
{
total = Integer.MAX_VALUE;
break;
}
}
return total;
}
else
{
return poolCapacity;
}
}
private Runner setSchedulers( ParentRunner suiteSuites, ParentRunner suiteClasses )
throws InitializationError
{
int parallelSuites = allGroups.get( SUITES );
int parallelClasses = allGroups.get( CLASSES );
int parallelMethods = allGroups.get( METHODS );
int poolSize = totalPoolSize();
ExecutorService commonPool = splitPool || poolSize == 0 ? null : createPool( poolSize );
master = createMaster( commonPool, poolSize );
if ( suiteSuites != null )
{
// a scheduler for parallel suites
if ( commonPool != null && parallelSuites > 0 )
{
Balancer balancer = BalancerFactory.createBalancerWithFairness( parallelSuites );
suiteSuites.setScheduler( createScheduler( null, commonPool, true, balancer ) );
}
else
{
suiteSuites.setScheduler( createScheduler( parallelSuites ) );
}
}
// schedulers for parallel classes
ArrayList<ParentRunner> allSuites = new ArrayList<>( suites );
allSuites.addAll( nestedSuites );
if ( suiteClasses != null )
{
allSuites.add( suiteClasses );
}
if ( !allSuites.isEmpty() )
{
setSchedulers( allSuites, parallelClasses, commonPool );
}
// schedulers for parallel methods
ArrayList<ParentRunner> allClasses = new ArrayList<>( classes );
allClasses.addAll( nestedClasses );
if ( !allClasses.isEmpty() )
{
setSchedulers( allClasses, parallelMethods, commonPool );
}
// resulting runner for Computer#getSuite() scheduled by master scheduler
ParentRunner all = createFinalRunner( removeNullRunners(
Arrays.<Runner>asList( suiteSuites, suiteClasses, createSuite( notParallelRunners ) )
) );
all.setScheduler( master );
return all;
}
private ParentRunner createFinalRunner( List<Runner> runners )
throws InitializationError
{
return new Suite( null, runners )
{
@Override
public void run( RunNotifier notifier )
{
try
{
beforeRunQuietly();
super.run( notifier );
}
finally
{
afterRunQuietly();
}
}
};
}
private void setSchedulers( Iterable<? extends ParentRunner> runners, int poolSize, ExecutorService commonPool )
{
if ( commonPool != null )
{
Balancer concurrencyLimit = BalancerFactory.createBalancerWithFairness( poolSize );
boolean doParallel = poolSize > 0;
for ( ParentRunner runner : runners )
{
runner.setScheduler(
createScheduler( runner.getDescription(), commonPool, doParallel, concurrencyLimit ) );
}
}
else
{
ExecutorService pool = null;
if ( poolSize == Integer.MAX_VALUE )
{
pool = Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY );
}
else if ( poolSize > 0 )
{
pool = Executors.newFixedThreadPool( poolSize, DAEMON_THREAD_FACTORY );
}
boolean doParallel = pool != null;
for ( ParentRunner runner : runners )
{
runner.setScheduler( createScheduler( runner.getDescription(), pool, doParallel,
BalancerFactory.createInfinitePermitsBalancer() ) );
}
}
}
private Scheduler createScheduler( Description desc, ExecutorService pool, boolean doParallel,
Balancer concurrency )
{
SchedulingStrategy strategy =
doParallel & pool != null
? new SharedThreadPoolStrategy( ParallelComputerBuilder.this.logger, pool )
: new InvokerStrategy( ParallelComputerBuilder.this.logger );
return new Scheduler( ParallelComputerBuilder.this.logger, desc, master, strategy, concurrency );
}
private Scheduler createScheduler( int poolSize )
{
final SchedulingStrategy strategy;
if ( poolSize == Integer.MAX_VALUE )
{
strategy = createParallelStrategyUnbounded( ParallelComputerBuilder.this.logger );
}
else if ( poolSize == 0 )
{
strategy = new InvokerStrategy( ParallelComputerBuilder.this.logger );
}
else
{
strategy = createParallelStrategy( ParallelComputerBuilder.this.logger, poolSize );
}
return new Scheduler( ParallelComputerBuilder.this.logger, null, master, strategy );
}
private boolean canSchedule( Runner runner )
{
return !( runner instanceof ErrorReportingRunner ) && runner instanceof ParentRunner;
}
private boolean isThreadSafe( Runner runner )
{
return runner.getDescription().getAnnotation( JCIP_NOT_THREAD_SAFE ) == null;
}
private class SuiteFilter
extends Filter
{
// Do NOT use allGroups in SuiteFilter.
@Override
public boolean shouldRun( Description description )
{
return true;
}
@Override
public void apply( Object child )
throws NoTestsRemainException
{
super.apply( child );
if ( child instanceof ParentRunner )
{
ParentRunner runner = ( ParentRunner ) child;
if ( !isThreadSafe( runner ) )
{
runner.setScheduler( notThreadSafeTests.newRunnerScheduler() );
}
else if ( child instanceof Suite )
{
nestedSuites.add( (Suite) child );
}
else
{
ParentRunner parentRunner = (ParentRunner) child;
nestedClasses.add( parentRunner );
nestedClassesChildren += parentRunner.getDescription().getChildren().size();
}
}
}
@Override
public String describe()
{
return "";
}
}
}
private static Suite createSuite( Collection<Runner> runners )
throws InitializationError
{
final List<Runner> onlyRunners = removeNullRunners( runners );
return onlyRunners.isEmpty() ? null : new Suite( null, onlyRunners )
{
};
}
private static List<Runner> removeNullRunners( Collection<Runner> runners )
{
final List<Runner> onlyRunners = new ArrayList<>( runners );
onlyRunners.removeAll( NULL_SINGLETON );
return onlyRunners;
}
}