Search in sources :

Example 16 with Future

use of java.util.concurrent.Future in project hadoop by apache.

the class TestRPCServerShutdown method testRPCServerShutdown.

/**
   *  Verify the RPC server can shutdown properly when callQueue is full.
   */
@Test(timeout = 30000)
public void testRPCServerShutdown() throws Exception {
    final int numClients = 3;
    final List<Future<Void>> res = new ArrayList<Future<Void>>();
    final ExecutorService executorService = Executors.newFixedThreadPool(numClients);
    conf.setInt(CommonConfigurationKeys.IPC_CLIENT_CONNECT_MAX_RETRIES_KEY, 0);
    RPC.Builder builder = newServerBuilder(conf).setQueueSizePerHandler(1).setNumHandlers(1).setVerbose(true);
    final Server server = setupTestServer(builder);
    final TestRpcService proxy = getClient(addr, conf);
    try {
        // Start another sleep RPC call to make reader thread block on CallQueue.
        for (int i = 0; i < numClients; i++) {
            res.add(executorService.submit(new Callable<Void>() {

                @Override
                public Void call() throws ServiceException, InterruptedException {
                    proxy.sleep(null, newSleepRequest(100000));
                    return null;
                }
            }));
        }
        while (server.getCallQueueLen() != 1 || countThreads(CallQueueManager.class.getName()) != 1 || countThreads(PBServerImpl.class.getName()) != 1) {
            Thread.sleep(100);
        }
    } finally {
        try {
            stop(server, proxy);
            assertEquals("Not enough clients", numClients, res.size());
            for (Future<Void> f : res) {
                try {
                    f.get();
                    fail("Future get should not return");
                } catch (ExecutionException e) {
                    ServiceException se = (ServiceException) e.getCause();
                    assertTrue("Unexpected exception: " + se, se.getCause() instanceof IOException);
                    LOG.info("Expected exception", e.getCause());
                }
            }
        } finally {
            executorService.shutdown();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) ServiceException(com.google.protobuf.ServiceException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 17 with Future

use of java.util.concurrent.Future in project hadoop by apache.

the class TestRetryCache method testOperations.

public void testOperations(final int input, final int numberOfThreads, final int pause, final boolean success, final boolean attemptedBefore, final Server.Call call) throws InterruptedException, ExecutionException {
    final int failureOutput = input + 1;
    ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
    List<Future<Integer>> list = new ArrayList<Future<Integer>>();
    for (int i = 0; i < numberOfThreads; i++) {
        Callable<Integer> worker = new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                Server.getCurCall().set(call);
                Assert.assertEquals(Server.getCurCall().get(), call);
                int randomPause = pause == 0 ? pause : r.nextInt(pause);
                return testServer.echo(input, failureOutput, randomPause, success);
            }
        };
        Future<Integer> submit = executorService.submit(worker);
        list.add(submit);
    }
    Assert.assertEquals(numberOfThreads, list.size());
    for (Future<Integer> future : list) {
        if (success) {
            Assert.assertEquals(input, future.get().intValue());
        } else {
            Assert.assertEquals(failureOutput, future.get().intValue());
        }
    }
    if (success) {
        // If the operation was successful, all the subsequent operations
        // by other threads should be retries. Operation count should be 1.
        int retries = numberOfThreads + (attemptedBefore ? 0 : -1);
        Assert.assertEquals(1, testServer.operationCount.get());
        Assert.assertEquals(retries, testServer.retryCount.get());
    } else {
        // If the operation failed, all the subsequent operations
        // should execute once more, hence the retry count should be 0 and
        // operation count should be the number of tries
        int opCount = numberOfThreads + (attemptedBefore ? 1 : 0);
        Assert.assertEquals(opCount, testServer.operationCount.get());
        Assert.assertEquals(0, testServer.retryCount.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Callable(java.util.concurrent.Callable)

Example 18 with Future

use of java.util.concurrent.Future in project hadoop by apache.

the class TestByteArrayManager method performanceTest.

static long performanceTest(final int arrayLength, final int maxArrays, final int nThreads, final int[] sleepTimeMSs, final ByteArrayManager impl) throws Exception {
    final ExecutorService pool = Executors.newFixedThreadPool(nThreads);
    final List<Future<Void>> futures = new ArrayList<Future<Void>>(sleepTimeMSs.length);
    final long startTime = Time.monotonicNow();
    for (int i = 0; i < sleepTimeMSs.length; i++) {
        final long sleepTime = sleepTimeMSs[i];
        futures.add(pool.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                byte[] array = impl.newByteArray(arrayLength);
                sleepMs(sleepTime);
                impl.release(array);
                return null;
            }
        }));
    }
    for (Future<Void> f : futures) {
        f.get();
    }
    final long endTime = Time.monotonicNow();
    pool.shutdown();
    return endTime - startTime;
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Callable(java.util.concurrent.Callable)

Example 19 with Future

use of java.util.concurrent.Future in project hadoop by apache.

the class BlockReportTestBase method testInterleavedBlockReports.

// See HDFS-10301
@Test(timeout = 300000)
public void testInterleavedBlockReports() throws IOException, ExecutionException, InterruptedException {
    int numConcurrentBlockReports = 3;
    DataNode dn = cluster.getDataNodes().get(DN_N0);
    final String poolId = cluster.getNamesystem().getBlockPoolId();
    LOG.info("Block pool id: " + poolId);
    final DatanodeRegistration dnR = dn.getDNRegistrationForBP(poolId);
    final StorageBlockReport[] reports = getBlockReports(dn, poolId, true, true);
    // Get the list of storage ids associated with the datanode
    // before the test
    BlockManager bm = cluster.getNameNode().getNamesystem().getBlockManager();
    final DatanodeDescriptor dnDescriptor = bm.getDatanodeManager().getDatanode(dn.getDatanodeId());
    DatanodeStorageInfo[] storageInfos = dnDescriptor.getStorageInfos();
    // Send the block report concurrently using
    // numThreads=numConcurrentBlockReports
    ExecutorService executorService = Executors.newFixedThreadPool(numConcurrentBlockReports);
    List<Future<Void>> futureList = new ArrayList<>(numConcurrentBlockReports);
    for (int i = 0; i < numConcurrentBlockReports; i++) {
        futureList.add(executorService.submit(new Callable<Void>() {

            @Override
            public Void call() throws IOException {
                sendBlockReports(dnR, poolId, reports);
                return null;
            }
        }));
    }
    for (Future<Void> future : futureList) {
        future.get();
    }
    executorService.shutdown();
    // Verify that the storages match before and after the test
    Assert.assertArrayEquals(storageInfos, dnDescriptor.getStorageInfos());
}
Also used : StorageBlockReport(org.apache.hadoop.hdfs.server.protocol.StorageBlockReport) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) DatanodeDescriptor(org.apache.hadoop.hdfs.server.blockmanagement.DatanodeDescriptor) DatanodeRegistration(org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration) DatanodeStorageInfo(org.apache.hadoop.hdfs.server.blockmanagement.DatanodeStorageInfo) BlockManager(org.apache.hadoop.hdfs.server.blockmanagement.BlockManager) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 20 with Future

use of java.util.concurrent.Future in project hadoop by apache.

the class Util method execute.

/** Execute the callables by a number of threads */
public static <T, E extends Callable<T>> void execute(int nThreads, List<E> callables) throws InterruptedException, ExecutionException {
    final ExecutorService executor = HadoopExecutors.newFixedThreadPool(nThreads);
    final List<Future<T>> futures = executor.invokeAll(callables);
    for (Future<T> f : futures) f.get();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46