/** * Try to generate a move/swap fromRegion between fromServer and toServer such that locality is improved. * Returns empty optional if no move can be found */ private Optional<Action> tryMoveOrSwap(Cluster cluster, int fromServer, int fromRegion, int toServer) { // Try move first. We know apriori fromRegion has the highest locality on toServer if (cluster.serverHasTooFewRegions(toServer)) { return Optional.of(getAction(fromServer, fromRegion, toServer, -1)); } // Compare locality gain/loss from swapping fromRegion with regions on toServer double fromRegionLocalityDelta = getWeightedLocality(cluster, fromRegion, toServer) - getWeightedLocality(cluster, fromRegion, fromServer); for (int toRegionIndex : getRandomIterationOrder(cluster.regionsPerServer[toServer].length)) { int toRegion = cluster.regionsPerServer[toServer][toRegionIndex]; double toRegionLocalityDelta = getWeightedLocality(cluster, toRegion, fromServer) - getWeightedLocality(cluster, toRegion, toServer); // If locality would remain neutral or improve, attempt the swap if (fromRegionLocalityDelta + toRegionLocalityDelta >= 0) { return Optional.of(getAction(fromServer, fromRegion, toServer, toRegion)); } } return Optional.absent(); }
@Override Cluster.Action generate(Cluster cluster) { if (this.masterServices == null) { int thisServer = pickRandomServer(cluster); // Pick the other server int otherServer = pickOtherRandomServer(cluster, thisServer); return pickRandomRegions(cluster, thisServer, otherServer); } // Randomly iterate through regions until you find one that is not on ideal host for (int region : getRandomIterationOrder(cluster.numRegions)) { int currentServer = cluster.regionIndexToServerIndex[region]; if (currentServer != cluster.getOrComputeRegionsToMostLocalEntities(LocalityType.SERVER)[region]) { Optional<Action> potential = tryMoveOrSwap( cluster, currentServer, region, cluster.getOrComputeRegionsToMostLocalEntities(LocalityType.SERVER)[region] ); if (potential.isPresent()) { return potential.get(); } } } return Cluster.NullAction; }
@Override public Optional<Long> sizeIfKnown() { return Optional.of((long) length); }
/** Constructor for use by subclasses. */ protected FluentIterable() { this.iterableDelegate = Optional.absent(); }
private Iterable<E> getDelegate() { return iterableDelegate.or(this); }
/** * Returns the equivalent {@code org.apache.hbase.thirdparty.com.google.common.base.Optional} value to the given {@code * java.util.Optional}, or {@code null} if the argument is null. * * @since 21.0 */ @Nullable public static <T> Optional<T> fromJavaUtil(@Nullable java.util.Optional<T> javaUtilOptional) { return (javaUtilOptional == null) ? null : fromNullable(javaUtilOptional.orElse(null)); }
/** * Returns the equivalent {@code java.util.Optional} value to this optional. * * <p>Unfortunately, the method reference {@code Optional::toJavaUtil} will not work, because it * could refer to either the static or instance version of this method. Write out the lambda * expression {@code o -> o.toJavaUtil()} instead. * * @since 21.0 */ public java.util.Optional<T> toJavaUtil() { return java.util.Optional.ofNullable(orNull()); }
/** * Returns the equivalent {@code java.util.Optional} value to the given {@code * org.apache.hbase.thirdparty.com.google.common.base.Optional}, or {@code null} if the argument is null. * * <p>If {@code googleOptional} is known to be non-null, use {@code googleOptional.toJavaUtil()} * instead. * * <p>Unfortunately, the method reference {@code Optional::toJavaUtil} will not work, because it * could refer to either the static or instance version of this method. Write out the lambda * expression {@code o -> Optional.toJavaUtil(o)} instead. * * @since 21.0 */ @Nullable public static <T> java.util.Optional<T> toJavaUtil(@Nullable Optional<T> googleOptional) { return googleOptional == null ? null : googleOptional.toJavaUtil(); }
@Override public Optional<Long> lengthIfKnown() { return Optional.of((long) seq.length()); }
/** * If {@code nullableReference} is non-null, returns an {@code Optional} instance containing that * reference; otherwise returns {@link Optional#absent}. * * <p><b>Comparison to {@code java.util.Optional}:</b> this method is equivalent to Java 8's * {@code Optional.ofNullable}. */ public static <T> Optional<T> fromNullable(@Nullable T nullableReference) { return (nullableReference == null) ? Optional.<T>absent() : new Present<T>(nullableReference); }
/** Constructs a graph with the properties specified in {@code builder}. */ ConfigurableNetwork(NetworkBuilder<? super N, ? super E> builder) { this( builder, builder.nodeOrder.<N, NetworkConnections<N, E>>createMap( builder.expectedNodeCount.or(DEFAULT_NODE_COUNT)), builder.edgeOrder.<E, N>createMap(builder.expectedEdgeCount.or(DEFAULT_EDGE_COUNT))); }
FluentIterable(Iterable<E> iterable) { checkNotNull(iterable); this.iterableDelegate = Optional.fromNullable(this != iterable ? iterable : null); }
/** * @since 18.0 */ // @Override on JDK8 @Nullable public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) { checkNotNull(annotationType); return FluentIterable.from(annotations).filter(annotationType).first().orNull(); }
@Override public Optional<Long> sizeIfKnown() { Optional<Long> optionalUnslicedSize = ByteSource.this.sizeIfKnown(); if (optionalUnslicedSize.isPresent()) { long unslicedSize = optionalUnslicedSize.get(); long off = Math.min(offset, unslicedSize); return Optional.of(Math.min(length, unslicedSize - off)); } return Optional.absent(); }
/** * Specifies the expected number of nodes in the graph. * * @throws IllegalArgumentException if {@code expectedNodeCount} is negative */ public GraphBuilder<N> expectedNodeCount(int expectedNodeCount) { this.expectedNodeCount = Optional.of(checkNonNegative(expectedNodeCount)); return this; }
@Override public <V> Optional<V> transform(Function<? super T, V> function) { checkNotNull(function); return Optional.absent(); }