Skip to content

Commit 16fac63

Browse files
Add host-id to AddressTranslator
1 parent b4e4b59 commit 16fac63

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

cassandra/connection.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,19 @@ def create(self, row):
244244
if port is None:
245245
port = self.port if self.port else 9042
246246

247+
# Extract host metadata for V2 address translation
248+
host_id = row.get("host_id")
249+
250+
# Use V2 API if available, fall back to V1
251+
translator = self.cluster.address_translator
252+
if hasattr(translator, 'translate_with_host_id'):
253+
translated_addr = translator.translate_with_host_id(addr, host_id)
254+
else:
255+
translated_addr = translator.translate(addr)
256+
247257
# create the endpoint with the translated address
248258
# TODO next major, create a TranslatedEndPoint type
249-
return DefaultEndPoint(
250-
self.cluster.address_translator.translate(addr),
251-
port)
259+
return DefaultEndPoint(translated_addr, port)
252260

253261

254262
@total_ordering

cassandra/policies.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,9 +1210,27 @@ class AddressTranslator(object):
12101210
def translate(self, addr):
12111211
"""
12121212
Accepts the node ip address, and returns a translated address to be used connecting to this node.
1213+
1214+
Legacy V1 API for backward compatibility. New implementations should override translate_with_host.
12131215
"""
12141216
raise NotImplementedError()
12151217

1218+
def translate_with_host_id(self, addr, host_id):
1219+
"""
1220+
V2 API: Accepts the node ip address and optional Host ID, returns translated address.
1221+
1222+
:param addr: The node IP address to translate
1223+
:param host_id: Host ID
1224+
:return: Translated address to be used for connecting
1225+
1226+
This method provides access to Host metadata (especially host_id) which is required
1227+
for PrivateLink and similar scenarios where translation is keyed by Host ID rather
1228+
than IP address.
1229+
1230+
Default implementation delegates to translate() for backward compatibility.
1231+
"""
1232+
return self.translate(addr)
1233+
12161234

12171235
class IdentityTranslator(AddressTranslator):
12181236
"""
@@ -1221,6 +1239,9 @@ class IdentityTranslator(AddressTranslator):
12211239
def translate(self, addr):
12221240
return addr
12231241

1242+
def translate_with_host_id(self, addr, host_id):
1243+
return addr
1244+
12241245

12251246
class EC2MultiRegionTranslator(AddressTranslator):
12261247
"""

tests/unit/test_policies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ def test_identity_translator(self):
14421442
def test_ec2_multi_region_translator(self, *_):
14431443
ec2t = EC2MultiRegionTranslator()
14441444
addr = '127.0.0.1'
1445-
translated = ec2t.translate(addr)
1445+
translated = ec2t.translate_with_host_id(addr, "")
14461446
assert translated is not addr # verifies that the resolver path is followed
14471447
assert translated == addr # and that it resolves to the same address
14481448

0 commit comments

Comments
 (0)