forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_resolver.go
171 lines (152 loc) · 5.99 KB
/
key_resolver.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package worker
import (
"errors"
"fmt"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/key"
"github.com/youtube/vitess/go/vt/mysqlctl/tmutils"
tabletmanagerdatapb "github.com/youtube/vitess/go/vt/proto/tabletmanagerdata"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtgate/vindexes"
)
// This file defines the interface and implementations of sharding key resolvers.
// keyspaceIDResolver defines the interface that needs to be satisifed to get a
// keyspace ID from a database row.
type keyspaceIDResolver interface {
// keyspaceID takes a table row, and returns the keyspace id as bytes.
// It will return an error if no sharding key can be found.
keyspaceID(row []sqltypes.Value) ([]byte, error)
}
// v2Resolver is the keyspace id resolver that is used by VTGate V2 deployments.
// In V2, the sharding key column name and type is the same for all tables,
// and the keyspace ID is stored in the sharding key database column.
type v2Resolver struct {
keyspaceInfo *topo.KeyspaceInfo
shardingColumnIndex int
}
// newV2Resolver returns a keyspaceIDResolver for a v2 table.
// V2 keyspaces have a preset sharding column name and type.
func newV2Resolver(keyspaceInfo *topo.KeyspaceInfo, td *tabletmanagerdatapb.TableDefinition) (keyspaceIDResolver, error) {
if keyspaceInfo.ShardingColumnName == "" {
return nil, errors.New("ShardingColumnName needs to be set for a v2 sharding key")
}
if keyspaceInfo.ShardingColumnType == topodatapb.KeyspaceIdType_UNSET {
return nil, errors.New("ShardingColumnType needs to be set for a v2 sharding key")
}
if td.Type != tmutils.TableBaseTable {
return nil, fmt.Errorf("a keyspaceID resolver can only be created for a base table, got %v", td.Type)
}
// Find the sharding key column index.
columnIndex, ok := tmutils.TableDefinitionGetColumn(td, keyspaceInfo.ShardingColumnName)
if !ok {
return nil, fmt.Errorf("table %v doesn't have a column named '%v'", td.Name, keyspaceInfo.ShardingColumnName)
}
return &v2Resolver{keyspaceInfo, columnIndex}, nil
}
// keyspaceID implements the keyspaceIDResolver interface.
func (r *v2Resolver) keyspaceID(row []sqltypes.Value) ([]byte, error) {
v := row[r.shardingColumnIndex]
switch r.keyspaceInfo.ShardingColumnType {
case topodatapb.KeyspaceIdType_BYTES:
return v.Raw(), nil
case topodatapb.KeyspaceIdType_UINT64:
i, err := v.ParseUint64()
if err != nil {
return nil, fmt.Errorf("Non numerical value: %v", err)
}
return key.Uint64Key(i).Bytes(), nil
default:
return nil, fmt.Errorf("unsupported ShardingColumnType: %v", r.keyspaceInfo.ShardingColumnType)
}
}
// v3Resolver is the keyspace id resolver that is used by VTGate V3 deployments.
// In V3, we use the VSchema to find a Unique VIndex of cost 0 or 1 for each
// table.
type v3Resolver struct {
shardingColumnIndex int
vindex vindexes.Unique
}
// newV3ResolverFromTableDefinition returns a keyspaceIDResolver for a v3 table.
func newV3ResolverFromTableDefinition(keyspaceSchema *vindexes.KeyspaceSchema, td *tabletmanagerdatapb.TableDefinition) (keyspaceIDResolver, error) {
if td.Type != tmutils.TableBaseTable {
return nil, fmt.Errorf("a keyspaceID resolver can only be created for a base table, got %v", td.Type)
}
tableSchema, ok := keyspaceSchema.Tables[td.Name]
if !ok {
return nil, fmt.Errorf("no vschema definition for table %v", td.Name)
}
// the primary vindex is most likely the sharding key, and has to
// be unique.
if len(tableSchema.ColumnVindexes) == 0 {
return nil, fmt.Errorf("no vindex definition for table %v", td.Name)
}
colVindex := tableSchema.ColumnVindexes[0]
if colVindex.Vindex.Cost() > 1 {
return nil, fmt.Errorf("primary vindex cost is too high for table %v", td.Name)
}
unique, ok := colVindex.Vindex.(vindexes.Unique)
if !ok {
return nil, fmt.Errorf("primary vindex is not unique for table %v", td.Name)
}
// Find the sharding key column index.
columnIndex, ok := tmutils.TableDefinitionGetColumn(td, colVindex.Column.Original())
if !ok {
return nil, fmt.Errorf("table %v has a Vindex on unknown column %v", td.Name, colVindex.Column)
}
return &v3Resolver{
shardingColumnIndex: columnIndex,
vindex: unique,
}, nil
}
// newV3ResolverFromColumnList returns a keyspaceIDResolver for a v3 table.
func newV3ResolverFromColumnList(keyspaceSchema *vindexes.KeyspaceSchema, name string, columns []string) (keyspaceIDResolver, error) {
tableSchema, ok := keyspaceSchema.Tables[name]
if !ok {
return nil, fmt.Errorf("no vschema definition for table %v", name)
}
// the primary vindex is most likely the sharding key, and has to
// be unique.
if len(tableSchema.ColumnVindexes) == 0 {
return nil, fmt.Errorf("no vindex definition for table %v", name)
}
colVindex := tableSchema.ColumnVindexes[0]
if colVindex.Vindex.Cost() > 1 {
return nil, fmt.Errorf("primary vindex cost is too high for table %v", name)
}
unique, ok := colVindex.Vindex.(vindexes.Unique)
if !ok {
return nil, fmt.Errorf("primary vindex is not unique for table %v", name)
}
// Find the sharding key column index.
columnIndex := -1
for i, n := range columns {
if colVindex.Column.EqualString(n) {
columnIndex = i
break
}
}
if columnIndex == -1 {
return nil, fmt.Errorf("table %v has a Vindex on unknown column %v", name, colVindex.Column)
}
return &v3Resolver{
shardingColumnIndex: columnIndex,
vindex: unique,
}, nil
}
// keyspaceID implements the keyspaceIDResolver interface.
func (r *v3Resolver) keyspaceID(row []sqltypes.Value) ([]byte, error) {
v := row[r.shardingColumnIndex]
ids := []interface{}{v}
ksids, err := r.vindex.Map(nil, ids)
if err != nil {
return nil, err
}
if len(ksids) != 1 {
return nil, fmt.Errorf("maping row to keyspace id returned an invalid array of keyspace ids: %v", ksids)
}
return ksids[0], nil
}