forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Fix metadataServer/rulesServer/targetsServer data race (thanos…
…-io#4811) Signed-off-by: Jimmiehan <hanjinming@outlook.com>
- Loading branch information
Showing
6 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package metadata | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/pkg/errors" | ||
"github.com/thanos-io/thanos/pkg/metadata/metadatapb" | ||
"github.com/thanos-io/thanos/pkg/store/storepb" | ||
"go.uber.org/atomic" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type testMetadataClient struct { | ||
grpc.ClientStream | ||
metadataErr, recvErr error | ||
response *metadatapb.MetricMetadataResponse | ||
sentResponse atomic.Bool | ||
} | ||
|
||
func (t *testMetadataClient) String() string { | ||
return "test" | ||
} | ||
|
||
func (t *testMetadataClient) Recv() (*metadatapb.MetricMetadataResponse, error) { | ||
if t.recvErr != nil { | ||
return nil, t.recvErr | ||
} | ||
|
||
if t.sentResponse.Load() { | ||
return nil, io.EOF | ||
} | ||
t.sentResponse.Store(true) | ||
|
||
return t.response, nil | ||
} | ||
|
||
func (t *testMetadataClient) MetricMetadata(ctx context.Context, in *metadatapb.MetricMetadataRequest, opts ...grpc.CallOption) (metadatapb.Metadata_MetricMetadataClient, error) { | ||
return t, t.metadataErr | ||
} | ||
|
||
var _ metadatapb.MetadataClient = &testMetadataClient{} | ||
|
||
// 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() []metadatapb.MetadataClient { | ||
es := &testMetadataClient{ | ||
recvErr: errors.New("err"), | ||
} | ||
size := 100 | ||
endpoints := make([]metadatapb.MetadataClient, 0, size) | ||
for i := 0; i < size; i++ { | ||
endpoints = append(endpoints, es) | ||
} | ||
return endpoints | ||
}) | ||
req := &metadatapb.MetricMetadataRequest{ | ||
Metric: `http_request_duration_bucket`, | ||
PartialResponseStrategy: storepb.PartialResponseStrategy_WARN, | ||
} | ||
s := &metadataServer{ | ||
ctx: context.Background(), | ||
} | ||
_ = p.MetricMetadata(req, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package targets | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/pkg/errors" | ||
"github.com/thanos-io/thanos/pkg/store/storepb" | ||
"github.com/thanos-io/thanos/pkg/targets/targetspb" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type testTargetsClient struct { | ||
grpc.ClientStream | ||
targetsErr, recvErr error | ||
response *targetspb.TargetsResponse | ||
sentResponse bool | ||
} | ||
|
||
func (t *testTargetsClient) String() string { | ||
return "test" | ||
} | ||
|
||
func (t *testTargetsClient) Recv() (*targetspb.TargetsResponse, error) { | ||
// A simulation of underlying grpc Recv behavior as per https://github.com/grpc/grpc-go/blob/7f2581f910fc21497091c4109b56d310276fc943/stream.go#L117-L125. | ||
if t.recvErr != nil { | ||
return nil, t.recvErr | ||
} | ||
|
||
if t.sentResponse { | ||
return nil, io.EOF | ||
} | ||
t.sentResponse = true | ||
|
||
return t.response, nil | ||
} | ||
|
||
func (t *testTargetsClient) Targets(ctx context.Context, in *targetspb.TargetsRequest, opts ...grpc.CallOption) (targetspb.Targets_TargetsClient, error) { | ||
return t, t.targetsErr | ||
} | ||
|
||
var _ targetspb.TargetsClient = &testTargetsClient{} | ||
|
||
// 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() []targetspb.TargetsClient { | ||
es := &testTargetsClient{ | ||
recvErr: errors.New("err"), | ||
} | ||
size := 100 | ||
endpoints := make([]targetspb.TargetsClient, 0, size) | ||
for i := 0; i < size; i++ { | ||
endpoints = append(endpoints, es) | ||
} | ||
return endpoints | ||
}) | ||
req := &targetspb.TargetsRequest{ | ||
State: targetspb.TargetsRequest_ANY, | ||
PartialResponseStrategy: storepb.PartialResponseStrategy_WARN, | ||
} | ||
s := &targetsServer{ | ||
ctx: context.Background(), | ||
} | ||
_ = p.Targets(req, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters