Skip to content

Commit bd8ee15

Browse files
craig[bot]benbardinarulajmani
committed
109630: backupccl: Refactor allocateDescriptorRewrites in restore_planning.go r=benbardin a=benbardin backupccl: Refactor allocateDescriptorRewrites in restore_planning.go, for easier reasoning and more comprehensive unit-testability. Most of this code ought to be a no-op from the user perspective. There are, however, two changes: 1. Allocation of new DB IDs has been moved later, to make the algorithm clearer. Previously they were assigned earlier in the process, in a for-loop that was a bit surprising. 2. Descriptor objects for databases are no longer updated with SetName() prior to allocation. These updates were not persisted to the database, and so not passed to the processor. That meant we had one method for tracking such an update during planning, and another during processing. Now we use the same method - NewDBName on a rewrite object - for both. Note that this PR does not necessarily complete the unit-testing work here. Some of the new `remap*()` functions are themselves quite long, and can likely be broken up cogently; and all of them could use their own independent unit testing. Nonetheless this PR marks a good checkpoint. Release note: None Part of: https://cockroachlabs.atlassian.net/browse/CRDB-31024 110069: base: make TestValidateAddrs work without a network connection r=nvanbenschoten a=arulajmani Looking up an invalid host name may result in a different error depending on whether the test has access to a network connection or not. This patch modifies the test to catch that. Fixes: #109052 Release note: None Co-authored-by: Ben Bardin <[email protected]> Co-authored-by: Arul Ajmani <[email protected]>
3 parents c800303 + aa3882a + 693b7ac commit bd8ee15

File tree

5 files changed

+1022
-474
lines changed

5 files changed

+1022
-474
lines changed

pkg/base/addr_validation_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ func TestValidateAddrs(t *testing.T) {
6464
t.Fatal("expected port resolution failure, got no error")
6565
}
6666
portExpectedErr := err.Error()
67-
// For the host name resolution error we can reliably expect "no such host"
68-
// below, but before we test anything we need to ensure we indeed have
69-
// a reliably non-resolvable host name.
70-
_, err = net.DefaultResolver.LookupIPAddr(context.Background(), "nonexistent.example.com")
71-
if err == nil {
72-
t.Fatal("expected host resolution failure, got no error")
67+
// ValidateAddrs uses a net.DefaultResolver for host name resolution.
68+
// Normally, when given a non-existent host name, we expect it to return a
69+
// "no such host" error. However, it may return a different error if there is
70+
// no network access. See
71+
// https://github.com/cockroachdb/cockroach/issues/109052.
72+
noSuchHostErr := func(host string) string {
73+
_, err := net.DefaultResolver.LookupIPAddr(context.Background(), host)
74+
if err == nil { // the supplied host is expected to be reliably non-resolvable
75+
t.Fatal("expected host resolution failure, got no error")
76+
}
77+
return err.Error()
7378
}
7479

7580
// The test cases.
@@ -149,8 +154,8 @@ func TestValidateAddrs(t *testing.T) {
149154
{addrs{"localhost:-1231", "", "", "", "", ""}, "invalid port", addrs{}},
150155
{addrs{"localhost:nonexistent", "", "", "", "", ""}, portExpectedErr, addrs{}},
151156
// Invalid address.
152-
{addrs{"nonexistent.example.com:26257", "", "", "", "", ""}, "no such host", addrs{}},
153-
{addrs{"333.333.333.333:26257", "", "", "", "", ""}, "no such host", addrs{}},
157+
{addrs{"nonexistent.example.com:26257", "", "", "", "", ""}, noSuchHostErr("nonexistent.example.com"), addrs{}},
158+
{addrs{"333.333.333.333:26257", "", "", "", "", ""}, noSuchHostErr("333.333.333.333"), addrs{}},
154159
}
155160

156161
for i, test := range testData {

pkg/ccl/backupccl/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ go_library(
157157
"@com_github_gogo_protobuf//types",
158158
"@com_github_kr_pretty//:pretty",
159159
"@com_github_robfig_cron_v3//:cron",
160+
"@org_golang_x_exp//maps",
160161
],
161162
)
162163

@@ -265,12 +266,16 @@ go_test(
265266
"//pkg/sql/catalog/bootstrap",
266267
"//pkg/sql/catalog/catalogkeys",
267268
"//pkg/sql/catalog/catenumpb",
269+
"//pkg/sql/catalog/dbdesc",
268270
"//pkg/sql/catalog/descbuilder",
269271
"//pkg/sql/catalog/descpb",
270272
"//pkg/sql/catalog/descs",
271273
"//pkg/sql/catalog/desctestutils",
274+
"//pkg/sql/catalog/funcdesc",
275+
"//pkg/sql/catalog/schemadesc",
272276
"//pkg/sql/catalog/systemschema",
273277
"//pkg/sql/catalog/tabledesc",
278+
"//pkg/sql/catalog/typedesc",
274279
"//pkg/sql/execinfra",
275280
"//pkg/sql/execinfrapb",
276281
"//pkg/sql/importer",
@@ -339,6 +344,7 @@ go_test(
339344
"@com_github_robfig_cron_v3//:cron",
340345
"@com_github_stretchr_testify//assert",
341346
"@com_github_stretchr_testify//require",
347+
"@org_golang_x_exp//slices",
342348
"@org_golang_x_sync//errgroup",
343349
],
344350
)

pkg/ccl/backupccl/backup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10837,7 +10837,7 @@ $$;
1083710837
err = sql.TestingDescsTxn(ctx, tgtServer, func(ctx context.Context, txn isql.Txn, col *descs.Collection) error {
1083810838
dbDesc, err := col.ByNameWithLeased(txn.KV()).Get().Database(ctx, "db1")
1083910839
require.NoError(t, err)
10840-
require.Equal(t, 107, int(dbDesc.GetID()))
10840+
require.Equal(t, 123, int(dbDesc.GetID()))
1084110841

1084210842
scDesc, err := col.ByNameWithLeased(txn.KV()).Get().Schema(ctx, dbDesc, "sc1")
1084310843
require.NoError(t, err)
@@ -10861,7 +10861,7 @@ $$;
1086110861
fnDesc, err := col.ByIDWithLeased(txn.KV()).WithoutNonPublic().Get().Function(ctx, descpb.ID(udfID))
1086210862
require.NoError(t, err)
1086310863
require.Equal(t, 130, int(fnDesc.GetID()))
10864-
require.Equal(t, 107, int(fnDesc.GetParentID()))
10864+
require.Equal(t, 123, int(fnDesc.GetParentID()))
1086510865
require.Equal(t, 125, int(fnDesc.GetParentSchemaID()))
1086610866
// Make sure db name and IDs are rewritten in function body.
1086710867
require.Equal(t, "SELECT a FROM db1.sc1.tbl1;\nSELECT nextval(129:::REGCLASS);", fnDesc.GetFunctionBody())

0 commit comments

Comments
 (0)