Skip to content

Commit

Permalink
Add impersonation to HiveMetadataFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Feb 7, 2022
1 parent ab00f59 commit a4142de
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public HiveMetadataFactory(
public TransactionalMetadata create(ConnectorIdentity identity, boolean autoCommit)
{
HiveMetastoreClosure hiveMetastoreClosure = new HiveMetastoreClosure(
memoizeMetastore(metastoreFactory.createMetastore(), perTransactionCacheMaximumSize)); // per-transaction cache
memoizeMetastore(metastoreFactory.createMetastore(Optional.of(identity)), perTransactionCacheMaximumSize)); // per-transaction cache

SemiTransactionalHiveMetastore metastore = new SemiTransactionalHiveMetastore(
hdfsEnvironment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;

Expand Down Expand Up @@ -156,7 +157,7 @@ private ConnectorPageSink createPageSink(HiveWritableTableHandle handle, boolean
session.getQueryId(),
new HivePageSinkMetadataProvider(
handle.getPageSinkMetadata(),
new HiveMetastoreClosure(memoizeMetastore(metastoreFactory.createMetastore(), perTransactionMetastoreCacheMaximumSize)),
new HiveMetastoreClosure(memoizeMetastore(metastoreFactory.createMetastore(Optional.of(session.getIdentity())), perTransactionMetastoreCacheMaximumSize)),
new HiveIdentity(session)),
typeManager,
hdfsEnvironment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.trino.plugin.hive.metastore.procedure.FlushHiveMetastoreCacheProcedure;
import io.trino.plugin.hive.metastore.recording.RecordingHiveMetastoreDecoratorModule;
import io.trino.spi.procedure.Procedure;
import io.trino.spi.security.ConnectorIdentity;

import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -85,9 +86,15 @@ public DecoratingHiveMetastoreFactory(HiveMetastoreFactory delegate, Set<HiveMet
}

@Override
public HiveMetastore createMetastore()
public boolean isImpersonationEnabled()
{
HiveMetastore metastore = delegate.createMetastore();
return delegate.isImpersonationEnabled();
}

@Override
public HiveMetastore createMetastore(Optional<ConnectorIdentity> identity)
{
HiveMetastore metastore = delegate.createMetastore(identity);
for (HiveMetastoreDecorator decorator : sortedDecorators) {
metastore = decorator.decorate(metastore);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@
*/
package io.trino.plugin.hive.metastore;

import io.trino.spi.security.ConnectorIdentity;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public interface HiveMetastoreFactory
{
HiveMetastore createMetastore();
boolean isImpersonationEnabled();

/**
* Create a metastore instance for the identity. An empty identity will
* only be provided when impersonation is disabled, and global caching is
* enabled.
*/
HiveMetastore createMetastore(Optional<ConnectorIdentity> identity);

static HiveMetastoreFactory ofInstance(HiveMetastore metastore)
{
Expand All @@ -35,7 +46,13 @@ private StaticHiveMetastoreFactory(HiveMetastore metastore)
}

@Override
public HiveMetastore createMetastore()
public boolean isImpersonationEnabled()
{
return false;
}

@Override
public HiveMetastore createMetastore(Optional<ConnectorIdentity> identity)
{
return metastore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import io.trino.plugin.hive.metastore.HiveMetastore;
import io.trino.plugin.hive.metastore.HiveMetastoreFactory;
import io.trino.plugin.hive.metastore.MetastoreConfig;
import io.trino.spi.security.ConnectorIdentity;

import javax.inject.Inject;

import java.util.Optional;

public class AlluxioHiveMetastoreFactory
implements HiveMetastoreFactory
{
Expand All @@ -33,7 +36,13 @@ public AlluxioHiveMetastoreFactory(TableMasterClient client, MetastoreConfig met
}

@Override
public HiveMetastore createMetastore()
public boolean isImpersonationEnabled()
{
return false;
}

@Override
public HiveMetastore createMetastore(Optional<ConnectorIdentity> identity)
{
return metastore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.trino.plugin.hive.metastore.HiveMetastore;
import io.trino.plugin.hive.metastore.HiveMetastoreFactory;
import io.trino.spi.NodeManager;
import io.trino.spi.security.ConnectorIdentity;
import org.weakref.jmx.Flatten;
import org.weakref.jmx.Nested;

Expand Down Expand Up @@ -96,11 +97,12 @@ public HiveMetastoreFactory createCachingHiveMetastoreFactory(HiveMetastoreFacto
return metastoreFactory;
}

// caching hive metastore currently handles caching for multiple users internally
CachingHiveMetastore cachingHiveMetastore = cachingHiveMetastore(
metastoreFactory.createMetastore(),
// Loading of cache entry in CachingHiveMetastore might trigger loading of another cache entry for different object type
// In case there are no empty executor slots, such operation would deadlock. Therefore, a reentrant executor needs to be
// used.
metastoreFactory.createMetastore(Optional.empty()),
new ReentrantBoundedExecutor(executorService, maxMetastoreRefreshThreads),
metastoreCacheTtl,
metastoreRefreshInterval,
Expand All @@ -119,7 +121,13 @@ private CachingHiveMetastoreFactory(CachingHiveMetastore metastore)
}

@Override
public HiveMetastore createMetastore()
public boolean isImpersonationEnabled()
{
return metastore.isImpersonationEnabled();
}

@Override
public HiveMetastore createMetastore(Optional<ConnectorIdentity> identity)
{
return metastore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
import io.trino.plugin.hive.metastore.HiveMetastore;
import io.trino.plugin.hive.metastore.HiveMetastoreFactory;
import io.trino.plugin.hive.metastore.MetastoreConfig;
import io.trino.spi.security.ConnectorIdentity;

import javax.inject.Inject;

import java.util.Optional;

public class FileHiveMetastoreFactory
implements HiveMetastoreFactory
{
Expand All @@ -34,7 +37,13 @@ public FileHiveMetastoreFactory(NodeVersion nodeVersion, HdfsEnvironment hdfsEnv
}

@Override
public HiveMetastore createMetastore()
public boolean isImpersonationEnabled()
{
return false;
}

@Override
public HiveMetastore createMetastore(Optional<ConnectorIdentity> identity)
{
return metastore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@
import io.trino.spi.statistics.ColumnStatisticType;
import io.trino.spi.type.Type;
import org.apache.hadoop.fs.Path;
import org.weakref.jmx.Flatten;
import org.weakref.jmx.Managed;

import javax.annotation.Nullable;
import javax.inject.Inject;
Expand Down Expand Up @@ -269,8 +267,6 @@ private static AWSCredentialsProvider getCustomAWSCredentialsProvider(String pro
}
}

@Managed
@Flatten
public GlueMetastoreStats getStats()
{
return stats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

import io.trino.plugin.hive.metastore.HiveMetastore;
import io.trino.plugin.hive.metastore.HiveMetastoreFactory;
import io.trino.spi.security.ConnectorIdentity;
import org.weakref.jmx.Flatten;
import org.weakref.jmx.Managed;

import javax.inject.Inject;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public class GlueHiveMetastoreFactory
Expand All @@ -42,7 +45,13 @@ public GlueHiveMetastore getMetastore()
}

@Override
public HiveMetastore createMetastore()
public boolean isImpersonationEnabled()
{
return false;
}

@Override
public HiveMetastore createMetastore(Optional<ConnectorIdentity> identity)
{
return metastore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

import io.trino.plugin.hive.metastore.HiveMetastore;
import io.trino.plugin.hive.metastore.HiveMetastoreFactory;
import io.trino.spi.security.ConnectorIdentity;

import javax.inject.Inject;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public class BridgingHiveMetastoreFactory
Expand All @@ -32,7 +35,13 @@ public BridgingHiveMetastoreFactory(ThriftMetastore thriftMetastore)
}

@Override
public HiveMetastore createMetastore()
public boolean isImpersonationEnabled()
{
return thriftMetastore.isImpersonationEnabled();
}

@Override
public HiveMetastore createMetastore(Optional<ConnectorIdentity> identity)
{
return new BridgingHiveMetastore(thriftMetastore);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import javax.inject.Inject;

import java.util.Optional;

import static io.trino.plugin.hive.metastore.cache.CachingHiveMetastore.memoizeMetastore;
import static io.trino.plugin.iceberg.IcebergSecurityConfig.IcebergSecurity.SYSTEM;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
Expand Down Expand Up @@ -75,7 +77,7 @@ public TrinoCatalog create(ConnectorIdentity identity)
case HIVE_METASTORE:
return new TrinoHiveCatalog(
catalogName,
memoizeMetastore(metastoreFactory.createMetastore(), 1000),
memoizeMetastore(metastoreFactory.createMetastore(Optional.of(identity)), 1000),
hdfsEnvironment,
typeManager,
tableOperationsProvider,
Expand Down

0 comments on commit a4142de

Please sign in to comment.