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
release-23.2: sql: add server.max_open_transactions_per_gateway cluster setting #118933
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package tests | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Test the server.max_open_transactions_per_gateway cluster setting. Only | ||
// non-admins are subject to the limit. | ||
func TestMaxOpenTxns(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
ctx := context.Background() | ||
srv, db, _ := serverutils.StartServer(t, base.TestServerArgs{}) | ||
defer srv.Stopper().Stop(ctx) | ||
s := srv.ApplicationLayer() | ||
|
||
rootSQLRunner := sqlutils.MakeSQLRunner(db) | ||
rootSQLRunner.Exec(t, "SET CLUSTER SETTING server.max_open_transactions_per_gateway = 4") | ||
rootSQLRunner.Exec(t, "CREATE USER testuser") | ||
|
||
testUserDB := s.SQLConn(t, serverutils.User("testuser")) | ||
|
||
tx1 := rootSQLRunner.Begin(t) | ||
_, err := testUserDB.Exec("SELECT 1") | ||
require.NoError(t, err) | ||
|
||
tx2 := rootSQLRunner.Begin(t) | ||
_, err = testUserDB.Exec("SELECT 1") | ||
require.NoError(t, err) | ||
|
||
tx3 := rootSQLRunner.Begin(t) | ||
_, err = testUserDB.Exec("SELECT 1") | ||
require.NoError(t, err) | ||
|
||
// After four transactions have been opened, testuser cannot run a query. | ||
tx4 := rootSQLRunner.Begin(t) | ||
_, err = testUserDB.Exec("SELECT 1") | ||
require.ErrorContains(t, err, "cannot execute operation due to server.max_open_transactions_per_gateway cluster setting") | ||
|
||
// testuser also cannot run anything in an explicit transaction. Starting the | ||
// transaction is allowed though. | ||
testuserTx, err := testUserDB.Begin() | ||
require.NoError(t, err) | ||
_, err = testuserTx.Exec("SELECT 1") | ||
require.ErrorContains(t, err, "cannot execute operation due to server.max_open_transactions_per_gateway cluster setting") | ||
require.NoError(t, testuserTx.Rollback()) | ||
|
||
// Increase the limit, allowing testuser to run queries in a transaction. | ||
rootSQLRunner.Exec(t, "SET CLUSTER SETTING server.max_open_transactions_per_gateway = 5") | ||
testuserTx, err = testUserDB.Begin() | ||
require.NoError(t, err) | ||
_, err = testuserTx.Exec("SELECT 1") | ||
require.NoError(t, err) | ||
|
||
// Lower the limit again, and verify that the setting applies to transactions | ||
// that were already open at the time the setting changed. | ||
rootSQLRunner.Exec(t, "SET CLUSTER SETTING server.max_open_transactions_per_gateway = 4") | ||
_, err = testuserTx.Exec("SELECT 2") | ||
require.ErrorContains(t, err, "cannot execute operation due to server.max_open_transactions_per_gateway cluster setting") | ||
require.NoError(t, testuserTx.Rollback()) | ||
|
||
// Making testuser admin should allow it to run queries. | ||
rootSQLRunner.Exec(t, "GRANT admin TO testuser") | ||
_, err = testUserDB.Exec("SELECT 1") | ||
require.NoError(t, err) | ||
|
||
testuserTx, err = testUserDB.Begin() | ||
require.NoError(t, err) | ||
_, err = testuserTx.Exec("SELECT 1") | ||
require.NoError(t, err) | ||
require.NoError(t, testuserTx.Rollback()) | ||
|
||
require.NoError(t, tx1.Commit()) | ||
require.NoError(t, tx2.Commit()) | ||
require.NoError(t, tx3.Commit()) | ||
require.NoError(t, tx4.Commit()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems a bit odd to use reporting metrics for a guardrail, but I suppose there is no major harm in it. I guess the alternative would be adding a new atomic counter somewhere, which would basically be the same thing?