Skip to content

Commit

Permalink
fix(cluster): remove duplicates when combining Host and KnownHosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Leszczynski authored and karol-kokoszka committed Aug 9, 2024
1 parent 634afe8 commit faf7f9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 2 additions & 4 deletions pkg/scyllaclient/client_scylla.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,8 @@ func (c *Client) CheckHostsChanged(ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
if len(cur) != len(c.config.Hosts) {
return true, err
}
return !strset.New(c.config.Hosts...).Has(cur...), nil
s := strset.New(c.config.Hosts...)
return s.Size() != len(cur) || !s.Has(cur...), nil
}

// hosts returns a list of all hosts in a cluster.
Expand Down
11 changes: 11 additions & 0 deletions pkg/service/cluster/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"

"github.com/pkg/errors"
"github.com/scylladb/go-set/strset"
"github.com/scylladb/scylla-manager/v3/pkg/service"
"github.com/scylladb/scylla-manager/v3/pkg/util/uuid"
"go.uber.org/multierr"
Expand Down Expand Up @@ -71,6 +72,16 @@ func (c *Cluster) Validate() error {
return service.ErrValidate(errors.Wrap(errs, "invalid cluster"))
}

// AllHosts returns KnownHosts with Host at the front and without duplicates.
func (c *Cluster) AllHosts() []string {
hostSet := strset.New(c.KnownHosts...)
if c.Host == "" {
return hostSet.List()
}
hostSet.Remove(c.Host)
return append([]string{c.Host}, hostSet.List()...)
}

// Filter filters Clusters.
type Filter struct {
Name string
Expand Down
10 changes: 2 additions & 8 deletions pkg/service/cluster/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ func (s *Service) clientConfig(c *Cluster) scyllaclient.Config {
config.Port = fmt.Sprint(c.Port)
}
config.AuthToken = c.AuthToken
config.Hosts = nil
if c.Host != "" {
config.Hosts = []string{c.Host}
}
config.Hosts = append(config.Hosts, c.KnownHosts...)
config.Hosts = c.AllHosts()
return config
}

Expand Down Expand Up @@ -467,9 +463,7 @@ func (s *Service) validateHostsConnectivity(ctx context.Context, c *Cluster) err
if err := s.loadKnownHosts(c); err != nil && !errors.Is(err, gocql.ErrNotFound) {
return errors.Wrap(err, "load known hosts")
}
if c.Host != "" {
c.KnownHosts = append([]string{c.Host}, c.KnownHosts...)
}
c.KnownHosts = c.AllHosts()

knownHosts, err := s.filterKnownHosts(ctx, c)
if err != nil {
Expand Down

0 comments on commit faf7f9e

Please sign in to comment.