Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Fix exemplarsServer data race (#4777) #4805

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 35 additions & 3 deletions pkg/exemplars/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"reflect"
"sync"
"testing"

"github.com/go-kit/kit/log"
Expand All @@ -17,14 +18,15 @@ import (
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/testutil"
"go.uber.org/atomic"
"google.golang.org/grpc"
)

type testExemplarClient struct {
grpc.ClientStream
exemplarErr, recvErr error
response *exemplarspb.ExemplarsResponse
sentResponse bool
sentResponse atomic.Bool
}

func (t *testExemplarClient) String() string {
Expand All @@ -37,10 +39,10 @@ func (t *testExemplarClient) Recv() (*exemplarspb.ExemplarsResponse, error) {
return nil, t.recvErr
}

if t.sentResponse {
if t.sentResponse.Load() {
return nil, io.EOF
}
t.sentResponse = true
t.sentResponse.Store(true)

return t.response, nil
}
Expand All @@ -55,6 +57,7 @@ type testExemplarServer struct {
grpc.ServerStream
sendErr error
responses []*exemplarspb.ExemplarsResponse
mu sync.Mutex
}

func (t *testExemplarServer) String() string {
Expand All @@ -65,6 +68,8 @@ func (t *testExemplarServer) Send(response *exemplarspb.ExemplarsResponse) error
if t.sendErr != nil {
return t.sendErr
}
t.mu.Lock()
defer t.mu.Unlock()
t.responses = append(t.responses, response)
return nil
}
Expand Down Expand Up @@ -286,3 +291,30 @@ func TestProxy(t *testing.T) {
})
}
}

// 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 {
es := &exemplarspb.ExemplarStore{
ExemplarsClient: &testExemplarClient{
recvErr: errors.New("err"),
},
LabelSets: []labels.Labels{labels.FromMap(map[string]string{"cluster": "A"})},
}
size := 100
endpoints := make([]*exemplarspb.ExemplarStore, 0, size)
for i := 0; i < size; i++ {
endpoints = append(endpoints, es)
}
return endpoints
}, labels.FromMap(map[string]string{"query": "foo"}))
req := &exemplarspb.ExemplarsRequest{
Query: `http_request_duration_bucket{query="foo"}`,
PartialResponseStrategy: storepb.PartialResponseStrategy_WARN,
}
s := &exemplarsServer{
ctx: context.Background(),
}
_ = p.Exemplars(req, s)
}