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 0805393
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion pkg/exemplars/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,4 @@ func TestProxyDataRace(t *testing.T) {
ctx: context.Background(),
}
_ = p.Exemplars(req, s)
}
}

0 comments on commit 0805393

Please sign in to comment.