Copyright 2018 The Kubernetes 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.
Original file: https://github.com/kubernetes/autoscaler/tree/cluster-autoscaler-chart-1.0.2/vertical-pod-autoscaler/pkg/recommender/model/aggregate_container_state.go
Copyright (c) 2024 China Unicom Digital Technology Co., Ltd.
Modifications:
Modified function SaveToCheckpoint
Modified struct ContainerStateAggregatorProxy
*/
package aggregate
import (
"fmt"
"math"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/util"
colocationv1 "openfuyao.com/colocation-management/pkg/apis/v1"
)
type ContainerNameToAggregateStateMap map[string]*AggregateContainerState
const (
SupportedCheckpointVersion = "v1"
)
var (
defaultControlledResources = []ResourceName{ResourceCPU, ResourceMemory}
)
type ContainerStateAggregator interface {
AddSample(sample *ContainerUsageSample)
SubtractSample(sample *ContainerUsageSample)
}
type AggregateContainerState struct {
AggregateCPUUsage util.Histogram
AggregateMemoryPeaks util.Histogram
FirstSampleStart time.Time
LastSampleStart time.Time
TotalSamplesCount int
CreationTime time.Time
}
func (a *AggregateContainerState) MergeContainerState(other *AggregateContainerState) {
a.AggregateCPUUsage.Merge(other.AggregateCPUUsage)
a.AggregateMemoryPeaks.Merge(other.AggregateMemoryPeaks)
if a.FirstSampleStart.IsZero() ||
(!other.FirstSampleStart.IsZero() && other.FirstSampleStart.Before(a.FirstSampleStart)) {
a.FirstSampleStart = other.FirstSampleStart
}
if other.LastSampleStart.After(a.LastSampleStart) {
a.LastSampleStart = other.LastSampleStart
}
a.TotalSamplesCount += other.TotalSamplesCount
}
func NewAggregateContainerState() *AggregateContainerState {
config := GetAggregationsConfig()
return &AggregateContainerState{
AggregateCPUUsage: util.NewDecayingHistogram(config.CPUHistogramOptions, config.CPUHistogramDecayHalfLife),
AggregateMemoryPeaks: util.NewDecayingHistogram(config.MemoryHistogramOptions, config.MemoryHistogramDecayHalfLife),
CreationTime: time.Now(),
}
}
func (a *AggregateContainerState) AddSample(sample *ContainerUsageSample) {
switch sample.Resource {
case ResourceCPU:
a.addCPUSample(sample)
case ResourceMemory:
a.AggregateMemoryPeaks.AddSample(BytesFromMemoryAmount(sample.Usage), 1.0, sample.MeasureStart)
default:
panic(fmt.Sprintf("AddSample doesn't support resource '%s'", sample.Resource))
}
}
func (a *AggregateContainerState) SubtractSample(sample *ContainerUsageSample) {
switch sample.Resource {
case ResourceMemory:
a.AggregateMemoryPeaks.SubtractSample(BytesFromMemoryAmount(sample.Usage), 1.0, sample.MeasureStart)
default:
panic(fmt.Sprintf("SubtractSample doesn't support resource '%s'", sample.Resource))
}
}
func (a *AggregateContainerState) addCPUSample(sample *ContainerUsageSample) {
cpuUsageCores := CoresFromCPUAmount(sample.Usage)
cpuRequestCores := CoresFromCPUAmount(sample.Request)
a.AggregateCPUUsage.AddSample(
cpuUsageCores, math.Max(cpuRequestCores, minSampleWeight), sample.MeasureStart)
if sample.MeasureStart.After(a.LastSampleStart) {
a.LastSampleStart = sample.MeasureStart
}
if a.FirstSampleStart.IsZero() || sample.MeasureStart.Before(a.FirstSampleStart) {
a.FirstSampleStart = sample.MeasureStart
}
a.TotalSamplesCount++
}
func (a *AggregateContainerState) SaveToCheckpoint() (*colocationv1.ContainerCheckpointStatus, error) {
memory, err := a.AggregateMemoryPeaks.SaveToChekpoint()
if err != nil {
return nil, err
}
cpu, err := a.AggregateCPUUsage.SaveToChekpoint()
if err != nil {
return nil, err
}
return &colocationv1.ContainerCheckpointStatus{
LastUpdateTime: metav1.NewTime(time.Now()),
FirstSampleStart: metav1.NewTime(a.FirstSampleStart),
LastSampleStart: metav1.NewTime(a.LastSampleStart),
TotalSamplesCount: a.TotalSamplesCount,
MemoryHistogram: *memory,
CPUHistogram: *cpu,
Version: SupportedCheckpointVersion,
}, nil
}
func (a *AggregateContainerState) LoadFromCheckpoint(checkpoint *colocationv1.ContainerCheckpointStatus) error {
if checkpoint.Version != SupportedCheckpointVersion {
return fmt.Errorf("unsupported checkpoint version %s", checkpoint.Version)
}
a.TotalSamplesCount = checkpoint.TotalSamplesCount
a.FirstSampleStart = checkpoint.FirstSampleStart.Time
a.LastSampleStart = checkpoint.LastSampleStart.Time
err := a.AggregateMemoryPeaks.LoadFromCheckpoint(&checkpoint.MemoryHistogram)
if err != nil {
return err
}
err = a.AggregateCPUUsage.LoadFromCheckpoint(&checkpoint.CPUHistogram)
if err != nil {
return err
}
return nil
}
func (a *AggregateContainerState) isExpired(now time.Time) bool {
if a.isEmpty() {
return now.Sub(a.CreationTime) >= GetAggregationsConfig().GetMemoryAggregationWindowLength()
}
return now.Sub(a.LastSampleStart) >= GetAggregationsConfig().GetMemoryAggregationWindowLength()
}
func (a *AggregateContainerState) isEmpty() bool {
return a.TotalSamplesCount == 0
}
type ContainerStateAggregatorProxy struct {
containerID ContainerID
cluster *NodeState
}
func NewContainerStateAggregatorProxy(cluster *NodeState, containerID ContainerID) ContainerStateAggregator {
return &ContainerStateAggregatorProxy{
containerID: containerID,
cluster: cluster}
}
func (p *ContainerStateAggregatorProxy) AddSample(sample *ContainerUsageSample) {
aggregator := p.cluster.findOrCreateAggregateContainerState(p.containerID)
aggregator.AddSample(sample)
}
func (p *ContainerStateAggregatorProxy) SubtractSample(sample *ContainerUsageSample) {
aggregator := p.cluster.findOrCreateAggregateContainerState(p.containerID)
aggregator.SubtractSample(sample)
}