-
Notifications
You must be signed in to change notification settings - Fork 3.6k
DRAFT: Implement unified blob cache plugin SPI #29184
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
Open
wendigo
wants to merge
2
commits into
master
Choose a base branch
from
user/serafin/unified-caching-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| cache-manager.name=memory | ||
| fs.memory-cache.max-size=2GB | ||
| fs.memory-cache.ttl=24h |
This file contains hidden or 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
45 changes: 45 additions & 0 deletions
45
core/trino-main/src/main/java/io/trino/cache/CacheManagerConfig.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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 io.trino.cache; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.airlift.configuration.Config; | ||
| import io.airlift.configuration.validation.FileExists; | ||
|
|
||
| import java.io.File; | ||
| import java.util.List; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
|
|
||
| public class CacheManagerConfig | ||
| { | ||
| private List<File> cacheManagerConfigFiles = ImmutableList.of(); | ||
|
|
||
| public List<@FileExists File> getCacheManagerConfigFiles() | ||
| { | ||
| return cacheManagerConfigFiles; | ||
| } | ||
|
|
||
| @Config("cache-manager.config-files") | ||
| public CacheManagerConfig setCacheManagerConfigFiles(String cacheManagerConfigFiles) | ||
| { | ||
| this.cacheManagerConfigFiles = Stream.of(cacheManagerConfigFiles.split(",")) | ||
| .map(String::trim) | ||
| .filter(s -> !s.isEmpty()) | ||
| .map(File::new) | ||
| .collect(toImmutableList()); | ||
| return this; | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
core/trino-main/src/main/java/io/trino/cache/CacheManagerContextInstance.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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 io.trino.cache; | ||
|
|
||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.trino.spi.cache.CacheManagerContext; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class CacheManagerContextInstance | ||
| implements CacheManagerContext | ||
| { | ||
| private final OpenTelemetry openTelemetry; | ||
| private final Tracer tracer; | ||
|
|
||
| public CacheManagerContextInstance(OpenTelemetry openTelemetry, Tracer tracer) | ||
| { | ||
| this.openTelemetry = requireNonNull(openTelemetry, "openTelemetry is null"); | ||
| this.tracer = requireNonNull(tracer, "tracer is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public OpenTelemetry getOpenTelemetry() | ||
| { | ||
| return openTelemetry; | ||
| } | ||
|
|
||
| @Override | ||
| public Tracer getTracer() | ||
| { | ||
| return tracer; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
core/trino-main/src/main/java/io/trino/cache/CacheManagerModule.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * 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 io.trino.cache; | ||
|
|
||
| import com.google.inject.Binder; | ||
| import com.google.inject.Module; | ||
| import com.google.inject.Scopes; | ||
|
|
||
| import static io.airlift.configuration.ConfigBinder.configBinder; | ||
|
|
||
| public class CacheManagerModule | ||
| implements Module | ||
| { | ||
| @Override | ||
| public void configure(Binder binder) | ||
| { | ||
| configBinder(binder).bindConfig(CacheManagerConfig.class); | ||
| binder.bind(CacheManagerRegistry.class).in(Scopes.SINGLETON); | ||
| } | ||
| } |
180 changes: 180 additions & 0 deletions
180
core/trino-main/src/main/java/io/trino/cache/CacheManagerRegistry.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /* | ||
| * 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 io.trino.cache; | ||
|
|
||
| import com.google.inject.Inject; | ||
| import io.airlift.configuration.secrets.SecretsResolver; | ||
| import io.airlift.log.Logger; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.trino.spi.cache.Blob; | ||
| import io.trino.spi.cache.BlobCache; | ||
| import io.trino.spi.cache.BlobCacheManager; | ||
| import io.trino.spi.cache.BlobCacheManagerFactory; | ||
| import io.trino.spi.cache.BlobSource; | ||
| import io.trino.spi.cache.CacheKey; | ||
| import io.trino.spi.cache.CacheTier; | ||
| import io.trino.spi.cache.ConnectorCacheFactory; | ||
| import io.trino.spi.cache.PassThroughBlob; | ||
| import io.trino.spi.catalog.CatalogName; | ||
| import io.trino.spi.classloader.ThreadContextClassLoader; | ||
| import jakarta.annotation.PreDestroy; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.base.Strings.isNullOrEmpty; | ||
| import static io.airlift.configuration.ConfigurationLoader.loadPropertiesFrom; | ||
| import static java.lang.String.format; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class CacheManagerRegistry | ||
| { | ||
| private static final Logger log = Logger.get(CacheManagerRegistry.class); | ||
|
|
||
| private static final String CACHE_MANAGER_NAME_PROPERTY = "cache-manager.name"; | ||
|
|
||
| private final OpenTelemetry openTelemetry; | ||
| private final Tracer tracer; | ||
| private final SecretsResolver secretsResolver; | ||
| private final List<File> configFiles; | ||
|
|
||
| private final Map<String, BlobCacheManagerFactory> blobCacheFactories = new ConcurrentHashMap<>(); | ||
| private final Map<CacheTier, BlobCacheManager> blobCacheManagers = new ConcurrentHashMap<>(); | ||
|
|
||
| @Inject | ||
| public CacheManagerRegistry( | ||
| OpenTelemetry openTelemetry, | ||
| Tracer tracer, | ||
| SecretsResolver secretsResolver, | ||
| CacheManagerConfig config) | ||
| { | ||
| this.openTelemetry = requireNonNull(openTelemetry, "openTelemetry is null"); | ||
| this.tracer = requireNonNull(tracer, "tracer is null"); | ||
| this.secretsResolver = requireNonNull(secretsResolver, "secretsResolver is null"); | ||
| this.configFiles = List.copyOf(config.getCacheManagerConfigFiles()); | ||
| } | ||
|
|
||
| public void addBlobCacheManagerFactory(BlobCacheManagerFactory factory) | ||
| { | ||
| requireNonNull(factory, "factory is null"); | ||
| if (blobCacheFactories.putIfAbsent(factory.getName(), factory) != null) { | ||
| throw new IllegalArgumentException(format("Blob cache manager factory '%s' is already registered", factory.getName())); | ||
| } | ||
| } | ||
|
|
||
| public synchronized void loadCacheManagers() | ||
| { | ||
| for (File configFile : configFiles) { | ||
| Map<String, String> properties = loadProperties(configFile); | ||
| String name = properties.remove(CACHE_MANAGER_NAME_PROPERTY); | ||
| checkArgument(!isNullOrEmpty(name), "Cache manager configuration %s does not contain %s", configFile, CACHE_MANAGER_NAME_PROPERTY); | ||
| loadBlobCacheManager(name, properties); | ||
| } | ||
| } | ||
|
|
||
| public synchronized void loadBlobCacheManager(String name, Map<String, String> properties) | ||
| { | ||
| log.info("-- Loading blob cache manager %s --", name); | ||
|
|
||
| BlobCacheManagerFactory factory = blobCacheFactories.get(name); | ||
| checkArgument(factory != null, "Blob cache manager factory '%s' is not registered. Available factories: %s", name, blobCacheFactories.keySet()); | ||
|
|
||
| CacheTier tier = factory.tier(); | ||
| if (blobCacheManagers.containsKey(tier)) { | ||
| throw new IllegalStateException(format("Blob cache manager for tier %s is already loaded", tier)); | ||
| } | ||
|
|
||
| BlobCacheManager manager; | ||
| try (ThreadContextClassLoader _ = new ThreadContextClassLoader(factory.getClass().getClassLoader())) { | ||
| manager = factory.create(secretsResolver.getResolvedConfiguration(properties), new CacheManagerContextInstance(openTelemetry, tracer)); | ||
| } | ||
|
|
||
| blobCacheManagers.put(tier, manager); | ||
| log.info("-- Loaded blob cache manager %s for tier %s --", name, tier); | ||
| } | ||
|
|
||
| public ConnectorCacheFactory createConnectorCacheFactory(CatalogName catalog) | ||
| { | ||
| requireNonNull(catalog, "catalog is null"); | ||
| return tier -> { | ||
| BlobCacheManager manager = blobCacheManagers.get(tier); | ||
| if (manager == null) { | ||
| log.warn("Catalog %s requested blob cache manager tier %s but none registered, using noop", catalog, tier); | ||
| return new NoopBlobCache(); | ||
| } | ||
| log.debug("Created new blob cache on tier %s for catalog %s", tier, catalog); | ||
| return manager.createBlobCache(catalog); | ||
| }; | ||
| } | ||
|
|
||
| public void drop(CatalogName catalog) | ||
| { | ||
| for (Map.Entry<CacheTier, BlobCacheManager> entry : blobCacheManagers.entrySet()) { | ||
| log.info("Dropping blob cache for tier %s and catalog %s", entry.getKey(), catalog); | ||
| try { | ||
| entry.getValue().drop(catalog); | ||
| } | ||
| catch (Throwable t) { | ||
| log.error(t, "Error dropping blob cache for tier %s and catalog %s", entry.getKey(), catalog); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @PreDestroy | ||
| public void shutdown() | ||
| { | ||
| for (Map.Entry<CacheTier, BlobCacheManager> entry : blobCacheManagers.entrySet()) { | ||
| try { | ||
| entry.getValue().shutdown(); | ||
| } | ||
| catch (Throwable t) { | ||
| log.error(t, "Error shutting down blob cache manager for tier %s", entry.getKey()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Map<String, String> loadProperties(File configFile) | ||
| { | ||
| try { | ||
| return new HashMap<>(loadPropertiesFrom(configFile.getPath())); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to read configuration file: " + configFile, e); | ||
| } | ||
| } | ||
|
|
||
| private static class NoopBlobCache | ||
| implements BlobCache | ||
| { | ||
| @Override | ||
| public Blob get(CacheKey key, BlobSource source) | ||
| { | ||
| return new PassThroughBlob(source); | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(CacheKey key) {} | ||
|
|
||
| @Override | ||
| public void invalidate(Collection<CacheKey> keys) {} | ||
| } | ||
| } | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.