This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Use Caffeine for request aggregation #6
Merged
SailReal
merged 7 commits into
develop
from
feature/use-caffeine-for-request-aggregation
Jan 21, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b210de
Switch to caffeine for request aggregation
SailReal 9c8d0ab
Cleanup and remove test
SailReal 81efaea
Separate quota caching and request deduplication
SailReal a10681f
Add test to verify that CompletionStage is resolved once using Caffeine
SailReal da8a766
Use interface with default methods for CloudProviderDecorator
SailReal c97678f
Enhance unit tests and pass Future to Caffeine
SailReal 8e1e242
Replace caches in tests by a local variable
SailReal 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
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
73 changes: 73 additions & 0 deletions
73
src/main/java/org/cryptomator/cloudaccess/requestdecorator/CloudProviderDecorator.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,73 @@ | ||
| package org.cryptomator.cloudaccess.requestdecorator; | ||
|
|
||
| import org.cryptomator.cloudaccess.api.CloudItemList; | ||
| import org.cryptomator.cloudaccess.api.CloudItemMetadata; | ||
| import org.cryptomator.cloudaccess.api.CloudPath; | ||
| import org.cryptomator.cloudaccess.api.CloudProvider; | ||
| import org.cryptomator.cloudaccess.api.ProgressListener; | ||
| import org.cryptomator.cloudaccess.api.Quota; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.time.Instant; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| interface CloudProviderDecorator extends CloudProvider { | ||
|
|
||
| CloudProvider delegate(); | ||
|
|
||
| @Override | ||
| default CompletionStage<CloudItemMetadata> itemMetadata(CloudPath node) { | ||
| return delegate().itemMetadata(node); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<Quota> quota(CloudPath folder) { | ||
| return delegate().quota(folder); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<CloudItemList> list(CloudPath folder, Optional<String> pageToken) { | ||
| return delegate().list(folder, pageToken); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<InputStream> read(CloudPath file, long offset, long count, ProgressListener progressListener) { | ||
| return delegate().read(file, offset, count, progressListener); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<Void> write(CloudPath file, boolean replace, InputStream data, long size, Optional<Instant> lastModified, ProgressListener progressListener) { | ||
| return delegate().write(file, replace, data, size, lastModified, progressListener); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<CloudPath> createFolder(CloudPath folder) { | ||
| return delegate().createFolder(folder); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<Void> deleteFile(CloudPath file) { | ||
| return delegate().deleteFile(file); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<Void> deleteFolder(CloudPath folder) { | ||
| return delegate().deleteFolder(folder); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<CloudPath> move(CloudPath source, CloudPath target, boolean replace) { | ||
| return delegate().move(source, target, replace); | ||
| } | ||
|
|
||
| @Override | ||
| default boolean cachingCapability() { | ||
| return delegate().cachingCapability(); | ||
| } | ||
|
|
||
| @Override | ||
| default CompletionStage<Void> pollRemoteChanges() { | ||
| return delegate().pollRemoteChanges(); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
...main/java/org/cryptomator/cloudaccess/requestdecorator/CloudProviderDecoratorFactory.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,18 @@ | ||
| package org.cryptomator.cloudaccess.requestdecorator; | ||
|
|
||
| import org.cryptomator.cloudaccess.api.CloudProvider; | ||
|
|
||
| /** | ||
| * Factory class to add a caching or request-deduplication decorator around an existing {@link CloudProvider}. | ||
| */ | ||
| public class CloudProviderDecoratorFactory { | ||
|
|
||
| public CloudProvider get(CloudProvider cloudProvider, boolean cloudCachingCapability) { | ||
| if (cloudCachingCapability) { | ||
| var quotaCachingDecorator = new QuotaRequestCachingDecorator(cloudProvider); | ||
| return new MetadataRequestDeduplicationDecorator(quotaCachingDecorator); | ||
| } else { | ||
| return new MetadataCachingProviderDecorator(cloudProvider); | ||
| } | ||
| } | ||
| } |
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
58 changes: 58 additions & 0 deletions
58
...a/org/cryptomator/cloudaccess/requestdecorator/MetadataRequestDeduplicationDecorator.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,58 @@ | ||
| package org.cryptomator.cloudaccess.requestdecorator; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.AsyncCache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import org.cryptomator.cloudaccess.api.CloudItemList; | ||
| import org.cryptomator.cloudaccess.api.CloudItemMetadata; | ||
| import org.cryptomator.cloudaccess.api.CloudPath; | ||
| import org.cryptomator.cloudaccess.api.CloudProvider; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| /** | ||
| * Decorates an existing CloudProvider by deduplicating identical itemMetadata and list-requests so that the delegate is called only once until the future is completed. | ||
| */ | ||
| class MetadataRequestDeduplicationDecorator implements CloudProviderDecorator { | ||
|
|
||
| // visible for testing | ||
| final AsyncCache<CloudPath, CloudItemMetadata> cachedItemMetadataRequests; | ||
| final AsyncCache<ItemListEntry, CloudItemList> cachedItemListRequests; | ||
|
|
||
| private final CloudProvider delegate; | ||
|
|
||
| public MetadataRequestDeduplicationDecorator(CloudProvider delegate) { | ||
| this(delegate, // | ||
| Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(0)).buildAsync(), // | ||
| Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(0)).buildAsync()); | ||
| } | ||
|
|
||
| MetadataRequestDeduplicationDecorator( | ||
| CloudProvider delegate, // | ||
| AsyncCache<CloudPath, CloudItemMetadata> cachedItemMetadataRequests, // | ||
| AsyncCache<ItemListEntry, CloudItemList> cachedItemListRequests) { | ||
| this.delegate = delegate; | ||
| this.cachedItemMetadataRequests = cachedItemMetadataRequests; | ||
| this.cachedItemListRequests = cachedItemListRequests; | ||
| } | ||
|
|
||
| @Override | ||
| public CloudProvider delegate() { | ||
| return delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletionStage<CloudItemMetadata> itemMetadata(CloudPath node) { | ||
| return cachedItemMetadataRequests.get(node, (key, executor) -> delegate.itemMetadata(key).toCompletableFuture()); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletionStage<CloudItemList> list(CloudPath folder, Optional<String> pageToken) { | ||
| var entry = new ItemListEntry(folder, pageToken); | ||
| return cachedItemListRequests.get(entry, (key, executor) -> delegate.list(key.path, key.pageToken).toCompletableFuture()); | ||
| } | ||
|
|
||
| record ItemListEntry(CloudPath path, Optional<String> pageToken) { | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
src/main/java/org/cryptomator/cloudaccess/requestdecorator/QuotaRequestCachingDecorator.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,51 @@ | ||
| package org.cryptomator.cloudaccess.requestdecorator; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.AsyncCache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import org.cryptomator.cloudaccess.api.CloudPath; | ||
| import org.cryptomator.cloudaccess.api.CloudProvider; | ||
| import org.cryptomator.cloudaccess.api.Quota; | ||
| import org.cryptomator.cloudaccess.api.exceptions.NotFoundException; | ||
| import org.cryptomator.cloudaccess.api.exceptions.QuotaNotAvailableException; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| /** | ||
| * Decorates an existing CloudProvider by caching quota-requests for a duration of default 10 seconds (can be set using <code>org.cryptomator.cloudaccess.metadatacachingprovider.timeoutSeconds</code>). | ||
| */ | ||
| class QuotaRequestCachingDecorator implements CloudProviderDecorator { | ||
|
|
||
| private final static int DEFAULT_CACHE_TIMEOUT_SECONDS = 10; | ||
|
|
||
| // visible for testing | ||
| final AsyncCache<CloudPath, Quota> quotaCache; | ||
|
|
||
| private final CloudProvider delegate; | ||
|
|
||
| public QuotaRequestCachingDecorator(CloudProvider delegate) { | ||
| this(delegate, Caffeine | ||
| .newBuilder() | ||
| .expireAfterWrite(Duration.ofSeconds(Integer.getInteger("org.cryptomator.cloudaccess.metadatacachingprovider.timeoutSeconds", DEFAULT_CACHE_TIMEOUT_SECONDS))) | ||
|
overheadhunter marked this conversation as resolved.
|
||
| .buildAsync()); | ||
| } | ||
|
|
||
| QuotaRequestCachingDecorator(CloudProvider delegate, AsyncCache<CloudPath, Quota> quotaCache) { | ||
| this.delegate = delegate; | ||
| this.quotaCache = quotaCache; | ||
| } | ||
|
|
||
| @Override | ||
| public CloudProvider delegate() { | ||
| return delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletionStage<Quota> quota(CloudPath folder) { | ||
| return quotaCache.get(folder, k -> delegate.quota(k).whenComplete((metadata, throwable) -> { | ||
| if (throwable != null && !(throwable instanceof NotFoundException) && !(throwable instanceof QuotaNotAvailableException)) { | ||
| quotaCache.synchronous().invalidate(folder); | ||
| } | ||
| }).toCompletableFuture().join()); | ||
|
SailReal marked this conversation as resolved.
|
||
| } | ||
| } | ||
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
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.