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
915bc044
Commit
915bc044
authored
Sep 21, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13632 from jiangyaoguo/create-new-cache-for-every-event-sink
Auto commit by PR queue bot
parents
81ec641f
e4ef28f0
Expand all
Show 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 @
915bc044
...
@@ -104,6 +104,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
...
@@ -104,6 +104,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
// The default math/rand package functions aren't thread safe, so create a
// The default math/rand package functions aren't thread safe, so create a
// new Rand object for each StartRecording call.
// new Rand object for each StartRecording call.
randGen
:=
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
()))
randGen
:=
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
()))
var
eventCache
*
historyCache
=
NewEventCache
()
return
eventBroadcaster
.
StartEventWatcher
(
return
eventBroadcaster
.
StartEventWatcher
(
func
(
event
*
api
.
Event
)
{
func
(
event
*
api
.
Event
)
{
// Make a copy before modification, because there could be multiple listeners.
// Make a copy before modification, because there could be multiple listeners.
...
@@ -111,7 +112,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
...
@@ -111,7 +112,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
eventCopy
:=
*
event
eventCopy
:=
*
event
event
=
&
eventCopy
event
=
&
eventCopy
previousEvent
:=
getEvent
(
event
)
previousEvent
:=
eventCache
.
getEvent
(
event
)
updateExistingEvent
:=
previousEvent
.
Count
>
0
updateExistingEvent
:=
previousEvent
.
Count
>
0
if
updateExistingEvent
{
if
updateExistingEvent
{
event
.
Count
=
previousEvent
.
Count
+
1
event
.
Count
=
previousEvent
.
Count
+
1
...
@@ -122,7 +123,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
...
@@ -122,7 +123,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartRecordingToSink(sink EventSin
tries
:=
0
tries
:=
0
for
{
for
{
if
recordEvent
(
sink
,
event
,
updateExistingEvent
)
{
if
recordEvent
(
sink
,
event
,
updateExistingEvent
,
eventCache
)
{
break
break
}
}
tries
++
tries
++
...
@@ -156,7 +157,7 @@ func isKeyNotFoundError(err error) bool {
...
@@ -156,7 +157,7 @@ func isKeyNotFoundError(err error) bool {
// was successfully recorded or discarded, false if it should be retried.
// was successfully recorded or discarded, false if it should be retried.
// If updateExistingEvent is false, it creates a new event, otherwise it updates
// If updateExistingEvent is false, it creates a new event, otherwise it updates
// existing event.
// 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
newEvent
*
api
.
Event
var
err
error
var
err
error
if
updateExistingEvent
{
if
updateExistingEvent
{
...
@@ -169,7 +170,7 @@ func recordEvent(sink EventSink, event *api.Event, updateExistingEvent bool) boo
...
@@ -169,7 +170,7 @@ func recordEvent(sink EventSink, event *api.Event, updateExistingEvent bool) boo
newEvent
,
err
=
sink
.
Create
(
event
)
newEvent
,
err
=
sink
.
Create
(
event
)
}
}
if
err
==
nil
{
if
err
==
nil
{
addOrUpdateEvent
(
newEvent
)
eventCache
.
addOrUpdateEvent
(
newEvent
)
return
true
return
true
}
}
...
...
pkg/client/record/event_test.go
View file @
915bc044
This diff is collapsed.
Click to expand it.
pkg/client/record/events_cache.go
View file @
915bc044
...
@@ -47,15 +47,17 @@ type historyCache struct {
...
@@ -47,15 +47,17 @@ type historyCache struct {
cache
*
lru
.
Cache
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
// 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.
// 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
)
key
:=
getEventKey
(
newEvent
)
previousEvents
.
Lock
()
eventCache
.
Lock
()
defer
previousEvents
.
Unlock
()
defer
eventCache
.
Unlock
()
previousEvents
.
cache
.
Add
(
eventCache
.
cache
.
Add
(
key
,
key
,
history
{
history
{
Count
:
newEvent
.
Count
,
Count
:
newEvent
.
Count
,
...
@@ -63,20 +65,20 @@ func addOrUpdateEvent(newEvent *api.Event) history {
...
@@ -63,20 +65,20 @@ func addOrUpdateEvent(newEvent *api.Event) history {
Name
:
newEvent
.
Name
,
Name
:
newEvent
.
Name
,
ResourceVersion
:
newEvent
.
ResourceVersion
,
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
// getEvent returns the entry corresponding to the given event, if one exists, otherwise a history object
// with a count of 0 is returned.
// with a count of 0 is returned.
func
getEvent
(
event
*
api
.
Event
)
history
{
func
(
eventCache
*
historyCache
)
getEvent
(
event
*
api
.
Event
)
history
{
key
:=
getEventKey
(
event
)
key
:=
getEventKey
(
event
)
previousEvents
.
RLock
()
eventCache
.
RLock
()
defer
previousEvents
.
RUnlock
()
defer
eventCache
.
RUnlock
()
return
getEventFromCache
(
key
)
return
eventCache
.
getEventFromCache
(
key
)
}
}
func
getEventFromCache
(
key
string
)
history
{
func
(
eventCache
*
historyCache
)
getEventFromCache
(
key
string
)
history
{
value
,
ok
:=
previousEvents
.
cache
.
Get
(
key
)
value
,
ok
:=
eventCache
.
cache
.
Get
(
key
)
if
ok
{
if
ok
{
historyValue
,
ok
:=
value
.
(
history
)
historyValue
,
ok
:=
value
.
(
history
)
if
ok
{
if
ok
{
...
...
pkg/client/record/events_cache_test.go
View file @
915bc044
...
@@ -25,6 +25,7 @@ import (
...
@@ -25,6 +25,7 @@ import (
func
TestAddOrUpdateEventNoExisting
(
t
*
testing
.
T
)
{
func
TestAddOrUpdateEventNoExisting
(
t
*
testing
.
T
)
{
// Arrange
// Arrange
eventCache
:=
NewEventCache
()
eventTime
:=
unversioned
.
Now
()
eventTime
:=
unversioned
.
Now
()
event
:=
api
.
Event
{
event
:=
api
.
Event
{
Reason
:
"my reasons are many"
,
Reason
:
"my reasons are many"
,
...
@@ -46,7 +47,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
...
@@ -46,7 +47,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
}
}
// Act
// Act
result
:=
addOrUpdateEvent
(
&
event
)
result
:=
eventCache
.
addOrUpdateEvent
(
&
event
)
// Assert
// Assert
compareEventWithHistoryEntry
(
&
event
,
&
result
,
t
)
compareEventWithHistoryEntry
(
&
event
,
&
result
,
t
)
...
@@ -54,6 +55,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
...
@@ -54,6 +55,7 @@ func TestAddOrUpdateEventNoExisting(t *testing.T) {
func
TestAddOrUpdateEventExisting
(
t
*
testing
.
T
)
{
func
TestAddOrUpdateEventExisting
(
t
*
testing
.
T
)
{
// Arrange
// Arrange
eventCache
:=
NewEventCache
()
event1Time
:=
unversioned
.
Unix
(
2324
,
2342
)
event1Time
:=
unversioned
.
Unix
(
2324
,
2342
)
event2Time
:=
unversioned
.
Now
()
event2Time
:=
unversioned
.
Now
()
event1
:=
api
.
Event
{
event1
:=
api
.
Event
{
...
@@ -100,9 +102,9 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
...
@@ -100,9 +102,9 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
}
}
// Act
// Act
addOrUpdateEvent
(
&
event1
)
eventCache
.
addOrUpdateEvent
(
&
event1
)
result1
:=
addOrUpdateEvent
(
&
event2
)
result1
:=
eventCache
.
addOrUpdateEvent
(
&
event2
)
result2
:=
getEvent
(
&
event1
)
result2
:=
eventCache
.
getEvent
(
&
event1
)
// Assert
// Assert
compareEventWithHistoryEntry
(
&
event2
,
&
result1
,
t
)
compareEventWithHistoryEntry
(
&
event2
,
&
result1
,
t
)
...
@@ -111,6 +113,7 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
...
@@ -111,6 +113,7 @@ func TestAddOrUpdateEventExisting(t *testing.T) {
func
TestGetEventNoExisting
(
t
*
testing
.
T
)
{
func
TestGetEventNoExisting
(
t
*
testing
.
T
)
{
// Arrange
// Arrange
eventCache
:=
NewEventCache
()
event
:=
api
.
Event
{
event
:=
api
.
Event
{
Reason
:
"to be or not to be"
,
Reason
:
"to be or not to be"
,
Message
:
"do I exist"
,
Message
:
"do I exist"
,
...
@@ -129,7 +132,7 @@ func TestGetEventNoExisting(t *testing.T) {
...
@@ -129,7 +132,7 @@ func TestGetEventNoExisting(t *testing.T) {
}
}
// Act
// Act
existingEvent
:=
getEvent
(
&
event
)
existingEvent
:=
eventCache
.
getEvent
(
&
event
)
// Assert
// Assert
if
existingEvent
.
Count
!=
0
{
if
existingEvent
.
Count
!=
0
{
...
@@ -139,6 +142,7 @@ func TestGetEventNoExisting(t *testing.T) {
...
@@ -139,6 +142,7 @@ func TestGetEventNoExisting(t *testing.T) {
func
TestGetEventExisting
(
t
*
testing
.
T
)
{
func
TestGetEventExisting
(
t
*
testing
.
T
)
{
// Arrange
// Arrange
eventCache
:=
NewEventCache
()
eventTime
:=
unversioned
.
Now
()
eventTime
:=
unversioned
.
Now
()
event
:=
api
.
Event
{
event
:=
api
.
Event
{
Reason
:
"do I exist"
,
Reason
:
"do I exist"
,
...
@@ -158,10 +162,10 @@ func TestGetEventExisting(t *testing.T) {
...
@@ -158,10 +162,10 @@ func TestGetEventExisting(t *testing.T) {
FirstTimestamp
:
eventTime
,
FirstTimestamp
:
eventTime
,
LastTimestamp
:
eventTime
,
LastTimestamp
:
eventTime
,
}
}
addOrUpdateEvent
(
&
event
)
eventCache
.
addOrUpdateEvent
(
&
event
)
// Act
// Act
existingEvent
:=
getEvent
(
&
event
)
existingEvent
:=
eventCache
.
getEvent
(
&
event
)
// Assert
// Assert
compareEventWithHistoryEntry
(
&
event
,
&
existingEvent
,
t
)
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