github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/cpu/nativepolicy/policy_async_handler.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 nativepolicy
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/util/sets"
    25  
    26  	nativepolicyutil "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/nativepolicy/util"
    27  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    28  )
    29  
    30  // clearResidualState is used to clean residual pods in local state
    31  func (p *NativePolicy) clearResidualState() {
    32  	general.Infof("exec clearResidualState")
    33  	residualSet := make(map[string]bool)
    34  
    35  	if p.metaServer == nil {
    36  		general.Errorf("nil metaServer")
    37  		return
    38  	}
    39  
    40  	ctx := context.Background()
    41  	podList, err := p.metaServer.GetPodList(ctx, nil)
    42  	if err != nil {
    43  		general.Errorf("get pod list failed: %v", err)
    44  		return
    45  	}
    46  
    47  	podSet := sets.NewString()
    48  	for _, pod := range podList {
    49  		podSet.Insert(fmt.Sprintf("%v", pod.UID))
    50  	}
    51  
    52  	p.Lock()
    53  	defer p.Unlock()
    54  
    55  	podEntries := p.state.GetPodEntries()
    56  	for podUID := range podEntries {
    57  		if !podSet.Has(podUID) {
    58  			residualSet[podUID] = true
    59  			p.residualHitMap[podUID] += 1
    60  			general.Infof("found pod: %s with state but doesn't show up in pod watcher, hit count: %d", podUID, p.residualHitMap[podUID])
    61  		}
    62  	}
    63  
    64  	podsToDelete := sets.NewString()
    65  	for podUID, hitCount := range p.residualHitMap {
    66  		if !residualSet[podUID] {
    67  			general.Infof("already found pod: %s in pod watcher or its state is cleared, delete it from residualHitMap", podUID)
    68  			delete(p.residualHitMap, podUID)
    69  			continue
    70  		}
    71  
    72  		if time.Duration(hitCount)*stateCheckPeriod >= maxResidualTime {
    73  			podsToDelete.Insert(podUID)
    74  		}
    75  	}
    76  
    77  	if podsToDelete.Len() > 0 {
    78  		for {
    79  			podUID, found := podsToDelete.PopAny()
    80  			if !found {
    81  				break
    82  			}
    83  
    84  			general.Infof("clear residual pod: %s in state", podUID)
    85  			delete(podEntries, podUID)
    86  		}
    87  
    88  		updatedMachineState, err := nativepolicyutil.GenerateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
    89  		if err != nil {
    90  			general.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
    91  			return
    92  		}
    93  
    94  		p.state.SetPodEntries(podEntries)
    95  		p.state.SetMachineState(updatedMachineState)
    96  	}
    97  }