Skip to content

Commit 5b5a485

Browse files
committed
Add config for controlling node ip resolution
1 parent e6411a5 commit 5b5a485

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

proxy/pkg/config/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type Config struct {
4747
OriginTlsClientCertPath string `split_words:"true" yaml:"origin_tls_client_cert_path"`
4848
OriginTlsClientKeyPath string `split_words:"true" yaml:"origin_tls_client_key_path"`
4949

50+
OriginPreferIpFromSystemLocal bool `default:"true" split_words:"true" yaml:"origin_prefer_ip_from_system_local"`
51+
5052
// Target bucket
5153

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

66+
TargetPreferIpFromSystemLocal bool `default:"true" split_words:"true" yaml:"target_prefer_ip_from_system_local"`
67+
6468
// Proxy bucket
6569

6670
ProxyListenAddress string `default:"localhost" split_words:"true" yaml:"proxy_listen_address"`

proxy/pkg/zdmproxy/controlconn.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type ControlConn struct {
5555
protocolEventSubscribers map[ProtocolEventObserver]interface{}
5656
authEnabled *atomic.Value
5757
metricsHandler *metrics.MetricHandler
58+
preferIpFromSystemLocal bool
5859
}
5960

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

6566
func NewControlConn(ctx context.Context, defaultPort int, connConfig ConnectionConfig,
6667
username string, password string, conf *config.Config, topologyConfig *common.TopologyConfig, proxyRand *rand.Rand,
67-
metricsHandler *metrics.MetricHandler) *ControlConn {
68+
metricsHandler *metrics.MetricHandler, preferIpFromSystemLocal bool) *ControlConn {
6869
authEnabled := &atomic.Value{}
6970
authEnabled.Store(true)
7071
return &ControlConn{
@@ -102,6 +103,7 @@ func NewControlConn(ctx context.Context, defaultPort int, connConfig ConnectionC
102103
protocolEventSubscribers: map[ProtocolEventObserver]interface{}{},
103104
authEnabled: authEnabled,
104105
metricsHandler: metricsHandler,
106+
preferIpFromSystemLocal: preferIpFromSystemLocal,
105107
}
106108
}
107109

@@ -445,6 +447,7 @@ func (cc *ControlConn) RefreshHosts(conn CqlConnection, ctx context.Context) ([]
445447
if partitionerExists {
446448
partitioner = partitionerColValue.AsNillableString()
447449
}
450+
448451
if partitioner != nil && !strings.Contains(*partitioner, "Murmur3Partitioner") && cc.topologyConfig.VirtualizationEnabled {
449452
if strings.Contains(*partitioner, "RandomPartitioner") {
450453
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) ([]
475478
}
476479
}
477480

478-
oldLocalhost, localHostExists := hostsById[localHost.HostId]
479-
if localHostExists {
480-
log.Warnf("Local host is also on the peers list: %v vs %v, ignoring the former one.", oldLocalhost, localHost)
481+
peersListLocalHost, peersListContainsLocalHost := hostsById[localHost.HostId]
482+
if cc.preferIpFromSystemLocal {
483+
if peersListContainsLocalHost {
484+
log.Warnf("Local host is also on the peers list: %v vs %v, ignoring the former one.", peersListLocalHost, localHost)
485+
}
486+
hostsById[localHost.HostId] = localHost
487+
} else if peersListContainsLocalHost {
488+
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)
489+
} else {
490+
log.Warnf("Local host is not on the peers list, it will be added: %v.", localHost)
491+
hostsById[localHost.HostId] = localHost
481492
}
482-
hostsById[localHost.HostId] = localHost
493+
483494
orderedLocalHosts := make([]*Host, 0, len(hostsById))
484495
for _, h := range hostsById {
485496
orderedLocalHosts = append(orderedLocalHosts, h)

proxy/pkg/zdmproxy/proxy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (p *ZdmProxy) initializeControlConnections(ctx context.Context) error {
249249

250250
originControlConn := NewControlConn(
251251
p.controlConnShutdownCtx, p.Conf.OriginPort, p.originConnectionConfig,
252-
p.Conf.OriginUsername, p.Conf.OriginPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler)
252+
p.Conf.OriginUsername, p.Conf.OriginPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler, p.Conf.OriginPreferIpFromSystemLocal)
253253

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

262262
targetControlConn := NewControlConn(
263263
p.controlConnShutdownCtx, p.Conf.TargetPort, p.targetConnectionConfig,
264-
p.Conf.TargetUsername, p.Conf.TargetPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler)
264+
p.Conf.TargetUsername, p.Conf.TargetPassword, p.Conf, topologyConfig, p.proxyRand, p.metricHandler, p.Conf.TargetPreferIpFromSystemLocal)
265265

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

0 commit comments

Comments
 (0)