Skip to content

Commit

Permalink
fix: handle virtual replicas (#168)
Browse files Browse the repository at this point in the history
Co-authored-by: Clément Denoix <[email protected]>
  • Loading branch information
kai687 and clemfromspace authored Jul 22, 2024
1 parent 0e3184a commit e922d0d
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions pkg/cmd/indices/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package delete

import (
"fmt"
"regexp"
"strings"

"github.com/MakeNowJust/heredoc"
Expand Down Expand Up @@ -66,7 +67,9 @@ func NewDeleteCmd(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co

if !confirm {
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("--confirm required when non-interactive shell is detected")
return cmdutil.FlagErrorf(
"--confirm required when non-interactive shell is detected",
)
}
opts.DoConfirm = true
}
Expand All @@ -80,7 +83,8 @@ func NewDeleteCmd(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
}

cmd.Flags().BoolVarP(&confirm, "confirm", "y", false, "skip confirmation prompt")
cmd.Flags().BoolVarP(&opts.IncludeReplicas, "includeReplicas", "r", false, "delete replica indices too")
cmd.Flags().
BoolVarP(&opts.IncludeReplicas, "includeReplicas", "r", false, "delete replica indices too")

return cmd
}
Expand Down Expand Up @@ -117,13 +121,17 @@ func runDeleteCmd(opts *DeleteOptions) error {

if opts.IncludeReplicas {
settings, err := index.GetSettings()

if err != nil {
return fmt.Errorf("can't get settings of index %q: %w", indexName, err)
}

replicas := settings.Replicas
for _, replicaName := range replicas.Get() {
pattern := regexp.MustCompile(`^virtual\((.*)\)$`)
matches := pattern.FindStringSubmatch(replicaName)
if len(matches) > 1 {
replicaName = matches[1]
}
replica := client.InitIndex(replicaName)
indices = append(indices, replica)
}
Expand Down Expand Up @@ -151,7 +159,9 @@ func runDeleteCmd(opts *DeleteOptions) error {
}

if err != nil {
opts.IO.StartProgressIndicatorWithLabel(fmt.Sprint("Deleting replica index ", index.GetName()))
opts.IO.StartProgressIndicatorWithLabel(
fmt.Sprint("Deleting replica index ", index.GetName()),
)
err := deleteReplicaIndex(client, index)
opts.IO.StopProgressIndicator()
if err != nil {
Expand All @@ -162,7 +172,12 @@ func runDeleteCmd(opts *DeleteOptions) error {

cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s Deleted indices %s\n", cs.SuccessIcon(), strings.Join(opts.Indices, ", "))
fmt.Fprintf(
opts.IO.Out,
"%s Deleted indices %s\n",
cs.SuccessIcon(),
strings.Join(opts.Indices, ", "),
)
}

return nil
Expand All @@ -178,7 +193,12 @@ func deleteReplicaIndex(client *search.Client, replicaIndex *search.Index) error

err = detachReplicaIndex(replicaName, primaryName, client)
if err != nil {
return fmt.Errorf("can't unlink replica index %s from primary index %s: %w", replicaName, primaryName, err)
return fmt.Errorf(
"can't unlink replica index %s from primary index %s: %w",
replicaName,
primaryName,
err,
)
}

_, err = replicaIndex.Delete()
Expand All @@ -193,7 +213,6 @@ func deleteReplicaIndex(client *search.Client, replicaIndex *search.Index) error
func findPrimaryIndex(replicaIndex *search.Index) (string, error) {
replicaName := replicaIndex.GetName()
settings, err := replicaIndex.GetSettings()

if err != nil {
return "", fmt.Errorf("can't get settings of replica index %q: %w", replicaName, err)
}
Expand All @@ -210,12 +229,15 @@ func findPrimaryIndex(replicaIndex *search.Index) (string, error) {
func detachReplicaIndex(replicaName string, primaryName string, client *search.Client) error {
primaryIndex := client.InitIndex(primaryName)
settings, err := primaryIndex.GetSettings()

if err != nil {
return fmt.Errorf("can't get settings of primary index %q: %w", primaryName, err)
}

replicas := settings.Replicas.Get()
isVirtual := isVirtualReplica(replicas, replicaName)
if isVirtual {
replicaName = fmt.Sprintf("virtual(%s)", replicaName)
}
indexOfReplica := findIndex(replicas, replicaName)

// Delete the replica at position `indexOfReplica` from the array
Expand All @@ -226,7 +248,6 @@ func detachReplicaIndex(replicaName string, primaryName string, client *search.C
Replicas: opt.Replicas(replicas...),
},
)

if err != nil {
return fmt.Errorf("can't update settings of index %q: %w", primaryName, err)
}
Expand All @@ -245,3 +266,16 @@ func findIndex(arr []string, target string) int {
}
return -1
}

func isVirtualReplica(replicas []string, replicaName string) bool {
pattern := regexp.MustCompile(fmt.Sprintf(`^virtual\(%s\)$`, replicaName))

for _, i := range replicas {
matches := pattern.MatchString(i)
if matches {
return true
}
}

return false
}

0 comments on commit e922d0d

Please sign in to comment.