-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the cluster_has_leader service for standby clusters
Before this patch we checked the expected standby leader state was `running` for all versions of Patroni. With this patch, for: * Patroni < 3.0.4, standby leaders are in `running` state. * Patroni >= 3.0.4, standby leaders can be in `streaming` or `in archive recovey` state. We will raise a warning for the latter. The tests where modified to account for this. Co-authored-by: Denis Laxalde <[email protected]>
- Loading branch information
Showing
8 changed files
with
246 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"members": [ | ||
{ | ||
"name": "srv1", | ||
"role": "standby_leader", | ||
"state": "stopped", | ||
"api_url": "https://10.20.199.3:8008/patroni", | ||
"host": "10.20.199.3", | ||
"port": 5432, | ||
"timeline": 51 | ||
}, | ||
{ | ||
"name": "srv2", | ||
"role": "replica", | ||
"state": "streaming", | ||
"api_url": "https://10.20.199.4:8008/patroni", | ||
"host": "10.20.199.4", | ||
"port": 5432, | ||
"timeline": 51, | ||
"lag": 0 | ||
}, | ||
{ | ||
"name": "srv3", | ||
"role": "replica", | ||
"state": "streaming", | ||
"api_url": "https://10.20.199.5:8008/patroni", | ||
"host": "10.20.199.5", | ||
"port": 5432, | ||
"timeline": 51, | ||
"lag": 0 | ||
} | ||
] | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/json/cluster_has_leader_ko_standby_leader_archiving.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"members": [ | ||
{ | ||
"name": "srv1", | ||
"role": "standby_leader", | ||
"state": "in archive recovery", | ||
"api_url": "https://10.20.199.3:8008/patroni", | ||
"host": "10.20.199.3", | ||
"port": 5432, | ||
"timeline": 51 | ||
}, | ||
{ | ||
"name": "srv2", | ||
"role": "replica", | ||
"state": "streaming", | ||
"api_url": "https://10.20.199.4:8008/patroni", | ||
"host": "10.20.199.4", | ||
"port": 5432, | ||
"timeline": 51, | ||
"lag": 0 | ||
}, | ||
{ | ||
"name": "srv3", | ||
"role": "replica", | ||
"state": "streaming", | ||
"api_url": "https://10.20.199.5:8008/patroni", | ||
"host": "10.20.199.5", | ||
"port": 5432, | ||
"timeline": 51, | ||
"lag": 0 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,132 @@ | ||
from pathlib import Path | ||
from typing import Iterator, Union | ||
|
||
import pytest | ||
from click.testing import CliRunner | ||
|
||
from check_patroni.cli import main | ||
|
||
from . import PatroniAPI | ||
from . import PatroniAPI, cluster_api_set_replica_running | ||
|
||
|
||
@pytest.fixture | ||
def cluster_has_leader_ok( | ||
patroni_api: PatroniAPI, old_replica_state: bool, datadir: Path, tmp_path: Path | ||
) -> Iterator[None]: | ||
cluster_path: Union[str, Path] = "cluster_has_leader_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_leader_ok") | ||
def test_cluster_has_leader_ok(runner: CliRunner, patroni_api: PatroniAPI) -> None: | ||
with patroni_api.routes({"cluster": "cluster_has_leader_ok.json"}): | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
assert result.exit_code == 0 | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
assert ( | ||
result.stdout | ||
== "CLUSTERHASLEADER OK - The cluster has a running leader. | has_leader=1;;@0\n" | ||
== "CLUSTERHASLEADER OK - The cluster has a running leader. | has_leader=1;;@0 is_leader=1 is_standby_leader=0 is_standby_leader_in_arc_rec=0;@1:1\n" | ||
) | ||
assert result.exit_code == 0 | ||
|
||
|
||
@pytest.fixture | ||
def cluster_has_leader_ok_standby_leader( | ||
patroni_api: PatroniAPI, old_replica_state: bool, datadir: Path, tmp_path: Path | ||
) -> Iterator[None]: | ||
cluster_path: Union[str, Path] = "cluster_has_leader_ok_standby_leader.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_leader_ok_standby_leader") | ||
def test_cluster_has_leader_ok_standby_leader( | ||
runner: CliRunner, patroni_api: PatroniAPI | ||
) -> None: | ||
with patroni_api.routes({"cluster": "cluster_has_leader_ok_standby_leader.json"}): | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
assert result.exit_code == 0 | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
assert ( | ||
result.stdout | ||
== "CLUSTERHASLEADER OK - The cluster has a running leader. | has_leader=1;;@0\n" | ||
== "CLUSTERHASLEADER OK - The cluster has a running leader. | has_leader=1;;@0 is_leader=0 is_standby_leader=1 is_standby_leader_in_arc_rec=0;@1:1\n" | ||
) | ||
assert result.exit_code == 0 | ||
|
||
|
||
@pytest.fixture | ||
def cluster_has_leader_ko( | ||
patroni_api: PatroniAPI, old_replica_state: bool, datadir: Path, tmp_path: Path | ||
) -> Iterator[None]: | ||
cluster_path: Union[str, Path] = "cluster_has_leader_ko.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_leader_ko") | ||
def test_cluster_has_leader_ko(runner: CliRunner, patroni_api: PatroniAPI) -> None: | ||
with patroni_api.routes({"cluster": "cluster_has_leader_ko.json"}): | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
assert ( | ||
result.stdout | ||
== "CLUSTERHASLEADER CRITICAL - The cluster has no running leader or the standby leader is in archive recovery. | has_leader=0;;@0 is_leader=0 is_standby_leader=0 is_standby_leader_in_arc_rec=0;@1:1\n" | ||
) | ||
assert result.exit_code == 2 | ||
|
||
|
||
@pytest.fixture | ||
def cluster_has_leader_ko_standby_leader( | ||
patroni_api: PatroniAPI, old_replica_state: bool, datadir: Path, tmp_path: Path | ||
) -> Iterator[None]: | ||
cluster_path: Union[str, Path] = "cluster_has_leader_ko_standby_leader.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_leader_ko_standby_leader") | ||
def test_cluster_has_leader_ko_standby_leader( | ||
runner: CliRunner, patroni_api: PatroniAPI | ||
) -> None: | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
assert ( | ||
result.stdout | ||
== "CLUSTERHASLEADER CRITICAL - The cluster has no running leader or the standby leader is in archive recovery. | has_leader=0;;@0 is_leader=0 is_standby_leader=0 is_standby_leader_in_arc_rec=0;@1:1\n" | ||
) | ||
assert result.exit_code == 2 | ||
|
||
|
||
@pytest.fixture | ||
def cluster_has_leader_ko_standby_leader_archiving( | ||
patroni_api: PatroniAPI, old_replica_state: bool, datadir: Path, tmp_path: Path | ||
) -> Iterator[None]: | ||
cluster_path: Union[ | ||
str, Path | ||
] = "cluster_has_leader_ko_standby_leader_archiving.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_leader_ko_standby_leader_archiving") | ||
def test_cluster_has_leader_ko_standby_leader_archiving( | ||
runner: CliRunner, patroni_api: PatroniAPI | ||
) -> None: | ||
result = runner.invoke(main, ["-e", patroni_api.endpoint, "cluster_has_leader"]) | ||
assert ( | ||
result.stdout | ||
== "CLUSTERHASLEADER CRITICAL - The cluster has no running leader. | has_leader=0;;@0\n" | ||
== "CLUSTERHASLEADER WARNING - The cluster has no running leader or the standby leader is in archive recovery. | has_leader=1;;@0 is_leader=0 is_standby_leader=1 is_standby_leader_in_arc_rec=1;@1:1\n" | ||
) | ||
assert result.exit_code == 1 |