@Override public void gainPrimacy() { List<Callable<Void>> callables = new ArrayList<>(); for (UfsJournal journal : mJournals.values()) { callables.add(() -> { journal.gainPrimacy(); return null; }); } try { CommonUtils.invokeAll(callables, 1 * Constants.HOUR_MS); } catch (TimeoutException | ExecutionException e) { throw new RuntimeException(e); } }
/** * Creates all the masters and registers them to the master registry. * * @param registry the master registry * @param context master context */ public static void createMasters(MasterRegistry registry, MasterContext context) { List<Callable<Void>> callables = new ArrayList<>(); for (final MasterFactory factory : alluxio.master.ServiceUtils.getMasterServiceLoader()) { callables.add(new Callable<Void>() { @Override public Void call() throws Exception { if (factory.isEnabled()) { factory.create(registry, context); } return null; } }); } try { CommonUtils.invokeAll(callables, 10 * Constants.SECOND_MS); } catch (Exception e) { throw new RuntimeException("Failed to start masters", e); } }
@Test public void invokeAllSuccess() throws Exception { int numTasks = 5; final CyclicBarrier b = new CyclicBarrier(numTasks); final AtomicInteger completed = new AtomicInteger(); List<Callable<Void>> tasks = new ArrayList<>(); for (int i = 0; i < numTasks; i++) { tasks.add(new Callable<Void>() { @Override public Void call() throws Exception { b.await(); completed.incrementAndGet(); return null; } }); } CommonUtils.invokeAll(tasks, 10 * Constants.SECOND_MS); assertEquals(numTasks, completed.get()); }
@Test public void invokeAllHang() throws Exception { int numTasks = 5; List<Callable<Void>> tasks = new ArrayList<>(); for (int i = 0; i < numTasks; i++) { tasks.add(new Callable<Void>() { @Override public Void call() throws Exception { Thread.sleep(10 * Constants.SECOND_MS); return null; } }); } try { CommonUtils.invokeAll(tasks, 50); fail("Expected a timeout exception"); } catch (TimeoutException e) { // Expected } }
@Test public void invokeAllPropagatesException() throws Exception { int numTasks = 5; final AtomicInteger id = new AtomicInteger(); List<Callable<Void>> tasks = new ArrayList<>(); final Exception testException = new Exception("test message"); for (int i = 0; i < numTasks; i++) { tasks.add(new Callable<Void>() { @Override public Void call() throws Exception { int myId = id.incrementAndGet(); // The 3rd task throws an exception if (myId == 3) { throw testException; } return null; } }); } try { CommonUtils.invokeAll(tasks, 2 * Constants.SECOND_MS); fail("Expected an exception to be thrown"); } catch (ExecutionException e) { assertSame(testException, e.getCause()); } }
CommonUtils.invokeAll(callables, (long) callables.size() * Constants.DEFAULT_REGISTRY_GET_TIMEOUT_MS);
/** * Tests that when one task throws an exception and other tasks time out, the exception is * propagated. */ @Test public void invokeAllPropagatesExceptionWithTimeout() throws Exception { int numTasks = 5; final AtomicInteger id = new AtomicInteger(); List<Callable<Void>> tasks = new ArrayList<>(); final Exception testException = new Exception("test message"); for (int i = 0; i < numTasks; i++) { tasks.add(() -> { int myId = id.incrementAndGet(); // The 3rd task throws an exception, other tasks sleep. if (myId == 3) { throw testException; } else { Thread.sleep(10 * Constants.SECOND_MS); } return null; }); } try { CommonUtils.invokeAll(tasks, 500); fail("Expected an exception to be thrown"); } catch (ExecutionException e) { assertSame(testException, e.getCause()); } }
@Test public void invokeAllExceptionAndHang() throws Exception { long start = System.currentTimeMillis(); RuntimeException testException = new RuntimeException("failed"); try { CommonUtils.invokeAll(Arrays.asList( () -> { Thread.sleep(10 * Constants.SECOND_MS); return null; }, () -> { throw testException; } ), 5 * Constants.SECOND_MS); fail("Expected an exception to be thrown"); } catch (ExecutionException e) { assertSame(testException, e.getCause()); } assertThat("invokeAll should exit early if one of the tasks throws an exception", System.currentTimeMillis() - start, Matchers.lessThan(2L * Constants.SECOND_MS)); }
/** * Creates all the masters and registers them to the master registry. * * @param registry the master registry * @param context master context */ public static void createMasters(MasterRegistry registry, MasterContext context) { List<Callable<Void>> callables = new ArrayList<>(); for (final MasterFactory factory : ServiceUtils.getMasterServiceLoader()) { callables.add(new Callable<Void>() { @Override public Void call() throws Exception { if (factory.isEnabled()) { factory.create(registry, context); } return null; } }); } try { CommonUtils.invokeAll(callables, 10, TimeUnit.SECONDS); } catch (Exception e) { throw new RuntimeException("Failed to start masters", e); } } }