-
Notifications
You must be signed in to change notification settings - Fork 208
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
fix: parallelize ensuring active pools on startup #1525
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -37,6 +37,7 @@ import ( | |
"github.com/hyperledger/firefly/pkg/blockchain" | ||
"github.com/hyperledger/firefly/pkg/core" | ||
"github.com/hyperledger/firefly/pkg/tokens" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type ConflictError struct { | ||
|
@@ -716,16 +717,31 @@ func (ft *FFTokens) handleNamespaceStarted(ctx context.Context, data fftypes.JSO | |
// Make sure any pools that are marked as active in our DB are indeed active | ||
namespace := data.GetString("namespace") | ||
log.L(ctx).Debugf("Token connector '%s' started namespace '%s'. Ensuring all token pools active.", ft.Name(), namespace) | ||
|
||
g, ctx := errgroup.WithContext(ctx) | ||
|
||
for _, pool := range ft.poolsToActivate[namespace] { | ||
if _, err := ft.EnsureTokenPoolActive(ctx, pool); err == nil { | ||
log.L(ctx).Debugf("Ensured token pool active '%s'", pool.ID) | ||
} else { | ||
// Log the error and continue trying to activate pools | ||
// At this point we've already started | ||
log.L(ctx).Errorf("Error ensuring token pool active '%s': %s", pool.ID, err.Error()) | ||
} | ||
currentPool := pool | ||
g.Go(func() error { | ||
_, err := ft.EnsureTokenPoolActive(ctx, currentPool) | ||
if err == nil { | ||
log.L(ctx).Debugf("Ensured token pool active '%s'", currentPool.ID) | ||
} else { | ||
log.L(ctx).Errorf("Error ensuring token pool active '%s': %s", currentPool.ID, err.Error()) | ||
} | ||
|
||
return err | ||
}) | ||
} | ||
|
||
// g.Wait waits for all goroutines activating token pools to complete | ||
// and returns the first non-nil error returned | ||
// by one of the goroutines. | ||
if err := g.Wait(); err != nil { | ||
// The above handleMessage will retry if errors occur | ||
return err | ||
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. This is a change in behavior - the previous It might be a positive change in behavior, but it would need some analysis/thinking (probably from @awrichar?) as to why the previous code intentionally did not fail on this condition before. 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. Yeah I thought about that, it felt weird to me that it didn't fail on startup... Would love to hear @awrichar thoughts on this one |
||
} | ||
|
||
return nil | ||
} | ||
|
||
|
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 feel like using `SetLimit() on the concurrency would be better than a completely unbounded parallelism against the token connector:
https://pkg.go.dev/golang.org/x/sync/errgroup#Group.SetLimit
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.
Good idea - will set that to the number of pools to activate