-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
health_streamer.go
257 lines (219 loc) · 7.31 KB
/
health_streamer.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
253
254
255
256
257
/*
Copyright 2020 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tabletserver
import (
"flag"
"fmt"
"io"
"sync"
"time"
"context"
"github.com/golang/protobuf/proto"
"vitess.io/vitess/go/history"
"vitess.io/vitess/go/sync2"
"vitess.io/vitess/go/vt/log"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
)
var (
// blpFunc is a legaacy feature.
// TODO(sougou): remove after legacy resharding worflows are removed.
blpFunc = vreplication.StatusSummary
errUnintialized = "tabletserver uninitialized"
streamHealthBufferSize = flag.Uint("stream_health_buffer_size", 20, "max streaming health entries to buffer per streaming health client")
)
// healthStreamer streams health information to callers.
type healthStreamer struct {
stats *tabletenv.Stats
degradedThreshold time.Duration
unhealthyThreshold sync2.AtomicDuration
mu sync.Mutex
ctx context.Context
cancel context.CancelFunc
clients map[chan *querypb.StreamHealthResponse]struct{}
state *querypb.StreamHealthResponse
history *history.History
}
func newHealthStreamer(env tabletenv.Env, alias topodatapb.TabletAlias) *healthStreamer {
return &healthStreamer{
stats: env.Stats(),
degradedThreshold: env.Config().Healthcheck.DegradedThresholdSeconds.Get(),
unhealthyThreshold: sync2.NewAtomicDuration(env.Config().Healthcheck.UnhealthyThresholdSeconds.Get()),
clients: make(map[chan *querypb.StreamHealthResponse]struct{}),
state: &querypb.StreamHealthResponse{
Target: &querypb.Target{},
TabletAlias: &alias,
RealtimeStats: &querypb.RealtimeStats{
HealthError: errUnintialized,
},
},
history: history.New(5),
}
}
func (hs *healthStreamer) InitDBConfig(target querypb.Target) {
// Weird test failures happen if we don't instantiate
// a separate variable.
inner := target
hs.state.Target = &inner
}
func (hs *healthStreamer) Open() {
hs.mu.Lock()
defer hs.mu.Unlock()
if hs.cancel != nil {
return
}
hs.ctx, hs.cancel = context.WithCancel(context.TODO())
}
func (hs *healthStreamer) Close() {
hs.mu.Lock()
defer hs.mu.Unlock()
if hs.cancel != nil {
hs.cancel()
hs.cancel = nil
}
}
func (hs *healthStreamer) Stream(ctx context.Context, callback func(*querypb.StreamHealthResponse) error) error {
ch, hsCtx := hs.register()
if hsCtx == nil {
return vterrors.Errorf(vtrpcpb.Code_UNAVAILABLE, "tabletserver is shutdown")
}
defer hs.unregister(ch)
for {
select {
case <-ctx.Done():
return nil
case <-hsCtx.Done():
return vterrors.Errorf(vtrpcpb.Code_UNAVAILABLE, "tabletserver is shutdown")
case shr, ok := <-ch:
if !ok {
return vterrors.Errorf(vtrpcpb.Code_RESOURCE_EXHAUSTED, "stream health buffer overflowed. client should reconnect for up-to-date status")
}
if err := callback(shr); err != nil {
if err == io.EOF {
return nil
}
return err
}
}
}
}
func (hs *healthStreamer) register() (chan *querypb.StreamHealthResponse, context.Context) {
hs.mu.Lock()
defer hs.mu.Unlock()
if hs.cancel == nil {
return nil, nil
}
ch := make(chan *querypb.StreamHealthResponse, *streamHealthBufferSize)
hs.clients[ch] = struct{}{}
// Send the current state immediately.
ch <- proto.Clone(hs.state).(*querypb.StreamHealthResponse)
return ch, hs.ctx
}
func (hs *healthStreamer) unregister(ch chan *querypb.StreamHealthResponse) {
hs.mu.Lock()
defer hs.mu.Unlock()
delete(hs.clients, ch)
}
func (hs *healthStreamer) ChangeState(tabletType topodatapb.TabletType, terTimestamp time.Time, lag time.Duration, err error, serving bool) {
hs.mu.Lock()
defer hs.mu.Unlock()
hs.state.Target.TabletType = tabletType
if tabletType == topodatapb.TabletType_MASTER {
hs.state.TabletExternallyReparentedTimestamp = terTimestamp.Unix()
} else {
hs.state.TabletExternallyReparentedTimestamp = 0
}
if err != nil {
hs.state.RealtimeStats.HealthError = err.Error()
} else {
hs.state.RealtimeStats.HealthError = ""
}
hs.state.RealtimeStats.SecondsBehindMaster = uint32(lag.Seconds())
hs.state.Serving = serving
hs.state.RealtimeStats.SecondsBehindMasterFilteredReplication, hs.state.RealtimeStats.BinlogPlayersCount = blpFunc()
hs.state.RealtimeStats.Qps = hs.stats.QPSRates.TotalRate()
shr := proto.Clone(hs.state).(*querypb.StreamHealthResponse)
for ch := range hs.clients {
select {
case ch <- shr:
default:
// We can't block this state change on broadcasting to a streaming health client, but we
// also don't want to silently fail to inform a streaming health client of a state change
// because it can allow a vtgate to get wedged in a state where it's wrong about whether
// a tablet is healthy and can't automatically recover (see
// https://github.com/vitessio/vitess/issues/5445). If we can't send a health update
// to this client we'll close() the channel which will ultimate fail the streaming health
// RPC and cause vtgates to reconnect.
//
// An alternative approach for streaming health would be to force a periodic broadcast even
// when there hasn't been an update and/or move away from using channels toward a model where
// old updates can be purged from the buffer in favor of more recent updates (since only the
// most recent health state really matters to gates).
log.Warning("A streaming health buffer is full. Closing the channel")
close(ch)
delete(hs.clients, ch)
}
}
hs.history.Add(&historyRecord{
Time: time.Now(),
serving: shr.Serving,
tabletType: shr.Target.TabletType,
lag: lag,
err: err,
})
}
func (hs *healthStreamer) AppendDetails(details []*kv) []*kv {
hs.mu.Lock()
defer hs.mu.Unlock()
if hs.state.Target.TabletType == topodatapb.TabletType_MASTER {
return details
}
sbm := time.Duration(hs.state.RealtimeStats.SecondsBehindMaster) * time.Second
class := healthyClass
switch {
case sbm > hs.unhealthyThreshold.Get():
class = unhealthyClass
case sbm > hs.degradedThreshold:
class = unhappyClass
}
details = append(details, &kv{
Key: "Replication Lag",
Class: class,
Value: fmt.Sprintf("%ds", hs.state.RealtimeStats.SecondsBehindMaster),
})
if hs.state.RealtimeStats.HealthError != "" {
details = append(details, &kv{
Key: "Replication Error",
Class: unhappyClass,
Value: hs.state.RealtimeStats.HealthError,
})
}
return details
}
func (hs *healthStreamer) SetUnhealthyThreshold(v time.Duration) {
hs.unhealthyThreshold.Set(v)
shr := proto.Clone(hs.state).(*querypb.StreamHealthResponse)
for ch := range hs.clients {
select {
case ch <- shr:
default:
log.Info("Resetting health streamer clients due to unhealthy threshold change")
close(ch)
delete(hs.clients, ch)
}
}
}