-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for cascading retries #35
Open
derrickburns
wants to merge
14
commits into
master
Choose a base branch
from
cascading-topics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ec16c20
Add support for cascading retries
derrickburns 57e2208
Support exponential backoff retries
derrickburns 12ed098
Fix addend
derrickburns ce2a132
Pass in index of delay topic
derrickburns bc3957d
Fix boundary condition where max delay is exceeded.
derrickburns de42b71
Fix boundary condition where max delay is exceeded.
derrickburns 90a6b18
Fix max attempts logic
derrickburns 107bac9
Add comments on retry mechanism
derrickburns 0f53694
Add comments
derrickburns 84aa969
Remove vendoring
derrickburns a06e880
Allow setting host url directly
derrickburns 6858e0b
Add tracing to all clients
derrickburns dfc904f
Update mocks to take context parameter
derrickburns 9b27424
Fixed mocks
derrickburns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,15 @@ package events | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"sync" | ||
"time" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/cloudevents/sdk-go/protocol/kafka_sarama/v2" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/cloudevents/sdk-go/v2/binding" | ||
"log" | ||
"sync" | ||
) | ||
|
||
type SaramaConsumer struct { | ||
|
@@ -19,6 +22,62 @@ type SaramaConsumer struct { | |
deadLetterProducer *KafkaCloudEventsProducer | ||
} | ||
|
||
//CascadingEventConsumer an event consumer that cascaded failures | ||
type CascadingEventConsumer struct { | ||
Consumers []EventConsumer | ||
} | ||
|
||
// NewCascadingCloudEventsConsumer create a cascading events consumer | ||
func NewCascadingCloudEventsConsumer(config *CloudEventsConfig) (EventConsumer, error) { | ||
topic := config.KafkaTopic | ||
delay := config.KafkaDelay | ||
var consumers []EventConsumer | ||
for nextDelay := range config.CascadeDelays { | ||
newconfig := *config | ||
newconfig.KafkaDelay = delay | ||
newconfig.KafkaTopic = topic | ||
newconfig.KafkaDeadLettersTopic = fmt.Sprintf(config.CascadePattern, config.KafkaTopic, nextDelay) | ||
consumer, err := NewSaramaCloudEventsConsumer(&newconfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
consumers = append(consumers, consumer) | ||
topic = newconfig.KafkaDeadLettersTopic | ||
delay = nextDelay | ||
} | ||
newconfig := *config | ||
newconfig.KafkaDelay = delay | ||
newconfig.KafkaTopic = topic | ||
consumer, err := NewSaramaCloudEventsConsumer(&newconfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
consumers = append(consumers, consumer) | ||
|
||
return &CascadingEventConsumer{ | ||
Consumers: consumers, | ||
}, nil | ||
} | ||
|
||
//RegisterHandler registers the handler with all consumers in the cascade | ||
func (c *CascadingEventConsumer) RegisterHandler(handler EventHandler) { | ||
for _, consumer := range c.Consumers { | ||
consumer.RegisterHandler(handler) | ||
} | ||
} | ||
|
||
//Start starts an async consumer goproc (that may sleep) | ||
func (c *CascadingEventConsumer) Start(ctx context.Context) error { | ||
for _, consumer := range c.Consumers { | ||
err := consumer.Start(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
//NewSaramaCloudEventsConsumer creates a new cloud events consumer | ||
func NewSaramaCloudEventsConsumer(config *CloudEventsConfig) (EventConsumer, error) { | ||
if err := validateConsumerConfig(config); err != nil { | ||
return nil, err | ||
|
@@ -32,19 +91,36 @@ func NewSaramaCloudEventsConsumer(config *CloudEventsConfig) (EventConsumer, err | |
}, nil | ||
} | ||
|
||
//Setup marks a consumer to be ready | ||
func (s *SaramaConsumer) Setup(session sarama.ConsumerGroupSession) error { | ||
// Mark the consumer as ready | ||
close(s.ready) | ||
return nil | ||
} | ||
|
||
//Cleanup frees any resources | ||
func (s *SaramaConsumer) Cleanup(session sarama.ConsumerGroupSession) error { | ||
return nil | ||
} | ||
|
||
//ConsumeClaim synchronously consumes all the messages in a claim, optionally delaying consumption a fixed amount of time | ||
func (s *SaramaConsumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error { | ||
delay := time.Second * time.Duration(s.config.KafkaDelay) | ||
for message := range claim.Messages() { | ||
m := kafka_sarama.NewMessageFromConsumerMessage(message) | ||
timestamp := message.Timestamp | ||
scheduledTime := timestamp.Add(delay) | ||
remaining := time.Now().Sub(scheduledTime) | ||
if delay == 0 || remaining > 0 { | ||
timer := time.NewTimer(remaining) | ||
select { | ||
case <-timer.C: | ||
break | ||
case <-session.Context().Done(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the context terminated when a rebalance happens? |
||
timer.Stop() | ||
return nil | ||
} | ||
} | ||
// just ignore non-cloud event messages | ||
if rs, rserr := binding.ToEvent(context.Background(), m); rserr == nil { | ||
s.handleCloudEvent(*rs) | ||
|
@@ -76,10 +152,12 @@ func (s *SaramaConsumer) sendToDeadLetterTopic(ce cloudevents.Event) { | |
} | ||
} | ||
|
||
//RegisterHandler register a handler to process events | ||
func (s *SaramaConsumer) RegisterHandler(handler EventHandler) { | ||
s.handlers = append(s.handlers, handler) | ||
} | ||
|
||
//Start starts an async consumer goproc (that may sleep) | ||
func (s *SaramaConsumer) Start(ctx context.Context) error { | ||
if err := s.initialize(); err != nil { | ||
return err | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think envocnfig supports parsing
time.Duration
out of the box, so it should be possible to use it directly, instead ofint
.Should we provide sensible defaults?