2023-03-13 21:56:44 +03:00
|
|
|
package kgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-12 12:45:53 +03:00
|
|
|
"fmt"
|
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"
|
2024-05-05 16:20:05 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2023-03-13 21:56:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type tp struct {
|
|
|
|
t string
|
|
|
|
p int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type consumer struct {
|
2024-12-19 13:21:32 +03:00
|
|
|
topic string
|
|
|
|
|
|
|
|
c *kgo.Client
|
2024-05-06 07:30:17 +03:00
|
|
|
htracer *hookTracer
|
2024-12-19 13:21:32 +03:00
|
|
|
|
|
|
|
handler broker.Handler
|
|
|
|
quit chan struct{}
|
|
|
|
done chan struct{}
|
|
|
|
recs chan kgo.FetchTopicPartition
|
|
|
|
|
|
|
|
kopts broker.Options
|
|
|
|
opts broker.SubscribeOptions
|
|
|
|
|
|
|
|
partition int32
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
type Subscriber struct {
|
2024-12-19 13:21:32 +03:00
|
|
|
consumers map[tp]*consumer
|
2023-03-13 21:56:44 +03:00
|
|
|
c *kgo.Client
|
2024-05-06 07:30:17 +03:00
|
|
|
htracer *hookTracer
|
2024-12-19 13:21:32 +03:00
|
|
|
topic string
|
|
|
|
|
|
|
|
handler broker.Handler
|
|
|
|
done chan struct{}
|
|
|
|
kopts broker.Options
|
|
|
|
opts broker.SubscribeOptions
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
sync.RWMutex
|
2024-12-19 13:21:32 +03:00
|
|
|
closed bool
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) Client() *kgo.Client {
|
2024-05-22 17:49:36 +03:00
|
|
|
return s.c
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) Options() broker.SubscribeOptions {
|
2023-03-13 21:56:44 +03:00
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) Topic() string {
|
2023-03-13 21:56:44 +03:00
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) Unsubscribe(ctx context.Context) error {
|
2023-03-13 21:56:44 +03:00
|
|
|
if s.closed {
|
|
|
|
return nil
|
|
|
|
}
|
2024-12-19 13:21:32 +03:00
|
|
|
// select {
|
2023-05-25 15:11:53 +03:00
|
|
|
// case <-ctx.Done():
|
|
|
|
// return ctx.Err()
|
2024-12-19 13:21:32 +03:00
|
|
|
// default:
|
|
|
|
s.c.PauseFetchTopics(s.topic)
|
|
|
|
s.c.CloseAllowingRebalance()
|
|
|
|
kc := make(map[string][]int32)
|
|
|
|
for ctp := range s.consumers {
|
|
|
|
kc[ctp.t] = append(kc[ctp.t], ctp.p)
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
2024-12-19 13:21:32 +03:00
|
|
|
s.killConsumers(ctx, kc)
|
|
|
|
close(s.done)
|
|
|
|
s.closed = true
|
|
|
|
s.c.ResumeFetchTopics(s.topic)
|
|
|
|
// }
|
2023-03-13 21:56:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) poll(ctx context.Context) {
|
2023-03-13 21:56:44 +03:00
|
|
|
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 19:26:06 +03:00
|
|
|
s.Lock()
|
2024-05-02 23:01:04 +03:00
|
|
|
for p, l := range lmap {
|
|
|
|
s.kopts.Meter.Counter(semconv.BrokerGroupLag, "topic", s.topic, "group", s.opts.Group, "partition", strconv.Itoa(int(p)), "lag", strconv.Itoa(int(l.Lag)))
|
2024-04-13 02:24:16 +03:00
|
|
|
}
|
2024-04-19 19:26:06 +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) {
|
2024-10-12 12:45:53 +03:00
|
|
|
s.kopts.Logger.Fatal(ctx, fmt.Sprintf("[kgo] fetch topic %s partition %d error", t, p), err)
|
2023-03-13 21:56:44 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
fetches.EachPartition(func(p kgo.FetchTopicPartition) {
|
2024-12-19 13:21:32 +03:00
|
|
|
nTp := tp{p.Topic, p.Partition}
|
|
|
|
s.consumers[nTp].recs <- p
|
2023-03-13 21:56:44 +03:00
|
|
|
})
|
|
|
|
s.c.AllowRebalance()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) killConsumers(ctx context.Context, lost map[string][]int32) {
|
2023-03-13 21:56:44 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
|
|
|
|
|
|
|
for topic, partitions := range lost {
|
|
|
|
for _, partition := range partitions {
|
2024-12-19 13:21:32 +03:00
|
|
|
nTp := tp{topic, partition}
|
|
|
|
pc := s.consumers[nTp]
|
|
|
|
delete(s.consumers, nTp)
|
2023-03-13 21:56:44 +03:00
|
|
|
close(pc.quit)
|
2024-10-12 12:45:53 +03:00
|
|
|
if s.kopts.Logger.V(logger.DebugLevel) {
|
|
|
|
s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] waiting for work to finish topic %s partition %d", topic, partition))
|
|
|
|
}
|
2023-03-13 21:56:44 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func() { <-pc.done; wg.Done() }()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) lost(ctx context.Context, _ *kgo.Client, lost map[string][]int32) {
|
2024-10-12 12:45:53 +03:00
|
|
|
if s.kopts.Logger.V(logger.DebugLevel) {
|
|
|
|
s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] lost %#+v", lost))
|
|
|
|
}
|
2023-03-13 21:56:44 +03:00
|
|
|
s.killConsumers(ctx, lost)
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) revoked(ctx context.Context, c *kgo.Client, revoked map[string][]int32) {
|
2024-10-12 12:45:53 +03:00
|
|
|
if s.kopts.Logger.V(logger.DebugLevel) {
|
|
|
|
s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] revoked %#+v", revoked))
|
|
|
|
}
|
2023-03-13 21:56:44 +03:00
|
|
|
s.killConsumers(ctx, revoked)
|
|
|
|
if err := c.CommitMarkedOffsets(ctx); err != nil {
|
2024-10-12 12:45:53 +03:00
|
|
|
s.kopts.Logger.Error(ctx, "[kgo] revoked CommitMarkedOffsets error", err)
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
func (s *Subscriber) assigned(_ context.Context, c *kgo.Client, assigned map[string][]int32) {
|
2023-03-13 21:56:44 +03:00
|
|
|
for topic, partitions := range assigned {
|
|
|
|
for _, partition := range partitions {
|
|
|
|
pc := &consumer{
|
|
|
|
c: c,
|
|
|
|
topic: topic,
|
|
|
|
partition: partition,
|
2024-05-06 07:30:17 +03:00
|
|
|
htracer: s.htracer,
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
recs: make(chan kgo.FetchTopicPartition, 100),
|
|
|
|
handler: s.handler,
|
|
|
|
kopts: s.kopts,
|
|
|
|
opts: s.opts,
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
2024-04-19 19:26:06 +03:00
|
|
|
s.Lock()
|
2023-03-13 21:56:44 +03:00
|
|
|
s.consumers[tp{topic, partition}] = pc
|
2024-04-19 19:26:06 +03:00
|
|
|
s.Unlock()
|
2023-03-13 21:56:44 +03:00
|
|
|
go pc.consume()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *consumer) consume() {
|
|
|
|
defer close(pc.done)
|
2024-10-12 12:45:53 +03:00
|
|
|
if pc.kopts.Logger.V(logger.DebugLevel) {
|
|
|
|
pc.kopts.Logger.Debug(pc.kopts.Context, fmt.Sprintf("starting, topic %s partition %d", pc.topic, pc.partition))
|
|
|
|
defer pc.kopts.Logger.Debug(pc.kopts.Context, fmt.Sprintf("killing, topic %s partition %d", pc.topic, pc.partition))
|
|
|
|
}
|
2023-03-13 21:56:44 +03:00
|
|
|
|
|
|
|
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-05-06 07:30:17 +03:00
|
|
|
ctx, sp := pc.htracer.WithProcessSpan(record)
|
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))
|
2024-05-06 07:30:17 +03:00
|
|
|
p.ctx = ctx
|
2024-02-21 15:08:58 +03:00
|
|
|
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 {
|
2024-05-05 16:20:05 +03:00
|
|
|
sp.AddEvent("codec unmarshal start")
|
|
|
|
err := pc.kopts.Codec.Unmarshal(record.Value, p.msg)
|
|
|
|
sp.AddEvent("codec unmarshal stop")
|
|
|
|
if err != nil {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
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-10-12 12:45:53 +03:00
|
|
|
pc.kopts.Logger.Fatal(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 {
|
2024-10-12 12:45:53 +03:00
|
|
|
pc.kopts.Logger.Error(pc.kopts.Context, "[kgo]: unmarshal error", err)
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
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-10-12 12:45:53 +03:00
|
|
|
pc.kopts.Logger.Fatal(pc.kopts.Context, "[kgo] Unmarshal err not handled wtf?")
|
2024-05-05 20:20:34 +03:00
|
|
|
sp.Finish()
|
2023-03-13 21:56:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-05-05 16:20:05 +03:00
|
|
|
sp.AddEvent("handler start")
|
2023-03-13 21:56:44 +03:00
|
|
|
err := pc.handler(p)
|
2024-05-05 16:20:05 +03:00
|
|
|
sp.AddEvent("handler stop")
|
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-05-05 16:20:05 +03:00
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
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 {
|
2024-05-05 20:20:34 +03:00
|
|
|
sp.AddEvent("error handler start")
|
2023-03-13 21:56:44 +03:00
|
|
|
_ = eh(p)
|
2024-05-05 20:20:34 +03:00
|
|
|
sp.AddEvent("error handler stop")
|
2023-03-13 21:56:44 +03:00
|
|
|
} else {
|
|
|
|
if pc.kopts.Logger.V(logger.ErrorLevel) {
|
2024-10-12 12:45:53 +03:00
|
|
|
pc.kopts.Logger.Error(pc.kopts.Context, "[kgo]: subscriber error", err)
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
2024-10-12 12:45:53 +03:00
|
|
|
pc.kopts.Logger.Fatal(pc.kopts.Context, "[kgo] ErrLostMessage wtf?")
|
2024-05-05 20:20:34 +03:00
|
|
|
sp.SetStatus(tracer.SpanStatusError, "ErrLostMessage")
|
|
|
|
sp.Finish()
|
2023-03-13 21:56:44 +03:00
|
|
|
return
|
|
|
|
}
|
2024-05-05 20:20:34 +03:00
|
|
|
sp.Finish()
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|