-
Notifications
You must be signed in to change notification settings - Fork 125
/
node_utils.go
359 lines (321 loc) · 9.91 KB
/
node_utils.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
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
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// 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 kvscheduler
import (
"google.golang.org/protobuf/proto"
kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
"go.ligato.io/vpp-agent/v3/plugins/kvscheduler/internal/graph"
"go.ligato.io/vpp-agent/v3/plugins/kvscheduler/internal/utils"
"go.ligato.io/vpp-agent/v3/proto/ligato/kvscheduler"
)
func nodeToKVPairWithMetadata(node graph.Node) kvs.KVWithMetadata {
return kvs.KVWithMetadata{
Key: node.GetKey(),
Value: node.GetValue(),
Metadata: node.GetMetadata(),
Origin: getNodeOrigin(node),
}
}
func nodesToKVPairsWithMetadata(nodes []graph.Node) (kvPairs []kvs.KVWithMetadata) {
for _, node := range nodes {
kvPairs = append(kvPairs, nodeToKVPairWithMetadata(node))
}
return kvPairs
}
// constructTargets builds targets for the graph based on derived values and dependencies.
func constructTargets(deps []kvs.Dependency, derives []kvs.KeyValuePair) (targets []graph.RelationTargetDef) {
targets = make([]graph.RelationTargetDef, 0, len(deps)+len(derives))
for _, dep := range deps {
target := graph.RelationTargetDef{
Relation: DependencyRelation,
Label: dep.Label,
Key: dep.Key,
Selector: graph.TargetSelector{
KeyPrefixes: dep.AnyOf.KeyPrefixes,
KeySelector: dep.AnyOf.KeySelector,
},
}
targets = append(targets, target)
}
for _, derived := range derives {
target := graph.RelationTargetDef{
Relation: DerivesRelation,
Label: derived.Key,
Key: derived.Key,
}
targets = append(targets, target)
}
return targets
}
// equalValueDetails compares value state details for equality.
func equalValueDetails(details1, details2 []string) bool {
if len(details1) != len(details2) {
return false
}
for _, d1 := range details1 {
found := false
for _, d2 := range details2 {
if d1 == d2 {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// getValueDetails returns further details about the value state.
func getValueDetails(node graph.Node) (details []string) {
state := getNodeState(node)
_, err := getNodeError(node)
if state == kvscheduler.ValueState_INVALID {
if ivErr, isIVErr := err.(*kvs.InvalidValueError); isIVErr {
details = ivErr.GetInvalidFields()
return
}
}
if state == kvscheduler.ValueState_PENDING {
for _, targets := range node.GetTargets(DependencyRelation) {
satisfied := false
for _, target := range targets.Nodes {
if isNodeAvailable(target) {
satisfied = true
}
}
if !satisfied {
details = append(details, targets.Label)
}
}
}
return details
}
// getValueStatus reads the value status from the corresponding node.
func getValueStatus(node graph.Node, key string) *kvscheduler.BaseValueStatus {
status := &kvscheduler.BaseValueStatus{
Value: &kvscheduler.ValueStatus{
Key: key,
},
}
status.Value.State = getNodeState(node)
if status.Value.State == kvscheduler.ValueState_NONEXISTENT {
// nothing else to get for non-existent value
return status
}
_, err := getNodeError(node)
if err != nil {
status.Value.Error = err.Error()
}
status.Value.LastOperation = getNodeLastOperation(node)
status.Value.State = getNodeState(node)
status.Value.Details = getValueDetails(node)
// derived nodes
if !isNodeDerived(node) {
for _, derivedNode := range getDerivedNodes(node) {
derValStatus := getValueStatus(derivedNode, derivedNode.GetKey())
status.DerivedValues = append(status.DerivedValues, derValStatus.Value)
}
}
return status
}
// functions returns selectors selecting non-derived NB values.
func nbBaseValsSelectors() []graph.FlagSelector {
return []graph.FlagSelector{
graph.WithoutFlags(&DerivedFlag{}),
graph.WithoutFlags(&ValueStateFlag{kvscheduler.ValueState_OBTAINED}),
}
}
// functions returns selectors selecting non-derived SB values.
func sbBaseValsSelectors() []graph.FlagSelector {
return []graph.FlagSelector{
graph.WithoutFlags(&DerivedFlag{}),
graph.WithFlags(&ValueStateFlag{kvscheduler.ValueState_OBTAINED}),
}
}
// function returns selectors selecting non-derived values belonging to the given
// descriptor.
func descrValsSelectors(descriptor string, onlyAvailable bool) []graph.FlagSelector {
descrSel := graph.WithFlags(&DescriptorFlag{descriptor})
baseSel := graph.WithoutFlags(&DerivedFlag{})
if onlyAvailable {
return []graph.FlagSelector{
descrSel, baseSel, graph.WithoutFlags(&UnavailValueFlag{}),
}
}
return []graph.FlagSelector{descrSel, baseSel}
}
// getNodeState returns state stored in the ValueState flag.
func getNodeState(node graph.Node) kvscheduler.ValueState {
if node != nil {
flag := node.GetFlag(ValueStateFlagIndex)
if flag != nil {
return flag.(*ValueStateFlag).valueState
}
}
return kvscheduler.ValueState_NONEXISTENT
}
func valueStateToOrigin(state kvscheduler.ValueState) kvs.ValueOrigin {
switch state {
case kvscheduler.ValueState_NONEXISTENT:
return kvs.UnknownOrigin
case kvscheduler.ValueState_OBTAINED:
return kvs.FromSB
}
return kvs.FromNB
}
// getNodeOrigin returns node origin based on the value state.
func getNodeOrigin(node graph.Node) kvs.ValueOrigin {
state := getNodeState(node)
return valueStateToOrigin(state)
}
// getNodeError returns node error stored in Error flag.
func getNodeError(node graph.Node) (retriable bool, err error) {
if node != nil {
errorFlag := node.GetFlag(ErrorFlagIndex)
if errorFlag != nil {
flag := errorFlag.(*ErrorFlag)
return flag.retriable, flag.err
}
}
return false, nil
}
// getNodeErrorString returns node error stored in Error flag as string.
func getNodeErrorString(node graph.Node) string {
_, err := getNodeError(node)
if err == nil {
return ""
}
return err.Error()
}
// getNodeLastUpdate returns info about the last update for a given node, stored in LastUpdate flag.
func getNodeLastUpdate(node graph.Node) *LastUpdateFlag {
if node == nil {
return nil
}
flag := node.GetFlag(LastUpdateFlagIndex)
if flag == nil {
return nil
}
return flag.(*LastUpdateFlag)
}
// getNodeLastAppliedValue return the last applied value for the given node
func getNodeLastAppliedValue(node graph.Node) proto.Message {
lastUpdate := getNodeLastUpdate(node)
if lastUpdate == nil {
return nil
}
return lastUpdate.value
}
// getNodeLastOperation returns last operation executed over the given node.
func getNodeLastOperation(node graph.Node) kvscheduler.TxnOperation {
if node != nil && getNodeState(node) != kvscheduler.ValueState_OBTAINED {
lastUpdate := getNodeLastUpdate(node)
if lastUpdate != nil {
return lastUpdate.txnOp
}
}
return kvscheduler.TxnOperation_UNDEFINED
}
func isNodeDerived(node graph.Node) bool {
return node.GetFlag(DerivedFlagIndex) != nil
}
func getNodeBaseKey(node graph.Node) string {
flag := node.GetFlag(DerivedFlagIndex)
if flag == nil {
return node.GetKey()
}
return flag.(*DerivedFlag).baseKey
}
// isNodePending checks whether the node is available for dependency resolution.
func isNodeAvailable(node graph.Node) bool {
if node == nil {
return false
}
return node.GetFlag(UnavailValueFlagIndex) == nil
}
// isNodeReady return true if the given node has all dependencies satisfied.
// Recursive calls are needed to handle circular dependencies - nodes of a strongly
// connected component are treated as if they were squashed into one.
func isNodeReady(node graph.Node) bool {
if getNodeOrigin(node) == kvs.FromSB {
// for SB values dependencies are not checked
return true
}
ready, _ := isNodeReadyRec(node, 0, make(map[string]int), false)
return ready
}
// isNodeReadyRec is a recursive call from within isNodeReady.
// visited = map{ key -> depth }
func isNodeReadyRec(node graph.Node, depth int, visited map[string]int, checkSCC bool) (ready bool, cycleDepth int) {
if targetDepth, wasVisited := visited[node.GetKey()]; wasVisited {
return true, targetDepth
}
cycleDepth = depth
visited[node.GetKey()] = depth
defer delete(visited, node.GetKey())
ready = true // for zero dependencies
var satisfiedLabel bool
cb := func(target graph.Node, label string) (skipLabel, abort bool) {
if target == nil { // end of the available targets for this label
if !satisfiedLabel {
ready = false
abort = true
return
}
satisfiedLabel = false // clear for the next label
return
}
if getNodeState(target) == kvscheduler.ValueState_REMOVED {
// do not consider values that are (being) removed
return
}
if isNodeAvailable(target) {
satisfiedLabel = true
if !checkSCC {
skipLabel = true
return
}
}
// test if node is inside a strongly-connected component (treated as one node)
targetReady, targetCycleDepth := isNodeReadyRec(target, depth+1, visited, true)
if targetReady && targetCycleDepth <= depth {
// this node is reachable from the target
satisfiedLabel = true
if targetCycleDepth < cycleDepth {
// update how far back in the branch this node can reach following dependencies
cycleDepth = targetCycleDepth
}
}
return
}
node.IterTargets(DependencyRelation, cb)
return
}
func canNodeHaveMetadata(node graph.Node) bool {
return !isNodeDerived(node)
}
func getDerivedNodes(node graph.Node) (derived []graph.Node) {
for _, derivedNodes := range node.GetTargets(DerivesRelation) {
derived = append(derived, derivedNodes.Nodes...)
}
return derived
}
func getDerivedKeys(node graph.Node) utils.KeySet {
set := utils.NewSliceBasedKeySet()
for _, derived := range getDerivedNodes(node) {
set.Add(derived.GetKey())
}
return set
}