package org.apache.cassandra.schema;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
import java.util.Locale;
import com.google.common.base.Objects;
import org.apache.cassandra.exceptions.ConfigurationException;
import static java.lang.String.format;
public final class SpeculativeRetryParam
{
public enum Kind
{
NONE, CUSTOM, PERCENTILE, ALWAYS
}
public static final SpeculativeRetryParam NONE = none();
public static final SpeculativeRetryParam ALWAYS = always();
public static final SpeculativeRetryParam DEFAULT = percentile(99);
private final Kind kind;
private final double value;
private final double threshold;
private SpeculativeRetryParam(Kind kind, double value)
{
this.kind = kind;
this.value = value;
if (kind == Kind.PERCENTILE)
threshold = value / 100;
else if (kind == Kind.CUSTOM)
threshold = TimeUnit.MILLISECONDS.toNanos((long) value);
else
threshold = value;
}
public Kind kind()
{
return kind;
}
public double threshold()
{
return threshold;
}
public static SpeculativeRetryParam none()
{
return new SpeculativeRetryParam(Kind.NONE, 0);
}
public static SpeculativeRetryParam always()
{
return new SpeculativeRetryParam(Kind.ALWAYS, 0);
}
public static SpeculativeRetryParam custom(double value)
{
return new SpeculativeRetryParam(Kind.CUSTOM, value);
}
public static SpeculativeRetryParam percentile(double value)
{
return new SpeculativeRetryParam(Kind.PERCENTILE, value);
}
public static SpeculativeRetryParam fromString(String value)
{
if (value.toLowerCase(Locale.ENGLISH).endsWith("ms"))
{
try
{
return custom(Double.parseDouble(value.substring(0, value.length() - "ms".length())));
}
catch (IllegalArgumentException e)
{
throw new ConfigurationException(format("Invalid value %s for option '%s'", value, TableParams.Option.SPECULATIVE_RETRY));
}
}
if (value.toUpperCase(Locale.ENGLISH).endsWith(Kind.PERCENTILE.toString()))
{
double threshold;
try
{
threshold = Double.parseDouble(value.substring(0, value.length() - Kind.PERCENTILE.toString().length()));
}
catch (IllegalArgumentException e)
{
throw new ConfigurationException(format("Invalid value %s for option '%s'", value, TableParams.Option.SPECULATIVE_RETRY));
}
if (threshold >= 0.0 && threshold <= 100.0)
return percentile(threshold);
throw new ConfigurationException(format("Invalid value %s for PERCENTILE option '%s': must be between 0.0 and 100.0",
value,
TableParams.Option.SPECULATIVE_RETRY));
}
if (value.equals(Kind.NONE.toString()))
return NONE;
if (value.equals(Kind.ALWAYS.toString()))
return ALWAYS;
throw new ConfigurationException(format("Invalid value %s for option '%s'", value, TableParams.Option.SPECULATIVE_RETRY));
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof SpeculativeRetryParam))
return false;
SpeculativeRetryParam srp = (SpeculativeRetryParam) o;
return kind == srp.kind && threshold == srp.threshold;
}
@Override
public int hashCode()
{
return Objects.hashCode(kind, threshold);
}
@Override
public String toString()
{
switch (kind)
{
case CUSTOM:
return format("%sms", value);
case PERCENTILE:
return format("%sPERCENTILE", new DecimalFormat("#.#####").format(value));
default:
return kind.toString();
}
}
}