Class LatencyAwarePolicy.Builder
- java.lang.Object
-
- com.datastax.driver.core.policies.LatencyAwarePolicy.Builder
-
- Enclosing class:
- LatencyAwarePolicy
public static class LatencyAwarePolicy.Builder extends Object
Helper builder object to create a latency aware policy.This helper allows to configure the different parameters used by
LatencyAwarePolicy. The only mandatory parameter is the child policy that will be wrapped with latency awareness. The other parameters can be set through the methods of this builder, but all have defaults (that are documented in the javadoc of each method) if you don't.If you observe that the resulting policy excludes hosts too aggressively or not enough so, the main parameters to check are the exclusion threshold (
withExclusionThreshold(double)) and scale (withScale(long, java.util.concurrent.TimeUnit)).- Since:
- 1.0.4
-
-
Field Summary
Fields Modifier and Type Field Description static doubleDEFAULT_EXCLUSION_THRESHOLDstatic intDEFAULT_MIN_MEASUREstatic longDEFAULT_RETRY_PERIOD_NANOSstatic longDEFAULT_SCALE_NANOSstatic longDEFAULT_UPDATE_RATE_NANOS
-
Constructor Summary
Constructors Constructor Description Builder(LoadBalancingPolicy childPolicy)Creates a new latency aware policy builder given the child policy that the resulting policy wraps.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description LatencyAwarePolicybuild()Builds a new latency aware policy using the options set on this builder.LatencyAwarePolicy.BuilderwithExclusionThreshold(double exclusionThreshold)Sets the exclusion threshold to use for the resulting latency aware policy.LatencyAwarePolicy.BuilderwithMininumMeasurements(int minMeasure)Sets the minimum number of measurements per-host to consider for the resulting latency aware policy.LatencyAwarePolicy.BuilderwithRetryPeriod(long retryPeriod, TimeUnit unit)Sets the retry period for the resulting latency aware policy.LatencyAwarePolicy.BuilderwithScale(long scale, TimeUnit unit)Sets the scale to use for the resulting latency aware policy.LatencyAwarePolicy.BuilderwithUpdateRate(long updateRate, TimeUnit unit)Sets the update rate for the resulting latency aware policy.
-
-
-
Field Detail
-
DEFAULT_EXCLUSION_THRESHOLD
public static final double DEFAULT_EXCLUSION_THRESHOLD
- See Also:
- Constant Field Values
-
DEFAULT_SCALE_NANOS
public static final long DEFAULT_SCALE_NANOS
-
DEFAULT_RETRY_PERIOD_NANOS
public static final long DEFAULT_RETRY_PERIOD_NANOS
-
DEFAULT_UPDATE_RATE_NANOS
public static final long DEFAULT_UPDATE_RATE_NANOS
-
DEFAULT_MIN_MEASURE
public static final int DEFAULT_MIN_MEASURE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Builder
public Builder(LoadBalancingPolicy childPolicy)
Creates a new latency aware policy builder given the child policy that the resulting policy wraps.- Parameters:
childPolicy- the load balancing policy to wrap with latency awareness.
-
-
Method Detail
-
withExclusionThreshold
public LatencyAwarePolicy.Builder withExclusionThreshold(double exclusionThreshold)
Sets the exclusion threshold to use for the resulting latency aware policy.The exclusion threshold controls how much worse the average latency of a node must be compared to the fastest performing node for it to be penalized by the policy.
The default exclusion threshold (if this method is not called) is 2. In other words, the resulting policy excludes nodes that are more than twice slower than the fastest node.
- Parameters:
exclusionThreshold- the exclusion threshold to use. Must be greater or equal to 1.- Returns:
- this builder.
- Throws:
IllegalArgumentException- ifexclusionThreshold < 1.
-
withScale
public LatencyAwarePolicy.Builder withScale(long scale, TimeUnit unit)
Sets the scale to use for the resulting latency aware policy.The
scaleprovides control on how the weight given to older latencies decreases over time. For a given host, if a new latencylis received at timet, and the previously calculated average isprevcalculated at timet', then the newly calculated averageavgfor that host is calculated thusly:
Typically, with ad = (t - t') / scale alpha = 1 - (ln(d+1) / d) avg = alpha * l + (1 - alpha) * prevscaleof 100 milliseconds (the default), if a new latency is measured and the previous measure is 10 millisecond old (sod=0.1), thenalphawill be around0.05. In other words, the new latency will weight 5% of the updated average. A bigger scale will get less weight to new measurements (compared to previous ones), a smaller one will give them more weight.The default scale (if this method is not used) is of 100 milliseconds. If unsure, try this default scale first and experiment only if it doesn't provide acceptable results (hosts are excluded too quickly or not fast enough and tuning the exclusion threshold doesn't help).
- Parameters:
scale- the scale to use.unit- the unit ofscale.- Returns:
- this builder.
- Throws:
IllegalArgumentException- ifscale <= 0.
-
withRetryPeriod
public LatencyAwarePolicy.Builder withRetryPeriod(long retryPeriod, TimeUnit unit)
Sets the retry period for the resulting latency aware policy.The retry period defines how long a node may be penalized by the policy before it is given a 2nd change. More precisely, a node is excluded from query plans if both his calculated average latency is
exclusionThresholdtimes slower than the fastest node average latency (at the time the query plan is computed) and his calculated average latency has been updated since less thanretryPeriod. Since penalized nodes will likely not see their latency updated, this is basically how long the policy will exclude a node.- Parameters:
retryPeriod- the retry period to use.unit- the unit forretryPeriod.- Returns:
- this builder.
- Throws:
IllegalArgumentException- ifretryPeriod < 0.
-
withUpdateRate
public LatencyAwarePolicy.Builder withUpdateRate(long updateRate, TimeUnit unit)
Sets the update rate for the resulting latency aware policy.The update rate defines how often the minimum average latency is recomputed. While the average latency score of each node is computed iteratively (updated each time a new latency is collected), the minimum score needs to be recomputed from scratch every time, which is slightly more costly. For this reason, the minimum is only re-calculated at the given fixed rate and cached between re-calculation.
The default update rate if 100 milliseconds, which should be appropriate for most applications. In particular, note that while we want to avoid to recompute the minimum for every query, that computation is not particularly intensive either and there is no reason to use a very slow rate (more than second is probably unnecessarily slow for instance).
- Parameters:
updateRate- the update rate to use.unit- the unit forupdateRate.- Returns:
- this builder.
- Throws:
IllegalArgumentException- ifupdateRate <e; 0.
-
withMininumMeasurements
public LatencyAwarePolicy.Builder withMininumMeasurements(int minMeasure)
Sets the minimum number of measurements per-host to consider for the resulting latency aware policy.Penalizing nodes is based on an average of their recently measured average latency. This average is only meaningful if a minimum of measurements have been collected (moreover, a newly started Cassandra node will tend to perform relatively poorly on the first queries due to the JVM warmup). This is what this option controls. If less that
minMeasuredata points have been collected for a given host, the policy will never penalize that host. Also, the 30% first measurement will be entirely ignored (in other words, the30% * minMeasurefirst measurement to a node are entirely ignored, while the70%next ones are accounted in the latency computed but the node won't get convicted until we've had at leastminMeasuremeasurements).Note that the number of collected measurements for a given host is reset if the node is restarted.
The default for this option (if this method is not called) is 50. Note that it is probably not a good idea to put this option too low if only to avoid the influence of JVM warm-up on newly restarted nodes.
- Parameters:
minMeasure- the minimum measurements to consider.- Returns:
- this builder.
- Throws:
IllegalArgumentException- ifminMeasure < 0.
-
build
public LatencyAwarePolicy build()
Builds a new latency aware policy using the options set on this builder.- Returns:
- the newly created
LatencyAwarePolicy.
-
-