Skip to content

Commit

Permalink
Merge pull request #707 from hzxuzhonghu/hash-reset
Browse files Browse the repository at this point in the history
FIX: go test timeout after running benchmark test
  • Loading branch information
kmesh-bot authored Aug 14, 2024
2 parents 890d41f + 4e84096 commit 6f8fd43
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pkg/controller/workload/workload_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func TestWorkloadStreamCreateAndSend(t *testing.T) {
if (err != nil) != tt.wantErr {
t.Errorf("workloadStream.WorklaodStreamCreateAndSend() error = %v, wantErr %v", err, tt.wantErr)
}
if workloadController.Processor != nil {
workloadController.Processor.hashName.Reset()
}
tt.afterFunc()
})
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/controller/workload/workload_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ package workload

import (
"fmt"
"hash"
"hash/fnv"
"math"
"os"

"gopkg.in/yaml.v3"
)

var (
hash = fnv.New32a()
)

const (
persistPath = "/mnt/workload_hash_name.yaml"
)
Expand All @@ -37,11 +34,13 @@ const (
type HashName struct {
numToStr map[uint32]string
strToNum map[string]uint32
hash hash.Hash32
}

func NewHashName() *HashName {
hashName := &HashName{
strToNum: make(map[string]uint32),
hash: fnv.New32a(),
}
// if read failed, initialize with an empty map
if err := hashName.readFromPersistFile(); err != nil {
Expand Down Expand Up @@ -94,15 +93,14 @@ func (h *HashName) flushDelta(str string, num uint32) error {
func (h *HashName) StrToNum(str string) uint32 {
var num uint32

hash.Reset()
hash.Write([]byte(str))

if num, exists := h.strToNum[str]; exists {
return num
}

h.hash.Reset()
h.hash.Write([]byte(str))
// Using linear probing to solve hash conflicts
for num = hash.Sum32(); num < math.MaxUint32; num++ {
for num = h.hash.Sum32(); num < math.MaxUint32; num++ {
// Create a new item if we find an empty slot
if _, exists := h.numToStr[num]; !exists {
h.numToStr[num] = str
Expand Down Expand Up @@ -137,3 +135,8 @@ func (h *HashName) Delete(str string) {
}
}
}

// Should only be used by test
func (h *HashName) Reset() {
os.Remove(persistPath)
}
3 changes: 3 additions & 0 deletions pkg/controller/workload/workload_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func cleanPersistFile() {
func TestWorkloadHash_Basic(t *testing.T) {
cleanPersistFile()
hashName := NewHashName()
defer hashName.Reset()

// "foo" does not collide with "bar"
// while "costarring" collides with "liquid"
Expand Down Expand Up @@ -110,4 +111,6 @@ func TestWorkloadHash_StrToNumAfterDelete(t *testing.T) {
return
}
}

hashName.Reset()
}
2 changes: 2 additions & 0 deletions pkg/controller/workload/workload_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func BenchmarkHandleDataWithService(b *testing.B) {
err := workloadController.Processor.handleDataWithService(workload)
assert.NoError(t, err)
}
workloadController.Processor.hashName.Reset()
}

func createTestWorkloadWithService() *workloadapi.Workload {
Expand Down Expand Up @@ -367,4 +368,5 @@ func hashNameClean(p *Processor) {
}
p.hashName.Delete(str)
}
p.hashName.Reset()
}

0 comments on commit 6f8fd43

Please sign in to comment.