Skip to content

* Added validation of WithTxControl option in non-interactive methods… #1738

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

rekby
Copy link
Member

@rekby rekby commented Apr 23, 2025

… of Client and Session

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Other information

Copy link

github-actions bot commented Apr 23, 2025

summary

Base version: v3.108.1 (master)
Suggested version: v3.108.2

@codecov-commenter
Copy link

codecov-commenter commented Apr 23, 2025

Codecov Report

Attention: Patch coverage is 45.16129% with 34 lines in your changes missing coverage. Please review.

Project coverage is 70.98%. Comparing base (4b3e883) to head (50f36af).

Files with missing lines Patch % Lines
internal/query/client.go 18.18% 12 Missing and 6 partials ⚠️
internal/query/session.go 11.11% 14 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1738      +/-   ##
==========================================
- Coverage   71.20%   70.98%   -0.23%     
==========================================
  Files         383      384       +1     
  Lines       39152    39212      +60     
==========================================
- Hits        27878    27833      -45     
- Misses      10148    10230      +82     
- Partials     1126     1149      +23     
Flag Coverage Δ
experiment 70.53% <45.16%> (-0.22%) ⬇️
go-1.21.x 70.92% <45.16%> (-0.17%) ⬇️
go-1.24.x 70.97% <45.16%> (-0.19%) ⬇️
integration 53.53% <33.87%> (-0.58%) ⬇️
macOS 38.26% <35.48%> (-0.04%) ⬇️
ubuntu 70.98% <45.16%> (-0.23%) ⬇️
unit 38.27% <35.48%> (-0.03%) ⬇️
windows 38.24% <35.48%> (-0.06%) ⬇️
ydb-24.1 51.31% <33.87%> (-0.55%) ⬇️
ydb-24.2 51.35% <33.87%> (-0.42%) ⬇️
ydb-24.3 51.86% <33.87%> (-0.42%) ⬇️
ydb-24.4 51.76% <33.87%> (-0.61%) ⬇️
ydb-25.1 53.43% <33.87%> (-0.22%) ⬇️
ydb-nightly 70.53% <45.16%> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rekby rekby force-pushed the issue-1737-deny-uncommitted-noninteractive branch from 3f3d26d to 813e20e Compare April 24, 2025 08:36
@rekby rekby requested a review from Copilot May 3, 2025 09:52
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds validation for the WithTxControl option in non‑interactive Client and Session methods to ensure that a proper commit flag accompanies the transaction control setting. Key changes include:

  • Updating tests to use SnapshotReadOnlyTxControl instead of SerializableReadWriteTxControl.
  • Adding a new check (checkTxControlWithCommit) in Session and Client methods.
  • Adjusting error handling and documentation for the newly added validation.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/integration/query_regression_test.go Updated tests to use SnapshotReadOnlyTxControl for transaction control validation.
internal/xreflect/is_nil.go & is_nil_test.go Added utility function and tests for nil pointer inspection used in transaction control logic.
internal/query/session.go Updated non‑interactive methods to call checkTxControlWithCommit before executing queries/exec.
internal/query/client.go Implemented checkTxControlWithCommit validation and updated error handling for WithTxControl.
examples/basic/native/query/series.go Updated example usage to apply SnapshotReadOnlyTxControl.
CHANGELOG.md Documented the added validation behavior and changes to transaction control options.

@@ -612,6 +638,15 @@ func New(ctx context.Context, cc grpc.ClientConnInterface, cfg *config.Config) *
}
}

// checkTxControlWithCommit checks that if WithTxControl is used, it must be with WithCommit
Copy link
Preview

Copilot AI May 3, 2025

Choose a reason for hiding this comment

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

Consider adding a detailed docstring for checkTxControlWithCommit to explain why non‑interactive methods require a commit flag and how txControl is validated.

