-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add support for cache tiering #20
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/textproto" | ||
| "os" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/alecthomas/errors" | ||
|
|
||
| "github.com/block/sfptc/internal/logging" | ||
| ) | ||
|
|
||
| // The Tiered cache combines multiple caches. | ||
| // | ||
| // It is not directly selectable from configuration, but instead is automatically used if multiple caches are | ||
| // configured. | ||
| type Tiered struct { | ||
| caches []Cache | ||
| } | ||
|
|
||
| // MaybeNewTiered creates a [Tiered] cache if multiple are provided, or if there is only one it will return that cache. | ||
| // | ||
| // If no caches are passed it will panic. | ||
| func MaybeNewTiered(ctx context.Context, caches []Cache) Cache { | ||
| logging.FromContext(ctx).InfoContext(ctx, "Constructing tiered cache", "tiers", len(caches)) | ||
| if len(caches) == 0 { | ||
| panic("Tiered cache requires at least one backing cache") | ||
| } | ||
| if len(caches) == 1 { | ||
| return caches[0] | ||
| } | ||
| return Tiered{caches} | ||
| } | ||
|
|
||
| var _ Cache = (*Tiered)(nil) | ||
|
|
||
| // Close all underlying caches. | ||
| func (t Tiered) Close() error { | ||
| wg := sync.WaitGroup{} | ||
| errs := make([]error, len(t.caches)) | ||
| for i, cache := range t.caches { | ||
| wg.Go(func() { errs[i] = errors.WithStack(cache.Close()) }) | ||
| } | ||
| wg.Wait() | ||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| // Create a new object. All underlying caches will be written to in sequence. | ||
| func (t Tiered) Create(ctx context.Context, key Key, headers textproto.MIMEHeader, ttl time.Duration) (io.WriteCloser, error) { | ||
| // The first error will cancel all outstanding writes. | ||
| ctx, cancel := context.WithCancelCause(ctx) | ||
|
|
||
| tw := tieredWriter{make([]io.WriteCloser, len(t.caches)), cancel} | ||
| // Note: we can't use errgroup here because we do not want to cancel the context on Wait(). | ||
| wg := sync.WaitGroup{} | ||
| for i, cache := range t.caches { | ||
| wg.Go(func() { | ||
| w, err := cache.Create(ctx, key, headers, ttl) | ||
| if err != nil { | ||
| cancel(err) | ||
| } | ||
| tw.writers[i] = w | ||
| }) | ||
| } | ||
| done := make(chan struct{}) | ||
| go func() { wg.Wait(); close(done) }() | ||
| select { | ||
| case <-done: | ||
| return tw, nil | ||
|
|
||
| case <-ctx.Done(): | ||
| return nil, errors.WithStack(context.Cause(ctx)) | ||
| } | ||
| } | ||
|
|
||
| // Delete from all underlying caches. All errors are returned. | ||
| func (t Tiered) Delete(ctx context.Context, key Key) error { | ||
| wg := sync.WaitGroup{} | ||
| errs := make([]error, len(t.caches)) | ||
| for i, cache := range t.caches { | ||
| wg.Go(func() { errs[i] = errors.WithStack(cache.Delete(ctx, key)) }) | ||
| } | ||
| wg.Wait() | ||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| // Open returns a reader from the first cache that succeeds. | ||
| // | ||
| // If all caches fail, all errors are returned. | ||
| func (t Tiered) Open(ctx context.Context, key Key) (io.ReadCloser, textproto.MIMEHeader, error) { | ||
| errs := make([]error, len(t.caches)) | ||
| for i, c := range t.caches { | ||
| r, headers, err := c.Open(ctx, key) | ||
| errs[i] = err | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| continue | ||
| } else if err != nil { | ||
| return nil, nil, errors.WithStack(err) | ||
| } | ||
| return r, headers, nil | ||
| } | ||
| return nil, nil, errors.Join(errs...) | ||
| } | ||
|
|
||
| func (t Tiered) String() string { | ||
| names := make([]string, len(t.caches)) | ||
| for i, c := range t.caches { | ||
| names[i] = c.String() | ||
| } | ||
| return "tiered:" + strings.Join(names, ",") | ||
| } | ||
|
|
||
| type tieredWriter struct { | ||
| writers []io.WriteCloser | ||
| cancel context.CancelCauseFunc | ||
| } | ||
|
|
||
| var _ io.WriteCloser = (*tieredWriter)(nil) | ||
|
|
||
| // Close all writers and return all errors. | ||
| func (t tieredWriter) Close() error { | ||
| wg := sync.WaitGroup{} | ||
| errs := make([]error, len(t.writers)) | ||
| for i, cache := range t.writers { | ||
| wg.Go(func() { errs[i] = errors.WithStack(cache.Close()) }) | ||
| } | ||
| wg.Wait() | ||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| func (t tieredWriter) Write(p []byte) (n int, err error) { | ||
| for _, cache := range t.writers { | ||
| n, err = cache.Write(p) | ||
| if err != nil { | ||
| if !errors.Is(err, context.Canceled) { | ||
| t.cancel(err) | ||
| } | ||
| return n, errors.WithStack(err) | ||
| } | ||
| } | ||
| return | ||
| } |
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,23 @@ | ||
| package cache_test | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/alecthomas/assert/v2" | ||
|
|
||
| "github.com/block/sfptc/internal/cache" | ||
| "github.com/block/sfptc/internal/cache/cachetest" | ||
| "github.com/block/sfptc/internal/logging" | ||
| ) | ||
|
|
||
| func TestTiered(t *testing.T) { | ||
| cachetest.Suite(t, func(t *testing.T) cache.Cache { | ||
| _, ctx := logging.Configure(t.Context(), logging.Config{}) | ||
| memory, err := cache.NewMemory(ctx, cache.MemoryConfig{LimitMB: 1024, MaxTTL: time.Hour}) | ||
| assert.NoError(t, err) | ||
| disk, err := cache.NewDisk(ctx, cache.DiskConfig{Root: t.TempDir(), LimitMB: 1024, MaxTTL: time.Hour}) | ||
| assert.NoError(t, err) | ||
| return cache.MaybeNewTiered(ctx, []cache.Cache{memory, disk}) | ||
| }) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ github-releases { | |
| private-orgs = ["alecthomas"] | ||
| } | ||
|
|
||
| memory {} | ||
|
|
||
| disk { | ||
| root = "./cache" | ||
| } | ||
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.
Example of tiering.