Search in sources :

Example 21 with HiveMetastore

use of io.trino.plugin.hive.metastore.HiveMetastore in project trino by trinodb.

the class HiveBenchmarkQueryRunner method createLocalQueryRunner.

public static LocalQueryRunner createLocalQueryRunner(File tempDir) {
    Session session = testSessionBuilder().setCatalog("hive").setSchema("tpch").build();
    LocalQueryRunner localQueryRunner = LocalQueryRunner.create(session);
    // add tpch
    localQueryRunner.createCatalog("tpch", new TpchConnectorFactory(1), ImmutableMap.of());
    // add hive
    File hiveDir = new File(tempDir, "hive_data");
    HiveMetastore metastore = createTestingFileHiveMetastore(hiveDir);
    metastore.createDatabase(Database.builder().setDatabaseName("tpch").setOwnerName(Optional.of("public")).setOwnerType(Optional.of(PrincipalType.ROLE)).build());
    Map<String, String> hiveCatalogConfig = ImmutableMap.<String, String>builder().put("hive.max-split-size", "10GB").buildOrThrow();
    localQueryRunner.createCatalog("hive", new TestingHiveConnectorFactory(metastore), hiveCatalogConfig);
    localQueryRunner.execute("CREATE TABLE orders AS SELECT * FROM tpch.sf1.orders");
    localQueryRunner.execute("CREATE TABLE lineitem AS SELECT * FROM tpch.sf1.lineitem");
    return localQueryRunner;
}
Also used : TpchConnectorFactory(io.trino.plugin.tpch.TpchConnectorFactory) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) FileHiveMetastore.createTestingFileHiveMetastore(io.trino.plugin.hive.metastore.file.FileHiveMetastore.createTestingFileHiveMetastore) File(java.io.File) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Session(io.trino.Session)

Example 22 with HiveMetastore

use of io.trino.plugin.hive.metastore.HiveMetastore in project trino by trinodb.

the class AbstractTestHiveLocal method initialize.

@BeforeClass(alwaysRun = true)
public void initialize() {
    tempDir = Files.createTempDir();
    HiveMetastore metastore = createMetastore(tempDir, HIVE_IDENTITY);
    metastore.createDatabase(Database.builder().setDatabaseName(testDbName).setOwnerName(Optional.of("public")).setOwnerType(Optional.of(PrincipalType.ROLE)).build());
    HiveConfig hiveConfig = new HiveConfig().setParquetTimeZone("America/Los_Angeles").setRcfileTimeZone("America/Los_Angeles");
    setup(testDbName, hiveConfig, metastore, HDFS_ENVIRONMENT);
}
Also used : HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) BeforeClass(org.testng.annotations.BeforeClass)

Example 23 with HiveMetastore

use of io.trino.plugin.hive.metastore.HiveMetastore in project trino by trinodb.

the class TestCachingHiveMetastore method testLoadAfterInvalidate.