Suggested change
// checkTxControlWithCommit checks that if WithTxControl is used, it must be with WithCommit
// checkTxControlWithCommit validates the transaction control object to ensure it includes a commit flag.
//
// Non-interactive methods require a commit flag because they execute operations that must be finalized
// to ensure data consistency and durability. Without the commit flag, the transaction would remain
// incomplete, potentially leading to undefined behavior or data loss.
//
// This function checks if the provided txControl object is non-nil and whether the Commit() method
// returns true. If the commit flag is missing, an error is returned to indicate that the transaction
// control is invalid for the intended operation.

Copilot uses AI. Check for mistakes.

@@ -32,6 +34,8 @@ var (
_ sessionPool = (*pool.Pool[*Session, Session])(nil)
)

var errNoCommit = xerrors.Wrap(errors.New("WithTxControl option is not allowed without CommitTx() option in Client methods, as these methods are non-interactive. You can either add the CommitTx() option to TxControl or use query.*TxControl methods (e.g., query.SnapshotReadOnlyTxControl) which already include the commit flag")) //nolint:lll
Copy link
Preview

Copilot AI May 3, 2025

Choose a reason for hiding this comment

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

Consider splitting the error message or adding inline comments to clarify the error rationale for future maintainers.

Suggested change
var errNoCommit = xerrors.Wrap(errors.New("WithTxControl option is not allowed without CommitTx() option in Client methods, as these methods are non-interactive. You can either add the CommitTx() option to TxControl or use query.*TxControl methods (e.g., query.SnapshotReadOnlyTxControl) which already include the commit flag")) //nolint:lll
// errNoCommit is raised when the WithTxControl option is used without the CommitTx() option.
// This is because Client methods are non-interactive and require explicit commit control.
// To resolve this, either add the CommitTx() option to TxControl or use query.*TxControl methods
// (e.g., query.SnapshotReadOnlyTxControl) that already include the commit flag.
var errNoCommit = xerrors.Wrap(errors.New("WithTxControl option is not allowed without CommitTx() option in Client methods."))
var errNoCommitResolution = xerrors.Wrap(errors.New("You can either add the CommitTx() option to TxControl or use query.*TxControl methods (e.g., query.SnapshotReadOnlyTxControl) which already include the commit flag")) //nolint:lll

Copilot uses AI. Check for mistakes.

@@ -25,7 +25,7 @@ func read(ctx context.Context, c query.Client, prefix string) error {
FROM
%s
`, "`"+path.Join(prefix, "series")+"`"),
query.WithTxControl(query.TxControl(query.BeginTx(query.WithSnapshotReadOnly()))),
query.WithTxControl(query.SnapshotReadOnlyTxControl()),
Copy link
Member Author

Choose a reason for hiding this comment

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

Pay attention to the line. I have changed the txcontrol in the example.

@@ -45,7 +45,7 @@ DECLARE $val AS UUID;
SELECT CAST($val AS Utf8)`,
query.WithIdempotent(),
query.WithParameters(ydb.ParamsBuilder().Param("$val").UUIDWithIssue1501Value(id).Build()),
query.WithTxControl(tx.SerializableReadWriteTxControl()),
query.WithTxControl(tx.SnapshotReadOnlyTxControl()),
Copy link
Member Author

Choose a reason for hiding this comment

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

Pay attention to the changes. I have changed txControl in tests

@@ -38,9 +38,9 @@ func TestDatabaseSqlWithTxControl(t *testing.T) {
ydb.WithTxControl(
tx.WithTxControlHook(ctx, func(txControl *tx.Control) {
hookCalled = true
require.Equal(t, tx.SerializableReadWriteTxControl(), txControl)
require.Equal(t, tx.SerializableReadWriteTxControl(tx.CommitTx()), txControl)
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 have added an explicit CommitTx to the test.

@rekby rekby requested a review from asmyasnikov May 3, 2025 12:50
CHANGELOG.md Outdated
@@ -1,3 +1,6 @@
* Added validation of WithTxControl option in non-interactive methods of Client and Session

=======
Copy link
Member

Choose a reason for hiding this comment

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

А это зачем?

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.

3 participants