-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathgrant_option_migration.go
142 lines (130 loc) · 4.64 KB
/
grant_option_migration.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
// Copyright 2021 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 migrations
import (
"context"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/migration"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
)
// grantOptionMigration iterates through every descriptor and sets a user's grant option bits
// equal to its privilege bits if it holds the "GRANT" privilege
func grantOptionMigration(
ctx context.Context, _ clusterversion.ClusterVersion, d migration.TenantDeps, _ *jobs.Job,
) error {
query := `SELECT id, descriptor, crdb_internal_mvcc_timestamp FROM system.descriptor ORDER BY ID ASC`
rows, err := d.InternalExecutor.QueryIterator(
ctx, "update-grant-options", nil /* txn */, query,
)
if err != nil {
return err
}
addGrantOptionFunc := func(ids []descpb.ID, descs []descpb.Descriptor, timestamps []hlc.Timestamp) error {
var modifiedDescs []catalog.MutableDescriptor
for i, id := range ids {
b := catalogkv.NewBuilderWithMVCCTimestamp(&descs[i], timestamps[i])
if b == nil {
return errors.Newf("unable to find descriptor for id %d", id)
}
err := b.RunMigrationOnlyChanges(ctx, nil /* DescGetter */) // RunPostDeserializationChanges previously
if err != nil {
return err
}
mutableDesc := b.BuildExistingMutable()
//if mutableDesc.HasPostDeserializationChanges() {
modifiedDescs = append(modifiedDescs, mutableDesc)
//}
}
if err := writeModifiedDescriptors(ctx, d, modifiedDescs); err != nil {
return err
}
return nil
}
return addGrantOptionMigration(ctx, rows, addGrantOptionFunc, 1<<19)
}
// addGrantOptionFunction is used in addGrantOptionMigration to maybe add grant options
// of descriptors specified by the id.
type addGrantOptionFunction func(ids []descpb.ID, descs []descpb.Descriptor, timestamps []hlc.Timestamp) error
// addGrantOptionMigration is an abstraction for adding grant options.
// The rows provided should be the result of a select ID, descriptor, crdb_internal_mvcc_timestamp
// from system.descriptor table.
// The datums returned from the query are parsed to grab the descpb.Descriptor
// and addGrantOptionFunction is called on the desc.
// If minBatchSizeInBytes is specified, fixDescriptors will only be called once the
// size of the descriptors in the id array surpasses minBatchSizeInBytes.
func addGrantOptionMigration(
ctx context.Context,
rows sqlutil.InternalRows,
grantOptionFunc addGrantOptionFunction,
minBatchSizeInBytes int,
) error {
defer func() { _ = rows.Close() }()
ok, err := rows.Next(ctx)
if err != nil {
return err
}
currSize := 0 // in bytes.
var ids []descpb.ID
var descs []descpb.Descriptor
var timestamps []hlc.Timestamp
for ; ok; ok, err = rows.Next(ctx) {
if err != nil {
return err
}
datums := rows.Cur()
id, desc, ts, err := unmarshalDescFromDescriptorRow(datums)
if err != nil {
return err
}
ids = append(ids, id)
descs = append(descs, desc)
timestamps = append(timestamps, ts)
currSize += desc.Size()
if currSize > minBatchSizeInBytes || minBatchSizeInBytes == 0 {
err = grantOptionFunc(ids, descs, timestamps)
if err != nil {
return err
}
// Reset size and id array after the batch is fixed.
currSize = 0
ids = nil
descs = nil
timestamps = nil
}
}
// Fix remaining descriptors.
return grantOptionFunc(ids, descs, timestamps)
}
// writeModifiedDescriptors writes the descriptors that we have given grant option privileges
// to back to batch
func writeModifiedDescriptors(
ctx context.Context, d migration.TenantDeps, modifiedDescs []catalog.MutableDescriptor,
) error {
return d.CollectionFactory.Txn(ctx, d.InternalExecutor, d.DB, func(
ctx context.Context, txn *kv.Txn, descriptors *descs.Collection,
) error {
batch := txn.NewBatch()
for _, desc := range modifiedDescs {
err := catalogkv.WriteDescToBatch(ctx, false, d.Settings, batch, d.Codec, desc.GetID(), desc)
if err != nil {
return err
}
}
return txn.Run(ctx, batch)
})
}