Skip to content

Commit

Permalink
platform agnostic lb ep hash
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenctl committed Nov 22, 2024
1 parent 7dd0bee commit 8fffbe3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
6 changes: 6 additions & 0 deletions changelog/v1.18.0-rc2/platform-agnostic-lbephash.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
changelog:
- type: NON_USER_FACING
description: >-
Don't use xor or native endian-ness when generating hashes.
This creates difficulty reproducing tests on different machines
where the hashes end up in goldenfiles.
4 changes: 2 additions & 2 deletions projects/gateway2/krtcollections/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func hashEndpoints(l PodLocality, emd EndpointWithMd) uint64 {
func hash(a, b uint64) uint64 {
hasher := fnv.New64a()
var buf [16]byte
binary.NativeEndian.PutUint64(buf[:8], a)
binary.NativeEndian.PutUint64(buf[8:], b)
binary.LittleEndian.PutUint64(buf[:8], a)
binary.LittleEndian.PutUint64(buf[8:], b)
hasher.Write(buf[:])
return hasher.Sum64()
}
Expand Down
20 changes: 16 additions & 4 deletions projects/gateway2/krtcollections/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package krtcollections

import (
"context"
"crypto/sha256"
"encoding/binary"
"testing"

envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
Expand Down Expand Up @@ -131,7 +133,6 @@ func TestEndpointsForUpstreamOrderDoesntMatter(t *testing.T) {
Zone: "zone2",
}, emd2)
g.Expect(result1.Equals(*result4)).To(BeFalse(), "not expected %v, got %v", result1, result2)

}

func TestEndpointsForUpstreamWithDiscoveredUpstream(t *testing.T) {
Expand Down Expand Up @@ -235,11 +236,23 @@ func TestEndpointsForUpstreamWithDiscoveredUpstream(t *testing.T) {
Zone: "zone",
}, emd2)

h1 := result1.LbEpsEqualityHash ^ result2.LbEpsEqualityHash
h2 := result3.LbEpsEqualityHash ^ result4.LbEpsEqualityHash
h1 := combineHashes(result1.LbEpsEqualityHash, result2.LbEpsEqualityHash)
h2 := combineHashes(result3.LbEpsEqualityHash, result4.LbEpsEqualityHash)

g.Expect(h1).NotTo(Equal(h2), "not expected %v, got %v", h1, h2)
}

func combineHashes(hash1, hash2 uint64) uint64 {
// Create a buffer for the combined hashes
buf := make([]byte, 16)
binary.LittleEndian.PutUint64(buf[:8], hash1)
binary.LittleEndian.PutUint64(buf[8:], hash2)

// Hash the combined buffer
sum := sha256.Sum256(buf)

// Return the first 8 bytes of the hash as a uint64
return binary.LittleEndian.Uint64(sum[:8])
}

func TestEndpoints(t *testing.T) {
Expand Down Expand Up @@ -960,5 +973,4 @@ func TestEndpoints(t *testing.T) {
g.Expect(eps.Equals(*res)).To(BeTrue(), "expected %v, got %v", res, eps)
})
}

}
12 changes: 9 additions & 3 deletions projects/gateway2/proxy_syncer/cla.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxy_syncer

import (
"encoding/binary"
"fmt"
"hash/fnv"

Expand Down Expand Up @@ -129,18 +130,23 @@ func NewPerClientEnvoyEndpoints(logger *zap.Logger, dbg *krt.DebugHandler, uccs
}

func PrioritizeEndpoints(logger *zap.Logger, destrule *DestinationRuleWrapper, ep krtcollections.EndpointsForUpstream, ucc krtcollections.UniqlyConnectedClient) UccWithEndpoints {
var additionalHash uint64
var priorityInfo *PriorityInfo

updatedHash := ep.LbEpsEqualityHash
if destrule != nil {
trafficPolicy := getTraficPolicy(destrule, ep.Port)
localityLb := getLocalityLbSetting(trafficPolicy)
if localityLb != nil {
priorityInfo = getPriorityInfoFromDestrule(localityLb)
hasher := fnv.New64()

oldHash := make([]byte, 8)
binary.LittleEndian.PutUint64(oldHash, updatedHash)
hasher.Write(oldHash)

hasher.Write([]byte(destrule.UID))
hasher.Write([]byte(fmt.Sprintf("%v", destrule.Generation)))
additionalHash = hasher.Sum64()
updatedHash = hasher.Sum64()
}
}
lbInfo := LoadBalancingInfo{
Expand All @@ -153,7 +159,7 @@ func PrioritizeEndpoints(logger *zap.Logger, destrule *DestinationRuleWrapper, e
return UccWithEndpoints{
Client: ucc,
Endpoints: resource.NewEnvoyResource(cla),
EndpointsHash: ep.LbEpsEqualityHash ^ additionalHash,
EndpointsHash: updatedHash,
endpointsName: ep.ResourceName(),
}
}
Expand Down

0 comments on commit 8fffbe3

Please sign in to comment.