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(i): Remove datastore.Txn from private db methods #2518

Merged
merged 2 commits into from
Apr 15, 2024

Conversation

nasdf
Copy link
Member

@nasdf nasdf commented Apr 12, 2024

Relevant issue(s)

Resolves #2517

Description

This PR removes the datastore.Txn parameter from private db methods.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

make test

Specify the platform(s) on which this was tested:

  • MacOS

@nasdf nasdf added the area/db-system Related to the core system related components of the DB label Apr 12, 2024
@nasdf nasdf added this to the DefraDB v0.11 milestone Apr 12, 2024
@nasdf nasdf self-assigned this Apr 12, 2024
@nasdf nasdf changed the title refactor(i): Remove txn params from private db methods refactor(i): Remove datastore.Txn from private db methods Apr 12, 2024
@nasdf nasdf requested a review from a team April 12, 2024 20:28
@@ -53,6 +53,11 @@ func ensureContextTxn(ctx context.Context, db transactionDB, readOnly bool) (con
return SetContextTxn(ctx, txn), txn, nil
}

// mustGetContextTxn returns the transaction from the context or panics.
func mustGetContextTxn(ctx context.Context) datastore.Txn {
return ctx.Value(txnContextKey{}).(datastore.Txn)
Copy link
Contributor

Choose a reason for hiding this comment

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

todo: This is a user input, and I think an error if not found would be much better than a panic - unless it is always guaranteed to exist at the point that this function is called, in which case please document this in this function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the docs to better reflect our usage pattern. It should be guaranteed to exist on any private function with db.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice - new doc changes look good, thanks Keenan :)

@@ -53,6 +53,11 @@ func ensureContextTxn(ctx context.Context, db transactionDB, readOnly bool) (con
return SetContextTxn(ctx, txn), txn, nil
}

// mustGetContextTxn returns the transaction from the context or panics.
func mustGetContextTxn(ctx context.Context) datastore.Txn {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: This feels like an odd place for such a function, why is this defined privately in db, instead of internal-public in core? Is the long term plan to move it, or to duplicate it? Or have we managed to remove all txn usage from all other packages?

Copy link
Member Author

Choose a reason for hiding this comment

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

From the standup discussion, our consensus was to use this only within the db package. Other APIs will still use txn params. I agree that it should move to core if we decide to expand it to other areas.

@@ -53,6 +53,11 @@ func ensureContextTxn(ctx context.Context, db transactionDB, readOnly bool) (con
return SetContextTxn(ctx, txn), txn, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Sorry I missed the PR in which this was added, would you mind noting that a new context.Context object may be returned from this function (in the case of implicit txn)? It is not clear from the existing documentation.

Something along the lines of:

... otherwise a new implicit transaction will be created, and returned within a new context object also containing the copied values from the input context.

I know this is expected with Go contexts, but it is important and thus worth being nice and explicit here IMO.

Copy link
Member

@jsimnz jsimnz Apr 15, 2024

Choose a reason for hiding this comment

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

Just adding some context here ;). A new context.Context will always be returned regardless of whether the implicit txn is created. Since the existing txn on the context is converted to an explicityTxn and then re-added to the context, thus creating a new context (since they are immutable).

With the exception of the error path

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

LGTM, thanks Keenan :)

@nasdf nasdf merged commit 7cb065b into sourcenetwork:develop Apr 15, 2024
26 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/db-system Related to the core system related components of the DB
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove txn parameter from private db funcs
3 participants