-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_actions.go
317 lines (266 loc) · 8.84 KB
/
host_actions.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// 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.
package goalstate
import (
"context"
"time"
hpb "github.com/uber/peloton/.gen/peloton/api/v0/host"
"github.com/uber/peloton/pkg/common/goalstate"
"github.com/uber/peloton/pkg/hostmgr/common"
"github.com/uber/peloton/pkg/hostmgr/host/mesoshelper"
log "github.com/sirupsen/logrus"
)
// HostUntrack untracks the host by removing it
// from the goal state engine's entity map
func HostUntrack(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
gsDriver := hostEntity.driver
gsDriver.DeleteHost(hostEntity.hostname)
return nil
}
// HostRequeue re-enqueues the host into the goal state engine
// This is needed as host entity GetState() & GetGoalState() are reads from DB
// which can error out (e.g. DB unavailable), the retry mechanism is handled by
// re-enqueuing into the goal state engine.
// TODO: remove once host state & goalState backed by host cache
func HostRequeue(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
gsDriver := hostEntity.driver
gsDriver.DeleteHost(hostEntity.hostname)
// Since RequeueHost is called when entity state and/or goal state is invalid
// due to DB failure, sleeping acts as a static backoff before attempting
// new DB read
time.Sleep(1 * time.Minute)
gsDriver.EnqueueHost(hostEntity.hostname, time.Now())
return nil
}
// HostDrain is an idempotent action enqueuing the host
// for draining of its tasks
func HostDrain(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
hostname := hostEntity.hostname
gsDriver := hostEntity.driver
currentState := hostEntity.GetState().(*hostEntityState)
desiredState := hostEntity.GetGoalState().(*hostEntityState)
// Get IP from host record in DB
h, err := gsDriver.hostInfoOps.Get(ctx, hostname)
if err != nil {
return err
}
IP := h.GetIp()
// Taking lock to update Mesos Master maintenance schedule
// This is required as each host action HostDrain appends
// a new host to the schedule on top of the existing schedule
// and Mesos Master allows schedule overwrite, so concurrency control
// is required
gsDriver.maintenanceScheduleLock.Lock()
defer gsDriver.maintenanceScheduleLock.Unlock()
if err := mesoshelper.AddHostToMaintenanceSchedule(
gsDriver.mesosMasterClient,
hostname,
IP,
); err != nil {
return err
}
hostInfoDiff := common.HostInfoDiff{
common.StateField: hpb.HostState_HOST_STATE_DRAINING,
}
compareFields := map[string]interface{}{
common.StateField: currentState.hostState,
common.GoalStateField: desiredState.hostState,
}
// Set host state to DRAINING in DB
if err := gsDriver.hostInfoOps.CompareAndSet(
ctx,
hostname,
hostInfoDiff,
compareFields,
); err != nil {
return err
}
// No need to enqueue into goal state as the host needs to first be fully drained
// to be then updated to drained state and enqueued again into
// the goal state engine
log.WithFields(log.Fields{
"hostname": hostname,
"action_name": "HostDrain",
}).Info("goal state action succeeded")
return nil
}
// HostDown is an idempotent action registering the host as DOWN in maintenance
func HostDown(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
hostname := hostEntity.hostname
gsDriver := hostEntity.driver
currentState := hostEntity.GetState().(*hostEntityState)
desiredState := hostEntity.GetGoalState().(*hostEntityState)
// Get IP from host record in DB
h, err := gsDriver.hostInfoOps.Get(ctx, hostname)
if err != nil {
return err
}
IP := h.GetIp()
// Register host as down
if err := mesoshelper.RegisterHostAsDown(gsDriver.mesosMasterClient, hostname, IP); err != nil {
return err
}
hostInfoDiff := common.HostInfoDiff{
common.StateField: hpb.HostState_HOST_STATE_DOWN,
}
compareFields := map[string]interface{}{
common.StateField: currentState.hostState,
common.GoalStateField: desiredState.hostState,
}
// Set host current state to DOWN in DB
if err = gsDriver.hostInfoOps.CompareAndSet(
ctx,
hostname,
hostInfoDiff,
compareFields,
); err != nil {
return err
}
// Enqueue into goal state engine for host untracking since
// state converged to goal state
gsDriver.EnqueueHost(hostname, time.Now())
log.WithFields(log.Fields{
"hostname": hostname,
"action_name": "HostDown",
}).Info("goal state action succeeded")
return nil
}
// HostUp is an idempotent action registering the host as UP, out of maintenance
func HostUp(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
hostname := hostEntity.hostname
gsDriver := hostEntity.driver
currentState := hostEntity.GetState().(*hostEntityState)
desiredState := hostEntity.GetGoalState().(*hostEntityState)
// Get IP from host record in DB
h, err := gsDriver.hostInfoOps.Get(ctx, hostname)
if err != nil {
return err
}
ip := h.GetIp()
// Register host as up
if err := mesoshelper.RegisterHostAsUp(
gsDriver.mesosMasterClient,
hostname,
ip,
); err != nil {
return err
}
hostInfoDiff := common.HostInfoDiff{
common.StateField: hpb.HostState_HOST_STATE_UP,
}
compareFields := map[string]interface{}{
common.StateField: currentState.hostState,
common.GoalStateField: desiredState.hostState,
}
// Set host state to UP in DB
if err := gsDriver.hostInfoOps.CompareAndSet(
ctx,
hostname,
hostInfoDiff,
compareFields,
); err != nil {
return err
}
// Enqueue host to goalstate engine for evaluation
gsDriver.EnqueueHost(hostname, time.Now())
log.WithFields(log.Fields{
"hostname": hostname,
"action_name": "HostUp",
}).Info("goal state action succeeded")
return nil
}
// HostTriggerMaintenance will make the goal state to DOWN by that we can start draining
func HostTriggerMaintenance(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
hostname := hostEntity.hostname
gsDriver := hostEntity.driver
currentState := hostEntity.GetState().(*hostEntityState)
desiredState := hostEntity.GetGoalState().(*hostEntityState)
hostInfoDiff := common.HostInfoDiff{
common.GoalStateField: hpb.HostState_HOST_STATE_DOWN,
}
compareFields := map[string]interface{}{
common.StateField: currentState.hostState,
common.GoalStateField: desiredState.hostState,
common.CurrentPoolField: currentState.hostPool,
common.DesiredPoolField: desiredState.hostPool,
}
// Set host goal state to DOWN in DB
if err := gsDriver.hostInfoOps.CompareAndSet(
ctx,
hostname,
hostInfoDiff,
compareFields,
); err != nil {
return err
}
// Enqueue into goal state engine for host by that
// state converged to goal state
gsDriver.EnqueueHost(hostname, time.Now())
return nil
}
// HostChangePool action will set the host's current pool to its desired pool
// And update the goal state to UP and enqueue the host to reevaluation.
func HostChangePool(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
hostname := hostEntity.hostname
gsDriver := hostEntity.driver
currentState := hostEntity.GetState().(*hostEntityState)
desiredState := hostEntity.GetGoalState().(*hostEntityState)
// Changing the host pool
if err := gsDriver.hostPoolMgr.ChangeHostPool(hostname,
currentState.hostPool,
desiredState.hostPool); err != nil {
return err
}
hostInfoDiff := common.HostInfoDiff{
common.GoalStateField: hpb.HostState_HOST_STATE_UP,
}
compareFields := map[string]interface{}{
common.StateField: currentState.hostState,
common.GoalStateField: desiredState.hostState,
common.CurrentPoolField: currentState.hostPool,
common.DesiredPoolField: desiredState.hostPool,
}
// Set host goal state to UP in DB
if err := gsDriver.hostInfoOps.CompareAndSet(
ctx,
hostname,
hostInfoDiff,
compareFields,
); err != nil {
return err
}
// Enqueue into goal state engine for host by that
// state converged to goal state
gsDriver.EnqueueHost(hostname, time.Now())
return nil
}
// HostInvalidAction action is a error state transition and should
// not happen
func HostInvalidAction(ctx context.Context, entity goalstate.Entity) error {
hostEntity := entity.(*hostEntity)
hostname := hostEntity.hostname
log.WithFields(log.Fields{
"hostname": hostname,
"state": hostEntity.GetState(),
"GoalState": hostEntity.GetGoalState(),
}).Error("Unexpected host state")
return nil
}