@Test(timeOut = 60_000, dataProviderClass = DataProviders.class, dataProvider = "trueFalse")
public void testLoadAfterInvalidate(boolean invalidateAll) throws Exception {
    // State
    CopyOnWriteArrayList<Column> tableColumns = new CopyOnWriteArrayList<>();
    ConcurrentMap<String, Partition> tablePartitionsByName = new ConcurrentHashMap<>();
    Map<String, String> tableParameters = new ConcurrentHashMap<>();
    tableParameters.put("frequent-changing-table-parameter", "parameter initial value");
    // Initialize data
    String databaseName = "my_database";
    String tableName = "my_table_name";
    tableColumns.add(new Column("value", toHiveType(VARCHAR), Optional.empty()));
    tableColumns.add(new Column("pk", toHiveType(VARCHAR), Optional.empty()));
    List<String> partitionNames = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        String partitionName = "pk=" + i;
        tablePartitionsByName.put(partitionName, Partition.builder().setDatabaseName(databaseName).setTableName(tableName).setColumns(ImmutableList.copyOf(tableColumns)).setValues(List.of(Integer.toString(i))).withStorage(storage -> storage.setStorageFormat(fromHiveStorageFormat(TEXTFILE))).setParameters(Map.of("frequent-changing-partition-parameter", "parameter initial value")).build());
        partitionNames.add(partitionName);
    }
    // Mock metastore
    CountDownLatch getTableEnteredLatch = new CountDownLatch(1);
    CountDownLatch getTableReturnLatch = new CountDownLatch(1);
    CountDownLatch getTableFinishedLatch = new CountDownLatch(1);
    CountDownLatch getPartitionsByNamesEnteredLatch = new CountDownLatch(1);
    CountDownLatch getPartitionsByNamesReturnLatch = new CountDownLatch(1);
    CountDownLatch getPartitionsByNamesFinishedLatch = new CountDownLatch(1);
    HiveMetastore mockMetastore = new UnimplementedHiveMetastore() {

        @Override
        public Optional<Table> getTable(String databaseName, String tableName) {
            Optional<Table> table = Optional.of(Table.builder().setDatabaseName(databaseName).setTableName(tableName).setTableType(EXTERNAL_TABLE.name()).setDataColumns(tableColumns).setParameters(ImmutableMap.copyOf(tableParameters)).withStorage(storage -> storage.setStorageFormat(fromHiveStorageFormat(TEXTFILE))).setOwner(Optional.empty()).build());
            // 1
            getTableEnteredLatch.countDown();
            // 2
            await(getTableReturnLatch, 10, SECONDS);
            return table;
        }

        @Override
        public Map<String, Optional<Partition>> getPartitionsByNames(Table table, List<String> partitionNames) {
            Map<String, Optional<Partition>> result = new HashMap<>();
            for (String partitionName : partitionNames) {
                result.put(partitionName, Optional.ofNullable(tablePartitionsByName.get(partitionName)));
            }
            // loader#1
            getPartitionsByNamesEnteredLatch.countDown();
            // loader#2
            await(getPartitionsByNamesReturnLatch, 10, SECONDS);
            return result;
        }
    };
    // Caching metastore
    metastore = cachingHiveMetastore(mockMetastore, executor, new Duration(5, TimeUnit.MINUTES), Optional.of(new Duration(1, TimeUnit.MINUTES)), 1000);
    // The test. Main thread does modifications and verifies subsequent load sees them. Background thread loads the state into the cache.
    ExecutorService executor = Executors.newFixedThreadPool(1);
    try {
        Future<Void> future = executor.submit(() -> {
            try {
                Table table;
                table = metastore.getTable(databaseName, tableName).orElseThrow();
                // 3
                getTableFinishedLatch.countDown();
                metastore.getPartitionsByNames(table, partitionNames);
                // 6
                getPartitionsByNamesFinishedLatch.countDown();
                return (Void) null;
            } catch (Throwable e) {
                log.error(e);
                throw e;
            }
        });
        // 21
        await(getTableEnteredLatch, 10, SECONDS);
        tableParameters.put("frequent-changing-table-parameter", "main-thread-put-xyz");
        if (invalidateAll) {
            metastore.flushCache();
        } else {
            metastore.invalidateTable(databaseName, tableName);
        }
        // 2
        getTableReturnLatch.countDown();
        // 3
        await(getTableFinishedLatch, 10, SECONDS);
        Table table = metastore.getTable(databaseName, tableName).orElseThrow();
        assertThat(table.getParameters()).isEqualTo(Map.of("frequent-changing-table-parameter", "main-thread-put-xyz"));
        // 4
        await(getPartitionsByNamesEnteredLatch, 10, SECONDS);
        String partitionName = partitionNames.get(2);
        Map<String, String> newPartitionParameters = Map.of("frequent-changing-partition-parameter", "main-thread-put-alice");
        tablePartitionsByName.put(partitionName, Partition.builder(tablePartitionsByName.get(partitionName)).setParameters(newPartitionParameters).build());
        if (invalidateAll) {
            metastore.flushCache();
        } else {
            metastore.invalidateTable(databaseName, tableName);
        }
        // 5
        getPartitionsByNamesReturnLatch.countDown();
        // 6
        await(getPartitionsByNamesFinishedLatch, 10, SECONDS);
        Map<String, Optional<Partition>> loadedPartitions = metastore.getPartitionsByNames(table, partitionNames);
        assertThat(loadedPartitions.get(partitionName)).isNotNull().isPresent().hasValueSatisfying(partition -> assertThat(partition.getParameters()).isEqualTo(newPartitionParameters));
        // verify no failure in the background thread
        future.get(10, SECONDS);
    } finally {
        getTableEnteredLatch.countDown();
        getTableReturnLatch.countDown();
        getTableFinishedLatch.countDown();
        getPartitionsByNamesEnteredLatch.countDown();
        getPartitionsByNamesReturnLatch.countDown();
        getPartitionsByNamesFinishedLatch.countDown();
        executor.shutdownNow();
        executor.awaitTermination(10, SECONDS);
    }
}
Also used : PARTITION_KEY(io.trino.plugin.hive.HiveColumnHandle.ColumnType.PARTITION_KEY) USER(io.trino.spi.security.PrincipalType.USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EXTERNAL_TABLE(org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE) Test(org.testng.annotations.Test) TEST_PARTITION1(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.TEST_PARTITION1) Duration(io.airlift.units.Duration) MockThriftMetastoreClient(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient) TEST_PARTITION2(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.TEST_PARTITION2) Future(java.util.concurrent.Future) Column(io.trino.plugin.hive.metastore.Column) Map(java.util.Map) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) CachingHiveMetastore.memoizeMetastore(io.trino.plugin.hive.metastore.cache.CachingHiveMetastore.memoizeMetastore) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) Assert.assertFalse(org.testng.Assert.assertFalse) HiveIdentity(io.trino.plugin.hive.authentication.HiveIdentity) Table(io.trino.plugin.hive.metastore.Table) ImmutableMap(com.google.common.collect.ImmutableMap) Range(io.trino.spi.predicate.Range) DataProviders(io.trino.testing.DataProviders) ThriftHiveMetastore(io.trino.plugin.hive.metastore.thrift.ThriftHiveMetastore) Domain(io.trino.spi.predicate.Domain) BAD_PARTITION(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.BAD_PARTITION) BeforeMethod(org.testng.annotations.BeforeMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SESSION(io.trino.testing.TestingConnectorSession.SESSION) Assert.assertNotNull(org.testng.Assert.assertNotNull) HDFS_ENVIRONMENT(io.trino.plugin.hive.HiveTestUtils.HDFS_ENVIRONMENT) Executors(java.util.concurrent.Executors) ValueSet(io.trino.spi.predicate.ValueSet) Preconditions.checkState(com.google.common.base.Preconditions.checkState) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) BAD_DATABASE(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.BAD_DATABASE) TEST_COLUMN(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.TEST_COLUMN) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MetastoreLocator(io.trino.plugin.hive.metastore.thrift.MetastoreLocator) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) TEST_ROLES(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.TEST_ROLES) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Partition(io.trino.plugin.hive.metastore.Partition) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) PartitionStatistics(io.trino.plugin.hive.PartitionStatistics) HivePrincipal(io.trino.plugin.hive.metastore.HivePrincipal) MoreExecutors.listeningDecorator(com.google.common.util.concurrent.MoreExecutors.listeningDecorator) Iterables(com.google.common.collect.Iterables) Logger(io.airlift.log.Logger) TEST_PARTITION_VALUES1(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.TEST_PARTITION_VALUES1) MetastoreConfig(io.trino.plugin.hive.metastore.MetastoreConfig) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) StorageFormat.fromHiveStorageFormat(io.trino.plugin.hive.metastore.StorageFormat.fromHiveStorageFormat) UnimplementedHiveMetastore(io.trino.plugin.hive.metastore.UnimplementedHiveMetastore) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) OptionalLong(java.util.OptionalLong) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) PARTITION_COLUMN_NAMES(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.PARTITION_COLUMN_NAMES) ImmutableList(com.google.common.collect.ImmutableList) ThriftMetastoreClient(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreClient) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) BridgingHiveMetastore(io.trino.plugin.hive.metastore.thrift.BridgingHiveMetastore) HiveColumnHandle.createBaseColumn(io.trino.plugin.hive.HiveColumnHandle.createBaseColumn) TEXTFILE(io.trino.plugin.hive.HiveStorageFormat.TEXTFILE) HiveType.toHiveType(io.trino.plugin.hive.HiveType.toHiveType) ExecutorService(java.util.concurrent.ExecutorService) AfterClass(org.testng.annotations.AfterClass) TEST_TABLE(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.TEST_TABLE) TupleDomain.withColumnDomains(io.trino.spi.predicate.TupleDomain.withColumnDomains) HiveColumnStatistics.createIntegerColumnStatistics(io.trino.plugin.hive.metastore.HiveColumnStatistics.createIntegerColumnStatistics) TupleDomain(io.trino.spi.predicate.TupleDomain) HIVE_STRING(io.trino.plugin.hive.HiveType.HIVE_STRING) ThriftMetastoreStats(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreStats) TimeUnit(java.util.concurrent.TimeUnit) HiveMetastoreClosure(io.trino.plugin.hive.HiveMetastoreClosure) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) ThriftMetastoreConfig(io.trino.plugin.hive.metastore.thrift.ThriftMetastoreConfig) MetastoreUtil.computePartitionKeyFilter(io.trino.plugin.hive.metastore.MetastoreUtil.computePartitionKeyFilter) CachingHiveMetastore.cachingHiveMetastore(io.trino.plugin.hive.metastore.cache.CachingHiveMetastore.cachingHiveMetastore) Assert.assertTrue(org.testng.Assert.assertTrue) TEST_DATABASE(io.trino.plugin.hive.metastore.thrift.MockThriftMetastoreClient.TEST_DATABASE) HiveConfig(io.trino.plugin.hive.HiveConfig) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Column(io.trino.plugin.hive.metastore.Column) HiveColumnHandle.createBaseColumn(io.trino.plugin.hive.HiveColumnHandle.createBaseColumn) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnimplementedHiveMetastore(io.trino.plugin.hive.metastore.UnimplementedHiveMetastore) Partition(io.trino.plugin.hive.metastore.Partition) Table(io.trino.plugin.hive.metastore.Table) Optional(java.util.Optional) ThriftHiveMetastore(io.trino.plugin.hive.metastore.thrift.ThriftHiveMetastore) UnimplementedHiveMetastore(io.trino.plugin.hive.metastore.UnimplementedHiveMetastore) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) BridgingHiveMetastore(io.trino.plugin.hive.metastore.thrift.BridgingHiveMetastore) CachingHiveMetastore.cachingHiveMetastore(io.trino.plugin.hive.metastore.cache.CachingHiveMetastore.cachingHiveMetastore) Duration(io.airlift.units.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.testng.annotations.Test)

