/
vschema.go
167 lines (144 loc) · 4.86 KB
/
vschema.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
/*
Copyright 2019 The Vitess 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.
*/
package topo
import (
"path"
"google.golang.org/protobuf/proto"
"context"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vterrors"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
// SaveVSchema first validates the VSchema, then saves it.
// If the VSchema is empty, just remove it.
func (ts *Server) SaveVSchema(ctx context.Context, keyspace string, vschema *vschemapb.Keyspace) error {
err := vindexes.ValidateKeyspace(vschema)
if err != nil {
return err
}
nodePath := path.Join(KeyspacesPath, keyspace, VSchemaFile)
data, err := proto.Marshal(vschema)
if err != nil {
return err
}
_, err = ts.globalCell.Update(ctx, nodePath, data, nil)
if err != nil {
log.Errorf("failed to update vschema for keyspace %s: %v", keyspace, err)
} else {
log.Infof("successfully updated vschema for keyspace %s: %+v", keyspace, vschema)
}
return err
}
// DeleteVSchema delete the keyspace if it exists
func (ts *Server) DeleteVSchema(ctx context.Context, keyspace string) error {
log.Infof("deleting vschema for keyspace %s", keyspace)
nodePath := path.Join(KeyspacesPath, keyspace, VSchemaFile)
return ts.globalCell.Delete(ctx, nodePath, nil)
}
// GetVSchema fetches the vschema from the topo.
func (ts *Server) GetVSchema(ctx context.Context, keyspace string) (*vschemapb.Keyspace, error) {
nodePath := path.Join(KeyspacesPath, keyspace, VSchemaFile)
data, _, err := ts.globalCell.Get(ctx, nodePath)
if err != nil {
return nil, err
}
var vs vschemapb.Keyspace
err = proto.Unmarshal(data, &vs)
if err != nil {
return nil, vterrors.Wrapf(err, "bad vschema data: %q", data)
}
return &vs, nil
}
// EnsureVSchema makes sure that a vschema is present for this keyspace or creates a blank one if it is missing
func (ts *Server) EnsureVSchema(ctx context.Context, keyspace string) error {
vschema, err := ts.GetVSchema(ctx, keyspace)
if err != nil && !IsErrType(err, NoNode) {
log.Infof("error in getting vschema for keyspace %s: %v", keyspace, err)
}
if vschema == nil || IsErrType(err, NoNode) {
err = ts.SaveVSchema(ctx, keyspace, &vschemapb.Keyspace{
Sharded: false,
Vindexes: make(map[string]*vschemapb.Vindex),
Tables: make(map[string]*vschemapb.Table),
})
if err != nil {
log.Errorf("could not create blank vschema: %v", err)
return err
}
}
return nil
}
// SaveRoutingRules saves the routing rules into the topo.
func (ts *Server) SaveRoutingRules(ctx context.Context, routingRules *vschemapb.RoutingRules) error {
data, err := proto.Marshal(routingRules)
if err != nil {
return err
}
if len(data) == 0 {
// No vschema, remove it. So we can remove the keyspace.
if err := ts.globalCell.Delete(ctx, RoutingRulesFile, nil); err != nil && !IsErrType(err, NoNode) {
return err
}
return nil
}
_, err = ts.globalCell.Update(ctx, RoutingRulesFile, data, nil)
return err
}
// GetRoutingRules fetches the routing rules from the topo.
func (ts *Server) GetRoutingRules(ctx context.Context) (*vschemapb.RoutingRules, error) {
rr := &vschemapb.RoutingRules{}
data, _, err := ts.globalCell.Get(ctx, RoutingRulesFile)
if err != nil {
if IsErrType(err, NoNode) {
return rr, nil
}
return nil, err
}
err = proto.Unmarshal(data, rr)
if err != nil {
return nil, vterrors.Wrapf(err, "bad routing rules data: %q", data)
}
return rr, nil
}
// SaveShardRoutingRules saves the shard routing rules into the topo.
func (ts *Server) SaveShardRoutingRules(ctx context.Context, shardRoutingRules *vschemapb.ShardRoutingRules) error {
data, err := proto.Marshal(shardRoutingRules)
if err != nil {
return err
}
if len(data) == 0 {
if err := ts.globalCell.Delete(ctx, ShardRoutingRulesFile, nil); err != nil && !IsErrType(err, NoNode) {
return err
}
return nil
}
_, err = ts.globalCell.Update(ctx, ShardRoutingRulesFile, data, nil)
return err
}
// GetShardRoutingRules fetches the shard routing rules from the topo.
func (ts *Server) GetShardRoutingRules(ctx context.Context) (*vschemapb.ShardRoutingRules, error) {
srr := &vschemapb.ShardRoutingRules{}
data, _, err := ts.globalCell.Get(ctx, ShardRoutingRulesFile)
if err != nil {
if IsErrType(err, NoNode) {
return srr, nil
}
return nil, err
}
err = proto.Unmarshal(data, srr)
if err != nil {
return nil, vterrors.Wrapf(err, "invalid shard routing rules: %q", data)
}
return srr, nil
}