Skip to content

Commit

Permalink
Merge pull request #769 from mathpl/feature/expose_high_water_mark
Browse files Browse the repository at this point in the history
Add Consumer.HighWaterMarks()
  • Loading branch information
eapache committed Nov 29, 2016
2 parents 353cc46 + b0e729b commit 4d11317
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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

0 comments on commit 4d11317

Please sign in to comment.