PHOENIX-2118 Remove/modfiy usages of Guava StopWatch and deprecated ComparisonChain methods
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/a8f0d769 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/a8f0d769 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/a8f0d769 Branch: refs/heads/calcite Commit: a8f0d7696b6ee105d55c7d3dce50563e816cd857 Parents: cf2bc55 Author: Samarth <[email protected]> Authored: Wed Jul 15 12:21:09 2015 -0700 Committer: Samarth <[email protected]> Committed: Wed Jul 15 12:21:09 2015 -0700 ---------------------------------------------------------------------- .../phoenix/monitoring/MetricsStopWatch.java | 8 +- .../query/ConnectionQueryServicesImpl.java | 4 +- .../java/org/apache/phoenix/query/KeyRange.java | 13 +--- .../apache/phoenix/util/PhoenixStopWatch.java | 81 ++++++++++++++++++++ 4 files changed, 91 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/a8f0d769/phoenix-core/src/main/java/org/apache/phoenix/monitoring/MetricsStopWatch.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/monitoring/MetricsStopWatch.java b/phoenix-core/src/main/java/org/apache/phoenix/monitoring/MetricsStopWatch.java index bffb9ad..ee260a8 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/monitoring/MetricsStopWatch.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/monitoring/MetricsStopWatch.java @@ -17,23 +17,23 @@ */ package org.apache.phoenix.monitoring; -import com.google.common.base.Stopwatch; +import org.apache.phoenix.util.PhoenixStopWatch; /** * * Stop watch that is cognizant of the fact whether or not metrics is enabled. * If metrics isn't enabled it doesn't do anything. Otherwise, it delegates - * calls to a {@code Stopwatch}. + * calls to a {@code PhoenixStopWatch}. * */ final class MetricsStopWatch { private final boolean isMetricsEnabled; - private final Stopwatch stopwatch; + private final PhoenixStopWatch stopwatch; MetricsStopWatch(boolean isMetricsEnabled) { this.isMetricsEnabled = isMetricsEnabled; - this.stopwatch = new Stopwatch(); + this.stopwatch = new PhoenixStopWatch(); } void start() { http://git-wip-us.apache.org/repos/asf/phoenix/blob/a8f0d769/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java index 52b038b..a17e28a 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java @@ -158,6 +158,7 @@ import org.apache.phoenix.util.JDBCUtil; import org.apache.phoenix.util.MetaDataUtil; import org.apache.phoenix.util.PhoenixContextExecutor; import org.apache.phoenix.util.PhoenixRuntime; +import org.apache.phoenix.util.PhoenixStopWatch; import org.apache.phoenix.util.PropertiesUtil; import org.apache.phoenix.util.ReadOnlyProps; import org.apache.phoenix.util.SchemaUtil; @@ -167,7 +168,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Joiner; -import com.google.common.base.Stopwatch; import com.google.common.base.Throwables; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; @@ -757,7 +757,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement QueryServicesOptions.DEFAULT_DELAY_FOR_SCHEMA_UPDATE_CHECK); boolean success = false; int numTries = 1; - Stopwatch watch = new Stopwatch(); + PhoenixStopWatch watch = new PhoenixStopWatch(); watch.start(); do { try { http://git-wip-us.apache.org/repos/asf/phoenix/blob/a8f0d769/phoenix-core/src/main/java/org/apache/phoenix/query/KeyRange.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/KeyRange.java b/phoenix-core/src/main/java/org/apache/phoenix/query/KeyRange.java index bca55e8..0612046 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/query/KeyRange.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/query/KeyRange.java @@ -80,23 +80,18 @@ public class KeyRange implements Writable { } }; public static final Comparator<KeyRange> COMPARATOR = new Comparator<KeyRange>() { - @SuppressWarnings("deprecation") @Override public int compare(KeyRange o1, KeyRange o2) { return ComparisonChain.start() -// .compareFalseFirst(o1.lowerUnbound(), o2.lowerUnbound()) - .compare(o2.lowerUnbound(), o1.lowerUnbound()) + .compareFalseFirst(o2.lowerUnbound(), o1.lowerUnbound()) .compare(o1.getLowerRange(), o2.getLowerRange(), Bytes.BYTES_COMPARATOR) // we want o1 lower inclusive to come before o2 lower inclusive, but // false comes before true, so we have to negate -// .compareTrueFirst(o1.isLowerInclusive(), o2.isLowerInclusive()) - .compare(o2.isLowerInclusive(), o1.isLowerInclusive()) + .compareFalseFirst(o2.isLowerInclusive(), o1.isLowerInclusive()) // for the same lower bounding, we want a finite upper bound to // be ordered before an infinite upper bound -// .compareTrueFirst(o1.upperUnbound(), o2.upperUnbound()) - .compare(o1.upperUnbound(), o2.upperUnbound()) + .compareFalseFirst(o1.upperUnbound(), o2.upperUnbound()) .compare(o1.getUpperRange(), o2.getUpperRange(), Bytes.BYTES_COMPARATOR) -// .compareFalseFirst(o1.isUpperInclusive(), o2.isUpperInclusive()) - .compare(o2.isUpperInclusive(), o1.isUpperInclusive()) + .compareFalseFirst(o2.isUpperInclusive(), o1.isUpperInclusive()) .result(); } }; http://git-wip-us.apache.org/repos/asf/phoenix/blob/a8f0d769/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixStopWatch.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixStopWatch.java b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixStopWatch.java new file mode 100644 index 0000000..9001b0a --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixStopWatch.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.phoenix.util; + +/** + * Bare-bones implementation of a stop watch that only measures time in milliseconds. If you want to be fancy then + * please use the guava Stopwatch. However, be warned that the Guava's Stopwatch is a beta class and is subject to + * incompatible changes and removal. So save the future upgrade pain and use this class instead. + */ +public class PhoenixStopWatch { + private boolean isRunning; + private long startTime; + private long elapsedTimeMs; + + /** + * Creates a new stop watch without starting it. + */ + public PhoenixStopWatch() {} + + /** + * Starts the stopwatch. + * + * @return this {@code PhoenixStopWatch} instance + * @throws IllegalStateException + * if the stopwatch is already running. + */ + public PhoenixStopWatch start() { + long currentTime = System.currentTimeMillis(); + if (isRunning) { throw new IllegalStateException("Watch is already running"); } + startTime = currentTime; + isRunning = true; + return this; + } + + /** + * Stops the stopwatch. Future calls to {@link #elapsedMillis()} will return the fixed duration that had elapsed up + * to this point. + * + * @return this {@code PhoenixStopWatch} instance + * @throws IllegalStateException + * if the stopwatch is already stopped. + */ + public PhoenixStopWatch stop() { + long currentTime = System.currentTimeMillis(); + if (!isRunning) { throw new IllegalStateException("Watch wasn't started"); } + elapsedTimeMs = currentTime - startTime; + startTime = 0; + isRunning = false; + return this; + } + + /** + * Returns the current elapsed time shown on this stopwatch, expressed in milliseconds. + */ + public long elapsedMillis() { + return elapsedTimeMs; + } + + /** + * Returns {@code true} if {@link #start()} has been called on this stopwatch, and {@link #stop()} has not been + * called since the last call to {@code start()}. + */ + public boolean isRunning() { + return isRunning; + } +}
