Skip to content

Commit

Permalink
Fixed sortedIndex assignments that cause miscalculated usage
Browse files Browse the repository at this point in the history
  • Loading branch information
freakmaxi committed Jan 25, 2022
1 parent e221461 commit ad17910
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions data-node/cache/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ func (c *container) Query(sha512Hex string, begins uint32, ends uint32) []byte {

c.sortedIndex[index.sortIndex] = nil

index.expiresAt = time.Now().UTC().Add(c.lifetime)
index.sortIndex = len(c.sortedIndex)
index.expiresAt = time.Now().UTC().Add(c.lifetime)

c.sortedIndex = append(c.sortedIndex, &index)
c.index[index.sha512Hex] = index
Expand Down Expand Up @@ -290,11 +290,11 @@ func (c *container) Upsert(sha512Hex string, begins uint32, ends uint32, data []
currentItem = indexItem{
sha512Hex: sha512Hex,
dataItems: make(dataContainerList, 0),
sortIndex: len(c.sortedIndex),
}
} else {
c.sortedIndex[currentItem.sortIndex] = nil
}
currentItem.sortIndex = len(c.sortedIndex)
currentItem.expiresAt = time.Now().UTC().Add(c.lifetime)

prevSize, newSize := currentItem.Merge(begins, ends, data)
Expand Down
59 changes: 59 additions & 0 deletions data-node/cache/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,65 @@ func TestContainer_PurgeV3(t *testing.T) {
assert.Less(t, int64(result), int64(container.limit))
}

func TestContainer_PurgeV4(t *testing.T) {
logger, _ := zap.NewDevelopment()

container := container{
limit: 1024 * 1024 * 1024 * 5,
lifetime: time.Second * 5,
mutex: &sync.Mutex{},
index: make(map[string]indexItem),
sortedIndex: make(indexItemList, 0),
logger: logger,
} // limit 5MB
container.start()

type dI struct {
name string
data []byte
index int
}

for i := 0; i < 1024; i++ {
dI := dI{
name: fmt.Sprintf("a%d", i+1),
data: make([]byte, 1024*(i+1)),
index: i,
}

container.Upsert(dI.name, 0, 0, dI.data)
if i == 0 {
container.sortedIndex[i].expiresAt = time.Now().UTC().Add(time.Second)
continue
}
container.sortedIndex[i].expiresAt = time.Now().UTC().Add(time.Second * -1)
}
container.Purge()

assert.Equal(t, int64(1024), container.usage)
}

func TestContainer_PurgeV5(t *testing.T) {
logger, _ := zap.NewDevelopment()

container := container{
limit: 1024 * 1024 * 5,
lifetime: time.Second,
mutex: &sync.Mutex{},
index: make(map[string]indexItem),
sortedIndex: make(indexItemList, 0),
logger: logger,
} // limit 5MB
container.start()

container.Upsert("a1", 5, 21, make([]byte, 21-5))
container.Upsert("a1", 22, 25, make([]byte, 25-22))
container.Upsert("a1", 18, 23, make([]byte, 23-18))
time.Sleep(time.Second * 2)

assert.Equal(t, int64(0), container.usage)
}

func TestIndexItem_MatchRangeV1(t *testing.T) {
item := indexItem{
sha512Hex: "test",
Expand Down

0 comments on commit ad17910

Please sign in to comment.