Skip to content
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 retry support for serializable transactions #386

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/mls/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *Store) PublishIdentityUpdate(ctx context.Context, req *identity.Publish
return nil, errors.New("IdentityUpdate is required")
}

if err := s.RunInTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable}, func(ctx context.Context, txQueries *queries.Queries) error {
if err := s.RunInSerializableTx(ctx, 3, func(ctx context.Context, txQueries *queries.Queries) error {
inboxLogEntries, err := txQueries.GetAllInboxLogs(ctx, new_update.GetInboxId())
if err != nil {
return err
Expand Down Expand Up @@ -605,3 +605,20 @@ func (s *Store) RunInTx(
done = true
return tx.Commit()
}

func (s *Store) RunInSerializableTx(ctx context.Context, numRetries int, fn func(ctx context.Context, txQueries *queries.Queries) error) error {
var err error
for i := 0; i < numRetries; i++ {
select {
case <-ctx.Done():
Copy link
Contributor

@insipx insipx Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noob Q: What does this select on this ctx do?

The default case returns nil when it finishes succesfully, but ctx seems like some sort of global context? When would it trigger?

Copy link

@37ng 37ng Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you ctrl+c or the context is out of time, then ctx.Done() channel will receive a struct and this select block will return the error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. It just stops you from continuing retries when the server is in the process of shutting down

return ctx.Err()
default:
err = s.RunInTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable}, fn)
if err == nil {
return nil
}
s.log.Warn("Error in serializable tx", zap.Error(err))
}
}
return err
}
Loading