Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/loadbalance/consist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ package loadbalance

import (
"context"
"hash/maphash"
"sort"
"sync"
"time"

"github.com/bytedance/gopkg/util/xxhash3"
"golang.org/x/sync/singleflight"

"github.com/cloudwego/kitex/pkg/discovery"
"github.com/cloudwego/kitex/pkg/utils"
)

var hashSeed = maphash.MakeSeed()

/*
type hints for sync.Map:
consistBalancer -> sync.Map[entry.CacheKey]*consistInfo
Expand Down Expand Up @@ -156,7 +158,7 @@ func (cp *consistPicker) Next(ctx context.Context, request interface{}) discover
if key == "" {
return nil
}
res := buildConsistResult(cp.info, xxhash3.HashString(key))
res := buildConsistResult(cp.info, maphash.String(hashSeed, key))
return res.Primary
// Todo(DMwangnima): Optimise Replica-related logic
// This comment part is previous implementation considering connecting to Replica
Expand Down Expand Up @@ -326,7 +328,7 @@ func (cb *consistBalancer) buildVirtualNodes(rNodes []realNode) []virtualNode {
}
// At this point, the index inside ret should be cur + j.
index := cur + j
ret[index].hash = xxhash3.Hash(b)
ret[index].hash = maphash.Bytes(hashSeed, b)
ret[index].RealNode = &rNodes[i]
}
cur += vLen
Expand Down
7 changes: 4 additions & 3 deletions pkg/remote/codec/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ package codec
import (
"context"
"errors"
"hash/maphash"
"strconv"
"testing"

"github.com/bytedance/gopkg/util/xxhash3"

"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/remote"
Expand All @@ -44,6 +43,8 @@ func (m *mockPayloadValidator) Key(ctx context.Context) string {
return "mockValidator"
}

var hashSeed = maphash.MakeSeed()

func (m *mockPayloadValidator) Generate(ctx context.Context, outPayload []byte) (need bool, value string, err error) {
if l := ctx.Value(mockGenerateSkipKey); l != nil {
return false, "", nil
Expand All @@ -55,7 +56,7 @@ func (m *mockPayloadValidator) Generate(ctx context.Context, outPayload []byte)
if l := ctx.Value(mockGenerateErrorKey); l != nil {
return false, "", errors.New("mockGenerateError")
}
hash := xxhash3.Hash(outPayload)
hash := maphash.Bytes(hashSeed, outPayload)
return true, strconv.FormatInt(int64(hash), 10), nil
}

Expand Down