perf fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-08-26 00:36:32 +03:00
parent e3b2a0d62b
commit fe6d7d340e
2 changed files with 34 additions and 26 deletions

25
kgo.go
View File

@ -19,6 +19,12 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
var pPool = sync.Pool{
New: func() interface{} {
return &broker.Message{}
},
}
type kBroker struct { type kBroker struct {
writer *kgo.Client // used only to push messages writer *kgo.Client // used only to push messages
kopts []kgo.Opt kopts []kgo.Opt
@ -397,7 +403,7 @@ func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) err
} }
// preallocate optimistic // preallocate optimistic
crecords := make([]*kgo.Record, 0, 1000) crecords := make([]*kgo.Record, 0, 10000)
eh := s.kopts.ErrorHandler eh := s.kopts.ErrorHandler
if s.opts.ErrorHandler != nil { if s.opts.ErrorHandler != nil {
@ -430,7 +436,6 @@ func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) err
return err return err
case <-ticker.C: case <-ticker.C:
v := atomic.LoadInt64(&cnt) v := atomic.LoadInt64(&cnt)
s.kopts.Logger.Debugf(ctx, "records to commit: %v", v)
if v == 0 { if v == 0 {
return nil return nil
} }
@ -439,9 +444,8 @@ func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) err
mu.Unlock() mu.Unlock()
return err return err
} }
s.kopts.Logger.Debugf(ctx, "records to need process after commit: %v", v-int64(len(crecords)))
atomic.AddInt64(&cnt, -int64(len(crecords))) atomic.AddInt64(&cnt, -int64(len(crecords)))
crecords = nil crecords = crecords[:0]
mu.Unlock() mu.Unlock()
} }
} }
@ -457,7 +461,10 @@ func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) err
case <-gctx.Done(): case <-gctx.Done():
return gctx.Err() return gctx.Err()
default: default:
p := &publication{topic: record.Topic, msg: &broker.Message{}} msg := pPool.Get().(*broker.Message)
msg.Header = nil
msg.Body = nil
p := &publication{topic: record.Topic, msg: msg}
if s.opts.BodyOnly { if s.opts.BodyOnly {
p.msg.Body = record.Value p.msg.Body = record.Value
} else { } else {
@ -471,12 +478,14 @@ func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) err
crecords = append(crecords, record) crecords = append(crecords, record)
mu.Unlock() mu.Unlock()
} }
pPool.Put(msg)
return nil return nil
} else { } else {
if s.kopts.Logger.V(logger.ErrorLevel) { if s.kopts.Logger.V(logger.ErrorLevel) {
s.kopts.Logger.Errorf(s.kopts.Context, "[kgo]: failed to unmarshal: %v", err) s.kopts.Logger.Errorf(s.kopts.Context, "[kgo]: failed to unmarshal: %v", err)
} }
} }
pPool.Put(msg)
return err return err
} }
} }
@ -501,6 +510,7 @@ func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) err
mu.Unlock() mu.Unlock()
} }
} }
pPool.Put(msg)
} }
} }
return nil return nil
@ -511,10 +521,6 @@ func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) err
return err return err
} }
if s.kopts.Logger.V(logger.DebugLevel) {
logger.Debugf(ctx, "commit %d records", len(crecords))
}
return s.reader.CommitRecords(ctx, crecords...) return s.reader.CommitRecords(ctx, crecords...)
} }
@ -525,6 +531,7 @@ func (k *kBroker) String() string {
func NewBroker(opts ...broker.Option) broker.Broker { func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.NewOptions(opts...) options := broker.NewOptions(opts...)
kopts := []kgo.Opt{ kopts := []kgo.Opt{
kgo.BatchCompression(kgo.NoCompression()),
kgo.WithLogger(&mlogger{l: options.Logger, ctx: options.Context}), kgo.WithLogger(&mlogger{l: options.Logger, ctx: options.Context}),
kgo.RequiredAcks(kgo.AllISRAcks()), kgo.RequiredAcks(kgo.AllISRAcks()),
kgo.RetryBackoffFn( kgo.RetryBackoffFn(

View File

@ -2,7 +2,6 @@ package kgo_test
import ( import (
"context" "context"
"fmt"
"os" "os"
"strings" "strings"
"sync/atomic" "sync/atomic"
@ -40,9 +39,8 @@ func TestPubSub(t *testing.T) {
b := kgo.NewBroker( b := kgo.NewBroker(
broker.Codec(jsoncodec.NewCodec()), broker.Codec(jsoncodec.NewCodec()),
broker.Addrs(addrs...), broker.Addrs(addrs...),
kgo.ClientID("test"), kgo.CommitInterval(5*time.Second),
kgo.CommitInterval(1*time.Second), kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024)),
kgo.Options(kg.FetchMaxBytes(10*1024)),
) )
if err := b.Init(); err != nil { if err := b.Init(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -57,10 +55,8 @@ func TestPubSub(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
}() }()
_ = bm
/*
fmt.Printf("prefill")
/*
msgs := make([]*broker.Message, 0, 600000) msgs := make([]*broker.Message, 0, 600000)
for i := 0; i < 600000; i++ { for i := 0; i < 600000; i++ {
msgs = append(msgs, bm) msgs = append(msgs, bm)
@ -70,7 +66,7 @@ func TestPubSub(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
t.Skip() t.Skip()
*/ */
done := make(chan bool, 1) done := make(chan bool, 1)
idx := int64(0) idx := int64(0)
fn := func(msg broker.Event) error { fn := func(msg broker.Event) error {
@ -79,7 +75,10 @@ func TestPubSub(t *testing.T) {
return msg.Ack() return msg.Ack()
} }
sub, err := b.Subscribe(ctx, "test", fn, broker.SubscribeAutoAck(true), broker.SubscribeGroup("test17"), broker.SubscribeBodyOnly(true)) sub, err := b.Subscribe(ctx, "test", fn,
broker.SubscribeAutoAck(true),
broker.SubscribeGroup("test23"),
broker.SubscribeBodyOnly(true))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -89,14 +88,16 @@ func TestPubSub(t *testing.T) {
} }
}() }()
for { ticker := time.NewTicker(2 * time.Minute)
if v := atomic.LoadInt64(&idx); v == 12637303 { defer ticker.Stop()
close(done)
break go func() {
} else { for {
fmt.Printf("processed %v\n", v) select {
case <-ticker.C:
close(done)
}
} }
time.Sleep(1 * time.Second) }()
}
<-done <-done
} }