Skip to content

Commit ddbe9b2

Browse files
committed
Replace session context with main context
Had to go around this problem using mainContext as sessions get closed quite often when the total topics is huge. Using context in struct instead of golang suggested approach of passing context around. Since sarama does not support passing context around. IBM/sarama#1776 golang/go#22602
1 parent 75bb54c commit ddbe9b2

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

cmd/redshiftbatcher/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func run(cmd *cobra.Command, args []string) {
6868
groupConfig,
6969
redshiftbatcher.NewConsumer(
7070
ready,
71+
ctx,
7172
groupConfig.Kafka,
7273
groupConfig.Sarama,
7374
groupConfig.LoaderTopicPrefix,

pkg/redshiftbatcher/batch_processor.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type batchProcessor struct {
3333
// session is required to commit the offsets on succesfull processing
3434
session sarama.ConsumerGroupSession
3535

36+
// mainContext for cancellations
37+
mainContext context.Context
38+
3639
// s3Sink
3740
s3sink *s3sink.S3Sink
3841

@@ -100,6 +103,7 @@ func newBatchProcessor(
100103
topic string,
101104
partition int32,
102105
session sarama.ConsumerGroupSession,
106+
mainContext context.Context,
103107
kafkaConfig kafka.KafkaConfig,
104108
saramaConfig kafka.SaramaConfig,
105109
kafkaLoaderTopicPrefix string,
@@ -146,6 +150,7 @@ func newBatchProcessor(
146150
partition: partition,
147151
autoCommit: saramaConfig.AutoCommit,
148152
session: session,
153+
mainContext: mainContext,
149154
s3sink: sink,
150155
s3BucketDir: viper.GetString("s3sink.bucketDir"),
151156
bodyBuf: bytes.NewBuffer(make([]byte, 0, 4096)),
@@ -179,8 +184,10 @@ func removeEmptyNullValues(value map[string]*string) map[string]*string {
179184
// TODO: get rid of this https://github.com/herryg91/gobatch/issues/2
180185
func (b *batchProcessor) ctxCancelled() bool {
181186
select {
182-
case <-b.session.Context().Done():
183-
klog.Infof(
187+
case <-b.mainContext.Done():
188+
err := b.mainContext.Err()
189+
klog.Warningf("Batch processing stopped, mainContext done, ctxErr: %v", err)
190+
klog.V(2).Infof(
184191
"topic:%s, batchId:%d, lastCommittedOffset:%d: Cancelled.\n",
185192
b.topic, b.batchId, b.lastCommittedOffset,
186193
)
@@ -393,12 +400,12 @@ func (b *batchProcessor) processMessage(message *serializer.Message, id int) {
393400
// otherwise return false in case of gracefull shutdown signals being captured,
394401
// this helps in cleanly shutting down the batch processing.
395402
func (b *batchProcessor) processBatch(
396-
ctx context.Context, datas []interface{}) bool {
403+
mainContext context.Context, datas []interface{}) bool {
397404

398405
b.s3Key = ""
399406
for id, data := range datas {
400407
select {
401-
case <-ctx.Done():
408+
case <-mainContext.Done():
402409
return false
403410
default:
404411
b.processMessage(data.(*serializer.Message), id)
@@ -423,7 +430,7 @@ func (b *batchProcessor) process(workerID int, datas []interface{}) {
423430
b.topic, b.batchId, len(datas),
424431
)
425432

426-
done := b.processBatch(b.session.Context(), datas)
433+
done := b.processBatch(b.mainContext, datas)
427434
if !done {
428435
b.handleShutdown()
429436
return

pkg/redshiftbatcher/consumer.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package redshiftbatcher
22

33
import (
4+
"context"
45
"github.com/Shopify/sarama"
56
"github.com/practo/klog/v2"
67
"github.com/practo/tipoca-stream/redshiftsink/pkg/kafka"
78
)
89

9-
func NewConsumer(ready chan bool, kafkaConfig kafka.KafkaConfig, saramaConfig kafka.SaramaConfig, loaderPrefix string) consumer {
10+
func NewConsumer(ready chan bool, mainContext context.Context, kafkaConfig kafka.KafkaConfig, saramaConfig kafka.SaramaConfig, loaderPrefix string) consumer {
1011
return consumer{
1112
ready: ready,
13+
mainContext: mainContext,
1214
kafkaConfig: kafkaConfig,
1315
saramaConfig: saramaConfig,
1416
kafkaLoaderTopicPrefix: loaderPrefix,
@@ -23,6 +25,7 @@ func NewConsumer(ready chan bool, kafkaConfig kafka.KafkaConfig, saramaConfig ka
2325
type consumer struct {
2426
// Ready is used to signal the main thread about the readiness
2527
ready chan bool
28+
mainContext context.Context
2629
kafkaConfig kafka.KafkaConfig
2730
saramaConfig kafka.SaramaConfig
2831
kafkaLoaderTopicPrefix string
@@ -60,6 +63,7 @@ func (c consumer) processMessage(
6063
message.Topic,
6164
message.Partition,
6265
session,
66+
c.mainContext,
6367
c.kafkaConfig,
6468
c.saramaConfig,
6569
c.kafkaLoaderTopicPrefix,
@@ -69,8 +73,9 @@ func (c consumer) processMessage(
6973
c.batcher.processor.session = session
7074

7175
select {
72-
case <-c.batcher.processor.session.Context().Done():
73-
klog.Info("Graceful shutdown requested, not inserting in batch")
76+
case <-c.mainContext.Done():
77+
err := c.mainContext.Err()
78+
klog.Warningf("Batch insert stopped, mainContext done, ctxErr: %v", err)
7479
return nil
7580
default:
7681
c.batcher.Insert(message)
@@ -98,7 +103,7 @@ func (c consumer) ConsumeClaim(session sarama.ConsumerGroupSession,
98103
// https://github.com/Shopify/sarama/blob/master/consumer_group.go#L27-L29
99104
for message := range claim.Messages() {
100105
select {
101-
case <-session.Context().Done():
106+
case <-c.mainContext.Done():
102107
klog.Infof(
103108
"%s: Gracefully shutdown. Stopped taking new messages.",
104109
claim.Topic(),

0 commit comments

Comments
 (0)