-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
drain_test.go
231 lines (204 loc) · 6.38 KB
/
drain_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package server_test
import (
"context"
"io"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/kr/pretty"
"google.golang.org/grpc"
)
// TestDrain tests the Drain RPC.
func TestDrain(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
doTestDrain(t)
}
// doTestDrain runs the drain test.
func doTestDrain(tt *testing.T) {
var drainSleepCallCount = 0
t := newTestDrainContext(tt, &drainSleepCallCount)
defer t.Close()
// Issue a probe. We're not draining yet, so the probe should
// reflect that.
resp := t.sendProbe()
t.assertDraining(resp, false)
t.assertRemaining(resp, false)
t.assertEqual(0, drainSleepCallCount)
// Issue a drain without shutdown, so we can probe more afterwards.
resp = t.sendDrainNoShutdown()
t.assertDraining(resp, true)
t.assertRemaining(resp, true)
t.assertEqual(1, drainSleepCallCount)
// Issue another probe. This checks that the server is still running
// (i.e. Shutdown: false was effective), the draining status is
// still properly reported, and the server slept once.
resp = t.sendProbe()
t.assertDraining(resp, true)
// probe-only has no remaining.
t.assertRemaining(resp, false)
t.assertEqual(1, drainSleepCallCount)
// Issue another drain. Verify that the remaining is zero (i.e. complete)
// and that the server did not sleep again.
resp = t.sendDrainNoShutdown()
t.assertDraining(resp, true)
t.assertRemaining(resp, false)
t.assertEqual(1, drainSleepCallCount)
// Now issue a drain request without drain but with shutdown.
// We're expecting the node to be shut down after that.
resp = t.sendShutdown()
if resp != nil {
t.assertDraining(resp, true)
t.assertRemaining(resp, false)
t.assertEqual(1, drainSleepCallCount)
}
// Now expect the server to be shut down.
testutils.SucceedsSoon(t, func() error {
_, err := t.c.Drain(context.Background(), &serverpb.DrainRequest{Shutdown: false})
if grpcutil.IsClosedConnection(err) {
return nil
}
return errors.AssertionFailedf("server not yet refusing RPC, got %v", err)
})
}
type testDrainContext struct {
*testing.T
tc *testcluster.TestCluster
c serverpb.AdminClient
connCloser func()
}
func newTestDrainContext(t *testing.T, drainSleepCallCount *int) *testDrainContext {
tc := &testDrainContext{
T: t,
tc: testcluster.StartTestCluster(t, 3, base.TestClusterArgs{
// We need to start the cluster insecure in order to not
// care about TLS settings for the RPC client connection.
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DrainSleepFn: func(time.Duration) {
*drainSleepCallCount++
},
},
},
Insecure: true,
},
}),
}
// We'll have the RPC talk to the first node.
var err error
tc.c, tc.connCloser, err = getAdminClientForServer(tc.tc.Server(0))
if err != nil {
tc.Close()
t.Fatal(err)
}
return tc
}
func (t *testDrainContext) Close() {
if t.connCloser != nil {
t.connCloser()
}
t.tc.Stopper().Stop(context.Background())
}
func (t *testDrainContext) sendProbe() *serverpb.DrainResponse {
return t.drainRequest(false /* drain */, false /* shutdown */)
}
func (t *testDrainContext) sendDrainNoShutdown() *serverpb.DrainResponse {
return t.drainRequest(true /* drain */, false /* shutdown */)
}
func (t *testDrainContext) drainRequest(drain, shutdown bool) *serverpb.DrainResponse {
// Issue a simple drain probe.
req := &serverpb.DrainRequest{Shutdown: shutdown}
if drain {
req.DoDrain = true
}
drainStream, err := t.c.Drain(context.Background(), req)
if err != nil {
t.Fatal(err)
}
resp, err := t.getDrainResponse(drainStream)
if err != nil {
t.Fatal(err)
}
return resp
}
func (t *testDrainContext) sendShutdown() *serverpb.DrainResponse {
req := &serverpb.DrainRequest{Shutdown: true}
drainStream, err := t.c.Drain(context.Background(), req)
if err != nil {
t.Fatal(err)
}
resp, err := t.getDrainResponse(drainStream)
if err != nil {
// It's possible we're getting "connection reset by peer" or some
// gRPC initialization failure because the server is shutting
// down. Tolerate that.
log.Infof(context.Background(), "RPC error: %v", err)
}
return resp
}
func (t *testDrainContext) assertDraining(resp *serverpb.DrainResponse, drain bool) {
if resp.IsDraining != drain {
t.Fatalf("expected draining %v, got %v", drain, resp.IsDraining)
}
}
func (t *testDrainContext) assertRemaining(resp *serverpb.DrainResponse, remaining bool) {
if actualRemaining := (resp.DrainRemainingIndicator > 0); remaining != actualRemaining {
t.Fatalf("expected remaining %v, got %v", remaining, actualRemaining)
}
}
func (t *testDrainContext) assertEqual(expected int, actual int) {
if expected == actual {
return
}
t.Fatalf("expected sleep call count to be %v, got %v", expected, actual)
}
func (t *testDrainContext) getDrainResponse(
stream serverpb.Admin_DrainClient,
) (*serverpb.DrainResponse, error) {
resp, err := stream.Recv()
if err != nil {
return nil, err
}
unexpected, err := stream.Recv()
if err != io.EOF {
if unexpected != nil {
t.Fatalf("unexpected additional response: %# v // %v", pretty.Formatter(unexpected), err)
}
if err == nil {
err = errors.New("unexpected response")
}
return nil, err
}
return resp, nil
}
func getAdminClientForServer(
s serverutils.TestServerInterface,
) (c serverpb.AdminClient, closer func(), err error) {
conn, err := grpc.Dial(s.ServingRPCAddr(), grpc.WithInsecure())
if err != nil {
return nil, nil, err
}
client := serverpb.NewAdminClient(conn)
return client, func() {
_ = conn.Close() // nolint:grpcconnclose
}, nil
}