mirror of https://github.com/k3s-io/k3s
RWLock for cache
parent
a00e3748d4
commit
17977478e7
|
@ -52,7 +52,7 @@ type schedulerCache struct {
|
||||||
period time.Duration
|
period time.Duration
|
||||||
|
|
||||||
// This mutex guards all fields within this cache struct.
|
// This mutex guards all fields within this cache struct.
|
||||||
mu sync.Mutex
|
mu sync.RWMutex
|
||||||
// a set of assumed pod keys.
|
// a set of assumed pod keys.
|
||||||
// The key could further be used to get an entry in podStates.
|
// The key could further be used to get an entry in podStates.
|
||||||
assumedPods map[string]bool
|
assumedPods map[string]bool
|
||||||
|
@ -112,8 +112,8 @@ func newSchedulerCache(ttl, period time.Duration, stop <-chan struct{}) *schedul
|
||||||
// Snapshot takes a snapshot of the current schedulerCache. The method has performance impact,
|
// Snapshot takes a snapshot of the current schedulerCache. The method has performance impact,
|
||||||
// and should be only used in non-critical path.
|
// and should be only used in non-critical path.
|
||||||
func (cache *schedulerCache) Snapshot() *Snapshot {
|
func (cache *schedulerCache) Snapshot() *Snapshot {
|
||||||
cache.mu.Lock()
|
cache.mu.RLock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.RUnlock()
|
||||||
|
|
||||||
nodes := make(map[string]*NodeInfo)
|
nodes := make(map[string]*NodeInfo)
|
||||||
for k, v := range cache.nodes {
|
for k, v := range cache.nodes {
|
||||||
|
@ -164,8 +164,8 @@ func (cache *schedulerCache) List(selector labels.Selector) ([]*v1.Pod, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cache *schedulerCache) FilteredList(podFilter PodFilter, selector labels.Selector) ([]*v1.Pod, error) {
|
func (cache *schedulerCache) FilteredList(podFilter PodFilter, selector labels.Selector) ([]*v1.Pod, error) {
|
||||||
cache.mu.Lock()
|
cache.mu.RLock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.RUnlock()
|
||||||
// podFilter is expected to return true for most or all of the pods. We
|
// podFilter is expected to return true for most or all of the pods. We
|
||||||
// can avoid expensive array growth without wasting too much memory by
|
// can avoid expensive array growth without wasting too much memory by
|
||||||
// pre-allocating capacity.
|
// pre-allocating capacity.
|
||||||
|
@ -216,8 +216,8 @@ func (cache *schedulerCache) finishBinding(pod *v1.Pod, now time.Time) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.mu.Lock()
|
cache.mu.RLock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.RUnlock()
|
||||||
|
|
||||||
glog.V(5).Infof("Finished binding for pod %v. Can be expired.", key)
|
glog.V(5).Infof("Finished binding for pod %v. Can be expired.", key)
|
||||||
currState, ok := cache.podStates[key]
|
currState, ok := cache.podStates[key]
|
||||||
|
@ -387,8 +387,8 @@ func (cache *schedulerCache) IsAssumedPod(pod *v1.Pod) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.mu.Lock()
|
cache.mu.RLock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.RUnlock()
|
||||||
|
|
||||||
b, found := cache.assumedPods[key]
|
b, found := cache.assumedPods[key]
|
||||||
if !found {
|
if !found {
|
||||||
|
@ -403,8 +403,8 @@ func (cache *schedulerCache) GetPod(pod *v1.Pod) (*v1.Pod, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.mu.Lock()
|
cache.mu.RLock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.RUnlock()
|
||||||
|
|
||||||
podState, ok := cache.podStates[key]
|
podState, ok := cache.podStates[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -539,8 +539,8 @@ func (cache *schedulerCache) RemovePDB(pdb *policy.PodDisruptionBudget) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cache *schedulerCache) ListPDBs(selector labels.Selector) ([]*policy.PodDisruptionBudget, error) {
|
func (cache *schedulerCache) ListPDBs(selector labels.Selector) ([]*policy.PodDisruptionBudget, error) {
|
||||||
cache.mu.Lock()
|
cache.mu.RLock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.RUnlock()
|
||||||
var pdbs []*policy.PodDisruptionBudget
|
var pdbs []*policy.PodDisruptionBudget
|
||||||
for _, pdb := range cache.pdbs {
|
for _, pdb := range cache.pdbs {
|
||||||
if selector.Matches(labels.Set(pdb.Labels)) {
|
if selector.Matches(labels.Set(pdb.Labels)) {
|
||||||
|
@ -551,8 +551,8 @@ func (cache *schedulerCache) ListPDBs(selector labels.Selector) ([]*policy.PodDi
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cache *schedulerCache) IsUpToDate(n *NodeInfo) bool {
|
func (cache *schedulerCache) IsUpToDate(n *NodeInfo) bool {
|
||||||
cache.mu.Lock()
|
cache.mu.RLock()
|
||||||
defer cache.mu.Unlock()
|
defer cache.mu.RUnlock()
|
||||||
node, ok := cache.nodes[n.Node().Name]
|
node, ok := cache.nodes[n.Node().Name]
|
||||||
return ok && n.generation == node.generation
|
return ok && n.generation == node.generation
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue