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

Add Consumer.HighWaterMarks() #769

Merged
merged 3 commits into from
Nov 29, 2016
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
20 changes: 20 additions & 0 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type Consumer interface {
// or OffsetOldest
ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error)

// HighWaterMarks returns the current high water marks for each topic and partition
// Consistency between partitions is not garanteed since high water marks are updated separately.
HighWaterMarks() map[string]map[int32]int64

// Close shuts down the consumer. It must be called after all child
// PartitionConsumers have already been closed.
Close() error
Expand Down Expand Up @@ -163,6 +167,22 @@ func (c *consumer) ConsumePartition(topic string, partition int32, offset int64)
return child, nil
}

func (c *consumer) HighWaterMarks() map[string]map[int32]int64 {
c.lock.Lock()
defer c.lock.Unlock()

hwms := make(map[string]map[int32]int64)
for topic, p := range c.children {
hwm := make(map[int32]int64, len(p))
for partition, pc := range p {
hwm[partition] = pc.HighWaterMarkOffset()
}
hwms[topic] = hwm
}

return hwms
}

func (c *consumer) addChild(child *partitionConsumer) error {
c.lock.Lock()
defer c.lock.Unlock()
Expand Down
16 changes: 16 additions & 0 deletions mocks/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ func (c *Consumer) Partitions(topic string) ([]int32, error) {
return c.metadata[topic], nil
}

func (c *Consumer) HighWaterMarks() map[string]map[int32]int64 {
c.l.Lock()
defer c.l.Unlock()

hwms := make(map[string]map[int32]int64, len(c.partitionConsumers))
for topic, partitionConsumers := range c.partitionConsumers {
hwm := make(map[int32]int64, len(partitionConsumers))
for partition, pc := range partitionConsumers {
hwm[partition] = pc.HighWaterMarkOffset()
}
hwms[topic] = hwm
}

return hwms
}

// Close implements the Close method from the sarama.Consumer interface. It will close
// all registered PartitionConsumer instances.
func (c *Consumer) Close() error {
Expand Down