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
8040719d
Commit
8040719d
authored
Oct 18, 2016
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid computing key func multiple times in cacher
parent
f10b0205
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
18 deletions
+14
-18
cacher.go
pkg/storage/cacher.go
+11
-18
watch_cache.go
pkg/storage/watch_cache.go
+3
-0
No files found.
pkg/storage/cacher.go
View file @
8040719d
...
@@ -125,6 +125,8 @@ func (i *indexedWatchers) terminateAll(objectType reflect.Type) {
...
@@ -125,6 +125,8 @@ func (i *indexedWatchers) terminateAll(objectType reflect.Type) {
}
}
}
}
type
filterObjectFunc
func
(
string
,
runtime
.
Object
)
bool
// Cacher is responsible for serving WATCH and LIST requests for a given
// Cacher is responsible for serving WATCH and LIST requests for a given
// resource from its internal cache and updating its cache in the background
// resource from its internal cache and updating its cache in the background
// based on the underlying storage contents.
// based on the underlying storage contents.
...
@@ -161,9 +163,6 @@ type Cacher struct {
...
@@ -161,9 +163,6 @@ type Cacher struct {
// Versioner is used to handle resource versions.
// Versioner is used to handle resource versions.
versioner
Versioner
versioner
Versioner
// keyFunc is used to get a key in the underyling storage for a given object.
keyFunc
func
(
runtime
.
Object
)
(
string
,
error
)
// triggerFunc is used for optimizing amount of watchers that needs to process
// triggerFunc is used for optimizing amount of watchers that needs to process
// an incoming event.
// an incoming event.
triggerFunc
TriggerPublisherFunc
triggerFunc
TriggerPublisherFunc
...
@@ -201,7 +200,6 @@ func NewCacherFromConfig(config CacherConfig) *Cacher {
...
@@ -201,7 +200,6 @@ func NewCacherFromConfig(config CacherConfig) *Cacher {
watchCache
:
watchCache
,
watchCache
:
watchCache
,
reflector
:
cache
.
NewReflector
(
listerWatcher
,
config
.
Type
,
watchCache
,
0
),
reflector
:
cache
.
NewReflector
(
listerWatcher
,
config
.
Type
,
watchCache
,
0
),
versioner
:
config
.
Versioner
,
versioner
:
config
.
Versioner
,
keyFunc
:
config
.
KeyFunc
,
triggerFunc
:
config
.
TriggerPublisherFunc
,
triggerFunc
:
config
.
TriggerPublisherFunc
,
watcherIdx
:
0
,
watcherIdx
:
0
,
watchers
:
indexedWatchers
{
watchers
:
indexedWatchers
{
...
@@ -328,7 +326,7 @@ func (c *Cacher) Watch(ctx context.Context, key string, resourceVersion string,
...
@@ -328,7 +326,7 @@ func (c *Cacher) Watch(ctx context.Context, key string, resourceVersion string,
c
.
Lock
()
c
.
Lock
()
defer
c
.
Unlock
()
defer
c
.
Unlock
()
forget
:=
forgetWatcher
(
c
,
c
.
watcherIdx
,
triggerValue
,
triggerSupported
)
forget
:=
forgetWatcher
(
c
,
c
.
watcherIdx
,
triggerValue
,
triggerSupported
)
watcher
:=
newCacheWatcher
(
watchRV
,
chanSize
,
initEvents
,
filterFunction
(
key
,
c
.
keyFunc
,
pred
),
forget
)
watcher
:=
newCacheWatcher
(
watchRV
,
chanSize
,
initEvents
,
filterFunction
(
key
,
pred
),
forget
)
c
.
watchers
.
addWatcher
(
watcher
,
c
.
watcherIdx
,
triggerValue
,
triggerSupported
)
c
.
watchers
.
addWatcher
(
watcher
,
c
.
watcherIdx
,
triggerValue
,
triggerSupported
)
c
.
watcherIdx
++
c
.
watcherIdx
++
...
@@ -382,7 +380,7 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
...
@@ -382,7 +380,7 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
if
err
!=
nil
||
listVal
.
Kind
()
!=
reflect
.
Slice
{
if
err
!=
nil
||
listVal
.
Kind
()
!=
reflect
.
Slice
{
return
fmt
.
Errorf
(
"need a pointer to slice, got %v"
,
listVal
.
Kind
())
return
fmt
.
Errorf
(
"need a pointer to slice, got %v"
,
listVal
.
Kind
())
}
}
filter
:=
filterFunction
(
key
,
c
.
keyFunc
,
pred
)
filter
:=
filterFunction
(
key
,
pred
)
objs
,
readResourceVersion
,
err
:=
c
.
watchCache
.
WaitUntilFreshAndList
(
listRV
,
trace
)
objs
,
readResourceVersion
,
err
:=
c
.
watchCache
.
WaitUntilFreshAndList
(
listRV
,
trace
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -394,7 +392,7 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
...
@@ -394,7 +392,7 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
if
!
ok
{
if
!
ok
{
return
fmt
.
Errorf
(
"non *storeElement returned from storage: %v"
,
obj
)
return
fmt
.
Errorf
(
"non *storeElement returned from storage: %v"
,
obj
)
}
}
if
filter
(
elem
.
Object
)
{
if
filter
(
elem
.
Key
,
elem
.
Object
)
{
listVal
.
Set
(
reflect
.
Append
(
listVal
,
reflect
.
ValueOf
(
elem
.
Object
)
.
Elem
()))
listVal
.
Set
(
reflect
.
Append
(
listVal
,
reflect
.
ValueOf
(
elem
.
Object
)
.
Elem
()))
}
}
}
}
...
@@ -524,14 +522,9 @@ func forgetWatcher(c *Cacher, index int, triggerValue string, triggerSupported b
...
@@ -524,14 +522,9 @@ func forgetWatcher(c *Cacher, index int, triggerValue string, triggerSupported b
}
}
}
}
func
filterFunction
(
key
string
,
keyFunc
func
(
runtime
.
Object
)
(
string
,
error
),
p
SelectionPredicate
)
Filter
Func
{
func
filterFunction
(
key
string
,
p
SelectionPredicate
)
filterObject
Func
{
f
:=
SimpleFilter
(
p
)
f
:=
SimpleFilter
(
p
)
filterFunc
:=
func
(
obj
runtime
.
Object
)
bool
{
filterFunc
:=
func
(
objKey
string
,
obj
runtime
.
Object
)
bool
{
objKey
,
err
:=
keyFunc
(
obj
)
if
err
!=
nil
{
glog
.
Errorf
(
"invalid object for filter. Obj: %v. Err: %v"
,
obj
,
err
)
return
false
}
if
!
hasPathPrefix
(
objKey
,
key
)
{
if
!
hasPathPrefix
(
objKey
,
key
)
{
return
false
return
false
}
}
...
@@ -626,12 +619,12 @@ type cacheWatcher struct {
...
@@ -626,12 +619,12 @@ type cacheWatcher struct {
sync
.
Mutex
sync
.
Mutex
input
chan
watchCacheEvent
input
chan
watchCacheEvent
result
chan
watch
.
Event
result
chan
watch
.
Event
filter
Filter
Func
filter
filterObject
Func
stopped
bool
stopped
bool
forget
func
(
bool
)
forget
func
(
bool
)
}
}
func
newCacheWatcher
(
resourceVersion
uint64
,
chanSize
int
,
initEvents
[]
watchCacheEvent
,
filter
Filter
Func
,
forget
func
(
bool
))
*
cacheWatcher
{
func
newCacheWatcher
(
resourceVersion
uint64
,
chanSize
int
,
initEvents
[]
watchCacheEvent
,
filter
filterObject
Func
,
forget
func
(
bool
))
*
cacheWatcher
{
watcher
:=
&
cacheWatcher
{
watcher
:=
&
cacheWatcher
{
input
:
make
(
chan
watchCacheEvent
,
chanSize
),
input
:
make
(
chan
watchCacheEvent
,
chanSize
),
result
:
make
(
chan
watch
.
Event
,
chanSize
),
result
:
make
(
chan
watch
.
Event
,
chanSize
),
...
@@ -709,10 +702,10 @@ func (c *cacheWatcher) add(event *watchCacheEvent) {
...
@@ -709,10 +702,10 @@ func (c *cacheWatcher) add(event *watchCacheEvent) {
// NOTE: sendWatchCacheEvent is assumed to not modify <event> !!!
// NOTE: sendWatchCacheEvent is assumed to not modify <event> !!!
func
(
c
*
cacheWatcher
)
sendWatchCacheEvent
(
event
*
watchCacheEvent
)
{
func
(
c
*
cacheWatcher
)
sendWatchCacheEvent
(
event
*
watchCacheEvent
)
{
curObjPasses
:=
event
.
Type
!=
watch
.
Deleted
&&
c
.
filter
(
event
.
Object
)
curObjPasses
:=
event
.
Type
!=
watch
.
Deleted
&&
c
.
filter
(
event
.
Key
,
event
.
Object
)
oldObjPasses
:=
false
oldObjPasses
:=
false
if
event
.
PrevObject
!=
nil
{
if
event
.
PrevObject
!=
nil
{
oldObjPasses
=
c
.
filter
(
event
.
PrevObject
)
oldObjPasses
=
c
.
filter
(
event
.
Key
,
event
.
PrevObject
)
}
}
if
!
curObjPasses
&&
!
oldObjPasses
{
if
!
curObjPasses
&&
!
oldObjPasses
{
// Watcher is not interested in that object.
// Watcher is not interested in that object.
...
...
pkg/storage/watch_cache.go
View file @
8040719d
...
@@ -46,6 +46,7 @@ type watchCacheEvent struct {
...
@@ -46,6 +46,7 @@ type watchCacheEvent struct {
Type
watch
.
EventType
Type
watch
.
EventType
Object
runtime
.
Object
Object
runtime
.
Object
PrevObject
runtime
.
Object
PrevObject
runtime
.
Object
Key
string
ResourceVersion
uint64
ResourceVersion
uint64
}
}
...
@@ -215,6 +216,7 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
...
@@ -215,6 +216,7 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
Type
:
event
.
Type
,
Type
:
event
.
Type
,
Object
:
event
.
Object
,
Object
:
event
.
Object
,
PrevObject
:
prevObject
,
PrevObject
:
prevObject
,
Key
:
key
,
ResourceVersion
:
resourceVersion
,
ResourceVersion
:
resourceVersion
,
}
}
if
w
.
onEvent
!=
nil
{
if
w
.
onEvent
!=
nil
{
...
@@ -376,6 +378,7 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]wa
...
@@ -376,6 +378,7 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]wa
result
[
i
]
=
watchCacheEvent
{
result
[
i
]
=
watchCacheEvent
{
Type
:
watch
.
Added
,
Type
:
watch
.
Added
,
Object
:
elem
.
Object
,
Object
:
elem
.
Object
,
Key
:
elem
.
Key
,
ResourceVersion
:
w
.
resourceVersion
,
ResourceVersion
:
w
.
resourceVersion
,
}
}
}
}
...
...
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