Skip to content

Commit

Permalink
Query: fix exemplarsServer data race (#4777)
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmiehan <hanjinming@outlook.com>
  • Loading branch information
hanjm committed Oct 28, 2021
1 parent 5eacdf0 commit 3a5450f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pkg/exemplars/exemplars.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package exemplars
import (
"context"
"sort"
"sync"

"github.com/pkg/errors"
"github.com/prometheus/prometheus/storage"
Expand Down Expand Up @@ -37,10 +38,13 @@ type exemplarsServer struct {

warnings []error
data []*exemplarspb.ExemplarData
mu sync.Mutex
}

func (srv *exemplarsServer) Send(res *exemplarspb.ExemplarsResponse) error {
if res.GetWarning() != "" {
srv.mu.Lock()
defer srv.mu.Unlock()
srv.warnings = append(srv.warnings, errors.New(res.GetWarning()))
return nil
}
Expand All @@ -49,6 +53,8 @@ func (srv *exemplarsServer) Send(res *exemplarspb.ExemplarsResponse) error {
return errors.New("empty exemplars data")
}

srv.mu.Lock()
defer srv.mu.Unlock()
srv.data = append(srv.data, res.GetData())
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/exemplars/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ func TestProxy(t *testing.T) {
}
}

// TestProxyDataRace find the concurrent data race bug.
// go test -race -run TestProxyDataRace -v
// TestProxyDataRace find the concurrent data race bug ( go test -race -run TestProxyDataRace -v ).
func TestProxyDataRace(t *testing.T) {
logger := log.NewLogfmtLogger(os.Stderr)
p := NewProxy(logger, func() []*exemplarspb.ExemplarStore {
Expand All @@ -318,4 +317,4 @@ func TestProxyDataRace(t *testing.T) {
ctx: context.Background(),
}
_ = p.Exemplars(req, s)
}
}

0 comments on commit 3a5450f

Please sign in to comment.