Skip to content

Commit

Permalink
Allow to omit the RAFT port (zalando#1759)
Browse files Browse the repository at this point in the history
If no ports are specified for self_addr, bind_addr and/or partner_addrs, then
Patroni will self-assign the startup API port + 10000 as RAFT port.
  • Loading branch information
mbanck-ntap committed Dec 31, 2021
1 parent a015e0e commit 152dc89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/SETTINGS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ Raft

A: Yes, on the third node you can run ``patroni_raft_controller`` (without Patroni and PostgreSQL). In such a setup, one can temporarily lose one node without affecting the primary.

- Q: Do I need to specify a port?

A: No, if you omit the port, Patroni will prepend the startup API port with 1 (i.e. add 10000 to it).

.. _postgresql_settings:

Expand Down
2 changes: 1 addition & 1 deletion patroni/dcs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_dcs(config):
if key.lower() == name and inspect.isclass(item) and issubclass(item, AbstractDCS):
# propagate some parameters
config[name].update({p: config[p] for p in ('namespace', 'name', 'scope', 'loop_wait',
'patronictl', 'ttl', 'retry_timeout') if p in config})
'patronictl', 'ttl', 'retry_timeout', 'restapi') if p in config})
return item(config[name])
except ImportError:
logger.debug('Failed to import %s', module_name)
Expand Down
16 changes: 16 additions & 0 deletions patroni/dcs/raft.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ def __init__(self, config):
super(Raft, self).__init__(config)
self._ttl = int(config.get('ttl') or 30)

# Set a default port if no ports were specified in config
self._default_port(config)

ready_event = threading.Event()
self._sync_obj = KVStoreTTL(ready_event.set, self._on_set, self._on_delete, commandsWaitLeader=False, **config)
self._sync_obj.startAutoTick()
Expand All @@ -277,6 +280,19 @@ def __init__(self, config):
logger.info('waiting on raft')
self.set_retry_timeout(int(config.get('retry_timeout') or 10))

def _default_port(self, config):
# Prepend '1' to the API port, i.e. use '18008' if the API port is '8008'
port = '1' + config['restapi']['listen'].rsplit(':', 1)[1]
n = 0
for addr in config['partner_addrs']:
if ":" not in addr:
config['partner_addrs'][n] = addr + ':' + port
n += 1
if ":" not in config['self_addr']:
config['self_addr'] = config['self_addr'] + ':' + port
if ":" not in config['bind_addr']:
config['bind_addr'] = config['bind_addr'] + ':' + port

def _on_set(self, key, value):
leader = (self._sync_obj.get(self.leader_path) or {}).get('value')
if key == value['created'] == value['updated'] and \
Expand Down

0 comments on commit 152dc89

Please sign in to comment.