diff --git a/itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/TestHs2HooksWithMiniKdc.java b/itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/TestHs2HooksWithMiniKdc.java index 99026b0..e11466d 100644 --- a/itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/TestHs2HooksWithMiniKdc.java +++ b/itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/TestHs2HooksWithMiniKdc.java @@ -37,35 +37,51 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Tests information retrieved from hooks, in Kerberos mode. */ public class TestHs2HooksWithMiniKdc { + private static final Logger LOG = LoggerFactory.getLogger(TestHs2HooksWithMiniKdc.class); + public static class PostExecHook implements ExecuteWithHookContext { - public static String userName = null; - public static String ipAddress = null; + private static String userName; + private static String ipAddress; + private static String operation; + private static Throwable error; public void run(HookContext hookContext) { - if (hookContext.getHookType().equals(HookType.POST_EXEC_HOOK)) { - Assert.assertNotNull(hookContext.getIpAddress(), "IP Address is null"); - ipAddress = hookContext.getIpAddress(); - Assert.assertNotNull(hookContext.getUserName(), "Username is null"); - userName = hookContext.getUserName(); + try { + if (hookContext.getHookType().equals(HookType.POST_EXEC_HOOK)) { + ipAddress = hookContext.getIpAddress(); + userName = hookContext.getUserName(); + operation = hookContext.getOperationName(); + } + } catch (Throwable t) { + LOG.error("Error in PostExecHook: " + t, t); + error = t; } } } public static class PreExecHook implements ExecuteWithHookContext { - public static String userName = null; - public static String ipAddress = null; + private static String userName; + private static String ipAddress; + private static String operation; + private static Throwable error; public void run(HookContext hookContext) { - if (hookContext.getHookType().equals(HookType.PRE_EXEC_HOOK)) { - Assert.assertNotNull(hookContext.getIpAddress(), "IP Address is null"); - ipAddress = hookContext.getIpAddress(); - Assert.assertNotNull(hookContext.getUserName(), "Username is null"); - userName = hookContext.getUserName(); + try { + if (hookContext.getHookType().equals(HookType.PRE_EXEC_HOOK)) { + ipAddress = hookContext.getIpAddress(); + userName = hookContext.getUserName(); + operation = hookContext.getOperationName(); + } + } catch (Throwable t) { + LOG.error("Error in PreExecHook: " + t, t); + error = t; } } } @@ -108,22 +124,36 @@ public static void afterTest() throws Exception { /** * Test get IpAddress and username from hook. - * @throws Exception */ @Test - public void testIpUserName() throws Exception { + public void testIpUserName() throws Throwable { miniHiveKdc.loginUser(MiniHiveKdc.HIVE_TEST_USER_1); hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL()); Statement stmt = hs2Conn.createStatement(); + stmt.executeQuery("show databases"); stmt.executeQuery("show tables"); + Throwable error = PostExecHook.error; + if (error != null) { + throw error; + } + error = PreExecHook.error; + if (error != null) { + throw error; + } + Assert.assertNotNull(PostExecHook.ipAddress, "ipaddress is null"); + Assert.assertNotNull(PostExecHook.userName, "userName is null"); + Assert.assertNotNull(PostExecHook.operation , "operation is null"); Assert.assertEquals(MiniHiveKdc.HIVE_TEST_USER_1, PostExecHook.userName); - Assert.assertNotNull(PostExecHook.ipAddress); - Assert.assertTrue(PostExecHook.ipAddress.contains("127.0.0.1")); + Assert.assertTrue(PostExecHook.ipAddress, PostExecHook.ipAddress.contains("127.0.0.1")); + Assert.assertEquals("SHOWTABLES", PostExecHook.operation); + Assert.assertNotNull(PreExecHook.ipAddress, "ipaddress is null"); + Assert.assertNotNull(PreExecHook.userName, "userName is null"); + Assert.assertNotNull(PreExecHook.operation , "operation is null"); Assert.assertEquals(MiniHiveKdc.HIVE_TEST_USER_1, PreExecHook.userName); - Assert.assertNotNull(PreExecHook.ipAddress); - Assert.assertTrue(PreExecHook.ipAddress.contains("127.0.0.1")); + Assert.assertTrue(PreExecHook.ipAddress, PreExecHook.ipAddress.contains("127.0.0.1")); + Assert.assertEquals("SHOWTABLES", PreExecHook.operation); } } diff --git a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/hooks/TestHs2Hooks.java b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/hooks/TestHs2Hooks.java index 49b9994..0676758 100644 --- a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/hooks/TestHs2Hooks.java +++ b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/hooks/TestHs2Hooks.java @@ -20,6 +20,7 @@ package org.apache.hadoop.hive.hooks; import java.util.Properties; +import java.sql.Statement; import junit.framework.Assert; @@ -32,38 +33,52 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Tests information retrieved from hooks. */ public class TestHs2Hooks { - + private static final Logger LOG = LoggerFactory.getLogger(TestHs2Hooks.class); private static HiveServer2 hiveServer2; - public static class PreExecHook implements ExecuteWithHookContext { - public static String userName = null; - public static String ipAddress = null; + public static class PostExecHook implements ExecuteWithHookContext { + private static String userName; + private static String ipAddress; + private static String operation; + private static Throwable error; public void run(HookContext hookContext) { - if (hookContext.getHookType().equals(HookType.PRE_EXEC_HOOK)) { - Assert.assertNotNull(hookContext.getIpAddress(), "IP Address is null"); - ipAddress = hookContext.getIpAddress(); - Assert.assertNotNull(hookContext.getUserName(), "Username is null"); - userName = hookContext.getUserName(); + try { + if (hookContext.getHookType().equals(HookType.POST_EXEC_HOOK)) { + ipAddress = hookContext.getIpAddress(); + userName = hookContext.getUserName(); + operation = hookContext.getOperationName(); + } + } catch (Throwable t) { + LOG.error("Error in PostExecHook: " + t, t); + error = t; } } } - public static class PostExecHook implements ExecuteWithHookContext { - public static String userName = null; - public static String ipAddress = null; + public static class PreExecHook implements ExecuteWithHookContext { + private static String userName; + private static String ipAddress; + private static String operation; + private static Throwable error; public void run(HookContext hookContext) { - if (hookContext.getHookType().equals(HookType.POST_EXEC_HOOK)) { - Assert.assertNotNull(hookContext.getIpAddress(), "IP Address is null"); - ipAddress = hookContext.getIpAddress(); - Assert.assertNotNull(hookContext.getUserName(), "Username is null"); - userName = hookContext.getUserName(); + try { + if (hookContext.getHookType().equals(HookType.PRE_EXEC_HOOK)) { + ipAddress = hookContext.getIpAddress(); + userName = hookContext.getUserName(); + operation = hookContext.getOperationName(); + } + } catch (Throwable t) { + LOG.error("Error in PreExecHook: " + t, t); + error = t; } } } @@ -94,26 +109,39 @@ public static void tearDownAfterClass() throws Exception { /** * Test get IpAddress and username from hook. - * @throws Exception */ @Test - public void testIpUserName() throws Exception { + public void testIpUserName() throws Throwable { Properties connProp = new Properties(); connProp.setProperty("user", System.getProperty("user.name")); connProp.setProperty("password", ""); HiveConnection connection = new HiveConnection("jdbc:hive2://localhost:10000/default", connProp); - connection.createStatement().execute("show tables"); + Statement stmt = connection.createStatement(); + stmt.executeQuery("show databases"); + stmt.executeQuery("show tables"); + Throwable error = PostExecHook.error; + if (error != null) { + throw error; + } + error = PreExecHook.error; + if (error != null) { + throw error; + } Assert.assertEquals(System.getProperty("user.name"), PostExecHook.userName); - Assert.assertNotNull(PostExecHook.ipAddress); - Assert.assertTrue(PostExecHook.ipAddress.contains("127.0.0.1")); + Assert.assertNotNull(PostExecHook.ipAddress, "ipaddress is null"); + Assert.assertNotNull(PostExecHook.userName, "userName is null"); + Assert.assertNotNull(PostExecHook.operation , "operation is null"); + Assert.assertTrue(PostExecHook.ipAddress, PostExecHook.ipAddress.contains("127.0.0.1")); + Assert.assertEquals("SHOWTABLES", PostExecHook.operation); Assert.assertEquals(System.getProperty("user.name"), PreExecHook.userName); - Assert.assertNotNull(PreExecHook.ipAddress); - Assert.assertTrue(PreExecHook.ipAddress.contains("127.0.0.1")); - - connection.close(); + Assert.assertNotNull(PreExecHook.ipAddress, "ipaddress is null"); + Assert.assertNotNull(PreExecHook.userName, "userName is null"); + Assert.assertNotNull(PreExecHook.operation , "operation is null"); + Assert.assertTrue(PreExecHook.ipAddress, PreExecHook.ipAddress.contains("127.0.0.1")); + Assert.assertEquals("SHOWTABLES", PreExecHook.operation); } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/Driver.java b/ql/src/java/org/apache/hadoop/hive/ql/Driver.java index 5b36f71..a105eca 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/Driver.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/Driver.java @@ -430,7 +430,8 @@ public int compile(String command, boolean resetTaskIds) { sem.validate(); perfLogger.PerfLogEnd(CLASS_NAME, PerfLogger.ANALYZE); - plan = new QueryPlan(command, sem, perfLogger.getStartTime(PerfLogger.DRIVER_RUN), queryId); + plan = new QueryPlan(command, sem, perfLogger.getStartTime(PerfLogger.DRIVER_RUN), queryId, + SessionState.get().getCommandType()); String queryStr = plan.getQueryStr(); conf.setVar(HiveConf.ConfVars.HIVEQUERYSTRING, queryStr); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java b/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java index 85d599a..8e1e6e2 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java @@ -104,16 +104,14 @@ private QueryProperties queryProperties; private transient Long queryStartTime; + private String operationName; public QueryPlan() { this.reducerTimeStatsPerJobList = new ArrayList(); } - public QueryPlan(String queryString, BaseSemanticAnalyzer sem, Long startTime) { - this(queryString, sem, startTime, null); - } - - public QueryPlan(String queryString, BaseSemanticAnalyzer sem, Long startTime, String queryId) { + public QueryPlan(String queryString, BaseSemanticAnalyzer sem, Long startTime, String queryId, + String operationName) { this.queryString = queryString; rootTasks = new ArrayList>(); @@ -134,6 +132,7 @@ public QueryPlan(String queryString, BaseSemanticAnalyzer sem, Long startTime, S query.putToQueryAttributes("queryString", this.queryString); queryProperties = sem.getQueryProperties(); queryStartTime = startTime; + this.operationName = operationName; } public String getQueryStr() { @@ -786,4 +785,8 @@ public Long getQueryStartTime() { public void setQueryStartTime(Long queryStartTime) { this.queryStartTime = queryStartTime; } + + public String getOperationName() { + return operationName; + } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java b/ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java index 49c095a..260444f 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java @@ -147,7 +147,7 @@ public String getIpAddress() { } public String getOperationName() { - return SessionState.get().getHiveOperation().name(); + return queryPlan.getOperationName(); } public String getUserName() { diff --git a/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java b/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java index 01e3635..87ef193 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java @@ -283,7 +283,7 @@ private ReturnInfo parseAndAnalyze(String query, String testName) // validate the plan sem.validate(); - QueryPlan plan = new QueryPlan(query, sem, 0L, testName); + QueryPlan plan = new QueryPlan(query, sem, 0L, testName, null); return new ReturnInfo(tree, sem, plan); }