/
AbstractTestHiveClientS3.java
468 lines (408 loc) · 19.3 KB
/
AbstractTestHiveClientS3.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Licensed 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 com.facebook.presto.hive;
import com.facebook.presto.GroupByHashPageIndexerFactory;
import com.facebook.presto.hive.metastore.CachingHiveMetastore;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ColumnMetadata;
import com.facebook.presto.spi.ConnectorPageSink;
import com.facebook.presto.spi.ConnectorPageSinkProvider;
import com.facebook.presto.spi.ConnectorPageSource;
import com.facebook.presto.spi.ConnectorPageSourceProvider;
import com.facebook.presto.spi.ConnectorSplit;
import com.facebook.presto.spi.ConnectorSplitManager;
import com.facebook.presto.spi.ConnectorSplitSource;
import com.facebook.presto.spi.ConnectorTableHandle;
import com.facebook.presto.spi.ConnectorTableLayoutResult;
import com.facebook.presto.spi.ConnectorTableMetadata;
import com.facebook.presto.spi.Constraint;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.TableNotFoundException;
import com.facebook.presto.spi.TupleDomain;
import com.facebook.presto.testing.MaterializedResult;
import com.facebook.presto.testing.MaterializedRow;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;
import io.airlift.json.JsonCodec;
import io.airlift.slice.Slice;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.thrift.TException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import static com.facebook.presto.hadoop.HadoopFileStatus.isDirectory;
import static com.facebook.presto.hive.HiveTableProperties.PARTITIONED_BY_PROPERTY;
import static com.facebook.presto.hive.HiveTableProperties.STORAGE_FORMAT_PROPERTY;
import static com.facebook.presto.hive.HiveTestUtils.DEFAULT_HIVE_DATA_STREAM_FACTORIES;
import static com.facebook.presto.hive.HiveTestUtils.DEFAULT_HIVE_RECORD_CURSOR_PROVIDER;
import static com.facebook.presto.hive.HiveTestUtils.SESSION;
import static com.facebook.presto.hive.HiveTestUtils.TYPE_MANAGER;
import static com.facebook.presto.hive.HiveTestUtils.getTypes;
import static com.facebook.presto.hive.util.Types.checkType;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.testing.MaterializedResult.materializeSourceDataStream;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService;
import static io.airlift.concurrent.MoreFutures.getFutureValue;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.airlift.testing.Assertions.assertEqualsIgnoreOrder;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
@Test(groups = "hive-s3")
public abstract class AbstractTestHiveClientS3
{
protected String writableBucket;
protected String database;
protected SchemaTableName tableS3;
protected SchemaTableName temporaryCreateTable;
protected HdfsEnvironment hdfsEnvironment;
protected TestingHiveMetastore metastoreClient;
protected HiveMetadata metadata;
protected ConnectorSplitManager splitManager;
protected ConnectorPageSinkProvider pageSinkProvider;
protected ConnectorPageSourceProvider pageSourceProvider;
private ExecutorService executor;
@BeforeClass
public void setUp()
throws Exception
{
executor = newCachedThreadPool(daemonThreadsNamed("hive-%s"));
}
@AfterClass
public void tearDown()
throws Exception
{
if (executor != null) {
executor.shutdownNow();
executor = null;
}
}
protected void setupHive(String databaseName)
{
database = databaseName;
tableS3 = new SchemaTableName(database, "presto_test_s3");
String random = UUID.randomUUID().toString().toLowerCase(ENGLISH).replace("-", "");
temporaryCreateTable = new SchemaTableName(database, "tmp_presto_test_create_s3_" + random);
}
protected void setup(String host, int port, String databaseName, String awsAccessKey, String awsSecretKey, String writableBucket)
{
this.writableBucket = writableBucket;
setupHive(databaseName);
HiveClientConfig hiveClientConfig = new HiveClientConfig()
.setS3AwsAccessKey(awsAccessKey)
.setS3AwsSecretKey(awsSecretKey);
String proxy = System.getProperty("hive.metastore.thrift.client.socks-proxy");
if (proxy != null) {
hiveClientConfig.setMetastoreSocksProxy(HostAndPort.fromString(proxy));
}
HiveConnectorId connectorId = new HiveConnectorId("hive-test");
HiveCluster hiveCluster = new TestingHiveCluster(hiveClientConfig, host, port);
ExecutorService executor = newCachedThreadPool(daemonThreadsNamed("hive-s3-%s"));
HdfsConfiguration hdfsConfiguration = new HiveHdfsConfiguration(new HdfsConfigurationUpdater(hiveClientConfig));
HivePartitionManager hivePartitionManager = new HivePartitionManager(connectorId, hiveClientConfig);
hdfsEnvironment = new HdfsEnvironment(hdfsConfiguration, hiveClientConfig);
metastoreClient = new TestingHiveMetastore(hiveCluster, executor, hiveClientConfig, writableBucket, hdfsEnvironment);
TypeRegistry typeManager = new TypeRegistry();
JsonCodec<PartitionUpdate> partitionUpdateCodec = JsonCodec.jsonCodec(PartitionUpdate.class);
metadata = new HiveMetadata(
connectorId,
hiveClientConfig,
metastoreClient,
hdfsEnvironment,
hivePartitionManager,
newDirectExecutorService(),
typeManager,
partitionUpdateCodec);
splitManager = new HiveSplitManager(
connectorId,
hiveClientConfig,
metastoreClient,
new NamenodeStats(),
hdfsEnvironment,
new HadoopDirectoryLister(),
executor);
pageSinkProvider = new HivePageSinkProvider(hdfsEnvironment, metastoreClient, new GroupByHashPageIndexerFactory(), typeManager, new HiveClientConfig(), partitionUpdateCodec);
pageSourceProvider = new HivePageSourceProvider(hiveClientConfig, hdfsEnvironment, DEFAULT_HIVE_RECORD_CURSOR_PROVIDER, DEFAULT_HIVE_DATA_STREAM_FACTORIES, TYPE_MANAGER);
}
@Test
public void testGetRecordsS3()
throws Exception
{
ConnectorTableHandle table = getTableHandle(tableS3);
List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(SESSION, table).values());
Map<String, Integer> columnIndex = indexColumns(columnHandles);
List<ConnectorTableLayoutResult> tableLayoutResults = metadata.getTableLayouts(SESSION, table, new Constraint<>(TupleDomain.all(), bindings -> true), Optional.empty());
HiveTableLayoutHandle layoutHandle = (HiveTableLayoutHandle) getOnlyElement(tableLayoutResults).getTableLayout().getHandle();
assertEquals(layoutHandle.getPartitions().size(), 1);
ConnectorSplitSource splitSource = splitManager.getSplits(SESSION, layoutHandle);
long sum = 0;
for (ConnectorSplit split : getAllSplits(splitSource)) {
try (ConnectorPageSource pageSource = pageSourceProvider.createPageSource(SESSION, split, columnHandles)) {
MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, getTypes(columnHandles));
for (MaterializedRow row : result) {
sum += (Long) row.getField(columnIndex.get("t_bigint"));
}
}
}
assertEquals(sum, 78300);
}
@Test
public void testGetFileStatus()
throws Exception
{
Path basePath = new Path("s3://presto-test-hive/");
Path tablePath = new Path(basePath, "presto_test_s3");
Path filePath = new Path(tablePath, "test1.csv");
FileSystem fs = hdfsEnvironment.getFileSystem(basePath);
assertTrue(isDirectory(fs.getFileStatus(basePath)));
assertTrue(isDirectory(fs.getFileStatus(tablePath)));
assertFalse(isDirectory(fs.getFileStatus(filePath)));
assertFalse(fs.exists(new Path(basePath, "foo")));
}
@Test
public void testRename()
throws Exception
{
Path basePath = new Path(format("s3://%s/rename/%s/", writableBucket, UUID.randomUUID()));
FileSystem fs = hdfsEnvironment.getFileSystem(basePath);
assertFalse(fs.exists(basePath));
// create file foo.txt
Path path = new Path(basePath, "foo.txt");
assertTrue(fs.createNewFile(path));
assertTrue(fs.exists(path));
// rename foo.txt to bar.txt
Path newPath = new Path(basePath, "bar.txt");
assertFalse(fs.exists(newPath));
assertTrue(fs.rename(path, newPath));
assertFalse(fs.exists(path));
assertTrue(fs.exists(newPath));
// create file foo.txt and rename to bar.txt
assertTrue(fs.createNewFile(path));
assertFalse(fs.rename(path, newPath));
assertTrue(fs.exists(path));
// rename foo.txt to foo.txt
assertTrue(fs.rename(path, path));
assertTrue(fs.exists(path));
// delete foo.txt
assertTrue(fs.delete(path, false));
assertFalse(fs.exists(path));
// create directory source with file
Path source = new Path(basePath, "source");
assertTrue(fs.createNewFile(new Path(source, "test.txt")));
// rename source to non-existing target
Path target = new Path(basePath, "target");
assertFalse(fs.exists(target));
assertTrue(fs.rename(source, target));
assertFalse(fs.exists(source));
assertTrue(fs.exists(target));
// create directory source with file
assertTrue(fs.createNewFile(new Path(source, "test.txt")));
// rename source to existing target
assertTrue(fs.rename(source, target));
assertFalse(fs.exists(source));
target = new Path(target, "source");
assertTrue(fs.exists(target));
assertTrue(fs.exists(new Path(target, "test.txt")));
// delete target
target = new Path(basePath, "target");
assertTrue(fs.exists(target));
assertTrue(fs.delete(target, true));
assertFalse(fs.exists(target));
// cleanup
fs.delete(basePath, true);
}
@Test
public void testTableCreation()
throws Exception
{
for (HiveStorageFormat storageFormat : HiveStorageFormat.values()) {
try {
doCreateTable(temporaryCreateTable, storageFormat, "presto_test");
}
finally {
dropTable(temporaryCreateTable);
}
}
}
private void doCreateTable(SchemaTableName tableName, HiveStorageFormat storageFormat, String tableOwner)
throws Exception
{
// begin creating the table
List<ColumnMetadata> columns = ImmutableList.<ColumnMetadata>builder()
.add(new ColumnMetadata("id", BIGINT, false))
.build();
Map<String, Object> properties = ImmutableMap.<String, Object>builder()
.put(STORAGE_FORMAT_PROPERTY, storageFormat)
.put(PARTITIONED_BY_PROPERTY, ImmutableList.of())
.build();
ConnectorTableMetadata tableMetadata = new ConnectorTableMetadata(tableName, columns, properties, tableOwner);
HiveOutputTableHandle outputHandle = metadata.beginCreateTable(SESSION, tableMetadata);
MaterializedResult data = MaterializedResult.resultBuilder(SESSION, BIGINT)
.row(1)
.row(3)
.row(2)
.build();
// write the records
ConnectorPageSink sink = pageSinkProvider.createPageSink(SESSION, outputHandle);
sink.appendPage(data.toPage(), null);
Collection<Slice> fragments = sink.commit();
// commit the table
metadata.commitCreateTable(SESSION, outputHandle, fragments);
// Hack to work around the metastore not being configured for S3.
// The metastore tries to validate the location when creating the
// table, which fails without explicit configuration for S3.
// We work around that by using a dummy location when creating the
// table and update it here to the correct S3 location.
metastoreClient.updateTableLocation(database, tableName.getTableName(), outputHandle.getWritePath().get());
// load the new table
ConnectorTableHandle tableHandle = getTableHandle(tableName);
List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(SESSION, tableHandle).values());
// verify the metadata
tableMetadata = metadata.getTableMetadata(SESSION, getTableHandle(tableName));
assertEquals(tableMetadata.getOwner(), tableOwner);
assertEquals(tableMetadata.getColumns(), columns);
// verify the data
List<ConnectorTableLayoutResult> tableLayoutResults = metadata.getTableLayouts(SESSION, tableHandle, new Constraint<>(TupleDomain.all(), bindings -> true), Optional.empty());
HiveTableLayoutHandle layoutHandle = (HiveTableLayoutHandle) getOnlyElement(tableLayoutResults).getTableLayout().getHandle();
assertEquals(layoutHandle.getPartitions().size(), 1);
ConnectorSplitSource splitSource = splitManager.getSplits(SESSION, layoutHandle);
ConnectorSplit split = getOnlyElement(getAllSplits(splitSource));
try (ConnectorPageSource pageSource = pageSourceProvider.createPageSource(SESSION, split, columnHandles)) {
MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, getTypes(columnHandles));
assertEqualsIgnoreOrder(result.getMaterializedRows(), data.getMaterializedRows());
}
}
private void dropTable(SchemaTableName table)
{
try {
metastoreClient.dropTable(table.getSchemaName(), table.getTableName());
}
catch (RuntimeException e) {
// this usually occurs because the table was not created
}
}
private ConnectorTableHandle getTableHandle(SchemaTableName tableName)
{
ConnectorTableHandle handle = metadata.getTableHandle(SESSION, tableName);
checkArgument(handle != null, "table not found: %s", tableName);
return handle;
}
private static List<ConnectorSplit> getAllSplits(ConnectorSplitSource source)
throws InterruptedException
{
ImmutableList.Builder<ConnectorSplit> splits = ImmutableList.builder();
while (!source.isFinished()) {
splits.addAll(getFutureValue(source.getNextBatch(1000)));
}
return splits.build();
}
private static ImmutableMap<String, Integer> indexColumns(List<ColumnHandle> columnHandles)
{
ImmutableMap.Builder<String, Integer> index = ImmutableMap.builder();
int i = 0;
for (ColumnHandle columnHandle : columnHandles) {
HiveColumnHandle hiveColumnHandle = checkType(columnHandle, HiveColumnHandle.class, "columnHandle");
index.put(hiveColumnHandle.getName(), i);
i++;
}
return index.build();
}
private static class TestingHiveMetastore
extends CachingHiveMetastore
{
private final String writableBucket;
private final HdfsEnvironment hdfsEnvironment;
public TestingHiveMetastore(HiveCluster hiveCluster, ExecutorService executor, HiveClientConfig hiveClientConfig, String writableBucket, HdfsEnvironment hdfsEnvironment)
{
super(hiveCluster, executor, hiveClientConfig);
this.writableBucket = writableBucket;
this.hdfsEnvironment = hdfsEnvironment;
}
@Override
public Optional<Database> getDatabase(String databaseName)
{
Optional<Database> database = super.getDatabase(databaseName);
if (database.isPresent()) {
database.get().setLocationUri("s3://" + writableBucket + "/");
}
return database;
}
@Override
public void createTable(Table table)
{
// hack to work around the metastore not being configured for S3
table.getSd().setLocation("/");
super.createTable(table);
}
@Override
public void dropTable(String databaseName, String tableName)
{
try {
Optional<Table> table = getTable(databaseName, tableName);
if (!table.isPresent()) {
throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));
}
// hack to work around the metastore not being configured for S3
Path path = new Path(table.get().getSd().getLocation());
table.get().getSd().setLocation("/");
// drop table
try (HiveMetastoreClient client = clientProvider.createMetastoreClient()) {
client.alter_table(databaseName, tableName, table.get());
client.drop_table(databaseName, tableName, false);
}
// drop data
hdfsEnvironment.getFileSystem(path).delete(path, true);
}
catch (Exception e) {
throw Throwables.propagate(e);
}
finally {
invalidateTable(databaseName, tableName);
}
}
public void updateTableLocation(String databaseName, String tableName, String location)
{
try {
Optional<Table> table = getTable(databaseName, tableName);
if (!table.isPresent()) {
throw new TableNotFoundException(new SchemaTableName(databaseName, tableName));
}
table.get().getSd().setLocation(location);
try (HiveMetastoreClient client = clientProvider.createMetastoreClient()) {
client.alter_table(databaseName, tableName, table.get());
}
}
catch (TException e) {
throw Throwables.propagate(e);
}
}
}
}