-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathrpc_node_shutdown.go
252 lines (231 loc) · 7.94 KB
/
rpc_node_shutdown.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2022 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 cli
import (
"context"
"fmt"
"io"
"math"
"time"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/util/contextutil"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// drainAndShutdown attempts to drain the server and then shut it
// down.
func drainAndShutdown(ctx context.Context, c serverpb.AdminClient, targetNode string) (err error) {
hardError, remainingWork, err := doDrain(ctx, c, targetNode)
if hardError {
return err
}
if remainingWork {
log.Warningf(ctx, "graceful shutdown may not have completed successfully; check the node's logs for details.")
}
if err != nil {
log.Warningf(ctx, "drain did not complete successfully; hard shutdown may cause disruption")
}
// We have already performed the drain above, so now go straight to
// shutdown. We try twice just in case there is a transient error.
hardErr, err := doShutdown(ctx, c, targetNode)
if err != nil && !hardErr {
log.Warningf(ctx, "hard shutdown attempt failed, retrying: %v", err)
_, err = doShutdown(ctx, c, targetNode)
}
return errors.Wrap(err, "hard shutdown failed")
}
// doDrain calls a graceful drain.
//
// If the function returns hardError true, then the caller should not
// proceed with an alternate strategy (it's likely the server has gone
// away).
func doDrain(
ctx context.Context, c serverpb.AdminClient, targetNode string,
) (hardError, remainingWork bool, err error) {
// The next step is to drain. The timeout is configurable
// via --drain-wait.
if drainCtx.drainWait == 0 {
return doDrainNoTimeout(ctx, c, targetNode)
}
shutdownSettings, err := c.Settings(ctx, &serverpb.SettingsRequest{
Keys: []string{
"server.shutdown.drain_wait",
"server.shutdown.connection_wait",
"server.shutdown.query_wait",
"server.shutdown.lease_transfer_wait",
},
UnredactedValues: true,
})
if err != nil {
return false, true, err
}
minWait := time.Duration(0)
for k, v := range shutdownSettings.KeyValues {
wait, err := time.ParseDuration(v.Value)
if err != nil {
return false, true, err
}
minWait += wait
if k == "server.shutdown.query_wait" {
minWait += wait
}
}
if minWait > drainCtx.drainWait {
log.Infof(ctx, "--drain-wait is %s, but the server.shutdown.{drain,query,connection,lease_transfer}_wait "+
"cluster settings require a value of at least %s; using the larger value",
drainCtx.drainWait, minWait)
drainCtx.drainWait = minWait + 10*time.Second
}
err = contextutil.RunWithTimeout(ctx, "drain", drainCtx.drainWait, func(ctx context.Context) (err error) {
hardError, remainingWork, err = doDrainNoTimeout(ctx, c, targetNode)
return err
})
if errors.HasType(err, (*contextutil.TimeoutError)(nil)) || grpcutil.IsTimeout(err) {
log.Infof(ctx, "drain timed out: %v", err)
err = errors.New("drain timeout, consider adjusting --drain-wait, especially under " +
"custom server.shutdown.{drain,query,connection,lease_transfer}_wait cluster settings")
}
return
}
func doDrainNoTimeout(
ctx context.Context, c serverpb.AdminClient, targetNode string,
) (hardError, remainingWork bool, err error) {
defer func() {
if server.IsWaitingForInit(err) {
log.Infof(ctx, "%v", err)
err = errors.New("node cannot be drained before it has been initialized")
}
}()
var (
remaining = uint64(math.MaxUint64)
prevRemaining = uint64(math.MaxUint64)
verbose = false
)
for ; ; prevRemaining = remaining {
// Tell the user we're starting to drain. This enables the user to
// mentally prepare for something to take some time, as opposed to
// wondering why nothing is happening.
fmt.Fprintf(stderr, "node is draining... ") // notice no final newline.
// Send a drain request with the drain bit set and the shutdown bit
// unset.
stream, err := c.Drain(ctx, &serverpb.DrainRequest{
DoDrain: true,
Shutdown: false,
NodeId: targetNode,
Verbose: verbose,
})
if err != nil {
fmt.Fprintf(stderr, "\n") // finish the line started above.
return !grpcutil.IsTimeout(err), remaining > 0, errors.Wrap(err, "error sending drain request")
}
for {
resp, err := stream.Recv()
if err == io.EOF {
// Done.
break
}
if err != nil {
// Unexpected error.
fmt.Fprintf(stderr, "\n") // finish the line started above.
log.Infof(ctx, "graceful drain failed: %v", err)
return false, remaining > 0, err
}
if resp.IsDraining {
// We want to assert that the node is quitting, and tell the
// story about how much work was performed in logs for
// debugging.
remaining = resp.DrainRemainingIndicator
finalString := ""
if remaining == 0 {
finalString = " (complete)"
}
// We use stderr so that the stdout output remains a
// simple 'ok' in case of success (for compatibility with
// scripts).
fmt.Fprintf(stderr, "remaining: %d%s\n", remaining, finalString)
} else {
// Either the server has decided it wanted to stop quitting; or
// we're running a pre-20.1 node which doesn't populate IsDraining.
// In either case, we need to stop sending drain requests.
remaining = 0
fmt.Fprintf(stderr, "done\n")
}
if resp.DrainRemainingDescription != "" {
// Only show this information in the log; we'd use this for debugging.
// (This can be revealed e.g. via --logtostderr.)
log.Infof(ctx, "drain details: %s\n", resp.DrainRemainingDescription)
}
// Iterate until end of stream, which indicates the drain is
// complete.
}
if remaining == 0 {
// No more work to do.
break
}
// If range lease transfer stalls or the number of remaining leases
// somehow increases, verbosity is set to help with troubleshooting.
if remaining >= prevRemaining {
verbose = true
}
// Avoid a busy wait with high CPU/network usage if the server
// replies with an incomplete drain too quickly.
time.Sleep(200 * time.Millisecond)
}
return false, remaining > 0, nil
}
// doShutdown attempts to trigger a server shutdown *without*
// draining. Use doDrain() prior to perform a drain, or
// drainAndShutdown() to combine both.
func doShutdown(
ctx context.Context, c serverpb.AdminClient, targetNode string,
) (hardError bool, err error) {
defer func() {
if err != nil {
if server.IsWaitingForInit(err) {
log.Infof(ctx, "encountered error: %v", err)
err = errors.New("node cannot be shut down before it has been initialized")
err = errors.WithHint(err, "You can still stop the process using a service manager or a signal.")
hardError = true
}
if grpcutil.IsClosedConnection(err) {
// This most likely means that we shut down successfully. Note
// that sometimes the connection can be shut down even before a
// DrainResponse gets sent back to us, so we don't require a
// response on the stream (see #14184).
err = nil
}
}
}()
// We use a shorter timeout because a shutdown request has nothing
// else to do than shut down the node immediately.
err = contextutil.RunWithTimeout(ctx, "hard shutdown", 10*time.Second, func(ctx context.Context) error {
// Send a drain request with the drain bit unset (no drain).
// and the shutdown bit set.
stream, err := c.Drain(ctx, &serverpb.DrainRequest{NodeId: targetNode, Shutdown: true})
if err != nil {
return errors.Wrap(err, "error sending shutdown request")
}
for {
_, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
}
})
if !errors.HasType(err, (*contextutil.TimeoutError)(nil)) {
hardError = true
}
return hardError, err
}