forked from cockroachdb/cockroach
/
zone.go
249 lines (220 loc) · 6.18 KB
/
zone.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
// Copyright 2014 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
// Author: Bram Gruneir (bram+code@cockroachlabs.com)
package cli
import (
"fmt"
"io/ioutil"
"strconv"
"github.com/cockroachdb/cockroach/config"
"github.com/cockroachdb/cockroach/util/log"
gogoproto "github.com/gogo/protobuf/proto"
yaml "gopkg.in/yaml.v1"
"github.com/spf13/cobra"
)
// zoneProtoToYAMLString takes a marshalled proto and returns
// its yaml representation.
func zoneProtoToYAMLString(val string) (string, error) {
var zone config.ZoneConfig
if err := gogoproto.Unmarshal([]byte(val), &zone); err != nil {
return "", err
}
ret, err := yaml.Marshal(zone)
if err != nil {
return "", err
}
return string(ret), nil
}
// formatZone is a callback used to format the raw zone config
// protobuf in a sql.Rows column for pretty printing.
func formatZone(val interface{}) string {
if str, ok := val.(string); ok {
if ret, err := zoneProtoToYAMLString(str); err == nil {
return ret
}
}
// Fallback to raw string in case of problems.
return fmt.Sprintf("%#v", val)
}
// A getZoneCmd command displays the zone config for the specified ID.
var getZoneCmd = &cobra.Command{
Use: "get [options] <object-ID>",
Short: "fetches and displays the zone config",
Long: `
Fetches and displays the zone configuration for <object-id>.
`,
Run: runGetZone,
}
// runGetZone retrieves the zone config for a given object id,
// and if present, outputs its YAML representation.
// TODO(marc): accept db/table names rather than IDs.
func runGetZone(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Usage()
return
}
id, err := strconv.Atoi(args[0])
if err != nil {
log.Errorf("could not parse object ID %s", args[0])
return
}
db := makeSQLClient()
err = runQueryWithFormat(db, fmtMap{"config": formatZone}, `SELECT * FROM system.zones WHERE id=$1`, id)
if err != nil {
log.Error(err)
return
}
if err != nil {
log.Error(err)
return
}
}
// A lsZonesCmd command displays a list of zone configs by objet ID.
var lsZonesCmd = &cobra.Command{
Use: "ls [options]",
Short: "list all zone configs by object ID",
Long: `
List zone configs.
`,
Run: runLsZones,
}
// TODO(marc): return db/table names rather than IDs.
func runLsZones(cmd *cobra.Command, args []string) {
if len(args) > 0 {
cmd.Usage()
return
}
db := makeSQLClient()
err := runQueryWithFormat(db, fmtMap{"config": formatZone}, `SELECT * FROM system.zones`)
if err != nil {
log.Error(err)
return
}
}
// A rmZoneCmd command removes a zone config by ID.
var rmZoneCmd = &cobra.Command{
Use: "rm [options] <object-id>",
Short: "remove a zone config by object id",
Long: `
Remove an existing zone config by object id. No action is taken if no
zone configuration exists for the specified object ID.
`,
Run: runRmZone,
}
// TODO(marc): accept db/table names rather than IDs.
func runRmZone(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Usage()
return
}
id, err := strconv.Atoi(args[0])
if err != nil {
log.Errorf("could not parse object ID %s", args[0])
return
}
db := makeSQLClient()
err = runQuery(db, `DELETE FROM system.zones WHERE id=$1`, id)
if err != nil {
log.Error(err)
return
}
fmt.Printf("Deleted zone key %q\n", args[0])
}
// A setZoneCmd command creates a new or updates an existing zone config.
var setZoneCmd = &cobra.Command{
Use: "set [options] <object-id> <zone-config-file>",
Short: "create or update zone config for object ID",
Long: `
Create or update a zone config for the specified object ID (first
argument: <object-id>) to the contents of the specified file
(second argument: <zone-config-file>).
The zone config format has the following YAML schema:
replicas:
- attrs: [comma-separated attribute list]
- attrs: ...
range_min_bytes: <size-in-bytes>
range_max_bytes: <size-in-bytes>
For example:
replicas:
- attrs: [us-east-1a, ssd]
- attrs: [us-east-1b, ssd]
- attrs: [us-west-1b, ssd]
range_min_bytes: 8388608
range_max_bytes: 67108864
`,
Run: runSetZone,
}
// runSetZone parses the yaml input file, converts it to proto,
// and inserts it in the system.zones table.
// TODO(marc): accept db/table names rather than IDs.
func runSetZone(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Usage()
return
}
id, err := strconv.Atoi(args[0])
if err != nil {
log.Errorf("could not parse object ID %s", args[0])
return
}
// Read in the config file.
body, err := ioutil.ReadFile(args[1])
if err != nil {
log.Errorf("unable to read zone config file %q: %s", args[1], err)
return
}
// Convert it to proto and marshal it again to put into the table.
// This is a bit more tedious than taking protos directly,
// but yaml is a more widely understood format.
var pbZoneConfig config.ZoneConfig
if err := yaml.Unmarshal(body, &pbZoneConfig); err != nil {
log.Errorf("unable to parse zone config file %q: %s", args[1], err)
return
}
if err := pbZoneConfig.Validate(); err != nil {
log.Error(err)
return
}
buf, err := gogoproto.Marshal(&pbZoneConfig)
if err != nil {
log.Errorf("unable to parse zone config file %q: %s", args[1], err)
return
}
db := makeSQLClient()
// TODO(marc): switch to UPSERT.
err = runQuery(db, `INSERT INTO system.zones VALUES ($1, $2)`, id, buf)
if err != nil {
log.Error(err)
return
}
}
var zoneCmds = []*cobra.Command{
getZoneCmd,
lsZonesCmd,
rmZoneCmd,
setZoneCmd,
}
var zoneCmd = &cobra.Command{
Use: "zone",
Short: "get, set, list and remove zones\n",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
func init() {
zoneCmd.AddCommand(zoneCmds...)
}