-
Notifications
You must be signed in to change notification settings - Fork 451
/
shard.go
338 lines (288 loc) · 8.82 KB
/
shard.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) 2016 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 shard
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/m3db/m3/src/cluster/generated/proto/placementpb"
)
var (
errInvalidProtoShardState = errors.New("invalid proto shard state")
defaultShardState State
defaultShardStateProto placementpb.ShardState
)
// NewShardStateFromProto creates new shard state from proto.
func NewShardStateFromProto(state placementpb.ShardState) (State, error) {
switch state {
case placementpb.ShardState_INITIALIZING:
return Initializing, nil
case placementpb.ShardState_AVAILABLE:
return Available, nil
case placementpb.ShardState_LEAVING:
return Leaving, nil
default:
return defaultShardState, errInvalidProtoShardState
}
}
// Proto returns the proto representation for the shard state.
func (s State) Proto() (placementpb.ShardState, error) {
switch s {
case Initializing:
return placementpb.ShardState_INITIALIZING, nil
case Available:
return placementpb.ShardState_AVAILABLE, nil
case Leaving:
return placementpb.ShardState_LEAVING, nil
default:
return defaultShardStateProto, errInvalidProtoShardState
}
}
// NewShard returns a new Shard
func NewShard(id uint32) Shard { return &shard{id: id, state: Unknown} }
// NewShardFromProto create a new shard from proto.
func NewShardFromProto(shard *placementpb.Shard) (Shard, error) {
state, err := NewShardStateFromProto(shard.State)
if err != nil {
return nil, err
}
return NewShard(shard.Id).
SetState(state).
SetSourceID(shard.SourceId).
SetCutoverNanos(shard.CutoverNanos).
SetCutoffNanos(shard.CutoffNanos), nil
}
type shard struct {
id uint32
state State
sourceID string
cutoverNanos int64
cutoffNanos int64
}
func (s *shard) ID() uint32 { return s.id }
func (s *shard) State() State { return s.state }
func (s *shard) SetState(state State) Shard { s.state = state; return s }
func (s *shard) SourceID() string { return s.sourceID }
func (s *shard) SetSourceID(sourceID string) Shard { s.sourceID = sourceID; return s }
func (s *shard) CutoverNanos() int64 {
if s.cutoverNanos != UnInitializedValue {
return s.cutoverNanos
}
// NB(xichen): if the value is not set, we return the default cutover nanos.
return DefaultShardCutoverNanos
}
func (s *shard) SetCutoverNanos(value int64) Shard {
// NB(cw): We use UnInitializedValue to represent the DefaultShardCutoverNanos
// so that we can save some space in the proto representation for the
// default value of cutover time.
if value == DefaultShardCutoverNanos {
value = UnInitializedValue
}
s.cutoverNanos = value
return s
}
func (s *shard) CutoffNanos() int64 {
if s.cutoffNanos != UnInitializedValue {
return s.cutoffNanos
}
// NB(xichen): if the value is not set, we return the default cutoff nanos.
return DefaultShardCutoffNanos
}
func (s *shard) SetCutoffNanos(value int64) Shard {
// NB(cw): We use UnInitializedValue to represent the DefaultShardCutoffNanos
// so that we can save some space in the proto representation for the
// default value of cutoff time.
if value == DefaultShardCutoffNanos {
value = UnInitializedValue
}
s.cutoffNanos = value
return s
}
func (s *shard) Equals(other Shard) bool {
return s.ID() == other.ID() &&
s.State() == other.State() &&
s.SourceID() == other.SourceID() &&
s.CutoverNanos() == other.CutoverNanos() &&
s.CutoffNanos() == other.CutoffNanos()
}
func (s *shard) Proto() (*placementpb.Shard, error) {
ss, err := s.State().Proto()
if err != nil {
return nil, err
}
return &placementpb.Shard{
Id: s.ID(),
State: ss,
SourceId: s.SourceID(),
CutoverNanos: s.cutoverNanos,
CutoffNanos: s.cutoffNanos,
}, nil
}
func (s *shard) Clone() Shard {
return NewShard(s.ID()).
SetState(s.State()).
SetSourceID(s.SourceID()).
SetCutoverNanos(s.CutoverNanos()).
SetCutoffNanos(s.CutoffNanos())
}
// SortableShardsByIDAsc are sortable shards by ID in ascending order
type SortableShardsByIDAsc []Shard
func (s SortableShardsByIDAsc) Len() int { return len(s) }
func (s SortableShardsByIDAsc) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s SortableShardsByIDAsc) Less(i, j int) bool {
return s[i].ID() < s[j].ID()
}
// SortableIDsAsc are sortable shard IDs in ascending order
type SortableIDsAsc []uint32
func (s SortableIDsAsc) Len() int { return len(s) }
func (s SortableIDsAsc) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s SortableIDsAsc) Less(i, j int) bool {
return s[i] < s[j]
}
// NewShards creates a new instance of Shards
func NewShards(ss []Shard) Shards {
shardMap := make(map[uint32]Shard, len(ss))
for _, s := range ss {
shardMap[s.ID()] = s
}
return shards{shardsMap: shardMap}
}
// NewShardsFromProto creates a new set of shards from proto.
func NewShardsFromProto(shards []*placementpb.Shard) (Shards, error) {
allShards := make([]Shard, 0, len(shards))
for _, s := range shards {
shard, err := NewShardFromProto(s)
if err != nil {
return nil, err
}
allShards = append(allShards, shard)
}
return NewShards(allShards), nil
}
type shards struct {
shardsMap map[uint32]Shard
}
func (ss shards) All() []Shard {
shards := make([]Shard, 0, len(ss.shardsMap))
for _, shard := range ss.shardsMap {
shards = append(shards, shard)
}
sort.Sort(SortableShardsByIDAsc(shards))
return shards
}
func (ss shards) AllIDs() []uint32 {
ids := make([]uint32, 0, len(ss.shardsMap))
for _, shard := range ss.shardsMap {
ids = append(ids, shard.ID())
}
sort.Sort(SortableIDsAsc(ids))
return ids
}
func (ss shards) NumShards() int {
return len(ss.shardsMap)
}
func (ss shards) Shard(id uint32) (Shard, bool) {
shard, ok := ss.shardsMap[id]
return shard, ok
}
func (ss shards) Add(shard Shard) {
ss.shardsMap[shard.ID()] = shard
}
func (ss shards) Remove(shard uint32) {
delete(ss.shardsMap, shard)
}
func (ss shards) Contains(shard uint32) bool {
_, ok := ss.shardsMap[shard]
return ok
}
func (ss shards) NumShardsForState(state State) int {
count := 0
for _, s := range ss.shardsMap {
if s.State() == state {
count++
}
}
return count
}
func (ss shards) ShardsForState(state State) []Shard {
var r []Shard
for _, s := range ss.shardsMap {
if s.State() == state {
r = append(r, s)
}
}
return r
}
func (ss shards) Equals(other Shards) bool {
shards := ss.All()
otherShards := other.All()
if len(shards) != len(otherShards) {
return false
}
for i, shard := range shards {
otherShard := otherShards[i]
if !shard.Equals(otherShard) {
return false
}
}
return true
}
func (ss shards) String() string {
var strs []string
for _, state := range validStates() {
ids := NewShards(ss.ShardsForState(state)).AllIDs()
str := fmt.Sprintf("%s=%v", state.String(), ids)
strs = append(strs, str)
}
return fmt.Sprintf("[%s]", strings.Join(strs, ", "))
}
func (ss shards) Proto() ([]*placementpb.Shard, error) {
res := make([]*placementpb.Shard, 0, len(ss.shardsMap))
// All() returns the shards in ID ascending order.
for _, shard := range ss.All() {
sp, err := shard.Proto()
if err != nil {
return nil, err
}
res = append(res, sp)
}
return res, nil
}
func (ss shards) Clone() Shards {
shards := make([]Shard, ss.NumShards())
for i, shard := range ss.All() {
shards[i] = shard.Clone()
}
return NewShards(shards)
}
// SortableShardProtosByIDAsc sorts shard protos by their ids in ascending order.
type SortableShardProtosByIDAsc []*placementpb.Shard
func (su SortableShardProtosByIDAsc) Len() int { return len(su) }
func (su SortableShardProtosByIDAsc) Less(i, j int) bool { return su[i].Id < su[j].Id }
func (su SortableShardProtosByIDAsc) Swap(i, j int) { su[i], su[j] = su[j], su[i] }
// validStates returns all the valid states.
func validStates() []State {
return []State{
Initializing,
Available,
Leaving,
}
}