Skip to content

Commit 33eaf5e

Browse files
committed
fix[cortextool]: fix cortextool ooo when export
Signed-off-by: Aguel Satria Wijaya <[email protected]>
1 parent e3ec67f commit 33eaf5e

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Order should be `CHANGE`, `FEATURE`, `ENHANCEMENT`, and `BUGFIX`
55
## unreleased/master
66

77
* [FEATURE] Add `--extra-headers` support for `cortextool rules` commands. #288
8+
* [BUGFIX] Fix out of bounds error on export with large timespans and/or series count.
89

910
## v0.11.0
1011

pkg/backfill/backfill.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func CreateBlocks(input IteratorCreator, mint, maxt int64, maxSamplesInAppender
8585

8686
var wroteHeader bool
8787

88-
for t := mint; t <= maxt; t = t + blockDuration {
88+
for t := mint; t <= maxt; t = t + blockDuration/2 {
8989
err := func() error {
9090
w, err := tsdb.NewBlockWriter(log.NewNopLogger(), outputDir, blockDuration)
9191
if err != nil {
@@ -101,7 +101,7 @@ func CreateBlocks(input IteratorCreator, mint, maxt int64, maxSamplesInAppender
101101
ctx := context.Background()
102102
app := w.Appender(ctx)
103103
i := input()
104-
tsUpper := t + blockDuration
104+
tsUpper := t + blockDuration/2
105105
samplesCount := 0
106106
for {
107107
err := i.Next()

pkg/commands/remote_read.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func (c *RemoteReadCommand) export(k *kingpin.ParseContext) error {
404404
return err
405405
}
406406

407-
iterator := func() backfill.Iterator {
407+
iteratorCreator := func() backfill.Iterator {
408408
return newTimeSeriesIterator(timeseries)
409409
}
410410

@@ -421,7 +421,7 @@ func (c *RemoteReadCommand) export(k *kingpin.ParseContext) error {
421421
defer pipeR.Close()
422422

423423
log.Infof("Store TSDB blocks in '%s'", c.tsdbPath)
424-
if err := backfill.CreateBlocks(iterator, int64(mint), int64(maxt), 1000, c.tsdbPath, true, pipeW); err != nil {
424+
if err := backfill.CreateBlocks(iteratorCreator, int64(mint), int64(maxt), 1000, c.tsdbPath, true, pipeW); err != nil {
425425
return err
426426
}
427427

pkg/commands/remote_read_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package commands
22

33
import (
4+
"fmt"
45
"io"
56
"testing"
7+
"time"
68

79
"github.com/prometheus/prometheus/prompb"
810
"github.com/stretchr/testify/assert"
11+
12+
"github.com/grafana/cortex-tools/pkg/backfill"
913
)
1014

1115
func TestTimeSeriesIterator(t *testing.T) {
@@ -144,3 +148,41 @@ func TestTimeSeriesIterator(t *testing.T) {
144148
}
145149

146150
}
151+
152+
// TestEarlyCommit writes samples of many series that don't fit into the same
153+
// append commit. It makes sure that batching the samples into many commits
154+
// doesn't cause the appends to advance the head block too far and make future
155+
// appends invalid.
156+
func TestEarlyCommit(t *testing.T) {
157+
maxSamplesPerBlock := 1000
158+
series := 100
159+
samples := 140
160+
161+
start := int64(time.Date(2023, 8, 30, 11, 42, 17, 0, time.UTC).UnixNano())
162+
inc := int64(time.Minute / time.Millisecond)
163+
end := start + (inc * int64(samples))
164+
ts := make([]*prompb.TimeSeries, series)
165+
for i := 0; i < series; i++ {
166+
s := &prompb.TimeSeries{
167+
Labels: []prompb.Label{
168+
{
169+
Name: "__name__",
170+
Value: fmt.Sprintf("metric_%d", i),
171+
},
172+
},
173+
Samples: make([]prompb.Sample, samples),
174+
}
175+
for j := 0; j < samples; j++ {
176+
s.Samples[j] = prompb.Sample{
177+
Value: float64(j),
178+
Timestamp: start + (inc * int64(j)),
179+
}
180+
}
181+
ts[i] = s
182+
}
183+
iterator := func() backfill.Iterator {
184+
return newTimeSeriesIterator(ts)
185+
}
186+
err := backfill.CreateBlocks(iterator, start, end, maxSamplesPerBlock, t.TempDir(), true, io.Discard)
187+
assert.NoError(t, err)
188+
}

0 commit comments

Comments
 (0)