-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
reader_catalog.go
270 lines (263 loc) · 10.1 KB
/
reader_catalog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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 replication
import (
"context"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/funcdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
)
// SetupOrAdvanceStandbyReaderCatalog sets up a reader catalog that reads from
// that tenant as of the passed timestamp -- potentially replacing the current
// reader catalog if needed -- or advances the current reader catalog to read as
// of that timestamp if that is possible to do in place by updating the ts and
// bumping the descriptor versions. In addition to mirroring all user-created
// catalog entries, this also updates the system.users, roles and role options,
// and database role settings tables in this cluster to read from those tables in
// the other cluster (and fails if they have incompatible indexes). Changes to
// the catalog are atomic.
func SetupOrAdvanceStandbyReaderCatalog(
ctx context.Context,
fromID roachpb.TenantID,
asOf hlc.Timestamp,
descsCol descs.DB,
st *cluster.Settings,
) error {
extracted, err := getCatalogForTenantAsOf(ctx, st, descsCol.KV(), fromID, asOf)
if err != nil {
return err
}
return descsCol.DescsTxn(
ctx, func(ctx context.Context, txn descs.Txn) error {
// Descriptors that no longer exist
descriptorsUpdated := catalog.DescriptorIDSet{}
namespaceUpdated := catalog.DescriptorIDSet{}
allExistingDescs, err := txn.Descriptors().GetAll(ctx, txn.KV())
if err != nil {
return err
}
// Resolve any existing descriptors within the tenant, which
// will be use to compute old values for writing.
b := txn.KV().NewBatch()
if err := extracted.ForEachDescriptor(func(desc catalog.Descriptor) error {
if !shouldSetupForReader(desc.GetID(), desc.GetParentID()) {
return nil
}
// Track this descriptor was updated.
descriptorsUpdated.Add(desc.GetID())
// If there is an existing descriptor with the same ID, we should
// determine the old bytes in storage for the upsert.
var existingRawBytes []byte
existingDesc, err := txn.Descriptors().MutableByID(txn.KV()).Desc(ctx, desc.GetID())
if err == nil {
existingRawBytes = existingDesc.GetRawBytesInStorage()
} else if errors.Is(err, catalog.ErrDescriptorNotFound) {
err = nil
} else {
return err
}
// Existing descriptor should never be a system descriptor.
if existingDesc != nil &&
existingDesc.GetParentID() == keys.SystemDatabaseID &&
desc.GetParentID() != keys.SystemDatabaseID {
return errors.AssertionFailedf("system database from replicated tenant " +
"and reader have a system table that collide, ensure both are created on the " +
"same version of CRDB.")
}
var mut catalog.MutableDescriptor
switch t := desc.DescriptorProto().GetUnion().(type) {
case *descpb.Descriptor_Table:
t.Table.Version = 1
var mutBuilder tabledesc.TableDescriptorBuilder
var mutTbl *tabledesc.Mutable
if existingRawBytes != nil {
t.Table.Version = existingDesc.GetVersion()
mutBuilder = existingDesc.NewBuilder().(tabledesc.TableDescriptorBuilder)
newDescBytes, err := protoutil.Marshal(t.Table)
if err != nil {
return err
}
if err := protoutil.Unmarshal(newDescBytes, mutBuilder.BuildExistingMutableTable().TableDesc()); err != nil {
return err
}
mutTbl = mutBuilder.BuildExistingMutableTable()
} else {
mutBuilder = tabledesc.NewBuilder(t.Table)
mutTbl = mutBuilder.BuildCreatedMutableTable()
}
mut = mutTbl
// Convert any physical tables into external row tables.
// Note: Materialized views will be converted, but their
// view definition will be wiped.
if mutTbl.IsPhysicalTable() {
mutTbl.ViewQuery = ""
mutTbl.SetExternalRowData(&descpb.ExternalRowData{TenantID: fromID, TableID: desc.GetID(), AsOf: asOf})
}
case *descpb.Descriptor_Database:
t.Database.Version = 1
var mutBuilder dbdesc.DatabaseDescriptorBuilder
if existingRawBytes != nil {
mutBuilder = existingDesc.NewBuilder().(dbdesc.DatabaseDescriptorBuilder)
newDescBytes, err := protoutil.Marshal(t.Database)
if err != nil {
return err
}
if err := protoutil.Unmarshal(newDescBytes, mutBuilder.BuildExistingMutableDatabase().DatabaseDesc()); err != nil {
return err
}
mut = mutBuilder.BuildExistingMutable()
} else {
mutBuilder = dbdesc.NewBuilder(t.Database)
mut = mutBuilder.BuildCreatedMutable()
}
case *descpb.Descriptor_Schema:
t.Schema.Version = 1
var mutBuilder schemadesc.SchemaDescriptorBuilder
if existingRawBytes != nil {
mutBuilder = existingDesc.NewBuilder().(schemadesc.SchemaDescriptorBuilder)
newDescBytes, err := protoutil.Marshal(t.Schema)
if err != nil {
return err
}
if err := protoutil.Unmarshal(newDescBytes, mutBuilder.BuildExistingMutableSchema().SchemaDesc()); err != nil {
return err
}
mut = mutBuilder.BuildExistingMutable()
} else {
mutBuilder = schemadesc.NewBuilder(t.Schema)
mut = mutBuilder.BuildCreatedMutable()
}
case *descpb.Descriptor_Function:
t.Function.Version = 1
var mutBuilder funcdesc.FunctionDescriptorBuilder
if existingRawBytes != nil {
mutBuilder = existingDesc.NewBuilder().(funcdesc.FunctionDescriptorBuilder)
newDescBytes, err := protoutil.Marshal(t.Function)
if err != nil {
return err
}
if err := protoutil.Unmarshal(newDescBytes, mutBuilder.BuildExistingMutableFunction().FuncDesc()); err != nil {
return err
}
mut = mutBuilder.BuildExistingMutable()
} else {
mutBuilder = funcdesc.NewBuilder(t.Function)
mut = mutBuilder.BuildCreatedMutable()
}
case *descpb.Descriptor_Type:
t.Type.Version = 1
var mutBuilder typedesc.TypeDescriptorBuilder
if existingRawBytes != nil {
mutBuilder = existingDesc.NewBuilder().(typedesc.TypeDescriptorBuilder)
newDescBytes, err := protoutil.Marshal(t.Type)
if err != nil {
return err
}
if err := protoutil.Unmarshal(newDescBytes, mutBuilder.BuildExistingMutableType()); err != nil {
return err
}
mut = mutBuilder.BuildExistingMutable()
} else {
mutBuilder = typedesc.NewBuilder(t.Type)
mut = mutBuilder.BuildCreatedMutable()
}
}
return errors.Wrapf(txn.Descriptors().WriteDescToBatch(ctx, true, mut, b),
"unable to create replicated descriptor: %d %T", mut.GetID(), mut)
}); err != nil {
return err
}
if err := extracted.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
if !shouldSetupForReader(e.GetID(), e.GetParentID()) {
return nil
}
namespaceUpdated.Add(e.GetID())
return errors.Wrapf(txn.Descriptors().UpsertNamespaceEntryToBatch(ctx, true, e, b), "namespace entry %v", e)
}); err != nil {
return err
}
// Figure out which descriptors should be deleted.
if err := allExistingDescs.ForEachDescriptor(func(desc catalog.Descriptor) error {
// Skip descriptors that were updated above
if !shouldSetupForReader(desc.GetID(), desc.GetParentID()) ||
descriptorsUpdated.Contains(desc.GetID()) {
return nil
}
// Delete the descriptor from the batch
return errors.Wrapf(txn.Descriptors().DeleteDescToBatch(ctx, true, desc.GetID(), b),
"deleting descriptor")
}); err != nil {
return err
}
// Figure out which namespaces should be deleted.
if err := allExistingDescs.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
// Skip descriptors that were updated above
if !shouldSetupForReader(e.GetID(), e.GetParentID()) ||
descriptorsUpdated.Contains(e.GetID()) {
return nil
}
return errors.Wrapf(txn.Descriptors().DeleteNamespaceEntryToBatch(ctx, true, e, b),
"deleting namespace")
}); err != nil {
return err
}
return errors.Wrap(txn.KV().Run(ctx, b), "executing bach for updating catalog")
})
}
// shouldSetupForReader determines if a descriptor should be setup
// access via external row data.
func shouldSetupForReader(id descpb.ID, parentID descpb.ID) bool {
switch id {
case keys.UsersTableID, keys.RoleMembersTableID, keys.RoleOptionsTableID,
keys.DatabaseRoleSettingsTableID, keys.TableStatisticsTableID:
return true
default:
return parentID != keys.SystemDatabaseID &&
id != keys.SystemDatabaseID
}
}
// getCatalogForTenantAsOf reads the descriptors from a given tenant
// at the given timestamp.
func getCatalogForTenantAsOf(
ctx context.Context,
st *cluster.Settings,
db *kv.DB,
tenantID roachpb.TenantID,
asOf hlc.Timestamp,
) (all nstree.Catalog, _ error) {
cf := descs.NewBareBonesCollectionFactory(st, keys.MakeSQLCodec(tenantID))
err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
err := txn.SetFixedTimestamp(ctx, asOf)
if err != nil {
return err
}
descsCol := cf.NewCollection(ctx)
defer descsCol.ReleaseAll(ctx)
all, err = descsCol.GetAllFromStorageUnvalidated(ctx, txn)
if err != nil {
return err
}
return nil
})
return all, err
}