Skip to content

Commit

Permalink
fix: data race in seriesIterator, use atomic int (#12223)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyzannur authored Mar 15, 2024
1 parent 2544f00 commit 60dcee1
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/iter/sample_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iter
import (
"container/heap"
"context"
"go.uber.org/atomic"
"io"
"sync"

Expand Down Expand Up @@ -521,7 +522,7 @@ func NewSampleQueryResponseIterator(resp *logproto.SampleQueryResponse) SampleIt
}

type seriesIterator struct {
i int
i *atomic.Int32
series logproto.Series
}

Expand Down Expand Up @@ -567,14 +568,14 @@ func NewMultiSeriesIterator(series []logproto.Series) SampleIterator {
// NewSeriesIterator iterates over sample in a series.
func NewSeriesIterator(series logproto.Series) SampleIterator {
return &seriesIterator{
i: -1,
i: atomic.NewInt32(-1),
series: series,
}
}

func (i *seriesIterator) Next() bool {
i.i++
return i.i < len(i.series.Samples)
i.i.Inc()
return int(i.i.Load()) < len(i.series.Samples)
}

func (i *seriesIterator) Error() error {
Expand All @@ -590,7 +591,7 @@ func (i *seriesIterator) StreamHash() uint64 {
}

func (i *seriesIterator) Sample() logproto.Sample {
return i.series.Samples[i.i]
return i.series.Samples[i.i.Load()]
}

func (i *seriesIterator) Close() error {
Expand Down

0 comments on commit 60dcee1

Please sign in to comment.