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
c4f97fa2
Unverified
Commit
c4f97fa2
authored
Apr 09, 2019
by
Kubernetes Prow Robot
Committed by
GitHub
Apr 09, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #76330 from wojtek-t/simplify_watchcache
Slightly simplify watchcache
parents
98104e44
c93f8898
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
18 deletions
+10
-18
watch_cache.go
...ng/src/k8s.io/apiserver/pkg/storage/cacher/watch_cache.go
+10
-18
No files found.
staging/src/k8s.io/apiserver/pkg/storage/cacher/watch_cache.go
View file @
c4f97fa2
...
@@ -77,14 +77,6 @@ func storeElementKey(obj interface{}) (string, error) {
...
@@ -77,14 +77,6 @@ func storeElementKey(obj interface{}) (string, error) {
return
elem
.
Key
,
nil
return
elem
.
Key
,
nil
}
}
// watchCacheElement is a single "watch event" stored in a cache.
// It contains the resource version of the object and the object
// itself.
type
watchCacheElement
struct
{
resourceVersion
uint64
watchCacheEvent
*
watchCacheEvent
}
// watchCache implements a Store interface.
// watchCache implements a Store interface.
// However, it depends on the elements implementing runtime.Object interface.
// However, it depends on the elements implementing runtime.Object interface.
//
//
...
@@ -111,7 +103,7 @@ type watchCache struct {
...
@@ -111,7 +103,7 @@ type watchCache struct {
// by endIndex (if cache is full it will be startIndex + capacity).
// by endIndex (if cache is full it will be startIndex + capacity).
// Both startIndex and endIndex can be greater than buffer capacity -
// Both startIndex and endIndex can be greater than buffer capacity -
// you should always apply modulo capacity to get an index in cache array.
// you should always apply modulo capacity to get an index in cache array.
cache
[]
watchCacheElem
ent
cache
[]
*
watchCacheEv
ent
startIndex
int
startIndex
int
endIndex
int
endIndex
int
...
@@ -150,7 +142,7 @@ func newWatchCache(
...
@@ -150,7 +142,7 @@ func newWatchCache(
capacity
:
capacity
,
capacity
:
capacity
,
keyFunc
:
keyFunc
,
keyFunc
:
keyFunc
,
getAttrsFunc
:
getAttrsFunc
,
getAttrsFunc
:
getAttrsFunc
,
cache
:
make
([]
watchCacheElem
ent
,
capacity
),
cache
:
make
([]
*
watchCacheEv
ent
,
capacity
),
startIndex
:
0
,
startIndex
:
0
,
endIndex
:
0
,
endIndex
:
0
,
store
:
cache
.
NewStore
(
storeElementKey
),
store
:
cache
.
NewStore
(
storeElementKey
),
...
@@ -247,23 +239,23 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
...
@@ -247,23 +239,23 @@ func (w *watchCache) processEvent(event watch.Event, resourceVersion uint64, upd
watchCacheEvent
.
PrevObjLabels
=
previousElem
.
Labels
watchCacheEvent
.
PrevObjLabels
=
previousElem
.
Labels
watchCacheEvent
.
PrevObjFields
=
previousElem
.
Fields
watchCacheEvent
.
PrevObjFields
=
previousElem
.
Fields
}
}
w
.
updateCache
(
watchCacheEvent
)
w
.
resourceVersion
=
resourceVersion
if
w
.
onEvent
!=
nil
{
if
w
.
onEvent
!=
nil
{
w
.
onEvent
(
watchCacheEvent
)
w
.
onEvent
(
watchCacheEvent
)
}
}
w
.
updateCache
(
resourceVersion
,
watchCacheEvent
)
w
.
resourceVersion
=
resourceVersion
w
.
cond
.
Broadcast
()
w
.
cond
.
Broadcast
()
return
updateFunc
(
elem
)
return
updateFunc
(
elem
)
}
}
// Assumes that lock is already held for write.
// Assumes that lock is already held for write.
func
(
w
*
watchCache
)
updateCache
(
resourceVersion
uint64
,
event
*
watchCacheEvent
)
{
func
(
w
*
watchCache
)
updateCache
(
event
*
watchCacheEvent
)
{
if
w
.
endIndex
==
w
.
startIndex
+
w
.
capacity
{
if
w
.
endIndex
==
w
.
startIndex
+
w
.
capacity
{
// Cache is full - remove the oldest element.
// Cache is full - remove the oldest element.
w
.
startIndex
++
w
.
startIndex
++
}
}
w
.
cache
[
w
.
endIndex
%
w
.
capacity
]
=
watchCacheElement
{
resourceVersion
,
event
}
w
.
cache
[
w
.
endIndex
%
w
.
capacity
]
=
event
w
.
endIndex
++
w
.
endIndex
++
}
}
...
@@ -416,7 +408,7 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]*w
...
@@ -416,7 +408,7 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]*w
case
size
>=
w
.
capacity
:
case
size
>=
w
.
capacity
:
// Once the watch event buffer is full, the oldest watch event we can deliver
// Once the watch event buffer is full, the oldest watch event we can deliver
// is the first one in the buffer.
// is the first one in the buffer.
oldest
=
w
.
cache
[
w
.
startIndex
%
w
.
capacity
]
.
r
esourceVersion
oldest
=
w
.
cache
[
w
.
startIndex
%
w
.
capacity
]
.
R
esourceVersion
case
w
.
listResourceVersion
>
0
:
case
w
.
listResourceVersion
>
0
:
// If the watch event buffer isn't full, the oldest watch event we can deliver
// If the watch event buffer isn't full, the oldest watch event we can deliver
// is one greater than the resource version of the last full list.
// is one greater than the resource version of the last full list.
...
@@ -426,7 +418,7 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]*w
...
@@ -426,7 +418,7 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]*w
// in the buffer.
// in the buffer.
// This should only happen in unit tests that populate the buffer without
// This should only happen in unit tests that populate the buffer without
// performing list/replace operations.
// performing list/replace operations.
oldest
=
w
.
cache
[
w
.
startIndex
%
w
.
capacity
]
.
r
esourceVersion
oldest
=
w
.
cache
[
w
.
startIndex
%
w
.
capacity
]
.
R
esourceVersion
default
:
default
:
return
nil
,
fmt
.
Errorf
(
"watch cache isn't correctly initialized"
)
return
nil
,
fmt
.
Errorf
(
"watch cache isn't correctly initialized"
)
}
}
...
@@ -466,12 +458,12 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]*w
...
@@ -466,12 +458,12 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]*w
// Binary search the smallest index at which resourceVersion is greater than the given one.
// Binary search the smallest index at which resourceVersion is greater than the given one.
f
:=
func
(
i
int
)
bool
{
f
:=
func
(
i
int
)
bool
{
return
w
.
cache
[(
w
.
startIndex
+
i
)
%
w
.
capacity
]
.
r
esourceVersion
>
resourceVersion
return
w
.
cache
[(
w
.
startIndex
+
i
)
%
w
.
capacity
]
.
R
esourceVersion
>
resourceVersion
}
}
first
:=
sort
.
Search
(
size
,
f
)
first
:=
sort
.
Search
(
size
,
f
)
result
:=
make
([]
*
watchCacheEvent
,
size
-
first
)
result
:=
make
([]
*
watchCacheEvent
,
size
-
first
)
for
i
:=
0
;
i
<
size
-
first
;
i
++
{
for
i
:=
0
;
i
<
size
-
first
;
i
++
{
result
[
i
]
=
w
.
cache
[(
w
.
startIndex
+
first
+
i
)
%
w
.
capacity
]
.
watchCacheEvent
result
[
i
]
=
w
.
cache
[(
w
.
startIndex
+
first
+
i
)
%
w
.
capacity
]
}
}
return
result
,
nil
return
result
,
nil
}
}
...
...
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