-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathrefresh.go
577 lines (525 loc) · 18.2 KB
/
refresh.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// 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 (
"fmt"
"os"
"strings"
"go.ligato.io/cn-infra/v2/logging"
"google.golang.org/protobuf/encoding/prototext"
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"
)
var enableGraphDump = os.Getenv("KVSCHEDULER_GRAPHDUMP") != ""
const (
nodeVisitBeginMark = "[BEGIN]"
nodeVisitEndMark = "[END]"
)
// resyncData stores data to be used for resync after refresh.
type resyncData struct {
first bool // true if startup-resync
values []kvForTxn
}
// refreshGraph updates all/some values in the graph to their *real* state
// using the Retrieve methods from descriptors.
func (s *Scheduler) refreshGraph(graphW graph.RWAccess,
keys utils.KeySet, resyncData *resyncData, verbose bool,
) {
if s.logGraphWalk {
keysToRefresh := "<ALL>"
if keys != nil && keys.Length() > 0 {
keysToRefresh = keys.String()
}
msg := fmt.Sprintf("refreshGrap (keys=%s)", keysToRefresh)
fmt.Printf("%s %s\n", nodeVisitBeginMark, msg)
defer fmt.Printf("%s %s\n", nodeVisitEndMark, msg)
}
refreshedKeys := utils.NewMapBasedKeySet()
// iterate over all descriptors, in order given by retrieve dependencies
for _, descriptor := range s.registry.GetAllDescriptors() {
handler := newDescriptorHandler(descriptor)
// get base values for this descriptor from memory before refresh
// (including those marked as unavailable which may need metadata update)
descrNodes := graphW.GetNodes(nil,
descrValsSelectors(descriptor.Name, false)...)
// check if this descriptor's key space should be refreshed as well
var skip bool
if keys != nil {
skip = keys.Length() > 0
for _, key := range keys.Iterate() {
if descriptor.KeySelector(key) {
skip = false
break
}
}
}
if skip {
// nothing to refresh in the key space of this descriptor
s.skipRefresh(descrNodes, nil, refreshedKeys)
continue
}
// get key-value pairs for correlation
var correlateCap int
if resyncData != nil && resyncData.first {
correlateCap = len(resyncData.values)
} else {
correlateCap = len(descrNodes)
}
correlate := make([]kvs.KVWithMetadata, 0, correlateCap)
if resyncData != nil && resyncData.first {
// for startup resync, use data received from NB
for _, kv := range resyncData.values {
if descriptor.KeySelector(kv.key) {
correlate = append(correlate,
kvs.KVWithMetadata{
Key: kv.key,
Value: kv.value,
Origin: kv.origin,
})
}
}
} else {
// for refresh of failed values or run-time resync, use in-memory
// kv-pairs for correlation
for _, node := range descrNodes {
if isNodeAvailable(node) {
correlate = append(correlate, nodeToKVPairWithMetadata(node))
}
}
}
// execute Retrieve operation
retrieved, ableToRetrieve, err := handler.retrieve(correlate)
// mark un-retrievable as refreshed
if !ableToRetrieve || err != nil {
l := s.Log.WithField("descriptor", descriptor.Name)
if err != nil {
l.Errorf("failed to retrieve values: %v", err)
l.Debugf("skipping refresh for the descriptor")
}
s.skipRefresh(descrNodes, nil, refreshedKeys)
continue
} else if verbose {
plural := "s"
if len(retrieved) == 1 {
plural = ""
}
var list strings.Builder
for i, d := range retrieved {
num := fmt.Sprintf("%d.", i+1)
list.WriteString(fmt.Sprintf("\n - %3s [%s]: %q (%s)\n %v",
num, descriptor.Name, d.Key, d.Origin, utils.ProtoToString(d.Value)))
if d.Metadata != nil {
list.WriteString(fmt.Sprintf("\n Metadata: %+v", d.Metadata))
}
}
s.Log.Debugf("%s descriptor retrieved %d item%s: %v",
descriptor.Name, len(retrieved), plural, list.String())
}
if keys != nil && keys.Length() > 0 {
// mark keys that should not be touched as refreshed
s.skipRefresh(descrNodes, keys, refreshedKeys)
}
// process retrieved kv-pairs
for _, retrievedKV := range retrieved {
if keys != nil && keys.Length() > 0 {
// do no touch values that aren't meant to be refreshed
if toRefresh := keys.Has(retrievedKV.Key); !toRefresh {
continue
}
}
if !s.validRetrievedKV(retrievedKV, descriptor, refreshedKeys) {
continue
}
// 1st attempt to determine value origin
if retrievedKV.Origin == kvs.UnknownOrigin {
// determine value origin based on the values for correlation
for _, kv := range correlate {
if kv.Key == retrievedKV.Key {
retrievedKV.Origin = kv.Origin
break
}
}
}
// 2nd attempt to determine value origin
if retrievedKV.Origin == kvs.UnknownOrigin {
// determine value origin based on the last revision
timeline := graphW.GetNodeTimeline(retrievedKV.Key)
if len(timeline) > 0 {
lastRev := timeline[len(timeline)-1]
valueStateFlag := lastRev.Flags.GetFlag(ValueStateFlagIndex)
valueState := valueStateFlag.(*ValueStateFlag).valueState
retrievedKV.Origin = valueStateToOrigin(valueState)
}
}
if retrievedKV.Origin == kvs.UnknownOrigin {
// will assume this is from SB
retrievedKV.Origin = kvs.FromSB
}
// refresh node that represents this kv-pair
s.refreshValue(graphW, retrievedKV, handler, refreshedKeys, 2)
}
// unset the metadata from base NB values that do not actually exists
for _, node := range descrNodes {
if refreshed := refreshedKeys.Has(node.GetKey()); !refreshed {
if getNodeOrigin(node) == kvs.FromNB && node.GetMetadata() != nil {
if s.logGraphWalk {
fmt.Printf(" -> unset metadata for key=%s\n", node.GetKey())
}
missingNode := graphW.SetNode(node.GetKey())
missingNode.SetMetadata(nil)
}
}
}
}
// update state of values that do not actually exist
for _, node := range graphW.GetNodes(nil) {
if refreshed := refreshedKeys.Has(node.GetKey()); refreshed {
continue
}
s.refreshUnavailNode(graphW, node, refreshedKeys, 2)
}
if enableGraphDump && verbose && s.config.PrintTxnSummary {
fmt.Println(dumpGraph(graphW))
}
}
// refreshValue refreshes node that represents the given retrieved key-value pair.
func (s *Scheduler) refreshValue(graphW graph.RWAccess, retrievedKV kvs.KVWithMetadata,
handler *descriptorHandler, refreshed utils.KeySet, indent int) {
if s.logGraphWalk {
indentStr := strings.Repeat(" ", indent)
msg := fmt.Sprintf("refreshValue (key=%s)", retrievedKV.Key)
fmt.Printf("%s%s %s\n", indentStr, nodeVisitBeginMark, msg)
defer fmt.Printf("%s%s %s\n", indentStr, nodeVisitEndMark, msg)
}
// refresh node that represents this kv-pair
node := graphW.SetNode(retrievedKV.Key)
node.SetLabel(handler.keyLabel(node.GetKey()))
node.SetValue(retrievedKV.Value)
if handler.descriptor.WithMetadata {
node.SetMetadataMap(handler.descriptor.Name)
node.SetMetadata(retrievedKV.Metadata)
}
s.refreshAvailNode(graphW, node, retrievedKV.Origin, false, node.GetKey(), refreshed, indent+2)
// determine the set of unavailable derived values
obsolete := getDerivedKeys(node)
derives := handler.derivedValues(node.GetKey(), node.GetValue())
for _, newDerived := range derives {
obsolete.Del(newDerived.Key)
}
// keep obsolete derived values still in the relation
for _, key := range obsolete.Iterate() {
derives = append(derives, kvs.KeyValuePair{Key: key}) // value unused
}
// refresh relations
dependencies := handler.dependencies(node.GetKey(), node.GetValue())
node.SetTargets(constructTargets(dependencies, derives))
// refresh derived values
for _, kv := range derives {
isObsolete := obsolete.Has(kv.Key)
derNode := graphW.SetNode(kv.Key)
if !isObsolete {
derDescr := s.registry.GetDescriptorForKey(kv.Key)
derHandler := newDescriptorHandler(derDescr)
derNode.SetValue(kv.Value)
dependencies := derHandler.dependencies(derNode.GetKey(), derNode.GetValue())
derNode.SetTargets(constructTargets(dependencies, nil))
s.refreshAvailNode(graphW, derNode, retrievedKV.Origin, true, node.GetKey(), refreshed, indent+2)
} else {
s.refreshUnavailNode(graphW, derNode, refreshed, indent+2)
}
}
}
// refreshAvailNode refreshes state of a node whose value was returned by Retrieve.
func (s *Scheduler) refreshAvailNode(graphW graph.RWAccess, node graph.NodeRW,
origin kvs.ValueOrigin, derived bool, baseKey string, refreshed utils.KeySet, indent int) {
if s.logGraphWalk {
indentStr := strings.Repeat(" ", indent)
var derivedMark string
if derived {
derivedMark = ", is-derived"
}
msg := fmt.Sprintf("refreshAvailNode (key=%s%s)", node.GetKey(), derivedMark)
fmt.Printf("%s%s %s\n", indentStr, nodeVisitBeginMark, msg)
defer fmt.Printf("%s%s %s\n", indentStr, nodeVisitEndMark, msg)
}
// validate first
descriptor := s.registry.GetDescriptorForKey(node.GetKey()) // nil for properties
if derived && !s.validRetrievedDerivedKV(node, descriptor, refreshed) {
graphW.DeleteNode(node.GetKey())
return
}
// update availability
if !isNodeAvailable(node) {
s.updatedStates.Add(baseKey)
node.DelFlags(UnavailValueFlagIndex)
}
refreshed.Add(node.GetKey())
// refresh state
if getNodeState(node) == kvscheduler.ValueState_NONEXISTENT {
// newly found node
if origin == kvs.FromSB {
s.refreshNodeState(node, kvscheduler.ValueState_OBTAINED, indent)
} else {
s.refreshNodeState(node, kvscheduler.ValueState_DISCOVERED, indent)
}
}
if getNodeState(node) == kvscheduler.ValueState_PENDING {
// no longer pending apparently
s.refreshNodeState(node, kvscheduler.ValueState_CONFIGURED, indent)
}
// update descriptor flag
if descriptor != nil {
node.SetFlags(&DescriptorFlag{descriptor.Name})
} else {
node.DelFlags(DescriptorFlagIndex)
}
// updated flags for derived values
if !derived {
node.DelFlags(DerivedFlagIndex)
} else {
node.SetFlags(&DerivedFlag{baseKey})
}
}
// refreshUnavailNode refreshes state of a node whose value is found to be unavailable.
func (s *Scheduler) refreshUnavailNode(graphW graph.RWAccess, node graph.Node, refreshed utils.KeySet, indent int) {
if s.logGraphWalk {
indentStr := strings.Repeat(" ", indent)
msg := fmt.Sprintf("refreshUnavailNode (key=%s, isDerived=%t)", node.GetKey(), isNodeDerived(node))
fmt.Printf("%s%s %s\n", indentStr, nodeVisitBeginMark, msg)
defer fmt.Printf("%s%s %s\n", indentStr, nodeVisitEndMark, msg)
}
refreshed.Add(node.GetKey())
if isNodeAvailable(node) {
s.updatedStates.Add(getNodeBaseKey(node))
}
state := getNodeState(node)
if getNodeOrigin(node) == kvs.FromSB || state == kvscheduler.ValueState_DISCOVERED {
// just remove from the graph
graphW.DeleteNode(node.GetKey())
return
}
// mark node as unavailable, but do not delete
nodeW := graphW.SetNode(node.GetKey())
if isNodeAvailable(node) {
nodeW.SetFlags(&UnavailValueFlag{})
}
// update state
if state == kvscheduler.ValueState_UNIMPLEMENTED {
// it is expected that unimplemented value is not retrieved
return
}
if state == kvscheduler.ValueState_CONFIGURED {
if getNodeLastUpdate(node).value == nil {
s.refreshNodeState(nodeW, kvscheduler.ValueState_REMOVED, indent)
} else {
s.refreshNodeState(nodeW, kvscheduler.ValueState_MISSING, indent)
}
}
}
func (s *Scheduler) refreshNodeState(node graph.NodeRW, newState kvscheduler.ValueState, indent int) {
if getNodeState(node) != newState {
if s.logGraphWalk {
fmt.Printf("%s -> change value state from %v to %v\n",
strings.Repeat(" ", indent), getNodeState(node), newState)
}
node.SetFlags(&ValueStateFlag{valueState: newState})
}
}
// skipRefresh is used to mark nodes as refreshed without actual refreshing
// if they should not (or cannot) be refreshed.
func (s *Scheduler) skipRefresh(nodes []graph.Node, except utils.KeySet, refreshed utils.KeySet) {
for _, node := range nodes {
if except != nil {
if toRefresh := except.Has(node.GetKey()); toRefresh {
continue
}
}
refreshed.Add(node.GetKey())
// skip refresh for derived nodes
for _, derivedNode := range getDerivedNodes(node) {
refreshed.Add(derivedNode.GetKey())
}
}
}
func dumpGraph(g graph.RWAccess) string {
keys := g.GetKeys()
var buf strings.Builder
graphInfo := fmt.Sprintf("%d nodes", len(keys))
buf.WriteString("+======================================================================================================================+\n")
buf.WriteString(fmt.Sprintf("| GRAPH %110s |\n", graphInfo))
buf.WriteString("+======================================================================================================================+\n")
writeLine := func(left, right string) {
n := 115 - len(left)
buf.WriteString(fmt.Sprintf("| %s %"+fmt.Sprint(n)+"s |\n", left, right))
}
writeLines := func(linesStr string, prefix string) {
lines := strings.Split(linesStr, "\n")
for _, line := range lines {
if line == "" {
continue
}
writeLine(fmt.Sprintf("%s%s", prefix, line), "")
}
}
for i, key := range keys {
node := g.GetNode(key)
keyLabel := key
if label := node.GetLabel(); label != key && label != "" {
keyLabel = fmt.Sprintf("%s (%s)", key, label)
}
descriptor := ""
if f := node.GetFlag(DescriptorFlagIndex); f != nil {
descriptor = fmt.Sprintf("[%s] ", f.GetValue())
}
lastUpdate := "-"
if f := node.GetFlag(LastUpdateFlagIndex); f != nil {
lastUpdate = f.GetValue()
}
unavailable := ""
if f := node.GetFlag(UnavailValueFlagIndex); f != nil {
unavailable = "<UNAVAILABLE> "
}
writeLine(fmt.Sprintf("%s%s", descriptor, keyLabel), fmt.Sprintf("%s %s %s",
unavailable,
lastUpdate,
getNodeState(node).String(),
))
writeLines(prototext.Format(node.GetValue()), " ")
if f := node.GetTargets(DependencyRelation); len(f) > 0 {
writeLine("Depends on:", "")
for _, dep := range f {
var nodeDeps []string
for _, node := range dep.Nodes {
nodeDeps = append(nodeDeps, node.GetKey())
}
if len(nodeDeps) > 1 {
writeLine(fmt.Sprintf(" - %s", dep.Label), "")
writeLines(strings.Join(nodeDeps, "\n"), " -> ")
} else if len(nodeDeps) == 1 {
writeLine(fmt.Sprintf(" - %s -> %v", dep.Label, strings.Join(nodeDeps, " ")), "")
} else {
writeLine(fmt.Sprintf(" - %s -> <UNAVAILABLE>", dep.Label), "")
}
}
}
if f := node.GetTargets(DerivesRelation); len(f) > 0 {
writeLine("Derives:", "")
var nodeDers []string
for _, der := range f {
if len(der.Nodes) == 0 {
nodeDers = append(nodeDers, der.Label)
} else {
for _, node := range der.Nodes {
desc := ""
if d := node.GetFlag(DescriptorFlagIndex); d != nil {
desc = fmt.Sprintf("[%s] ", d.GetValue())
}
nodeDers = append(nodeDers, fmt.Sprintf("%s%s", desc, node.GetKey()))
}
}
}
writeLines(strings.Join(nodeDers, "\n"), " - ")
}
if f := node.GetSources(DependencyRelation); len(f) > 0 {
writeLine("Dependency for:", "")
var nodeDeps []string
for _, perLabel := range f {
for _, node := range perLabel.Nodes {
desc := ""
if d := node.GetFlag(DescriptorFlagIndex); d != nil {
desc = fmt.Sprintf("[%s] ", d.GetValue())
}
nodeDeps = append(nodeDeps, fmt.Sprintf("%s%s", desc, node.GetKey()))
}
}
writeLines(strings.Join(nodeDeps, "\n"), " - ")
}
if f := node.GetSources(DerivesRelation); len(f) > 0 {
var nodeDers []string
for _, perLabel := range f {
for _, der := range perLabel.Nodes {
nodeDers = append(nodeDers, der.GetKey())
}
}
writeLine(fmt.Sprintf("Derived from: %s", strings.Join(nodeDers, " ")), "")
}
if f := node.GetMetadata(); f != nil {
writeLine(fmt.Sprintf("Metadata: %+v", f), "")
}
if f := node.GetFlag(ErrorFlagIndex); f != nil {
writeLine(fmt.Sprintf("Errors: %+v", f.GetValue()), "")
}
if i+1 != len(keys) {
buf.WriteString("+----------------------------------------------------------------------------------------------------------------------+\n")
}
}
buf.WriteString("+======================================================================================================================+\n")
return buf.String()
}
// validRetrievedKV verifies validity of a retrieved KV-pair.
func (s *Scheduler) validRetrievedKV(kv kvs.KVWithMetadata, descriptor *kvs.KVDescriptor, refreshed utils.KeySet) bool {
if kv.Key == "" {
s.Log.WithFields(logging.Fields{
"descriptor": descriptor.Name,
}).Warn("Descriptor retrieved value with empty key")
return false
}
if alreadyRetrieved := refreshed.Has(kv.Key); alreadyRetrieved {
s.Log.WithFields(logging.Fields{
"descriptor": descriptor.Name,
"key": kv.Key,
}).Warn("The same value was retrieved more than once")
return false
}
if kv.Value == nil {
s.Log.WithFields(logging.Fields{
"descriptor": descriptor.Name,
"key": kv.Key,
}).Warn("Descriptor retrieved nil value")
return false
}
if !descriptor.KeySelector(kv.Key) {
s.Log.WithFields(logging.Fields{
"descriptor": descriptor.Name,
"key": kv.Key,
"value": kv.Value,
}).Warn("Descriptor retrieved value outside of its key space")
return false
}
return true
}
// validRetrievedDerivedKV verifies validity of a KV-pair derived from a retrieved value.
func (s *Scheduler) validRetrievedDerivedKV(node graph.Node, descriptor *kvs.KVDescriptor, refreshed utils.KeySet) bool {
descriptorName := "<NONE>"
if descriptor != nil {
descriptorName = descriptor.Name
}
if node.GetValue() == nil {
s.Log.WithFields(logging.Fields{
"descriptor": descriptorName,
"key": node.GetKey(),
}).Warn("Derived nil value")
return false
}
if alreadyRetrieved := refreshed.Has(node.GetKey()); alreadyRetrieved {
s.Log.WithFields(logging.Fields{
"descriptor": descriptorName,
"key": node.GetKey(),
}).Warn("The same value was retrieved more than once")
// return true -> let's overwrite invalidly retrieved derived value
}
return true
}