|
| 1 | +/* |
| 2 | +Copyright 2023 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 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + flowcontrol "k8s.io/api/flowcontrol/v1beta3" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | + "k8s.io/apiserver/pkg/endpoints/request" |
| 29 | + fq "k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing" |
| 30 | + fqs "k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing/queueset" |
| 31 | + "k8s.io/apiserver/pkg/util/flowcontrol/fairqueuing/testing/eventclock" |
| 32 | + "k8s.io/apiserver/pkg/util/flowcontrol/metrics" |
| 33 | + fcrequest "k8s.io/apiserver/pkg/util/flowcontrol/request" |
| 34 | + "k8s.io/client-go/informers" |
| 35 | + clientsetfake "k8s.io/client-go/kubernetes/fake" |
| 36 | +) |
| 37 | + |
| 38 | +// TestQueueWaitTimeLatencyTracker tests the queue wait times recorded by the P&F latency tracker |
| 39 | +// when calling Handle. |
| 40 | +func TestQueueWaitTimeLatencyTracker(t *testing.T) { |
| 41 | + metrics.Register() |
| 42 | + |
| 43 | + var fsObj *flowcontrol.FlowSchema |
| 44 | + var plcObj *flowcontrol.PriorityLevelConfiguration |
| 45 | + cfgObjs := []runtime.Object{} |
| 46 | + |
| 47 | + plName := "test-pl" |
| 48 | + username := "test-user" |
| 49 | + fsName := "test-fs" |
| 50 | + lendable := int32(0) |
| 51 | + borrowingLimit := int32(0) |
| 52 | + fsObj = &flowcontrol.FlowSchema{ |
| 53 | + ObjectMeta: metav1.ObjectMeta{ |
| 54 | + Name: fsName, |
| 55 | + }, |
| 56 | + Spec: flowcontrol.FlowSchemaSpec{ |
| 57 | + MatchingPrecedence: 100, |
| 58 | + PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{ |
| 59 | + Name: plName, |
| 60 | + }, |
| 61 | + DistinguisherMethod: &flowcontrol.FlowDistinguisherMethod{ |
| 62 | + Type: flowcontrol.FlowDistinguisherMethodByUserType, |
| 63 | + }, |
| 64 | + Rules: []flowcontrol.PolicyRulesWithSubjects{{ |
| 65 | + Subjects: []flowcontrol.Subject{{ |
| 66 | + Kind: flowcontrol.SubjectKindUser, |
| 67 | + User: &flowcontrol.UserSubject{Name: username}, |
| 68 | + }}, |
| 69 | + NonResourceRules: []flowcontrol.NonResourcePolicyRule{{ |
| 70 | + Verbs: []string{"*"}, |
| 71 | + NonResourceURLs: []string{"*"}, |
| 72 | + }}, |
| 73 | + }}, |
| 74 | + }, |
| 75 | + } |
| 76 | + plcObj = &flowcontrol.PriorityLevelConfiguration{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Name: plName, |
| 79 | + }, |
| 80 | + Spec: flowcontrol.PriorityLevelConfigurationSpec{ |
| 81 | + Type: flowcontrol.PriorityLevelEnablementLimited, |
| 82 | + Limited: &flowcontrol.LimitedPriorityLevelConfiguration{ |
| 83 | + NominalConcurrencyShares: 100, |
| 84 | + LendablePercent: &lendable, |
| 85 | + BorrowingLimitPercent: &borrowingLimit, |
| 86 | + LimitResponse: flowcontrol.LimitResponse{ |
| 87 | + Type: flowcontrol.LimitResponseTypeQueue, |
| 88 | + Queuing: &flowcontrol.QueuingConfiguration{ |
| 89 | + Queues: 10, |
| 90 | + HandSize: 2, |
| 91 | + QueueLengthLimit: 10, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | + cfgObjs = append(cfgObjs, fsObj, plcObj) |
| 98 | + |
| 99 | + clientset := clientsetfake.NewSimpleClientset(cfgObjs...) |
| 100 | + informerFactory := informers.NewSharedInformerFactory(clientset, time.Second) |
| 101 | + flowcontrolClient := clientset.FlowcontrolV1beta3() |
| 102 | + startTime := time.Now() |
| 103 | + clk, _ := eventclock.NewFake(startTime, 0, nil) |
| 104 | + controller := newTestableController(TestableConfig{ |
| 105 | + Name: "Controller", |
| 106 | + Clock: clk, |
| 107 | + AsFieldManager: ConfigConsumerAsFieldManager, |
| 108 | + FoundToDangling: func(found bool) bool { return !found }, |
| 109 | + InformerFactory: informerFactory, |
| 110 | + FlowcontrolClient: flowcontrolClient, |
| 111 | + ServerConcurrencyLimit: 24, |
| 112 | + RequestWaitLimit: time.Minute, |
| 113 | + ReqsGaugeVec: metrics.PriorityLevelConcurrencyGaugeVec, |
| 114 | + ExecSeatsGaugeVec: metrics.PriorityLevelExecutionSeatsGaugeVec, |
| 115 | + QueueSetFactory: fqs.NewQueueSetFactory(clk), |
| 116 | + }) |
| 117 | + |
| 118 | + informerFactory.Start(nil) |
| 119 | + |
| 120 | + status := informerFactory.WaitForCacheSync(nil) |
| 121 | + if names := unsynced(status); len(names) > 0 { |
| 122 | + t.Fatalf("WaitForCacheSync did not successfully complete, resources=%#v", names) |
| 123 | + } |
| 124 | + |
| 125 | + go func() { |
| 126 | + controller.Run(nil) |
| 127 | + }() |
| 128 | + |
| 129 | + // ensure that the controller has run its first loop. |
| 130 | + err := wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (done bool, err error) { |
| 131 | + return controller.hasPriorityLevelState(plcObj.Name), nil |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + t.Errorf("expected the controller to reconcile the priority level configuration object: %s, error: %s", plcObj.Name, err) |
| 135 | + } |
| 136 | + |
| 137 | + reqInfo := &request.RequestInfo{ |
| 138 | + IsResourceRequest: false, |
| 139 | + Path: "/foobar", |
| 140 | + Verb: "GET", |
| 141 | + } |
| 142 | + noteFn := func(fs *flowcontrol.FlowSchema, plc *flowcontrol.PriorityLevelConfiguration, fd string) {} |
| 143 | + workEstr := func() fcrequest.WorkEstimate { return fcrequest.WorkEstimate{InitialSeats: 1} } |
| 144 | + |
| 145 | + flowUser := testUser{name: "test-user"} |
| 146 | + rd := RequestDigest{ |
| 147 | + RequestInfo: reqInfo, |
| 148 | + User: flowUser, |
| 149 | + } |
| 150 | + |
| 151 | + // Add 1 second to the fake clock during QueueNoteFn |
| 152 | + newTime := startTime.Add(time.Second) |
| 153 | + qnf := fq.QueueNoteFn(func(bool) { clk.FakePassiveClock.SetTime(newTime) }) |
| 154 | + ctx := request.WithLatencyTrackers(context.Background()) |
| 155 | + controller.Handle(ctx, rd, noteFn, workEstr, qnf, func() {}) |
| 156 | + |
| 157 | + latencyTracker, ok := request.LatencyTrackersFrom(ctx) |
| 158 | + if !ok { |
| 159 | + t.Fatalf("error getting latency tracker: %v", err) |
| 160 | + } |
| 161 | + |
| 162 | + expectedLatency := time.Second // newTime - startTime |
| 163 | + latency := latencyTracker.APFQueueWaitTracker.GetLatency() |
| 164 | + if latency != expectedLatency { |
| 165 | + t.Errorf("unexpected latency, got %s, expected %s", latency, expectedLatency) |
| 166 | + } |
| 167 | +} |
0 commit comments