vitess.io/vitess@v0.16.2/go/vt/binlog/keyspace_id_resolver.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package binlog 18 19 import ( 20 "fmt" 21 "strings" 22 23 "context" 24 25 "vitess.io/vitess/go/sqltypes" 26 "vitess.io/vitess/go/vt/key" 27 "vitess.io/vitess/go/vt/topo" 28 "vitess.io/vitess/go/vt/vtgate/vindexes" 29 "vitess.io/vitess/go/vt/vttablet/tabletserver/schema" 30 ) 31 32 // keyspaceIDResolver is constructed for a tableMap entry in RBR. It 33 // is used for each row, and passed in the value used for figuring out 34 // the keyspace id. 35 type keyspaceIDResolver interface { 36 // keyspaceID takes a table row, and returns the keyspace id as bytes. 37 // It will return an error if no sharding key can be found. 38 // The bitmap describes which columns are present in the row. 39 keyspaceID(value sqltypes.Value) ([]byte, error) 40 } 41 42 // keyspaceIDResolverFactory creates a keyspaceIDResolver for a table 43 // given its schema. It returns the index of the field to used to compute 44 // the keyspaceID, and a function that given a value for that 45 // field, returns the keyspace id. 46 type keyspaceIDResolverFactory func(*schema.Table) (int, keyspaceIDResolver, error) 47 48 // newKeyspaceIDResolverFactory creates a new 49 // keyspaceIDResolverFactory for the provided keyspace and cell. 50 func newKeyspaceIDResolverFactory(ctx context.Context, ts *topo.Server, keyspace string, cell string) (keyspaceIDResolverFactory, error) { 51 return newKeyspaceIDResolverFactoryV3(ctx, ts, keyspace, cell) 52 } 53 54 // newKeyspaceIDResolverFactoryV3 finds the SrvVSchema in the cell, 55 // gets the keyspace part, and uses it to find the column name. 56 func newKeyspaceIDResolverFactoryV3(ctx context.Context, ts *topo.Server, keyspace string, cell string) (keyspaceIDResolverFactory, error) { 57 srvVSchema, err := ts.GetSrvVSchema(ctx, cell) 58 if err != nil { 59 return nil, err 60 } 61 kschema, ok := srvVSchema.Keyspaces[keyspace] 62 if !ok { 63 return nil, fmt.Errorf("SrvVSchema has no entry for keyspace %v", keyspace) 64 } 65 keyspaceSchema, err := vindexes.BuildKeyspaceSchema(kschema, keyspace) 66 if err != nil { 67 return nil, fmt.Errorf("cannot build vschema for keyspace %v: %v", keyspace, err) 68 } 69 return func(table *schema.Table) (int, keyspaceIDResolver, error) { 70 // Find the v3 schema. 71 tableSchema, ok := keyspaceSchema.Tables[table.Name.String()] 72 if !ok { 73 return -1, nil, fmt.Errorf("no vschema definition for table %v", table.Name) 74 } 75 76 // use the lowest cost unique vindex as the sharding key 77 colVindex, err := vindexes.FindVindexForSharding(table.Name.String(), tableSchema.ColumnVindexes) 78 if err != nil { 79 return -1, nil, err 80 } 81 82 // TODO @rafael - when rewriting the mapping function, this will need to change. 83 // for now it's safe to assume the sharding key will be always on index 0. 84 shardingColumnName := colVindex.Columns[0].String() 85 for i, col := range table.Fields { 86 if strings.EqualFold(col.Name, shardingColumnName) { 87 // We found the column. 88 return i, &keyspaceIDResolverFactoryV3{ 89 // Only SingleColumn vindexes are returned by FindVindexForSharding. 90 vindex: colVindex.Vindex.(vindexes.SingleColumn), 91 }, nil 92 } 93 } 94 // The column was not found. 95 return -1, nil, fmt.Errorf("cannot find column %v in table %v", shardingColumnName, table.Name) 96 }, nil 97 } 98 99 // keyspaceIDResolverFactoryV3 uses the Vindex to compute the value. 100 type keyspaceIDResolverFactoryV3 struct { 101 vindex vindexes.SingleColumn 102 } 103 104 func (r *keyspaceIDResolverFactoryV3) keyspaceID(v sqltypes.Value) ([]byte, error) { 105 destinations, err := r.vindex.Map(context.TODO(), nil, []sqltypes.Value{v}) 106 if err != nil { 107 return nil, err 108 } 109 if len(destinations) != 1 { 110 return nil, fmt.Errorf("mapping row to keyspace id returned an invalid array of destinations: %v", key.DestinationsString(destinations)) 111 } 112 ksid, ok := destinations[0].(key.DestinationKeyspaceID) 113 if !ok || len(ksid) == 0 { 114 return nil, fmt.Errorf("could not map %v to a keyspace id, got destination %v", v, destinations[0]) 115 } 116 return ksid, nil 117 }