Skip to content

Commit 4810e59

Browse files
authored
Merge pull request cockroachdb#31417 from vilterp/backport2.1-31407
release-2.1: server(ccl)/ui: identify zone configs by zone name, not ID
2 parents 464baec + d7254ab commit 4810e59

File tree

7 files changed

+417
-280
lines changed

7 files changed

+417
-280
lines changed

pkg/ccl/serverccl/admin_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2018 The Cockroach Authors.
2+
//
3+
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
4+
// License (the "License"); you may not use this file except in compliance with
5+
// the License. You may obtain a copy of the License at
6+
//
7+
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
8+
9+
package serverccl
10+
11+
import (
12+
"context"
13+
"reflect"
14+
"strings"
15+
"testing"
16+
17+
"github.com/cockroachdb/cockroach/pkg/base"
18+
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
19+
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
20+
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
21+
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
22+
)
23+
24+
var adminPrefix = "/_admin/v1/"
25+
26+
// TestAdminAPIDataDistributionPartitioning partitions a table and verifies
27+
// that we see all zone configs (#27718).
28+
func TestAdminAPIDataDistributionPartitioning(t *testing.T) {
29+
defer leaktest.AfterTest(t)()
30+
31+
testCluster := serverutils.StartTestCluster(t, 3, base.TestClusterArgs{})
32+
defer testCluster.Stopper().Stop(context.Background())
33+
34+
firstServer := testCluster.Server(0)
35+
sqlDB := sqlutils.MakeSQLRunner(testCluster.ServerConn(0))
36+
37+
sqlDB.Exec(t, `CREATE DATABASE roachblog`)
38+
sqlDB.Exec(t, `USE roachblog`)
39+
sqlDB.Exec(t, `CREATE TABLE posts (id INT PRIMARY KEY, title text, body text)`)
40+
sqlDB.Exec(t, `CREATE TABLE comments (
41+
id INT,
42+
post_id INT REFERENCES posts,
43+
user_region STRING,
44+
body text,
45+
PRIMARY KEY (user_region, id)
46+
) PARTITION BY LIST (user_region) (
47+
PARTITION us VALUES IN ('US'),
48+
PARTITION eu VALUES IN ('EU'),
49+
PARTITION DEFAULT VALUES IN (default)
50+
)`)
51+
52+
// Create a zone config for each partition.
53+
// Would use locality constraints except this test cluster hasn't been started up with localities.
54+
sqlDB.Exec(t, `ALTER PARTITION us OF TABLE comments CONFIGURE ZONE USING gc.ttlseconds = 9001`)
55+
sqlDB.Exec(t, `ALTER PARTITION eu OF TABLE comments CONFIGURE ZONE USING gc.ttlseconds = 9002`)
56+
57+
// Assert that we get all roachblog zone configs back.
58+
expectedZoneConfigNames := map[string]struct{}{
59+
"roachblog.comments.eu": {},
60+
"roachblog.comments.us": {},
61+
}
62+
63+
var resp serverpb.DataDistributionResponse
64+
if err := serverutils.GetJSONProto(firstServer, adminPrefix+"data_distribution", &resp); err != nil {
65+
t.Fatal(err)
66+
}
67+
68+
actualZoneConfigNames := map[string]struct{}{}
69+
for name := range resp.ZoneConfigs {
70+
if strings.HasPrefix(name, "roachblog.") {
71+
actualZoneConfigNames[name] = struct{}{}
72+
}
73+
}
74+
if !reflect.DeepEqual(actualZoneConfigNames, expectedZoneConfigNames) {
75+
t.Fatalf("expected zone config names %v; got %v", expectedZoneConfigNames, actualZoneConfigNames)
76+
}
77+
}

pkg/ccl/serverccl/doc.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2018 The Cockroach Authors.
2+
//
3+
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
4+
// License (the "License"); you may not use this file except in compliance with
5+
// the License. You may obtain a copy of the License at
6+
//
7+
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
8+
9+
// Package serverccl houses tests that verify CCL behavior of a running CockroachDB server.
10+
package serverccl

