| 1 |
/* |
| 2 |
Copyright 2022 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 request |
| 18 |
|
| 19 |
import ( |
| 20 |
"time" |
| 21 |
|
| 22 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 |
) |
| 24 |
|
| 25 |
const ( |
| 26 |
minimumSeats = 1 |
| 27 |
maximumSeatsLimit = 10 |
| 28 |
objectsPerSeat = 100.0 |
| 29 |
watchesPerSeat = 10.0 |
| 30 |
enableMutatingWorkEstimator = true |
| 31 |
) |
| 32 |
|
| 33 |
var eventAdditionalDuration = 5 * time.Millisecond |
| 34 |
|
| 35 |
// WorkEstimatorConfig holds work estimator parameters. |
| 36 |
type WorkEstimatorConfig struct { |
| 37 |
*ListWorkEstimatorConfig `json:"listWorkEstimatorConfig,omitempty"` |
| 38 |
*MutatingWorkEstimatorConfig `json:"mutatingWorkEstimatorConfig,omitempty"` |
| 39 |
|
| 40 |
// MinimumSeats is the minimum number of seats a request must occupy. |
| 41 |
MinimumSeats uint64 `json:"minimumSeats,omitempty"` |
| 42 |
|
| 43 |
// MaximumSeatsLimit is an upper limit on the max seats a request can occupy. |
| 44 |
// |
| 45 |
// NOTE: work_estimate_seats_samples metric uses the value of maximumSeats |
| 46 |
// as the upper bound, so when we change maximumSeats we should also |
| 47 |
// update the buckets of the metric. |
| 48 |
MaximumSeatsLimit uint64 `json:"maximumSeatsLimit,omitempty"` |
| 49 |
} |
| 50 |
|
| 51 |
// ListWorkEstimatorConfig holds work estimator parameters related to list requests. |
| 52 |
type ListWorkEstimatorConfig struct { |
| 53 |
ObjectsPerSeat float64 `json:"objectsPerSeat,omitempty"` |
| 54 |
} |
| 55 |
|
| 56 |
// MutatingWorkEstimatorConfig holds work estimator |
| 57 |
// parameters related to watches of mutating objects. |
| 58 |
type MutatingWorkEstimatorConfig struct { |
| 59 |
// TODO(wojtekt): Remove it once we tune the algorithm to not fail |
| 60 |
// scalability tests. |
| 61 |
Enabled bool `json:"enable,omitempty"` |
| 62 |
EventAdditionalDuration metav1.Duration `json:"eventAdditionalDurationMs,omitempty"` |
| 63 |
WatchesPerSeat float64 `json:"watchesPerSeat,omitempty"` |
| 64 |
} |
| 65 |
|
| 66 |
// DefaultWorkEstimatorConfig creates a new WorkEstimatorConfig with default values. |
| 67 |
func DefaultWorkEstimatorConfig() *WorkEstimatorConfig { |
| 68 |
return &WorkEstimatorConfig{ |
| 69 |
MinimumSeats: minimumSeats, |
| 70 |
MaximumSeatsLimit: maximumSeatsLimit, |
| 71 |
ListWorkEstimatorConfig: defaultListWorkEstimatorConfig(), |
| 72 |
MutatingWorkEstimatorConfig: defaultMutatingWorkEstimatorConfig(), |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// defaultListWorkEstimatorConfig creates a new ListWorkEstimatorConfig with default values. |
| 77 |
func defaultListWorkEstimatorConfig() *ListWorkEstimatorConfig { |
| 78 |
return &ListWorkEstimatorConfig{ObjectsPerSeat: objectsPerSeat} |
| 79 |
} |
| 80 |
|
| 81 |
// defaultMutatingWorkEstimatorConfig creates a new MutatingWorkEstimatorConfig with default values. |
| 82 |
func defaultMutatingWorkEstimatorConfig() *MutatingWorkEstimatorConfig { |
| 83 |
return &MutatingWorkEstimatorConfig{ |
| 84 |
Enabled: enableMutatingWorkEstimator, |
| 85 |
EventAdditionalDuration: metav1.Duration{Duration: eventAdditionalDuration}, |
| 86 |
WatchesPerSeat: watchesPerSeat, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// eventAdditionalDuration converts eventAdditionalDurationMs to a time.Duration type. |
| 91 |
func (c *MutatingWorkEstimatorConfig) eventAdditionalDuration() time.Duration { |
| 92 |
return c.EventAdditionalDuration.Duration |
| 93 |
} |