/** * 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)); }
/** * 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)); }
@Override public WriteSummary call() throws TimeoutException, InterruptedException { long checksum = 0; long pos = 0; CommonUtils.waitFor("Writing to local completes", () -> mLocalWriter.isClosed()); synchronized (buffer) { int len = buffer.position(); while (pos < len) { checksum += BufferUtils.byteToInt(buffer.get((int) pos)); pos++; } } return new WriteSummary(pos, checksum); } });
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()); }
private void edgeLockTest(LockMode take, LockMode tryToTake, boolean expectBlocking) throws Exception { InodeLockManager lockManager = new InodeLockManager(); AtomicBoolean threadFinished = new AtomicBoolean(false); LockResource lock = lockManager.lockEdge(new Edge(10, "name"), take); Thread t = new Thread(() -> { // Use a new Edge each time to make sure we aren't comparing edges by reference. try (LockResource lr = lockManager.lockEdge(new Edge(10, "name"), tryToTake)) { threadFinished.set(true); } }); t.start(); if (expectBlocking) { CommonUtils.sleepMs(20); assertFalse(threadFinished.get()); lock.close(); } CommonUtils.waitFor("lock to be acquired by the second thread", () -> threadFinished.get()); } }
@Override public void apply(FileSystem fs) throws Exception { try (FileOutStream out = fs.createFile(FILE, CreateFilePOptions.newBuilder() .setBlockSizeBytes(Constants.KB).setWriteType(WritePType.ASYNC_THROUGH).build())) { out.write("test".getBytes()); } // Nested file try (FileOutStream out = fs.createFile(NESTED_FILE, CreateFilePOptions.newBuilder().setBlockSizeBytes(Constants.KB) .setWriteType(WritePType.ASYNC_THROUGH).setRecursive(true).build())) { out.write("test".getBytes()); } CommonUtils.waitFor("files to be persisted", () -> { try { return fs.getStatus(FILE).isPersisted() && fs.getStatus(NESTED_FILE).isPersisted(); } catch (IOException | AlluxioException e) { throw new RuntimeException(e); } }); }
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); } }
private void inodeLockTest(LockMode take, LockMode tryToTake, boolean expectBlocking) throws Exception { InodeLockManager lockManager = new InodeLockManager(); AtomicBoolean threadFinished = new AtomicBoolean(false); MutableInodeFile inode = MutableInodeFile.create(0, 0, "name", 0, CreateFileContext.defaults()); LockResource lock = lockManager.lockInode(inode, take); Thread t = new Thread(() -> { // Copy the inode to make sure we aren't comparing inodes by reference. MutableInodeFile inodeCopy = MutableInodeFile.fromJournalEntry(inode.toJournalEntry().getInodeFile()); try (LockResource lr = lockManager.lockInode(inodeCopy, tryToTake)) { threadFinished.set(true); } }); t.start(); if (expectBlocking) { CommonUtils.sleepMs(20); assertFalse(threadFinished.get()); lock.close(); } CommonUtils.waitFor("lock to be acquired by the second thread", () -> threadFinished.get()); }
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()); }