Commit e4ef28f0 authored by jiangyaoguo's avatar jiangyaoguo

generate new event cache for every event sink

parent 8a95a82c
......@@ -104,6 +104,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
// The default math/rand package functions aren't thread safe, so create a
// new Rand object for each StartRecording call.
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
var eventCache *historyCache = NewEventCache()
return eventBroadcaster.StartEventWatcher(
func(event *api.Event) {
// Make a copy before modification, because there could be multiple listeners.
......@@ -111,7 +112,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
eventCopy := *event
event = &eventCopy
previousEvent := getEvent(event)
previousEvent := eventCache.getEvent(event)
updateExistingEvent := previousEvent.Count > 0
if updateExistingEvent {
event.Count = previousEvent.Count + 1
......@@ -122,7 +123,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
tries := 0
for {
if recordEvent(sink, event, updateExistingEvent) {
if recordEvent(sink, event, updateExistingEvent, eventCache) {
break
}
tries++
......@@ -156,7 +157,7 @@ func isKeyNotFoundError(err error) bool {
// was successfully recorded or discarded, false if it should be retried.
// If updateExistingEvent is false, it creates a new event, otherwise it updates
// existing event.
func recordEvent(sink EventSink, event *api.Event, updateExistingEvent bool) bool {
func recordEvent(sink EventSink, event *api.Event, updateExistingEvent bool, eventCache *historyCache) bool {
var newEvent *api.Event
var err error
if updateExistingEvent {
......@@ -169,7 +170,7 @@ func recordEvent(sink EventSink, event *api.Event, updateExistingEvent bool) boo
newEvent, err = sink.Create(event)
}
if err == nil {
addOrUpdateEvent(newEvent)
eventCache.addOrUpdateEvent(newEvent)
return true
}
......
......@@ -47,15 +47,17 @@ type historyCache struct {
cache *lru.Cache
}
var previousEvents = historyCache{cache: lru.New(maxLruCacheEntries)}
func NewEventCache() *historyCache {
return &historyCache{cache: lru.New(maxLruCacheEntries)}
}
// 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 (eventCache *historyCache) addOrUpdateEvent(newEvent *api.Event) history {
key := getEventKey(newEvent)
previousEvents.Lock()
defer previousEvents.Unlock()
previousEvents.cache.Add(
eventCache.Lock()
defer eventCache.Unlock()
eventCache.cache.Add(
key,
history{
Count: newEvent.Count,
......@@ -63,20 +65,20 @@ func addOrUpdateEvent(newEvent *api.Event) history {
Name: newEvent.Name,
ResourceVersion: newEvent.ResourceVersion,
})
return getEventFromCache(key)
return eventCache.getEventFromCache(key)
}
// 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 {
func (eventCache *historyCache) getEvent(event *api.Event) history {
key := getEventKey(event)
previousEvents.RLock()
defer previousEvents.RUnlock()
return getEventFromCache(key)
eventCache.RLock()
defer eventCache.RUnlock()
return eventCache.getEventFromCache(key)
}
func getEventFromCache(key string) history {
value, ok := previousEvents.cache.Get(key)
func (eventCache *historyCache) getEventFromCache(key string) history {
value, ok := eventCache.cache.Get(key)
if ok {
historyValue, ok := value.(history)
if ok {
......
......@@ -25,6 +25,7 @@ import (
func TestAddOrUpdateEventNoExisting(t *testing.T) {
// Arrange
eventCache := NewEventCache()
eventTime := unversioned.Now()
event := api.Event{
Reason: "my reasons are many",
......@@ -46,7 +47,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
}
// Act
result := addOrUpdateEvent(&event)
result := eventCache.addOrUpdateEvent(&event)
// Assert
compareEventWithHistoryEntry(&event, &result, t)
......@@ -54,6 +55,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
func TestAddOrUpdateEventExisting(t *testing.T) {
// Arrange
eventCache := NewEventCache()
event1Time := unversioned.Unix(2324, 2342)
event2Time := unversioned.Now()
event1 := api.Event{
......@@ -100,9 +102,9 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
}
// Act
addOrUpdateEvent(&event1)
result1 := addOrUpdateEvent(&event2)
result2 := getEvent(&event1)
eventCache.addOrUpdateEvent(&event1)
result1 := eventCache.addOrUpdateEvent(&event2)
result2 := eventCache.getEvent(&event1)
// Assert
compareEventWithHistoryEntry(&event2, &result1, t)
......@@ -111,6 +113,7 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
func TestGetEventNoExisting(t *testing.T) {
// Arrange
eventCache := NewEventCache()
event := api.Event{
Reason: "to be or not to be",
Message: "do I exist",
......@@ -129,7 +132,7 @@ func TestGetEventNoExisting(t *testing.T) {
}
// Act
existingEvent := getEvent(&event)
existingEvent := eventCache.getEvent(&event)
// Assert
if existingEvent.Count != 0 {
......@@ -139,6 +142,7 @@ func TestGetEventNoExisting(t *testing.T) {
func TestGetEventExisting(t *testing.T) {
// Arrange
eventCache := NewEventCache()
eventTime := unversioned.Now()
event := api.Event{
Reason: "do I exist",
......@@ -158,10 +162,10 @@ func TestGetEventExisting(t *testing.T) {
FirstTimestamp: eventTime,
LastTimestamp: eventTime,
}
addOrUpdateEvent(&event)
eventCache.addOrUpdateEvent(&event)
// Act
existingEvent := getEvent(&event)
existingEvent := eventCache.getEvent(&event)
// Assert
compareEventWithHistoryEntry(&event, &existingEvent, t)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment