-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlliveness: embed current region into session ID
Follow up on #90408 and #91019. Previously, the sqlliveness subsystem was using enum.One when constructing the session ID since it doesn't have the regional data yet. This commit implements that missing part of it by ensuring that the regional representation is plumbed all the way to the sqlliveness subsystem whenever the SQL pod is started, and use it to construct the session ID. This will enable our REGIONAL BY ROW work to put the data in the right region. The multi-region enum for the system database will be read during startup for that to work. Since doing this already requires loading the system DB's descriptor (which indirectly tests whether the system DB has been bootstrapped) for the tenant, we can remove the call to checkTenantExists. Epic: None Release note: None
- Loading branch information
1 parent
979dfb0
commit 3685865
Showing
11 changed files
with
396 additions
and
87 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2022 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 multiregionccl_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/ccl/multiregionccl/multiregionccltestutils" | ||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"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" | ||
) | ||
|
||
// TestGetLocalityRegionEnumPhysicalRepresentation is in the ccl package since | ||
// it utilizes adding regions to a database. | ||
func TestGetLocalityRegionEnumPhysicalRepresentation(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
ctx := context.Background() | ||
tc, sqlDB, cleanup := multiregionccltestutils.TestingCreateMultiRegionCluster( | ||
t, 3 /* numServers */, base.TestingKnobs{}) | ||
defer cleanup() | ||
|
||
tDB := sqlutils.MakeSQLRunner(sqlDB) | ||
tDB.Exec(t, `CREATE DATABASE foo PRIMARY REGION "us-east1" REGIONS "us-east1", "us-east2", "us-east3"`) | ||
|
||
s0 := tc.ServerTyped(0) | ||
ief := s0.InternalExecutorFactory().(descs.TxnManager) | ||
dbID := sqlutils.QueryDatabaseID(t, sqlDB, "foo") | ||
|
||
t.Run("with locality that exists", func(t *testing.T) { | ||
regionEnum, err := sql.GetLocalityRegionEnumPhysicalRepresentation( | ||
ctx, ief, s0.DB(), descpb.ID(dbID), roachpb.Locality{ | ||
Tiers: []roachpb.Tier{{Key: "region", Value: "us-east2"}}, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
|
||
enumMembers := getEnumMembers(t, ctx, tc.Server(0), descpb.ID(dbID)) | ||
require.NotEmpty(t, enumMembers) | ||
require.Equal(t, enumMembers["us-east2"], regionEnum) | ||
}) | ||
|
||
t.Run("with non-existent locality", func(t *testing.T) { | ||
regionEnum, err := sql.GetLocalityRegionEnumPhysicalRepresentation( | ||
ctx, ief, s0.DB(), descpb.ID(dbID), roachpb.Locality{ | ||
Tiers: []roachpb.Tier{{Key: "region", Value: "europe-west1"}}, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
|
||
// Fallback to primary region if the locality is provided, but non-existent. | ||
enumMembers := getEnumMembers(t, ctx, tc.Server(0), descpb.ID(dbID)) | ||
require.NotEmpty(t, enumMembers) | ||
require.Equal(t, enumMembers["us-east1"], regionEnum) | ||
}) | ||
|
||
t.Run("without locality", func(t *testing.T) { | ||
regionEnum, err := sql.GetLocalityRegionEnumPhysicalRepresentation( | ||
ctx, ief, s0.DB(), descpb.ID(dbID), roachpb.Locality{}) | ||
require.NoError(t, err) | ||
|
||
// Fallback to primary region is locality information is missing. | ||
enumMembers := getEnumMembers(t, ctx, tc.Server(0), descpb.ID(dbID)) | ||
require.NotEmpty(t, enumMembers) | ||
require.Equal(t, enumMembers["us-east1"], regionEnum) | ||
}) | ||
} | ||
|
||
func getEnumMembers( | ||
t *testing.T, ctx context.Context, ts serverutils.TestServerInterface, dbID descpb.ID, | ||
) map[string][]byte { | ||
t.Helper() | ||
enumMembers := make(map[string][]byte) | ||
err := sql.TestingDescsTxn(ctx, ts, func(ctx context.Context, txn *kv.Txn, descsCol *descs.Collection) error { | ||
_, dbDesc, err := descsCol.GetImmutableDatabaseByID(ctx, txn, dbID, | ||
tree.DatabaseLookupFlags{Required: true}) | ||
require.NoError(t, err) | ||
regionEnumID, err := dbDesc.MultiRegionEnumID() | ||
require.NoError(t, err) | ||
regionEnumDesc, err := descsCol.GetImmutableTypeByID(ctx, txn, regionEnumID, | ||
tree.ObjectLookupFlags{CommonLookupFlags: tree.CommonLookupFlags{Required: true}}) | ||
require.NoError(t, err) | ||
for ord := 0; ord < regionEnumDesc.NumEnumMembers(); ord++ { | ||
enumMembers[regionEnumDesc.GetMemberLogicalRepresentation(ord)] = regionEnumDesc.GetMemberPhysicalRepresentation(ord) | ||
} | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
return enumMembers | ||
} |
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
Oops, something went wrong.