Skip to content

Commit

Permalink
Fix bug where split chunk size could go below SplitUntil
Browse files Browse the repository at this point in the history
  • Loading branch information
vcschapp committed Jul 22, 2022
1 parent 2faca92 commit 7a80c01
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
4 changes: 3 additions & 1 deletion mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ const splitBits = 4

func (m *mgr) splitChunk(c *chunk) {
frac := c.duration() / splitBits
if hasSubSecondD(frac) {
if frac < c.stream.SplitUntil {
frac = c.stream.SplitUntil
} else if hasSubSecondD(frac) {
frac = frac + time.Second/2
frac = frac.Round(time.Second)
}
Expand Down
39 changes: 30 additions & 9 deletions mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1621,15 +1621,18 @@ func TestQueryManager_Query(t *testing.T) {
chunks []expectedChunk
}
testCases := []struct {
name string
chunks []expectedChunk
name string
splitUntil time.Duration
chunks []expectedChunk
}{
{
name: "Already Minimum Chunk Size",
chunks: []expectedChunk{{time.Second, 0, 2, nil}},
name: "Already Minimum Chunk Size",
splitUntil: time.Second,
chunks: []expectedChunk{{time.Second, 0, 2, nil}},
},
{
name: "One Split in Half",
name: "One Split in Half",
splitUntil: time.Second,
chunks: []expectedChunk{
{
size: 2 * time.Second,
Expand All @@ -1643,7 +1646,8 @@ func TestQueryManager_Query(t *testing.T) {
},
},
{
name: "One Split in Thirds",
name: "One Split in Thirds",
splitUntil: time.Second,
chunks: []expectedChunk{
{
size: 3 * time.Second,
Expand All @@ -1658,7 +1662,8 @@ func TestQueryManager_Query(t *testing.T) {
},
},
{
name: "One Split in Quarters",
name: "One Split in Quarters",
splitUntil: time.Second,
chunks: []expectedChunk{
{
size: 4 * time.Second,
Expand All @@ -1674,7 +1679,8 @@ func TestQueryManager_Query(t *testing.T) {
},
},
{
name: "Odd Splits",
name: "Odd Splits",
splitUntil: time.Second,
chunks: []expectedChunk{
{
size: 5 * time.Second,
Expand All @@ -1695,6 +1701,21 @@ func TestQueryManager_Query(t *testing.T) {
},
},
},
{
name: "Split Range Cannot Go Below SplitUntil",
splitUntil: 50 * time.Second,
chunks: []expectedChunk{
{
size: 100 * time.Second,
start: 0,
end: 2,
chunks: []expectedChunk{
{50 * time.Second, 0, 1, nil},
{50 * time.Second, 1, 2, nil},
},
},
},
},
}

// Run the sub-tests.
Expand Down Expand Up @@ -1778,7 +1799,7 @@ func TestQueryManager_Query(t *testing.T) {
End: defaultStart.Add(offset),
Limit: maxLimit,
Chunk: testCase.chunks[0].size,
SplitUntil: time.Second,
SplitUntil: testCase.splitUntil,
})
require.NoError(t, err)
require.NotNil(t, s)
Expand Down

0 comments on commit 7a80c01

Please sign in to comment.