diff --git common/src/java/org/apache/hadoop/hive/conf/HiveConf.java common/src/java/org/apache/hadoop/hive/conf/HiveConf.java index 7f4afd9..6413a33 100644 --- common/src/java/org/apache/hadoop/hive/conf/HiveConf.java +++ common/src/java/org/apache/hadoop/hive/conf/HiveConf.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -1579,6 +1580,15 @@ HIVE_SECURITY_COMMAND_WHITELIST("hive.security.command.whitelist", "set,reset,dfs,add,list,delete,compile", "Comma separated list of non-SQL Hive commands users are authorized to execute"), + HIVE_SERVER2_SESSION_CHECK_INTERVAL("hive.server2.session.check.interval", "0s", new TimeValidator(), + "The check interval for sessions, which would be disabled with zero or minus value."), + HIVE_SERVER2_IDLE_SESSION_TIMEOUT("hive.server2.idle.session.timeout", "0s", new TimeValidator(), + "Session will be closed when it's not accessed for this duration, which can be disabled with zero or minus value."), + HIVE_SERVER2_IDLE_OPERATION_TIMEOUT("hive.server2.idle.operation.timeout", "0s", new TimeValidator(), + "Operation will be closed when it's not accessed for this duration of time (msec), which can be disabled with zero value.\n" + + " With positive value, it's checked for operations in terminal state only (FINISHED, CANCELED, CLOSED, ERROR).\n" + + " With negative value, it's checked for all of the operations regardless of state."), + HIVE_CONF_RESTRICTED_LIST("hive.conf.restricted.list", "hive.security.authenticator.manager,hive.security.authorization.manager,hive.users.in.admin.role", "Comma separated list of configuration options which are immutable at runtime"), @@ -1968,6 +1978,53 @@ public void setIntVar(ConfVars var, int val) { setIntVar(this, var, val); } + public static long getTimeVar(Configuration conf, ConfVars var, TimeUnit outUnit) { + assert (var.valClass == String.class) : var.varname; + return toTime(getVar(conf, var), outUnit); + } + + public long getTimeVar(ConfVars var, TimeUnit outUnit) { + String value = getVar(this, var); + return toTime(value, outUnit); + } + + public static long toTime(String value, TimeUnit outUnit) { + String[] parsed = parseTime(value.trim()); + return toTime(parsed[0].trim(), parsed[1].trim(), outUnit); + } + + private static String[] parseTime(String value) { + char[] chars = value.toCharArray(); + int i = 0; + for (; i < chars.length && (chars[i] == '-' || Character.isDigit(chars[i])); i++) { + } + return new String[] {value.substring(0, i), value.substring(i)}; + } + + public static long toTime(String timePart, String unitPart, TimeUnit outUnit) { + return outUnit.convert(Long.valueOf(timePart), unitFor(unitPart)); + } + + public static TimeUnit unitFor(String unit) { + unit = unit.toLowerCase(); + if (unit.equals("d") || unit.startsWith("day")) { + return TimeUnit.DAYS; + } else if (unit.equals("h") || unit.startsWith("hour")) { + return TimeUnit.HOURS; + } else if (unit.equals("m") || unit.startsWith("min")) { + return TimeUnit.MINUTES; + } else if (unit.equals("s") || unit.startsWith("sec")) { + return TimeUnit.SECONDS; + } else if (unit.equals("ms") || unit.startsWith("msec")) { + return TimeUnit.MILLISECONDS; + } else if (unit.equals("us") || unit.startsWith("usec")) { + return TimeUnit.MICROSECONDS; + } else if (unit.equals("ns") || unit.startsWith("nsec")) { + return TimeUnit.NANOSECONDS; + } + throw new IllegalArgumentException("Invalid time unit " + unit); + } + public static long getLongVar(Configuration conf, ConfVars var) { assert (var.valClass == Long.class) : var.varname; return conf.getLong(var.varname, var.defaultLongVal); diff --git common/src/java/org/apache/hadoop/hive/conf/Validator.java common/src/java/org/apache/hadoop/hive/conf/Validator.java index cea9c41..06dc6f9 100644 --- common/src/java/org/apache/hadoop/hive/conf/Validator.java +++ common/src/java/org/apache/hadoop/hive/conf/Validator.java @@ -22,6 +22,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; /** @@ -156,4 +157,36 @@ public String validate(String value) { return null; } } + + public static class TimeValidator implements Validator { + + private TimeUnit timeUnit = TimeUnit.SECONDS; + private Long min; + private Long max; + + public TimeValidator() { + } + + public TimeValidator(Long min, Long max, TimeUnit timeUnit) { + this.timeUnit = timeUnit; + this.min = min; + this.max = max; + } + + @Override + public String validate(String value) { + try { + long time = HiveConf.toTime(value, timeUnit); + if (min != null && time < min) { + return value + " is smaller than " + min + " " + timeUnit.name().toLowerCase(); + } + if (max != null && time > max) { + return value + " is bigger than " + max + " " + timeUnit.name().toLowerCase(); + } + } catch (Exception e) { + return e.toString(); + } + return null; + } + } } diff --git service/src/java/org/apache/hive/service/cli/OperationState.java service/src/java/org/apache/hive/service/cli/OperationState.java index 3e15f0c..b539ae1 100644 --- service/src/java/org/apache/hive/service/cli/OperationState.java +++ service/src/java/org/apache/hive/service/cli/OperationState.java @@ -25,29 +25,26 @@ * */ public enum OperationState { - INITIALIZED(TOperationState.INITIALIZED_STATE), - RUNNING(TOperationState.RUNNING_STATE), - FINISHED(TOperationState.FINISHED_STATE), - CANCELED(TOperationState.CANCELED_STATE), - CLOSED(TOperationState.CLOSED_STATE), - ERROR(TOperationState.ERROR_STATE), - UNKNOWN(TOperationState.UKNOWN_STATE), - PENDING(TOperationState.PENDING_STATE); + INITIALIZED(TOperationState.INITIALIZED_STATE, false), + RUNNING(TOperationState.RUNNING_STATE, false), + FINISHED(TOperationState.FINISHED_STATE, true), + CANCELED(TOperationState.CANCELED_STATE, true), + CLOSED(TOperationState.CLOSED_STATE, true), + ERROR(TOperationState.ERROR_STATE, true), + UNKNOWN(TOperationState.UKNOWN_STATE, false), + PENDING(TOperationState.PENDING_STATE, false); private final TOperationState tOperationState; + private final boolean terminal; - OperationState(TOperationState tOperationState) { + private OperationState(TOperationState tOperationState, boolean terminal) { this.tOperationState = tOperationState; + this.terminal = terminal; } + // must be sync with TOperationState in order public static OperationState getOperationState(TOperationState tOperationState) { - // TODO: replace this with a Map? - for (OperationState opState : values()) { - if (tOperationState.equals(opState.tOperationState)) { - return opState; - } - } - return OperationState.UNKNOWN; + return OperationState.values()[tOperationState.getValue()]; } public static void validateTransition(OperationState oldState, @@ -91,7 +88,8 @@ public static void validateTransition(OperationState oldState, default: // fall-through } - throw new HiveSQLException("Illegal Operation state transition"); + throw new HiveSQLException("Illegal Operation state transition " + + "from " + oldState + " to " + newState); } public void validateTransition(OperationState newState) @@ -102,4 +100,8 @@ public void validateTransition(OperationState newState) public TOperationState toTOperationState() { return tOperationState; } + + public boolean isTerminal() { + return terminal; + } } diff --git service/src/java/org/apache/hive/service/cli/operation/Operation.java service/src/java/org/apache/hive/service/cli/operation/Operation.java index 45fbd61..f8538a2 100644 --- service/src/java/org/apache/hive/service/cli/operation/Operation.java +++ service/src/java/org/apache/hive/service/cli/operation/Operation.java @@ -47,14 +47,19 @@ protected final boolean runAsync; protected volatile Future backgroundHandle; + private long operationTimeout; + private long lastAccessTime; + protected static final EnumSet DEFAULT_FETCH_ORIENTATION_SET = EnumSet.of(FetchOrientation.FETCH_NEXT,FetchOrientation.FETCH_FIRST); protected Operation(HiveSession parentSession, OperationType opType, boolean runInBackground) { - super(); this.parentSession = parentSession; this.runAsync = runInBackground; this.opHandle = new OperationHandle(opType, parentSession.getProtocolVersion()); + lastAccessTime = System.currentTimeMillis(); + operationTimeout = HiveConf.getTimeInMsec(parentSession.getHiveConf(), + HiveConf.ConfVars.HIVE_SERVER2_IDLE_OPERATION_TIMEOUT); } public Future getBackgroundHandle() { @@ -106,10 +111,33 @@ protected void setHasResultSet(boolean hasResultSet) { opHandle.setHasResultSet(hasResultSet); } - protected final OperationState setState(OperationState newState) throws HiveSQLException { + public boolean isTimedOut(long current) { + if (operationTimeout == 0) { + return false; + } + if (operationTimeout > 0) { + // check only when it's in terminal state + return state.isTerminal() && getLastAccessTime() + operationTimeout <= current; + } + return getLastAccessTime() + -operationTimeout <= current; + } + + public long getLastAccessTime() { + return lastAccessTime; + } + + public long getOperationTimeout() { + return operationTimeout; + } + + public void setOperationTimeout(long operationTimeout) { + this.operationTimeout = operationTimeout; + } + + protected final void setState(OperationState newState) throws HiveSQLException { state.validateTransition(newState); this.state = newState; - return this.state; + this.lastAccessTime = System.currentTimeMillis(); } protected void setOperationException(HiveSQLException operationException) { @@ -120,6 +148,7 @@ protected final void assertState(OperationState state) throws HiveSQLException { if (this.state != state) { throw new HiveSQLException("Expected state " + state + ", but found " + this.state); } + this.lastAccessTime = System.currentTimeMillis(); } public boolean isRunning() { diff --git service/src/java/org/apache/hive/service/cli/operation/OperationManager.java service/src/java/org/apache/hive/service/cli/operation/OperationManager.java index 21c33bc..b7f664a 100644 --- service/src/java/org/apache/hive/service/cli/operation/OperationManager.java +++ service/src/java/org/apache/hive/service/cli/operation/OperationManager.java @@ -18,6 +18,7 @@ package org.apache.hive.service.cli.operation; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -129,15 +130,18 @@ public GetFunctionsOperation newGetFunctionsOperation(HiveSession parentSession, return operation; } - public synchronized Operation getOperation(OperationHandle operationHandle) - throws HiveSQLException { - Operation operation = handleToOperation.get(operationHandle); + public Operation getOperation(OperationHandle operationHandle) throws HiveSQLException { + Operation operation = _getOperation(operationHandle); if (operation == null) { throw new HiveSQLException("Invalid OperationHandle: " + operationHandle); } return operation; } + private synchronized Operation _getOperation(OperationHandle operationHandle) { + return handleToOperation.get(operationHandle); + } + private synchronized void addOperation(Operation operation) { handleToOperation.put(operation.getHandle(), operation); } @@ -191,4 +195,27 @@ public RowSet getOperationNextRowSet(OperationHandle opHandle, throws HiveSQLException { return getOperation(opHandle).getNextRowSet(orientation, maxRows); } + + public List closeExpiredOperations(OperationHandle[] handles) { + List removed = new ArrayList(); + long current = System.currentTimeMillis(); + for (OperationHandle handle : handles) { + Operation operation = _getOperation(handle); + if (operation == null) { + LOG.warn("Invalid Operation " + handle); + removed.add(handle); + continue; + } + if (operation.isTimedOut(current)) { + LOG.warn("Operation " + handle + " is Timed-out and will be closed"); + try { + closeOperation(operation.getHandle()); + } catch (Exception e) { + LOG.warn("Exception is thrown closing operation " + operation.getHandle(), e); + } + removed.add(handle); + } + } + return removed; + } } diff --git service/src/java/org/apache/hive/service/cli/session/HiveSession.java service/src/java/org/apache/hive/service/cli/session/HiveSession.java index 9785e95..cc93e49 100644 --- service/src/java/org/apache/hive/service/cli/session/HiveSession.java +++ service/src/java/org/apache/hive/service/cli/session/HiveSession.java @@ -157,4 +157,6 @@ public void cancelDelegationToken(HiveAuthFactory authFactory, String tokenStr) public void renewDelegationToken(HiveAuthFactory authFactory, String tokenStr) throws HiveSQLException; + + public void closeExpiredOperations(); } diff --git service/src/java/org/apache/hive/service/cli/session/HiveSessionBase.java service/src/java/org/apache/hive/service/cli/session/HiveSessionBase.java index eee1cc6..409a4ca 100644 --- service/src/java/org/apache/hive/service/cli/session/HiveSessionBase.java +++ service/src/java/org/apache/hive/service/cli/session/HiveSessionBase.java @@ -74,4 +74,6 @@ public String getIpAddress(); public void setIpAddress(String ipAddress); + + public long getLastAccessTime(); } diff --git service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java index bc0a02c..16f84f0 100644 --- service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -87,6 +87,8 @@ private IMetaStoreClient metastoreClient = null; private final Set opHandleSet = new HashSet(); + private long lastAccessTime; + public HiveSessionImpl(TProtocolVersion protocol, String username, String password, HiveConf serverhiveConf, String ipAddress) { this.username = username; @@ -106,6 +108,8 @@ public HiveSessionImpl(TProtocolVersion protocol, String username, String passwo sessionState = new SessionState(hiveConf, username); sessionState.setUserIpAddress(ipAddress); sessionState.setIsHiveServerQuery(true); + + lastAccessTime = System.currentTimeMillis(); SessionState.start(sessionState); } @@ -214,11 +218,13 @@ protected synchronized void acquire() throws HiveSQLException { // need to make sure that the this connections session state is // stored in the thread local for sessions. SessionState.setCurrentSessionState(sessionState); + lastAccessTime = System.currentTimeMillis(); } protected synchronized void release() { assert sessionState != null; SessionState.detachSession(); + lastAccessTime = System.currentTimeMillis(); } @Override @@ -507,6 +513,19 @@ public void setUserName(String userName) { this.username = userName; } + public long getLastAccessTime() { + return lastAccessTime; + } + + @Override + public void closeExpiredOperations() { + OperationHandle[] handles = opHandleSet.toArray(new OperationHandle[0]); + if (handles.length > 0) { + OperationManager manager = sessionManager.getOperationManager(); + opHandleSet.removeAll(manager.closeExpiredOperations(handles)); + } + } + @Override public void cancelOperation(OperationHandle opHandle) throws HiveSQLException { acquire(); diff --git service/src/java/org/apache/hive/service/cli/session/SessionManager.java service/src/java/org/apache/hive/service/cli/session/SessionManager.java index d573592..dc76f9f 100644 --- service/src/java/org/apache/hive/service/cli/session/SessionManager.java +++ service/src/java/org/apache/hive/service/cli/session/SessionManager.java @@ -18,6 +18,8 @@ package org.apache.hive.service.cli.session; +import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -45,6 +47,8 @@ */ public class SessionManager extends CompositeService { + private static final int SESSION_CHECK_INTERVAL = 60000; // 1 min + private static final Log LOG = LogFactory.getLog(CompositeService.class); public static final String HIVERCFILE = ".hiverc"; private HiveConf hiveConf; @@ -53,6 +57,12 @@ private final OperationManager operationManager = new OperationManager(); private ThreadPoolExecutor backgroundOperationPool; + private Thread timeoutChecker; + private long checkInterval; + private long sessionTimeout; + + private volatile boolean shutdown; + public SessionManager() { super("SessionManager"); } @@ -78,6 +88,8 @@ public synchronized void init(HiveConf hiveConf) { backgroundOperationPool = new ThreadPoolExecutor(backgroundPoolSize, backgroundPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue(backgroundPoolQueueSize)); backgroundOperationPool.allowCoreThreadTimeOut(true); + checkInterval = HiveConf.getTimeInMsec(hiveConf, ConfVars.HIVE_SERVER2_SESSION_CHECK_INTERVAL); + sessionTimeout = HiveConf.getTimeInMsec(hiveConf, ConfVars.HIVE_SERVER2_IDLE_SESSION_TIMEOUT); addService(operationManager); super.init(hiveConf); } @@ -94,6 +106,42 @@ private void applyAuthorizationConfigPolicy(HiveConf newHiveConf) throws HiveExc @Override public synchronized void start() { super.start(); + if (checkInterval <= 0) { + return; + } + final long interval = Math.max(checkInterval, TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS)); + timeoutChecker = new Thread(new Runnable() { + public void run() { + for (sleepInterval(interval); !shutdown; sleepInterval(interval)) { + long current = System.currentTimeMillis(); + for (HiveSession session : new ArrayList(handleToSession.values())) { + if (sessionTimeout > 0 && session.getLastAccessTime() + sessionTimeout <= current) { + SessionHandle handle = session.getSessionHandle(); + LOG.warn("Session " + handle + " is Timed-out (last access : " + + new Date(session.getLastAccessTime()) + ") and will be closed"); + try { + closeSession(handle); + } catch (HiveSQLException e) { + LOG.warn("Exception is thrown closing session " + handle, e); + } + } else { + session.closeExpiredOperations(); + } + } + } + } + + private void sleepInterval(long interval) { + try { + Thread.sleep(interval); + } catch (InterruptedException e) { + // ignore + } + } + }); + timeoutChecker.setName("Session Checker [" + interval + " msec]"); + timeoutChecker.setDaemon(true); + timeoutChecker.start(); } @Override @@ -109,6 +157,10 @@ public synchronized void stop() { " seconds has been exceeded. RUNNING background operations will be shut down", e); } } + shutdown = true; + if (timeoutChecker != null) { + timeoutChecker.interrupt(); + } } public SessionHandle openSession(TProtocolVersion protocol, String username, String password, String ipAddress,