Skip to content

Commit

Permalink
cluster_has_replica: account for standby leaders
Browse files Browse the repository at this point in the history
Before only leader where taken into account.

Fix #72
  • Loading branch information
blogh committed Oct 19, 2024
1 parent 86149a3 commit 1605fd9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

* cluster_has_replica now properly accounts for standby leaders (#72, reported by @MLyssens)

### Misc

* Update the tests and the documentation to reflect that master is replaced by
Expand Down
2 changes: 1 addition & 1 deletion check_patroni/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def debug_member(member: Any, health: str) -> None:
# members because leader_tl will remain None. it's not
# a big deal since having no leader is rare.
for tmember in cluster_item_dict["members"]:
if tmember["role"] == "leader":
if tmember["role"] in ["leader", "standby_leader"]:
leader_tl = int(tmember["timeline"])
break

Expand Down
33 changes: 33 additions & 0 deletions tests/json/cluster_has_replica_standby_cluster_ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"members": [
{
"name": "srv1",
"role": "standby_leader",
"state": "running",
"api_url": "https://10.20.199.3:8008/patroni",
"host": "10.20.199.3",
"port": 5432,
"timeline": 51
},
{
"name": "srv2",
"role": "replica",
"state": "in archive recovery",
"api_url": "https://10.20.199.4:8008/patroni",
"host": "10.20.199.4",
"port": 5432,
"timeline": 51,
"lag": 0
},
{
"name": "srv3",
"role": "sync_standby",
"state": "streaming",
"api_url": "https://10.20.199.5:8008/patroni",
"host": "10.20.199.5",
"port": 5432,
"timeline": 51,
"lag": 0
}
]
}
25 changes: 25 additions & 0 deletions tests/test_cluster_has_replica.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ def test_cluster_has_replica_ok_with_count_thresholds_lag(
assert result.exit_code == 0


@pytest.fixture
def cluster_has_replica_standby_cluster_ok(
patroni_api: PatroniAPI, old_replica_state: bool, datadir: Path, tmp_path: Path
) -> Iterator[None]:
cluster_path: Union[str, Path] = "cluster_has_replica_standby_cluster_ok.json"
patroni_path = "cluster_has_replica_patroni_verion_3.1.0.json"
if old_replica_state:
cluster_path = cluster_api_set_replica_running(datadir / cluster_path, tmp_path)
patroni_path = "cluster_has_replica_patroni_verion_3.0.0.json"
with patroni_api.routes({"cluster": cluster_path, "patroni": patroni_path}):
yield None


@pytest.mark.usefixtures("cluster_has_replica_standby_cluster_ok")
def test_cluster_has_relica_standby_cluster_ok(
runner: CliRunner, patroni_api: PatroniAPI
) -> None:
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_replica"])
assert (
result.stdout
== "CLUSTERHASREPLICA OK - healthy_replica is 2 | healthy_replica=2 srv2_lag=0 srv2_sync=0 srv2_timeline=51 srv3_lag=0 srv3_sync=1 srv3_timeline=51 sync_replica=1 unhealthy_replica=0\n"
)
assert result.exit_code == 0


@pytest.fixture
def cluster_has_replica_ko(
patroni_api: PatroniAPI, old_replica_state: bool, datadir: Path, tmp_path: Path
Expand Down

0 comments on commit 1605fd9

Please sign in to comment.