Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling caching missing tables in CachingMetastore #17177

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import java.util.function.Supplier;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Verify.verifyNotNull;
import static com.google.common.cache.CacheLoader.asyncReloading;
Expand Down Expand Up @@ -113,6 +114,7 @@ public enum StatsRecording
}

protected final HiveMetastore delegate;
private final boolean cacheMissing;
private final LoadingCache<String, Optional<Database>> databaseCache;
private final LoadingCache<String, List<String>> databaseNamesCache;
private final LoadingCache<HiveTableName, Optional<Table>> tableCache;
Expand Down Expand Up @@ -146,6 +148,7 @@ public static CachingHiveMetastoreBuilder builder(CachingHiveMetastoreBuilder ot
other.refreshMills,
other.maximumSize,
other.statsRecording,
other.cacheMissing,
other.partitionCacheEnabled);
}

Expand All @@ -157,6 +160,8 @@ public static CachingHiveMetastore memoizeMetastore(HiveMetastore delegate, long
.statsCacheEnabled(true)
.maximumSize(maximumSize)
.statsRecording(StatsRecording.DISABLED)
.cacheMissing(true)
.partitionCacheEnabled(true)
.build();
}

Expand All @@ -172,7 +177,8 @@ public static class CachingHiveMetastoreBuilder
private OptionalLong refreshMills = OptionalLong.empty();
private Long maximumSize;
private StatsRecording statsRecording = StatsRecording.ENABLED;
private boolean partitionCacheEnabled = true;
private Boolean cacheMissing;
private Boolean partitionCacheEnabled;

public CachingHiveMetastoreBuilder() {}

Expand All @@ -186,7 +192,8 @@ private CachingHiveMetastoreBuilder(
OptionalLong refreshMills,
Long maximumSize,
StatsRecording statsRecording,
boolean partitionCacheEnabled)
Boolean cacheMissing,
Boolean partitionCacheEnabled)
{
this.delegate = delegate;
this.executor = executor;
Expand All @@ -197,6 +204,7 @@ private CachingHiveMetastoreBuilder(
this.refreshMills = refreshMills;
this.maximumSize = maximumSize;
this.statsRecording = statsRecording;
this.cacheMissing = cacheMissing;
this.partitionCacheEnabled = partitionCacheEnabled;
}

Expand Down Expand Up @@ -272,6 +280,13 @@ public CachingHiveMetastoreBuilder statsRecording(StatsRecording statsRecording)
return this;
}

@CanIgnoreReturnValue
public CachingHiveMetastoreBuilder cacheMissing(boolean cacheMissing)
{
this.cacheMissing = cacheMissing;
return this;
}

