forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_state.go
271 lines (235 loc) · 6.6 KB
/
unit_state.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
package registry
import (
"path"
"sort"
"time"
"github.com/coreos/fleet/etcd"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/unit"
)
const (
// Legacy namespace for unit states
statePrefix = "/state/"
// Namespace for unit states stored per-machine
statesPrefix = "/states/"
)
// legacyUnitStatePath returns the path where UnitState objects were formerly
// reported before being moved to a machine-specific namespace
// https://github.com/coreos/fleet/issues/638
func (r *EtcdRegistry) legacyUnitStatePath(jobName string) string {
return path.Join(r.keyPrefix, statePrefix, jobName)
}
// unitStatesNamespace generates a keypath of a namespace containing all
// UnitState objects for a particular job
func (r *EtcdRegistry) unitStatesNamespace(jobName string) string {
return path.Join(r.keyPrefix, statesPrefix, jobName)
}
// unitStatePath generates a keypath where the UnitState object for a given
// machine ID + jobName combination is stored
func (r *EtcdRegistry) unitStatePath(machID, jobName string) string {
return path.Join(r.unitStatesNamespace(jobName), machID)
}
// UnitStates returns a list of all UnitStates stored in the registry, sorted
// by unit name and then machine ID.
func (r *EtcdRegistry) UnitStates() (states []*unit.UnitState, err error) {
var mus map[MUSKey]*unit.UnitState
mus, err = r.statesByMUSKey()
if err != nil {
return
}
var sorted MUSKeys
for key, _ := range mus {
sorted = append(sorted, key)
}
sort.Sort(sorted)
for _, key := range sorted {
states = append(states, mus[key])
}
return
}
// MUSKey is used to index UnitStates by name + machineID
type MUSKey struct {
name string
machID string
}
// MUSKeys provides for sorting of UnitStates by their MUSKey
type MUSKeys []MUSKey
func (mk MUSKeys) Len() int { return len(mk) }
func (mk MUSKeys) Less(i, j int) bool {
mi := mk[i]
mj := mk[j]
return mi.name < mj.name || (mi.name == mj.name && mi.machID < mj.machID)
}
func (mk MUSKeys) Swap(i, j int) { mk[i], mk[j] = mk[j], mk[i] }
// statesByMUSKey returns a map of all UnitStates stored in the registry indexed by MUSKey
func (r *EtcdRegistry) statesByMUSKey() (map[MUSKey]*unit.UnitState, error) {
mus := make(map[MUSKey]*unit.UnitState)
// For backwards compatibility, first retrieve any states stored in the
// old format
req := etcd.Get{
Key: path.Join(r.keyPrefix, statePrefix),
Recursive: true,
}
res, err := r.etcd.Do(&req)
if err != nil && !isKeyNotFound(err) {
return nil, err
}
if res != nil {
for _, node := range res.Node.Nodes {
_, name := path.Split(node.Key)
var usm unitStateModel
if err := unmarshal(node.Value, &usm); err != nil {
log.Errorf("Error unmarshalling UnitState(%s): %v", name, err)
continue
}
us := modelToUnitState(&usm, name)
if us != nil {
key := MUSKey{name, us.MachineID}
mus[key] = us
}
}
}
// Now retrieve states stored in the new format and overlay them
req = etcd.Get{
Key: path.Join(r.keyPrefix, statesPrefix),
Recursive: true,
}
res, err = r.etcd.Do(&req)
if err != nil && !isKeyNotFound(err) {
return nil, err
}
if res != nil {
for _, dir := range res.Node.Nodes {
_, name := path.Split(dir.Key)
for _, node := range dir.Nodes {
_, machID := path.Split(node.Key)
var usm unitStateModel
if err := unmarshal(node.Value, &usm); err != nil {
log.Errorf("Error unmarshalling UnitState(%s) from Machine(%s): %v", name, machID, err)
continue
}
us := modelToUnitState(&usm, name)
if us != nil {
key := MUSKey{name, machID}
mus[key] = us
}
}
}
}
return mus, nil
}
// getUnitState retrieves the current UnitState of the provided Job's Unit
func (r *EtcdRegistry) getUnitState(jobName string) *unit.UnitState {
legacyKey := r.legacyUnitStatePath(jobName)
req := etcd.Get{
Key: legacyKey,
Recursive: true,
}
res, err := r.etcd.Do(&req)
if err != nil {
if !isKeyNotFound(err) {
log.Errorf("Error retrieving UnitState(%s): %v", jobName, err)
}
return nil
}
var usm unitStateModel
if err := unmarshal(res.Node.Value, &usm); err != nil {
log.Errorf("Error unmarshalling UnitState(%s): %v", jobName, err)
return nil
}
return modelToUnitState(&usm, jobName)
}
// SaveUnitState persists the given UnitState to the Registry
func (r *EtcdRegistry) SaveUnitState(jobName string, unitState *unit.UnitState, ttl time.Duration) {
usm := unitStateToModel(unitState)
if usm == nil {
log.Errorf("Unable to save nil UnitState model")
return
}
json, err := marshal(usm)
if err != nil {
log.Errorf("Error marshalling UnitState: %v", err)
return
}
legacyKey := r.legacyUnitStatePath(jobName)
req := etcd.Set{
Key: legacyKey,
Value: json,
TTL: ttl,
}
r.etcd.Do(&req)
newKey := r.unitStatePath(unitState.MachineID, jobName)
req = etcd.Set{
Key: newKey,
Value: json,
TTL: ttl,
}
r.etcd.Do(&req)
}
// Delete the state from the Registry for the given Job's Unit
func (r *EtcdRegistry) RemoveUnitState(jobName string) error {
// TODO(jonboulle): consider https://github.com/coreos/fleet/issues/465
legacyKey := r.legacyUnitStatePath(jobName)
req := etcd.Delete{
Key: legacyKey,
}
_, err := r.etcd.Do(&req)
if err != nil && !isKeyNotFound(err) {
return err
}
// TODO(jonboulle): deal properly with multiple states
newKey := r.unitStatesNamespace(jobName)
req = etcd.Delete{
Key: newKey,
Recursive: true,
}
_, err = r.etcd.Do(&req)
if err != nil && !isKeyNotFound(err) {
return err
}
return nil
}
type unitStateModel struct {
LoadState string `json:"loadState"`
ActiveState string `json:"activeState"`
SubState string `json:"subState"`
MachineState *machine.MachineState `json:"machineState"`
UnitHash string `json:"unitHash"`
}
func modelToUnitState(usm *unitStateModel, name string) *unit.UnitState {
if usm == nil {
return nil
}
us := unit.UnitState{
LoadState: usm.LoadState,
ActiveState: usm.ActiveState,
SubState: usm.SubState,
UnitHash: usm.UnitHash,
UnitName: name,
}
if usm.MachineState != nil {
us.MachineID = usm.MachineState.ID
}
return &us
}
func unitStateToModel(us *unit.UnitState) *unitStateModel {
if us == nil {
return nil
}
// Refuse to create a UnitState without a Hash
// See https://github.com/coreos/fleet/issues/720
//if len(us.UnitHash) == 0 {
// return nil
//}
usm := unitStateModel{
LoadState: us.LoadState,
ActiveState: us.ActiveState,
SubState: us.SubState,
UnitHash: us.UnitHash,
}
if us.MachineID != "" {
usm.MachineState = &machine.MachineState{ID: us.MachineID}
}
return &usm
}