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
f10b0205
Commit
f10b0205
authored
Oct 18, 2016
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Store keys in watchCache store
parent
9895f337
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
91 additions
and
22 deletions
+91
-22
cacher.go
pkg/storage/cacher.go
+5
-5
watch_cache.go
pkg/storage/watch_cache.go
+78
-12
watch_cache_test.go
pkg/storage/watch_cache_test.go
+8
-5
No files found.
pkg/storage/cacher.go
View file @
f10b0205
...
@@ -183,7 +183,7 @@ type Cacher struct {
...
@@ -183,7 +183,7 @@ type Cacher struct {
// internal cache and updating its cache in the background based on the given
// internal cache and updating its cache in the background based on the given
// configuration.
// configuration.
func
NewCacherFromConfig
(
config
CacherConfig
)
*
Cacher
{
func
NewCacherFromConfig
(
config
CacherConfig
)
*
Cacher
{
watchCache
:=
newWatchCache
(
config
.
CacheCapacity
)
watchCache
:=
newWatchCache
(
config
.
CacheCapacity
,
config
.
KeyFunc
)
listerWatcher
:=
newCacherListerWatcher
(
config
.
Storage
,
config
.
ResourcePrefix
,
config
.
NewListFunc
)
listerWatcher
:=
newCacherListerWatcher
(
config
.
Storage
,
config
.
ResourcePrefix
,
config
.
NewListFunc
)
// Give this error when it is constructed rather than when you get the
// Give this error when it is constructed rather than when you get the
...
@@ -390,12 +390,12 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
...
@@ -390,12 +390,12 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
}
}
trace
.
Step
(
fmt
.
Sprintf
(
"Listed %d items from cache"
,
len
(
objs
)))
trace
.
Step
(
fmt
.
Sprintf
(
"Listed %d items from cache"
,
len
(
objs
)))
for
_
,
obj
:=
range
objs
{
for
_
,
obj
:=
range
objs
{
object
,
ok
:=
obj
.
(
runtime
.
Objec
t
)
elem
,
ok
:=
obj
.
(
*
storeElemen
t
)
if
!
ok
{
if
!
ok
{
return
fmt
.
Errorf
(
"non
runtime.Objec
t returned from storage: %v"
,
obj
)
return
fmt
.
Errorf
(
"non
*storeElemen
t returned from storage: %v"
,
obj
)
}
}
if
filter
(
o
bject
)
{
if
filter
(
elem
.
O
bject
)
{
listVal
.
Set
(
reflect
.
Append
(
listVal
,
reflect
.
ValueOf
(
o
bject
)
.
Elem
()))
listVal
.
Set
(
reflect
.
Append
(
listVal
,
reflect
.
ValueOf
(
elem
.
O
bject
)
.
Elem
()))
}
}
}
}
trace
.
Step
(
fmt
.
Sprintf
(
"Filtered %d items"
,
listVal
.
Len
()))
trace
.
Step
(
fmt
.
Sprintf
(
"Filtered %d items"
,
listVal
.
Len
()))
...
...
pkg/storage/watch_cache.go
View file @
f10b0205
...
@@ -49,6 +49,23 @@ type watchCacheEvent struct {
...
@@ -49,6 +49,23 @@ type watchCacheEvent struct {
ResourceVersion
uint64
ResourceVersion
uint64
}
}
// Computing a key of an object is generally non-trivial (it performs
// e.g. validation underneath). To avoid computing it multiple times
// (to serve the event in different List/Watch requests), in the
// underlying store we are keeping pair (key, object).
type
storeElement
struct
{
Key
string
Object
runtime
.
Object
}
func
storeElementKey
(
obj
interface
{})
(
string
,
error
)
{
elem
,
ok
:=
obj
.
(
*
storeElement
)
if
!
ok
{
return
""
,
fmt
.
Errorf
(
"not a storeElement: %v"
,
obj
)
}
return
elem
.
Key
,
nil
}
// watchCacheElement is a single "watch event" stored in a cache.
// watchCacheElement is a single "watch event" stored in a cache.
// It contains the resource version of the object and the object
// It contains the resource version of the object and the object
// itself.
// itself.
...
@@ -72,6 +89,9 @@ type watchCache struct {
...
@@ -72,6 +89,9 @@ type watchCache struct {
// Maximum size of history window.
// Maximum size of history window.
capacity
int
capacity
int
// keyFunc is used to get a key in the underlying storage for a given object.
keyFunc
func
(
runtime
.
Object
)
(
string
,
error
)
// cache is used a cyclic buffer - its first element (with the smallest
// cache is used a cyclic buffer - its first element (with the smallest
// resourceVersion) is defined by startIndex, its last element is defined
// resourceVersion) is defined by startIndex, its last element is defined
// by endIndex (if cache is full it will be startIndex + capacity).
// by endIndex (if cache is full it will be startIndex + capacity).
...
@@ -100,13 +120,14 @@ type watchCache struct {
...
@@ -100,13 +120,14 @@ type watchCache struct {
clock
clock
.
Clock
clock
clock
.
Clock
}
}
func
newWatchCache
(
capacity
int
)
*
watchCache
{
func
newWatchCache
(
capacity
int
,
keyFunc
func
(
runtime
.
Object
)
(
string
,
error
)
)
*
watchCache
{
wc
:=
&
watchCache
{
wc
:=
&
watchCache
{
capacity
:
capacity
,
capacity
:
capacity
,
keyFunc
:
keyFunc
,
cache
:
make
([]
watchCacheElement
,
capacity
),
cache
:
make
([]
watchCacheElement
,
capacity
),
startIndex
:
0
,
startIndex
:
0
,
endIndex
:
0
,
endIndex
:
0
,
store
:
cache
.
NewStore
(
cache
.
MetaNamespaceKeyFunc
),
store
:
cache
.
NewStore
(
storeElementKey
),
resourceVersion
:
0
,
resourceVersion
:
0
,
clock
:
clock
.
RealClock
{},
clock
:
clock
.
RealClock
{},
}
}
...
@@ -114,6 +135,7 @@ func newWatchCache(capacity int) *watchCache {
...
@@ -114,6 +135,7 @@ func newWatchCache(capacity int) *watchCache {
return
wc
return
wc
}
}
// Add takes runtime.Object as an argument.
func
(
w
*
watchCache
)
Add
(
obj
interface
{})
error
{
func
(
w
*
watchCache
)
Add
(
obj
interface
{})
error
{
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -121,10 +143,11 @@ func (w *watchCache) Add(obj interface{}) error {
...
@@ -121,10 +143,11 @@ func (w *watchCache) Add(obj interface{}) error {
}
}
event
:=
watch
.
Event
{
Type
:
watch
.
Added
,
Object
:
object
}
event
:=
watch
.
Event
{
Type
:
watch
.
Added
,
Object
:
object
}
f
:=
func
(
obj
runtime
.
Object
)
error
{
return
w
.
store
.
Add
(
obj
)
}
f
:=
func
(
elem
*
storeElement
)
error
{
return
w
.
store
.
Add
(
elem
)
}
return
w
.
processEvent
(
event
,
resourceVersion
,
f
)
return
w
.
processEvent
(
event
,
resourceVersion
,
f
)
}
}
// Update takes runtime.Object as an argument.
func
(
w
*
watchCache
)
Update
(
obj
interface
{})
error
{
func
(
w
*
watchCache
)
Update
(
obj
interface
{})
error
{
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -132,10 +155,11 @@ func (w *watchCache) Update(obj interface{}) error {
...
@@ -132,10 +155,11 @@ func (w *watchCache) Update(obj interface{}) error {
}
}
event
:=
watch
.
Event
{
Type
:
watch
.
Modified
,
Object
:
object
}
event
:=
watch
.
Event
{
Type
:
watch
.
Modified
,
Object
:
object
}
f
:=
func
(
obj
runtime
.
Object
)
error
{
return
w
.
store
.
Update
(
obj
)
}
f
:=
func
(
elem
*
storeElement
)
error
{
return
w
.
store
.
Update
(
elem
)
}
return
w
.
processEvent
(
event
,
resourceVersion
,
f
)
return
w
.
processEvent
(
event
,
resourceVersion
,
f
)
}
}
// Delete takes runtime.Object as an argument.
func
(
w
*
watchCache
)
Delete
(
obj
interface
{})
error
{
func
(
w
*
watchCache
)
Delete
(
obj
interface
{})
error
{
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -143,7 +167,7 @@ func (w *watchCache) Delete(obj interface{}) error {
...
@@ -143,7 +167,7 @@ func (w *watchCache) Delete(obj interface{}) error {
}
}
event
:=
watch
.
Event
{
Type
:
watch
.
Deleted
,
Object
:
object
}
event
:=
watch
.
Event
{
Type
:
watch
.
Deleted
,
Object
:
object
}
f
:=
func
(
obj
runtime
.
Object
)
error
{
return
w
.
store
.
Delete
(
obj
)
}
f
:=
func
(
elem
*
storeElement
)
error
{
return
w
.
store
.
Delete
(
elem
)
}
return
w
.
processEvent
(
event
,
resourceVersion
,
f
)
return
w
.
processEvent
(
event
,
resourceVersion
,
f
)
}
}
...
@@ -170,16 +194,22 @@ func parseResourceVersion(resourceVersion string) (uint64, error) {
...
@@ -170,16 +194,22 @@ func parseResourceVersion(resourceVersion string) (uint64, error) {
return
strconv
.
ParseUint
(
resourceVersion
,
10
,
64
)
return
strconv
.
ParseUint
(
resourceVersion
,
10
,
64
)
}
}
func
(
w
*
watchCache
)
processEvent
(
event
watch
.
Event
,
resourceVersion
uint64
,
updateFunc
func
(
runtime
.
Object
)
error
)
error
{
func
(
w
*
watchCache
)
processEvent
(
event
watch
.
Event
,
resourceVersion
uint64
,
updateFunc
func
(
*
storeElement
)
error
)
error
{
key
,
err
:=
w
.
keyFunc
(
event
.
Object
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"couldn't compute key: %v"
,
err
)
}
elem
:=
&
storeElement
{
Key
:
key
,
Object
:
event
.
Object
}
w
.
Lock
()
w
.
Lock
()
defer
w
.
Unlock
()
defer
w
.
Unlock
()
previous
,
exists
,
err
:=
w
.
store
.
Get
(
e
vent
.
Object
)
previous
,
exists
,
err
:=
w
.
store
.
Get
(
e
lem
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
var
prevObject
runtime
.
Object
var
prevObject
runtime
.
Object
if
exists
{
if
exists
{
prevObject
=
previous
.
(
runtime
.
Object
)
prevObject
=
previous
.
(
*
storeElement
)
.
Object
}
}
watchCacheEvent
:=
watchCacheEvent
{
watchCacheEvent
:=
watchCacheEvent
{
Type
:
event
.
Type
,
Type
:
event
.
Type
,
...
@@ -193,7 +223,7 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
...
@@ -193,7 +223,7 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
w
.
updateCache
(
resourceVersion
,
&
watchCacheEvent
)
w
.
updateCache
(
resourceVersion
,
&
watchCacheEvent
)
w
.
resourceVersion
=
resourceVersion
w
.
resourceVersion
=
resourceVersion
w
.
cond
.
Broadcast
()
w
.
cond
.
Broadcast
()
return
updateFunc
(
e
vent
.
Object
)
return
updateFunc
(
e
lem
)
}
}
// Assumes that lock is already held for write.
// Assumes that lock is already held for write.
...
@@ -206,12 +236,14 @@ func (w *watchCache) updateCache(resourceVersion uint64, event *watchCacheEvent)
...
@@ -206,12 +236,14 @@ func (w *watchCache) updateCache(resourceVersion uint64, event *watchCacheEvent)
w
.
endIndex
++
w
.
endIndex
++
}
}
// List returns list of pointers to <storeElement> objects.
func
(
w
*
watchCache
)
List
()
[]
interface
{}
{
func
(
w
*
watchCache
)
List
()
[]
interface
{}
{
w
.
RLock
()
w
.
RLock
()
defer
w
.
RUnlock
()
defer
w
.
RUnlock
()
return
w
.
store
.
List
()
return
w
.
store
.
List
()
}
}
// WaitUntilFreshAndList returns list of pointers to <storeElement> objects.
func
(
w
*
watchCache
)
WaitUntilFreshAndList
(
resourceVersion
uint64
,
trace
*
util
.
Trace
)
([]
interface
{},
uint64
,
error
)
{
func
(
w
*
watchCache
)
WaitUntilFreshAndList
(
resourceVersion
uint64
,
trace
*
util
.
Trace
)
([]
interface
{},
uint64
,
error
)
{
startTime
:=
w
.
clock
.
Now
()
startTime
:=
w
.
clock
.
Now
()
go
func
()
{
go
func
()
{
...
@@ -249,30 +281,56 @@ func (w *watchCache) ListKeys() []string {
...
@@ -249,30 +281,56 @@ func (w *watchCache) ListKeys() []string {
return
w
.
store
.
ListKeys
()
return
w
.
store
.
ListKeys
()
}
}
// Get takes runtime.Object as a parameter. However, it returns
// pointer to <storeElement>.
func
(
w
*
watchCache
)
Get
(
obj
interface
{})
(
interface
{},
bool
,
error
)
{
func
(
w
*
watchCache
)
Get
(
obj
interface
{})
(
interface
{},
bool
,
error
)
{
object
,
ok
:=
obj
.
(
runtime
.
Object
)
if
!
ok
{
return
nil
,
false
,
fmt
.
Errorf
(
"obj does not implement runtime.Object interface: %v"
,
obj
)
}
key
,
err
:=
w
.
keyFunc
(
object
)
if
err
!=
nil
{
return
nil
,
false
,
fmt
.
Errorf
(
"couldn't compute key: %v"
,
err
)
}
w
.
RLock
()
w
.
RLock
()
defer
w
.
RUnlock
()
defer
w
.
RUnlock
()
return
w
.
store
.
Get
(
obj
)
return
w
.
store
.
Get
(
&
storeElement
{
Key
:
key
,
Object
:
object
}
)
}
}
// GetByKey returns pointer to <storeElement>.
func
(
w
*
watchCache
)
GetByKey
(
key
string
)
(
interface
{},
bool
,
error
)
{
func
(
w
*
watchCache
)
GetByKey
(
key
string
)
(
interface
{},
bool
,
error
)
{
w
.
RLock
()
w
.
RLock
()
defer
w
.
RUnlock
()
defer
w
.
RUnlock
()
return
w
.
store
.
GetByKey
(
key
)
return
w
.
store
.
GetByKey
(
key
)
}
}
// Replace takes slice of runtime.Object as a paramater.
func
(
w
*
watchCache
)
Replace
(
objs
[]
interface
{},
resourceVersion
string
)
error
{
func
(
w
*
watchCache
)
Replace
(
objs
[]
interface
{},
resourceVersion
string
)
error
{
version
,
err
:=
parseResourceVersion
(
resourceVersion
)
version
,
err
:=
parseResourceVersion
(
resourceVersion
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
toReplace
:=
make
([]
interface
{},
0
,
len
(
objs
))
for
_
,
obj
:=
range
objs
{
object
,
ok
:=
obj
.
(
runtime
.
Object
)
if
!
ok
{
return
fmt
.
Errorf
(
"didn't get runtime.Object for replace: %#v"
,
obj
)
}
key
,
err
:=
w
.
keyFunc
(
object
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"couldn't compute key: %v"
,
err
)
}
toReplace
=
append
(
toReplace
,
&
storeElement
{
Key
:
key
,
Object
:
object
})
}
w
.
Lock
()
w
.
Lock
()
defer
w
.
Unlock
()
defer
w
.
Unlock
()
w
.
startIndex
=
0
w
.
startIndex
=
0
w
.
endIndex
=
0
w
.
endIndex
=
0
if
err
:=
w
.
store
.
Replace
(
objs
,
resourceVersion
);
err
!=
nil
{
if
err
:=
w
.
store
.
Replace
(
toReplace
,
resourceVersion
);
err
!=
nil
{
return
err
return
err
}
}
w
.
resourceVersion
=
version
w
.
resourceVersion
=
version
...
@@ -311,7 +369,15 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]wa
...
@@ -311,7 +369,15 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]wa
allItems
:=
w
.
store
.
List
()
allItems
:=
w
.
store
.
List
()
result
:=
make
([]
watchCacheEvent
,
len
(
allItems
))
result
:=
make
([]
watchCacheEvent
,
len
(
allItems
))
for
i
,
item
:=
range
allItems
{
for
i
,
item
:=
range
allItems
{
result
[
i
]
=
watchCacheEvent
{
Type
:
watch
.
Added
,
Object
:
item
.
(
runtime
.
Object
)}
elem
,
ok
:=
item
.
(
*
storeElement
)
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"not a storeElement: %v"
,
elem
)
}
result
[
i
]
=
watchCacheEvent
{
Type
:
watch
.
Added
,
Object
:
elem
.
Object
,
ResourceVersion
:
w
.
resourceVersion
,
}
}
}
return
result
,
nil
return
result
,
nil
}
}
...
...
pkg/storage/watch_cache_test.go
View file @
f10b0205
...
@@ -44,7 +44,10 @@ func makeTestPod(name string, resourceVersion uint64) *api.Pod {
...
@@ -44,7 +44,10 @@ func makeTestPod(name string, resourceVersion uint64) *api.Pod {
// newTestWatchCache just adds a fake clock.
// newTestWatchCache just adds a fake clock.
func
newTestWatchCache
(
capacity
int
)
*
watchCache
{
func
newTestWatchCache
(
capacity
int
)
*
watchCache
{
wc
:=
newWatchCache
(
capacity
)
keyFunc
:=
func
(
obj
runtime
.
Object
)
(
string
,
error
)
{
return
NamespaceKeyFunc
(
"prefix"
,
obj
)
}
wc
:=
newWatchCache
(
capacity
,
keyFunc
)
wc
.
clock
=
clock
.
NewFakeClock
(
time
.
Now
())
wc
.
clock
=
clock
.
NewFakeClock
(
time
.
Now
())
return
wc
return
wc
}
}
...
@@ -60,7 +63,7 @@ func TestWatchCacheBasic(t *testing.T) {
...
@@ -60,7 +63,7 @@ func TestWatchCacheBasic(t *testing.T) {
if
item
,
ok
,
_
:=
store
.
Get
(
pod1
);
!
ok
{
if
item
,
ok
,
_
:=
store
.
Get
(
pod1
);
!
ok
{
t
.
Errorf
(
"didn't find pod"
)
t
.
Errorf
(
"didn't find pod"
)
}
else
{
}
else
{
if
!
api
.
Semantic
.
DeepEqual
(
pod1
,
item
)
{
if
!
api
.
Semantic
.
DeepEqual
(
&
storeElement
{
Key
:
"prefix/ns/pod"
,
Object
:
pod1
}
,
item
)
{
t
.
Errorf
(
"expected %v, got %v"
,
pod1
,
item
)
t
.
Errorf
(
"expected %v, got %v"
,
pod1
,
item
)
}
}
}
}
...
@@ -71,7 +74,7 @@ func TestWatchCacheBasic(t *testing.T) {
...
@@ -71,7 +74,7 @@ func TestWatchCacheBasic(t *testing.T) {
if
item
,
ok
,
_
:=
store
.
Get
(
pod2
);
!
ok
{
if
item
,
ok
,
_
:=
store
.
Get
(
pod2
);
!
ok
{
t
.
Errorf
(
"didn't find pod"
)
t
.
Errorf
(
"didn't find pod"
)
}
else
{
}
else
{
if
!
api
.
Semantic
.
DeepEqual
(
pod2
,
item
)
{
if
!
api
.
Semantic
.
DeepEqual
(
&
storeElement
{
Key
:
"prefix/ns/pod"
,
Object
:
pod2
}
,
item
)
{
t
.
Errorf
(
"expected %v, got %v"
,
pod1
,
item
)
t
.
Errorf
(
"expected %v, got %v"
,
pod1
,
item
)
}
}
}
}
...
@@ -90,7 +93,7 @@ func TestWatchCacheBasic(t *testing.T) {
...
@@ -90,7 +93,7 @@ func TestWatchCacheBasic(t *testing.T) {
{
{
podNames
:=
sets
.
String
{}
podNames
:=
sets
.
String
{}
for
_
,
item
:=
range
store
.
List
()
{
for
_
,
item
:=
range
store
.
List
()
{
podNames
.
Insert
(
item
.
(
*
api
.
Pod
)
.
ObjectMeta
.
Name
)
podNames
.
Insert
(
item
.
(
*
storeElement
)
.
Object
.
(
*
api
.
Pod
)
.
ObjectMeta
.
Name
)
}
}
if
!
podNames
.
HasAll
(
"pod1"
,
"pod2"
,
"pod3"
)
{
if
!
podNames
.
HasAll
(
"pod1"
,
"pod2"
,
"pod3"
)
{
t
.
Errorf
(
"missing pods, found %v"
,
podNames
)
t
.
Errorf
(
"missing pods, found %v"
,
podNames
)
...
@@ -108,7 +111,7 @@ func TestWatchCacheBasic(t *testing.T) {
...
@@ -108,7 +111,7 @@ func TestWatchCacheBasic(t *testing.T) {
{
{
podNames
:=
sets
.
String
{}
podNames
:=
sets
.
String
{}
for
_
,
item
:=
range
store
.
List
()
{
for
_
,
item
:=
range
store
.
List
()
{
podNames
.
Insert
(
item
.
(
*
api
.
Pod
)
.
ObjectMeta
.
Name
)
podNames
.
Insert
(
item
.
(
*
storeElement
)
.
Object
.
(
*
api
.
Pod
)
.
ObjectMeta
.
Name
)
}
}
if
!
podNames
.
HasAll
(
"pod4"
,
"pod5"
)
{
if
!
podNames
.
HasAll
(
"pod4"
,
"pod5"
)
{
t
.
Errorf
(
"missing pods, found %v"
,
podNames
)
t
.
Errorf
(
"missing pods, found %v"
,
podNames
)
...
...
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