Skip to content

Commit dd3750f

Browse files
committed
fix group commit & introduce date resets
1 parent 207b685 commit dd3750f

File tree

1 file changed

+34
-57
lines changed

1 file changed

+34
-57
lines changed

cmd/kaf/group.go

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
78
"sort"
8-
"time"
99
"unicode"
1010

1111
"text/tabwriter"
@@ -19,6 +19,10 @@ import (
1919
"github.com/Shopify/sarama"
2020
"github.com/spf13/cobra"
2121

22+
"strconv"
23+
24+
"time"
25+
2226
"github.com/birdayz/kaf"
2327
)
2428

@@ -112,86 +116,59 @@ func (r *resetHandler) ConsumeClaim(s sarama.ConsumerGroupSession, c sarama.Cons
112116

113117
func createGroupCommitOffsetCmd() *cobra.Command {
114118
var topic string
115-
var offset int64
119+
var offset string
116120
var partition int32
117121
res := &cobra.Command{
118122
Use: "commit",
119123
Args: cobra.ExactArgs(1),
120124
Run: func(cmd *cobra.Command, args []string) {
121125
client := getClient()
122-
config := getConfig()
123-
admin := getClusterAdmin()
124126

125127
group := args[0]
126128

127-
var memberID string
129+
var off int64
128130

129-
var b *sarama.Broker
130-
131-
de, err := admin.DescribeConsumerGroups([]string{group})
131+
i, err := strconv.ParseInt(offset, 10, 64)
132132
if err != nil {
133-
errorExit("Failed to fetch consumer group details: %v", err)
134-
}
135-
136-
rr := &sarama.JoinGroupRequest{
137-
GroupId: group,
138-
MemberId: "",
139-
SessionTimeout: int32(time.Second * 20 / time.Millisecond),
140-
ProtocolType: de[0].ProtocolType,
141-
}
142-
143-
rr.AddGroupProtocol("", []byte{})
144-
145-
b, err = client.Coordinator(group)
146-
if err != nil {
147-
errorExit("Failed to find coordinator for group: %v", err)
148-
}
149-
_ = b.Open(config)
150-
151-
resp, err := b.JoinGroup(rr)
152-
if err != nil {
153-
errorExit("Failed to join group: %v", err)
154-
}
155-
156-
if resp.Err != sarama.ErrNoError {
157-
errorExit("Failed to join group: %v", resp.Err)
158-
}
133+
// No int -> try timestamp
134+
t, err := time.Parse(time.RFC3339, offset)
135+
if err != nil {
136+
errorExit("offset is neither offset nor timestamp", nil)
137+
}
138+
_ = t
159139

160-
generationID := resp.GenerationId
161-
memberID = resp.MemberId
140+
o, err := client.GetOffset(topic, partition, t.UnixNano()/(int64(time.Millisecond)/int64(time.Nanosecond)))
141+
if err != nil {
142+
errorExit("Failed to determine offset for timestamp: %v", err)
143+
}
144+
off = o
162145

163-
commit := &sarama.OffsetCommitRequest{
164-
Version: 1,
165-
ConsumerGroup: group,
166-
ConsumerGroupGeneration: generationID,
167-
ConsumerID: memberID,
146+
fmt.Printf("Determined offset %v from timestamp.\n", off)
147+
} else {
148+
off = i
168149
}
169-
commit.AddBlock(topic, partition, offset, 0, "reset by kaf")
170150

171-
commitResp, err := b.CommitOffset(commit)
151+
g, err := sarama.NewConsumerGroupFromClient(group, client)
172152
if err != nil {
173-
errorExit("Failed to commit: %v", err)
174-
}
175-
176-
for _, partitionErrors := range commitResp.Errors {
177-
for _, _error := range partitionErrors {
178-
if _error == sarama.ErrUnknownMemberId {
179-
errorExit("Failed to commit: %v", _error)
180-
}
181-
}
153+
errorExit("Failed to create consumer group: %v", err)
182154
}
183155

156+
err = g.Consume(context.Background(), []string{topic}, &resetHandler{
157+
topic: topic,
158+
partition: partition,
159+
offset: off,
160+
client: client,
161+
group: group,
162+
})
184163
if err != nil {
185-
errorExit("Failed to set offset: %v", err)
164+
errorExit("Failed to commit offset: %v", err)
186165
}
187166

188-
b.LeaveGroup(&sarama.LeaveGroupRequest{GroupId: group, MemberId: memberID})
189-
190-
fmt.Printf("Set offset to %v.", offset)
167+
fmt.Printf("Set offset to %v.", off)
191168
},
192169
}
193170
res.Flags().StringVarP(&topic, "topic", "t", "", "topic")
194-
res.Flags().Int64VarP(&offset, "offset", "o", 0, "offset to commit")
171+
res.Flags().StringVarP(&offset, "offset", "o", "", "offset to commit")
195172
res.Flags().Int32VarP(&partition, "partition", "p", 0, "partition")
196173
return res
197174
}

0 commit comments

Comments
 (0)