Skip to content

Commit

Permalink
subcmd/apply: Support fail-safe approach (#185)
Browse files Browse the repository at this point in the history
Up until now, the `apply` command implemented a fail-fast
strategy. This means that an attempt to configure a batch
of topics (by passing a path to a folder contains a list
of topics configurations files) will be terminated upon
the first failure (w/o even trying to apply the configuration
for the next-in-line topics).
This commit introduces the fail-safe support:
It allows the user to run the `apply` command in a fail-safe
manner, which means that instead of failing upon the first
failure, we'll simply continue to the next topic configuration,
while aggregating all the errors along the way.
Eventually, if there were any errors, all of them will be shown.

Signed-off-by: shimon-armis <[email protected]>
  • Loading branch information
shimonturjeman authored Apr 5, 2024
1 parent 25a6f37 commit 4b41f0b
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions cmd/topicctl/subcmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type applyCmdConfig struct {
skipConfirm bool
ignoreFewerPartitionsError bool
sleepLoopDuration time.Duration
failFast bool

shared sharedOptions

Expand Down Expand Up @@ -112,6 +113,12 @@ func init() {
10*time.Second,
"Amount of time to wait between partition checks",
)
applyCmd.Flags().BoolVar(
&applyConfig.failFast,
"fail-fast",
true,
"Fail upon the first error encountered during apply process",
)

addSharedConfigOnlyFlags(applyCmd, &applyConfig.shared)
RootCmd.AddCommand(applyCmd)
Expand All @@ -132,6 +139,14 @@ func applyPreRun(cmd *cobra.Command, args []string) error {
return nil
}

func appendError(aggregatedErr error, err error) error {
if aggregatedErr == nil {
return err
}

return fmt.Errorf("%v\n%v", aggregatedErr, err)
}

func applyRun(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -145,6 +160,8 @@ func applyRun(cmd *cobra.Command, args []string) error {

// Keep a cache of the admin clients with the cluster config path as the key
adminClients := map[string]admin.Client{}
// Keep track of any errors that occur during the apply process
var errs error

defer func() {
for _, adminClient := range adminClients {
Expand All @@ -167,7 +184,10 @@ func applyRun(cmd *cobra.Command, args []string) error {
for _, match := range matches {
matchCount++
if err := applyTopic(ctx, match, adminClients); err != nil {
return err
if applyConfig.failFast {
return err
}
errs = appendError(errs, err)
}
}
}
Expand All @@ -176,7 +196,7 @@ func applyRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("No topic configs match the provided args (%+v)", args)
}

return nil
return errs
}

func applyTopic(
Expand Down

0 comments on commit 4b41f0b

Please sign in to comment.