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
cf199dbd
Commit
cf199dbd
authored
Apr 12, 2016
by
Seth Jennings
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix immediate evict in cache
parent
e9c1d591
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
4 deletions
+32
-4
cache.go
pkg/util/cache/cache.go
+5
-2
cache_test.go
pkg/util/cache/cache_test.go
+27
-2
No files found.
pkg/util/cache/cache.go
View file @
cf199dbd
...
@@ -27,6 +27,9 @@ const (
...
@@ -27,6 +27,9 @@ const (
type
Cache
[]
*
cacheShard
type
Cache
[]
*
cacheShard
func
NewCache
(
maxSize
int
)
Cache
{
func
NewCache
(
maxSize
int
)
Cache
{
if
maxSize
<
shardsCount
{
maxSize
=
shardsCount
}
cache
:=
make
(
Cache
,
shardsCount
)
cache
:=
make
(
Cache
,
shardsCount
)
for
i
:=
0
;
i
<
shardsCount
;
i
++
{
for
i
:=
0
;
i
<
shardsCount
;
i
++
{
cache
[
i
]
=
&
cacheShard
{
cache
[
i
]
=
&
cacheShard
{
...
@@ -61,14 +64,14 @@ func (s *cacheShard) add(index uint64, obj interface{}) bool {
...
@@ -61,14 +64,14 @@ func (s *cacheShard) add(index uint64, obj interface{}) bool {
s
.
Lock
()
s
.
Lock
()
defer
s
.
Unlock
()
defer
s
.
Unlock
()
_
,
isOverwrite
:=
s
.
items
[
index
]
_
,
isOverwrite
:=
s
.
items
[
index
]
s
.
items
[
index
]
=
obj
if
!
isOverwrite
&&
len
(
s
.
items
)
>=
s
.
maxSize
{
if
len
(
s
.
items
)
>
s
.
maxSize
{
var
randomKey
uint64
var
randomKey
uint64
for
randomKey
=
range
s
.
items
{
for
randomKey
=
range
s
.
items
{
break
break
}
}
delete
(
s
.
items
,
randomKey
)
delete
(
s
.
items
,
randomKey
)
}
}
s
.
items
[
index
]
=
obj
return
isOverwrite
return
isOverwrite
}
}
...
...
pkg/util/cache/cache_test.go
View file @
cf199dbd
...
@@ -24,13 +24,16 @@ const (
...
@@ -24,13 +24,16 @@ const (
maxTestCacheSize
int
=
shardsCount
*
2
maxTestCacheSize
int
=
shardsCount
*
2
)
)
func
ExpectEntry
(
t
*
testing
.
T
,
cache
Cache
,
index
uint64
,
expectedValue
interface
{})
{
func
ExpectEntry
(
t
*
testing
.
T
,
cache
Cache
,
index
uint64
,
expectedValue
interface
{})
bool
{
elem
,
found
:=
cache
.
Get
(
index
)
elem
,
found
:=
cache
.
Get
(
index
)
if
!
found
{
if
!
found
{
t
.
Error
(
"Expected to find entry with key 1"
)
t
.
Errorf
(
"Expected to find entry with key %d"
,
index
)
return
false
}
else
if
elem
!=
expectedValue
{
}
else
if
elem
!=
expectedValue
{
t
.
Errorf
(
"Expected to find %v, got %v"
,
expectedValue
,
elem
)
t
.
Errorf
(
"Expected to find %v, got %v"
,
expectedValue
,
elem
)
return
false
}
}
return
true
}
}
func
TestBasic
(
t
*
testing
.
T
)
{
func
TestBasic
(
t
*
testing
.
T
)
{
...
@@ -63,3 +66,25 @@ func TestOverwrite(t *testing.T) {
...
@@ -63,3 +66,25 @@ func TestOverwrite(t *testing.T) {
cache
.
Add
(
1
,
"yyy"
)
cache
.
Add
(
1
,
"yyy"
)
ExpectEntry
(
t
,
cache
,
1
,
"yyy"
)
ExpectEntry
(
t
,
cache
,
1
,
"yyy"
)
}
}
// TestEvict this test will fail sporatically depending on what add()
// selects for the randomKey to be evicted. Ensure that randomKey
// is never the key we most recently added. Since the chance of failure
// on each evict is 50%, if we do it 7 times, it should catch the problem
// if it exists >99% of the time.
func
TestEvict
(
t
*
testing
.
T
)
{
cache
:=
NewCache
(
shardsCount
)
var
found
bool
for
retry
:=
0
;
retry
<
7
;
retry
++
{
cache
.
Add
(
uint64
(
shardsCount
),
"xxx"
)
found
=
ExpectEntry
(
t
,
cache
,
uint64
(
shardsCount
),
"xxx"
)
if
!
found
{
break
}
cache
.
Add
(
0
,
"xxx"
)
found
=
ExpectEntry
(
t
,
cache
,
0
,
"xxx"
)
if
!
found
{
break
}
}
}
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