-
Notifications
You must be signed in to change notification settings - Fork 6
Handling transient cases when reading a secret #1298
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
aperez-worklytics
wants to merge
7
commits into
rc-v0.6.7
Choose a base branch
from
connection_setup_fixes
base: rc-v0.6.7
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
7 commits
Select commit
Hold shift + click to select a range
2c2c53f
Handling reaload case
aperez-worklytics 54d4e6d
Adding transient cases
aperez-worklytics f8e47a9
More transient stuff
aperez-worklytics 5bf440e
Add missing AwsExceptionUtils (fixes CI compilation failure)
aperez-worklytics 372611c
Merge branch 'rc-v0.6.5' into connection_setup_fixes
aperez-worklytics 42f2988
Using right NonNull
aperez-worklytics 8a0e617
Feedback
aperez-worklytics 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
19 changes: 19 additions & 0 deletions
19
java/core/src/main/java/co/worklytics/psoxy/gateway/TransientConfigException.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,19 @@ | ||
| package co.worklytics.psoxy.gateway; | ||
|
|
||
| /** | ||
| * Signals that a config/secret backend had a transient failure (credential rotation, network | ||
| * blip, service hiccup) and the value may still be accessible on the next attempt. | ||
| * | ||
| * Distinct from a missing value ({@code Optional.empty()} / {@code NEGATIVE_VALUE}): callers | ||
| * should NOT treat this as "property not configured" — they should retry or serve a cached value. | ||
| */ | ||
| public class TransientConfigException extends RuntimeException { | ||
|
|
||
| public TransientConfigException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
|
|
||
| public TransientConfigException(String message) { | ||
| super(message); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -8,22 +8,53 @@ | |
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Ticker; | ||
| import com.google.common.cache.CacheBuilder; | ||
| import com.google.common.cache.CacheLoader; | ||
| import com.google.common.cache.LoadingCache; | ||
| import com.google.common.util.concurrent.Futures; | ||
| import com.google.common.util.concurrent.ListenableFuture; | ||
| import com.google.common.util.concurrent.UncheckedExecutionException; | ||
| import co.worklytics.psoxy.gateway.ConfigService; | ||
| import co.worklytics.psoxy.gateway.SecretStore; | ||
| import co.worklytics.psoxy.gateway.TransientConfigException; | ||
| import co.worklytics.psoxy.gateway.WritableConfigService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.NonNull; | ||
| import lombok.SneakyThrows; | ||
| import lombok.extern.java.Log; | ||
|
|
||
| import java.util.logging.Level; | ||
|
|
||
|
|
||
| @RequiredArgsConstructor | ||
| @Log | ||
| public class CachingConfigServiceDecorator implements WritableConfigService, SecretStore { | ||
|
|
||
| static final int MAX_TRANSIENT_RETRIES = 3; | ||
| static final long DEFAULT_TRANSIENT_RETRY_DELAY_MS = 500L; | ||
|
|
||
| final ConfigService delegate; | ||
| final Duration defaultTtl; | ||
| final Ticker ticker; | ||
| final long transientRetryDelayMs; | ||
|
|
||
| public CachingConfigServiceDecorator(ConfigService delegate, Duration defaultTtl) { | ||
| this(delegate, defaultTtl, Ticker.systemTicker(), DEFAULT_TRANSIENT_RETRY_DELAY_MS); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| CachingConfigServiceDecorator(ConfigService delegate, Duration defaultTtl, Ticker ticker) { | ||
| this(delegate, defaultTtl, ticker, DEFAULT_TRANSIENT_RETRY_DELAY_MS); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| CachingConfigServiceDecorator(ConfigService delegate, Duration defaultTtl, Ticker ticker, long transientRetryDelayMs) { | ||
| this.delegate = delegate; | ||
| this.defaultTtl = defaultTtl; | ||
| this.ticker = ticker; | ||
| this.transientRetryDelayMs = transientRetryDelayMs; | ||
| } | ||
|
|
||
| private volatile LoadingCache<ConfigProperty, String> cache; | ||
|
|
||
|
|
@@ -39,12 +70,55 @@ LoadingCache<ConfigProperty, String> getCache() { | |
| if (this.cache == null) { | ||
| this.cache = CacheBuilder.newBuilder() | ||
| .maximumSize(100) | ||
| .expireAfterWrite(defaultTtl.getSeconds(), TimeUnit.SECONDS) | ||
| .ticker(ticker) | ||
| .refreshAfterWrite(defaultTtl.getSeconds(), TimeUnit.SECONDS) | ||
|
Member
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. agree this is better. |
||
| .recordStats() | ||
| .build(new CacheLoader<ConfigProperty, String>() { //req for java8-backwards compatibility | ||
| @Override | ||
| public String load(ConfigProperty key) { | ||
| return delegate.getConfigPropertyAsOptional(key).orElse(NEGATIVE_VALUE); | ||
| public String load(@NonNull ConfigProperty key) { | ||
| TransientConfigException lastException = null; | ||
|
Comment on lines
77
to
+79
|
||
| for (int attempt = 0; attempt < MAX_TRANSIENT_RETRIES; attempt++) { | ||
| try { | ||
| return delegate.getConfigPropertyAsOptional(key).orElse(NEGATIVE_VALUE); | ||
| } catch (TransientConfigException e) { | ||
| lastException = e; | ||
| log.log(Level.WARNING, String.format("Transient failure on attempt {0}/{1} for config property {2}", | ||
| attempt + 1, MAX_TRANSIENT_RETRIES, key.name())); | ||
| } | ||
| try { | ||
| if (transientRetryDelayMs > 0) | ||
| Thread.sleep(transientRetryDelayMs); | ||
| } catch (InterruptedException ie) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new TransientConfigException("Config load for " + key.name() + " interrupted during retry", ie); | ||
| } | ||
| } | ||
| throw lastException; | ||
| } | ||
|
|
||
| @Override | ||
| public ListenableFuture<String> reload(@NonNull ConfigProperty key, @NonNull String oldValue) { | ||
| try { | ||
|
aperez-worklytics marked this conversation as resolved.
|
||
| String newValue = delegate.getConfigPropertyAsOptional(key).orElse(NEGATIVE_VALUE); | ||
| // Fallback heuristic for backends that still swallow exceptions | ||
| // (e.g. GCP SecretManagerConfigService): if the value was valid | ||
| // before but now comes back empty, assume transient and retain. | ||
| if (NEGATIVE_VALUE.equals(newValue) && !NEGATIVE_VALUE.equals(oldValue)) { | ||
| log.log(Level.WARNING, | ||
| "Backend returned empty for config property {0} which was previously set; assuming transient failure and retaining cached value", | ||
| key.name()); | ||
| return Futures.immediateFuture(oldValue); | ||
| } | ||
|
Comment on lines
+103
to
+111
|
||
| return Futures.immediateFuture(newValue); | ||
| } catch (TransientConfigException e) { | ||
| // Backend explicitly signalled a transient failure. | ||
| // Returning the old value resets the write-time so Guava waits a | ||
| // full TTL before retrying, rather than retrying on every request. | ||
| log.log(Level.WARNING, | ||
| "Transient failure reloading config property {0}; retaining cached value until next refresh cycle", | ||
| key.name()); | ||
| return Futures.immediateFuture(oldValue); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
@@ -85,8 +159,22 @@ public Optional<String> getConfigPropertyAsOptional(ConfigProperty property) { | |
| } else { | ||
| return Optional.of(value); | ||
| } | ||
| } catch (UncheckedExecutionException e) { | ||
| // Guava wraps RuntimeExceptions from load() in UncheckedExecutionException. | ||
| // TransientConfigException is a RuntimeException, so it lands here. | ||
| Throwable cause = e.getCause(); | ||
| if (cause instanceof TransientConfigException) { | ||
| // load() retried MAX_TRANSIENT_RETRIES times and still failed. Nothing was | ||
| // cached, so the next request will retry immediately. Re-throw so callers can | ||
| // distinguish a transient store outage from a genuinely missing property. | ||
| log.log(Level.WARNING, | ||
| "Transient backend failure for config property {0}; all retries exhausted", | ||
| property.name()); | ||
| throw (TransientConfigException) cause; | ||
| } | ||
| throw (cause instanceof RuntimeException) ? (RuntimeException) cause : e; | ||
| } catch (ExecutionException e) { | ||
| //unwrap if possible, re-throw | ||
| // Guava wraps checked exceptions from load() in ExecutionException. | ||
| if (e.getCause() == null) { | ||
| throw e; | ||
| } else { | ||
|
|
||
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
14 changes: 14 additions & 0 deletions
14
java/impl/aws/src/main/java/co/worklytics/psoxy/aws/AwsExceptionUtils.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,14 @@ | ||
| package co.worklytics.psoxy.aws; | ||
|
|
||
| import software.amazon.awssdk.awscore.exception.AwsServiceException; | ||
|
|
||
| class AwsExceptionUtils { | ||
|
|
||
| static boolean isAccessDenied(AwsServiceException e) { | ||
| if (e.awsErrorDetails() == null) { | ||
| return false; | ||
| } | ||
| String code = e.awsErrorDetails().errorCode(); | ||
| return code != null && (code.contains("AccessDenied") || code.contains("Forbidden")); | ||
| } | ||
| } |
Oops, something went wrong.
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.