Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] improve the perf of schema and schema cache #1033

Merged
merged 6 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 9 additions & 14 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,24 @@ type partitionProducer struct {
}

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

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

func (s *schemaCache) Put(schema *SchemaInfo, schemaVersion []byte) {
s.lock.Lock()
defer s.lock.Unlock()

key := schema.hash()
s.schemas[key] = schemaVersion
s.schemas.Store(key, schemaVersion)
}

func (s *schemaCache) Get(schema *SchemaInfo) (schemaVersion []byte) {
s.lock.RLock()
defer s.lock.RUnlock()

return s.schemas[schema.hash()]
val, ok := s.schemas.Load(schema.hash())
if !ok {
return nil
}
return val.([]byte)
}

func newPartitionProducer(client *client, topic string, options *ProducerOptions, partitionIdx int,
Expand Down Expand Up @@ -1106,7 +1101,7 @@ func (p *partitionProducer) SendAsync(ctx context.Context, msg *ProducerMessage,

func (p *partitionProducer) internalSendAsync(ctx context.Context, msg *ProducerMessage,
callback func(MessageID, *ProducerMessage, error), flushImmediately bool) {
//Register transaction operation to transaction and the transaction coordinator.
// Register transaction operation to transaction and the transaction coordinator.
var newCallback func(MessageID, *ProducerMessage, error)
if msg.Transaction != nil {
transactionImpl := (msg.Transaction).(*transaction)
Expand Down
43 changes: 26 additions & 17 deletions pulsar/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"fmt"
"hash/maphash"
"reflect"
"sync/atomic"
"unsafe"

log "github.com/sirupsen/logrus"
Expand All @@ -37,27 +38,27 @@
type SchemaType int

const (
NONE SchemaType = iota //No schema defined
shibd marked this conversation as resolved.
Show resolved Hide resolved
STRING //Simple String encoding with UTF-8
JSON //JSON object encoding and validation
PROTOBUF //Protobuf message encoding and decoding
AVRO //Serialize and deserialize via Avro
NONE SchemaType = iota // No schema defined
STRING // Simple String encoding with UTF-8
JSON // JSON object encoding and validation
PROTOBUF // Protobuf message encoding and decoding
AVRO // Serialize and deserialize via Avro
BOOLEAN //
INT8 //A 8-byte integer.
INT16 //A 16-byte integer.
INT32 //A 32-byte integer.
INT64 //A 64-byte integer.
FLOAT //A float number.
DOUBLE //A double number
INT8 // A 8-byte integer.
INT16 // A 16-byte integer.
INT32 // A 32-byte integer.
INT64 // A 64-byte integer.
FLOAT // A float number.
DOUBLE // A double number
_ //
_ //
_ //
KeyValue //A Schema that contains Key Schema and Value Schema.
BYTES = -1 //A bytes array.
KeyValue // A Schema that contains Key Schema and Value Schema.
BYTES = -1 // A bytes array.
AUTO = -2 //
AutoConsume = -3 //Auto Consume Type.
AutoConsume = -3 // Auto Consume Type.
AutoPublish = -4 // Auto Publish Type.
ProtoNative = 20 //Protobuf native message encoding and decoding
ProtoNative = 20 // Protobuf native message encoding and decoding
)

// Encapsulates data around the schema definition
Expand All @@ -66,13 +67,21 @@
Schema string
Type SchemaType
Properties map[string]string
hashVal atomic.Uint64

Check failure on line 70 in pulsar/schema.go

View workflow job for this annotation

GitHub Actions / integration-tests (1.16)

undefined: "sync/atomic".Uint64

Check failure on line 70 in pulsar/schema.go

View workflow job for this annotation

GitHub Actions / integration-tests (1.18)

undefined: atomic.Uint64
}

func (s SchemaInfo) hash() uint64 {
func (s *SchemaInfo) hash() uint64 {
oldHash := s.hashVal.Load()
if oldHash != 0 {
return oldHash
}

h := maphash.Hash{}
h.SetSeed(seed)
h.Write([]byte(s.Schema))
return h.Sum64()
newHash := h.Sum64()
s.hashVal.CompareAndSwap(oldHash, newHash)
shibd marked this conversation as resolved.
Show resolved Hide resolved
return newHash
}

type Schema interface {
Expand Down