|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from . import internal_utils |
| 4 | +from . platforms.internal_platform_utils_provider import InternalPlaformUtilsProvider |
| 5 | + |
| 6 | +from .. import consts |
| 7 | +from ..node_state import PostgresNodeState |
| 8 | +from ..enums import NodeStatus |
| 9 | +from ..raise_error import RaiseError |
| 10 | +from ..exceptions import ExecUtilException |
| 11 | + |
| 12 | +from testgres.operations.os_ops import OsOperations |
| 13 | + |
| 14 | +import typing |
| 15 | +import time |
| 16 | + |
| 17 | + |
| 18 | +class InternalNodeUtils: |
| 19 | + T_PLATFORM_UTILS = InternalPlaformUtilsProvider.T_PLATFORM_UTILS |
| 20 | + |
| 21 | + @staticmethod |
| 22 | + def get_pg_node_state( |
| 23 | + os_ops: OsOperations, |
| 24 | + bin_dir: str, |
| 25 | + data_dir: str, |
| 26 | + utils_log_file: typing.Optional[str], |
| 27 | + ) -> PostgresNodeState: |
| 28 | + assert isinstance(os_ops, OsOperations) |
| 29 | + assert type(bin_dir) is str |
| 30 | + assert type(data_dir) is str |
| 31 | + assert utils_log_file is None or type(utils_log_file) is str |
| 32 | + |
| 33 | + C_MAX_ATTEMPTS = 3 |
| 34 | + C_SLEEP_TIME1 = 1 |
| 35 | + C_SLEEP_TIME_MULT = 2 |
| 36 | + |
| 37 | + pg_ctl_params = [ |
| 38 | + os_ops.build_path(bin_dir, consts.BINARY_NAME__PG_CTL), |
| 39 | + "-D", |
| 40 | + data_dir, |
| 41 | + "status", |
| 42 | + ] |
| 43 | + |
| 44 | + attempt = 0 |
| 45 | + sleep_time = C_SLEEP_TIME1 |
| 46 | + |
| 47 | + platform_utils_provider = InternalPlaformUtilsProvider( |
| 48 | + os_ops, |
| 49 | + ) |
| 50 | + |
| 51 | + while True: |
| 52 | + assert type(attempt) is int |
| 53 | + assert attempt >= 0 |
| 54 | + assert attempt < C_MAX_ATTEMPTS |
| 55 | + |
| 56 | + attempt += 1 |
| 57 | + |
| 58 | + if attempt > 1: |
| 59 | + internal_utils.send_log_debug("Sleep {} second(s) before an attempt #{}".format( |
| 60 | + sleep_time, |
| 61 | + attempt, |
| 62 | + )) |
| 63 | + time.sleep(sleep_time) |
| 64 | + sleep_time = sleep_time * C_SLEEP_TIME_MULT |
| 65 | + |
| 66 | + exec_r = internal_utils.execute_utility3( |
| 67 | + os_ops, |
| 68 | + pg_ctl_params, |
| 69 | + utils_log_file, |
| 70 | + check=False, |
| 71 | + ) |
| 72 | + |
| 73 | + status_code = exec_r.returncode |
| 74 | + out = exec_r.stdout |
| 75 | + error = exec_r.stderr |
| 76 | + |
| 77 | + assert type(status_code) is int |
| 78 | + assert type(out) is str |
| 79 | + assert type(error) is str |
| 80 | + |
| 81 | + # ----------------- |
| 82 | + if status_code == consts.PG_CTL__STATUS__NODE_IS_STOPPED: |
| 83 | + return PostgresNodeState(NodeStatus.Stopped, None) |
| 84 | + |
| 85 | + # ----------------- |
| 86 | + if status_code == consts.PG_CTL__STATUS__BAD_DATADIR: |
| 87 | + return PostgresNodeState(NodeStatus.Uninitialized, None) |
| 88 | + |
| 89 | + # ----------------- |
| 90 | + if status_code == consts.PG_CTL__STATUS__OK: |
| 91 | + pid = __class__._parse_pid( |
| 92 | + out, |
| 93 | + pg_ctl_params, |
| 94 | + ) |
| 95 | + assert type(pid) is int |
| 96 | + assert pid != 0 |
| 97 | + |
| 98 | + # ----------------- detect zombie |
| 99 | + if platform_utils_provider.get().ProcessIsZombie_soft_check(os_ops, pid) is True: |
| 100 | + internal_utils.send_log_debug("Postmaster process {} is a zombie.".format( |
| 101 | + pid, |
| 102 | + )) |
| 103 | + return PostgresNodeState(NodeStatus.Zombie, pid) |
| 104 | + |
| 105 | + # ----------------- |
| 106 | + return PostgresNodeState(NodeStatus.Running, pid) |
| 107 | + |
| 108 | + assert status_code != consts.PG_CTL__STATUS__OK |
| 109 | + |
| 110 | + errMsg = "Getting of a node status [data_dir is {0}] failed.".format( |
| 111 | + data_dir, |
| 112 | + ) |
| 113 | + |
| 114 | + e1 = ExecUtilException( |
| 115 | + message=errMsg, |
| 116 | + command=pg_ctl_params, |
| 117 | + exit_code=status_code, |
| 118 | + out=out, |
| 119 | + error=error, |
| 120 | + ) |
| 121 | + |
| 122 | + if status_code == consts.PG_CTL__STATUS__FAILED: |
| 123 | + internal_utils.send_log_debug( |
| 124 | + "pg_ctl fails with an error: {}".format( |
| 125 | + exec_r.stderr, |
| 126 | + )) |
| 127 | + |
| 128 | + try: |
| 129 | + find_postmaster_r = platform_utils_provider.get().FindPostmaster( |
| 130 | + os_ops, |
| 131 | + bin_dir, |
| 132 | + data_dir, |
| 133 | + ) |
| 134 | + except Exception as e2: |
| 135 | + raise e2 from e1 |
| 136 | + |
| 137 | + assert type(find_postmaster_r) is __class__.T_PLATFORM_UTILS.FindPostmasterResult |
| 138 | + |
| 139 | + if find_postmaster_r.code == __class__.T_PLATFORM_UTILS.FindPostmasterResultCode.ok: |
| 140 | + # Postmaster is alive. Let's wait a few seconds and check its status again. |
| 141 | + internal_utils.send_log_debug( |
| 142 | + "Postmaster is found and has PID {}.".format( |
| 143 | + find_postmaster_r.pid, |
| 144 | + )) |
| 145 | + |
| 146 | + if attempt < C_MAX_ATTEMPTS: |
| 147 | + continue |
| 148 | + |
| 149 | + raise e1 |
| 150 | + |
| 151 | + @staticmethod |
| 152 | + def _parse_pid( |
| 153 | + out: str, |
| 154 | + pg_ctl_params, |
| 155 | + ) -> int: |
| 156 | + assert type(out) is str |
| 157 | + |
| 158 | + if out == "": |
| 159 | + RaiseError.pg_ctl_returns_an_empty_string( |
| 160 | + pg_ctl_params, |
| 161 | + ) |
| 162 | + |
| 163 | + C_PID_PREFIX = "(PID: " |
| 164 | + |
| 165 | + i = out.find(C_PID_PREFIX) |
| 166 | + |
| 167 | + if i == -1: |
| 168 | + RaiseError.pg_ctl_returns_an_unexpected_string( |
| 169 | + out, |
| 170 | + pg_ctl_params, |
| 171 | + ) |
| 172 | + |
| 173 | + assert i > 0 |
| 174 | + assert i < len(out) |
| 175 | + assert len(C_PID_PREFIX) <= len(out) |
| 176 | + assert i <= len(out) - len(C_PID_PREFIX) |
| 177 | + |
| 178 | + i += len(C_PID_PREFIX) |
| 179 | + start_pid_s = i |
| 180 | + |
| 181 | + while True: |
| 182 | + if i == len(out): |
| 183 | + RaiseError.pg_ctl_returns_an_unexpected_string( |
| 184 | + out, |
| 185 | + pg_ctl_params, |
| 186 | + ) |
| 187 | + |
| 188 | + ch = out[i] |
| 189 | + |
| 190 | + if ch == ")": |
| 191 | + break |
| 192 | + |
| 193 | + if ch.isdigit(): |
| 194 | + i += 1 |
| 195 | + continue |
| 196 | + |
| 197 | + RaiseError.pg_ctl_returns_an_unexpected_string( |
| 198 | + out, |
| 199 | + pg_ctl_params, |
| 200 | + ) |
| 201 | + assert False |
| 202 | + |
| 203 | + if i == start_pid_s: |
| 204 | + RaiseError.pg_ctl_returns_an_unexpected_string( |
| 205 | + out, |
| 206 | + pg_ctl_params, |
| 207 | + ) |
| 208 | + |
| 209 | + # TODO: Let's verify a length of pid string. |
| 210 | + |
| 211 | + pid = int(out[start_pid_s:i]) |
| 212 | + |
| 213 | + if pid == 0: |
| 214 | + RaiseError.pg_ctl_returns_a_zero_pid( |
| 215 | + out, |
| 216 | + pg_ctl_params, |
| 217 | + ) |
| 218 | + |
| 219 | + assert pid != 0 |
| 220 | + |
| 221 | + return pid |
0 commit comments