forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevstate.go
More file actions
383 lines (360 loc) · 10.8 KB
/
Copy pathdevstate.go
File metadata and controls
383 lines (360 loc) · 10.8 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (c) 2026 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package evetest
import (
"strings"
"time"
eveinfo "github.com/lf-edge/eve-api/go/info"
evemetrics "github.com/lf-edge/eve-api/go/metrics"
"github.com/lf-edge/eve/evetest/controller"
api "github.com/lf-edge/eve/evetest/grpcapi/go"
uuid "github.com/satori/go.uuid"
)
// deviceStateEvent carries a single device state update delivered to
// processDeviceStateEvents via deviceStateCh.
// Exactly one of infoMsg, metricMsg, or reqEvent is non-nil.
type deviceStateEvent struct {
devName string
infoMsg *eveinfo.ZInfoMsg
metricMsg *evemetrics.ZMetricMsg
reqEvent *controller.ReqEvent
}
// startDeviceStateWatcher subscribes to ZiDevice info messages and API request
// events for a newly onboarded device, and starts forwarding goroutines that
// relay them to deviceStateCh.
func (th *TestHarness) startDeviceStateWatcher(devName string, devUUID uuid.UUID) {
infoCh := make(chan *eveinfo.ZInfoMsg, 16)
unsubInfo, err := th.adamClient.SubscribeToDeviceInfoMsgs(devUUID,
func(msg *eveinfo.ZInfoMsg) bool {
switch msg.GetZtype() {
case eveinfo.ZInfoTypes_ZiDevice,
eveinfo.ZInfoTypes_ZiApp,
eveinfo.ZInfoTypes_ZiNetworkInstance:
return true
}
return false
}, infoCh)
if err != nil {
th.log.Errorf("Failed to subscribe to device info for %q: %v", devName, err)
return
}
reqCh := make(chan *controller.ReqEvent, 16)
unsubReq, err := th.adamClient.SubscribeToDeviceRequests(devUUID, reqCh)
if err != nil {
unsubInfo()
th.log.Errorf("Failed to subscribe to device requests for %q: %v", devName, err)
return
}
metricsCh := make(chan *evemetrics.ZMetricMsg, 16)
unsubMetrics, err := th.adamClient.SubscribeToDeviceMetrics(devUUID, metricsCh)
if err != nil {
unsubInfo()
unsubReq()
th.log.Errorf("Failed to subscribe to device metrics for %q: %v", devName, err)
return
}
th.devicesM.Lock()
th.devices[devName].unsubscribeInfo = unsubInfo
th.devices[devName].unsubscribeReq = unsubReq
th.devices[devName].unsubscribeMetrics = unsubMetrics
th.devicesM.Unlock()
th.wg.Add(3)
go func() {
defer th.wg.Done()
for {
select {
case msg, ok := <-infoCh:
if !ok {
return
}
select {
case th.deviceStateCh <- deviceStateEvent{devName: devName, infoMsg: msg}:
case <-th.ctx.Done():
return
}
case <-th.ctx.Done():
return
}
}
}()
go func() {
defer th.wg.Done()
for {
select {
case msg, ok := <-metricsCh:
if !ok {
return
}
select {
case th.deviceStateCh <- deviceStateEvent{devName: devName, metricMsg: msg}:
case <-th.ctx.Done():
return
}
case <-th.ctx.Done():
return
}
}
}()
go func() {
defer th.wg.Done()
for {
select {
case ev, ok := <-reqCh:
if !ok {
return
}
select {
case th.deviceStateCh <- deviceStateEvent{devName: devName, reqEvent: ev}:
case <-th.ctx.Done():
return
}
case <-th.ctx.Done():
return
}
}
}()
}
// processDeviceStateEvents is a goroutine that processes device info and request events,
// keeping deviceState.state, deviceState.interfaces, and deviceState.lastRequestAt
// up to date for all onboarded devices.
func (th *TestHarness) processDeviceStateEvents() {
defer th.wg.Done()
suspectTicker := time.NewTicker(30 * time.Second)
defer suspectTicker.Stop()
for {
select {
case event := <-th.deviceStateCh:
switch {
case event.infoMsg != nil:
th.handleDeviceInfoEvent(event.devName, event.infoMsg)
case event.metricMsg != nil:
th.handleDeviceMetricsEvent(event.devName, event.metricMsg)
default:
th.handleDeviceRequestEvent(event.devName, event.reqEvent)
}
case <-suspectTicker.C:
th.checkSuspectDevices()
case <-th.ctx.Done():
return
}
}
}
func (th *TestHarness) handleDeviceInfoEvent(devName string, msg *eveinfo.ZInfoMsg) {
th.devicesM.Lock()
defer th.devicesM.Unlock()
dev, ok := th.devices[devName]
if !ok {
return
}
switch msg.GetZtype() {
case eveinfo.ZInfoTypes_ZiDevice:
dinfo := msg.GetDinfo()
if dinfo == nil {
return
}
newState := zDeviceStateToEVEDeviceState(
dinfo.GetState(), dinfo.GetSwList(), dev.lastRequestAt)
if dev.state != newState {
th.log.Infof("Device %s state changed: %s -> %s",
devName, shortDeviceState(dev.state), shortDeviceState(newState))
dev.state = newState
}
dev.interfaces = extractInterfacesFromSystemAdapter(dinfo.GetSystemAdapter())
if ts := dinfo.GetBootTime(); ts != nil {
bootTime := ts.AsTime()
if !dev.lastBootTime.IsZero() && !bootTime.Equal(dev.lastBootTime) {
diff := bootTime.Sub(dev.lastBootTime)
// bootTime can jitter by ±1s between successive messages during
// the same boot (gopsutil reads btime differently on each call).
// Only count a genuine reboot when the change is at least 5 seconds.
if diff >= 5*time.Second {
dev.rebootCount++
th.log.Infof("Device %s rebooted "+
"(boot time: %s, previous boot time: %s), "+
"total observed reboots this test: %d",
devName, bootTime.Format(time.RFC3339),
dev.lastBootTime.Format(time.RFC3339),
dev.rebootCount)
}
}
dev.lastBootTime = bootTime
}
case eveinfo.ZInfoTypes_ZiApp:
ainfo := msg.GetAinfo()
if ainfo == nil {
return
}
if dev.deployedApps == nil {
dev.deployedApps = make(map[string]eveinfo.ZSwState)
}
appID := ainfo.GetAppID()
state := ainfo.GetState()
if state == eveinfo.ZSwState_INVALID {
delete(dev.deployedApps, appID)
} else {
dev.deployedApps[appID] = state
}
if dev.deployCond != nil {
dev.deployCond.Broadcast()
}
case eveinfo.ZInfoTypes_ZiNetworkInstance:
niInfo := msg.GetNiinfo()
if niInfo == nil {
return
}
if dev.deployedNIs == nil {
dev.deployedNIs = make(map[string]eveinfo.ZNetworkInstanceState)
}
niID := niInfo.GetNetworkID()
state := niInfo.GetState()
if state == eveinfo.ZNetworkInstanceState_ZNETINST_STATE_UNSPECIFIED {
delete(dev.deployedNIs, niID)
} else {
dev.deployedNIs[niID] = state
}
if dev.deployCond != nil {
dev.deployCond.Broadcast()
}
}
}
func (th *TestHarness) handleDeviceRequestEvent(devName string, ev *controller.ReqEvent) {
th.devicesM.Lock()
defer th.devicesM.Unlock()
dev, ok := th.devices[devName]
if !ok {
return
}
if ev.Timestamp.After(dev.lastRequestAt) {
dev.lastRequestAt = ev.Timestamp
}
}
func (th *TestHarness) handleDeviceMetricsEvent(devName string, msg *evemetrics.ZMetricMsg) {
dm := msg.GetDm()
if dm == nil {
return
}
lpc := dm.GetLastProcessedConfig()
if lpc == nil {
return
}
ts := lpc.AsTime()
if ts.IsZero() {
return
}
th.devicesM.Lock()
defer th.devicesM.Unlock()
dev, ok := th.devices[devName]
if !ok {
return
}
if ts.After(dev.lastProcessedConfigTs) {
dev.lastProcessedConfigTs = ts
if dev.configAppliedCond != nil {
dev.configAppliedCond.Broadcast()
}
}
}
// incExpectedRebootCount increments the expected reboot counter for the named device.
func (th *TestHarness) incExpectedRebootCount(devName string) {
th.devicesM.Lock()
defer th.devicesM.Unlock()
if dev, ok := th.devices[devName]; ok {
dev.expectedRebootCount++
}
}
// checkRebootCounts compares the observed reboot count against the expected
// reboot count for every onboarded device. A mismatch indicates either an
// unexpected reboot (device crashed) or a requested reboot that never occurred.
func (th *TestHarness) checkRebootCounts() {
th.devicesM.Lock()
defer th.devicesM.Unlock()
for devName, dev := range th.devices {
if dev.ID == uuid.Nil {
continue
}
if dev.rebootCount != dev.expectedRebootCount {
th.t.Errorf("Device %q: expected %d reboot(s) but observed %d",
devName, dev.expectedRebootCount, dev.rebootCount)
}
}
}
// checkSuspectDevices marks devices as SUSPECT if they have not made a
// controller request in the last 5 minutes.
func (th *TestHarness) checkSuspectDevices() {
th.devicesM.Lock()
defer th.devicesM.Unlock()
for devName, dev := range th.devices {
if dev.ID == uuid.Nil || dev.unsubscribeInfo == nil {
continue
}
if !dev.lastRequestAt.IsZero() && time.Since(dev.lastRequestAt) > 5*time.Minute {
if dev.state != api.EVEDeviceState_EVE_DEVICE_STATE_SUSPECT {
th.log.Warnf("Device %s state changed: %s -> SUSPECT",
devName, shortDeviceState(dev.state))
dev.state = api.EVEDeviceState_EVE_DEVICE_STATE_SUSPECT
}
}
}
}
func shortDeviceState(s api.EVEDeviceState) string {
return strings.TrimPrefix(s.String(), "EVE_DEVICE_STATE_")
}
// zDeviceStateToEVEDeviceState maps a ZDeviceState to an EVEDeviceState,
// applying overrides for TESTING (activated SW under update testing) and
// SUSPECT (no controller request in last 5 minutes).
func zDeviceStateToEVEDeviceState(zState eveinfo.ZDeviceState,
swList []*eveinfo.ZInfoDevSW, lastRequestAt time.Time) api.EVEDeviceState {
// SUSPECT: no request in last 5 minutes.
if !lastRequestAt.IsZero() && time.Since(lastRequestAt) > 5*time.Minute {
return api.EVEDeviceState_EVE_DEVICE_STATE_SUSPECT
}
// TESTING: activated SW is under update testing.
for _, sw := range swList {
if sw.GetActivated() && sw.GetSubStatus() == eveinfo.BaseOsSubStatus_UPDATE_TESTING {
return api.EVEDeviceState_EVE_DEVICE_STATE_TESTING
}
}
switch zState {
case eveinfo.ZDeviceState_ZDEVICE_STATE_ONLINE:
return api.EVEDeviceState_EVE_DEVICE_STATE_ONLINE
case eveinfo.ZDeviceState_ZDEVICE_STATE_REBOOTING:
return api.EVEDeviceState_EVE_DEVICE_STATE_REBOOTING
case eveinfo.ZDeviceState_ZDEVICE_STATE_MAINTENANCE_MODE:
return api.EVEDeviceState_EVE_DEVICE_STATE_MAINTENANCE_MODE
case eveinfo.ZDeviceState_ZDEVICE_STATE_BASEOS_UPDATING:
return api.EVEDeviceState_EVE_DEVICE_STATE_UPGRADING
case eveinfo.ZDeviceState_ZDEVICE_STATE_BOOTING:
return api.EVEDeviceState_EVE_DEVICE_STATE_BOOTING
case eveinfo.ZDeviceState_ZDEVICE_STATE_PREPARING_POWEROFF:
return api.EVEDeviceState_EVE_DEVICE_STATE_PREPARING_POWEROFF
case eveinfo.ZDeviceState_ZDEVICE_STATE_POWERING_OFF:
return api.EVEDeviceState_EVE_DEVICE_STATE_POWERING_OFF
case eveinfo.ZDeviceState_ZDEVICE_STATE_PREPARED_POWEROFF:
return api.EVEDeviceState_EVE_DEVICE_STATE_PREPARED_POWEROFF
default:
return api.EVEDeviceState_EVE_DEVICE_STATE_UNDEFINED
}
}
// extractInterfacesFromSystemAdapter builds the interface status list from
// the active DevicePortStatus (identified by CurrentIndex) in SystemAdapterInfo.
func extractInterfacesFromSystemAdapter(sa *eveinfo.SystemAdapterInfo) []*api.EVEInterfaceStatus {
if sa == nil {
return nil
}
statuses := sa.GetStatus()
idx := int(sa.GetCurrentIndex())
if idx >= len(statuses) {
return nil
}
ports := statuses[idx].GetPorts()
interfaces := make([]*api.EVEInterfaceStatus, 0, len(ports))
for _, p := range ports {
interfaces = append(interfaces, &api.EVEInterfaceStatus{
LogicalLabel: p.GetName(),
IpAddresses: p.GetIPAddrs(),
MacAddress: p.GetMacAddr(),
Up: p.GetUp(),
})
}
return interfaces
}