@Override public boolean waitForReady(int timeoutMs) { try { CommonUtils.waitFor(this + " to start", () -> mStopped == false, WaitForOptions.defaults().setTimeoutMs(timeoutMs)); return true; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } catch (TimeoutException e) { return false; } } }
/** * Persists the given file to the under file system. * * @param fs {@link FileSystem} to carry out Alluxio operations * @param fsContext the {@link FileSystemContext} linked to the {@link FileSystem} client * @param uri the uri of the file to persist */ public static void persistFile(final FileSystem fs, final FileSystemContext fsContext, final AlluxioURI uri) throws IOException, TimeoutException, InterruptedException { FileSystemMasterClient client = fsContext.acquireMasterClient(); try { client.scheduleAsyncPersist(uri); } finally { fsContext.releaseMasterClient(client); } CommonUtils.waitFor(String.format("%s to be persisted", uri) , () -> { try { return fs.getStatus(uri).isPersisted(); } catch (Exception e) { Throwables.propagateIfPossible(e); throw new RuntimeException(e); } }, WaitForOptions.defaults().setTimeoutMs(20 * Constants.MINUTE_MS) .setInterval(Constants.SECOND_MS)); }
/** * Tests getting and setting fields. */ @Test public void fields() { Random random = new Random(); int interval = random.nextInt(); int timeout = random.nextInt(); WaitForOptions options = WaitForOptions.defaults(); options.setInterval(interval); options.setTimeoutMs(timeout); Assert.assertEquals(interval, options.getInterval()); Assert.assertEquals(timeout, options.getTimeoutMs()); }
/** * @return the default instance of {@link WaitForOptions} */ public static WaitForOptions defaults() { return new WaitForOptions().setInterval(DEFAULT_INTERVAL).setTimeoutMs(NEVER); }
/** * Waits for an operation to return a non-null value with a specified timeout. * * @param description the description of this operation * @param operation the operation * @param options the options to use * @param <T> the type of the return value * @throws TimeoutException if the function times out while waiting to get a non-null value * @return the first non-null value generated by the operation */ public static <T> T waitForResult(String description, Supplier<T> operation, WaitForOptions options) throws InterruptedException, TimeoutException { T t; long start = System.currentTimeMillis(); int interval = options.getInterval(); int timeout = options.getTimeoutMs(); while ((t = operation.get()) == null) { if (timeout != WaitForOptions.NEVER && System.currentTimeMillis() - start > timeout) { throw new TimeoutException("Timed out waiting for " + description + " options: " + options); } Thread.sleep(interval); } return t; }
@Override public boolean waitForReady(int timeoutMs) { return CommonUtils.waitFor(this + " to start", input -> mStopped == false, WaitForOptions.defaults().setTimeoutMs(timeoutMs).setThrowOnTimeout(false)); } }
throw new IllegalStateException("Unrecognized job status: " + jobInfo.getStatus()); }, WaitForOptions.defaults().setInterval(1000)); } catch (IOException e) { LOG.warn("Failed to close job master client: {}", e.toString());
/** * Waits for a condition to be satisfied. * * @param description a description of what causes condition to evaluate to true * @param condition the condition to wait on * @throws TimeoutException if the function times out while waiting for the condition to be true */ public static void waitFor(String description, Supplier<Boolean> condition) throws InterruptedException, TimeoutException { waitFor(description, condition, WaitForOptions.defaults()); }
/** * @return the default instance of {@link WaitForOptions} */ public static WaitForOptions defaults() { return new WaitForOptions().setInterval(DEFAULT_INTERVAL).setTimeoutMs(NEVER) .setThrowOnTimeout(true); }
@Test public void defaults() { WaitForOptions options = WaitForOptions.defaults(); Assert.assertEquals(WaitForOptions.DEFAULT_INTERVAL, options.getInterval()); Assert.assertEquals(WaitForOptions.NEVER, options.getTimeoutMs()); }
WaitForOptions options) { long start = System.currentTimeMillis(); int interval = options.getInterval(); int timeout = options.getTimeoutMs(); while (!condition.apply(null)) { if (Thread.interrupted()) { if (options.isThrowOnTimeout()) { throw new RuntimeException( "Timed out waiting for " + description + " options: " + options);
@Override public boolean waitForReady(int timeoutMs) { return CommonUtils.waitFor(this + " to start", input -> (mServingThread == null || isServing()), WaitForOptions.defaults().setTimeoutMs(timeoutMs).setThrowOnTimeout(false)); } }
private void testNthSuccess(int n) throws Exception { CountCondition cond = new CountCondition(n); int intervalMs = 10; WaitForOptions opts = WaitForOptions.defaults().setInterval(intervalMs); long start = System.currentTimeMillis(); CommonUtils.waitFor("", cond, opts); long durationMs = System.currentTimeMillis() - start; assertThat((int) durationMs, Matchers.greaterThanOrEqualTo((n - 1) * intervalMs)); assertEquals(n, cond.invocations()); }
/** * Waits for a condition to be satisfied. * * @param description a description of what causes condition to evaluate to true * @param condition the condition to wait on * @return whether the condition returned true; returns false e.g. if the thread was interrupted * or hit the timeout with options.isThrowOnTimeout() = false */ public static boolean waitFor(String description, Supplier<Boolean> condition) { return waitFor(description, input -> condition.get(), WaitForOptions.defaults()); }
/** * Waits for an operation to return a non-null value with a specified timeout. * * @param description the description of this operation * @param operation the operation * @param options the options to use * @param <T> the type of the return value * @return the return value, null if it times out */ public static <T> T waitForResult(String description, Function<Void, T> operation, WaitForOptions options) { T t; long start = System.currentTimeMillis(); int interval = options.getInterval(); int timeout = options.getTimeoutMs(); while ((t = operation.apply(null)) == null) { if (timeout != WaitForOptions.NEVER && System.currentTimeMillis() - start > timeout) { throw new RuntimeException("Timed out waiting for " + description + " options: " + options); } CommonUtils.sleepMs(interval); } return t; }
/** * Waits for a new master to start until a timeout occurs. * * @param timeoutMs the number of milliseconds to wait before giving up and throwing an exception */ public void waitForNewMaster(int timeoutMs) throws TimeoutException, InterruptedException { CommonUtils.waitFor("the new leader master to start", () -> getLeaderIndex() != -1, WaitForOptions.defaults().setTimeoutMs(timeoutMs)); }
/** * Waits for all nodes to be registered. * * @param timeoutMs maximum amount of time to wait, in milliseconds */ public synchronized void waitForAllNodesRegistered(int timeoutMs) throws TimeoutException, InterruptedException { MetaMasterClient metaMasterClient = getMetaMasterClient(); CommonUtils.waitFor("all nodes registered", () -> { try { MasterInfo masterInfo = metaMasterClient.getMasterInfo(Collections.emptySet()); int liveNodeNum = masterInfo.getMasterAddressesList().size() + masterInfo.getWorkerAddressesList().size(); if (liveNodeNum == (mNumMasters + mNumWorkers)) { return true; } else { LOG.info("Master addresses: {}. Worker addresses: {}", masterInfo.getMasterAddressesList(), masterInfo.getWorkerAddressesList()); return false; } } catch (UnavailableException e) { return false; } catch (Exception e) { throw new RuntimeException(e); } }, WaitForOptions.defaults().setInterval(200).setTimeoutMs(timeoutMs)); }
@Override public boolean waitForReady(int timeoutMs) { return CommonUtils.waitFor(this + " to start", input -> mThriftServer != null && mThriftServer.isServing() && mWebServer != null && mWebServer.getServer().isRunning(), WaitForOptions.defaults().setTimeoutMs(timeoutMs).setThrowOnTimeout(false)); }
/** * Waits for a condition to be satisfied. * * @param description a description of what causes condition to evaluate to true * @param condition the condition to wait on * @return whether the condition returned true; returns false e.g. if the thread was interrupted * or hit the timeout with options.isThrowOnTimeout() = false */ public static boolean waitFor(String description, Function<Void, Boolean> condition) { return waitFor(description, condition, WaitForOptions.defaults()); }
@Override public boolean waitForReady(int timeoutMs) { try { CommonUtils.waitFor(this + " to start", () -> (mServingThread == null || isServing()), WaitForOptions.defaults().setTimeoutMs(timeoutMs)); return true; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } catch (TimeoutException e) { return false; } } }