@Override public long getDelay(final TimeUnit unit) { return DelayedUtil.getRemainingTime(unit, getTimeout()); }
@Override public String toString() { long timeout = getTimeout(); return "timeout=" + timeout + ", delay=" + getDelay(TimeUnit.MILLISECONDS); } }
protected long syncSlots(final FSDataOutputStream stream, final ByteSlot[] slots, final int offset, final int count) throws IOException { long totalSynced = 0; for (int i = 0; i < count; ++i) { final ByteSlot data = slots[offset + i]; data.writeTo(stream); totalSynced += data.size(); } syncStream(stream); sendPostSyncSignal(); if (LOG.isTraceEnabled()) { LOG.trace("Sync slots=" + count + '/' + syncMaxSlot + ", flushed=" + StringUtils.humanSize(totalSynced)); } return totalSynced; }
@Override public boolean visit(final Result r) throws IOException { if (r != null && !r.isEmpty()) { long st = 0; if (LOG.isTraceEnabled()) { st = System.currentTimeMillis(); } visitMetaEntry(visitor, r); if (LOG.isTraceEnabled()) { long et = System.currentTimeMillis(); LOG.trace("[T] LOAD META PERF " + StringUtils.humanTimeDiff(et - st)); } } else if (isDebugEnabled) { LOG.debug("NULL result from meta - ignoring but this is strange."); } return true; } });
@VisibleForTesting public void setOwner(String owner) { this.owner = StringUtils.isEmpty(owner) ? null : owner; }
@Test public void testDelayedContainerEquals() { Object o1 = new Object(); Object o2 = new Object(); ZeroDelayContainer<Long> lnull = new ZeroDelayContainer(null); ZeroDelayContainer<Long> l10a = new ZeroDelayContainer<>(10L); ZeroDelayContainer<Long> l10b = new ZeroDelayContainer(10L); ZeroDelayContainer<Long> l15 = new ZeroDelayContainer(15L); ZeroDelayContainer<Object> onull = new ZeroDelayContainer<>(null); ZeroDelayContainer<Object> o1ca = new ZeroDelayContainer<>(o1); ZeroDelayContainer<Object> o1cb = new ZeroDelayContainer<>(o1); ZeroDelayContainer<Object> o2c = new ZeroDelayContainer<>(o2); ZeroDelayContainer[] items = new ZeroDelayContainer[] { lnull, l10a, l10b, l15, onull, o1ca, o1cb, o2c, }; assertContainersEquals(lnull, items, lnull, onull); assertContainersEquals(l10a, items, l10a, l10b); assertContainersEquals(l10b, items, l10a, l10b); assertContainersEquals(l15, items, l15); assertContainersEquals(onull, items, lnull, onull); assertContainersEquals(o1ca, items, o1ca, o1cb); assertContainersEquals(o1cb, items, o1ca, o1cb); assertContainersEquals(o2c, items, o2c); }
private void assertContainersEquals(final ZeroDelayContainer src, final ZeroDelayContainer[] items, final ZeroDelayContainer... matches) { for (int i = 0; i < items.length; ++i) { boolean shouldMatch = false; for (int j = 0; j < matches.length; ++j) { if (items[i] == matches[j]) { shouldMatch = true; break; } } boolean isMatching = src.equals(items[i]); assertEquals(src.getObject() + " unexpectedly match " + items[i].getObject(), shouldMatch, isMatching); } }
@Override public void write(int b) { ensureCapacity(size + 1); buf[size++] = (byte)b; }
private void releaseSlot(final ByteSlot slot) { slot.reset(); slotsCache.offer(slot); }
private ByteSlot acquireSlot() { ByteSlot slot = slotsCache.poll(); return slot != null ? slot : new ByteSlot(); }
public DelayedContainerWithTimestamp(final T object, final long timeout) { super(object); setTimeout(timeout); }
@Override public int compareTo(final Delayed other) { return DelayedUtil.compareDelayed(this, other); }
private static long getTimeout(final Delayed o) { assert o instanceof DelayedWithTimeout : "expected DelayedWithTimeout instance, got " + o; return ((DelayedWithTimeout)o).getTimeout(); }
public static int compareDelayed(final Delayed o1, final Delayed o2) { return Long.compare(getTimeout(o1), getTimeout(o2)); }
@Override public void run() { while (executor.isRunning()) { final DelayedWithTimeout task = DelayedUtil.takeWithoutInterrupt(queue); if (task == null || task == DelayedUtil.DELAYED_POISON) { // the executor may be shutting down, // and the task is just the shutdown request continue; } LOG.trace("Executing {}", task); // execute the task if (task instanceof InlineChore) { execInlineChore((InlineChore) task); } else if (task instanceof DelayedProcedure) { execDelayedProcedure((DelayedProcedure<TEnvironment>) task); } else { LOG.error("CODE-BUG unknown timeout task type {}", task); } } }
/** * Called by the ProcedureExecutor when the timeout set by setTimeout() is expired. * <p/> * Another usage for this method is to implement retrying. A procedure can set the state to * {@code WAITING_TIMEOUT} by calling {@code setState} method, and throw a * {@link ProcedureSuspendedException} to halt the execution of the procedure, and do not forget a * call {@link #setTimeout(int)} method to set the timeout. And you should also override this * method to wake up the procedure, and also return false to tell the ProcedureExecutor that the * timeout event has been handled. * @return true to let the framework handle the timeout as abort, false in case the procedure * handled the timeout itself. */ protected synchronized boolean setTimeoutFailure(TEnvironment env) { if (state == ProcedureState.WAITING_TIMEOUT) { long timeDiff = EnvironmentEdgeManager.currentTime() - lastUpdate; setFailure("ProcedureExecutor", new TimeoutIOException("Operation timed out after " + StringUtils.humanTimeDiff(timeDiff))); return true; } return false; }
@Override public void write(byte[] b, int off, int len) { ensureCapacity(size + len); System.arraycopy(b, off, buf, size, len); size += len; }
public void awaitTermination() { try { final long startTime = EnvironmentEdgeManager.currentTime(); for (int i = 0; isAlive(); ++i) { sendStopSignal(); join(250); // Log every two seconds; send interrupt too. if (i > 0 && (i % 8) == 0) { LOG.warn("Waiting termination of thread {}, {}; sending interrupt", getName(), StringUtils.humanTimeDiff(EnvironmentEdgeManager.currentTime() - startTime)); interrupt(); } } } catch (InterruptedException e) { LOG.warn("{} join wait got interrupted", getName(), e); } } }