Skip to content

Commit

Permalink
mypy: pglookout/pglookout.py [BF-1560]
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Giffard committed Mar 23, 2023
1 parent 9b4e13f commit d0304b6
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 214 deletions.
10 changes: 5 additions & 5 deletions pglookout/cluster_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from logging.handlers import SysLogHandler
from pglookout import logutil
from pglookout.common import get_iso_timestamp, parse_iso_datetime
from pglookout.common_types import MemberState, ObserverState, ReplicationSlotAsDict
from pglookout.common_types import MemberState, ObservedState, ReplicationSlotAsDict
from pglookout.config import Config
from pglookout.pgutil import mask_connection_info
from pglookout.statsd import StatsClient
Expand Down Expand Up @@ -78,7 +78,7 @@ def __init__(
self,
config: Config,
cluster_state: dict[str, MemberState],
observer_state: dict[str, ObserverState],
observer_state: dict[str, ObservedState],
create_alert_file: Callable[[str], None],
cluster_monitor_check_queue: Queue[str],
failover_decision_queue: Queue[str],
Expand All @@ -96,7 +96,7 @@ def __init__(
self.stats: StatsClient = stats
self.running: bool = True
self.cluster_state: dict[str, MemberState] = cluster_state
self.observer_state: dict[str, ObserverState] = observer_state
self.observer_state: dict[str, ObservedState] = observer_state
self.config: Config = config
self.create_alert_file: Callable[[str], None] = create_alert_file
self.db_conns: dict[str, psycopg2.connection | None] = {}
Expand Down Expand Up @@ -152,7 +152,7 @@ def _connect_to_db(self, instance: str, dsn: str | None) -> psycopg2.connection
self.db_conns[instance] = conn
return conn

def _fetch_observer_state(self, instance: str, uri: str) -> ObserverState | None:
def _fetch_observer_state(self, instance: str, uri: str) -> ObservedState | None:
now_iso = get_iso_timestamp()
result = {"fetch_time": now_iso, "connection": True}
fetch_uri = uri + "/state.json"
Expand Down Expand Up @@ -194,7 +194,7 @@ def _fetch_observer_state(self, instance: str, uri: str) -> ObserverState | None
self.stats.unexpected_exception(ex, where="_fetch_observer_state")
result["connection"] = False

return result
return cast(ObservedState, result)

def fetch_observer_state(self, instance: str, uri: str) -> None:
start_time = time.monotonic()
Expand Down
23 changes: 20 additions & 3 deletions pglookout/common_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from datetime import datetime
from typing import Any, Dict, TypedDict
from typing import TypedDict, Union


class ReplicationSlotAsDict(TypedDict, total=True):
Expand Down Expand Up @@ -44,10 +44,27 @@ class MemberState(TypedDict, total=False):


# Note for future improvements:
# If we want ObserverState to accept arbitrary keys, we have three choices:
# If we want ObservedState to accept arbitrary keys, we have three choices:
# - Use a different type (pydantic, dataclasses, etc.)
# - Use a TypedDict for static keys (connection, fetch_time) and a sub-dict for
# dynamic keys (received from state.json).
# - Wait for something like `allow_extra` to be implemented into TypedDict (unlikely)
# https://github.com/python/mypy/issues/4617
ObserverState = Dict[str, Any]
class ObservedState(dict[str, Union[MemberState, bool, str]]):
"""Represents an observed state, from the perspective of an observer.
Note:
The content of this type is dynamic, as it depends on the number of
members in the cluster. There are two static keys, connection and
fetch_time, and N dynamic keys, one for each member of the cluster.
Like so::
connection: bool
fetch_time: str
name_or_ip_1: MemberState
name_or_ip_2: MemberState
...
name_or_ip_N: MemberState
"""

pass
3 changes: 2 additions & 1 deletion pglookout/current_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import annotations

from . import version
from pglookout.default import JSON_STATE_FILE_PATH

import argparse
import json
Expand Down Expand Up @@ -43,7 +44,7 @@ def main(args: list[str] | None = None) -> int:
try:
with open(arg.state, "r") as fp:
config = json.load(fp)
state_file_path = config.get("json_state_file_path", "/tmp/pglookout_state.json") # pylint: disable=no-member
state_file_path = config.get("json_state_file_path", JSON_STATE_FILE_PATH) # pylint: disable=no-member
if time.monotonic() - os.stat(state_file_path).st_mtime > 60.0:
# file older than one minute, pglookout probably dead, exit with minus one
return -1
Expand Down
10 changes: 10 additions & 0 deletions pglookout/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Aiven, Helsinki, Finland. https://aiven.io/
from typing import Final

WARNING_REPLICATION_TIME_LAG: Final[float] = 30.0
MAX_FAILOVER_REPLICATION_TIME_LAG: Final[float] = 120.0
REPLICATION_CATCHUP_TIMEOUT: Final[float] = 300.0
MISSING_MASTER_FROM_CONFIG_TIMEOUT: Final[float] = 15.0
MAINTENANCE_MODE_FILE: Final[str] = "/tmp/pglookout_maintenance_mode_file"
PG_DATA_DIRECTORY: Final[str] = "/var/lib/pgsql/data"
JSON_STATE_FILE_PATH: Final[str] = "/tmp/pglookout_state.json"
Loading

0 comments on commit d0304b6

Please sign in to comment.