/
group.go
158 lines (127 loc) · 3.63 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package kafeman
import (
"context"
"sync"
"github.com/Shopify/sarama"
"github.com/pkg/errors"
"github.com/worldbug/kafeman/internal/admin"
"github.com/worldbug/kafeman/internal/models"
"github.com/worldbug/kafeman/internal/sarama_config"
"github.com/worldbug/kafeman/internal/utils"
"github.com/segmentio/kafka-go"
)
func (k *kafeman) client() kafka.Client {
return kafka.Client{
Addr: kafka.TCP(k.config.GetCurrentCluster().Brokers...),
}
}
func (k *kafeman) GetGroupsList(ctx context.Context) ([]string, error) {
return admin.NewAdmin(k.config).GetGroupsList(ctx)
}
func (k *kafeman) DescribeGroups(ctx context.Context, groupList []string) ([]GroupInfo, error) {
cli := k.client()
batches := utils.BatchesFromSlice(groupList, 20)
describe := make([]GroupInfo, 0, len(groupList))
wg := &sync.WaitGroup{}
m := &sync.Mutex{}
for _, b := range batches {
wg.Add(1)
go func(batch []string, m *sync.Mutex, wg *sync.WaitGroup) {
defer wg.Done()
groups, err := cli.DescribeGroups(ctx, &kafka.DescribeGroupsRequest{
GroupIDs: batch,
})
if err != nil {
for _, name := range batch {
describe = append(describe, GroupInfo{
Name: name,
State: "Empty",
Consumers: 0,
})
}
return
}
m.Lock()
for _, g := range groups.Groups {
describe = append(describe, GroupInfo{
Name: g.GroupID,
State: g.GroupState,
Consumers: len(g.Members),
})
}
m.Unlock()
}(b, m, wg)
}
wg.Wait()
return describe, nil
}
type GroupInfo struct {
Name string
State string
Consumers int
}
func (k *kafeman) GetOffsetsForConsumer(ctx context.Context, group, topic string) (map[int]int64, error) {
cli := k.client()
offsets, err := cli.ConsumerOffsets(ctx, kafka.TopicAndGroup{
Topic: topic,
GroupId: group,
})
return offsets, err
}
func (k *kafeman) DescribeGroup(ctx context.Context, group string) models.Group {
return admin.NewAdmin(k.config).DescribeGroup(ctx, group)
}
func (k *kafeman) DeleteGroup(group string) error {
return admin.NewAdmin(k.config).DeleteGroup(group)
}
func (k *kafeman) SetGroupOffset(ctx context.Context, group, topic string, offsets []models.Offset) error {
config, err := sarama_config.GetSaramaFromConfig(k.config)
if err != nil {
return errors.Wrap(err, "Cant create sarama config")
}
client, err := sarama.NewClient(k.config.GetCurrentCluster().Brokers, config)
if err != nil {
return errors.Wrapf(err, "Error create client for group %s", group)
}
groupInfo := k.DescribeGroup(ctx, group)
_, ok := groupInfo.Offsets[topic]
if ok {
return errors.New("Group has active consumers on this topic")
}
cg, err := sarama.NewConsumerGroupFromClient(group, client)
if err != nil {
return errors.Wrap(err, "Cant create consumer group")
}
defer cg.Close()
err = cg.Consume(ctx, []string{topic}, newCommitConsumerGroup(offsets, topic))
if err != nil {
return errors.Wrap(err, "Cant consume")
}
return nil
}
func newCommitConsumerGroup(
offsets []models.Offset,
topic string,
) *commitConsumerGroup {
return &commitConsumerGroup{
offsets: offsets,
topic: topic,
}
}
type commitConsumerGroup struct {
offsets []models.Offset
topic string
}
func (c *commitConsumerGroup) Setup(session sarama.ConsumerGroupSession) error {
for _, o := range c.offsets {
session.MarkOffset(c.topic, o.Partition, o.Offset, "")
session.ResetOffset(c.topic, o.Partition, o.Offset, "")
}
return nil
}
func (c *commitConsumerGroup) Cleanup(sarama.ConsumerGroupSession) error {
return nil
}
func (c *commitConsumerGroup) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
return nil
}