forked from segmentio/topicctl
/
reset.go
182 lines (158 loc) · 5.39 KB
/
reset.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package subcmd
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/phenixrizen/topicctl/pkg/apply"
"github.com/phenixrizen/topicctl/pkg/cli"
"github.com/phenixrizen/topicctl/pkg/groups"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var resetOffsetsCmd = &cobra.Command{
Use: "reset-offsets [topic name] [group name]",
Short: "reset consumer group offsets",
Args: cobra.MinimumNArgs(2),
PreRunE: resetOffsetsPreRun,
RunE: resetOffsetsRun,
}
type resetOffsetsCmdConfig struct {
offset int64
partitions []int
partitionOffsetMap map[string]int64
toEarliest bool
toLatest bool
shared sharedOptions
}
var resetOffsetsConfig resetOffsetsCmdConfig
func init() {
resetOffsetsCmd.Flags().Int64Var(
&resetOffsetsConfig.offset,
"offset",
-2,
"Desired offset for the target partitions",
)
resetOffsetsCmd.Flags().IntSliceVar(
&resetOffsetsConfig.partitions,
"partitions",
[]int{},
"List of partitions to reset e.g. 1,2,3,.. (defaults to all)",
)
resetOffsetsCmd.Flags().StringToInt64Var(
&resetOffsetsConfig.partitionOffsetMap,
"partition-offset-map",
map[string]int64{},
"Map of partition IDs to their corresponding desired offsets e.g. 1=5,2=10,3=12,...",
)
resetOffsetsCmd.Flags().BoolVar(
&resetOffsetsConfig.toEarliest,
"to-earliest",
false,
"Resets offsets of consumer group members to earliest offsets of partitions")
resetOffsetsCmd.Flags().BoolVar(
&resetOffsetsConfig.toLatest,
"to-latest",
false,
"Resets offsets of consumer group members to latest offsets of partitions")
addSharedFlags(resetOffsetsCmd, &resetOffsetsConfig.shared)
RootCmd.AddCommand(resetOffsetsCmd)
}
func resetOffsetsPreRun(cmd *cobra.Command, args []string) error {
resetOffsetSpecification := "You must choose only one of the following reset-offset specifications: --to-earliest, --to-latest, --offset."
offsetMapSpecification := "--partition-offset-map option cannot be coupled with any of the following options: --partitions, --to-earliest, --to-latest, --offset."
if len(resetOffsetsConfig.partitionOffsetMap) > 0 && (cmd.Flags().Changed("offset") ||
len(resetOffsetsConfig.partitions) > 0 ||
resetOffsetsConfig.toEarliest ||
resetOffsetsConfig.toLatest) {
return errors.New(offsetMapSpecification)
} else if resetOffsetsConfig.toEarliest && resetOffsetsConfig.toLatest {
return errors.New(resetOffsetSpecification)
} else if cmd.Flags().Changed("offset") && (resetOffsetsConfig.toEarliest || resetOffsetsConfig.toLatest) {
return errors.New(resetOffsetSpecification)
}
return resetOffsetsConfig.shared.validate()
}
func resetOffsetsRun(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
adminClient, err := resetOffsetsConfig.shared.getAdminClient(ctx, nil, true)
if err != nil {
return err
}
defer adminClient.Close()
topic := args[0]
group := args[1]
topicInfo, err := adminClient.GetTopic(ctx, topic, false)
if err != nil {
return err
}
partitionIDsMap := map[int]struct{}{}
for _, partitionInfo := range topicInfo.Partitions {
partitionIDsMap[partitionInfo.ID] = struct{}{}
}
var resetOffsetsStrategy string
if resetOffsetsConfig.toLatest {
resetOffsetsStrategy = groups.LatestResetOffsetsStrategy
} else if resetOffsetsConfig.toEarliest {
resetOffsetsStrategy = groups.EarliestResetOffsetsStrategy
}
partitionOffsets := map[int]int64{}
if len(resetOffsetsConfig.partitionOffsetMap) > 0 {
for partition, offset := range resetOffsetsConfig.partitionOffsetMap {
var partitionID int
if partitionID, err = strconv.Atoi(partition); err != nil {
return fmt.Errorf("Partition value %s must be a number", partition)
}
if _, ok := partitionIDsMap[partitionID]; !ok {
return fmt.Errorf("Partition %d not found in topic %s", partitionID, topic)
}
partitionOffsets[partitionID] = offset
}
} else if len(resetOffsetsConfig.partitions) > 0 {
for _, partition := range resetOffsetsConfig.partitions {
if _, ok := partitionIDsMap[partition]; !ok {
return fmt.Errorf("Partition %d not found in topic %s", partition, topic)
}
if resetOffsetsConfig.toEarliest || resetOffsetsConfig.toLatest {
partitionOffsets[partition], err = groups.GetEarliestOrLatestOffset(ctx, adminClient.GetConnector(), topic, resetOffsetsStrategy, partition)
if err != nil {
return err
}
} else {
partitionOffsets[partition] = resetOffsetsConfig.offset
}
}
} else {
for _, partitionInfo := range topicInfo.Partitions {
if resetOffsetsConfig.toEarliest || resetOffsetsConfig.toLatest {
partitionOffsets[partitionInfo.ID], err = groups.GetEarliestOrLatestOffset(ctx, adminClient.GetConnector(), topic, resetOffsetsStrategy, partitionInfo.ID)
if err != nil {
return err
}
} else {
partitionOffsets[partitionInfo.ID] = resetOffsetsConfig.offset
}
}
}
log.Infof(
"This will reset the offsets for the following partitions in topic %s for group %s:\n%s",
topic,
group,
groups.FormatPartitionOffsets(partitionOffsets),
)
log.Info(
"Please ensure that all other consumers are stopped, otherwise the reset might be overridden.",
)
ok, _ := apply.Confirm("OK to continue?", false)
if !ok {
return errors.New("Stopping because of user response")
}
cliRunner := cli.NewCLIRunner(adminClient, log.Infof, !noSpinner)
return cliRunner.ResetOffsets(
ctx,
topic,
group,
partitionOffsets,
)
}