Skip to content

Support Iterator for fastcache#44

Open
ehnuje wants to merge 1 commit intoVictoriaMetrics:masterfrom
ehnuje:0131-fastcache-iterator
Open

Support Iterator for fastcache#44
ehnuje wants to merge 1 commit intoVictoriaMetrics:masterfrom
ehnuje:0131-fastcache-iterator

Conversation

@ehnuje
Copy link
Copy Markdown

@ehnuje ehnuje commented Jan 31, 2021

func TestCacheIterator(t *testing.T) {
	c := New(1024)
	defer c.Reset()

	numEntries := 100
	keyValMap := make(map[string][]byte)
	for i := 0; i < numEntries; i++ {
		k := []byte(fmt.Sprintf("key %d", i))
		v := []byte(fmt.Sprintf("value %d", i))
		c.Set(k, v)
		keyValMap[string(k)] = v
		vv := c.Get(nil, k)
		if string(vv) != string(v) {
			t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
		}
	}

	itr := c.Iterator()
	for itr.SetNext() {
		entry, err := itr.Value()
		if err != nil {
			t.Fatal("unexpected error from itr.Value()", "err", err)
		}

		val, exist := keyValMap[string(entry.key)]
		if !exist {
			t.Fatal("failed to retrieve an entry from cache which should exist")
		}
		if !bytes.Equal(val, entry.value) {
			t.Fatalf("value from iterator is not the same as the expected one for key %q; got %q; want %q",
				entry.key, entry.value, val)
		}
	}
}

@elimist3
Copy link
Copy Markdown

LTGM!

From the looks of it, this is a highly desirable feature and this PR looks pretty solid!

@hhhhax
Copy link
Copy Markdown

hhhhax commented Jul 26, 2023

When the number of traversals I traverse is large enough, it will cause the array to go out of bounds and cause a panic. Can you help me check it out?
`c := New(2 * 1024 * 1024 * 1024 * 1024)
defer c.Reset()

numEntries := 10000000
keyValMap := make(map[string][]byte)
for i := 0; i < numEntries; i++ {
	k := []byte(fmt.Sprintf("key %d", i))
	v := []byte(fmt.Sprintf("value %d", i))
	c.Set(k, v)
	keyValMap[string(k)] = v
	vv := c.Get(nil, k)
	if string(vv) != string(v) {
		t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
	}
}

go func() {
	for {
		numEntries := 10000000
		keyValMap := make(map[string][]byte)
		for i := 0; i < numEntries; i++ {
			k := []byte(fmt.Sprintf("key %d", i))
			v := []byte(fmt.Sprintf("value %d", i))
			keyValMap[string(k)] = v
			vv := c.Get(nil, k)
			if string(vv) != string(v) {
				t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
			}
		}

	}
}()

for {
	time.Sleep(1 * time.Second)
	itr := c.Iterator()
	count := 0
	for itr.HasNext() {
		count++
		_, _, err := itr.Value()
		if err != nil {
			t.Fatal("unexpected error from itr.Value()", "err", err)
		}

		//val, exist := keyValMap[string(key)]
		//if !exist {
		//	t.Fatal("failed to retrieve an entry from cache which should exist")
		//}
		//if !bytes.Equal(val, value) {
		//	t.Fatalf("value from iterator is not the same as the expected one for key %q; got %q; want %q",
		//		key, value, val)
		//}
	}
	fmt.Printf("count:%v\n", count)
}`

chunkIdx := idx / chunkSize
chunk := b.chunks[chunkIdx]

kvLenBuf := chunk[idx : idx+4]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause the array to go out of bounds and generate Panic

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this line of code may solve the problem, can you help me take a look? idx = idx % chunkSize

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think idx %= chunkSize should be added like in (*bucket).Get().

FYI, here is the panic log:

2026/03/31 08:19:17 [Recovery] 2026/03/31 - 08:19:17 panic recovered:
runtime error: slice bounds out of range [:144261] with capacity 65536

HT4w5 added a commit to HT4w5/flux that referenced this pull request Mar 30, 2026
Implement cache dumping with fastcache iterator. Will switch back to official when the iterator PR is merged.

See VictoriaMetrics/fastcache#44
idx := v & ((1 << bucketSizeBits) - 1)
gen := v >> bucketSizeBits

if gen == b.gen && idx < b.idx || gen+1 == b.gen && idx >= b.idx {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the condition should be:

gen == bGen && idx < b.idx || gen+1 == bGen && idx >= b.idx || gen == maxGen && bGen == 1 && idx >= b.idx

gen == maxGen && bGen == 1 && idx >= b.idx is the situation where maxGen is reached on the last wrap and the current generation is reset to 1.

cache *Cache
currBucketSize int
currBucketIdx int
currBucketKeys [][]byte
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider storing (hash, idx) pairs in iterator instead of bucket keys? Let SetNext() verify key hash on retrieval?

This removes the necessity of copying all the keys from the bucket.

}

// SetNext moves to the next element and returns true if the value exists.
func (it *Iterator) SetNext() bool {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently SetNext() allocates a new slice when retrieving the value. Consider using a single GetNext(kDst []byte, vDst []byte) ([]byte, []byte, bool) similar to (*Cache).Get for zero-copy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants