-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
probe.go
260 lines (224 loc) · 6.88 KB
/
probe.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
258
259
260
/*
Copyright 2019 The Knative 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 readiness
import (
"context"
"fmt"
"io"
"os"
"sync"
"time"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/serving/pkg/queue/health"
)
const (
aggressiveProbeTimeout = 100 * time.Millisecond
// PollTimeout is set equal to the queue-proxy's ExecProbe timeout to take
// advantage of the full window
PollTimeout = 10 * time.Second
retryInterval = 50 * time.Millisecond
)
// Probe holds all wrapped *corev1.Probe along with a barrier to sync single probing execution
type Probe struct {
probes []*wrappedProbe
out io.Writer // To make tests not log errors in good cases.
// Barrier sync to ensure only one probe is happening at the same time.
// When a probe is active `gv` will be non-nil.
// When the probe finishes the `gv` will be reset to nil.
mu sync.RWMutex
gv *gateValue
}
type wrappedProbe struct {
*corev1.Probe
count int32
pollTimeout time.Duration // To make tests not run for 10 seconds.
out io.Writer // To make tests not log errors in good cases.
autoDetectHTTP2 bool // Feature gate to enable HTTP2 auto-detection.
}
// gateValue is a write-once boolean impl.
type gateValue struct {
broadcast chan struct{}
result bool
}
// newGV returns a gateValue which is ready to write.
func newGV() *gateValue {
return &gateValue{
broadcast: make(chan struct{}),
}
}
// only `writer` must call `write` to set the value.
// `write` will panic if called more than once.
func (gv *gateValue) write(val bool) {
gv.result = val
close(gv.broadcast)
}
// `read` can be called multiple times.
func (gv *gateValue) read() bool {
<-gv.broadcast
return gv.result
}
// NewProbe returns a pointer to a new Probe.
func NewProbe(probes []*corev1.Probe) *Probe {
wrappedProbes := []*wrappedProbe{}
for _, p := range probes {
wrappedProbes = append(wrappedProbes, &wrappedProbe{
Probe: p,
out: os.Stderr,
pollTimeout: PollTimeout,
})
}
return &Probe{
probes: wrappedProbes,
out: os.Stderr,
}
}
// NewProbeWithHTTP2AutoDetection returns a pointer to a new Probe that has HTTP2
// auto-detection enabled.
func NewProbeWithHTTP2AutoDetection(probes []*corev1.Probe) *Probe {
wrappedProbes := []*wrappedProbe{}
for _, p := range probes {
wrappedProbes = append(wrappedProbes, &wrappedProbe{
Probe: p,
out: os.Stderr,
pollTimeout: PollTimeout,
autoDetectHTTP2: true,
})
}
return &Probe{
probes: wrappedProbes,
out: os.Stderr,
}
}
// shouldProbeAggressively indicates whether the Knative probe with aggressive retries should be used.
func (p *wrappedProbe) shouldProbeAggressively() bool {
return p.PeriodSeconds == 0
}
// ProbeContainer executes the defined Probe against the user-container
func (p *Probe) ProbeContainer() bool {
gv, writer := func() (*gateValue, bool) {
p.mu.Lock()
defer p.mu.Unlock()
// If gateValue exists (i.e. right now something is probing, attach to that probe).
if p.gv != nil {
return p.gv, false
}
p.gv = newGV()
return p.gv, true
}()
if writer {
res := p.probeContainerImpl()
gv.write(res)
p.mu.Lock()
defer p.mu.Unlock()
p.gv = nil
return res
}
return gv.read()
}
func (p *Probe) probeContainerImpl() bool {
var probeGroup errgroup.Group
for _, probe := range p.probes {
innerProbe := probe
probeGroup.Go(func() error {
switch {
case innerProbe.HTTPGet != nil:
return innerProbe.httpProbe()
case innerProbe.TCPSocket != nil:
return innerProbe.tcpProbe()
case innerProbe.GRPC != nil:
return innerProbe.grpcProbe()
case innerProbe.Exec != nil:
// Should never be reachable. Exec probes to be translated to
// TCP probes when container is built.
return fmt.Errorf("exec probe not supported")
default:
return fmt.Errorf("no probe found")
}
})
}
err := probeGroup.Wait()
if err != nil {
fmt.Fprintln(p.out, err.Error())
return false
}
return true
}
func (p *wrappedProbe) doProbe(probe func(time.Duration) error) error {
if !p.shouldProbeAggressively() {
return probe(time.Duration(p.TimeoutSeconds) * time.Second)
}
var failCount int
var lastProbeErr error
pollErr := wait.PollUntilContextTimeout(context.Background(), retryInterval, p.pollTimeout, true, func(context.Context) (bool, error) {
if err := probe(aggressiveProbeTimeout); err != nil {
// Reset count of consecutive successes to zero.
p.count = 0
// Don't log this now since we probe every 50ms and some failures are
// expected if the user container takes longer than that to start up.
// We'll log the lastProbeErr if we don't eventually succeed.
lastProbeErr = err
failCount++
return false, nil
}
p.count++
// Return success if count of consecutive successes is equal to or greater
// than the probe's SuccessThreshold.
return p.count >= p.SuccessThreshold, nil
})
if pollErr != nil && lastProbeErr != nil {
fmt.Fprintf(p.out, "aggressive probe error (failed %d times): %v\n", failCount, lastProbeErr)
}
return pollErr
}
// tcpProbe function executes TCP probe once if its standard probe
// otherwise TCP probe polls condition function which returns true
// if the probe count is greater than success threshold and false if TCP probe fails
func (p *wrappedProbe) tcpProbe() error {
config := health.TCPProbeConfigOptions{
Address: p.TCPSocket.Host + ":" + p.TCPSocket.Port.String(),
}
return p.doProbe(func(to time.Duration) error {
config.SocketTimeout = to
return health.TCPProbe(config)
})
}
// httpProbe function executes HTTP probe once if its standard probe
// otherwise HTTP probe polls condition function which returns true
// if the probe count is greater than success threshold and false if HTTP probe fails
func (p *wrappedProbe) httpProbe() error {
config := health.HTTPProbeConfigOptions{
HTTPGetAction: p.HTTPGet,
MaxProtoMajor: 1,
}
if p.autoDetectHTTP2 {
// A value of 0 indicates that the prober should find out which version is
// supported.
config.MaxProtoMajor = 0
}
return p.doProbe(func(to time.Duration) error {
config.Timeout = to
return health.HTTPProbe(config)
})
}
// grpcProbe function executes gRPC probe
func (p *wrappedProbe) grpcProbe() error {
config := health.GRPCProbeConfigOptions{
GRPCAction: p.GRPC,
}
return p.doProbe(func(to time.Duration) error {
config.Timeout = to
return health.GRPCProbe(config)
})
}