Repository: sentry Updated Branches: refs/heads/sentry-ha-redesign 829bf1717 -> 101bc048c
SENTRY-1765: CounterWait.update throw exception when input is old (ALex Kolbasov, reviewed by Na Li, Kalyan Kalvagadda and Vamsee Yarlagadda) Project: http://git-wip-us.apache.org/repos/asf/sentry/repo Commit: http://git-wip-us.apache.org/repos/asf/sentry/commit/101bc048 Tree: http://git-wip-us.apache.org/repos/asf/sentry/tree/101bc048 Diff: http://git-wip-us.apache.org/repos/asf/sentry/diff/101bc048 Branch: refs/heads/sentry-ha-redesign Commit: 101bc048ca11af6f142ef0ecbf5a6e1e17028457 Parents: 829bf17 Author: Alexander Kolbasov <[email protected]> Authored: Fri May 12 15:40:33 2017 -0700 Committer: Alexander Kolbasov <[email protected]> Committed: Fri May 12 15:40:33 2017 -0700 ---------------------------------------------------------------------- .../sentry/service/thrift/CounterWait.java | 28 +++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sentry/blob/101bc048/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/service/thrift/CounterWait.java ---------------------------------------------------------------------- diff --git a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/service/thrift/CounterWait.java b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/service/thrift/CounterWait.java index 2b4ee84..9c9bb69 100644 --- a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/service/thrift/CounterWait.java +++ b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/service/thrift/CounterWait.java @@ -19,6 +19,8 @@ package org.apache.sentry.service.thrift; import org.apache.http.annotation.ThreadSafe; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.Semaphore; @@ -70,6 +72,8 @@ public final class CounterWait { // NOTE: We use PriorityBlockingQueue for waiters because it is thread-safe, // we are not using its blocking queue semantics. + private static final Logger LOGGER = LoggerFactory.getLogger(CounterWait.class); + /** Counter value. May only increase. */ private final AtomicLong currentId = new AtomicLong(0); @@ -87,20 +91,26 @@ public final class CounterWait { * value or any value below it. * <p> * The counter value should only increase. - * An attempt to decrease the value is raising - * {@link IllegalArgumentException}. - * The usual case is to have a single updater thread, but we enforce this - * by synchronizing the call. + * An attempt to decrease the value is ignored. * * @param newValue the new counter value */ public synchronized void update(long newValue) { - // Make sure the counter is never decremented - if (newValue < currentId.get()) { - throw new IllegalArgumentException("new counter value " + - String.valueOf(newValue) + - "is smaller then the previous one " + currentId); + // update() is synchronized so the value can't change. + long oldValue = currentId.get(); + + // Avoid doing extra work if not needed + if (oldValue == newValue) { + return; // no-op } + + // Make sure the counter is never decremented. + if (newValue < oldValue) { + LOGGER.error("new counter value {} is smaller then the previous one {}", + newValue, oldValue); + return; // no-op + } + currentId.set(newValue); // Wake up any threads waiting for a counter to reach this value.
