-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmain.go
319 lines (275 loc) · 9.68 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
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
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/namsral/flag"
"go.ligato.io/cn-infra/v2/agent"
"go.ligato.io/cn-infra/v2/examples/model"
"go.ligato.io/cn-infra/v2/logging"
"go.ligato.io/cn-infra/v2/messaging"
"go.ligato.io/cn-infra/v2/messaging/kafka"
"go.ligato.io/cn-infra/v2/utils/safeclose"
)
//********************************************************************
// The following functions show how to use the Agent's Kafka APIs
// to perform synchronous/asynchronous calls and how to watch on
// these events.
//********************************************************************
var (
// Flags used to read the input arguments. Applies for both, sync and async message
messageCount = flag.String("messageCount", os.Getenv("MSG_COUNT"), "Number of messages which will be send. Set to '0' to just watch")
)
// PluginName represents name of plugin.
const PluginName = "kafka-hash-example"
func main() {
// Init example plugin and its dependencies
ep := &ExamplePlugin{
Deps: Deps{
Log: logging.ForPlugin(PluginName),
Kafka: &kafka.DefaultPlugin,
},
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 use of Kafka plugin API from another plugin.
// The Kafka ConsumerHandle is required to read messages from a topic
// and PluginConnection is needed to start consuming on that topic.
type ExamplePlugin struct {
Deps // plugin dependencies are injected
subscription chan messaging.ProtoMessage
kafkaSyncPublisher messaging.ProtoPublisher
kafkaAsyncPublisher messaging.ProtoPublisher
kafkaWatcher messaging.ProtoWatcher
// Successfully published kafka message is sent through the message channel.
// In case of a failure it sent through the error channel.
asyncSubscription chan messaging.ProtoMessage
asyncSuccessChannel chan messaging.ProtoMessage
asyncErrorChannel chan messaging.ProtoMessageErr
// Fields below are used to properly finish the example.
messagesSent bool
syncRecv bool
asyncRecv bool
asyncSuccess bool
exampleFinished chan struct{}
}
// Deps lists dependencies of ExamplePlugin.
type Deps struct {
Kafka messaging.Mux // injected
//local.PluginLogDeps // injected
Log logging.PluginLogger
}
// Consts
const (
topic1 = "example-sync-topic"
topic2 = "example-async-topic"
connection = "example-proto-connection"
subscriber = "example-part-watcher"
)
// These vars are applied for both, sync and async case
var (
// How many messages will be sent
messageCountNum = 10
)
// String returns plugin name
func (plugin *ExamplePlugin) String() string {
return PluginName
}
// Init initializes and starts producers and consumers.
func (plugin *ExamplePlugin) Init() (err error) {
// handle flags
flag.Parse()
// message count flag
if *messageCount != "" {
messageCountNum, err = resolveMsgCount(*messageCount)
if err != nil {
return fmt.Errorf("'messageCount' has to be a number, not %v", *messageCount)
}
if messageCountNum < 0 {
plugin.Log.Warnf("'messageCount' %v is not a positive number, defaulting to 0")
messageCountNum = 0
}
} else {
plugin.Log.Info("messageCount arg not set, using default value")
}
plugin.Log.Infof("Message count: %v", *messageCount)
// Init channels required for async handler.
plugin.asyncSuccessChannel = make(chan messaging.ProtoMessage)
plugin.asyncErrorChannel = make(chan messaging.ProtoMessageErr)
// Create a synchronous publisher for the selected topic.
plugin.kafkaSyncPublisher, err = plugin.Kafka.NewSyncPublisher(connection, topic1)
if err != nil {
return err
}
// Create an asynchronous publisher for the selected topic.
plugin.kafkaAsyncPublisher, err = plugin.Kafka.NewAsyncPublisher(connection, topic2, messaging.ToProtoMsgChan(plugin.asyncSuccessChannel),
messaging.ToProtoMsgErrChan(plugin.asyncErrorChannel))
if err != nil {
return err
}
// Initialize sync watcher.
plugin.kafkaWatcher = plugin.Kafka.NewWatcher(subscriber)
// Prepare subscription channel. Relevant kafka messages are send to this
// channel so that the watcher can read it.
plugin.subscription = make(chan messaging.ProtoMessage)
// The watcher is consuming messages
err = plugin.kafkaWatcher.Watch(messaging.ToProtoMsgChan(plugin.subscription), topic1)
if err != nil {
plugin.Log.Error(err)
}
// Prepare subscription channel. Relevant kafka messages are send to this
// channel so that the watcher can read it
plugin.asyncSubscription = make(chan messaging.ProtoMessage)
// The watcher is consuming messages
err = plugin.kafkaWatcher.Watch(messaging.ToProtoMsgChan(plugin.asyncSubscription), topic2)
if err != nil {
plugin.Log.Error(err)
}
plugin.Log.Info("Initialization of the custom plugin for the Kafka example is completed")
// Run sync and async kafka consumers.
go plugin.syncEventHandler()
go plugin.asyncEventHandler()
// Run the producer to send notifications.
go plugin.producer()
// Verify results and close the example.
go plugin.closeExample()
return err
}
func (plugin *ExamplePlugin) closeExample() {
for {
if plugin.messagesSent && plugin.asyncSuccess && plugin.syncRecv && plugin.asyncRecv {
time.Sleep(2 * time.Second)
err := plugin.kafkaWatcher.StopWatch(topic1)
if err != nil {
plugin.Log.Errorf("Error while stopping watcher: %v", err)
} else {
plugin.Log.Info("Sync watcher closed")
}
err = plugin.kafkaWatcher.StopWatch(topic2)
if err != nil {
plugin.Log.Errorf("Error while stopping watcher: %v", err)
} else {
plugin.Log.Info("Async watcher closed")
}
plugin.Log.Info("kafka example finished, sending shutdown ...")
close(plugin.exampleFinished)
break
}
}
}
// Close closes the subscription and the channels used by the async producer.
func (plugin *ExamplePlugin) Close() error {
return safeclose.Close(plugin.subscription)
}
/***********************
* Kafka Example calls *
***********************/
// Send Kafka notifications
func (plugin *ExamplePlugin) producer() {
// Wait for the both event handlers to initialize
time.Sleep(2 * time.Second)
// Synchronous message with protobuf-encoded data.
enc := &etcdexample.EtcdExample{
StringVal: "value",
Uint32Val: uint32(0),
BoolVal: true,
}
plugin.Log.Info("Sending Kafka notification (protobuf)")
// Send several sync messages with offsets offsetLast+1, offsetLast+2,...
plugin.Log.Infof("Sending %v sync Kafka notifications (protobuf) ...", messageCountNum)
for i := 0; i < messageCountNum; i++ {
err := plugin.kafkaSyncPublisher.Put("proto-key", enc)
if err != nil {
plugin.Log.Errorf("Failed to sync-send a proto message, error %v", err)
}
}
// Send message with protobuf encoded data asynchronously.
// Delivery status is propagated back to the application through
// the configured pair of channels - one for the success events and one for
// the errors.
plugin.Log.Infof("Sending %v async Kafka notifications (protobuf) ...", messageCountNum)
for i := 0; i < messageCountNum; i++ {
err := plugin.kafkaAsyncPublisher.Put("async-proto-key", enc)
if err != nil {
plugin.Log.Errorf("Failed to async-send a proto message, error %v", err)
}
}
// Mark that all messages were sent
plugin.messagesSent = true
}
/*************
* Consumers *
*************/
// syncEventHandler is a Kafka consumer synchronously processing events from
// a channel associated with a specific topic. If a producer sends a message
// matching this destination criteria, the consumer will receive it.
func (plugin *ExamplePlugin) syncEventHandler() {
plugin.Log.Info("Started Kafka event handler...")
msgCounter := 0
if messageCountNum == 0 {
// Set as done
plugin.syncRecv = true
}
// Watch on message channel for sync kafka events
for message := range plugin.subscription {
plugin.Log.Infof("Received Kafka Message, topic '%s', partition '%v', offset '%v', key: '%s', ",
message.GetTopic(), message.GetPartition(), message.GetOffset(), message.GetKey())
// mark the offset
plugin.kafkaWatcher.MarkOffset(message, "")
msgCounter++
if msgCounter == messageCountNum {
plugin.syncRecv = true
}
}
}
// asyncEventHandler is a Kafka consumer asynchronously processing events from
// a channel associated with a specific topic. If a producer sends a message
// matching this destination criteria, the consumer will receive it.
func (plugin *ExamplePlugin) asyncEventHandler() {
plugin.Log.Info("Started Kafka async event handler...")
msgCounter := 0
asyncMsgSucc := 0
if messageCountNum == 0 {
// Set as done
plugin.asyncSuccess = true
plugin.asyncRecv = true
}
for {
select {
// Channel subscribed with watcher
case message := <-plugin.asyncSubscription:
plugin.Log.Infof("Received async Kafka Message, topic '%s', partition '%v', offset '%v', key: '%s', ",
message.GetTopic(), message.GetPartition(), message.GetOffset(), message.GetKey())
// mark the offset
plugin.kafkaWatcher.MarkOffset(message, "")
msgCounter++
if msgCounter == messageCountNum {
plugin.asyncRecv = true
}
case message := <-plugin.asyncSuccessChannel:
plugin.Log.Infof("Async message successfully delivered, topic '%s', partition '%v', offset '%v', key: '%s', ",
message.GetTopic(), message.GetPartition(), message.GetOffset(), message.GetKey())
asyncMsgSucc++
if asyncMsgSucc == messageCountNum {
plugin.asyncSuccess = true
}
// Error callback channel
case err := <-plugin.asyncErrorChannel:
plugin.Log.Errorf("Failed to publish async message, %v", err)
}
}
}
func resolveMsgCount(count string) (int, error) {
result, err := strconv.Atoi(count)
return result, err
}