forked from cockroachdb/cockroach
/
zone.go
138 lines (129 loc) · 4.82 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
// 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)
package server
import (
"bytes"
"net/http"
"net/url"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/proto"
"github.com/cockroachdb/cockroach/storage"
"github.com/cockroachdb/cockroach/storage/engine"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/log"
)
const (
// minRangeMaxBytes is the minimum value for range max bytes.
minRangeMaxBytes = 1 << 20
)
// A zoneHandler implements the adminHandler interface.
type zoneHandler struct {
db *client.KV // Key-value database client
}
// Put writes a zone config for the specified key prefix (which is
// treated as a key). The zone config is parsed from the input
// "body". The specified body must validly parse into a zone config
// struct.
func (zh *zoneHandler) Put(path string, body []byte, r *http.Request) error {
if len(path) == 0 {
return util.Errorf("no path specified for zone Put")
}
config := &proto.ZoneConfig{}
if err := util.UnmarshalRequest(r, body, config, util.AllEncodings); err != nil {
return util.Errorf("zone config has invalid format: %q: %s", body, err)
}
if len(config.ReplicaAttrs) == 0 {
return util.Errorf("attributes for at least one replica must be specified in zone config")
}
if config.RangeMaxBytes < minRangeMaxBytes {
return util.Errorf("RangeMaxBytes %d less than minimum allowed %d", config.RangeMaxBytes, minRangeMaxBytes)
}
if config.RangeMinBytes >= config.RangeMaxBytes {
return util.Errorf("RangeMinBytes %d is greater than or equal to RangeMaxBytes %d",
config.RangeMinBytes, config.RangeMaxBytes)
}
zoneKey := engine.MakeKey(engine.KeyConfigZonePrefix, proto.Key(path[1:]))
if err := zh.db.PutProto(zoneKey, config); err != nil {
return err
}
return nil
}
// Get retrieves the zone configuration for the specified key. If the
// key is empty, all zone configurations are returned. Otherwise, the
// leading "/" path delimiter is stripped and the zone configuration
// matching the remainder is retrieved. Note that this will retrieve
// the default zone config if "key" is equal to "/", and will list all
// configs if "key" is equal to "". The body result contains
// JSON-formatted output for a listing of keys and JSON-formatted
// output for retrieval of a zone config.
func (zh *zoneHandler) Get(path string, r *http.Request) (body []byte, contentType string, err error) {
// Scan all zones if the key is empty.
if len(path) == 0 {
sr := &proto.ScanResponse{}
if err = zh.db.Call(proto.Scan, &proto.ScanRequest{
RequestHeader: proto.RequestHeader{
Key: engine.KeyConfigZonePrefix,
EndKey: engine.KeyConfigZonePrefix.PrefixEnd(),
User: storage.UserRoot,
},
MaxResults: maxGetResults,
}, sr); err != nil {
return
}
if len(sr.Rows) == maxGetResults {
log.Warningf("retrieved maximum number of results (%d); some may be missing", maxGetResults)
}
var prefixes []string
for _, kv := range sr.Rows {
trimmed := bytes.TrimPrefix(kv.Key, engine.KeyConfigZonePrefix)
prefixes = append(prefixes, url.QueryEscape(string(trimmed)))
}
// Encode the response.
body, contentType, err = util.MarshalResponse(r, prefixes, util.AllEncodings)
} else {
zoneKey := engine.MakeKey(engine.KeyConfigZonePrefix, proto.Key(path[1:]))
var ok bool
config := &proto.ZoneConfig{}
if ok, _, err = zh.db.GetProto(zoneKey, config); err != nil {
return
}
// On get, if there's no zone config for the requested prefix,
// return a not found error.
if !ok {
err = util.Errorf("no config found for key prefix %q", path)
return
}
body, contentType, err = util.MarshalResponse(r, config, util.AllEncodings)
}
return
}
// Delete removes the zone config specified by key.
func (zh *zoneHandler) Delete(path string, r *http.Request) error {
if len(path) == 0 {
return util.Errorf("no path specified for zone Delete")
}
if path == "/" {
return util.Errorf("the default zone configuration cannot be deleted")
}
zoneKey := engine.MakeKey(engine.KeyConfigZonePrefix, proto.Key(path[1:]))
return zh.db.Call(proto.Delete, &proto.DeleteRequest{
RequestHeader: proto.RequestHeader{
Key: zoneKey,
User: storage.UserRoot,
},
}, &proto.DeleteResponse{})
}