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

Add config for controlling node ip resolution ordering #131

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions proxy/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Config struct {
OriginTlsClientCertPath string `split_words:"true" yaml:"origin_tls_client_cert_path"`
OriginTlsClientKeyPath string `split_words:"true" yaml:"origin_tls_client_key_path"`

OriginPreferIpFromSystemLocal bool `default:"true" split_words:"true" yaml:"origin_prefer_ip_from_system_local"`

// Target bucket

TargetContactPoints string `split_words:"true" yaml:"target_contact_points"`
Expand All @@ -61,6 +63,8 @@ type Config struct {
TargetTlsClientCertPath string `split_words:"true" yaml:"target_tls_client_cert_path"`
TargetTlsClientKeyPath string `split_words:"true" yaml:"target_tls_client_key_path"`

TargetPreferIpFromSystemLocal bool `default:"true" split_words:"true" yaml:"target_prefer_ip_from_system_local"`

// Proxy bucket

ProxyListenAddress string `default:"localhost" split_words:"true" yaml:"proxy_listen_address"`
Expand Down
21 changes: 16 additions & 5 deletions proxy/pkg/zdmproxy/controlconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ControlConn struct {
protocolEventSubscribers map[ProtocolEventObserver]interface{}
authEnabled *atomic.Value
metricsHandler *metrics.MetricHandler
preferIpFromSystemLocal bool
}

const ProxyVirtualRack = "rack0"
Expand All @@ -64,7 +65,7 @@ const ccReadTimeout = 10 * time.Second

func NewControlConn(ctx context.Context, defaultPort int, connConfig ConnectionConfig,
username string, password string, conf *config.Config, topologyConfig *common.TopologyConfig, proxyRand *rand.Rand,
metricsHandler *metrics.MetricHandler) *ControlConn {
metricsHandler *metrics.MetricHandler, preferIpFromSystemLocal bool) *ControlConn {
authEnabled := &atomic.Value{}
authEnabled.Store(true)
return &ControlConn{
Expand Down Expand Up @@ -102,6 +103,7 @@ func NewControlConn(ctx context.Context, defaultPort int, connConfig ConnectionC
protocolEventSubscribers: map[ProtocolEventObserver]interface{}{},
authEnabled: authEnabled,
metricsHandler: metricsHandler,
preferIpFromSystemLocal: preferIpFromSystemLocal,
}
}

Expand Down Expand Up @@ -445,6 +447,7 @@ func (cc *ControlConn) RefreshHosts(conn CqlConnection, ctx context.Context) ([]
if partitionerExists {
partitioner = partitionerColValue.AsNillableString()
}

if partitioner != nil && !strings.Contains(*partitioner, "Murmur3Partitioner") && cc.topologyConfig.VirtualizationEnabled {
if strings.Contains(*partitioner, "RandomPartitioner") {
log.Debugf("Cluster %v uses the Random partitioner, but the proxy will return Murmur3 to the client instead. This is the expected behaviour.", cc.connConfig.GetClusterType())
Expand Down Expand Up @@ -475,11 +478,19 @@ func (cc *ControlConn) RefreshHosts(conn CqlConnection, ctx context.Context) ([]
}
}

oldLocalhost, localHostExists := hostsById[localHost.HostId]
if localHostExists {
log.Warnf("Local host is also on the peers list: %v vs %v, ignoring the former one.", oldLocalhost, localHost)
peersListLocalHost, peersListContainsLocalHost := hostsById[localHost.HostId]
if cc.preferIpFromSystemLocal {
if peersListContainsLocalHost {
log.Warnf("Local host is also on the peers list, local host will be used as the source of truth: %v vs %v, ignoring the former one.", peersListLocalHost, localHost)
}
hostsById[localHost.HostId] = localHost
} else if peersListContainsLocalHost {
log.Warnf("Local host is also on the peers list, the peers list will be used as the source of truth: %v vs %v, ignoring the latter one.", peersListLocalHost, localHost)
} else {
log.Tracef("Local host is not on the peers list, it will be added: %v.", localHost)
hostsById[localHost.HostId] = localHost
}
hostsById[localHost.HostId] = localHost

orderedLocalHosts := make([]*Host, 0, len(hostsById))
for _, h := range hostsById {
orderedLocalHosts = append(orderedLocalHosts, h)
Expand Down
4 changes: 2 additions & 2 deletions proxy/pkg/zdmproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (p *ZdmProxy) initializeControlConnections(ctx context.Context) error {

originControlConn := NewControlConn(
p.controlConnShutdownCtx, p.Conf.OriginPort, p.originConnectionConfig,
p.Conf.OriginUsername, p.Conf.OriginPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler)
p.Conf.OriginUsername, p.Conf.OriginPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler, p.Conf.OriginPreferIpFromSystemLocal)

if err := originControlConn.Start(p.controlConnShutdownWg, ctx); err != nil {
return fmt.Errorf("failed to initialize origin control connection: %w", err)
Expand All @@ -261,7 +261,7 @@ func (p *ZdmProxy) initializeControlConnections(ctx context.Context) error {

targetControlConn := NewControlConn(
p.controlConnShutdownCtx, p.Conf.TargetPort, p.targetConnectionConfig,
p.Conf.TargetUsername, p.Conf.TargetPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler)
p.Conf.TargetUsername, p.Conf.TargetPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler, p.Conf.TargetPreferIpFromSystemLocal)

if err := targetControlConn.Start(p.controlConnShutdownWg, ctx); err != nil {
return fmt.Errorf("failed to initialize target control connection: %w", err)
Expand Down