
Currently kubelet syncs all pods every 10s. This is not preferred because * Some pods may have been sync'd recently. * This may cause all the pods to be sync'd at once, causing undesirable CPU spikes. This PR replaces the global syncs with independent, periodic pod syncs. At the end of syncing, each pod worker will enqueue itslef with a future timestamp ( current time + sync interval), when it will be due for another sync. * If the pod worker encoutners an sync error, it may requeue with a different timestamp to retry sooner. * If a sync is triggered by the update channel (events or spec changes), the pod worker would enqueue a new sync time. This change is necessary for moving to long or no periodic sync period once pod lifecycle event generator is completed. We will still rely on the mechanism to requeue the pod on sync error. This change also makes sure that if a sync does not succeed (either due to real error or the per-container backoff mechanism), an error would be propagated back to the pod worker, which is responsible for requeuing.
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
/*
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
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.
|
|
*/
|
|
|
|
package queue
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"k8s.io/kubernetes/pkg/types"
|
|
"k8s.io/kubernetes/pkg/util"
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
|
)
|
|
|
|
func newTestBasicWorkQueue() (*basicWorkQueue, *util.FakeClock) {
|
|
fakeClock := &util.FakeClock{Time: time.Now()}
|
|
wq := &basicWorkQueue{
|
|
clock: fakeClock,
|
|
queue: make(map[types.UID]time.Time),
|
|
}
|
|
return wq, fakeClock
|
|
}
|
|
|
|
func compareResults(t *testing.T, expected, actual []types.UID) {
|
|
expectedSet := sets.NewString()
|
|
for _, u := range expected {
|
|
expectedSet.Insert(string(u))
|
|
}
|
|
actualSet := sets.NewString()
|
|
for _, u := range actual {
|
|
actualSet.Insert(string(u))
|
|
}
|
|
if !expectedSet.Equal(actualSet) {
|
|
t.Errorf("Expected %#v, got %#v", expectedSet.List(), actualSet.List())
|
|
}
|
|
}
|
|
|
|
func TestGetWork(t *testing.T) {
|
|
q, clock := newTestBasicWorkQueue()
|
|
q.Enqueue(types.UID("foo1"), -1*time.Minute)
|
|
q.Enqueue(types.UID("foo2"), -1*time.Minute)
|
|
q.Enqueue(types.UID("foo3"), 1*time.Minute)
|
|
q.Enqueue(types.UID("foo4"), 1*time.Minute)
|
|
expected := []types.UID{types.UID("foo1"), types.UID("foo2")}
|
|
compareResults(t, expected, q.GetWork())
|
|
compareResults(t, []types.UID{}, q.GetWork())
|
|
// Dial the time to 1 hour ahead.
|
|
clock.Step(time.Hour)
|
|
expected = []types.UID{types.UID("foo3"), types.UID("foo4")}
|
|
compareResults(t, expected, q.GetWork())
|
|
compareResults(t, []types.UID{}, q.GetWork())
|
|
}
|
|
|
|
func TestEnqueueKeepGreaterTimestamp(t *testing.T) {
|
|
q, _ := newTestBasicWorkQueue()
|
|
item := types.UID("foo")
|
|
q.Enqueue(item, -7*time.Hour)
|
|
q.Enqueue(item, 3*time.Hour)
|
|
compareResults(t, []types.UID{}, q.GetWork())
|
|
|
|
q.Enqueue(item, 3*time.Hour)
|
|
q.Enqueue(item, -7*time.Hour)
|
|
compareResults(t, []types.UID{}, q.GetWork())
|
|
}
|