Skip to content

Commit 0804b93

Browse files
author
avandras
committed
Simplify site switchover name
1 parent 412d0a6 commit 0804b93

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

patroni/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ def do_POST_switchover(self) -> None:
11931193
self.do_POST_failover(action='switchover')
11941194

11951195
@check_access
1196-
def do_POST_multisite_switchover(self):
1196+
def do_POST_site_switchover(self):
11971197
request = self._read_json_content()
11981198
(status_code, data) = (400, '')
11991199
if not request:

patroni/ctl.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
from .config import Config
6565
from .dcs import AbstractDCS, Cluster, get_dcs as _get_dcs, Member
6666
from .exceptions import PatroniException
67+
from .multisite import MultisiteController
6768
from .postgresql.misc import postgres_version_to_int
6869
from .postgresql.mpp import get_mpp
6970
from .request import PatroniRequest
@@ -343,7 +344,7 @@ def is_citus_cluster() -> bool:
343344
__dcs_cache: Dict[Tuple[str, Optional[int]], AbstractDCS] = {}
344345

345346

346-
def get_dcs(scope: str, group: Optional[int]) -> AbstractDCS:
347+
def get_dcs(scope: str, group: Optional[int], multisite: Optional[bool] = False) -> AbstractDCS:
347348
"""Get the DCS object.
348349
349350
:param scope: cluster name.
@@ -358,13 +359,20 @@ def get_dcs(scope: str, group: Optional[int]) -> AbstractDCS:
358359
"""
359360
if (scope, group) in __dcs_cache:
360361
return __dcs_cache[(scope, group)]
362+
361363
config = _get_configuration()
362364
config.update({'scope': scope, 'patronictl': True})
363365
if group is not None:
364366
config['citus'] = {'group': group, 'database': 'postgres'}
365367
config.setdefault('name', scope)
368+
366369
try:
367370
dcs = _get_dcs(config)
371+
# TODO: might be necessary for site switchover candidates collection
372+
# if multisite:
373+
# _, dcs = MultisiteController.get_dcs_config(config)
374+
# else:
375+
# dcs = _get_dcs(config)
368376
if is_citus_cluster() and group is None:
369377
dcs.is_mpp_coordinator = lambda: True
370378
click.get_current_context().obj['__mpp'] = dcs.mpp
@@ -1376,7 +1384,7 @@ def _do_failover_or_switchover(action: str, cluster_name: str, group: Optional[i
13761384
output_members(cluster, cluster_name, group=group)
13771385

13781386

1379-
def _do_multisite_switchover(cluster_name: str, group: Optional[int],
1387+
def _do_site_switchover(cluster_name: str, group: Optional[int],
13801388
switchover_leader: Optional[str], candidate: Optional[str],
13811389
force: bool, scheduled: Optional[str] = None) -> None:
13821390
"""Perform a site switchover operation in the cluster.
@@ -1442,13 +1450,17 @@ def _do_multisite_switchover(cluster_name: str, group: Optional[int],
14421450
if leader_site != switchover_leader:
14431451
raise PatroniCtlException(f'Site {switchover_leader} is not the leader of cluster {cluster_name}')
14441452

1453+
# multisite_dcs = get_dcs(cluster_name, group, True)
1454+
# multisite_cluster = multisite_dcs.get_cluster()
1455+
14451456
candidate_names = [str(m.multisite['name']) for m in cluster.members
14461457
if m.multisite and m.multisite['name'] != leader_site]
14471458
# We sort the names for consistent output to the client
14481459
candidate_names.sort()
14491460

1450-
if not candidate_names:
1451-
raise PatroniCtlException('No candidates found to switch over to')
1461+
# TODO: once there is a reliable way for getting the candidate sites when on the leader site, turn this back on
1462+
# if not candidate_names:
1463+
# raise PatroniCtlException('No candidates found to switch over to')
14521464

14531465
if candidate is None and not force:
14541466
candidate = click.prompt('Candidate ' + str(candidate_names), type=str, default='')
@@ -1494,11 +1506,11 @@ def _do_multisite_switchover(cluster_name: str, group: Optional[int],
14941506

14951507
r = None
14961508
try:
1497-
# We would already have throw an exception if there was no leader
1509+
# We would already have thrown an exception if there was no leader
14981510
member = cluster.leader.member if cluster.leader else candidate and cluster.get_member(candidate, False)
14991511
if TYPE_CHECKING: # pragma: no cover
15001512
assert isinstance(member, Member)
1501-
r = request_patroni(member, 'post', 'multisite_switchover', switchover_value)
1513+
r = request_patroni(member, 'post', 'site_switchover', switchover_value)
15021514

15031515
if r.status in (200, 202):
15041516
logging.debug(r)
@@ -1568,22 +1580,22 @@ def switchover(cluster_name: str, group: Optional[int], leader: Optional[str],
15681580
_do_failover_or_switchover('switchover', cluster_name, group, candidate, force, leader, scheduled)
15691581

15701582

1571-
@ctl.command('multisite-switchover', help='Switchover to another data centre')
1583+
@ctl.command('site-switchover', help='Switchover to another data centre')
15721584
@arg_cluster_name
15731585
@option_citus_group
15741586
@click.option('--leader-site', '--primary-site', 'leader_site', help='The name of the current leader site', default=None)
15751587
@click.option('--candidate-site', 'candidate_site', help='The name of the candidate', default=None)
15761588
@click.option('--scheduled', help='Timestamp of a scheduled switchover in unambiguous format (e.g. ISO 8601)',
15771589
default=None)
15781590
@option_force
1579-
def multisite_switchover(cluster_name: str, group: Optional[int], leader_site: Optional[str],
1591+
def site_switchover(cluster_name: str, group: Optional[int], leader_site: Optional[str],
15801592
candidate_site: Optional[str], force: bool, scheduled: Optional[str]) -> None:
15811593
"""Process ``multisite-switchover`` command of ``patronictl`` utility.
15821594
15831595
Perform a site switchover operation in the multisite cluster.
15841596
15851597
.. seealso::
1586-
Refer to :func:`_do_multisite_switchover` for details.
1598+
Refer to :func:`_do_site_switchover` for details.
15871599
15881600
:param cluster_name: name of the Patroni cluster.
15891601
:param group: filter Citus group within we should perform a switchover. If ``None``, user will be prompted for
@@ -1594,7 +1606,7 @@ def multisite_switchover(cluster_name: str, group: Optional[int], leader_site: O
15941606
:param force: perform the switchover without asking for confirmations.
15951607
:param scheduled: timestamp when the switchover should be scheduled to occur. If ``now`` perform immediately.
15961608
"""
1597-
_do_multisite_switchover(cluster_name, group, leader_site, candidate_site, force, scheduled)
1609+
_do_site_switchover(cluster_name, group, leader_site, candidate_site, force, scheduled)
15981610

15991611

16001612

patroni/multisite.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,10 @@ def __init__(self, config, on_change=None):
7373
self.stop_requested = False
7474
self.on_change = on_change
7575

76-
msconfig = config['multisite']
77-
78-
from .dcs import get_dcs
79-
80-
# Multisite configuration inherits values from main configuration
81-
inherited_keys = ['name', 'scope', 'namespace', 'loop_wait', 'ttl', 'retry_timeout']
82-
for key in inherited_keys:
83-
if key not in msconfig and key in config:
84-
msconfig[key] = config[key]
85-
86-
msconfig.setdefault('observe_interval', config.get('loop_wait'))
87-
88-
# TODO: fetch default host/port from postgresql section
89-
if 'host' not in msconfig or 'port' not in msconfig:
90-
raise Exception("Missing host or port from multisite configuration")
91-
92-
# Disable etcd3 lease ownership detection warning
93-
msconfig['multisite'] = True
76+
msconfig, self.dcs = self.get_dcs_config(config)
9477

9578
self.config = msconfig
96-
9779
self.name = msconfig['name']
98-
self.dcs = get_dcs(msconfig)
9980

10081
if msconfig.get('update_crd'):
10182
self._state_updater = KubernetesStateManagement(msconfig.get('update_crd'),
@@ -120,6 +101,29 @@ def __init__(self, config, on_change=None):
120101

121102
self._dcs_error = None
122103

104+
@staticmethod
105+
def get_dcs_config(config):
106+
msconfig = config['multisite']
107+
108+
# Multisite configuration inherits values from main configuration
109+
inherited_keys = ['name', 'scope', 'namespace', 'loop_wait', 'ttl', 'retry_timeout']
110+
for key in inherited_keys:
111+
if key not in msconfig and key in config:
112+
msconfig[key] = config[key]
113+
114+
msconfig.setdefault('observe_interval', config.get('loop_wait'))
115+
116+
# TODO: fetch default host/port from postgresql section
117+
if 'host' not in msconfig or 'port' not in msconfig:
118+
raise Exception("Missing host or port from multisite configuration")
119+
120+
# Disable etcd3 lease ownership detection warning
121+
msconfig['multisite'] = True
122+
123+
from .dcs import get_dcs
124+
125+
return msconfig, get_dcs(msconfig)
126+
123127
def status(self):
124128
return {
125129
"status": "Leader" if self._has_leader or self._standby_config is None else "Standby",

0 commit comments

Comments
 (0)