6464from .config import Config
6565from .dcs import AbstractDCS , Cluster , get_dcs as _get_dcs , Member
6666from .exceptions import PatroniException
67+ from .multisite import MultisiteController
6768from .postgresql .misc import postgres_version_to_int
6869from .postgresql .mpp import get_mpp
6970from .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
0 commit comments