-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add draft of throttled copy #258
Open
bearrito
wants to merge
11
commits into
folbricht:master
Choose a base branch
from
bearrito:feature/throttle-copy
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
11 commits
Select commit
Hold shift + click to select a range
f1ac460
Add draft of throttled copy
80627b2
Try fix failing test
4e9f68e
Create limiting store
0fc9e60
Add more test + remove deadline for now
1ccde5b
Cleanup some of the test code duplication
0702186
Fix cache copy call
dd156c5
Add in timeouts
56497cc
Add throttle /w tests on Reads
3560177
Remove timeout + add http tests
681a6f5
Add integration tests with copy
eb52eb8
Remove test case
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 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 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 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,36 @@ | ||
package desync | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCopy(t *testing.T) { | ||
src_store_dir := t.TempDir() | ||
dst_store_dir := t.TempDir() | ||
|
||
src_store, err := NewLocalStore(src_store_dir, StoreOptions{}) | ||
require.NoError(t, err) | ||
|
||
dst_store, err := NewLocalStore(dst_store_dir, StoreOptions{}) | ||
require.NoError(t, err) | ||
|
||
first_chunk_data := []byte("some data") | ||
first_chunk := NewChunk(first_chunk_data) | ||
first_chunk_id := first_chunk.ID() | ||
|
||
src_store.StoreChunk(first_chunk) | ||
has_the_stored_chunk, _ := src_store.HasChunk(first_chunk_id) | ||
require.True(t, has_the_stored_chunk) | ||
|
||
chunks := make([]ChunkID, 1) | ||
chunks[0] = first_chunk_id | ||
|
||
Copy(context.Background(), chunks, src_store, dst_store, 1, NewProgressBar("")) | ||
require.NoError(t, err) | ||
has_the_chunk, _ := dst_store.HasChunk(first_chunk_id) | ||
|
||
require.True(t, has_the_chunk) | ||
} |
This file contains 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 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 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,94 @@ | ||
package desync | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
type ThrottleOptions struct { | ||
eventRate float64 | ||
burstRate int | ||
|
||
|
||
} | ||
|
||
|
||
type RateLimitedStore struct { | ||
|
||
wrappedStore WriteStore | ||
|
||
limiter *rate.Limiter | ||
|
||
options ThrottleOptions | ||
|
||
} | ||
|
||
var RateLimitedExceeded = errors.New("Rate Limit Exceeded") | ||
|
||
|
||
func NewRateLimitedStore(s WriteStore, options ThrottleOptions) *RateLimitedStore { | ||
|
||
limiter := rate.NewLimiter(rate.Limit(options.eventRate), options.burstRate) | ||
return &RateLimitedStore{wrappedStore: s,limiter: limiter, options: options } | ||
} | ||
|
||
func (s RateLimitedStore) GetChunk(id ChunkID) (*Chunk, error) { | ||
|
||
chunk,err := s.wrappedStore.GetChunk(id) | ||
if err != nil{ | ||
return chunk, err | ||
} | ||
ctx := context.Background() | ||
|
||
err = s.limiter.WaitN(ctx,1) | ||
return chunk, err | ||
} | ||
|
||
func (s RateLimitedStore) HasChunk(id ChunkID) (bool, error) { | ||
|
||
|
||
|
||
has,err := s.wrappedStore.HasChunk(id) | ||
if err != nil{ | ||
return has, err | ||
} | ||
ctx :=context.Background() | ||
err = s.limiter.WaitN(ctx,1) | ||
return has, err | ||
|
||
} | ||
|
||
|
||
func (s RateLimitedStore) StoreChunk(chunk *Chunk) error { | ||
|
||
// This isn't ideal because what I'm really interested is in size over the wire. | ||
_, err := chunk.Data() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
|
||
ctx := context.Background() | ||
|
||
|
||
|
||
err = s.limiter.WaitN(ctx,1) | ||
if err != nil { | ||
|
||
fmt.Println("Rate limit context error:", err) | ||
return RateLimitedExceeded | ||
} | ||
|
||
return s.wrappedStore.StoreChunk(chunk) | ||
|
||
} | ||
|
||
func (s RateLimitedStore) String() string { | ||
return s.wrappedStore.String() | ||
} | ||
|
||
|
||
func (s RateLimitedStore) Close() error { return nil } |
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.
I think you can actually achieve this by using a limit of "bytes per second" (must be equal or larger than max-chunk-size). The here in
StoreChunk()
you canIt's a little more interesting in
GetChunk()
since you don't know how large it is. One way would be toWaitN(ctx, <avg-chunk-size>)
before calling the wrapped store, orWaitN(ctx, <size of data>)
after actually pulling the chunk from the wrapped store. The latter can cause spikes early on when concurrency is used, but should average to the rate you set over time.As for
HasChunk()
, not sure if you really need to limit it, it's very small. You can just use1
like you have now if you want to limit it too, could do that before calling the wrappedStore.