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

refactor: changes to storage-related transaction handling within AutoOps packages #1365

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

kakcy
Copy link
Contributor

@kakcy kakcy commented Dec 4, 2024

This pull request includes significant changes to the AutoOpsService and ProgressiveRollout functionalities, focusing on refactoring the transaction handling mechanism. The primary change is the transition from using RunInTransaction to RunInTransactionV2, which simplifies the transaction management and improves code maintainability.

Part of #1252

@kakcy kakcy force-pushed the update-storage-client-auto-ops branch from db6b44e to 6e1ae57 Compare December 6, 2024 01:49
@kakcy kakcy marked this pull request as ready for review December 9, 2024 06:01
pkg/storage/v2/mysql/client.go Outdated Show resolved Hide resolved
pkg/storage/v2/mysql/client.go Show resolved Hide resolved
@@ -199,3 +207,29 @@ func (c *client) RunInTransaction(ctx context.Context, tx Transaction, f func()
}
return err
}

func (c *client) RunInTransactionV2(ctx *context.Context, f func(tx Transaction) error) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need to use pointer here, since context.Context is interface already, reference: https://github.com/bucketeer-io/bucketeer/blob/main/pkg/account/storage/v2/storage.go#L66-L73

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ubisoft-potato
Thank you for review 👍

In this PR, Transaction instances are managed using Context.WithValue().
https://github.com/bucketeer-io/bucketeer/blob/update-storage-client-auto-ops/pkg/storage/v2/mysql/client.go#L212-L216

Also, each MySQLClient side uses Context.Value with Client.Qe() in between to check whether there is a Transaction instance and switch the instance to execute the query.

Client.Qe()

ex: AutoOpsStorage.CreateAutoOpsRule()
https://github.com/bucketeer-io/bucketeer/blob/update-storage-client-auto-ops/pkg/autoops/api/api.go#L195

https://github.com/bucketeer-io/bucketeer/blob/update-storage-client-auto-ops/pkg/autoops/storage/v2/auto_ops_rule.go#L71

I think that if no pointer is passed, the context will not be updated and the transaction instance will not be saved.
As a result, I think transactions won't work and each query will be executed separately.

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi @kakcy.

The current implementation of RunInTransactionV2 takes a pointer to context.Context which is problematic for several reasons:

  1. Go Context Convention: Context should always be passed as a value, not as a pointer. This is a well-established Go convention and is documented in the context package.

  2. Immutability Pattern: Context is designed to follow an immutability pattern. Instead of modifying an existing context, you should create a new one and pass it down the call chain.

  3. Thread Safety: Modifying a context through a pointer could lead to race conditions in concurrent operations.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of passing tx here, we can pass ctx:

func (c *client) RunInTransactionV2(ctx context.Context, f func(ctx context.Context) error) error {

  ctx = context.WithValue(ctx, transactionKey, tx)

  if err = f(ctx); err == nil {
      err = tx.Commit()
  }

}

So we can still access the newset tx in the ctx.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @kakcy , I want to quote a small paragraph in https://www.oreilly.com/library/view/concurrency-in-go/9781491941294/ (it's a very good book)

image

This book has a chapter about context package, and it notes that we should not pass by reference but using the instance instead. As the book also states, go philosophy is "sharing by communicating, not communicating by sharing", so maybe we can find a way to not update the context ctx but using its tree-forked paradigm to share the transaction tx as @Ubisoft-potato stated?

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your detailed explanations.
I will modify it to match the philosophy of the Golang.

if err != nil {
return fmt.Errorf("account: begin tx: %w", err)
}
*ctx = context.WithValue(*ctx, transactionKey, tx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here:

ctx = context.WithValue(ctx, transactionKey, tx)

@kakcy
Copy link
Contributor Author

kakcy commented Jan 6, 2025

@Ubisoft-potato @hvn2k1
I've refactored it to pass a context containing a transaction to the callback, so please review it.
f9f281d

@Ubisoft-potato
Copy link
Collaborator

LGTM now! 👍

@hvn2k1
Copy link
Contributor

hvn2k1 commented Jan 8, 2025

LGTM too! Nice work!

Copy link
Contributor

@kentakozuka kentakozuka left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants