Merge pull request #669 from Random-Liu/cleanup-event-backoff

Cleanup event backoff.
This commit is contained in:
Lantao Liu 2018-03-15 16:28:59 -07:00 committed by GitHub
commit 1eaef9ee10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 16 deletions

View File

@ -82,8 +82,7 @@ func newEventMonitor(c *containerstore.Store, s *sandboxstore.Store) *eventMonit
sandboxStore: s, sandboxStore: s,
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
backOff: newBackOff(backOffInitDuration, backOffMaxDuration, backOff: newBackOff(),
backOffExpireCheckDuration, clock.RealClock{}),
} }
} }
@ -134,6 +133,7 @@ func (em *eventMonitor) start() (<-chan struct{}, error) {
break break
} }
if em.backOff.isInBackOff(cID) { if em.backOff.isInBackOff(cID) {
logrus.Infof("Events for container %q is in backoff, enqueue event %+v", cID, evt)
em.backOff.enBackOff(cID, evt) em.backOff.enBackOff(cID, evt)
break break
} }
@ -314,13 +314,13 @@ func handleSandboxExit(ctx context.Context, e *eventtypes.TaskExit, sb sandboxst
return nil return nil
} }
func newBackOff(min, max, check time.Duration, c clock.Clock) *backOff { func newBackOff() *backOff {
return &backOff{ return &backOff{
queuePool: map[string]*backOffQueue{}, queuePool: map[string]*backOffQueue{},
minDuration: min, minDuration: backOffInitDuration,
maxDuration: max, maxDuration: backOffMaxDuration,
checkDuration: check, checkDuration: backOffExpireCheckDuration,
clock: c, clock: clock.RealClock{},
} }
} }

View File

@ -68,7 +68,8 @@ func TestBackOff(t *testing.T) {
} }
t.Logf("Should be able to backOff a event") t.Logf("Should be able to backOff a event")
actual := newBackOff(backOffInitDuration, backOffMaxDuration, backOffExpireCheckDuration, testClock) actual := newBackOff()
actual.clock = testClock
for k, queue := range inputQueues { for k, queue := range inputQueues {
for _, event := range queue.events { for _, event := range queue.events {
actual.enBackOff(k, event) actual.enBackOff(k, event)
@ -92,18 +93,16 @@ func TestBackOff(t *testing.T) {
notExistKey := "containerNotExist" notExistKey := "containerNotExist"
assert.Equal(t, actual.isInBackOff(notExistKey), false) assert.Equal(t, actual.isInBackOff(notExistKey), false)
t.Logf("No containers should be expired")
assert.Empty(t, actual.getExpiredContainers())
t.Logf("Should be able to get all keys which are expired for backOff") t.Logf("Should be able to get all keys which are expired for backOff")
testClock.Sleep(backOffInitDuration) testClock.Sleep(backOffInitDuration)
expKeyMap := map[string]struct{}{}
for k := range inputQueues {
expKeyMap[k] = struct{}{}
}
actKeyList := actual.getExpiredContainers() actKeyList := actual.getExpiredContainers()
actKeyMap := map[string]struct{}{} //assert.Equal can't compare slice without order assert.Equal(t, len(inputQueues), len(actKeyList))
for _, k := range actKeyList { for k := range inputQueues {
actKeyMap[k] = struct{}{} assert.Contains(t, actKeyList, k)
} }
assert.Equal(t, actKeyMap, expKeyMap)
t.Logf("Should be able to get out all backOff events") t.Logf("Should be able to get out all backOff events")
doneQueues := map[string]*backOffQueue{} doneQueues := map[string]*backOffQueue{}