-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
multitenant: add SQL server startup guardrails #94973
Merged
+274
−33
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package serverccl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/clusterversion" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/settings/cluster" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval" | ||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
) | ||
|
||
// TestServerStartupGuardrails ensures that a SQL server will fail to start if | ||
// its binary version (TBV) is less than the tenant's logical version (TLV). | ||
func TestServerStartupGuardrails(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
v := func(major, minor int32) roachpb.Version { | ||
return roachpb.Version{Major: clusterversion.DevOffset + major, Minor: minor} | ||
} | ||
|
||
tests := []struct { | ||
storageBinaryVersion roachpb.Version | ||
storageBinaryMinSupportedVersion roachpb.Version | ||
tenantBinaryVersion roachpb.Version | ||
tenantBinaryMinSupportedVersion roachpb.Version | ||
TenantLogicalVersionKey clusterversion.Key | ||
expErrMatch string // empty if expecting a nil error | ||
}{ | ||
// First test case ensures that a tenant server can start if the server binary | ||
// version is not too low for the tenant logical version. | ||
{ | ||
storageBinaryVersion: v(22, 2), | ||
storageBinaryMinSupportedVersion: v(22, 1), | ||
tenantBinaryVersion: v(22, 2), | ||
tenantBinaryMinSupportedVersion: v(22, 2), | ||
TenantLogicalVersionKey: clusterversion.V22_2, | ||
expErrMatch: "", | ||
}, | ||
// Second test case ensures that a tenant server is prevented from starting if | ||
// its binary version is too low for the current tenant logical version. | ||
{ | ||
storageBinaryVersion: v(22, 2), | ||
storageBinaryMinSupportedVersion: v(22, 1), | ||
tenantBinaryVersion: v(22, 1), | ||
tenantBinaryMinSupportedVersion: v(21, 2), | ||
TenantLogicalVersionKey: clusterversion.V22_2, | ||
expErrMatch: fmt.Sprintf("preventing SQL server from starting because its binary version is too low for the tenant active version: "+ | ||
"server binary version = %v, tenant active version = %v", v(22, 1), v(22, 2)), | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
storageSettings := cluster.MakeTestingClusterSettingsWithVersions( | ||
test.storageBinaryVersion, | ||
test.storageBinaryMinSupportedVersion, | ||
false, /* initializeVersion */ | ||
) | ||
|
||
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{ | ||
// Disable the default test tenant, since we create one explicitly | ||
// below. | ||
DisableDefaultTestTenant: true, | ||
Settings: storageSettings, | ||
Knobs: base.TestingKnobs{ | ||
Server: &server.TestingKnobs{ | ||
BinaryVersionOverride: test.storageBinaryVersion, | ||
DisableAutomaticVersionUpgrade: make(chan struct{}), | ||
}, | ||
SQLEvalContext: &eval.TestingKnobs{ | ||
TenantLogicalVersionKeyOverride: test.TenantLogicalVersionKey, | ||
}, | ||
}, | ||
}) | ||
|
||
tenantSettings := cluster.MakeTestingClusterSettingsWithVersions( | ||
test.tenantBinaryVersion, | ||
test.tenantBinaryMinSupportedVersion, | ||
true, /* initializeVersion */ | ||
) | ||
|
||
// The tenant will be created with an active version equal to the version | ||
// corresponding to TenantLogicalVersionKey. Tenant creation is expected | ||
// to succeed for all test cases but server creation is expected to succeed | ||
// only if tenantBinaryVersion is at least equal to the version corresponding | ||
// to TenantLogicalVersionKey. | ||
tenantServer, err := s.StartTenant(context.Background(), | ||
base.TestTenantArgs{ | ||
Settings: tenantSettings, | ||
TenantID: serverutils.TestTenantID(), | ||
TestingKnobs: base.TestingKnobs{ | ||
Server: &server.TestingKnobs{ | ||
BinaryVersionOverride: test.tenantBinaryVersion, | ||
DisableAutomaticVersionUpgrade: make(chan struct{}), | ||
}, | ||
}, | ||
}) | ||
|
||
if !testutils.IsError(err, test.expErrMatch) { | ||
t.Fatalf("test %d: got error %s, wanted error matching '%s'", i, err, test.expErrMatch) | ||
} | ||
|
||
// Only attempt to stop the tenant if it was started successfully. | ||
if err == nil { | ||
tenantServer.Stopper().Stop(context.Background()) | ||
} | ||
s.Stopper().Stop(context.Background()) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -24,9 +24,9 @@ import ( | |
) | ||
|
||
// RowDecoder decodes rows from the settings table. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this is safe for concurrent use while you're here |
||
// It is safe for concurrent use. | ||
type RowDecoder struct { | ||
codec keys.SQLCodec | ||
alloc tree.DatumAlloc | ||
columns []catalog.Column | ||
decoder valueside.Decoder | ||
} | ||
|
@@ -45,17 +45,20 @@ func MakeRowDecoder(codec keys.SQLCodec) RowDecoder { | |
// present, the setting key will be returned but the value will be zero and the | ||
// tombstone bool will be set. | ||
func (d *RowDecoder) DecodeRow( | ||
kv roachpb.KeyValue, | ||
kv roachpb.KeyValue, alloc *tree.DatumAlloc, | ||
) (setting string, val settings.EncodedValue, tombstone bool, _ error) { | ||
// First we need to decode the setting name field from the index key. | ||
if alloc == nil { | ||
alloc = &tree.DatumAlloc{} | ||
} | ||
{ | ||
types := []*types.T{d.columns[0].GetType()} | ||
nameRow := make([]rowenc.EncDatum, 1) | ||
_, _, err := rowenc.DecodeIndexKey(d.codec, types, nameRow, nil, kv.Key) | ||
if err != nil { | ||
return "", settings.EncodedValue{}, false, errors.Wrap(err, "failed to decode key") | ||
} | ||
if err := nameRow[0].EnsureDecoded(types[0], &d.alloc); err != nil { | ||
if err := nameRow[0].EnsureDecoded(types[0], alloc); err != nil { | ||
return "", settings.EncodedValue{}, false, err | ||
} | ||
setting = string(tree.MustBeDString(nameRow[0].Datum)) | ||
|
@@ -70,7 +73,7 @@ func (d *RowDecoder) DecodeRow( | |
return "", settings.EncodedValue{}, false, err | ||
} | ||
|
||
datums, err := d.decoder.Decode(&d.alloc, bytes) | ||
datums, err := d.decoder.Decode(alloc, bytes) | ||
if err != nil { | ||
return "", settings.EncodedValue{}, false, err | ||
} | ||
|
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
Oops, something went wrong.
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.
@dt do you see any reason to not expose this like this?
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.
cockroach/pkg/clusterversion/clusterversion.go
Lines 241 to 244 in b258d20
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.
fwiw I did not end using
BinaryMinSupportedVersionKey
because it's different from the tenant creation min version but I decided to keep this, assuming we will unify them? I can remove it. We need the key specifically rather than the version because we map the key to the initial values we want from https://github.com/cockroachdb/cockroach/blob/af0beec136262540f2e777c1b52a93ad21ac7481/pkg/sql/catalog/bootstrap/previous_releases.go#L26There 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.
ah, I see. I guess what we could do is just write a test that asserts that the key here maps to the min binary version or something like that.
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.
and in a similar test (or in the same one) ensure that when BinaryVersionKey is passed to
ByKey
it returns the same version as binaryVersion?