mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 15:13:08 +00:00
Switch events history to use LRU cache instead of map
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
@@ -45,7 +45,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
|
||||
}
|
||||
|
||||
// Act
|
||||
result := AddOrUpdateEvent(&event)
|
||||
result := addOrUpdateEvent(&event)
|
||||
|
||||
// Assert
|
||||
compareEventWithHistoryEntry(&event, &result, t)
|
||||
@@ -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)
|
||||
@@ -128,7 +128,7 @@ func TestGetEventNoExisting(t *testing.T) {
|
||||
}
|
||||
|
||||
// Act
|
||||
existingEvent := GetEvent(&event)
|
||||
existingEvent := getEvent(&event)
|
||||
|
||||
// Assert
|
||||
if existingEvent.Count != 0 {
|
||||
@@ -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.")
|
||||
|
Reference in New Issue
Block a user