@CanIgnoreReturnValue
public CachingHiveMetastoreBuilder partitionCacheEnabled(boolean partitionCacheEnabled)
{
Expand All @@ -285,6 +300,8 @@ public CachingHiveMetastore build()
requireNonNull(statsCacheEnabled, "statsCacheEnabled is null");
requireNonNull(delegate, "delegate not set");
requireNonNull(maximumSize, "maximumSize not set");
requireNonNull(cacheMissing, "cacheMissing not set");
requireNonNull(partitionCacheEnabled, "partitionCacheEnabled not set");
return new CachingHiveMetastore(
delegate,
metadataCacheEnabled,
Expand All @@ -295,6 +312,7 @@ public CachingHiveMetastore build()
executor,
maximumSize,
statsRecording,
cacheMissing,
partitionCacheEnabled);
}
}
Expand All @@ -309,10 +327,12 @@ protected CachingHiveMetastore(
Optional<Executor> executor,
long maximumSize,
StatsRecording statsRecording,
boolean cacheMissing,
boolean partitionCacheEnabled)
{
checkArgument(metadataCacheEnabled || statsCacheEnabled, "Cache not enabled");
this.delegate = requireNonNull(delegate, "delegate is null");
this.cacheMissing = cacheMissing;
requireNonNull(executor, "executor is null");

CacheFactory cacheFactory;
Expand Down Expand Up @@ -401,6 +421,28 @@ private AtomicReference<PartitionStatistics> refreshTableStatistics(HiveTableNam
private static <K, V> V get(LoadingCache<K, V> cache, K key)
{
try {
V value = cache.getUnchecked(key);
checkState(!(value instanceof Optional), "This must not be used for caches with Optional values, as it doesn't implement cacheMissing logic. Use getOptional()");
return value;
}
catch (UncheckedExecutionException e) {
throwIfInstanceOf(e.getCause(), TrinoException.class);
throw e;
}
}

private <K, V> Optional<V> getOptional(LoadingCache<K, Optional<V>> cache, K key)
{
try {
Optional<V> value = cache.getIfPresent(key);
@SuppressWarnings("OptionalAssignedToNull")
boolean valueIsPresent = value != null;
if (valueIsPresent) {
if (value.isPresent() || cacheMissing) {
return value;
}
cache.invalidate(key);
}
return cache.getUnchecked(key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary call this method if the value is not present?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, cache.getIfPresent didn't load anything yet

}
catch (UncheckedExecutionException e) {
Expand Down Expand Up @@ -524,7 +566,7 @@ private static <K, V> Map<K, V> getAll(
@Override
public Optional<Database> getDatabase(String databaseName)
{
return get(databaseCache, databaseName);
return getOptional(databaseCache, databaseName);
}

private Optional<Database> loadDatabase(String databaseName)
Expand Down Expand Up @@ -552,7 +594,7 @@ private Table getExistingTable(String databaseName, String tableName)
@Override
public Optional<Table> getTable(String databaseName, String tableName)
{
return get(tableCache, hiveTableName(databaseName, tableName));
return getOptional(tableCache, hiveTableName(databaseName, tableName));
}

@Override
Expand Down Expand Up @@ -943,7 +985,7 @@ public Optional<List<String>> getPartitionNamesByFilter(
List<String> columnNames,
TupleDomain<String> partitionKeysFilter)
{
return get(partitionFilterCache, partitionFilter(databaseName, tableName, columnNames, partitionKeysFilter));
return getOptional(partitionFilterCache, partitionFilter(databaseName, tableName, columnNames, partitionKeysFilter));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be also used in bulk loaded cache?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps, but I didn't find the bulk loaded cache which caches absence, can you help me with that?

}

private Optional<List<String>> loadPartitionNamesByFilter(PartitionFilter partitionFilter)
Expand Down Expand Up @@ -1168,7 +1210,7 @@ public Set<HivePrivilegeInfo> listTablePrivileges(String databaseName, String ta
@Override
public Optional<String> getConfigValue(String name)
{
return get(configValuesCache, name);
return getOptional(configValuesCache, name);
}

private Optional<String> loadConfigValue(String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CachingHiveMetastoreConfig
private Optional<Duration> metastoreRefreshInterval = Optional.empty();
private long metastoreCacheMaximumSize = 10000;
private int maxMetastoreRefreshThreads = 10;
private boolean cacheMissing = true;
private boolean partitionCacheEnabled = true;

@NotNull
Expand Down Expand Up @@ -101,6 +102,18 @@ public CachingHiveMetastoreConfig setMaxMetastoreRefreshThreads(int maxMetastore
return this;
}

public boolean isCacheMissing()
{
return cacheMissing;
}

@Config("hive.metastore-cache.cache-missing")
public CachingHiveMetastoreConfig setCacheMissing(boolean cacheMissing)
{
this.cacheMissing = cacheMissing;
return this;
}

public boolean isPartitionCacheEnabled()
{
return partitionCacheEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public SharedHiveMetastoreCache(
.statsCacheTtl(statsCacheTtl)
.refreshInterval(config.getMetastoreRefreshInterval())
.maximumSize(config.getMetastoreCacheMaximumSize())
.cacheMissing(config.isCacheMissing())
.partitionCacheEnabled(config.isPartitionCacheEnabled());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ protected final void setup(HostAndPort metastoreAddress, String databaseName)
.cacheTtl(new Duration(1, MINUTES))
.refreshInterval(new Duration(15, SECONDS))
.maximumSize(10000)
.cacheMissing(new CachingHiveMetastoreConfig().isCacheMissing())
.partitionCacheEnabled(new CachingHiveMetastoreConfig().isPartitionCacheEnabled())
.build();

Expand Down
Loading