-
Notifications
You must be signed in to change notification settings - Fork 452
/
mirrored_custom_groups.go
232 lines (202 loc) · 7.1 KB
/
mirrored_custom_groups.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
// Copyright (c) 2019 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 selector
import (
"fmt"
"github.com/m3db/m3/src/cluster/placement"
"github.com/m3db/m3/src/x/errors"
"go.uber.org/zap"
)
type mirroredCustomGroupSelector struct {
instanceIDToGroupID InstanceGroupIDFunc
logger *zap.Logger
opts placement.Options
}
// InstanceGroupIDFunc maps an instance to its mirrored group.
type InstanceGroupIDFunc func(inst placement.Instance) (string, error)
// NewMapInstanceGroupIDFunc creates a simple lookup function for an instances group, which
// looks up the group ID for an instance by the instance ID.
func NewMapInstanceGroupIDFunc(instanceToGroup map[string]string) InstanceGroupIDFunc {
return func(inst placement.Instance) (string, error) {
gid, ok := instanceToGroup[inst.ID()]
if !ok {
return "", fmt.Errorf(
"instance %s doesn't have a corresponding group in ID to group map",
inst.ID(),
)
}
return gid, nil
}
}
// NewMirroredCustomGroupSelector constructs a placement.InstanceSelector which assigns shardsets
// according to their group ID (provided by instanceToGroupID). That is, instances with the
// same group ID are assigned the same shardset ID, and will receive the same mirrored traffic.
func NewMirroredCustomGroupSelector(
instanceToGroupID InstanceGroupIDFunc,
opts placement.Options,
) placement.InstanceSelector {
return &mirroredCustomGroupSelector{
logger: opts.InstrumentOptions().Logger(),
instanceIDToGroupID: instanceToGroupID,
opts: opts,
}
}
func (e *mirroredCustomGroupSelector) SelectInitialInstances(
candidates []placement.Instance,
rf int,
) ([]placement.Instance, error) {
return e.selectInstances(
candidates,
placement.NewPlacement().SetReplicaFactor(rf),
true,
)
}
func (e *mirroredCustomGroupSelector) SelectAddingInstances(
candidates []placement.Instance,
p placement.Placement,
) ([]placement.Instance, error) {
return e.selectInstances(candidates, p, e.opts.AddAllCandidates())
}
// SelectReplaceInstances attempts to find a replacement instance in the same group
// for each of the leavingInstances
func (e *mirroredCustomGroupSelector) SelectReplaceInstances(
candidates []placement.Instance,
leavingInstanceIDs []string,
p placement.Placement,
) ([]placement.Instance, error) {
candidates, err := getValidCandidates(p, candidates, e.opts)
if err != nil {
return nil, err
}
// find a replacement for each leaving instance.
candidatesByGroup, err := e.groupInstancesByID(candidates)
if err != nil {
return nil, err
}
leavingInstances, err := getLeavingInstances(p, leavingInstanceIDs)
if err != nil {
return nil, err
}
replacementGroups := make([]mirroredReplacementGroup, 0, len(leavingInstances))
for _, leavingInstance := range leavingInstances {
// try to find an instance in the same group as the leaving instance.
groupID, err := e.getGroup(leavingInstance)
if err != nil {
return nil, err
}
replacementGroup := candidatesByGroup[groupID]
if len(replacementGroup) == 0 {
return nil, newErrNoValidReplacement(leavingInstance.ID(), groupID)
}
replacementNode := replacementGroup[len(replacementGroup)-1]
candidatesByGroup[groupID] = replacementGroup[:len(replacementGroup)-1]
replacementGroups = append(
replacementGroups,
mirroredReplacementGroup{
Leaving: leavingInstance,
Replacement: replacementNode,
})
}
return assignShardsetIDsToReplacements(leavingInstanceIDs, replacementGroups)
}
type mirroredReplacementGroup struct {
Leaving placement.Instance
Replacement placement.Instance
}
// selectInstances does the actual work of the class. It groups candidate instances by their
// group ID, and assigns them shardsets.
// N.B. (amains): addAllInstances is a parameter here (instead of using e.opts) because it
// only applies to the SelectAddingInstances case.
func (e *mirroredCustomGroupSelector) selectInstances(
candidates []placement.Instance,
p placement.Placement,
addAllInstances bool,
) ([]placement.Instance, error) {
candidates, err := getValidCandidates(p, candidates, e.opts)
if err != nil {
return nil, err
}
groups, err := e.groupWithRF(candidates, p.ReplicaFactor())
if err != nil {
return nil, err
}
// no groups => no instances
if len(groups) == 0 {
return nil, nil
}
if !addAllInstances {
groups = groups[:1]
}
return assignShardsetsToGroupedInstances(groups, p), nil
}
func (e *mirroredCustomGroupSelector) groupWithRF(
candidates []placement.Instance,
rf int) ([][]placement.Instance, error) {
byGroupID, err := e.groupInstancesByID(candidates)
if err != nil {
return nil, err
}
groups := make([][]placement.Instance, 0, len(byGroupID))
// validate and convert to slice
for groupID, group := range byGroupID {
if len(group) > rf {
fullGroup := group
group = group[:rf]
var droppedIDs []string
for _, dropped := range fullGroup[rf:] {
droppedIDs = append(droppedIDs, dropped.ID())
}
e.logger.Warn(
"mirroredCustomGroupSelector: found more hosts than RF in group; "+
"using only RF hosts",
zap.Strings("droppedIDs", droppedIDs),
zap.String("groupID", groupID),
)
}
groups = append(groups, group)
}
return groups, nil
}
func (e *mirroredCustomGroupSelector) groupInstancesByID(candidates []placement.Instance) (map[string][]placement.Instance, error) {
byGroupID := make(map[string][]placement.Instance)
for _, candidate := range candidates {
groupID, err := e.getGroup(candidate)
if err != nil {
return nil, err
}
byGroupID[groupID] = append(byGroupID[groupID], candidate)
}
return byGroupID, nil
}
// small wrapper around e.instanceIDToGroupID providing context on error.
func (e *mirroredCustomGroupSelector) getGroup(inst placement.Instance) (string, error) {
groupID, err := e.instanceIDToGroupID(inst)
if err != nil {
return "", errors.Wrapf(err, "finding group for %s", inst.ID())
}
return groupID, nil
}
func newErrNoValidReplacement(leavingInstID string, groupID string) error {
return fmt.Errorf(
"leaving instance %s has no valid replacements in the same group (%s)",
leavingInstID,
groupID,
)
}