Skip to content

Commit ad17910

Browse files
committed
Fixed sortedIndex assignments that cause miscalculated usage
1 parent e221461 commit ad17910

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

data-node/cache/container.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ func (c *container) Query(sha512Hex string, begins uint32, ends uint32) []byte {
260260

261261
c.sortedIndex[index.sortIndex] = nil
262262

263-
index.expiresAt = time.Now().UTC().Add(c.lifetime)
264263
index.sortIndex = len(c.sortedIndex)
264+
index.expiresAt = time.Now().UTC().Add(c.lifetime)
265265

266266
c.sortedIndex = append(c.sortedIndex, &index)
267267
c.index[index.sha512Hex] = index
@@ -290,11 +290,11 @@ func (c *container) Upsert(sha512Hex string, begins uint32, ends uint32, data []
290290
currentItem = indexItem{
291291
sha512Hex: sha512Hex,
292292
dataItems: make(dataContainerList, 0),
293-
sortIndex: len(c.sortedIndex),
294293
}
295294
} else {
296295
c.sortedIndex[currentItem.sortIndex] = nil
297296
}
297+
currentItem.sortIndex = len(c.sortedIndex)
298298
currentItem.expiresAt = time.Now().UTC().Add(c.lifetime)
299299

300300
prevSize, newSize := currentItem.Merge(begins, ends, data)

data-node/cache/container_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,65 @@ func TestContainer_PurgeV3(t *testing.T) {
129129
assert.Less(t, int64(result), int64(container.limit))
130130
}
131131

132+
func TestContainer_PurgeV4(t *testing.T) {
133+
logger, _ := zap.NewDevelopment()
134+
135+
container := container{
136+
limit: 1024 * 1024 * 1024 * 5,
137+
lifetime: time.Second * 5,
138+
mutex: &sync.Mutex{},
139+
index: make(map[string]indexItem),
140+
sortedIndex: make(indexItemList, 0),
141+
logger: logger,
142+
} // limit 5MB
143+
container.start()
144+
145+
type dI struct {
146+
name string
147+
data []byte
148+
index int
149+
}
150+
151+
for i := 0; i < 1024; i++ {
152+
dI := dI{
153+
name: fmt.Sprintf("a%d", i+1),
154+
data: make([]byte, 1024*(i+1)),
155+
index: i,
156+
}
157+
158+
container.Upsert(dI.name, 0, 0, dI.data)
159+
if i == 0 {
160+
container.sortedIndex[i].expiresAt = time.Now().UTC().Add(time.Second)
161+
continue
162+
}
163+
container.sortedIndex[i].expiresAt = time.Now().UTC().Add(time.Second * -1)
164+
}
165+
container.Purge()
166+
167+
assert.Equal(t, int64(1024), container.usage)
168+
}
169+
170+
func TestContainer_PurgeV5(t *testing.T) {
171+
logger, _ := zap.NewDevelopment()
172+
173+
container := container{
174+
limit: 1024 * 1024 * 5,
175+
lifetime: time.Second,
176+
mutex: &sync.Mutex{},
177+
index: make(map[string]indexItem),
178+
sortedIndex: make(indexItemList, 0),
179+
logger: logger,
180+
} // limit 5MB
181+
container.start()
182+
183+
container.Upsert("a1", 5, 21, make([]byte, 21-5))
184+
container.Upsert("a1", 22, 25, make([]byte, 25-22))
185+
container.Upsert("a1", 18, 23, make([]byte, 23-18))
186+
time.Sleep(time.Second * 2)
187+
188+
assert.Equal(t, int64(0), container.usage)
189+
}
190+
132191
func TestIndexItem_MatchRangeV1(t *testing.T) {
133192
item := indexItem{
134193
sha512Hex: "test",

0 commit comments

Comments
 (0)