-
Notifications
You must be signed in to change notification settings - Fork 4
Multithreading queue #108
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
base: main
Are you sure you want to change the base?
Multithreading queue #108
Changes from 10 commits
ee9888e
b1a0d1a
c6218d4
66dec99
f0cb595
bb2f201
c8abaad
701304f
125c590
27313d3
bf12c63
5ae1615
bdf7f77
c59f39c
534f3e4
fbfa225
2eed453
f78e4b8
d200bb4
f5539f1
dcf3ef2
d84c967
bd6c12e
5e06959
777f7b1
9fdb0c4
8bf2b0f
a89e0db
6f98006
529bffc
d539b51
f1179c5
e5abf22
5b4afbe
d4f2d41
1b44ad2
186dbf9
d5cd8df
c216924
b1637f7
bd31f2d
166706b
641f4ff
7198fe3
bbf3ada
52264ba
57fa87d
ebe4c04
a199413
4547d2e
bd39aa3
a1aa8ac
52c60ed
a70c565
c9a1a0b
7d35001
fbd533d
210c22c
28579bc
9130089
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 |
|---|---|---|
|
|
@@ -249,7 +249,13 @@ func (a *App) Start() error { | |
| return err | ||
| } | ||
|
|
||
| a.q = queue.NewQueue(a.wallet, cfg.QueueInterval) | ||
| refreshInterval := time.Second * time.Duration(cfg.QueueInterval) | ||
| a.q, err = queue.NewQueue(a.wallet, refreshInterval, cfg.QueueThreads) | ||
| if err != nil { | ||
| log.Error().Err(err).Msg("failed to initialize Queue module") | ||
| return err | ||
| } | ||
|
|
||
|
Comment on lines
+284
to
+302
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slice bound uses
Cast to -threadCount := cfg.QueueConfig.QueueThreads
-queueWallets := offsetWallets[:threadCount]
+threadCount := int(cfg.QueueConfig.QueueThreads)
+queueWallets := offsetWallets[:threadCount]
🤖 Prompt for AI Agents |
||
| go a.q.Listen() | ||
|
|
||
| prover := proofs.NewProver(a.wallet, a.q, a.fileSystem, cfg.ProofInterval, cfg.ProofThreads, int(params.ChunkSize)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,182 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package queue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "reflect" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "slices" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/JackalLabs/sequoia/config" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/cosmos/cosmos-sdk/types" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| walletTypes "github.com/desmos-labs/cosmos-go-wallet/types" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/desmos-labs/cosmos-go-wallet/wallet" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| storageTypes "github.com/jackalLabs/canine-chain/v4/x/storage/types" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/rs/zerolog/log" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var _ Queue = &Pool{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type Pool struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workers []*worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workerChannels []chan *Message // worker id should correspond to index of this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wallet *wallet.Wallet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func NewPool(wallet *wallet.Wallet, config config.QueueConfig) (*Pool, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workerWallets, err := initAuthClaimers(wallet, config.QueueThreads) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, errors.Join(errors.New("failed to initialize auth claimers"), err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workers, workerChannels := createWorkers(workerWallets, config.MaxRetryAttempt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if workers == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| panic("no workers created") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if workerChannels == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| panic("no worker channels created") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(workerChannels) != len(workers) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| panic("size of workers does not match size of worker channels") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pool := &Pool{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wallet: wallet, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workers: workers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workerChannels: workerChannels, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return pool, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Remove error return or add error handling. The function always returns -func NewPool(main *wallet.Wallet, queryClient storageTypes.QueryClient, workerWallets []*wallet.Wallet, config config.QueueConfig) (*Pool, error) {
+func NewPool(main *wallet.Wallet, queryClient storageTypes.QueryClient, workerWallets []*wallet.Wallet, config config.QueueConfig) *Pool {
// ... existing code ...
- return pool, nil
+ return pool
}🤖 Prompt for AI Agents
Comment on lines
+26
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Remove unused The If the parameter is not needed: -func NewPool(main *wallet.Wallet, queryClient storageTypes.QueryClient, workerWallets []*wallet.Wallet, config config.QueueConfig) (*Pool, error) {
+func NewPool(main *wallet.Wallet, workerWallets []*wallet.Wallet, config config.QueueConfig) (*Pool, error) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (p *Pool) Stop() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, c := range p.workerChannels { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close(c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (p *Pool) Listen() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, w := range p.workers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| go w.start() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (p *Pool) Add(msg types.Msg) (*Message, *sync.WaitGroup) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var wg sync.WaitGroup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wg.Add(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| m := &Message{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| msg: msg, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wg: &wg, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err: nil, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res: nil, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| msgIndex: -1, // no longer relevant(?) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ = p.sendToAny(m) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return m, &wg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (p *Pool) sendToAny(msg *Message) (workerId int) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set := make([]reflect.SelectCase, 0, len(p.workerChannels)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, ch := range p.workerChannels { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set = append(set, reflect.SelectCase{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dir: reflect.SelectSend, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Chan: reflect.ValueOf(ch), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Send: reflect.ValueOf(msg), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // blocks until a worker is free | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| to, _, _ := reflect.Select(set) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func createWorkers(workerWallets []*wallet.Wallet, maxRetryAttempt int8) ([]*worker, []chan *Message) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wChannels := make([]chan *Message, 0, len(workerWallets)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _ = range len(workerWallets) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wChannels = append(wChannels, make(chan *Message)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workers := make([]*worker, 0, len(workerWallets)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i, w := range workerWallets { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| worker := newWorker(int8(i), w, maxRetryAttempt, wChannels[i]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workers = append(workers, worker) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return workers, wChannels | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func initAuthClaimers(wallet *wallet.Wallet, count int8) (workerWallets []*wallet.Wallet, err error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query := &storageTypes.QueryProvider{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Address: wallet.AccAddress(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cl := storageTypes.NewQueryClient(wallet.Client.GRPCConn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res, err := cl.Provider(context.Background(), query) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, errors.Join(errors.New("unable to query provider auth claimers"), err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| claimers := res.Provider.AuthClaimers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i := range count { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workerWallet := newOffsetWallet(wallet, int(i)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !slices.Contains(claimers, workerWallet.AccAddress()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := addClaimer(wallet, workerWallet) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, errors.Join(errors.New("failed to add claimer on chain"), err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workerWallets = append(workerWallets, workerWallet) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return workerWallets, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func addClaimer(main *wallet.Wallet, claimer *wallet.Wallet) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| msg := storageTypes.NewMsgAddClaimer(main.AccAddress(), claimer.AccAddress()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| txData := walletTypes.NewTransactionData(msg).WithFeeAuto().WithGasAuto() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res, err := main.BroadcastTxCommit(txData) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errors.Join(errors.New("unable to broadcast MsgAddClaimer"), err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Info().Msg(res.TxHash) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func newOffsetWallet(main *wallet.Wallet, index int) *wallet.Wallet { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w, err := main.CloneWalletOffset(byte(index + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential byte overflow in wallet offset calculation. Casting -w, err := main.CloneWalletOffset(byte(index + 1))
+if index >= 255 {
+ panic("wallet index too large: maximum 254 supported")
+}
+w, err := main.CloneWalletOffset(byte(index + 1))📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| panic(err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return w | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func newBuffer(in <-chan *Message) <-chan *Message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // From: https://blogtitle.github.io/go-advanced-concurrency-patterns-part-4-unlimited-buffer-channels/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var buf []*Message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out := make(chan *Message) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer close(out) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for msg := range in { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| select { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case out <- msg: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buf = append(buf, msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, v := range buf { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out <- v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.