Skip to content

Commit

Permalink
repl: fix div by zero error
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo committed Aug 6, 2024
1 parent 5c90191 commit 5f0301c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/repl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const (
// multiple-flush-threads, which should help it group commit and still be fast.
// This is only used as an initial starting value. It will auto-scale based on the DefaultTargetBatchTime.
DefaultBatchSize = 1000

// minBatchSize is the minimum batch size that we will allow the targetBatchSize to be.
minBatchSize = 5
// DefaultTargetBatchTime is the target time for flushing REPLACE/DELETE statements.
DefaultTargetBatchTime = time.Millisecond * 500

Expand Down Expand Up @@ -623,6 +624,9 @@ func (c *Client) feedback(numberOfKeys int, d time.Duration) {
if len(c.timingHistory) >= 10 {
timePerKey := table.LazyFindP90(c.timingHistory)
newBatchSize := int64(float64(c.targetBatchTime) / float64(timePerKey))
if newBatchSize < minBatchSize {
newBatchSize = minBatchSize
}
atomic.StoreInt64(&c.targetBatchSize, newBatchSize)
c.timingHistory = nil // reset
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/repl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ func TestFeedback(t *testing.T) {
client.feedback(1000, time.Second)
}
assert.Equal(t, int64(500), client.targetBatchSize) // less keys.

// Test with a way slower chunk.
for range 10 {
client.feedback(500, time.Second*100)
}
assert.Equal(t, int64(5), client.targetBatchSize) // equals the minimum.
}

// TestBlockWait tests that the BlockWait function will:
Expand Down

0 comments on commit 5f0301c

Please sign in to comment.