-
Notifications
You must be signed in to change notification settings - Fork 451
/
helper.go
278 lines (224 loc) · 7.42 KB
/
helper.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package storage
import (
"errors"
"fmt"
"github.com/m3db/m3/src/cluster/generated/proto/placementpb"
"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cluster/placement"
"github.com/golang/protobuf/proto"
)
var (
errInvalidProtoForSinglePlacement = errors.New("invalid proto for single placement")
errInvalidProtoForPlacementSnapshots = errors.New("invalid proto for placement snapshots")
errNoPlacementInTheSnapshots = errors.New("not placement in the snapshots")
)
// helper handles placement marshaling and validation.
type helper interface {
// Placement retrieves the placement stored in kv.Store.
Placement() (placement.Placement, int, error)
// PlacementProto retrieves the proto stored in kv.Store.
PlacementProto() (proto.Message, int, error)
// GenerateProto generates the proto message for the new placement, it may read the kv.Store
// if existing placement data is needed.
GenerateProto(p placement.Placement) (proto.Message, error)
// ValidateProto validates if the given proto message is valid for placement.
ValidateProto(proto proto.Message) error
// PlacementForVersion returns the placement of a specific version.
PlacementForVersion(version int) (placement.Placement, error)
}
// newHelper returns a new placement storage helper.
func newHelper(store kv.Store, key string, opts placement.Options) helper {
if opts.IsStaged() {
return newStagedPlacementHelper(store, key)
}
return newPlacementHelper(store, key)
}
type placementHelper struct {
store kv.Store
key string
}
func newPlacementHelper(store kv.Store, key string) helper {
return &placementHelper{
store: store,
key: key,
}
}
func (h *placementHelper) PlacementForVersion(version int) (placement.Placement, error) {
values, err := h.store.History(h.key, version, version+1)
if err != nil {
return nil, err
}
if len(values) != 1 {
return nil, fmt.Errorf("invalid number of placements returned: %d, expecting 1", len(values))
}
return placementFromValue(values[0])
}
func (h *placementHelper) Placement() (placement.Placement, int, error) {
v, err := h.store.Get(h.key)
if err != nil {
return nil, 0, err
}
p, err := placementFromValue(v)
return p, v.Version(), err
}
func (h *placementHelper) PlacementProto() (proto.Message, int, error) {
v, err := h.store.Get(h.key)
if err != nil {
return nil, 0, err
}
p, err := placementProtoFromValue(v)
return p, v.Version(), err
}
func (h *placementHelper) GenerateProto(p placement.Placement) (proto.Message, error) {
return p.Proto()
}
func (h *placementHelper) ValidateProto(proto proto.Message) error {
placementProto, ok := proto.(*placementpb.Placement)
if !ok {
return errInvalidProtoForSinglePlacement
}
p, err := placement.NewPlacementFromProto(placementProto)
if err != nil {
return err
}
return placement.Validate(p)
}
type stagedPlacementHelper struct {
store kv.Store
key string
}
func newStagedPlacementHelper(store kv.Store, key string) helper {
return &stagedPlacementHelper{
store: store,
key: key,
}
}
// Placement returns the last placement in the snapshots.
func (h *stagedPlacementHelper) Placement() (placement.Placement, int, error) {
ps, v, err := h.placements()
if err != nil {
return nil, 0, err
}
l := len(ps)
if l == 0 {
return nil, 0, errNoPlacementInTheSnapshots
}
return ps[l-1], v, nil
}
func (h *stagedPlacementHelper) PlacementProto() (proto.Message, int, error) {
value, err := h.store.Get(h.key)
if err != nil {
return nil, 0, err
}
ps, err := placementSnapshotsProtoFromValue(value)
return ps, value.Version(), err
}
// GenerateProto generates a proto message with the placement appended to the snapshots.
func (h *stagedPlacementHelper) GenerateProto(p placement.Placement) (proto.Message, error) {
ps, _, err := h.placements()
if err != nil && err != kv.ErrNotFound {
return nil, err
}
if l := len(ps); l > 0 {
lastCutoverNanos := ps[l-1].CutoverNanos()
// When there is valid placement in the snapshots, the new placement must be scheduled after last placement.
if lastCutoverNanos >= p.CutoverNanos() {
return nil, fmt.Errorf("invalid placement: cutover nanos %d must be later than last placement cutover nanos %d",
p.CutoverNanos(), lastCutoverNanos)
}
}
ps = append(ps, p)
return ps.Proto()
}
func (h *stagedPlacementHelper) ValidateProto(proto proto.Message) error {
placementsProto, ok := proto.(*placementpb.PlacementSnapshots)
if !ok {
return errInvalidProtoForPlacementSnapshots
}
_, err := placement.NewPlacementsFromProto(placementsProto)
return err
}
func (h *stagedPlacementHelper) placements() (placement.Placements, int, error) {
value, err := h.store.Get(h.key)
if err != nil {
return nil, 0, err
}
ps, err := placementsFromValue(value)
return ps, value.Version(), err
}
func (h *stagedPlacementHelper) PlacementForVersion(version int) (placement.Placement, error) {
values, err := h.store.History(h.key, version, version+1)
if err != nil {
return nil, err
}
if len(values) != 1 {
return nil, fmt.Errorf("invalid number of placements returned: %d, expecting 1", len(values))
}
ps, err := placementsFromValue(values[0])
if err != nil {
return nil, err
}
l := len(ps)
if l == 0 {
return nil, errNoPlacementInTheSnapshots
}
return ps[l-1], nil
}
func placementProtoFromValue(v kv.Value) (*placementpb.Placement, error) {
var placementProto placementpb.Placement
if err := v.Unmarshal(&placementProto); err != nil {
return nil, err
}
return &placementProto, nil
}
func placementFromValue(v kv.Value) (placement.Placement, error) {
placementProto, err := placementProtoFromValue(v)
if err != nil {
return nil, err
}
p, err := placement.NewPlacementFromProto(placementProto)
if err != nil {
return nil, err
}
return p.SetVersion(v.Version()), nil
}
func placementSnapshotsProtoFromValue(v kv.Value) (*placementpb.PlacementSnapshots, error) {
var placementsProto placementpb.PlacementSnapshots
if err := v.Unmarshal(&placementsProto); err != nil {
return nil, err
}
return &placementsProto, nil
}
func placementsFromValue(v kv.Value) (placement.Placements, error) {
placementsProto, err := placementSnapshotsProtoFromValue(v)
if err != nil {
return nil, err
}
ps, err := placement.NewPlacementsFromProto(placementsProto)
if err != nil {
return nil, err
}
for i, p := range ps {
ps[i] = p.SetVersion(v.Version())
}
return ps, nil
}