private void registerSuccess(Object caller, int index, long start) { successCounters.incrementAndGet(index); retryCounters.remove(caller); if (policy == LoadBalancerPolicy.LATENCY_LAST) { latencyTimes.set(index, System.currentTimeMillis() - start); } }
private void registerFailure(Object caller, int index, boolean last) { failureCounters.incrementAndGet(index); if (policy == LoadBalancerPolicy.LATENCY_LAST) { // Make sure we don't pick a backend with a failed request next time latencyTimes.set(index, Long.MAX_VALUE - 1); } if (suspensionTime > 0) { // Try to save the failure time in the failureTimes array long now = System.currentTimeMillis(); long oldestFailureTime = now - failureRateTimeUnit.toMillis(failureRateTime); int i; for (i = 0; i < failureTimes[index].length(); i++) { if (failureTimes[index].get(i) < oldestFailureTime) { failureTimes[index].set(i, now); break; } } // If all failureTimes slots are used then we can suspend the endpoint if (failureTimes[index].length() == 0 || i == failureTimes[index].length() - 1) { suspensionTimes.set(index, now + suspensionTimeUnit.toMillis(suspensionTime)); } } // Remove the caller if (last) { retryCounters.remove(caller); } }
/** * Increments the count of the bucket closest to n, rounding UP. * * @param n */ public void add(long n) { int index = Arrays.binarySearch(bucketOffsets, n); if (index < 0) { // inexact match, take the first bucket higher than n index = -index - 1; } // else exact match; we're good buckets.incrementAndGet(index); }
public synchronized void removeTask(TaskHandle taskHandle) { taskHandle.destroy(); tasks.remove(taskHandle); // record completed stats long threadUsageNanos = taskHandle.getThreadUsageNanos(); int priorityLevel = calculatePriorityLevel(threadUsageNanos); completedTasksPerLevel.incrementAndGet(priorityLevel); }