2023-03-13 21:56:44 +03:00
|
|
|
package kgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-13 02:24:16 +03:00
|
|
|
"strconv"
|
2023-03-13 21:56:44 +03:00
|
|
|
"sync"
|
2024-04-13 02:24:16 +03:00
|
|
|
"time"
|
2023-03-13 21:56:44 +03:00
|
|
|
|
2024-04-13 02:24:16 +03:00
|
|
|
"github.com/twmb/franz-go/pkg/kadm"
|
2023-03-13 21:56:44 +03:00
|
|
|
"github.com/twmb/franz-go/pkg/kgo"
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-04-13 02:24:16 +03:00
|
|
|
"go.unistack.org/micro/v3/semconv"
|
2023-03-13 21:56:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type tp struct {
|
|
|
|
t string
|
|
|
|
p int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type consumer struct {
|
|
|
|
c *kgo.Client
|
|
|
|
topic string
|
|
|
|
partition int32
|
|
|
|
opts broker.SubscribeOptions
|
|
|
|
kopts broker.Options
|
|
|
|
handler broker.Handler
|
|
|
|
quit chan struct{}
|
|
|
|
done chan struct{}
|
|
|
|
recs chan kgo.FetchTopicPartition
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
|
|
|
c *kgo.Client
|
|
|
|
topic string
|
|
|
|
opts broker.SubscribeOptions
|
|
|
|
kopts broker.Options
|
|
|
|
handler broker.Handler
|
|
|
|
closed bool
|
|
|
|
done chan struct{}
|
|
|
|
consumers map[tp]*consumer
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Options() broker.SubscribeOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Unsubscribe(ctx context.Context) error {
|
|
|
|
if s.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
2023-05-25 15:11:53 +03:00
|
|
|
// case <-ctx.Done():
|
|
|
|
// return ctx.Err()
|
2023-03-13 21:56:44 +03:00
|
|
|
default:
|
2023-05-13 15:12:26 +03:00
|
|
|
s.c.PauseFetchTopics(s.topic)
|
2023-05-25 15:11:53 +03:00
|
|
|
s.c.CloseAllowingRebalance()
|
2023-05-13 15:12:26 +03:00
|
|
|
kc := make(map[string][]int32)
|
|
|
|
for ctp := range s.consumers {
|
|
|
|
kc[ctp.t] = append(kc[ctp.t], ctp.p)
|
|
|
|
}
|
|
|
|
s.killConsumers(ctx, kc)
|
2023-03-13 21:56:44 +03:00
|
|
|
close(s.done)
|
|
|
|
s.closed = true
|
2024-02-21 15:08:58 +03:00
|
|
|
s.c.ResumeFetchTopics(s.topic)
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) poll(ctx context.Context) {
|
|
|
|
maxInflight := DefaultSubscribeMaxInflight
|
|
|
|
if s.opts.Context != nil {
|
|
|
|
if n, ok := s.opts.Context.Value(subscribeMaxInflightKey{}).(int); n > 0 && ok {
|
|
|
|
maxInflight = n
|
|
|
|
}
|
|
|
|
}
|
2024-04-13 02:24:16 +03:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
ac := kadm.NewClient(s.c)
|
|
|
|
ticker := time.NewTicker(DefaultStatsInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2024-04-13 02:40:45 +03:00
|
|
|
dgls, err := ac.Lag(ctx, s.opts.Group)
|
2024-04-13 02:24:16 +03:00
|
|
|
if err != nil || !dgls.Ok() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dgl, ok := dgls[s.opts.Group]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lmap, ok := dgl.Lag[s.topic]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-19 13:26:49 +03:00
|
|
|
s.Lock()
|
2024-04-13 02:24:16 +03:00
|
|
|
for tp := range s.consumers {
|
|
|
|
if v, ok := lmap[tp.p]; ok {
|
|
|
|
s.kopts.Meter.Counter(semconv.BrokerGroupLag, "topic", s.topic, "group", s.opts.Group, "partition", strconv.Itoa(int(tp.p)), "lag", strconv.Itoa(int(v.Lag)))
|
|
|
|
}
|
|
|
|
}
|
2024-04-19 13:26:49 +03:00
|
|
|
s.Unlock()
|
2024-04-13 02:24:16 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2023-05-13 15:12:26 +03:00
|
|
|
s.c.CloseAllowingRebalance()
|
2023-03-13 21:56:44 +03:00
|
|
|
return
|
|
|
|
case <-s.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
fetches := s.c.PollRecords(ctx, maxInflight)
|
2023-05-13 15:12:26 +03:00
|
|
|
if !s.closed && fetches.IsClientClosed() {
|
2023-03-13 21:56:44 +03:00
|
|
|
s.closed = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fetches.EachError(func(t string, p int32, err error) {
|
|
|
|
s.kopts.Logger.Fatalf(ctx, "[kgo] fetch topic %s partition %d err: %v", t, p, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
fetches.EachPartition(func(p kgo.FetchTopicPartition) {
|
|
|
|
tp := tp{p.Topic, p.Partition}
|
|
|
|
s.consumers[tp].recs <- p
|
|
|
|
})
|
|
|
|
s.c.AllowRebalance()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) killConsumers(ctx context.Context, lost map[string][]int32) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
|
|
|
|
|
|
|
for topic, partitions := range lost {
|
|
|
|
for _, partition := range partitions {
|
|
|
|
tp := tp{topic, partition}
|
|
|
|
pc := s.consumers[tp]
|
|
|
|
delete(s.consumers, tp)
|
|
|
|
close(pc.quit)
|
|
|
|
s.kopts.Logger.Debugf(ctx, "[kgo] waiting for work to finish topic %s partition %d", topic, partition)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() { <-pc.done; wg.Done() }()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) lost(ctx context.Context, _ *kgo.Client, lost map[string][]int32) {
|
|
|
|
s.kopts.Logger.Debugf(ctx, "[kgo] lost %#+v", lost)
|
|
|
|
s.killConsumers(ctx, lost)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) revoked(ctx context.Context, c *kgo.Client, revoked map[string][]int32) {
|
|
|
|
s.kopts.Logger.Debugf(ctx, "[kgo] revoked %#+v", revoked)
|
|
|
|
s.killConsumers(ctx, revoked)
|
|
|
|
if err := c.CommitMarkedOffsets(ctx); err != nil {
|
|
|
|
s.kopts.Logger.Errorf(ctx, "[kgo] revoked CommitMarkedOffsets err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) assigned(_ context.Context, c *kgo.Client, assigned map[string][]int32) {
|
|
|
|
for topic, partitions := range assigned {
|
|
|
|
for _, partition := range partitions {
|
|
|
|
pc := &consumer{
|
|
|
|
c: c,
|
|
|
|
topic: topic,
|
|
|
|
partition: partition,
|
|
|
|
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
done: make(chan struct{}),
|
2024-02-21 15:08:58 +03:00
|
|
|
recs: make(chan kgo.FetchTopicPartition, 100),
|
2023-03-13 21:56:44 +03:00
|
|
|
handler: s.handler,
|
|
|
|
kopts: s.kopts,
|
|
|
|
opts: s.opts,
|
|
|
|
}
|
2024-04-19 13:16:23 +03:00
|
|
|
s.Lock()
|
2023-03-13 21:56:44 +03:00
|
|
|
s.consumers[tp{topic, partition}] = pc
|
2024-04-19 13:16:23 +03:00
|
|
|
s.Unlock()
|
2023-03-13 21:56:44 +03:00
|
|
|
go pc.consume()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *consumer) consume() {
|
|
|
|
defer close(pc.done)
|
|
|
|
pc.kopts.Logger.Debugf(pc.kopts.Context, "starting, topic %s partition %d", pc.topic, pc.partition)
|
|
|
|
defer pc.kopts.Logger.Debugf(pc.kopts.Context, "killing, topic %s partition %d", pc.topic, pc.partition)
|
|
|
|
|
|
|
|
eh := pc.kopts.ErrorHandler
|
|
|
|
if pc.opts.ErrorHandler != nil {
|
|
|
|
eh = pc.opts.ErrorHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pc.quit:
|
|
|
|
return
|
|
|
|
case p := <-pc.recs:
|
|
|
|
for _, record := range p.Records {
|
2024-04-13 02:24:16 +03:00
|
|
|
ts := time.Now()
|
|
|
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Inc()
|
2023-03-13 21:56:44 +03:00
|
|
|
p := eventPool.Get().(*event)
|
|
|
|
p.msg.Header = nil
|
|
|
|
p.msg.Body = nil
|
|
|
|
p.topic = record.Topic
|
|
|
|
p.err = nil
|
|
|
|
p.ack = false
|
2024-02-21 15:08:58 +03:00
|
|
|
p.msg.Header = metadata.New(len(record.Headers))
|
|
|
|
for _, hdr := range record.Headers {
|
|
|
|
p.msg.Header.Set(hdr.Key, string(hdr.Value))
|
|
|
|
}
|
2023-03-13 21:56:44 +03:00
|
|
|
if pc.kopts.Codec.String() == "noop" {
|
|
|
|
p.msg.Body = record.Value
|
|
|
|
} else if pc.opts.BodyOnly {
|
|
|
|
p.msg.Body = record.Value
|
|
|
|
} else {
|
|
|
|
if err := pc.kopts.Codec.Unmarshal(record.Value, p.msg); err != nil {
|
2024-04-13 02:24:16 +03:00
|
|
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", record.Topic, "topic", record.Topic, "status", "failure").Inc()
|
2023-03-13 21:56:44 +03:00
|
|
|
p.err = err
|
|
|
|
p.msg.Body = record.Value
|
|
|
|
if eh != nil {
|
|
|
|
_ = eh(p)
|
2024-04-13 02:24:16 +03:00
|
|
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Dec()
|
2023-03-13 21:56:44 +03:00
|
|
|
if p.ack {
|
|
|
|
pc.c.MarkCommitRecords(record)
|
|
|
|
} else {
|
|
|
|
eventPool.Put(p)
|
2024-02-21 15:08:58 +03:00
|
|
|
pc.kopts.Logger.Fatalf(pc.kopts.Context, "[kgo] ErrLostMessage wtf?")
|
2023-03-13 21:56:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
eventPool.Put(p)
|
2024-04-13 02:24:16 +03:00
|
|
|
te := time.Since(ts)
|
|
|
|
pc.kopts.Meter.Summary(semconv.SubscribeMessageLatencyMicroseconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
|
|
|
pc.kopts.Meter.Histogram(semconv.SubscribeMessageDurationSeconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
2023-03-13 21:56:44 +03:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if pc.kopts.Logger.V(logger.ErrorLevel) {
|
|
|
|
pc.kopts.Logger.Errorf(pc.kopts.Context, "[kgo]: failed to unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2024-04-13 02:24:16 +03:00
|
|
|
te := time.Since(ts)
|
|
|
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Dec()
|
|
|
|
pc.kopts.Meter.Summary(semconv.SubscribeMessageLatencyMicroseconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
|
|
|
pc.kopts.Meter.Histogram(semconv.SubscribeMessageDurationSeconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
2023-03-13 21:56:44 +03:00
|
|
|
eventPool.Put(p)
|
2024-02-21 15:08:58 +03:00
|
|
|
pc.kopts.Logger.Fatalf(pc.kopts.Context, "[kgo] Unmarshal err not handled wtf?")
|
2023-03-13 21:56:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := pc.handler(p)
|
2024-02-21 15:08:58 +03:00
|
|
|
if err == nil {
|
2024-04-13 02:24:16 +03:00
|
|
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", record.Topic, "topic", record.Topic, "status", "success").Inc()
|
2024-02-21 15:08:58 +03:00
|
|
|
} else {
|
2024-04-13 02:24:16 +03:00
|
|
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", record.Topic, "topic", record.Topic, "status", "failure").Inc()
|
2024-02-21 15:08:58 +03:00
|
|
|
}
|
2024-04-13 02:24:16 +03:00
|
|
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Dec()
|
2023-03-13 21:56:44 +03:00
|
|
|
if err == nil && pc.opts.AutoAck {
|
|
|
|
p.ack = true
|
|
|
|
} else if err != nil {
|
|
|
|
p.err = err
|
|
|
|
if eh != nil {
|
|
|
|
_ = eh(p)
|
|
|
|
} else {
|
|
|
|
if pc.kopts.Logger.V(logger.ErrorLevel) {
|
|
|
|
pc.kopts.Logger.Errorf(pc.kopts.Context, "[kgo]: subscriber error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-13 02:24:16 +03:00
|
|
|
te := time.Since(ts)
|
|
|
|
pc.kopts.Meter.Summary(semconv.SubscribeMessageLatencyMicroseconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
|
|
|
pc.kopts.Meter.Histogram(semconv.SubscribeMessageDurationSeconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
2023-03-13 21:56:44 +03:00
|
|
|
if p.ack {
|
|
|
|
eventPool.Put(p)
|
|
|
|
pc.c.MarkCommitRecords(record)
|
|
|
|
} else {
|
|
|
|
eventPool.Put(p)
|
|
|
|
pc.kopts.Logger.Fatalf(pc.kopts.Context, "[kgo] ErrLostMessage wtf?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|