com.github.phantomthief.failover.impl.WeightFailoverBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-failover Show documentation
Show all versions of simple-failover Show documentation
A simple failover library for Java
package com.github.phantomthief.failover.impl;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.IntUnaryOperator;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import com.github.phantomthief.util.ThrowableFunction;
import com.github.phantomthief.util.ThrowablePredicate;
@SuppressWarnings({"checkstyle:VisibilityModifier", "checkstyle:HiddenField"})
public class WeightFailoverBuilder {
private static final Logger logger = getLogger(WeightFailoverBuilder.class);
private static final int DEFAULT_INIT_WEIGHT = 100;
private static final int DEFAULT_FAIL_REDUCE_WEIGHT = 5;
private static final int DEFAULT_SUCCESS_INCREASE_WEIGHT = 1;
private static final long DEFAULT_CHECK_DURATION = SECONDS.toMillis(1);
IntUnaryOperator failReduceWeight;
IntUnaryOperator successIncreaseWeight;
Map initWeightMap;
ToDoubleFunction checker;
long checkDuration;
Consumer onMinWeight;
Consumer onRecovered;
int minWeight = 0;
Integer weightOnMissingNode;
String name;
Predicate filter;
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder name(String value) {
this.name = value;
return (WeightFailoverBuilder) this;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder autoAddOnMissing(int weight) {
checkArgument(weight >= 0);
WeightFailoverBuilder thisBuilder = (WeightFailoverBuilder) this;
this.weightOnMissingNode = weight;
return thisBuilder;
}
@CheckReturnValue
@SuppressWarnings("unchecked")
@Nonnull
public WeightFailoverBuilder onMinWeight(Consumer listener) {
checkNotNull(listener);
WeightFailoverBuilder thisBuilder = (WeightFailoverBuilder) this;
thisBuilder.onMinWeight = listener;
return thisBuilder;
}
@CheckReturnValue
@SuppressWarnings("unchecked")
@Nonnull
public WeightFailoverBuilder onRecovered(Consumer listener) {
checkNotNull(listener);
WeightFailoverBuilder thisBuilder = (WeightFailoverBuilder) this;
thisBuilder.onRecovered = listener;
return thisBuilder;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder minWeight(int value) {
checkArgument(value >= 0);
this.minWeight = value;
return this;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder failReduceRate(double rate) {
checkArgument(rate > 0 && rate <= 1);
failReduceWeight = i -> Math.max(1, (int) (rate * i));
return this;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder failReduce(int weight) {
checkArgument(weight > 0);
failReduceWeight = i -> weight;
return this;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder successIncreaseRate(double rate) {
checkArgument(rate > 0 && rate <= 1);
successIncreaseWeight = i -> Math.max(1, (int) (rate * i));
return this;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder successIncrease(int weight) {
checkArgument(weight > 0);
successIncreaseWeight = i -> weight;
return this;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder checkDuration(long time, TimeUnit unit) {
checkNotNull(unit);
checkArgument(time > 0);
checkDuration = unit.toMillis(time);
return this;
}
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder filter(@Nonnull Predicate filter) {
checkNotNull(filter);
@SuppressWarnings("unchecked")
WeightFailoverBuilder thisBuilder = (WeightFailoverBuilder) this;
thisBuilder.filter = filter;
return thisBuilder;
}
@SuppressWarnings("unchecked")
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder
checker(@Nonnull ThrowableFunction super E, Double, Throwable> failChecker) {
checkNotNull(failChecker);
WeightFailoverBuilder thisBuilder = (WeightFailoverBuilder) this;
thisBuilder.checker = t -> {
try {
return failChecker.apply(t);
} catch (Throwable e) {
logger.error("", e);
return 0;
}
};
return thisBuilder;
}
@SuppressWarnings("unchecked")
@CheckReturnValue
@Nonnull
public WeightFailoverBuilder checker(
@Nonnull ThrowablePredicate super E, Throwable> failChecker,
@Nonnegative double recoveredInitRate) {
checkArgument(recoveredInitRate >= 0 && recoveredInitRate <= 1);
checkNotNull(failChecker);
return checker(it -> failChecker.test(it) ? recoveredInitRate : 0);
}
@Nonnull
public WeightFailover build(Collection extends E> original) {
return build(original, DEFAULT_INIT_WEIGHT);
}
@Nonnull
public WeightFailover build(Collection extends E> original, int initWeight) {
checkNotNull(original);
checkArgument(initWeight > 0);
return build(original.stream().collect(toMap(identity(), i -> initWeight, (u, v) -> u)));
}
@SuppressWarnings("unchecked")
@Nonnull
public WeightFailover build(Map extends E, Integer> original) {
checkNotNull(original);
WeightFailoverBuilder thisBuilder = (WeightFailoverBuilder) this;
thisBuilder.initWeightMap = (Map) original;
return thisBuilder.build();
}
private WeightFailover build() {
ensure();
return new WeightFailover<>(this);
}
private void ensure() {
if (minWeight <= 0) { // if min weight>0, there is no checker need.
checkNotNull(checker);
} else {
if (checker != null) {
logger.warn(
"a failover checker found but minWeight>0. the checker would never reached.");
}
}
if (failReduceWeight == null) {
failReduceWeight = i -> DEFAULT_FAIL_REDUCE_WEIGHT;
}
if (successIncreaseWeight == null) {
successIncreaseWeight = i -> DEFAULT_SUCCESS_INCREASE_WEIGHT;
}
if (checkDuration == 0) {
checkDuration = DEFAULT_CHECK_DURATION;
}
}
}