Skip to content

Commit

Permalink
Merge pull request kubernetes#4444 from saad-ali/eventsCacheLru
Browse files Browse the repository at this point in the history
Switch events history to use LRU cache instead of map
  • Loading branch information
erictune committed Feb 18, 2015
2 parents dba24d1 + 2735e6a commit 7990b54
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 24 deletions.
4 changes: 4 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 121 additions & 0 deletions Godeps/_workspace/src/github.com/golang/groupcache/lru/lru.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions Godeps/_workspace/src/github.com/golang/groupcache/lru/lru_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/client/record/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func StartRecording(recorder EventRecorder, source api.EventSource) watch.Interf
event = &eventCopy
event.Source = source

previousEvent := GetEvent(event)
previousEvent := getEvent(event)
updateExistingEvent := previousEvent.Count > 0
if updateExistingEvent {
event.Count = previousEvent.Count + 1
Expand Down Expand Up @@ -102,7 +102,7 @@ func recordEvent(recorder EventRecorder, event *api.Event, updateExistingEvent b
newEvent, err = recorder.Create(event)
}
if err == nil {
AddOrUpdateEvent(newEvent)
addOrUpdateEvent(newEvent)
return true
}

Expand Down
45 changes: 31 additions & 14 deletions pkg/client/record/events_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package record
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/groupcache/lru"
"sync"
)

type History struct {
type history struct {
// The number of times the event has occured since first occurance.
Count int

Expand All @@ -36,36 +37,52 @@ type History struct {
ResourceVersion string
}

type historyMap struct {
const (
maxLruCacheEntries = 4096
)

type historyCache struct {
sync.RWMutex
table map[string]History
cache *lru.Cache
}

var previousEvents = historyMap{table: make(map[string]History)}
var previousEvents = historyCache{cache: lru.New(maxLruCacheEntries)}

// AddOrUpdateEvent creates a new entry for the given event in the previous events hash table if the event
// addOrUpdateEvent creates a new entry for the given event in the previous events hash table if the event
// doesn't already exist, otherwise it updates the existing entry.
func AddOrUpdateEvent(newEvent *api.Event) History {
func addOrUpdateEvent(newEvent *api.Event) history {
key := getEventKey(newEvent)
previousEvents.Lock()
defer previousEvents.Unlock()
previousEvents.table[key] =
History{
previousEvents.cache.Add(
key,
history{
Count: newEvent.Count,
FirstTimestamp: newEvent.FirstTimestamp,
Name: newEvent.Name,
ResourceVersion: newEvent.ResourceVersion,
}
return previousEvents.table[key]
})
return getEventFromCache(key)
}

// GetEvent returns the entry corresponding to the given event, if one exists, otherwise a History object
// with a count of 1 is returned.
func GetEvent(event *api.Event) History {
// getEvent returns the entry corresponding to the given event, if one exists, otherwise a history object
// with a count of 0 is returned.
func getEvent(event *api.Event) history {
key := getEventKey(event)
previousEvents.RLock()
defer previousEvents.RUnlock()
return previousEvents.table[key]
return getEventFromCache(key)
}

func getEventFromCache(key string) history {
value, ok := previousEvents.cache.Get(key)
if ok {
historyValue, ok := value.(history)
if ok {
return historyValue
}
}
return history{}
}

func getEventKey(event *api.Event) string {
Expand Down
16 changes: 8 additions & 8 deletions pkg/client/record/events_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
}

// Act
result := AddOrUpdateEvent(&event)
result := addOrUpdateEvent(&event)

// Assert
compareEventWithHistoryEntry(&event, &result, t)
Expand Down Expand Up @@ -99,9 +99,9 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
}

// Act
AddOrUpdateEvent(&event1)
result1 := AddOrUpdateEvent(&event2)
result2 := GetEvent(&event1)
addOrUpdateEvent(&event1)
result1 := addOrUpdateEvent(&event2)
result2 := getEvent(&event1)

// Assert
compareEventWithHistoryEntry(&event2, &result1, t)
Expand All @@ -128,7 +128,7 @@ func TestGetEventNoExisting(t *testing.T) {
}

// Act
existingEvent := GetEvent(&event)
existingEvent := getEvent(&event)

// Assert
if existingEvent.Count != 0 {
Expand Down Expand Up @@ -157,16 +157,16 @@ func TestGetEventExisting(t *testing.T) {
FirstTimestamp: eventTime,
LastTimestamp: eventTime,
}
AddOrUpdateEvent(&event)
addOrUpdateEvent(&event)

// Act
existingEvent := GetEvent(&event)
existingEvent := getEvent(&event)

// Assert
compareEventWithHistoryEntry(&event, &existingEvent, t)
}

func compareEventWithHistoryEntry(expected *api.Event, actual *History, t *testing.T) {
func compareEventWithHistoryEntry(expected *api.Event, actual *history, t *testing.T) {

if actual.Count != expected.Count {
t.Fatalf("There should be one existing instance of this event in the hash table.")
Expand Down

0 comments on commit 7990b54

Please sign in to comment.