kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 6cf7a2ef99e18fc11a817600c8bf490f3bb84feb04fcdefebb7950b33171d891 |
|---|---|
| Author | Sylvain Lebresne <sylvain@datastax.com> · 2013-11-19 12:46 +01:00 |
| Committer | Sylvain Lebresne <sylvain@datastax.com> · 2013-11-19 12:46 +01:00 |
| Parents | aad6cce2e34d |
1 file changed
modified driver-core/src/main/java/com/datastax/driver/core/policies/LatencyAwarePolicy.java +22 −9
| @@ -339,7 +339,8 @@ | ||
| 339 | 339 | /** |
| 340 | 340 | * The latency score for the host this is the stats of at the time of the snapshot. |
| 341 | 341 | * |
| 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. | |
| 343 | 344 | */ |
| 344 | 345 | public long getLatencyScore() { |
| 345 | 346 | return average; |
| @@ -364,7 +365,7 @@ | ||
| 364 | 365 | public void update(Host host, long newLatencyNanos) { |
| 365 | 366 | HostLatencyTracker hostTracker = latencies.get(host); |
| 366 | 367 | if (hostTracker == null) { |
| 367 | - hostTracker = new HostLatencyTracker(scale); | |
| 368 | + hostTracker = new HostLatencyTracker(scale, (30L * minMeasure) / 100L); | |
| 368 | 369 | HostLatencyTracker old = latencies.putIfAbsent(host, hostTracker); |
| 369 | 370 | if (old != null) |
| 370 | 371 | hostTracker = old; |
| @@ -420,11 +421,13 @@ | ||
| 420 | 421 | |
| 421 | 422 | private static class HostLatencyTracker { |
| 422 | 423 | |
| 424 | + private final long thresholdToAccount; | |
| 423 | 425 | private final double scale; |
| 424 | 426 | private final AtomicReference<TimestampedAverage> current = new AtomicReference<TimestampedAverage>(); |
| 425 | 427 | |
| 426 | - HostLatencyTracker(long scale) { | |
| 428 | + HostLatencyTracker(long scale, long thresholdToAccount) { | |
| 427 | 429 | this.scale = (double)scale; // We keep in double since that's how we'll use it. |
| 430 | + this.thresholdToAccount = thresholdToAccount; | |
| 428 | 431 | } |
| 429 | 432 | |
| 430 | 433 | public void add(long newLatencyNanos) { |
| @@ -439,8 +442,12 @@ | ||
| 439 | 442 | |
| 440 | 443 | long currentTimestamp = System.nanoTime(); |
| 441 | 444 | |
| 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); | |
| 444 | 451 | |
| 445 | 452 | // Note: it's possible for the delay to be 0, in which case newLatencyNanos will basically be |
| 446 | 453 | // discarded. It's fine: nanoTime is precise enough in practice that even if it happens, it |
| @@ -459,7 +466,7 @@ | ||
| 459 | 466 | double prevWeight = Math.log(scaledDelay+1) / scaledDelay; |
| 460 | 467 | long newAverage = (long)((1.0 - prevWeight) * newLatencyNanos + prevWeight * previous.average); |
| 461 | 468 | |
| 462 | - return new TimestampedAverage(currentTimestamp, newAverage, previous.nbMeasure+1); | |
| 469 | + return new TimestampedAverage(currentTimestamp, newAverage, nbMeasure); | |
| 463 | 470 | } |
| 464 | 471 | |
| 465 | 472 | public TimestampedAverage getCurrentAverage() { |
| @@ -633,9 +640,15 @@ | ||
| 633 | 640 | * Cassandra node will tend to perform relatively poorly on the first |
| 634 | 641 | * queries due to the JVM warmup). This is what this option controls. |
| 635 | 642 | * 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. | |
| 639 | 652 | * <p> |
| 640 | 653 | * The default for this option (if this method is not called) is <b>50</b>. |
| 641 | 654 | * Note that it is probably not a good idea to put this option too low |