From 5b5a485c735c54d3221cd94b97be516b9fd0a053 Mon Sep 17 00:00:00 2001 From: mini-ninja-64 Date: Thu, 3 Oct 2024 12:04:10 +0100 Subject: [PATCH 1/2] Add config for controlling node ip resolution --- proxy/pkg/config/config.go | 4 ++++ proxy/pkg/zdmproxy/controlconn.go | 21 ++++++++++++++++----- proxy/pkg/zdmproxy/proxy.go | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/proxy/pkg/config/config.go b/proxy/pkg/config/config.go index cc62842..f3197ae 100644 --- a/proxy/pkg/config/config.go +++ b/proxy/pkg/config/config.go @@ -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"` @@ -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"` diff --git a/proxy/pkg/zdmproxy/controlconn.go b/proxy/pkg/zdmproxy/controlconn.go index 17fd804..b0659e1 100644 --- a/proxy/pkg/zdmproxy/controlconn.go +++ b/proxy/pkg/zdmproxy/controlconn.go @@ -55,6 +55,7 @@ type ControlConn struct { protocolEventSubscribers map[ProtocolEventObserver]interface{} authEnabled *atomic.Value metricsHandler *metrics.MetricHandler + preferIpFromSystemLocal bool } const ProxyVirtualRack = "rack0" @@ -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{ @@ -102,6 +103,7 @@ func NewControlConn(ctx context.Context, defaultPort int, connConfig ConnectionC protocolEventSubscribers: map[ProtocolEventObserver]interface{}{}, authEnabled: authEnabled, metricsHandler: metricsHandler, + preferIpFromSystemLocal: preferIpFromSystemLocal, } } @@ -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()) @@ -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: %v vs %v, ignoring the former one.", peersListLocalHost, localHost) + } + hostsById[localHost.HostId] = localHost + } else if peersListContainsLocalHost { + log.Infof("Local host is on the peers list aswell, the peers list will be used as the source of truth: %v vs %v, ignoring the latter one.", peersListLocalHost, localHost) + } else { + log.Warnf("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) diff --git a/proxy/pkg/zdmproxy/proxy.go b/proxy/pkg/zdmproxy/proxy.go index 027d698..c22a240 100644 --- a/proxy/pkg/zdmproxy/proxy.go +++ b/proxy/pkg/zdmproxy/proxy.go @@ -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) @@ -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) From a830f37c823cdec1c447b07f384704ca53429416 Mon Sep 17 00:00:00 2001 From: mini-ninja-64 Date: Wed, 6 Nov 2024 11:48:16 +0000 Subject: [PATCH 2/2] Standardize logging --- proxy/pkg/zdmproxy/controlconn.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/proxy/pkg/zdmproxy/controlconn.go b/proxy/pkg/zdmproxy/controlconn.go index b0659e1..9d42d00 100644 --- a/proxy/pkg/zdmproxy/controlconn.go +++ b/proxy/pkg/zdmproxy/controlconn.go @@ -481,13 +481,13 @@ func (cc *ControlConn) RefreshHosts(conn CqlConnection, ctx context.Context) ([] peersListLocalHost, peersListContainsLocalHost := hostsById[localHost.HostId] if cc.preferIpFromSystemLocal { if peersListContainsLocalHost { - log.Warnf("Local host is also on the peers list: %v vs %v, ignoring the former one.", peersListLocalHost, localHost) + 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.Infof("Local host is on the peers list aswell, the peers list will be used as the source of truth: %v vs %v, ignoring the latter one.", peersListLocalHost, localHost) + 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.Warnf("Local host is not on the peers list, it will be added: %v.", localHost) + log.Tracef("Local host is not on the peers list, it will be added: %v.", localHost) hostsById[localHost.HostId] = localHost }