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();
}
}
}
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());
}
}
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;
}
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());
}
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();
}
Aggregations