Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle deleting virtual replicas #168

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading