-
Notifications
You must be signed in to change notification settings - Fork 72
[Feature] Parallel Executor #1916
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // DISCLAIMER | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // Copyright 2025 ArangoDB GmbH, Cologne, Germany | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||
| // You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||
| // limitations under the License. | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // Copyright holder is ArangoDB GmbH, Cologne, Germany | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| package executor | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "github.com/rs/zerolog" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| "github.com/arangodb/kube-arangodb/pkg/logging" | ||||||||||||||||||||||||||||||||
| "github.com/arangodb/kube-arangodb/pkg/util/errors" | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func Run(ctx context.Context, log logging.Logger, threads int, f RunFunc) error { | ||||||||||||||||||||||||||||||||
| h := &handler{ | ||||||||||||||||||||||||||||||||
| th: NewThreadManager(threads), | ||||||||||||||||||||||||||||||||
| completed: make(chan struct{}), | ||||||||||||||||||||||||||||||||
| log: log, | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| go h.run(ctx, f) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return h.Wait() | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| type RunFunc func(ctx context.Context, log logging.Logger, t Thread, h Handler) error | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| type Executor interface { | ||||||||||||||||||||||||||||||||
| Completed() bool | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Wait() error | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| type Handler interface { | ||||||||||||||||||||||||||||||||
| RunAsync(ctx context.Context, f RunFunc) Executor | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| WaitForSubThreads(t Thread) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| type handler struct { | ||||||||||||||||||||||||||||||||
| lock sync.Mutex | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| th ThreadManager | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| handlers []*handler | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| completed chan struct{} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| log logging.Logger | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| err error | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func (h *handler) WaitForSubThreads(t Thread) { | ||||||||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||||||||
| t.Release() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if h.subThreadsCompleted() { | ||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| time.Sleep(10 * time.Millisecond) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+75
to
+82
|
||||||||||||||||||||||||||||||||
| for { | |
| t.Release() | |
| if h.subThreadsCompleted() { | |
| return | |
| } | |
| time.Sleep(10 * time.Millisecond) | |
| t.Release() | |
| h.lock.Lock() | |
| defer h.lock.Unlock() | |
| for !h.subThreadsCompleted() { | |
| h.cond.Wait() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // | ||
| // DISCLAIMER | ||
| // | ||
| // Copyright 2025 ArangoDB GmbH, Cologne, Germany | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
| // | ||
|
|
||
| package executor | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/arangodb/kube-arangodb/pkg/logging" | ||
| ) | ||
|
|
||
| func Test_Executor(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| logger := logging.Global().RegisterAndGetLogger("test", logging.Trace) | ||
|
|
||
| require.NoError(t, Run(ctx, logger, 1, func(ctx context.Context, log logging.Logger, th Thread, h Handler) error { | ||
| log.Info("Start main thread") | ||
| defer log.Info("Complete main thread") | ||
|
|
||
| h.RunAsync(ctx, func(ctx context.Context, log logging.Logger, th Thread, h Handler) error { | ||
| log.Info("Start second thread") | ||
| defer log.Info("Complete second thread") | ||
|
|
||
| h.RunAsync(ctx, func(ctx context.Context, log logging.Logger, th Thread, h Handler) error { | ||
| log.Info("Start third thread") | ||
| defer log.Info("Complete third thread") | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| h.WaitForSubThreads(th) | ||
|
|
||
| return nil | ||
| })) | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||||||||||||
| // | ||||||||||||||||||
| // DISCLAIMER | ||||||||||||||||||
| // | ||||||||||||||||||
| // Copyright 2025 ArangoDB GmbH, Cologne, Germany | ||||||||||||||||||
| // | ||||||||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||||||||
| // You may obtain a copy of the License at | ||||||||||||||||||
| // | ||||||||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||
| // | ||||||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||||||
| // limitations under the License. | ||||||||||||||||||
| // | ||||||||||||||||||
| // Copyright holder is ArangoDB GmbH, Cologne, Germany | ||||||||||||||||||
| // | ||||||||||||||||||
|
|
||||||||||||||||||
| package executor | ||||||||||||||||||
|
|
||||||||||||||||||
| import "sync" | ||||||||||||||||||
|
|
||||||||||||||||||
| func NewThreadManager(threads int) ThreadManager { | ||||||||||||||||||
|
||||||||||||||||||
| func NewThreadManager(threads int) ThreadManager { | |
| func NewThreadManager(threads int) ThreadManager { | |
| if threads <= 0 { | |
| panic("threads must be a positive integer") | |
| } |
Copilot
AI
Jun 23, 2025
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.
Wait sends the thread ID back to the channel without checking the released flag, which can cause duplicate sends and inconsistent state. Consider guarding against multiple sends or updating the released flag.
Copilot
AI
Jun 23, 2025
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.
The Wait method unconditionally pushes the thread ID back into the channel without checking if it was already released, which can lead to duplicate IDs in the pool and potential deadlocks. Consider guarding against double-release or redesigning Wait to properly handle the acquire/release lifecycle.
Copilot
AI
Jun 23, 2025
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.
This unexported Wait method is never used; consider removing it to reduce dead code and improve clarity.
| func (t *thread) Wait() { | |
| t.lock.Lock() | |
| defer t.lock.Unlock() | |
| t.parent.threads <- t.id | |
| t.id = <-t.parent.threads | |
| } |
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.
[nitpick] Busy-wait loop with fixed Sleep may be inefficient; consider using a synchronization primitive like sync.Cond or a WaitGroup to avoid polling.