This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
forked from cockroachdb/cockroach
/
system.go
448 lines (418 loc) · 16.3 KB
/
system.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Marc Berhault (marc@cockroachlabs.com)
package sqlbase
import (
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/util/log"
"golang.org/x/net/context"
)
// sql CREATE commands and full schema for each system table.
// These strings are *not* used at runtime, but are checked by the
// `TestSystemTableLiterals` test that compares the table generated by
// evaluating the `CREATE TABLE` statement to the descriptor literal that is
// actually used at runtime.
const (
// NamespaceTableSchema is checked in TestSystemTables.
NamespaceTableSchema = `
CREATE TABLE system.namespace (
parentID INT,
name STRING,
id INT,
PRIMARY KEY (parentID, name)
);`
// DescriptorTableSchema is checked in TestSystemTables.
DescriptorTableSchema = `
CREATE TABLE system.descriptor (
id INT PRIMARY KEY,
descriptor BYTES
);`
// UsersTableSchema is checked in TestSystemTables.
UsersTableSchema = `
CREATE TABLE system.users (
username STRING PRIMARY KEY,
hashedPassword BYTES
);`
// ZonesTableSchema is checked in TestSystemTables.
// Zone settings per DB/Table.
ZonesTableSchema = `
CREATE TABLE system.zones (
id INT PRIMARY KEY,
config BYTES
);`
)
// These system tables are not part of the system config.
const (
// LeaseTableSchema is checked in TestSystemTables.
LeaseTableSchema = `
CREATE TABLE system.lease (
descID INT,
version INT,
nodeID INT,
expiration TIMESTAMP,
PRIMARY KEY (descID, version, expiration, nodeID)
);`
// EventLogTableSchema describes the schema of the event log table.
EventLogTableSchema = `
CREATE TABLE system.eventlog (
timestamp TIMESTAMP NOT NULL,
eventType STRING NOT NULL,
targetID INT NOT NULL,
reportingID INT NOT NULL,
info STRING,
uniqueID BYTES DEFAULT experimental_unique_bytes(),
PRIMARY KEY (timestamp, uniqueID)
);`
// RangeEventTableSchema defines the schema of the event log table. It is
// currently envisioned as a wide table; many different event types can be
// recorded to the table.
RangeEventTableSchema = `
CREATE TABLE system.rangelog (
timestamp TIMESTAMP NOT NULL,
rangeID INT NOT NULL,
storeID INT NOT NULL,
eventType STRING NOT NULL,
otherRangeID INT,
info STRING,
uniqueID INT DEFAULT unique_rowid(),
PRIMARY KEY (timestamp, uniqueID)
);`
// UITableSchema is checked in TestSystemTables.
// blobs based on unique keys.
UITableSchema = `
CREATE TABLE system.ui (
key STRING PRIMARY KEY,
value BYTES,
lastUpdated TIMESTAMP NOT NULL
);`
)
func pk(name string) IndexDescriptor {
return IndexDescriptor{
Name: "primary",
ID: 1,
Unique: true,
ColumnNames: []string{name},
ColumnDirections: singleASC,
ColumnIDs: singleID1,
}
}
// Helpers used to make some of the TableDescriptor literals below more concise.
var (
colTypeInt = ColumnType{Kind: ColumnType_INT}
colTypeString = ColumnType{Kind: ColumnType_STRING}
colTypeBytes = ColumnType{Kind: ColumnType_BYTES}
colTypeTimestamp = ColumnType{Kind: ColumnType_TIMESTAMP}
singleASC = []IndexDescriptor_Direction{IndexDescriptor_ASC}
singleID1 = []ColumnID{1}
)
// These system config TableDescriptor literals should match the descriptor
// that would be produced by evaluating one of the above `CREATE TABLE`
// statements. See the `TestSystemTableLiterals` which checks that they do
// indeed match, and has suggestions on writing and maintaining them.
var (
// SystemDB is the descriptor for the system database.
SystemDB = DatabaseDescriptor{
Name: "system",
ID: keys.SystemDatabaseID,
// Assign max privileges to root user.
Privileges: NewPrivilegeDescriptor(security.RootUser,
SystemConfigAllowedPrivileges[keys.SystemDatabaseID]),
}
// NamespaceTable is the descriptor for the namespace table.
NamespaceTable = TableDescriptor{
Name: "namespace",
ID: keys.NamespaceTableID,
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "parentID", ID: 1, Type: colTypeInt},
{Name: "name", ID: 2, Type: colTypeString},
{Name: "id", ID: 3, Type: colTypeInt, Nullable: true},
},
NextColumnID: 4,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"parentID", "name"}, ColumnIDs: []ColumnID{1, 2}},
{Name: "fam_3_id", ID: 3, ColumnNames: []string{"id"}, ColumnIDs: []ColumnID{3}, DefaultColumnID: 3},
},
NextFamilyID: 4,
PrimaryIndex: IndexDescriptor{
Name: "primary",
ID: 1,
Unique: true,
ColumnNames: []string{"parentID", "name"},
ColumnDirections: []IndexDescriptor_Direction{IndexDescriptor_ASC, IndexDescriptor_ASC},
ColumnIDs: []ColumnID{1, 2},
},
NextIndexID: 2,
Privileges: NewPrivilegeDescriptor(security.RootUser, SystemConfigAllowedPrivileges[2]),
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
// DescriptorTable is the descriptor for the descriptor table.
DescriptorTable = TableDescriptor{
Name: "descriptor",
ID: keys.DescriptorTableID,
Privileges: NewPrivilegeDescriptor(security.RootUser, SystemConfigAllowedPrivileges[3]),
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "id", ID: 1, Type: colTypeInt},
{Name: "descriptor", ID: 2, Type: colTypeBytes, Nullable: true},
},
NextColumnID: 3,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"id"}, ColumnIDs: singleID1},
{Name: "fam_2_descriptor", ID: 2, ColumnNames: []string{"descriptor"}, ColumnIDs: []ColumnID{2}, DefaultColumnID: 2},
},
PrimaryIndex: pk("id"),
NextFamilyID: 3,
NextIndexID: 2,
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
// UsersTable is the descriptor for the users table.
UsersTable = TableDescriptor{
Name: "users",
ID: keys.UsersTableID,
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "username", ID: 1, Type: colTypeString},
{Name: "hashedPassword", ID: 2, Type: colTypeBytes, Nullable: true},
},
NextColumnID: 3,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"username"}, ColumnIDs: singleID1},
{Name: "fam_2_hashedPassword", ID: 2, ColumnNames: []string{"hashedPassword"}, ColumnIDs: []ColumnID{2}, DefaultColumnID: 2},
},
PrimaryIndex: pk("username"),
NextFamilyID: 3,
NextIndexID: 2,
Privileges: NewPrivilegeDescriptor(security.RootUser, SystemConfigAllowedPrivileges[4]),
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
// ZonesTable is the descriptor for the zones table.
ZonesTable = TableDescriptor{
Name: "zones",
ID: keys.ZonesTableID,
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "id", ID: 1, Type: colTypeInt},
{Name: "config", ID: 2, Type: colTypeBytes, Nullable: true},
},
NextColumnID: 3,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"id"}, ColumnIDs: singleID1},
{Name: "fam_2_config", ID: 2, ColumnNames: []string{"config"}, ColumnIDs: []ColumnID{2}, DefaultColumnID: 2},
},
PrimaryIndex: pk("id"),
NextFamilyID: 3,
NextIndexID: 2,
Privileges: NewPrivilegeDescriptor(security.RootUser, SystemConfigAllowedPrivileges[5]),
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
// SystemConfigAllowedPrivileges describes the privileges allowed for each
// system config object. No user may have more than those privileges, and
// the root user must have exactly those privileges. CREATE|DROP|ALL
// should always be denied.
SystemConfigAllowedPrivileges = map[ID]privilege.List{
keys.SystemDatabaseID: privilege.ReadData,
keys.NamespaceTableID: privilege.ReadData,
keys.DescriptorTableID: privilege.ReadData,
keys.UsersTableID: privilege.ReadWriteData,
keys.ZonesTableID: privilege.ReadWriteData,
}
)
// These system TableDescriptor literals should match the descriptor that
// would be produced by evaluating one of the above `CREATE TABLE` statements
// for system tables that are not system config tables. See the
// `TestSystemTableLiterals` which checks that they do indeed match, and has
// suggestions on writing and maintaining them.
var (
// LeaseTable is the descriptor for the leases table.
LeaseTable = TableDescriptor{
Name: "lease",
ID: keys.LeaseTableID,
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "descID", ID: 1, Type: colTypeInt},
{Name: "version", ID: 2, Type: colTypeInt},
{Name: "nodeID", ID: 3, Type: colTypeInt},
{Name: "expiration", ID: 4, Type: colTypeTimestamp},
},
NextColumnID: 5,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"descID", "version", "nodeID", "expiration"}, ColumnIDs: []ColumnID{1, 2, 3, 4}},
},
PrimaryIndex: IndexDescriptor{
Name: "primary",
ID: 1,
Unique: true,
ColumnNames: []string{"descID", "version", "expiration", "nodeID"},
ColumnDirections: []IndexDescriptor_Direction{IndexDescriptor_ASC, IndexDescriptor_ASC, IndexDescriptor_ASC, IndexDescriptor_ASC},
ColumnIDs: []ColumnID{1, 2, 4, 3},
},
NextFamilyID: 1,
NextIndexID: 2,
Privileges: NewDefaultPrivilegeDescriptor(),
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
experimentalUniqueBytesString = "experimental_unique_bytes()"
// EventLogTable is the descriptor for the event log table.
EventLogTable = TableDescriptor{
Name: "eventlog",
ID: keys.EventLogTableID,
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "timestamp", ID: 1, Type: colTypeTimestamp},
{Name: "eventType", ID: 2, Type: colTypeString},
{Name: "targetID", ID: 3, Type: colTypeInt},
{Name: "reportingID", ID: 4, Type: colTypeInt},
{Name: "info", ID: 5, Type: colTypeString, Nullable: true},
{Name: "uniqueID", ID: 6, Type: colTypeBytes, DefaultExpr: &experimentalUniqueBytesString},
},
NextColumnID: 7,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"timestamp", "uniqueID"}, ColumnIDs: []ColumnID{1, 6}},
{Name: "fam_2_eventType", ID: 2, ColumnNames: []string{"eventType"}, ColumnIDs: []ColumnID{2}, DefaultColumnID: 2},
{Name: "fam_3_targetID", ID: 3, ColumnNames: []string{"targetID"}, ColumnIDs: []ColumnID{3}, DefaultColumnID: 3},
{Name: "fam_4_reportingID", ID: 4, ColumnNames: []string{"reportingID"}, ColumnIDs: []ColumnID{4}, DefaultColumnID: 4},
{Name: "fam_5_info", ID: 5, ColumnNames: []string{"info"}, ColumnIDs: []ColumnID{5}, DefaultColumnID: 5},
},
PrimaryIndex: IndexDescriptor{
Name: "primary",
ID: 1,
Unique: true,
ColumnNames: []string{"timestamp", "uniqueID"},
ColumnDirections: []IndexDescriptor_Direction{IndexDescriptor_ASC, IndexDescriptor_ASC},
ColumnIDs: []ColumnID{1, 6},
},
NextFamilyID: 6,
NextIndexID: 2,
Privileges: NewDefaultPrivilegeDescriptor(),
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
uniqueRowIDString = "unique_rowid()"
// RangeEventTable is the descriptor for the range log table.
RangeEventTable = TableDescriptor{
Name: "rangelog",
ID: keys.RangeEventTableID,
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "timestamp", ID: 1, Type: colTypeTimestamp},
{Name: "rangeID", ID: 2, Type: colTypeInt},
{Name: "storeID", ID: 3, Type: colTypeInt},
{Name: "eventType", ID: 4, Type: colTypeString},
{Name: "otherRangeID", ID: 5, Type: colTypeInt, Nullable: true},
{Name: "info", ID: 6, Type: colTypeString, Nullable: true},
{Name: "uniqueID", ID: 7, Type: colTypeInt, DefaultExpr: &uniqueRowIDString},
},
NextColumnID: 8,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"timestamp", "uniqueID"}, ColumnIDs: []ColumnID{1, 7}},
{Name: "fam_2_rangeID", ID: 2, ColumnNames: []string{"rangeID"}, ColumnIDs: []ColumnID{2}, DefaultColumnID: 2},
{Name: "fam_3_storeID", ID: 3, ColumnNames: []string{"storeID"}, ColumnIDs: []ColumnID{3}, DefaultColumnID: 3},
{Name: "fam_4_eventType", ID: 4, ColumnNames: []string{"eventType"}, ColumnIDs: []ColumnID{4}, DefaultColumnID: 4},
{Name: "fam_5_otherRangeID", ID: 5, ColumnNames: []string{"otherRangeID"}, ColumnIDs: []ColumnID{5}, DefaultColumnID: 5},
{Name: "fam_6_info", ID: 6, ColumnNames: []string{"info"}, ColumnIDs: []ColumnID{6}, DefaultColumnID: 6},
},
PrimaryIndex: IndexDescriptor{
Name: "primary",
ID: 1,
Unique: true,
ColumnNames: []string{"timestamp", "uniqueID"},
ColumnDirections: []IndexDescriptor_Direction{IndexDescriptor_ASC, IndexDescriptor_ASC},
ColumnIDs: []ColumnID{1, 7},
},
NextFamilyID: 7,
NextIndexID: 2,
Privileges: NewDefaultPrivilegeDescriptor(),
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
// UITable is the descriptor for the ui table.
UITable = TableDescriptor{
Name: "ui",
ID: keys.UITableID,
ParentID: 1,
Version: 1,
Columns: []ColumnDescriptor{
{Name: "key", ID: 1, Type: colTypeString},
{Name: "value", ID: 2, Type: colTypeBytes, Nullable: true},
{Name: "lastUpdated", ID: 3, Type: ColumnType{Kind: ColumnType_TIMESTAMP}},
},
NextColumnID: 4,
Families: []ColumnFamilyDescriptor{
{Name: "primary", ID: 0, ColumnNames: []string{"key"}, ColumnIDs: singleID1},
{Name: "fam_2_value", ID: 2, ColumnNames: []string{"value"}, ColumnIDs: []ColumnID{2}, DefaultColumnID: 2},
{Name: "fam_3_lastUpdated", ID: 3, ColumnNames: []string{"lastUpdated"}, ColumnIDs: []ColumnID{3}, DefaultColumnID: 3},
},
NextFamilyID: 4,
PrimaryIndex: pk("key"),
NextIndexID: 2,
Privileges: NewDefaultPrivilegeDescriptor(),
FormatVersion: InterleavedFormatVersion,
NextMutationID: 1,
}
)
// Create the key/value pairs for the default zone config entry.
func createDefaultZoneConfig() []roachpb.KeyValue {
var ret []roachpb.KeyValue
value := roachpb.Value{}
desc := config.DefaultZoneConfig()
if err := value.SetProto(&desc); err != nil {
log.Fatalf(context.TODO(), "could not marshal %v", desc)
}
ret = append(ret, roachpb.KeyValue{
Key: MakeZoneKey(keys.RootNamespaceID),
Value: value,
})
return ret
}
// addSystemDatabaseToSchema populates the supplied MetadataSchema with the
// System database and its tables. The descriptors for these objects exist
// statically in this file, but a MetadataSchema can be used to persist these
// descriptors to the cockroach store.
func addSystemDatabaseToSchema(target *MetadataSchema) {
// Add system database.
target.AddConfigDescriptor(keys.RootNamespaceID, &SystemDB)
// Add system config tables.
target.AddConfigDescriptor(keys.SystemDatabaseID, &NamespaceTable)
target.AddConfigDescriptor(keys.SystemDatabaseID, &DescriptorTable)
target.AddConfigDescriptor(keys.SystemDatabaseID, &UsersTable)
target.AddConfigDescriptor(keys.SystemDatabaseID, &ZonesTable)
// Add all the other system tables.
target.AddDescriptor(keys.SystemDatabaseID, &LeaseTable)
target.AddDescriptor(keys.SystemDatabaseID, &EventLogTable)
target.AddDescriptor(keys.SystemDatabaseID, &RangeEventTable)
target.AddDescriptor(keys.SystemDatabaseID, &UITable)
target.otherKV = append(target.otherKV, createDefaultZoneConfig()...)
}
// IsSystemConfigID returns true if this ID is for a system config object.
func IsSystemConfigID(id ID) bool {
return id > 0 && id <= keys.MaxSystemConfigDescID
}