forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orchestrator.go
271 lines (227 loc) · 6.95 KB
/
orchestrator.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
// Copyright (c) 2019 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 orchestrator
import (
"os"
"strings"
"github.com/ligato/cn-infra/datasync"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/rpc/grpc"
"golang.org/x/net/context"
api "github.com/ligato/vpp-agent/api/genericmanager"
"github.com/ligato/vpp-agent/pkg/models"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
)
var (
// EnableStatusPublishing enables status publishing.
EnableStatusPublishing = os.Getenv("ENABLE_STATUS_PUBLISHING") != ""
debugOrchestrator = os.Getenv("DEBUG_ORCHESTRATOR") != ""
)
// Plugin implements sync service for GRPC.
type Plugin struct {
Deps
manager *genericManagerSvc
// datasync channels
changeChan chan datasync.ChangeEvent
resyncChan chan datasync.ResyncEvent
watchDataReg datasync.WatchRegistration
*dispatcher
}
// Deps represents dependencies for the plugin.
type Deps struct {
infra.PluginDeps
GRPC grpc.Server
KVScheduler kvs.KVScheduler
Watcher datasync.KeyValProtoWatcher
StatusPublisher datasync.KeyProtoValWriter
}
// Init registers the service to GRPC server.
func (p *Plugin) Init() (err error) {
p.dispatcher = &dispatcher{
log: logging.DefaultRegistry.NewLogger("dispatcher"),
db: newMemStore(),
kvs: p.KVScheduler,
}
// register grpc service
p.manager = &genericManagerSvc{
log: p.log,
dispatch: p.dispatcher,
}
if grpcServer := p.GRPC.GetServer(); grpcServer != nil {
api.RegisterGenericManagerServer(grpcServer, p.manager)
} else {
p.log.Infof("grpc server not available")
}
nbPrefixes := p.kvs.GetRegisteredNBKeyPrefixes()
if len(nbPrefixes) > 0 {
p.log.Infof("Watch starting for %d registered NB prefixes", len(nbPrefixes))
} else {
p.log.Warnf("No registered NB prefixes found in KVScheduler (ensure that all KVDescriptors are registered before this)")
}
var prefixes []string
for _, prefix := range nbPrefixes {
p.log.Debugf("- watching NB prefix: %s", prefix)
prefixes = append(prefixes, prefix)
}
// initialize datasync channels
p.resyncChan = make(chan datasync.ResyncEvent)
p.changeChan = make(chan datasync.ChangeEvent)
p.watchDataReg, err = p.Watcher.Watch(p.PluginName.String(),
p.changeChan, p.resyncChan, prefixes...)
if err != nil {
return err
}
return nil
}
// AfterInit subscribes to known NB prefixes.
func (p *Plugin) AfterInit() (err error) {
go p.watchEvents()
statusChan := make(chan *kvs.BaseValueStatus, 100)
p.kvs.WatchValueStatus(statusChan, nil)
go p.watchStatus(statusChan)
return nil
}
// InitialSync will start initial synchronization with downstream.
func (p *Plugin) InitialSync() {
// FIXME: KVScheduler needs to have some type of sync that only refreshes state from SB
p.Log.Debugf("starting initial sync")
txn := p.KVScheduler.StartNBTransaction()
ctx := kvs.WithResync(context.Background(), kvs.DownstreamResync, true)
if _, err := txn.Commit(ctx); err != nil {
p.Log.Warnf("initial sync failed: %v", err)
} else {
p.Log.Infof("initial sync complete")
}
}
func (p *Plugin) watchEvents() {
for {
select {
case e := <-p.changeChan:
p.log.Debugf("=> received CHANGE event (%v changes)", len(e.GetChanges()))
var err error
var kvPairs []KeyVal
for _, x := range e.GetChanges() {
kv := KeyVal{
Key: x.GetKey(),
}
if x.GetChangeType() != datasync.Delete {
kv.Val, err = models.UnmarshalLazyValue(kv.Key, x)
if err != nil {
p.log.Errorf("decoding value for key %q failed: %v", kv.Key, err)
continue
}
}
kvPairs = append(kvPairs, kv)
}
if len(kvPairs) == 0 {
p.log.Warn("no valid kv pairs received in change event")
e.Done(nil)
continue
}
p.log.Debugf("Change with %d items", len(kvPairs))
ctx := e.GetContext()
if ctx == nil {
ctx = context.Background()
}
_, withDataSrc := DataSrcFromContext(ctx)
if !withDataSrc {
ctx = DataSrcContext(ctx, "datasync")
}
ctx = kvs.WithRetryDefault(ctx)
_, err = p.PushData(ctx, kvPairs)
e.Done(err)
case e := <-p.resyncChan:
p.log.Debugf("=> received RESYNC event (%v prefixes)", len(e.GetValues()))
var kvPairs []KeyVal
for prefix, iter := range e.GetValues() {
var keyVals []datasync.KeyVal
for x, done := iter.GetNext(); !done; x, done = iter.GetNext() {
key := x.GetKey()
val, err := models.UnmarshalLazyValue(key, x)
if err != nil {
p.log.Errorf("unmarshal value for key %q failed: %v", key, err)
continue
}
kvPairs = append(kvPairs, KeyVal{
Key: key,
Val: val,
})
p.log.Debugf(" -- key: %s", x.GetKey())
keyVals = append(keyVals, x)
}
if len(keyVals) > 0 {
p.log.Debugf("- %q (%v items)", prefix, len(keyVals))
} else {
p.log.Debugf("- %q (no items)", prefix)
}
for _, x := range keyVals {
p.log.Debugf("\t - %q: (rev: %v)", x.GetKey(), x.GetRevision())
}
}
p.log.Debugf("Resync with %d items", len(kvPairs))
ctx := e.GetContext()
if ctx == nil {
ctx = context.Background()
}
_, withDataSrc := DataSrcFromContext(ctx)
if !withDataSrc {
ctx = DataSrcContext(ctx, "datasync")
}
ctx = kvs.WithResync(ctx, kvs.FullResync, true)
ctx = kvs.WithRetryDefault(ctx)
_, err := p.PushData(ctx, kvPairs)
e.Done(err)
}
}
}
func (p *Plugin) watchStatus(ch <-chan *kvs.BaseValueStatus) {
for {
select {
case s := <-ch:
p.debugf("STATUS: %15s %v ===> %v (%v) %v",
s.Value.State, s.Value.Details, s.Value.Key, s.Value.LastOperation, s.Value.Error)
for _, dv := range s.DerivedValues {
p.debugf(" \t%15s %v ---> %v (%v) %v",
dv.State, dv.Details, dv.Key, dv.LastOperation, dv.Error)
}
if EnableStatusPublishing {
p.publishStatuses([]Result{
{Key: s.Value.Key, Status: s.Value},
})
}
}
}
}
func (p *Plugin) publishStatuses(results []Result) {
if p.StatusPublisher == nil {
return
}
p.debugf("publishing %d statuses", len(results))
for _, res := range results {
statusKey := strings.Replace(res.Key, "config/", "config-status/", 1)
if statusKey == res.Key {
p.debugf("replace for key %q failed", res.Key)
continue
}
if err := p.StatusPublisher.Put(statusKey, res.Status, datasync.WithClientLifetimeTTL()); err != nil {
p.debugf("publishing status for key %q failed: %v", statusKey, err)
}
}
}
func (p *Plugin) debugf(f string, a ...interface{}) {
if debugOrchestrator {
p.log.Debugf(f, a...)
}
}