Debian Codegraph @

pkg/util/flowcontrol/apf_filter.go

raw
1 /*
2 Copyright 2019 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package flowcontrol
18
19 import (
20 "context"
21 "strconv"
22 "time"
23
24 endpointsrequest "k8s.io/apiserver/pkg/endpoints/request"
25 "k8s.io/apiserver/pkg/server/httplog"
26 "k8s.io/apiserver/pkg/server/mux"
27 fq "k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing"
28 "k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing/eventclock"
29 fqs "k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing/queueset"
30 "k8s.io/apiserver/pkg/util/flowcontrol/metrics"
31 fcrequest "k8s.io/apiserver/pkg/util/flowcontrol/request"
32 kubeinformers "k8s.io/client-go/informers"
33 "k8s.io/klog/v2"
34 "k8s.io/utils/clock"
35
36 flowcontrol "k8s.io/api/flowcontrol/v1"
37 flowcontrolclient "k8s.io/client-go/kubernetes/typed/flowcontrol/v1"
38 )
39
40 // ConfigConsumerAsFieldManager is how the config consuminng
41 // controller appears in an ObjectMeta ManagedFieldsEntry.Manager
42 const ConfigConsumerAsFieldManager = "api-priority-and-fairness-config-consumer-v1"
43
44 // Interface defines how the API Priority and Fairness filter interacts with the underlying system.
45 type Interface interface {
46 // Handle takes care of queuing and dispatching a request
47 // characterized by the given digest. The given `noteFn` will be
48 // invoked with the results of request classification.
49 // The given `workEstimator` is called, if at all, after noteFn.
50 // `workEstimator` will be invoked only when the request
51 // is classified as non 'exempt'.
52 // 'workEstimator', when invoked, must return the
53 // work parameters for the request.
54 // If the request is queued then `queueNoteFn` will be called twice,
55 // first with `true` and then with `false`; otherwise
56 // `queueNoteFn` will not be called at all. If Handle decides
57 // that the request should be executed then `execute()` will be
58 // invoked once to execute the request; otherwise `execute()` will
59 // not be invoked.
60 // Handle() should never return while execute() is running, even if
61 // ctx is cancelled or times out.
62 Handle(ctx context.Context,
63 requestDigest RequestDigest,
64 noteFn func(fs *flowcontrol.FlowSchema, pl *flowcontrol.PriorityLevelConfiguration, flowDistinguisher string),
65 workEstimator func() fcrequest.WorkEstimate,
66 queueNoteFn fq.QueueNoteFn,
67 execFn func(),
68 )
69
70 // Run monitors config objects from the main apiservers and causes
71 // any needed changes to local behavior. This method ceases
72 // activity and returns after the given channel is closed.
73 Run(stopCh <-chan struct{}) error
74
75 // Install installs debugging endpoints to the web-server.
76 Install(c *mux.PathRecorderMux)
77
78 // WatchTracker provides the WatchTracker interface.
79 WatchTracker
80
81 // MaxSeatsTracker is invoked from the work estimator to track max seats
82 // that can be occupied by a request for a priority level.
83 MaxSeatsTracker
84 }
85
86 // This request filter implements https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/1040-priority-and-fairness/README.md
87
88 // New creates a new instance to implement API priority and fairness
89 func New(
90 informerFactory kubeinformers.SharedInformerFactory,
91 flowcontrolClient flowcontrolclient.FlowcontrolV1Interface,
92 serverConcurrencyLimit int,
93 ) Interface {
94 clk := eventclock.Real{}
95 return NewTestable(TestableConfig{
96 Name: "Controller",
97 Clock: clk,
98 AsFieldManager: ConfigConsumerAsFieldManager,
99 FoundToDangling: func(found bool) bool { return !found },
100 InformerFactory: informerFactory,
101 FlowcontrolClient: flowcontrolClient,
102 ServerConcurrencyLimit: serverConcurrencyLimit,
103 ReqsGaugeVec: metrics.PriorityLevelConcurrencyGaugeVec,
104 ExecSeatsGaugeVec: metrics.PriorityLevelExecutionSeatsGaugeVec,
105 QueueSetFactory: fqs.NewQueueSetFactory(clk),
106 })
107 }
108
109 // TestableConfig carries the parameters to an implementation that is testable
110 type TestableConfig struct {
111 // Name of the controller
112 Name string
113
114 // Clock to use in timing deliberate delays
115 Clock clock.PassiveClock
116
117 // AsFieldManager is the string to use in the metadata for
118 // server-side apply. Normally this is
119 // `ConfigConsumerAsFieldManager`. This is exposed as a parameter
120 // so that a test of competing controllers can supply different
121 // values.
122 AsFieldManager string
123
124 // FoundToDangling maps the boolean indicating whether a
125 // FlowSchema's referenced PLC exists to the boolean indicating
126 // that FlowSchema's status should indicate a dangling reference.
127 // This is a parameter so that we can write tests of what happens
128 // when servers disagree on that bit of Status.
129 FoundToDangling func(bool) bool
130
131 // InformerFactory to use in building the controller
132 InformerFactory kubeinformers.SharedInformerFactory
133
134 // FlowcontrolClient to use for manipulating config objects
135 FlowcontrolClient flowcontrolclient.FlowcontrolV1Interface
136
137 // ServerConcurrencyLimit for the controller to enforce
138 ServerConcurrencyLimit int
139
140 // GaugeVec for metrics about requests, broken down by phase and priority_level
141 ReqsGaugeVec metrics.RatioedGaugeVec
142
143 // RatioedGaugePairVec for metrics about seats occupied by all phases of execution
144 ExecSeatsGaugeVec metrics.RatioedGaugeVec
145
146 // QueueSetFactory for the queuing implementation
147 QueueSetFactory fq.QueueSetFactory
148 }
149
150 // NewTestable is extra flexible to facilitate testing
151 func NewTestable(config TestableConfig) Interface {
152 return newTestableController(config)
153 }
154
155 func (cfgCtlr *configController) Handle(ctx context.Context, requestDigest RequestDigest,
156 noteFn func(fs *flowcontrol.FlowSchema, pl *flowcontrol.PriorityLevelConfiguration, flowDistinguisher string),
157 workEstimator func() fcrequest.WorkEstimate,
158 queueNoteFn fq.QueueNoteFn,
159 execFn func()) {
160 fs, pl, isExempt, req, startWaitingTime := cfgCtlr.startRequest(ctx, requestDigest, noteFn, workEstimator, queueNoteFn)
161 queued := startWaitingTime != time.Time{}
162 if req == nil {
163 if queued {
164 observeQueueWaitTime(ctx, pl.Name, fs.Name, strconv.FormatBool(req != nil), cfgCtlr.clock.Since(startWaitingTime))
165 }
166 klog.V(7).Infof("Handle(%#+v) => fsName=%q, distMethod=%#+v, plName=%q, isExempt=%v, reject", requestDigest, fs.Name, fs.Spec.DistinguisherMethod, pl.Name, isExempt)
167 return
168 }
169 klog.V(7).Infof("Handle(%#+v) => fsName=%q, distMethod=%#+v, plName=%q, isExempt=%v, queued=%v", requestDigest, fs.Name, fs.Spec.DistinguisherMethod, pl.Name, isExempt, queued)
170 var executed bool
171 idle, panicking := true, true
172 defer func() {
173 klog.V(7).Infof("Handle(%#+v) => fsName=%q, distMethod=%#+v, plName=%q, isExempt=%v, queued=%v, Finish() => panicking=%v idle=%v",
174 requestDigest, fs.Name, fs.Spec.DistinguisherMethod, pl.Name, isExempt, queued, panicking, idle)
175 if idle {
176 cfgCtlr.maybeReap(pl.Name)
177 }
178 }()
179 idle = req.Finish(func() {
180 if queued {
181 observeQueueWaitTime(ctx, pl.Name, fs.Name, strconv.FormatBool(req != nil), cfgCtlr.clock.Since(startWaitingTime))
182 }
183 metrics.AddDispatch(ctx, pl.Name, fs.Name)
184 fqs.OnRequestDispatched(req)
185 executed = true
186 startExecutionTime := cfgCtlr.clock.Now()
187 defer func() {
188 executionTime := cfgCtlr.clock.Since(startExecutionTime)
189 httplog.AddKeyValue(ctx, "apf_execution_time", executionTime)
190 metrics.ObserveExecutionDuration(ctx, pl.Name, fs.Name, executionTime)
191 }()
192 execFn()
193 })
194 if queued && !executed {
195 observeQueueWaitTime(ctx, pl.Name, fs.Name, strconv.FormatBool(req != nil), cfgCtlr.clock.Since(startWaitingTime))
196 }
197 panicking = false
198 }
199
200 func observeQueueWaitTime(ctx context.Context, priorityLevelName, flowSchemaName, execute string, waitTime time.Duration) {
201 metrics.ObserveWaitingDuration(ctx, priorityLevelName, flowSchemaName, execute, waitTime)
202 endpointsrequest.TrackAPFQueueWaitLatency(ctx, waitTime)
203 }