@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; } } }
/** * @return the default instance of {@link WaitForOptions} */ public static WaitForOptions defaults() { return new WaitForOptions().setInterval(DEFAULT_INTERVAL).setTimeoutMs(NEVER); }
@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; } } }
/** * Attempts to look up the {@link Server} for the given class. Because {@link Server}s can be * looked up and added to the registry in parallel, the lookup is retried until the given timeout * period has elapsed. * * @param clazz the class of the {@link Server} to get * @param timeoutMs timeout for looking up the server * @param <W> the type of the {@link Server} to get * @return the {@link Server} instance */ public <W extends T> W get(final Class<W> clazz, int timeoutMs) { try { CommonUtils.waitFor("server " + clazz.getName() + " to be created", () -> { try (LockResource r = new LockResource(mLock)) { return mRegistry.get(clazz) != null; } }, WaitForOptions.defaults().setTimeoutMs(timeoutMs)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (TimeoutException e) { throw new RuntimeException(e); } T server = mRegistry.get(clazz); if (!(clazz.isInstance(server))) { throw new RuntimeException("Server is not an instance of " + clazz.getName()); } return clazz.cast(server); }
/** * 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; } }
/** * 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)); }
/** * 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()); }
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)); }
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)); }
@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); } }
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()); }