1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
6
"fmt"
6
7
"os"
7
8
"sort"
8
- "time"
9
9
"unicode"
10
10
11
11
"text/tabwriter"
@@ -19,6 +19,10 @@ import (
19
19
"github.com/Shopify/sarama"
20
20
"github.com/spf13/cobra"
21
21
22
+ "strconv"
23
+
24
+ "time"
25
+
22
26
"github.com/birdayz/kaf"
23
27
)
24
28
@@ -112,86 +116,59 @@ func (r *resetHandler) ConsumeClaim(s sarama.ConsumerGroupSession, c sarama.Cons
112
116
113
117
func createGroupCommitOffsetCmd () * cobra.Command {
114
118
var topic string
115
- var offset int64
119
+ var offset string
116
120
var partition int32
117
121
res := & cobra.Command {
118
122
Use : "commit" ,
119
123
Args : cobra .ExactArgs (1 ),
120
124
Run : func (cmd * cobra.Command , args []string ) {
121
125
client := getClient ()
122
- config := getConfig ()
123
- admin := getClusterAdmin ()
124
126
125
127
group := args [0 ]
126
128
127
- var memberID string
129
+ var off int64
128
130
129
- var b * sarama.Broker
130
-
131
- de , err := admin .DescribeConsumerGroups ([]string {group })
131
+ i , err := strconv .ParseInt (offset , 10 , 64 )
132
132
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
159
139
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
162
145
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
168
149
}
169
- commit .AddBlock (topic , partition , offset , 0 , "reset by kaf" )
170
150
171
- commitResp , err := b . CommitOffset ( commit )
151
+ g , err := sarama . NewConsumerGroupFromClient ( group , client )
172
152
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 )
182
154
}
183
155
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
+ })
184
163
if err != nil {
185
- errorExit ("Failed to set offset: %v" , err )
164
+ errorExit ("Failed to commit offset: %v" , err )
186
165
}
187
166
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 )
191
168
},
192
169
}
193
170
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" )
195
172
res .Flags ().Int32VarP (& partition , "partition" , "p" , 0 , "partition" )
196
173
return res
197
174
}
0 commit comments