Remove unused code from commons fork. Reviewed at https://reviews.apache.org/r/37874/
Project: http://git-wip-us.apache.org/repos/asf/aurora/repo Commit: http://git-wip-us.apache.org/repos/asf/aurora/commit/85f99544 Tree: http://git-wip-us.apache.org/repos/asf/aurora/tree/85f99544 Diff: http://git-wip-us.apache.org/repos/asf/aurora/diff/85f99544 Branch: refs/heads/master Commit: 85f995447703c117d0df59a8fe34d6e9045bbcd5 Parents: 782f883 Author: Bill Farner <[email protected]> Authored: Sun Aug 30 14:55:39 2015 -0700 Committer: Bill Farner <[email protected]> Committed: Sun Aug 30 14:55:39 2015 -0700 ---------------------------------------------------------------------- .../modules/LocalServiceRegistry.java | 11 - .../org/apache/aurora/common/base/Closures.java | 34 - .../org/apache/aurora/common/base/Commands.java | 42 -- .../aurora/common/base/MorePreconditions.java | 54 -- .../aurora/common/collections/Multimaps.java | 134 ---- .../apache/aurora/common/inject/Bindings.java | 147 ---- .../aurora/common/io/CompatibilityCodec.java | 95 --- .../apache/aurora/common/io/ThriftCodec.java | 104 --- .../apache/aurora/common/net/Environment.java | 47 -- .../org/apache/aurora/common/stats/Stats.java | 60 +- .../aurora/common/stats/StatsProvider.java | 19 +- .../aurora/common/util/BackoffDecider.java | 663 ------------------- .../aurora/common/util/BackoffHelper.java | 24 +- .../aurora/common/util/CommandExecutor.java | 42 -- .../org/apache/aurora/common/util/Random.java | 18 +- .../org/apache/aurora/common/util/Stat.java | 351 ---------- .../apache/aurora/common/util/StateMachine.java | 33 +- .../org/apache/aurora/common/util/Timer.java | 71 -- .../aurora/common/zookeeper/CandidateImpl.java | 38 +- .../aurora/common/zookeeper/ServerSetImpl.java | 72 +- .../common/zookeeper/SingletonService.java | 89 +-- .../common/zookeeper/guice/ServerSetModule.java | 267 -------- .../guice/client/ZooKeeperClientModule.java | 62 -- .../modules/LifecycleModuleTest.java | 5 +- .../modules/LocalServiceRegistryTest.java | 37 +- .../apache/aurora/common/base/ClosuresTest.java | 60 +- .../apache/aurora/common/base/CommandsTest.java | 84 --- .../common/collections/Iterables2Test.java | 12 - .../aurora/common/inject/BindingsTest.java | 37 -- .../aurora/common/io/CodecTestUtilities.java | 34 - .../common/io/CompatibilityCodecTest.java | 101 --- .../aurora/common/io/ThriftCodecTest.java | 54 -- .../apache/aurora/common/stats/StatsTest.java | 16 +- .../aurora/common/util/BackoffDeciderTest.java | 324 --------- .../aurora/common/util/StateMachineTest.java | 140 +--- .../common/zookeeper/CandidateImplTest.java | 69 +- .../common/zookeeper/ServerSetImplTest.java | 7 +- .../aurora/common/zookeeper/ServerSetsTest.java | 2 +- .../common/zookeeper/SingletonServiceTest.java | 109 +-- .../zookeeper/guice/ServerSetModuleTest.java | 76 --- .../benchmark/fakes/FakeStatsProvider.java | 15 - 41 files changed, 66 insertions(+), 3593 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/application/modules/LocalServiceRegistry.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/application/modules/LocalServiceRegistry.java b/commons/src/main/java/org/apache/aurora/common/application/modules/LocalServiceRegistry.java index 027e4a7..806951b 100644 --- a/commons/src/main/java/org/apache/aurora/common/application/modules/LocalServiceRegistry.java +++ b/commons/src/main/java/org/apache/aurora/common/application/modules/LocalServiceRegistry.java @@ -216,17 +216,6 @@ public class LocalServiceRegistry { } /** - * Creates a primary local service. - * - * @param port Service port. - * @param shutdownCommand A command that will shut down the service. - * @return A new primary local service. - */ - public static LocalService primaryService(int port, Command shutdownCommand) { - return new LocalService(true, ImmutableSet.<String>of(), port, shutdownCommand); - } - - /** * Creates a named auxiliary service. * * @param name Service name. http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/base/Closures.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/base/Closures.java b/commons/src/main/java/org/apache/aurora/common/base/Closures.java index d741e9c..d6cb82a 100644 --- a/commons/src/main/java/org/apache/aurora/common/base/Closures.java +++ b/commons/src/main/java/org/apache/aurora/common/base/Closures.java @@ -13,10 +13,8 @@ */ package org.apache.aurora.common.base; -import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -41,38 +39,6 @@ public final class Closures { } /** - * Converts a closure into a function returning {@code null}. - */ - public static <T> Function<T, Void> asFunction(final ExceptionalClosure<T, ?> closure) { - checkNotNull(closure); - - // CHECKSTYLE:OFF IllegalCatch - return new Function<T, Void>() { - @Override public Void apply(T item) { - try { - closure.execute(item); - } catch (Exception e) { - Throwables.propagate(e); - } - return null; - } - }; - // CHECKSTYLE:ON IllegalCatch - } - - /** - * Varargs equivalent of {@link #combine(Iterable)}. - * - * @param closures Closures to combine. - * @param <T> Type accepted by the closures. - * @return A single closure that will fan out all calls to {@link Closure#execute(Object)} to - * the wrapped closures. - */ - public static <T> Closure<T> combine(Closure<T>... closures) { - return combine(ImmutableList.copyOf(closures)); - } - - /** * Combines multiple closures into a single closure, whose calls are replicated sequentially * in the order that they were provided. * If an exception is encountered from a closure it propagates to the top-level closure and the http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/base/Commands.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/base/Commands.java b/commons/src/main/java/org/apache/aurora/common/base/Commands.java index 6d9fc4c..8a88ae3 100644 --- a/commons/src/main/java/org/apache/aurora/common/base/Commands.java +++ b/commons/src/main/java/org/apache/aurora/common/base/Commands.java @@ -13,10 +13,6 @@ */ package org.apache.aurora.common.base; -import com.google.common.collect.ImmutableList; - -import static com.google.common.base.Preconditions.checkNotNull; - /** * Utility functions for working with commands. * @@ -36,42 +32,4 @@ public final class Commands { private Commands() { // utility } - - /** - * Converts a command into a supplier returning null. - * - * @return A supplier whose {@link Supplier#get()} will cause the given - * {@code command} to be executed and {@code null} to be returned. - */ - public static <E extends Exception> ExceptionalSupplier<Void, E> asSupplier( - final ExceptionalCommand<E> command) { - checkNotNull(command); - - return new ExceptionalSupplier<Void, E>() { - @Override public Void get() throws E { - command.execute(); - return null; - } - }; - } - - /** - * Combines multiple {@code commands} into a single command. A {@link RuntimeException} thrown - * during the execution of one of the commands will prevent the subsequent commands from being - * executed. - * - * @param commands Commands to compound. - * @return A command whose {@link Command#execute()} will cause the given {@code commands} to be - * executed serially. - */ - public static Command compound(Iterable<Command> commands) { - final ImmutableList<Command> executableCommands = ImmutableList.copyOf(commands); - return new Command() { - @Override public void execute() { - for (Command command : executableCommands) { - command.execute(); - } - } - }; - } } http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/base/MorePreconditions.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/base/MorePreconditions.java b/commons/src/main/java/org/apache/aurora/common/base/MorePreconditions.java index 1fe19d4..781f97a 100644 --- a/commons/src/main/java/org/apache/aurora/common/base/MorePreconditions.java +++ b/commons/src/main/java/org/apache/aurora/common/base/MorePreconditions.java @@ -87,58 +87,4 @@ public final class MorePreconditions { Preconditions.checkArgument(!Iterables.isEmpty(argument), message, args); return argument; } - - /** - * Checks that a double falls within a specified range, inclusive - * - * @param argument argument to validate. - * @param minimum minimum possible valid value for the argument. - * @param maximum maximum possible valid value for the argument. - * @param message the message template for validation exception messages where %s serves as the - * sole argument placeholder. - * @return the argument if it is valid. - * @throws IllegalArgumentException if the argument falls outside of the specified range. - */ - public static double checkArgumentRange(double argument, double minimum, double maximum, - String message) { - Preconditions.checkArgument(minimum <= argument, message, argument); - Preconditions.checkArgument(argument <= maximum, message, argument); - return argument; - } - - /** - * Checks that an int falls within a specified range, inclusive - * - * @param argument argument to validate. - * @param minimum minimum possible valid value for the argument. - * @param maximum maximum possible valid value for the argument. - * @param message the message template for validation exception messages where %s serves as the - * sole argument placeholder. - * @return the argument if it is valid. - * @throws IllegalArgumentException if the argument falls outside of the specified range. - */ - public static int checkArgumentRange(int argument, int minimum, int maximum, - String message) { - Preconditions.checkArgument(minimum <= argument, message, argument); - Preconditions.checkArgument(argument <= maximum, message, argument); - return argument; - } - - /** - * Checks that at least one of the specified arguments is true. - * - * @param message the message for validation exception messages. - * @param arguments one or more arguments to check. - * @return true if at least one of the arguments is true. - * @throws IllegalArgumentException if none of the arguments are true. - */ - public static boolean checkArguments(String message, - Boolean... arguments) { - for (Boolean argument : arguments) { - if (argument) { - return true; - } - } - throw new IllegalArgumentException(message); - } } http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/collections/Multimaps.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/collections/Multimaps.java b/commons/src/main/java/org/apache/aurora/common/collections/Multimaps.java deleted file mode 100644 index 7f94a8a..0000000 --- a/commons/src/main/java/org/apache/aurora/common/collections/Multimaps.java +++ /dev/null @@ -1,134 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.collections; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; -import com.google.common.collect.Multiset; -import com.google.common.collect.Ordering; -import com.google.common.collect.Sets; - -/** - * Utility class for functions related to Multimaps in the java collections library. - * - * @author William Farner - */ -public final class Multimaps { - - private Multimaps() { - // Utility. - } - - /** - * Prunes a multimap based on a predicate, returning the pruned values. The input map will be - * modified. - * - * @param map The multimap to prune. - * @param filterRule The pruning rule. When the predicate returns {@code false} for an entry, it - * will be pruned, otherwise it will be retained. - * @param <K> The key type in the multimap. - * @param <V> The value type in the multimap. - * @return A new multimap, containing the pruned keys/values. - */ - public static <K, V> Multimap<K, V> prune(Multimap<K, V> map, - Predicate<? super Collection<V>> filterRule) { - Preconditions.checkNotNull(map); - Preconditions.checkNotNull(filterRule); - Multimap<K, V> pruned = ArrayListMultimap.create(); - Iterator<Map.Entry<K, Collection<V>>> asMapItr = map.asMap().entrySet().iterator(); - while (asMapItr.hasNext()) { - Map.Entry<K, Collection<V>> asMapEntry = asMapItr.next(); - if (!filterRule.apply(asMapEntry.getValue())) { - pruned.putAll(asMapEntry.getKey(), asMapEntry.getValue()); - asMapItr.remove(); - } - } - - return pruned; - } - - private static final class AtLeastSize implements Predicate<Collection<?>> { - private final int minSize; - - AtLeastSize(int minSize) { - Preconditions.checkArgument(minSize >= 0); - this.minSize = minSize; - } - - @Override - public boolean apply(Collection<?> c) { - return c.size() >= minSize; - } - } - - /** - * Convenience method to prune key/values pairs where the size of the value collection is below a - * threshold. - * - * @param map The multimap to prune. - * @param minSize The minimum size for retained value collections. - * @param <K> The key type in the multimap. - * @param <V> The value type in the multimap. - * @return A new multimap, containing the pruned keys/values. - * @throws IllegalArgumentException if minSize < 0 - */ - public static <K, V> Multimap<K, V> prune(Multimap<K, V> map, int minSize) { - return prune(map, new AtLeastSize(minSize)); - } - - /** - * Returns the set of keys associated with groups of a size greater than or equal to a given size. - * - * @param map The multimap to search. - * @param minSize The minimum size to return associated keys for. - * @param <K> The key type for the multimap. - * @return The keys associated with groups of size greater than or equal to {@code minSize}. - * @throws IllegalArgumentException if minSize < 0 - */ - public static <K> Set<K> getLargeGroups(Multimap<K, ?> map, int minSize) { - return Sets.newHashSet( - Maps.filterValues(map.asMap(), new AtLeastSize(minSize)).keySet()); - } - - /** - * Returns the set of keys associated with the largest values in the multimap. - * - * @param map The multimap to search. - * @param topValues Number of groupings to find the keys for. - * @return The keys associated with the largest groups, of maximum size {@code topValues}. - */ - public static <K> Set<K> getLargestGroups(Multimap<K, ?> map, int topValues) { - Ordering<Multiset.Entry<K>> groupOrdering = new Ordering<Multiset.Entry<K>>() { - @Override - public int compare(Multiset.Entry<K> entry1, Multiset.Entry<K> entry2) { - return entry1.getCount() - entry2.getCount(); - // overflow-safe, since sizes are nonnegative - } - }; - Set<K> topKeys = Sets.newHashSetWithExpectedSize(topValues); - for (Multiset.Entry<K> entry - : groupOrdering.greatestOf(map.keys().entrySet(), topValues)) { - topKeys.add(entry.getElement()); - } - return topKeys; - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/inject/Bindings.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/inject/Bindings.java b/commons/src/main/java/org/apache/aurora/common/inject/Bindings.java index f910f22..d474ef9 100644 --- a/commons/src/main/java/org/apache/aurora/common/inject/Bindings.java +++ b/commons/src/main/java/org/apache/aurora/common/inject/Bindings.java @@ -26,9 +26,6 @@ import com.google.inject.Key; import com.google.inject.Module; import com.google.inject.PrivateModule; import com.google.inject.TypeLiteral; -import com.google.inject.binder.LinkedBindingBuilder; -import com.google.inject.multibindings.Multibinder; -import com.google.inject.name.Names; /** * A utility that helps with guice bindings. @@ -43,14 +40,6 @@ public final class Bindings { } /** - * Equivalent to calling {@code requireBinding(binder, Key.get(required, Names.named(namedKey)))}. - */ - public static void requireNamedBinding(Binder binder, Class<?> required, String namedKey) { - requireBinding(binder, Key.get(Preconditions.checkNotNull(required), - Names.named(Preconditions.checkNotNull(namedKey)))); - } - - /** * Equivalent to calling {@code requireBinding(binder, Key.get(required))}. */ public static void requireBinding(Binder binder, Class<?> required) { @@ -106,70 +95,6 @@ public final class Bindings { } /** - * A guice binding helper that allows for any combination of Class, TypeLiteral or Key binding - * without forcing guiced implementation to provide all the overloaded binding methods they would - * otherwise have to. - * - * @param <T> The type this helper can be used to bind implementations for. - */ - public interface BindHelper<T> { - - /** - * Associates this BindHelper with an Injector instance. - * - * @param binder The binder for the injector implementations will be bound in. - * @return A binding builder that can be used to bind an implementation with. - */ - LinkedBindingBuilder<T> with(Binder binder); - } - - /** - * Creates a BindHelper for the given binding key that can be used to bind a single instance. - * - * @param key The binding key the returned BindHelper can be use to bind implementations for. - * @param <T> The type the returned BindHelper can be used to bind implementations for. - * @return A BindHelper that can be used to bind an implementation with. - */ - public static <T> BindHelper<T> binderFor(final Key<T> key) { - return new BindHelper<T>() { - public LinkedBindingBuilder<T> with(Binder binder) { - return binder.bind(key); - } - }; - } - - /** - * Creates a BindHelper for the given type that can be used to add a binding of to a set. - * - * @param type The type the returned BindHelper can be use to bind implementations for. - * @param <T> The type the returned BindHelper can be used to bind implementations for. - * @return A BindHelper that can be used to bind an implementation with. - */ - public static <T> BindHelper<T> multiBinderFor(final Class<T> type) { - return new BindHelper<T>() { - public LinkedBindingBuilder<T> with(Binder binder) { - return Multibinder.newSetBinder(binder, type).addBinding(); - } - }; - } - - /** - * Checks that the given annotation instance is a {@link BindingAnnotation @BindingAnnotation}. - * - * @param annotation The annotation instance to check. - * @param <T> The type of the binding annotation. - * @return The checked binding annotation. - * @throws NullPointerException If the given {@code annotation} is null. - * @throws IllegalArgumentException If the given {@code annotation} is not a - * {@literal @BindingAnnotation}. - */ - public static <T extends Annotation> T checkBindingAnnotation(T annotation) { - Preconditions.checkNotNull(annotation); - checkBindingAnnotation(annotation.annotationType()); - return annotation; - } - - /** * Checks that the given annotation type is a {@link BindingAnnotation @BindingAnnotation}. * * @param annotationType The annotation type to check. @@ -225,24 +150,6 @@ public final class Bindings { } /** - * Creates a key factory that produces keys for a given annotation instance. - * - * @param annotation The annotation instance to apply to all keys. - * @return A key factory that creates annotated keys. - */ - public static KeyFactory annotatedKeyFactory(final Annotation annotation) { - checkBindingAnnotation(annotation); - return new KeyFactory() { - @Override public <T> Key<T> create(Class<T> type) { - return Key.get(type, annotation); - } - @Override public <T> Key<T> create(TypeLiteral<T> type) { - return Key.get(type, annotation); - } - }; - } - - /** * Creates a key factory that produces keys for a given annotation type. * * @param annotationType The annotation type to apply to all keys. @@ -259,58 +166,4 @@ public final class Bindings { } }; } - - /** - * A utility that helps rebind keys. - */ - public static final class Rebinder { - private final Binder binder; - private final KeyFactory bindToFactory; - - /** - * Creates a Rebinder that links bindings to keys from the given {@code bindToFactory}. - * - * @param binder A binder to rebind keys in. - * @param bindToFactory A factory for the rebinding key. - */ - public Rebinder(Binder binder, KeyFactory bindToFactory) { - this.binder = Preconditions.checkNotNull(binder); - this.bindToFactory = Preconditions.checkNotNull(bindToFactory); - } - - /** - * Rebinds the given key to another, linking bindings. - * - * @param fromKey The source key to rebind. - * @return The key that {@code key} was rebound to. - */ - public <T> Key<T> rebind(Key<T> fromKey) { - Key<T> toKey = bindToFactory.create(fromKey.getTypeLiteral()); - binder.bind(toKey).to(fromKey); - requireBinding(binder, fromKey); - return toKey; - } - } - - /** - * Creates a Rebinder that rebinds keys to the given annotation instance. - * - * @param binder A binder to rebind keys in. - * @param annotation The annotation instance to rebind keys to. - * @return A Rebinder targeting the given {@code annotationType}. - */ - public static Rebinder rebinder(Binder binder, Annotation annotation) { - return new Rebinder(binder, annotatedKeyFactory(annotation)); - } - - /** - * Creates a Rebinder that rebinds keys to the given annotation type. - * - * @param binder A binder to rebind keys in. - * @param annotationType The annotation type to rebind keys to. - * @return A Rebinder targeting the given {@code annotationType}. - */ - public static Rebinder rebinder(Binder binder, Class<? extends Annotation> annotationType) { - return new Rebinder(binder, annotatedKeyFactory(annotationType)); - } } http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/io/CompatibilityCodec.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/io/CompatibilityCodec.java b/commons/src/main/java/org/apache/aurora/common/io/CompatibilityCodec.java deleted file mode 100644 index c49c7dd..0000000 --- a/commons/src/main/java/org/apache/aurora/common/io/CompatibilityCodec.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.io; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PushbackInputStream; - -import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; - -/** - * A codec that composes two codecs: a primary and a compatibility codec. It always serializes with - * the primary codec, but can make a decision on deserialization based on the first few bytes of the - * serialized format whether to use the compatibility codec. This allows for easier transition - * between storage formats as the codec remains able to read the old serialized format. - * - * @author Attila Szegedi - * - * @param <T> the type of objects this codec is for. - */ -public class CompatibilityCodec<T> implements Codec<T> { - private final Codec<T> primaryCodec; - private final Codec<T> secondaryCodec; - private final int prefixLength; - private final Predicate<byte[]> discriminator; - - private CompatibilityCodec(Codec<T> primaryCodec, Codec<T> secondaryCodec, int prefixLength, - Predicate<byte[]> discriminator) { - Preconditions.checkNotNull(primaryCodec); - Preconditions.checkNotNull(secondaryCodec); - this.primaryCodec = primaryCodec; - this.secondaryCodec = secondaryCodec; - this.prefixLength = prefixLength; - this.discriminator = discriminator; - } - - /** - * Creates a new compatibility codec instance. - * - * @param primaryCodec the codec used to serialize objects, as well as deserialize them when the - * first byte of the serialized format matches the discriminator. - * @param secondaryCodec the codec used to deserialize objects when the first byte of the - * serialized format does not match the discriminator. - * @param prefixLength the length, in bytes, of the prefix of the message that is inspected for - * determining the format. - * @param discriminator a predicate that will receive an array of at most prefixLength bytes - * (it can receive less if the serialized format is shorter) and has to return true - * if the primary codec should be used for deserialization, otherwise false. - */ - public static <T> CompatibilityCodec<T> create(Codec<T> primaryCodec, Codec<T> secondaryCodec, - int prefixLength, Predicate<byte[]> discriminator) { - return new CompatibilityCodec<T>(primaryCodec, secondaryCodec, prefixLength, discriminator); - } - - @Override - public T deserialize(InputStream source) throws IOException { - final PushbackInputStream in = new PushbackInputStream(source, prefixLength); - final byte[] prefix = readAtMostPrefix(in); - in.unread(prefix); - return (discriminator.apply(prefix) ? primaryCodec : secondaryCodec).deserialize(in); - } - - private byte[] readAtMostPrefix(InputStream in) throws IOException { - final byte[] prefix = new byte[prefixLength]; - int read = 0; - do { - final int readNow = in.read(prefix, read, prefixLength - read); - if (readNow == -1) { - byte[] newprefix = new byte[read]; - System.arraycopy(prefix, 0, newprefix, 0, read); - return newprefix; - } - read += readNow; - } while (read < prefixLength); - return prefix; - } - - @Override - public void serialize(T item, OutputStream sink) throws IOException { - primaryCodec.serialize(item, sink); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/io/ThriftCodec.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/io/ThriftCodec.java b/commons/src/main/java/org/apache/aurora/common/io/ThriftCodec.java deleted file mode 100644 index 6644788..0000000 --- a/commons/src/main/java/org/apache/aurora/common/io/ThriftCodec.java +++ /dev/null @@ -1,104 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.io; - -import com.google.common.base.Function; -import com.google.common.base.Preconditions; -import com.google.common.base.Supplier; -import org.apache.aurora.common.base.MoreSuppliers; -import org.apache.thrift.TBase; -import org.apache.thrift.TException; -import org.apache.thrift.protocol.TBinaryProtocol; -import org.apache.thrift.protocol.TCompactProtocol; -import org.apache.thrift.protocol.TJSONProtocol; -import org.apache.thrift.protocol.TProtocol; -import org.apache.thrift.transport.TIOStreamTransport; -import org.apache.thrift.transport.TTransport; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * A {@code Codec} that can encode and decode thrift structs. - */ -public class ThriftCodec<T extends TBase> implements Codec<T> { - - public static final Function<TTransport, TProtocol> JSON_PROTOCOL = - new Function<TTransport, TProtocol>() { - @Override public TProtocol apply(TTransport transport) { - return new TJSONProtocol(transport); - } - }; - - public static final Function<TTransport, TProtocol> BINARY_PROTOCOL = - new Function<TTransport, TProtocol>() { - @Override public TProtocol apply(TTransport transport) { - return new TBinaryProtocol(transport); - } - }; - - public static final Function<TTransport, TProtocol> COMPACT_PROTOCOL = - new Function<TTransport, TProtocol>() { - @Override public TProtocol apply(TTransport transport) { - return new TCompactProtocol(transport); - } - }; - - private final Supplier<T> templateSupplier; - private final Function<TTransport, TProtocol> protocolFactory; - - public static <T extends TBase> ThriftCodec<T> create(final Class<T> thriftStructType, - Function<TTransport, TProtocol> protocolFactory) { - return new ThriftCodec<T>(MoreSuppliers.of(thriftStructType), protocolFactory); - } - - /** - * @deprecated use {@link ThriftCodec#create(Class, Function)} instead. - */ - @Deprecated - public ThriftCodec(final Class<T> thriftStructType, - Function<TTransport, TProtocol> protocolFactory) { - this(MoreSuppliers.of(thriftStructType), protocolFactory); - } - - public ThriftCodec(Supplier<T> templateSupplier, - Function<TTransport, TProtocol> protocolFactory) { - this.templateSupplier = Preconditions.checkNotNull(templateSupplier); - this.protocolFactory = Preconditions.checkNotNull(protocolFactory); - } - - @Override - public void serialize(T item, OutputStream sink) throws IOException { - Preconditions.checkNotNull(item); - Preconditions.checkNotNull(sink); - try { - item.write(protocolFactory.apply(new TIOStreamTransport(null, sink))); - } catch (TException e) { - throw new IOException("Problem serializing thrift struct: " + item, e); - } - } - - @Override - public T deserialize(InputStream source) throws IOException { - Preconditions.checkNotNull(source); - T template = templateSupplier.get(); - try { - template.read(protocolFactory.apply(new TIOStreamTransport(source, null))); - } catch (TException e) { - throw new IOException("Problem de-serializing thrift struct from stream", e); - } - return template; - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/net/Environment.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/net/Environment.java b/commons/src/main/java/org/apache/aurora/common/net/Environment.java deleted file mode 100644 index 65fd15e..0000000 --- a/commons/src/main/java/org/apache/aurora/common/net/Environment.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.net; - -/** - * Represents a network environment at the granularity of a datacenter. - * - * @author John Sirois - */ -public interface Environment { - - /** - * Returns the name of this network environment's datacenter. - * - * @return the name of this environment's datacenter - */ - String dcName(); - - /** - * Creates a fully qualified hostname for a given unqualified hostname in the network - * environment's datacenter. Does not confirm that the host exists. - * - * @param hostname The simple hostname to qualify. - * @return The fully qualified hostname. - */ - String fullyQualify(String hostname); - - /** - * Checks if a given {@code hostname} is a valid hostname for a host in this network environment; - * does not guarantee that the host exists in this network environment. - * - * @param hostname The simple hostname to check for membership in this network environment. - * @return {@code true} if the hostname is a valid hostname for this network environment. - */ - boolean contains(String hostname); -} http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/stats/Stats.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/stats/Stats.java b/commons/src/main/java/org/apache/aurora/common/stats/Stats.java index 2191f77..f2bd05d 100644 --- a/commons/src/main/java/org/apache/aurora/common/stats/Stats.java +++ b/commons/src/main/java/org/apache/aurora/common/stats/Stats.java @@ -14,7 +14,6 @@ package org.apache.aurora.common.stats; import java.util.Collection; -import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentMap; @@ -53,7 +52,7 @@ public class Stats { // Store stats in the order they were registered, so that derived variables are // sampled after their inputs. private static final Collection<RecordingStat<? extends Number>> ORDERED_NUMERIC_STATS = - new ConcurrentLinkedQueue<RecordingStat<? extends Number>>(); + new ConcurrentLinkedQueue<>(); private static final Cache<String, RecordingStat<? extends Number>> NUMERIC_STATS = CacheBuilder.newBuilder().build(); @@ -128,11 +127,7 @@ public class Stats { /** * A {@link StatRegistry} that provides stats registered with the global {@link Stat}s repository. */ - public static final StatRegistry STAT_REGISTRY = new StatRegistry() { - @Override public Iterable<RecordingStat<? extends Number>> getStats() { - return Stats.getNumericVariables(); - } - }; + public static final StatRegistry STAT_REGISTRY = Stats::getNumericVariables; private static class ExportStat implements Callable<RecordingStat<? extends Number>> { private final AtomicBoolean called = new AtomicBoolean(false); @@ -144,7 +139,7 @@ public class Stats { this.name = name; this.stat = (stat instanceof RecordingStat) ? (RecordingStat<? extends Number>) stat - : new RecordingStatImpl<T>(stat); + : new RecordingStatImpl<>(stat); } @Override @@ -299,27 +294,6 @@ public class Stats { } /** - * Creates and exports an {@link AtomicDouble}. - * - * @param name The name to export the stat with. - * @return A reference to the {@link AtomicDouble} created. - */ - public static AtomicDouble exportDouble(String name) { - return exportDouble(name, 0.0); - } - - /** - * Creates and exports an {@link AtomicDouble} with initial value. - * - * @param name The name to export the stat with. - * @param initialValue The initial stat value. - * @return A reference to the {@link AtomicDouble} created. - */ - public static AtomicDouble exportDouble(String name, double initialValue) { - return export(name, new AtomicDouble(initialValue)); - } - - /** * Exports a metric that tracks the size of a collection. * * @param name Name of the stat to export. @@ -334,34 +308,6 @@ public class Stats { } /** - * Exports a metric that tracks the size of a map. - * - * @param name Name of the stat to export. - * @param map Map whose size should be tracked. - */ - public static void exportSize(String name, final Map<?, ?> map) { - export(new StatImpl<Integer>(name) { - @Override public Integer read() { - return map.size(); - } - }); - } - - /** - * Exports a metric that tracks the size of a cache. - * - * @param name Name of the stat to export. - * @param cache Cache whose size should be tracked. - */ - public static void exportSize(String name, final Cache<?, ?> cache) { - export(new StatImpl<Long>(name) { - @Override public Long read() { - return cache.size(); - } - }); - } - - /** * Exports a 'static' statistic, which will not be registered for time series tracking. * * @param var Variable to statically export. http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/stats/StatsProvider.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/stats/StatsProvider.java b/commons/src/main/java/org/apache/aurora/common/stats/StatsProvider.java index cb1c56b..6b1fa4b 100644 --- a/commons/src/main/java/org/apache/aurora/common/stats/StatsProvider.java +++ b/commons/src/main/java/org/apache/aurora/common/stats/StatsProvider.java @@ -13,10 +13,10 @@ */ package org.apache.aurora.common.stats; -import com.google.common.base.Supplier; - import java.util.concurrent.atomic.AtomicLong; +import com.google.common.base.Supplier; + /** * A minimal interface to a Stats repository. * @@ -61,21 +61,6 @@ public interface StatsProvider { * @param latencyMicros The elapsed time required to complete the request. */ void requestComplete(long latencyMicros); - - /** - * Accumulates the error counter and the request counter. - */ - void incErrors(); - - /** - * Accumulates the reconnect counter. - */ - void incReconnects(); - - /** - * Accumulates the timeout counter. - */ - void incTimeouts(); } /** http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/util/BackoffDecider.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/BackoffDecider.java b/commons/src/main/java/org/apache/aurora/common/util/BackoffDecider.java deleted file mode 100644 index e11a52c..0000000 --- a/commons/src/main/java/org/apache/aurora/common/util/BackoffDecider.java +++ /dev/null @@ -1,663 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.util; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import org.apache.aurora.common.base.MorePreconditions; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; -import org.apache.aurora.common.stats.Stats; -import org.apache.aurora.common.stats.StatsProvider; - -import javax.annotation.Nullable; -import java.util.Deque; -import java.util.Set; -import java.util.concurrent.atomic.AtomicLong; -import java.util.logging.Logger; - -/** - * Handles logic for deciding whether to back off from calls to a backend. - * - * This works by offering a guard method {@link #shouldBackOff()}, which instructs the caller - * whether they should avoid making the call. The backoff logic will maintain statistics about - * the failure rate, and push into a backoff state (silent period) when the failure rate exceeds - * the configured threshold. At the end of the quiet period, a recovery state will be entered, - * during which the decider will allow traffic to ramp back up to full capacity. - * - * The expected use case looks something like this: - * - * <pre> - * void sendRequestGuarded() { - * if (!decider.shouldBackOff()) { - * boolean success = sendRequestUnguarded(); - * if (success) { - * decider.addSuccess(); - * } else { - * decider.addFailure(); - * } - * } - * } - * </pre> - * - * @author William Farner - */ -public class BackoffDecider { - private static final Logger LOG = Logger.getLogger(BackoffDecider.class.getName()); - - // The group that this decider is a part of. - private final Iterable<BackoffDecider> deciderGroup; - - private final TimedStateMachine stateMachine; - - private final String name; - - private final double toleratedFailureRate; - - @VisibleForTesting final RequestWindow requests; - - // Used to calculate backoff durations when in backoff state. - private final BackoffStrategy strategy; - - private final Amount<Long, Time> recoveryPeriod; - private long previousBackoffPeriodNs = 0; - - // Used for random selection during recovery period. - private final Random random; - - private final Clock clock; - private final AtomicLong backoffs; - private final RecoveryType recoveryType; - - /** - * Different types of recovery mechanisms to use after exiting the backoff state. - */ - public static enum RecoveryType { - // Randomly allows traffic to flow through, with a linearly-ascending probability. - RANDOM_LINEAR, - // Allows full traffic capacity to flow during the recovery period. - FULL_CAPACITY - } - - private BackoffDecider(String name, int seedSize, double toleratedFailureRate, - @Nullable Iterable<BackoffDecider> deciderGroup, BackoffStrategy strategy, - @Nullable Amount<Long, Time> recoveryPeriod, - long requestWindowNs, int numBuckets, RecoveryType recoveryType, StatsProvider statsProvider, - Random random, Clock clock) { - MorePreconditions.checkNotBlank(name); - Preconditions.checkArgument(seedSize > 0); - Preconditions.checkArgument(toleratedFailureRate >= 0 && toleratedFailureRate < 1.0); - Preconditions.checkNotNull(strategy); - Preconditions.checkArgument(recoveryPeriod == null || recoveryPeriod.getValue() > 0); - Preconditions.checkArgument(requestWindowNs > 0); - Preconditions.checkArgument(numBuckets > 0); - Preconditions.checkNotNull(recoveryType); - Preconditions.checkNotNull(statsProvider); - Preconditions.checkNotNull(random); - Preconditions.checkNotNull(clock); - - this.name = name; - this.toleratedFailureRate = toleratedFailureRate; - this.deciderGroup = deciderGroup; - this.strategy = strategy; - this.recoveryPeriod = recoveryPeriod; - this.recoveryType = recoveryType; - - this.random = random; - this.clock = clock; - - this.backoffs = statsProvider.makeCounter(name + "_backoffs"); - this.requests = new RequestWindow(requestWindowNs, numBuckets, seedSize); - - this.stateMachine = new TimedStateMachine(name); - } - - /** - * Checks whether the caller should back off and if not then returns immediately; otherwise the - * method blocks until it is safe for the caller to proceed without backing off further based on - * all data available at the time of this call. - * - * @return the amount of time in nanoseconds spent awaiting backoff - * @throws InterruptedException if the calling thread was interrupted while backing off - */ - public long awaitBackoff() throws InterruptedException { - if (shouldBackOff()) { - long backoffTimeMs = stateMachine.getStateRemainingMs(); - - if (backoffTimeMs > 0) { - // Wait without holding any external locks. - Object waitCondition = new Object(); - synchronized (waitCondition) { - waitCondition.wait(backoffTimeMs); - } - return backoffTimeMs; - } - } - return 0; - } - - /** - * Checks whether this decider instructs the caller that it should back off from the associated - * backend. This is determined based on the response history for the backend as well as the - * backoff state of the decider group (if configured). - * - * @return {@code true} if the decider is in backoff mode, otherwise {@code false}. - */ - @SuppressWarnings("fallthrough") - public synchronized boolean shouldBackOff() { - - boolean preventRequest; - switch (stateMachine.getState()) { - case NORMAL: - preventRequest = false; - break; - - case BACKOFF: - if (deciderGroup != null && allOthersBackingOff()) { - LOG.info("Backends in group with " + name + " down, forcing back up."); - stateMachine.transitionUnbounded(State.FORCED_NORMAL); - return false; - } else if (stateMachine.isStateExpired()) { - long recoveryPeriodNs = recoveryPeriod == null ? stateMachine.getStateDurationNs() - : recoveryPeriod.as(Time.NANOSECONDS); - - // The silent period has expired, move to recovery state (and drop to its case block). - stateMachine.transition(State.RECOVERY, recoveryPeriodNs); - LOG.info(String.format("%s recovering for %s ms", name, - Amount.of(recoveryPeriodNs, Time.NANOSECONDS).as(Time.MILLISECONDS))); - } else { - preventRequest = true; - break; - } - - case RECOVERY: - if (deciderGroup != null && allOthersBackingOff()) { - return false; - } else if (stateMachine.isStateExpired()) { - // We have reached the end of the recovery period, return to normal. - stateMachine.transitionUnbounded(State.NORMAL); - previousBackoffPeriodNs = 0; - preventRequest = false; - } else { - switch (recoveryType) { - case RANDOM_LINEAR: - // In the recovery period, allow request rate to return linearly to the full load. - preventRequest = random.nextDouble() > stateMachine.getStateFractionComplete(); - break; - case FULL_CAPACITY: - preventRequest = false; - break; - default: - throw new IllegalStateException("Unhandled recovery type " + recoveryType); - } - } - - break; - - case FORCED_NORMAL: - if (!allOthersBackingOff()) { - // We were in forced normal state, but at least one other backend is up, try recovering. - stateMachine.transition(State.RECOVERY, stateMachine.getStateDurationNs()); - preventRequest = false; - } else { - preventRequest = true; - } - - break; - - default: - LOG.severe("Unrecognized state: " + stateMachine.getState()); - preventRequest = false; - } - - if (preventRequest) { - backoffs.incrementAndGet(); - } - return preventRequest; - } - - private boolean allOthersBackingOff() { - // Search for another decider that is not backing off. - for (BackoffDecider decider : deciderGroup) { - State deciderState = decider.stateMachine.getState(); - boolean inBackoffState = deciderState == State.BACKOFF || deciderState == State.FORCED_NORMAL; - if ((decider != this) && !inBackoffState) { - return false; - } - } - - return true; - } - - /** - * Records a failed request to the backend. - */ - public void addFailure() { - addResult(false); - } - - /** - * Records a successful request to the backend. - */ - public void addSuccess() { - addResult(true); - } - - /** - * Transitions the state to BACKOFF and logs a message appropriately if it is doing so because of high fail rate - * or by force. - * - * @param failRate rate of request failures on this host. - * @param force if {@code true}, forces the transition to BACKOFF. Typically used in cases when the host - * was not found to be alive by LiveHostChecker. - */ - public synchronized void transitionToBackOff(double failRate, boolean force) { - long prevBackoffMs = Amount.of(previousBackoffPeriodNs, Time.NANOSECONDS) - .as(Time.MILLISECONDS); - - long backoffPeriodNs = Amount.of(strategy.calculateBackoffMs(prevBackoffMs), Time.MILLISECONDS) - .as(Time.NANOSECONDS); - if (!force) { - LOG.info(String.format("%s failure rate at %g, backing off for %s ms", name,failRate, - Amount.of(backoffPeriodNs, Time.NANOSECONDS).as(Time.MILLISECONDS))); - } else { - LOG.info(String.format("%s forced to back off for %s ms", name, - Amount.of(backoffPeriodNs, Time.NANOSECONDS).as(Time.MILLISECONDS))); - } - stateMachine.transition(State.BACKOFF, backoffPeriodNs); - previousBackoffPeriodNs = backoffPeriodNs; - } - - @SuppressWarnings("fallthrough") - private synchronized void addResult(boolean success) { - // Disallow statistics updating if we are in backoff state. - if (stateMachine.getState() == State.BACKOFF) { - return; - } - - requests.addResult(success); - double failRate = requests.getFailureRate(); - boolean highFailRate = requests.isSeeded() && (failRate > toleratedFailureRate); - - switch (stateMachine.getState()) { - case NORMAL: - if (!highFailRate) { - // No-op. - break; - } else { - // Artificially move into recovery state (by falling through) with a zero-duration - // time window, to trigger the initial backoff period. - stateMachine.setStateDurationNs(0); - } - - case RECOVERY: - if (highFailRate) { - // We were trying to recover, and the failure rate is still too high. Go back to - // backoff state for a longer duration. - requests.reset(); - - // transition the state machine to BACKOFF state, due to high fail rate. - transitionToBackOff(failRate, false); - } else { - // Do nothing. We only exit the recovery state by expiration. - } - break; - - case FORCED_NORMAL: - if (!highFailRate) { - stateMachine.transition(State.RECOVERY, stateMachine.getStateDurationNs()); - } - break; - - case BACKOFF: - throw new IllegalStateException("Backoff state may only be exited by expiration."); - } - } - - /** - * Creates a builder object. - * - * @param name Name for the backoff decider to build. - * @return A builder. - */ - public static Builder builder(String name) { - return new Builder(name); - } - - /** - * Builder class to configure a BackoffDecider. - * - * The builder allows for customization of many different parameters to the BackoffDecider, while - * defining defaults wherever possible. The following defaults are used: - * - * <ul> - * <li> seed size - The number of requests to accumulate before a backoff will be considered. - * 100 - * - * <li> tolerated failure rate - Maximum failure rate before backing off. - * 0.5 - * - * <li> decider group - Group this decider is a part of, to prevent complete backend failure. - * null (disabled) - * - * <li> strategy - Used to calculate subsequent backoff durations. - * TruncatedBinaryBackoff, initial 100 ms, max 10s - * - * <li> recovery period - Fixed recovery period while ramping traffic back to full capacity.. - * null (use last backoff period) - * - * <li> request window - Duration of the sliding window of requests to track statistics for. - * 10 seconds - * - * <li> num buckets - The number of time slices within the request window, for stat expiration. - * The sliding request window advances in intervals of request window / num buckets. - * 100 - * - * <li> recovery type - Defines behavior during the recovery period, and how traffic is permitted. - * random linear - * - * <li> stat provider - The stats provider to export statistics to. - * Stats.STATS_PROVIDER - * </ul> - * - */ - public static class Builder { - private String name; - private int seedSize = 100; - private double toleratedFailureRate = 0.5; - private Set<BackoffDecider> deciderGroup = null; - private BackoffStrategy strategy = new TruncatedBinaryBackoff( - Amount.of(100L, Time.MILLISECONDS), Amount.of(10L, Time.SECONDS)); - private Amount<Long, Time> recoveryPeriod = null; - private long requestWindowNs = Amount.of(10L, Time.SECONDS).as(Time.NANOSECONDS); - private int numBuckets = 100; - private RecoveryType recoveryType = RecoveryType.RANDOM_LINEAR; - private StatsProvider statsProvider = Stats.STATS_PROVIDER; - private Random random = Random.Util.newDefaultRandom(); - private Clock clock = Clock.SYSTEM_CLOCK; - - Builder(String name) { - this.name = name; - } - - /** - * Sets the number of requests that must be accumulated before the error rate will be - * calculated. This improves the genesis problem where the first few requests are errors, - * causing flapping in and out of backoff state. - * - * @param seedSize Request seed size. - * @return A reference to the builder. - */ - public Builder withSeedSize(int seedSize) { - this.seedSize = seedSize; - return this; - } - - /** - * Sets the tolerated failure rate for the decider. If the rate is exceeded for the time - * window, the decider begins backing off. - * - * @param toleratedRate The tolerated failure rate (between 0.0 and 1.0, exclusive). - * @return A reference to the builder. - */ - public Builder withTolerateFailureRate(double toleratedRate) { - this.toleratedFailureRate = toleratedRate; - return this; - } - - /** - * Makes the decider a part of a group. When a decider is a part of a group, it will monitor - * the other deciders to ensure that all deciders do not back off at once. - * - * @param deciderGroup Group to make this decider a part of. More deciders may be added to the - * group after this call is made. - * @return A reference to the builder. - */ - public Builder groupWith(Set<BackoffDecider> deciderGroup) { - this.deciderGroup = deciderGroup; - return this; - } - - /** - * Overrides the default backoff strategy. - * - * @param strategy Backoff strategy to use. - * @return A reference to the builder. - */ - public Builder withStrategy(BackoffStrategy strategy) { - this.strategy = strategy; - return this; - } - - /** - * Overrides the default recovery period behavior. By default, the recovery period is equal - * to the previous backoff period (which is equivalent to setting the recovery period to null - * here). A non-null value here will assign a fixed recovery period. - * - * @param recoveryPeriod Fixed recovery period. - * @return A reference to the builder. - */ - public Builder withRecoveryPeriod(@Nullable Amount<Long, Time> recoveryPeriod) { - this.recoveryPeriod = recoveryPeriod; - return this; - } - - /** - * Sets the time window over which to analyze failures. Beyond the time window, request history - * is discarded (and ignored). - * - * @param requestWindow The analysis time window. - * @return A reference to the builder. - */ - public Builder withRequestWindow(Amount<Long, Time> requestWindow) { - this.requestWindowNs = requestWindow.as(Time.NANOSECONDS); - return this; - } - - /** - * Sets the number of time slices that the decider will use to partition aggregate statistics. - * - * @param numBuckets Bucket count. - * @return A reference to the builder. - */ - public Builder withBucketCount(int numBuckets) { - this.numBuckets = numBuckets; - return this; - } - - /** - * Sets the recovery mechanism to use when in the recovery period. - * - * @param recoveryType The recovery mechanism to use. - * @return A reference to the builder. - */ - public Builder withRecoveryType(RecoveryType recoveryType) { - this.recoveryType = recoveryType; - return this; - } - - /** - * Sets the stats provider that statistics should be exported to. - * - * @param statsProvider Stats provider to use. - * @return A reference to the builder. - */ - public Builder withStatsProvider(StatsProvider statsProvider) { - this.statsProvider = statsProvider; - return this; - } - - @VisibleForTesting public Builder withRandom(Random random) { - this.random = random; - return this; - } - - @VisibleForTesting public Builder withClock(Clock clock) { - this.clock = clock; - return this; - } - - /** - * Gets a reference to the built decider object. - * @return A decider object. - */ - public BackoffDecider build() { - BackoffDecider decider = new BackoffDecider(name, seedSize, toleratedFailureRate, - deciderGroup, strategy, recoveryPeriod, requestWindowNs, numBuckets, recoveryType, - statsProvider, random, clock); - if (deciderGroup != null) deciderGroup.add(decider); - return decider; - } - } - - private class TimeSlice { - int requestCount = 0; - int failureCount = 0; - final long bucketStartNs; - - public TimeSlice() { - bucketStartNs = clock.nowNanos(); - } - } - - class RequestWindow { - // These store the sum of the respective fields contained within buckets. Doing so removes the - // need to accumulate the counts within the buckets every time the backoff state is - // recalculated. - @VisibleForTesting long totalRequests = 0; - @VisibleForTesting long totalFailures = 0; - - private final long durationNs; - private final long bucketLengthNs; - private final int seedSize; - - // Stores aggregate request/failure counts for time slices. - private final Deque<TimeSlice> buckets = Lists.newLinkedList(); - - RequestWindow(long durationNs, int bucketCount, int seedSize) { - this.durationNs = durationNs; - this.bucketLengthNs = durationNs / bucketCount; - buckets.addFirst(new TimeSlice()); - this.seedSize = seedSize; - } - - void reset() { - totalRequests = 0; - totalFailures = 0; - buckets.clear(); - buckets.addFirst(new TimeSlice()); - } - - void addResult(boolean success) { - maybeShuffleBuckets(); - buckets.peekFirst().requestCount++; - totalRequests++; - - if (!success) { - buckets.peekFirst().failureCount++; - totalFailures++; - } - } - - void maybeShuffleBuckets() { - // Check if the first bucket is still relevant. - if (clock.nowNanos() - buckets.peekFirst().bucketStartNs >= bucketLengthNs) { - - // Remove old buckets. - while (!buckets.isEmpty() - && buckets.peekLast().bucketStartNs < clock.nowNanos() - durationNs) { - TimeSlice removed = buckets.removeLast(); - totalRequests -= removed.requestCount; - totalFailures -= removed.failureCount; - } - - buckets.addFirst(new TimeSlice()); - } - } - - boolean isSeeded() { - return totalRequests >= seedSize; - } - - double getFailureRate() { - return totalRequests == 0 ? 0 : ((double) totalFailures) / totalRequests; - } - } - - private static enum State { - NORMAL, // All requests are being permitted. - BACKOFF, // Quiet period while waiting for backend to recover/improve. - RECOVERY, // Ramping period where an ascending fraction of requests is being permitted. - FORCED_NORMAL // All other backends in the group are backing off, so this one is forced normal. - } - private class TimedStateMachine { - final StateMachine<State> stateMachine; - - private long stateEndNs; - private long stateDurationNs; - - TimedStateMachine(String name) { - stateMachine = StateMachine.<State>builder(name + "_backoff_state_machine") - .addState(State.NORMAL, State.BACKOFF, State.FORCED_NORMAL) - .addState(State.BACKOFF, State.RECOVERY, State.FORCED_NORMAL) - .addState(State.RECOVERY, State.NORMAL, State.BACKOFF, State.FORCED_NORMAL) - .addState(State.FORCED_NORMAL, State.RECOVERY) - .initialState(State.NORMAL) - .build(); - } - - State getState() { - return stateMachine.getState(); - } - - void transitionUnbounded(State state) { - stateMachine.transition(state); - } - - void transition(State state, long durationNs) { - transitionUnbounded(state); - this.stateEndNs = clock.nowNanos() + durationNs; - this.stateDurationNs = durationNs; - } - - long getStateDurationNs() { - return stateDurationNs; - } - - long getStateDurationMs() { - return Amount.of(stateDurationNs, Time.NANOSECONDS).as(Time.MILLISECONDS); - } - - void setStateDurationNs(long stateDurationNs) { - this.stateDurationNs = stateDurationNs; - } - - long getStateRemainingNs() { - return stateEndNs - clock.nowNanos(); - } - - long getStateRemainingMs() { - return Amount.of(getStateRemainingNs(), Time.NANOSECONDS).as(Time.MILLISECONDS); - } - - double getStateFractionComplete() { - return 1.0 - ((double) getStateRemainingNs()) / stateDurationNs; - } - - boolean isStateExpired() { - return clock.nowNanos() > stateEndNs; - } - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/util/BackoffHelper.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/BackoffHelper.java b/commons/src/main/java/org/apache/aurora/common/util/BackoffHelper.java index 8f31ea7..e789f80 100644 --- a/commons/src/main/java/org/apache/aurora/common/util/BackoffHelper.java +++ b/commons/src/main/java/org/apache/aurora/common/util/BackoffHelper.java @@ -13,14 +13,15 @@ */ package org.apache.aurora.common.util; +import java.util.logging.Logger; + import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; + import org.apache.aurora.common.base.ExceptionalSupplier; import org.apache.aurora.common.quantity.Amount; import org.apache.aurora.common.quantity.Time; -import java.util.logging.Logger; - /** * A utility for dealing with backoffs of retryable actions. * @@ -57,25 +58,6 @@ public class BackoffHelper { } /** - * Creates a new BackoffHelper that uses truncated binary backoff starting at the given - * {@code initialBackoff} and maxing out at the given {@code maxBackoff}. This will either: - * <ul> - * <li>{@code stopAtMax == true} : throw {@code BackoffExpiredException} when maxBackoff is - * reached</li> - * <li>{@code stopAtMax == false} : continue backing off with maxBackoff</li> - * </ul> - * - * @param initialBackoff the initial amount of time to back off - * @param maxBackoff the maximum amount of time to back off - * @param stopAtMax if true, this will throw {@code BackoffStoppedException} when the max backoff is - * reached - */ - public BackoffHelper(Amount<Long, Time> initialBackoff, Amount<Long, Time> maxBackoff, - boolean stopAtMax) { - this(new TruncatedBinaryBackoff(initialBackoff, maxBackoff, stopAtMax)); - } - - /** * Creates a BackoffHelper that uses the given {@code backoffStrategy} to calculate backoffs * between retries. * http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/util/CommandExecutor.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/CommandExecutor.java b/commons/src/main/java/org/apache/aurora/common/util/CommandExecutor.java deleted file mode 100644 index e591721..0000000 --- a/commons/src/main/java/org/apache/aurora/common/util/CommandExecutor.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.util; - -import org.apache.aurora.common.base.ExceptionalCommand; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; - -/** - * Asynchronous executor of enqueued tasks in a rate limited manner. - * - * @author Srinivasan Rajagopal - */ -public interface CommandExecutor { - - /** - * Enqueue a task to be executed with retry semantics defined. - * - * @param name Human readable name for this task. - * @param task task to execute. - * @param exceptionClass Concrete exception type. - * @param maxTries num of tries in case of failure. - * @param retryDelay interval between retries in case of failure. - */ - <E extends Exception> void execute( - String name, - ExceptionalCommand<E> task, - Class<E> exceptionClass, - int maxTries, - Amount<Long, Time> retryDelay); -} http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/util/Random.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/Random.java b/commons/src/main/java/org/apache/aurora/common/util/Random.java index a1f1496..90d111e 100644 --- a/commons/src/main/java/org/apache/aurora/common/util/Random.java +++ b/commons/src/main/java/org/apache/aurora/common/util/Random.java @@ -25,17 +25,17 @@ public interface Random { /** * @see java.util.Random#nextDouble() */ - public double nextDouble(); + double nextDouble(); /** * @see java.util.Random#nextInt(int) */ - public int nextInt(int n); + int nextInt(int n); /** * A Random that wraps a java.util.Random. */ - static class SystemRandom implements Random { + class SystemRandom implements Random { private final java.util.Random rand; public SystemRandom(java.util.Random rand) { @@ -54,7 +54,7 @@ public interface Random { } // Utility class. - public static class Util { + class Util { private Util() {} /** @@ -64,15 +64,5 @@ public interface Random { public static Random newDefaultRandom() { return new SystemRandom(new java.util.Random()); } - - /** - * Adapts a java.util.Random into a Random. - * - * @param rand The java.util.Random to adapt. - * @return A new Random. - */ - public static Random fromSystemRandom(java.util.Random rand) { - return new SystemRandom(rand); - } } } http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/util/Stat.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/Stat.java b/commons/src/main/java/org/apache/aurora/common/util/Stat.java deleted file mode 100644 index 2fec9d9..0000000 --- a/commons/src/main/java/org/apache/aurora/common/util/Stat.java +++ /dev/null @@ -1,351 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.util; - -//*************************************************************** -// - -import java.io.DataInput; -import java.io.DataInputStream; -import java.io.DataOutput; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Serializable; -import java.text.NumberFormat; - -/** - * This class is designed to provide basic statistics collection. - * For each instance of this object statistics and be added to it - * then the sum, mean, std dev, min and max can be gathered at the - * end. To reuse this object, a clear method can be called to reset - * the statistics. - */ -public class Stat implements Serializable { - - /** - * Add a number to the statistics collector. - * doubles are used for all collections. - * - * @param x number added to the statistics. - */ - public void addNumber(int x) { - addNumber((double) x); - } - - /** - * Add a number to the statistics collector. - * doubles are used for all collections. - * - * @param x number added to the statistics. - */ - public void addNumber(float x) { - addNumber((double) x); - } - - /** - * Add a number to the statistics collector. - * doubles are used for all collections. - * - * @param x number added to the statistics. - */ - public synchronized void addNumber(double x) { - if (_max < x) { - _max = x; - } - if (_min > x) { - _min = x; - } - - _sum += x; - _sumOfSq += (x * x); - _number++; - - return; - } - - - /** - * Clear the statistics counters... - */ - public void clear() { - _max = 0; - _min = Double.MAX_VALUE; - _number = 0; - _mean = 0; - _stdDev = 0; - _sum = 0; - _sumOfSq = 0; - } - - - /** - * Create a string representation of the - * statistics collected so far. NOTE this - * is formatted and may not suit all needs - * and thus the user should just call the - * needed methods to get mean, std dev, etc. - * and format the data as needed. - * - * @return String Java string formatted output of results. - */ - public String toString() { - return toString(false); - } - - - /** - * Create a string representation of the - * statistics collected so far. The results - * are formatted in percentage format if - * passed in true, otherwise the results - * are the same as the toString call. NOTE this - * is formatted and may not suit all needs - * and thus the user should just call the - * needed methods to get mean, std dev, etc. - * and format the data as needed. - * - * @param percent Format as percentages if set to true. - * @return String Java string formatted output of results. - */ - public String toString(boolean percent) { - calculate(); - NumberFormat nf = NumberFormat.getInstance(); - nf.setMaximumFractionDigits(4); - - if (_number > 1) { - StringBuffer results = new StringBuffer(); - if (percent) { - results.append("Number:" + nf.format(_number * 100) + "%"); - } else { - results.append("Number:" + nf.format(_number)); - } - - if (percent) { - results.append(" Max:" + nf.format(_max * 100) + "%"); - } else { - results.append(" Max:" + nf.format(_max)); - } - - if (percent) { - results.append(" Min:" + nf.format(_min * 100) + "%"); - } else { - results.append(" Min:" + nf.format(_min)); - } - - if (percent) { - results.append(" Mean:" + nf.format(_mean * 100) + "%"); - } else { - results.append(" Mean:" + nf.format(_mean)); - } - - results.append(" Sum:" + nf.format(_sum)); - results.append(" STD:" + nf.format(_stdDev)); - return results.toString(); - } else if (_number == 1) { - if (percent) { - return ("Number:" + nf.format(_sum * 100) + "%"); - } else { - return ("Number:" + nf.format(_sum)); - } - } else { - return ("Number: N/A"); - } - } - - - private void calculate() { - getMean(); - getStandardDev(); - } - - - /** - * Get the max data element added to the statistics - * object so far. - * - * @return double - Maximum entry added so far. - */ - public double getMax() { - return _max; - } - - - /** - * Get the min data element added to the statistics - * object so far. - * - * @return double - Min entry added so far. - */ - public double getMin() { - return _min; - } - - - /** - * Get the number of data elements added to the statistics - * object so far. - * - * @return double - Number of entries added so far. - */ - public long getNumberOfElements() { - return _number; - } - - - /** - * Get the average or mean of data elements added to the - * statistics object so far. - * - * @return double - Mean of entries added so far. - */ - public double getMean() { - if (_number > 0) { - _mean = _sum / _number; - } - return _mean; - } - - /** - * Get the ratio of the sum of elements divided by the number - * of elements added * 100 - * - * @return double - Percent of entries added so far. - */ - public double getPercent() { - if (_number > 0) { - _mean = _sum / _number; - } - _mean = _mean * 100; - return _mean; - } - - - /** - * Get the sum or mean of data elements added to the - * statistics object so far. - * - * @return double - Sum of entries added so far. - */ - public double getSum() { - return _sum; - } - - - /** - * Get the sum of the squares of the data elements added - * to the statistics object so far. - * - * @return double - Sum of the squares of the entries added so far. - */ - public double getSumOfSq() { - return _sumOfSq; - } - - - /** - * Get the standard deviation of the data elements added - * to the statistics object so far. - * - * @return double - Sum of the standard deviation of the entries added so far. - */ - public double getStandardDev() { - if (_number > 1) { - _stdDev = Math.sqrt((_sumOfSq - ((_sum * _sum) / _number)) / (_number - 1)); - } - return _stdDev; - } - - - /** - * Read the data from the InputStream so it can be used to populate - * the current objects state. - * - * @param in java.io.InputStream to write to. - * @throws IOException - */ - public void readFromDataInput(InputStream in) throws IOException { - DataInput di = new DataInputStream(in); - readFromDataInput(di); - return; - } - - - /** - * Read the data from the DataInput so it can be used to populate - * the current objects state. - * - * @param in java.io.InputStream to write to. - * @throws IOException - */ - public void readFromDataInput(DataInput in) throws IOException { - _max = in.readDouble(); - _min = in.readDouble(); - _number = in.readLong(); - _mean = in.readDouble(); - _stdDev = in.readDouble(); - _sum = in.readDouble(); - _sumOfSq = in.readDouble(); - return; - } - - - /** - * Write the data to the output steam so it can be streamed to an - * other process, wire or storage median in a format that another Stats - * object can read. - * - * @param out java.io.OutputStream to write to. - * @throws IOException - */ - public void writeToDataOutput(OutputStream out) throws IOException { - DataOutput dout = new DataOutputStream(out); - writeToDataOutput(dout); - return; - - } - - - /** - * Write the data to the data output object so it can be written to an - * other process, wire or storage median in a format that another Stats - * object can read. - * - * @param out java.io.DataOutput to write to. - * @throws IOException - */ - public void writeToDataOutput(DataOutput out) throws IOException { - out.writeDouble(_max); - out.writeDouble(_min); - out.writeLong(_number); - out.writeDouble(_mean); - out.writeDouble(_stdDev); - out.writeDouble(_sum); - out.writeDouble(_sumOfSq); - return; - } - - - // ************************************ - private static final long serialVersionUID = 1L; - private double _max = 0 ; - private double _min = Double.MAX_VALUE ; - private long _number = 0 ; - private double _mean = 0 ; - private double _stdDev = 0 ; - private double _sum = 0 ; - private double _sumOfSq ; -} - http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/util/StateMachine.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/StateMachine.java b/commons/src/main/java/org/apache/aurora/common/util/StateMachine.java index 89046da..9fbfbb9 100644 --- a/commons/src/main/java/org/apache/aurora/common/util/StateMachine.java +++ b/commons/src/main/java/org/apache/aurora/common/util/StateMachine.java @@ -31,14 +31,13 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; -import org.apache.commons.lang.builder.HashCodeBuilder; - import org.apache.aurora.common.base.Closure; import org.apache.aurora.common.base.Closures; -import org.apache.aurora.common.base.ExceptionalSupplier; +import org.apache.commons.lang.builder.HashCodeBuilder; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; + import static org.apache.aurora.common.base.MorePreconditions.checkNotBlank; /** @@ -132,34 +131,6 @@ public class StateMachine<T> { } /** - * Executes the supplied {@code work} if the state machine is in the {@code expectedState}, - * postponing any concurrently requested {@link #transition(Object)} until after the execution of - * the work. - * - * @param expectedState The expected state the work should be performed in. - * @param work The work to perform in the {@code expectedState}. - * @param <O> The type returned by the unit of work. - * @param <E> The type of exception that may be thrown by the unit of work. - * @return The result of the unit of work if the current state is the {@code expectedState}. - * @throws IllegalStateException if the current state is not the {@code expectedState}. - * @throws E if the unit of work throws. - */ - public <O, E extends Exception> O doInState(T expectedState, ExceptionalSupplier<O, E> work) - throws E { - - checkNotNull(expectedState); - checkNotNull(work); - - readLock.lock(); - try { - checkState(expectedState); - return work.get(); - } finally { - readLock.unlock(); - } - } - - /** * Transitions the machine into state {@code nextState}. * * @param nextState The state to move into. http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/util/Timer.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/util/Timer.java b/commons/src/main/java/org/apache/aurora/common/util/Timer.java deleted file mode 100644 index 15602ac..0000000 --- a/commons/src/main/java/org/apache/aurora/common/util/Timer.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.util; - -import org.apache.aurora.common.base.Commands; -import org.apache.aurora.common.base.ExceptionalCommand; -import org.apache.aurora.common.base.ExceptionalSupplier; -import org.apache.aurora.common.stats.SlidingStats; - -/** - * A utility for timing blocks of code. - * - * <p>TODO(John Sirois): consider instead: - * <T, E extends Exception> Pair<T, Long> doTimed(ExceptionalSupplier<T, E> timedWork) throws E - * or a subinterface of Command/Closure/Supplier/Function that exposes a timing method as other ways - * to factor in timing. - * - * @author John Sirois - */ -public final class Timer { - - /** - * Times the block of code encapsulated by {@code timedWork} recoding the result in {@code stat}. - * - * @param stat the stat to record the timing with - * @param timedWork the code to time - * @param <E> the type of exception {@code timedWork} may throw - * @throws E if {@code timedWork} throws - */ - public static <E extends Exception> void doTimed(SlidingStats stat, - final ExceptionalCommand<E> timedWork) throws E { - doTimed(stat, Commands.asSupplier(timedWork)); - } - - /** - * Times the block of code encapsulated by {@code timedWork} recoding the result in {@code stat}. - * - * @param stat the stat to record the timing with - * @param timedWork the code to time - * @param <T> the type of result {@code timedWork} returns - * @param <E> the type of exception {@code timedWork} may throw - * @return the result of {@code timedWork} if it completes normally - * @throws E if {@code timedWork} throws - */ - public static <T, E extends Exception> T doTimed(SlidingStats stat, - ExceptionalSupplier<T, E> timedWork) throws E { - StartWatch timer = new StartWatch(); - timer.start(); - try { - return timedWork.get(); - } finally { - timer.stop(); - stat.accumulate(timer.getTime()); - } - } - - private Timer() { - // utility - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/85f99544/commons/src/main/java/org/apache/aurora/common/zookeeper/CandidateImpl.java ---------------------------------------------------------------------- diff --git a/commons/src/main/java/org/apache/aurora/common/zookeeper/CandidateImpl.java b/commons/src/main/java/org/apache/aurora/common/zookeeper/CandidateImpl.java index e16a64d..b10a403 100644 --- a/commons/src/main/java/org/apache/aurora/common/zookeeper/CandidateImpl.java +++ b/commons/src/main/java/org/apache/aurora/common/zookeeper/CandidateImpl.java @@ -68,44 +68,12 @@ public class CandidateImpl implements Candidate { }; private final Group group; - private final Function<Iterable<String>, String> judge; - private final Supplier<byte[]> dataSupplier; /** - * Equivalent to {@link #CandidateImpl(Group, com.google.common.base.Function, Supplier)} using a - * judge that always picks the lowest numbered candidate ephemeral node - by proxy the oldest or - * 1st candidate and a default supplier that provides the ip address of this host according to - * {@link java.net.InetAddress#getLocalHost()} as the leader identifying data. + * Creates a candidate that can be used to offer leadership for the given {@code group}. */ public CandidateImpl(Group group) { - this(group, MOST_RECENT_JUDGE, IP_ADDRESS_DATA_SUPPLIER); - } - - /** - * Creates a candidate that can be used to offer leadership for the given {@code group} using - * a judge that always picks the lowest numbered candidate ephemeral node - by proxy the oldest - * or 1st. The dataSupplier should produce bytes that identify this process as leader. These bytes - * will become available to all participants via the {@link Candidate#getLeaderData()} method. - */ - public CandidateImpl(Group group, Supplier<byte[]> dataSupplier) { - this(group, MOST_RECENT_JUDGE, dataSupplier); - } - - /** - * Creates a candidate that can be used to offer leadership for the given {@code group}. The - * {@code judge} is used to pick the current leader from all group members whenever the group - * membership changes. To form a well-behaved election group with one leader, all candidates - * should use the same judge. The dataSupplier should produce bytes that identify this process - * as leader. These bytes will become available to all participants via the - * {@link Candidate#getLeaderData()} method. - */ - public CandidateImpl( - Group group, - Function<Iterable<String>, String> judge, - Supplier<byte[]> dataSupplier) { this.group = Preconditions.checkNotNull(group); - this.judge = Preconditions.checkNotNull(judge); - this.dataSupplier = Preconditions.checkNotNull(dataSupplier); } @Override @@ -122,7 +90,7 @@ public class CandidateImpl implements Candidate { public Supplier<Boolean> offerLeadership(final Leader leader) throws JoinException, WatchException, InterruptedException { - final Membership membership = group.join(dataSupplier, new Command() { + final Membership membership = group.join(IP_ADDRESS_DATA_SUPPLIER, new Command() { @Override public void execute() { leader.onDefeated(); } @@ -176,6 +144,6 @@ public class CandidateImpl implements Candidate { @Nullable private String getLeader(Iterable<String> memberIds) { - return Iterables.isEmpty(memberIds) ? null : judge.apply(memberIds); + return Iterables.isEmpty(memberIds) ? null : MOST_RECENT_JUDGE.apply(memberIds); } }
