diff --git a/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLock.java b/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLock.java index 6cb5fba827..90fbfe4a44 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLock.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLock.java @@ -66,14 +66,14 @@ private boolean tryAcquire(long timeout, TimeUnit unit) { try { if (underlying.tryLock(0, unit)) { LOG.debug(LOCK_ACQUIRED_MSG); - return locked(true); + return aquired(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); if (LOG.isDebugEnabled()) { LOG.debug("Interrupted Exception ignored", e); } - return locked(false); + return failedToAquire(); } // If the first shot fails, then we log the waiting messages. @@ -85,23 +85,31 @@ private boolean tryAcquire(long timeout, TimeUnit unit) { try { if (!underlying.tryLock(timeout, unit)) { LOG.error(ErrorMsg.COMPILE_LOCK_TIMED_OUT.getErrorCodedMsg() + ": " + command); - return locked(false); + return failedToAquire(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); if (LOG.isDebugEnabled()) { LOG.debug("Interrupted Exception ignored", e); } - return locked(false); + return failedToAquire(); } } else { underlying.lock(); } LOG.debug(LOCK_ACQUIRED_MSG); + return aquired(); + } + + private boolean aquired() { return locked(true); } + private boolean failedToAquire() { + return locked(false); + } + private boolean locked(boolean isLocked) { this.isLocked = isLocked; return isLocked; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLockFactory.java b/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLockFactory.java index 848cbc5a8a..306140019d 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLockFactory.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/lock/CompileLockFactory.java @@ -32,7 +32,7 @@ */ public final class CompileLockFactory { - private static final ReentrantLock SERIALIZABLE_COMPILE_LOCK = new ReentrantLock(); + private static final ReentrantLock SERIALIZABLE_COMPILE_LOCK = new ReentrantLock(true); private CompileLockFactory() { } @@ -67,12 +67,12 @@ public static CompileLock newInstance(HiveConf conf, String command) { private final Semaphore globalCompileQuotas; SessionWithQuotaCompileLock(int compilePoolSize) { - globalCompileQuotas = new Semaphore(compilePoolSize); + globalCompileQuotas = new Semaphore(compilePoolSize, true); } @Override public void lock() { - getSessionLock().lock(); + SessionState.get().getCompileLock().lock(); globalCompileQuotas.acquireUninterruptibly(); } @@ -81,13 +81,14 @@ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { boolean result = false; long startTime = System.nanoTime(); + ReentrantLock compileLock = SessionState.get().getCompileLock(); try { - result = getSessionLock().tryLock(time, unit) + result = compileLock.tryLock(time, unit) && globalCompileQuotas.tryAcquire( getRemainingTime(startTime, unit.toNanos(time)), TimeUnit.NANOSECONDS); } finally { - if (!result && getSessionLock().isHeldByCurrentThread()) { - getSessionLock().unlock(); + if (!result && compileLock.isHeldByCurrentThread()) { + compileLock.unlock(); } } return result; @@ -95,14 +96,10 @@ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { @Override public void unlock() { - getSessionLock().unlock(); + SessionState.get().getCompileLock().unlock(); globalCompileQuotas.release(); } - private ReentrantLock getSessionLock() { - return SessionState.get().getCompileLock(); - } - private long getRemainingTime(long startTime, long time) { long timeout = time - (System.nanoTime() - startTime); return (timeout < 0) ? 0 : timeout; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java index 71e130b608..9291e110e0 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java @@ -135,7 +135,7 @@ protected ClassLoader parentLoader; // Session-scope compile lock. - private final ReentrantLock compileLock = new ReentrantLock(); + private final ReentrantLock compileLock = new ReentrantLock(true); /** * current configuration.