Skip to content

add DCLocalRoundRobin host selection policy - #1956

Open
isopov wants to merge 1 commit into
apache:trunkfrom
isopov:dc-local-host-selection-policy
Open

add DCLocalRoundRobin host selection policy#1956
isopov wants to merge 1 commit into
apache:trunkfrom
isopov:dc-local-host-selection-policy

Conversation

@isopov

@isopov isopov commented Jun 8, 2026

Copy link
Copy Markdown

As requested in #956 we need an option to not fallback to remote datacenter in DCAware host selection policy.
Unfortunately we first try to connect to remote datacenter host and receive local hosts there. So the way with HostFilter does not work for us. It seems that such host selection policy should work for us...

@joao-r-reis

joao-r-reis commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Adding a whole new type for this seems unnecessary, we just need a new boolean field on the existing policy like localOnly.

Also, there's nothing in the integration test suite that verifies that the driver will be able to successfully connect and initialize with the scenario you're describing. Unfortunately this would require a cluster with two datacenters and the cluster we create for CI only has one datacenter. Maybe we could change the CI cluster to have two datacenters though.

@isopov

isopov commented Jun 22, 2026

Copy link
Copy Markdown
Author

In my app I finished not with fork of this driver, but with this custom selection policy:

func wrapForceDC(internal gocql.HostSelectionPolicy, dcLocal string) gocql.HostSelectionPolicy {
	return &forceDCPolicy{
		dc:       dcLocal,
		internal: internal,
	}
}

type forceDCPolicy struct {
	dc       string
	internal gocql.HostSelectionPolicy
}

func (f *forceDCPolicy) AddHost(host *gocql.HostInfo) {
	if f.IsLocal(host) {
		f.internal.AddHost(host)
	}
}

func (f *forceDCPolicy) RemoveHost(host *gocql.HostInfo) {
	if f.IsLocal(host) {
		f.internal.RemoveHost(host)
	}
}

func (f *forceDCPolicy) HostUp(host *gocql.HostInfo) {
	if f.IsLocal(host) {
		f.internal.HostUp(host)
	}
}

func (f *forceDCPolicy) HostDown(host *gocql.HostInfo) {
	if f.IsLocal(host) {
		f.internal.HostDown(host)
	}
}

func (f *forceDCPolicy) Init(s *gocql.Session) {
	f.internal.Init(s)
}
func (f *forceDCPolicy) KeyspaceChanged(e gocql.KeyspaceUpdateEvent) {
	f.internal.KeyspaceChanged(e)
}
func (f *forceDCPolicy) SetPartitioner(p string) {
	f.internal.SetPartitioner(p)
}

func (f *forceDCPolicy) IsLocal(host *gocql.HostInfo) bool {
	return host.DataCenter() == f.dc
}

func (f *forceDCPolicy) Pick(query gocql.ExecutableQuery) gocql.NextHost {
	return f.internal.Pick(query)
}

And use it like this:

wrapForceDC(gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy(), gocql.ShuffleReplicas()), dcLocal)

@isopov

isopov commented Jun 22, 2026

Copy link
Copy Markdown
Author

Also, there's nothing in the integration test suite that verifies that the driver will be able to successfully connect and initialize with the scenario you're describing

We already have code dealing with several datacenters and fallback to not preferred one and no tests for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants