Skip to content

Commit 5387c70

Browse files
committed
handle error: storage belongs to different cluster
1 parent 025f7ed commit 5387c70

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/charm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ def _on_peer_relation_changed(self, event: HookEvent):
478478
try:
479479
# Update the members of the cluster in the Patroni configuration on this unit.
480480
self.update_config()
481+
if self._patroni.cluster_system_id_mismatch(unit_name=self.unit.name):
482+
self.unit.status = BlockedStatus("Failed start postgresql. Storage belongs to a foreign cluster")
483+
return
481484
except RetryError:
482485
self.unit.status = BlockedStatus("failed to update cluster members on member")
483486
return

tests/integration/ha_tests/test_self_healing.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pytest_operator.plugin import OpsTest
1010
from tenacity import Retrying, stop_after_delay, wait_fixed
1111

12+
from .test_restore_cluster import SECOND_APPLICATION
1213
from ..helpers import (
1314
CHARM_SERIES,
1415
db_connect,
@@ -554,8 +555,19 @@ async def test_network_cut_without_ip_change(
554555
@pytest.mark.group(1)
555556
async def test_deploy_zero_units(ops_test: OpsTest):
556557
"""Scale the database to zero units and scale up again."""
557-
app = await app_name(ops_test)
558+
charm = await ops_test.build_charm(".")
559+
async with ops_test.fast_forward():
560+
await ops_test.model.deploy(
561+
charm,
562+
num_units=1,
563+
application_name=SECOND_APPLICATION,
564+
series=CHARM_SERIES,
565+
storage={"pgdata": {"pool": "lxd-btrfs", "size": 2048}},
566+
config={"profile": "testing"},
567+
)
568+
await ops_test.model.wait_for_idle(status="active", timeout=1500)
558569

570+
app = await app_name(ops_test)
559571
dbname = f"{APPLICATION_NAME.replace('-', '_')}_first_database"
560572
connection_string, _ = await get_db_connection(ops_test, dbname=dbname)
561573

@@ -580,6 +592,13 @@ async def test_deploy_zero_units(ops_test: OpsTest):
580592
if await unit.is_leader_from_status:
581593
primary_storage = storage_id(ops_test, unit.name)
582594

595+
logger.info(f"get storage id app: {SECOND_APPLICATION}")
596+
second_storage = ""
597+
for unit in ops_test.model.applications[SECOND_APPLICATION].units:
598+
if await unit.is_leader_from_status:
599+
second_storage = storage_id(ops_test, unit.name)
600+
break
601+
583602
# Scale the database to zero units.
584603
logger.info("scaling database to zero units")
585604
await scale_application(ops_test, app, 0)
@@ -601,13 +620,23 @@ async def test_deploy_zero_units(ops_test: OpsTest):
601620
await add_unit_with_storage(ops_test, app=app, storage=primary_storage)
602621
await ops_test.model.wait_for_idle(status="active", timeout=1500)
603622

623+
logger.info("scaling database to tow unit using foreign cluster's storage")
624+
await add_unit_with_storage(ops_test, app=app, storage=second_storage)
625+
await ops_test.model.block_until(
626+
lambda:
627+
timeout=1500
628+
)
629+
604630
connection_string, _ = await get_db_connection(ops_test, dbname=dbname)
605631
logger.info("checking whether writes are increasing")
606632
await are_writes_increasing(ops_test)
607633

608634
logger.info("check test database data")
609635
await validate_test_data(connection_string)
610636

637+
638+
639+
611640
# Scale up to two units.
612641
logger.info("scaling database to two unit")
613642
prev_units = [unit.name for unit in ops_test.model.applications[app].units]

0 commit comments

Comments
 (0)