pkg/ccl/serverccl/main_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 The Cockroach Authors.
2+
//
3+
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
4+
// License (the "License"); you may not use this file except in compliance with
5+
// the License. You may obtain a copy of the License at
6+
//
7+
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
8+
9+
package serverccl
10+
11+
import (
12+
"os"
13+
"testing"
14+
15+
_ "github.com/cockroachdb/cockroach/pkg/ccl"
16+
"github.com/cockroachdb/cockroach/pkg/ccl/utilccl"
17+
"github.com/cockroachdb/cockroach/pkg/security"
18+
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
19+
"github.com/cockroachdb/cockroach/pkg/server"
20+
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
21+
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
22+
"github.com/cockroachdb/cockroach/pkg/util/randutil"
23+
)
24+
25+
func TestMain(m *testing.M) {
26+
defer utilccl.TestingEnableEnterprise()()
27+
security.SetAssetLoader(securitytest.EmbeddedAssets)
28+
randutil.SeedForTests()
29+
serverutils.InitTestServerFactory(server.TestServerFactory)
30+
serverutils.InitTestClusterFactory(testcluster.TestClusterFactory)
31+
os.Exit(m.Run())
32+
}
33+
34+
//go:generate ../../util/leaktest/add-leaktest.sh *_test.go

pkg/server/admin.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ func (s *adminServer) DataDistribution(
14071407
) (*serverpb.DataDistributionResponse, error) {
14081408
resp := &serverpb.DataDistributionResponse{
14091409
DatabaseInfo: make(map[string]serverpb.DataDistributionResponse_DatabaseInfo),
1410-
ZoneConfigs: make(map[int64]serverpb.DataDistributionResponse_ZoneConfig),
1410+
ZoneConfigs: make(map[string]serverpb.DataDistributionResponse_ZoneConfig),
14111411
}
14121412

14131413
// Get ids and names for databases and tables.
@@ -1528,8 +1528,11 @@ func (s *adminServer) DataDistribution(
15281528

15291529
// Get zone configs.
15301530
// TODO(vilterp): this can be done in parallel with getting table/db names and replica counts.
1531-
zoneConfigsQuery := `SELECT zone_id, cli_specifier, config_sql, config_protobuf
1532-
FROM crdb_internal.zones WHERE cli_specifier IS NOT NULL`
1531+
zoneConfigsQuery := `
1532+
SELECT zone_name, config_sql, config_protobuf
1533+
FROM crdb_internal.zones
1534+
WHERE zone_name IS NOT NULL
1535+
`
15331536
rows2, _ /* cols */, err := s.server.internalExecutor.QueryWithUser(
15341537
ctx, "admin-replica-matrix", nil /* txn */, userName, zoneConfigsQuery,
15351538
)
@@ -1538,19 +1541,18 @@ func (s *adminServer) DataDistribution(
15381541
}
15391542

15401543
for _, row := range rows2 {
1541-
zcID := int64(tree.MustBeDInt(row[0]))
1542-
zcCliSpecifier := string(tree.MustBeDString(row[1]))
1543-
zcSQL := tree.MustBeDString(row[2])
1544-
zcBytes := tree.MustBeDBytes(row[3])
1544+
zoneName := string(tree.MustBeDString(row[0]))
1545+
zcSQL := tree.MustBeDString(row[1])
1546+
zcBytes := tree.MustBeDBytes(row[2])
15451547
var zcProto config.ZoneConfig
15461548
if err := protoutil.Unmarshal([]byte(zcBytes), &zcProto); err != nil {
15471549
return nil, s.serverError(err)
15481550
}
15491551

1550-
resp.ZoneConfigs[zcID] = serverpb.DataDistributionResponse_ZoneConfig{
1551-
CliSpecifier: zcCliSpecifier,
1552-
Config: zcProto,
1553-
ConfigSQL: string(zcSQL),
1552+
resp.ZoneConfigs[zoneName] = serverpb.DataDistributionResponse_ZoneConfig{
1553+
ZoneName: zoneName,
1554+
Config: zcProto,
1555+
ConfigSQL: string(zcSQL),
15541556
}
15551557
}
15561558

0 commit comments

Comments
 (0)