Repository navigation
feat: expose resolve storage status #261
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e714680
feat: expose resolve storage status
nicklasl 5178081
refactor: enrich resolve storage metadata
nicklasl 44145d8
test: cover future resolve fetch timestamp
nicklasl fb42633
docs: show context-aware storage check
nicklasl b8dc947
fix: snapshot resolve context
nicklasl d6d8bbf
feat: showcase storage status in demo app
nicklasl 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
52 changes: 52 additions & 0 deletions
52
Confidence/src/main/java/com/spotify/confidence/ResolveStorageStatus.kt
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,52 @@ | ||
| package com.spotify.confidence | ||
|
|
||
| import com.spotify.confidence.client.Clock | ||
| import java.util.Date | ||
|
|
||
| /** The status of the cached flag resolution on disk. */ | ||
| sealed interface ResolveStorageStatus { | ||
| /** No cached flag resolution is available. */ | ||
| object Empty : ResolveStorageStatus | ||
|
|
||
| /** The cached flag resolution is stale according to the supplied [ResolveStorageCheck]. */ | ||
| data class Stale(val lastFetchedAt: Date?) : ResolveStorageStatus | ||
|
|
||
| /** The cached flag resolution is fresh according to the supplied [ResolveStorageCheck]. */ | ||
| data class Fresh(val lastFetchedAt: Date?) : ResolveStorageStatus | ||
| } | ||
|
|
||
| /** Determines resolve storage status from its metadata. */ | ||
| fun interface ResolveStorageCheck { | ||
| /** Returns the storage status according to [metadata]. */ | ||
| fun check(metadata: ResolveStorageMetadata): ResolveStorageStatus | ||
| } | ||
|
|
||
| /** Metadata available when checking resolve storage. */ | ||
| data class ResolveStorageMetadata( | ||
| val isEmpty: Boolean, | ||
| val lastFetchedAt: Date?, | ||
| val context: Map<String, ConfidenceValue> | ||
| ) | ||
|
|
||
| /** Considers resolve storage stale when its fetch time is unknown or at least [maxAgeMillis] old. */ | ||
| class MaxAgeStorageCheck internal constructor( | ||
| val maxAgeMillis: Long, | ||
| private val clock: Clock | ||
| ) : ResolveStorageCheck { | ||
| constructor(maxAgeMillis: Long) : this(maxAgeMillis, Clock.CalendarBacked.systemUTC()) | ||
|
|
||
| init { | ||
| require(maxAgeMillis > 0) { "maxAgeMillis must be positive" } | ||
| } | ||
|
|
||
| override fun check(metadata: ResolveStorageMetadata): ResolveStorageStatus { | ||
| if (metadata.isEmpty) return ResolveStorageStatus.Empty | ||
| val fetchedAt = metadata.lastFetchedAt?.let { Date(it.time) } | ||
| val isStale = fetchedAt == null || clock.currentTime().time - fetchedAt.time >= maxAgeMillis | ||
| return if (isStale) { | ||
| ResolveStorageStatus.Stale(fetchedAt) | ||
| } else { | ||
| ResolveStorageStatus.Fresh(fetchedAt) | ||
| } | ||
| } | ||
| } |
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
26 changes: 26 additions & 0 deletions
26
Confidence/src/main/java/com/spotify/confidence/cache/ResolveStorageMetadataStorage.kt
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,26 @@ | ||
| package com.spotify.confidence.cache | ||
|
|
||
| import com.spotify.confidence.FlagResolution | ||
| import com.spotify.confidence.ResolveStorageMetadata | ||
| import kotlinx.serialization.Serializable | ||
| import java.util.Date | ||
|
|
||
| @Serializable | ||
| internal data class StoredResolveStorageMetadata( | ||
| val lastFetchedAtMillis: Long | ||
| ) | ||
|
|
||
| internal interface ResolveStorageMetadataStorage { | ||
| fun markFetched() | ||
|
|
||
| fun getLastFetchedAt(): Date? | ||
| } | ||
|
|
||
| internal fun DiskStorage.getResolveStorageMetadata(): ResolveStorageMetadata { | ||
| val resolution = read() | ||
| return ResolveStorageMetadata( | ||
| isEmpty = resolution == FlagResolution.EMPTY, | ||
| lastFetchedAt = (this as? ResolveStorageMetadataStorage)?.getLastFetchedAt()?.let { Date(it.time) }, | ||
| context = resolution.context.toMap() | ||
| ) | ||
| } |
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.
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.
if we have an interface for getting time then we could easily mock it in the tests.