Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
e4ef28f0
Commit
e4ef28f0
authored
Sep 07, 2015
by
jiangyaoguo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generate new event cache for every event sink
parent
8a95a82c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
23 deletions
+30
-23
event.go
pkg/client/record/event.go
+5
-4
event_test.go
pkg/client/record/event_test.go
+0
-0
events_cache.go
pkg/client/record/events_cache.go
+14
-12
events_cache_test.go
pkg/client/record/events_cache_test.go
+11
-7
No files found.
pkg/client/record/event.go
View file @
e4ef28f0
...
...
@@ -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
}
...
...
pkg/client/record/event_test.go
View file @
e4ef28f0
This diff is collapsed.
Click to expand it.
pkg/client/record/events_cache.go
View file @
e4ef28f0
...
...
@@ -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
{
...
...
pkg/client/record/events_cache_test.go
View file @
e4ef28f0
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment