-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathpublish_state.go
160 lines (136 loc) · 4.21 KB
/
publish_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
package ifplugin
import (
"strings"
"github.com/pkg/errors"
"go.ligato.io/cn-infra/v2/datasync"
"go.ligato.io/cn-infra/v2/health/statuscheck"
"go.ligato.io/cn-infra/v2/health/statuscheck/model/status"
"go.ligato.io/vpp-agent/v3/proto/ligato/vpp"
interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
)
// watchStatusEvents watches for resync event of interface state data.
func (p *IfPlugin) watchStatusEvents() {
defer p.wg.Done()
p.Log.Debug("Start watching interface state events")
for {
select {
case e := <-p.resyncStatusChan:
p.onStatusResyncEvent(e)
case <-p.ctx.Done():
p.Log.Debug("Stop watching interface state events")
return
}
}
}
// onStatusResyncEvent is triggered during resync of interface state data
func (p *IfPlugin) onStatusResyncEvent(e datasync.ResyncEvent) {
p.Log.Debugf("received status resync event (%d prefixes)", len(e.GetValues()))
var wasError error
for prefix, vals := range e.GetValues() {
var keys []string
for {
x, stop := vals.GetNext()
if stop {
break
}
keys = append(keys, x.GetKey())
}
if len(keys) > 0 {
p.Log.Debugf("- %q (%v items)", prefix, len(keys))
err := p.resyncIfStateEvents(keys)
if err != nil {
wasError = err
}
} else {
p.Log.Debugf("- %q (no items)", prefix)
}
}
e.Done(wasError)
}
// resyncIfStateEvents deletes obsolete operation status of network interfaces in DB.
func (p *IfPlugin) resyncIfStateEvents(keys []string) error {
p.publishLock.Lock()
defer p.publishLock.Unlock()
p.Log.Debugf("resync interface state events with %d keys", len(keys))
for _, key := range keys {
ifaceName := strings.TrimPrefix(key, interfaces.StatePrefix)
if ifaceName == key {
continue
}
_, found := p.intfIndex.LookupByName(ifaceName)
if !found {
err := p.PublishStatistics.Put(key, nil /*means delete*/)
if err != nil {
return errors.WithMessagef(err, "publish statistic for key %s failed", key)
}
p.Log.Debugf("Obsolete interface status for %v deleted", key)
} else {
p.Log.WithField("ifaceName", ifaceName).Debug("interface status is needed")
}
}
return nil
}
// publishIfStateEvents goroutine is used to watch interface state notifications
// that are propagated to Messaging topic.
func (p *IfPlugin) publishIfStateEvents() {
defer p.wg.Done()
// store last errors to prevent repeating
var lastPublishErr error
var lastNotifErr error
for {
select {
case ifState := <-p.ifStateChan:
p.publishLock.Lock()
key := interfaces.InterfaceStateKey(ifState.State.Name)
if debugIfStates {
p.Log.Debugf("Publishing interface state: %+v", ifState)
}
if p.PublishStatistics != nil {
err := p.PublishStatistics.Put(key, ifState.State)
if err != nil {
if lastPublishErr == nil || lastPublishErr.Error() != err.Error() {
p.Log.Error(err)
}
}
lastPublishErr = err
}
// Note: state change is sometimes delivered as an unknown notification
stateChange := ifState.Type == interfaces.InterfaceNotification_UPDOWN ||
ifState.Type == interfaces.InterfaceNotification_UNKNOWN
// Marshall data into JSON & send kafka message.
if p.NotifyStates != nil && stateChange {
err := p.NotifyStates.Put(key, ifState.State)
if err != nil {
if lastNotifErr == nil || lastNotifErr.Error() != err.Error() {
p.Log.Error(err)
}
}
lastNotifErr = err
}
// Send interface state data to global agent status
if p.statusCheckReg && ifState.State.InternalName != "" {
p.StatusCheck.ReportStateChangeWithMeta(p.PluginName, statuscheck.OK, nil, &status.InterfaceStats_Interface{
InternalName: ifState.State.InternalName,
Index: ifState.State.IfIndex,
Status: ifState.State.AdminStatus.String(),
MacAddress: ifState.State.PhysAddress,
})
}
if stateChange || ifState.State.OperStatus == interfaces.InterfaceState_DELETED {
if debugIfStates {
p.Log.Debugf("Updating link state: %+v", ifState)
}
p.linkStateDescriptor.UpdateLinkState(ifState)
if p.PushNotification != nil {
p.PushNotification(&vpp.Notification{
Interface: ifState,
})
}
}
p.publishLock.Unlock()
case <-p.ctx.Done():
// Stop watching for state data updates.
return
}
}
}