Skip to content

Commit

Permalink
FIX: use maphash instead of crypto/sha256 for hash function of hashma…
Browse files Browse the repository at this point in the history
…p in Schema.hash()

- replace sha256 hash function with hash/maphash
- use uint64 (expected from maphash) as schema cache hashmap key
- init static seed as it is random generated
  • Loading branch information
bpereto committed May 29, 2023
1 parent 27e3625 commit 2615403
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ type partitionProducer struct {

type schemaCache struct {
lock sync.RWMutex
schemas map[string][]byte
schemas map[uint64][]byte
}

func newSchemaCache() *schemaCache {
return &schemaCache{
schemas: make(map[string][]byte),
schemas: make(map[uint64][]byte),
}
}

Expand All @@ -122,9 +122,9 @@ func (s *schemaCache) Get(schema *SchemaInfo) (schemaVersion []byte) {
s.lock.RLock()
defer s.lock.RUnlock()

key := schema.hash()
return s.schemas[key]
return s.schemas[schema.hash()]
}

func newPartitionProducer(client *client, topic string, options *ProducerOptions, partitionIdx int,
metrics *internal.LeveledMetrics) (
*partitionProducer, error) {
Expand Down
12 changes: 7 additions & 5 deletions pulsar/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package pulsar

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"hash/maphash"
"reflect"
"unsafe"

Expand Down Expand Up @@ -69,10 +68,11 @@ type SchemaInfo struct {
Properties map[string]string
}

func (s SchemaInfo) hash() string {
h := sha256.New()
func (s SchemaInfo) hash() uint64 {
h := maphash.Hash{}
h.SetSeed(seed)
h.Write([]byte(s.Schema))
return hex.EncodeToString(h.Sum(nil))
return h.Sum64()
}

type Schema interface {
Expand Down Expand Up @@ -183,6 +183,8 @@ type ProtoSchema struct {
SchemaInfo
}

var seed = maphash.MakeSeed()

// NewProtoSchema creates a new ProtoSchema
// Note: the function will panic if creation of codec fails
func NewProtoSchema(protoAvroSchemaDef string, properties map[string]string) *ProtoSchema {
Expand Down

0 comments on commit 2615403

Please sign in to comment.