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
7e787b14
Commit
7e787b14
authored
Dec 12, 2016
by
Chao Xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix leaking goroutine issues in watch cache
parent
d5055d49
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
3 deletions
+69
-3
BUILD
pkg/storage/BUILD
+1
-0
cacher.go
pkg/storage/cacher.go
+12
-3
cacher_whitebox_test.go
pkg/storage/cacher_whitebox_test.go
+56
-0
No files found.
pkg/storage/BUILD
View file @
7e787b14
...
@@ -50,6 +50,7 @@ go_library(
...
@@ -50,6 +50,7 @@ go_library(
go_test(
go_test(
name = "go_default_test",
name = "go_default_test",
srcs = [
srcs = [
"cacher_whitebox_test.go",
"selection_predicate_test.go",
"selection_predicate_test.go",
"time_budget_test.go",
"time_budget_test.go",
"util_test.go",
"util_test.go",
...
...
pkg/storage/cacher.go
View file @
7e787b14
...
@@ -750,6 +750,7 @@ type cacheWatcher struct {
...
@@ -750,6 +750,7 @@ type cacheWatcher struct {
sync
.
Mutex
sync
.
Mutex
input
chan
watchCacheEvent
input
chan
watchCacheEvent
result
chan
watch
.
Event
result
chan
watch
.
Event
done
chan
struct
{}
filter
watchFilterFunc
filter
watchFilterFunc
stopped
bool
stopped
bool
forget
func
(
bool
)
forget
func
(
bool
)
...
@@ -759,6 +760,7 @@ func newCacheWatcher(resourceVersion uint64, chanSize int, initEvents []watchCac
...
@@ -759,6 +760,7 @@ func newCacheWatcher(resourceVersion uint64, chanSize int, initEvents []watchCac
watcher
:=
&
cacheWatcher
{
watcher
:=
&
cacheWatcher
{
input
:
make
(
chan
watchCacheEvent
,
chanSize
),
input
:
make
(
chan
watchCacheEvent
,
chanSize
),
result
:
make
(
chan
watch
.
Event
,
chanSize
),
result
:
make
(
chan
watch
.
Event
,
chanSize
),
done
:
make
(
chan
struct
{}),
filter
:
filter
,
filter
:
filter
,
stopped
:
false
,
stopped
:
false
,
forget
:
forget
,
forget
:
forget
,
...
@@ -783,6 +785,7 @@ func (c *cacheWatcher) stop() {
...
@@ -783,6 +785,7 @@ func (c *cacheWatcher) stop() {
defer
c
.
Unlock
()
defer
c
.
Unlock
()
if
!
c
.
stopped
{
if
!
c
.
stopped
{
c
.
stopped
=
true
c
.
stopped
=
true
close
(
c
.
done
)
close
(
c
.
input
)
close
(
c
.
input
)
}
}
}
}
...
@@ -847,13 +850,19 @@ func (c *cacheWatcher) sendWatchCacheEvent(event *watchCacheEvent) {
...
@@ -847,13 +850,19 @@ func (c *cacheWatcher) sendWatchCacheEvent(event *watchCacheEvent) {
glog
.
Errorf
(
"unexpected copy error: %v"
,
err
)
glog
.
Errorf
(
"unexpected copy error: %v"
,
err
)
return
return
}
}
var
watchEvent
watch
.
Event
switch
{
switch
{
case
curObjPasses
&&
!
oldObjPasses
:
case
curObjPasses
&&
!
oldObjPasses
:
c
.
result
<-
watch
.
Event
{
Type
:
watch
.
Added
,
Object
:
object
}
watchEvent
=
watch
.
Event
{
Type
:
watch
.
Added
,
Object
:
object
}
case
curObjPasses
&&
oldObjPasses
:
case
curObjPasses
&&
oldObjPasses
:
c
.
result
<-
watch
.
Event
{
Type
:
watch
.
Modified
,
Object
:
object
}
watchEvent
=
watch
.
Event
{
Type
:
watch
.
Modified
,
Object
:
object
}
case
!
curObjPasses
&&
oldObjPasses
:
case
!
curObjPasses
&&
oldObjPasses
:
c
.
result
<-
watch
.
Event
{
Type
:
watch
.
Deleted
,
Object
:
object
}
watchEvent
=
watch
.
Event
{
Type
:
watch
.
Deleted
,
Object
:
object
}
}
select
{
case
c
.
result
<-
watchEvent
:
// don't block on c.result if c.done is closed
case
<-
c
.
done
:
}
}
}
}
...
...
pkg/storage/cacher_whitebox_test.go
0 → 100644
View file @
7e787b14
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
storage
import
(
"sync"
"testing"
"time"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/wait"
)
// verifies the cacheWatcher.process goroutine is properly cleaned up even if
// the writes to cacheWatcher.result channel is blocked.
func
TestCacheWatcherCleanupNotBlockedByResult
(
t
*
testing
.
T
)
{
var
lock
sync
.
RWMutex
count
:=
0
filter
:=
func
(
string
,
labels
.
Set
,
fields
.
Set
)
bool
{
return
true
}
forget
:=
func
(
bool
)
{
lock
.
Lock
()
defer
lock
.
Unlock
()
count
++
}
initEvents
:=
[]
watchCacheEvent
{
{
Object
:
&
api
.
Pod
{}},
{
Object
:
&
api
.
Pod
{}},
}
// set the size of the buffer of w.result to 0, so that the writes to
// w.result is blocked.
w
:=
newCacheWatcher
(
0
,
0
,
initEvents
,
filter
,
forget
)
w
.
Stop
()
if
err
:=
wait
.
PollImmediate
(
1
*
time
.
Second
,
5
*
time
.
Second
,
func
()
(
bool
,
error
)
{
lock
.
RLock
()
defer
lock
.
RUnlock
()
return
count
==
2
,
nil
});
err
!=
nil
{
t
.
Fatalf
(
"expected forget() to be called twice, because sendWatchCacheEvent should not be blocked by the result channel: %v"
,
err
)
}
}
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