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
5e53e281
Commit
5e53e281
authored
Dec 02, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #17545 from yujuhong/no_auto_updates
Auto commit by PR queue bot
parents
60925e67
dc42d25f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
3 additions
and
76 deletions
+3
-76
runtime_cache.go
pkg/kubelet/container/runtime_cache.go
+3
-52
runtime_cache_test.go
pkg/kubelet/container/runtime_cache_test.go
+0
-24
No files found.
pkg/kubelet/container/runtime_cache.go
View file @
5e53e281
...
...
@@ -23,8 +23,7 @@ import (
var
(
// TODO(yifan): Maybe set the them as parameters for NewCache().
defaultCachePeriod
=
time
.
Second
*
2
defaultUpdateInterval
=
time
.
Millisecond
*
100
defaultCachePeriod
=
time
.
Second
*
2
)
type
RuntimeCache
interface
{
...
...
@@ -39,8 +38,7 @@ type podsGetter interface {
// NewRuntimeCache creates a container runtime cache.
func
NewRuntimeCache
(
getter
podsGetter
)
(
RuntimeCache
,
error
)
{
return
&
runtimeCache
{
getter
:
getter
,
updating
:
false
,
getter
:
getter
,
},
nil
}
...
...
@@ -48,13 +46,6 @@ func NewRuntimeCache(getter podsGetter) (RuntimeCache, error) {
// before updating the pods, so the timestamp is at most as new as the pods
// (and can be slightly older). The timestamp always moves forward. Callers are
// expected not to modify the pods returned from GetPods.
// The pod updates can be triggered by a request (e.g., GetPods or
// ForceUpdateIfOlder) if the cached pods are considered stale. These requests
// will be blocked until the cache update is completed. To reduce the cache miss
// penalty, upon a miss, runtimeCache would start a separate goroutine
// (updatingThread) if one does not exist, to periodically updates the cache.
// updatingThread would stop after a period of inactivity (no incoming requests)
// to conserve resources.
type
runtimeCache
struct
{
sync
.
Mutex
// The underlying container runtime used to update the cache.
...
...
@@ -63,15 +54,10 @@ type runtimeCache struct {
cacheTime
time
.
Time
// The content of the cache.
pods
[]
*
Pod
// Whether the background thread updating the cache is running.
updating
bool
// Time when the background thread should be stopped.
updatingThreadStopTime
time
.
Time
}
// GetPods returns the cached pods if they are not outdated; otherwise, it
// retrieves the latest pods and return them.
// If the cache updating loop has stopped, this function will restart it.
func
(
r
*
runtimeCache
)
GetPods
()
([]
*
Pod
,
error
)
{
r
.
Lock
()
defer
r
.
Unlock
()
...
...
@@ -80,12 +66,6 @@ func (r *runtimeCache) GetPods() ([]*Pod, error) {
return
nil
,
err
}
}
// Stop refreshing thread if there were no requests within the default cache period
r
.
updatingThreadStopTime
=
time
.
Now
()
.
Add
(
defaultCachePeriod
)
if
!
r
.
updating
{
r
.
updating
=
true
go
r
.
startUpdatingCache
()
}
return
r
.
pods
,
nil
}
...
...
@@ -103,7 +83,7 @@ func (r *runtimeCache) updateCache() error {
if
err
!=
nil
{
return
err
}
r
.
writePodsIfNewer
(
pods
,
timestamp
)
r
.
pods
,
r
.
cacheTime
=
pods
,
timestamp
return
nil
}
...
...
@@ -114,32 +94,3 @@ func (r *runtimeCache) getPodsWithTimestamp() ([]*Pod, time.Time, error) {
pods
,
err
:=
r
.
getter
.
GetPods
(
false
)
return
pods
,
timestamp
,
err
}
// writePodsIfNewer writes the pods and timestamp if they are newer than the
// cached ones.
func
(
r
*
runtimeCache
)
writePodsIfNewer
(
pods
[]
*
Pod
,
timestamp
time
.
Time
)
{
if
timestamp
.
After
(
r
.
cacheTime
)
{
r
.
pods
,
r
.
cacheTime
=
pods
,
timestamp
}
}
// startUpdateingCache continues to invoke GetPods to get the newest result until
// there are no requests within the default cache period.
func
(
r
*
runtimeCache
)
startUpdatingCache
()
{
run
:=
true
for
run
{
time
.
Sleep
(
defaultUpdateInterval
)
pods
,
timestamp
,
err
:=
r
.
getPodsWithTimestamp
()
if
err
!=
nil
{
continue
}
r
.
Lock
()
if
time
.
Now
()
.
After
(
r
.
updatingThreadStopTime
)
{
r
.
updating
=
false
run
=
false
}
r
.
writePodsIfNewer
(
pods
,
timestamp
)
r
.
Unlock
()
}
}
pkg/kubelet/container/runtime_cache_test.go
View file @
5e53e281
...
...
@@ -86,27 +86,3 @@ func TestForceUpdateIfOlder(t *testing.T) {
t
.
Errorf
(
"expected %#v, got %#v"
,
newpods
,
actual
)
}
}
func
TestUpdatePodsOnlyIfNewer
(
t
*
testing
.
T
)
{
runtime
:=
&
FakeRuntime
{}
cache
:=
newTestRuntimeCache
(
runtime
)
// Cache new pods with a future timestamp.
newpods
:=
[]
*
Pod
{{
ID
:
"1111"
},
{
ID
:
"2222"
},
{
ID
:
"3333"
}}
cache
.
Lock
()
cache
.
pods
=
newpods
cache
.
cacheTime
=
time
.
Now
()
.
Add
(
20
*
time
.
Minute
)
cache
.
Unlock
()
// Instruct runime to return a list of old pods.
oldpods
:=
[]
*
Pod
{{
ID
:
"1111"
}}
runtime
.
PodList
=
oldpods
// Try to update the cache; the attempt should not succeed because the
// cache timestamp is newer than the current time.
cache
.
updateCacheWithLock
()
actual
:=
cache
.
getCachedPods
()
if
!
reflect
.
DeepEqual
(
newpods
,
actual
)
{
t
.
Errorf
(
"expected %#v, got %#v"
,
newpods
,
actual
)
}
}
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