diff --git a/docs/SETTINGS.rst b/docs/SETTINGS.rst index 7433d43888..ba504b3315 100644 --- a/docs/SETTINGS.rst +++ b/docs/SETTINGS.rst @@ -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: diff --git a/patroni/dcs/__init__.py b/patroni/dcs/__init__.py index cdb4854e3f..608e4c2c28 100644 --- a/patroni/dcs/__init__.py +++ b/patroni/dcs/__init__.py @@ -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) diff --git a/patroni/dcs/raft.py b/patroni/dcs/raft.py index fed6d5df38..9e67aba273 100644 --- a/patroni/dcs/raft.py +++ b/patroni/dcs/raft.py @@ -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() @@ -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 \