-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmain.go
355 lines (307 loc) · 11.7 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
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
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/messaging/kafka/mux"
"go.ligato.io/cn-infra/v2/utils/safeclose"
)
//********************************************************************
// This example shows 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
offsetMsg = flag.String("offsetMsg", os.Getenv("KAFKA_OFFSET"), "Use 'latest', 'oldest' or exact number of message offset")
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-manual-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.ProtoPartitionWatcher
// 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
asyncSuccess bool
exampleFinished chan struct{}
}
// Deps lists dependencies of ExamplePlugin.
type Deps struct {
Kafka messaging.Mux // injected
//local.PluginLogDeps // injected
Log logging.PluginLogger
}
const (
// Partition sync messages are sent and watched on
syncMessagePartition = 1
// Partiton async messages are sent and watched on
asyncMessagePartition = 2
)
// These vars are applied for both, sync and async case
var (
// Offset for sync messages watcher
messageOffset int64
// How many messages will be sent
messageCountNum = 10
)
// Consts
const (
topic1 = "example-sync-topic"
topic2 = "example-async-topic"
connection = "example-proto-connection"
subscriber = "example-part-watcher"
)
// 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()
// sync/async offset flag
if *offsetMsg != "" {
messageOffset, err = resolveOffset(*offsetMsg)
if err != nil {
return fmt.Errorf("incorrect sync offset value %v", *offsetMsg)
}
} else {
plugin.Log.Info("offset arg not set, using default value")
}
// 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("Offset: %v, message count: %v", messageOffset, messageCountNum)
// Create a synchronous and asynchronous publisher.
// In the manual mode, every publisher has selected its target partition.
plugin.kafkaSyncPublisher, err = plugin.Kafka.NewSyncPublisherToPartition(connection, topic1, syncMessagePartition)
if err != nil {
return err
}
// Async publisher requires two more channels to send success/error callback.
plugin.asyncSuccessChannel = make(chan messaging.ProtoMessage)
plugin.asyncErrorChannel = make(chan messaging.ProtoMessageErr)
plugin.kafkaAsyncPublisher, err = plugin.Kafka.NewAsyncPublisherToPartition(connection, topic2, asyncMessagePartition,
messaging.ToProtoMsgChan(plugin.asyncSuccessChannel), messaging.ToProtoMsgErrChan(plugin.asyncErrorChannel))
if err != nil {
return err
}
// Initialize sync watcher.
plugin.kafkaWatcher = plugin.Kafka.NewPartitionWatcher(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 on a custom partition and an offset.
// If there is a producer who stores message to the same partition under
// the same or a newer offset, the message will be consumed.
err = plugin.kafkaWatcher.WatchPartition(messaging.ToProtoMsgChan(plugin.subscription), topic1,
syncMessagePartition, messageOffset)
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 on custom partition and offset.
// If there is a producer who stores message to the same partition under
// the same or a newer offset, the message will be consumed.
err = plugin.kafkaWatcher.WatchPartition(messaging.ToProtoMsgChan(plugin.asyncSubscription), topic2,
asyncMessagePartition, messageOffset)
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.
go plugin.producer()
// Verify results and close the example if successful.
go plugin.closeExample()
return err
}
func (plugin *ExamplePlugin) closeExample() {
for {
if plugin.messagesSent && plugin.asyncSuccess {
time.Sleep(2 * time.Second)
err := plugin.kafkaWatcher.StopWatchPartition(topic1, syncMessagePartition, messageOffset)
if err != nil {
plugin.Log.Errorf("Error while stopping watcher: %v", err)
} else {
plugin.Log.Info("Sync watcher closed")
}
err = plugin.kafkaWatcher.StopWatchPartition(topic2, asyncMessagePartition, messageOffset)
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)
}
/*************
* Producers *
*************/
// producer sends messages to a desired topic and in the manual mode also to a specified partition. Tho number of messages
// sent can be set with flag
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: "sync-dummy-message",
Uint32Val: uint32(0),
BoolVal: true,
}
// 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, partition and a starting offset.
// If a producer sends a message matching this destination criteria, the consumer
// will receive it.
func (plugin *ExamplePlugin) syncEventHandler() {
plugin.Log.Info("Started Kafka sync event handler...")
// Producer sends several messages (set in messageCount).
// Consumer should receive only messages from desired partition and offset.
receivedMessageCounter := 0
for message := range plugin.subscription {
plugin.Log.Infof("Received sync Kafka Message, topic '%s', partition '%v', offset '%v', key: '%s', ",
message.GetTopic(), message.GetPartition(), message.GetOffset(), message.GetKey())
// Note: mark the offset if required
receivedMessageCounter++
if message.GetPartition() != syncMessagePartition {
plugin.Log.Errorf("Received sync message with unexpected partition: %v", message.GetOffset())
}
if message.GetOffset() < messageOffset {
plugin.Log.Errorf("Received sync message with unexpected offset: %v", message.GetOffset())
}
}
}
// asyncEventHandler is a Kafka consumer asynchronously processing events from
// a channel associated with a specific topic, partition and a starting offset.
// 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...")
asyncSuccessCounter := 0
if messageCountNum == 0 {
plugin.asyncSuccess = 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())
// Note: mark the offset if required
if message.GetPartition() != asyncMessagePartition {
plugin.Log.Errorf("Received async message with unexpected partition: %v", message.GetOffset())
}
if message.GetOffset() < messageOffset {
plugin.Log.Errorf("Received async message with unexpected offset: %v", message.GetOffset())
}
// Success callback channel
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())
// Note: mark the offset if required
asyncSuccessCounter++
if asyncSuccessCounter == messageCountNum {
plugin.asyncSuccess = true
}
// Error callback channel
case err := <-plugin.asyncErrorChannel:
plugin.Log.Errorf("Failed to publish async message, %v", err)
}
}
}
func resolveOffset(offset string) (int64, error) {
if offset == "latest" {
return mux.OffsetNewest, nil
} else if offset == "oldest" {
return mux.OffsetOldest, nil
}
result, err := strconv.Atoi(offset)
return int64(result), err
}
func resolveMsgCount(count string) (int, error) {
result, err := strconv.Atoi(count)
return result, err
}