-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Add shuffle scheduler #1218
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
a76e775
Add shuffle scheduler
hermanschaaf b3eb7e0
Add switch case
hermanschaaf 7c5f03f
Update comment
hermanschaaf 8643b60
Use random shuffle
hermanschaaf 27c4e22
Hash table names for random seed
hermanschaaf 15f8bf2
Merge branch 'main' into shuffle-scheduler
kodiakhq[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package scheduler | ||
|
|
||
| import ( | ||
| "context" | ||
| "hash/fnv" | ||
| "math/rand" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/cloudquery/plugin-sdk/v4/schema" | ||
| ) | ||
|
|
||
| func (s *syncClient) syncShuffle(ctx context.Context, resolvedResources chan<- *schema.Resource) { | ||
| // We have this because plugins can return sometimes clients in a random way which will cause | ||
| // differences between this run and the next one. | ||
| preInitialisedClients := make([][]schema.ClientMeta, len(s.tables)) | ||
| tableNames := make([]string, len(s.tables)) | ||
| for i, table := range s.tables { | ||
| tableNames[i] = table.Name | ||
| clients := []schema.ClientMeta{s.client} | ||
| if table.Multiplex != nil { | ||
| clients = table.Multiplex(s.client) | ||
| } | ||
| preInitialisedClients[i] = clients | ||
| // we do this here to avoid locks so we initial the metrics structure once in the main goroutines | ||
| // and then we can just read from it in the other goroutines concurrently given we are not writing to it. | ||
| s.metrics.initWithClients(table, clients) | ||
| } | ||
|
|
||
| // First interleave the tables like in round-robin | ||
| tableClients := roundRobinInterleave(s.tables, preInitialisedClients) | ||
|
|
||
| // Then shuffle the tableClients to randomize the order in which they are retrieved. | ||
| // We use a fixed seed so that runs with the same tables and clients perform similarly across syncs | ||
| // however, if the table order changes, the seed will change and the shuffle order will be different, | ||
| // so users have a little bit of control over the randomization. | ||
| seed := hashTableNames(tableNames) | ||
| shuffle(tableClients, seed) | ||
|
|
||
| var wg sync.WaitGroup | ||
| for _, tc := range tableClients { | ||
| table := tc.table | ||
| cl := tc.client | ||
| if err := s.scheduler.tableSems[0].Acquire(ctx, 1); err != nil { | ||
| // This means context was cancelled | ||
| wg.Wait() | ||
| return | ||
| } | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| defer s.scheduler.tableSems[0].Release(1) | ||
| // Not checking for error here as nothing much to do. | ||
| // the error is logged and this happens when context is cancelled. | ||
| // This currently uses the DFS algorithm to resolve the tables, but this | ||
| // may change in the future. | ||
| s.resolveTableDfs(ctx, table, cl, nil, resolvedResources, 1) | ||
| }() | ||
| } | ||
|
|
||
| // Wait for all the worker goroutines to finish | ||
| wg.Wait() | ||
| } | ||
|
|
||
| func hashTableNames(tableNames []string) int64 { | ||
| h := fnv.New32a() | ||
| h.Write([]byte(strings.Join(tableNames, ","))) | ||
| return int64(h.Sum32()) | ||
| } | ||
|
|
||
| func shuffle(tableClients []tableClient, seed int64) { | ||
| r := rand.New(rand.NewSource(seed)) | ||
| r.Shuffle(len(tableClients), func(i, j int) { | ||
| tableClients[i], tableClients[j] = tableClients[j], tableClients[i] | ||
| }) | ||
| } |
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.