forked from greenplum-db/pxf-archive
-
Notifications
You must be signed in to change notification settings - Fork 3
/
HiveMetastoreCompatibilityTest.java
330 lines (276 loc) · 16.5 KB
/
HiveMetastoreCompatibilityTest.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package org.greenplum.pxf.plugins.hive;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStore;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClientCompatibility1xx;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.GetTableResult;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;
import org.greenplum.pxf.api.model.Metadata;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockConstructionWithAnswer;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
public class HiveMetastoreCompatibilityTest {
private HiveMetaStoreClientCompatibility1xx hiveCompatiblityClient;
private HiveClientWrapper hiveClientWrapper;
private HiveClientWrapper.HiveClientFactory hiveClientFactory;
private ThriftHiveMetastore.Client mockThriftClient;
private Map<String, String> hiveTableParameters;
@BeforeEach
@SuppressWarnings("unchecked")
public void setup() throws MetaException {
mockThriftClient = mock(ThriftHiveMetastore.Client.class);
hiveClientFactory = new HiveClientWrapper.HiveClientFactory();
hiveClientWrapper = new HiveClientWrapper();
hiveClientWrapper.setHiveClientFactory(hiveClientFactory);
hiveTableParameters = new HashMap<>();
}
@Test
public void getTableMetaException() throws Exception {
String name = "orphan";
try (MockedStatic<HiveMetaStore> hiveMetaStoreMockedStatic = mockStatic(HiveMetaStore.class)) {
hiveMetaStoreMockedStatic.when(() -> HiveMetaStore.newRetryingHMSHandler(any(String.class), any(), any(Boolean.class)))
.thenReturn(mockThriftClient);
when(mockThriftClient.get_table_req(any())).thenThrow(new MetaException("some meta failure"));
Configuration configuration = new Configuration();
hiveCompatiblityClient = new HiveMetaStoreClientCompatibility1xx(new HiveConf(configuration, HiveConf.class));
Exception e = assertThrows(MetaException.class,
() -> hiveCompatiblityClient.getTable("default", name));
assertEquals("some meta failure", e.getMessage());
}
}
@Test
public void getTableNoSuchObjectException() throws Exception {
String name = "orphan";
try (MockedStatic<HiveMetaStore> hiveMetaStoreMockedStatic = mockStatic(HiveMetaStore.class)) {
hiveMetaStoreMockedStatic.when(() -> HiveMetaStore.newRetryingHMSHandler(any(String.class), any(), any(Boolean.class)))
.thenReturn(mockThriftClient);
when(mockThriftClient.get_table_req(any())).thenThrow(new NoSuchObjectException("where's my table"));
Configuration configuration = new Configuration();
hiveCompatiblityClient = new HiveMetaStoreClientCompatibility1xx(new HiveConf(configuration, HiveConf.class));
Exception e = assertThrows(NoSuchObjectException.class,
() -> hiveCompatiblityClient.getTable("default", name));
assertEquals("where's my table", e.getMessage());
}
}
@Test
public void getTableFallback() throws Exception {
String name = "orphan";
Table hiveTable = new Table();
hiveTable.setTableName(name);
hiveTable.setTableType("MANAGED_TABLE");
try (MockedStatic<HiveMetaStore> hiveMetaStoreMockedStatic = mockStatic(HiveMetaStore.class)) {
hiveMetaStoreMockedStatic.when(() -> HiveMetaStore.newRetryingHMSHandler(any(String.class), any(), any(Boolean.class)))
.thenReturn(mockThriftClient);
when(mockThriftClient.get_table_req(any())).thenThrow(new TApplicationException("fallback"));
when(mockThriftClient.get_table("default", name)).thenReturn(hiveTable);
Configuration configuration = new Configuration();
hiveCompatiblityClient = new HiveMetaStoreClientCompatibility1xx(new HiveConf(configuration, HiveConf.class));
Table resultTable = hiveCompatiblityClient.getTable("default", name);
assertEquals(name, resultTable.getTableName());
}
}
@Test
public void getTableFailedToConnectToMetastoreFallback() throws Exception {
String name = "orphan";
try (MockedStatic<HiveMetaStore> hiveMetaStoreMockedStatic = mockStatic(HiveMetaStore.class)) {
hiveMetaStoreMockedStatic.when(() -> HiveMetaStore.newRetryingHMSHandler(any(String.class), any(), any(Boolean.class)))
.thenReturn(mockThriftClient);
when(mockThriftClient.get_table_req(any())).thenThrow(new TApplicationException("fallback"));
when(mockThriftClient.get_table("default", name)).thenThrow(new TTransportException("oops. where's the metastore?"));
Configuration configuration = new Configuration();
hiveCompatiblityClient = new HiveMetaStoreClientCompatibility1xx(new HiveConf(configuration, HiveConf.class));
Exception e = assertThrows(TTransportException.class,
() -> hiveCompatiblityClient.getTable("default", name));
assertEquals("oops. where's the metastore?", e.getMessage());
}
}
@Test
public void getTableFailedToConnectToMetastore() throws Exception {
String name = "orphan";
try (MockedStatic<HiveMetaStore> hiveMetaStoreMockedStatic = mockStatic(HiveMetaStore.class)) {
hiveMetaStoreMockedStatic.when(() -> HiveMetaStore.newRetryingHMSHandler(any(String.class), any(), any(Boolean.class)))
.thenReturn(mockThriftClient);
when(mockThriftClient.get_table_req(any())).thenThrow(new TTransportException("oops. where's the metastore?"));
Configuration configuration = new Configuration();
hiveCompatiblityClient = new HiveMetaStoreClientCompatibility1xx(new HiveConf(configuration, HiveConf.class));
Exception e = assertThrows(TTransportException.class,
() -> hiveCompatiblityClient.getTable("default", name));
assertEquals("oops. where's the metastore?", e.getMessage());
}
}
@Test
public void getTableFailedToConnectToMetastoreNoRetries() throws Exception {
String name = "orphan";
Table hiveTable = new Table();
hiveTable.setTableName(name);
hiveTable.setTableType("MANAGED_TABLE");
try (MockedStatic<HiveMetaStore> hiveMetaStoreMockedStatic = mockStatic(HiveMetaStore.class)) {
hiveMetaStoreMockedStatic.when(() -> HiveMetaStore.newRetryingHMSHandler(any(String.class), any(), any(Boolean.class)))
.thenReturn(mockThriftClient);
when(mockThriftClient.get_table_req(any())).thenThrow(new TApplicationException("fallback"));
when(mockThriftClient.get_table("default", name))
.thenThrow(new TTransportException("oops. where's the metastore? 1"))
.thenThrow(new TTransportException("oops. where's the metastore? 2"))
.thenThrow(new TTransportException("oops. where's the metastore? 3"))
.thenReturn(hiveTable);
Configuration configuration = new Configuration();
HiveConf hiveConf = new HiveConf(configuration, HiveConf.class);
hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTFAILURERETRIES, 0);
IMetaStoreClient client = hiveClientFactory.initHiveClient(hiveConf).getClient();
Exception e = assertThrows(TTransportException.class,
() -> hiveClientWrapper.getHiveTable(client, new Metadata.Item("default", name)));
assertEquals("oops. where's the metastore? 1", e.getMessage());
}
}
@Test
public void getTableFailedToConnectToMetastoreFiveFailedRetries() throws Exception {
String name = "orphan";
Table hiveTable = new Table();
hiveTable.setTableName(name);
hiveTable.setTableType("MANAGED_TABLE");
try (MockedConstruction<TSocket> tSocketMockedConstruction = mockConstruction(TSocket.class, (mockSocket, context) ->
when(mockSocket.isOpen()).thenReturn(true));
MockedConstruction<ThriftHiveMetastore.Client> thriftHiveMetastoreClientMockedConstruction = mockConstruction(ThriftHiveMetastore.Client.class,
(mockClient, context) -> {
when(mockClient.get_table_req(any())).thenThrow(new TApplicationException("fallback"));
when(mockClient.get_table("default", name))
.thenThrow(new TTransportException("oops. where's the metastore?"));
}
)) {
Configuration configuration = new Configuration();
HiveConf hiveConf = new HiveConf(configuration, HiveConf.class);
hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTFAILURERETRIES, 5);
hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "test://test:1234");
IMetaStoreClient client = hiveClientFactory.initHiveClient(hiveConf).getClient();
Exception e = assertThrows(TTransportException.class,
() -> hiveClientWrapper.getHiveTable(client, new Metadata.Item("default", name)));
assertEquals("oops. where's the metastore?", e.getMessage());
assertEquals(6, thriftHiveMetastoreClientMockedConstruction.constructed().size());
assertEquals(6, tSocketMockedConstruction.constructed().size());
}
}
@Test
public void getTableFailedToConnectToMetastoreNoFallback1Retry2ndSuccess() throws Exception {
String name = "orphan";
Table hiveTable = new Table();
hiveTable.setTableName(name);
hiveTable.setTableType("MANAGED_TABLE");
hiveTable.setParameters(hiveTableParameters);
try (MockedConstruction<TSocket> tSocketMockedConstruction = mockConstruction(TSocket.class, (mockSocket, context) ->
when(mockSocket.isOpen()).thenReturn(true));
MockedConstruction<ThriftHiveMetastore.Client> thriftHiveMetastoreClientMockedConstruction = mockConstructionWithAnswer(ThriftHiveMetastore.Client.class,
// first run through
invocation -> {
if (invocation.getMethod().getName().equals("get_table_req")) {
throw new TTransportException("oops. where's the metastore? 1");
}
return null;
},
// second run through (retry 1 = success)
invocation -> {
if (invocation.getMethod().getName().equals("get_table_req")) {
GetTableResult tempRes = new GetTableResult(hiveTable);
return tempRes;
}
return null;
}
)) {
Configuration configuration = new Configuration();
HiveConf hiveConf = new HiveConf(configuration, HiveConf.class);
hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTFAILURERETRIES, 1);
hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "test://test:1234");
IMetaStoreClient client = hiveClientFactory.initHiveClient(hiveConf).getClient();
Table resultTable = hiveClientWrapper.getHiveTable(client, new Metadata.Item("default", name));
assertEquals(hiveTable.getTableName(), resultTable.getTableName());
assertEquals(2, thriftHiveMetastoreClientMockedConstruction.constructed().size());
assertEquals(2, tSocketMockedConstruction.constructed().size());
}
}
@Test
public void getTableFailedToConnectToMetastoreFiveRetries3rdSuccess() throws Exception {
String name = "orphan";
Table hiveTable = new Table();
hiveTable.setTableName(name);
hiveTable.setTableType("MANAGED_TABLE");
hiveTable.setParameters(hiveTableParameters);
try (MockedConstruction<TSocket> tSocketMockedConstruction = mockConstruction(TSocket.class, (mockSocket, context) ->
when(mockSocket.isOpen()).thenReturn(true));
MockedConstruction<ThriftHiveMetastore.Client> thriftHiveMetastoreClientMockedConstruction = mockConstructionWithAnswer(ThriftHiveMetastore.Client.class,
// first run through
invocation -> {
if (invocation.getMethod().getName().equals("get_table_req")) {
throw new TApplicationException("fallback 1");
} else if (invocation.getMethod().getName().equals("get_table")) {
throw new TTransportException("oops. where's the metastore? 1");
}
return null;
},
// second run through (retry 1)
invocation -> {
if (invocation.getMethod().getName().equals("get_table_req")) {
throw new TApplicationException("fallback 2");
} else if (invocation.getMethod().getName().equals("get_table")) {
throw new TTransportException("oops. where's the metastore? 2");
}
return null;
},
// third run through (retry 2)
invocation -> {
if (invocation.getMethod().getName().equals("get_table_req")) {
throw new TApplicationException("fallback 3");
} else if (invocation.getMethod().getName().equals("get_table")) {
throw new TTransportException("oops. where's the metastore? 3");
}
return null;
},
// placebo run through
// the second to last invocation keeps getting skipped so place this here as a placebo
// https://github.com/mockito/mockito/pull/2545
invocation -> {
if (invocation.getMethod().getName().equals("get_table_req")) {
throw new TApplicationException("fallback ???");
} else if (invocation.getMethod().getName().equals("get_table")) {
throw new TTransportException("oops. where's the metastore? ???");
}
return null;
},
// final run through (retry 3 = success)
invocation -> {
if (invocation.getMethod().getName().equals("get_table_req")) {
throw new TApplicationException("fallback");
} else if (invocation.getMethod().getName().equals("get_table")) {
return hiveTable;
}
return null;
}
)) {
Configuration configuration = new Configuration();
HiveConf hiveConf = new HiveConf(configuration, HiveConf.class);
hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTFAILURERETRIES, 5);
hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "test://test:1234");
IMetaStoreClient client = hiveClientFactory.initHiveClient(hiveConf).getClient();
Table resultTable = hiveClientWrapper.getHiveTable(client, new Metadata.Item("default", name));
assertEquals(hiveTable.getTableName(), resultTable.getTableName());
assertEquals(4, thriftHiveMetastoreClientMockedConstruction.constructed().size());
assertEquals(4, tSocketMockedConstruction.constructed().size());
}
}
}