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
7f61d379
Unverified
Commit
7f61d379
authored
Nov 09, 2016
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix watching from resourceVersion=0 in etcd3 watcher
parent
dbb4def4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
14 deletions
+55
-14
event.go
pkg/storage/etcd3/event.go
+4
-3
watcher.go
pkg/storage/etcd3/watcher.go
+2
-10
watcher_test.go
pkg/storage/etcd3/watcher_test.go
+49
-1
No files found.
pkg/storage/etcd3/event.go
View file @
7f61d379
...
...
@@ -30,14 +30,15 @@ type event struct {
isCreated
bool
}
func
parseKV
(
kv
*
mvccpb
.
KeyValue
,
prevVal
[]
byte
)
*
event
{
// parseKV converts a KeyValue retrieved from an initial sync() listing to a synthetic isCreated event.
func
parseKV
(
kv
*
mvccpb
.
KeyValue
)
*
event
{
return
&
event
{
key
:
string
(
kv
.
Key
),
value
:
kv
.
Value
,
prevValue
:
prevVa
l
,
prevValue
:
ni
l
,
rev
:
kv
.
ModRevision
,
isDeleted
:
false
,
isCreated
:
kv
.
ModRevision
==
kv
.
CreateRevision
,
isCreated
:
true
,
}
}
...
...
pkg/storage/etcd3/watcher.go
View file @
7f61d379
...
...
@@ -146,6 +146,7 @@ func (wc *watchChan) ResultChan() <-chan watch.Event {
// sync tries to retrieve existing data and send them to process.
// The revision to watch will be set to the revision in response.
// All events sent will have isCreated=true
func
(
wc
*
watchChan
)
sync
()
error
{
opts
:=
[]
clientv3
.
OpOption
{}
if
wc
.
recursive
{
...
...
@@ -156,17 +157,8 @@ func (wc *watchChan) sync() error {
return
err
}
wc
.
initialRev
=
getResp
.
Header
.
Revision
for
_
,
kv
:=
range
getResp
.
Kvs
{
prevResp
,
err
:=
wc
.
watcher
.
client
.
Get
(
wc
.
ctx
,
string
(
kv
.
Key
),
clientv3
.
WithRev
(
kv
.
ModRevision
-
1
),
clientv3
.
WithSerializable
())
if
err
!=
nil
{
return
err
}
var
prevVal
[]
byte
if
len
(
prevResp
.
Kvs
)
>
0
{
prevVal
=
prevResp
.
Kvs
[
0
]
.
Value
}
wc
.
sendEvent
(
parseKV
(
kv
,
prevVal
))
wc
.
sendEvent
(
parseKV
(
kv
))
}
return
nil
}
...
...
pkg/storage/etcd3/watcher_test.go
View file @
7f61d379
...
...
@@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"sync"
"testing"
"time"
...
...
@@ -141,17 +142,64 @@ func TestDeleteTriggerWatch(t *testing.T) {
// TestWatchFromZero tests that
// - watch from 0 should sync up and grab the object added before
// - watch from 0 is able to return events for objects whose previous version has been compacted
// - watch from non-0 should just watch changes after given version
func
TestWatchFromZero
(
t
*
testing
.
T
)
{
ctx
,
store
,
cluster
:=
testSetup
(
t
)
defer
cluster
.
Terminate
(
t
)
key
,
storedObj
:=
testPropogateStore
(
ctx
,
t
,
store
,
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
}})
key
,
storedObj
:=
testPropogateStore
(
ctx
,
t
,
store
,
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"ns"
}})
w
,
err
:=
store
.
Watch
(
ctx
,
key
,
"0"
,
storage
.
Everything
)
if
err
!=
nil
{
t
.
Fatalf
(
"Watch failed: %v"
,
err
)
}
testCheckResult
(
t
,
0
,
watch
.
Added
,
w
,
storedObj
)
w
.
Stop
()
// Update
out
:=
&
api
.
Pod
{}
err
=
store
.
GuaranteedUpdate
(
ctx
,
key
,
out
,
true
,
nil
,
storage
.
SimpleUpdate
(
func
(
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"ns"
,
Annotations
:
map
[
string
]
string
{
"a"
:
"1"
}}},
nil
}))
if
err
!=
nil
{
t
.
Fatalf
(
"GuaranteedUpdate failed: %v"
,
err
)
}
// Make sure when we watch from 0 we receive an ADDED event
w
,
err
=
store
.
Watch
(
ctx
,
key
,
"0"
,
storage
.
Everything
)
if
err
!=
nil
{
t
.
Fatalf
(
"Watch failed: %v"
,
err
)
}
testCheckResult
(
t
,
1
,
watch
.
Added
,
w
,
out
)
w
.
Stop
()
// Update again
out
=
&
api
.
Pod
{}
err
=
store
.
GuaranteedUpdate
(
ctx
,
key
,
out
,
true
,
nil
,
storage
.
SimpleUpdate
(
func
(
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"ns"
}},
nil
}))
if
err
!=
nil
{
t
.
Fatalf
(
"GuaranteedUpdate failed: %v"
,
err
)
}
// Compact previous versions
revToCompact
,
err
:=
strconv
.
Atoi
(
out
.
ResourceVersion
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error converting %q to an int: %v"
,
storedObj
.
ResourceVersion
,
err
)
}
_
,
err
=
cluster
.
RandClient
()
.
Compact
(
ctx
,
int64
(
revToCompact
),
clientv3
.
WithCompactPhysical
())
if
err
!=
nil
{
t
.
Fatalf
(
"Error compacting: %v"
,
err
)
}
// Make sure we can still watch from 0 and receive an ADDED event
w
,
err
=
store
.
Watch
(
ctx
,
key
,
"0"
,
storage
.
Everything
)
if
err
!=
nil
{
t
.
Fatalf
(
"Watch failed: %v"
,
err
)
}
testCheckResult
(
t
,
2
,
watch
.
Added
,
w
,
out
)
}
// TestWatchFromNoneZero tests that
...
...
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