/** * 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()); }
@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; } } }
@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; } } }
@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; } } }
/** * 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)); }
private boolean waitForChannelReady(ManagedChannel managedChannel) { try { Boolean res = CommonUtils.waitForResult("channel to be ready", () -> { ConnectivityState currentState = managedChannel.getState(true); switch (currentState) { case READY: return true; case TRANSIENT_FAILURE: case SHUTDOWN: return false; case IDLE: case CONNECTING: return null; default: return null; } }, WaitForOptions.defaults().setTimeoutMs((int) mHealthCheckTimeoutMs)); return res; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } catch (TimeoutException e) { return false; } }
public boolean waitForChannelReady() { try { Boolean res = CommonUtils.waitForResult("channel to be ready", () -> { ConnectivityState currentState = mManagedChannel.getState(true); switch (currentState) { case READY: return true; case TRANSIENT_FAILURE: case SHUTDOWN: return false; case IDLE: case CONNECTING: return null; default: return null; } }, WaitForOptions.defaults().setTimeoutMs((int) mHealthCheckTimeoutMs)); return res; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } catch (TimeoutException e) { return false; } } }
/** * Waits for the file to complete before opening it. * * @param uri the file path to check * @return whether the file is completed or not */ private boolean waitForFileCompleted(AlluxioURI uri) { try { CommonUtils.waitFor("file completed", () -> { try { return mFileSystem.getStatus(uri).isCompleted(); } catch (Exception e) { throw new RuntimeException(e); } }, WaitForOptions.defaults().setTimeoutMs(MAX_OPEN_WAITTIME_MS)); return true; } catch (InterruptedException ie) { Thread.currentThread().interrupt(); return false; } catch (TimeoutException te) { return false; } }
@Override public boolean waitForReady(int timeoutMs) { try { CommonUtils.waitFor(this + " to start", () -> isServing() && mWebServer != null && mWebServer.getServer().isRunning(), WaitForOptions.defaults().setTimeoutMs(timeoutMs)); return true; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } catch (TimeoutException e) { return false; } }
@Override public boolean waitForReady(int timeoutMs) { try { CommonUtils.waitFor(this + " to start", () -> isServing() && mWebServer != null && mWebServer.getServer().isRunning(), WaitForOptions.defaults().setTimeoutMs(timeoutMs)); return true; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } catch (TimeoutException e) { return false; } }
@Override public boolean waitForReady(int timeoutMs) { try { CommonUtils.waitFor(this + " to start", () -> isServing() && mRegistry.get(BlockWorker.class).getWorkerId() != null && mWebServer != null && mWebServer.getServer().isRunning(), 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)); }
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()); }
/** * 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()); }
private void addAbsent(AlluxioURI path) throws Exception { final ThreadPoolExecutor pool = Whitebox.getInternalState(mUfsAbsentPathCache, "mPool"); final long initialTasks = pool.getCompletedTaskCount(); mUfsAbsentPathCache.process(path, Collections.emptyList()); // Wait until the async task is completed. CommonUtils.waitFor("path (" + path + ") to be added to absent cache", () -> pool.getCompletedTaskCount() != initialTasks, WaitForOptions.defaults().setTimeoutMs(10000)); }
private void removeAbsent(AlluxioURI path) throws Exception { final ThreadPoolExecutor pool = Whitebox.getInternalState(mUfsAbsentPathCache, "mPool"); final long initialTasks = pool.getCompletedTaskCount(); mUfsAbsentPathCache.process(path, Collections.emptyList()); // Wait until the async task is completed. CommonUtils.waitFor("path (" + path + ") to be removed from absent cache", () -> pool.getCompletedTaskCount() != initialTasks, WaitForOptions.defaults().setTimeoutMs(10000)); }
@Override public void apply(Clients clients) throws Exception { FileSystem fs = clients.getFileSystemClient(); Utils.createFile(fs, PATH); clients.getFileSystemMasterClient().scheduleAsyncPersist(PATH); Utils.createFile(fs, NESTED_PATH); clients.getFileSystemMasterClient().scheduleAsyncPersist(NESTED_PATH); CommonUtils.waitFor("file to be async persisted", () -> { try { return fs.getStatus(PATH).isPersisted() && fs.getStatus(NESTED_PATH).isPersisted(); } catch (Exception e) { throw new RuntimeException(e); } }, WaitForOptions.defaults().setTimeoutMs(10 * Constants.SECOND_MS)); }
@Test(expected = TimeoutException.class) public void waitForTimeout() throws Exception { CountCondition cond = new CountCondition(100); WaitForOptions opts = WaitForOptions.defaults().setInterval(3).setTimeoutMs(100); CommonUtils.waitFor("", cond, opts); } }
@Test public void defaults() { WaitForOptions options = WaitForOptions.defaults(); Assert.assertEquals(WaitForOptions.DEFAULT_INTERVAL, options.getInterval()); Assert.assertEquals(WaitForOptions.NEVER, options.getTimeoutMs()); }
private void waitUntilPersisted(final AlluxioURI testFile) throws Exception { // Persistence completion is asynchronous, so waiting is necessary. CommonUtils.waitFor("async persistence is completed for file", () -> { try { FileInfo fileInfo = mFileSystemMaster.getFileInfo(testFile, GET_STATUS_CONTEXT); return fileInfo.getPersistenceState().equals(PersistenceState.PERSISTED.toString()); } catch (FileDoesNotExistException | InvalidPathException | AccessControlException | IOException e) { return false; } }, WaitForOptions.defaults().setTimeoutMs(30000)); FileInfo fileInfo = mFileSystemMaster.getFileInfo(testFile, GET_STATUS_CONTEXT); Map<Long, PersistJob> persistJobs = getPersistJobs(); Assert.assertEquals(0, getPersistRequests().size()); // We update the file info before removing the persist job, so we must wait here. CommonUtils.waitFor("persist jobs list to be empty", () -> persistJobs.isEmpty(), WaitForOptions.defaults().setTimeoutMs(5 * Constants.SECOND_MS)); Assert.assertEquals(PersistenceState.PERSISTED.toString(), fileInfo.getPersistenceState()); }