From d389af83a9d1bc0cc936e9a8ac88b163c982116f Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Thu, 15 Mar 2018 18:49:36 +0000 Subject: [PATCH] Cleanup event backoff. Signed-off-by: Lantao Liu --- pkg/server/events.go | 14 +++++++------- pkg/server/events_test.go | 17 ++++++++--------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pkg/server/events.go b/pkg/server/events.go index 80ba45c91d27..0ba38444cc12 100644 --- a/pkg/server/events.go +++ b/pkg/server/events.go @@ -82,8 +82,7 @@ func newEventMonitor(c *containerstore.Store, s *sandboxstore.Store) *eventMonit sandboxStore: s, ctx: ctx, cancel: cancel, - backOff: newBackOff(backOffInitDuration, backOffMaxDuration, - backOffExpireCheckDuration, clock.RealClock{}), + backOff: newBackOff(), } } @@ -134,6 +133,7 @@ func (em *eventMonitor) start() (<-chan struct{}, error) { break } if em.backOff.isInBackOff(cID) { + logrus.Infof("Events for container %q is in backoff, enqueue event %+v", cID, evt) em.backOff.enBackOff(cID, evt) break } @@ -314,13 +314,13 @@ func handleSandboxExit(ctx context.Context, e *eventtypes.TaskExit, sb sandboxst return nil } -func newBackOff(min, max, check time.Duration, c clock.Clock) *backOff { +func newBackOff() *backOff { return &backOff{ queuePool: map[string]*backOffQueue{}, - minDuration: min, - maxDuration: max, - checkDuration: check, - clock: c, + minDuration: backOffInitDuration, + maxDuration: backOffMaxDuration, + checkDuration: backOffExpireCheckDuration, + clock: clock.RealClock{}, } } diff --git a/pkg/server/events_test.go b/pkg/server/events_test.go index c326f462c5b6..baef19c6ef0c 100644 --- a/pkg/server/events_test.go +++ b/pkg/server/events_test.go @@ -68,7 +68,8 @@ func TestBackOff(t *testing.T) { } 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 _, event := range queue.events { actual.enBackOff(k, event) @@ -92,18 +93,16 @@ func TestBackOff(t *testing.T) { notExistKey := "containerNotExist" 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") testClock.Sleep(backOffInitDuration) - expKeyMap := map[string]struct{}{} - for k := range inputQueues { - expKeyMap[k] = struct{}{} - } actKeyList := actual.getExpiredContainers() - actKeyMap := map[string]struct{}{} //assert.Equal can't compare slice without order - for _, k := range actKeyList { - actKeyMap[k] = struct{}{} + assert.Equal(t, len(inputQueues), len(actKeyList)) + for k := range inputQueues { + assert.Contains(t, actKeyList, k) } - assert.Equal(t, actKeyMap, expKeyMap) t.Logf("Should be able to get out all backOff events") doneQueues := map[string]*backOffQueue{}