-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
findepi
merged 3 commits into
trinodb:master
from
findepi:findepi/allow-disabling-caching-missing-tables-in-cachingmetastore-6c57b6
Apr 25, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -146,6 +148,7 @@ public static CachingHiveMetastoreBuilder builder(CachingHiveMetastoreBuilder ot | |
other.refreshMills, | ||
other.maximumSize, | ||
other.statsRecording, | ||
other.cacheMissing, | ||
other.partitionCacheEnabled); | ||
} | ||
|
||
|
@@ -157,6 +160,8 @@ public static CachingHiveMetastore memoizeMetastore(HiveMetastore delegate, long | |
.statsCacheEnabled(true) | ||
.maximumSize(maximumSize) | ||
.statsRecording(StatsRecording.DISABLED) | ||
.cacheMissing(true) | ||
.partitionCacheEnabled(true) | ||
.build(); | ||
} | ||
|
||
|
@@ -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() {} | ||
|
||
|
@@ -186,7 +192,8 @@ private CachingHiveMetastoreBuilder( | |
OptionalLong refreshMills, | ||
Long maximumSize, | ||
StatsRecording statsRecording, | ||
boolean partitionCacheEnabled) | ||
Boolean cacheMissing, | ||
Boolean partitionCacheEnabled) | ||
{ | ||
this.delegate = delegate; | ||
this.executor = executor; | ||
|
@@ -197,6 +204,7 @@ private CachingHiveMetastoreBuilder( | |
this.refreshMills = refreshMills; | ||
this.maximumSize = maximumSize; | ||
this.statsRecording = statsRecording; | ||
this.cacheMissing = cacheMissing; | ||
this.partitionCacheEnabled = partitionCacheEnabled; | ||
} | ||
|
||
|
@@ -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) | ||
{ | ||
|
@@ -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, | ||
|
@@ -295,6 +312,7 @@ public CachingHiveMetastore build() | |
executor, | ||
maximumSize, | ||
statsRecording, | ||
cacheMissing, | ||
partitionCacheEnabled); | ||
} | ||
} | ||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
catch (UncheckedExecutionException e) { | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't it be also used in bulk loaded cache? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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