diff --git common/src/java/org/apache/hadoop/hive/conf/HiveConf.java common/src/java/org/apache/hadoop/hive/conf/HiveConf.java index 32ab3d8..70b2fcd 100644 --- common/src/java/org/apache/hadoop/hive/conf/HiveConf.java +++ common/src/java/org/apache/hadoop/hive/conf/HiveConf.java @@ -33,6 +33,7 @@ import java.util.Map.Entry; import java.util.Properties; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -789,6 +790,10 @@ HIVE_SERVER2_SSL_KEYSTORE_PATH("hive.server2.keystore.path", ""), HIVE_SERVER2_SSL_KEYSTORE_PASSWORD("hive.server2.keystore.password", ""), + HIVE_SERVER2_SESSION_CHECK_INTERVAL("hive.server2.session.check.interval", "0", new TimeValidator()), + HIVE_SERVER2_IDLE_SESSION_TIMEOUT("hive.server2.idle.session.timeout", "0", new TimeValidator()), + HIVE_SERVER2_IDLE_OPERATION_TIMEOUT("hive.server2.idle.operation.timeout", "0", new TimeValidator()), + HIVE_SECURITY_COMMAND_WHITELIST("hive.security.command.whitelist", "set,reset,dfs,add,delete,compile"), HIVE_CONF_RESTRICTED_LIST("hive.conf.restricted.list", ""), @@ -1117,6 +1122,36 @@ public static void setVar(Configuration conf, ConfVars var, String val) { conf.set(var.varname, val); } + private static final Pattern TIME_PATTERN = Pattern.compile("(-?\\d+)\\s*([a-zA-Z]*)"); + + public static long getTimeVar(Configuration conf, ConfVars var) { + return toTimeValue(getVar(conf, var)); + } + + private static long toTimeValue(String value) { + Matcher matcher = TIME_PATTERN.matcher(value); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid time value " + value); + } + long timePart = Long.valueOf(matcher.group(1)); + String unitPart = matcher.group(2); + if (unitPart == null || unitPart.isEmpty() || unitPart.equalsIgnoreCase("msec")) { + return timePart; + } + unitPart = unitPart.toLowerCase(); + if (unitPart.startsWith("s")) { + return TimeUnit.MILLISECONDS.convert(timePart, TimeUnit.SECONDS); + } else if (unitPart.startsWith("m")) { + return TimeUnit.MILLISECONDS.convert(timePart, TimeUnit.MINUTES); + } else if (unitPart.startsWith("h")) { + return TimeUnit.MILLISECONDS.convert(timePart, TimeUnit.HOURS); + } else if (unitPart.startsWith("d")) { + return TimeUnit.MILLISECONDS.convert(timePart, TimeUnit.DAYS); + } + throw new IllegalArgumentException("Invalid time value " + value); + } + + public static ConfVars getConfVars(String name) { return vars.get(name); } @@ -1359,6 +1394,19 @@ public String validate(String value) { } } + public static class TimeValidator implements Validator { + + @Override + public String validate(String value) { + try { + toTimeValue(value); + } catch (Exception e) { + return e.toString(); + } + return null; + } + } + public static class RatioValidator implements Validator { @Override public String validate(String value) { diff --git conf/hive-default.xml.template conf/hive-default.xml.template index c574ab5..66f2372 100644 --- conf/hive-default.xml.template +++ conf/hive-default.xml.template @@ -882,7 +882,33 @@ hive.server2.transport.mode binary Server transport mode. "binary" or "http". - + + + + hive.server2.session.check.interval + 0 + The check inteval for sessions, which would be disabled with zero or minus value. + Accepts a numeric value which is msec by default but also can be used with other time units appended (sec, min, hour, day) + + + + + hive.server2.idle.session.timeout + 0 + Session will be closed when it's not accessed for this duration, which can be disabled with zero or minus value. + Accepts a numeric value which is msec by default but also can be used with other time units appended (sec, min, hour, day) + + + + + hive.server2.idle.operation.timeout + 0 + Operation will be closed when it's not accessed for this duration (msec), which can be disabled with zero value. + Accepts a numeric value which is msec by default but also can be used with other time units appended (sec, min, hour, day). + With positive value, it's checked for operations in termial state only (FINISHED, CANCELED, CLOSED, ERROR). + With negative vable, it's checked for all of the operations regardless of state. + + hive.server2.thrift.http.port diff --git service/src/java/org/apache/hive/service/cli/OperationState.java service/src/java/org/apache/hive/service/cli/OperationState.java index 1ec6bd1..0f0170f 100644 --- service/src/java/org/apache/hive/service/cli/OperationState.java +++ service/src/java/org/apache/hive/service/cli/OperationState.java @@ -25,30 +25,25 @@ * */ 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; } - 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, OperationState newState) @@ -101,4 +96,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 6f4b8dc..fa26f3e 100644 --- service/src/java/org/apache/hive/service/cli/operation/Operation.java +++ service/src/java/org/apache/hive/service/cli/operation/Operation.java @@ -40,10 +40,13 @@ public static final long DEFAULT_FETCH_MAX_ROWS = 100; protected boolean hasResultSet; + private long lastAccessTime; + protected Operation(HiveSession parentSession, OperationType opType) { super(); this.parentSession = parentSession; opHandle = new OperationHandle(opType); + lastAccessTime = System.currentTimeMillis(); } public void setConfiguration(HiveConf configuration) { @@ -79,16 +82,34 @@ protected void setHasResultSet(boolean hasResultSet) { opHandle.setHasResultSet(hasResultSet); } - protected final OperationState setState(OperationState newState) throws HiveSQLException { + public boolean isTimedOut(long current) { + long timeout = HiveConf.getLongVar(configuration, + HiveConf.ConfVars.HIVE_SERVER2_IDLE_OPERATION_TIMEOUT); + if (timeout == 0) { + return false; + } + if (timeout > 0) { + // check only when in terminal state + return getState().isTerminal() && getLastAccessTime() + timeout <= current; + } + return getLastAccessTime() < current + -timeout; + } + + public long getLastAccessTime() { + return lastAccessTime; + } + + protected final void setState(OperationState newState) throws HiveSQLException { state.validateTransition(newState); this.state = newState; - return this.state; + this.lastAccessTime = System.currentTimeMillis(); } 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/session/HiveSession.java service/src/java/org/apache/hive/service/cli/session/HiveSession.java index 00058cc..4415018 100644 --- service/src/java/org/apache/hive/service/cli/session/HiveSession.java +++ service/src/java/org/apache/hive/service/cli/session/HiveSession.java @@ -179,4 +179,8 @@ public RowSet fetchResults(OperationHandle opHandle, FetchOrientation orientatio public String getUserName(); public void setUserName(String userName); + + public long getLastAccessTime(); + + public void ping(); } 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 cfda752..5691734 100644 --- service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -19,6 +19,7 @@ package org.apache.hive.service.cli.session; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -51,6 +52,7 @@ import org.apache.hive.service.cli.operation.GetTableTypesOperation; import org.apache.hive.service.cli.operation.GetTypeInfoOperation; import org.apache.hive.service.cli.operation.MetadataOperation; +import org.apache.hive.service.cli.operation.Operation; import org.apache.hive.service.cli.operation.OperationManager; /** @@ -76,6 +78,8 @@ private IMetaStoreClient metastoreClient = null; private final Set opHandleSet = new HashSet(); + private long lastAccessTime; + public HiveSessionImpl(String username, String password, Map sessionConf) { this.username = username; this.password = password; @@ -88,6 +92,8 @@ public HiveSessionImpl(String username, String password, Map ses // set an explicit session name to control the download directory name hiveConf.set(ConfVars.HIVESESSIONID.varname, sessionHandle.getHandleIdentifier().toString()); + + lastAccessTime = System.currentTimeMillis(); sessionState = new SessionState(hiveConf); SessionState.start(sessionState); } @@ -112,11 +118,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; // no need to release sessionState... + lastAccessTime = System.currentTimeMillis(); } public SessionHandle getSessionHandle() { @@ -381,6 +389,34 @@ public void setUserName(String userName) { this.username = userName; } + public long getLastAccessTime() { + return lastAccessTime; + } + + @Override + public void ping() { + long current = System.currentTimeMillis(); + OperationManager manager = sessionManager.getOperationManager(); + for (OperationHandle handle : new ArrayList(opHandleSet)) { + Operation operation = null; + try { + operation = manager.getOperation(handle); + } catch (HiveSQLException e) { + LOG.warn("Invalid Operation " + operation.getHandle()); + opHandleSet.remove(handle); + continue; + } + if (operation.isTimedOut(current)) { + LOG.warn("Operation " + operation.getHandle() + " is Timed-out and will be closed"); + try { + closeOperation(operation.getHandle()); + } catch (Exception e) { + LOG.warn("Exception is thrown closing operation " + operation.getHandle(), e); + } + } + } + } + @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 e262b72..93796b9 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; @@ -42,6 +44,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); private HiveConf hiveConf; @@ -52,6 +56,12 @@ private ThreadPoolExecutor backgroundOperationPool; + private Thread checker; + private long checkInterval; + private long sessionTimeout; + + private volatile boolean shutdown; + public SessionManager() { super("SessionManager"); } @@ -71,6 +81,8 @@ public synchronized void init(HiveConf hiveConf) { backgroundOperationPool = new ThreadPoolExecutor(backgroundPoolSize, backgroundPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue(backgroundPoolQueueSize)); backgroundOperationPool.allowCoreThreadTimeOut(true); + checkInterval = HiveConf.getTimeVar(hiveConf, ConfVars.HIVE_SERVER2_SESSION_CHECK_INTERVAL); + sessionTimeout = HiveConf.getTimeVar(hiveConf, ConfVars.HIVE_SERVER2_IDLE_SESSION_TIMEOUT); addService(operationManager); super.init(hiveConf); } @@ -78,6 +90,41 @@ public synchronized void init(HiveConf hiveConf) { @Override public synchronized void start() { super.start(); + if (checkInterval <= 0) { + return; + } + final long interval = Math.max(checkInterval, TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS)); + checker = new Thread(new Runnable() { + public void run() { + for (sleepInterval(); !shutdown; sleepInterval()) { + 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.ping(); + } + } + } + } + + private void sleepInterval() { + try { + Thread.sleep(interval); + } catch (InterruptedException e) { + // ignore + } + } + }); + checker.setDaemon(true); + checker.start(); } @Override @@ -93,6 +140,10 @@ public synchronized void stop() { " seconds has been exceeded. RUNNING background operations will be shut down", e); } } + shutdown = true; + if (checker != null) { + checker.interrupt(); + } } public SessionHandle openSession(String username, String password, Map sessionConf)