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
12eaf67a
Commit
12eaf67a
authored
Sep 14, 2015
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix resourcVersion=0 in cacher
parent
c9570e34
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
12 deletions
+59
-12
cacher_test.go
pkg/storage/cacher_test.go
+45
-12
watch_cache.go
pkg/storage/watch_cache.go
+14
-0
No files found.
pkg/storage/cacher_test.go
View file @
12eaf67a
...
@@ -212,8 +212,8 @@ func TestWatch(t *testing.T) {
...
@@ -212,8 +212,8 @@ func TestWatch(t *testing.T) {
Action
:
"create"
,
Action
:
"create"
,
Node
:
&
etcd
.
Node
{
Node
:
&
etcd
.
Node
{
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
CreatedIndex
:
1
,
CreatedIndex
:
2
,
ModifiedIndex
:
1
,
ModifiedIndex
:
2
,
},
},
},
},
event
:
watch
.
Added
,
event
:
watch
.
Added
,
...
@@ -225,8 +225,8 @@ func TestWatch(t *testing.T) {
...
@@ -225,8 +225,8 @@ func TestWatch(t *testing.T) {
Action
:
"create"
,
Action
:
"create"
,
Node
:
&
etcd
.
Node
{
Node
:
&
etcd
.
Node
{
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podBar
)),
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podBar
)),
CreatedIndex
:
2
,
CreatedIndex
:
3
,
ModifiedIndex
:
2
,
ModifiedIndex
:
3
,
},
},
},
},
event
:
watch
.
Added
,
event
:
watch
.
Added
,
...
@@ -238,13 +238,13 @@ func TestWatch(t *testing.T) {
...
@@ -238,13 +238,13 @@ func TestWatch(t *testing.T) {
Action
:
"set"
,
Action
:
"set"
,
Node
:
&
etcd
.
Node
{
Node
:
&
etcd
.
Node
{
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
CreatedIndex
:
1
,
CreatedIndex
:
2
,
ModifiedIndex
:
3
,
ModifiedIndex
:
4
,
},
},
PrevNode
:
&
etcd
.
Node
{
PrevNode
:
&
etcd
.
Node
{
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
CreatedIndex
:
1
,
CreatedIndex
:
2
,
ModifiedIndex
:
1
,
ModifiedIndex
:
2
,
},
},
},
},
event
:
watch
.
Modified
,
event
:
watch
.
Modified
,
...
@@ -253,7 +253,7 @@ func TestWatch(t *testing.T) {
...
@@ -253,7 +253,7 @@ func TestWatch(t *testing.T) {
}
}
// Set up Watch for object "podFoo".
// Set up Watch for object "podFoo".
watcher
,
err
:=
cacher
.
Watch
(
"pods/ns/foo"
,
1
,
storage
.
Everything
)
watcher
,
err
:=
cacher
.
Watch
(
"pods/ns/foo"
,
2
,
storage
.
Everything
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -276,13 +276,13 @@ func TestWatch(t *testing.T) {
...
@@ -276,13 +276,13 @@ func TestWatch(t *testing.T) {
}
}
// Check whether we get too-old error.
// Check whether we get too-old error.
_
,
err
=
cacher
.
Watch
(
"pods/ns/foo"
,
0
,
storage
.
Everything
)
_
,
err
=
cacher
.
Watch
(
"pods/ns/foo"
,
1
,
storage
.
Everything
)
if
err
==
nil
{
if
err
==
nil
{
t
.
Errorf
(
"ex
pe
cted 'error too old' error"
)
t
.
Errorf
(
"ex
ep
cted 'error too old' error"
)
}
}
// Now test watch with initial state.
// Now test watch with initial state.
initialWatcher
,
err
:=
cacher
.
Watch
(
"pods/ns/foo"
,
1
,
storage
.
Everything
)
initialWatcher
,
err
:=
cacher
.
Watch
(
"pods/ns/foo"
,
2
,
storage
.
Everything
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -302,6 +302,39 @@ func TestWatch(t *testing.T) {
...
@@ -302,6 +302,39 @@ func TestWatch(t *testing.T) {
}
}
}
}
// Now test watch from "now".
nowWatcher
,
err
:=
cacher
.
Watch
(
"pods/ns/foo"
,
0
,
storage
.
Everything
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
select
{
case
event
:=
<-
nowWatcher
.
ResultChan
()
:
if
obj
:=
event
.
Object
.
(
*
api
.
Pod
);
event
.
Type
!=
watch
.
Added
||
obj
.
ResourceVersion
!=
"4"
{
t
.
Errorf
(
"unexpected event: %v"
,
event
)
}
case
<-
time
.
After
(
time
.
Millisecond
*
100
)
:
t
.
Errorf
(
"timed out waiting for an event"
)
}
// Emit a new event and check if it is observed by the watcher.
fakeClient
.
WatchResponse
<-
&
etcd
.
Response
{
Action
:
"set"
,
Node
:
&
etcd
.
Node
{
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
CreatedIndex
:
2
,
ModifiedIndex
:
5
,
},
PrevNode
:
&
etcd
.
Node
{
Value
:
string
(
runtime
.
EncodeOrDie
(
testapi
.
Default
.
Codec
(),
podFoo
)),
CreatedIndex
:
2
,
ModifiedIndex
:
4
,
},
}
event
:=
<-
nowWatcher
.
ResultChan
()
obj
:=
event
.
Object
.
(
*
api
.
Pod
)
if
event
.
Type
!=
watch
.
Modified
||
obj
.
ResourceVersion
!=
"5"
{
t
.
Errorf
(
"unexpected event: %v"
,
event
)
}
close
(
fakeClient
.
WatchResponse
)
close
(
fakeClient
.
WatchResponse
)
}
}
...
...
pkg/storage/watch_cache.go
View file @
12eaf67a
...
@@ -250,6 +250,20 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]wa
...
@@ -250,6 +250,20 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]wa
if
size
>
0
{
if
size
>
0
{
oldest
=
w
.
cache
[
w
.
startIndex
%
w
.
capacity
]
.
resourceVersion
oldest
=
w
.
cache
[
w
.
startIndex
%
w
.
capacity
]
.
resourceVersion
}
}
if
resourceVersion
==
0
{
// resourceVersion = 0 means that we don't require any specific starting point
// and we would like to start watching from ~now.
// However, to keep backward compatibility, we additionally need to return the
// current state and only then start watching from that point.
//
// TODO: In v2 api, we should stop returning the current state - #13969.
allItems
:=
w
.
store
.
List
()
result
:=
make
([]
watchCacheEvent
,
len
(
allItems
))
for
i
,
item
:=
range
allItems
{
result
[
i
]
=
watchCacheEvent
{
Type
:
watch
.
Added
,
Object
:
item
.
(
runtime
.
Object
)}
}
return
result
,
nil
}
if
resourceVersion
<
oldest
{
if
resourceVersion
<
oldest
{
return
nil
,
fmt
.
Errorf
(
"too old resource version: %d (%d)"
,
resourceVersion
,
oldest
)
return
nil
,
fmt
.
Errorf
(
"too old resource version: %d (%d)"
,
resourceVersion
,
oldest
)
}
}
...
...
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