forked from apache/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestHs2Hooks.java
231 lines (210 loc) · 7.99 KB
/
TestHs2Hooks.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* 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.
*/
//The tests here are heavily based on some timing, so there is some chance to fail.
package org.apache.hadoop.hive.hooks;
import java.io.Serializable;
import java.lang.Override;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import junit.framework.Assert;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
import org.apache.hadoop.hive.ql.hooks.ExecuteWithHookContext;
import org.apache.hadoop.hive.ql.hooks.HookContext;
import org.apache.hadoop.hive.ql.hooks.HookContext.HookType;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHook;
import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHookContext;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hive.jdbc.HiveConnection;
import org.apache.hive.service.server.HiveServer2;
import org.junit.AfterClass;
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.
*/
public class TestHs2Hooks {
private static final Logger LOG = LoggerFactory.getLogger(TestHs2Hooks.class);
private static HiveServer2 hiveServer2;
public static class PostExecHook implements ExecuteWithHookContext {
public static String userName;
public static String ipAddress;
public static String operation;
public static Throwable error;
@Override
public void run(HookContext hookContext) {
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;
public static String ipAddress;
public static String operation;
public static Throwable error;
@Override
public void run(HookContext hookContext) {
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;
}
}
}
public static class SemanticAnalysisHook implements HiveSemanticAnalyzerHook {
public static String userName;
public static String command;
public static String ipAddress;
public static Throwable preAnalyzeError;
public static Throwable postAnalyzeError;
@Override
public ASTNode preAnalyze(HiveSemanticAnalyzerHookContext context,
ASTNode ast) throws SemanticException {
try {
userName = context.getUserName();
ipAddress = context.getIpAddress();
command = context.getCommand();
} catch (Throwable t) {
LOG.error("Error in semantic analysis hook preAnalyze: " + t, t);
preAnalyzeError = t;
}
return ast;
}
@Override
public void postAnalyze(HiveSemanticAnalyzerHookContext context,
List<Task<? extends Serializable>> rootTasks) throws SemanticException {
try {
userName = context.getUserName();
ipAddress = context.getIpAddress();
command = context.getCommand();
} catch (Throwable t) {
LOG.error("Error in semantic analysis hook postAnalyze: " + t, t);
postAnalyzeError = t;
}
}
}
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
HiveConf hiveConf = new HiveConf();
hiveConf.setVar(ConfVars.PREEXECHOOKS,
PreExecHook.class.getName());
hiveConf.setVar(ConfVars.POSTEXECHOOKS,
PostExecHook.class.getName());
hiveConf.setVar(ConfVars.SEMANTIC_ANALYZER_HOOK,
SemanticAnalysisHook.class.getName());
hiveConf.setBoolVar(ConfVars.HIVE_SUPPORT_CONCURRENCY, false);
hiveServer2 = new HiveServer2();
hiveServer2.init(hiveConf);
hiveServer2.start();
Thread.sleep(2000);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
if (hiveServer2 != null) {
hiveServer2.stop();
}
}
@Before
public void setUpTest() throws Exception {
PreExecHook.userName = null;
PreExecHook.ipAddress = null;
PreExecHook.operation = null;
PreExecHook.error = null;
PostExecHook.userName = null;
PostExecHook.ipAddress = null;
PostExecHook.operation = null;
PostExecHook.error = null;
SemanticAnalysisHook.userName = null;
SemanticAnalysisHook.ipAddress = null;
SemanticAnalysisHook.command = null;
SemanticAnalysisHook.preAnalyzeError = null;
SemanticAnalysisHook.postAnalyzeError = null;
}
/**
* Test that hook context properties are correctly set.
*/
@Test
public void testHookContexts() 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);
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, "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, "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);
error = SemanticAnalysisHook.preAnalyzeError;
if (error != null) {
throw error;
}
error = SemanticAnalysisHook.postAnalyzeError;
if (error != null) {
throw error;
}
Assert.assertNotNull(SemanticAnalysisHook.ipAddress,
"semantic hook context ipaddress is null");
Assert.assertNotNull(SemanticAnalysisHook.userName,
"semantic hook context userName is null");
Assert.assertNotNull(SemanticAnalysisHook.command ,
"semantic hook context command is null");
Assert.assertTrue(SemanticAnalysisHook.ipAddress,
SemanticAnalysisHook.ipAddress.contains("127.0.0.1"));
Assert.assertEquals("show tables", SemanticAnalysisHook.command);
}
}