replace xxhash with xxh3 (#5457)

* update config file paths hash

* update filemgr

* use xxh3 for hashutil.Hash

* update hashutil digest, fix trace buffer test

* update comments

* update namegen, go mod tidy
This commit is contained in:
Caleb Doxsey 2025-01-31 08:44:08 -07:00 committed by GitHub
parent 5e94b2f8f1
commit dc9a6bdb81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 76 additions and 66 deletions

View file

@ -1,18 +1,16 @@
// Package hashutil provides NON-CRYPTOGRAPHIC utility functions for hashing.
//
// http://cyan4973.github.io/xxHash/
//
//nolint:errcheck
package hashutil
import (
"encoding/binary"
"github.com/cespare/xxhash/v2"
"github.com/mitchellh/hashstructure/v2"
"github.com/zeebo/xxh3"
)
// MustHash returns the xxhash of an arbitrary value or struct. Returns 0
// MustHash returns the xxh3 hash of an arbitrary value or struct. Returns 0
// on error.
// NOT SUITABLE FOR CRYTOGRAPHIC HASHING.
func MustHash(v any) uint64 {
@ -23,17 +21,17 @@ func MustHash(v any) uint64 {
return hash
}
// Hash returns the xxhash of an arbitrary value or struct.
// Hash returns the xxh3 hash of an arbitrary value or struct.
// NOT SUITABLE FOR CRYTOGRAPHIC HASHING.
func Hash(v any) (uint64, error) {
opts := &hashstructure.HashOptions{
Hasher: xxhash.New(),
Hasher: xxh3.New(),
}
return hashstructure.Hash(v, hashstructure.FormatV2, opts)
}
type Digest struct {
xxhash.Digest
xxh3.Hasher
}
func NewDigest() *Digest {

View file

@ -1,10 +1,11 @@
// Package hashutil provides NON-CRYPTOGRAPHIC utility functions for hashing
package hashutil
package hashutil_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/pomerium/pomerium/internal/hashutil"
)
func TestHash(t *testing.T) {
@ -15,8 +16,8 @@ func TestHash(t *testing.T) {
want uint64
wantErr bool
}{
{"string", "string", 6134271061086542852, false},
{"num", 7, 609900476111905877, false},
{"string", "string", 15613163272824911089, false},
{"num", 7, 9324454920402081455, false},
{
"compound struct",
struct {
@ -26,7 +27,7 @@ func TestHash(t *testing.T) {
[]string{"Battletoads", "Mega Man 1", "Clash at Demonhead"},
12,
},
1349584765528830812, false,
9585735524299267794, false,
},
{
"compound struct with embedded func (errors!)",
@ -40,10 +41,10 @@ func TestHash(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MustHash(tt.v); got != tt.want {
if got := hashutil.MustHash(tt.v); got != tt.want {
t.Errorf("MustHash() = %v, want %v", got, tt.want)
}
got, err := Hash(tt.v)
got, err := hashutil.Hash(tt.v)
if tt.wantErr {
assert.Error(t, err)
} else {

View file

@ -7,11 +7,12 @@ import (
"slices"
"sync"
"github.com/pomerium/pomerium/internal/hashutil"
commonv1 "go.opentelemetry.io/proto/otlp/common/v1"
resourcev1 "go.opentelemetry.io/proto/otlp/resource/v1"
tracev1 "go.opentelemetry.io/proto/otlp/trace/v1"
"google.golang.org/protobuf/proto"
"github.com/pomerium/pomerium/internal/hashutil"
)
type ScopeBuffer struct {
@ -31,6 +32,7 @@ func NewScopeBuffer(scope *ScopeInfo) *ScopeBuffer {
type ResourceBuffer struct {
resource *ResourceInfo
scopeIDs []string
spansByScope map[string]*ScopeBuffer
}
@ -48,14 +50,15 @@ func (rb *ResourceBuffer) Insert(scope *ScopeInfo, span *tracev1.Span) {
} else {
spans = NewScopeBuffer(scope)
rb.spansByScope[scope.ID()] = spans
rb.scopeIDs = append(rb.scopeIDs, scope.ID())
}
spans.Insert(span)
}
func (rb *ResourceBuffer) Flush() []*tracev1.ScopeSpans {
out := make([]*tracev1.ScopeSpans, 0, len(rb.spansByScope))
for _, key := range slices.Sorted(maps.Keys(rb.spansByScope)) {
spans := rb.spansByScope[key]
for _, scopeID := range rb.scopeIDs {
spans := rb.spansByScope[scopeID]
slices.SortStableFunc(spans.spans, func(a, b *tracev1.Span) int {
return cmp.Compare(a.StartTimeUnixNano, b.StartTimeUnixNano)
})
@ -66,6 +69,7 @@ func (rb *ResourceBuffer) Flush() []*tracev1.ScopeSpans {
}
out = append(out, scopeSpans)
}
rb.scopeIDs = nil
clear(rb.spansByScope)
return out
}
@ -73,11 +77,13 @@ func (rb *ResourceBuffer) Flush() []*tracev1.ScopeSpans {
func (rb *ResourceBuffer) Merge(other *ResourceBuffer) {
for scope, otherSpans := range other.spansByScope {
if ourSpans, ok := rb.spansByScope[scope]; !ok {
rb.scopeIDs = append(rb.scopeIDs, scope)
rb.spansByScope[scope] = otherSpans
} else {
ourSpans.Insert(otherSpans.spans...)
}
}
other.scopeIDs = nil
clear(other.spansByScope)
}