/** * Gets a snapshot of the queue internals. * * @return The snapshot, including Queue Size, Item Fill Rate and elapsed time of the oldest item. */ synchronized QueueStats getStatistics() { int size = this.writes.size(); double fillRatio = calculateFillRatio(this.totalLength, size); int processingTime = this.lastDurationMillis; if (processingTime == 0 && size > 0) { // We get in here when this method is invoked prior to any operation being completed. Since lastDurationMillis // is only set when an item is completed, in this special case we just estimate based on the amount of time // the first item in the queue has been added. processingTime = (int) ((this.timeSupplier.get() - this.writes.peekFirst().getQueueAddedTimestamp()) / AbstractTimer.NANOS_TO_MILLIS); } return new QueueStats(size, fillRatio, processingTime); }
/** * Removes all the completed writes (whether successful or failed) from the beginning of the queue, until the first * non-completed item is encountered or the queue is empty. * * @return A CleanupStatus representing the state of the Operation. If there were failed writes, this will be WriteFailed, * otherwise it will be one of QueueEmpty or QueueNotEmpty, depending on the final state of the queue when this method * finishes. */ synchronized CleanupStatus removeFinishedWrites() { Exceptions.checkNotClosed(this.closed, this); long currentTime = this.timeSupplier.get(); long totalElapsed = 0; int removedCount = 0; boolean failedWrite = false; while (!this.writes.isEmpty() && this.writes.peekFirst().isDone()) { Write w = this.writes.removeFirst(); this.totalLength = Math.max(0, this.totalLength - w.data.getLength()); removedCount++; totalElapsed += currentTime - w.getQueueAddedTimestamp(); failedWrite |= w.getFailureCause() != null; } if (removedCount > 0) { this.lastDurationMillis = (int) (totalElapsed / removedCount / AbstractTimer.NANOS_TO_MILLIS); } return failedWrite ? CleanupStatus.WriteFailed : this.writes.isEmpty() ? CleanupStatus.QueueEmpty : CleanupStatus.QueueNotEmpty; }
long expectedElapsed = write.getQueueAddedTimestamp(); int removed = 1; while (!writes.isEmpty() && writes.peekFirst().isDone()) { expectedElapsed += writes.pollFirst().getQueueAddedTimestamp(); removed++;