@ -15,8 +15,10 @@ package tech.pegasys.pantheon.ethereum.eth.manager;
import tech.pegasys.pantheon.util.ExceptionUtils ;
import java.time.Duration ;
import java.util.Collection ;
import java.util.concurrent.CancellationException ;
import java.util.concurrent.CompletableFuture ;
import java.util.concurrent.ConcurrentLinkedDeque ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
@ -47,6 +49,8 @@ public class EthScheduler {
private final ExecutorService servicesExecutor ;
private final ExecutorService computationExecutor ;
private Collection < CompletableFuture < ? > > serviceFutures = new ConcurrentLinkedDeque < > ( ) ;
public EthScheduler (
final int syncWorkerCount , final int txWorkerCount , final int computationWorkerCount ) {
this (
@ -126,7 +130,10 @@ public class EthScheduler {
}
public < T > CompletableFuture < T > scheduleServiceTask ( final EthTask < T > task ) {
return task . runAsync ( servicesExecutor ) ;
final CompletableFuture < T > serviceFuture = task . runAsync ( servicesExecutor ) ;
serviceFutures . add ( serviceFuture ) ;
serviceFuture . whenComplete ( ( r , t ) - > serviceFutures . remove ( serviceFuture ) ) ;
return serviceFuture ;
}
public < T > CompletableFuture < T > scheduleComputationTask ( final Supplier < T > computation ) {
@ -230,6 +237,7 @@ public class EthScheduler {
void awaitStop ( ) throws InterruptedException {
shutdown . await ( ) ;
serviceFutures . forEach ( future - > future . cancel ( true ) ) ;
if ( ! syncWorkerExecutor . awaitTermination ( 2L , TimeUnit . MINUTES ) ) {
LOG . error ( "{} worker executor did not shutdown cleanly." , this . getClass ( ) . getSimpleName ( ) ) ;
syncWorkerExecutor . shutdownNow ( ) ;