Elffuss BraidHub

kikocis/java-driver public

Ignore first measurements completely in LatencyAwarePolicy
Changeset6cf7a2ef99e18fc11a817600c8bf490f3bb84feb04fcdefebb7950b33171d891
AuthorSylvain Lebresne <sylvain@datastax.com> · 2013-11-19 12:46 +01:00
CommitterSylvain Lebresne <sylvain@datastax.com> · 2013-11-19 12:46 +01:00
Parentsaad6cce2e34d

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/policies/LatencyAwarePolicy.java +22 −9
@@ -339,7 +339,8 @@
339339 /**
340340 * The latency score for the host this is the stats of at the time of the snapshot.
341341 *
342- * @return the latency score for the host this is the stats of at the time of the snapshot.
342+ * @return the latency score for the host this is the stats of at the time of the snapshot,
343+ * or {@code -1L} if not enough measurements have been taken to assign a score.
343344 */
344345 public long getLatencyScore() {
345346 return average;
@@ -364,7 +365,7 @@
364365 public void update(Host host, long newLatencyNanos) {
365366 HostLatencyTracker hostTracker = latencies.get(host);
366367 if (hostTracker == null) {
367- hostTracker = new HostLatencyTracker(scale);
368+ hostTracker = new HostLatencyTracker(scale, (30L * minMeasure) / 100L);
368369 HostLatencyTracker old = latencies.putIfAbsent(host, hostTracker);
369370 if (old != null)
370371 hostTracker = old;
@@ -420,11 +421,13 @@
420421
421422 private static class HostLatencyTracker {
422423
424+ private final long thresholdToAccount;
423425 private final double scale;
424426 private final AtomicReference<TimestampedAverage> current = new AtomicReference<TimestampedAverage>();
425427
426- HostLatencyTracker(long scale) {
428+ HostLatencyTracker(long scale, long thresholdToAccount) {
427429 this.scale = (double)scale; // We keep in double since that's how we'll use it.
430+ this.thresholdToAccount = thresholdToAccount;
428431 }
429432
430433 public void add(long newLatencyNanos) {
@@ -439,8 +442,12 @@
439442
440443 long currentTimestamp = System.nanoTime();
441444
442- if (previous == null)
443- return new TimestampedAverage(currentTimestamp, newLatencyNanos, 1);
445+ long nbMeasure = previous == null ? 1 : previous.nbMeasure + 1;
446+ if (nbMeasure < thresholdToAccount)
447+ return new TimestampedAverage(currentTimestamp, -1L, nbMeasure);
448+
449+ if (previous == null || previous.average < 0)
450+ return new TimestampedAverage(currentTimestamp, newLatencyNanos, nbMeasure);
444451
445452 // Note: it's possible for the delay to be 0, in which case newLatencyNanos will basically be
446453 // discarded. It's fine: nanoTime is precise enough in practice that even if it happens, it
@@ -459,7 +466,7 @@
459466 double prevWeight = Math.log(scaledDelay+1) / scaledDelay;
460467 long newAverage = (long)((1.0 - prevWeight) * newLatencyNanos + prevWeight * previous.average);
461468
462- return new TimestampedAverage(currentTimestamp, newAverage, previous.nbMeasure+1);
469+ return new TimestampedAverage(currentTimestamp, newAverage, nbMeasure);
463470 }
464471
465472 public TimestampedAverage getCurrentAverage() {
@@ -633,9 +640,15 @@
633640 * Cassandra node will tend to perform relatively poorly on the first
634641 * queries due to the JVM warmup). This is what this option controls.
635642 * If less that {@code minMeasure} data points have been collected for
636- * a given host, the policy will never penalize that host. Note that
637- * the number of collected measurements for a given host is reseted if
638- * the node is restarted.
643+ * a given host, the policy will never penalize that host. Also, the
644+ * 30% first measurement will be entirely ignored (in other words, the
645+ * {@code 30% * minMeasure} first measurement to a node are entirely
646+ * ignored, while the {@code 70%} next ones are accounted in the latency
647+ * computed but the node won't get convicted until we've had at least
648+ * {@code minMeasure} measurements).
649+ * <p>
650+ * Note that the number of collected measurements for a given host is
651+ * reseted if the node is restarted.
639652 * <p>
640653 * The default for this option (if this method is not called) is <b>50</b>.
641654 * Note that it is probably not a good idea to put this option too low