Repository: hive Updated Branches: refs/heads/master 4d8b6c28b -> 0ec6e8893
HIVE-12777 : Add api to create session with handle to restore session in CLIService (Rajat Khandelwal, reviewed by amareshwari) Project: http://git-wip-us.apache.org/repos/asf/hive/repo Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/0ec6e889 Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/0ec6e889 Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/0ec6e889 Branch: refs/heads/master Commit: 0ec6e889397f8155970753dd0cbcd88d64075120 Parents: 4d8b6c2 Author: Rajat Khandelwal <pro...@apache.org> Authored: Mon Feb 1 10:27:01 2016 +0530 Committer: Amareshwari Sriramadasu <amareshw...@apache.org> Committed: Mon Feb 1 10:27:01 2016 +0530 ---------------------------------------------------------------------- .../org/apache/hive/service/cli/CLIService.java | 11 +++++ .../apache/hive/service/cli/SessionHandle.java | 5 +- .../service/cli/session/HiveSessionImpl.java | 16 ++++-- .../cli/session/HiveSessionImplwithUGI.java | 11 +++-- .../service/cli/session/SessionManager.java | 36 +++++++++----- .../hive/service/cli/CLIServiceRestoreTest.java | 52 ++++++++++++++++++++ 6 files changed, 110 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hive/blob/0ec6e889/service/src/java/org/apache/hive/service/cli/CLIService.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/service/cli/CLIService.java b/service/src/java/org/apache/hive/service/cli/CLIService.java index 0d5ae1e..cc6e138 100644 --- a/service/src/java/org/apache/hive/service/cli/CLIService.java +++ b/service/src/java/org/apache/hive/service/cli/CLIService.java @@ -201,6 +201,17 @@ public class CLIService extends CompositeService implements ICLIService { return sessionHandle; } + /** + * Used to restore session + */ + public void createSessionWithSessionHandle(SessionHandle sessionHandle, String username, String password, + Map<String, String> configuration) + throws HiveSQLException { + sessionManager.createSession(sessionHandle, SERVER_VERSION, username, password, null, + configuration, false, null); + LOG.debug(sessionHandle + ": createSessionWithSessionHandle()"); + } + /* (non-Javadoc) * @see org.apache.hive.service.cli.ICLIService#openSession(java.lang.String, java.lang.String, java.util.Map) */ http://git-wip-us.apache.org/repos/asf/hive/blob/0ec6e889/service/src/java/org/apache/hive/service/cli/SessionHandle.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/service/cli/SessionHandle.java b/service/src/java/org/apache/hive/service/cli/SessionHandle.java index 4e4f021..9fc30c6 100644 --- a/service/src/java/org/apache/hive/service/cli/SessionHandle.java +++ b/service/src/java/org/apache/hive/service/cli/SessionHandle.java @@ -45,7 +45,10 @@ public class SessionHandle extends Handle { super(tSessionHandle.getSessionId()); this.protocol = protocol; } - + public SessionHandle(HandleIdentifier handleId, TProtocolVersion protocol) { + super(handleId); + this.protocol = protocol; + } public UUID getSessionId() { return getHandleIdentifier().getPublicId(); } http://git-wip-us.apache.org/repos/asf/hive/blob/0ec6e889/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java index 3c5700b..6aee80c 100644 --- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -111,15 +111,15 @@ public class HiveSessionImpl implements HiveSession { private volatile long lastAccessTime; private volatile long lastIdleTime; - public HiveSessionImpl(TProtocolVersion protocol, String username, String password, - HiveConf serverhiveConf, String ipAddress) { + + public HiveSessionImpl(SessionHandle sessionHandle, TProtocolVersion protocol, String username, String password, + HiveConf serverhiveConf, String ipAddress) { this.username = username; this.password = password; creationTime = System.currentTimeMillis(); - this.sessionHandle = new SessionHandle(protocol); + this.sessionHandle = sessionHandle != null ? sessionHandle : new SessionHandle(protocol); this.hiveConf = new HiveConf(serverhiveConf); this.ipAddress = ipAddress; - try { // In non-impersonation mode, map scheduler queue to current user // if fair scheduler is configured. @@ -132,13 +132,19 @@ public class HiveSessionImpl implements HiveSession { } // Set an explicit session name to control the download directory name hiveConf.set(ConfVars.HIVESESSIONID.varname, - sessionHandle.getHandleIdentifier().toString()); + this.sessionHandle.getHandleIdentifier().toString()); // Use thrift transportable formatter hiveConf.set(ListSinkOperator.OUTPUT_FORMATTER, FetchFormatter.ThriftFormatter.class.getName()); hiveConf.setInt(ListSinkOperator.OUTPUT_PROTOCOL, protocol.getValue()); } + public HiveSessionImpl(TProtocolVersion protocol, String username, String password, + HiveConf serverhiveConf, String ipAddress) { + this(null, protocol, username, password, serverhiveConf, ipAddress); + } + + @Override /** * Opens a new HiveServer2 session for the client connection. http://git-wip-us.apache.org/repos/asf/hive/blob/0ec6e889/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java index c56d97a..ab4d12f 100644 --- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java +++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java @@ -30,6 +30,7 @@ import org.apache.hadoop.hive.shims.Utils; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hive.service.auth.HiveAuthFactory; import org.apache.hive.service.cli.HiveSQLException; +import org.apache.hive.service.cli.SessionHandle; import org.apache.hive.service.rpc.thrift.TProtocolVersion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,9 +50,13 @@ public class HiveSessionImplwithUGI extends HiveSessionImpl { private HiveSession proxySession = null; - public HiveSessionImplwithUGI(TProtocolVersion protocol, String username, String password, - HiveConf hiveConf, String ipAddress, String delegationToken) throws HiveSQLException { - super(protocol, username, password, hiveConf, ipAddress); + public HiveSessionImplwithUGI(TProtocolVersion protocol, String username, + String password, HiveConf hiveConf, String ipAddress, String delegationToken) throws HiveSQLException { + this(null, protocol, username, password, hiveConf, ipAddress, delegationToken); + } + public HiveSessionImplwithUGI(SessionHandle sessionHandle, TProtocolVersion protocol, String username, + String password, HiveConf hiveConf, String ipAddress, String delegationToken) throws HiveSQLException { + super(sessionHandle, protocol, username, password, hiveConf, ipAddress); setSessionUGI(username); setDelegationToken(delegationToken); } http://git-wip-us.apache.org/repos/asf/hive/blob/0ec6e889/service/src/java/org/apache/hive/service/cli/session/SessionManager.java ---------------------------------------------------------------------- diff --git a/service/src/java/org/apache/hive/service/cli/session/SessionManager.java b/service/src/java/org/apache/hive/service/cli/session/SessionManager.java index de18f1d..79eddd9 100644 --- a/service/src/java/org/apache/hive/service/cli/session/SessionManager.java +++ b/service/src/java/org/apache/hive/service/cli/session/SessionManager.java @@ -47,6 +47,7 @@ import org.apache.hive.service.cli.HiveSQLException; import org.apache.hive.service.cli.SessionHandle; import org.apache.hive.service.cli.operation.Operation; import org.apache.hive.service.cli.operation.OperationManager; +import org.apache.hive.service.rpc.thrift.TOpenSessionReq; import org.apache.hive.service.rpc.thrift.TProtocolVersion; import org.apache.hive.service.server.HiveServer2; import org.apache.hive.service.server.ThreadFactoryWithGarbageCleanup; @@ -260,7 +261,7 @@ public class SessionManager extends CompositeService { * The username passed to this method is the effective username. * If withImpersonation is true (==doAs true) we wrap all the calls in HiveSession * within a UGI.doAs, where UGI corresponds to the effective user. - * @see org.apache.hive.service.cli.thrift.ThriftCLIService#getUserName() + * @see org.apache.hive.service.cli.thrift.ThriftCLIService#getUserName(TOpenSessionReq) * * @param protocol * @param username @@ -275,20 +276,29 @@ public class SessionManager extends CompositeService { public SessionHandle openSession(TProtocolVersion protocol, String username, String password, String ipAddress, Map<String, String> sessionConf, boolean withImpersonation, String delegationToken) throws HiveSQLException { + return createSession(null, protocol, username, password, ipAddress, sessionConf, + withImpersonation, delegationToken).getSessionHandle(); + } + public HiveSession createSession(SessionHandle sessionHandle, TProtocolVersion protocol, String username, + String password, String ipAddress, Map<String, String> sessionConf, boolean withImpersonation, + String delegationToken) + throws HiveSQLException { + HiveSession session; // If doAs is set to true for HiveServer2, we will create a proxy object for the session impl. // Within the proxy object, we wrap the method call in a UserGroupInformation#doAs if (withImpersonation) { HiveSessionImplwithUGI hiveSessionUgi; if (sessionImplWithUGIclassName == null) { - hiveSessionUgi = new HiveSessionImplwithUGI(protocol, username, password, + hiveSessionUgi = new HiveSessionImplwithUGI(sessionHandle, protocol, username, password, hiveConf, ipAddress, delegationToken); } else { try { Class<?> clazz = Class.forName(sessionImplWithUGIclassName); - Constructor<?> constructor = clazz.getConstructor(String.class, String.class, Map.class, String.class); - hiveSessionUgi = (HiveSessionImplwithUGI) constructor.newInstance(new Object[] - {protocol, username, password, hiveConf, ipAddress, delegationToken}); + Constructor<?> constructor = clazz.getConstructor(SessionHandle.class, TProtocolVersion.class, String.class, + String.class, HiveConf.class, String.class, String.class); + hiveSessionUgi = (HiveSessionImplwithUGI) constructor.newInstance(sessionHandle, + protocol, username, password, hiveConf, ipAddress, delegationToken); } catch (Exception e) { throw new HiveSQLException("Cannot initilize session class:" + sessionImplWithUGIclassName); } @@ -297,15 +307,17 @@ public class SessionManager extends CompositeService { hiveSessionUgi.setProxySession(session); } else { if (sessionImplclassName == null) { - session = new HiveSessionImpl(protocol, username, password, hiveConf, ipAddress); + session = new HiveSessionImpl(sessionHandle, protocol, username, password, hiveConf, + ipAddress); } else { try { - Class<?> clazz = Class.forName(sessionImplclassName); - Constructor<?> constructor = clazz.getConstructor(String.class, String.class, Map.class); - session = (HiveSession) constructor.newInstance(new Object[] - {protocol, username, password, hiveConf, ipAddress}); + Class<?> clazz = Class.forName(sessionImplclassName); + Constructor<?> constructor = clazz.getConstructor(SessionHandle.class, TProtocolVersion.class, + String.class, String.class, HiveConf.class, String.class); + session = (HiveSession) constructor.newInstance(sessionHandle, protocol, username, password, + hiveConf, ipAddress); } catch (Exception e) { - throw new HiveSQLException("Cannot initilize session class:" + sessionImplclassName); + throw new HiveSQLException("Cannot initilize session class:" + sessionImplclassName, e); } } } @@ -339,7 +351,7 @@ public class SessionManager extends CompositeService { throw new HiveSQLException("Failed to execute session hooks: " + e.getMessage(), e); } handleToSession.put(session.getSessionHandle(), session); - return session.getSessionHandle(); + return session; } public void closeSession(SessionHandle sessionHandle) throws HiveSQLException { http://git-wip-us.apache.org/repos/asf/hive/blob/0ec6e889/service/src/test/org/apache/hive/service/cli/CLIServiceRestoreTest.java ---------------------------------------------------------------------- diff --git a/service/src/test/org/apache/hive/service/cli/CLIServiceRestoreTest.java b/service/src/test/org/apache/hive/service/cli/CLIServiceRestoreTest.java new file mode 100644 index 0000000..af0cc40 --- /dev/null +++ b/service/src/test/org/apache/hive/service/cli/CLIServiceRestoreTest.java @@ -0,0 +1,52 @@ +/** + * 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.hive.service.cli; + + +import org.apache.hadoop.hive.conf.HiveConf; + +import org.junit.Assert; +import org.junit.Test; + +public class CLIServiceRestoreTest { + + CLIService service = getService(); + + @Test + public void testRestore() throws HiveSQLException { + SessionHandle session = service.openSession("foo", "bar", null); + service.stop(); + service = getService(); + try { + service.getSessionManager().getSession(session); + Assert.fail("session already exists before restore"); + } catch (HiveSQLException e) { + Assert.assertTrue(e.getMessage().contains("Invalid SessionHandle")); + } + service.createSessionWithSessionHandle(session, "foo", "bar", null); + Assert.assertNotNull(service.getSessionManager().getSession(session)); + service.stop(); + } + + public CLIService getService() { + CLIService service = new CLIService(null); + service.init(new HiveConf()); + service.start(); + return service; + } +}