-
Notifications
You must be signed in to change notification settings - Fork 602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a bug that obscures the byte queue when it is full and is extended #251
Conversation
Codecov Report
@@ Coverage Diff @@
## master #251 +/- ##
=========================================
Coverage ? 86.56%
=========================================
Files ? 15
Lines ? 640
Branches ? 0
=========================================
Hits ? 554
Misses ? 71
Partials ? 15
Continue to review full report at Codecov.
|
LGTM I've tested it with test included in #234 but it does not solve that issues package bigcache
import (
"math/rand"
"runtime"
"strconv"
"testing"
"time"
)
const (
entries = 3000
repeats = 1000
)
var valBig = append(make([]byte, 100*1024), 1)
var valMed = append(make([]byte, 1024), 1)
var valSmall = append(make([]byte, 2), 1)
func getValue() []byte {
x := rand.Float64()
if x < 0.7 {
return valSmall
} else if x < 0.9 {
return valMed
}
return valBig
}
// https://github.com/allegro/bigcache/issues/234#issuecomment-673895517
func Test_issue_234(t *testing.T) {
t.Log("Number of entries: ", entries)
printAllocs(t)
rand.Seed(1)
config := Config{
Shards: 128,
LifeWindow: time.Hour,
CleanWindow: time.Second,
MaxEntriesInWindow: entries,
MaxEntrySize: 1024,
Verbose: true,
HardMaxCacheSize: 128,
OnRemoveWithReason: func(key string, entry []byte, reason RemoveReason) {
t.Log("Evicted:", len(entry), " reason: ", reason)
},
}
bigcache, err := NewBigCache(config)
if err != nil {
panic(err)
}
for i := 0; i < repeats; i++ {
printAllocs(t)
for j := 0; j < entries; j++ {
key := strconv.FormatInt(int64(j), 10)
err := bigcache.Set(key, getValue())
if err != nil {
t.Fatal(err)
}
}
}
}
func printAllocs(t *testing.T) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
t.Logf("Alloc: %6d MB \n", m.Alloc/1e6)
} |
@janisz A more minimalistic test for the bug from #234 is:
|
@mxplusb @cristaloleg Can you look at this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks!
@mxplusb |
@Fabianexe I released v2.2.5 just now, which includes this fix, so you should be good to go! |
Refs: #253 |
Hi,
On searching for the reason for the panics in the current version where versions pre 2.2 works well, I think I have found the cause.
With PR #207 it gets possible that the byte queen is full and
q.tail == q.head
. However, in theallocateAdditionalMemory
method, the condition for resetting head and tail isq.tail < q.head
. Thus the tail and head are not changed, and the byte queen is obscured after it. An example that leads to panic in the old logic is shown in the testTestPushEntryAfterAllocateAdditionMemoryInFull
.This is a different reason for panics then #236, so I am not sure which of the reported panic ( #226 #234 #235 ) is caused by this bug.