Skip to content

Commit

Permalink
fix(pubsub): fix iterator distribution bound calculations (#6125)
Browse files Browse the repository at this point in the history
* fix iterator distribution bounds

* add comments to test

* run gofmt
  • Loading branch information
hongalex committed Jun 3, 2022
1 parent 70c3c84 commit 6c470ff
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pubsub/iterator.go
Expand Up @@ -161,8 +161,8 @@ func (it *messageIterator) checkDrained() {
// min/maxDurationPerLeaseExtension.
func (it *messageIterator) addToDistribution(receiveTime time.Time) {
d := time.Since(receiveTime)
d = minDuration(d, minDurationPerLeaseExtension)
d = maxDuration(d, maxDurationPerLeaseExtension)
d = maxDuration(d, minDurationPerLeaseExtension)
d = minDuration(d, maxDurationPerLeaseExtension)
it.ackTimeDist.Record(int(d / time.Second))
}

Expand Down
41 changes: 41 additions & 0 deletions pubsub/iterator_test.go
Expand Up @@ -496,3 +496,44 @@ func TestIterator_BoundedDuration(t *testing.T) {
})
}
}

func TestAddToDistribution(t *testing.T) {
srv := pstest.NewServer()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

srv.Publish(fullyQualifiedTopicName, []byte("creating a topic"), nil)

_, client, err := initConn(ctx, srv.Addr)
if err != nil {
t.Fatal(err)
}
iter := newMessageIterator(client.subc, fullyQualifiedTopicName, &pullOptions{})

// Start with a datapoint that's too small that should be bounded to 10s.
receiveTime := time.Now().Add(time.Duration(-1) * time.Second)
iter.addToDistribution(receiveTime)
deadline := iter.ackTimeDist.Percentile(.99)
want := 10
if deadline != want {
t.Errorf("99th percentile ack distribution got: %v, want %v", deadline, want)
}

// The next datapoint should not be bounded.
receiveTime = time.Now().Add(time.Duration(-300) * time.Second)
iter.addToDistribution(receiveTime)
deadline = iter.ackTimeDist.Percentile(.99)
want = 300
if deadline != want {
t.Errorf("99th percentile ack distribution got: %v, want %v", deadline, want)
}

// Lastly, add a datapoint that should be bounded to 600s
receiveTime = time.Now().Add(time.Duration(-1000) * time.Second)
iter.addToDistribution(receiveTime)
deadline = iter.ackTimeDist.Percentile(.99)
want = 600
if deadline != want {
t.Errorf("99th percentile ack distribution got: %v, want %v", deadline, want)
}
}

0 comments on commit 6c470ff

Please sign in to comment.