|
| 1 | +package worker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-kit/log" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/weaveworks/common/httpgrpc" |
| 13 | + "go.uber.org/atomic" |
| 14 | + "google.golang.org/grpc" |
| 15 | + "google.golang.org/grpc/metadata" |
| 16 | + |
| 17 | + "github.com/cortexproject/cortex/pkg/frontend/v2/frontendv2pb" |
| 18 | + "github.com/cortexproject/cortex/pkg/querier/stats" |
| 19 | + "github.com/cortexproject/cortex/pkg/scheduler/schedulerpb" |
| 20 | +) |
| 21 | + |
| 22 | +// mock querier request handler |
| 23 | +type mockRequestHandler struct { |
| 24 | + mock.Mock |
| 25 | +} |
| 26 | + |
| 27 | +func (m *mockRequestHandler) Handle(ctx context.Context, req *httpgrpc.HTTPRequest) (*httpgrpc.HTTPResponse, error) { |
| 28 | + args := m.Called(ctx, req) |
| 29 | + return args.Get(0).(*httpgrpc.HTTPResponse), args.Error(1) |
| 30 | +} |
| 31 | + |
| 32 | +type mockFrontendForQuerierServer struct { |
| 33 | + mock.Mock |
| 34 | +} |
| 35 | + |
| 36 | +func (m *mockFrontendForQuerierServer) QueryResult(_ context.Context, _ *frontendv2pb.QueryResultRequest) (*frontendv2pb.QueryResultResponse, error) { |
| 37 | + return &frontendv2pb.QueryResultResponse{}, nil |
| 38 | +} |
| 39 | + |
| 40 | +type mockSchedulerForQuerierClient struct { |
| 41 | + mock.Mock |
| 42 | +} |
| 43 | + |
| 44 | +func (m *mockSchedulerForQuerierClient) QuerierLoop(ctx context.Context, opts ...grpc.CallOption) (schedulerpb.SchedulerForQuerier_QuerierLoopClient, error) { |
| 45 | + args := m.Called(ctx, opts) |
| 46 | + return args.Get(0).(schedulerpb.SchedulerForQuerier_QuerierLoopClient), args.Error(1) |
| 47 | +} |
| 48 | + |
| 49 | +func (m *mockSchedulerForQuerierClient) NotifyQuerierShutdown(ctx context.Context, in *schedulerpb.NotifyQuerierShutdownRequest, opts ...grpc.CallOption) (*schedulerpb.NotifyQuerierShutdownResponse, error) { |
| 50 | + args := m.Called(ctx, in, opts) |
| 51 | + return args.Get(0).(*schedulerpb.NotifyQuerierShutdownResponse), args.Error(1) |
| 52 | +} |
| 53 | + |
| 54 | +// mock SchedulerForQuerier_QuerierLoopClient |
| 55 | +type mockQuerierLoopClient struct { |
| 56 | + ctx context.Context |
| 57 | + mock.Mock |
| 58 | +} |
| 59 | + |
| 60 | +func (m *mockQuerierLoopClient) Send(msg *schedulerpb.QuerierToScheduler) error { |
| 61 | + args := m.Called(msg) |
| 62 | + return args.Error(0) |
| 63 | +} |
| 64 | + |
| 65 | +func (m *mockQuerierLoopClient) Recv() (*schedulerpb.SchedulerToQuerier, error) { |
| 66 | + args := m.Called() |
| 67 | + |
| 68 | + if fn, ok := args.Get(0).(func() (*schedulerpb.SchedulerToQuerier, error)); ok { |
| 69 | + return fn() |
| 70 | + } |
| 71 | + |
| 72 | + return args.Get(0).(*schedulerpb.SchedulerToQuerier), args.Error(1) |
| 73 | +} |
| 74 | + |
| 75 | +func (m *mockQuerierLoopClient) Header() (metadata.MD, error) { |
| 76 | + args := m.Called() |
| 77 | + return args.Get(0).(metadata.MD), args.Error(1) |
| 78 | +} |
| 79 | + |
| 80 | +func (m *mockQuerierLoopClient) Trailer() metadata.MD { |
| 81 | + args := m.Called() |
| 82 | + return args.Get(0).(metadata.MD) |
| 83 | +} |
| 84 | + |
| 85 | +func (m *mockQuerierLoopClient) CloseSend() error { |
| 86 | + args := m.Called() |
| 87 | + return args.Error(0) |
| 88 | +} |
| 89 | + |
| 90 | +func (m *mockQuerierLoopClient) Context() context.Context { |
| 91 | + args := m.Called() |
| 92 | + return args.Get(0).(context.Context) |
| 93 | +} |
| 94 | + |
| 95 | +func (m *mockQuerierLoopClient) SendMsg(msg interface{}) error { |
| 96 | + args := m.Called(msg) |
| 97 | + return args.Error(0) |
| 98 | +} |
| 99 | + |
| 100 | +func (m *mockQuerierLoopClient) RecvMsg(msg interface{}) error { |
| 101 | + args := m.Called(msg) |
| 102 | + return args.Error(0) |
| 103 | +} |
| 104 | + |
| 105 | +// To show https://github.com/cortexproject/cortex/issues/6599 issue has been resolved |
| 106 | +func Test_ToShowNotPanic_RelatedIssue6599(t *testing.T) { |
| 107 | + cfg := Config{} |
| 108 | + frontendAddress := ":50001" |
| 109 | + userID := "user-1" |
| 110 | + recvCount := 20000 |
| 111 | + |
| 112 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) |
| 113 | + defer cancel() |
| 114 | + |
| 115 | + recvCall := atomic.Uint32{} |
| 116 | + |
| 117 | + // mocking query scheduler |
| 118 | + querierLoopClient := &mockQuerierLoopClient{} |
| 119 | + querierLoopClient.ctx = ctx |
| 120 | + querierLoopClient.On("Send", mock.Anything).Return(nil) |
| 121 | + querierLoopClient.On("Context").Return(querierLoopClient.ctx) |
| 122 | + querierLoopClient.On("Recv").Return(func() (*schedulerpb.SchedulerToQuerier, error) { |
| 123 | + recvCall.Add(1) |
| 124 | + if recvCall.Load() <= uint32(recvCount) { |
| 125 | + return &schedulerpb.SchedulerToQuerier{ |
| 126 | + QueryID: 1, |
| 127 | + HttpRequest: &httpgrpc.HTTPRequest{}, |
| 128 | + FrontendAddress: frontendAddress, |
| 129 | + UserID: userID, |
| 130 | + StatsEnabled: true, |
| 131 | + }, nil |
| 132 | + } else { |
| 133 | + <-querierLoopClient.ctx.Done() |
| 134 | + return nil, context.Canceled |
| 135 | + } |
| 136 | + |
| 137 | + }) |
| 138 | + |
| 139 | + requestHandler := &mockRequestHandler{} |
| 140 | + requestHandler.On("Handle", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { |
| 141 | + stat := stats.FromContext(args.Get(0).(context.Context)) |
| 142 | + |
| 143 | + // imitate add query-stat at fetchSeriesFromStores |
| 144 | + go stat.AddFetchedChunkBytes(10) |
| 145 | + }).Return(&httpgrpc.HTTPResponse{}, nil) |
| 146 | + |
| 147 | + sp, _ := newSchedulerProcessor(cfg, requestHandler, log.NewNopLogger(), nil) |
| 148 | + schedulerClient := &mockSchedulerForQuerierClient{} |
| 149 | + schedulerClient.On("QuerierLoop", mock.Anything, mock.Anything).Return(querierLoopClient, nil) |
| 150 | + |
| 151 | + sp.schedulerClientFactory = func(conn *grpc.ClientConn) schedulerpb.SchedulerForQuerierClient { |
| 152 | + return schedulerClient |
| 153 | + } |
| 154 | + |
| 155 | + // frontendForQuerierServer |
| 156 | + grpcServer := grpc.NewServer() |
| 157 | + server := &mockFrontendForQuerierServer{} |
| 158 | + frontendv2pb.RegisterFrontendForQuerierServer(grpcServer, server) |
| 159 | + |
| 160 | + lis, err := net.Listen("tcp", frontendAddress) |
| 161 | + require.NoError(t, err) |
| 162 | + stopChan := make(chan struct{}) |
| 163 | + go func() { |
| 164 | + defer close(stopChan) |
| 165 | + if err := grpcServer.Serve(lis); err != nil { |
| 166 | + return |
| 167 | + } |
| 168 | + }() |
| 169 | + defer func() { |
| 170 | + grpcServer.GracefulStop() |
| 171 | + <-stopChan // Wait util stop complete |
| 172 | + }() |
| 173 | + |
| 174 | + sp.processQueriesOnSingleStream(ctx, nil, lis.Addr().String()) |
| 175 | +} |
0 commit comments