Skip to content
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

Use workflowID as partition key on replication message #820

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/logging/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
TagTopicName = "topic-name"
TagConsumerName = "consumer-name"
TagPartition = "partition"
TagPartitionKey = "partition-key"
TagOffset = "offset"
TagScope = "scope"
TagFailover = "failover"
Expand Down
27 changes: 24 additions & 3 deletions common/messaging/kafkaProducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ func (p *kafkaProducer) Publish(task *replicator.ReplicationTask) error {
return err
}

partitionKey := p.getKey(task)

msg := &sarama.ProducerMessage{
Topic: p.topic,
Key: partitionKey,
Value: sarama.ByteEncoder(payload),
}

partition, offset, err := p.producer.SendMessage(msg)
if err != nil {
p.logger.WithFields(bark.Fields{
logging.TagPartition: partition,
logging.TagOffset: offset,
logging.TagErr: err,
logging.TagPartition: partition,
logging.TagPartitionKey: partitionKey,
logging.TagOffset: offset,
logging.TagErr: err,
}).Warn("Failed to publish message to kafka")

return err
Expand Down Expand Up @@ -119,3 +123,20 @@ func (p *kafkaProducer) serializeTask(task *replicator.ReplicationTask) ([]byte,

return payload, nil
}

func (p *kafkaProducer) getKey(task *replicator.ReplicationTask) sarama.Encoder {
if task == nil {
return nil
}

switch task.GetTaskType() {
case replicator.ReplicationTaskTypeHistory:
// Use workflowID as the partition key so all replication tasks for a workflow are dispatched to the same
// Kafka partition. This will give us some ordering guarantee for workflow replication tasks atleast at
// the messaging layer perspective
attributes := task.HistoryTaskAttributes
return sarama.StringEncoder(attributes.GetWorkflowId())
}

return nil
}