forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
268 lines (229 loc) · 8.52 KB
/
main.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
package main
import (
"log"
"strings"
"time"
"github.com/ligato/cn-infra/agent"
"github.com/ligato/cn-infra/datasync"
"github.com/ligato/cn-infra/datasync/kvdbsync"
"github.com/ligato/cn-infra/db/keyval/etcd"
"github.com/ligato/cn-infra/examples/model"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/servicelabel"
"github.com/ligato/cn-infra/utils/safeclose"
"golang.org/x/net/context"
)
// *************************************************************************
// This example demonstrates the usage of datasync API with etcd
// as the data store.
// ExamplePlugin spawns a data publisher and a data consumer (watcher)
// as two separate go routines.
// The publisher executes two operations on the same key: CREATE + UPDATE.
// The consumer is notified with each change and reports the events into
// the log.
// ************************************************************************/
// PluginName represents name of plugin.
const PluginName = "datasync-example"
func main() {
// Prepare ETCD data sync plugin as an plugin dependency
etcdDataSync := kvdbsync.NewPlugin(kvdbsync.UseDeps(func(deps *kvdbsync.Deps) {
deps.KvPlugin = &etcd.DefaultPlugin
}))
// Init example plugin dependencies
ep := &ExamplePlugin{
Deps: Deps{
Log: logging.ForPlugin(PluginName),
ServiceLabel: &servicelabel.DefaultPlugin,
Publisher: etcdDataSync,
Watcher: etcdDataSync,
},
exampleFinished: make(chan struct{}),
}
// Start Agent with example plugin including dependencies
a := agent.NewAgent(
agent.AllPlugins(ep),
agent.QuitOnClose(ep.exampleFinished),
)
if err := a.Run(); err != nil {
log.Fatal(err)
}
}
// ExamplePlugin demonstrates the usage of datasync API.
type ExamplePlugin struct {
Deps
changeChannel chan datasync.ChangeEvent // Channel used by the watcher for change events.
resyncChannel chan datasync.ResyncEvent // Channel used by the watcher for resync events.
context context.Context // Used to cancel watching.
cancel context.CancelFunc
watchDataReg datasync.WatchRegistration // To subscribe on data change/resync events.
// Fields below are used to properly finish the example.
eventCounter uint8
publisherDone bool
exampleFinished chan struct{}
}
// Deps lists dependencies of ExamplePlugin.
type Deps struct {
Log logging.PluginLogger
ServiceLabel servicelabel.ReaderAPI
Publisher datasync.KeyProtoValWriter // injected - To write ETCD data
Watcher datasync.KeyValProtoWatcher // injected - To watch ETCD data
}
// String returns plugin name
func (plugin *ExamplePlugin) String() string {
return PluginName
}
// Init starts the consumer.
func (plugin *ExamplePlugin) Init() error {
// Initialize plugin fields.
plugin.resyncChannel = make(chan datasync.ResyncEvent)
plugin.changeChannel = make(chan datasync.ChangeEvent)
plugin.context, plugin.cancel = context.WithCancel(context.Background())
// Start the consumer (ETCD watcher).
go plugin.consumer()
// Subscribe watcher to be able to watch on data changes and resync events.
err := plugin.subscribeWatcher()
if err != nil {
return err
}
plugin.Log.Info("Initialization of the custom plugin for the datasync example is completed")
return nil
}
// Close shutdowns both the publisher and the consumer.
// Channels used to propagate data resync and data change events are closed
// as well.
func (plugin *ExamplePlugin) Close() error {
return safeclose.Close(plugin.resyncChannel, plugin.changeChannel)
}
// AfterInit starts the publisher and prepares for the shutdown.
func (plugin *ExamplePlugin) AfterInit() error {
go plugin.etcdPublisher()
go plugin.closeExample()
return nil
}
// etcdPublisher creates a simple data, then demonstrates CREATE and UPDATE
// operations with ETCD.
func (plugin *ExamplePlugin) etcdPublisher() {
// Wait for the consumer to initialize
time.Sleep(1 * time.Second)
plugin.Log.Print("KeyValPublisher started")
// Convert data into the proto format.
exampleData := plugin.buildData("string1", 0, true)
// PUT: demonstrate how to use the Data Broker Put() API to store
// a simple data structure into ETCD.
label := etcdKeyPrefixLabel(plugin.ServiceLabel.GetAgentLabel(), "index")
plugin.Log.Infof("Write data to %v", label)
plugin.Publisher.Put(label, exampleData)
// Prepare different set of data.
plugin.Log.Infof("Update data at %v", label)
exampleData = plugin.buildData("string2", 1, false)
// UPDATE: demonstrate how use the Data Broker Put() API to change
// an already stored data in ETCD.
plugin.Publisher.Put(label, exampleData)
// Prepare another different set of data.
plugin.Log.Infof("Update data at %v", label)
exampleData = plugin.buildData("string3", 2, false)
// UPDATE: only to demonstrate Unregister functionality
plugin.Publisher.Put(label, exampleData)
// Wait for the consumer (change should not be passed to listener)
time.Sleep(2 * time.Second)
plugin.publisherDone = true
}
// consumer (watcher) is subscribed to watch on data store changes.
// Changes arrive via data change channel, get identified based on the key
// and printed into the log.
func (plugin *ExamplePlugin) consumer() {
plugin.Log.Print("KeyValProtoWatcher started")
for {
select {
// WATCH: demonstrate how to receive data change events.
case dataChng := <-plugin.changeChannel:
plugin.Log.Printf("Received event: %v", dataChng)
// If event arrives, the key is extracted and used together with
// the expected prefix to identify item.
key := dataChng.GetKey()
if strings.HasPrefix(key, etcdKeyPrefix(plugin.ServiceLabel.GetAgentLabel())) {
var value, previousValue etcdexample.EtcdExample
// The first return value is diff - boolean flag whether previous value exists or not
err := dataChng.GetValue(&value)
if err != nil {
plugin.Log.Error(err)
}
diff, err := dataChng.GetPrevValue(&previousValue)
if err != nil {
plugin.Log.Error(err)
}
plugin.Log.Infof("Event arrived to etcd eventHandler, key %v, update: %v, change type: %v,",
dataChng.GetKey(), diff, dataChng.GetChangeType())
// Increase event counter (expecting two events).
plugin.eventCounter++
if plugin.eventCounter == 2 {
// After creating/updating data, unregister key
plugin.Log.Infof("Unregister key %v", etcdKeyPrefix(plugin.ServiceLabel.GetAgentLabel()))
plugin.watchDataReg.Unregister(etcdKeyPrefix(plugin.ServiceLabel.GetAgentLabel()))
}
}
// Here you would test for other event types with one if statement
// for each key prefix:
//
// if strings.HasPrefix(key, etcd prefix) { ... }
// Here you would also watch for resync events
// (not published in this example):
//
// case resyncEvent := <-plugin.ResyncEvent:
// ...
case rs := <-plugin.resyncChannel:
// Resync event notification
plugin.Log.Infof("Resync event %v called", rs)
rs.Done(nil)
case <-plugin.context.Done():
plugin.Log.Debugf("Stop watching events")
return
}
}
}
// subscribeWatcher subscribes for data change and data resync events.
// Events are delivered to the consumer via the selected channels.
// ETCD watcher adapter is used to perform the registration behind the scenes.
func (plugin *ExamplePlugin) subscribeWatcher() (err error) {
prefix := etcdKeyPrefix(plugin.ServiceLabel.GetAgentLabel())
plugin.Log.Infof("Prefix: %v", prefix)
plugin.watchDataReg, err = plugin.Watcher.
Watch("Example etcd plugin", plugin.changeChannel, plugin.resyncChannel, prefix)
if err != nil {
return err
}
plugin.Log.Info("KeyValProtoWatcher subscribed")
return nil
}
func (plugin *ExamplePlugin) closeExample() {
for {
// Two events are expected for successful example completion.
if plugin.publisherDone {
if plugin.eventCounter != 2 {
plugin.Log.Error("etcd/datasync example failed", plugin.eventCounter)
}
// Close the watcher
plugin.cancel()
plugin.Log.Infof("etcd/datasync example finished, sending shutdown ...")
// Close the example
close(plugin.exampleFinished)
break
}
}
}
// Create simple ETCD data structure with provided data values.
func (plugin *ExamplePlugin) buildData(stringVal string, uint32Val uint32, boolVal bool) *etcdexample.EtcdExample {
return &etcdexample.EtcdExample{
StringVal: stringVal,
Uint32Val: uint32Val,
BoolVal: boolVal,
}
}
// The ETCD key prefix used for this example
func etcdKeyPrefix(agentLabel string) string {
return "/vnf-agent/" + agentLabel + "/api/v1/example/db/simple/"
}
// The ETCD key (the key prefix + label)
func etcdKeyPrefixLabel(agentLabel string, index string) string {
return etcdKeyPrefix(agentLabel) + index
}