Aggregations

HiveMetastore (io.trino.plugin.hive.metastore.HiveMetastore)23 File (java.io.File)10 MetastoreConfig (io.trino.plugin.hive.metastore.MetastoreConfig)9 NoHdfsAuthentication (io.trino.plugin.hive.authentication.NoHdfsAuthentication)8 FileHiveMetastore.createTestingFileHiveMetastore (io.trino.plugin.hive.metastore.file.FileHiveMetastore.createTestingFileHiveMetastore)8 Optional (java.util.Optional)8 Map (java.util.Map)7 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 HdfsConfig (io.trino.plugin.hive.HdfsConfig)6 HdfsConfiguration (io.trino.plugin.hive.HdfsConfiguration)6 HdfsConfigurationInitializer (io.trino.plugin.hive.HdfsConfigurationInitializer)6 HdfsEnvironment (io.trino.plugin.hive.HdfsEnvironment)6 HiveHdfsConfiguration (io.trino.plugin.hive.HiveHdfsConfiguration)6 NodeVersion (io.trino.plugin.hive.NodeVersion)6 List (java.util.List)6 Test (org.testng.annotations.Test)6 Session (io.trino.Session)5 CatalogName (io.trino.plugin.base.CatalogName)5 ThriftHiveMetastore (io.trino.plugin.hive.metastore.thrift.ThriftHiveMetastore)5