mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-26 07:30:14 +00:00
Compare commits
@@ -0,0 +1,48 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Screenshots**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Environment**
|
||||
- Patroni version:
|
||||
- PostgreSQL version:
|
||||
- DCS (and its version):
|
||||
|
||||
**Patroni configuration file**
|
||||
```
|
||||
Please copy&paste your Patroni configuration file here
|
||||
```
|
||||
|
||||
**patronictl show-config**
|
||||
```
|
||||
Please copy&paste the output of "patronictl show-config" command here
|
||||
```
|
||||
|
||||
**Have you checked Patroni logs?**
|
||||
Please provide a snippet of Patroni log files here
|
||||
|
||||
**Have you checked PostgreSQL logs?**
|
||||
Please provide a snippet here
|
||||
|
||||
**Have you tried to use GitHub issue search?**
|
||||
Maybe there is already a similar issue solved.
|
||||
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
+36
-29
@@ -7,20 +7,23 @@ addons:
|
||||
- expect-dev # for unbuffer
|
||||
env:
|
||||
global:
|
||||
- ETCDVERSION=3.0.17 ZKVERSION=3.4.11 CONSULVERSION=0.7.4
|
||||
- PYVERSIONS="2.7 3.5 3.6"
|
||||
- EXCLUDE_BEHAVE="3.5"
|
||||
- ETCDVERSION=3.3.13 ZKVERSION=3.4.14 CONSULVERSION=0.7.4
|
||||
- PYVERSIONS="2.7 3.6"
|
||||
- BOTO_CONFIG=/doesnotexist
|
||||
matrix:
|
||||
include:
|
||||
- python: "3.5"
|
||||
env: TEST_SUITE="python setup.py"
|
||||
env: PYVERSIONS="2.7 3.5 3.6" TEST_SUITE="python setup.py"
|
||||
- python: "3.6"
|
||||
env: DCS="etcd" TEST_SUITE="behave"
|
||||
- python: "3.6"
|
||||
env: DCS="etcd3" TEST_SUITE="behave"
|
||||
- python: "3.6"
|
||||
env: DCS="exhibitor" TEST_SUITE="behave"
|
||||
- python: "3.6"
|
||||
env: DCS="consul" TEST_SUITE="behave"
|
||||
- python: "3.6"
|
||||
env: DCS="raft" TEST_SUITE="behave"
|
||||
- python: "3.6"
|
||||
env: DCS="kubernetes" TEST_SUITE="behave"
|
||||
branches:
|
||||
@@ -34,10 +37,8 @@ before_cache:
|
||||
- |
|
||||
rm -fr $HOME/mycache/python*
|
||||
for pv in $PYVERSIONS; do
|
||||
if [[ $TEST_SUITE != "behave" || $pv != $EXCLUDE_BEHAVE ]]; then
|
||||
fpv=$(basename $(readlink $HOME/virtualenv/python${pv}))
|
||||
mv $HOME/virtualenv/${fpv} $HOME/mycache/${fpv}
|
||||
fi
|
||||
fpv=$(basename $(readlink $HOME/virtualenv/python${pv}))
|
||||
mv $HOME/virtualenv/${fpv} $HOME/mycache/${fpv}
|
||||
done
|
||||
install:
|
||||
- |
|
||||
@@ -58,6 +59,7 @@ install:
|
||||
function get_etcd() {
|
||||
EC=~/mycache/etcd_${ETCDVERSION}
|
||||
if [[ ! -x $EC ]]; then
|
||||
rm -rf ~/mycache/etcd_*
|
||||
curl -L https://github.com/coreos/etcd/releases/download/v${ETCDVERSION}/etcd-v${ETCDVERSION}-linux-amd64.tar.gz \
|
||||
| tar xz -C . --strip=1 --wildcards --no-anchored etcd
|
||||
[[ ${PIPESTATUS[0]} == 0 ]] || return 1
|
||||
@@ -66,6 +68,10 @@ install:
|
||||
ln -s $EC etcd
|
||||
}
|
||||
|
||||
function get_etcd3() {
|
||||
get_etcd
|
||||
}
|
||||
|
||||
function get_kubernetes() {
|
||||
wget -O localkube "https://storage.googleapis.com/minikube/k8sReleases/v1.7.0/localkube-linux-amd64"
|
||||
chmod +x localkube
|
||||
@@ -102,6 +108,10 @@ install:
|
||||
done&
|
||||
}
|
||||
|
||||
function get_raft() {
|
||||
return 0
|
||||
}
|
||||
|
||||
attempt_num=1
|
||||
until get_${DCS}; do
|
||||
[[ $attempt_num -ge 3 ]] && exit 1
|
||||
@@ -111,41 +121,38 @@ install:
|
||||
fi
|
||||
|
||||
for pv in $PYVERSIONS; do
|
||||
if [[ $TEST_SUITE != "behave" || $pv != $EXCLUDE_BEHAVE ]]; then
|
||||
fpv=$(basename $(readlink $HOME/virtualenv/python$pv))
|
||||
if [[ -d ~/mycache/${fpv} ]]; then
|
||||
mv ~/virtualenv/${fpv} ~/virtualenv/${fpv}.bckp
|
||||
mv ~/mycache/${fpv} ~/virtualenv/${fpv}
|
||||
fi
|
||||
source ~/virtualenv/python${pv}/bin/activate
|
||||
# explicitly install all needed python modules to cache them
|
||||
for p in '-r requirements.txt' 'psycopg2-binary behave codacy-coverage coverage coveralls flake8 mock pytest-cov pytest setuptools'; do
|
||||
pip install $p --upgrade
|
||||
done
|
||||
fpv=$(basename $(readlink $HOME/virtualenv/python$pv))
|
||||
if [[ -d ~/mycache/${fpv} ]]; then
|
||||
mv ~/virtualenv/${fpv} ~/virtualenv/${fpv}.bckp
|
||||
mv ~/mycache/${fpv} ~/virtualenv/${fpv}
|
||||
fi
|
||||
source ~/virtualenv/python${pv}/bin/activate
|
||||
# explicitly install all needed python modules to cache them
|
||||
for p in '-r requirements.txt' 'psycopg2-binary behave codacy-coverage coverage coveralls flake8 mock pytest-cov pytest setuptools'; do
|
||||
pip install $p --upgrade
|
||||
done
|
||||
done
|
||||
script:
|
||||
- |
|
||||
for pv in $PYVERSIONS; do
|
||||
if [[ $TEST_SUITE == "behave" && $pv == $EXCLUDE_BEHAVE ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
source ~/virtualenv/python${pv}/bin/activate
|
||||
|
||||
if [[ $TEST_SUITE != "behave" ]]; then
|
||||
echo Running unit tests using python${pv}
|
||||
unbuffer $TEST_SUITE test
|
||||
$TEST_SUITE flake8
|
||||
elif [[ $pv != $EXCLUDE_BEHAVE ]]; then
|
||||
echo Running acceptance tests using python${pv}
|
||||
if [[ $TEST_SUITE = "behave" ]]; then
|
||||
echo Running integration tests using python${pv}
|
||||
if ! PATH=.:/usr/lib/postgresql/9.6/bin:$PATH unbuffer $TEST_SUITE; then
|
||||
# output all log files when tests are failing
|
||||
grep . features/output/*_failed/*postgres?.*
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo Running unit tests using python${pv}
|
||||
unbuffer $TEST_SUITE test
|
||||
$TEST_SUITE flake8
|
||||
fi
|
||||
mv .coverage /tmp/.coverage.$pv
|
||||
done
|
||||
mv /tmp/.coverage.* .
|
||||
python -m coverage combine
|
||||
|
||||
set +e
|
||||
after_success:
|
||||
|
||||
+2
-2
@@ -14,7 +14,7 @@ ARG PGDATA
|
||||
ARG LC_ALL
|
||||
ARG LANG
|
||||
|
||||
ENV ETCDVERSION=2.3.8 CONFDVERSION=0.16.0
|
||||
ENV ETCDVERSION=3.3.13 CONFDVERSION=0.16.0
|
||||
|
||||
RUN set -ex \
|
||||
&& export DEBIAN_FRONTEND=noninteractive \
|
||||
@@ -30,7 +30,7 @@ RUN set -ex \
|
||||
\
|
||||
# Cleanup all locales but en_US.UTF-8
|
||||
&& find /usr/share/i18n/charmaps/ -type f ! -name UTF-8.gz -delete \
|
||||
&& find /usr/share/i18n/locales/ -type f ! -name en_US ! -name en_GB ! -name i18n ! -name iso14651_t1 ! -name iso14651_t1_common ! -name 'translit_*' -delete \
|
||||
&& find /usr/share/i18n/locales/ -type f ! -name en_US ! -name en_GB ! -name i18n* ! -name iso14651_t1 ! -name iso14651_t1_common ! -name 'translit_*' -delete \
|
||||
&& echo 'en_US.UTF-8 UTF-8' > /usr/share/i18n/SUPPORTED \
|
||||
\
|
||||
# Make sure we have a en_US.UTF-8 locale available
|
||||
|
||||
+4
-2
@@ -96,7 +96,7 @@ Patroni can be installed with pip:
|
||||
|
||||
where dependencies can be either empty, or consist of one or more of the following:
|
||||
|
||||
etcd
|
||||
etcd or etcd3
|
||||
`python-etcd` module in order to use Etcd as DCS
|
||||
consul
|
||||
`python-consul` module in order to use Consul as DCS
|
||||
@@ -106,6 +106,8 @@ exhibitor
|
||||
`kazoo` module in order to use Exhibitor as DCS (same dependencies as for Zookeeper)
|
||||
kubernetes
|
||||
`kubernetes` module in order to use Kubernetes as DCS in Patroni
|
||||
raft
|
||||
`pysyncobj` module in order to use python Raft implementation as DCS
|
||||
aws
|
||||
`boto` in order to use AWS callbacks
|
||||
|
||||
@@ -124,7 +126,7 @@ Running and Configuring
|
||||
To get started, do the following from different terminals:
|
||||
::
|
||||
|
||||
> etcd --data-dir=data/etcd
|
||||
> etcd --data-dir=data/etcd --enable-v2=true
|
||||
> ./patroni.py postgres0.yml
|
||||
> ./patroni.py postgres1.yml
|
||||
|
||||
|
||||
+43
-35
@@ -6,57 +6,31 @@ networks:
|
||||
demo:
|
||||
|
||||
services:
|
||||
etcd1:
|
||||
etcd1: &etcd
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/etcd.env
|
||||
environment:
|
||||
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
|
||||
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
|
||||
ETCD_INITIAL_CLUSTER: etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
|
||||
ETCD_INITIAL_CLUSTER_STATE: new
|
||||
ETCD_INITIAL_CLUSTER_TOKEN: tutorial
|
||||
container_name: demo-etcd1
|
||||
hostname: etcd1
|
||||
command: etcd -name etcd1 -initial-advertise-peer-urls http://etcd1:2380
|
||||
|
||||
etcd2:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/etcd.env
|
||||
<<: *etcd
|
||||
container_name: demo-etcd2
|
||||
hostname: etcd2
|
||||
command: etcd -name etcd2 -initial-advertise-peer-urls http://etcd2:2380
|
||||
|
||||
etcd3:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/etcd.env
|
||||
<<: *etcd
|
||||
container_name: demo-etcd3
|
||||
hostname: etcd3
|
||||
command: etcd -name etcd3 -initial-advertise-peer-urls http://etcd3:2380
|
||||
|
||||
patroni1:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/patroni.env
|
||||
hostname: patroni1
|
||||
container_name: demo-patroni1
|
||||
environment:
|
||||
PATRONI_NAME: patroni1
|
||||
|
||||
patroni2:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/patroni.env
|
||||
hostname: patroni2
|
||||
container_name: demo-patroni2
|
||||
environment:
|
||||
PATRONI_NAME: patroni2
|
||||
|
||||
patroni3:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/patroni.env
|
||||
hostname: patroni3
|
||||
container_name: demo-patroni3
|
||||
environment:
|
||||
PATRONI_NAME: patroni3
|
||||
|
||||
haproxy:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
@@ -67,3 +41,37 @@ services:
|
||||
- "5000:5000"
|
||||
- "5001:5001"
|
||||
command: haproxy
|
||||
environment: &haproxy_env
|
||||
ETCDCTL_ENDPOINTS: http://etcd1:2379,http://etcd2:2379,http://etcd3:2379
|
||||
PATRONI_ETCD3_HOSTS: "'etcd1:2379','etcd2:2379','etcd3:2379'"
|
||||
PATRONI_SCOPE: demo
|
||||
|
||||
patroni1:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/patroni.env
|
||||
hostname: patroni1
|
||||
container_name: demo-patroni1
|
||||
environment:
|
||||
<<: *haproxy_env
|
||||
PATRONI_NAME: patroni1
|
||||
|
||||
patroni2:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/patroni.env
|
||||
hostname: patroni2
|
||||
container_name: demo-patroni2
|
||||
environment:
|
||||
<<: *haproxy_env
|
||||
PATRONI_NAME: patroni2
|
||||
|
||||
patroni3:
|
||||
image: patroni
|
||||
networks: [ demo ]
|
||||
env_file: docker/patroni.env
|
||||
hostname: patroni3
|
||||
container_name: demo-patroni3
|
||||
environment:
|
||||
<<: *haproxy_env
|
||||
PATRONI_NAME: patroni3
|
||||
|
||||
@@ -25,7 +25,7 @@ case "$1" in
|
||||
while ! etcdctl cluster-health 2> /dev/null; do
|
||||
sleep 1
|
||||
done
|
||||
exec dumb-init $CONFD etcd -node $(echo $ETCDCTL_ENDPOINTS | sed 's/,/ -node /g')
|
||||
exec dumb-init $CONFD etcdv3 -node $(echo $ETCDCTL_ENDPOINTS | sed 's/,/ -node /g')
|
||||
fi
|
||||
;;
|
||||
etcd)
|
||||
@@ -37,7 +37,7 @@ case "$1" in
|
||||
esac
|
||||
|
||||
## We start an etcd
|
||||
if [ -z "$PATRONI_ETCD_HOSTS" ] && [ -z "$PATRONI_ZOOKEEPER_HOSTS" ]; then
|
||||
if [ -z "$PATRONI_ETCD3_HOSTS" ] && [ -z "$PATRONI_ZOOKEEPER_HOSTS" ]; then
|
||||
export PATRONI_ETCD_URL="http://127.0.0.1:2379"
|
||||
etcd --data-dir /tmp/etcd.data -advertise-client-urls=$PATRONI_ETCD_URL -listen-client-urls=http://0.0.0.0:2379 > /var/log/etcd.log 2> /var/log/etcd.err &
|
||||
fi
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
|
||||
ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
|
||||
ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
|
||||
ETCD_INITIAL_CLUSTER_STATE=new
|
||||
ETCD_INITIAL_CLUSTER_TOKEN=tutorial
|
||||
@@ -1,6 +1,3 @@
|
||||
PATRONI_SCOPE=demo
|
||||
PATRONI_ETCD_HOSTS='etcd1:2379','etcd2:2379','etcd3:2379'
|
||||
|
||||
PATRONI_RESTAPI_USERNAME=admin
|
||||
PATRONI_RESTAPI_PASSWORD=admin
|
||||
PATRONI_SUPERUSER_USERNAME=postgres
|
||||
@@ -9,6 +6,3 @@ PATRONI_REPLICATION_USERNAME=replicator
|
||||
PATRONI_REPLICATION_PASSWORD=replicate
|
||||
PATRONI_admin_PASSWORD=admin
|
||||
PATRONI_admin_OPTIONS=createdb,createrole
|
||||
|
||||
# for etcdctl
|
||||
ETCDCTL_ENDPOINTS=http://etcd1:2379,http://etcd2:2379,http://etcd3:2379
|
||||
|
||||
+44
-4
@@ -35,8 +35,8 @@ Example: defining ``PATRONI_admin_PASSWORD=strongpasswd`` and ``PATRONI_admin_OP
|
||||
|
||||
Consul
|
||||
------
|
||||
- **PATRONI\_CONSUL\_HOST**: the host:port for the Consul endpoint.
|
||||
- **PATRONI\_CONSUL\_URL**: url for the Consul, in format: http(s)://host:port
|
||||
- **PATRONI\_CONSUL\_HOST**: the host:port for the Consul local agent.
|
||||
- **PATRONI\_CONSUL\_URL**: url for the Consul local agent, in format: http(s)://host:port
|
||||
- **PATRONI\_CONSUL\_PORT**: (optional) Consul port
|
||||
- **PATRONI\_CONSUL\_SCHEME**: (optional) **http** or **https**, defaults to **http**
|
||||
- **PATRONI\_CONSUL\_TOKEN**: (optional) ACL token
|
||||
@@ -66,9 +66,27 @@ Etcd
|
||||
- **PATRONI\_ETCD\_CERT**: File with the client certificate.
|
||||
- **PATRONI\_ETCD\_KEY**: File with the client key. Can be empty if the key is part of certificate.
|
||||
|
||||
Etcdv3
|
||||
------
|
||||
Environment names for Etcdv3 are similar as for Etcd, you just need to use ``ETCD3`` instead of ``ETCD`` in the variable name. Example: ``PATRONI_ETCD3_HOST``, ``PATRONI_ETCD3_CACERT``, and so on.
|
||||
|
||||
.. warning::
|
||||
Keys created with protocol version 2 are not visible with protocol version 3 and the other way around, therefore it is not possible to switch from Etcd to Etcdv3 just by updating Patroni configuration.
|
||||
|
||||
|
||||
ZooKeeper
|
||||
---------
|
||||
- **PATRONI\_ZOOKEEPER\_HOSTS**: comma separated list of ZooKeeper cluster members: "'host1:port1','host2:port2','etc...'". It is important to quote every single entity!
|
||||
- **PATRONI\_ZOOKEEPER\_HOSTS**: Comma separated list of ZooKeeper cluster members: "'host1:port1','host2:port2','etc...'". It is important to quote every single entity!
|
||||
- **PATRONI\_ZOOKEEPER\_USE\_SSL**: (optional) Whether SSL is used or not. Defaults to ``false``. If set to ``false``, all SSL specific parameters are ignored.
|
||||
- **PATRONI\_ZOOKEEPER\_CACERT**: (optional) The CA certificate. If present it will enable validation.
|
||||
- **PATRONI\_ZOOKEEPER\_CERT**: (optional) File with the client certificate.
|
||||
- **PATRONI\_ZOOKEEPER\_KEY**: (optional) File with the client key.
|
||||
- **PATRONI\_ZOOKEEPER\_KEY\_PASSWORD**: (optional) The client key password.
|
||||
- **PATRONI\_ZOOKEEPER\_VERIFY**: (optional) Whether to verify certificate or not. Defaults to ``true``.
|
||||
|
||||
.. note::
|
||||
It is required to install ``kazoo>=2.6.0`` to support SSL.
|
||||
|
||||
|
||||
Exhibitor
|
||||
---------
|
||||
@@ -79,6 +97,7 @@ Exhibitor
|
||||
|
||||
Kubernetes
|
||||
----------
|
||||
- **PATRONI\_KUBERNETES\_BYPASS\_API\_SERVICE**: (optional) When communicating with the Kubernetes API, Patroni is usually relying on the `kubernetes` service, the address of which is exposed in the pods via the `KUBERNETES_SERVICE_HOST` environment variable. If `PATRONI_KUBERNETES_BYPASS_API_SERVICE` is set to ``true``, Patroni will resolve the list of API nodes behind the service and connect directly to them.
|
||||
- **PATRONI\_KUBERNETES\_NAMESPACE**: (optional) Kubernetes namespace where the Patroni pod is running. Default value is `default`.
|
||||
- **PATRONI\_KUBERNETES\_LABELS**: Labels in format ``{label1: value1, label2: value2}``. These labels will be used to find existing objects (Pods and either Endpoints or ConfigMaps) associated with the current cluster. Also Patroni will set them on every object (Endpoint or ConfigMap) it creates.
|
||||
- **PATRONI\_KUBERNETES\_SCOPE\_LABEL**: (optional) name of the label containing cluster name. Default value is `cluster-name`.
|
||||
@@ -87,6 +106,15 @@ Kubernetes
|
||||
- **PATRONI\_KUBERNETES\_POD\_IP**: (optional) IP address of the pod Patroni is running in. This value is required when `PATRONI_KUBERNETES_USE_ENDPOINTS` is enabled and is used to populate the leader endpoint subsets when the pod's PostgreSQL is promoted.
|
||||
- **PATRONI\_KUBERNETES\_PORTS**: (optional) if the Service object has the name for the port, the same name must appear in the Endpoint object, otherwise service won't work. For example, if your service is defined as ``{Kind: Service, spec: {ports: [{name: postgresql, port: 5432, targetPort: 5432}]}}``, then you have to set ``PATRONI_KUBERNETES_PORTS='[{"name": "postgresql", "port": 5432}]'`` and Patroni will use it for updating subsets of the leader Endpoint. This parameter is used only if `PATRONI_KUBERNETES_USE_ENDPOINTS` is set.
|
||||
|
||||
Raft
|
||||
----
|
||||
|
||||
- **PATRONI\_RAFT\_SELF\_ADDR**: ``ip:port`` to listen on for Raft connections. The ``self_addr`` must be accessible from other nodes of the cluster. If not set, the node will not participate in consensus.
|
||||
- **PATRONI\_RAFT\_BIND\_ADDR**: (optional) ``ip:port`` to listen on for Raft connections. If not specified the ``self_addr`` will be used.
|
||||
- **PATRONI\_RAFT\_PARTNER\_ADDRS**: list of other Patroni nodes in the cluster in format ``"'ip1:port1','ip2:port2'"``. It is important to quote every single entity!
|
||||
- **PATRONI\_RAFT\_DATA\_DIR**: directory where to store Raft log and snapshot. If not specified the current working directory is used.
|
||||
- **PATRONI\_RAFT\_PASSWORD**: (optional) Encrypt Raft traffic with a specified password, requires ``cryptography`` python module.
|
||||
|
||||
PostgreSQL
|
||||
----------
|
||||
- **PATRONI\_POSTGRESQL\_LISTEN**: IP address + port that Postgres listens to. Multiple comma-separated addresses are permitted, as long as the port component is appended after to the last one with a colon, i.e. ``listen: 127.0.0.1,127.0.0.2:5432``. Patroni will use the first address from this list to establish local connections to the PostgreSQL node.
|
||||
@@ -99,23 +127,32 @@ PostgreSQL
|
||||
- **PATRONI\_REPLICATION\_PASSWORD**: replication password; the user will be created during initialization.
|
||||
- **PATRONI\_REPLICATION\_SSLMODE**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **PATRONI\_REPLICATION\_SSLKEY**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **PATRONI\_REPLICATION\_SSLPASSWORD**: (optional) maps to the `sslpassword <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLPASSWORD>`__ connection parameter, which specifies the password for the secret key specified in ``PATRONI_REPLICATION_SSLKEY``.
|
||||
- **PATRONI\_REPLICATION\_SSLCERT**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **PATRONI\_REPLICATION\_SSLROOTCERT**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **PATRONI\_REPLICATION\_SSLCRL**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **PATRONI\_REPLICATION\_GSSENCMODE**: (optional) maps to the `gssencmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE>`__ connection parameter, which determines whether or with what priority a secure GSS TCP/IP connection will be negotiated with the server
|
||||
- **PATRONI\_REPLICATION\_CHANNEL\_BINDING**: (optional) maps to the `channel_binding <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CHANNEL-BINDING>`__ connection parameter, which controls the client's use of channel binding.
|
||||
- **PATRONI\_SUPERUSER\_USERNAME**: name for the superuser, set during initialization (initdb) and later used by Patroni to connect to the postgres. Also this user is used by pg_rewind.
|
||||
- **PATRONI\_SUPERUSER\_PASSWORD**: password for the superuser, set during initialization (initdb).
|
||||
- **PATRONI\_SUPERUSER\_SSLMODE**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **PATRONI\_SUPERUSER\_SSLKEY**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **PATRONI\_SUPERUSER\_SSLPASSWORD**: (optional) maps to the `sslpassword <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLPASSWORD>`__ connection parameter, which specifies the password for the secret key specified in ``PATRONI_SUPERUSER_SSLKEY``.
|
||||
- **PATRONI\_SUPERUSER\_SSLCERT**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **PATRONI\_SUPERUSER\_SSLROOTCERT**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **PATRONI\_SUPERUSER\_SSLCRL**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **PATRONI\_SUPERUSER\_GSSENCMODE**: (optional) maps to the `gssencmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE>`__ connection parameter, which determines whether or with what priority a secure GSS TCP/IP connection will be negotiated with the server
|
||||
- **PATRONI\_SUPERUSER\_CHANNEL\_BINDING**: (optional) maps to the `channel_binding <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CHANNEL-BINDING>`__ connection parameter, which controls the client's use of channel binding.
|
||||
- **PATRONI\_REWIND\_USERNAME**: name for the user for ``pg_rewind``; the user will be created during initialization of postgres 11+ and all necessary `permissions <https://www.postgresql.org/docs/11/app-pgrewind.html#id-1.9.5.8.8>`__ will be granted.
|
||||
- **PATRONI\_REWIND\_PASSWORD**: password for the user for ``pg_rewind``; the user will be created during initialization.
|
||||
- **PATRONI\_REWIND\_SSLMODE**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **PATRONI\_REWIND\_SSLKEY**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **PATRONI\_REWIND\_SSLPASSWORD**: (optional) maps to the `sslpassword <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLPASSWORD>`__ connection parameter, which specifies the password for the secret key specified in ``PATRONI_REWIND_SSLKEY``.
|
||||
- **PATRONI\_REWIND\_SSLCERT**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **PATRONI\_REWIND\_SSLROOTCERT**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **PATRONI\_REWIND\_SSLCRL**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **PATRONI\_REWIND\_GSSENCMODE**: (optional) maps to the `gssencmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE>`__ connection parameter, which determines whether or with what priority a secure GSS TCP/IP connection will be negotiated with the server
|
||||
- **PATRONI\_REWIND\_CHANNEL\_BINDING**: (optional) maps to the `channel_binding <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CHANNEL-BINDING>`__ connection parameter, which controls the client's use of channel binding.
|
||||
|
||||
REST API
|
||||
--------
|
||||
@@ -126,10 +163,13 @@ REST API
|
||||
- **PATRONI\_RESTAPI\_CERTFILE**: Specifies the file with the certificate in the PEM format. If the certfile is not specified or is left empty, the API server will work without SSL.
|
||||
- **PATRONI\_RESTAPI\_KEYFILE**: Specifies the file with the secret key in the PEM format.
|
||||
- **PATRONI\_RESTAPI\_CAFILE**: Specifies the file with the CA_BUNDLE with certificates of trusted CAs to use while verifying client certs.
|
||||
- **PATRONI\_RESTAPI\_VERIFY\_CLIENT**: ``none``, ``optional`` or ``required``. When ``none`` REST API will not check client certificates. When ``required`` client certificates are required for all REST API calls. When ``optional`` client certificates are required for all unsafe REST API endpoints. If ``verify_client`` is set to ``optional`` or ``required`` basic-auth is not checked.
|
||||
- **PATRONI\_RESTAPI\_VERIFY\_CLIENT**: ``none`` (default), ``optional`` or ``required``. When ``none`` REST API will not check client certificates. When ``required`` client certificates are required for all REST API calls. When ``optional`` client certificates are required for all unsafe REST API endpoints. When ``required`` is used, then client authentication succeeds, if the certificate signature verification succeeds. For ``optional`` the client cert will only be checked for ``PUT``, ``POST``, ``PATCH``, and ``DELETE`` requests.
|
||||
- **PATRONI\_RESTAPI\_HTTP\_EXTRA\_HEADERS**: (optional) HTTP headers let the REST API server pass additional information with an HTTP response.
|
||||
- **PATRONI\_RESTAPI\_HTTPS\_EXTRA\_HEADERS**: (optional) HTTPS headers let the REST API server pass additional information with an HTTP response when TLS is enabled. This will also pass additional information set in ``http_extra_headers``.
|
||||
|
||||
CTL
|
||||
---
|
||||
- **PATRONICTL\_CONFIG\_FILE**: location of the configuration file.
|
||||
- **PATRONI\_CTL\_INSECURE**: Allow connections to REST API without verifying SSL certs.
|
||||
- **PATRONI\_CTL\_CACERT**: Specifies the file with the CA_BUNDLE file or directory with certificates of trusted CAs to use while verifying REST API SSL certs. If not provided patronictl will use the value provided for REST API "cafile" parameter.
|
||||
- **PATRONI\_CTL\_CERTFILE**: Specifies the file with the client certificate in the PEM format. If not provided patronictl will use the value provided for REST API "certfile" parameter.
|
||||
|
||||
+11
-2
@@ -72,7 +72,7 @@ Patroni can be installed with pip:
|
||||
|
||||
where dependencies can be either empty, or consist of one or more of the following:
|
||||
|
||||
etcd
|
||||
etcd or etcd3
|
||||
`python-etcd` module in order to use Etcd as DCS
|
||||
consul
|
||||
`python-consul` module in order to use Consul as DCS
|
||||
@@ -82,6 +82,8 @@ exhibitor
|
||||
`kazoo` module in order to use Exhibitor as DCS (same dependencies as for Zookeeper)
|
||||
kubernetes
|
||||
`kubernetes` module in order to use Kubernetes as DCS in Patroni
|
||||
raft
|
||||
`pysyncobj` module in order to use python Raft implementation as DCS
|
||||
aws
|
||||
`boto` in order to use AWS callbacks
|
||||
|
||||
@@ -97,6 +99,13 @@ independently of Patroni.
|
||||
|
||||
.. _running_configuring:
|
||||
|
||||
Planning the Number of PostgreSQL Nodes
|
||||
---------------------------------------
|
||||
|
||||
Patroni/PostgreSQL nodes are decoupled from DCS nodes (except when Patroni implements RAFT on its own) and therefore
|
||||
there is no requirement on the minimal number of nodes. Running a cluster consisting of one master and one standby is
|
||||
perfectly fine. You can add more standby nodes later.
|
||||
|
||||
Running and Configuring
|
||||
-----------------------
|
||||
|
||||
@@ -107,7 +116,7 @@ obtain those files from the git repository and replace `./patroni.py` below with
|
||||
To get started, do the following from different terminals:
|
||||
::
|
||||
|
||||
> etcd --data-dir=data/etcd
|
||||
> etcd --data-dir=data/etcd --enable-v2=true
|
||||
> ./patroni.py postgres0.yml
|
||||
> ./patroni.py postgres1.yml
|
||||
|
||||
|
||||
+201
-98
@@ -15,12 +15,14 @@ Dynamic configuration is stored in the DCS (Distributed Configuration Store) and
|
||||
- **ttl**: the TTL to acquire the leader lock (in seconds). Think of it as the length of time before initiation of the automatic failover process. Default value: 30
|
||||
- **retry\_timeout**: timeout for DCS and PostgreSQL operation retries (in seconds). DCS or network issues shorter than this will not cause Patroni to demote the leader. Default value: 10
|
||||
- **maximum\_lag\_on\_failover**: the maximum bytes a follower may lag to be able to participate in leader election.
|
||||
- **max\_timelines\_history**: maximum number of timeline history items kept in DCS. Default value: 0. When set to 0, it keeps the full history in DCS.
|
||||
- **master\_start\_timeout**: the amount of time a master is allowed to recover from failures before failover is triggered (in seconds). Default is 300 seconds. When set to 0 failover is done immediately after a crash is detected if possible. When using asynchronous replication a failover can cause lost transactions. Worst case failover time for master failure is: loop\_wait + master\_start\_timeout + loop\_wait, unless master\_start\_timeout is zero, in which case it's just loop\_wait. Set the value according to your durability/availability tradeoff.
|
||||
- **master\_stop\_timeout**: The number of seconds Patroni is allowed to wait when stopping Postgres and effective only when synchronous_mode is enabled. When set to > 0 and the synchronous_mode is enabled, Patroni sends SIGKILL to the postmaster if the stop operation is running for more than the value set by master_stop_timeout. Set the value according to your durability/availability tradeoff. If the parameter is not set or set <= 0, master_stop_timeout does not apply.
|
||||
- **synchronous\_mode**: turns on synchronous replication mode. In this mode a replica will be chosen as synchronous and only the latest leader and synchronous replica are able to participate in leader election. Synchronous mode makes sure that successfully committed transactions will not be lost at failover, at the cost of losing availability for writes when Patroni cannot ensure transaction durability. See :ref:`replication modes documentation <replication_modes>` for details.
|
||||
- **synchronous\_mode\_strict**: prevents disabling synchronous replication if no synchronous replicas are available, blocking all client writes to the master. See :ref:`replication modes documentation <replication_modes>` for details.
|
||||
- **postgresql**:
|
||||
- **use\_pg\_rewind**: whether or not to use pg_rewind. Defaults to `false`.
|
||||
- **use\_slots**: whether or not to use replication_slots. Defaults to `true` on PostgreSQL 9.4+.
|
||||
- **use\_slots**: whether or not to use replication slots. Defaults to `true` on PostgreSQL 9.4+.
|
||||
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower. There is no recovery.conf anymore in PostgreSQL 12, but you may continue using this section, because Patroni handles it transparently.
|
||||
- **parameters**: list of configuration settings for Postgres.
|
||||
- **standby\_cluster**: if this section is defined, we want to bootstrap a standby cluster.
|
||||
@@ -32,7 +34,7 @@ Dynamic configuration is stored in the DCS (Distributed Configuration Store) and
|
||||
- **archive\_cleanup\_command**: cleanup command for standby leader
|
||||
- **recovery\_min\_apply\_delay**: how long to wait before actually apply WAL records on a standby leader
|
||||
- **slots**: define permanent replication slots. These slots will be preserved during switchover/failover. Patroni will try to create slots before opening connections to the cluster.
|
||||
- **my_slot_name**: the name of replication slot. It is the responsibility of the operator to make sure that there are no clashes in names between replication slots automatically created by Patroni for members and permanent replication slots.
|
||||
- **my_slot_name**: the name of replication slot. If the permanent slot name matches with the name of the current primary it will not be created. Everything else is the responsibility of the operator to make sure that there are no clashes in names between replication slots automatically created by Patroni for members and permanent replication slots.
|
||||
- **type**: slot type. Could be ``physical`` or ``logical``. If the slot is logical, you have to additionally define ``database`` and ``plugin``.
|
||||
- **database**: the database name where logical slots should be created.
|
||||
- **plugin**: the plugin name for the logical slot.
|
||||
@@ -61,25 +63,26 @@ Log
|
||||
|
||||
Bootstrap configuration
|
||||
-----------------------
|
||||
- **dcs**: This section will be written into `/<namespace>/<scope>/config` of the given configuration store after initializing of new cluster. The global dynamic configuration for the cluster. Under the ``bootstrap.dcs`` you can put any of the parameters described in the :ref:`Dynamic Configuration settings <dynamic_configuration_settings>` and after Patroni initialized (bootstrapped) the new cluster, it will write this section into `/<namespace>/<scope>/config` of the configuration store. All later changes of ``bootstrap.dcs`` will not take any effect! If you want to change them please use either ``patronictl edit-config`` or Patroni :ref:`REST API <rest_api>`.
|
||||
- **method**: custom script to use for bootstrapping this cluster.
|
||||
See :ref:`custom bootstrap methods documentation <custom_bootstrap>` for details.
|
||||
When ``initdb`` is specified revert to the default ``initdb`` command. ``initdb`` is also triggered when no ``method``
|
||||
parameter is present in the configuration file.
|
||||
- **initdb**: List options to be passed on to initdb.
|
||||
- **- data-checksums**: Must be enabled when pg_rewind is needed on 9.3.
|
||||
- **- encoding: UTF8**: default encoding for new databases.
|
||||
- **- locale: UTF8**: default locale for new databases.
|
||||
- **pg\_hba**: list of lines that you should add to pg\_hba.conf.
|
||||
- **- host all all 0.0.0.0/0 md5**.
|
||||
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for replication.
|
||||
- **users**: Some additional users which need to be created after initializing new cluster
|
||||
- **admin**: the name of user
|
||||
- **password: zalando**:
|
||||
- **options**: list of options for CREATE USER statement
|
||||
- **- createrole**
|
||||
- **- createdb**
|
||||
- **post\_bootstrap** or **post\_init**: An additional script that will be executed after initializing the cluster. The script receives a connection string URL (with the cluster superuser as a user name). The PGPASSFILE variable is set to the location of pgpass file.
|
||||
- **bootstrap**:
|
||||
- **dcs**: This section will be written into `/<namespace>/<scope>/config` of the given configuration store after initializing of new cluster. The global dynamic configuration for the cluster. Under the ``bootstrap.dcs`` you can put any of the parameters described in the :ref:`Dynamic Configuration settings <dynamic_configuration_settings>` and after Patroni initialized (bootstrapped) the new cluster, it will write this section into `/<namespace>/<scope>/config` of the configuration store. All later changes of ``bootstrap.dcs`` will not take any effect! If you want to change them please use either ``patronictl edit-config`` or Patroni :ref:`REST API <rest_api>`.
|
||||
- **method**: custom script to use for bootstrapping this cluster.
|
||||
See :ref:`custom bootstrap methods documentation <custom_bootstrap>` for details.
|
||||
When ``initdb`` is specified revert to the default ``initdb`` command. ``initdb`` is also triggered when no ``method``
|
||||
parameter is present in the configuration file.
|
||||
- **initdb**: List options to be passed on to initdb.
|
||||
- **- data-checksums**: Must be enabled when pg_rewind is needed on 9.3.
|
||||
- **- encoding: UTF8**: default encoding for new databases.
|
||||
- **- locale: UTF8**: default locale for new databases.
|
||||
- **pg\_hba**: list of lines that you should add to pg\_hba.conf.
|
||||
- **- host all all 0.0.0.0/0 md5**.
|
||||
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for replication.
|
||||
- **users**: Some additional users which need to be created after initializing new cluster
|
||||
- **admin**: the name of user
|
||||
- **password: zalando**:
|
||||
- **options**: list of options for CREATE USER statement
|
||||
- **- createrole**
|
||||
- **- createdb**
|
||||
- **post\_bootstrap** or **post\_init**: An additional script that will be executed after initializing the cluster. The script receives a connection string URL (with the cluster superuser as a user name). The PGPASSFILE variable is set to the location of pgpass file.
|
||||
|
||||
.. _consul_settings:
|
||||
|
||||
@@ -87,20 +90,35 @@ Consul
|
||||
------
|
||||
Most of the parameters are optional, but you have to specify one of the **host** or **url**
|
||||
|
||||
- **host**: the host:port for the Consul endpoint, in format: http(s)://host:port
|
||||
- **url**: url for the Consul endpoint
|
||||
- **port**: (optional) Consul port
|
||||
- **scheme**: (optional) **http** or **https**, defaults to **http**
|
||||
- **token**: (optional) ACL token
|
||||
- **verify**: (optional) whether to verify the SSL certificate for HTTPS requests
|
||||
- **host**: the host:port for the Consul local agent.
|
||||
- **url**: url for the Consul local agent, in format: http(s)://host:port.
|
||||
- **port**: (optional) Consul port.
|
||||
- **scheme**: (optional) **http** or **https**, defaults to **http**.
|
||||
- **token**: (optional) ACL token.
|
||||
- **verify**: (optional) whether to verify the SSL certificate for HTTPS requests.
|
||||
- **cacert**: (optional) The ca certificate. If present it will enable validation.
|
||||
- **cert**: (optional) file with the client certificate
|
||||
- **cert**: (optional) file with the client certificate.
|
||||
- **key**: (optional) file with the client key. Can be empty if the key is part of **cert**.
|
||||
- **dc**: (optional) Datacenter to communicate with. By default the datacenter of the host is used.
|
||||
- **consistency**: (optional) Select consul consistency mode. Possible values are ``default``, ``consistent``, or ``stale`` (more details in `consul API reference <https://www.consul.io/api/features/consistency.html/>`__)
|
||||
- **checks**: (optional) list of Consul health checks used for the session. By default an empty list is used.
|
||||
- **register\_service**: (optional) whether or not to register a service with the name defined by the scope parameter and the tag master, replica or standby-leader depending on the node's role. Defaults to **false**
|
||||
- **service\_check\_interval**: (optional) how often to perform health check against registered url
|
||||
- **register\_service**: (optional) whether or not to register a service with the name defined by the scope parameter and the tag master, replica or standby-leader depending on the node's role. Defaults to **false**.
|
||||
- **service\_tags**: (optional) additional static tags to add to the Consul service apart from the role (``master``/``replica``/``standby-leader``). By default an empty list is used.
|
||||
- **service\_check\_interval**: (optional) how often to perform health check against registered url.
|
||||
|
||||
The ``token`` needs to have the following ACL permissions:
|
||||
|
||||
::
|
||||
|
||||
service_prefix "${scope}" {
|
||||
policy = "write"
|
||||
}
|
||||
key_prefix "${namespace}/${scope}" {
|
||||
policy = "write"
|
||||
}
|
||||
session_prefix "" {
|
||||
policy = "write"
|
||||
}
|
||||
|
||||
Etcd
|
||||
----
|
||||
@@ -109,8 +127,8 @@ Most of the parameters are optional, but you have to specify one of the **host**
|
||||
- **host**: the host:port for the etcd endpoint.
|
||||
- **hosts**: list of etcd endpoint in format host1:port1,host2:port2,etc... Could be a comma separated string or an actual yaml list.
|
||||
- **use\_proxies**: If this parameter is set to true, Patroni will consider **hosts** as a list of proxies and will not perform a topology discovery of etcd cluster.
|
||||
- **url**: url for the etcd
|
||||
- **proxy**: proxy url for the etcd. If you are connecting to the etcd using proxy, use this parameter instead of **url**
|
||||
- **url**: url for the etcd.
|
||||
- **proxy**: proxy url for the etcd. If you are connecting to the etcd using proxy, use this parameter instead of **url**.
|
||||
- **srv**: Domain to search the SRV record(s) for cluster autodiscovery.
|
||||
- **protocol**: (optional) http or https, if not specified http is used. If the **url** or **proxy** is specified - will take protocol from them.
|
||||
- **username**: (optional) username for etcd authentication.
|
||||
@@ -119,20 +137,39 @@ Most of the parameters are optional, but you have to specify one of the **host**
|
||||
- **cert**: (optional) file with the client certificate.
|
||||
- **key**: (optional) file with the client key. Can be empty if the key is part of **cert**.
|
||||
|
||||
Etcdv3
|
||||
------
|
||||
If you want that Patroni works with Etcd cluster via protocol version 3, you need to use the ``etcd3`` section in the Patroni configuration file. All configuration parameters are the same as for ``etcd``.
|
||||
|
||||
.. warning::
|
||||
Keys created with protocol version 2 are not visible with protocol version 3 and the other way around, therefore it is not possible to switch from ``etcd`` to ``etcd3`` just by updating Patroni config file.
|
||||
|
||||
|
||||
ZooKeeper
|
||||
----------
|
||||
- **hosts**: list of ZooKeeper cluster members in format: ['host1:port1', 'host2:port2', 'etc...'].
|
||||
- **hosts**: List of ZooKeeper cluster members in format: ['host1:port1', 'host2:port2', 'etc...'].
|
||||
- **use_ssl**: (optional) Whether SSL is used or not. Defaults to ``false``. If set to ``false``, all SSL specific parameters are ignored.
|
||||
- **cacert**: (optional) The CA certificate. If present it will enable validation.
|
||||
- **cert**: (optional) File with the client certificate.
|
||||
- **key**: (optional) File with the client key.
|
||||
- **key_password**: (optional) The client key password.
|
||||
- **verify**: (optional) Whether to verify certificate or not. Defaults to ``true``.
|
||||
|
||||
.. note::
|
||||
It is required to install ``kazoo>=2.6.0`` to support SSL.
|
||||
|
||||
|
||||
Exhibitor
|
||||
---------
|
||||
- **hosts**: initial list of Exhibitor (ZooKeeper) nodes in format: 'host1,host2,etc...'. This list updates automatically whenever the Exhibitor (ZooKeeper) cluster topology changes.
|
||||
- **poll\_interval**: how often the list of ZooKeeper and Exhibitor nodes should be updated from Exhibitor
|
||||
- **poll\_interval**: how often the list of ZooKeeper and Exhibitor nodes should be updated from Exhibitor.
|
||||
- **port**: Exhibitor port.
|
||||
|
||||
.. _kubernetes_settings:
|
||||
|
||||
Kubernetes
|
||||
----------
|
||||
- **bypass\_api\_service**: (optional) When communicating with the Kubernetes API, Patroni is usually relying on the `kubernetes` service, the address of which is exposed in the pods via the `KUBERNETES_SERVICE_HOST` environment variable. If `bypass_api_service` is set to ``true``, Patroni will resolve the list of API nodes behind the service and connect directly to them.
|
||||
- **namespace**: (optional) Kubernetes namespace where Patroni pod is running. Default value is `default`.
|
||||
- **labels**: Labels in format ``{label1: value1, label2: value2}``. These labels will be used to find existing objects (Pods and either Endpoints or ConfigMaps) associated with the current cluster. Also Patroni will set them on every object (Endpoint or ConfigMap) it creates.
|
||||
- **scope\_label**: (optional) name of the label containing cluster name. Default value is `cluster-name`.
|
||||
@@ -141,87 +178,153 @@ Kubernetes
|
||||
- **pod\_ip**: (optional) IP address of the pod Patroni is running in. This value is required when `use_endpoints` is enabled and is used to populate the leader endpoint subsets when the pod's PostgreSQL is promoted.
|
||||
- **ports**: (optional) if the Service object has the name for the port, the same name must appear in the Endpoint object, otherwise service won't work. For example, if your service is defined as ``{Kind: Service, spec: {ports: [{name: postgresql, port: 5432, targetPort: 5432}]}}``, then you have to set ``kubernetes.ports: [{"name": "postgresql", "port": 5432}]`` and Patroni will use it for updating subsets of the leader Endpoint. This parameter is used only if `kubernetes.use_endpoints` is set.
|
||||
|
||||
|
||||
.. _raft_settings:
|
||||
|
||||
Raft
|
||||
----
|
||||
- **self\_addr**: ``ip:port`` to listen on for Raft connections. The ``self_addr`` must be accessible from other nodes of the cluster. If not set, the node will not participate in consensus.
|
||||
- **bind\_addr**: (optional) ``ip:port`` to listen on for Raft connections. If not specified the ``self_addr`` will be used.
|
||||
- **partner\_addrs**: list of other Patroni nodes in the cluster in format: ['ip1:port', 'ip2:port', 'etc...']
|
||||
- **data\_dir**: directory where to store Raft log and snapshot. If not specified the current working directory is used.
|
||||
- **password**: (optional) Encrypt Raft traffic with a specified password, requires ``cryptography`` python module.
|
||||
|
||||
Short FAQ about Raft implementation
|
||||
|
||||
- Q: How to list all the nodes providing consensus?
|
||||
|
||||
A: ``syncobj_admin -conn host:port`` -status where the host:port is the address of one of the cluster nodes
|
||||
|
||||
- Q: Node that was a part of consensus and has gone and I can't reuse the same IP for other node. How to remove this node from the consensus?
|
||||
|
||||
A: ``syncobj_admin -conn host:port -remove host2:port2`` where the ``host2:port2`` is the address of the node you want to remove from consensus.
|
||||
|
||||
- Q: Where to get the ``syncobj_admin`` utility?
|
||||
|
||||
A: It is installed together with ``pysyncobj`` module (python RAFT implementation), which is Patroni dependancy.
|
||||
|
||||
- Q: it is possible to run Patroni node without adding in to the consensus?
|
||||
|
||||
A: Yes, just comment out or remove ``raft.self_addr`` from Patroni configuration.
|
||||
|
||||
- Q: It is possible to run Patroni and PostgreSQL only on two nodes?
|
||||
|
||||
A: Yes, on the third node you can run ``patroni_raft_controller`` (without Patroni and PostgreSQL). In such setup one can temporary loose one node without affecting the primary.
|
||||
|
||||
|
||||
.. _postgresql_settings:
|
||||
|
||||
PostgreSQL
|
||||
----------
|
||||
- **authentication**:
|
||||
- **superuser**:
|
||||
- **username**: name for the superuser, set during initialization (initdb) and later used by Patroni to connect to the postgres.
|
||||
- **password**: password for the superuser, set during initialization (initdb).
|
||||
- **sslmode**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **sslkey**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **sslcert**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **sslrootcert**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **sslcrl**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **replication**:
|
||||
- **username**: replication username; the user will be created during initialization. Replicas will use this user to access master via streaming replication
|
||||
- **password**: replication password; the user will be created during initialization.
|
||||
- **sslmode**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **sslkey**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **sslcert**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **sslrootcert**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **sslcrl**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **rewind**:
|
||||
- **username**: name for the user for ``pg_rewind``; the user will be created during initialization of postgres 11+ and all necessary `permissions <https://www.postgresql.org/docs/11/app-pgrewind.html#id-1.9.5.8.8>`__ will be granted.
|
||||
- **password**: password for the user for ``pg_rewind``; the user will be created during initialization.
|
||||
- **sslmode**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **sslkey**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **sslcert**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **sslrootcert**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **sslcrl**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **callbacks**: callback scripts to run on certain actions. Patroni will pass the action, role and cluster name. (See scripts/aws.py as an example of how to write them.)
|
||||
- **on\_reload**: run this script when configuration reload is triggered.
|
||||
- **on\_restart**: run this script when the postgres restarts (without changing role).
|
||||
- **on\_role\_change**: run this script when the postgres is being promoted or demoted.
|
||||
- **on\_start**: run this script when the postgres starts.
|
||||
- **on\_stop**: run this script when the postgres stops.
|
||||
- **connect\_address**: IP address + port through which Postgres is accessible from other nodes and applications.
|
||||
- **create\_replica\_methods**: an ordered list of the create methods for turning a Patroni node into a new replica.
|
||||
"basebackup" is the default method; other methods are assumed to refer to scripts, each of which is configured as its
|
||||
own config item. See :ref:`custom replica creation methods documentation <custom_replica_creation>` for further explanation.
|
||||
- **data\_dir**: The location of the Postgres data directory, either :ref:`existing <existing_data>` or to be initialized by Patroni.
|
||||
- **config\_dir**: The location of the Postgres configuration directory, defaults to the data directory. Must be writable by Patroni.
|
||||
- **bin\_dir**: Path to PostgreSQL binaries (pg_ctl, pg_rewind, pg_basebackup, postgres). The default value is an empty string meaning that PATH environment variable will be used to find the executables.
|
||||
- **listen**: IP address + port that Postgres listens to; must be accessible from other nodes in the cluster, if you're using streaming replication. Multiple comma-separated addresses are permitted, as long as the port component is appended after to the last one with a colon, i.e. ``listen: 127.0.0.1,127.0.0.2:5432``. Patroni will use the first address from this list to establish local connections to the PostgreSQL node.
|
||||
- **use\_unix\_socket**: specifies that Patroni should prefer to use unix sockets to connect to the cluster. Default value is ``false``. If ``unix_socket_directories`` is defined, Patroni will use the first suitable value from it to connect to the cluster and fallback to tcp if nothing is suitable. If ``unix_socket_directories`` is not specified in ``postgresql.parameters``, Patroni will assume that the default value should be used and omit ``host`` from the connection parameters.
|
||||
- **pgpass**: path to the `.pgpass <https://www.postgresql.org/docs/current/static/libpq-pgpass.html>`__ password file. Patroni creates this file before executing pg\_basebackup, the post_init script and under some other circumstances. The location must be writable by Patroni.
|
||||
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
||||
- **custom\_conf** : path to an optional custom ``postgresql.conf`` file, that will be used in place of ``postgresql.base.conf``. The file must exist on all cluster nodes, be readable by PostgreSQL and will be included from its location on the real ``postgresql.conf``. Note that Patroni will not monitor this file for changes, nor backup it. However, its settings can still be overridden by Patroni's own configuration facilities - see :ref:`dynamic configuration <dynamic_configuration>` for details.
|
||||
- **parameters**: list of configuration settings for Postgres. Many of these are required for replication to work.
|
||||
- **pg\_hba**: list of lines that Patroni will use to generate ``pg_hba.conf``. This parameter has higher priority than ``bootstrap.pg_hba``. Together with :ref:`dynamic configuration <dynamic_configuration>` it simplifies management of ``pg_hba.conf``.
|
||||
- **- host all all 0.0.0.0/0 md5**.
|
||||
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for replication.
|
||||
- **pg\_ident**: list of lines that Patroni will use to generate ``pg_ident.conf``. Together with :ref:`dynamic configuration <dynamic_configuration>` it simplifies management of ``pg_ident.conf``.
|
||||
- **- mapname1 systemname1 pguser1**.
|
||||
- **- mapname1 systemname2 pguser2**.
|
||||
- **pg\_ctl\_timeout**: How long should pg_ctl wait when doing ``start``, ``stop`` or ``restart``. Default value is 60 seconds.
|
||||
- **use\_pg\_rewind**: try to use pg\_rewind on the former leader when it joins cluster as a replica.
|
||||
- **remove\_data\_directory\_on\_rewind\_failure**: If this option is enabled, Patroni will remove the PostgreSQL data directory and recreate the replica. Otherwise it will try to follow the new leader. Default value is **false**.
|
||||
- **remove\_data\_directory\_on\_diverged\_timelines**: Patroni will remove the PostgreSQL data directory and recreate the replica if it notices that timelines are diverging and the former master can not start streaming from the new master. This option is useful when ``pg_rewind`` can not be used. Default value is **false**.
|
||||
- **replica\_method**: for each create_replica_methods other than basebackup, you would add a configuration section of the same name. At a minimum, this should include "command" with a full path to the actual script to be executed. Other configuration parameters will be passed along to the script in the form "parameter=value".
|
||||
- **postgresql**:
|
||||
- **authentication**:
|
||||
- **superuser**:
|
||||
- **username**: name for the superuser, set during initialization (initdb) and later used by Patroni to connect to the postgres.
|
||||
- **password**: password for the superuser, set during initialization (initdb).
|
||||
- **sslmode**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **sslkey**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **sslpassword**: (optional) maps to the `sslpassword <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLPASSWORD>`__ connection parameter, which specifies the password for the secret key specified in ``sslkey``.
|
||||
- **sslcert**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **sslrootcert**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **sslcrl**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **gssencmode**: (optional) maps to the `gssencmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE>`__ connection parameter, which determines whether or with what priority a secure GSS TCP/IP connection will be negotiated with the server
|
||||
- **channel_binding**: (optional) maps to the `channel_binding <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CHANNEL-BINDING>`__ connection parameter, which controls the client's use of channel binding.
|
||||
- **replication**:
|
||||
- **username**: replication username; the user will be created during initialization. Replicas will use this user to access master via streaming replication
|
||||
- **password**: replication password; the user will be created during initialization.
|
||||
- **sslmode**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **sslkey**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **sslpassword**: (optional) maps to the `sslpassword <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLPASSWORD>`__ connection parameter, which specifies the password for the secret key specified in ``sslkey``.
|
||||
- **sslcert**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **sslrootcert**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **sslcrl**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **gssencmode**: (optional) maps to the `gssencmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE>`__ connection parameter, which determines whether or with what priority a secure GSS TCP/IP connection will be negotiated with the server
|
||||
- **channel_binding**: (optional) maps to the `channel_binding <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CHANNEL-BINDING>`__ connection parameter, which controls the client's use of channel binding.
|
||||
- **rewind**:
|
||||
- **username**: name for the user for ``pg_rewind``; the user will be created during initialization of postgres 11+ and all necessary `permissions <https://www.postgresql.org/docs/11/app-pgrewind.html#id-1.9.5.8.8>`__ will be granted.
|
||||
- **password**: password for the user for ``pg_rewind``; the user will be created during initialization.
|
||||
- **sslmode**: (optional) maps to the `sslmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLMODE>`__ connection parameter, which allows a client to specify the type of TLS negotiation mode with the server. For more information on how each mode works, please visit the `PostgreSQL documentation <https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS>`__. The default mode is ``prefer``.
|
||||
- **sslkey**: (optional) maps to the `sslkey <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLKEY>`__ connection parameter, which specifies the location of the secret key used with the client's certificate.
|
||||
- **sslpassword**: (optional) maps to the `sslpassword <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLPASSWORD>`__ connection parameter, which specifies the password for the secret key specified in ``sslkey``.
|
||||
- **sslcert**: (optional) maps to the `sslcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCERT>`__ connection parameter, which specifies the location of the client certificate.
|
||||
- **sslrootcert**: (optional) maps to the `sslrootcert <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLROOTCERT>`__ connection parameter, which specifies the location of a file containing one ore more certificate authorities (CA) certificates that the client will use to verify a server's certificate.
|
||||
- **sslcrl**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
||||
- **gssencmode**: (optional) maps to the `gssencmode <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-GSSENCMODE>`__ connection parameter, which determines whether or with what priority a secure GSS TCP/IP connection will be negotiated with the server
|
||||
- **channel_binding**: (optional) maps to the `channel_binding <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CHANNEL-BINDING>`__ connection parameter, which controls the client's use of channel binding.
|
||||
- **callbacks**: callback scripts to run on certain actions. Patroni will pass the action, role and cluster name. (See scripts/aws.py as an example of how to write them.)
|
||||
- **on\_reload**: run this script when configuration reload is triggered.
|
||||
- **on\_restart**: run this script when the postgres restarts (without changing role).
|
||||
- **on\_role\_change**: run this script when the postgres is being promoted or demoted.
|
||||
- **on\_start**: run this script when the postgres starts.
|
||||
- **on\_stop**: run this script when the postgres stops.
|
||||
- **connect\_address**: IP address + port through which Postgres is accessible from other nodes and applications.
|
||||
- **create\_replica\_methods**: an ordered list of the create methods for turning a Patroni node into a new replica.
|
||||
"basebackup" is the default method; other methods are assumed to refer to scripts, each of which is configured as its
|
||||
own config item. See :ref:`custom replica creation methods documentation <custom_replica_creation>` for further explanation.
|
||||
- **data\_dir**: The location of the Postgres data directory, either :ref:`existing <existing_data>` or to be initialized by Patroni.
|
||||
- **config\_dir**: The location of the Postgres configuration directory, defaults to the data directory. Must be writable by Patroni.
|
||||
- **bin\_dir**: Path to PostgreSQL binaries (pg_ctl, pg_rewind, pg_basebackup, postgres). The default value is an empty string meaning that PATH environment variable will be used to find the executables.
|
||||
- **listen**: IP address + port that Postgres listens to; must be accessible from other nodes in the cluster, if you're using streaming replication. Multiple comma-separated addresses are permitted, as long as the port component is appended after to the last one with a colon, i.e. ``listen: 127.0.0.1,127.0.0.2:5432``. Patroni will use the first address from this list to establish local connections to the PostgreSQL node.
|
||||
- **use\_unix\_socket**: specifies that Patroni should prefer to use unix sockets to connect to the cluster. Default value is ``false``. If ``unix_socket_directories`` is defined, Patroni will use the first suitable value from it to connect to the cluster and fallback to tcp if nothing is suitable. If ``unix_socket_directories`` is not specified in ``postgresql.parameters``, Patroni will assume that the default value should be used and omit ``host`` from the connection parameters.
|
||||
- **pgpass**: path to the `.pgpass <https://www.postgresql.org/docs/current/static/libpq-pgpass.html>`__ password file. Patroni creates this file before executing pg\_basebackup, the post_init script and under some other circumstances. The location must be writable by Patroni.
|
||||
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
||||
- **custom\_conf** : path to an optional custom ``postgresql.conf`` file, that will be used in place of ``postgresql.base.conf``. The file must exist on all cluster nodes, be readable by PostgreSQL and will be included from its location on the real ``postgresql.conf``. Note that Patroni will not monitor this file for changes, nor backup it. However, its settings can still be overridden by Patroni's own configuration facilities - see :ref:`dynamic configuration <dynamic_configuration>` for details.
|
||||
- **parameters**: list of configuration settings for Postgres. Many of these are required for replication to work.
|
||||
- **pg\_hba**: list of lines that Patroni will use to generate ``pg_hba.conf``. This parameter has higher priority than ``bootstrap.pg_hba``. Together with :ref:`dynamic configuration <dynamic_configuration>` it simplifies management of ``pg_hba.conf``.
|
||||
- **- host all all 0.0.0.0/0 md5**.
|
||||
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for replication.
|
||||
- **pg\_ident**: list of lines that Patroni will use to generate ``pg_ident.conf``. Together with :ref:`dynamic configuration <dynamic_configuration>` it simplifies management of ``pg_ident.conf``.
|
||||
- **- mapname1 systemname1 pguser1**.
|
||||
- **- mapname1 systemname2 pguser2**.
|
||||
- **pg\_ctl\_timeout**: How long should pg_ctl wait when doing ``start``, ``stop`` or ``restart``. Default value is 60 seconds.
|
||||
- **use\_pg\_rewind**: try to use pg\_rewind on the former leader when it joins cluster as a replica.
|
||||
- **remove\_data\_directory\_on\_rewind\_failure**: If this option is enabled, Patroni will remove the PostgreSQL data directory and recreate the replica. Otherwise it will try to follow the new leader. Default value is **false**.
|
||||
- **remove\_data\_directory\_on\_diverged\_timelines**: Patroni will remove the PostgreSQL data directory and recreate the replica if it notices that timelines are diverging and the former master can not start streaming from the new master. This option is useful when ``pg_rewind`` can not be used. Default value is **false**.
|
||||
- **replica\_method**: for each create_replica_methods other than basebackup, you would add a configuration section of the same name. At a minimum, this should include "command" with a full path to the actual script to be executed. Other configuration parameters will be passed along to the script in the form "parameter=value".
|
||||
- **pre\_promote**: a fencing script that executes during a failover after acquiring the leader lock but before promoting the replica. If the script exits with a non-zero code, Patroni does not promote the replica and removes the leader key from DCS.
|
||||
|
||||
REST API
|
||||
--------
|
||||
- **connect\_address**: IP address (or hostname) and port, to access the Patroni's :ref:`REST API <rest_api>`. All the members of the cluster must be able to connect to this address, so unless the Patroni setup is intended for a demo inside the localhost, this address must be a non "localhost" or loopback address (ie: "localhost" or "127.0.0.1"). It can serve as an endpoint for HTTP health checks (read below about the "listen" REST API parameter), and also for user queries (either directly or via the REST API), as well as for the health checks done by the cluster members during leader elections (for example, to determine whether the master is still running, or if there is a node which has a WAL position that is ahead of the one doing the query; etc.) The connect_address is put in the member key in DCS, making it possible to translate the member name into the address to connect to its REST API.
|
||||
- **restapi**:
|
||||
- **connect\_address**: IP address (or hostname) and port, to access the Patroni's :ref:`REST API <rest_api>`. All the members of the cluster must be able to connect to this address, so unless the Patroni setup is intended for a demo inside the localhost, this address must be a non "localhost" or loopback address (ie: "localhost" or "127.0.0.1"). It can serve as an endpoint for HTTP health checks (read below about the "listen" REST API parameter), and also for user queries (either directly or via the REST API), as well as for the health checks done by the cluster members during leader elections (for example, to determine whether the master is still running, or if there is a node which has a WAL position that is ahead of the one doing the query; etc.) The connect_address is put in the member key in DCS, making it possible to translate the member name into the address to connect to its REST API.
|
||||
|
||||
- **listen**: IP address (or hostname) and port that Patroni will listen to for the REST API - to provide also the same health checks and cluster messaging between the participating nodes, as described above. to provide health-check information for HAProxy (or any other load balancer capable of doing a HTTP "OPTION" or "GET" checks).
|
||||
- **listen**: IP address (or hostname) and port that Patroni will listen to for the REST API - to provide also the same health checks and cluster messaging between the participating nodes, as described above. to provide health-check information for HAProxy (or any other load balancer capable of doing a HTTP "OPTION" or "GET" checks).
|
||||
|
||||
- **Optional**:
|
||||
- **authentication**:
|
||||
- **authentication**: (optional)
|
||||
- **username**: Basic-auth username to protect unsafe REST API endpoints.
|
||||
- **password**: Basic-auth password to protect unsafe REST API endpoints.
|
||||
- **certfile**: (optional): Specifies the file with the certificate in the PEM format. If the certfile is not specified or is left empty, the API server will work without SSL.
|
||||
- **keyfile**: (optional): Specifies the file with the secret key in the PEM format.
|
||||
- **cafile**: (optional): Specifies the file with the CA_BUNDLE with certificates of trusted CAs to use while verifying client certs.
|
||||
- **verify\_client**: (optional): ``none`` (default), ``optional`` or ``required``. When ``none`` REST API will not check client certificates. When ``required`` client certificates are required for all REST API calls. When ``optional`` client certificates are required for all unsafe REST API endpoints. When ``required`` is used, then client authentication succeeds, if the certificate signature verification succeeds. For ``optional`` the client cert will only be checked for ``PUT``, ``POST``, ``PATCH``, and ``DELETE`` requests.
|
||||
- **http\_extra\_headers**: (optional): HTTP headers let the REST API server pass additional information with an HTTP response.
|
||||
- **https\_extra\_headers**: (optional): HTTPS headers let the REST API server pass additional information with an HTTP response when TLS is enabled. This will also pass additional information set in ``http_extra_headers``.
|
||||
|
||||
- **certfile**: Specifies the file with the certificate in the PEM format. If the certfile is not specified or is left empty, the API server will work without SSL.
|
||||
- **keyfile**: Specifies the file with the secret key in the PEM format.
|
||||
- **cafile**: Specifies the file with the CA_BUNDLE with certificates of trusted CAs to use while verifying client certs.
|
||||
- **verify\_client**: ``none``, ``optional`` or ``required``. When ``none`` REST API will not check client certificates. When ``required`` client certificates are required for all REST API calls. When ``optional`` client certificates are required for all unsafe REST API endpoints. If ``verify_client`` is set to ``optional`` or ``required`` basic-auth is not checked.
|
||||
Here is an example of both **http_extra_headers** and **https_extra_headers**:
|
||||
|
||||
.. code:: YAML
|
||||
|
||||
restapi:
|
||||
listen: <listen>
|
||||
connect_address: <connect_address>
|
||||
authentication:
|
||||
username: <username>
|
||||
password: <password>
|
||||
http_extra_headers:
|
||||
'X-Frame-Options': 'SAMEORIGIN'
|
||||
'X-XSS-Protection': '1; mode=block'
|
||||
'X-Content-Type-Options': 'nosniff'
|
||||
cafile: <ca file>
|
||||
certfile: <cert>
|
||||
keyfile: <key>
|
||||
https_extra_headers:
|
||||
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'
|
||||
|
||||
.. _patronictl_settings:
|
||||
|
||||
CTL
|
||||
---
|
||||
- **Optional**:
|
||||
- **ctl**: (optional)
|
||||
- **insecure**: Allow connections to REST API without verifying SSL certs.
|
||||
- **cacert**: Specifies the file with the CA_BUNDLE file or directory with certificates of trusted CAs to use while verifying REST API SSL certs. If not provided patronictl will use the value provided for REST API "cafile" parameter.
|
||||
- **certfile**: Specifies the file with the client certificate in the PEM format. If not provided patronictl will use the value provided for REST API "certfile" parameter.
|
||||
|
||||
@@ -20,6 +20,8 @@ Patroni configuration is stored in the DCS (Distributed Configuration Store). Th
|
||||
It is possible to set/override some of the "Local" configuration parameters with environment variables.
|
||||
Environment configuration is very useful when you are running in a dynamic environment and you don't know some of the parameters in advance (for example it's not possible to know your external IP address when you are running inside ``docker``).
|
||||
|
||||
The local configuration can be either a single YAML file or a directory. When it is a directory, all YAML files in that directory are loaded one by one in sorted order. In case a key is defined in multiple files, the occurrence in the last file takes precedence.
|
||||
|
||||
Some of the PostgreSQL parameters must hold the same values on the master and the replicas. For those, values set either in the local patroni configuration files or via the environment variables take no effect. To alter or set their values one must change the shared configuration in the DCS. Below is the actual list of such parameters together with the default values:
|
||||
|
||||
- max_connections: 100
|
||||
@@ -35,6 +37,7 @@ For the parameters below, PostgreSQL does not require equal values among the mas
|
||||
- max_wal_senders: 5
|
||||
- max_replication_slots: 5
|
||||
- wal_keep_segments: 8
|
||||
- wal_keep_size: 128MB
|
||||
|
||||
These parameters are validated to ensure they are sane, or meet a minimum value.
|
||||
|
||||
@@ -76,6 +79,7 @@ Also, the following Patroni configuration options can be changed only dynamicall
|
||||
- loop_wait: 10
|
||||
- retry_timeouts: 10
|
||||
- maximum_lag_on_failover: 1048576
|
||||
- max_timelines_history: 0
|
||||
- check_timeline: false
|
||||
- postgresql.use_slots: true
|
||||
|
||||
|
||||
@@ -23,6 +23,21 @@ A Patroni cluster can be started with a data directory from a single-node Postgr
|
||||
3. Start Patroni (e.g. ``patroni /etc/patroni/patroni.yml``). It automatically detects that PostgreSQL daemon is already running but its configuration might be out-of-date.
|
||||
4. Ask Patroni to restart the node with ``patronictl restart cluster-name node-name``. This step is only required if PostgreSQL configuration is out-of-date.
|
||||
|
||||
Major Upgrade of PostgreSQL Version
|
||||
===================================
|
||||
|
||||
The only possible way to do a major upgrade currently is:
|
||||
|
||||
1. Stop Patroni
|
||||
2. Upgrade PostgreSQL binaries and perform `pg_upgrade <https://www.postgresql.org/docs/current/pgupgrade.html>`_ on the master node
|
||||
3. Update patroni.yml
|
||||
4. Remove the initialize key from DCS or wipe complete cluster state from DCS. The second one could be achieved by running ``patronictl remove <cluster-name>``. It is necessary because pg_upgrade runs initdb which actually creates a new database with a new PostgreSQL system identifier.
|
||||
5. If you wiped the cluster state in the previous step, you may wish to copy patroni.dynamic.json from old data dir to the new one. It will help you to retain some PostgreSQL parameters you had set before.
|
||||
6. Start Patroni on the master node.
|
||||
7. Upgrade PostgreSQL binaries, update patroni.yml and wipe the data_dir on standby nodes.
|
||||
8. Start Patroni on the standby nodes and wait for the replication to complete.
|
||||
|
||||
Running pg_upgrade on standby nodes is not supported by PostgreSQL. If you know what you are doing, you can try the rsync procedure described in https://www.postgresql.org/docs/current/pgupgrade.html instead of wiping data_dir on standby nodes. The safest way is however to let Patroni replicate the data for you.
|
||||
|
||||
FAQ
|
||||
---
|
||||
|
||||
@@ -22,6 +22,7 @@ We call Patroni a "template" because it is far from being a one-size-fits-all or
|
||||
rest_api
|
||||
ENVIRONMENT
|
||||
SETTINGS
|
||||
security
|
||||
replica_bootstrap
|
||||
replication_modes
|
||||
pause
|
||||
|
||||
@@ -3,6 +3,392 @@
|
||||
Release notes
|
||||
=============
|
||||
|
||||
Version 2.0.1
|
||||
-------------
|
||||
|
||||
**New features**
|
||||
|
||||
- Use ``more`` as pager in ``patronictl edit-config`` if ``less`` is not available (Pavel Golub)
|
||||
|
||||
On Windows it would be the ``more.com``. In addition to that, ``cdiff`` was changed to ``ydiff`` in ``requirements.txt``, but ``patronictl`` still supports both for compatibility.
|
||||
|
||||
- Added support of ``raft`` ``bind_addr`` and ``password`` (Alexander Kukushkin)
|
||||
|
||||
``raft.bind_addr`` might be useful when running behind NAT. ``raft.password`` enables traffic encryption (requires the ``cryptography`` module).
|
||||
|
||||
- Added ``sslpassword`` connection parameter support (Kostiantyn Nemchenko)
|
||||
|
||||
The connection parameter was introduced in PostgreSQL 13.
|
||||
|
||||
**Stability improvements**
|
||||
|
||||
- Changed the behavior in pause (Alexander)
|
||||
|
||||
1. Patroni will not call the ``bootstrap`` method if the ``PGDATA`` directory is missing/empty.
|
||||
2. Patroni will not exit on sysid mismatch in pause, only log a warning.
|
||||
3. The node will not try to grab the leader key in pause mode if Postgres is running not in recovery (accepting writes) but the sysid doesn't match with the initialize key.
|
||||
|
||||
- Apply ``master_start_timeout`` when executing crash recovery (Alexander)
|
||||
|
||||
If Postgres crashed on the leader node, Patroni does a crash-recovery by starting Postgres in single-user mode. During the crash-recovery the leader lock is being updated. If the crash-recovery didn't finish in ``master_start_timeout`` seconds, Patroni will stop it forcefully and release the leader lock.
|
||||
|
||||
- Removed the ``secure`` extra from the ``urllib3`` requirements (Alexander)
|
||||
|
||||
The only reason for adding it there was the ``ipaddress`` dependency for python 2.7.
|
||||
|
||||
**Bugfixes**
|
||||
|
||||
- Fixed a bug in the ``Kubernetes.update_leader()`` (Alexander)
|
||||
|
||||
An unhandled exception was preventing demoting the primary when the update of the leader object failed.
|
||||
|
||||
- Fixed hanging ``patronictl`` when RAFT is being used (Alexander)
|
||||
|
||||
When using ``patronictl`` with Patroni config, ``self_addr`` should be added to the ``partner_addrs``.
|
||||
|
||||
- Fixed bug in ``get_guc_value()`` (Alexander)
|
||||
|
||||
Patroni was failing to get the value of ``restore_command`` on PostgreSQL 12, therefore fetching missing WALs for ``pg_rewind`` didn't work.
|
||||
|
||||
|
||||
Version 2.0.0
|
||||
-------------
|
||||
|
||||
This version enhances compatibility with PostgreSQL 13, adds support of multiple synchronous standbys, has significant improvements in handling of ``pg_rewind``, adds support of Etcd v3 and Patroni on pure RAFT (without Etcd, Consul, or Zookeeper), and makes it possible to optionally call the ``pre_promote`` (fencing) script.
|
||||
|
||||
**PostgreSQL 13 support**
|
||||
|
||||
- Don't fire ``on_reload`` when promoting to ``standby_leader`` on PostgreSQL 13+ (Alexander Kukushkin)
|
||||
|
||||
When promoting to ``standby_leader`` we change ``primary_conninfo``, update the role and reload Postgres. Since ``on_role_change`` and ``on_reload`` effectively duplicate each other, Patroni will call only ``on_role_change``.
|
||||
|
||||
- Added support for ``gssencmode`` and ``channel_binding`` connection parameters (Alexander)
|
||||
|
||||
PostgreSQL 12 introduced ``gssencmode`` and 13 ``channel_binding`` connection parameters and now they can be used if defined in the ``postgresql.authentication`` section.
|
||||
|
||||
- Handle renaming of ``wal_keep_segments`` to ``wal_keep_size`` (Alexander)
|
||||
|
||||
In case of misconfiguration (``wal_keep_segments`` on 13 and ``wal_keep_size`` on older versions) Patroni will automatically adjust the configuration.
|
||||
|
||||
- Use ``pg_rewind`` with ``--restore-target-wal`` on 13 if possible (Alexander)
|
||||
|
||||
On PostgreSQL 13 Patroni checks if ``restore_command`` is configured and tells ``pg_rewind`` to use it.
|
||||
|
||||
|
||||
**New features**
|
||||
|
||||
- [BETA] Implemented support of Patroni on pure RAFT (Alexander)
|
||||
|
||||
This makes it possible to run Patroni without 3rd party dependencies, like Etcd, Consul, or Zookeeper. For HA you will have to run either three Patroni nodes or two nodes with Patroni and one node with ``patroni_raft_controller``. For more information please check the :ref:`documentation <raft_settings>`.
|
||||
|
||||
- [BETA] Implemented support for Etcd v3 protocol via gPRC-gateway (Alexander)
|
||||
|
||||
Etcd 3.0 was released more than four years ago and Etcd 3.4 has v2 disabled by default. There are also chances that v2 will be completely removed from Etcd, therefore we implemented support of Etcd v3 in Patroni. In order to start using it you have to explicitly create the ``etcd3`` section is the Patroni configuration file.
|
||||
|
||||
- Supporting multiple synchronous standbys (Krishna Sarabu)
|
||||
|
||||
It allows running a cluster with more than one synchronous replicas. The maximum number of synchronous replicas is controlled by the new parameter ``synchronous_node_count``. It is set to 1 by default and has no effect when the ``synchronous_mode`` is set to ``off``.
|
||||
|
||||
- Added possibility to call the ``pre_promote`` script (Sergey Dudoladov)
|
||||
|
||||
Unlike callbacks, the ``pre_promote`` script is called synchronously after acquiring the leader lock, but before promoting Postgres. If the script fails or exits with a non-zero exitcode, the current node will release the leader lock.
|
||||
|
||||
- Added support for configuration directories (Floris van Nee)
|
||||
|
||||
YAML files in the directory loaded and applied in alphabetical order.
|
||||
|
||||
- Advanced validation of PostgreSQL parameters (Alexander)
|
||||
|
||||
In case the specific parameter is not supported by the current PostgreSQL version or when its value is incorrect, Patroni will remove the parameter completely or try to fix the value.
|
||||
|
||||
- Wake up the main thread when the forced checkpoint after promote completed (Alexander)
|
||||
|
||||
Replicas are waiting for checkpoint indication via member key of the leader in DCS. The key is normally updated only once per HA loop. Without waking the main thread up, replicas will have to wait up to ``loop_wait`` seconds longer than necessary.
|
||||
|
||||
- Use of ``pg_stat_wal_recevier`` view on 9.6+ (Alexander)
|
||||
|
||||
The view contains up-to-date values of ``primary_conninfo`` and ``primary_slot_name``, while the contents of ``recovery.conf`` could be stale.
|
||||
|
||||
- Improved handing of IPv6 addresses in the Patroni config file (Mateusz Kowalski)
|
||||
|
||||
The IPv6 address is supposed to be enclosed into square brackets, but Patroni was expecting to get it plain. Now both formats are supported.
|
||||
|
||||
- Added Consul ``service_tags`` configuration parameter (Robert Edström)
|
||||
|
||||
They are useful for dynamic service discovery, for example by load balancers.
|
||||
|
||||
- Implemented SSL support for Zookeeper (Kostiantyn Nemchenko)
|
||||
|
||||
It requires ``kazoo>=2.6.0``.
|
||||
|
||||
- Implemented ``no_params`` option for custom bootstrap method (Kostiantyn)
|
||||
|
||||
It allows calling ``wal-g``, ``pgBackRest`` and other backup tools without wrapping them into shell scripts.
|
||||
|
||||
- Move WAL and tablespaces after a failed init (Feike Steenbergen)
|
||||
|
||||
When doing ``reinit``, Patroni was already removing not only ``PGDATA`` but also the symlinked WAL directory and tablespaces. Now the ``move_data_directory()`` method will do a similar job, i.e. rename WAL directory and tablespaces and update symlinks in PGDATA.
|
||||
|
||||
|
||||
**Improved in pg_rewind support**
|
||||
|
||||
- Improved timeline divergence check (Alexander)
|
||||
|
||||
We don't need to rewind when the replayed location on the replica is not ahead of the switchpoint or the end of the checkpoint record on the former primary is the same as the switchpoint. In order to get the end of the checkpoint record we use ``pg_waldump`` and parse its output.
|
||||
|
||||
- Try to fetch missing WAL if ``pg_rewind`` complains about it (Alexander)
|
||||
|
||||
It could happen that the WAL segment required for ``pg_rewind`` doesn't exist in the ``pg_wal`` directory anymore and therefore ``pg_rewind`` can't find the checkpoint location before the divergence point. Starting from PostgreSQL 13 ``pg_rewind`` could use ``restore_command`` for fetching missing WALs. For older PostgreSQL versions Patroni parses the errors of a failed rewind attempt and tries to fetch the missing WAL by calling the ``restore_command`` on its own.
|
||||
|
||||
- Detect a new timeline in the standby cluster and trigger rewind/reinitialize if necessary (Alexander)
|
||||
|
||||
The ``standby_cluster`` is decoupled from the primary cluster and therefore doesn't immediately know about leader elections and timeline switches. In order to detect the fact, the ``standby_leader`` periodically checks for new history files in ``pg_wal``.
|
||||
|
||||
- Shorten and beautify history log output (Alexander)
|
||||
|
||||
When Patroni is trying to figure out the necessity of ``pg_rewind``, it could write the content of the history file from the primary into the log. The history file is growing with every failover/switchover and eventually starts taking up too many lines, most of which are not so useful. Instead of showing the raw data, Patroni will show only 3 lines before the current replica timeline and 2 lines after.
|
||||
|
||||
|
||||
**Improvements on K8s**
|
||||
|
||||
- Get rid of ``kubernetes`` python module (Alexander)
|
||||
|
||||
The official python kubernetes client contains a lot of auto-generated code and therefore very heavy. Patroni uses only a small fraction of K8s API endpoints and implementing support for them wasn't hard.
|
||||
|
||||
- Make it possible to bypass the ``kubernetes`` service (Alexander)
|
||||
|
||||
When running on K8s, Patroni is usually communicating with the K8s API via the ``kubernetes`` service, the address of which is exposed in the ``KUBERNETES_SERVICE_HOST`` environment variable. Like any other service, the ``kubernetes`` service is handled by ``kube-proxy``, which in turn, depending on the configuration, is either relying on a userspace program or ``iptables`` for traffic routing. Skipping the intermediate component and connecting directly to the K8s master nodes allows us to implement a better retry strategy and mitigate risks of demoting Postgres when K8s master nodes are upgraded.
|
||||
|
||||
- Sync HA loops of all pods of a Patroni cluster (Alexander)
|
||||
|
||||
Not doing so was increasing failure detection time from ``ttl`` to ``ttl + loop_wait``.
|
||||
|
||||
- Populate ``references`` and ``nodename`` in the subsets addresses on K8s (Alexander)
|
||||
|
||||
Some load-balancers are relying on this information.
|
||||
|
||||
- Fix possible race conditions in the ``update_leader()`` (Alexander)
|
||||
|
||||
The concurrent update of the leader configmap or endpoint happening outside of Patroni might cause the ``update_leader()`` call to fail. In this case Patroni rechecks that the current node is still owning the leader lock and repeats the update.
|
||||
|
||||
- Explicitly disallow patching non-existent config (Alexander)
|
||||
|
||||
For DCS other than ``kubernetes`` the PATCH call is failing with an exception due to ``cluster.config`` being ``None``, but on Kubernetes it was happily creating the config annotation and preventing writing bootstrap configuration after the bootstrap finished.
|
||||
|
||||
- Fix bug in ``pause`` (Alexander)
|
||||
|
||||
Replicas were removing ``primary_conninfo`` and restarting Postgres when the leader key was absent, but they should do nothing.
|
||||
|
||||
|
||||
**Improvements in REST API**
|
||||
|
||||
- Defer TLS handshake until worker thread has started (Alexander, Ben Harris)
|
||||
|
||||
If the TLS handshake was done in the API thread and the client-side didn't send any data, the API thread was blocked (risking DoS).
|
||||
|
||||
- Check ``basic-auth`` independently from client certificate in REST API (Alexander)
|
||||
|
||||
Previously only the client certificate was validated. Doing two checks independently is an absolutely valid use-case.
|
||||
|
||||
- Write double ``CRLF`` after HTTP headers of the ``OPTIONS`` request (Sergey Burladyan)
|
||||
|
||||
HAProxy was happy with a single ``CRLF``, while Consul health-check complained about broken connection and unexpected EOF.
|
||||
|
||||
- ``GET /cluster`` was showing stale members info for Zookeeper (Alexander)
|
||||
|
||||
The endpoint was using the Patroni internal cluster view. For Patroni itself it didn't cause any issues, but when exposed to the outside world we need to show up-to-date information, especially replication lag.
|
||||
|
||||
- Fixed health-checks for standby cluster (Alexander)
|
||||
|
||||
The ``GET /standby-leader`` for a master and ``GET /master`` for a ``standby_leader`` were incorrectly responding with 200.
|
||||
|
||||
- Implemented ``DELETE /switchover`` (Alexander)
|
||||
|
||||
The REST API call deletes the scheduled switchover.
|
||||
|
||||
- Created ``/readiness`` and ``/liveness`` endpoints (Alexander)
|
||||
|
||||
They could be useful to eliminate "unhealthy" pods from subsets addresses when the K8s service is used with label selectors.
|
||||
|
||||
- Enhanced ``GET /replica`` and ``GET /async`` REST API health-checks (Krishna, Alexander)
|
||||
|
||||
Checks now support optional keyword ``?lag=<max-lag>`` and will respond with 200 only if the lag is smaller than the supplied value. If relying on this feature please keep in mind that information about WAL position on the leader is updated only every ``loop_wait`` seconds!
|
||||
|
||||
- Added support for user defined HTTP headers in the REST API response (Yogesh Sharma)
|
||||
|
||||
This feature might be useful if requests are made from a browser.
|
||||
|
||||
|
||||
**Improvements in patronictl**
|
||||
|
||||
- Don't try to call non-existing leader in ``patronictl pause`` (Alexander)
|
||||
|
||||
While pausing a cluster without a leader on K8s, ``patronictl`` was showing warnings that member "None" could not be accessed.
|
||||
|
||||
- Handle the case when member ``conn_url`` is missing (Alexander)
|
||||
|
||||
On K8s it is possible that the pod doesn't have the necessary annotations because Patroni is not yet running. It was making ``patronictl`` to fail.
|
||||
|
||||
- Added ability to print ASCII cluster topology (Maxim Fedotov, Alexander)
|
||||
|
||||
It is very useful to get overview of the cluster with cascading replication.
|
||||
|
||||
- Implement ``patronictl flush switchover`` (Alexander)
|
||||
|
||||
Before that ``patronictl flush`` only supported cancelling scheduled restarts.
|
||||
|
||||
|
||||
**Bugfixes**
|
||||
|
||||
- Attribute error during bootstrap of the cluster with existing PGDATA (Krishna)
|
||||
|
||||
When trying to create/update the ``/history`` key, Patroni was accessing the ``ClusterConfig`` object which wasn't created in DCS yet.
|
||||
|
||||
- Improved exception handling in Consul (Alexander)
|
||||
|
||||
Unhandled exception in the ``touch_member()`` method caused the whole Patroni process to crash.
|
||||
|
||||
- Enforce ``synchronous_commit=local`` for the ``post_init`` script (Alexander)
|
||||
|
||||
Patroni was already doing that when creating users (``replication``, ``rewind``), but missing it in the case of ``post_init`` was an oversight. As a result, if the script wasn't doing it internally on it's own the bootstrap in ``synchronous_mode`` wasn't able to finish.
|
||||
|
||||
- Increased ``maxsize`` in the Consul pool manager (ponvenkates)
|
||||
|
||||
With the default ``size=1`` some warnings were generated.
|
||||
|
||||
- Patroni was wrongly reporting Postgres as running (Alexander)
|
||||
|
||||
The state wasn't updated when for example Postgres crashed due to an out-of-disk error.
|
||||
|
||||
- Put ``*`` into ``pgpass`` instead of missing or empty values (Alexander)
|
||||
|
||||
If for example the ``standby_cluster.port`` is not specified, the ``pgpass`` file was incorrectly generated.
|
||||
|
||||
- Skip physical replication slot creation on the leader node with special characters (Krishna)
|
||||
|
||||
Patroni appeared to be creating a dormant slot (when ``slots`` defined) for the leader node when the name contained special chars such as '-' (for e.g. "abc-us-1").
|
||||
|
||||
- Avoid removing non-existent ``pg_hba.conf`` in the custom bootstrap (Krishna)
|
||||
|
||||
Patroni was failing if ``pg_hba.conf`` happened to be located outside of the ``pgdata`` dir after custom bootstrap.
|
||||
|
||||
|
||||
Version 1.6.5
|
||||
-------------
|
||||
|
||||
**New features**
|
||||
|
||||
- Master stop timeout (Krishna Sarabu)
|
||||
|
||||
The number of seconds Patroni is allowed to wait when stopping Postgres. Effective only when ``synchronous_mode`` is enabled. When set to value greater than 0 and the ``synchronous_mode`` is enabled, Patroni sends ``SIGKILL`` to the postmaster if the stop operation is running for more than the value set by ``master_stop_timeout``. Set the value according to your durability/availability tradeoff. If the parameter is not set or set to non-positive value, ``master_stop_timeout`` does not have an effect.
|
||||
|
||||
- Don't create permanent physical slot with name of the primary (Alexander Kukushkin)
|
||||
|
||||
It is a common problem that the primary recycles WAL segments while the replica is down. Now we have a good solution for static clusters, with a fixed number of nodes and names that never change. You just need to list the names of all nodes in the ``slots`` so the primary will not remove the slot when the node is down (not registered in DCS).
|
||||
|
||||
- First draft of Config Validator (Igor Yanchenko)
|
||||
|
||||
Use ``patroni --validate-config patroni.yaml`` in order to validate Patroni configuration.
|
||||
|
||||
- Possibility to configure max length of timelines history (Krishna)
|
||||
|
||||
Patroni writes the history of failovers/switchovers into the ``/history`` key in DCS. Over time the size of this key becomes big, but in most cases only the last few lines are interesting. The ``max_timelines_history`` parameter allows to specify the maximum number of timeline history items to be kept in DCS.
|
||||
|
||||
- Kazoo 2.7.0 compatibility (Danyal Prout)
|
||||
|
||||
Some non-public methods in Kazoo changed their signatures, but Patroni was relying on them.
|
||||
|
||||
|
||||
**Improvements in patronictl**
|
||||
|
||||
- Show member tags (Kostiantyn Nemchenko, Alexander)
|
||||
|
||||
Tags are configured individually for every node and there was no easy way to get an overview of them
|
||||
|
||||
- Improve members output (Alexander)
|
||||
|
||||
The redundant cluster name won't be shown anymore on every line, only in the table header.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ patronictl list
|
||||
+ Cluster: batman (6813309862653668387) +---------+----+-----------+---------------------+
|
||||
| Member | Host | Role | State | TL | Lag in MB | Tags |
|
||||
+-------------+----------------+--------+---------+----+-----------+---------------------+
|
||||
| postgresql0 | 127.0.0.1:5432 | Leader | running | 3 | | clonefrom: true |
|
||||
| | | | | | | noloadbalance: true |
|
||||
| | | | | | | nosync: true |
|
||||
+-------------+----------------+--------+---------+----+-----------+---------------------+
|
||||
| postgresql1 | 127.0.0.1:5433 | | running | 3 | 0.0 | |
|
||||
+-------------+----------------+--------+---------+----+-----------+---------------------+
|
||||
|
||||
- Fail if a config file is specified explicitly but not found (Kaarel Moppel)
|
||||
|
||||
Previously ``patronictl`` was only reporting a ``DEBUG`` message.
|
||||
|
||||
- Solved the problem of not initialized K8s pod breaking patronictl (Alexander)
|
||||
|
||||
Patroni is relying on certain pod annotations on K8s. When one of the Patroni pods is stopping or starting there is no valid annotation yet and ``patronictl`` was failing with an exception.
|
||||
|
||||
|
||||
**Stability improvements**
|
||||
|
||||
- Apply 1 second backoff if LIST call to K8s API server failed (Alexander)
|
||||
|
||||
It is mostly necessary to avoid flooding logs, but also helps to prevent starvation of the main thread.
|
||||
|
||||
- Retry if the ``retry-after`` HTTP header is returned by K8s API (Alexander)
|
||||
|
||||
If the K8s API server is overwhelmed with requests it might ask to retry.
|
||||
|
||||
- Scrub ``KUBERNETES_`` environment from the postmaster (Feike Steenbergen)
|
||||
|
||||
The ``KUBERNETES_`` environment variables are not required for PostgreSQL, yet having them exposed to the postmaster will also expose them to backends and to regular database users (using pl/perl for example).
|
||||
|
||||
- Clean up tablespaces on reinitialize (Krishna)
|
||||
|
||||
During reinit, Patroni was removing only ``PGDATA`` and leaving user-defined tablespace directories. This is causing Patroni to loop in reinit. The previous workarond for the problem was implementing the :ref:`custom bootstrap <custom_bootstrap>` script.
|
||||
|
||||
- Explicitly execute ``CHECKPOINT`` after promote happened (Alexander)
|
||||
|
||||
It helps to reduce the time before the new primary is usable for ``pg_rewind``.
|
||||
|
||||
- Smart refresh of Etcd members (Alexander)
|
||||
|
||||
In case Patroni failed to execute a request on all members of the Etcd cluster, Patroni will re-check ``A`` or ``SRV`` records for changes of IPs/hosts before retrying the next time.
|
||||
|
||||
- Skip missing values from ``pg_controldata`` (Feike)
|
||||
|
||||
Values are missing when trying to use binaries of a version that doesn't match PGDATA. Patroni will try to start Postgres anyway, and Postgres will complain that the major version doesn't match and abort with an error.
|
||||
|
||||
|
||||
**Bugfixes**
|
||||
|
||||
- Disable SSL verification for Consul when required (Julien Riou)
|
||||
|
||||
Starting from a certain version of ``urllib3``, the ``cert_reqs`` must be explicitly set to ``ssl.CERT_NONE`` in order to effectively disable SSL verification.
|
||||
|
||||
- Avoid opening replication connection on every cycle of HA loop (Alexander)
|
||||
|
||||
Regression was introduced in 1.6.4.
|
||||
|
||||
- Call ``on_role_change`` callback on failed primary (Alexander)
|
||||
|
||||
In certain cases it could lead to the virtual IP remaining attached to the old primary. Regression was introduced in 1.4.5.
|
||||
|
||||
- Reset rewind state if postgres started after successful pg_rewind (Alexander)
|
||||
|
||||
As a result of this bug Patroni was starting up manually shut down postgres in the pause mode.
|
||||
|
||||
- Convert ``recovery_min_apply_delay`` to ``ms`` when checking ``recovery.conf``
|
||||
|
||||
Patroni was indefinitely restarting replica if ``recovery_min_apply_delay`` was configured on PostgreSQL older than 12.
|
||||
|
||||
- PyInstaller compatibility (Alexander)
|
||||
|
||||
PyInstaller freezes (packages) Python applications into stand-alone executables. The compatibility was broken when we switched to the ``spawn`` method instead of ``fork`` for ``multiprocessing``.
|
||||
|
||||
|
||||
Version 1.6.4
|
||||
-------------
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ arguments to them, i.e. the name of the cluster and the path to the data directo
|
||||
<custom_bootstrap_method_name>:
|
||||
command: <path_to_custom_bootstrap_script> [param1 [, ...]]
|
||||
keep_existing_recovery_conf: False
|
||||
no_params: False
|
||||
recovery_conf:
|
||||
recovery_target_action: promote
|
||||
recovery_target_timeline: latest
|
||||
@@ -40,6 +41,8 @@ in the configuration files, Patroni supplies two cluster-specific ones:
|
||||
--datadir
|
||||
Path to the data directory of the cluster instance to be bootstrapped
|
||||
|
||||
Passing these two additional flags can be disabled by setting a special ``no_params`` parameter to ``True``.
|
||||
|
||||
If the bootstrap script returns 0, Patroni tries to configure and start the PostgreSQL instance produced by it. If any
|
||||
of the intermediate steps fail, or the script returns a non-zero value, Patroni assumes that the bootstrap has failed,
|
||||
cleans up after itself and releases the initialize lock to give another node the opportunity to bootstrap.
|
||||
|
||||
@@ -57,11 +57,16 @@ You can ensure that a standby never becomes the synchronous standby by setting `
|
||||
|
||||
Synchronous mode can be switched on and off via Patroni REST interface. See :ref:`dynamic configuration <dynamic_configuration>` for instructions.
|
||||
|
||||
Note: Because of the way synchronous replication is implemented in PostgreSQL it is still possible to lose transactions even when using ``synchronous_mode_strict``. If the PostgreSQL backend is cancelled while waiting to acknowledge replication (as a result of packet cancellation due to client timeout or backend failure) transaction changes become visible for other backends. Such changes are not yet replicated and may be lost in case of standby promotion.
|
||||
|
||||
Synchronous Replication Factor
|
||||
------------------------------
|
||||
The parameter ``synchronous_node_count`` is used by Patroni to manage number of synchronous standby databases. It is set to 1 by default. It has no effect when ``synchronous_mode`` is set to off. When enabled, Patroni manages precise number of synchronous standby databases based on parameter ``synchronous_node_count`` and adjusts the state in DCS & synchronous_standby_names as members join and leave.
|
||||
|
||||
Synchronous mode implementation
|
||||
-------------------------------
|
||||
|
||||
When in synchronous mode Patroni maintains synchronization state in the DCS, containing the latest primary and current synchronous standby. This state is updated with strict ordering constraints to ensure the following invariants:
|
||||
When in synchronous mode Patroni maintains synchronization state in the DCS, containing the latest primary and current synchronous standby databases. This state is updated with strict ordering constraints to ensure the following invariants:
|
||||
|
||||
- A node must be marked as the latest leader whenever it can accept write transactions. Patroni crashing or PostgreSQL not shutting down can cause violations of this invariant.
|
||||
|
||||
@@ -69,9 +74,9 @@ When in synchronous mode Patroni maintains synchronization state in the DCS, con
|
||||
|
||||
- A node that is not the leader or current synchronous standby is not allowed to promote itself automatically.
|
||||
|
||||
Patroni will only ever assign one standby to ``synchronous_standby_names`` because with multiple candidates it is not possible to know which node was acting as synchronous during the failure.
|
||||
Patroni will only assign one or more synchronous standby nodes based on ``synchronous_node_count`` parameter to ``synchronous_standby_names``.
|
||||
|
||||
On each HA loop iteration Patroni re-evaluates synchronous standby choice. If the current synchronous standby is connected and has not requested its synchronous status to be removed it remains picked. Otherwise the cluster member available for sync that is furthest ahead in replication is picked.
|
||||
On each HA loop iteration Patroni re-evaluates synchronous standby nodes choice. If the current list of synchronous standby nodes are connected and has not requested its synchronous status to be removed it remains picked. Otherwise the cluster member available for sync that is furthest ahead in replication is picked.
|
||||
|
||||
|
||||
.. [1] The data is still there, but recovering it requires a manual recovery effort by data recovery specialists. When Patroni is allowed to rewind with ``use_pg_rewind`` the forked timeline will be automatically erased to rejoin the failed primary with the cluster.
|
||||
|
||||
+50
-7
@@ -19,6 +19,13 @@ For all health check ``GET`` requests Patroni returns a JSON document with the s
|
||||
|
||||
- ``GET /replica``: replica health check endpoint. It returns HTTP status code **200** only when the Patroni node is in the state ``running``, the role is ``replica`` and ``noloadbalance`` tag is not set.
|
||||
|
||||
- ``GET /replica?lag=<max-lag>``: replica check endpoint. In addition to checks from ``replica``, it also checks replication latency and returns status code **200** only when it is below specified value. The key cluster.last_leader_operation from DCS is used for Leader wal position and compute latency on replica for performance reasons. max-lag can be specified in bytes (integer) or in human readable values, for e.g. 16kB, 64MB, 1GB.
|
||||
|
||||
- ``GET /replica?lag=1048576``
|
||||
- ``GET /replica?lag=1024kB``
|
||||
- ``GET /replica?lag=10MB``
|
||||
- ``GET /replica?lag=1GB``
|
||||
|
||||
- ``GET /read-only``: like the above endpoint, but also includes the primary.
|
||||
|
||||
- ``GET /standby-leader``: returns HTTP status code **200** only when the Patroni node is running as the leader in a :ref:`standby cluster <standby_cluster>`.
|
||||
@@ -27,8 +34,45 @@ For all health check ``GET`` requests Patroni returns a JSON document with the s
|
||||
|
||||
- ``GET /asynchronous`` or ``GET /async``: returns HTTP status code **200** only when the Patroni node is running as an asynchronous standby.
|
||||
|
||||
- ``GET /asynchronous?lag=<max-lag>`` or ``GET /async?lag=<max-lag>``: asynchronous standby check endpoint. In addition to checks from ``asynchronous`` or ``async``, it also checks replication latency and returns status code **200** only when it is below specified value. The key cluster.last_leader_operation from DCS is used for Leader wal position and compute latency on replica for performance reasons. max-lag can be specified in bytes (integer) or in human readable values, for e.g. 16kB, 64MB, 1GB.
|
||||
|
||||
- ``GET /async?lag=1048576``
|
||||
- ``GET /async?lag=1024kB``
|
||||
- ``GET /async?lag=10MB``
|
||||
- ``GET /async?lag=1GB``
|
||||
|
||||
- ``GET /health``: returns HTTP status code **200** only when PostgreSQL is up and running.
|
||||
|
||||
- ``GET /liveness``: always returns HTTP status code **200** what only indicates that Patroni is running. Could be used for ``livenessProbe``.
|
||||
|
||||
- ``GET /readiness``: returns HTTP status code **200** when the Patroni node is running as the leader or when PostgreSQL is up and running. The endpoint could be used for ``readinessProbe`` when it is not possible to use Kubenetes endpoints for leader elections (OpenShift).
|
||||
|
||||
Both, ``readiness`` and ``liveness`` endpoints are very light-weight and not executing any SQL. Probes should be configured in such a way that they start failing about time when the leader key is expiring. With the default value of ``ttl``, which is ``30s`` example probes would look like:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
scheme: HTTP
|
||||
path: /readiness
|
||||
port: 8008
|
||||
initialDelaySeconds: 3
|
||||
periodSeconds: 10
|
||||
timeoutSeconds: 5
|
||||
successThreshold: 1
|
||||
failureThreshold: 3
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
scheme: HTTP
|
||||
path: /liveness
|
||||
port: 8008
|
||||
initialDelaySeconds: 3
|
||||
periodSeconds: 10
|
||||
timeoutSeconds: 5
|
||||
successThreshold: 1
|
||||
failureThreshold: 3
|
||||
|
||||
|
||||
Monitoring endpoint
|
||||
-------------------
|
||||
|
||||
@@ -149,7 +193,6 @@ Config endpoint
|
||||
"parameters": {
|
||||
"hot_standby": "on",
|
||||
"wal_log_hints": "on",
|
||||
"wal_keep_segments": 8,
|
||||
"wal_level": "hot_standby",
|
||||
"max_wal_senders": 5,
|
||||
"max_replication_slots": 5,
|
||||
@@ -177,7 +220,6 @@ Config endpoint
|
||||
"parameters": {
|
||||
"hot_standby": "on",
|
||||
"wal_log_hints": "on",
|
||||
"wal_keep_segments": 8,
|
||||
"wal_level": "hot_standby",
|
||||
"max_wal_senders": 5,
|
||||
"max_replication_slots": 5,
|
||||
@@ -229,7 +271,6 @@ If you want to remove (reset) some setting just patch it with ``null``:
|
||||
"parameters": {
|
||||
"hot_standby": "on",
|
||||
"unix_socket_directories": ".",
|
||||
"wal_keep_segments": 8,
|
||||
"wal_level": "hot_standby",
|
||||
"wal_log_hints": "on",
|
||||
"max_wal_senders": 5,
|
||||
@@ -245,7 +286,7 @@ The above call removes ``postgresql.parameters.max_connections`` from the dynami
|
||||
.. code-block:: bash
|
||||
|
||||
$ curl -s -XPUT -d \
|
||||
'{"maximum_lag_on_failover":1048576,"retry_timeout":10,"postgresql":{"use_slots":true,"use_pg_rewind":true,"parameters":{"hot_standby":"on","wal_log_hints":"on","wal_keep_segments":8,"wal_level":"hot_standby","unix_socket_directories":".","max_wal_senders":5}},"loop_wait":3,"ttl":20}' \
|
||||
'{"maximum_lag_on_failover":1048576,"retry_timeout":10,"postgresql":{"use_slots":true,"use_pg_rewind":true,"parameters":{"hot_standby":"on","wal_log_hints":"on","wal_level":"hot_standby","unix_socket_directories":".","max_wal_senders":5}},"loop_wait":3,"ttl":20}' \
|
||||
http://localhost:8008/config | jq .
|
||||
{
|
||||
"ttl": 20,
|
||||
@@ -256,7 +297,6 @@ The above call removes ``postgresql.parameters.max_connections`` from the dynami
|
||||
"parameters": {
|
||||
"hot_standby": "on",
|
||||
"unix_socket_directories": ".",
|
||||
"wal_keep_segments": 8,
|
||||
"wal_level": "hot_standby",
|
||||
"wal_log_hints": "on",
|
||||
"max_wal_senders": 5
|
||||
@@ -299,7 +339,10 @@ Example: schedule a switchover from the leader to any other healthy replica in t
|
||||
|
||||
Depending on the situation the request might finish with a different HTTP status code and body. The status code **200** is returned when the switchover or failover successfully completed. If the switchover was successfully scheduled, Patroni will return HTTP status code **202**. In case something went wrong, the error status code (one of **400**, **412** or **503**) will be returned with some details in the response body. For more information please check the source code of ``patroni/api.py:do_POST_failover()`` method.
|
||||
|
||||
The switchover and failover endpoints are used by ``patronictl switchover`` and ``patronictl failover``, respectively.
|
||||
- ``DELETE /switchover``: delete the scheduled switchover
|
||||
|
||||
The ``POST /switchover`` and ``POST failover`` endpoints are used by ``patronictl switchover`` and ``patronictl failover``, respectively.
|
||||
The ``DELETE /switchover`` is used by ``patronictl flush <cluster-name> switchover``.
|
||||
|
||||
|
||||
Restart endpoint
|
||||
@@ -315,7 +358,7 @@ Restart endpoint
|
||||
|
||||
- ``DELETE /restart``: delete the scheduled restart
|
||||
|
||||
``POST /restart`` and ``DELETE /restart`` endpoints are used by ``patronictl restart`` and ``patronictl flush`` respectively.
|
||||
``POST /restart`` and ``DELETE /restart`` endpoints are used by ``patronictl restart`` and ``patronictl flush <cluster-name> restart`` respectively.
|
||||
|
||||
|
||||
Reload endpoint
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
.. _security:
|
||||
|
||||
=======================
|
||||
Security Considerations
|
||||
=======================
|
||||
|
||||
A Patroni cluster has two interfaces to be protected from unauthorized access: the distributed configuration storage (DCS) and the Patroni REST API.
|
||||
|
||||
Protecting DCS
|
||||
==============
|
||||
|
||||
Patroni and patronictl both store and retrieve data to/from the DCS.
|
||||
|
||||
Despite DCS doesn't contain any sensitive information, it allows changing some of Patroni/Postgres configuration. Therefore the very first thing that should be protected is DCS itself.
|
||||
|
||||
The details of protection depend on the type of DCS used. The authentication and encryption parameters (tokens/basic-auth/client certificates) for the supported types of DCS are covered in :ref:`SETTINGS <bootstrap_settings>`
|
||||
|
||||
The general recommendation is to enable TLS for all DCS communication.
|
||||
|
||||
Protecting the REST API
|
||||
=======================
|
||||
|
||||
Protecting the REST API is a more complicated task.
|
||||
|
||||
The Patroni REST API is used by Patroni itself during the leader race, by the ``patronictl`` tool in order to perform failovers/switchovers/reinitialize/restarts/reloads, by HAProxy or any other kind of load balancer to perform HTTP health checks, and of course could also be used for monitoring.
|
||||
|
||||
From the point of view of security, REST API contains safe (``GET`` requests, only retrieve information) and unsafe (``PUT``, ``POST``, ``PATCH`` and ``DELETE`` requests, change the state of nodes) endpoints.
|
||||
|
||||
The unsafe endpoints can be protected with HTTP basic-auth by setting the ``restapi.authentication.username`` and ``restapi.authentication.password`` parameters. There is no way to protect the safe endpoints without enabling TLS.
|
||||
|
||||
When TLS for the REST API is enabled and a PKI is established, mutual authentication of the API server and API client is possible for all endpoints.
|
||||
|
||||
The ``restapi`` section parameters enable TLS client authentication to the server. Depending on the value of the ``verify_client`` parameter, the API server requires a successful client certificate verification for both safe and unsafe API calls (``verify_client: required``), or only for unsafe API calls (``verify_client: optional``), or for no API calls (``verify_client: none``).
|
||||
|
||||
The ``ctl`` section parameters enable TLS server authentication to the client (the ``patronictl`` tool which uses the same config as patroni). Set ``insecure: true`` to disable the server certificate verification by the client. See :ref:`SETTINGS <patronictl_settings>` for a detailed description of the TLS client parameters.
|
||||
|
||||
Protecting the PostgreSQL database proper from unauthorized access is beyond the scope of this document and is covered in https://www.postgresql.org/docs/current/client-authentication.html
|
||||
@@ -14,7 +14,8 @@ Group=postgres
|
||||
# Read in configuration file if it exists, otherwise proceed
|
||||
EnvironmentFile=-/etc/patroni_env.conf
|
||||
|
||||
WorkingDirectory=~
|
||||
# the default is the user's home directory, and if you want to change it, you must provide an absolute path.
|
||||
# WorkingDirectory=/home/sameuser
|
||||
|
||||
# Where to send early-startup messages from the server
|
||||
# This is normally controlled by the global default set by systemd
|
||||
|
||||
@@ -28,6 +28,27 @@ Feature: basic replication
|
||||
When I issue a GET request to http://127.0.0.1:8009/async
|
||||
Then I receive a response code 200
|
||||
|
||||
Scenario: check multi sync replication
|
||||
Given I issue a PATCH request to http://127.0.0.1:8008/config with {"synchronous_node_count": 2}
|
||||
Then I receive a response code 200
|
||||
And I sleep for 10 seconds
|
||||
Then "sync" key in DCS has sync_standby=postgres1,postgres2 after 5 seconds
|
||||
When I issue a GET request to http://127.0.0.1:8010/sync
|
||||
Then I receive a response code 200
|
||||
When I issue a GET request to http://127.0.0.1:8009/sync
|
||||
Then I receive a response code 200
|
||||
When I issue a PATCH request to http://127.0.0.1:8008/config with {"synchronous_node_count": 1}
|
||||
Then I receive a response code 200
|
||||
And I shut down postgres1
|
||||
And I sleep for 10 seconds
|
||||
Then "sync" key in DCS has sync_standby=postgres2 after 10 seconds
|
||||
When I start postgres1
|
||||
And "members/postgres1" key in DCS has state=running after 10 seconds
|
||||
When I issue a GET request to http://127.0.0.1:8010/sync
|
||||
Then I receive a response code 200
|
||||
When I issue a GET request to http://127.0.0.1:8009/async
|
||||
Then I receive a response code 200
|
||||
|
||||
Scenario: check the basic failover in synchronous mode
|
||||
Given I run patronictl.py pause batman
|
||||
Then I receive a response returncode 0
|
||||
|
||||
+89
-13
@@ -165,6 +165,11 @@ class PatroniController(AbstractController):
|
||||
config = yaml.safe_load(f)
|
||||
config.pop('etcd', None)
|
||||
|
||||
raft_port = os.environ.get('RAFT_PORT')
|
||||
if raft_port:
|
||||
os.environ['RAFT_PORT'] = str(int(raft_port) + 1)
|
||||
config['raft'] = {'data_dir': self._output_dir, 'self_addr': 'localhost:' + os.environ['RAFT_PORT']}
|
||||
|
||||
host = config['postgresql']['listen'].split(':')[0]
|
||||
|
||||
config['postgresql']['listen'] = config['postgresql']['connect_address'] = '{0}:{1}'.format(host, self.__PORT)
|
||||
@@ -373,21 +378,36 @@ class ConsulController(AbstractDcsController):
|
||||
super(ConsulController, self).start(max_wait_limit)
|
||||
|
||||
|
||||
class EtcdController(AbstractDcsController):
|
||||
class AbstractEtcdController(AbstractDcsController):
|
||||
|
||||
""" handles all etcd related tasks, used for the tests setup and cleanup """
|
||||
|
||||
def __init__(self, context):
|
||||
super(EtcdController, self).__init__(context)
|
||||
os.environ['PATRONI_ETCD_HOST'] = 'localhost:2379'
|
||||
|
||||
import etcd
|
||||
self._client = etcd.Client(port=2379)
|
||||
def __init__(self, context, client_cls):
|
||||
super(AbstractEtcdController, self).__init__(context)
|
||||
self._client_cls = client_cls
|
||||
|
||||
def _start(self):
|
||||
return subprocess.Popen(["etcd", "--debug", "--data-dir", self._work_directory],
|
||||
stdout=self._log, stderr=subprocess.STDOUT)
|
||||
|
||||
def _is_running(self):
|
||||
from patroni.dcs.etcd import DnsCachingResolver
|
||||
# if etcd is running, but we didn't start it
|
||||
try:
|
||||
self._client = self._client_cls({'host': 'localhost', 'port': 2379, 'retry_timeout': 30,
|
||||
'patronictl': 1}, DnsCachingResolver())
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
class EtcdController(AbstractEtcdController):
|
||||
|
||||
def __init__(self, context):
|
||||
from patroni.dcs.etcd import EtcdClient
|
||||
super(EtcdController, self).__init__(context, EtcdClient)
|
||||
os.environ['PATRONI_ETCD_HOST'] = 'localhost:2379'
|
||||
|
||||
def query(self, key, scope='batman'):
|
||||
import etcd
|
||||
try:
|
||||
@@ -404,12 +424,25 @@ class EtcdController(AbstractDcsController):
|
||||
except Exception as e:
|
||||
assert False, "exception when cleaning up etcd contents: {0}".format(e)
|
||||
|
||||
def _is_running(self):
|
||||
# if etcd is running, but we didn't start it
|
||||
|
||||
class Etcd3Controller(AbstractEtcdController):
|
||||
|
||||
def __init__(self, context):
|
||||
from patroni.dcs.etcd3 import Etcd3Client
|
||||
super(Etcd3Controller, self).__init__(context, Etcd3Client)
|
||||
os.environ['PATRONI_ETCD3_HOST'] = 'localhost:2379'
|
||||
|
||||
def query(self, key, scope='batman'):
|
||||
import base64
|
||||
response = self._client.range(self.path(key, scope))
|
||||
for k in response.get('kvs', []):
|
||||
return base64.b64decode(k['value']).decode('utf-8') if 'value' in k else None
|
||||
|
||||
def cleanup_service_tree(self):
|
||||
try:
|
||||
return bool(self._client.machines)
|
||||
except Exception:
|
||||
return False
|
||||
self._client.deleteprefix(self.path(scope=''))
|
||||
except Exception as e:
|
||||
assert False, "exception when cleaning up etcd contents: {0}".format(e)
|
||||
|
||||
|
||||
class KubernetesController(AbstractDcsController):
|
||||
@@ -421,8 +454,9 @@ class KubernetesController(AbstractDcsController):
|
||||
self._label_selector = ','.join('{0}={1}'.format(k, v) for k, v in self._labels.items())
|
||||
os.environ['PATRONI_KUBERNETES_LABELS'] = json.dumps(self._labels)
|
||||
os.environ['PATRONI_KUBERNETES_USE_ENDPOINTS'] = 'true'
|
||||
os.environ['PATRONI_KUBERNETES_BYPASS_API_SERVICE'] = 'true'
|
||||
|
||||
from kubernetes import client as k8s_client, config as k8s_config
|
||||
from patroni.dcs.kubernetes import k8s_client, k8s_config
|
||||
k8s_config.load_kube_config(context='local')
|
||||
self._client = k8s_client
|
||||
self._api = self._client.CoreV1Api()
|
||||
@@ -531,6 +565,48 @@ class ExhibitorController(ZooKeeperController):
|
||||
os.environ.update({'PATRONI_EXHIBITOR_HOSTS': 'localhost', 'PATRONI_EXHIBITOR_PORT': '8181'})
|
||||
|
||||
|
||||
class RaftController(AbstractDcsController):
|
||||
|
||||
CONTROLLER_ADDR = 'localhost:1234'
|
||||
|
||||
def __init__(self, context):
|
||||
super(RaftController, self).__init__(context)
|
||||
os.environ.update(PATRONI_RAFT_PARTNER_ADDRS="'" + self.CONTROLLER_ADDR + "'", RAFT_PORT='1234')
|
||||
self._raft = None
|
||||
|
||||
def _start(self):
|
||||
env = os.environ.copy()
|
||||
del env['PATRONI_RAFT_PARTNER_ADDRS']
|
||||
env['PATRONI_RAFT_SELF_ADDR'] = self.CONTROLLER_ADDR
|
||||
env['PATRONI_RAFT_DATA_DIR'] = self._work_directory
|
||||
return subprocess.Popen([sys.executable, '-m', 'coverage', 'run',
|
||||
'--source=patroni', '-p', 'patroni_raft_controller.py'],
|
||||
stdout=self._log, stderr=subprocess.STDOUT, env=env)
|
||||
|
||||
def query(self, key, scope='batman'):
|
||||
ret = self._raft.get(self.path(key, scope))
|
||||
return ret and ret['value']
|
||||
|
||||
def set(self, key, value):
|
||||
self._raft.set(self.path(key), value)
|
||||
|
||||
def cleanup_service_tree(self):
|
||||
from patroni.dcs.raft import KVStoreTTL
|
||||
from pysyncobj import SyncObjConf
|
||||
|
||||
if self._raft:
|
||||
self._raft.destroy()
|
||||
self._raft._SyncObj__thread.join()
|
||||
self.stop()
|
||||
os.makedirs(self._work_directory)
|
||||
self.start()
|
||||
|
||||
ready_event = threading.Event()
|
||||
conf = SyncObjConf(appendEntriesUseBatch=False, dynamicMembershipChange=True, onReady=ready_event.set)
|
||||
self._raft = KVStoreTTL(None, [self.CONTROLLER_ADDR], conf)
|
||||
ready_event.wait()
|
||||
|
||||
|
||||
class PatroniPoolController(object):
|
||||
|
||||
BACKUP_SCRIPT = [sys.executable, 'features/backup_create.py']
|
||||
|
||||
@@ -8,6 +8,8 @@ Scenario: check API requests on a stand-alone server
|
||||
Then I receive a response code 200
|
||||
And I receive a response state running
|
||||
And I receive a response role master
|
||||
When I issue a GET request to http://127.0.0.1:8008/standby_leader
|
||||
Then I receive a response code 503
|
||||
When I issue a GET request to http://127.0.0.1:8008/health
|
||||
Then I receive a response code 200
|
||||
When I issue a GET request to http://127.0.0.1:8008/replica
|
||||
@@ -59,9 +61,15 @@ Scenario: check the scheduled restart
|
||||
And postgres0 role is the primary after 10 seconds
|
||||
|
||||
Scenario: check API requests for the primary-replica pair in the pause mode
|
||||
Given I run patronictl.py pause batman
|
||||
Given I start postgres1
|
||||
Then replication works from postgres0 to postgres1 after 20 seconds
|
||||
When I run patronictl.py pause batman
|
||||
Then I receive a response returncode 0
|
||||
When I kill postmaster on postgres1
|
||||
And I issue a GET request to http://127.0.0.1:8009/replica
|
||||
Then I receive a response code 503
|
||||
When I run patronictl.py restart batman postgres1 --force
|
||||
Then I receive a response returncode 0
|
||||
When I start postgres1
|
||||
Then replication works from postgres0 to postgres1 after 20 seconds
|
||||
When I issue a GET request to http://127.0.0.1:8009/replica
|
||||
Then I receive a response code 200
|
||||
|
||||
@@ -25,8 +25,9 @@ Feature: standby cluster
|
||||
Then postgres1 is a leader of batman1 after 10 seconds
|
||||
When I add the table foo to postgres0
|
||||
Then table foo is present on postgres1 after 20 seconds
|
||||
And I sleep for 3 seconds
|
||||
When I issue a GET request to http://127.0.0.1:8009/master
|
||||
Then I receive a response code 200
|
||||
Then I receive a response code 503
|
||||
When I issue a GET request to http://127.0.0.1:8009/standby_leader
|
||||
Then I receive a response code 200
|
||||
And I receive a response role standby_leader
|
||||
@@ -40,7 +41,8 @@ Feature: standby cluster
|
||||
And I kill postmaster on postgres1
|
||||
Then postgres2 is replicating from postgres0 after 32 seconds
|
||||
When I issue a GET request to http://127.0.0.1:8010/master
|
||||
Then I receive a response code 200
|
||||
Then I receive a response code 503
|
||||
And I sleep for 3 seconds
|
||||
When I issue a GET request to http://127.0.0.1:8010/standby_leader
|
||||
Then I receive a response code 200
|
||||
And I receive a response role standby_leader
|
||||
|
||||
@@ -21,7 +21,7 @@ def write_label(context, content, name):
|
||||
context.pctl.write_label(name, content)
|
||||
|
||||
|
||||
@step('"{name}" key in DCS has {key:w}={value:w} after {time_limit:d} seconds')
|
||||
@step('"{name}" key in DCS has {key:w}={value} after {time_limit:d} seconds')
|
||||
def check_member(context, name, key, value, time_limit):
|
||||
time_limit *= context.timeout_multiplier
|
||||
max_time = time.time() + int(time_limit)
|
||||
|
||||
@@ -10,7 +10,8 @@ SELECT * FROM pg_catalog.pg_stat_replication
|
||||
WHERE application_name = '{0}'
|
||||
"""
|
||||
|
||||
callback = sys.executable + " features/callback2.py "
|
||||
executable = sys.executable if os.name != 'nt' else sys.executable.replace('\\', '/')
|
||||
callback = executable + " features/callback2.py "
|
||||
|
||||
|
||||
@step('I start {name:w} with callback configured')
|
||||
@@ -18,7 +19,7 @@ def start_patroni_with_callbacks(context, name):
|
||||
return context.pctl.start(name, custom_config={
|
||||
"postgresql": {
|
||||
"callbacks": {
|
||||
"on_role_change": sys.executable + " features/callback.py"
|
||||
"on_role_change": executable + " features/callback.py"
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -31,7 +32,7 @@ def start_patroni(context, name, cluster_name):
|
||||
"postgresql": {
|
||||
"callbacks": {c: callback + name for c in ('on_start', 'on_stop', 'on_restart', 'on_role_change')},
|
||||
"backup_restore": {
|
||||
"command": (sys.executable + " features/backup_restore.py --sourcedir=" +
|
||||
"command": (executable + " features/backup_restore.py --sourcedir=" +
|
||||
os.path.join(context.pctl.patroni_path, 'data', 'basebackup'))}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -33,7 +33,5 @@ postgresql:
|
||||
__EOF__
|
||||
|
||||
unset PATRONI_SUPERUSER_PASSWORD PATRONI_REPLICATION_PASSWORD
|
||||
export KUBERNETES_NAMESPACE=$PATRONI_KUBERNETES_NAMESPACE
|
||||
export POD_NAME=$PATRONI_NAME
|
||||
|
||||
exec /usr/bin/python3 /usr/local/bin/patroni /home/postgres/patroni.yml
|
||||
exec /usr/bin/python3 /usr/local/bin/patroni /home/postgres/patroni.yml
|
||||
|
||||
@@ -118,6 +118,8 @@ objects:
|
||||
fieldRef:
|
||||
apiVersion: v1
|
||||
fieldPath: metadata.namespace
|
||||
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
||||
value: 'true'
|
||||
- name: PATRONI_KUBERNETES_LABELS
|
||||
value: '{application: ${APPLICATION_NAME}, cluster-name: ${PATRONI_CLUSTER_NAME}}'
|
||||
- name: PATRONI_SUPERUSER_USERNAME
|
||||
@@ -152,6 +154,16 @@ objects:
|
||||
image: docker-registry.default.svc:5000/${NAMESPACE}/patroni:latest
|
||||
imagePullPolicy: IfNotPresent
|
||||
name: ${APPLICATION_NAME}
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
scheme: HTTP
|
||||
path: /readiness
|
||||
port: 8008
|
||||
initialDelaySeconds: 3
|
||||
periodSeconds: 10
|
||||
timeoutSeconds: 5
|
||||
successThreshold: 1
|
||||
failureThreshold: 3
|
||||
ports:
|
||||
- containerPort: 8008
|
||||
protocol: TCP
|
||||
@@ -240,6 +252,34 @@ objects:
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: ${SERVICE_ACCOUNT}
|
||||
# Following privileges are only required if deployed not in the "default"
|
||||
# namespace and you want Patroni to bypass kubernetes service
|
||||
# (PATRONI_KUBERNETES_BYPASS_API_SERVICE=true)
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: patroni-k8s-ep-access
|
||||
rules:
|
||||
- apiGroups:
|
||||
- ""
|
||||
resources:
|
||||
- endpoints
|
||||
resourceNames:
|
||||
- kubernetes
|
||||
verbs:
|
||||
- get
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: ${NAMESPACE}-${SERVICE_ACCOUNT}-k8s-ep-access
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: patroni-k8s-ep-access
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: ${SERVICE_ACCOUNT}
|
||||
namespace: ${NAMESPACE}
|
||||
parameters:
|
||||
- description: The name of the application for labelling all artifacts.
|
||||
displayName: Application Name
|
||||
|
||||
@@ -5,11 +5,9 @@ metadata:
|
||||
annotations:
|
||||
description: |-
|
||||
Patroni Postgresql database cluster, with persistent storage.
|
||||
|
||||
WARNING: Any data stored will be lost upon pod destruction. Only use this template for testing.
|
||||
iconClass: icon-postgresql
|
||||
openshift.io/display-name: Patroni Postgresql (Persistent)
|
||||
openshift.io/long-description: This template deploys a a patroni postgresql HA cluster without persistent storage.
|
||||
openshift.io/long-description: This template deploys a a patroni postgresql HA cluster with persistent storage.
|
||||
tags: postgresql
|
||||
objects:
|
||||
- apiVersion: v1
|
||||
@@ -106,6 +104,20 @@ objects:
|
||||
application: ${APPLICATION_NAME}
|
||||
cluster-name: ${PATRONI_CLUSTER_NAME}
|
||||
spec:
|
||||
initContainers:
|
||||
- command:
|
||||
- sh
|
||||
- -c
|
||||
- "mkdir -p /home/postgres/pgdata/pgroot/data && chmod 0700 /home/postgres/pgdata/pgroot/data"
|
||||
image: docker-registry.default.svc:5000/${NAMESPACE}/patroni:latest
|
||||
imagePullPolicy: IfNotPresent
|
||||
name: fix-perms
|
||||
resources: {}
|
||||
terminationMessagePath: /dev/termination-log
|
||||
terminationMessagePolicy: File
|
||||
volumeMounts:
|
||||
- mountPath: /home/postgres/pgdata
|
||||
name: ${APPLICATION_NAME}
|
||||
containers:
|
||||
- env:
|
||||
- name: PATRONI_KUBERNETES_POD_IP
|
||||
@@ -118,6 +130,8 @@ objects:
|
||||
fieldRef:
|
||||
apiVersion: v1
|
||||
fieldPath: metadata.namespace
|
||||
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
||||
value: 'true'
|
||||
- name: PATRONI_KUBERNETES_LABELS
|
||||
value: '{application: ${APPLICATION_NAME}, cluster-name: ${PATRONI_CLUSTER_NAME}}'
|
||||
- name: PATRONI_SUPERUSER_USERNAME
|
||||
@@ -152,6 +166,16 @@ objects:
|
||||
image: docker-registry.default.svc:5000/${NAMESPACE}/patroni:latest
|
||||
imagePullPolicy: IfNotPresent
|
||||
name: ${APPLICATION_NAME}
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
scheme: HTTP
|
||||
path: /readiness
|
||||
port: 8008
|
||||
initialDelaySeconds: 3
|
||||
periodSeconds: 10
|
||||
timeoutSeconds: 5
|
||||
successThreshold: 1
|
||||
failureThreshold: 3
|
||||
ports:
|
||||
- containerPort: 8008
|
||||
protocol: TCP
|
||||
@@ -252,6 +276,34 @@ objects:
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: ${SERVICE_ACCOUNT}
|
||||
# Following privileges are only required if deployed not in the "default"
|
||||
# namespace and you want Patroni to bypass kubernetes service
|
||||
# (PATRONI_KUBERNETES_BYPASS_API_SERVICE=true)
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: patroni-k8s-ep-access
|
||||
rules:
|
||||
- apiGroups:
|
||||
- ""
|
||||
resources:
|
||||
- endpoints
|
||||
resourceNames:
|
||||
- kubernetes
|
||||
verbs:
|
||||
- get
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: ${NAMESPACE}-${SERVICE_ACCOUNT}-k8s-ep-access
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: patroni-k8s-ep-access
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: ${SERVICE_ACCOUNT}
|
||||
namespace: ${NAMESPACE}
|
||||
parameters:
|
||||
- description: The name of the application for labelling all artifacts.
|
||||
displayName: Application Name
|
||||
@@ -300,4 +352,4 @@ parameters:
|
||||
- description: The size of the persistent volume to create.
|
||||
displayName: Persistent Volume Size
|
||||
name: PVC_SIZE
|
||||
value: 5Gi
|
||||
value: 5Gi
|
||||
|
||||
@@ -10,7 +10,7 @@ spec:
|
||||
clusterIP: None
|
||||
|
||||
---
|
||||
apiVersion: apps/v1beta1
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: &cluster_name patronidemo
|
||||
@@ -31,6 +31,16 @@ spec:
|
||||
- name: *cluster_name
|
||||
image: patroni # docker build -t patroni .
|
||||
imagePullPolicy: IfNotPresent
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
scheme: HTTP
|
||||
path: /readiness
|
||||
port: 8008
|
||||
initialDelaySeconds: 3
|
||||
periodSeconds: 10
|
||||
timeoutSeconds: 5
|
||||
successThreshold: 1
|
||||
failureThreshold: 3
|
||||
ports:
|
||||
- containerPort: 8008
|
||||
protocol: TCP
|
||||
@@ -48,6 +58,8 @@ spec:
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: metadata.namespace
|
||||
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
||||
value: 'true'
|
||||
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
|
||||
value: 'true'
|
||||
- name: PATRONI_KUBERNETES_LABELS
|
||||
@@ -123,6 +135,25 @@ spec:
|
||||
- port: 5432
|
||||
targetPort: 5432
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: patronidemo-repl
|
||||
labels:
|
||||
application: patroni
|
||||
cluster-name: &cluster_name patronidemo
|
||||
role: replica
|
||||
spec:
|
||||
type: ClusterIP
|
||||
selector:
|
||||
application: patroni
|
||||
cluster-name: *cluster_name
|
||||
role: replica
|
||||
ports:
|
||||
- port: 5432
|
||||
targetPort: 5432
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
@@ -210,3 +241,37 @@ roleRef:
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: patronidemo
|
||||
|
||||
# Following privileges are only required if deployed not in the "default"
|
||||
# namespace and you want Patroni to bypass kubernetes service
|
||||
# (PATRONI_KUBERNETES_BYPASS_API_SERVICE=true)
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: patroni-k8s-ep-access
|
||||
rules:
|
||||
- apiGroups:
|
||||
- ""
|
||||
resources:
|
||||
- endpoints
|
||||
resourceNames:
|
||||
- kubernetes
|
||||
verbs:
|
||||
- get
|
||||
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: patroni-k8s-ep-access
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: patroni-k8s-ep-access
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: patronidemo
|
||||
# The namespace must be specified explicitly.
|
||||
# If deploying to the different namespace you have to change it.
|
||||
namespace: default
|
||||
|
||||
+26
-80
@@ -4,30 +4,28 @@ import signal
|
||||
import sys
|
||||
import time
|
||||
|
||||
from patroni.version import __version__
|
||||
from .daemon import AbstractPatroniDaemon, abstract_main
|
||||
from .version import __version__
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PATRONI_ENV_PREFIX = 'PATRONI_'
|
||||
KUBERNETES_ENV_PREFIX = 'KUBERNETES_'
|
||||
|
||||
|
||||
class Patroni(object):
|
||||
class Patroni(AbstractPatroniDaemon):
|
||||
|
||||
def __init__(self, conf):
|
||||
def __init__(self, config):
|
||||
from patroni.api import RestApiServer
|
||||
from patroni.dcs import get_dcs
|
||||
from patroni.ha import Ha
|
||||
from patroni.log import PatroniLogger
|
||||
from patroni.postgresql import Postgresql
|
||||
from patroni.request import PatroniRequest
|
||||
from patroni.watchdog import Watchdog
|
||||
|
||||
self.setup_signal_handlers()
|
||||
super(Patroni, self).__init__(config)
|
||||
|
||||
self.version = __version__
|
||||
self.logger = PatroniLogger()
|
||||
self.config = conf
|
||||
self.logger.reload_config(self.config.get('log', {}))
|
||||
self.dcs = get_dcs(self.config)
|
||||
self.watchdog = Watchdog(self.config)
|
||||
self.load_dynamic_configuration()
|
||||
@@ -70,14 +68,14 @@ class Patroni(object):
|
||||
def nosync(self):
|
||||
return bool(self.tags.get('nosync', False))
|
||||
|
||||
def reload_config(self, sighup=False):
|
||||
def reload_config(self, sighup=False, local=False):
|
||||
try:
|
||||
self.tags = self.get_tags()
|
||||
self.logger.reload_config(self.config.get('log', {}))
|
||||
self.watchdog.reload_config(self.config)
|
||||
if sighup:
|
||||
super(Patroni, self).reload_config(sighup, local)
|
||||
if local:
|
||||
self.tags = self.get_tags()
|
||||
self.request.reload_config(self.config)
|
||||
self.api.reload_config(self.config['restapi'])
|
||||
self.api.reload_config(self.config['restapi'])
|
||||
self.watchdog.reload_config(self.config)
|
||||
self.postgresql.reload_config(self.config['postgresql'], sighup)
|
||||
self.dcs.reload_config(self.config)
|
||||
except Exception:
|
||||
@@ -87,15 +85,6 @@ class Patroni(object):
|
||||
def replicatefrom(self):
|
||||
return self.tags.get('replicatefrom')
|
||||
|
||||
def sighup_handler(self, *args):
|
||||
self._received_sighup = True
|
||||
|
||||
def sigterm_handler(self, *args):
|
||||
with self._sigterm_lock:
|
||||
if not self._received_sigterm:
|
||||
self._received_sigterm = True
|
||||
sys.exit()
|
||||
|
||||
@property
|
||||
def noloadbalance(self):
|
||||
return bool(self.tags.get('noloadbalance', False))
|
||||
@@ -113,48 +102,24 @@ class Patroni(object):
|
||||
elif self.ha.watch(nap_time):
|
||||
self.next_run = time.time()
|
||||
|
||||
@property
|
||||
def received_sigterm(self):
|
||||
with self._sigterm_lock:
|
||||
return self._received_sigterm
|
||||
|
||||
def run(self):
|
||||
self.api.start()
|
||||
self.logger.start()
|
||||
self.next_run = time.time()
|
||||
super(Patroni, self).run()
|
||||
|
||||
while not self.received_sigterm:
|
||||
if self._received_sighup:
|
||||
self._received_sighup = False
|
||||
if self.config.reload_local_configuration():
|
||||
self.reload_config(True)
|
||||
else:
|
||||
self.postgresql.config.reload_config(self.config['postgresql'], True)
|
||||
def _run_cycle(self):
|
||||
logger.info(self.ha.run_cycle())
|
||||
|
||||
logger.info(self.ha.run_cycle())
|
||||
if self.dcs.cluster and self.dcs.cluster.config and self.dcs.cluster.config.data \
|
||||
and self.config.set_dynamic_configuration(self.dcs.cluster.config):
|
||||
self.reload_config()
|
||||
|
||||
if self.dcs.cluster and self.dcs.cluster.config and self.dcs.cluster.config.data \
|
||||
and self.config.set_dynamic_configuration(self.dcs.cluster.config):
|
||||
self.reload_config()
|
||||
if self.postgresql.role != 'uninitialized':
|
||||
self.config.save_cache()
|
||||
|
||||
if self.postgresql.role != 'uninitialized':
|
||||
self.config.save_cache()
|
||||
self.schedule_next_run()
|
||||
|
||||
self.schedule_next_run()
|
||||
|
||||
def setup_signal_handlers(self):
|
||||
from threading import Lock
|
||||
|
||||
self._received_sighup = False
|
||||
self._sigterm_lock = Lock()
|
||||
self._received_sigterm = False
|
||||
if os.name != 'nt':
|
||||
signal.signal(signal.SIGHUP, self.sighup_handler)
|
||||
signal.signal(signal.SIGTERM, self.sigterm_handler)
|
||||
|
||||
def shutdown(self):
|
||||
with self._sigterm_lock:
|
||||
self._received_sigterm = True
|
||||
def _shutdown(self):
|
||||
try:
|
||||
self.api.shutdown()
|
||||
except Exception:
|
||||
@@ -163,33 +128,14 @@ class Patroni(object):
|
||||
self.ha.shutdown()
|
||||
except Exception:
|
||||
logger.exception('Exception during Ha.shutdown')
|
||||
self.logger.shutdown()
|
||||
|
||||
|
||||
def patroni_main():
|
||||
import argparse
|
||||
from patroni.config import Config, ConfigParseError
|
||||
from multiprocessing import freeze_support
|
||||
from patroni.validator import schema
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--version', action='version', version='%(prog)s {0}'.format(__version__))
|
||||
parser.add_argument('configfile', nargs='?', default='',
|
||||
help='Patroni may also read the configuration from the {0} environment variable'
|
||||
.format(Config.PATRONI_CONFIG_VARIABLE))
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
conf = Config(args.configfile)
|
||||
except ConfigParseError as e:
|
||||
if e.value:
|
||||
print(e.value)
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
patroni = Patroni(conf)
|
||||
try:
|
||||
patroni.run()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
patroni.shutdown()
|
||||
freeze_support()
|
||||
abstract_main(Patroni, schema)
|
||||
|
||||
|
||||
def fatal(string, *args):
|
||||
|
||||
+121
-53
@@ -9,20 +9,28 @@ import datetime
|
||||
import os
|
||||
import six
|
||||
import socket
|
||||
import sys
|
||||
|
||||
from patroni.exceptions import PostgresConnectionException, PostgresException
|
||||
from patroni.postgresql.misc import postgres_version_to_int
|
||||
from patroni.utils import deep_compare, parse_bool, patch_config, Retry, \
|
||||
RetryFailedError, parse_int, split_host_port, tzutc, uri, cluster_as_json
|
||||
from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||
from six.moves.socketserver import ThreadingMixIn
|
||||
from six.moves.urllib_parse import urlparse, parse_qs
|
||||
from threading import Thread
|
||||
|
||||
from .exceptions import PostgresConnectionException, PostgresException
|
||||
from .postgresql.misc import postgres_version_to_int
|
||||
from .utils import deep_compare, enable_keepalive, parse_bool, patch_config, Retry, \
|
||||
RetryFailedError, parse_int, split_host_port, tzutc, uri, cluster_as_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RestApiHandler(BaseHTTPRequestHandler):
|
||||
|
||||
def _write_status_code_only(self, status_code):
|
||||
message = self.responses[status_code][0]
|
||||
self.wfile.write('{0} {1} {2}\r\n\r\n'.format(self.protocol_version, status_code, message).encode('utf-8'))
|
||||
self.log_request(status_code)
|
||||
|
||||
def _write_response(self, status_code, body, content_type='text/html', headers=None):
|
||||
self.send_response(status_code)
|
||||
headers = headers or {}
|
||||
@@ -30,6 +38,8 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
headers['Content-Type'] = content_type
|
||||
for name, value in headers.items():
|
||||
self.send_header(name, value)
|
||||
for name, value in self.server.http_extra_headers.items():
|
||||
self.send_header(name, value)
|
||||
self.end_headers()
|
||||
self.wfile.write(body.encode('utf-8'))
|
||||
|
||||
@@ -80,61 +90,82 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self, write_status_code_only=False):
|
||||
"""Default method for processing all GET requests which can not be routed to other methods"""
|
||||
|
||||
time_start = time.time()
|
||||
request_type = 'OPTIONS' if write_status_code_only else 'GET'
|
||||
|
||||
path = '/master' if self.path == '/' else self.path
|
||||
response = self.get_postgresql_status()
|
||||
|
||||
patroni = self.server.patroni
|
||||
cluster = patroni.dcs.cluster
|
||||
|
||||
if not cluster and patroni.ha.is_paused():
|
||||
primary_status_code = 200 if response['role'] == 'master' else 503
|
||||
else:
|
||||
primary_status_code = 200 if patroni.ha.is_leader() else 503
|
||||
leader_optime = cluster and cluster.last_leader_operation or 0
|
||||
replayed_location = response.get('xlog', {}).get('replayed_location', 0)
|
||||
max_replica_lag = parse_int(self.path_query.get('lag', [sys.maxsize])[0], 'B')
|
||||
if max_replica_lag is None:
|
||||
max_replica_lag = sys.maxsize
|
||||
is_lagging = leader_optime and leader_optime > replayed_location + max_replica_lag
|
||||
|
||||
replica_status_code = 200 if not patroni.noloadbalance and \
|
||||
replica_status_code = 200 if not patroni.noloadbalance and not is_lagging and \
|
||||
response.get('role') == 'replica' and response.get('state') == 'running' else 503
|
||||
|
||||
if not cluster and patroni.ha.is_paused():
|
||||
primary_status_code = 200 if response.get('role') == 'master' else 503
|
||||
standby_leader_status_code = 200 if response.get('role') == 'standby_leader' else 503
|
||||
elif patroni.ha.is_leader():
|
||||
if patroni.ha.is_standby_cluster():
|
||||
primary_status_code = replica_status_code = 503
|
||||
standby_leader_status_code = 200 if response.get('role') in ('replica', 'standby_leader') else 503
|
||||
else:
|
||||
primary_status_code = 200
|
||||
standby_leader_status_code = 503
|
||||
else:
|
||||
primary_status_code = standby_leader_status_code = 503
|
||||
|
||||
status_code = 503
|
||||
|
||||
if patroni.ha.is_standby_cluster() and ('standby_leader' in path or 'standby-leader' in path):
|
||||
status_code = 200 if patroni.ha.is_leader() else 503
|
||||
if 'standby_leader' in path or 'standby-leader' in path:
|
||||
status_code = standby_leader_status_code
|
||||
elif 'master' in path or 'leader' in path or 'primary' in path or 'read-write' in path:
|
||||
status_code = primary_status_code
|
||||
elif 'replica' in path:
|
||||
status_code = replica_status_code
|
||||
elif 'read-only' in path:
|
||||
status_code = 200 if primary_status_code == 200 else replica_status_code
|
||||
status_code = 200 if 200 in (primary_status_code, standby_leader_status_code) else replica_status_code
|
||||
elif 'health' in path:
|
||||
status_code = 200 if response.get('state') == 'running' else 503
|
||||
elif cluster: # dcs is available
|
||||
is_synchronous = cluster.is_synchronous_mode() and cluster.sync \
|
||||
and cluster.sync.sync_standby == patroni.postgresql.name
|
||||
and patroni.postgresql.name in cluster.sync.members
|
||||
if path in ('/sync', '/synchronous') and is_synchronous:
|
||||
status_code = replica_status_code
|
||||
elif path in ('/async', '/asynchronous') and not is_synchronous:
|
||||
status_code = replica_status_code
|
||||
|
||||
if write_status_code_only: # when haproxy sends OPTIONS request it reads only status code and nothing more
|
||||
message = self.responses[status_code][0]
|
||||
self.wfile.write('{0} {1} {2}\r\n'.format(self.protocol_version, status_code, message).encode('utf-8'))
|
||||
self._write_status_code_only(status_code)
|
||||
else:
|
||||
self._write_status_response(status_code, response)
|
||||
|
||||
time_end = time.time()
|
||||
self.log_message('%s %s %s latency: %s ms', request_type, path,
|
||||
status_code, (time_end - time_start) * 1000)
|
||||
|
||||
def do_OPTIONS(self):
|
||||
self.do_GET(write_status_code_only=True)
|
||||
|
||||
def do_GET_liveness(self):
|
||||
self._write_status_code_only(200)
|
||||
|
||||
def do_GET_readiness(self):
|
||||
patroni = self.server.patroni
|
||||
if patroni.ha.is_leader():
|
||||
status_code = 200
|
||||
elif patroni.postgresql.state == 'running':
|
||||
status_code = 200 if patroni.dcs.cluster else 503
|
||||
else:
|
||||
status_code = 503
|
||||
self._write_status_code_only(status_code)
|
||||
|
||||
def do_GET_patroni(self):
|
||||
response = self.get_postgresql_status(True)
|
||||
self._write_status_response(200, response)
|
||||
|
||||
def do_GET_cluster(self):
|
||||
cluster = self.server.patroni.dcs.cluster or self.server.patroni.dcs.get_cluster()
|
||||
cluster = self.server.patroni.dcs.get_cluster(True)
|
||||
self._write_json_response(200, cluster_as_json(cluster))
|
||||
|
||||
def do_GET_history(self):
|
||||
@@ -167,6 +198,8 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
request = self._read_json_content()
|
||||
if request:
|
||||
cluster = self.server.patroni.dcs.get_cluster()
|
||||
if not (cluster.config and cluster.config.modify_index):
|
||||
return self.send_error(503)
|
||||
data = cluster.config.data.copy()
|
||||
if patch_config(data, request):
|
||||
value = json.dumps(data, separators=(',', ':'))
|
||||
@@ -283,6 +316,20 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
code = 404
|
||||
self._write_response(code, data)
|
||||
|
||||
@check_auth
|
||||
def do_DELETE_switchover(self):
|
||||
failover = self.server.patroni.dcs.get_cluster().failover
|
||||
if failover and failover.scheduled_at:
|
||||
if not self.server.patroni.dcs.manual_failover('', '', index=failover.index):
|
||||
return self.send_error(409)
|
||||
else:
|
||||
data = "scheduled switchover deleted"
|
||||
code = 200
|
||||
else:
|
||||
data = "no switchover is scheduled"
|
||||
code = 404
|
||||
self._write_response(code, data)
|
||||
|
||||
@check_auth
|
||||
def do_POST_reinitialize(self):
|
||||
request = self._read_json_content(body_is_optional=True)
|
||||
@@ -322,13 +369,13 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
if leader and (not cluster.leader or cluster.leader.name != leader):
|
||||
return 'leader name does not match'
|
||||
if candidate:
|
||||
if action == 'switchover' and cluster.is_synchronous_mode() and cluster.sync.sync_standby != candidate:
|
||||
if action == 'switchover' and cluster.is_synchronous_mode() and candidate not in cluster.sync.members:
|
||||
return 'candidate name does not match with sync_standby'
|
||||
members = [m for m in cluster.members if m.name == candidate]
|
||||
if not members:
|
||||
return 'candidate does not exists'
|
||||
elif cluster.is_synchronous_mode():
|
||||
members = [m for m in cluster.members if m.name == cluster.sync.sync_standby]
|
||||
members = [m for m in cluster.members if m.name in cluster.sync.members]
|
||||
if not members:
|
||||
return action + ' is not possible: can not find sync_standby'
|
||||
else:
|
||||
@@ -405,6 +452,9 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
|
||||
ret = BaseHTTPRequestHandler.parse_request(self)
|
||||
if ret:
|
||||
urlpath = urlparse(self.path)
|
||||
self.path = urlpath.path
|
||||
self.path_query = parse_qs(urlpath.query) or {}
|
||||
mname = self.path.lstrip('/').split('/')[0]
|
||||
mname = self.command + ('_' + mname if mname else '')
|
||||
if hasattr(self, 'do_' + mname):
|
||||
@@ -418,67 +468,63 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
||||
return retry(self.server.query, sql, *params)
|
||||
|
||||
def get_postgresql_status(self, retry=False):
|
||||
postgresql = self.server.patroni.postgresql
|
||||
try:
|
||||
cluster = self.server.patroni.dcs.cluster
|
||||
|
||||
if self.server.patroni.postgresql.state not in ('running', 'restarting', 'starting'):
|
||||
if postgresql.state not in ('running', 'restarting', 'starting'):
|
||||
raise RetryFailedError('')
|
||||
stmt = ("SELECT pg_catalog.to_char(pg_catalog.pg_postmaster_start_time(), 'YYYY-MM-DD HH24:MI:SS.MS TZ'),"
|
||||
" CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0"
|
||||
" ELSE ('x' || pg_catalog.substr(pg_catalog.pg_{0}file_name("
|
||||
"pg_catalog.pg_current_{0}_{1}()), 1, 8))::bit(32)::int END,"
|
||||
" CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0"
|
||||
" ELSE pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_current_{0}_{1}(), '0/0')::bigint END,"
|
||||
" pg_catalog.pg_{0}_{1}_diff(COALESCE(pg_catalog.pg_last_{0}_receive_{1}(),"
|
||||
" pg_catalog.pg_last_{0}_replay_{1}()), '0/0')::bigint,"
|
||||
" pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_last_{0}_replay_{1}(), '0/0')::bigint,"
|
||||
stmt = ("SELECT " + postgresql.POSTMASTER_START_TIME + ", " + postgresql.TL_LSN + ","
|
||||
" pg_catalog.to_char(pg_catalog.pg_last_xact_replay_timestamp(), 'YYYY-MM-DD HH24:MI:SS.MS TZ'),"
|
||||
" pg_catalog.pg_is_in_recovery() AND pg_catalog.pg_is_{0}_replay_paused(), "
|
||||
" pg_catalog.array_to_json(pg_catalog.array_agg(pg_catalog.row_to_json(ri))) "
|
||||
"FROM (SELECT (SELECT rolname FROM pg_authid WHERE oid = usesysid) AS usename,"
|
||||
" application_name, client_addr, w.state, sync_state, sync_priority"
|
||||
" FROM pg_catalog.pg_stat_get_wal_senders() w, pg_catalog.pg_stat_get_activity(pid)) AS ri")
|
||||
|
||||
row = self.query(stmt.format(self.server.patroni.postgresql.wal_name,
|
||||
self.server.patroni.postgresql.lsn_name), retry=retry)[0]
|
||||
row = self.query(stmt.format(postgresql.wal_name, postgresql.lsn_name), retry=retry)[0]
|
||||
|
||||
result = {
|
||||
'state': self.server.patroni.postgresql.state,
|
||||
'state': postgresql.state,
|
||||
'postmaster_start_time': row[0],
|
||||
'role': 'replica' if row[1] == 0 else 'master',
|
||||
'server_version': self.server.patroni.postgresql.server_version,
|
||||
'server_version': postgresql.server_version,
|
||||
'cluster_unlocked': bool(not cluster or cluster.is_unlocked()),
|
||||
'xlog': ({
|
||||
'received_location': row[3],
|
||||
'replayed_location': row[4],
|
||||
'replayed_timestamp': row[5],
|
||||
'paused': row[6]} if row[1] == 0 else {
|
||||
'received_location': row[4] or row[3],
|
||||
'replayed_location': row[3],
|
||||
'replayed_timestamp': row[6],
|
||||
'paused': row[5]} if row[1] == 0 else {
|
||||
'location': row[2]
|
||||
})
|
||||
}
|
||||
|
||||
if result['role'] == 'replica' and self.server.patroni.ha.is_standby_cluster():
|
||||
result['role'] = self.server.patroni.postgresql.role
|
||||
result['role'] = postgresql.role
|
||||
|
||||
if row[1] > 0:
|
||||
result['timeline'] = row[1]
|
||||
else:
|
||||
leader_timeline = None if not cluster or cluster.is_unlocked() else cluster.leader.timeline
|
||||
result['timeline'] = self.server.patroni.postgresql.replica_cached_timeline(leader_timeline)
|
||||
result['timeline'] = postgresql.replica_cached_timeline(leader_timeline)
|
||||
|
||||
if row[7]:
|
||||
result['replication'] = row[7]
|
||||
|
||||
return result
|
||||
except (psycopg2.Error, RetryFailedError, PostgresConnectionException):
|
||||
state = self.server.patroni.postgresql.state
|
||||
state = postgresql.state
|
||||
if state == 'running':
|
||||
logger.exception('get_postgresql_status')
|
||||
state = 'unknown'
|
||||
return {'state': state, 'role': self.server.patroni.postgresql.role}
|
||||
return {'state': state, 'role': postgresql.role}
|
||||
|
||||
def handle_one_request(self):
|
||||
self.__start_time = time.time()
|
||||
BaseHTTPRequestHandler.handle_one_request(self)
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
logger.debug("API thread: %s - - [%s] %s", self.client_address[0], self.log_date_time_string(), fmt % args)
|
||||
latency = 1000.0 * (time.time() - self.__start_time)
|
||||
logger.debug("API thread: %s - - %s latency: %0.3f ms", self.client_address[0], fmt % args, latency)
|
||||
|
||||
|
||||
class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
||||
@@ -489,6 +535,7 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
||||
self.patroni = patroni
|
||||
self.__listen = None
|
||||
self.__ssl_options = None
|
||||
self.http_extra_headers = {}
|
||||
self.reload_config(config)
|
||||
self.daemon = True
|
||||
|
||||
@@ -525,10 +572,10 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
||||
if self.__protocol == 'https' and self.__ssl_options.get('verify_client') in ('required', 'optional'):
|
||||
return rh._write_response(403, 'client certificate required')
|
||||
|
||||
reason = self.check_auth_header(rh.headers.get('Authorization'))
|
||||
if reason:
|
||||
headers = {'WWW-Authenticate': 'Basic realm="' + self.patroni.__class__.__name__ + '"'}
|
||||
return rh._write_response(401, reason, headers=headers)
|
||||
reason = self.check_auth_header(rh.headers.get('Authorization'))
|
||||
if reason:
|
||||
headers = {'WWW-Authenticate': 'Basic realm="' + self.patroni.__class__.__name__ + '"'}
|
||||
return rh._write_response(401, reason, headers=headers)
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
@@ -599,12 +646,33 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
||||
if reloading_config:
|
||||
self.start()
|
||||
|
||||
def process_request_thread(self, request, client_address):
|
||||
if isinstance(request, tuple):
|
||||
sock, newsock = request
|
||||
try:
|
||||
request = sock.context.wrap_socket(newsock, do_handshake_on_connect=sock.do_handshake_on_connect,
|
||||
suppress_ragged_eofs=sock.suppress_ragged_eofs, server_side=True)
|
||||
except socket.error:
|
||||
return
|
||||
super(RestApiServer, self).process_request_thread(request, client_address)
|
||||
|
||||
def get_request(self):
|
||||
sock = self.socket
|
||||
newsock, addr = socket.socket.accept(sock)
|
||||
enable_keepalive(newsock, 10, 3)
|
||||
if hasattr(sock, 'context'): # SSLSocket, we want to do the deferred handshake from a thread
|
||||
newsock = (sock, newsock)
|
||||
return newsock, addr
|
||||
|
||||
def reload_config(self, config):
|
||||
if 'listen' not in config: # changing config in runtime
|
||||
raise ValueError('Can not find "restapi.listen" config')
|
||||
|
||||
ssl_options = {n: config[n] for n in ('certfile', 'keyfile', 'cafile') if n in config}
|
||||
|
||||
self.http_extra_headers = config.get('http_extra_headers') or {}
|
||||
self.http_extra_headers.update((config.get('https_extra_headers') or {}) if ssl_options.get('certfile') else {})
|
||||
|
||||
if isinstance(config.get('verify_client'), six.string_types):
|
||||
ssl_options['verify_client'] = config['verify_client'].lower()
|
||||
|
||||
|
||||
+46
-12
@@ -21,8 +21,11 @@ _AUTH_ALLOWED_PARAMETERS = (
|
||||
'sslmode',
|
||||
'sslcert',
|
||||
'sslkey',
|
||||
'sslpassword',
|
||||
'sslrootcert',
|
||||
'sslcrl'
|
||||
'sslcrl',
|
||||
'gssencmode',
|
||||
'channel_binding'
|
||||
)
|
||||
|
||||
|
||||
@@ -59,8 +62,10 @@ class Config(object):
|
||||
'maximum_lag_on_failover': 1048576,
|
||||
'check_timeline': False,
|
||||
'master_start_timeout': 300,
|
||||
'master_stop_timeout': 0,
|
||||
'synchronous_mode': False,
|
||||
'synchronous_mode_strict': False,
|
||||
'synchronous_node_count': 1,
|
||||
'standby_cluster': {
|
||||
'create_replica_methods': '',
|
||||
'host': '',
|
||||
@@ -73,7 +78,8 @@ class Config(object):
|
||||
'postgresql': {
|
||||
'bin_dir': '',
|
||||
'use_slots': True,
|
||||
'parameters': CaseInsensitiveDict({p: v[0] for p, v in ConfigHandler.CMDLINE_OPTIONS.items()})
|
||||
'parameters': CaseInsensitiveDict({p: v[0] for p, v in ConfigHandler.CMDLINE_OPTIONS.items()
|
||||
if p not in ('wal_keep_segments', 'wal_keep_size')})
|
||||
},
|
||||
'watchdog': {
|
||||
'mode': 'automatic',
|
||||
@@ -87,7 +93,7 @@ class Config(object):
|
||||
self.__environment_configuration = self._build_environment_configuration()
|
||||
|
||||
# Patroni reads the configuration from the command-line argument if it exists, otherwise from the environment
|
||||
self._config_file = configfile and os.path.isfile(configfile) and configfile
|
||||
self._config_file = configfile and os.path.exists(configfile) and configfile
|
||||
if self._config_file:
|
||||
self._local_configuration = self._load_config_file()
|
||||
else:
|
||||
@@ -115,12 +121,32 @@ class Config(object):
|
||||
def check_mode(self, mode):
|
||||
return bool(parse_bool(self._dynamic_configuration.get(mode)))
|
||||
|
||||
def _load_config_path(self, path):
|
||||
"""
|
||||
If path is a file, loads the yml file pointed to by path.
|
||||
If path is a directory, loads all yml files in that directory in alphabetical order
|
||||
"""
|
||||
if os.path.isfile(path):
|
||||
files = [path]
|
||||
elif os.path.isdir(path):
|
||||
files = [os.path.join(path, f) for f in sorted(os.listdir(path))
|
||||
if (f.endswith('.yml') or f.endswith('.yaml')) and os.path.isfile(os.path.join(path, f))]
|
||||
else:
|
||||
logger.error('config path %s is neither directory nor file', path)
|
||||
raise ConfigParseError('invalid config path')
|
||||
|
||||
overall_config = {}
|
||||
for fname in files:
|
||||
with open(fname) as f:
|
||||
config = yaml.safe_load(f)
|
||||
patch_config(overall_config, config)
|
||||
return overall_config
|
||||
|
||||
def _load_config_file(self):
|
||||
"""Loads config.yaml from filesystem and applies some values which were set via ENV"""
|
||||
with open(self._config_file) as f:
|
||||
config = yaml.safe_load(f)
|
||||
patch_config(config, self.__environment_configuration)
|
||||
return config
|
||||
config = self._load_config_path(self._config_file)
|
||||
patch_config(config, self.__environment_configuration)
|
||||
return config
|
||||
|
||||
def _load_cache(self):
|
||||
if os.path.isfile(self._cache_file):
|
||||
@@ -239,7 +265,8 @@ class Config(object):
|
||||
if value:
|
||||
ret[section][param] = value
|
||||
|
||||
_set_section_values('restapi', ['listen', 'connect_address', 'certfile', 'keyfile', 'cafile', 'verify_client'])
|
||||
_set_section_values('restapi', ['listen', 'connect_address', 'certfile', 'keyfile', 'cafile', 'verify_client',
|
||||
'http_extra_headers', 'https_extra_headers'])
|
||||
_set_section_values('ctl', ['insecure', 'cacert', 'certfile', 'keyfile'])
|
||||
_set_section_values('postgresql', ['listen', 'connect_address', 'config_dir', 'data_dir', 'pgpass', 'bin_dir'])
|
||||
_set_section_values('log', ['level', 'traceback_level', 'format', 'dateformat', 'max_queue_size',
|
||||
@@ -290,6 +317,10 @@ class Config(object):
|
||||
logger.exception('Exception when parsing list %s', value)
|
||||
return None
|
||||
|
||||
_set_section_values('raft', ['data_dir', 'self_addr', 'partner_addrs', 'password', 'bind_addr'])
|
||||
if 'raft' in ret and 'partner_addrs' in ret['raft']:
|
||||
ret['raft']['partner_addrs'] = _parse_list(ret['raft']['partner_addrs'])
|
||||
|
||||
for param in list(os.environ.keys()):
|
||||
if param.startswith(PATRONI_ENV_PREFIX):
|
||||
# PATRONI_(ETCD|CONSUL|ZOOKEEPER|EXHIBITOR|...)_(HOSTS?|PORT|..)
|
||||
@@ -297,7 +328,8 @@ class Config(object):
|
||||
if suffix in ('HOST', 'HOSTS', 'PORT', 'USE_PROXIES', 'PROTOCOL', 'SRV', 'URL', 'PROXY',
|
||||
'CACERT', 'CERT', 'KEY', 'VERIFY', 'TOKEN', 'CHECKS', 'DC', 'CONSISTENCY',
|
||||
'REGISTER_SERVICE', 'SERVICE_CHECK_INTERVAL', 'NAMESPACE', 'CONTEXT',
|
||||
'USE_ENDPOINTS', 'SCOPE_LABEL', 'ROLE_LABEL', 'POD_IP', 'PORTS', 'LABELS') and name:
|
||||
'USE_ENDPOINTS', 'SCOPE_LABEL', 'ROLE_LABEL', 'POD_IP', 'PORTS', 'LABELS',
|
||||
'BYPASS_API_SERVICE', 'KEY_PASSWORD', 'USE_SSL') and name:
|
||||
value = os.environ.pop(param)
|
||||
if suffix == 'PORT':
|
||||
value = value and parse_int(value)
|
||||
@@ -305,12 +337,13 @@ class Config(object):
|
||||
value = value and _parse_list(value)
|
||||
elif suffix == 'LABELS':
|
||||
value = _parse_dict(value)
|
||||
elif suffix in ('USE_PROXIES', 'REGISTER_SERVICE'):
|
||||
elif suffix in ('USE_PROXIES', 'REGISTER_SERVICE', 'BYPASS_API_SERVICE'):
|
||||
value = parse_bool(value)
|
||||
if value:
|
||||
ret[name.lower()][suffix.lower()] = value
|
||||
if 'etcd' in ret:
|
||||
ret['etcd'].update(_get_auth('etcd'))
|
||||
for dcs in ('etcd', 'etcd3'):
|
||||
if dcs in ret:
|
||||
ret[dcs].update(_get_auth(dcs))
|
||||
|
||||
users = {}
|
||||
for param in list(os.environ.keys()):
|
||||
@@ -378,6 +411,7 @@ class Config(object):
|
||||
'retry_timeout',
|
||||
'synchronous_mode',
|
||||
'synchronous_mode_strict',
|
||||
'synchronous_node_count',
|
||||
)
|
||||
|
||||
pg_config.update({p: config[p] for p in updated_fields if p in config})
|
||||
|
||||
+171
-69
@@ -6,7 +6,7 @@ import click
|
||||
import codecs
|
||||
import datetime
|
||||
import dateutil.parser
|
||||
import cdiff
|
||||
import dateutil.tz
|
||||
import copy
|
||||
import difflib
|
||||
import io
|
||||
@@ -19,10 +19,10 @@ import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import tzlocal
|
||||
import yaml
|
||||
|
||||
from click import ClickException
|
||||
from collections import defaultdict
|
||||
from contextlib import contextmanager
|
||||
from patroni.dcs import get_dcs as _get_dcs
|
||||
from patroni.exceptions import PatroniException
|
||||
@@ -31,8 +31,12 @@ from patroni.postgresql.misc import postgres_version_to_int
|
||||
from patroni.utils import cluster_as_json, patch_config, polling_loop
|
||||
from patroni.request import PatroniRequest
|
||||
from patroni.version import __version__
|
||||
from prettytable import PrettyTable
|
||||
from prettytable import ALL, FRAME, PrettyTable
|
||||
from six.moves.urllib_parse import urlparse
|
||||
try:
|
||||
from ydiff import markup_to_pager, PatchStream
|
||||
except ImportError: # pragma: no cover
|
||||
from cdiff import markup_to_pager, PatchStream
|
||||
|
||||
CONFIG_DIR_PATH = click.get_app_dir('patroni')
|
||||
CONFIG_FILE_PATH = os.path.join(CONFIG_DIR_PATH, 'patronictl.yaml')
|
||||
@@ -46,6 +50,34 @@ class PatroniCtlException(ClickException):
|
||||
pass
|
||||
|
||||
|
||||
class PatronictlPrettyTable(PrettyTable):
|
||||
|
||||
def __init__(self, header, *args, **kwargs):
|
||||
PrettyTable.__init__(self, *args, **kwargs)
|
||||
self.__table_header = header
|
||||
self.__hline_num = 0
|
||||
self.__hline = None
|
||||
|
||||
def _is_first_hline(self):
|
||||
return self.__hline_num == 0
|
||||
|
||||
def _set_hline(self, value):
|
||||
self.__hline = value
|
||||
|
||||
def _get_hline(self):
|
||||
ret = self.__hline
|
||||
|
||||
# Inject nice table header
|
||||
if self._is_first_hline() and self.__table_header:
|
||||
header = self.__table_header[:len(ret) - 2]
|
||||
ret = "".join([ret[0], header, ret[1 + len(header):]])
|
||||
|
||||
self.__hline_num += 1
|
||||
return ret
|
||||
|
||||
_hrule = property(_get_hline, _set_hline)
|
||||
|
||||
|
||||
def parse_dcs(dcs):
|
||||
if dcs is None:
|
||||
return None
|
||||
@@ -69,7 +101,11 @@ def load_config(path, dcs):
|
||||
from patroni.config import Config
|
||||
|
||||
if not (os.path.exists(path) and os.access(path, os.R_OK)):
|
||||
logging.debug('Ignoring configuration file "%s". It does not exists or is not readable.', path)
|
||||
if path != CONFIG_FILE_PATH: # bail if non-default config location specified but file not found / readable
|
||||
raise PatroniCtlException('Provided config file {0} not existing or no read rights.'
|
||||
' Check the -c/--config-file parameter'.format(path))
|
||||
else:
|
||||
logging.debug('Ignoring configuration file "%s". It does not exists or is not readable.', path)
|
||||
else:
|
||||
logging.debug('Loading configuration from file %s', path)
|
||||
config = Config(path, validator=None).copy()
|
||||
@@ -90,7 +126,7 @@ def store_config(config, path):
|
||||
yaml.dump(config, fd)
|
||||
|
||||
|
||||
option_format = click.option('--format', '-f', 'fmt', help='Output format (pretty, json, yaml)', default='pretty')
|
||||
option_format = click.option('--format', '-f', 'fmt', help='Output format (pretty, tsv, json, yaml)', default='pretty')
|
||||
option_watchrefresh = click.option('-w', '--watch', type=float, help='Auto update the screen every X seconds')
|
||||
option_watch = click.option('-W', is_flag=True, help='Auto update the screen every 2 seconds')
|
||||
option_force = click.option('--force', is_flag=True, help='Do not ask for confirmation at any point')
|
||||
@@ -133,31 +169,34 @@ def request_patroni(member, method='GET', endpoint=None, data=None):
|
||||
return request_executor(member, method, endpoint, data)
|
||||
|
||||
|
||||
def print_output(columns, rows=None, alignment=None, fmt='pretty', header=True, delimiter='\t'):
|
||||
rows = rows or []
|
||||
if fmt == 'pretty':
|
||||
t = PrettyTable(columns)
|
||||
for k, v in (alignment or {}).items():
|
||||
t.align[k] = v
|
||||
for r in rows:
|
||||
t.add_row(r)
|
||||
click.echo(t)
|
||||
return
|
||||
def print_output(columns, rows, alignment=None, fmt='pretty', header=None, delimiter='\t'):
|
||||
if fmt in {'json', 'yaml', 'yml'}:
|
||||
elements = [{k: v for k, v in zip(columns, r) if not header or str(v)} for r in rows]
|
||||
func = json.dumps if fmt == 'json' else format_config_for_editing
|
||||
click.echo(func(elements))
|
||||
elif fmt in {'pretty', 'tsv', 'topology'}:
|
||||
list_cluster = bool(header and columns and columns[0] == 'Cluster')
|
||||
if list_cluster and 'Tags' in columns: # we want to format member tags as YAML
|
||||
i = columns.index('Tags')
|
||||
for row in rows:
|
||||
if row[i]:
|
||||
row[i] = format_config_for_editing(row[i], fmt != 'pretty').strip()
|
||||
if list_cluster and fmt != 'tsv': # skip cluster name if pretty-printing
|
||||
columns = columns[1:] if columns else []
|
||||
rows = [row[1:] for row in rows]
|
||||
|
||||
if fmt in ['json', 'yaml', 'yml']:
|
||||
elements = [dict(zip(columns, r)) for r in rows]
|
||||
if fmt == 'json':
|
||||
click.echo(json.dumps(elements))
|
||||
elif fmt in ('yaml', 'yml'):
|
||||
click.echo(yaml.safe_dump(elements, encoding=None, default_flow_style=False, allow_unicode=True, width=200))
|
||||
|
||||
if fmt == 'tsv':
|
||||
if columns is not None and header:
|
||||
click.echo(delimiter.join(columns))
|
||||
|
||||
for r in rows:
|
||||
c = [str(c) for c in r]
|
||||
click.echo(delimiter.join(c))
|
||||
if fmt == 'tsv':
|
||||
for r in ([columns] if columns else []) + rows:
|
||||
click.echo(delimiter.join(map(str, r)))
|
||||
else:
|
||||
hrules = ALL if any(any(isinstance(c, six.string_types) and '\n' in c for c in r) for r in rows) else FRAME
|
||||
table = PatronictlPrettyTable(header, columns, hrules=hrules)
|
||||
table.align = 'l'
|
||||
for k, v in (alignment or {}).items():
|
||||
table.align[k] = v
|
||||
for r in rows:
|
||||
table.add_row(r)
|
||||
click.echo(table)
|
||||
|
||||
|
||||
def watching(w, watch, max_count=None, clear=True):
|
||||
@@ -190,7 +229,7 @@ def watching(w, watch, max_count=None, clear=True):
|
||||
|
||||
def get_all_members(cluster, role='master'):
|
||||
if role == 'master':
|
||||
if cluster.leader is not None:
|
||||
if cluster.leader is not None and cluster.leader.name:
|
||||
yield cluster.leader
|
||||
return
|
||||
|
||||
@@ -207,6 +246,15 @@ def get_any_member(cluster, role='master', member=None):
|
||||
return m
|
||||
|
||||
|
||||
def get_all_members_leader_first(cluster):
|
||||
leader_name = cluster.leader.member.name if cluster.leader and cluster.leader.member.api_url else None
|
||||
if leader_name:
|
||||
yield cluster.leader.member
|
||||
for member in cluster.members:
|
||||
if member.api_url and member.name != leader_name:
|
||||
yield member
|
||||
|
||||
|
||||
def get_cursor(cluster, connect_parameters, role='master', member=None):
|
||||
member = get_any_member(cluster, role=role, member=member)
|
||||
if member is None:
|
||||
@@ -306,8 +354,7 @@ def dsn(obj, cluster_name, role, member):
|
||||
|
||||
@ctl.command('query', help='Query a Patroni PostgreSQL member')
|
||||
@arg_cluster_name
|
||||
@option_format
|
||||
@click.option('--format', 'fmt', help='Output format (pretty, json)', default='tsv')
|
||||
@click.option('--format', 'fmt', help='Output format (pretty, tsv, json, yaml)', default='tsv')
|
||||
@click.option('--file', '-f', 'p_file', help='Execute the SQL commands from this file', type=click.File('rb'))
|
||||
@click.option('--password', help='force password prompt', is_flag=True)
|
||||
@click.option('-U', '--username', help='database user name', type=str)
|
||||
@@ -364,8 +411,8 @@ def query(
|
||||
if cursor is None:
|
||||
cluster = dcs.get_cluster()
|
||||
|
||||
output, cursor = query_member(cluster, cursor, member, role, command, connect_parameters)
|
||||
print_output(None, output, fmt=fmt, delimiter=delimiter)
|
||||
output, header = query_member(cluster, cursor, member, role, command, connect_parameters)
|
||||
print_output(header, output, fmt=fmt, delimiter=delimiter)
|
||||
|
||||
|
||||
def query_member(cluster, cursor, member, role, command, connect_parameters):
|
||||
@@ -382,15 +429,8 @@ def query_member(cluster, cursor, member, role, command, connect_parameters):
|
||||
logging.debug(message)
|
||||
return [[timestamp(0), message]], None
|
||||
|
||||
cursor.execute('SELECT pg_catalog.pg_is_in_recovery()')
|
||||
in_recovery = cursor.fetchone()[0]
|
||||
|
||||
if in_recovery and role == 'master' or not in_recovery and role == 'replica':
|
||||
cursor.connection.close()
|
||||
return None, None
|
||||
|
||||
cursor.execute(command)
|
||||
return cursor.fetchall(), cursor
|
||||
return cursor.fetchall(), [d.name for d in cursor.description]
|
||||
except (psycopg2.OperationalError, psycopg2.DatabaseError) as oe:
|
||||
logging.debug(oe)
|
||||
if cursor is not None and not cursor.connection.closed:
|
||||
@@ -445,7 +485,7 @@ def parse_scheduled(scheduled):
|
||||
try:
|
||||
scheduled_at = dateutil.parser.parse(scheduled)
|
||||
if scheduled_at.tzinfo is None:
|
||||
scheduled_at = tzlocal.get_localzone().localize(scheduled_at)
|
||||
scheduled_at = scheduled_at.replace(tzinfo=dateutil.tz.tzlocal())
|
||||
except (ValueError, TypeError):
|
||||
message = 'Unable to parse scheduled timestamp ({0}). It should be in an unambiguous format (e.g. ISO 8601)'
|
||||
raise PatroniCtlException(message.format(scheduled))
|
||||
@@ -604,7 +644,7 @@ def _do_failover_or_switchover(obj, action, cluster_name, master, candidate, for
|
||||
dcs = get_dcs(obj, cluster_name)
|
||||
cluster = dcs.get_cluster()
|
||||
|
||||
if action == 'switchover' and cluster.leader is None:
|
||||
if action == 'switchover' and (cluster.leader is None or not cluster.leader.name):
|
||||
raise PatroniCtlException('This cluster has no master')
|
||||
|
||||
if master is None:
|
||||
@@ -720,30 +760,60 @@ def switchover(obj, cluster_name, master, candidate, force, scheduled):
|
||||
_do_failover_or_switchover(obj, 'switchover', cluster_name, master, candidate, force, scheduled)
|
||||
|
||||
|
||||
def generate_topology(level, member, topology):
|
||||
members = topology.get(member['name'], [])
|
||||
|
||||
if level > 0:
|
||||
member['name'] = '{0}+ {1}'.format((' ' * (level - 1) * 2), member['name'])
|
||||
|
||||
if member['name']:
|
||||
yield member
|
||||
|
||||
for member in members:
|
||||
for member in generate_topology(level + 1, member, topology):
|
||||
yield member
|
||||
|
||||
|
||||
def topology_sort(members):
|
||||
topology = defaultdict(list)
|
||||
leader = next((m for m in members if m['role'].endswith('leader')), {'name': None})
|
||||
replicas = set(member['name'] for member in members if not member['role'].endswith('leader'))
|
||||
for member in members:
|
||||
if not member['role'].endswith('leader'):
|
||||
parent = member.get('tags', {}).get('replicatefrom')
|
||||
parent = parent if parent and parent != member['name'] and parent in replicas else leader['name']
|
||||
topology[parent].append(member)
|
||||
for member in generate_topology(0, leader, topology):
|
||||
yield member
|
||||
|
||||
|
||||
def output_members(cluster, name, extended=False, fmt='pretty'):
|
||||
rows = []
|
||||
logging.debug(cluster)
|
||||
initialize = {None: 'uninitialized', '': 'initializing'}.get(cluster.initialize, cluster.initialize)
|
||||
cluster = cluster_as_json(cluster)
|
||||
|
||||
columns = ['Cluster', 'Member', 'Host', 'Role', 'State', 'TL', 'Lag in MB']
|
||||
for c in ('Pending restart', 'Scheduled restart'):
|
||||
for c in ('Pending restart', 'Scheduled restart', 'Tags'):
|
||||
if extended or any(m.get(c.lower().replace(' ', '_')) for m in cluster['members']):
|
||||
columns.append(c)
|
||||
|
||||
# Show Host as 'host:port' if somebody is running on non-standard port or two nodes are running on the same host
|
||||
append_port = any(m['port'] != 5432 for m in cluster['members']) or\
|
||||
len(set(m['host'] for m in cluster['members'])) < len(cluster['members'])
|
||||
members = [m for m in cluster['members'] if 'host' in m]
|
||||
append_port = any('port' in m and m['port'] != 5432 for m in members) or\
|
||||
len(set(m['host'] for m in members)) < len(members)
|
||||
|
||||
for m in cluster['members']:
|
||||
sort = topology_sort if fmt == 'topology' else iter
|
||||
for m in sort(cluster['members']):
|
||||
logging.debug(m)
|
||||
|
||||
lag = m.get('lag', '')
|
||||
m.update(cluster=name, member=m['name'], tl=m.get('timeline', ''),
|
||||
role='' if m['role'] == 'replica' else m['role'].replace('_', ' ').title(),
|
||||
m.update(cluster=name, member=m['name'], host=m.get('host', ''), tl=m.get('timeline', ''),
|
||||
role=m['role'].replace('_', ' ').title(),
|
||||
lag_in_mb=round(lag/1024/1024) if isinstance(lag, six.integer_types) else lag,
|
||||
pending_restart='*' if m.get('pending_restart') else '')
|
||||
|
||||
if append_port:
|
||||
if append_port and m['host'] and m.get('port'):
|
||||
m['host'] = ':'.join([m['host'], str(m['port'])])
|
||||
|
||||
if 'scheduled_restart' in m:
|
||||
@@ -754,9 +824,9 @@ def output_members(cluster, name, extended=False, fmt='pretty'):
|
||||
|
||||
rows.append([m.get(n.lower().replace(' ', '_'), '') for n in columns])
|
||||
|
||||
print_output(columns, rows, {'Lag in MB': 'r', 'TL': 'r'}, fmt)
|
||||
print_output(columns, rows, {'Lag in MB': 'r', 'TL': 'r'}, fmt, ' Cluster: {0} ({1}) '.format(name, initialize))
|
||||
|
||||
if fmt != 'pretty': # Omit service info when using machine-readable formats
|
||||
if fmt not in ('pretty', 'topology'): # Omit service info when using machine-readable formats
|
||||
return
|
||||
|
||||
service_info = []
|
||||
@@ -800,6 +870,16 @@ def members(obj, cluster_names, fmt, watch, w, extended, ts):
|
||||
output_members(cluster, cluster_name, extended, fmt)
|
||||
|
||||
|
||||
@ctl.command('topology', help='Prints ASCII topology for given cluster')
|
||||
@click.argument('cluster_names', nargs=-1)
|
||||
@option_watch
|
||||
@option_watchrefresh
|
||||
@click.pass_obj
|
||||
@click.pass_context
|
||||
def topology(ctx, obj, cluster_names, watch, w):
|
||||
ctx.forward(members, fmt='topology')
|
||||
|
||||
|
||||
def timestamp(precision=6):
|
||||
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:precision - 7]
|
||||
|
||||
@@ -867,25 +947,45 @@ def scaffold(obj, cluster_name, sysid):
|
||||
click.echo("Cluster {0} has been created successfully".format(cluster_name))
|
||||
|
||||
|
||||
@ctl.command('flush', help='Flush scheduled events')
|
||||
@ctl.command('flush', help='Discard scheduled events')
|
||||
@click.argument('cluster_name')
|
||||
@click.argument('member_names', nargs=-1)
|
||||
@click.argument('target', type=click.Choice(['restart']))
|
||||
@click.argument('target', type=click.Choice(['restart', 'switchover']))
|
||||
@click.option('--role', '-r', help='Flush only members with this role', default='any',
|
||||
type=click.Choice(['master', 'replica', 'any']))
|
||||
@option_force
|
||||
@click.pass_obj
|
||||
def flush(obj, cluster_name, member_names, force, role, target):
|
||||
cluster = get_dcs(obj, cluster_name).get_cluster()
|
||||
dcs = get_dcs(obj, cluster_name)
|
||||
cluster = dcs.get_cluster()
|
||||
|
||||
members = get_members(cluster, cluster_name, member_names, role, force, 'flush')
|
||||
for member in members:
|
||||
if target == 'restart':
|
||||
if target == 'restart':
|
||||
for member in get_members(cluster, cluster_name, member_names, role, force, 'flush'):
|
||||
if member.data.get('scheduled_restart'):
|
||||
r = request_patroni(member, 'delete', 'restart')
|
||||
check_response(r, member.name, 'flush scheduled restart')
|
||||
else:
|
||||
click.echo('No scheduled restart for member {0}'.format(member.name))
|
||||
elif target == 'switchover':
|
||||
failover = cluster.failover
|
||||
if not failover or not failover.scheduled_at:
|
||||
return click.echo('No pending scheduled switchover')
|
||||
for member in get_all_members_leader_first(cluster):
|
||||
try:
|
||||
r = request_patroni(member, 'delete', 'switchover')
|
||||
if r.status in (200, 404):
|
||||
prefix = 'Success' if r.status == 200 else 'Failed'
|
||||
return click.echo('{0}: {1}'.format(prefix, r.data.decode('utf-8')))
|
||||
except Exception as err:
|
||||
logging.warning(str(err))
|
||||
logging.warning('Member %s is not accessible', member.name)
|
||||
|
||||
click.echo('Failed: member={0}, status_code={1}, ({2})'.format(
|
||||
member.name, r.status, r.data.decode('utf-8')))
|
||||
|
||||
logging.warning('Failing over to DCS')
|
||||
click.echo('{0} Could not find any accessible member of cluster {1}'.format(timestamp(), cluster_name))
|
||||
dcs.manual_failover('', '', index=failover.index)
|
||||
|
||||
|
||||
def wait_until_pause_is_applied(dcs, paused, old_cluster):
|
||||
@@ -912,12 +1012,7 @@ def toggle_pause(config, cluster_name, paused, wait):
|
||||
if cluster.is_paused() == paused:
|
||||
raise PatroniCtlException('Cluster is {0} paused'.format(paused and 'already' or 'not'))
|
||||
|
||||
members = []
|
||||
if cluster.leader:
|
||||
members.append(cluster.leader.member)
|
||||
members.extend([m for m in cluster.members if m.api_url and (not members or members[0].name != m.name)])
|
||||
|
||||
for member in members:
|
||||
for member in get_all_members_leader_first(cluster):
|
||||
try:
|
||||
r = request_patroni(member, 'patch', 'config', {'pause': paused or None})
|
||||
except Exception as err:
|
||||
@@ -979,7 +1074,7 @@ def show_diff(before_editing, after_editing):
|
||||
If the output is to a tty the diff will be colored. Inputs are expected to be unicode strings.
|
||||
"""
|
||||
def listify(string):
|
||||
return [l+'\n' for l in string.rstrip('\n').split('\n')]
|
||||
return [line + '\n' for line in string.rstrip('\n').split('\n')]
|
||||
|
||||
unified_diff = difflib.unified_diff(listify(before_editing), listify(after_editing))
|
||||
|
||||
@@ -994,18 +1089,25 @@ def show_diff(before_editing, after_editing):
|
||||
side_by_side = False
|
||||
width = 80
|
||||
tab_width = 8
|
||||
cdiff.markup_to_pager(cdiff.PatchStream(buf), opts)
|
||||
wrap = True
|
||||
if find_executable('less'):
|
||||
pager = None
|
||||
else:
|
||||
pager = 'more.com' if sys.platform == 'win32' else 'more'
|
||||
pager_options = None
|
||||
|
||||
markup_to_pager(PatchStream(buf), opts)
|
||||
else:
|
||||
for line in unified_diff:
|
||||
click.echo(line.rstrip('\n'))
|
||||
|
||||
|
||||
def format_config_for_editing(data):
|
||||
def format_config_for_editing(data, default_flow_style=False):
|
||||
"""Formats configuration as YAML for human consumption.
|
||||
|
||||
:param data: configuration as nested dictionaries
|
||||
:returns unicode YAML of the configuration"""
|
||||
return yaml.safe_dump(data, default_flow_style=False, encoding=None, allow_unicode=True)
|
||||
return yaml.safe_dump(data, default_flow_style=default_flow_style, encoding=None, allow_unicode=True, width=200)
|
||||
|
||||
|
||||
def apply_config_changes(before_editing, data, kvpairs):
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import abc
|
||||
import os
|
||||
import signal
|
||||
import six
|
||||
import sys
|
||||
|
||||
from threading import Lock
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
class AbstractPatroniDaemon(object):
|
||||
|
||||
def __init__(self, config):
|
||||
from patroni.log import PatroniLogger
|
||||
|
||||
self.setup_signal_handlers()
|
||||
|
||||
self.logger = PatroniLogger()
|
||||
self.config = config
|
||||
AbstractPatroniDaemon.reload_config(self, local=True)
|
||||
|
||||
def sighup_handler(self, *args):
|
||||
self._received_sighup = True
|
||||
|
||||
def sigterm_handler(self, *args):
|
||||
with self._sigterm_lock:
|
||||
if not self._received_sigterm:
|
||||
self._received_sigterm = True
|
||||
sys.exit()
|
||||
|
||||
def setup_signal_handlers(self):
|
||||
self._received_sighup = False
|
||||
self._sigterm_lock = Lock()
|
||||
self._received_sigterm = False
|
||||
if os.name != 'nt':
|
||||
signal.signal(signal.SIGHUP, self.sighup_handler)
|
||||
signal.signal(signal.SIGTERM, self.sigterm_handler)
|
||||
|
||||
@property
|
||||
def received_sigterm(self):
|
||||
with self._sigterm_lock:
|
||||
return self._received_sigterm
|
||||
|
||||
def reload_config(self, sighup=False, local=False):
|
||||
if local:
|
||||
self.logger.reload_config(self.config.get('log', {}))
|
||||
|
||||
@abc.abstractmethod
|
||||
def _run_cycle(self):
|
||||
"""_run_cycle"""
|
||||
|
||||
def run(self):
|
||||
self.logger.start()
|
||||
while not self.received_sigterm:
|
||||
if self._received_sighup:
|
||||
self._received_sighup = False
|
||||
self.reload_config(True, self.config.reload_local_configuration())
|
||||
|
||||
self._run_cycle()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _shutdown(self):
|
||||
"""_shutdown"""
|
||||
|
||||
def shutdown(self):
|
||||
with self._sigterm_lock:
|
||||
self._received_sigterm = True
|
||||
self._shutdown()
|
||||
self.logger.shutdown()
|
||||
|
||||
|
||||
def abstract_main(cls, validator=None):
|
||||
import argparse
|
||||
|
||||
from .config import Config, ConfigParseError
|
||||
from .version import __version__
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--version', action='version', version='%(prog)s {0}'.format(__version__))
|
||||
if validator:
|
||||
parser.add_argument('--validate-config', action='store_true', help='Run config validator and exit')
|
||||
parser.add_argument('configfile', nargs='?', default='',
|
||||
help='Patroni may also read the configuration from the {0} environment variable'
|
||||
.format(Config.PATRONI_CONFIG_VARIABLE))
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
if validator and args.validate_config:
|
||||
Config(args.configfile, validator=validator)
|
||||
sys.exit()
|
||||
|
||||
config = Config(args.configfile)
|
||||
except ConfigParseError as e:
|
||||
if e.value:
|
||||
print(e.value)
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
controller = cls(config)
|
||||
try:
|
||||
controller.run()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
controller.shutdown()
|
||||
+66
-33
@@ -13,7 +13,7 @@ import time
|
||||
|
||||
from collections import defaultdict, namedtuple
|
||||
from copy import deepcopy
|
||||
from patroni.exceptions import PatroniException
|
||||
from patroni.exceptions import PatroniFatalException
|
||||
from patroni.utils import parse_bool, uri
|
||||
from random import randint
|
||||
from six.moves.urllib_parse import urlparse, urlunparse, parse_qsl
|
||||
@@ -66,8 +66,11 @@ def dcs_modules():
|
||||
module_prefix = __package__ + '.'
|
||||
|
||||
if getattr(sys, 'frozen', False):
|
||||
importer = pkgutil.get_importer(dcs_dirname)
|
||||
return [module for module in list(importer.toc) if module.startswith(module_prefix) and module.count('.') == 2]
|
||||
toc = set()
|
||||
for importer in pkgutil.iter_importers(dcs_dirname):
|
||||
if hasattr(importer, 'toc'):
|
||||
toc |= importer.toc
|
||||
return [module for module in toc if module.startswith(module_prefix) and module.count('.') == 2]
|
||||
else:
|
||||
return [module_prefix + name for _, name, is_pkg in pkgutil.iter_modules([dcs_dirname]) if not is_pkg]
|
||||
|
||||
@@ -99,7 +102,7 @@ def get_dcs(config):
|
||||
and inspect.isclass(item) and issubclass(item, AbstractDCS))
|
||||
except ImportError:
|
||||
logger.info('Failed to import %s', module_name)
|
||||
raise PatroniException("""Can not find suitable configuration of distributed configuration store
|
||||
raise PatroniFatalException("""Can not find suitable configuration of distributed configuration store
|
||||
Available implementations: """ + ', '.join(sorted(set(available_implementations))))
|
||||
|
||||
|
||||
@@ -137,10 +140,10 @@ class Member(namedtuple('Member', 'index,name,session,data')):
|
||||
@property
|
||||
def conn_url(self):
|
||||
conn_url = self.data.get('conn_url')
|
||||
conn_kwargs = self.data.get('conn_kwargs')
|
||||
if conn_url:
|
||||
return conn_url
|
||||
|
||||
conn_kwargs = self.data.get('conn_kwargs')
|
||||
if conn_kwargs:
|
||||
conn_url = uri('postgresql', (conn_kwargs.get('host'), conn_kwargs.get('port', 5432)))
|
||||
self.data['conn_url'] = conn_url
|
||||
@@ -148,16 +151,19 @@ class Member(namedtuple('Member', 'index,name,session,data')):
|
||||
|
||||
def conn_kwargs(self, auth=None):
|
||||
defaults = {
|
||||
"host": "",
|
||||
"port": "",
|
||||
"database": ""
|
||||
"host": None,
|
||||
"port": None,
|
||||
"database": None
|
||||
}
|
||||
ret = self.data.get('conn_kwargs')
|
||||
if ret:
|
||||
defaults.update(ret)
|
||||
ret = defaults
|
||||
else:
|
||||
r = urlparse(self.conn_url)
|
||||
conn_url = self.conn_url
|
||||
if not conn_url:
|
||||
return {} # due to the invalid conn_url we don't care about authentication parameters
|
||||
r = urlparse(conn_url)
|
||||
ret = {
|
||||
'host': r.hostname,
|
||||
'port': r.port or 5432,
|
||||
@@ -335,13 +341,17 @@ class ClusterConfig(namedtuple('ClusterConfig', 'index,data,modify_index')):
|
||||
self.data.get('permanent_slots') or self.data.get('slots')
|
||||
) or {}
|
||||
|
||||
@property
|
||||
def max_timelines_history(self):
|
||||
return self.data.get('max_timelines_history', 0)
|
||||
|
||||
|
||||
class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
||||
"""Immutable object (namedtuple) which represents last observed synhcronous replication state
|
||||
|
||||
:param index: modification index of a synchronization key in a Configuration Store
|
||||
:param leader: reference to member that was leader
|
||||
:param sync_standby: standby that was last synchronized to leader
|
||||
:param sync_standby: synchronous standby list (comma delimited) which are last synchronized to leader
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
@@ -373,15 +383,22 @@ class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
||||
data = {}
|
||||
return SyncState(index, data.get('leader'), data.get('sync_standby'))
|
||||
|
||||
@property
|
||||
def members(self):
|
||||
""" Returns sync_standby in list """
|
||||
return self.sync_standby and self.sync_standby.split(',') or []
|
||||
|
||||
def matches(self, name):
|
||||
"""
|
||||
Returns if a node name matches one of the nodes in the sync state
|
||||
|
||||
>>> s = SyncState(1, 'foo', 'bar')
|
||||
>>> s = SyncState(1, 'foo', 'bar,zoo')
|
||||
>>> s.matches('foo')
|
||||
True
|
||||
>>> s.matches('bar')
|
||||
True
|
||||
>>> s.matches('zoo')
|
||||
True
|
||||
>>> s.matches('baz')
|
||||
False
|
||||
>>> s.matches(None)
|
||||
@@ -389,7 +406,7 @@ class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
||||
>>> SyncState(1, None, None).matches('foo')
|
||||
False
|
||||
"""
|
||||
return name is not None and name in (self.leader, self.sync_standby)
|
||||
return name is not None and name in [self.leader] + self.members
|
||||
|
||||
|
||||
class TimelineHistory(namedtuple('TimelineHistory', 'index,value,lines')):
|
||||
@@ -449,21 +466,21 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_leader_operat
|
||||
def is_synchronous_mode(self):
|
||||
return self.check_mode('synchronous_mode')
|
||||
|
||||
def get_replication_slots(self, name, role):
|
||||
def get_replication_slots(self, my_name, role):
|
||||
# if the replicatefrom tag is set on the member - we should not create the replication slot for it on
|
||||
# the current master, because that member would replicate from elsewhere. We still create the slot if
|
||||
# the replicatefrom destination member is currently not a member of the cluster (fallback to the
|
||||
# master), or if replicatefrom destination member happens to be the current master
|
||||
use_slots = self.config and self.config.data.get('postgresql', {}).get('use_slots', True)
|
||||
if role in ('master', 'standby_leader'):
|
||||
slot_members = [m.name for m in self.members if use_slots and m.name != name and
|
||||
(m.replicatefrom is None or m.replicatefrom == name or
|
||||
slot_members = [m.name for m in self.members if use_slots and m.name != my_name and
|
||||
(m.replicatefrom is None or m.replicatefrom == my_name or
|
||||
not self.has_member(m.replicatefrom))]
|
||||
permanent_slots = (self.config and self.config.permanent_slots or {}).copy()
|
||||
else:
|
||||
# only manage slots for replicas that replicate from this one, except for the leader among them
|
||||
slot_members = [m.name for m in self.members if use_slots and
|
||||
m.replicatefrom == name and m.name != self.leader.name]
|
||||
m.replicatefrom == my_name and m.name != self.leader.name]
|
||||
permanent_slots = {}
|
||||
|
||||
slots = {slot_name_from_member_name(name): {'type': 'physical'} for name in slot_members}
|
||||
@@ -484,22 +501,22 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_leader_operat
|
||||
logger.error("Slot name may only contain lower case letters, numbers, and the underscore chars")
|
||||
continue
|
||||
|
||||
if name in slots:
|
||||
logger.error("Permanent replication slot {'%s': %s} is conflicting with" +
|
||||
" physical replication slot for cluster member", name, value)
|
||||
continue
|
||||
|
||||
value = deepcopy(value)
|
||||
if not value:
|
||||
value = {'type': 'physical'}
|
||||
|
||||
value = deepcopy(value) if value else {'type': 'physical'}
|
||||
if isinstance(value, dict):
|
||||
if 'type' not in value:
|
||||
value['type'] = 'logical' if value.get('database') and value.get('plugin') else 'physical'
|
||||
|
||||
if value['type'] == 'physical' or value['type'] == 'logical' \
|
||||
and value.get('database') and value.get('plugin'):
|
||||
slots[name] = value
|
||||
if value['type'] == 'physical':
|
||||
# Don't try to create permanent physical replication slot for yourself
|
||||
if name != slot_name_from_member_name(my_name):
|
||||
slots[name] = value
|
||||
continue
|
||||
elif value['type'] == 'logical' and value.get('database') and value.get('plugin'):
|
||||
if name in slots:
|
||||
logger.error("Permanent logical replication slot {'%s': %s} is conflicting with" +
|
||||
" physical replication slot for cluster member", name, value)
|
||||
else:
|
||||
slots[name] = value
|
||||
continue
|
||||
|
||||
logger.error("Bad value for slot '%s' in permanent_slots: %s", name, permanent_slots[name])
|
||||
@@ -633,7 +650,12 @@ class AbstractDCS(object):
|
||||
If the current node was running as a master and exception raised,
|
||||
instance would be demoted."""
|
||||
|
||||
def get_cluster(self):
|
||||
def _bypass_caches(self):
|
||||
"""Used only in zookeeper"""
|
||||
|
||||
def get_cluster(self, force=False):
|
||||
if force:
|
||||
self._bypass_caches()
|
||||
try:
|
||||
cluster = self._load_cluster()
|
||||
except Exception:
|
||||
@@ -749,10 +771,19 @@ class AbstractDCS(object):
|
||||
otherwise it should return `!False`"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def delete_leader(self):
|
||||
"""Voluntarily remove leader key from DCS
|
||||
def _delete_leader(self):
|
||||
"""Remove leader key from DCS.
|
||||
This method should remove leader key if current instance is the leader"""
|
||||
|
||||
def delete_leader(self, last_operation=None):
|
||||
"""Update optime/leader and voluntarily remove leader key from DCS.
|
||||
This method should remove leader key if current instance is the leader.
|
||||
:param last_operation: latest checkpoint location in bytes"""
|
||||
|
||||
if last_operation:
|
||||
self.write_leader_optime(last_operation)
|
||||
return self._delete_leader()
|
||||
|
||||
@abc.abstractmethod
|
||||
def cancel_initialization(self):
|
||||
""" Removes the initialize key for a cluster """
|
||||
@@ -763,8 +794,10 @@ class AbstractDCS(object):
|
||||
|
||||
@staticmethod
|
||||
def sync_state(leader, sync_standby):
|
||||
"""Build sync_state dict"""
|
||||
return {'leader': leader, 'sync_standby': sync_standby}
|
||||
"""Build sync_state dict
|
||||
sync_standby dictionary key being kept for backward compatibility
|
||||
"""
|
||||
return {'leader': leader, 'sync_standby': sync_standby and ','.join(sorted(sync_standby)) or None}
|
||||
|
||||
def write_sync_state(self, leader, sync_standby, index=None):
|
||||
sync_value = self.sync_state(leader, sync_standby)
|
||||
|
||||
+12
-6
@@ -53,9 +53,8 @@ class HTTPClient(object):
|
||||
kwargs['cert_file'] = cert
|
||||
if ca_cert:
|
||||
kwargs['ca_certs'] = ca_cert
|
||||
if verify or ca_cert:
|
||||
kwargs['cert_reqs'] = ssl.CERT_REQUIRED
|
||||
self.http = urllib3.PoolManager(num_pools=10, **kwargs)
|
||||
kwargs['cert_reqs'] = ssl.CERT_REQUIRED if verify or ca_cert else ssl.CERT_NONE
|
||||
self.http = urllib3.PoolManager(num_pools=10, maxsize=10, **kwargs)
|
||||
self._ttl = None
|
||||
|
||||
def set_read_timeout(self, timeout):
|
||||
@@ -220,6 +219,7 @@ class Consul(AbstractDCS):
|
||||
self.__session_checks = config.get('checks', [])
|
||||
self._register_service = config.get('register_service', False)
|
||||
if self._register_service:
|
||||
self._service_tags = config.get('service_tags', [])
|
||||
self._service_name = service_name_from_scope_name(self._scope)
|
||||
if self._scope != self._service_name:
|
||||
logger.warning('Using %s as consul service name instead of scope name %s', self._service_name,
|
||||
@@ -368,7 +368,11 @@ class Consul(AbstractDCS):
|
||||
def touch_member(self, data, permanent=False):
|
||||
cluster = self.cluster
|
||||
member = cluster and cluster.get_member(self._name, fallback_to_leader=False)
|
||||
create_member = not permanent and self.refresh_session()
|
||||
|
||||
try:
|
||||
create_member = not permanent and self.refresh_session()
|
||||
except DCSError:
|
||||
return False
|
||||
|
||||
if member and (create_member or member.session != self._session):
|
||||
self._client.kv.delete(self.member_path)
|
||||
@@ -411,12 +415,14 @@ class Consul(AbstractDCS):
|
||||
conn_parts = urlparse(data['conn_url'])
|
||||
check = base.Check.http(api_parts.geturl(), self._service_check_interval,
|
||||
deregister='{0}s'.format(self._client.http.ttl * 10))
|
||||
tags = self._service_tags[:]
|
||||
tags.append(role)
|
||||
params = {
|
||||
'service_id': '{0}/{1}'.format(self._scope, self._name),
|
||||
'address': conn_parts.hostname,
|
||||
'port': conn_parts.port,
|
||||
'check': check,
|
||||
'tags': [role]
|
||||
'tags': tags
|
||||
}
|
||||
|
||||
if state == 'stopped':
|
||||
@@ -504,7 +510,7 @@ class Consul(AbstractDCS):
|
||||
return self._client.kv.put(self.history_path, value)
|
||||
|
||||
@catch_consul_errors
|
||||
def delete_leader(self):
|
||||
def _delete_leader(self):
|
||||
cluster = self.cluster
|
||||
if cluster and isinstance(cluster.leader, Leader) and cluster.leader.name == self._name:
|
||||
return self._client.kv.delete(self.leader_path, cas=cluster.leader.index)
|
||||
|
||||
+216
-132
@@ -1,4 +1,5 @@
|
||||
from __future__ import absolute_import
|
||||
import abc
|
||||
import etcd
|
||||
import json
|
||||
import logging
|
||||
@@ -11,6 +12,7 @@ import time
|
||||
|
||||
from dns.exception import DNSException
|
||||
from dns import resolver
|
||||
from urllib3 import Timeout
|
||||
from urllib3.exceptions import HTTPError, ReadTimeoutError, ProtocolError
|
||||
from six.moves.queue import Queue
|
||||
from six.moves.http_client import HTTPException
|
||||
@@ -69,6 +71,9 @@ class DnsCachingResolver(Thread):
|
||||
def resolve_async(self, host, port, attempt=0):
|
||||
self._resolve_queue.put(((host, port), attempt))
|
||||
|
||||
def remove(self, host, port):
|
||||
self._cache.pop((host, port), None)
|
||||
|
||||
@staticmethod
|
||||
def _do_resolve(host, port):
|
||||
try:
|
||||
@@ -78,7 +83,8 @@ class DnsCachingResolver(Thread):
|
||||
return []
|
||||
|
||||
|
||||
class Client(etcd.Client):
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
class AbstractEtcdClientWithFailover(etcd.Client):
|
||||
|
||||
def __init__(self, config, dns_resolver, cache_ttl=300):
|
||||
self._dns_resolver = dns_resolver
|
||||
@@ -86,11 +92,12 @@ class Client(etcd.Client):
|
||||
self._machines_cache_updated = 0
|
||||
args = {p: config.get(p) for p in ('host', 'port', 'protocol', 'use_proxies', 'username', 'password',
|
||||
'cert', 'ca_cert') if config.get(p)}
|
||||
super(Client, self).__init__(read_timeout=config['retry_timeout'], **args)
|
||||
super(AbstractEtcdClientWithFailover, self).__init__(read_timeout=config['retry_timeout'], **args)
|
||||
# For some reason python3-etcd on debian and ubuntu are not based on the latest version
|
||||
# Workaround for the case when https://github.com/jplana/python-etcd/pull/196 is not applied
|
||||
self.http.connection_pool_kw.pop('ssl_version', None)
|
||||
self._config = config
|
||||
self._initial_machines_cache = []
|
||||
self._load_machines_cache()
|
||||
self._allow_reconnect = True
|
||||
# allow passing retry argument to api_execute in params
|
||||
@@ -98,14 +105,13 @@ class Client(etcd.Client):
|
||||
self._read_options.add('retry')
|
||||
self._del_conditions.add('retry')
|
||||
|
||||
def _calculate_timeouts(self, etcd_nodes=None, timeout=None):
|
||||
def _calculate_timeouts(self, etcd_nodes, timeout=None):
|
||||
"""Calculate a request timeout and number of retries per single etcd node.
|
||||
In case if the timeout per node is too small (less than one second) we will reduce the number of nodes.
|
||||
For the cluster with only one node we will try to do 2 retries.
|
||||
For clusters with 2 nodes we will try to do 1 retry for every node.
|
||||
No retries for clusters with 3 or more nodes. We better rely on switching to a different node."""
|
||||
|
||||
etcd_nodes = etcd_nodes or len(self._machines_cache) + 1
|
||||
per_node_timeout = timeout = float(timeout or self.read_timeout)
|
||||
|
||||
max_retries = 4 - min(etcd_nodes, 3)
|
||||
@@ -126,23 +132,41 @@ class Client(etcd.Client):
|
||||
|
||||
return etcd_nodes, per_node_timeout, per_node_retries - 1
|
||||
|
||||
def reload_config(self, config):
|
||||
self.username = config.get('username')
|
||||
self.password = config.get('password')
|
||||
|
||||
def _get_headers(self):
|
||||
basic_auth = ':'.join((self.username, self.password)) if self.username and self.password else None
|
||||
return urllib3.make_headers(basic_auth=basic_auth, user_agent=USER_AGENT)
|
||||
|
||||
def _build_request_parameters(self, timeout=None):
|
||||
kwargs = {'headers': self._get_headers(), 'redirect': self.allow_redirect}
|
||||
def _prepare_common_parameters(self, etcd_nodes, timeout=None):
|
||||
kwargs = {'headers': self._get_headers(), 'redirect': self.allow_redirect, 'preload_content': False}
|
||||
|
||||
if timeout is not None:
|
||||
kwargs.update(retries=0, timeout=timeout)
|
||||
else:
|
||||
_, per_node_timeout, per_node_retries = self._calculate_timeouts()
|
||||
kwargs.update(timeout=per_node_timeout, retries=per_node_retries)
|
||||
_, per_node_timeout, per_node_retries = self._calculate_timeouts(etcd_nodes)
|
||||
connect_timeout = max(1, per_node_timeout/2)
|
||||
kwargs.update(timeout=Timeout(connect=connect_timeout, total=per_node_timeout), retries=per_node_retries)
|
||||
return kwargs
|
||||
|
||||
def set_machines_cache_ttl(self, cache_ttl):
|
||||
self._machines_cache_ttl = cache_ttl
|
||||
|
||||
@abc.abstractmethod
|
||||
def _prepare_get_members(self, etcd_nodes):
|
||||
"""returns: request parameters"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def _get_members(self, base_uri, **kwargs):
|
||||
"""returns: list of clientURLs"""
|
||||
|
||||
@property
|
||||
def machines_cache(self):
|
||||
base_uri, cache = self._base_uri, self._machines_cache
|
||||
return ([base_uri] if base_uri in cache else []) + [machine for machine in cache if machine != base_uri]
|
||||
|
||||
@property
|
||||
def machines(self):
|
||||
"""Original `machines` method(property) of `etcd.Client` class raise exception
|
||||
@@ -155,73 +179,64 @@ class Client(etcd.Client):
|
||||
Also this method implements the same timeout-retry logic as `api_execute`, because
|
||||
the original method was retrying 2 times with the `read_timeout` on each node."""
|
||||
|
||||
kwargs = self._build_request_parameters()
|
||||
machines_cache = self.machines_cache
|
||||
kwargs = self._prepare_get_members(len(machines_cache))
|
||||
|
||||
while True:
|
||||
for base_uri in machines_cache:
|
||||
try:
|
||||
response = self.http.request(self._MGET, self._base_uri + self.version_prefix + '/machines', **kwargs)
|
||||
data = self._handle_server_response(response).data.decode('utf-8')
|
||||
machines = [m.strip() for m in data.split(',') if m.strip()]
|
||||
machines = list(self._get_members(base_uri, **kwargs))
|
||||
logger.debug("Retrieved list of machines: %s", machines)
|
||||
if not machines:
|
||||
raise etcd.EtcdException
|
||||
random.shuffle(machines)
|
||||
for url in machines:
|
||||
r = urlparse(url)
|
||||
port = r.port or (443 if r.scheme == 'https' else 80)
|
||||
self._dns_resolver.resolve_async(r.hostname, port)
|
||||
return machines
|
||||
if machines:
|
||||
random.shuffle(machines)
|
||||
self._update_dns_cache(self._dns_resolver.resolve_async, machines)
|
||||
return machines
|
||||
except Exception as e:
|
||||
# We can't get the list of machines, if one server is in the
|
||||
# machines cache, try on it
|
||||
logger.error("Failed to get list of machines from %s%s: %r", self._base_uri, self.version_prefix, e)
|
||||
if self._machines_cache:
|
||||
self._base_uri = self._machines_cache.pop(0)
|
||||
logger.info("Retrying on %s", self._base_uri)
|
||||
elif self._update_machines_cache:
|
||||
raise etcd.EtcdException("Could not get the list of servers, "
|
||||
"maybe you provided the wrong "
|
||||
"host(s) to connect to?")
|
||||
else:
|
||||
return []
|
||||
self.http.clear()
|
||||
logger.error("Failed to get list of machines from %s%s: %r", base_uri, self.version_prefix, e)
|
||||
|
||||
raise etcd.EtcdConnectionFailed('No more machines in the cluster')
|
||||
|
||||
def set_read_timeout(self, timeout):
|
||||
self._read_timeout = timeout
|
||||
|
||||
def _do_http_request(self, request_executor, method, url, fields=None, **kwargs):
|
||||
try:
|
||||
response = request_executor(method, url, fields=fields, **kwargs)
|
||||
response.data.decode('utf-8')
|
||||
self._check_cluster_id(response)
|
||||
except (HTTPError, HTTPException, socket.error, socket.timeout) as e:
|
||||
if (isinstance(fields, dict) and fields.get("wait") == "true" and
|
||||
isinstance(e, (ReadTimeoutError, ProtocolError))):
|
||||
logger.debug("Watch timed out.")
|
||||
def _do_http_request(self, retry, machines_cache, request_executor, method, path, fields=None, **kwargs):
|
||||
if fields is not None:
|
||||
kwargs['fields'] = fields
|
||||
some_request_failed = False
|
||||
for i, base_uri in enumerate(machines_cache):
|
||||
if i > 0:
|
||||
logger.info("Retrying on %s", base_uri)
|
||||
try:
|
||||
response = request_executor(method, base_uri + path, **kwargs)
|
||||
response.data.decode('utf-8')
|
||||
if some_request_failed:
|
||||
self.set_base_uri(base_uri)
|
||||
self._refresh_machines_cache()
|
||||
return response
|
||||
except (HTTPError, HTTPException, socket.error, socket.timeout) as e:
|
||||
self.http.clear()
|
||||
# switch to the next etcd node because we don't know exactly what happened,
|
||||
# whether the key didn't received an update or there is a network problem.
|
||||
self._machines_cache.insert(0, self._base_uri)
|
||||
self._base_uri = self._next_server()
|
||||
raise etcd.EtcdWatchTimedOut("Watch timed out: {0}".format(e), cause=e)
|
||||
logger.error("Request to server %s failed: %r", self._base_uri, e)
|
||||
logger.info("Reconnection allowed, looking for another server.")
|
||||
self._base_uri = self._next_server(cause=e)
|
||||
response = False
|
||||
return response
|
||||
if not retry and i + 1 < len(machines_cache):
|
||||
self.set_base_uri(machines_cache[i + 1])
|
||||
if (isinstance(fields, dict) and fields.get("wait") == "true" and
|
||||
isinstance(e, (ReadTimeoutError, ProtocolError))):
|
||||
logger.debug("Watch timed out.")
|
||||
raise etcd.EtcdWatchTimedOut("Watch timed out: {0}".format(e), cause=e)
|
||||
logger.error("Request to server %s failed: %r", base_uri, e)
|
||||
logger.info("Reconnection allowed, looking for another server.")
|
||||
if not retry:
|
||||
raise etcd.EtcdException('{0} {1} request failed'.format(method, path))
|
||||
some_request_failed = True
|
||||
|
||||
raise etcd.EtcdConnectionFailed('No more machines in the cluster')
|
||||
|
||||
@abc.abstractmethod
|
||||
def _prepare_request(self, kwargs, params=None, method=None):
|
||||
"""returns: request_executor"""
|
||||
|
||||
def api_execute(self, path, method, params=None, timeout=None):
|
||||
if not path.startswith('/'):
|
||||
raise ValueError('Path does not start with /')
|
||||
|
||||
retry = params.pop('retry', None) if isinstance(params, dict) else None
|
||||
kwargs = {'fields': params, 'preload_content': False}
|
||||
|
||||
if method in [self._MGET, self._MDELETE]:
|
||||
request_executor = self.http.request
|
||||
elif method in [self._MPUT, self._MPOST]:
|
||||
request_executor = self.http.request_encode_body
|
||||
kwargs['encode_multipart'] = False
|
||||
else:
|
||||
raise etcd.EtcdException('HTTP method {0} not supported'.format(method))
|
||||
|
||||
# Update machines_cache if previous attempt of update has failed
|
||||
if self._update_machines_cache:
|
||||
@@ -229,44 +244,36 @@ class Client(etcd.Client):
|
||||
elif not self._use_proxies and time.time() - self._machines_cache_updated > self._machines_cache_ttl:
|
||||
self._refresh_machines_cache()
|
||||
|
||||
kwargs.update(self._build_request_parameters(timeout))
|
||||
machines_cache = self.machines_cache
|
||||
etcd_nodes = len(machines_cache)
|
||||
|
||||
if retry:
|
||||
machines_cache = [self._base_uri] + self._machines_cache
|
||||
|
||||
response = False
|
||||
kwargs = self._prepare_common_parameters(etcd_nodes, timeout)
|
||||
request_executor = self._prepare_request(kwargs, params, method)
|
||||
|
||||
while True:
|
||||
try:
|
||||
some_request_failed = False
|
||||
while not response:
|
||||
response = self._do_http_request(request_executor, method, self._base_uri + path, **kwargs)
|
||||
|
||||
if response is False:
|
||||
if not retry:
|
||||
raise etcd.EtcdException('{0} {1} request failed'.format(method, path))
|
||||
some_request_failed = True
|
||||
if some_request_failed:
|
||||
self._refresh_machines_cache()
|
||||
if response:
|
||||
break
|
||||
except etcd.EtcdConnectionFailed:
|
||||
if not retry:
|
||||
raise
|
||||
response = self._do_http_request(retry, machines_cache, request_executor, method, path, **kwargs)
|
||||
return self._handle_server_response(response)
|
||||
except etcd.EtcdWatchTimedOut:
|
||||
raise
|
||||
except etcd.EtcdConnectionFailed as ex:
|
||||
try:
|
||||
if self._load_machines_cache():
|
||||
machines_cache = self.machines_cache
|
||||
etcd_nodes = len(machines_cache)
|
||||
except Exception as e:
|
||||
logger.debug('Failed to update list of etcd nodes: %r', e)
|
||||
sleeptime = retry.sleeptime
|
||||
remaining_time = retry.stoptime - sleeptime - time.time()
|
||||
nodes, timeout, retries = self._calculate_timeouts(len(machines_cache), remaining_time)
|
||||
nodes, timeout, retries = self._calculate_timeouts(etcd_nodes, remaining_time)
|
||||
if nodes == 0:
|
||||
self._update_machines_cache = True
|
||||
raise
|
||||
raise ex
|
||||
retry.sleep_func(sleeptime)
|
||||
retry.update_delay()
|
||||
# We still have some time left. Partially restore `_machines_cache` and retry request
|
||||
kwargs.update(timeout=timeout, retries=retries)
|
||||
self._base_uri = machines_cache[0]
|
||||
self._machines_cache = machines_cache[1:nodes]
|
||||
|
||||
return self._handle_server_response(response)
|
||||
# We still have some time left. Partially reduce `machines_cache` and retry request
|
||||
kwargs.update(timeout=Timeout(connect=max(1, timeout/2), total=timeout), retries=retries)
|
||||
machines_cache = machines_cache[:nodes]
|
||||
|
||||
@staticmethod
|
||||
def get_srv_record(host):
|
||||
@@ -327,6 +334,13 @@ class Client(etcd.Client):
|
||||
machines_cache = self._get_machines_cache_from_dns(self._config['host'], self._config['port'])
|
||||
return machines_cache
|
||||
|
||||
@staticmethod
|
||||
def _update_dns_cache(func, machines):
|
||||
for url in machines:
|
||||
r = urlparse(url)
|
||||
port = r.port or (443 if r.scheme == 'https' else 80)
|
||||
func(r.hostname, port)
|
||||
|
||||
def _load_machines_cache(self):
|
||||
"""This method should fill up `_machines_cache` from scratch.
|
||||
It could happen only in two cases:
|
||||
@@ -338,37 +352,91 @@ class Client(etcd.Client):
|
||||
if 'srv' not in self._config and 'host' not in self._config and 'hosts' not in self._config:
|
||||
raise Exception('Neither srv, hosts, host nor url are defined in etcd section of config')
|
||||
|
||||
self._machines_cache = self._get_machines_cache_from_config()
|
||||
|
||||
machines_cache = self._get_machines_cache_from_config()
|
||||
# Can not bootstrap list of etcd-cluster members, giving up
|
||||
if not self._machines_cache:
|
||||
if not machines_cache:
|
||||
raise etcd.EtcdException
|
||||
|
||||
# After filling up initial list of machines_cache we should ask etcd-cluster about actual list
|
||||
self._base_uri = self._next_server()
|
||||
self._refresh_machines_cache()
|
||||
self._update_machines_cache = False
|
||||
# enforce resolving dns name,they might get new ips
|
||||
self._update_dns_cache(self._dns_resolver.remove, machines_cache)
|
||||
|
||||
def _refresh_machines_cache(self):
|
||||
self._machines_cache = self._get_machines_cache_from_config() if self._use_proxies else self.machines
|
||||
if self._base_uri in self._machines_cache:
|
||||
self._machines_cache.remove(self._base_uri)
|
||||
elif self._machines_cache:
|
||||
self._base_uri = self._next_server()
|
||||
# The etcd cluster could change its topology over time and depending on how we resolve the initial
|
||||
# topology (list of hosts in the Patroni config or DNS records, A or SRV) we might get into the situation
|
||||
# the the real topology doesn't match anymore with the topology resolved from the configuration file.
|
||||
# In case if the "initial" topology is the same as before we will not override the `_machines_cache`.
|
||||
ret = set(machines_cache) != set(self._initial_machines_cache)
|
||||
if ret:
|
||||
self._initial_machines_cache = self._machines_cache = machines_cache
|
||||
|
||||
# After filling up the initial list of machines_cache we should ask etcd-cluster about actual list
|
||||
self._refresh_machines_cache(True)
|
||||
|
||||
self._update_machines_cache = False
|
||||
return ret
|
||||
|
||||
def _refresh_machines_cache(self, updating_cache=False):
|
||||
if self._use_proxies:
|
||||
self._machines_cache = self._get_machines_cache_from_config()
|
||||
else:
|
||||
try:
|
||||
self._machines_cache = self.machines
|
||||
except etcd.EtcdConnectionFailed:
|
||||
if updating_cache:
|
||||
raise etcd.EtcdException("Could not get the list of servers, "
|
||||
"maybe you provided the wrong "
|
||||
"host(s) to connect to?")
|
||||
return
|
||||
|
||||
if self._base_uri not in self._machines_cache:
|
||||
self.set_base_uri(self._machines_cache[0])
|
||||
self._machines_cache_updated = time.time()
|
||||
|
||||
def set_base_uri(self, value):
|
||||
logger.info('Selected new etcd server %s', value)
|
||||
self._base_uri = value
|
||||
|
||||
class Etcd(AbstractDCS):
|
||||
|
||||
def __init__(self, config):
|
||||
super(Etcd, self).__init__(config)
|
||||
self._ttl = int(config.get('ttl') or 30)
|
||||
class EtcdClient(AbstractEtcdClientWithFailover):
|
||||
|
||||
ERROR_CLS = EtcdError
|
||||
|
||||
def __del__(self):
|
||||
if self.http is not None:
|
||||
try:
|
||||
self.http.clear()
|
||||
except (ReferenceError, TypeError):
|
||||
pass
|
||||
|
||||
def _prepare_get_members(self, etcd_nodes):
|
||||
return self._prepare_common_parameters(etcd_nodes)
|
||||
|
||||
def _get_members(self, base_uri, **kwargs):
|
||||
response = self.http.request(self._MGET, base_uri + self.version_prefix + '/machines', **kwargs)
|
||||
data = self._handle_server_response(response).data.decode('utf-8')
|
||||
return [m.strip() for m in data.split(',') if m.strip()]
|
||||
|
||||
def _prepare_request(self, kwargs, params=None, method=None):
|
||||
kwargs['fields'] = params
|
||||
if method in (self._MPOST, self._MPUT):
|
||||
kwargs['encode_multipart'] = False
|
||||
return self.http.request
|
||||
|
||||
|
||||
class AbstractEtcd(AbstractDCS):
|
||||
|
||||
def __init__(self, config, client_cls, retry_errors_cls):
|
||||
super(AbstractEtcd, self).__init__(config)
|
||||
self._retry = Retry(deadline=config['retry_timeout'], max_delay=1, max_tries=-1,
|
||||
retry_exceptions=(etcd.EtcdLeaderElectionInProgress, EtcdRaftInternal))
|
||||
self._client = self.get_etcd_client(config)
|
||||
retry_exceptions=retry_errors_cls)
|
||||
self._ttl = int(config.get('ttl') or 30)
|
||||
self._client = self.get_etcd_client(config, client_cls)
|
||||
self.__do_not_watch = False
|
||||
self._has_failed = False
|
||||
|
||||
def reload_config(self, config):
|
||||
super(AbstractEtcd, self).reload_config(config)
|
||||
self._client.reload_config(config.get(self.__class__.__name__.lower(), {}))
|
||||
|
||||
def retry(self, *args, **kwargs):
|
||||
retry = self._retry.copy()
|
||||
kwargs['retry'] = retry
|
||||
@@ -385,22 +453,13 @@ class Etcd(AbstractDCS):
|
||||
if isinstance(raise_ex, Exception):
|
||||
raise raise_ex
|
||||
|
||||
def catch_etcd_errors(func):
|
||||
def wrapper(self, *args, **kwargs):
|
||||
try:
|
||||
retval = func(self, *args, **kwargs) is not None
|
||||
self._has_failed = False
|
||||
return retval
|
||||
except (RetryFailedError, etcd.EtcdException) as e:
|
||||
self._handle_exception(e)
|
||||
return False
|
||||
except Exception as e:
|
||||
self._handle_exception(e, raise_ex=EtcdError('unexpected error'))
|
||||
|
||||
return wrapper
|
||||
|
||||
@staticmethod
|
||||
def get_etcd_client(config):
|
||||
def set_socket_options(sock, socket_options):
|
||||
if socket_options:
|
||||
for opt in socket_options:
|
||||
sock.setsockopt(*opt)
|
||||
|
||||
def get_etcd_client(self, config, client_cls):
|
||||
if 'proxy' in config:
|
||||
config['use_proxies'] = True
|
||||
config['url'] = config['proxy']
|
||||
@@ -449,9 +508,7 @@ class Etcd(AbstractDCS):
|
||||
sock = None
|
||||
try:
|
||||
sock = socket.socket(af, socktype, proto)
|
||||
if socket_options:
|
||||
for opt in socket_options:
|
||||
sock.setsockopt(*opt)
|
||||
self.set_socket_options(sock, socket_options)
|
||||
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
|
||||
sock.settimeout(timeout)
|
||||
if source_address:
|
||||
@@ -475,7 +532,7 @@ class Etcd(AbstractDCS):
|
||||
client = None
|
||||
while not client:
|
||||
try:
|
||||
client = Client(config, dns_resolver)
|
||||
client = client_cls(config, dns_resolver)
|
||||
if 'use_proxies' in config and not client.machines:
|
||||
raise etcd.EtcdException
|
||||
except etcd.EtcdException:
|
||||
@@ -485,9 +542,10 @@ class Etcd(AbstractDCS):
|
||||
|
||||
def set_ttl(self, ttl):
|
||||
ttl = int(ttl)
|
||||
self.__do_not_watch = self._ttl != ttl
|
||||
ret = self._ttl != ttl
|
||||
self._ttl = ttl
|
||||
self._client.set_machines_cache_ttl(ttl*10)
|
||||
return ret
|
||||
|
||||
@property
|
||||
def ttl(self):
|
||||
@@ -497,6 +555,31 @@ class Etcd(AbstractDCS):
|
||||
self._retry.deadline = retry_timeout
|
||||
self._client.set_read_timeout(retry_timeout)
|
||||
|
||||
|
||||
def catch_etcd_errors(func):
|
||||
def wrapper(self, *args, **kwargs):
|
||||
try:
|
||||
retval = func(self, *args, **kwargs) is not None
|
||||
self._has_failed = False
|
||||
return retval
|
||||
except (RetryFailedError, etcd.EtcdException) as e:
|
||||
self._handle_exception(e)
|
||||
return False
|
||||
except Exception as e:
|
||||
self._handle_exception(e, raise_ex=self._client.ERROR_CLS('unexpected error'))
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class Etcd(AbstractEtcd):
|
||||
|
||||
def __init__(self, config):
|
||||
super(Etcd, self).__init__(config, EtcdClient, (etcd.EtcdLeaderElectionInProgress, EtcdRaftInternal))
|
||||
self.__do_not_watch = False
|
||||
|
||||
def set_ttl(self, ttl):
|
||||
self.__do_not_watch = super(Etcd, self).set_ttl(ttl)
|
||||
|
||||
@staticmethod
|
||||
def member(node):
|
||||
return Member.from_node(node.modifiedIndex, os.path.basename(node.key), node.ttl, node.value)
|
||||
@@ -594,7 +677,7 @@ class Etcd(AbstractDCS):
|
||||
return self.retry(self._client.write, self.initialize_path, sysid, prevExist=(not create_new))
|
||||
|
||||
@catch_etcd_errors
|
||||
def delete_leader(self):
|
||||
def _delete_leader(self):
|
||||
return self._client.delete(self.leader_path, prevValue=self._name)
|
||||
|
||||
@catch_etcd_errors
|
||||
@@ -627,13 +710,14 @@ class Etcd(AbstractDCS):
|
||||
|
||||
while timeout >= 1: # when timeout is too small urllib3 doesn't have enough time to connect
|
||||
try:
|
||||
self._client.watch(self.leader_path, index=leader_index, timeout=timeout + 0.5)
|
||||
result = self._client.watch(self.leader_path, index=leader_index, timeout=timeout + 0.5)
|
||||
self._has_failed = False
|
||||
if result.action == 'compareAndSwap':
|
||||
time.sleep(0.01)
|
||||
# Synchronous work of all cluster members with etcd is less expensive
|
||||
# than reestablishing http connection every time from every replica.
|
||||
return True
|
||||
except etcd.EtcdWatchTimedOut:
|
||||
self._client.http.clear()
|
||||
self._has_failed = False
|
||||
return False
|
||||
except (etcd.EtcdEventIndexCleared, etcd.EtcdWatcherCleared): # Watch failed
|
||||
|
||||
@@ -0,0 +1,795 @@
|
||||
from __future__ import absolute_import
|
||||
import base64
|
||||
import etcd
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import six
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import urllib3
|
||||
|
||||
from threading import Condition, Lock, Thread
|
||||
|
||||
from . import ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
||||
from .etcd import AbstractEtcdClientWithFailover, AbstractEtcd, catch_etcd_errors
|
||||
from ..exceptions import DCSError, PatroniException
|
||||
from ..utils import deep_compare, enable_keepalive, iter_response_objects, RetryFailedError, USER_AGENT
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Etcd3Error(DCSError):
|
||||
pass
|
||||
|
||||
|
||||
class UnsupportedEtcdVersion(PatroniException):
|
||||
pass
|
||||
|
||||
|
||||
# google.golang.org/grpc/codes
|
||||
GRPCCode = type('Enum', (), {'OK': 0, 'Canceled': 1, 'Unknown': 2, 'InvalidArgument': 3, 'DeadlineExceeded': 4,
|
||||
'NotFound': 5, 'AlreadyExists': 6, 'PermissionDenied': 7, 'ResourceExhausted': 8,
|
||||
'FailedPrecondition': 9, 'Aborted': 10, 'OutOfRange': 11, 'Unimplemented': 12,
|
||||
'Internal': 13, 'Unavailable': 14, 'DataLoss': 15, 'Unauthenticated': 16})
|
||||
GRPCcodeToText = {v: k for k, v in GRPCCode.__dict__.items() if not k.startswith('__') and isinstance(v, int)}
|
||||
|
||||
|
||||
class Etcd3Exception(etcd.EtcdException):
|
||||
pass
|
||||
|
||||
|
||||
class Etcd3ClientError(Etcd3Exception):
|
||||
|
||||
def __init__(self, code=None, error=None, status=None):
|
||||
if not hasattr(self, 'error'):
|
||||
self.error = error and error.strip()
|
||||
self.codeText = GRPCcodeToText.get(code)
|
||||
self.status = status
|
||||
|
||||
def __repr__(self):
|
||||
return "<{0} error: '{1}', code: {2}>".format(self.__class__.__name__, self.error, self.code)
|
||||
|
||||
__str__ = __repr__
|
||||
|
||||
def as_dict(self):
|
||||
return {'error': self.error, 'code': self.code, 'codeText': self.codeText, 'status': self.status}
|
||||
|
||||
@classmethod
|
||||
def get_subclasses(cls):
|
||||
for subclass in cls.__subclasses__():
|
||||
for subsubclass in subclass.get_subclasses():
|
||||
yield subsubclass
|
||||
yield subclass
|
||||
|
||||
|
||||
class Unknown(Etcd3ClientError):
|
||||
code = GRPCCode.Unknown
|
||||
|
||||
|
||||
class InvalidArgument(Etcd3ClientError):
|
||||
code = GRPCCode.InvalidArgument
|
||||
|
||||
|
||||
class DeadlineExceeded(Etcd3ClientError):
|
||||
code = GRPCCode.DeadlineExceeded
|
||||
error = "context deadline exceeded"
|
||||
|
||||
|
||||
class NotFound(Etcd3ClientError):
|
||||
code = GRPCCode.NotFound
|
||||
|
||||
|
||||
class FailedPrecondition(Etcd3ClientError):
|
||||
code = GRPCCode.FailedPrecondition
|
||||
|
||||
|
||||
class Unavailable(Etcd3ClientError):
|
||||
code = GRPCCode.Unavailable
|
||||
|
||||
|
||||
# https://github.com/etcd-io/etcd/blob/master/etcdserver/api/v3rpc/rpctypes/error.go
|
||||
class LeaseNotFound(NotFound):
|
||||
error = "etcdserver: requested lease not found"
|
||||
|
||||
|
||||
class UserEmpty(InvalidArgument):
|
||||
error = "etcdserver: user name is empty"
|
||||
|
||||
|
||||
class PermissionDenied(Etcd3ClientError):
|
||||
code = GRPCCode.PermissionDenied
|
||||
error = "etcdserver: permission denied"
|
||||
|
||||
|
||||
class AuthNotEnabled(FailedPrecondition):
|
||||
error = "etcdserver: authentication is not enabled"
|
||||
|
||||
|
||||
class InvalidAuthToken(Etcd3ClientError):
|
||||
code = GRPCCode.Unauthenticated
|
||||
error = "etcdserver: invalid auth token"
|
||||
|
||||
|
||||
errStringToClientError = {s.error: s for s in Etcd3ClientError.get_subclasses() if hasattr(s, 'error')}
|
||||
errCodeToClientError = {s.code: s for s in Etcd3ClientError.__subclasses__()}
|
||||
|
||||
|
||||
def _raise_for_data(data, status_code=None):
|
||||
try:
|
||||
error = data.get('error') or data.get('Error')
|
||||
if isinstance(error, dict): # streaming response
|
||||
status_code = error.get('http_code')
|
||||
code = error['grpc_code']
|
||||
error = error['message']
|
||||
else:
|
||||
code = data.get('code') or data.get('Code')
|
||||
except Exception:
|
||||
error = str(data)
|
||||
code = GRPCCode.Unknown
|
||||
err = errStringToClientError.get(error) or errCodeToClientError.get(code) or Unknown
|
||||
raise err(code, error, status_code)
|
||||
|
||||
|
||||
def to_bytes(v):
|
||||
return v if isinstance(v, bytes) else v.encode('utf-8')
|
||||
|
||||
|
||||
def prefix_range_end(v):
|
||||
v = bytearray(to_bytes(v))
|
||||
for i in range(len(v) - 1, -1, -1):
|
||||
if v[i] < 0xff:
|
||||
v[i] += 1
|
||||
break
|
||||
return bytes(v)
|
||||
|
||||
|
||||
def base64_encode(v):
|
||||
return base64.b64encode(to_bytes(v)).decode('utf-8')
|
||||
|
||||
|
||||
def base64_decode(v):
|
||||
return base64.b64decode(v).decode('utf-8')
|
||||
|
||||
|
||||
def build_range_request(key, range_end=None):
|
||||
fields = {'key': base64_encode(key)}
|
||||
if range_end:
|
||||
fields['range_end'] = base64_encode(range_end)
|
||||
return fields
|
||||
|
||||
|
||||
class Etcd3Client(AbstractEtcdClientWithFailover):
|
||||
|
||||
ERROR_CLS = Etcd3Error
|
||||
|
||||
def __init__(self, config, dns_resolver, cache_ttl=300):
|
||||
self._token = None
|
||||
self._cluster_version = None
|
||||
self.version_prefix = '/v3beta'
|
||||
super(Etcd3Client, self).__init__(config, dns_resolver, cache_ttl)
|
||||
|
||||
if six.PY2: # pragma: no cover
|
||||
# Old grpc-gateway sometimes sends double 'transfer-encoding: chunked' headers,
|
||||
# what breaks the old (python2.7) httplib.HTTPConnection (it closes the socket).
|
||||
def dedup_addheader(httpm, key, value):
|
||||
prev = httpm.dict.get(key)
|
||||
if prev is None:
|
||||
httpm.dict[key] = value
|
||||
elif key != 'transfer-encoding' or prev != value:
|
||||
combined = ", ".join((prev, value))
|
||||
httpm.dict[key] = combined
|
||||
|
||||
import httplib
|
||||
httplib.HTTPMessage.addheader = dedup_addheader
|
||||
|
||||
try:
|
||||
self.authenticate()
|
||||
except Exception as e:
|
||||
logger.fatal('Etcd3 authentication failed: %r', e)
|
||||
sys.exit(1)
|
||||
|
||||
def _get_headers(self):
|
||||
headers = urllib3.make_headers(user_agent=USER_AGENT)
|
||||
if self._token and self._cluster_version >= (3, 3, 0):
|
||||
headers['authorization'] = self._token
|
||||
return headers
|
||||
|
||||
def _prepare_request(self, kwargs, params=None, method=None):
|
||||
if params is not None:
|
||||
kwargs['body'] = json.dumps(params)
|
||||
kwargs['headers']['Content-Type'] = 'application/json'
|
||||
return self.http.urlopen
|
||||
|
||||
@staticmethod
|
||||
def _handle_server_response(response):
|
||||
data = response.data
|
||||
try:
|
||||
data = data.decode('utf-8')
|
||||
data = json.loads(data)
|
||||
except (TypeError, ValueError, UnicodeError) as e:
|
||||
if response.status < 400:
|
||||
raise etcd.EtcdException('Server response was not valid JSON: %r' % e)
|
||||
if response.status < 400:
|
||||
return data
|
||||
_raise_for_data(data, response.status)
|
||||
|
||||
def _ensure_version_prefix(self, base_uri, **kwargs):
|
||||
if self.version_prefix != '/v3':
|
||||
response = self.http.urlopen(self._MGET, base_uri + '/version', **kwargs)
|
||||
response = self._handle_server_response(response)
|
||||
|
||||
server_version_str = response['etcdserver']
|
||||
server_version = tuple(int(x) for x in server_version_str.split('.'))
|
||||
cluster_version_str = response['etcdcluster']
|
||||
self._cluster_version = tuple(int(x) for x in cluster_version_str.split('.'))
|
||||
|
||||
if self._cluster_version < (3, 0) or server_version < (3, 0, 4):
|
||||
raise UnsupportedEtcdVersion('Detected Etcd version {0} is lower than 3.0.4'.format(server_version_str))
|
||||
|
||||
if self._cluster_version < (3, 3):
|
||||
if self.version_prefix != '/v3alpha':
|
||||
if self._cluster_version < (3, 1):
|
||||
logger.warning('Detected Etcd version %s is lower than 3.1.0, watches are not supported',
|
||||
cluster_version_str)
|
||||
if self.username and self.password:
|
||||
logger.warning('Detected Etcd version %s is lower than 3.3.0, authentication is not supported',
|
||||
cluster_version_str)
|
||||
self.version_prefix = '/v3alpha'
|
||||
elif self._cluster_version < (3, 4):
|
||||
self.version_prefix = '/v3beta'
|
||||
else:
|
||||
self.version_prefix = '/v3'
|
||||
|
||||
def _prepare_get_members(self, etcd_nodes):
|
||||
kwargs = self._prepare_common_parameters(etcd_nodes)
|
||||
self._prepare_request(kwargs, {})
|
||||
return kwargs
|
||||
|
||||
def _get_members(self, base_uri, **kwargs):
|
||||
self._ensure_version_prefix(base_uri, **kwargs)
|
||||
resp = self.http.urlopen(self._MPOST, base_uri + self.version_prefix + '/cluster/member/list', **kwargs)
|
||||
members = self._handle_server_response(resp)['members']
|
||||
return set(url for member in members for url in member.get('clientURLs', []))
|
||||
|
||||
def call_rpc(self, method, fields, retry=None):
|
||||
fields['retry'] = retry
|
||||
return self.api_execute(self.version_prefix + method, self._MPOST, fields)
|
||||
|
||||
def authenticate(self):
|
||||
if self._cluster_version >= (3, 3) and self.username and self.password:
|
||||
logger.info('Trying to authenticate on Etcd...')
|
||||
old_token, self._token = self._token, None
|
||||
try:
|
||||
response = self.call_rpc('/auth/authenticate', {'name': self.username, 'password': self.password})
|
||||
except AuthNotEnabled:
|
||||
logger.info('Etcd authentication is not enabled')
|
||||
self._token = None
|
||||
except Exception:
|
||||
self._token = old_token
|
||||
raise
|
||||
else:
|
||||
self._token = response.get('token')
|
||||
return old_token != self._token
|
||||
|
||||
def _handle_auth_errors(func):
|
||||
def wrapper(self, *args, **kwargs):
|
||||
def retry(ex):
|
||||
if self.username and self.password:
|
||||
self.authenticate()
|
||||
return func(self, *args, **kwargs)
|
||||
else:
|
||||
logger.fatal('Username or password not set, authentication is not possible')
|
||||
raise ex
|
||||
|
||||
try:
|
||||
return func(self, *args, **kwargs)
|
||||
except (UserEmpty, PermissionDenied) as e: # no token provided
|
||||
# PermissionDenied is raised on 3.0 and 3.1
|
||||
if self._cluster_version < (3, 3) and (not isinstance(e, PermissionDenied)
|
||||
or self._cluster_version < (3, 2)):
|
||||
raise UnsupportedEtcdVersion('Authentication is required by Etcd cluster but not '
|
||||
'supported on version lower than 3.3.0. Cluster version: '
|
||||
'{0}'.format('.'.join(map(str, self._cluster_version))))
|
||||
return retry(e)
|
||||
except InvalidAuthToken as e:
|
||||
logger.error('Invalid auth token: %s', self._token)
|
||||
return retry(e)
|
||||
|
||||
return wrapper
|
||||
|
||||
@_handle_auth_errors
|
||||
def range(self, key, range_end=None, retry=None):
|
||||
params = build_range_request(key, range_end)
|
||||
params['serializable'] = True # For better performance. We can tolerate stale reads.
|
||||
return self.call_rpc('/kv/range', params, retry)
|
||||
|
||||
def prefix(self, key, retry=None):
|
||||
return self.range(key, prefix_range_end(key), retry)
|
||||
|
||||
def lease_grant(self, ttl, retry=None):
|
||||
return self.call_rpc('/lease/grant', {'TTL': ttl}, retry)['ID']
|
||||
|
||||
def lease_keepalive(self, ID, retry=None):
|
||||
return self.call_rpc('/lease/keepalive', {'ID': ID}, retry).get('result', {}).get('TTL')
|
||||
|
||||
def txn(self, compare, success, retry=None):
|
||||
return self.call_rpc('/kv/txn', {'compare': [compare], 'success': [success]}, retry).get('succeeded')
|
||||
|
||||
@_handle_auth_errors
|
||||
def put(self, key, value, lease=None, create_revision=None, mod_revision=None, retry=None):
|
||||
fields = {'key': base64_encode(key), 'value': base64_encode(value)}
|
||||
if lease:
|
||||
fields['lease'] = lease
|
||||
if create_revision is not None:
|
||||
compare = {'target': 'CREATE', 'create_revision': create_revision}
|
||||
elif mod_revision is not None:
|
||||
compare = {'target': 'MOD', 'mod_revision': mod_revision}
|
||||
else:
|
||||
return self.call_rpc('/kv/put', fields, retry)
|
||||
compare['key'] = fields['key']
|
||||
return self.txn(compare, {'request_put': fields}, retry)
|
||||
|
||||
@_handle_auth_errors
|
||||
def deleterange(self, key, range_end=None, mod_revision=None, retry=None):
|
||||
fields = build_range_request(key, range_end)
|
||||
if mod_revision is None:
|
||||
return self.call_rpc('/kv/deleterange', fields, retry)
|
||||
compare = {'target': 'MOD', 'mod_revision': mod_revision, 'key': fields['key']}
|
||||
return self.txn(compare, {'request_delete_range': fields}, retry)
|
||||
|
||||
def deleteprefix(self, key, retry=None):
|
||||
return self.deleterange(key, prefix_range_end(key), retry=retry)
|
||||
|
||||
def watchrange(self, key, range_end=None, start_revision=None, filters=None):
|
||||
"""returns: response object"""
|
||||
params = build_range_request(key, range_end)
|
||||
if start_revision is not None:
|
||||
params['start_revision'] = start_revision
|
||||
params['filters'] = filters or []
|
||||
kwargs = self._prepare_common_parameters(1, self.read_timeout)
|
||||
request_executor = self._prepare_request(kwargs, {'create_request': params})
|
||||
kwargs.update(timeout=urllib3.Timeout(connect=kwargs['timeout']), retries=0)
|
||||
return request_executor(self._MPOST, self._base_uri + self.version_prefix + '/watch', **kwargs)
|
||||
|
||||
def watchprefix(self, key, start_revision=None, filters=None):
|
||||
return self.watchrange(key, prefix_range_end(key), start_revision, filters)
|
||||
|
||||
|
||||
class KVCache(Thread):
|
||||
|
||||
def __init__(self, dcs, client):
|
||||
Thread.__init__(self)
|
||||
self.daemon = True
|
||||
self._dcs = dcs
|
||||
self._client = client
|
||||
self.condition = Condition()
|
||||
self._config_key = base64_encode(dcs.config_path)
|
||||
self._leader_key = base64_encode(dcs.leader_path)
|
||||
self._optime_key = base64_encode(dcs.leader_optime_path)
|
||||
self._name = base64_encode(dcs._name)
|
||||
self._is_ready = False
|
||||
self._response = None
|
||||
self._response_lock = Lock()
|
||||
self._object_cache = {}
|
||||
self._object_cache_lock = Lock()
|
||||
self.start()
|
||||
|
||||
def set(self, value, overwrite=False):
|
||||
with self._object_cache_lock:
|
||||
name = value['key']
|
||||
old_value = self._object_cache.get(name)
|
||||
ret = not old_value or int(old_value['mod_revision']) < int(value['mod_revision'])
|
||||
if ret or overwrite and old_value['mod_revision'] == value['mod_revision']:
|
||||
self._object_cache[name] = value
|
||||
return ret, old_value
|
||||
|
||||
def delete(self, name, mod_revision):
|
||||
with self._object_cache_lock:
|
||||
old_value = self._object_cache.get(name)
|
||||
ret = old_value and int(old_value['mod_revision']) < int(mod_revision)
|
||||
if ret:
|
||||
del self._object_cache[name]
|
||||
return not old_value or ret, old_value
|
||||
|
||||
def copy(self):
|
||||
with self._object_cache_lock:
|
||||
return [v.copy() for v in self._object_cache.values()]
|
||||
|
||||
def get(self, name):
|
||||
with self._object_cache_lock:
|
||||
return self._object_cache.get(name)
|
||||
|
||||
def _process_event(self, event):
|
||||
kv = event['kv']
|
||||
key = kv['key']
|
||||
if event.get('type') == 'DELETE':
|
||||
success, old_value = self.delete(key, kv['mod_revision'])
|
||||
else:
|
||||
success, old_value = self.set(kv, True)
|
||||
|
||||
if success:
|
||||
old_value = old_value and old_value.get('value')
|
||||
new_value = kv.get('value')
|
||||
|
||||
value_changed = old_value != new_value and \
|
||||
(key == self._leader_key or key == self._optime_key and new_value is not None or
|
||||
key == self._config_key and old_value is not None and new_value is not None)
|
||||
|
||||
if value_changed:
|
||||
logger.debug('%s changed from %s to %s', key, old_value, new_value)
|
||||
|
||||
# We also want to wake up HA loop on replicas if leader optime was updated
|
||||
if value_changed and (key != self._optime_key or self.get(self._leader_key) != self._name):
|
||||
self._dcs.event.set()
|
||||
|
||||
def _process_message(self, message):
|
||||
logger.debug('Received message: %s', message)
|
||||
if 'error' in message:
|
||||
_raise_for_data(message)
|
||||
for event in message.get('result', {}).get('events', []):
|
||||
self._process_event(event)
|
||||
|
||||
@staticmethod
|
||||
def _finish_response(response):
|
||||
try:
|
||||
response.close()
|
||||
finally:
|
||||
response.release_conn()
|
||||
|
||||
def _do_watch(self, revision):
|
||||
with self._response_lock:
|
||||
self._response = None
|
||||
response = self._client.watchprefix(self._dcs.cluster_prefix, revision)
|
||||
with self._response_lock:
|
||||
if self._response is None:
|
||||
self._response = response
|
||||
|
||||
if not self._response:
|
||||
return self._finish_response(response)
|
||||
|
||||
for message in iter_response_objects(response):
|
||||
self._process_message(message)
|
||||
|
||||
def _build_cache(self):
|
||||
result = self._dcs.retry(self._client.prefix, self._dcs.cluster_prefix)
|
||||
with self._object_cache_lock:
|
||||
self._object_cache = {node['key']: node for node in result.get('kvs', [])}
|
||||
with self.condition:
|
||||
self._is_ready = True
|
||||
self.condition.notify()
|
||||
|
||||
try:
|
||||
self._do_watch(result['header']['revision'])
|
||||
except Exception as e:
|
||||
logger.error('watchprefix failed: %r', e)
|
||||
finally:
|
||||
with self.condition:
|
||||
self._is_ready = False
|
||||
with self._response_lock:
|
||||
response, self._response = self._response, None
|
||||
if response:
|
||||
self._finish_response(response)
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
try:
|
||||
self._build_cache()
|
||||
except Exception as e:
|
||||
logger.error('KVCache.run %r', e)
|
||||
time.sleep(1)
|
||||
|
||||
def kill_stream(self):
|
||||
sock = None
|
||||
with self._response_lock:
|
||||
if self._response:
|
||||
try:
|
||||
sock = self._response.connection.sock
|
||||
except Exception:
|
||||
sock = None
|
||||
else:
|
||||
self._response = False
|
||||
if sock:
|
||||
try:
|
||||
sock.shutdown(socket.SHUT_RDWR)
|
||||
sock.close()
|
||||
except Exception as e:
|
||||
logger.debug('Error on socket.shutdown: %r', e)
|
||||
|
||||
def is_ready(self):
|
||||
"""Must be called only when holding the lock on `condition`"""
|
||||
return self._is_ready
|
||||
|
||||
|
||||
class PatroniEtcd3Client(Etcd3Client):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._kv_cache = None
|
||||
super(PatroniEtcd3Client, self).__init__(*args, **kwargs)
|
||||
|
||||
def configure(self, etcd3):
|
||||
self._etcd3 = etcd3
|
||||
|
||||
def start_watcher(self):
|
||||
if self._cluster_version >= (3, 1):
|
||||
self._kv_cache = KVCache(self._etcd3, self)
|
||||
|
||||
def _restart_watcher(self):
|
||||
if self._kv_cache:
|
||||
self._kv_cache.kill_stream()
|
||||
|
||||
def set_base_uri(self, value):
|
||||
super(PatroniEtcd3Client, self).set_base_uri(value)
|
||||
self._restart_watcher()
|
||||
|
||||
def authenticate(self):
|
||||
ret = super(PatroniEtcd3Client, self).authenticate()
|
||||
if ret:
|
||||
self._restart_watcher()
|
||||
return ret
|
||||
|
||||
def _wait_cache(self, timeout):
|
||||
stop_time = time.time() + timeout
|
||||
while not self._kv_cache.is_ready():
|
||||
timeout = stop_time - time.time()
|
||||
if timeout <= 0:
|
||||
raise RetryFailedError('Exceeded retry deadline')
|
||||
self._kv_cache.condition.wait(timeout)
|
||||
|
||||
def get_cluster(self):
|
||||
if self._kv_cache:
|
||||
with self._kv_cache.condition:
|
||||
self._wait_cache(self._etcd3._retry.deadline)
|
||||
return self._kv_cache.copy()
|
||||
else:
|
||||
return self._etcd3.retry(self.prefix, self._etcd3.cluster_prefix).get('kvs', [])
|
||||
|
||||
def call_rpc(self, method, fields, retry=None):
|
||||
ret = super(PatroniEtcd3Client, self).call_rpc(method, fields, retry)
|
||||
|
||||
if self._kv_cache:
|
||||
value = delete = None
|
||||
if method == '/kv/txn' and ret.get('succeeded'):
|
||||
on_success = fields['success'][0]
|
||||
value = on_success.get('request_put')
|
||||
delete = on_success.get('request_delete_range')
|
||||
elif method == '/kv/put' and ret:
|
||||
value = fields
|
||||
elif method == '/kv/deleterange' and ret:
|
||||
delete = fields
|
||||
|
||||
if value:
|
||||
value['mod_revision'] = ret['header']['revision']
|
||||
self._kv_cache.set(value)
|
||||
elif delete and 'range_end' not in delete:
|
||||
self._kv_cache.delete(delete['key'], ret['header']['revision'])
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
class Etcd3(AbstractEtcd):
|
||||
|
||||
def __init__(self, config):
|
||||
super(Etcd3, self).__init__(config, PatroniEtcd3Client, (DeadlineExceeded, Unavailable, FailedPrecondition))
|
||||
self.__do_not_watch = False
|
||||
self._lease = None
|
||||
self._last_lease_refresh = 0
|
||||
|
||||
self._client.configure(self)
|
||||
if not self._ctl:
|
||||
self._client.start_watcher()
|
||||
self.create_lease()
|
||||
|
||||
def set_socket_options(self, sock, socket_options):
|
||||
enable_keepalive(sock, self.ttl, int(self.loop_wait + self._retry.deadline))
|
||||
|
||||
def set_ttl(self, ttl):
|
||||
self.__do_not_watch = super(Etcd3, self).set_ttl(ttl)
|
||||
if self.__do_not_watch:
|
||||
self._lease = None
|
||||
|
||||
def _do_refresh_lease(self, retry=None):
|
||||
if self._lease and self._last_lease_refresh + self._loop_wait > time.time():
|
||||
return False
|
||||
|
||||
if self._lease and not self._client.lease_keepalive(self._lease, retry):
|
||||
self._lease = None
|
||||
|
||||
ret = not self._lease
|
||||
if ret:
|
||||
self._lease = self._client.lease_grant(self._ttl, retry)
|
||||
|
||||
self._last_lease_refresh = time.time()
|
||||
return ret
|
||||
|
||||
def refresh_lease(self):
|
||||
try:
|
||||
return self.retry(self._do_refresh_lease)
|
||||
except (Etcd3ClientError, RetryFailedError):
|
||||
logger.exception('refresh_lease')
|
||||
raise Etcd3Error('Failed ro keepalive/grant lease')
|
||||
|
||||
def create_lease(self):
|
||||
while not self._lease:
|
||||
try:
|
||||
self.refresh_lease()
|
||||
except Etcd3Error:
|
||||
logger.info('waiting on etcd')
|
||||
time.sleep(5)
|
||||
|
||||
@property
|
||||
def cluster_prefix(self):
|
||||
return self.client_path('')
|
||||
|
||||
@staticmethod
|
||||
def member(node):
|
||||
return Member.from_node(node['mod_revision'], os.path.basename(node['key']), node['lease'], node['value'])
|
||||
|
||||
def _load_cluster(self):
|
||||
cluster = None
|
||||
try:
|
||||
path_len = len(self.cluster_prefix)
|
||||
|
||||
nodes = {}
|
||||
for node in self._client.get_cluster():
|
||||
node['key'] = base64_decode(node['key'])
|
||||
node['value'] = base64_decode(node.get('value', ''))
|
||||
node['lease'] = node.get('lease')
|
||||
nodes[node['key'][path_len:].lstrip('/')] = node
|
||||
|
||||
# get initialize flag
|
||||
initialize = nodes.get(self._INITIALIZE)
|
||||
initialize = initialize and initialize['value']
|
||||
|
||||
# get global dynamic configuration
|
||||
config = nodes.get(self._CONFIG)
|
||||
config = config and ClusterConfig.from_node(config['mod_revision'], config['value'])
|
||||
|
||||
# get timeline history
|
||||
history = nodes.get(self._HISTORY)
|
||||
history = history and TimelineHistory.from_node(history['mod_revision'], history['value'])
|
||||
|
||||
# get last leader operation
|
||||
last_leader_operation = nodes.get(self._LEADER_OPTIME)
|
||||
last_leader_operation = 0 if last_leader_operation is None else int(last_leader_operation['value'])
|
||||
|
||||
# get list of members
|
||||
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
||||
|
||||
# get leader
|
||||
leader = nodes.get(self._LEADER)
|
||||
if not self._ctl and leader and leader['value'] == self._name and self._lease != leader.get('lease'):
|
||||
logger.warning('I am the leader but not owner of the lease')
|
||||
|
||||
if leader:
|
||||
member = Member(-1, leader['value'], None, {})
|
||||
member = ([m for m in members if m.name == leader['value']] or [member])[0]
|
||||
leader = Leader(leader['mod_revision'], leader['lease'], member)
|
||||
|
||||
# failover key
|
||||
failover = nodes.get(self._FAILOVER)
|
||||
if failover:
|
||||
failover = Failover.from_node(failover['mod_revision'], failover['value'])
|
||||
|
||||
# get synchronization state
|
||||
sync = nodes.get(self._SYNC)
|
||||
sync = SyncState.from_node(sync and sync['mod_revision'], sync and sync['value'])
|
||||
|
||||
cluster = Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
||||
except UnsupportedEtcdVersion:
|
||||
raise
|
||||
except Exception as e:
|
||||
self._handle_exception(e, 'get_cluster', raise_ex=Etcd3Error('Etcd is not responding properly'))
|
||||
self._has_failed = False
|
||||
return cluster
|
||||
|
||||
@catch_etcd_errors
|
||||
def touch_member(self, data, permanent=False):
|
||||
if not permanent:
|
||||
try:
|
||||
self.refresh_lease()
|
||||
except Etcd3Error:
|
||||
return False
|
||||
|
||||
cluster = self.cluster
|
||||
member = cluster and cluster.get_member(self._name, fallback_to_leader=False)
|
||||
|
||||
if member and member.session == self._lease and deep_compare(data, member.data):
|
||||
return True
|
||||
|
||||
data = json.dumps(data, separators=(',', ':'))
|
||||
try:
|
||||
return self._client.put(self.member_path, data, None if permanent else self._lease)
|
||||
except LeaseNotFound:
|
||||
self._lease = None
|
||||
logger.error('Our lease disappeared from Etcd, can not "touch_member"')
|
||||
|
||||
@catch_etcd_errors
|
||||
def take_leader(self):
|
||||
return self.retry(self._client.put, self.leader_path, self._name, self._lease)
|
||||
|
||||
@catch_etcd_errors
|
||||
def _do_attempt_to_acquire_leader(self, permanent):
|
||||
try:
|
||||
return self.retry(self._client.put, self.leader_path, self._name, None if permanent else self._lease, 0)
|
||||
except LeaseNotFound:
|
||||
self._lease = None
|
||||
logger.error('Our lease disappeared from Etcd. Will try to get a new one and retry attempt')
|
||||
self.refresh_lease()
|
||||
return self.retry(self._client.put, self.leader_path, self._name, None if permanent else self._lease, 0)
|
||||
|
||||
def attempt_to_acquire_leader(self, permanent=False):
|
||||
if not self._lease and not permanent:
|
||||
self.refresh_lease()
|
||||
|
||||
ret = self._do_attempt_to_acquire_leader(permanent)
|
||||
if not ret:
|
||||
logger.info('Could not take out TTL lock')
|
||||
return ret
|
||||
|
||||
@catch_etcd_errors
|
||||
def set_failover_value(self, value, index=None):
|
||||
return self._client.put(self.failover_path, value, mod_revision=index)
|
||||
|
||||
@catch_etcd_errors
|
||||
def set_config_value(self, value, index=None):
|
||||
return self._client.put(self.config_path, value, mod_revision=index)
|
||||
|
||||
@catch_etcd_errors
|
||||
def _write_leader_optime(self, last_operation):
|
||||
return self._client.put(self.leader_optime_path, last_operation)
|
||||
|
||||
@catch_etcd_errors
|
||||
def _update_leader(self):
|
||||
if not self._lease:
|
||||
self.refresh_lease()
|
||||
elif self.retry(self._client.lease_keepalive, self._lease):
|
||||
self._last_lease_refresh = time.time()
|
||||
|
||||
if self._lease:
|
||||
cluster = self.cluster
|
||||
leader_lease = cluster and isinstance(cluster.leader, Leader) and cluster.leader.session
|
||||
if leader_lease != self._lease:
|
||||
self.take_leader()
|
||||
return bool(self._lease)
|
||||
|
||||
@catch_etcd_errors
|
||||
def initialize(self, create_new=True, sysid=""):
|
||||
return self.retry(self._client.put, self.initialize_path, sysid, None, 0 if create_new else None)
|
||||
|
||||
@catch_etcd_errors
|
||||
def _delete_leader(self):
|
||||
cluster = self.cluster
|
||||
if cluster and isinstance(cluster.leader, Leader) and cluster.leader.name == self._name:
|
||||
return self._client.deleterange(self.leader_path, mod_revision=cluster.leader.index)
|
||||
|
||||
@catch_etcd_errors
|
||||
def cancel_initialization(self):
|
||||
return self.retry(self._client.deleterange, self.initialize_path)
|
||||
|
||||
@catch_etcd_errors
|
||||
def delete_cluster(self):
|
||||
return self.retry(self._client.deleteprefix, self.cluster_prefix)
|
||||
|
||||
@catch_etcd_errors
|
||||
def set_history_value(self, value):
|
||||
return self._client.put(self.history_path, value)
|
||||
|
||||
@catch_etcd_errors
|
||||
def set_sync_state_value(self, value, index=None):
|
||||
return self.retry(self._client.put, self.sync_path, value, mod_revision=index)
|
||||
|
||||
@catch_etcd_errors
|
||||
def delete_sync_state(self, index=None):
|
||||
return self.retry(self._client.deleterange, self.sync_path, mod_revision=index)
|
||||
|
||||
def watch(self, leader_index, timeout):
|
||||
if self.__do_not_watch:
|
||||
self.__do_not_watch = False
|
||||
return True
|
||||
|
||||
try:
|
||||
return super(Etcd3, self).watch(None, timeout)
|
||||
finally:
|
||||
self.event.clear()
|
||||
+593
-110
@@ -1,13 +1,16 @@
|
||||
from __future__ import absolute_import
|
||||
import datetime
|
||||
import functools
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
import socket
|
||||
import six
|
||||
import sys
|
||||
import time
|
||||
import urllib3
|
||||
import yaml
|
||||
|
||||
from kubernetes import client as k8s_client, config as k8s_config, watch as k8s_watch
|
||||
from urllib3 import Timeout
|
||||
from urllib3.exceptions import HTTPError
|
||||
from six.moves.http_client import HTTPException
|
||||
@@ -15,15 +18,410 @@ from threading import Condition, Lock, Thread
|
||||
|
||||
from . import AbstractDCS, Cluster, ClusterConfig, Failover, Leader, Member, SyncState, TimelineHistory
|
||||
from ..exceptions import DCSError
|
||||
from ..utils import deep_compare, Retry, RetryFailedError, tzutc, USER_AGENT
|
||||
from ..utils import deep_compare, iter_response_objects, keepalive_socket_options,\
|
||||
Retry, RetryFailedError, tzutc, uri, USER_AGENT
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
KUBE_CONFIG_DEFAULT_LOCATION = os.environ.get('KUBECONFIG', '~/.kube/config')
|
||||
SERVICE_HOST_ENV_NAME = 'KUBERNETES_SERVICE_HOST'
|
||||
SERVICE_PORT_ENV_NAME = 'KUBERNETES_SERVICE_PORT'
|
||||
SERVICE_TOKEN_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/token'
|
||||
SERVICE_CERT_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
|
||||
|
||||
|
||||
class KubernetesError(DCSError):
|
||||
pass
|
||||
|
||||
|
||||
# this function does the same mapping of snake_case => camelCase for > 97% of cases as autogenerated swagger code
|
||||
def to_camel_case(value):
|
||||
reserved = {'api', 'apiv3', 'cidr', 'cpu', 'csi', 'id', 'io', 'ip', 'ipc', 'pid', 'tls', 'uri', 'url', 'uuid'}
|
||||
words = value.split('_')
|
||||
return words[0] + ''.join(w.upper() if w in reserved else w.title() for w in words[1:])
|
||||
|
||||
|
||||
class K8sConfig(object):
|
||||
|
||||
class ConfigException(Exception):
|
||||
pass
|
||||
|
||||
def __init__(self):
|
||||
self.pool_config = {'maxsize': 10, 'num_pools': 10} # configuration for urllib3.PoolManager
|
||||
self._make_headers()
|
||||
|
||||
def _make_headers(self, token=None, **kwargs):
|
||||
self._headers = urllib3.make_headers(user_agent=USER_AGENT, **kwargs)
|
||||
if token:
|
||||
self._headers['authorization'] = 'Bearer ' + token
|
||||
|
||||
def load_incluster_config(self):
|
||||
if SERVICE_HOST_ENV_NAME not in os.environ or SERVICE_PORT_ENV_NAME not in os.environ:
|
||||
raise self.ConfigException('Service host/port is not set.')
|
||||
if not os.environ[SERVICE_HOST_ENV_NAME] or not os.environ[SERVICE_PORT_ENV_NAME]:
|
||||
raise self.ConfigException('Service host/port is set but empty.')
|
||||
if not os.path.isfile(SERVICE_CERT_FILENAME):
|
||||
raise self.ConfigException('Service certificate file does not exists.')
|
||||
with open(SERVICE_CERT_FILENAME) as f:
|
||||
if not f.read():
|
||||
raise self.ConfigException('Cert file exists but empty.')
|
||||
if not os.path.isfile(SERVICE_TOKEN_FILENAME):
|
||||
raise self.ConfigException('Service token file does not exists.')
|
||||
with open(SERVICE_TOKEN_FILENAME) as f:
|
||||
token = f.read()
|
||||
if not token:
|
||||
raise self.ConfigException('Token file exists but empty.')
|
||||
self._make_headers(token=token)
|
||||
self.pool_config['ca_certs'] = SERVICE_CERT_FILENAME
|
||||
self._server = uri('https', (os.environ[SERVICE_HOST_ENV_NAME], os.environ[SERVICE_PORT_ENV_NAME]))
|
||||
|
||||
@staticmethod
|
||||
def _get_by_name(config, section, name):
|
||||
for c in config[section + 's']:
|
||||
if c['name'] == name:
|
||||
return c[section]
|
||||
|
||||
def load_kube_config(self, context=None):
|
||||
with open(os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)) as f:
|
||||
config = yaml.safe_load(f)
|
||||
|
||||
context = self._get_by_name(config, 'context', context or config['current-context'])
|
||||
cluster = self._get_by_name(config, 'cluster', context['cluster'])
|
||||
user = self._get_by_name(config, 'user', context['user'])
|
||||
|
||||
self._server = cluster['server'].rstrip('/')
|
||||
if self._server.startswith('https'):
|
||||
self.pool_config.update({v: user[k] for k, v in {'client-certificate': 'cert_file',
|
||||
'client-key': 'key_file'}.items() if k in user})
|
||||
if 'certificate-authority' in cluster:
|
||||
self.pool_config['ca_certs'] = cluster['certificate-authority']
|
||||
self.pool_config['cert_reqs'] = 'CERT_NONE' if cluster.get('insecure-skip-tls-verify') else 'CERT_REQUIRED'
|
||||
if user.get('token'):
|
||||
self._make_headers(token=user['token'])
|
||||
elif 'username' in user and 'password' in user:
|
||||
self._headers = self._make_headers(basic_auth=':'.join((user['username'], user['password'])))
|
||||
|
||||
@property
|
||||
def server(self):
|
||||
return self._server
|
||||
|
||||
@property
|
||||
def headers(self):
|
||||
return self._headers.copy()
|
||||
|
||||
|
||||
class K8sObject(object):
|
||||
|
||||
def __init__(self, kwargs):
|
||||
self._dict = {k: self._wrap(k, v) for k, v in kwargs.items()}
|
||||
|
||||
def get(self, name, default=None):
|
||||
return self._dict.get(name, default)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return self.get(to_camel_case(name))
|
||||
|
||||
@classmethod
|
||||
def _wrap(cls, parent, value):
|
||||
if isinstance(value, dict):
|
||||
# we know that `annotations` and `labels` are dicts and therefore don't want to convert them into K8sObject
|
||||
return value if parent in {'annotations', 'labels'} and \
|
||||
all(isinstance(v, six.string_types) for v in value.values()) else cls(value)
|
||||
elif isinstance(value, list):
|
||||
return [cls._wrap(None, v) for v in value]
|
||||
else:
|
||||
return value
|
||||
|
||||
def to_dict(self):
|
||||
return self._dict
|
||||
|
||||
def __repr__(self):
|
||||
return json.dumps(self, indent=4, default=lambda o: o.to_dict())
|
||||
|
||||
|
||||
class K8sException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class K8sConnectionFailed(K8sException):
|
||||
pass
|
||||
|
||||
|
||||
class K8sClient(object):
|
||||
|
||||
class rest(object):
|
||||
|
||||
class ApiException(Exception):
|
||||
def __init__(self, status=None, reason=None, http_resp=None):
|
||||
self.status = http_resp.status if http_resp else status
|
||||
self.reason = http_resp.reason if http_resp else reason
|
||||
self.body = http_resp.data if http_resp else None
|
||||
self.headers = http_resp.getheaders() if http_resp else None
|
||||
|
||||
def __str__(self):
|
||||
error_message = "({0})\nReason: {1}\n".format(self.status, self.reason)
|
||||
if self.headers:
|
||||
error_message += "HTTP response headers: {0}\n".format(self.headers)
|
||||
if self.body:
|
||||
error_message += "HTTP response body: {0}\n".format(self.body)
|
||||
return error_message
|
||||
|
||||
class ApiClient(object):
|
||||
|
||||
_API_URL_PREFIX = '/api/v1/namespaces/'
|
||||
|
||||
def __init__(self, bypass_api_service=False):
|
||||
self._bypass_api_service = bypass_api_service
|
||||
self.pool_manager = urllib3.PoolManager(**k8s_config.pool_config)
|
||||
self._base_uri = k8s_config.server
|
||||
self._api_servers_cache = [k8s_config.server]
|
||||
self._api_servers_cache_updated = 0
|
||||
self.set_api_servers_cache_ttl(10)
|
||||
self.set_read_timeout(10)
|
||||
try:
|
||||
self._load_api_servers_cache()
|
||||
except K8sException:
|
||||
pass
|
||||
|
||||
def set_read_timeout(self, timeout):
|
||||
self._read_timeout = timeout
|
||||
|
||||
def set_api_servers_cache_ttl(self, ttl):
|
||||
self._api_servers_cache_ttl = ttl - 0.5
|
||||
|
||||
def set_base_uri(self, value):
|
||||
logger.info('Selected new K8s API server endpoint %s', value)
|
||||
# We will connect by IP of the master node which is not listed as alternative name
|
||||
self.pool_manager.connection_pool_kw['assert_hostname'] = False
|
||||
self._base_uri = value
|
||||
|
||||
@staticmethod
|
||||
def _handle_server_response(response, _preload_content):
|
||||
if response.status not in range(200, 206):
|
||||
raise k8s_client.rest.ApiException(http_resp=response)
|
||||
return K8sObject(json.loads(response.data.decode('utf-8'))) if _preload_content else response
|
||||
|
||||
@staticmethod
|
||||
def _make_headers(headers):
|
||||
ret = k8s_config.headers
|
||||
ret.update(headers or {})
|
||||
return ret
|
||||
|
||||
@property
|
||||
def api_servers_cache(self):
|
||||
base_uri, cache = self._base_uri, self._api_servers_cache
|
||||
return ([base_uri] if base_uri in cache else []) + [machine for machine in cache if machine != base_uri]
|
||||
|
||||
def _get_api_servers(self, api_servers_cache):
|
||||
_, per_node_timeout, per_node_retries = self._calculate_timeouts(len(api_servers_cache))
|
||||
kwargs = {'headers': self._make_headers({}), 'preload_content': True, 'retries': per_node_retries,
|
||||
'timeout': urllib3.Timeout(connect=max(1, per_node_timeout/2.0), total=per_node_timeout)}
|
||||
path = self._API_URL_PREFIX + 'default/endpoints/kubernetes'
|
||||
for base_uri in api_servers_cache:
|
||||
try:
|
||||
response = self.pool_manager.request('GET', base_uri + path, **kwargs)
|
||||
endpoint = self._handle_server_response(response, True)
|
||||
for subset in endpoint.subsets:
|
||||
for port in subset.ports:
|
||||
if port.name == 'https' and port.protocol == 'TCP':
|
||||
addresses = [uri('https', (a.ip, port.port)) for a in subset.addresses]
|
||||
if addresses:
|
||||
random.shuffle(addresses)
|
||||
return addresses
|
||||
except Exception as e:
|
||||
if isinstance(e, k8s_client.rest.ApiException) and e.status == 403:
|
||||
raise
|
||||
self.pool_manager.clear()
|
||||
logger.error('Failed to get "kubernetes" endpoint from %s: %r', base_uri, e)
|
||||
raise K8sConnectionFailed('No more K8s API server nodes in the cluster')
|
||||
|
||||
def _refresh_api_servers_cache(self, updating_cache=False):
|
||||
if self._bypass_api_service:
|
||||
try:
|
||||
api_servers_cache = [k8s_config.server] if updating_cache else self.api_servers_cache
|
||||
self._api_servers_cache = self._get_api_servers(api_servers_cache)
|
||||
if updating_cache:
|
||||
self.pool_manager.clear()
|
||||
except k8s_client.rest.ApiException: # 403 Permission denied
|
||||
logger.warning("Kubernetes RBAC doesn't allow GET access to the 'kubernetes' "
|
||||
"endpoint in the 'default' namespace. Disabling 'bypass_api_service'.")
|
||||
self._bypass_api_service = False
|
||||
self._api_servers_cache = [k8s_config.server]
|
||||
if not updating_cache:
|
||||
self.pool_manager.clear()
|
||||
except K8sConnectionFailed:
|
||||
if updating_cache:
|
||||
raise K8sException("Could not get the list of K8s API server nodes")
|
||||
return
|
||||
else:
|
||||
self._api_servers_cache = [k8s_config.server]
|
||||
|
||||
if self._base_uri not in self._api_servers_cache:
|
||||
self.set_base_uri(self._api_servers_cache[0])
|
||||
self._api_servers_cache_updated = time.time()
|
||||
|
||||
def refresh_api_servers_cache(self):
|
||||
if self._bypass_api_service and time.time() - self._api_servers_cache_updated > self._api_servers_cache_ttl:
|
||||
self._refresh_api_servers_cache()
|
||||
|
||||
def _load_api_servers_cache(self):
|
||||
self._update_api_servers_cache = True
|
||||
self._refresh_api_servers_cache(True)
|
||||
self._update_api_servers_cache = False
|
||||
|
||||
def _calculate_timeouts(self, api_servers, timeout=None):
|
||||
"""Calculate a request timeout and number of retries per single K8s API server node.
|
||||
In case if the timeout per node is too small (less than one second) we will reduce the number of nodes.
|
||||
For the cluster with only one API server node we will try to do 1 retry.
|
||||
No retries for clusters with 2 or more API server nodes. We better rely on switching to a different node."""
|
||||
|
||||
per_node_timeout = timeout = float(timeout or self._read_timeout)
|
||||
|
||||
max_retries = 3 - min(api_servers, 2)
|
||||
per_node_retries = 1
|
||||
min_timeout = 1.0
|
||||
|
||||
while api_servers > 0:
|
||||
per_node_timeout = float(timeout) / api_servers
|
||||
if per_node_timeout >= min_timeout:
|
||||
# for small clusters we will try to do more than one try on every node
|
||||
while per_node_retries < max_retries and per_node_timeout / (per_node_retries + 1) >= min_timeout:
|
||||
per_node_retries += 1
|
||||
per_node_timeout /= per_node_retries
|
||||
break
|
||||
# if the timeout per one node is to small try to reduce number of nodes
|
||||
api_servers -= 1
|
||||
max_retries = 1
|
||||
|
||||
return api_servers, per_node_timeout, per_node_retries - 1
|
||||
|
||||
def _do_http_request(self, retry, api_servers_cache, method, path, **kwargs):
|
||||
some_request_failed = False
|
||||
for i, base_uri in enumerate(api_servers_cache):
|
||||
if i > 0:
|
||||
logger.info('Retrying on %s', base_uri)
|
||||
try:
|
||||
response = self.pool_manager.request(method, base_uri + path, **kwargs)
|
||||
if some_request_failed:
|
||||
self.set_base_uri(base_uri)
|
||||
self._refresh_api_servers_cache()
|
||||
return response
|
||||
except (HTTPError, HTTPException, socket.error, socket.timeout) as e:
|
||||
self.pool_manager.clear()
|
||||
if not retry:
|
||||
# switch to the next node if request failed and retry is not allowed
|
||||
if i + 1 < len(api_servers_cache):
|
||||
self.set_base_uri(api_servers_cache[i + 1])
|
||||
raise K8sException('{0} {1} request failed'.format(method, path))
|
||||
logger.error('Request to server %s failed: %r', base_uri, e)
|
||||
some_request_failed = True
|
||||
|
||||
raise K8sConnectionFailed('No more API server nodes in the cluster')
|
||||
|
||||
def request(self, retry, method, path, timeout=None, **kwargs):
|
||||
if self._update_api_servers_cache:
|
||||
self._load_api_servers_cache()
|
||||
|
||||
api_servers_cache = self.api_servers_cache
|
||||
api_servers = len(api_servers_cache)
|
||||
|
||||
if timeout:
|
||||
if isinstance(timeout, six.integer_types + (float,)):
|
||||
timeout = urllib3.Timeout(total=timeout)
|
||||
elif isinstance(timeout, tuple) and len(timeout) == 2:
|
||||
timeout = urllib3.Timeout(connect=timeout[0], read=timeout[1])
|
||||
retries = 0
|
||||
else:
|
||||
_, timeout, retries = self._calculate_timeouts(api_servers)
|
||||
timeout = urllib3.Timeout(connect=max(1, timeout/2.0), total=timeout)
|
||||
kwargs.update(retries=retries, timeout=timeout)
|
||||
|
||||
while True:
|
||||
try:
|
||||
return self._do_http_request(retry, api_servers_cache, method, path, **kwargs)
|
||||
except K8sConnectionFailed as ex:
|
||||
try:
|
||||
self._load_api_servers_cache()
|
||||
api_servers_cache = self.api_servers_cache
|
||||
api_servers = len(api_servers)
|
||||
except Exception as e:
|
||||
logger.debug('Failed to update list of K8s master nodes: %r', e)
|
||||
|
||||
sleeptime = retry.sleeptime
|
||||
remaining_time = retry.stoptime - sleeptime - time.time()
|
||||
nodes, timeout, retries = self._calculate_timeouts(api_servers, remaining_time)
|
||||
if nodes == 0:
|
||||
self._update_api_servers_cache = True
|
||||
raise ex
|
||||
retry.sleep_func(sleeptime)
|
||||
retry.update_delay()
|
||||
# We still have some time left. Partially reduce `api_servers_cache` and retry request
|
||||
kwargs.update(timeout=urllib3.Timeout(connect=max(1, timeout/2.0), total=timeout), retries=retries)
|
||||
api_servers_cache = api_servers_cache[:nodes]
|
||||
|
||||
def call_api(self, method, path, headers=None, body=None, _retry=None,
|
||||
_preload_content=True, _request_timeout=None, **kwargs):
|
||||
headers = self._make_headers(headers)
|
||||
fields = {to_camel_case(k): v for k, v in kwargs.items()} # resource_version => resourceVersion
|
||||
body = json.dumps(body, default=lambda o: o.to_dict()) if body is not None else None
|
||||
|
||||
response = self.request(_retry, method, self._API_URL_PREFIX + path, headers=headers, fields=fields,
|
||||
body=body, preload_content=_preload_content, timeout=_request_timeout)
|
||||
|
||||
return self._handle_server_response(response, _preload_content)
|
||||
|
||||
class CoreV1Api(object):
|
||||
|
||||
def __init__(self, api_client=None):
|
||||
self._api_client = api_client or k8s_client.ApiClient()
|
||||
|
||||
def __getattr__(self, func): # `func` name pattern: (action)_namespaced_(kind)
|
||||
action, kind = func.split('_namespaced_') # (read|list|create|patch|replace|delete|delete_collection)
|
||||
kind = kind.replace('_', '') + ('s' * int(kind[-1] != 's')) # plural, single word
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
method = {'read': 'GET', 'list': 'GET', 'create': 'POST',
|
||||
'replace': 'PUT'}.get(action, action.split('_')[0]).upper()
|
||||
|
||||
if action == 'create' or len(args) == 1: # namespace is a first argument and name in not in arguments
|
||||
path = '/'.join([args[0], kind])
|
||||
else: # name, namespace followed by optional body
|
||||
path = '/'.join([args[1], kind, args[0]])
|
||||
|
||||
headers = {'Content-Type': 'application/strategic-merge-patch+json'} if action == 'patch' else {}
|
||||
|
||||
if len(args) == 3: # name, namespace, body
|
||||
body = args[2]
|
||||
elif action == 'create': # namespace, body
|
||||
body = args[1]
|
||||
elif action == 'delete': # name, namespace
|
||||
body = kwargs.pop('body', None)
|
||||
else:
|
||||
body = None
|
||||
|
||||
return self._api_client.call_api(method, path, headers, body, **kwargs)
|
||||
return wrapper
|
||||
|
||||
class _K8sObjectTemplate(K8sObject):
|
||||
"""The template for objects which we create locally, e.g. k8s_client.V1ObjectMeta & co"""
|
||||
def __init__(self, **kwargs):
|
||||
self._dict = {to_camel_case(k): v for k, v in kwargs.items()}
|
||||
|
||||
def __init__(self):
|
||||
self.__cls_cache = {}
|
||||
self.__cls_lock = Lock()
|
||||
|
||||
def __getattr__(self, name):
|
||||
with self.__cls_lock:
|
||||
if name not in self.__cls_cache:
|
||||
self.__cls_cache[name] = type(name, (self._K8sObjectTemplate,), {})
|
||||
return self.__cls_cache[name]
|
||||
|
||||
|
||||
k8s_client = K8sClient()
|
||||
k8s_config = K8sConfig()
|
||||
|
||||
|
||||
class KubernetesRetriableException(k8s_client.rest.ApiException):
|
||||
|
||||
def __init__(self, orig):
|
||||
@@ -31,48 +429,51 @@ class KubernetesRetriableException(k8s_client.rest.ApiException):
|
||||
self.body = orig.body
|
||||
self.headers = orig.headers
|
||||
|
||||
@property
|
||||
def sleeptime(self):
|
||||
try:
|
||||
return int(self.headers['retry-after'])
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
class CoreV1ApiProxy(object):
|
||||
|
||||
def __init__(self, use_endpoints=False):
|
||||
self._api = k8s_client.CoreV1Api()
|
||||
self._api.api_client.user_agent = USER_AGENT
|
||||
self._api.api_client.rest_client.pool_manager.connection_pool_kw['maxsize'] = 10
|
||||
self._request_timeout = None
|
||||
self._use_endpoints = use_endpoints
|
||||
def __init__(self, use_endpoints=False, bypass_api_service=False):
|
||||
self._api_client = k8s_client.ApiClient(bypass_api_service)
|
||||
self._core_v1_api = k8s_client.CoreV1Api(self._api_client)
|
||||
self._use_endpoints = bool(use_endpoints)
|
||||
|
||||
def configure_timeouts(self, loop_wait, retry_timeout, ttl):
|
||||
# Normally every loop_wait seconds we should have receive something from the socket.
|
||||
# If we didn't received anything after the loop_wait + retry_timeout it is a time
|
||||
# to start worrying (send keepalive messages). Finally, the connection should be
|
||||
# considered as dead if we received nothing from the socket after the ttl seconds.
|
||||
cnt = 3
|
||||
idle = int(loop_wait + retry_timeout)
|
||||
intvl = max(1, int(float(ttl - idle) / cnt))
|
||||
self._api.api_client.rest_client.pool_manager.connection_pool_kw['socket_options'] = [
|
||||
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
|
||||
(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle),
|
||||
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, intvl),
|
||||
(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, cnt),
|
||||
(socket.IPPROTO_TCP, 18, int(ttl * 1000)) # TCP_USER_TIMEOUT
|
||||
]
|
||||
self._request_timeout = (1, retry_timeout / 3.0)
|
||||
self._api_client.pool_manager.connection_pool_kw['socket_options'] = \
|
||||
list(keepalive_socket_options(ttl, int(loop_wait + retry_timeout)))
|
||||
self._api_client.set_read_timeout(retry_timeout)
|
||||
self._api_client.set_api_servers_cache_ttl(loop_wait)
|
||||
|
||||
def refresh_api_servers_cache(self):
|
||||
self._api_client.refresh_api_servers_cache()
|
||||
|
||||
def __getattr__(self, func):
|
||||
if func.endswith('_kind'):
|
||||
func = func[:-4] + ('endpoints' if self._use_endpoints else 'config_map')
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
if '_request_timeout' not in kwargs:
|
||||
kwargs['_request_timeout'] = self._request_timeout
|
||||
try:
|
||||
return getattr(self._api, func)(*args, **kwargs)
|
||||
return getattr(self._core_v1_api, func)(*args, **kwargs)
|
||||
except k8s_client.rest.ApiException as e:
|
||||
if e.status in (502, 503, 504): # XXX
|
||||
if e.status in (500, 503, 504) or e.headers and 'retry-after' in e.headers: # XXX
|
||||
raise KubernetesRetriableException(e)
|
||||
raise
|
||||
return wrapper
|
||||
|
||||
@property
|
||||
def use_endpoints(self):
|
||||
return self._use_endpoints
|
||||
|
||||
|
||||
def catch_kubernetes_errors(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
@@ -84,21 +485,21 @@ def catch_kubernetes_errors(func):
|
||||
elif e.status != 409: # Object exists or conflict in resource_version
|
||||
logger.exception('Unexpected error from Kubernetes API')
|
||||
return False
|
||||
except (RetryFailedError, HTTPException, HTTPError, socket.error, socket.timeout):
|
||||
except (RetryFailedError, K8sException):
|
||||
return False
|
||||
return wrapper
|
||||
|
||||
|
||||
class ObjectCache(Thread):
|
||||
|
||||
def __init__(self, dcs, func, retry, condition):
|
||||
def __init__(self, dcs, func, retry, condition, name=None):
|
||||
Thread.__init__(self)
|
||||
self.daemon = True
|
||||
self._api_client = k8s_client.ApiClient()
|
||||
self._dcs = dcs
|
||||
self._func = func
|
||||
self._retry = retry
|
||||
self._condition = condition
|
||||
self._name = name # name of this pod
|
||||
self._is_ready = False
|
||||
self._object_cache = {}
|
||||
self._object_cache_lock = Lock()
|
||||
@@ -106,7 +507,11 @@ class ObjectCache(Thread):
|
||||
self.start()
|
||||
|
||||
def _list(self):
|
||||
return self._func(_request_timeout=(self._retry.deadline, Timeout.DEFAULT_TIMEOUT))
|
||||
try:
|
||||
return self._func(_retry=self._retry.copy())
|
||||
except Exception:
|
||||
time.sleep(1)
|
||||
raise
|
||||
|
||||
def _watch(self, resource_version):
|
||||
return self._func(_request_timeout=(self._retry.deadline, Timeout.DEFAULT_TIMEOUT),
|
||||
@@ -132,6 +537,10 @@ class ObjectCache(Thread):
|
||||
with self._object_cache_lock:
|
||||
return self._object_cache.copy()
|
||||
|
||||
def get(self, name):
|
||||
with self._object_cache_lock:
|
||||
return self._object_cache.get(name)
|
||||
|
||||
def _build_cache(self):
|
||||
objects = self._list()
|
||||
return_type = 'V1' + objects.kind[:-4]
|
||||
@@ -143,8 +552,7 @@ class ObjectCache(Thread):
|
||||
|
||||
response = self._watch(objects.metadata.resource_version)
|
||||
try:
|
||||
for line in k8s_watch.watch.iter_resp_lines(response):
|
||||
event = json.loads(line)
|
||||
for event in iter_response_objects(response):
|
||||
obj = event['object']
|
||||
if obj.get('code') == 410:
|
||||
break
|
||||
@@ -153,8 +561,7 @@ class ObjectCache(Thread):
|
||||
name = obj['metadata']['name']
|
||||
|
||||
if ev_type in ('ADDED', 'MODIFIED'):
|
||||
obj = k8s_watch.watch.SimpleNamespace(data=json.dumps(obj))
|
||||
obj = self._api_client.deserialize(obj, return_type)
|
||||
obj = K8sObject(obj)
|
||||
success, old_value = self.set(name, obj)
|
||||
if success:
|
||||
new_value = (obj.metadata.annotations or {}).get(self._annotations_map.get(name))
|
||||
@@ -169,9 +576,14 @@ class ObjectCache(Thread):
|
||||
if old_value:
|
||||
old_value = (old_value.metadata.annotations or {}).get(self._annotations_map.get(name))
|
||||
|
||||
if old_value != new_value and \
|
||||
(name != self._dcs.config_path or old_value is not None and new_value is not None):
|
||||
value_changed = old_value != new_value and \
|
||||
(name != self._dcs.config_path or old_value is not None and new_value is not None)
|
||||
|
||||
if value_changed:
|
||||
logger.debug('%s changed from %s to %s', name, old_value, new_value)
|
||||
|
||||
# Do not wake up HA loop if we run as leader and received leader object update event
|
||||
if value_changed or name == self._dcs.leader_path and self._name != new_value:
|
||||
self._dcs.event.set()
|
||||
finally:
|
||||
with self._condition:
|
||||
@@ -204,32 +616,28 @@ class Kubernetes(AbstractDCS):
|
||||
config['namespace'] = ''
|
||||
super(Kubernetes, self).__init__(config)
|
||||
self._retry = Retry(deadline=config['retry_timeout'], max_delay=1, max_tries=-1,
|
||||
retry_exceptions=(KubernetesRetriableException, HTTPException,
|
||||
HTTPError, socket.error, socket.timeout))
|
||||
retry_exceptions=KubernetesRetriableException)
|
||||
self._ttl = None
|
||||
try:
|
||||
k8s_config.load_incluster_config()
|
||||
except k8s_config.ConfigException:
|
||||
k8s_config.load_kube_config(context=config.get('context', 'local'))
|
||||
|
||||
self.__subsets = None
|
||||
use_endpoints = config.get('use_endpoints') and (config.get('patronictl') or 'pod_ip' in config)
|
||||
if use_endpoints:
|
||||
addresses = [k8s_client.V1EndpointAddress(ip='127.0.0.1' if config.get('patronictl') else config['pod_ip'])]
|
||||
ports = []
|
||||
for p in config.get('ports', [{}]):
|
||||
port = {'port': int(p.get('port', '5432'))}
|
||||
port.update({n: p[n] for n in ('name', 'protocol') if p.get(n)})
|
||||
ports.append(k8s_client.V1EndpointPort(**port))
|
||||
self.__subsets = [k8s_client.V1EndpointSubset(addresses=addresses, ports=ports)]
|
||||
self._should_create_config_service = True
|
||||
self._api = CoreV1ApiProxy(use_endpoints)
|
||||
self.__my_pod = None
|
||||
self.__ips = [] if config.get('patronictl') else [config.get('pod_ip')]
|
||||
self.__ports = []
|
||||
for p in config.get('ports', [{}]):
|
||||
port = {'port': int(p.get('port', '5432'))}
|
||||
port.update({n: p[n] for n in ('name', 'protocol') if p.get(n)})
|
||||
self.__ports.append(k8s_client.V1EndpointPort(**port))
|
||||
|
||||
self._api = CoreV1ApiProxy(config.get('use_endpoints'), config.get('bypass_api_service'))
|
||||
self._should_create_config_service = self._api.use_endpoints
|
||||
self.reload_config(config)
|
||||
# leader_observed_record, leader_resource_version, and leader_observed_time are used only for leader race!
|
||||
self._leader_observed_record = {}
|
||||
self._leader_observed_time = None
|
||||
self._leader_resource_version = None
|
||||
self._leader_observed_subsets = []
|
||||
self._config_resource_version = None
|
||||
self.__do_not_watch = False
|
||||
|
||||
self._condition = Condition()
|
||||
@@ -240,17 +648,19 @@ class Kubernetes(AbstractDCS):
|
||||
|
||||
kinds_func = functools.partial(self._api.list_namespaced_kind, self._namespace,
|
||||
label_selector=self._label_selector)
|
||||
self._kinds = ObjectCache(self, kinds_func, self._retry, self._condition)
|
||||
self._kinds = ObjectCache(self, kinds_func, self._retry, self._condition, self._name)
|
||||
|
||||
def retry(self, *args, **kwargs):
|
||||
return self._retry.copy()(*args, **kwargs)
|
||||
retry = self._retry.copy()
|
||||
kwargs['_retry'] = retry
|
||||
return retry(*args, **kwargs)
|
||||
|
||||
def client_path(self, path):
|
||||
return super(Kubernetes, self).client_path(path)[1:].replace('/', '-')
|
||||
|
||||
@property
|
||||
def leader_path(self):
|
||||
return self._base_path[1:] if self.__subsets else super(Kubernetes, self).leader_path
|
||||
return self._base_path[1:] if self._api.use_endpoints else super(Kubernetes, self).leader_path
|
||||
|
||||
def set_ttl(self, ttl):
|
||||
ttl = int(ttl)
|
||||
@@ -275,8 +685,7 @@ class Kubernetes(AbstractDCS):
|
||||
member.data['pod_labels'] = pod.metadata.labels
|
||||
return member
|
||||
|
||||
def _wait_caches(self):
|
||||
stop_time = time.time() + self._retry.deadline
|
||||
def _wait_caches(self, stop_time):
|
||||
while not (self._pods.is_ready() and self._kinds.is_ready()):
|
||||
timeout = stop_time - time.time()
|
||||
if timeout <= 0:
|
||||
@@ -284,16 +693,17 @@ class Kubernetes(AbstractDCS):
|
||||
self._condition.wait(timeout)
|
||||
|
||||
def _load_cluster(self):
|
||||
stop_time = time.time() + self._retry.deadline
|
||||
self._api.refresh_api_servers_cache()
|
||||
try:
|
||||
with self._condition:
|
||||
self._wait_caches()
|
||||
self._wait_caches(stop_time)
|
||||
|
||||
members = [self.member(pod) for pod in self._pods.copy().values()]
|
||||
nodes = self._kinds.copy()
|
||||
|
||||
config = nodes.get(self.config_path)
|
||||
metadata = config and config.metadata
|
||||
self._config_resource_version = metadata.resource_version if metadata else None
|
||||
annotations = metadata and metadata.annotations or {}
|
||||
|
||||
# get initialize flag
|
||||
@@ -311,7 +721,6 @@ class Kubernetes(AbstractDCS):
|
||||
leader = nodes.get(self.leader_path)
|
||||
metadata = leader and leader.metadata
|
||||
self._leader_resource_version = metadata.resource_version if metadata else None
|
||||
self._leader_observed_subsets = leader.subsets if self.__subsets and leader and leader.subsets else []
|
||||
annotations = metadata and metadata.annotations or {}
|
||||
|
||||
# get last leader operation
|
||||
@@ -360,50 +769,71 @@ class Kubernetes(AbstractDCS):
|
||||
return p1.name == p2.name and p1.port == p2.port and (p1.protocol or 'TCP') == (p2.protocol or 'TCP')
|
||||
|
||||
@staticmethod
|
||||
def subsets_changed(last_observed_subsets, subsets):
|
||||
def subsets_changed(last_observed_subsets, ip, ports):
|
||||
"""
|
||||
>>> Kubernetes.subsets_changed([], [])
|
||||
False
|
||||
>>> Kubernetes.subsets_changed([], [k8s_client.V1EndpointSubset()])
|
||||
>>> Kubernetes.subsets_changed([], None, [])
|
||||
True
|
||||
>>> s1 = [k8s_client.V1EndpointSubset(addresses=[k8s_client.V1EndpointAddress(ip='1.2.3.4')])]
|
||||
>>> s2 = [k8s_client.V1EndpointSubset(addresses=[k8s_client.V1EndpointAddress(ip='1.2.3.5')])]
|
||||
>>> Kubernetes.subsets_changed(s1, s2)
|
||||
>>> ip = '1.2.3.4'
|
||||
>>> a = [k8s_client.V1EndpointAddress(ip=ip)]
|
||||
>>> s = [k8s_client.V1EndpointSubset(addresses=a)]
|
||||
>>> Kubernetes.subsets_changed(s, '1.2.3.5', [])
|
||||
True
|
||||
>>> a = [k8s_client.V1EndpointAddress(ip='1.2.3.4')]
|
||||
>>> s1 = [k8s_client.V1EndpointSubset(addresses=a, ports=[k8s_client.V1EndpointPort(protocol='TCP', port=1)])]
|
||||
>>> s2 = [k8s_client.V1EndpointSubset(addresses=a, ports=[k8s_client.V1EndpointPort(port=5432)])]
|
||||
>>> Kubernetes.subsets_changed(s1, s2)
|
||||
>>> s = [k8s_client.V1EndpointSubset(addresses=a, ports=[k8s_client.V1EndpointPort(protocol='TCP', port=1)])]
|
||||
>>> Kubernetes.subsets_changed(s, '1.2.3.4', [k8s_client.V1EndpointPort(port=5432)])
|
||||
True
|
||||
>>> p1 = k8s_client.V1EndpointPort(name='port1', port=1)
|
||||
>>> p2 = k8s_client.V1EndpointPort(name='port2', port=2)
|
||||
>>> p3 = k8s_client.V1EndpointPort(name='port3', port=3)
|
||||
>>> s1 = [k8s_client.V1EndpointSubset(addresses=a, ports=[p1, p2])]
|
||||
>>> s2 = [k8s_client.V1EndpointSubset(addresses=a, ports=[p2, p3])]
|
||||
>>> Kubernetes.subsets_changed(s1, s2)
|
||||
>>> s = [k8s_client.V1EndpointSubset(addresses=a, ports=[p1, p2])]
|
||||
>>> Kubernetes.subsets_changed(s, ip, [p2, p3])
|
||||
True
|
||||
>>> s2 = [k8s_client.V1EndpointSubset(addresses=a, ports=[p2, p1])]
|
||||
>>> Kubernetes.subsets_changed(s1, s2)
|
||||
>>> Kubernetes.subsets_changed(s, ip, [p2, p1])
|
||||
False
|
||||
"""
|
||||
if len(last_observed_subsets) != len(subsets):
|
||||
|
||||
if len(last_observed_subsets) != 1:
|
||||
return True
|
||||
if subsets == []:
|
||||
return False
|
||||
if len(last_observed_subsets[0].addresses or []) != 1 or \
|
||||
last_observed_subsets[0].addresses[0].ip != subsets[0].addresses[0].ip or \
|
||||
len(last_observed_subsets[0].ports) != len(subsets[0].ports):
|
||||
last_observed_subsets[0].addresses[0].ip != ip or \
|
||||
len(last_observed_subsets[0].ports) != len(ports):
|
||||
return True
|
||||
if len(subsets[0].ports) == 1:
|
||||
return not Kubernetes.compare_ports(last_observed_subsets[0].ports[0], subsets[0].ports[0])
|
||||
if len(ports) == 1:
|
||||
return not Kubernetes.compare_ports(last_observed_subsets[0].ports[0], ports[0])
|
||||
observed_ports = {p.name: p for p in last_observed_subsets[0].ports}
|
||||
for p in subsets[0].ports:
|
||||
for p in ports:
|
||||
if p.name not in observed_ports or not Kubernetes.compare_ports(p, observed_ports.pop(p.name)):
|
||||
return True
|
||||
return False
|
||||
|
||||
@catch_kubernetes_errors
|
||||
def patch_or_create(self, name, annotations, resource_version=None, patch=False, retry=True, subsets=None):
|
||||
def __target_ref(self, leader_ip, latest_subsets, pod):
|
||||
# we want to re-use existing target_ref if possible
|
||||
for subset in latest_subsets:
|
||||
for address in subset.addresses or []:
|
||||
if address.ip == leader_ip and address.target_ref and address.target_ref.name == self._name:
|
||||
return address.target_ref
|
||||
return k8s_client.V1ObjectReference(kind='Pod', uid=pod.metadata.uid, namespace=self._namespace,
|
||||
name=self._name, resource_version=pod.metadata.resource_version)
|
||||
|
||||
def _map_subsets(self, endpoints, ips):
|
||||
leader = self._kinds.get(self.leader_path)
|
||||
latest_subsets = leader and leader.subsets or []
|
||||
if not ips:
|
||||
# We want to have subsets empty
|
||||
if latest_subsets:
|
||||
endpoints['subsets'] = []
|
||||
return
|
||||
|
||||
pod = self._pods.get(self._name)
|
||||
leader_ip = ips[0] or pod and pod.status.pod_ip
|
||||
# don't touch subsets if our (leader) ip is unknown or subsets is valid
|
||||
if leader_ip and self.subsets_changed(latest_subsets, leader_ip, self.__ports):
|
||||
kwargs = {'hostname': pod.spec.hostname, 'node_name': pod.spec.node_name,
|
||||
'target_ref': self.__target_ref(leader_ip, latest_subsets, pod)} if pod else {}
|
||||
address = k8s_client.V1EndpointAddress(ip=leader_ip, **kwargs)
|
||||
endpoints['subsets'] = [k8s_client.V1EndpointSubset(addresses=[address], ports=self.__ports)]
|
||||
|
||||
def _patch_or_create(self, name, annotations, resource_version=None, patch=False, retry=None, ips=None):
|
||||
metadata = {'namespace': self._namespace, 'name': name, 'labels': self._labels, 'annotations': annotations}
|
||||
if patch or resource_version:
|
||||
if resource_version is not None:
|
||||
@@ -415,27 +845,29 @@ class Kubernetes(AbstractDCS):
|
||||
metadata['annotations'] = {k: v for k, v in metadata['annotations'].items() if v is not None}
|
||||
|
||||
metadata = k8s_client.V1ObjectMeta(**metadata)
|
||||
if subsets is not None and self.__subsets:
|
||||
if ips is not None and self._api.use_endpoints:
|
||||
endpoints = {'metadata': metadata}
|
||||
if self.subsets_changed(self._leader_observed_subsets, subsets):
|
||||
endpoints['subsets'] = subsets
|
||||
self._map_subsets(endpoints, ips)
|
||||
body = k8s_client.V1Endpoints(**endpoints)
|
||||
else:
|
||||
body = k8s_client.V1ConfigMap(metadata=metadata)
|
||||
ret = self.retry(func, self._namespace, body) if retry else func(self._namespace, body)
|
||||
ret = retry(func, self._namespace, body) if retry else func(self._namespace, body)
|
||||
if ret:
|
||||
self._kinds.set(name, ret)
|
||||
return ret
|
||||
|
||||
@catch_kubernetes_errors
|
||||
def patch_or_create(self, name, annotations, resource_version=None, patch=False, retry=True, ips=None):
|
||||
if retry is True:
|
||||
retry = self.retry
|
||||
return self._patch_or_create(name, annotations, resource_version, patch, retry, ips)
|
||||
|
||||
def patch_or_create_config(self, annotations, resource_version=None, patch=False, retry=True):
|
||||
# SCOPE-config endpoint requires corresponding service otherwise it might be "cleaned" by k8s master
|
||||
if self.__subsets and not patch and not resource_version:
|
||||
if self._api.use_endpoints and not patch and not resource_version:
|
||||
self._should_create_config_service = True
|
||||
self._create_config_service()
|
||||
ret = self.patch_or_create(self.config_path, annotations, resource_version, patch, retry)
|
||||
if ret:
|
||||
self._config_resource_version = ret.metadata.resource_version
|
||||
return ret
|
||||
return self.patch_or_create(self.config_path, annotations, resource_version, patch, retry)
|
||||
|
||||
def _create_config_service(self):
|
||||
metadata = k8s_client.V1ObjectMeta(namespace=self._namespace, name=self.config_path, labels=self._labels)
|
||||
@@ -454,20 +886,59 @@ class Kubernetes(AbstractDCS):
|
||||
def _update_leader(self):
|
||||
"""Unused"""
|
||||
|
||||
def _update_leader_with_retry(self, annotations, resource_version, ips):
|
||||
retry = self._retry.copy()
|
||||
|
||||
def _retry(*args, **kwargs):
|
||||
kwargs['_retry'] = retry
|
||||
return retry(*args, **kwargs)
|
||||
|
||||
try:
|
||||
return self._patch_or_create(self.leader_path, annotations, resource_version, ips=ips, retry=_retry)
|
||||
except k8s_client.rest.ApiException as e:
|
||||
if e.status == 409:
|
||||
logger.warning('Concurrent update of %s', self.leader_path)
|
||||
else:
|
||||
logger.exception('Permission denied' if e.status == 403 else 'Unexpected error from Kubernetes API')
|
||||
return False
|
||||
except (RetryFailedError, K8sException):
|
||||
return False
|
||||
|
||||
deadline = retry.stoptime - time.time()
|
||||
if deadline < 2:
|
||||
return False
|
||||
|
||||
retry.sleep_func(1) # Give a chance for ObjectCache to receive the latest version
|
||||
|
||||
kind = self._kinds.get(self.leader_path)
|
||||
kind_annotations = kind and kind.metadata.annotations or {}
|
||||
kind_resource_version = kind and kind.metadata.resource_version
|
||||
|
||||
# There is different leader or resource_version in cache didn't change
|
||||
if kind and (kind_annotations.get(self._LEADER) != self._name or kind_resource_version == resource_version):
|
||||
return False
|
||||
|
||||
retry.deadline = deadline - 1 # Update deadline and retry
|
||||
return self.patch_or_create(self.leader_path, annotations, kind_resource_version, ips=ips, retry=_retry)
|
||||
|
||||
def update_leader(self, last_operation, access_is_restricted=False):
|
||||
kind = self._kinds.get(self.leader_path)
|
||||
kind_annotations = kind and kind.metadata.annotations or {}
|
||||
|
||||
if kind and kind_annotations.get(self._LEADER) != self._name:
|
||||
return False
|
||||
|
||||
now = datetime.datetime.now(tzutc).isoformat()
|
||||
leader_observed_record = kind_annotations or self._leader_observed_record
|
||||
annotations = {self._LEADER: self._name, 'ttl': str(self._ttl), 'renewTime': now,
|
||||
'acquireTime': self._leader_observed_record.get('acquireTime') or now,
|
||||
'transitions': self._leader_observed_record.get('transitions') or '0'}
|
||||
'acquireTime': leader_observed_record.get('acquireTime') or now,
|
||||
'transitions': leader_observed_record.get('transitions') or '0'}
|
||||
if last_operation:
|
||||
annotations[self._OPTIME] = last_operation
|
||||
|
||||
subsets = [] if access_is_restricted else self.__subsets
|
||||
|
||||
ret = self.patch_or_create(self.leader_path, annotations, self._leader_resource_version, subsets=subsets)
|
||||
if ret:
|
||||
self._leader_resource_version = ret.metadata.resource_version
|
||||
return ret
|
||||
resource_version = kind and kind.metadata.resource_version
|
||||
ips = [] if access_is_restricted else self.__ips
|
||||
return self._update_leader_with_retry(annotations, resource_version, ips)
|
||||
|
||||
def attempt_to_acquire_leader(self, permanent=False):
|
||||
now = datetime.datetime.now(tzutc).isoformat()
|
||||
@@ -484,11 +955,9 @@ class Kubernetes(AbstractDCS):
|
||||
else:
|
||||
annotations['acquireTime'] = self._leader_observed_record.get('acquireTime') or now
|
||||
annotations['transitions'] = str(transitions)
|
||||
subsets = [] if self.__subsets else None
|
||||
ret = self.patch_or_create(self.leader_path, annotations, self._leader_resource_version, subsets=subsets)
|
||||
if ret:
|
||||
self._leader_resource_version = ret.metadata.resource_version
|
||||
else:
|
||||
ips = [] if self._api.use_endpoints else None
|
||||
ret = self.patch_or_create(self.leader_path, annotations, self._leader_resource_version, ips=ips)
|
||||
if not ret:
|
||||
logger.info('Could not take out TTL lock')
|
||||
return ret
|
||||
|
||||
@@ -504,6 +973,11 @@ class Kubernetes(AbstractDCS):
|
||||
patch = bool(self.cluster and isinstance(self.cluster.failover, Failover) and self.cluster.failover.index)
|
||||
return self.patch_or_create(self.failover_path, annotations, index, bool(index or patch), False)
|
||||
|
||||
@property
|
||||
def _config_resource_version(self):
|
||||
config = self._kinds.get(self.config_path)
|
||||
return config and config.metadata.resource_version
|
||||
|
||||
def set_config_value(self, value, index=None):
|
||||
return self.patch_or_create_config({self._CONFIG: value}, index, bool(self._config_resource_version), False)
|
||||
|
||||
@@ -526,7 +1000,9 @@ class Kubernetes(AbstractDCS):
|
||||
'annotations': {'status': json.dumps(data, separators=(',', ':'))}}
|
||||
body = k8s_client.V1Pod(metadata=k8s_client.V1ObjectMeta(**metadata))
|
||||
ret = self._api.patch_namespaced_pod(self._name, self._namespace, body)
|
||||
if self.__subsets and self._should_create_config_service:
|
||||
if ret:
|
||||
self._pods.set(self._name, ret)
|
||||
if self._should_create_config_service:
|
||||
self._create_config_service()
|
||||
return ret
|
||||
|
||||
@@ -535,9 +1011,16 @@ class Kubernetes(AbstractDCS):
|
||||
resource_version = cluster.config.index if cluster and cluster.config and cluster.config.index else None
|
||||
return self.patch_or_create_config({self._INITIALIZE: sysid}, resource_version)
|
||||
|
||||
def delete_leader(self):
|
||||
if self.cluster and isinstance(self.cluster.leader, Leader) and self.cluster.leader.name == self._name:
|
||||
self.patch_or_create(self.leader_path, {self._LEADER: None}, self._leader_resource_version, True, False, [])
|
||||
def _delete_leader(self):
|
||||
"""Unused"""
|
||||
|
||||
def delete_leader(self, last_operation=None):
|
||||
kind = self._kinds.get(self.leader_path)
|
||||
if kind and (kind.metadata.annotations or {}).get(self._LEADER) == self._name:
|
||||
annotations = {self._LEADER: None}
|
||||
if last_operation:
|
||||
annotations[self._OPTIME] = last_operation
|
||||
self.patch_or_create(self.leader_path, annotations, kind.metadata.resource_version, True, False, [])
|
||||
self.reset_cluster()
|
||||
|
||||
def cancel_initialization(self):
|
||||
@@ -565,6 +1048,6 @@ class Kubernetes(AbstractDCS):
|
||||
return True
|
||||
|
||||
try:
|
||||
return super(Kubernetes, self).watch(None, timeout)
|
||||
return super(Kubernetes, self).watch(None, timeout + 0.5)
|
||||
finally:
|
||||
self.event.clear()
|
||||
|
||||
@@ -0,0 +1,416 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
|
||||
from patroni.dcs import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
||||
from pysyncobj import SyncObj, SyncObjConf, replicated, FAIL_REASON
|
||||
from pysyncobj.transport import Node, TCPTransport, CONNECTION_STATE
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MessageNode(Node):
|
||||
|
||||
def __init__(self, address):
|
||||
self.address = address
|
||||
|
||||
|
||||
class UtilityTransport(TCPTransport):
|
||||
|
||||
def __init__(self, syncObj, selfNode, otherNodes):
|
||||
super(UtilityTransport, self).__init__(syncObj, selfNode, otherNodes)
|
||||
self._selfIsReadonlyNode = False
|
||||
|
||||
def _connectIfNecessarySingle(self, node):
|
||||
pass
|
||||
|
||||
def connectionState(self, node):
|
||||
return self._connections[node].state
|
||||
|
||||
def isDisconnected(self, node):
|
||||
return self.connectionState(node) == CONNECTION_STATE.DISCONNECTED
|
||||
|
||||
def connectIfRequiredSingle(self, node):
|
||||
if self.isDisconnected(node):
|
||||
return self._connections[node].connect(node.ip, node.port)
|
||||
|
||||
def disconnectSingle(self, node):
|
||||
self._connections[node].disconnect()
|
||||
|
||||
|
||||
class SyncObjUtility(SyncObj):
|
||||
|
||||
def __init__(self, otherNodes, conf):
|
||||
autoTick = conf.autoTick
|
||||
conf.autoTick = False
|
||||
super(SyncObjUtility, self).__init__(None, otherNodes, conf, transportClass=UtilityTransport)
|
||||
conf.autoTick = autoTick
|
||||
self._SyncObj__transport.setOnMessageReceivedCallback(self._onMessageReceived)
|
||||
self.__result = None
|
||||
|
||||
def setPartnerNode(self, partner):
|
||||
self.__node = partner
|
||||
|
||||
def sendMessage(self, message):
|
||||
# Abuse the fact that node address is send as a first message
|
||||
self._SyncObj__transport._selfNode = MessageNode(message)
|
||||
self._SyncObj__transport.connectIfRequiredSingle(self.__node)
|
||||
while not self._SyncObj__transport.isDisconnected(self.__node):
|
||||
self._poller.poll(0.5)
|
||||
return self.__result
|
||||
|
||||
def _onMessageReceived(self, _, message):
|
||||
self.__result = message
|
||||
self._SyncObj__transport.disconnectSingle(self.__node)
|
||||
|
||||
|
||||
class MyTCPTransport(TCPTransport):
|
||||
|
||||
def _onIncomingMessageReceived(self, conn, message):
|
||||
if self._syncObj.encryptor and not conn.sendRandKey:
|
||||
conn.sendRandKey = message
|
||||
conn.recvRandKey = os.urandom(32)
|
||||
conn.send(conn.recvRandKey)
|
||||
return
|
||||
|
||||
# Utility messages
|
||||
if isinstance(message, list) and message[0] == 'members':
|
||||
conn.send(self._syncObj._get_members())
|
||||
return True
|
||||
|
||||
return super(MyTCPTransport, self)._onIncomingMessageReceived(conn, message)
|
||||
|
||||
|
||||
class DynMemberSyncObj(SyncObj):
|
||||
|
||||
def __init__(self, selfAddress, partnerAddrs, conf):
|
||||
add_self = False
|
||||
utility = SyncObjUtility(partnerAddrs, conf)
|
||||
for node in utility._SyncObj__otherNodes:
|
||||
utility.setPartnerNode(node)
|
||||
response = utility.sendMessage(['members'])
|
||||
if response:
|
||||
partnerAddrs = [member['addr'] for member in response if member['addr'] != selfAddress]
|
||||
add_self = selfAddress and len(partnerAddrs) == len(response)
|
||||
break
|
||||
|
||||
super(DynMemberSyncObj, self).__init__(selfAddress, partnerAddrs, conf, transportClass=MyTCPTransport)
|
||||
if add_self:
|
||||
threading.Thread(target=utility.sendMessage, args=(['add', selfAddress],)).start()
|
||||
|
||||
def _get_members(self):
|
||||
ret = [{'addr': node.id, 'leader': node == self._getLeader(),
|
||||
'status': CONNECTION_STATE.CONNECTED if node in self._SyncObj__connectedNodes
|
||||
else CONNECTION_STATE.DISCONNECTED} for node in self._SyncObj__otherNodes]
|
||||
ret.append({'addr': self._SyncObj__selfNode.id, 'leader': self._isLeader(),
|
||||
'status': CONNECTION_STATE.CONNECTED})
|
||||
return ret
|
||||
|
||||
def _SyncObj__doChangeCluster(self, request, reverse=False):
|
||||
ret = False
|
||||
if not self._SyncObj__selfNode or request[0] != 'add' or reverse or request[1] != self._SyncObj__selfNode.id:
|
||||
ret = super(DynMemberSyncObj, self)._SyncObj__doChangeCluster(request, reverse)
|
||||
if ret:
|
||||
self.forceLogCompaction()
|
||||
return ret
|
||||
|
||||
|
||||
class KVStoreTTL(DynMemberSyncObj):
|
||||
|
||||
def __init__(self, selfAddress, partnerAddrs, conf, on_set=None, on_delete=None):
|
||||
self.__on_set = on_set
|
||||
self.__on_delete = on_delete
|
||||
self.__limb = {}
|
||||
self.__retry_timeout = None
|
||||
self.__early_apply_local_log = selfAddress is not None
|
||||
self.applied_local_log = False
|
||||
super(KVStoreTTL, self).__init__(selfAddress, partnerAddrs, conf)
|
||||
self.__data = {}
|
||||
|
||||
@staticmethod
|
||||
def __check_requirements(old_value, **kwargs):
|
||||
return ('prevExist' not in kwargs or bool(kwargs['prevExist']) == bool(old_value)) and \
|
||||
('prevValue' not in kwargs or old_value and old_value['value'] == kwargs['prevValue']) and \
|
||||
(not kwargs.get('prevIndex') or old_value and old_value['index'] == kwargs['prevIndex'])
|
||||
|
||||
def set_retry_timeout(self, retry_timeout):
|
||||
self.__retry_timeout = retry_timeout
|
||||
|
||||
def retry(self, func, *args, **kwargs):
|
||||
event = threading.Event()
|
||||
ret = {'result': None, 'error': -1}
|
||||
|
||||
def callback(result, error):
|
||||
ret.update(result=result, error=error)
|
||||
event.set()
|
||||
|
||||
kwargs['callback'] = callback
|
||||
timeout = kwargs.pop('timeout', None) or self.__retry_timeout
|
||||
deadline = timeout and time.time() + timeout
|
||||
|
||||
while True:
|
||||
event.clear()
|
||||
func(*args, **kwargs)
|
||||
event.wait(timeout)
|
||||
if ret['error'] == FAIL_REASON.SUCCESS:
|
||||
return ret['result']
|
||||
elif ret['error'] == FAIL_REASON.REQUEST_DENIED:
|
||||
break
|
||||
elif deadline:
|
||||
timeout = deadline - time.time()
|
||||
if timeout <= 0:
|
||||
break
|
||||
time.sleep(1)
|
||||
return False
|
||||
|
||||
@replicated
|
||||
def _set(self, key, value, **kwargs):
|
||||
old_value = self.__data.get(key, {})
|
||||
if not self.__check_requirements(old_value, **kwargs):
|
||||
return False
|
||||
|
||||
if old_value and old_value['created'] != value['created']:
|
||||
value['created'] = value['updated']
|
||||
value['index'] = self._SyncObj__raftLastApplied + 1
|
||||
|
||||
self.__data[key] = value
|
||||
if self.__on_set:
|
||||
self.__on_set(key, value)
|
||||
return True
|
||||
|
||||
def set(self, key, value, ttl=None, **kwargs):
|
||||
old_value = self.__data.get(key, {})
|
||||
if not self.__check_requirements(old_value, **kwargs):
|
||||
return False
|
||||
|
||||
value = {'value': value, 'updated': time.time()}
|
||||
value['created'] = old_value.get('created', value['updated'])
|
||||
if ttl:
|
||||
value['expire'] = value['updated'] + ttl
|
||||
return self.retry(self._set, key, value, **kwargs)
|
||||
|
||||
def __pop(self, key):
|
||||
self.__data.pop(key)
|
||||
if self.__on_delete:
|
||||
self.__on_delete(key)
|
||||
|
||||
@replicated
|
||||
def _delete(self, key, recursive=False, **kwargs):
|
||||
if recursive:
|
||||
for k in list(self.__data.keys()):
|
||||
if k.startswith(key):
|
||||
self.__pop(k)
|
||||
elif not self.__check_requirements(self.__data.get(key, {}), **kwargs):
|
||||
return False
|
||||
else:
|
||||
self.__pop(key)
|
||||
return True
|
||||
|
||||
def delete(self, key, recursive=False, **kwargs):
|
||||
if not recursive and not self.__check_requirements(self.__data.get(key, {}), **kwargs):
|
||||
return False
|
||||
return self.retry(self._delete, key, recursive=recursive, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def __values_match(old, new):
|
||||
return all(old.get(n) == new.get(n) for n in ('created', 'updated', 'expire', 'value'))
|
||||
|
||||
@replicated
|
||||
def _expire(self, key, value, callback=None):
|
||||
current = self.__data.get(key)
|
||||
if current and self.__values_match(current, value):
|
||||
self.__pop(key)
|
||||
|
||||
def __expire_keys(self):
|
||||
for key, value in self.__data.items():
|
||||
if value and 'expire' in value and value['expire'] <= time.time() and \
|
||||
not (key in self.__limb and self.__values_match(self.__limb[key], value)):
|
||||
self.__limb[key] = value
|
||||
|
||||
def callback(*args):
|
||||
if key in self.__limb and self.__values_match(self.__limb[key], value):
|
||||
self.__limb.pop(key)
|
||||
self._expire(key, value, callback=callback)
|
||||
|
||||
def get(self, key, recursive=False):
|
||||
if not recursive:
|
||||
return self.__data.get(key)
|
||||
return {k: v for k, v in self.__data.items() if k.startswith(key)}
|
||||
|
||||
def _onTick(self, timeToWait=0.0):
|
||||
# The SyncObj starts applying the local log only when there is at least one node connected.
|
||||
# We want to change this behavior and apply the local log even when there is nobody except us.
|
||||
# It gives us at least some picture about the last known cluster state.
|
||||
if self.__early_apply_local_log and not self.applied_local_log and self._SyncObj__needLoadDumpFile:
|
||||
self._SyncObj__raftCommitIndex = self._SyncObj__getCurrentLogIndex()
|
||||
self._SyncObj__raftCurrentTerm = self._SyncObj__getCurrentLogTerm()
|
||||
|
||||
super(KVStoreTTL, self)._onTick(timeToWait)
|
||||
|
||||
# The SyncObj calls onReady callback only when cluster got the leader and is ready for writes.
|
||||
# In some cases for us it is safe to "signal" the Raft object when the local log is fully applied.
|
||||
# We are using the `applied_local_log` property for that, but not calling the callback function.
|
||||
if self.__early_apply_local_log and not self.applied_local_log and self._SyncObj__raftCommitIndex != 1 and \
|
||||
self._SyncObj__raftLastApplied == self._SyncObj__raftCommitIndex:
|
||||
self.applied_local_log = True
|
||||
|
||||
if self._isLeader():
|
||||
self.__expire_keys()
|
||||
else:
|
||||
self.__limb.clear()
|
||||
|
||||
|
||||
class Raft(AbstractDCS):
|
||||
|
||||
def __init__(self, config):
|
||||
super(Raft, self).__init__(config)
|
||||
self._ttl = int(config.get('ttl') or 30)
|
||||
|
||||
self_addr = config.get('self_addr')
|
||||
partner_addrs = config.get('partner_addrs', [])
|
||||
if self._ctl:
|
||||
if self_addr:
|
||||
partner_addrs.append(self_addr)
|
||||
self_addr = None
|
||||
|
||||
ready_event = threading.Event()
|
||||
file_template = os.path.join(config.get('data_dir', ''), (self_addr or ''))
|
||||
conf = SyncObjConf(password=config.get('password'), appendEntriesUseBatch=False,
|
||||
bindAddress=config.get('bind_addr'), commandsWaitLeader=False,
|
||||
fullDumpFile=(file_template + '.dump' if self_addr else None),
|
||||
journalFile=(file_template + '.journal' if self_addr else None),
|
||||
onReady=ready_event.set, dynamicMembershipChange=True)
|
||||
|
||||
self._sync_obj = KVStoreTTL(self_addr, partner_addrs, conf, self._on_set, self._on_delete)
|
||||
while True:
|
||||
ready_event.wait(5)
|
||||
if ready_event.isSet() or self._sync_obj.applied_local_log:
|
||||
break
|
||||
else:
|
||||
logger.info('waiting on raft')
|
||||
self._sync_obj.forceLogCompaction()
|
||||
self.set_retry_timeout(int(config.get('retry_timeout') or 10))
|
||||
|
||||
def _on_set(self, key, value):
|
||||
leader = (self._sync_obj.get(self.leader_path) or {}).get('value')
|
||||
if key == value['created'] == value['updated'] and \
|
||||
(key.startswith(self.members_path) or key == self.leader_path and leader != self._name) or \
|
||||
key == self.leader_optime_path and leader != self._name or key in (self.config_path, self.sync_path):
|
||||
self.event.set()
|
||||
|
||||
def _on_delete(self, key):
|
||||
if key == self.leader_path:
|
||||
self.event.set()
|
||||
|
||||
def set_ttl(self, ttl):
|
||||
self._ttl = ttl
|
||||
|
||||
@property
|
||||
def ttl(self):
|
||||
return self._ttl
|
||||
|
||||
def set_retry_timeout(self, retry_timeout):
|
||||
self._sync_obj.set_retry_timeout(retry_timeout)
|
||||
|
||||
@staticmethod
|
||||
def member(key, value):
|
||||
return Member.from_node(value['index'], os.path.basename(key), None, value['value'])
|
||||
|
||||
def _load_cluster(self):
|
||||
prefix = self.client_path('')
|
||||
response = self._sync_obj.get(prefix, recursive=True)
|
||||
if not response:
|
||||
return Cluster(None, None, None, None, [], None, None, None)
|
||||
nodes = {os.path.relpath(key, prefix).replace('\\', '/'): value for key, value in response.items()}
|
||||
|
||||
# get initialize flag
|
||||
initialize = nodes.get(self._INITIALIZE)
|
||||
initialize = initialize and initialize['value']
|
||||
|
||||
# get global dynamic configuration
|
||||
config = nodes.get(self._CONFIG)
|
||||
config = config and ClusterConfig.from_node(config['index'], config['value'])
|
||||
|
||||
# get timeline history
|
||||
history = nodes.get(self._HISTORY)
|
||||
history = history and TimelineHistory.from_node(history['index'], history['value'])
|
||||
|
||||
# get last leader operation
|
||||
last_leader_operation = nodes.get(self._LEADER_OPTIME)
|
||||
last_leader_operation = 0 if last_leader_operation is None else int(last_leader_operation['value'])
|
||||
|
||||
# get list of members
|
||||
members = [self.member(k, n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
||||
|
||||
# get leader
|
||||
leader = nodes.get(self._LEADER)
|
||||
if leader:
|
||||
member = Member(-1, leader['value'], None, {})
|
||||
member = ([m for m in members if m.name == leader['value']] or [member])[0]
|
||||
leader = Leader(leader['index'], None, member)
|
||||
|
||||
# failover key
|
||||
failover = nodes.get(self._FAILOVER)
|
||||
if failover:
|
||||
failover = Failover.from_node(failover['index'], failover['value'])
|
||||
|
||||
# get synchronization state
|
||||
sync = nodes.get(self._SYNC)
|
||||
sync = SyncState.from_node(sync and sync['index'], sync and sync['value'])
|
||||
|
||||
return Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
||||
|
||||
def _write_leader_optime(self, last_operation):
|
||||
return self._sync_obj.set(self.leader_optime_path, last_operation, timeout=1)
|
||||
|
||||
def _update_leader(self):
|
||||
ret = self._sync_obj.set(self.leader_path, self._name, ttl=self._ttl, prevValue=self._name)
|
||||
if not ret and self._sync_obj.get(self.leader_path) is None:
|
||||
ret = self.attempt_to_acquire_leader()
|
||||
return ret
|
||||
|
||||
def attempt_to_acquire_leader(self, permanent=False):
|
||||
return self._sync_obj.set(self.leader_path, self._name, prevExist=False,
|
||||
ttl=None if permanent else self._ttl)
|
||||
|
||||
def set_failover_value(self, value, index=None):
|
||||
return self._sync_obj.set(self.failover_path, value, prevIndex=index)
|
||||
|
||||
def set_config_value(self, value, index=None):
|
||||
return self._sync_obj.set(self.config_path, value, prevIndex=index)
|
||||
|
||||
def touch_member(self, data, permanent=False):
|
||||
data = json.dumps(data, separators=(',', ':'))
|
||||
return self._sync_obj.set(self.member_path, data, None if permanent else self._ttl, timeout=2)
|
||||
|
||||
def take_leader(self):
|
||||
return self._sync_obj.set(self.leader_path, self._name, ttl=self._ttl)
|
||||
|
||||
def initialize(self, create_new=True, sysid=''):
|
||||
return self._sync_obj.set(self.initialize_path, sysid, prevExist=(not create_new))
|
||||
|
||||
def _delete_leader(self):
|
||||
return self._sync_obj.delete(self.leader_path, prevValue=self._name, timeout=1)
|
||||
|
||||
def cancel_initialization(self):
|
||||
return self._sync_obj.delete(self.initialize_path)
|
||||
|
||||
def delete_cluster(self):
|
||||
return self._sync_obj.delete(self.client_path(''), recursive=True)
|
||||
|
||||
def set_history_value(self, value):
|
||||
return self._sync_obj.set(self.history_path, value)
|
||||
|
||||
def set_sync_state_value(self, value, index=None):
|
||||
return self._sync_obj.set(self.sync_path, value, prevIndex=index)
|
||||
|
||||
def delete_sync_state(self, index=None):
|
||||
return self._sync_obj.delete(self.sync_path, prevIndex=index)
|
||||
|
||||
def watch(self, leader_index, timeout):
|
||||
try:
|
||||
return super(Raft, self).watch(leader_index, timeout)
|
||||
finally:
|
||||
self.event.clear()
|
||||
+38
-12
@@ -63,20 +63,25 @@ class ZooKeeper(AbstractDCS):
|
||||
if isinstance(hosts, list):
|
||||
hosts = ','.join(hosts)
|
||||
|
||||
mapping = {'use_ssl': 'use_ssl', 'verify': 'verify_certs', 'cacert': 'ca',
|
||||
'cert': 'certfile', 'key': 'keyfile', 'key_password': 'keyfile_password'}
|
||||
kwargs = {v: config[k] for k, v in mapping.items() if k in config}
|
||||
|
||||
self._client = KazooClient(hosts, handler=PatroniSequentialThreadingHandler(config['retry_timeout']),
|
||||
timeout=config['ttl'], connection_retry=KazooRetry(max_delay=1, max_tries=-1,
|
||||
sleep_func=time.sleep), command_retry=KazooRetry(deadline=config['retry_timeout'],
|
||||
max_delay=1, max_tries=-1, sleep_func=time.sleep))
|
||||
max_delay=1, max_tries=-1, sleep_func=time.sleep), **kwargs)
|
||||
self._client.add_listener(self.session_listener)
|
||||
|
||||
self._fetch_cluster = True
|
||||
self._fetch_optime = True
|
||||
|
||||
self._orig_kazoo_connect = self._client._connection._connect
|
||||
self._client._connection._connect = self._kazoo_connect
|
||||
|
||||
self._client.start()
|
||||
|
||||
def _kazoo_connect(self, host, port):
|
||||
def _kazoo_connect(self, *args):
|
||||
"""Kazoo is using Ping's to determine health of connection to zookeeper. If there is no
|
||||
response on Ping after Ping interval (1/2 from read_timeout) it will consider current
|
||||
connection dead and try to connect to another node. Without this "magic" it was taking
|
||||
@@ -88,16 +93,20 @@ class ZooKeeper(AbstractDCS):
|
||||
than loop_wait, because we can spend up to 2 seconds when calling `touch_member()` and
|
||||
`write_leader_optime()` methods, which also may hang..."""
|
||||
|
||||
ret = self._orig_kazoo_connect(host, port)
|
||||
ret = self._orig_kazoo_connect(*args)
|
||||
return max(self.loop_wait - 2, 2)*1000, ret[1]
|
||||
|
||||
def session_listener(self, state):
|
||||
if state in [KazooState.SUSPENDED, KazooState.LOST]:
|
||||
self.cluster_watcher(None)
|
||||
|
||||
def optime_watcher(self, event):
|
||||
self._fetch_optime = True
|
||||
self.event.set()
|
||||
|
||||
def cluster_watcher(self, event):
|
||||
self._fetch_cluster = True
|
||||
self.event.set()
|
||||
self.optime_watcher(event)
|
||||
|
||||
def reload_config(self, config):
|
||||
self.set_retry_timeout(config['retry_timeout'])
|
||||
@@ -141,6 +150,12 @@ class ZooKeeper(AbstractDCS):
|
||||
except NoNodeError:
|
||||
return None
|
||||
|
||||
def get_leader_optime(self, leader):
|
||||
watch = self.optime_watcher if not leader or leader.name != self._name else None
|
||||
optime = self.get_node(self.leader_optime_path, watch)
|
||||
self._fetch_optime = False
|
||||
return optime and int(optime[0]) or 0
|
||||
|
||||
@staticmethod
|
||||
def member(name, value, znode):
|
||||
return Member.from_node(znode.version, name, znode.ephemeralOwner, value)
|
||||
@@ -154,7 +169,7 @@ class ZooKeeper(AbstractDCS):
|
||||
def load_members(self, sync_standby):
|
||||
members = []
|
||||
for member in self.get_children(self.members_path, self.cluster_watcher):
|
||||
watch = member == sync_standby and self.cluster_watcher or None
|
||||
watch = member in sync_standby and self.cluster_watcher or None
|
||||
data = self.get_node(self.members_path + member, watch)
|
||||
if data is not None:
|
||||
members.append(self.member(member, *data))
|
||||
@@ -178,16 +193,12 @@ class ZooKeeper(AbstractDCS):
|
||||
history = self.get_node(self.history_path, watch=self.cluster_watcher) if self._HISTORY in nodes else None
|
||||
history = history and TimelineHistory.from_node(history[1].mzxid, history[0])
|
||||
|
||||
# get last leader operation
|
||||
last_leader_operation = self._OPTIME in nodes and self._fetch_cluster and self.get_node(self.leader_optime_path)
|
||||
last_leader_operation = last_leader_operation and int(last_leader_operation[0]) or 0
|
||||
|
||||
# get synchronization state
|
||||
sync = self.get_node(self.sync_path, watch=self.cluster_watcher) if self._SYNC in nodes else None
|
||||
sync = SyncState.from_node(sync and sync[1].version, sync and sync[0])
|
||||
|
||||
# get list of members
|
||||
sync_standby = sync.leader == self._name and sync.sync_standby or None
|
||||
sync_standby = sync.leader == self._name and sync.members or []
|
||||
members = self.load_members(sync_standby) if self._MEMBERS[:-1] in nodes else []
|
||||
|
||||
# get leader
|
||||
@@ -206,6 +217,9 @@ class ZooKeeper(AbstractDCS):
|
||||
leader = Leader(leader[1].version, leader[1].ephemeralOwner, member)
|
||||
self._fetch_cluster = member.index == -1
|
||||
|
||||
# get last leader operation
|
||||
last_leader_operation = self._OPTIME in nodes and self.get_leader_optime(leader)
|
||||
|
||||
# failover key
|
||||
failover = self.get_node(self.failover_path, watch=self.cluster_watcher) if self._FAILOVER in nodes else None
|
||||
failover = failover and Failover.from_node(failover[1].version, failover[0])
|
||||
@@ -221,8 +235,20 @@ class ZooKeeper(AbstractDCS):
|
||||
logger.exception('get_cluster')
|
||||
self.cluster_watcher(None)
|
||||
raise ZooKeeperError('ZooKeeper in not responding properly')
|
||||
# Optime ZNode was updated or doesn't exist and we are not leader
|
||||
elif (self._fetch_optime and not self._fetch_cluster or not cluster.last_leader_operation) and\
|
||||
not (cluster.leader and cluster.leader.name == self._name):
|
||||
try:
|
||||
optime = self.get_leader_optime(cluster.leader)
|
||||
cluster = Cluster(cluster.initialize, cluster.config, cluster.leader, optime,
|
||||
cluster.members, cluster.failover, cluster.sync, cluster.history)
|
||||
except Exception:
|
||||
pass
|
||||
return cluster
|
||||
|
||||
def _bypass_caches(self):
|
||||
self._fetch_cluster = True
|
||||
|
||||
def _create(self, path, value, retry=False, ephemeral=False):
|
||||
try:
|
||||
if retry:
|
||||
@@ -315,7 +341,7 @@ class ZooKeeper(AbstractDCS):
|
||||
def _update_leader(self):
|
||||
return True
|
||||
|
||||
def delete_leader(self):
|
||||
def _delete_leader(self):
|
||||
self._client.restart()
|
||||
return True
|
||||
|
||||
@@ -346,6 +372,6 @@ class ZooKeeper(AbstractDCS):
|
||||
return self.set_sync_state_value("{}", index)
|
||||
|
||||
def watch(self, leader_index, timeout):
|
||||
if super(ZooKeeper, self).watch(leader_index, timeout):
|
||||
if super(ZooKeeper, self).watch(leader_index, timeout) and not self._fetch_optime:
|
||||
self._fetch_cluster = True
|
||||
return self._fetch_cluster
|
||||
|
||||
@@ -13,6 +13,10 @@ class PatroniException(Exception):
|
||||
return repr(self.value)
|
||||
|
||||
|
||||
class PatroniFatalException(PatroniException):
|
||||
pass
|
||||
|
||||
|
||||
class PostgresException(PatroniException):
|
||||
pass
|
||||
|
||||
|
||||
+173
-73
@@ -10,11 +10,11 @@ import uuid
|
||||
from collections import namedtuple
|
||||
from multiprocessing.pool import ThreadPool
|
||||
from patroni.async_executor import AsyncExecutor, CriticalTask
|
||||
from patroni.exceptions import DCSError, PostgresConnectionException, PatroniException
|
||||
from patroni.exceptions import DCSError, PostgresConnectionException, PatroniFatalException
|
||||
from patroni.postgresql import ACTION_ON_START, ACTION_ON_ROLE_CHANGE
|
||||
from patroni.postgresql.misc import postgres_version_to_int
|
||||
from patroni.postgresql.rewind import Rewind
|
||||
from patroni.utils import polling_loop, tzutc, is_standby_cluster as _is_standby_cluster
|
||||
from patroni.utils import polling_loop, tzutc, is_standby_cluster as _is_standby_cluster, parse_int
|
||||
from patroni.dcs import RemoteMember
|
||||
from threading import RLock
|
||||
|
||||
@@ -70,8 +70,9 @@ class Ha(object):
|
||||
self._was_paused = False
|
||||
self._leader_timeline = None
|
||||
self.recovering = False
|
||||
self._post_bootstrap_task = None
|
||||
self._async_response = CriticalTask()
|
||||
self._crash_recovery_executed = False
|
||||
self._crash_recovery_started = None
|
||||
self._start_timeout = None
|
||||
self._async_executor = AsyncExecutor(self.state_handler.cancellable, self.wakeup)
|
||||
self.watchdog = patroni.watchdog
|
||||
@@ -87,6 +88,9 @@ class Ha(object):
|
||||
# already running as replica was aborted due to cluster not beeing initialized in DCS.
|
||||
self._join_aborted = False
|
||||
|
||||
# used only in backoff after failing a pre_promote script
|
||||
self._released_leader_key_timestamp = 0
|
||||
|
||||
def check_mode(self, mode):
|
||||
# Try to protect from the case when DCS was wiped out during pause
|
||||
if self.cluster and self.cluster.config and self.cluster.config.modify_index:
|
||||
@@ -94,6 +98,11 @@ class Ha(object):
|
||||
else:
|
||||
return self.patroni.config.check_mode(mode)
|
||||
|
||||
def master_stop_timeout(self):
|
||||
""" Master stop timeout """
|
||||
ret = parse_int(self.patroni.config['master_stop_timeout'])
|
||||
return ret if ret and ret > 0 and self.is_synchronous_mode() else None
|
||||
|
||||
def is_paused(self):
|
||||
return self.check_mode('pause')
|
||||
|
||||
@@ -148,7 +157,11 @@ class Ha(object):
|
||||
last_operation = self.state_handler.last_operation()
|
||||
except Exception:
|
||||
logger.exception('Exception when called state_handler.last_operation()')
|
||||
ret = self.dcs.update_leader(last_operation, self._leader_access_is_restricted)
|
||||
try:
|
||||
ret = self.dcs.update_leader(last_operation, self._leader_access_is_restricted)
|
||||
except Exception:
|
||||
logger.exception('Unexpected exception raised from update_leader, please report it as a BUG')
|
||||
ret = False
|
||||
self.set_is_leader(ret)
|
||||
if ret:
|
||||
self.watchdog.keepalive()
|
||||
@@ -193,6 +206,8 @@ class Ha(object):
|
||||
try:
|
||||
timeline, wal_position, pg_control_timeline = self.state_handler.timeline_wal_position()
|
||||
data['xlog_location'] = wal_position
|
||||
if not timeline: # try pg_stat_wal_receiver to get the timeline
|
||||
timeline = self.state_handler.received_timeline()
|
||||
if not timeline:
|
||||
# So far the only way to get the current timeline on the standby is from
|
||||
# the replication connection. In order to avoid opening the replication
|
||||
@@ -204,7 +219,7 @@ class Ha(object):
|
||||
if self.state_handler.role == 'standby_leader':
|
||||
timeline = pg_control_timeline or self.state_handler.pg_control_timeline()
|
||||
else:
|
||||
timeline = self.state_handler.replica_cached_timeline(timeline)
|
||||
timeline = self.state_handler.replica_cached_timeline(self._leader_timeline)
|
||||
if timeline:
|
||||
data['timeline'] = timeline
|
||||
except Exception:
|
||||
@@ -245,7 +260,8 @@ class Ha(object):
|
||||
elif self.cluster.initialize is None and not self.patroni.nofailover and 'bootstrap' in self.patroni.config:
|
||||
if self.dcs.initialize(create_new=True): # race for initialization
|
||||
self.state_handler.bootstrapping = True
|
||||
self._post_bootstrap_task = CriticalTask()
|
||||
with self._async_response:
|
||||
self._async_response.reset()
|
||||
|
||||
if self.is_standby_cluster():
|
||||
ret = self._async_executor.try_run_async('bootstrap_standby_leader', self.bootstrap_standby_leader)
|
||||
@@ -272,7 +288,8 @@ class Ha(object):
|
||||
clone_source = self.get_remote_master()
|
||||
msg = 'clone from remote master {0}'.format(clone_source.conn_url)
|
||||
result = self.clone(clone_source, msg)
|
||||
self._post_bootstrap_task.complete(result)
|
||||
with self._async_response: # pretend that post_bootstrap was already executed
|
||||
self._async_response.complete(result)
|
||||
if result:
|
||||
self.state_handler.set_role('standby_leader')
|
||||
|
||||
@@ -317,8 +334,9 @@ class Ha(object):
|
||||
and not self._crash_recovery_executed and \
|
||||
(self.cluster.is_unlocked() or self._rewind.can_rewind):
|
||||
self._crash_recovery_executed = True
|
||||
self._crash_recovery_started = time.time()
|
||||
msg = 'doing crash recovery in a single user mode'
|
||||
return self._async_executor.try_run_async(msg, self.state_handler.fix_cluster_state) or msg
|
||||
return self._async_executor.try_run_async(msg, self._rewind.ensure_clean_shutdown) or msg
|
||||
|
||||
self.load_cluster_from_dcs()
|
||||
|
||||
@@ -359,7 +377,7 @@ class Ha(object):
|
||||
elif self.patroni.replicatefrom and self.patroni.replicatefrom != self.state_handler.name:
|
||||
node_to_follow = cluster.get_member(self.patroni.replicatefrom)
|
||||
else:
|
||||
node_to_follow = cluster.leader
|
||||
node_to_follow = cluster.leader if cluster.leader and cluster.leader.name else None
|
||||
|
||||
node_to_follow = node_to_follow if node_to_follow and node_to_follow.name != self.state_handler.name else None
|
||||
|
||||
@@ -384,15 +402,22 @@ class Ha(object):
|
||||
if self.is_paused():
|
||||
if not (self._rewind.is_needed and self._rewind.can_rewind_or_reinitialize_allowed)\
|
||||
or self.cluster.is_unlocked():
|
||||
self.state_handler.set_role('master' if is_leader else 'replica')
|
||||
if is_leader:
|
||||
self.state_handler.set_role('master')
|
||||
return 'continue to run as master without lock'
|
||||
elif not node_to_follow:
|
||||
elif self.state_handler.role != 'standby_leader':
|
||||
self.state_handler.set_role('replica')
|
||||
|
||||
if not node_to_follow:
|
||||
return 'no action'
|
||||
elif is_leader:
|
||||
self.demote('immediate-nolock')
|
||||
return demote_reason
|
||||
|
||||
if self.is_standby_cluster() and self._leader_timeline and \
|
||||
self.state_handler.get_history(self._leader_timeline + 1):
|
||||
self._rewind.trigger_check_diverged_lsn()
|
||||
|
||||
msg = self._handle_rewind_or_reinitialize()
|
||||
if msg:
|
||||
return msg
|
||||
@@ -410,6 +435,7 @@ class Ha(object):
|
||||
self.state_handler.follow, args=(node_to_follow, role))
|
||||
else:
|
||||
self.state_handler.follow(node_to_follow, role, do_reload=True)
|
||||
self._rewind.trigger_check_diverged_lsn()
|
||||
elif role == 'standby_leader' and self.state_handler.role != role:
|
||||
self.state_handler.set_role(role)
|
||||
self.state_handler.call_nowait(ACTION_ON_ROLE_CHANGE)
|
||||
@@ -432,28 +458,34 @@ class Ha(object):
|
||||
promoting standbys that were guaranteed to be replicating synchronously.
|
||||
"""
|
||||
if self.is_synchronous_mode():
|
||||
current = self.cluster.sync.leader and self.cluster.sync.sync_standby
|
||||
picked, allow_promote = self.state_handler.pick_synchronous_standby(self.cluster)
|
||||
if picked != current:
|
||||
# We need to revoke privilege from current before replacing it in the config
|
||||
if current:
|
||||
logger.info("Removing synchronous privilege from %s", current)
|
||||
if not self.dcs.write_sync_state(self.state_handler.name, None, index=self.cluster.sync.index):
|
||||
sync_node_count = self.patroni.config['synchronous_node_count']
|
||||
current = self.cluster.sync.leader and self.cluster.sync.members or []
|
||||
picked, allow_promote = self.state_handler.pick_synchronous_standby(self.cluster, sync_node_count)
|
||||
if set(picked) != set(current):
|
||||
# update synchronous standby list in dcs temporarily to point to common nodes in current and picked
|
||||
sync_common = list(set(current).intersection(set(allow_promote)))
|
||||
if set(sync_common) != set(current):
|
||||
logger.info("Updating synchronous privilege temporarily from %s to %s", current, sync_common)
|
||||
if not self.dcs.write_sync_state(self.state_handler.name,
|
||||
sync_common or None,
|
||||
index=self.cluster.sync.index):
|
||||
logger.info('Synchronous replication key updated by someone else.')
|
||||
return
|
||||
|
||||
if self.is_synchronous_mode_strict() and picked is None:
|
||||
picked = '*'
|
||||
# Update db param and wait for x secs
|
||||
if self.is_synchronous_mode_strict() and not picked:
|
||||
picked = ['*']
|
||||
logger.warning("No standbys available!")
|
||||
|
||||
logger.info("Assigning synchronous standby status to %s", picked)
|
||||
self.state_handler.config.set_synchronous_standby(picked)
|
||||
|
||||
if picked and picked != '*' and not allow_promote:
|
||||
if picked and picked[0] != '*' and set(allow_promote) != set(picked) and not allow_promote:
|
||||
# Wait for PostgreSQL to enable synchronous mode and see if we can immediately set sync_standby
|
||||
time.sleep(2)
|
||||
picked, allow_promote = self.state_handler.pick_synchronous_standby(self.cluster)
|
||||
if allow_promote:
|
||||
_, allow_promote = self.state_handler.pick_synchronous_standby(self.cluster,
|
||||
sync_node_count)
|
||||
if allow_promote and set(allow_promote) != set(sync_common):
|
||||
try:
|
||||
cluster = self.dcs.get_cluster()
|
||||
except DCSError:
|
||||
@@ -461,18 +493,18 @@ class Ha(object):
|
||||
if cluster.sync.leader and cluster.sync.leader != self.state_handler.name:
|
||||
logger.info("Synchronous replication key updated by someone else")
|
||||
return
|
||||
if not self.dcs.write_sync_state(self.state_handler.name, picked, index=cluster.sync.index):
|
||||
if not self.dcs.write_sync_state(self.state_handler.name, allow_promote, index=cluster.sync.index):
|
||||
logger.info("Synchronous replication key updated by someone else")
|
||||
return
|
||||
logger.info("Synchronous standby status assigned to %s", picked)
|
||||
logger.info("Synchronous standby status assigned to %s", allow_promote)
|
||||
else:
|
||||
if self.cluster.sync.leader and self.dcs.delete_sync_state(index=self.cluster.sync.index):
|
||||
logger.info("Disabled synchronous replication")
|
||||
self.state_handler.config.set_synchronous_standby(None)
|
||||
self.state_handler.config.set_synchronous_standby([])
|
||||
|
||||
def is_sync_standby(self, cluster):
|
||||
return cluster.leader and cluster.sync.leader == cluster.leader.name \
|
||||
and cluster.sync.sync_standby == self.state_handler.name
|
||||
and self.state_handler.name in cluster.sync.members
|
||||
|
||||
def while_not_sync_standby(self, func):
|
||||
"""Runs specified action while trying to make sure that the node is not assigned synchronous standby status.
|
||||
@@ -518,9 +550,10 @@ class Ha(object):
|
||||
if cluster_history:
|
||||
self.dcs.set_history_value('[]')
|
||||
elif not cluster_history or cluster_history[-1][0] != master_timeline - 1 or len(cluster_history[-1]) != 4:
|
||||
cluster_history = {l[0]: l for l in cluster_history or []}
|
||||
cluster_history = {line[0]: line for line in cluster_history or []}
|
||||
history = self.state_handler.get_history(master_timeline)
|
||||
if history:
|
||||
if history and self.cluster.config:
|
||||
history = history[-self.cluster.config.max_timelines_history:]
|
||||
for line in history:
|
||||
# enrich current history with promotion timestamps stored in DCS
|
||||
if len(line) == 3 and line[0] in cluster_history \
|
||||
@@ -534,13 +567,27 @@ class Ha(object):
|
||||
return self.follow(demote_reason, message)
|
||||
|
||||
def enforce_master_role(self, message, promote_message):
|
||||
if not self.is_paused() and not self.watchdog.is_running and not self.watchdog.activate():
|
||||
if self.state_handler.is_leader():
|
||||
self.demote('immediate')
|
||||
return 'Demoting self because watchdog could not be activated'
|
||||
else:
|
||||
self.release_leader_key_voluntarily()
|
||||
return 'Not promoting self because watchdog could not be activated'
|
||||
"""
|
||||
Ensure the node that has won the race for the leader key meets criteria
|
||||
for promoting its PG server to the 'master' role.
|
||||
"""
|
||||
if not self.is_paused():
|
||||
if not self.watchdog.is_running and not self.watchdog.activate():
|
||||
if self.state_handler.is_leader():
|
||||
self.demote('immediate')
|
||||
return 'Demoting self because watchdog could not be activated'
|
||||
else:
|
||||
self.release_leader_key_voluntarily()
|
||||
return 'Not promoting self because watchdog could not be activated'
|
||||
|
||||
with self._async_response:
|
||||
if self._async_response.result is False:
|
||||
logger.warning("Releasing the leader key voluntarily because the pre-promote script failed")
|
||||
self._released_leader_key_timestamp = time.time()
|
||||
self.release_leader_key_voluntarily()
|
||||
# discard the result of the failed pre-promote script to be able to re-try promote
|
||||
self._async_response.reset()
|
||||
return 'Promotion cancelled because the pre-promote script failed'
|
||||
|
||||
if self.state_handler.is_leader():
|
||||
# Inform the state handler about its master role.
|
||||
@@ -560,7 +607,7 @@ class Ha(object):
|
||||
# Somebody else updated sync state, it may be due to us losing the lock. To be safe, postpone
|
||||
# promotion until next cycle. TODO: trigger immediate retry of run_cycle
|
||||
return 'Postponing promotion because synchronous replication state was updated by somebody else'
|
||||
self.state_handler.config.set_synchronous_standby('*' if self.is_synchronous_mode_strict() else None)
|
||||
self.state_handler.config.set_synchronous_standby(['*'] if self.is_synchronous_mode_strict() else [])
|
||||
if self.state_handler.role != 'master':
|
||||
self.set_leader_access_is_restricted(self.cluster.has_permanent_logical_slots(self.state_handler.name))
|
||||
|
||||
@@ -568,8 +615,10 @@ class Ha(object):
|
||||
self._rewind.reset_state()
|
||||
logger.info("cleared rewind state after becoming the leader")
|
||||
|
||||
with self._async_response:
|
||||
self._async_response.reset()
|
||||
self._async_executor.try_run_async('promote', self.state_handler.promote,
|
||||
args=(self.dcs.loop_wait, on_success,
|
||||
args=(self.dcs.loop_wait, self._async_response, on_success,
|
||||
self._leader_access_is_restricted))
|
||||
return promote_message
|
||||
|
||||
@@ -705,6 +754,10 @@ class Ha(object):
|
||||
return self._is_healthiest_node(members, check_replication_lag=False)
|
||||
|
||||
def is_healthiest_node(self):
|
||||
if time.time() - self._released_leader_key_timestamp < self.dcs.ttl:
|
||||
logger.info('backoff: skip leader race after pre_promote script failure and releasing the lock voluntarily')
|
||||
return False
|
||||
|
||||
if self.is_paused() and not self.patroni.nofailover and \
|
||||
self.cluster.failover and not self.cluster.failover.scheduled_at:
|
||||
ret = self.manual_failover_process_no_leader()
|
||||
@@ -714,8 +767,10 @@ class Ha(object):
|
||||
if self.state_handler.is_starting(): # postgresql still starting up is unhealthy
|
||||
return False
|
||||
|
||||
if self.state_handler.is_leader(): # leader is always the healthiest
|
||||
return True
|
||||
if self.state_handler.is_leader():
|
||||
# in pause leader is the healthiest only when no initialize or sysid matches with initialize!
|
||||
return not self.is_paused() or not self.cluster.initialize\
|
||||
or self.state_handler.sysid == self.cluster.initialize
|
||||
|
||||
if self.is_paused():
|
||||
return False
|
||||
@@ -742,13 +797,13 @@ class Ha(object):
|
||||
|
||||
return self._is_healthiest_node(members.values())
|
||||
|
||||
def _delete_leader(self):
|
||||
def _delete_leader(self, last_operation=None):
|
||||
self.set_is_leader(False)
|
||||
self.dcs.delete_leader()
|
||||
self.dcs.delete_leader(last_operation)
|
||||
self.dcs.reset_cluster()
|
||||
|
||||
def release_leader_key_voluntarily(self):
|
||||
self._delete_leader()
|
||||
def release_leader_key_voluntarily(self, last_operation=None):
|
||||
self._delete_leader(last_operation)
|
||||
self.touch_member()
|
||||
logger.info("Leader key released")
|
||||
|
||||
@@ -772,13 +827,15 @@ class Ha(object):
|
||||
|
||||
self._rewind.trigger_check_diverged_lsn()
|
||||
self.state_handler.stop(mode_control['stop'], checkpoint=mode_control['checkpoint'],
|
||||
on_safepoint=self.watchdog.disable if self.watchdog.is_running else None)
|
||||
on_safepoint=self.watchdog.disable if self.watchdog.is_running else None,
|
||||
stop_timeout=self.master_stop_timeout())
|
||||
self.state_handler.set_role('demoted')
|
||||
self.set_is_leader(False)
|
||||
|
||||
if mode_control['release']:
|
||||
checkpoint_location = self.state_handler.latest_checkpoint_location() if mode == 'graceful' else None
|
||||
with self._async_executor:
|
||||
self.release_leader_key_voluntarily()
|
||||
self.release_leader_key_voluntarily(checkpoint_location)
|
||||
time.sleep(2) # Give a time to somebody to take the leader lock
|
||||
if mode_control['offline']:
|
||||
node_to_follow, leader = None, None
|
||||
@@ -793,7 +850,7 @@ class Ha(object):
|
||||
self._async_executor.try_run_async('starting after demotion', self.state_handler.follow, (node_to_follow,))
|
||||
else:
|
||||
if self.is_synchronous_mode():
|
||||
self.state_handler.config.set_synchronous_standby(None)
|
||||
self.state_handler.config.set_synchronous_standby([])
|
||||
if self._rewind.rewind_or_reinitialize_needed_and_possible(leader):
|
||||
return False # do not start postgres, but run pg_rewind on the next iteration
|
||||
self.state_handler.follow(node_to_follow)
|
||||
@@ -854,7 +911,7 @@ class Ha(object):
|
||||
else:
|
||||
if self.is_synchronous_mode():
|
||||
if failover.candidate and not self.cluster.sync.matches(failover.candidate):
|
||||
logger.warning('Failover candidate=%s does not match with sync_standby=%s',
|
||||
logger.warning('Failover candidate=%s does not match with sync_standbys=%s',
|
||||
failover.candidate, self.cluster.sync.sync_standby)
|
||||
members = []
|
||||
else:
|
||||
@@ -892,9 +949,9 @@ class Ha(object):
|
||||
self.load_cluster_from_dcs()
|
||||
|
||||
if self.is_standby_cluster():
|
||||
# standby leader disappeared, and this is a healthiest
|
||||
# standby leader disappeared, and this is the healthiest
|
||||
# replica, so it should become a new standby leader.
|
||||
# This imply that we need to start following a remote master
|
||||
# This implies we need to start following a remote master
|
||||
msg = 'promoted self to a standby leader by acquiring session lock'
|
||||
return self.enforce_follow_remote_master(msg)
|
||||
else:
|
||||
@@ -938,7 +995,7 @@ class Ha(object):
|
||||
return msg
|
||||
|
||||
# check if the node is ready to be used by pg_rewind
|
||||
self._rewind.check_for_checkpoint_after_promote()
|
||||
self._rewind.ensure_checkpoint_after_promote(self.wakeup)
|
||||
|
||||
if self.is_standby_cluster():
|
||||
# in case of standby cluster we don't really need to
|
||||
@@ -1083,7 +1140,7 @@ class Ha(object):
|
||||
return (False, 'restart failed')
|
||||
|
||||
def _do_reinitialize(self, cluster):
|
||||
self.state_handler.stop('immediate')
|
||||
self.state_handler.stop('immediate', stop_timeout=self.patroni.config['retry_timeout'])
|
||||
# Commented redundant data directory cleanup here
|
||||
# self.state_handler.remove_data_directory()
|
||||
|
||||
@@ -1112,10 +1169,28 @@ class Ha(object):
|
||||
self._async_executor.run_async(self._do_reinitialize, args=(self.cluster, ))
|
||||
|
||||
def handle_long_action_in_progress(self):
|
||||
"""
|
||||
Figure out what to do with the task AsyncExecutor is performing.
|
||||
"""
|
||||
if self.has_lock() and self.update_lock():
|
||||
if self._async_executor.scheduled_action == 'doing crash recovery in a single user mode':
|
||||
time_left = self.patroni.config['master_start_timeout'] - (time.time() - self._crash_recovery_started)
|
||||
if time_left <= 0 and self.is_failover_possible(self.cluster.members):
|
||||
logger.info("Demoting self because crash recovery is taking too long")
|
||||
self.state_handler.cancellable.cancel(True)
|
||||
self.demote('immediate')
|
||||
return 'terminated crash recovery because of startup timeout'
|
||||
|
||||
return 'updated leader lock during ' + self._async_executor.scheduled_action
|
||||
elif not self.state_handler.bootstrapping:
|
||||
# Don't have lock, make sure we are not starting up a master in the background
|
||||
# Don't have lock, make sure we are not promoting or starting up a master in the background
|
||||
if self._async_executor.scheduled_action == 'promote':
|
||||
with self._async_response:
|
||||
cancel = self._async_response.cancel()
|
||||
if cancel:
|
||||
self.state_handler.cancellable.cancel()
|
||||
return 'lost leader before promote'
|
||||
|
||||
if self.state_handler.role == 'master':
|
||||
logger.info("Demoting master during " + self._async_executor.scheduled_action)
|
||||
if self._async_executor.scheduled_action == 'restart':
|
||||
@@ -1126,7 +1201,6 @@ class Ha(object):
|
||||
self.state_handler.terminate_starting_postmaster(postmaster=task.result)
|
||||
self.demote('immediate-nolock')
|
||||
return 'lost leader lock during ' + self._async_executor.scheduled_action
|
||||
|
||||
if self.cluster.is_unlocked():
|
||||
logger.info('not healthy enough for leader race')
|
||||
|
||||
@@ -1143,39 +1217,45 @@ class Ha(object):
|
||||
if not self.state_handler.is_running():
|
||||
self.watchdog.disable()
|
||||
if self.has_lock():
|
||||
self.state_handler.set_role('demoted')
|
||||
if self.state_handler.role in ('master', 'standby_leader'):
|
||||
self.state_handler.set_role('demoted')
|
||||
self._delete_leader()
|
||||
return 'removed leader key after trying and failing to start postgres'
|
||||
return 'failed to start postgres'
|
||||
self._crash_recovery_executed = False
|
||||
if self._rewind.executed and not self._rewind.failed:
|
||||
self._rewind.reset_state()
|
||||
return None
|
||||
|
||||
def cancel_initialization(self):
|
||||
logger.info('removing initialize key after failed attempt to bootstrap the cluster')
|
||||
self.dcs.cancel_initialization()
|
||||
self.state_handler.stop('immediate')
|
||||
self.state_handler.stop('immediate', stop_timeout=self.patroni.config['retry_timeout'])
|
||||
self.state_handler.move_data_directory()
|
||||
raise PatroniException('Failed to bootstrap cluster')
|
||||
raise PatroniFatalException('Failed to bootstrap cluster')
|
||||
|
||||
def post_bootstrap(self):
|
||||
with self._async_response:
|
||||
result = self._async_response.result
|
||||
# bootstrap has failed if postgres is not running
|
||||
if not self.state_handler.is_running() or self._post_bootstrap_task.result is False:
|
||||
if not self.state_handler.is_running() or result is False:
|
||||
self.cancel_initialization()
|
||||
|
||||
if self._post_bootstrap_task.result is None:
|
||||
if result is None:
|
||||
if not self.state_handler.is_leader():
|
||||
return 'waiting for end of recovery after bootstrap'
|
||||
|
||||
self.state_handler.set_role('master')
|
||||
ret = self._async_executor.try_run_async('post_bootstrap', self.state_handler.bootstrap.post_bootstrap,
|
||||
args=(self.patroni.config['bootstrap'], self._post_bootstrap_task))
|
||||
args=(self.patroni.config['bootstrap'], self._async_response))
|
||||
return ret or 'running post_bootstrap'
|
||||
|
||||
self.state_handler.bootstrapping = False
|
||||
self.dcs.set_config_value(json.dumps(self.patroni.config.dynamic_configuration, separators=(',', ':')))
|
||||
if not self.watchdog.activate():
|
||||
logger.error('Cancelling bootstrap because watchdog activation failed')
|
||||
self.cancel_initialization()
|
||||
self.dcs.initialize(create_new=(self.cluster.initialize is None), sysid=self.state_handler.sysid)
|
||||
self.dcs.set_config_value(json.dumps(self.patroni.config.dynamic_configuration, separators=(',', ':')))
|
||||
self.state_handler.slots_handler.sync_replication_slots(self.cluster)
|
||||
self.dcs.take_leader()
|
||||
self.set_is_leader(True)
|
||||
@@ -1275,7 +1355,7 @@ class Ha(object):
|
||||
# is data directory empty?
|
||||
if self.state_handler.data_directory_empty():
|
||||
self.state_handler.set_role('uninitialized')
|
||||
self.state_handler.stop('immediate')
|
||||
self.state_handler.stop('immediate', stop_timeout=self.patroni.config['retry_timeout'])
|
||||
# In case datadir went away while we were master.
|
||||
self.watchdog.disable()
|
||||
|
||||
@@ -1284,20 +1364,29 @@ class Ha(object):
|
||||
self.release_leader_key_voluntarily()
|
||||
return 'released leader key voluntarily as data dir empty and currently leader'
|
||||
|
||||
if self.is_paused():
|
||||
return 'running with empty data directory'
|
||||
return self.bootstrap() # new node
|
||||
else:
|
||||
# check if we are allowed to join
|
||||
data_sysid = self.state_handler.sysid
|
||||
if not self.sysid_valid(data_sysid):
|
||||
# data directory is not empty, but no valid sysid, cluster must be broken, suggest reinit
|
||||
return ("data dir for the cluster is not empty, but system ID is invalid; consider doing"
|
||||
"reinitialize")
|
||||
return ("data dir for the cluster is not empty, "
|
||||
"but system ID is invalid; consider doing reinitialize")
|
||||
|
||||
if self.sysid_valid(self.cluster.initialize):
|
||||
if self.cluster.initialize != data_sysid:
|
||||
logger.fatal("system ID mismatch, node %s belongs to a different cluster: %s != %s",
|
||||
self.state_handler.name, self.cluster.initialize, data_sysid)
|
||||
sys.exit(1)
|
||||
if self.is_paused():
|
||||
logger.warning('system ID has changed while in paused mode. Patroni will exit when resuming'
|
||||
' unless system ID is reset: %s != %s', self.cluster.initialize, data_sysid)
|
||||
if self.has_lock():
|
||||
self.release_leader_key_voluntarily()
|
||||
return 'released leader key voluntarily due to the system ID mismatch'
|
||||
else:
|
||||
logger.fatal('system ID mismatch, node %s belongs to a different cluster: %s != %s',
|
||||
self.state_handler.name, self.cluster.initialize, data_sysid)
|
||||
sys.exit(1)
|
||||
elif self.cluster.is_unlocked() and not self.is_paused():
|
||||
# "bootstrap", but data directory is not empty
|
||||
if not self.state_handler.cb_called and self.state_handler.is_running() \
|
||||
@@ -1310,6 +1399,7 @@ class Ha(object):
|
||||
|
||||
if not self.state_handler.is_healthy():
|
||||
if self.is_paused():
|
||||
self.state_handler.set_state('stopped')
|
||||
if self.has_lock():
|
||||
self._delete_leader()
|
||||
return 'removed leader lock because postgres is not running'
|
||||
@@ -1322,6 +1412,8 @@ class Ha(object):
|
||||
(self._rewind.is_needed and self._rewind.can_rewind_or_reinitialize_allowed):
|
||||
return 'postgres is not running'
|
||||
|
||||
if self.state_handler.state in ('running', 'starting'):
|
||||
self.state_handler.set_state('crashed')
|
||||
# try to start dead postgres
|
||||
return self.recover()
|
||||
|
||||
@@ -1356,8 +1448,14 @@ class Ha(object):
|
||||
|
||||
def run_cycle(self):
|
||||
with self._async_executor:
|
||||
info = self._run_cycle()
|
||||
return (self.is_paused() and 'PAUSE: ' or '') + info
|
||||
try:
|
||||
info = self._run_cycle()
|
||||
return (self.is_paused() and 'PAUSE: ' or '') + info
|
||||
except PatroniFatalException:
|
||||
raise
|
||||
except Exception:
|
||||
logger.exception('Unexpected exception')
|
||||
return 'Unexpected exception raised, please report it as a BUG'
|
||||
|
||||
def shutdown(self):
|
||||
if self.is_paused():
|
||||
@@ -1369,10 +1467,12 @@ class Ha(object):
|
||||
# This might not be the desired behavior of users, as a graceful shutdown of the host can mean lost data.
|
||||
# We probably need to something smarter here.
|
||||
disable_wd = self.watchdog.disable if self.watchdog.is_running else None
|
||||
self.while_not_sync_standby(lambda: self.state_handler.stop(checkpoint=False, on_safepoint=disable_wd))
|
||||
self.while_not_sync_standby(lambda: self.state_handler.stop(checkpoint=False, on_safepoint=disable_wd,
|
||||
stop_timeout=self.master_stop_timeout()))
|
||||
if not self.state_handler.is_running():
|
||||
if self.has_lock():
|
||||
self.dcs.delete_leader()
|
||||
if self.is_leader():
|
||||
checkpoint_location = self.state_handler.latest_checkpoint_location()
|
||||
self.dcs.delete_leader(checkpoint_location)
|
||||
self.touch_member()
|
||||
else:
|
||||
# XXX: what about when Patroni is started as the wrong user that has access to the watchdog device
|
||||
@@ -1383,7 +1483,7 @@ class Ha(object):
|
||||
|
||||
def watch(self, timeout):
|
||||
# watch on leader key changes if the postgres is running and leader is known and current node is not lock owner
|
||||
if self._async_executor.busy or self.cluster.is_unlocked() or self.has_lock(False):
|
||||
if self._async_executor.busy or not self.cluster or self.cluster.is_unlocked() or self.has_lock(False):
|
||||
leader_index = None
|
||||
else:
|
||||
leader_index = self.cluster.leader.index
|
||||
|
||||
+258
-139
@@ -8,16 +8,19 @@ import time
|
||||
|
||||
from contextlib import contextmanager
|
||||
from copy import deepcopy
|
||||
from dateutil import tz
|
||||
from datetime import datetime
|
||||
from patroni.postgresql.callback_executor import CallbackExecutor
|
||||
from patroni.postgresql.bootstrap import Bootstrap
|
||||
from patroni.postgresql.cancellable import CancellableSubprocess
|
||||
from patroni.postgresql.config import ConfigHandler
|
||||
from patroni.postgresql.config import ConfigHandler, mtime
|
||||
from patroni.postgresql.connection import Connection, get_connection_cursor
|
||||
from patroni.postgresql.misc import parse_history, postgres_major_version_to_int
|
||||
from patroni.postgresql.misc import parse_history, parse_lsn, postgres_major_version_to_int
|
||||
from patroni.postgresql.postmaster import PostmasterProcess
|
||||
from patroni.postgresql.slots import SlotsHandler
|
||||
from patroni.exceptions import PostgresConnectionException
|
||||
from patroni.utils import Retry, RetryFailedError, polling_loop
|
||||
from patroni.utils import Retry, RetryFailedError, polling_loop, data_directory_is_empty, parse_int
|
||||
from psutil import TimeoutExpired
|
||||
from threading import current_thread, Lock
|
||||
|
||||
|
||||
@@ -45,6 +48,16 @@ def null_context():
|
||||
|
||||
class Postgresql(object):
|
||||
|
||||
POSTMASTER_START_TIME = "pg_catalog.to_char(pg_catalog.pg_postmaster_start_time(), 'YYYY-MM-DD HH24:MI:SS.MS TZ')"
|
||||
TL_LSN = ("CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0 "
|
||||
"ELSE ('x' || pg_catalog.substr(pg_catalog.pg_{0}file_name("
|
||||
"pg_catalog.pg_current_{0}_{1}()), 1, 8))::bit(32)::int END, " # master timeline
|
||||
"CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0 "
|
||||
"ELSE pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_current_{0}_{1}(), '0/0')::bigint END, " # write_lsn
|
||||
"pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_last_{0}_replay_{1}(), '0/0')::bigint, "
|
||||
"pg_catalog.pg_{0}_{1}_diff(COALESCE(pg_catalog.pg_last_{0}_receive_{1}(), '0/0'), '0/0')::bigint, "
|
||||
"pg_catalog.pg_is_in_recovery() AND pg_catalog.pg_is_{0}_replay_paused()")
|
||||
|
||||
def __init__(self, config):
|
||||
self.name = config['name']
|
||||
self.scope = config['scope']
|
||||
@@ -124,6 +137,10 @@ class Postgresql(object):
|
||||
def callback(self):
|
||||
return self.config.get('callbacks') or {}
|
||||
|
||||
@property
|
||||
def wal_dir(self):
|
||||
return os.path.join(self._data_dir, 'pg_' + self.wal_name)
|
||||
|
||||
@property
|
||||
def wal_name(self):
|
||||
return 'wal' if self._major_version >= 100000 else 'xlog'
|
||||
@@ -134,17 +151,17 @@ class Postgresql(object):
|
||||
|
||||
@property
|
||||
def cluster_info_query(self):
|
||||
pg_control_timeline = 'timeline_id FROM pg_catalog.pg_control_checkpoint()' \
|
||||
if self._major_version >= 90600 and self.role == 'standby_leader' else '0'
|
||||
return ("SELECT CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0 "
|
||||
"ELSE ('x' || pg_catalog.substr(pg_catalog.pg_{0}file_name("
|
||||
"pg_catalog.pg_current_{0}_{1}()), 1, 8))::bit(32)::int END, "
|
||||
"CASE WHEN pg_catalog.pg_is_in_recovery() THEN GREATEST("
|
||||
" pg_catalog.pg_{0}_{1}_diff(COALESCE("
|
||||
"pg_catalog.pg_last_{0}_receive_{1}(), '0/0'), '0/0')::bigint,"
|
||||
" pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_last_{0}_replay_{1}(), '0/0')::bigint)"
|
||||
"ELSE pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_current_{0}_{1}(), '0/0')::bigint "
|
||||
"END, {2}").format(self.wal_name, self.lsn_name, pg_control_timeline)
|
||||
if self._major_version >= 90600:
|
||||
extra = (", CASE WHEN latest_end_lsn IS NULL THEN NULL ELSE received_tli END,"
|
||||
" slot_name, conninfo FROM pg_catalog.pg_stat_get_wal_receiver()")
|
||||
if self.role == 'standby_leader':
|
||||
extra = "timeline_id" + extra + ", pg_catalog.pg_control_checkpoint()"
|
||||
else:
|
||||
extra = "0" + extra
|
||||
else:
|
||||
extra = "0, NULL, NULL, NULL"
|
||||
|
||||
return ("SELECT " + self.TL_LSN + ", {2}").format(self.wal_name, self.lsn_name, extra)
|
||||
|
||||
def _version_file_exists(self):
|
||||
return not self.data_directory_empty() and os.path.isfile(self._version_file)
|
||||
@@ -266,9 +283,7 @@ class Postgresql(object):
|
||||
def data_directory_empty(self):
|
||||
if self.pg_control_exists():
|
||||
return False
|
||||
if not os.path.exists(self._data_dir):
|
||||
return True
|
||||
return all(os.name != 'nt' and (n.startswith('.') or n == 'lost+found') for n in os.listdir(self._data_dir))
|
||||
return data_directory_is_empty(self._data_dir)
|
||||
|
||||
def replica_method_options(self, method):
|
||||
return deepcopy(self.config.get(method, {}))
|
||||
@@ -291,7 +306,9 @@ class Postgresql(object):
|
||||
if not self._cluster_info_state:
|
||||
try:
|
||||
result = self._is_leader_retry(self._query, self.cluster_info_query).fetchone()
|
||||
self._cluster_info_state = dict(zip(['timeline', 'wal_position', 'pg_control_timeline'], result))
|
||||
self._cluster_info_state = dict(zip(['timeline', 'wal_position', 'replayed_location',
|
||||
'received_location', 'replay_paused', 'pg_control_timeline',
|
||||
'received_tli', 'slot_name', 'conninfo'], result))
|
||||
except RetryFailedError as e: # SELECT failed two times
|
||||
self._cluster_info_state = {'error': str(e)}
|
||||
if not self.is_starting() and self.pg_isready() == STATE_REJECT:
|
||||
@@ -302,6 +319,21 @@ class Postgresql(object):
|
||||
|
||||
return self._cluster_info_state.get(name)
|
||||
|
||||
def replayed_location(self):
|
||||
return self._cluster_info_state_get('replayed_location')
|
||||
|
||||
def received_location(self):
|
||||
return self._cluster_info_state_get('received_location')
|
||||
|
||||
def primary_slot_name(self):
|
||||
return self._cluster_info_state_get('slot_name')
|
||||
|
||||
def primary_conninfo(self):
|
||||
return self._cluster_info_state_get('conninfo')
|
||||
|
||||
def received_timeline(self):
|
||||
return self._cluster_info_state_get('received_tli')
|
||||
|
||||
def is_leader(self):
|
||||
return bool(self._cluster_info_state_get('timeline'))
|
||||
|
||||
@@ -311,6 +343,17 @@ class Postgresql(object):
|
||||
except (TypeError, ValueError):
|
||||
logger.exception('Failed to parse timeline from pg_controldata output')
|
||||
|
||||
def latest_checkpoint_location(self):
|
||||
"""Returns checkpoint location for the cleanly shut down primary"""
|
||||
|
||||
data = self.controldata()
|
||||
lsn = data.get('Latest checkpoint location')
|
||||
if data.get('Database cluster state') == 'shut down' and lsn:
|
||||
try:
|
||||
return str(parse_lsn(lsn))
|
||||
except (IndexError, ValueError) as e:
|
||||
logger.error('Exception when parsing lsn %s: %r', lsn, e)
|
||||
|
||||
def is_running(self):
|
||||
"""Returns PostmasterProcess if one is running on the data directory or None. If most recently seen process
|
||||
is running updates the cached process based on pid file."""
|
||||
@@ -415,7 +458,13 @@ class Postgresql(object):
|
||||
self.set_state('starting')
|
||||
self._pending_restart = False
|
||||
|
||||
configuration = self.config.effective_configuration
|
||||
try:
|
||||
if not self._major_version:
|
||||
self.configure_server_parameters()
|
||||
configuration = self.config.effective_configuration
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
self.config.check_directories()
|
||||
self.config.write_postgresql_conf(configuration)
|
||||
self.config.resolve_connection_addresses()
|
||||
@@ -423,7 +472,7 @@ class Postgresql(object):
|
||||
self.config.replace_pg_ident()
|
||||
|
||||
options = ['--{0}={1}'.format(p, configuration[p]) for p in self.config.CMDLINE_OPTIONS
|
||||
if p in configuration and p != 'wal_keep_segments']
|
||||
if p in configuration and p not in ('wal_keep_segments', 'wal_keep_size')]
|
||||
|
||||
if self.cancellable.is_cancelled:
|
||||
return False
|
||||
@@ -460,11 +509,13 @@ class Postgresql(object):
|
||||
else:
|
||||
return None
|
||||
|
||||
def checkpoint(self, connect_kwargs=None):
|
||||
def checkpoint(self, connect_kwargs=None, timeout=None):
|
||||
check_not_is_in_recovery = connect_kwargs is not None
|
||||
connect_kwargs = connect_kwargs or self.config.local_connect_kwargs
|
||||
for p in ['connect_timeout', 'options']:
|
||||
connect_kwargs.pop(p, None)
|
||||
if timeout:
|
||||
connect_kwargs['connect_timeout'] = timeout
|
||||
try:
|
||||
with get_connection_cursor(**connect_kwargs) as cur:
|
||||
cur.execute("SET statement_timeout = 0")
|
||||
@@ -477,7 +528,7 @@ class Postgresql(object):
|
||||
logger.exception('Exception during CHECKPOINT')
|
||||
return 'not accessible or not healty'
|
||||
|
||||
def stop(self, mode='fast', block_callbacks=False, checkpoint=None, on_safepoint=None):
|
||||
def stop(self, mode='fast', block_callbacks=False, checkpoint=None, on_safepoint=None, stop_timeout=None):
|
||||
"""Stop PostgreSQL
|
||||
|
||||
Supports a callback when a safepoint is reached. A safepoint is when no user backend can return a successful
|
||||
@@ -489,7 +540,7 @@ class Postgresql(object):
|
||||
if checkpoint is None:
|
||||
checkpoint = False if mode == 'immediate' else True
|
||||
|
||||
success, pg_signaled = self._do_stop(mode, block_callbacks, checkpoint, on_safepoint)
|
||||
success, pg_signaled = self._do_stop(mode, block_callbacks, checkpoint, on_safepoint, stop_timeout)
|
||||
if success:
|
||||
# block_callbacks is used during restart to avoid
|
||||
# running start/stop callbacks in addition to restart ones
|
||||
@@ -502,7 +553,7 @@ class Postgresql(object):
|
||||
self.set_state('stop failed')
|
||||
return success
|
||||
|
||||
def _do_stop(self, mode, block_callbacks, checkpoint, on_safepoint):
|
||||
def _do_stop(self, mode, block_callbacks, checkpoint, on_safepoint, stop_timeout):
|
||||
postmaster = self.is_running()
|
||||
if not postmaster:
|
||||
if on_safepoint:
|
||||
@@ -510,7 +561,7 @@ class Postgresql(object):
|
||||
return True, False
|
||||
|
||||
if checkpoint and not self.is_starting():
|
||||
self.checkpoint()
|
||||
self.checkpoint(timeout=stop_timeout)
|
||||
|
||||
if not block_callbacks:
|
||||
self.set_state('stopping')
|
||||
@@ -529,10 +580,28 @@ class Postgresql(object):
|
||||
postmaster.wait_for_user_backends_to_close()
|
||||
on_safepoint()
|
||||
|
||||
postmaster.wait()
|
||||
try:
|
||||
postmaster.wait(timeout=stop_timeout)
|
||||
except TimeoutExpired:
|
||||
logger.warning("Timeout during postmaster stop, aborting Postgres.")
|
||||
if not self.terminate_postmaster(postmaster, mode, stop_timeout):
|
||||
postmaster.wait()
|
||||
|
||||
return True, True
|
||||
|
||||
def terminate_postmaster(self, postmaster, mode, stop_timeout):
|
||||
if mode in ['fast', 'smart']:
|
||||
try:
|
||||
success = postmaster.signal_stop('immediate', self.pgcommand('pg_ctl'))
|
||||
if success:
|
||||
return True
|
||||
postmaster.wait(timeout=stop_timeout)
|
||||
return True
|
||||
except TimeoutExpired:
|
||||
pass
|
||||
logger.warning("Sending SIGKILL to Postmaster and its children")
|
||||
return postmaster.signal_kill()
|
||||
|
||||
def terminate_starting_postmaster(self, postmaster):
|
||||
"""Terminates a postmaster that has not yet opened ports or possibly even written a pid file. Blocks
|
||||
until the process goes away."""
|
||||
@@ -548,9 +617,9 @@ class Postgresql(object):
|
||||
except psycopg2.Error:
|
||||
pass
|
||||
|
||||
def reload(self):
|
||||
def reload(self, block_callbacks=False):
|
||||
ret = self.pg_ctl('reload')
|
||||
if ret:
|
||||
if ret and not block_callbacks:
|
||||
self.call_nowait(ACTION_ON_RELOAD)
|
||||
return ret
|
||||
|
||||
@@ -630,9 +699,17 @@ class Postgresql(object):
|
||||
return False
|
||||
return True
|
||||
|
||||
def get_guc_value(self, name):
|
||||
cmd = [self.pgcommand('postgres'), '-D', self._data_dir, '-C', name]
|
||||
try:
|
||||
data = subprocess.check_output(cmd)
|
||||
if data:
|
||||
return data.decode('utf-8').strip()
|
||||
except Exception as e:
|
||||
logger.error('Failed to execute %s: %r', cmd, e)
|
||||
|
||||
def controldata(self):
|
||||
""" return the contents of pg_controldata, or non-True value if pg_controldata call failed """
|
||||
result = {}
|
||||
# Don't try to call pg_controldata during backup restore
|
||||
if self._version_file_exists() and self.state != 'creating replica':
|
||||
try:
|
||||
@@ -640,34 +717,28 @@ class Postgresql(object):
|
||||
env.update(LANG='C', LC_ALL='C')
|
||||
data = subprocess.check_output([self.pgcommand('pg_controldata'), self._data_dir], env=env)
|
||||
if data:
|
||||
data = data.decode('utf-8').splitlines()
|
||||
# pg_controldata output depends on major verion. Some of parameters are prefixed by 'Current '
|
||||
result = {l.split(':')[0].replace('Current ', '', 1): l.split(':', 1)[1].strip() for l in data
|
||||
if l and ':' in l}
|
||||
data = filter(lambda e: ':' in e, data.decode('utf-8').splitlines())
|
||||
# pg_controldata output depends on major version. Some of parameters are prefixed by 'Current '
|
||||
return {k.replace('Current ', '', 1): v.strip() for k, v in map(lambda e: e.split(':', 1), data)}
|
||||
except subprocess.CalledProcessError:
|
||||
logger.exception("Error when calling pg_controldata")
|
||||
return result
|
||||
return {}
|
||||
|
||||
@contextmanager
|
||||
def get_replication_connection_cursor(self, host='localhost', port=5432, database=None, **kwargs):
|
||||
def get_replication_connection_cursor(self, host='localhost', port=5432, **kwargs):
|
||||
conn_kwargs = self.config.replication.copy()
|
||||
conn_kwargs.update(host=host, port=int(port), database=database or self._database, connect_timeout=3,
|
||||
user=conn_kwargs.pop('username'), replication=1, options='-c statement_timeout=2000')
|
||||
conn_kwargs.update(host=host, port=int(port) if port else None, user=conn_kwargs.pop('username'),
|
||||
connect_timeout=3, replication=1, options='-c statement_timeout=2000')
|
||||
with get_connection_cursor(**conn_kwargs) as cur:
|
||||
yield cur
|
||||
|
||||
def get_local_timeline_lsn_from_replication_connection(self):
|
||||
timeline = lsn = None
|
||||
def get_replica_timeline(self):
|
||||
try:
|
||||
with self.get_replication_connection_cursor(**self.config.local_replication_address) as cur:
|
||||
cur.execute('IDENTIFY_SYSTEM')
|
||||
timeline, lsn = cur.fetchone()[1:3]
|
||||
return cur.fetchone()[1]
|
||||
except Exception:
|
||||
logger.exception('Can not fetch local timeline and lsn from replication connection')
|
||||
return timeline, lsn
|
||||
|
||||
def get_replica_timeline(self):
|
||||
return self.get_local_timeline_lsn_from_replication_connection()[0]
|
||||
|
||||
def replica_cached_timeline(self, master_timeline):
|
||||
if not self._cached_replica_timeline or not master_timeline or self._cached_replica_timeline != master_timeline:
|
||||
@@ -678,19 +749,19 @@ class Postgresql(object):
|
||||
return self._cluster_info_state_get('timeline')
|
||||
|
||||
def get_history(self, timeline):
|
||||
history_path = 'pg_{0}/{1:08X}.history'.format(self.wal_name, timeline)
|
||||
try:
|
||||
cursor = self._connection.cursor()
|
||||
cursor.execute('SELECT isdir, modification FROM pg_catalog.pg_stat_file(%s)', (history_path,))
|
||||
isdir, modification = cursor.fetchone()
|
||||
if not isdir:
|
||||
cursor.execute('SELECT pg_catalog.pg_read_file(%s)', (history_path,))
|
||||
history = list(parse_history(cursor.fetchone()[0]))
|
||||
history_path = os.path.join(self.wal_dir, '{0:08X}.history'.format(timeline))
|
||||
history_mtime = mtime(history_path)
|
||||
if history_mtime:
|
||||
try:
|
||||
with open(history_path, 'r') as f:
|
||||
history = f.read()
|
||||
history = list(parse_history(history))
|
||||
if history[-1][0] == timeline - 1:
|
||||
history[-1].append(modification.isoformat())
|
||||
history_mtime = datetime.fromtimestamp(history_mtime).replace(tzinfo=tz.tzlocal())
|
||||
history[-1].append(history_mtime.isoformat())
|
||||
return history
|
||||
except Exception:
|
||||
logger.exception('Failed to read and parse %s', (history_path,))
|
||||
except Exception:
|
||||
logger.exception('Failed to read and parse %s', (history_path,))
|
||||
|
||||
def follow(self, member, role='replica', timeout=None, do_reload=False):
|
||||
recovery_params = self.config.build_recovery_params(member)
|
||||
@@ -708,7 +779,8 @@ class Postgresql(object):
|
||||
if self.is_running():
|
||||
if do_reload:
|
||||
self.config.write_postgresql_conf()
|
||||
self.reload()
|
||||
if self.reload(block_callbacks=change_role) and change_role:
|
||||
self.set_role(role)
|
||||
else:
|
||||
self.restart(block_callbacks=change_role, role=role)
|
||||
else:
|
||||
@@ -725,9 +797,38 @@ class Postgresql(object):
|
||||
if data.get('Database cluster state') == 'in production':
|
||||
return True
|
||||
|
||||
def promote(self, wait_seconds, on_success=None, access_is_restricted=False):
|
||||
def _pre_promote(self):
|
||||
"""
|
||||
Runs a fencing script after the leader lock is acquired but before the replica is promoted.
|
||||
If the script exits with a non-zero code, promotion does not happen and the leader key is removed from DCS.
|
||||
"""
|
||||
|
||||
cmd = self.config.get('pre_promote')
|
||||
if not cmd:
|
||||
return True
|
||||
|
||||
ret = self.cancellable.call(shlex.split(cmd))
|
||||
if ret is not None:
|
||||
logger.info('pre_promote script `%s` exited with %s', cmd, ret)
|
||||
return ret == 0
|
||||
|
||||
def promote(self, wait_seconds, task, on_success=None, access_is_restricted=False):
|
||||
if self.role == 'master':
|
||||
return True
|
||||
|
||||
ret = self._pre_promote()
|
||||
with task:
|
||||
if task.is_cancelled:
|
||||
return False
|
||||
task.complete(ret)
|
||||
|
||||
if ret is False:
|
||||
return False
|
||||
|
||||
if self.cancellable.is_cancelled:
|
||||
logger.info("PostgreSQL promote cancelled.")
|
||||
return False
|
||||
|
||||
ret = self.pg_ctl('promote', '-W')
|
||||
if ret:
|
||||
self.set_role('master')
|
||||
@@ -738,21 +839,31 @@ class Postgresql(object):
|
||||
ret = self._wait_promote(wait_seconds)
|
||||
return ret
|
||||
|
||||
@staticmethod
|
||||
def _wal_position(is_leader, wal_position, received_location, replayed_location):
|
||||
return wal_position if is_leader else max(received_location or 0, replayed_location or 0)
|
||||
|
||||
def timeline_wal_position(self):
|
||||
# This method could be called from different threads (simultaneously with some other `_query` calls).
|
||||
# If it is called not from main thread we will create a new cursor to execute statement.
|
||||
if current_thread().ident == self.__thread_ident:
|
||||
return (self._cluster_info_state_get('timeline'),
|
||||
self._cluster_info_state_get('wal_position'),
|
||||
self._cluster_info_state_get('pg_control_timeline'))
|
||||
timeline = self._cluster_info_state_get('timeline')
|
||||
wal_position = self._cluster_info_state_get('wal_position')
|
||||
replayed_location = self.replayed_location()
|
||||
received_location = self.received_location()
|
||||
pg_control_timeline = self._cluster_info_state_get('pg_control_timeline')
|
||||
else:
|
||||
with self.connection().cursor() as cursor:
|
||||
cursor.execute(self.cluster_info_query)
|
||||
(timeline, wal_position, replayed_location,
|
||||
received_location, _, pg_control_timeline) = cursor.fetchone()[:6]
|
||||
|
||||
with self.connection().cursor() as cursor:
|
||||
cursor.execute(self.cluster_info_query)
|
||||
return cursor.fetchone()[:3]
|
||||
wal_position = self._wal_position(timeline, wal_position, received_location, replayed_location)
|
||||
return (timeline, wal_position, pg_control_timeline)
|
||||
|
||||
def postmaster_start_time(self):
|
||||
try:
|
||||
query = "SELECT pg_catalog.to_char(pg_catalog.pg_postmaster_start_time(), 'YYYY-MM-DD HH24:MI:SS.MS TZ')"
|
||||
query = "SELECT " + self.POSTMASTER_START_TIME
|
||||
if current_thread().ident == self.__thread_ident:
|
||||
return self.query(query).fetchone()[0]
|
||||
with self.connection().cursor() as cursor:
|
||||
@@ -762,17 +873,59 @@ class Postgresql(object):
|
||||
return None
|
||||
|
||||
def last_operation(self):
|
||||
return str(self._cluster_info_state_get('wal_position'))
|
||||
return str(self._wal_position(self.is_leader(), self._cluster_info_state_get('wal_position'),
|
||||
self.received_location(), self.replayed_location()))
|
||||
|
||||
def configure_server_parameters(self):
|
||||
self._major_version = self.get_major_version()
|
||||
self.config.setup_server_parameters()
|
||||
return True
|
||||
|
||||
def pg_wal_realpath(self):
|
||||
"""Returns a dict containing the symlink (key) and target (value) for the wal directory"""
|
||||
links = {}
|
||||
for pg_wal_dir in ('pg_xlog', 'pg_wal'):
|
||||
pg_wal_path = os.path.join(self._data_dir, pg_wal_dir)
|
||||
if os.path.exists(pg_wal_path) and os.path.islink(pg_wal_path):
|
||||
pg_wal_realpath = os.path.realpath(pg_wal_path)
|
||||
links[pg_wal_path] = pg_wal_realpath
|
||||
return links
|
||||
|
||||
def pg_tblspc_realpaths(self):
|
||||
"""Returns a dict containing the symlink (key) and target (values) for the tablespaces"""
|
||||
links = {}
|
||||
pg_tblsp_dir = os.path.join(self._data_dir, 'pg_tblspc')
|
||||
if os.path.exists(pg_tblsp_dir):
|
||||
for tsdn in os.listdir(pg_tblsp_dir):
|
||||
pg_tsp_path = os.path.join(pg_tblsp_dir, tsdn)
|
||||
if parse_int(tsdn) and os.path.islink(pg_tsp_path):
|
||||
pg_tsp_rpath = os.path.realpath(pg_tsp_path)
|
||||
links[pg_tsp_path] = pg_tsp_rpath
|
||||
return links
|
||||
|
||||
def move_data_directory(self):
|
||||
if os.path.isdir(self._data_dir) and not self.is_running():
|
||||
try:
|
||||
new_name = '{0}_{1}'.format(self._data_dir, time.strftime('%Y-%m-%d-%H-%M-%S'))
|
||||
postfix = time.strftime('%Y-%m-%d-%H-%M-%S')
|
||||
|
||||
# let's see if the wal directory is a symlink, in this case we
|
||||
# should move the target
|
||||
for (source, pg_wal_realpath) in self.pg_wal_realpath().items():
|
||||
logger.info('renaming WAL directory and updating symlink: %s', pg_wal_realpath)
|
||||
new_name = '{0}_{1}'.format(pg_wal_realpath, postfix)
|
||||
os.rename(pg_wal_realpath, new_name)
|
||||
os.unlink(source)
|
||||
os.symlink(source, new_name)
|
||||
|
||||
# Move user defined tablespace directory
|
||||
for (source, pg_tsp_rpath) in self.pg_tblspc_realpaths().items():
|
||||
logger.info('renaming user defined tablespace directory and updating symlink: %s', pg_tsp_rpath)
|
||||
new_name = '{0}_{1}'.format(pg_tsp_rpath, postfix)
|
||||
os.rename(pg_tsp_rpath, new_name)
|
||||
os.unlink(source)
|
||||
os.symlink(source, new_name)
|
||||
|
||||
new_name = '{0}_{1}'.format(self._data_dir, postfix)
|
||||
logger.info('renaming data directory to %s', new_name)
|
||||
os.rename(self._data_dir, new_name)
|
||||
except OSError:
|
||||
@@ -790,105 +943,71 @@ class Postgresql(object):
|
||||
os.remove(self._data_dir)
|
||||
elif os.path.isdir(self._data_dir):
|
||||
|
||||
# let's see if pg_xlog|pg_wal is a symlink, in this case we
|
||||
# let's see if wal directory is a symlink, in this case we
|
||||
# should clean the target
|
||||
for pg_wal_dir in ('pg_xlog', 'pg_wal'):
|
||||
pg_wal_path = os.path.join(self._data_dir, pg_wal_dir)
|
||||
if os.path.exists(pg_wal_path) and os.path.islink(pg_wal_path):
|
||||
pg_wal_realpath = os.path.realpath(pg_wal_path)
|
||||
logger.info('Removing WAL directory: %s', pg_wal_realpath)
|
||||
shutil.rmtree(pg_wal_realpath)
|
||||
for pg_wal_realpath in self.pg_wal_realpath().values():
|
||||
logger.info('Removing WAL directory: %s', pg_wal_realpath)
|
||||
shutil.rmtree(pg_wal_realpath)
|
||||
|
||||
# Remove user defined tablespace directories
|
||||
for pg_tsp_rpath in self.pg_tblspc_realpaths().values():
|
||||
logger.info('Removing user defined tablespace directory: %s', pg_tsp_rpath)
|
||||
shutil.rmtree(pg_tsp_rpath, ignore_errors=True)
|
||||
|
||||
shutil.rmtree(self._data_dir)
|
||||
except (IOError, OSError):
|
||||
logger.exception('Could not remove data directory %s', self._data_dir)
|
||||
self.move_data_directory()
|
||||
|
||||
def pick_synchronous_standby(self, cluster):
|
||||
def _get_synchronous_commit_param(self):
|
||||
return self.query("SHOW synchronous_commit").fetchone()[0]
|
||||
|
||||
def pick_synchronous_standby(self, cluster, sync_node_count=1):
|
||||
"""Finds the best candidate to be the synchronous standby.
|
||||
|
||||
Current synchronous standby is always preferred, unless it has disconnected or does not want to be a
|
||||
synchronous standby any longer.
|
||||
|
||||
:returns tuple of candidate name or None, and bool showing if the member is the active synchronous standby.
|
||||
:returns tuple of candidates list and synchronous standby list.
|
||||
"""
|
||||
current = cluster.sync.sync_standby
|
||||
current = current.lower() if current else current
|
||||
if self._major_version < 90600:
|
||||
sync_node_count = 1
|
||||
members = {m.name.lower(): m for m in cluster.members}
|
||||
candidates = []
|
||||
# Pick candidates based on who has flushed WAL farthest.
|
||||
# TODO: for synchronous_commit = remote_write we actually want to order on write_location
|
||||
sync_nodes = []
|
||||
# Pick candidates based on who has higher replay/remote_write/flush lsn.
|
||||
sync_commit_par = self._get_synchronous_commit_param()
|
||||
sort_col = {'remote_apply': 'replay', 'remote_write': 'write'}.get(sync_commit_par, 'flush')
|
||||
# pg_stat_replication.sync_state has 4 possible states - async, potential, quorum, sync.
|
||||
# Sort clause "ORDER BY sync_state DESC" is to get the result in required order and to keep
|
||||
# the result consistent in case if a synchronous standby member is slowed down OR async node
|
||||
# receiving changes faster than the sync member (very rare but possible). Such cases would
|
||||
# trigger sync standby member swapping frequently and the sort on sync_state desc should
|
||||
# help in keeping the query result consistent.
|
||||
for app_name, state, sync_state in self.query(
|
||||
"SELECT pg_catalog.lower(application_name), state, sync_state"
|
||||
" FROM pg_catalog.pg_stat_replication"
|
||||
" ORDER BY flush_{0} DESC".format(self.lsn_name)):
|
||||
" WHERE state = 'streaming'"
|
||||
" ORDER BY sync_state DESC, {0}_{1} DESC".format(sort_col, self.lsn_name)):
|
||||
member = members.get(app_name)
|
||||
if state != 'streaming' or not member or member.tags.get('nosync', False):
|
||||
if not member or member.tags.get('nosync', False):
|
||||
continue
|
||||
candidates.append(member.name)
|
||||
if sync_state == 'sync':
|
||||
return member.name, True
|
||||
if sync_state == 'potential' and app_name == current:
|
||||
# Prefer current even if not the best one any more to avoid indecisivness and spurious swaps.
|
||||
return cluster.sync.sync_standby, False
|
||||
if sync_state in ('async', 'potential'):
|
||||
candidates.append(member.name)
|
||||
sync_nodes.append(member.name)
|
||||
if len(candidates) >= sync_node_count:
|
||||
break
|
||||
|
||||
if candidates:
|
||||
return candidates[0], False
|
||||
return None, False
|
||||
|
||||
def read_postmaster_opts(self):
|
||||
"""returns the list of option names/values from postgres.opts, Empty dict if read failed or no file"""
|
||||
result = {}
|
||||
try:
|
||||
with open(os.path.join(self._data_dir, 'postmaster.opts')) as f:
|
||||
data = f.read()
|
||||
for opt in data.split('" "'):
|
||||
if '=' in opt and opt.startswith('--'):
|
||||
name, val = opt.split('=', 1)
|
||||
result[name.strip('-')] = val.rstrip('"\n')
|
||||
except IOError:
|
||||
logger.exception('Error when reading postmaster.opts')
|
||||
return result
|
||||
|
||||
def single_user_mode(self, command=None, options=None):
|
||||
"""run a given command in a single-user mode. If the command is empty - then just start and stop"""
|
||||
cmd = [self.pgcommand('postgres'), '--single', '-D', self._data_dir]
|
||||
for opt, val in sorted((options or {}).items()):
|
||||
cmd.extend(['-c', '{0}={1}'.format(opt, val)])
|
||||
# need a database name to connect
|
||||
cmd.append(self._database)
|
||||
return self.cancellable.call(cmd, communicate_input=command)
|
||||
|
||||
def cleanup_archive_status(self):
|
||||
status_dir = os.path.join(self._data_dir, 'pg_' + self.wal_name, 'archive_status')
|
||||
try:
|
||||
for f in os.listdir(status_dir):
|
||||
path = os.path.join(status_dir, f)
|
||||
try:
|
||||
if os.path.islink(path):
|
||||
os.unlink(path)
|
||||
elif os.path.isfile(path):
|
||||
os.remove(path)
|
||||
except OSError:
|
||||
logger.exception('Unable to remove %s', path)
|
||||
except OSError:
|
||||
logger.exception('Unable to list %s', status_dir)
|
||||
|
||||
def fix_cluster_state(self):
|
||||
self.cleanup_archive_status()
|
||||
|
||||
# Start in a single user mode and stop to produce a clean shutdown
|
||||
opts = self.read_postmaster_opts()
|
||||
opts.update({'archive_mode': 'on', 'archive_command': 'false'})
|
||||
self.config.remove_recovery_conf()
|
||||
return self.single_user_mode(options=opts) == 0 or None
|
||||
return candidates, sync_nodes
|
||||
|
||||
def schedule_sanity_checks_after_pause(self):
|
||||
"""
|
||||
After coming out of pause we have to:
|
||||
1. sync replication slots, because it might happen that slots were removed
|
||||
2. get new 'Database system identifier' to make sure that it wasn't changed
|
||||
1. configure server parameters if necessary
|
||||
2. sync replication slots, because it might happen that slots were removed
|
||||
3. get new 'Database system identifier' to make sure that it wasn't changed
|
||||
"""
|
||||
if not self._major_version:
|
||||
self.configure_server_parameters()
|
||||
self.slots_handler.schedule()
|
||||
self._sysid = None
|
||||
|
||||
@@ -98,7 +98,8 @@ class Bootstrap(object):
|
||||
|
||||
def _custom_bootstrap(self, config):
|
||||
self._postgresql.set_state('running custom bootstrap script')
|
||||
params = ['--scope=' + self._postgresql.scope, '--datadir=' + self._postgresql.data_dir]
|
||||
params = [] if config.get('no_params') else ['--scope=' + self._postgresql.scope,
|
||||
'--datadir=' + self._postgresql.data_dir]
|
||||
try:
|
||||
logger.info('Running custom bootstrap script: %s', config['command'])
|
||||
if self._postgresql.cancellable.call(shlex.split(config['command']) + params) != 0:
|
||||
@@ -130,6 +131,7 @@ class Bootstrap(object):
|
||||
r['host'] = 'localhost' # set it to localhost to write into pgpass
|
||||
|
||||
env = self._postgresql.config.write_pgpass(r) if 'password' in r else None
|
||||
env['PGOPTIONS'] = '-c synchronous_commit=local'
|
||||
|
||||
try:
|
||||
ret = self._postgresql.cancellable.call(shlex.split(cmd) + [connstring], env=env)
|
||||
@@ -354,7 +356,8 @@ END;$$""".format(f, rewind['username'])
|
||||
self._running_custom_bootstrap = False
|
||||
# If we don't have custom configuration for pg_hba.conf we need to restore original file
|
||||
if not postgresql.config.get('pg_hba'):
|
||||
os.unlink(postgresql.config.pg_hba_conf)
|
||||
if os.path.exists(postgresql.config.pg_hba_conf):
|
||||
os.unlink(postgresql.config.pg_hba_conf)
|
||||
postgresql.config.restore_configuration_files()
|
||||
postgresql.config.write_postgresql_conf()
|
||||
postgresql.config.replace_pg_ident()
|
||||
@@ -362,7 +365,7 @@ END;$$""".format(f, rewind['username'])
|
||||
# at this point there should be no recovery.conf
|
||||
postgresql.config.remove_recovery_conf()
|
||||
|
||||
if postgresql.config.hba_file and postgresql.config.hba_file != postgresql.config.pg_hba_conf:
|
||||
if postgresql.config.hba_file:
|
||||
postgresql.restart()
|
||||
else:
|
||||
postgresql.config.replace_pg_hba()
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import logging
|
||||
import os
|
||||
import psutil
|
||||
import subprocess
|
||||
|
||||
from patroni.exceptions import PostgresException
|
||||
from patroni.utils import polling_loop
|
||||
from six import string_types
|
||||
from threading import Lock
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -13,6 +11,10 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class CancellableExecutor(object):
|
||||
|
||||
"""
|
||||
There must be only one such process so that AsyncExecutor can easily cancel it.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._process = None
|
||||
self._process_cmd = None
|
||||
@@ -75,16 +77,16 @@ class CancellableSubprocess(CancellableExecutor):
|
||||
for s in ('stdin', 'stdout', 'stderr'):
|
||||
kwargs.pop(s, None)
|
||||
|
||||
communicate_input = 'communicate_input' in kwargs
|
||||
if communicate_input:
|
||||
input_data = kwargs.pop('communicate_input', None)
|
||||
if not isinstance(input_data, string_types):
|
||||
input_data = ''
|
||||
if input_data and input_data[-1] != '\n':
|
||||
input_data += '\n'
|
||||
communicate = kwargs.pop('communicate', None)
|
||||
if isinstance(communicate, dict):
|
||||
input_data = communicate.get('input')
|
||||
if input_data:
|
||||
if input_data[-1] != '\n':
|
||||
input_data += '\n'
|
||||
input_data = input_data.encode('utf-8')
|
||||
kwargs['stdin'] = subprocess.PIPE
|
||||
kwargs['stdout'] = open(os.devnull, 'w')
|
||||
kwargs['stderr'] = subprocess.STDOUT
|
||||
kwargs['stdout'] = subprocess.PIPE
|
||||
kwargs['stderr'] = subprocess.PIPE
|
||||
|
||||
try:
|
||||
with self._lock:
|
||||
@@ -95,10 +97,8 @@ class CancellableSubprocess(CancellableExecutor):
|
||||
started = self._start_process(*args, **kwargs)
|
||||
|
||||
if started:
|
||||
if communicate_input:
|
||||
if input_data:
|
||||
self._process.communicate(input_data)
|
||||
self._process.stdin.close()
|
||||
if isinstance(communicate, dict):
|
||||
communicate['stdout'], communicate['stderr'] = self._process.communicate(input_data)
|
||||
return self._process.wait()
|
||||
finally:
|
||||
with self._lock:
|
||||
@@ -114,16 +114,20 @@ class CancellableSubprocess(CancellableExecutor):
|
||||
with self._lock:
|
||||
return self._is_cancelled
|
||||
|
||||
def cancel(self):
|
||||
def cancel(self, kill=False):
|
||||
with self._lock:
|
||||
self._is_cancelled = True
|
||||
if self._process is None or not self._process.is_running():
|
||||
return
|
||||
|
||||
logger.info('Terminating %s', self._process_cmd)
|
||||
self._process.terminate()
|
||||
|
||||
for _ in polling_loop(10):
|
||||
with self._lock:
|
||||
if self._process is None or not self._process.is_running():
|
||||
return
|
||||
if kill:
|
||||
break
|
||||
|
||||
self._kill_process()
|
||||
|
||||
+102
-62
@@ -6,11 +6,12 @@ import socket
|
||||
import stat
|
||||
import time
|
||||
|
||||
from patroni.exceptions import PatroniException
|
||||
from six.moves.urllib_parse import urlparse, parse_qsl, unquote
|
||||
from urllib3.response import HTTPHeaderDict
|
||||
|
||||
from .validator import CaseInsensitiveDict, recovery_parameters,\
|
||||
transform_postgresql_parameter_value, transform_recovery_parameter_value
|
||||
from ..dcs import slot_name_from_member_name, RemoteMember
|
||||
from ..exceptions import PatroniFatalException
|
||||
from ..utils import compare_values, parse_bool, parse_int, split_host_port, uri, \
|
||||
validate_directory, is_subpath
|
||||
|
||||
@@ -113,17 +114,17 @@ def parse_dsn(value):
|
||||
Very simple equivalent of `psycopg2.extensions.parse_dsn` introduced in 2.7.0.
|
||||
We are not using psycopg2 function in order to remain compatible with 2.5.4+.
|
||||
There is one minor difference though, this function removes `dbname` from the result
|
||||
and sets the sslmode` to `prefer` if it is not present in the connection string.
|
||||
This is necessary to simplify comparison of the old and the new values.
|
||||
and sets the `sslmode`, 'gssencmode', and `channel_binding` to `prefer` if it is not present in
|
||||
the connection string. This is necessary to simplify comparison of the old and the new values.
|
||||
|
||||
>>> r = parse_dsn('postgresql://u%2Fse:pass@:%2f123,[%2Fhost2]/db%2Fsdf?application_name=mya%2Fpp&ssl=true')
|
||||
>>> r == {'application_name': 'mya/pp', 'host': ',/host2', 'sslmode': 'require',\
|
||||
'password': 'pass', 'port': '/123', 'user': 'u/se'}
|
||||
'password': 'pass', 'port': '/123', 'user': 'u/se', 'gssencmode': 'prefer', 'channel_binding': 'prefer'}
|
||||
True
|
||||
>>> r = parse_dsn(" host = 'host' dbname = db\\\\ name requiressl=1 ")
|
||||
>>> r == {'host': 'host', 'sslmode': 'require'}
|
||||
>>> r == {'host': 'host', 'sslmode': 'require', 'gssencmode': 'prefer', 'channel_binding': 'prefer'}
|
||||
True
|
||||
>>> parse_dsn('requiressl = 0\\\\') == {'sslmode': 'prefer'}
|
||||
>>> parse_dsn('requiressl = 0\\\\') == {'sslmode': 'prefer', 'gssencmode': 'prefer', 'channel_binding': 'prefer'}
|
||||
True
|
||||
>>> parse_dsn("host=a foo = '") is None
|
||||
True
|
||||
@@ -147,6 +148,8 @@ def parse_dsn(value):
|
||||
ret.setdefault('sslmode', 'prefer')
|
||||
if 'dbname' in ret:
|
||||
del ret['dbname']
|
||||
ret.setdefault('gssencmode', 'prefer')
|
||||
ret.setdefault('channel_binding', 'prefer')
|
||||
return ret
|
||||
|
||||
|
||||
@@ -252,21 +255,6 @@ class ConfigWriter(object):
|
||||
self.writeline("{0} = '{1}'".format(param, self.escape(value)))
|
||||
|
||||
|
||||
class CaseInsensitiveDict(HTTPHeaderDict):
|
||||
|
||||
def add(self, key, val):
|
||||
self[key] = val
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._container[key.lower()][1]
|
||||
|
||||
def __repr__(self):
|
||||
return str(dict(self.items()))
|
||||
|
||||
def copy(self):
|
||||
return CaseInsensitiveDict(self._container.values())
|
||||
|
||||
|
||||
class ConfigHandler(object):
|
||||
|
||||
# List of parameters which must be always passed to postmaster as command line options
|
||||
@@ -292,6 +280,7 @@ class ConfigHandler(object):
|
||||
'max_connections': (100, lambda v: int(v) >= 25, 90100),
|
||||
'max_wal_senders': (10, lambda v: int(v) >= 3, 90100),
|
||||
'wal_keep_segments': (8, lambda v: int(v) >= 1, 90100),
|
||||
'wal_keep_size': ('128MB', lambda v: parse_int(v, 'MB') >= 16, 130000),
|
||||
'max_prepared_transactions': (0, lambda v: int(v) >= 0, 90100),
|
||||
'max_locks_per_transaction': (64, lambda v: int(v) >= 32, 90100),
|
||||
'track_commit_timestamp': ('off', lambda v: parse_bool(v) is not None, 90500),
|
||||
@@ -300,24 +289,7 @@ class ConfigHandler(object):
|
||||
'wal_log_hints': ('on', lambda _: False, 90400)
|
||||
})
|
||||
|
||||
_RECOVERY_PARAMETERS = {
|
||||
'archive_cleanup_command',
|
||||
'restore_command',
|
||||
'recovery_end_command',
|
||||
'recovery_target',
|
||||
'recovery_target_name',
|
||||
'recovery_target_time',
|
||||
'recovery_target_xid',
|
||||
'recovery_target_lsn',
|
||||
'recovery_target_inclusive',
|
||||
'recovery_target_timeline',
|
||||
'recovery_target_action',
|
||||
'recovery_min_apply_delay',
|
||||
'primary_conninfo',
|
||||
'primary_slot_name',
|
||||
'promote_trigger_file',
|
||||
'trigger_file'
|
||||
}
|
||||
_RECOVERY_PARAMETERS = set(recovery_parameters.keys())
|
||||
|
||||
def __init__(self, postgresql, config):
|
||||
self._postgresql = postgresql
|
||||
@@ -337,8 +309,8 @@ class ConfigHandler(object):
|
||||
self._auto_conf_mtime = None
|
||||
self._pgpass = os.path.abspath(config.get('pgpass') or os.path.join(os.path.expanduser('~'), 'pgpass'))
|
||||
if os.path.exists(self._pgpass) and not os.path.isfile(self._pgpass):
|
||||
raise PatroniException("'{}' exists and it's not a file, check your `postgresql.pgpass` configuration"
|
||||
.format(self._pgpass))
|
||||
raise PatroniFatalException("'{0}' exists and it's not a file, check your `postgresql.pgpass` configuration"
|
||||
.format(self._pgpass))
|
||||
self._passfile = None
|
||||
self._passfile_mtime = None
|
||||
self._synchronous_standby_names = None
|
||||
@@ -374,7 +346,7 @@ class ConfigHandler(object):
|
||||
configuration.append(os.path.basename(self._postgresql_base_conf_name))
|
||||
if not self.hba_file:
|
||||
configuration.append('pg_hba.conf')
|
||||
if not self._server_parameters.get('ident_file'):
|
||||
if not self.ident_file:
|
||||
configuration.append('pg_ident.conf')
|
||||
return configuration
|
||||
|
||||
@@ -419,8 +391,9 @@ class ConfigHandler(object):
|
||||
include = self._config.get('custom_conf') or self._postgresql_base_conf_name
|
||||
f.writeline("include '{0}'\n".format(ConfigWriter.escape(include)))
|
||||
for name, value in sorted((configuration or self._server_parameters).items()):
|
||||
value = transform_postgresql_parameter_value(self._postgresql.major_version, name, value)
|
||||
if (not self._postgresql.bootstrap.running_custom_bootstrap or name != 'hba_file') \
|
||||
and name not in self._RECOVERY_PARAMETERS:
|
||||
and name not in self._RECOVERY_PARAMETERS and value is not None:
|
||||
f.write_param(name, value)
|
||||
# when we are doing custom bootstrap we assume that we don't know superuser password
|
||||
# and in order to be able to change it, we are opening trust access from a certain address
|
||||
@@ -481,7 +454,7 @@ class ConfigHandler(object):
|
||||
:returns: True if pg_ident.conf was rewritten.
|
||||
"""
|
||||
|
||||
if not self._server_parameters.get('ident_file') and self._config.get('pg_ident'):
|
||||
if not self.ident_file and self._config.get('pg_ident'):
|
||||
with ConfigWriter(self._pg_ident_conf) as f:
|
||||
f.writelines(self._config['pg_ident'])
|
||||
return True
|
||||
@@ -492,6 +465,10 @@ class ConfigHandler(object):
|
||||
ret = member.conn_kwargs(self.replication)
|
||||
ret['application_name'] = self._postgresql.name
|
||||
ret.setdefault('sslmode', 'prefer')
|
||||
if self._postgresql.major_version >= 120000:
|
||||
ret.setdefault('gssencmode', 'prefer')
|
||||
if self._postgresql.major_version >= 130000:
|
||||
ret.setdefault('channel_binding', 'prefer')
|
||||
if self._krbsrvname:
|
||||
ret['krbsrvname'] = self._krbsrvname
|
||||
if 'database' in ret:
|
||||
@@ -500,8 +477,9 @@ class ConfigHandler(object):
|
||||
|
||||
def format_dsn(self, params, include_dbname=False):
|
||||
# A list of keywords that can be found in a conninfo string. Follows what is acceptable by libpq
|
||||
keywords = ('dbname', 'user', 'passfile' if params.get('passfile') else 'password', 'host', 'port', 'sslmode',
|
||||
'sslcompression', 'sslcert', 'sslkey', 'sslrootcert', 'sslcrl', 'application_name', 'krbsrvname')
|
||||
keywords = ('dbname', 'user', 'passfile' if params.get('passfile') else 'password', 'host', 'port',
|
||||
'sslmode', 'sslcompression', 'sslcert', 'sslkey', 'sslpassword', 'sslrootcert', 'sslcrl',
|
||||
'application_name', 'krbsrvname', 'gssencmode', 'channel_binding')
|
||||
if include_dbname:
|
||||
params = params.copy()
|
||||
params['dbname'] = params.get('database') or self._postgresql.database
|
||||
@@ -518,6 +496,13 @@ class ConfigHandler(object):
|
||||
if kw not in skip and params.get(kw) is not None)
|
||||
|
||||
def _write_recovery_params(self, fd, recovery_params):
|
||||
if self._postgresql.major_version >= 90500:
|
||||
pause_at_recovery_target = parse_bool(recovery_params.pop('pause_at_recovery_target', None))
|
||||
if pause_at_recovery_target is not None:
|
||||
recovery_params.setdefault('recovery_target_action', 'pause' if pause_at_recovery_target else 'promote')
|
||||
else:
|
||||
if str(recovery_params.pop('recovery_target_action', None)).lower() == 'promote':
|
||||
recovery_params.setdefault('pause_at_recovery_target', 'false')
|
||||
for name, value in sorted(recovery_params.items()):
|
||||
if name == 'primary_conninfo':
|
||||
if 'password' in value and self._postgresql.major_version >= 100000:
|
||||
@@ -525,6 +510,10 @@ class ConfigHandler(object):
|
||||
value['passfile'] = self._passfile = self._pgpass
|
||||
self._passfile_mtime = mtime(self._pgpass)
|
||||
value = self.format_dsn(value)
|
||||
else:
|
||||
value = transform_recovery_parameter_value(self._postgresql.major_version, name, value)
|
||||
if value is None:
|
||||
continue
|
||||
fd.write_param(name, value)
|
||||
|
||||
def build_recovery_params(self, member):
|
||||
@@ -568,7 +557,8 @@ class ConfigHandler(object):
|
||||
|
||||
@property
|
||||
def _recovery_parameters_to_compare(self):
|
||||
skip_params = {'recovery_target_inclusive', 'recovery_target_action', self._triggerfile_wrong_name}
|
||||
skip_params = {'pause_at_recovery_target', 'recovery_target_inclusive',
|
||||
'recovery_target_action', 'standby_mode', self._triggerfile_wrong_name}
|
||||
return self._RECOVERY_PARAMETERS - skip_params
|
||||
|
||||
def _read_recovery_params(self):
|
||||
@@ -614,6 +604,7 @@ class ConfigHandler(object):
|
||||
values[match.group(1)] = [value, True]
|
||||
self._recovery_conf_mtime = recovery_conf_mtime
|
||||
values.setdefault('recovery_min_apply_delay', ['0', True])
|
||||
values['recovery_min_apply_delay'][0] = parse_int(values['recovery_min_apply_delay'][0], 'ms')
|
||||
values.update({param: ['', True] for param in self._recovery_parameters_to_compare if param not in values})
|
||||
return values, True
|
||||
|
||||
@@ -644,6 +635,17 @@ class ConfigHandler(object):
|
||||
elif not primary_conninfo:
|
||||
return False
|
||||
|
||||
wal_receiver_primary_conninfo = self._postgresql.primary_conninfo()
|
||||
if wal_receiver_primary_conninfo:
|
||||
wal_receiver_primary_conninfo = parse_dsn(wal_receiver_primary_conninfo)
|
||||
# when wal receiver is alive use primary_conninfo from pg_stat_wal_receiver for comparison
|
||||
if wal_receiver_primary_conninfo:
|
||||
primary_conninfo = wal_receiver_primary_conninfo
|
||||
# There could be no password in the primary_conninfo or it is masked.
|
||||
# Just copy the "desired" value in order to make comparison succeed.
|
||||
if 'password' in wanted_primary_conninfo:
|
||||
primary_conninfo['password'] = wanted_primary_conninfo['password']
|
||||
|
||||
if 'passfile' in primary_conninfo and 'password' not in primary_conninfo \
|
||||
and 'password' in wanted_primary_conninfo:
|
||||
if self._check_passfile(primary_conninfo['passfile'], wanted_primary_conninfo):
|
||||
@@ -651,7 +653,7 @@ class ConfigHandler(object):
|
||||
else:
|
||||
return False
|
||||
|
||||
return all(primary_conninfo.get(p) == str(v) for p, v in wanted_primary_conninfo.items())
|
||||
return all(primary_conninfo.get(p) == str(v) for p, v in wanted_primary_conninfo.items() if v is not None)
|
||||
|
||||
def check_recovery_conf(self, member):
|
||||
"""Returns a tuple. The first boolean element indicates that recovery params don't match
|
||||
@@ -687,6 +689,13 @@ class ConfigHandler(object):
|
||||
else: # empty string, primary_conninfo is not in the config
|
||||
primary_conninfo[0] = {}
|
||||
|
||||
# when wal receiver is alive take primary_slot_name from pg_stat_wal_receiver
|
||||
wal_receiver_primary_slot_name = self._postgresql.primary_slot_name()
|
||||
if not wal_receiver_primary_slot_name and self._postgresql.primary_conninfo():
|
||||
wal_receiver_primary_slot_name = ''
|
||||
if wal_receiver_primary_slot_name is not None:
|
||||
self._current_recovery_params['primary_slot_name'][0] = wal_receiver_primary_slot_name
|
||||
|
||||
required = {'restart': 0, 'reload': 0}
|
||||
|
||||
def record_missmatch(mtype):
|
||||
@@ -703,6 +712,9 @@ class ConfigHandler(object):
|
||||
if param == 'recovery_min_apply_delay':
|
||||
if not compare_values('integer', 'ms', value[0], wanted_recovery_params.get(param, 0)):
|
||||
record_missmatch(value[1])
|
||||
elif param == 'standby_mode':
|
||||
if not compare_values('bool', None, value[0], wanted_recovery_params.get(param, 'on')):
|
||||
record_missmatch(value[1])
|
||||
elif param == 'primary_conninfo':
|
||||
if not self._check_primary_conninfo(value[0], wanted_recovery_params.get('primary_conninfo', {})):
|
||||
record_missmatch(value[1])
|
||||
@@ -722,7 +734,7 @@ class ConfigHandler(object):
|
||||
def escape(value):
|
||||
return re.sub(r'([:\\])', r'\\\1', str(value))
|
||||
|
||||
record = {n: escape(record.get(n, '*')) for n in ('host', 'port', 'user', 'password')}
|
||||
record = {n: escape(record.get(n) or '*') for n in ('host', 'port', 'user', 'password')}
|
||||
return '{host}:{port}:*:{user}:{password}'.format(**record)
|
||||
|
||||
def write_pgpass(self, record):
|
||||
@@ -804,8 +816,20 @@ class ConfigHandler(object):
|
||||
parameters.pop('synchronous_standby_names', None)
|
||||
else:
|
||||
parameters['synchronous_standby_names'] = self._synchronous_standby_names
|
||||
if self._postgresql.major_version >= 90600 and parameters['wal_level'] == 'hot_standby':
|
||||
parameters['wal_level'] = 'replica'
|
||||
|
||||
# Handle hot_standby <-> replica rename
|
||||
if parameters.get('wal_level') == ('hot_standby' if self._postgresql.major_version >= 90600 else 'replica'):
|
||||
parameters['wal_level'] = 'replica' if self._postgresql.major_version >= 90600 else 'hot_standby'
|
||||
|
||||
# Try to recalcualte wal_keep_segments <-> wal_keep_size assuming that typical wal_segment_size is 16MB.
|
||||
# The real segment size could be estimated from pg_control, but we don't really care, because the only goal of
|
||||
# this exercise is improving cross version compatibility and user must set the correct parameter in the config.
|
||||
if self._postgresql.major_version >= 130000:
|
||||
wal_keep_segments = parameters.pop('wal_keep_segments', self.CMDLINE_OPTIONS['wal_keep_segments'][0])
|
||||
parameters.setdefault('wal_keep_size', str(wal_keep_segments * 16) + 'MB')
|
||||
elif self._postgresql.major_version:
|
||||
wal_keep_size = parse_int(parameters.pop('wal_keep_size', self.CMDLINE_OPTIONS['wal_keep_size'][0]), 'MB')
|
||||
parameters.setdefault('wal_keep_segments', int((wal_keep_size + 8) / 16))
|
||||
ret = CaseInsensitiveDict({k: v for k, v in parameters.items() if not self._postgresql.major_version or
|
||||
self._postgresql.major_version >= self.CMDLINE_OPTIONS.get(k, (0, 1, 90100))[2]})
|
||||
ret.update({k: os.path.join(self._config_dir, ret[k]) for k in ('hba_file', 'ident_file') if k in ret})
|
||||
@@ -937,10 +961,12 @@ class ConfigHandler(object):
|
||||
logger.warning('Removing invalid parameter `%s` from postgresql.parameters', param)
|
||||
server_parameters.pop(param)
|
||||
|
||||
if not server_parameters.get('hba_file') and config.get('pg_hba'):
|
||||
if (not server_parameters.get('hba_file') or server_parameters['hba_file'] == self._pg_hba_conf) \
|
||||
and config.get('pg_hba'):
|
||||
hba_changed = self._config.get('pg_hba', []) != config['pg_hba']
|
||||
|
||||
if not server_parameters.get('ident_file') and config.get('pg_ident'):
|
||||
if (not server_parameters.get('ident_file') or server_parameters['ident_file'] == self._pg_hba_conf) \
|
||||
and config.get('pg_ident'):
|
||||
ident_changed = self._config.get('pg_ident', []) != config['pg_ident']
|
||||
|
||||
self._config = config
|
||||
@@ -979,16 +1005,20 @@ class ConfigHandler(object):
|
||||
else:
|
||||
logger.info('No PostgreSQL configuration items changed, nothing to reload.')
|
||||
|
||||
def set_synchronous_standby(self, name):
|
||||
def set_synchronous_standby(self, sync_members):
|
||||
"""Sets a node to be synchronous standby and if changed does a reload for PostgreSQL."""
|
||||
if name and name != '*':
|
||||
name = quote_ident(name)
|
||||
if name != self._synchronous_standby_names:
|
||||
if name is None:
|
||||
if sync_members and sync_members != ['*']:
|
||||
sync_members = [quote_ident(x) for x in sync_members]
|
||||
if self._postgresql.major_version >= 90600 and len(sync_members) > 1:
|
||||
sync_param = '{0} ({1})'.format(len(sync_members), ','.join(sync_members))
|
||||
else:
|
||||
sync_param = next(iter(sync_members), None)
|
||||
if sync_param != self._synchronous_standby_names:
|
||||
if sync_param is None:
|
||||
self._server_parameters.pop('synchronous_standby_names', None)
|
||||
else:
|
||||
self._server_parameters['synchronous_standby_names'] = name
|
||||
self._synchronous_standby_names = name
|
||||
self._server_parameters['synchronous_standby_names'] = sync_param
|
||||
self._synchronous_standby_names = sync_param
|
||||
if self._postgresql.state == 'running':
|
||||
self.write_postgresql_conf()
|
||||
self._postgresql.reload()
|
||||
@@ -1024,6 +1054,10 @@ class ConfigHandler(object):
|
||||
|
||||
for name, cname in options_mapping.items():
|
||||
value = parse_int(effective_configuration[name])
|
||||
if cname not in data:
|
||||
logger.warning('%s is missing from pg_controldata output', cname)
|
||||
continue
|
||||
|
||||
cvalue = parse_int(data[cname])
|
||||
if cvalue > value:
|
||||
effective_configuration[name] = cvalue
|
||||
@@ -1043,9 +1077,15 @@ class ConfigHandler(object):
|
||||
return self._config['authentication'].get('rewind', self._superuser) \
|
||||
if self._postgresql.major_version >= 110000 else self._superuser
|
||||
|
||||
@property
|
||||
def ident_file(self):
|
||||
ident_file = self._server_parameters.get('ident_file')
|
||||
return None if ident_file == self._pg_ident_conf else ident_file
|
||||
|
||||
@property
|
||||
def hba_file(self):
|
||||
return self._server_parameters.get('hba_file')
|
||||
hba_file = self._server_parameters.get('hba_file')
|
||||
return None if hba_file == self._pg_hba_conf else hba_file
|
||||
|
||||
@property
|
||||
def pg_hba_conf(self):
|
||||
|
||||
@@ -7,7 +7,7 @@ import signal
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from patroni import PATRONI_ENV_PREFIX
|
||||
from patroni import PATRONI_ENV_PREFIX, KUBERNETES_ENV_PREFIX
|
||||
|
||||
# avoid spawning the resource tracker process
|
||||
if sys.version_info >= (3, 8): # pragma: no cover
|
||||
@@ -105,6 +105,42 @@ class PostmasterProcess(psutil.Process):
|
||||
except psutil.NoSuchProcess:
|
||||
return None
|
||||
|
||||
def signal_kill(self):
|
||||
"""to suspend and kill postmaster and all children
|
||||
|
||||
:returns True if postmaster and children are killed, False if error
|
||||
"""
|
||||
try:
|
||||
self.suspend()
|
||||
except psutil.NoSuchProcess:
|
||||
return True
|
||||
except psutil.Error as e:
|
||||
logger.warning('Failed to suspend postmaster: %s', e)
|
||||
|
||||
try:
|
||||
children = self.children(recursive=True)
|
||||
except psutil.NoSuchProcess:
|
||||
return True
|
||||
except psutil.Error as e:
|
||||
logger.warning('Failed to get a list of postmaster children: %s', e)
|
||||
children = []
|
||||
|
||||
try:
|
||||
self.kill()
|
||||
except psutil.NoSuchProcess:
|
||||
return True
|
||||
except psutil.Error as e:
|
||||
logger.warning('Could not kill postmaster: %s', e)
|
||||
return False
|
||||
|
||||
for child in children:
|
||||
try:
|
||||
child.kill()
|
||||
except psutil.Error:
|
||||
pass
|
||||
psutil.wait_procs(children + [self])
|
||||
return True
|
||||
|
||||
def signal_stop(self, mode, pg_ctl='pg_ctl'):
|
||||
"""Signal postmaster process to stop
|
||||
|
||||
@@ -176,7 +212,8 @@ class PostmasterProcess(psutil.Process):
|
||||
# In order to make everything portable we can't use fork&exec approach here, so we will call
|
||||
# ourselves and pass list of arguments which must be used to start postgres.
|
||||
# On Windows, in order to run a side-by-side assembly the specified env must include a valid SYSTEMROOT.
|
||||
env = {p: os.environ[p] for p in os.environ if not p.startswith(PATRONI_ENV_PREFIX)}
|
||||
env = {p: os.environ[p] for p in os.environ if not p.startswith(
|
||||
PATRONI_ENV_PREFIX) and not p.startswith(KUBERNETES_ENV_PREFIX)}
|
||||
try:
|
||||
proc = PostmasterProcess._from_pidfile(data_dir)
|
||||
if proc and not proc._is_postmaster_process():
|
||||
|
||||
+255
-29
@@ -1,10 +1,15 @@
|
||||
import logging
|
||||
import os
|
||||
import shlex
|
||||
import six
|
||||
import subprocess
|
||||
|
||||
from patroni.dcs import Leader
|
||||
from patroni.postgresql.connection import get_connection_cursor
|
||||
from patroni.postgresql.misc import parse_history, parse_lsn
|
||||
from threading import Lock, Thread
|
||||
|
||||
from .connection import get_connection_cursor
|
||||
from .misc import parse_history, parse_lsn
|
||||
from ..async_executor import CriticalTask
|
||||
from ..dcs import Leader
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -12,10 +17,16 @@ REWIND_STATUS = type('Enum', (), {'INITIAL': 0, 'CHECKPOINT': 1, 'CHECK': 2, 'NE
|
||||
'NOT_NEED': 4, 'SUCCESS': 5, 'FAILED': 6})
|
||||
|
||||
|
||||
def format_lsn(lsn, full=False):
|
||||
template = '{0:X}/{1:08X}' if full else '{0:X}/{1:X}'
|
||||
return template.format(lsn >> 32, lsn & 0xFFFFFFFF)
|
||||
|
||||
|
||||
class Rewind(object):
|
||||
|
||||
def __init__(self, postgresql):
|
||||
self._postgresql = postgresql
|
||||
self._checkpoint_task_lock = Lock()
|
||||
self.reset_state()
|
||||
|
||||
@staticmethod
|
||||
@@ -60,32 +71,102 @@ class Rewind(object):
|
||||
except Exception:
|
||||
return logger.exception('Exception when working with leader')
|
||||
|
||||
def _get_checkpoint_end(self, timeline, lsn):
|
||||
"""The checkpoint record size in WAL depends on postgres major version and platform (memory alignment).
|
||||
Hence, the only reliable way to figure out where it ends, read the record from file with the help of pg_waldump
|
||||
and parse the output. We are trying to read two records, and expect that it wil fail to read the second one:
|
||||
`pg_waldump: fatal: error in WAL record at 0/182E220: invalid record length at 0/182E298: wanted 24, got 0`
|
||||
The error message contains information about LSN of the next record, which is exactly where checkpoint ends."""
|
||||
|
||||
cmd = self._postgresql.pgcommand('pg_{0}dump'.format(self._postgresql.wal_name))
|
||||
lsn8 = format_lsn(lsn, True)
|
||||
lsn = format_lsn(lsn)
|
||||
env = os.environ.copy()
|
||||
env.update(LANG='C', LC_ALL='C', PGDATA=self._postgresql.data_dir)
|
||||
try:
|
||||
waldump = subprocess.Popen([cmd, '-t', str(timeline), '-s', lsn, '-n', '2'],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
|
||||
out, err = waldump.communicate()
|
||||
waldump.wait()
|
||||
except Exception as e:
|
||||
logger.error('Failed to execute `%s -t %s -s %s -n 2`: %r', cmd, timeline, lsn, e)
|
||||
else:
|
||||
out = out.decode('utf-8').rstrip().split('\n')
|
||||
err = err.decode('utf-8').rstrip().split('\n')
|
||||
pattern = 'error in WAL record at {0}: invalid record length at '.format(lsn)
|
||||
|
||||
if len(out) == 1 and len(err) == 1 and ', lsn: {0}, prev '.format(lsn8) in out[0] and pattern in err[0]:
|
||||
i = err[0].find(pattern) + len(pattern)
|
||||
j = err[0].find(": wanted ", i)
|
||||
if j > -1:
|
||||
try:
|
||||
return parse_lsn(err[0][i:j])
|
||||
except Exception as e:
|
||||
logger.error('Failed to parse lsn %s: %r', err[0][i:j], e)
|
||||
logger.error('Failed to parse `%s -t %s -s %s -n 2` output', cmd, timeline, lsn)
|
||||
logger.error(' stdout=%s', '\n'.join(out))
|
||||
logger.error(' stderr=%s', '\n'.join(err))
|
||||
|
||||
return 0
|
||||
|
||||
def _get_local_timeline_lsn_from_controldata(self):
|
||||
timeline = lsn = None
|
||||
in_recovery = timeline = lsn = None
|
||||
data = self._postgresql.controldata()
|
||||
try:
|
||||
if data.get('Database cluster state') == 'shut down in recovery':
|
||||
in_recovery = True
|
||||
lsn = data.get('Minimum recovery ending location')
|
||||
timeline = int(data.get("Min recovery ending loc's timeline"))
|
||||
if lsn == '0/0' or timeline == 0: # it was a master when it crashed
|
||||
data['Database cluster state'] = 'shut down'
|
||||
if data.get('Database cluster state') == 'shut down':
|
||||
in_recovery = False
|
||||
lsn = data.get('Latest checkpoint location')
|
||||
timeline = int(data.get("Latest checkpoint's TimeLineID"))
|
||||
except (TypeError, ValueError):
|
||||
logger.exception('Failed to get local timeline and lsn from pg_controldata output')
|
||||
return timeline, lsn
|
||||
|
||||
if lsn is not None:
|
||||
try:
|
||||
lsn = parse_lsn(lsn)
|
||||
except (IndexError, ValueError) as e:
|
||||
logger.error('Exception when parsing lsn %s: %r', lsn, e)
|
||||
lsn = None
|
||||
|
||||
return in_recovery, timeline, lsn
|
||||
|
||||
def _get_local_timeline_lsn(self):
|
||||
if self._postgresql.is_running(): # if postgres is running - get timeline and lsn from replication connection
|
||||
timeline, lsn = self._postgresql.get_local_timeline_lsn_from_replication_connection()
|
||||
if self._postgresql.is_running(): # if postgres is running - get timeline from replication connection
|
||||
in_recovery = True
|
||||
timeline = self._postgresql.received_timeline() or self._postgresql.get_replica_timeline()
|
||||
lsn = self._postgresql.replayed_location()
|
||||
else: # otherwise analyze pg_controldata output
|
||||
timeline, lsn = self._get_local_timeline_lsn_from_controldata()
|
||||
logger.info('Local timeline=%s lsn=%s', timeline, lsn)
|
||||
return timeline, lsn
|
||||
in_recovery, timeline, lsn = self._get_local_timeline_lsn_from_controldata()
|
||||
|
||||
log_lsn = format_lsn(lsn) if isinstance(lsn, six.integer_types) else lsn
|
||||
logger.info('Local timeline=%s lsn=%s', timeline, log_lsn)
|
||||
return in_recovery, timeline, lsn
|
||||
|
||||
@staticmethod
|
||||
def _log_master_history(history, i):
|
||||
start = max(0, i - 3)
|
||||
end = None if i + 4 >= len(history) else i + 2
|
||||
history_show = []
|
||||
|
||||
def format_history_line(line):
|
||||
return '{0}\t{1}\t{2}'.format(line[0], format_lsn(line[1]), line[2])
|
||||
|
||||
for line in history[start:end]:
|
||||
history_show.append(format_history_line(line))
|
||||
|
||||
if line != history[-1]:
|
||||
history_show.append('...')
|
||||
history_show.append(format_history_line(history[-1]))
|
||||
|
||||
logger.info('master: history=%s', '\n'.join(history_show))
|
||||
|
||||
def _check_timeline_and_lsn(self, leader):
|
||||
local_timeline, local_lsn = self._get_local_timeline_lsn()
|
||||
in_recovery, local_timeline, local_lsn = self._get_local_timeline_lsn()
|
||||
if local_timeline is None or local_lsn is None:
|
||||
return
|
||||
|
||||
@@ -104,25 +185,32 @@ class Rewind(object):
|
||||
logger.info('master_timeline=%s', master_timeline)
|
||||
if local_timeline > master_timeline: # Not always supported by pg_rewind
|
||||
need_rewind = True
|
||||
elif local_timeline == master_timeline:
|
||||
need_rewind = False
|
||||
elif master_timeline > 1:
|
||||
cur.execute('TIMELINE_HISTORY %s', (master_timeline,))
|
||||
history = bytes(cur.fetchone()[1]).decode('utf-8')
|
||||
logger.info('master: history=%s', history)
|
||||
else: # local_timeline == master_timeline == 1
|
||||
need_rewind = False
|
||||
logger.debug('master: history=%s', history)
|
||||
except Exception:
|
||||
return logger.exception('Exception when working with master via replication connection')
|
||||
|
||||
if history is not None:
|
||||
for parent_timeline, switchpoint, _ in parse_history(history):
|
||||
history = list(parse_history(history))
|
||||
for i, (parent_timeline, switchpoint, _) in enumerate(history):
|
||||
if parent_timeline == local_timeline:
|
||||
try:
|
||||
need_rewind = parse_lsn(local_lsn) >= switchpoint
|
||||
except (IndexError, ValueError):
|
||||
logger.exception('Exception when parsing lsn')
|
||||
# We don't need to rewind when:
|
||||
# 1. for replica: replayed location is not ahead of switchpoint
|
||||
# 2. for the former primary: end of checkpoint record is the same as switchpoint
|
||||
if in_recovery:
|
||||
need_rewind = local_lsn > switchpoint
|
||||
elif local_lsn >= switchpoint:
|
||||
need_rewind = True
|
||||
else:
|
||||
need_rewind = switchpoint != self._get_checkpoint_end(local_timeline, local_lsn)
|
||||
break
|
||||
elif parent_timeline > local_timeline:
|
||||
break
|
||||
self._log_master_history(history, i)
|
||||
|
||||
self._state = need_rewind and REWIND_STATUS.NEED or REWIND_STATUS.NOT_NEED
|
||||
|
||||
@@ -131,25 +219,114 @@ class Rewind(object):
|
||||
self._check_timeline_and_lsn(leader)
|
||||
return leader and leader.conn_url and self._state == REWIND_STATUS.NEED
|
||||
|
||||
def check_for_checkpoint_after_promote(self):
|
||||
if self._state == REWIND_STATUS.INITIAL and self._postgresql.is_leader() and \
|
||||
self._postgresql.get_master_timeline() == self._postgresql.pg_control_timeline():
|
||||
self._state = REWIND_STATUS.CHECKPOINT
|
||||
def __checkpoint(self, task, wakeup):
|
||||
try:
|
||||
result = self._postgresql.checkpoint()
|
||||
except Exception as e:
|
||||
result = 'Exception: ' + str(e)
|
||||
with task:
|
||||
task.complete(not bool(result))
|
||||
if task.result:
|
||||
wakeup()
|
||||
|
||||
def ensure_checkpoint_after_promote(self, wakeup):
|
||||
"""After promote issue a CHECKPOINT from a new thread and asynchronously check the result.
|
||||
In case if CHECKPOINT failed, just check that timeline in pg_control was updated."""
|
||||
|
||||
if self._state == REWIND_STATUS.INITIAL and self._postgresql.is_leader():
|
||||
with self._checkpoint_task_lock:
|
||||
if self._checkpoint_task:
|
||||
with self._checkpoint_task:
|
||||
if self._checkpoint_task.result:
|
||||
self._state = REWIND_STATUS.CHECKPOINT
|
||||
if self._checkpoint_task.result is not False:
|
||||
return
|
||||
else:
|
||||
self._checkpoint_task = CriticalTask()
|
||||
return Thread(target=self.__checkpoint, args=(self._checkpoint_task, wakeup)).start()
|
||||
|
||||
if self._postgresql.get_master_timeline() == self._postgresql.pg_control_timeline():
|
||||
self._state = REWIND_STATUS.CHECKPOINT
|
||||
|
||||
def checkpoint_after_promote(self):
|
||||
return self._state == REWIND_STATUS.CHECKPOINT
|
||||
|
||||
def _fetch_missing_wal(self, restore_command, wal_filename):
|
||||
cmd = ''
|
||||
length = len(restore_command)
|
||||
i = 0
|
||||
while i < length:
|
||||
if restore_command[i] == '%' and i + 1 < length:
|
||||
i += 1
|
||||
if restore_command[i] == 'p':
|
||||
cmd += os.path.join(self._postgresql.wal_dir, wal_filename)
|
||||
elif restore_command[i] == 'f':
|
||||
cmd += wal_filename
|
||||
elif restore_command[i] == 'r':
|
||||
cmd += '000000010000000000000001'
|
||||
elif restore_command[i] == '%':
|
||||
cmd += '%'
|
||||
else:
|
||||
cmd += '%'
|
||||
i -= 1
|
||||
else:
|
||||
cmd += restore_command[i]
|
||||
i += 1
|
||||
|
||||
logger.info('Trying to fetch the missing wal: %s', cmd)
|
||||
return self._postgresql.cancellable.call(shlex.split(cmd)) == 0
|
||||
|
||||
def _find_missing_wal(self, data):
|
||||
# could not open file "$PGDATA/pg_wal/0000000A00006AA100000068": No such file or directory
|
||||
pattern = 'could not open file "'
|
||||
for line in data.decode('utf-8').split('\n'):
|
||||
b = line.find(pattern)
|
||||
if b > -1:
|
||||
b += len(pattern)
|
||||
e = line.find('": ', b)
|
||||
if e > -1 and '/' in line[b:e]:
|
||||
waldir, wal_filename = line[b:e].rsplit('/', 1)
|
||||
if waldir.endswith('/pg_' + self._postgresql.wal_name) and len(wal_filename) == 24:
|
||||
return wal_filename
|
||||
|
||||
def pg_rewind(self, r):
|
||||
# prepare pg_rewind connection
|
||||
env = self._postgresql.config.write_pgpass(r)
|
||||
env['PGOPTIONS'] = '-c statement_timeout=0'
|
||||
env.update(LANG='C', LC_ALL='C', PGOPTIONS='-c statement_timeout=0')
|
||||
dsn = self._postgresql.config.format_dsn(r, True)
|
||||
logger.info('running pg_rewind from %s', dsn)
|
||||
try:
|
||||
return self._postgresql.cancellable.call([self._postgresql.pgcommand('pg_rewind'), '-D',
|
||||
self._postgresql.data_dir, '--source-server', dsn], env=env) == 0
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
restore_command = self._postgresql.config.get('recovery_conf', {}).get('restore_command') \
|
||||
if self._postgresql.major_version < 120000 else self._postgresql.get_guc_value('restore_command')
|
||||
|
||||
cmd = [self._postgresql.pgcommand('pg_rewind')]
|
||||
if self._postgresql.major_version >= 130000 and restore_command:
|
||||
cmd.append('--restore-target-wal')
|
||||
cmd.extend(['-D', self._postgresql.data_dir, '--source-server', dsn])
|
||||
|
||||
while True:
|
||||
results = {}
|
||||
ret = self._postgresql.cancellable.call(cmd, env=env, communicate=results)
|
||||
|
||||
logger.info('pg_rewind exit code=%s', ret)
|
||||
if ret is None:
|
||||
return False
|
||||
|
||||
logger.info(' stdout=%s', results['stdout'].decode('utf-8'))
|
||||
logger.info(' stderr=%s', results['stderr'].decode('utf-8'))
|
||||
if ret == 0:
|
||||
return True
|
||||
|
||||
if not restore_command or self._postgresql.major_version >= 130000:
|
||||
return False
|
||||
|
||||
missing_wal = self._find_missing_wal(results['stderr']) or self._find_missing_wal(results['stdout'])
|
||||
if not missing_wal:
|
||||
return False
|
||||
|
||||
if not self._fetch_missing_wal(restore_command, missing_wal):
|
||||
logger.info('Failed to fetch WAL segment %s required for pg_rewind', missing_wal)
|
||||
return False
|
||||
|
||||
def execute(self, leader):
|
||||
if self._postgresql.is_running() and not self._postgresql.stop(checkpoint=False):
|
||||
@@ -191,6 +368,8 @@ class Rewind(object):
|
||||
|
||||
def reset_state(self):
|
||||
self._state = REWIND_STATUS.INITIAL
|
||||
with self._checkpoint_task_lock:
|
||||
self._checkpoint_task = None
|
||||
|
||||
@property
|
||||
def is_needed(self):
|
||||
@@ -203,3 +382,50 @@ class Rewind(object):
|
||||
@property
|
||||
def failed(self):
|
||||
return self._state == REWIND_STATUS.FAILED
|
||||
|
||||
def read_postmaster_opts(self):
|
||||
"""returns the list of option names/values from postgres.opts, Empty dict if read failed or no file"""
|
||||
result = {}
|
||||
try:
|
||||
with open(os.path.join(self._postgresql.data_dir, 'postmaster.opts')) as f:
|
||||
data = f.read()
|
||||
for opt in data.split('" "'):
|
||||
if '=' in opt and opt.startswith('--'):
|
||||
name, val = opt.split('=', 1)
|
||||
result[name.strip('-')] = val.rstrip('"\n')
|
||||
except IOError:
|
||||
logger.exception('Error when reading postmaster.opts')
|
||||
return result
|
||||
|
||||
def single_user_mode(self, communicate=None, options=None):
|
||||
"""run a given command in a single-user mode. If the command is empty - then just start and stop"""
|
||||
cmd = [self._postgresql.pgcommand('postgres'), '--single', '-D', self._postgresql.data_dir]
|
||||
for opt, val in sorted((options or {}).items()):
|
||||
cmd.extend(['-c', '{0}={1}'.format(opt, val)])
|
||||
# need a database name to connect
|
||||
cmd.append('template1')
|
||||
return self._postgresql.cancellable.call(cmd, communicate=communicate)
|
||||
|
||||
def cleanup_archive_status(self):
|
||||
status_dir = os.path.join(self._postgresql.wal_dir, 'archive_status')
|
||||
try:
|
||||
for f in os.listdir(status_dir):
|
||||
path = os.path.join(status_dir, f)
|
||||
try:
|
||||
if os.path.islink(path):
|
||||
os.unlink(path)
|
||||
elif os.path.isfile(path):
|
||||
os.remove(path)
|
||||
except OSError:
|
||||
logger.exception('Unable to remove %s', path)
|
||||
except OSError:
|
||||
logger.exception('Unable to list %s', status_dir)
|
||||
|
||||
def ensure_clean_shutdown(self):
|
||||
self.cleanup_archive_status()
|
||||
|
||||
# Start in a single user mode and stop to produce a clean shutdown
|
||||
opts = self.read_postmaster_opts()
|
||||
opts.update({'archive_mode': 'on', 'archive_command': 'false'})
|
||||
self._postgresql.config.remove_recovery_conf()
|
||||
return self.single_user_mode(communicate={}, options=opts) == 0 or None
|
||||
|
||||
@@ -0,0 +1,518 @@
|
||||
import abc
|
||||
import logging
|
||||
import six
|
||||
|
||||
from collections import namedtuple
|
||||
from urllib3.response import HTTPHeaderDict
|
||||
|
||||
from ..utils import parse_bool, parse_int, parse_real
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CaseInsensitiveDict(HTTPHeaderDict):
|
||||
|
||||
def add(self, key, val):
|
||||
self[key] = val
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._container[key.lower()][1]
|
||||
|
||||
def __repr__(self):
|
||||
return str(dict(self.items()))
|
||||
|
||||
def copy(self):
|
||||
return CaseInsensitiveDict(self._container.values())
|
||||
|
||||
|
||||
class Bool(namedtuple('Bool', 'version_from,version_till')):
|
||||
|
||||
@staticmethod
|
||||
def transform(name, value):
|
||||
if parse_bool(value) is not None:
|
||||
return value
|
||||
logger.warning('Removing bool parameter=%s from the config due to the invalid value=%s', name, value)
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
class Number(namedtuple('Number', 'version_from,version_till,min_val,max_val,unit')):
|
||||
|
||||
@staticmethod
|
||||
@abc.abstractmethod
|
||||
def parse(value, unit):
|
||||
"""parse value"""
|
||||
|
||||
def transform(self, name, value):
|
||||
num_value = self.parse(value, self.unit)
|
||||
if num_value is not None:
|
||||
if num_value < self.min_val:
|
||||
logger.warning('Value=%s of parameter=%s is too low, increasing to %s%s',
|
||||
value, name, self.min_val, self.unit or '')
|
||||
return self.min_val
|
||||
if num_value > self.max_val:
|
||||
logger.warning('Value=%s of parameter=%s is too big, decreasing to %s%s',
|
||||
value, name, self.max_val, self.unit or '')
|
||||
return self.max_val
|
||||
return value
|
||||
logger.warning('Removing %s parameter=%s from the config due to the invalid value=%s',
|
||||
self.__class__.__name__.lower(), name, value)
|
||||
|
||||
|
||||
class Integer(Number):
|
||||
|
||||
@staticmethod
|
||||
def parse(value, unit):
|
||||
return parse_int(value, unit)
|
||||
|
||||
|
||||
class Real(Number):
|
||||
|
||||
@staticmethod
|
||||
def parse(value, unit):
|
||||
return parse_real(value, unit)
|
||||
|
||||
|
||||
class Enum(namedtuple('Enum', 'version_from,version_till,possible_values')):
|
||||
|
||||
def transform(self, name, value):
|
||||
if str(value).lower() in self.possible_values:
|
||||
return value
|
||||
logger.warning('Removing enum parameter=%s from the config due to the invalid value=%s', name, value)
|
||||
|
||||
|
||||
class EnumBool(Enum):
|
||||
|
||||
def transform(self, name, value):
|
||||
if parse_bool(value) is not None:
|
||||
return value
|
||||
return super(EnumBool, self).transform(name, value)
|
||||
|
||||
|
||||
class String(namedtuple('String', 'version_from,version_till')):
|
||||
|
||||
@staticmethod
|
||||
def transform(name, value):
|
||||
return value
|
||||
|
||||
|
||||
# Format:
|
||||
# key - parameter name
|
||||
# value - tuple or multiple tuples if something was changing in GUC across postgres versions
|
||||
parameters = CaseInsensitiveDict({
|
||||
'allow_system_table_mods': Bool(90300, None),
|
||||
'application_name': String(90300, None),
|
||||
'archive_command': String(90300, None),
|
||||
'archive_mode': (
|
||||
Bool(90300, 90500),
|
||||
EnumBool(90500, None, ('always',))
|
||||
),
|
||||
'archive_timeout': Integer(90300, None, 0, 1073741823, 's'),
|
||||
'array_nulls': Bool(90300, None),
|
||||
'authentication_timeout': Integer(90300, None, 1, 600, 's'),
|
||||
'autovacuum': Bool(90300, None),
|
||||
'autovacuum_analyze_scale_factor': Real(90300, None, 0, 100, None),
|
||||
'autovacuum_analyze_threshold': Integer(90300, None, 0, 2147483647, None),
|
||||
'autovacuum_freeze_max_age': Integer(90300, None, 100000, 2000000000, None),
|
||||
'autovacuum_max_workers': (
|
||||
Integer(90300, 90600, 1, 8388607, None),
|
||||
Integer(90600, None, 1, 262143, None)
|
||||
),
|
||||
'autovacuum_multixact_freeze_max_age': Integer(90300, None, 10000, 2000000000, None),
|
||||
'autovacuum_naptime': Integer(90300, None, 1, 2147483, 's'),
|
||||
'autovacuum_vacuum_cost_delay': (
|
||||
Integer(90300, 120000, -1, 100, 'ms'),
|
||||
Real(120000, None, -1, 100, 'ms')
|
||||
),
|
||||
'autovacuum_vacuum_cost_limit': Integer(90300, None, -1, 10000, None),
|
||||
'autovacuum_vacuum_insert_scale_factor': Real(130000, None, 0, 100, None),
|
||||
'autovacuum_vacuum_insert_threshold': Integer(130000, None, -1, 2147483647, None),
|
||||
'autovacuum_vacuum_scale_factor': Real(90300, None, 0, 100, None),
|
||||
'autovacuum_vacuum_threshold': Integer(90300, None, 0, 2147483647, None),
|
||||
'autovacuum_work_mem': Integer(90400, None, -1, 2147483647, 'kB'),
|
||||
'backend_flush_after': Integer(90600, None, 0, 256, '8kB'),
|
||||
'backslash_quote': EnumBool(90300, None, ('safe_encoding',)),
|
||||
'backtrace_functions': String(130000, None),
|
||||
'bgwriter_delay': Integer(90300, None, 10, 10000, 'ms'),
|
||||
'bgwriter_flush_after': Integer(90600, None, 0, 256, '8kB'),
|
||||
'bgwriter_lru_maxpages': (
|
||||
Integer(90300, 100000, 0, 1000, None),
|
||||
Integer(100000, None, 0, 1073741823, None)
|
||||
),
|
||||
'bgwriter_lru_multiplier': Real(90300, None, 0, 10, None),
|
||||
'bonjour': Bool(90300, None),
|
||||
'bonjour_name': String(90300, None),
|
||||
'bytea_output': Enum(90300, None, ('escape', 'hex')),
|
||||
'check_function_bodies': Bool(90300, None),
|
||||
'checkpoint_completion_target': Real(90300, None, 0, 1, None),
|
||||
'checkpoint_flush_after': Integer(90600, None, 0, 256, '8kB'),
|
||||
'checkpoint_segments': Integer(90300, 90500, 1, 2147483647, None),
|
||||
'checkpoint_timeout': (
|
||||
Integer(90300, 90600, 30, 3600, 's'),
|
||||
Integer(90600, None, 30, 86400, 's')
|
||||
),
|
||||
'checkpoint_warning': Integer(90300, None, 0, 2147483647, 's'),
|
||||
'client_encoding': String(90300, None),
|
||||
'client_min_messages': Enum(90300, None, ('debug5', 'debug4', 'debug3', 'debug2',
|
||||
'debug1', 'log', 'notice', 'warning', 'error')),
|
||||
'cluster_name': String(90500, None),
|
||||
'commit_delay': Integer(90300, None, 0, 100000, None),
|
||||
'commit_siblings': Integer(90300, None, 0, 1000, None),
|
||||
'config_file': String(90300, None),
|
||||
'constraint_exclusion': EnumBool(90300, None, ('partition',)),
|
||||
'cpu_index_tuple_cost': Real(90300, None, 0, 1.79769e+308, None),
|
||||
'cpu_operator_cost': Real(90300, None, 0, 1.79769e+308, None),
|
||||
'cpu_tuple_cost': Real(90300, None, 0, 1.79769e+308, None),
|
||||
'cursor_tuple_fraction': Real(90300, None, 0, 1, None),
|
||||
'data_directory': String(90300, None),
|
||||
'data_sync_retry': Bool(90400, None),
|
||||
'DateStyle': String(90300, None),
|
||||
'db_user_namespace': Bool(90300, None),
|
||||
'deadlock_timeout': Integer(90300, None, 1, 2147483647, 'ms'),
|
||||
'debug_pretty_print': Bool(90300, None),
|
||||
'debug_print_parse': Bool(90300, None),
|
||||
'debug_print_plan': Bool(90300, None),
|
||||
'debug_print_rewritten': Bool(90300, None),
|
||||
'default_statistics_target': Integer(90300, None, 1, 10000, None),
|
||||
'default_table_access_method': String(120000, None),
|
||||
'default_tablespace': String(90300, None),
|
||||
'default_text_search_config': String(90300, None),
|
||||
'default_transaction_deferrable': Bool(90300, None),
|
||||
'default_transaction_isolation': Enum(90300, None, ('serializable', 'repeatable read',
|
||||
'read committed', 'read uncommitted')),
|
||||
'default_transaction_read_only': Bool(90300, None),
|
||||
'default_with_oids': Bool(90300, 120000),
|
||||
'dynamic_library_path': String(90300, None),
|
||||
'dynamic_shared_memory_type': (
|
||||
Enum(90400, 120000, ('posix', 'sysv', 'mmap', 'none')),
|
||||
Enum(120000, None, ('posix', 'sysv', 'mmap'))
|
||||
),
|
||||
'effective_cache_size': Integer(90300, None, 1, 2147483647, '8kB'),
|
||||
'effective_io_concurrency': Integer(90300, None, 0, 1000, None),
|
||||
'enable_bitmapscan': Bool(90300, None),
|
||||
'enable_gathermerge': Bool(100000, None),
|
||||
'enable_hashagg': Bool(90300, None),
|
||||
'enable_hashjoin': Bool(90300, None),
|
||||
'enable_incremental_sort': Bool(130000, None),
|
||||
'enable_indexonlyscan': Bool(90300, None),
|
||||
'enable_indexscan': Bool(90300, None),
|
||||
'enable_material': Bool(90300, None),
|
||||
'enable_mergejoin': Bool(90300, None),
|
||||
'enable_nestloop': Bool(90300, None),
|
||||
'enable_parallel_append': Bool(110000, None),
|
||||
'enable_parallel_hash': Bool(110000, None),
|
||||
'enable_partition_pruning': Bool(110000, None),
|
||||
'enable_partitionwise_aggregate': Bool(110000, None),
|
||||
'enable_partitionwise_join': Bool(110000, None),
|
||||
'enable_seqscan': Bool(90300, None),
|
||||
'enable_sort': Bool(90300, None),
|
||||
'enable_tidscan': Bool(90300, None),
|
||||
'escape_string_warning': Bool(90300, None),
|
||||
'event_source': String(90300, None),
|
||||
'exit_on_error': Bool(90300, None),
|
||||
'external_pid_file': String(90300, None),
|
||||
'extra_float_digits': Integer(90300, None, -15, 3, None),
|
||||
'force_parallel_mode': EnumBool(90600, None, ('regress',)),
|
||||
'from_collapse_limit': Integer(90300, None, 1, 2147483647, None),
|
||||
'fsync': Bool(90300, None),
|
||||
'full_page_writes': Bool(90300, None),
|
||||
'geqo': Bool(90300, None),
|
||||
'geqo_effort': Integer(90300, None, 1, 10, None),
|
||||
'geqo_generations': Integer(90300, None, 0, 2147483647, None),
|
||||
'geqo_pool_size': Integer(90300, None, 0, 2147483647, None),
|
||||
'geqo_seed': Real(90300, None, 0, 1, None),
|
||||
'geqo_selection_bias': Real(90300, None, 1.5, 2, None),
|
||||
'geqo_threshold': Integer(90300, None, 2, 2147483647, None),
|
||||
'gin_fuzzy_search_limit': Integer(90300, None, 0, 2147483647, None),
|
||||
'gin_pending_list_limit': Integer(90500, None, 64, 2147483647, 'kB'),
|
||||
'hash_mem_multiplier': Real(130000, None, 1, 1000, None),
|
||||
'hba_file': String(90300, None),
|
||||
'hot_standby': Bool(90300, None),
|
||||
'hot_standby_feedback': Bool(90300, None),
|
||||
'huge_pages': EnumBool(90400, None, ('try',)),
|
||||
'ident_file': String(90300, None),
|
||||
'idle_in_transaction_session_timeout': Integer(90600, None, 0, 2147483647, 'ms'),
|
||||
'ignore_checksum_failure': Bool(90300, None),
|
||||
'ignore_invalid_pages': Bool(130000, None),
|
||||
'ignore_system_indexes': Bool(90300, None),
|
||||
'IntervalStyle': Enum(90300, None, ('postgres', 'postgres_verbose', 'sql_standard', 'iso_8601')),
|
||||
'jit': Bool(110000, None),
|
||||
'jit_above_cost': Real(110000, None, -1, 1.79769e+308, None),
|
||||
'jit_debugging_support': Bool(110000, None),
|
||||
'jit_dump_bitcode': Bool(110000, None),
|
||||
'jit_expressions': Bool(110000, None),
|
||||
'jit_inline_above_cost': Real(110000, None, -1, 1.79769e+308, None),
|
||||
'jit_optimize_above_cost': Real(110000, None, -1, 1.79769e+308, None),
|
||||
'jit_profiling_support': Bool(110000, None),
|
||||
'jit_provider': String(110000, None),
|
||||
'jit_tuple_deforming': Bool(110000, None),
|
||||
'join_collapse_limit': Integer(90300, None, 1, 2147483647, None),
|
||||
'krb_caseins_users': Bool(90300, None),
|
||||
'krb_server_keyfile': String(90300, None),
|
||||
'krb_srvname': String(90300, 90400),
|
||||
'lc_messages': String(90300, None),
|
||||
'lc_monetary': String(90300, None),
|
||||
'lc_numeric': String(90300, None),
|
||||
'lc_time': String(90300, None),
|
||||
'listen_addresses': String(90300, None),
|
||||
'local_preload_libraries': String(90300, None),
|
||||
'lock_timeout': Integer(90300, None, 0, 2147483647, 'ms'),
|
||||
'lo_compat_privileges': Bool(90300, None),
|
||||
'log_autovacuum_min_duration': Integer(90300, None, -1, 2147483647, 'ms'),
|
||||
'log_checkpoints': Bool(90300, None),
|
||||
'log_connections': Bool(90300, None),
|
||||
'log_destination': String(90300, None),
|
||||
'log_directory': String(90300, None),
|
||||
'log_disconnections': Bool(90300, None),
|
||||
'log_duration': Bool(90300, None),
|
||||
'log_error_verbosity': Enum(90300, None, ('terse', 'default', 'verbose')),
|
||||
'log_executor_stats': Bool(90300, None),
|
||||
'log_file_mode': Integer(90300, None, 0, 511, None),
|
||||
'log_filename': String(90300, None),
|
||||
'logging_collector': Bool(90300, None),
|
||||
'log_hostname': Bool(90300, None),
|
||||
'logical_decoding_work_mem': Integer(130000, None, 64, 2147483647, 'kB'),
|
||||
'log_line_prefix': String(90300, None),
|
||||
'log_lock_waits': Bool(90300, None),
|
||||
'log_min_duration_sample': Integer(130000, None, -1, 2147483647, 'ms'),
|
||||
'log_min_duration_statement': Integer(90300, None, -1, 2147483647, 'ms'),
|
||||
'log_min_error_statement': Enum(90300, None, ('debug5', 'debug4', 'debug3', 'debug2', 'debug1', 'info',
|
||||
'notice', 'warning', 'error', 'log', 'fatal', 'panic')),
|
||||
'log_min_messages': Enum(90300, None, ('debug5', 'debug4', 'debug3', 'debug2', 'debug1', 'info',
|
||||
'notice', 'warning', 'error', 'log', 'fatal', 'panic')),
|
||||
'log_parameter_max_length': Integer(130000, None, -1, 1073741823, 'B'),
|
||||
'log_parameter_max_length_on_error': Integer(130000, None, -1, 1073741823, 'B'),
|
||||
'log_parser_stats': Bool(90300, None),
|
||||
'log_planner_stats': Bool(90300, None),
|
||||
'log_replication_commands': Bool(90500, None),
|
||||
'log_rotation_age': Integer(90300, None, 0, 35791394, 'min'),
|
||||
'log_rotation_size': Integer(90300, None, 0, 2097151, 'kB'),
|
||||
'log_statement': Enum(90300, None, ('none', 'ddl', 'mod', 'all')),
|
||||
'log_statement_sample_rate': Real(130000, None, 0, 1, None),
|
||||
'log_statement_stats': Bool(90300, None),
|
||||
'log_temp_files': Integer(90300, None, -1, 2147483647, 'kB'),
|
||||
'log_timezone': String(90300, None),
|
||||
'log_transaction_sample_rate': Real(120000, None, 0, 1, None),
|
||||
'log_truncate_on_rotation': Bool(90300, None),
|
||||
'maintenance_io_concurrency': Integer(130000, None, 0, 1000, None),
|
||||
'maintenance_work_mem': Integer(90300, None, 1024, 2147483647, 'kB'),
|
||||
'max_connections': (
|
||||
Integer(90300, 90600, 1, 8388607, None),
|
||||
Integer(90600, None, 1, 262143, None)
|
||||
),
|
||||
'max_files_per_process': (
|
||||
Integer(90300, 130000, 25, 2147483647, None),
|
||||
Integer(130000, None, 64, 2147483647, None)
|
||||
),
|
||||
'max_locks_per_transaction': Integer(90300, None, 10, 2147483647, None),
|
||||
'max_logical_replication_workers': Integer(100000, None, 0, 262143, None),
|
||||
'max_parallel_maintenance_workers': Integer(110000, None, 0, 1024, None),
|
||||
'max_parallel_workers': Integer(100000, None, 0, 1024, None),
|
||||
'max_parallel_workers_per_gather': Integer(90600, None, 0, 1024, None),
|
||||
'max_pred_locks_per_page': Integer(100000, None, 0, 2147483647, None),
|
||||
'max_pred_locks_per_relation': Integer(100000, None, -2147483648, 2147483647, None),
|
||||
'max_pred_locks_per_transaction': Integer(90300, None, 10, 2147483647, None),
|
||||
'max_prepared_transactions': (
|
||||
Integer(90300, 90600, 0, 8388607, None),
|
||||
Integer(90600, None, 0, 262143, None)
|
||||
),
|
||||
'max_replication_slots': (
|
||||
Integer(90400, 90600, 0, 8388607, None),
|
||||
Integer(90600, None, 0, 262143, None)
|
||||
),
|
||||
'max_slot_wal_keep_size': Integer(130000, None, -1, 2147483647, 'MB'),
|
||||
'max_stack_depth': Integer(90300, None, 100, 2147483647, 'kB'),
|
||||
'max_standby_archive_delay': Integer(90300, None, -1, 2147483647, 'ms'),
|
||||
'max_standby_streaming_delay': Integer(90300, None, -1, 2147483647, 'ms'),
|
||||
'max_sync_workers_per_subscription': Integer(100000, None, 0, 262143, None),
|
||||
'max_wal_senders': (
|
||||
Integer(90300, 90600, 0, 8388607, None),
|
||||
Integer(90600, None, 0, 262143, None)
|
||||
),
|
||||
'max_wal_size': (
|
||||
Integer(90500, 100000, 2, 2147483647, '16MB'),
|
||||
Integer(100000, None, 2, 2147483647, 'MB')
|
||||
),
|
||||
'max_worker_processes': (
|
||||
Integer(90400, 90600, 1, 8388607, None),
|
||||
Integer(90600, None, 0, 262143, None)
|
||||
),
|
||||
'min_parallel_index_scan_size': Integer(100000, None, 0, 715827882, '8kB'),
|
||||
'min_parallel_relation_size': Integer(90600, 100000, 0, 715827882, '8kB'),
|
||||
'min_parallel_table_scan_size': Integer(100000, None, 0, 715827882, '8kB'),
|
||||
'min_wal_size': (
|
||||
Integer(90500, 100000, 2, 2147483647, '16MB'),
|
||||
Integer(100000, None, 2, 2147483647, 'MB')
|
||||
),
|
||||
'old_snapshot_threshold': Integer(90600, None, -1, 86400, 'min'),
|
||||
'operator_precedence_warning': Bool(90500, None),
|
||||
'parallel_leader_participation': Bool(110000, None),
|
||||
'parallel_setup_cost': Real(90600, None, 0, 1.79769e+308, None),
|
||||
'parallel_tuple_cost': Real(90600, None, 0, 1.79769e+308, None),
|
||||
'password_encryption': (
|
||||
Bool(90300, 100000),
|
||||
Enum(100000, None, ('md5', 'scram-sha-256'))
|
||||
),
|
||||
'plan_cache_mode': Enum(120000, None, ('auto', 'force_generic_plan', 'force_custom_plan')),
|
||||
'port': Integer(90300, None, 1, 65535, None),
|
||||
'post_auth_delay': Integer(90300, None, 0, 2147, 's'),
|
||||
'pre_auth_delay': Integer(90300, None, 0, 60, 's'),
|
||||
'quote_all_identifiers': Bool(90300, None),
|
||||
'random_page_cost': Real(90300, None, 0, 1.79769e+308, None),
|
||||
'replacement_sort_tuples': Integer(90600, 110000, 0, 2147483647, None),
|
||||
'restart_after_crash': Bool(90300, None),
|
||||
'row_security': Bool(90500, None),
|
||||
'search_path': String(90300, None),
|
||||
'seq_page_cost': Real(90300, None, 0, 1.79769e+308, None),
|
||||
'session_preload_libraries': String(90400, None),
|
||||
'session_replication_role': Enum(90300, None, ('origin', 'replica', 'local')),
|
||||
'shared_buffers': Integer(90300, None, 16, 1073741823, '8kB'),
|
||||
'shared_memory_type': Enum(120000, None, ('sysv', 'mmap')),
|
||||
'shared_preload_libraries': String(90300, None),
|
||||
'sql_inheritance': Bool(90300, 100000),
|
||||
'ssl': Bool(90300, None),
|
||||
'ssl_ca_file': String(90300, None),
|
||||
'ssl_cert_file': String(90300, None),
|
||||
'ssl_ciphers': String(90300, None),
|
||||
'ssl_crl_file': String(90300, None),
|
||||
'ssl_dh_params_file': String(100000, None),
|
||||
'ssl_ecdh_curve': String(90400, None),
|
||||
'ssl_key_file': String(90300, None),
|
||||
'ssl_max_protocol_version': Enum(120000, None, ('', 'tlsv1', 'tlsv1.1', 'tlsv1.2', 'tlsv1.3')),
|
||||
'ssl_min_protocol_version': Enum(120000, None, ('tlsv1', 'tlsv1.1', 'tlsv1.2', 'tlsv1.3')),
|
||||
'ssl_passphrase_command': String(110000, None),
|
||||
'ssl_passphrase_command_supports_reload': Bool(110000, None),
|
||||
'ssl_prefer_server_ciphers': Bool(90400, None),
|
||||
'ssl_renegotiation_limit': Integer(90300, 90500, 0, 2147483647, 'kB'),
|
||||
'standard_conforming_strings': Bool(90300, None),
|
||||
'statement_timeout': Integer(90300, None, 0, 2147483647, 'ms'),
|
||||
'stats_temp_directory': String(90300, None),
|
||||
'superuser_reserved_connections': (
|
||||
Integer(90300, 90600, 0, 8388607, None),
|
||||
Integer(90600, None, 0, 262143, None),
|
||||
),
|
||||
'synchronize_seqscans': Bool(90300, None),
|
||||
'synchronous_commit': (
|
||||
EnumBool(90300, 90600, ('local', 'remote_write')),
|
||||
EnumBool(90600, None, ('local', 'remote_write', 'remote_apply'))
|
||||
),
|
||||
'synchronous_standby_names': String(90300, None),
|
||||
'syslog_facility': Enum(90300, None, ('local0', 'local1', 'local2', 'local3',
|
||||
'local4', 'local5', 'local6', 'local7')),
|
||||
'syslog_ident': String(90300, None),
|
||||
'syslog_sequence_numbers': Bool(90600, None),
|
||||
'syslog_split_messages': Bool(90600, None),
|
||||
'tcp_keepalives_count': Integer(90300, None, 0, 2147483647, None),
|
||||
'tcp_keepalives_idle': Integer(90300, None, 0, 2147483647, 's'),
|
||||
'tcp_keepalives_interval': Integer(90300, None, 0, 2147483647, 's'),
|
||||
'tcp_user_timeout': Integer(120000, None, 0, 2147483647, 'ms'),
|
||||
'temp_buffers': Integer(90300, None, 100, 1073741823, '8kB'),
|
||||
'temp_file_limit': Integer(90300, None, -1, 2147483647, 'kB'),
|
||||
'temp_tablespaces': String(90300, None),
|
||||
'TimeZone': String(90300, None),
|
||||
'timezone_abbreviations': String(90300, None),
|
||||
'trace_notify': Bool(90300, None),
|
||||
'trace_recovery_messages': Enum(90300, None, ('debug5', 'debug4', 'debug3', 'debug2',
|
||||
'debug1', 'log', 'notice', 'warning', 'error')),
|
||||
'trace_sort': Bool(90300, None),
|
||||
'track_activities': Bool(90300, None),
|
||||
'track_activity_query_size': (
|
||||
Integer(90300, 110000, 100, 102400, None),
|
||||
Integer(110000, 130000, 100, 102400, 'B'),
|
||||
Integer(130000, None, 100, 1048576, 'B')
|
||||
),
|
||||
'track_commit_timestamp': Bool(90500, None),
|
||||
'track_counts': Bool(90300, None),
|
||||
'track_functions': Enum(90300, None, ('none', 'pl', 'all')),
|
||||
'track_io_timing': Bool(90300, None),
|
||||
'transaction_deferrable': Bool(90300, None),
|
||||
'transaction_isolation': Enum(90300, None, ('serializable', 'repeatable read',
|
||||
'read committed', 'read uncommitted')),
|
||||
'transaction_read_only': Bool(90300, None),
|
||||
'transform_null_equals': Bool(90300, None),
|
||||
'unix_socket_directories': String(90300, None),
|
||||
'unix_socket_group': String(90300, None),
|
||||
'unix_socket_permissions': Integer(90300, None, 0, 511, None),
|
||||
'update_process_title': Bool(90300, None),
|
||||
'vacuum_cleanup_index_scale_factor': Real(110000, None, 0, 1e+10, None),
|
||||
'vacuum_cost_delay': (
|
||||
Integer(90300, 120000, 0, 100, 'ms'),
|
||||
Real(120000, None, 0, 100, 'ms')
|
||||
),
|
||||
'vacuum_cost_limit': Integer(90300, None, 1, 10000, None),
|
||||
'vacuum_cost_page_dirty': Integer(90300, None, 0, 10000, None),
|
||||
'vacuum_cost_page_hit': Integer(90300, None, 0, 10000, None),
|
||||
'vacuum_cost_page_miss': Integer(90300, None, 0, 10000, None),
|
||||
'vacuum_defer_cleanup_age': Integer(90300, None, 0, 1000000, None),
|
||||
'vacuum_freeze_min_age': Integer(90300, None, 0, 1000000000, None),
|
||||
'vacuum_freeze_table_age': Integer(90300, None, 0, 2000000000, None),
|
||||
'vacuum_multixact_freeze_min_age': Integer(90300, None, 0, 1000000000, None),
|
||||
'vacuum_multixact_freeze_table_age': Integer(90300, None, 0, 2000000000, None),
|
||||
'wal_buffers': Integer(90300, None, -1, 262143, '8kB'),
|
||||
'wal_compression': Bool(90500, None),
|
||||
'wal_consistency_checking': String(100000, None),
|
||||
'wal_init_zero': Bool(120000, None),
|
||||
'wal_keep_segments': Integer(90300, 130000, 0, 2147483647, None),
|
||||
'wal_keep_size': Integer(130000, None, 0, 2147483647, 'MB'),
|
||||
'wal_level': (
|
||||
Enum(90300, 90400, ('minimal', 'archive', 'hot_standby')),
|
||||
Enum(90400, 90600, ('minimal', 'archive', 'hot_standby', 'logical')),
|
||||
Enum(90600, None, ('minimal', 'replica', 'logical'))
|
||||
),
|
||||
'wal_log_hints': Bool(90400, None),
|
||||
'wal_receiver_create_temp_slot': Bool(130000, None),
|
||||
'wal_receiver_status_interval': Integer(90300, None, 0, 2147483, 's'),
|
||||
'wal_receiver_timeout': Integer(90300, None, 0, 2147483647, 'ms'),
|
||||
'wal_recycle': Bool(120000, None),
|
||||
'wal_retrieve_retry_interval': Integer(90500, None, 1, 2147483647, 'ms'),
|
||||
'wal_sender_timeout': Integer(90300, None, 0, 2147483647, 'ms'),
|
||||
'wal_skip_threshold': Integer(130000, None, 0, 2147483647, 'kB'),
|
||||
'wal_sync_method': Enum(90300, None, ('fsync', 'fdatasync', 'open_sync', 'open_datasync')),
|
||||
'wal_writer_delay': Integer(90300, None, 1, 10000, 'ms'),
|
||||
'wal_writer_flush_after': Integer(90600, None, 0, 2147483647, '8kB'),
|
||||
'work_mem': Integer(90300, None, 64, 2147483647, 'kB'),
|
||||
'xmlbinary': Enum(90300, None, ('base64', 'hex')),
|
||||
'xmloption': Enum(90300, None, ('content', 'document')),
|
||||
'zero_damaged_pages': Bool(90300, None)
|
||||
})
|
||||
|
||||
|
||||
recovery_parameters = CaseInsensitiveDict({
|
||||
'archive_cleanup_command': String(90300, None),
|
||||
'pause_at_recovery_target': Bool(90300, 90500),
|
||||
'primary_conninfo': String(90300, None),
|
||||
'primary_slot_name': String(90400, None),
|
||||
'promote_trigger_file': String(120000, None),
|
||||
'recovery_end_command': String(90300, None),
|
||||
'recovery_min_apply_delay': Integer(90400, None, 0, 2147483647, 'ms'),
|
||||
'recovery_target': Enum(90400, None, ('immediate', '')),
|
||||
'recovery_target_action': Enum(90500, None, ('pause', 'promote', 'shutdown')),
|
||||
'recovery_target_inclusive': Bool(90300, None),
|
||||
'recovery_target_lsn': String(100000, None),
|
||||
'recovery_target_name': String(90400, None),
|
||||
'recovery_target_time': String(90300, None),
|
||||
'recovery_target_timeline': String(90300, None),
|
||||
'recovery_target_xid': String(90300, None),
|
||||
'restore_command': String(90300, None),
|
||||
'standby_mode': Bool(90300, 120000),
|
||||
'trigger_file': String(90300, 120000)
|
||||
})
|
||||
|
||||
|
||||
def _transform_parameter_value(validators, version, name, value):
|
||||
validators = validators.get(name)
|
||||
if validators:
|
||||
for validator in (validators if isinstance(validators[0], tuple) else [validators]):
|
||||
if version >= validator.version_from and\
|
||||
(validator.version_till is None or version < validator.version_till):
|
||||
return validator.transform(name, value)
|
||||
logger.warning('Removing unexpected parameter=%s value=%s from the config', name, value)
|
||||
|
||||
|
||||
def transform_postgresql_parameter_value(version, name, value):
|
||||
if '.' in name:
|
||||
return value
|
||||
return _transform_parameter_value(parameters, version, name, value)
|
||||
|
||||
|
||||
def transform_recovery_parameter_value(version, name, value):
|
||||
return _transform_parameter_value(recovery_parameters, version, name, value)
|
||||
@@ -0,0 +1,34 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
from patroni.daemon import AbstractPatroniDaemon, abstract_main
|
||||
from patroni.dcs.raft import KVStoreTTL
|
||||
from pysyncobj import SyncObjConf
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RaftController(AbstractPatroniDaemon):
|
||||
|
||||
def __init__(self, config):
|
||||
super(RaftController, self).__init__(config)
|
||||
|
||||
raft_config = self.config.get('raft')
|
||||
self_addr = raft_config['self_addr']
|
||||
template = os.path.join(raft_config.get('data_dir', ''), self_addr)
|
||||
self._syncobj_config = SyncObjConf(autoTick=False, appendEntriesUseBatch=False, dynamicMembershipChange=True,
|
||||
journalFile=template + '.journal', fullDumpFile=template + '.dump')
|
||||
self._raft = KVStoreTTL(self_addr, raft_config.get('partner_addrs', []), self._syncobj_config)
|
||||
|
||||
def _run_cycle(self):
|
||||
try:
|
||||
self._raft.doTick(self._syncobj_config.autoTickPeriod)
|
||||
except Exception:
|
||||
logger.exception('doTick')
|
||||
|
||||
def _shutdown(self):
|
||||
self._raft.destroy()
|
||||
|
||||
|
||||
def main():
|
||||
abstract_main(RaftController)
|
||||
+74
-4
@@ -1,8 +1,11 @@
|
||||
import json.decoder as json_decoder
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import random
|
||||
import re
|
||||
import socket
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
@@ -334,7 +337,7 @@ class Retry(object):
|
||||
logger.warning('Retry got exception: %s', e)
|
||||
raise RetryFailedError("Too many retry attempts")
|
||||
self._attempts += 1
|
||||
sleeptime = self.sleeptime
|
||||
sleeptime = hasattr(e, 'sleeptime') and e.sleeptime or self.sleeptime
|
||||
|
||||
if self._cur_stoptime is not None and time.time() + sleeptime >= self._cur_stoptime:
|
||||
logger.warning('Retry got exception: %s', e)
|
||||
@@ -357,6 +360,8 @@ def polling_loop(timeout, interval=1):
|
||||
|
||||
def split_host_port(value, default_port):
|
||||
t = value.rsplit(':', 1)
|
||||
if ':' in t[0]:
|
||||
t[0] = t[0].strip('[]')
|
||||
t.append(default_port)
|
||||
return t[0], int(t[1])
|
||||
|
||||
@@ -371,6 +376,27 @@ def uri(proto, netloc, path='', user=None):
|
||||
return '{0}://{1}{2}{3}{4}'.format(proto, user, host, port, path)
|
||||
|
||||
|
||||
def iter_response_objects(response):
|
||||
prev = ''
|
||||
decoder = json_decoder.JSONDecoder()
|
||||
for chunk in response.read_chunked(decode_content=False):
|
||||
if isinstance(chunk, bytes):
|
||||
chunk = chunk.decode('utf-8')
|
||||
chunk = prev + chunk
|
||||
|
||||
length = len(chunk)
|
||||
idx = json_decoder.WHITESPACE.match(chunk, 0).end()
|
||||
while idx < length:
|
||||
try:
|
||||
message, idx = decoder.raw_decode(chunk, idx)
|
||||
except ValueError: # malformed or incomplete JSON, unlikely to happen
|
||||
break
|
||||
else:
|
||||
yield message
|
||||
idx = json_decoder.WHITESPACE.match(chunk, idx).end()
|
||||
prev = chunk[idx:]
|
||||
|
||||
|
||||
def is_standby_cluster(config):
|
||||
# Check whether or not provided configuration describes a standby cluster
|
||||
return isinstance(config, dict) and (config.get('host') or config.get('port') or config.get('restore_command'))
|
||||
@@ -385,14 +411,17 @@ def cluster_as_json(cluster):
|
||||
if m.name == leader_name:
|
||||
config = cluster.config.data if cluster.config and cluster.config.modify_index else {}
|
||||
role = 'standby_leader' if is_standby_cluster(config.get('standby_cluster')) else 'leader'
|
||||
elif m.name == cluster.sync.sync_standby:
|
||||
elif m.name in cluster.sync.members:
|
||||
role = 'sync_standby'
|
||||
else:
|
||||
role = 'replica'
|
||||
|
||||
member = {'name': m.name, 'role': role, 'state': m.data.get('state', ''), 'api_url': m.api_url}
|
||||
conn_kwargs = m.conn_kwargs()
|
||||
member = {'name': m.name, 'host': conn_kwargs['host'], 'port': int(conn_kwargs['port']),
|
||||
'role': role, 'state': m.data.get('state', ''), 'api_url': m.api_url}
|
||||
if conn_kwargs.get('host'):
|
||||
member['host'] = conn_kwargs['host']
|
||||
if conn_kwargs.get('port'):
|
||||
member['port'] = int(conn_kwargs['port'])
|
||||
optional_attributes = ('timeline', 'pending_restart', 'scheduled_restart', 'tags')
|
||||
member.update({n: m.data[n] for n in optional_attributes if n in m.data})
|
||||
|
||||
@@ -442,3 +471,44 @@ def validate_directory(d, msg="{} {}"):
|
||||
raise PatroniException(msg.format(d, "the directory is not writable"))
|
||||
else:
|
||||
raise PatroniException(msg.format(d, "is not a directory"))
|
||||
|
||||
|
||||
def data_directory_is_empty(data_dir):
|
||||
if not os.path.exists(data_dir):
|
||||
return True
|
||||
return all(os.name != 'nt' and (n.startswith('.') or n == 'lost+found') for n in os.listdir(data_dir))
|
||||
|
||||
|
||||
def keepalive_intvl(timeout, idle, cnt=3):
|
||||
return max(1, int(float(timeout - idle) / cnt))
|
||||
|
||||
|
||||
def keepalive_socket_options(timeout, idle, cnt=3):
|
||||
yield (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||
|
||||
if sys.platform.startswith('linux'):
|
||||
yield (socket.SOL_TCP, 18, int(timeout * 1000)) # TCP_USER_TIMEOUT
|
||||
TCP_KEEPIDLE = getattr(socket, 'TCP_KEEPIDLE', None)
|
||||
TCP_KEEPINTVL = getattr(socket, 'TCP_KEEPINTVL', None)
|
||||
TCP_KEEPCNT = getattr(socket, 'TCP_KEEPCNT', None)
|
||||
elif sys.platform.startswith('darwin'):
|
||||
TCP_KEEPIDLE = 0x10 # (named "TCP_KEEPALIVE" in C)
|
||||
TCP_KEEPINTVL = 0x101
|
||||
TCP_KEEPCNT = 0x102
|
||||
else:
|
||||
return
|
||||
|
||||
intvl = keepalive_intvl(timeout, idle, cnt)
|
||||
yield (socket.IPPROTO_TCP, TCP_KEEPIDLE, idle)
|
||||
yield (socket.IPPROTO_TCP, TCP_KEEPINTVL, intvl)
|
||||
yield (socket.IPPROTO_TCP, TCP_KEEPCNT, cnt)
|
||||
|
||||
|
||||
def enable_keepalive(sock, timeout, idle, cnt=3):
|
||||
SIO_KEEPALIVE_VALS = getattr(socket, 'SIO_KEEPALIVE_VALS', None)
|
||||
if SIO_KEEPALIVE_VALS is not None: # Windows
|
||||
intvl = keepalive_intvl(timeout, idle, cnt)
|
||||
return sock.ioctl(SIO_KEEPALIVE_VALS, (1, idle * 1000, intvl * 1000))
|
||||
|
||||
for opt in keepalive_socket_options(timeout, idle, cnt):
|
||||
sock.setsockopt(*opt)
|
||||
|
||||
@@ -0,0 +1,379 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import socket
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from patroni.utils import split_host_port, data_directory_is_empty
|
||||
from patroni.ctl import find_executable
|
||||
from patroni.dcs import dcs_modules
|
||||
from patroni.exceptions import ConfigParseError
|
||||
from six import string_types
|
||||
|
||||
|
||||
def data_directory_empty(data_dir):
|
||||
if os.path.isfile(os.path.join(data_dir, "global", "pg_control")):
|
||||
return False
|
||||
return data_directory_is_empty(data_dir)
|
||||
|
||||
|
||||
def validate_connect_address(address):
|
||||
try:
|
||||
host, _ = split_host_port(address, 1)
|
||||
except (AttributeError, TypeError, ValueError):
|
||||
raise ConfigParseError("contains a wrong value")
|
||||
if host in ["127.0.0.1", "0.0.0.0", "*", "::1", "localhost"]:
|
||||
raise ConfigParseError('must not contain "127.0.0.1", "0.0.0.0", "*", "::1", "localhost"')
|
||||
return True
|
||||
|
||||
|
||||
def validate_host_port(host_port, listen=False, multiple_hosts=False):
|
||||
try:
|
||||
hosts, port = split_host_port(host_port, None)
|
||||
except (ValueError, TypeError):
|
||||
raise ConfigParseError("contains a wrong value")
|
||||
else:
|
||||
if multiple_hosts:
|
||||
hosts = hosts.split(",")
|
||||
else:
|
||||
hosts = [hosts]
|
||||
for host in hosts:
|
||||
proto = socket.getaddrinfo(host, "", 0, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
|
||||
s = socket.socket(proto[0][0], socket.SOCK_STREAM)
|
||||
try:
|
||||
if s.connect_ex((host, port)) == 0:
|
||||
if listen:
|
||||
raise ConfigParseError("Port {} is already in use.".format(port))
|
||||
elif not listen:
|
||||
raise ConfigParseError("{} is not reachable".format(host_port))
|
||||
except socket.gaierror as e:
|
||||
raise ConfigParseError(e)
|
||||
finally:
|
||||
s.close()
|
||||
return True
|
||||
|
||||
|
||||
def comma_separated_host_port(string):
|
||||
assert all([validate_host_port(s.strip()) for s in string.split(",")]), "didn't pass the validation"
|
||||
return True
|
||||
|
||||
|
||||
def validate_host_port_listen(host_port):
|
||||
return validate_host_port(host_port, listen=True)
|
||||
|
||||
|
||||
def validate_host_port_listen_multiple_hosts(host_port):
|
||||
return validate_host_port(host_port, listen=True, multiple_hosts=True)
|
||||
|
||||
|
||||
def is_ipv4_address(ip):
|
||||
try:
|
||||
socket.inet_aton(ip)
|
||||
except Exception:
|
||||
raise ConfigParseError("Is not a valid ipv4 address")
|
||||
return True
|
||||
|
||||
|
||||
def is_ipv6_address(ip):
|
||||
try:
|
||||
socket.inet_pton(socket.AF_INET6, ip)
|
||||
except Exception:
|
||||
raise ConfigParseError("Is not a valid ipv6 address")
|
||||
return True
|
||||
|
||||
|
||||
def get_major_version(bin_dir=None):
|
||||
if not bin_dir:
|
||||
binary = 'postgres'
|
||||
else:
|
||||
binary = os.path.join(bin_dir, 'postgres')
|
||||
version = subprocess.check_output([binary, '--version']).decode()
|
||||
version = re.match(r'^[^\s]+ [^\s]+ (\d+)(\.(\d+))?', version)
|
||||
return '.'.join([version.group(1), version.group(3)]) if int(version.group(1)) < 10 else version.group(1)
|
||||
|
||||
|
||||
def validate_data_dir(data_dir):
|
||||
if not data_dir:
|
||||
raise ConfigParseError("is an empty string")
|
||||
elif os.path.exists(data_dir) and not os.path.isdir(data_dir):
|
||||
raise ConfigParseError("is not a directory")
|
||||
elif not data_directory_empty(data_dir):
|
||||
if not os.path.exists(os.path.join(data_dir, "PG_VERSION")):
|
||||
raise ConfigParseError("doesn't look like a valid data directory")
|
||||
else:
|
||||
with open(os.path.join(data_dir, "PG_VERSION"), "r") as version:
|
||||
pgversion = version.read().strip()
|
||||
waldir = ("pg_wal" if float(pgversion) >= 10 else "pg_xlog")
|
||||
if not os.path.isdir(os.path.join(data_dir, waldir)):
|
||||
raise ConfigParseError("data dir for the cluster is not empty, but doesn't contain"
|
||||
" \"{}\" directory".format(waldir))
|
||||
bin_dir = schema.data.get("postgresql", {}).get("bin_dir", None)
|
||||
major_version = get_major_version(bin_dir)
|
||||
if pgversion != major_version:
|
||||
raise ConfigParseError("data_dir directory postgresql version ({}) doesn't match with "
|
||||
"'postgres --version' output ({})".format(pgversion, major_version))
|
||||
return True
|
||||
|
||||
|
||||
class Result(object):
|
||||
def __init__(self, status, error="didn't pass validation", level=0, path="", data=""):
|
||||
self.status = status
|
||||
self.path = path
|
||||
self.data = data
|
||||
self.level = level
|
||||
self._error = error
|
||||
if not self.status:
|
||||
self.error = error
|
||||
else:
|
||||
self.error = None
|
||||
|
||||
def __repr__(self):
|
||||
return self.path + (" " + str(self.data) + " " + self._error if self.error else "")
|
||||
|
||||
|
||||
class Case(object):
|
||||
def __init__(self, schema):
|
||||
self._schema = schema
|
||||
|
||||
|
||||
class Or(object):
|
||||
def __init__(self, *args):
|
||||
self.args = args
|
||||
|
||||
|
||||
class Optional(object):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
|
||||
class Directory(object):
|
||||
def __init__(self, contains=None, contains_executable=None):
|
||||
self.contains = contains
|
||||
self.contains_executable = contains_executable
|
||||
|
||||
def validate(self, name):
|
||||
if not name:
|
||||
yield Result(False, "is an empty string")
|
||||
elif not os.path.exists(name):
|
||||
yield Result(False, "Directory '{}' does not exist.".format(name))
|
||||
elif not os.path.isdir(name):
|
||||
yield Result(False, "'{}' is not a directory.".format(name))
|
||||
else:
|
||||
if self.contains:
|
||||
for path in self.contains:
|
||||
if not os.path.exists(os.path.join(name, path)):
|
||||
yield Result(False, "'{}' does not contain '{}'".format(name, path))
|
||||
if self.contains_executable:
|
||||
for program in self.contains_executable:
|
||||
if not find_executable(program, name):
|
||||
yield Result(False, "'{}' does not contain '{}'".format(name, program))
|
||||
|
||||
|
||||
class Schema(object):
|
||||
def __init__(self, validator):
|
||||
self.validator = validator
|
||||
|
||||
def __call__(self, data):
|
||||
for i in self.validate(data):
|
||||
if not i.status:
|
||||
print(i)
|
||||
|
||||
def validate(self, data):
|
||||
self.data = data
|
||||
if isinstance(self.validator, string_types):
|
||||
yield Result(isinstance(self.data, string_types), "is not a string", level=1, data=self.data)
|
||||
elif issubclass(type(self.validator), type):
|
||||
validator = self.validator
|
||||
if self.validator == str:
|
||||
validator = string_types
|
||||
yield Result(isinstance(self.data, validator),
|
||||
"is not {}".format(_get_type_name(self.validator)), level=1, data=self.data)
|
||||
elif callable(self.validator):
|
||||
if hasattr(self.validator, "expected_type"):
|
||||
if not isinstance(data, self.validator.expected_type):
|
||||
yield Result(False, "is not {}"
|
||||
.format(_get_type_name(self.validator.expected_type)), level=1, data=self.data)
|
||||
return
|
||||
try:
|
||||
self.validator(data)
|
||||
yield Result(True, data=self.data)
|
||||
except Exception as e:
|
||||
yield Result(False, "didn't pass validation: {}".format(e), data=self.data)
|
||||
elif isinstance(self.validator, dict):
|
||||
if not len(self.validator):
|
||||
yield Result(isinstance(self.data, dict), "is not a dictionary", level=1, data=self.data)
|
||||
elif isinstance(self.validator, list):
|
||||
if not isinstance(self.data, list):
|
||||
yield Result(isinstance(self.data, list), "is not a list", level=1, data=self.data)
|
||||
return
|
||||
for i in self.iter():
|
||||
yield i
|
||||
|
||||
def iter(self):
|
||||
if isinstance(self.validator, dict):
|
||||
if not isinstance(self.data, dict):
|
||||
yield Result(False, "is not a dictionary.", level=1)
|
||||
else:
|
||||
for i in self.iter_dict():
|
||||
yield i
|
||||
elif isinstance(self.validator, list):
|
||||
if len(self.data) == 0:
|
||||
yield Result(False, "is an empty list", data=self.data)
|
||||
if len(self.validator) > 0:
|
||||
for key, value in enumerate(self.data):
|
||||
for v in Schema(self.validator[0]).validate(value):
|
||||
yield Result(v.status, v.error,
|
||||
path=(str(key) + ("." + v.path if v.path else "")), level=v.level, data=value)
|
||||
elif isinstance(self.validator, Directory):
|
||||
for v in self.validator.validate(self.data):
|
||||
yield v
|
||||
elif isinstance(self.validator, Or):
|
||||
for i in self.iter_or():
|
||||
yield i
|
||||
|
||||
def iter_dict(self):
|
||||
for key in self.validator.keys():
|
||||
for d in self._data_key(key):
|
||||
if d not in self.data and not isinstance(key, Optional):
|
||||
yield Result(False, "is not defined.", path=d)
|
||||
elif d not in self.data and isinstance(key, Optional):
|
||||
continue
|
||||
else:
|
||||
validator = self.validator[key]
|
||||
if isinstance(key, Or) and isinstance(self.validator[key], Case):
|
||||
validator = self.validator[key]._schema[d]
|
||||
for v in Schema(validator).validate(self.data[d]):
|
||||
yield Result(v.status, v.error,
|
||||
path=(d + ("." + v.path if v.path else "")), level=v.level, data=v.data)
|
||||
|
||||
def iter_or(self):
|
||||
results = []
|
||||
for a in self.validator.args:
|
||||
r = []
|
||||
for v in Schema(a).validate(self.data):
|
||||
r.append(v)
|
||||
if any([x.status for x in r]) and not all([x.status for x in r]):
|
||||
results += filter(lambda x: not x.status, r)
|
||||
else:
|
||||
results += r
|
||||
if not any([x.status for x in results]):
|
||||
max_level = 3
|
||||
for v in sorted(results, key=lambda x: x.level):
|
||||
if v.level > max_level:
|
||||
break
|
||||
max_level = v.level
|
||||
yield Result(v.status, v.error, path=v.path, level=v.level, data=v.data)
|
||||
|
||||
def _data_key(self, key):
|
||||
if isinstance(self.data, dict) and isinstance(key, str):
|
||||
yield key
|
||||
elif isinstance(key, Optional):
|
||||
yield key.name
|
||||
elif isinstance(key, Or):
|
||||
if any([i in self.data for i in key.args]):
|
||||
for i in key.args:
|
||||
if i in self.data:
|
||||
yield i
|
||||
else:
|
||||
for i in key.args:
|
||||
yield i
|
||||
|
||||
|
||||
def _get_type_name(python_type):
|
||||
return {str: 'a string', int: 'and integer', float: 'a number', bool: 'a boolean',
|
||||
list: 'an array', dict: 'a dictionary', string_types: "a string"}.get(
|
||||
python_type, getattr(python_type, __name__, "unknown type"))
|
||||
|
||||
|
||||
def assert_(condition, message="Wrong value"):
|
||||
assert condition, message
|
||||
|
||||
|
||||
userattributes = {"username": "", Optional("password"): ""}
|
||||
available_dcs = [m.split(".")[-1] for m in dcs_modules()]
|
||||
comma_separated_host_port.expected_type = string_types
|
||||
validate_connect_address.expected_type = string_types
|
||||
validate_host_port_listen.expected_type = string_types
|
||||
validate_host_port_listen_multiple_hosts.expected_type = string_types
|
||||
validate_data_dir.expected_type = string_types
|
||||
|
||||
schema = Schema({
|
||||
"name": str,
|
||||
"scope": str,
|
||||
"restapi": {
|
||||
"listen": validate_host_port_listen,
|
||||
"connect_address": validate_connect_address
|
||||
},
|
||||
Optional("bootstrap"): {
|
||||
"dcs": {
|
||||
Optional("ttl"): int,
|
||||
Optional("loop_wait"): int,
|
||||
Optional("retry_timeout"): int,
|
||||
Optional("maximum_lag_on_failover"): int
|
||||
},
|
||||
"pg_hba": [str],
|
||||
"initdb": [Or(str, dict)]
|
||||
},
|
||||
Or(*available_dcs): Case({
|
||||
"consul": {
|
||||
Or("host", "url"): Case({
|
||||
"host": validate_host_port,
|
||||
"url": str})
|
||||
},
|
||||
"etcd": {
|
||||
Or("host", "hosts", "srv", "url", "proxy"): Case({
|
||||
"host": validate_host_port,
|
||||
"hosts": Or(comma_separated_host_port, [validate_host_port]),
|
||||
"srv": str,
|
||||
"url": str,
|
||||
"proxy": str})
|
||||
},
|
||||
"exhibitor": {
|
||||
"hosts": [str],
|
||||
"port": lambda i: assert_(int(i) <= 65535),
|
||||
Optional("pool_interval"): int
|
||||
},
|
||||
"zookeeper": {
|
||||
"hosts": Or(comma_separated_host_port, [validate_host_port]),
|
||||
},
|
||||
"kubernetes": {
|
||||
"labels": {},
|
||||
Optional("namespace"): str,
|
||||
Optional("scope_label"): str,
|
||||
Optional("role_label"): str,
|
||||
Optional("use_endpoints"): bool,
|
||||
Optional("pod_ip"): Or(is_ipv4_address, is_ipv6_address),
|
||||
Optional("ports"): [{"name": str, "port": int}],
|
||||
},
|
||||
}),
|
||||
"postgresql": {
|
||||
"listen": validate_host_port_listen_multiple_hosts,
|
||||
"connect_address": validate_connect_address,
|
||||
"authentication": {
|
||||
"replication": userattributes,
|
||||
"superuser": userattributes,
|
||||
"rewind": userattributes
|
||||
},
|
||||
"data_dir": validate_data_dir,
|
||||
Optional("bin_dir"): Directory(contains_executable=["pg_ctl", "initdb", "pg_controldata", "pg_basebackup",
|
||||
"postgres", "pg_isready"]),
|
||||
Optional("parameters"): {
|
||||
Optional("unix_socket_directories"): lambda s: assert_(all([isinstance(s, string_types), len(s)]))
|
||||
},
|
||||
Optional("pg_hba"): [str],
|
||||
Optional("pg_ident"): [str],
|
||||
Optional("pg_ctl_timeout"): int,
|
||||
Optional("use_pg_rewind"): bool
|
||||
},
|
||||
Optional("watchdog"): {
|
||||
Optional("mode"): lambda m: assert_(m in ["off", "automatic", "required"]),
|
||||
Optional("device"): str
|
||||
},
|
||||
Optional("tags"): {
|
||||
Optional("nofailover"): bool,
|
||||
Optional("clonefrom"): bool,
|
||||
Optional("noloadbalance"): bool,
|
||||
Optional("replicatefrom"): str,
|
||||
Optional("nosync"): bool
|
||||
}
|
||||
})
|
||||
+1
-1
@@ -1 +1 @@
|
||||
__version__ = '1.6.4'
|
||||
__version__ = '2.0.1'
|
||||
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
from patroni.raft_controller import main
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -30,6 +30,13 @@ etcd:
|
||||
#It is possible to change this behavior through by setting:
|
||||
#use_proxies: true
|
||||
|
||||
#raft:
|
||||
# data_dir: .
|
||||
# self_addr: 127.0.0.1:2222
|
||||
# partner_addrs:
|
||||
# - 127.0.0.1:2223
|
||||
# - 127.0.0.1:2224
|
||||
|
||||
bootstrap:
|
||||
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
|
||||
# and all other cluster members will use it as a `global configuration`
|
||||
@@ -108,6 +115,8 @@ postgresql:
|
||||
# same as KRB5CCNAME used by the GSS
|
||||
# krb_server_keyfile: /var/spool/keytabs/postgres
|
||||
unix_socket_directories: '.'
|
||||
# Additional fencing script executed after acquiring the leader lock but before promoting the replica
|
||||
#pre_promote: /path/to/pre_promote.sh
|
||||
|
||||
#watchdog:
|
||||
# mode: automatic # Allowed values: off, automatic, required
|
||||
|
||||
@@ -30,6 +30,13 @@ etcd:
|
||||
#It is possible to change this behavior through by setting:
|
||||
#use_proxies: true
|
||||
|
||||
#raft:
|
||||
# data_dir: .
|
||||
# self_addr: 127.0.0.1:2223
|
||||
# partner_addrs:
|
||||
# - 127.0.0.1:2222
|
||||
# - 127.0.0.1:2224
|
||||
|
||||
bootstrap:
|
||||
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
|
||||
# and all other cluster members will use it as a `global configuration`
|
||||
@@ -105,6 +112,9 @@ postgresql:
|
||||
basebackup:
|
||||
- verbose
|
||||
- max-rate: 100M
|
||||
# Additional fencing script executed after acquiring the leader lock but before promoting the replica
|
||||
#pre_promote: /path/to/pre_promote.sh
|
||||
|
||||
tags:
|
||||
nofailover: false
|
||||
noloadbalance: false
|
||||
|
||||
@@ -30,6 +30,13 @@ etcd:
|
||||
#It is possible to change this behavior through by setting:
|
||||
#use_proxies: true
|
||||
|
||||
#raft:
|
||||
# data_dir: .
|
||||
# self_addr: 127.0.0.1:2224
|
||||
# partner_addrs:
|
||||
# - 127.0.0.1:2222
|
||||
# - 127.0.0.1:2223
|
||||
|
||||
bootstrap:
|
||||
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
|
||||
# and all other cluster members will use it as a `global configuration`
|
||||
|
||||
+3
-3
@@ -1,4 +1,5 @@
|
||||
urllib3>=1.19.1,!=1.21
|
||||
ipaddress; python_version=="2.7"
|
||||
boto
|
||||
PyYAML
|
||||
six >= 1.7
|
||||
@@ -7,8 +8,7 @@ python-etcd>=0.4.3,<0.5
|
||||
python-consul>=0.7.1
|
||||
click>=4.1
|
||||
prettytable>=0.7
|
||||
tzlocal
|
||||
python-dateutil
|
||||
pysyncobj>=0.3.7
|
||||
psutil>=2.0.0
|
||||
cdiff
|
||||
kubernetes>=2.0.0,<=10.0.1,!=4.0.*,!=5.0.*
|
||||
ydiff>=1.2.0
|
||||
|
||||
@@ -22,8 +22,8 @@ AUTHOR_EMAIL = '[email protected], [email protected], alexk
|
||||
KEYWORDS = 'etcd governor patroni postgresql postgres ha haproxy confd' +\
|
||||
' zookeeper exhibitor consul streaming replication kubernetes k8s'
|
||||
|
||||
EXTRAS_REQUIRE = {'aws': ['boto'], 'etcd': ['python-etcd'], 'consul': ['python-consul'],
|
||||
'exhibitor': ['kazoo'], 'zookeeper': ['kazoo'], 'kubernetes': ['kubernetes']}
|
||||
EXTRAS_REQUIRE = {'aws': ['boto'], 'etcd': ['python-etcd'], 'etcd3': ['python-etcd'], 'consul': ['python-consul'],
|
||||
'exhibitor': ['kazoo'], 'zookeeper': ['kazoo'], 'kubernetes': ['ipaddress'], 'raft': ['pysyncobj']}
|
||||
COVERAGE_XML = True
|
||||
COVERAGE_HTML = False
|
||||
|
||||
@@ -51,10 +51,60 @@ CLASSIFIERS = [
|
||||
|
||||
CONSOLE_SCRIPTS = ['patroni = patroni:main',
|
||||
'patronictl = patroni.ctl:ctl',
|
||||
'patroni_raft_controller = patroni.raft_controller:main',
|
||||
"patroni_wale_restore = patroni.scripts.wale_restore:main",
|
||||
"patroni_aws = patroni.scripts.aws:main"]
|
||||
|
||||
|
||||
class Flake8(Command):
|
||||
|
||||
user_options = []
|
||||
|
||||
def initialize_options(self):
|
||||
from flake8.main import application
|
||||
|
||||
self.flake8 = application.Application()
|
||||
self.flake8.initialize([])
|
||||
|
||||
def finalize_options(self):
|
||||
pass
|
||||
|
||||
def package_files(self):
|
||||
seen_package_directories = ()
|
||||
directories = self.distribution.package_dir or {}
|
||||
empty_directory_exists = "" in directories
|
||||
packages = self.distribution.packages or []
|
||||
for package in packages:
|
||||
if package in directories:
|
||||
package_directory = directories[package]
|
||||
elif empty_directory_exists:
|
||||
package_directory = os.path.join(directories[""], package)
|
||||
else:
|
||||
package_directory = package
|
||||
|
||||
if not package_directory.startswith(seen_package_directories):
|
||||
seen_package_directories += (package_directory + ".",)
|
||||
yield package_directory
|
||||
|
||||
def targets(self):
|
||||
return [package for package in self.package_files()] + ['tests', 'setup.py']
|
||||
|
||||
def run(self):
|
||||
self.flake8.run_checks(self.targets())
|
||||
self.flake8.formatter.start()
|
||||
self.flake8.report_errors()
|
||||
self.flake8.report_statistics()
|
||||
self.flake8.report_benchmarks()
|
||||
self.flake8.formatter.stop()
|
||||
try:
|
||||
self.flake8.exit()
|
||||
except SystemExit as e:
|
||||
# Cause system exit only if exit code is not zero (terminates
|
||||
# other possibly remaining/pending setuptools commands).
|
||||
if e.code:
|
||||
raise
|
||||
|
||||
|
||||
class PyTest(Command):
|
||||
|
||||
user_options = [('cov=', None, 'Run coverage'), ('cov-xml=', None, 'Generate junit xml report'),
|
||||
@@ -93,9 +143,13 @@ class PyTest(Command):
|
||||
|
||||
def run(self):
|
||||
from pkg_resources import evaluate_marker
|
||||
requirements = self.distribution.install_requires + ['mock>=2.0.0', 'pytest-cov', 'pytest'] +\
|
||||
[v for k, v in self.distribution.extras_require.items() if not k.startswith(':') or evaluate_marker(k[1:])]
|
||||
self.distribution.fetch_build_eggs(requirements)
|
||||
|
||||
requirements = set(self.distribution.install_requires + ['mock>=2.0.0', 'pytest-cov', 'pytest'])
|
||||
for k, v in self.distribution.extras_require.items():
|
||||
if not k.startswith(':') or evaluate_marker(k[1:]):
|
||||
requirements.update(v)
|
||||
|
||||
self.distribution.fetch_build_eggs(list(requirements))
|
||||
self.run_tests()
|
||||
|
||||
|
||||
@@ -106,7 +160,7 @@ def read(fname):
|
||||
|
||||
def setup_package(version):
|
||||
# Assemble additional setup commands
|
||||
cmdclass = {'test': PyTest}
|
||||
cmdclass = {'test': PyTest, 'flake8': Flake8}
|
||||
|
||||
install_requires = []
|
||||
|
||||
@@ -116,8 +170,8 @@ def setup_package(version):
|
||||
continue
|
||||
extra = False
|
||||
for e, v in EXTRAS_REQUIRE.items():
|
||||
if r.startswith(v[0]):
|
||||
EXTRAS_REQUIRE[e] = [r]
|
||||
if v and r.startswith(v[0]):
|
||||
EXTRAS_REQUIRE[e] = [r] if e != 'kubernetes' or sys.version_info < (3, 0, 0) else []
|
||||
extra = True
|
||||
if not extra:
|
||||
install_requires.append(r)
|
||||
|
||||
+15
-11
@@ -1,4 +1,3 @@
|
||||
import datetime
|
||||
import os
|
||||
import shutil
|
||||
import unittest
|
||||
@@ -23,6 +22,7 @@ class MockResponse(object):
|
||||
def __init__(self, status_code=200):
|
||||
self.status_code = status_code
|
||||
self.content = '{}'
|
||||
self.reason = 'Not Found'
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
@@ -36,6 +36,10 @@ class MockResponse(object):
|
||||
def getheader(*args):
|
||||
return ''
|
||||
|
||||
@staticmethod
|
||||
def getheaders():
|
||||
return {'content-type': 'json'}
|
||||
|
||||
|
||||
def requests_get(url, **kwargs):
|
||||
members = '[{"id":14855829450254237642,"peerURLs":["http://localhost:2380","http://localhost:7001"],' +\
|
||||
@@ -66,6 +70,7 @@ class MockPostmaster(object):
|
||||
self.wait_for_user_backends_to_close = Mock()
|
||||
self.signal_stop = Mock(return_value=None)
|
||||
self.wait = Mock()
|
||||
self.signal_kill = Mock(return_value=False)
|
||||
|
||||
|
||||
class MockCursor(object):
|
||||
@@ -75,6 +80,7 @@ class MockCursor(object):
|
||||
self.closed = False
|
||||
self.rowcount = 0
|
||||
self.results = []
|
||||
self.description = [Mock()]
|
||||
|
||||
def execute(self, sql, *params):
|
||||
if sql.startswith('blabla'):
|
||||
@@ -86,13 +92,13 @@ class MockCursor(object):
|
||||
elif sql.startswith('SELECT slot_name'):
|
||||
self.results = [('blabla', 'physical'), ('foobar', 'physical'), ('ls', 'logical', 'a', 'b')]
|
||||
elif sql.startswith('SELECT CASE WHEN pg_catalog.pg_is_in_recovery()'):
|
||||
self.results = [(1, 2, 1)]
|
||||
self.results = [(1, 2, 1, 0, False, 1, 1, None, None)]
|
||||
elif sql.startswith('SELECT pg_catalog.pg_is_in_recovery()'):
|
||||
self.results = [(False, 2)]
|
||||
elif sql.startswith('SELECT pg_catalog.to_char'):
|
||||
replication_info = '[{"application_name":"walreceiver","client_addr":"1.2.3.4",' +\
|
||||
'"state":"streaming","sync_state":"async","sync_priority":0}]'
|
||||
self.results = [('', 0, '', '', '', '', False, replication_info)]
|
||||
self.results = [('', 0, '', 0, '', '', False, replication_info)]
|
||||
elif sql.startswith('SELECT name, setting'):
|
||||
self.results = [('wal_segment_size', '2048', '8kB', 'integer', 'internal'),
|
||||
('wal_block_size', '8192', None, 'integer', 'internal'),
|
||||
@@ -104,12 +110,7 @@ class MockCursor(object):
|
||||
('autovacuum', 'on', None, 'bool', 'sighup'),
|
||||
('unix_socket_directories', '/tmp', None, 'string', 'postmaster')]
|
||||
elif sql.startswith('IDENTIFY_SYSTEM'):
|
||||
self.results = [('1', 2, '0/402EEC0', '')]
|
||||
elif sql.startswith('SELECT isdir, modification'):
|
||||
self.results = [(False, datetime.datetime.now())]
|
||||
elif sql.startswith('SELECT pg_catalog.pg_read_file'):
|
||||
self.results = [('1\t0/40159C0\tno recovery target specified\n\n'
|
||||
'2\t1/40159C0\tno recovery target specified\n',)]
|
||||
self.results = [('1', 3, '0/402EEC0', '')]
|
||||
elif sql.startswith('TIMELINE_HISTORY '):
|
||||
self.results = [('', b'x\t0/40159C0\tno recovery target specified\n\n'
|
||||
b'1\t0/40159C0\tno recovery target specified\n\n'
|
||||
@@ -164,10 +165,13 @@ class PostgresInit(unittest.TestCase):
|
||||
'search_path': 'public', 'hot_standby': 'on', 'max_wal_senders': 5,
|
||||
'wal_keep_segments': 8, 'wal_log_hints': 'on', 'max_locks_per_transaction': 64,
|
||||
'max_worker_processes': 8, 'max_connections': 100, 'max_prepared_transactions': 0,
|
||||
'track_commit_timestamp': 'off', 'unix_socket_directories': '/tmp', 'trigger_file': 'bla',
|
||||
'stats_temp_directory': '/tmp'}
|
||||
'track_commit_timestamp': 'off', 'unix_socket_directories': '/tmp',
|
||||
'trigger_file': 'bla', 'stats_temp_directory': '/tmp', 'zero_damaged_pages': '',
|
||||
'force_parallel_mode': '1', 'constraint_exclusion': '',
|
||||
'max_stack_depth': 'Z', 'vacuum_cost_limit': -1, 'vacuum_cost_delay': 200}
|
||||
|
||||
@patch('psycopg2.connect', psycopg2_connect)
|
||||
@patch('patroni.postgresql.CallbackExecutor', Mock())
|
||||
@patch.object(ConfigHandler, 'write_postgresql_conf', Mock())
|
||||
@patch.object(ConfigHandler, 'replace_pg_hba', Mock())
|
||||
@patch.object(ConfigHandler, 'replace_pg_ident', Mock())
|
||||
|
||||
+64
-18
@@ -30,6 +30,8 @@ class MockPostgresql(object):
|
||||
pending_restart = True
|
||||
wal_name = 'wal'
|
||||
lsn_name = 'lsn'
|
||||
POSTMASTER_START_TIME = 'pg_catalog.to_char(pg_catalog.pg_postmaster_start_time'
|
||||
TL_LSN = 'CASE WHEN pg_catalog.pg_is_in_recovery()'
|
||||
|
||||
@staticmethod
|
||||
def connection():
|
||||
@@ -146,7 +148,8 @@ class MockRestApiServer(RestApiServer):
|
||||
self.serve_forever = Mock()
|
||||
MockRestApiServer._BaseServer__is_shut_down = Mock()
|
||||
MockRestApiServer._BaseServer__shutdown_request = True
|
||||
config = config or {'listen': '127.0.0.1:8008', 'auth': 'test:test', 'certfile': 'dumb', 'verify_client': 'a'}
|
||||
config = config or {'listen': '127.0.0.1:8008', 'auth': 'test:test', 'certfile': 'dumb', 'verify_client': 'a',
|
||||
'http_extra_headers': {'foo': 'bar'}, 'https_extra_headers': {'foo': 'sbar'}}
|
||||
super(MockRestApiServer, self).__init__(MockPatroni(), config)
|
||||
Handler(MockRequest(request), ('0.0.0.0', 8080), self)
|
||||
|
||||
@@ -159,7 +162,11 @@ class TestRestApiHandler(unittest.TestCase):
|
||||
_authorization = '\nAuthorization: Basic dGVzdDp0ZXN0'
|
||||
|
||||
def test_do_GET(self):
|
||||
MockPatroni.dcs.cluster.last_leader_operation = 20
|
||||
MockRestApiServer(RestApiHandler, 'GET /replica')
|
||||
MockRestApiServer(RestApiHandler, 'GET /replica?lag=1M')
|
||||
MockRestApiServer(RestApiHandler, 'GET /replica?lag=10MB')
|
||||
MockRestApiServer(RestApiHandler, 'GET /replica?lag=10485760')
|
||||
MockRestApiServer(RestApiHandler, 'GET /read-only')
|
||||
with patch.object(RestApiHandler, 'get_postgresql_status', Mock(return_value={})):
|
||||
MockRestApiServer(RestApiHandler, 'GET /replica')
|
||||
@@ -168,15 +175,17 @@ class TestRestApiHandler(unittest.TestCase):
|
||||
with patch.object(RestApiHandler, 'get_postgresql_status', Mock(return_value={'state': 'running'})):
|
||||
MockRestApiServer(RestApiHandler, 'GET /health')
|
||||
MockRestApiServer(RestApiHandler, 'GET /master')
|
||||
MockPatroni.dcs.cluster.sync.sync_standby = MockPostgresql.name
|
||||
MockPatroni.dcs.cluster.sync.members = [MockPostgresql.name]
|
||||
MockPatroni.dcs.cluster.is_synchronous_mode = Mock(return_value=True)
|
||||
with patch.object(RestApiHandler, 'get_postgresql_status', Mock(return_value={'role': 'replica'})):
|
||||
MockRestApiServer(RestApiHandler, 'GET /synchronous')
|
||||
with patch.object(RestApiHandler, 'get_postgresql_status', Mock(return_value={'role': 'replica'})):
|
||||
MockPatroni.dcs.cluster.sync.sync_standby = ''
|
||||
MockPatroni.dcs.cluster.sync.members = []
|
||||
MockRestApiServer(RestApiHandler, 'GET /asynchronous')
|
||||
MockPatroni.ha.is_leader = Mock(return_value=True)
|
||||
MockRestApiServer(RestApiHandler, 'GET /replica')
|
||||
with patch.object(MockHa, 'is_leader', Mock(return_value=True)):
|
||||
MockRestApiServer(RestApiHandler, 'GET /replica')
|
||||
with patch.object(MockHa, 'is_standby_cluster', Mock(return_value=True)):
|
||||
MockRestApiServer(RestApiHandler, 'GET /standby_leader')
|
||||
MockPatroni.dcs.cluster = None
|
||||
with patch.object(RestApiHandler, 'get_postgresql_status', Mock(return_value={'role': 'master'})):
|
||||
MockRestApiServer(RestApiHandler, 'GET /master')
|
||||
@@ -191,6 +200,16 @@ class TestRestApiHandler(unittest.TestCase):
|
||||
def test_do_OPTIONS(self):
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'OPTIONS / HTTP/1.0'))
|
||||
|
||||
def test_do_GET_liveness(self):
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'GET /liveness HTTP/1.0'))
|
||||
|
||||
def test_do_GET_readiness(self):
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'GET /readiness HTTP/1.0'))
|
||||
with patch.object(MockHa, 'is_leader', Mock(return_value=True)):
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'GET /readiness HTTP/1.0'))
|
||||
with patch.object(MockPostgresql, 'state', PropertyMock(return_value='stopped')):
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'GET /readiness HTTP/1.0'))
|
||||
|
||||
@patch.object(MockPostgresql, 'state', PropertyMock(return_value='stopped'))
|
||||
def test_do_GET_patroni(self):
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'GET /patroni'))
|
||||
@@ -201,8 +220,8 @@ class TestRestApiHandler(unittest.TestCase):
|
||||
|
||||
@patch.object(MockPatroni, 'dcs')
|
||||
def test_do_GET_cluster(self, mock_dcs):
|
||||
mock_dcs.cluster = get_cluster_initialized_without_leader()
|
||||
mock_dcs.cluster.members[1].data['xlog_location'] = 11
|
||||
mock_dcs.get_cluster.return_value = get_cluster_initialized_without_leader()
|
||||
mock_dcs.get_cluster.return_value.members[1].data['xlog_location'] = 11
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'GET /cluster'))
|
||||
|
||||
@patch.object(MockPatroni, 'dcs')
|
||||
@@ -232,6 +251,8 @@ class TestRestApiHandler(unittest.TestCase):
|
||||
MockRestApiServer(RestApiHandler, request)
|
||||
mock_dcs.set_config_value.return_value = False
|
||||
MockRestApiServer(RestApiHandler, request)
|
||||
mock_dcs.get_cluster.return_value.config = None
|
||||
MockRestApiServer(RestApiHandler, request)
|
||||
|
||||
@patch.object(MockPatroni, 'dcs')
|
||||
def test_do_PUT_config(self, mock_dcs):
|
||||
@@ -307,6 +328,15 @@ class TestRestApiHandler(unittest.TestCase):
|
||||
request = 'DELETE /restart HTTP/1.0' + self._authorization
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, request))
|
||||
|
||||
@patch.object(MockPatroni, 'dcs')
|
||||
def test_do_DELETE_switchover(self, mock_dcs):
|
||||
request = 'DELETE /switchover HTTP/1.0' + self._authorization
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, request))
|
||||
mock_dcs.manual_failover.return_value = False
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, request))
|
||||
mock_dcs.get_cluster.return_value.failover = None
|
||||
self.assertIsNotNone(MockRestApiServer(RestApiHandler, request))
|
||||
|
||||
@patch.object(MockPatroni, 'dcs')
|
||||
def test_do_POST_reinitialize(self, mock_dcs):
|
||||
cluster = mock_dcs.get_cluster.return_value
|
||||
@@ -413,25 +443,27 @@ class TestRestApiHandler(unittest.TestCase):
|
||||
MockRestApiServer(RestApiHandler, post + '37\n\n{"candidate":"2","scheduled_at": "1"}')
|
||||
|
||||
|
||||
@patch('ssl.SSLContext.load_cert_chain', Mock())
|
||||
@patch('ssl.SSLContext.wrap_socket', Mock(return_value=0))
|
||||
@patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock())
|
||||
class TestRestApiServer(unittest.TestCase):
|
||||
|
||||
@patch('ssl.SSLContext.load_cert_chain', Mock())
|
||||
@patch('ssl.SSLContext.wrap_socket', Mock(return_value=0))
|
||||
@patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock())
|
||||
def setUp(self):
|
||||
self.srv = MockRestApiServer(Mock(), '', {'listen': '*:8008', 'certfile': 'a', 'verify_client': 'required'})
|
||||
|
||||
@patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock())
|
||||
def test_reload_config(self):
|
||||
bad_config = {'listen': 'foo'}
|
||||
self.assertRaises(ValueError, MockRestApiServer, None, '', bad_config)
|
||||
srv = MockRestApiServer(Mock(), '', {'listen': '*:8008', 'certfile': 'a', 'verify_client': 'required'})
|
||||
self.assertRaises(ValueError, srv.reload_config, bad_config)
|
||||
self.assertRaises(ValueError, srv.reload_config, {})
|
||||
self.assertRaises(ValueError, self.srv.reload_config, bad_config)
|
||||
self.assertRaises(ValueError, self.srv.reload_config, {})
|
||||
with patch.object(socket.socket, 'setsockopt', Mock(side_effect=socket.error)):
|
||||
srv.reload_config({'listen': ':8008'})
|
||||
self.srv.reload_config({'listen': ':8008'})
|
||||
|
||||
def test_check_auth(self):
|
||||
srv = MockRestApiServer(Mock(), '', {'listen': '*:8008', 'certfile': 'a', 'verify_client': 'required'})
|
||||
mock_rh = Mock()
|
||||
mock_rh.request.getpeercert.return_value = None
|
||||
self.assertIsNot(srv.check_auth(mock_rh), True)
|
||||
self.assertIsNot(self.srv.check_auth(mock_rh), True)
|
||||
|
||||
def test_handle_error(self):
|
||||
try:
|
||||
@@ -439,6 +471,20 @@ class TestRestApiServer(unittest.TestCase):
|
||||
except Exception:
|
||||
self.assertIsNone(MockRestApiServer.handle_error(None, ('127.0.0.1', 55555)))
|
||||
|
||||
@patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock(side_effect=socket.error))
|
||||
def test_socket_error(self):
|
||||
with patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock(side_effect=socket.error)):
|
||||
self.assertRaises(socket.error, MockRestApiServer, Mock(), '', {'listen': '*:8008'})
|
||||
self.assertRaises(socket.error, MockRestApiServer, Mock(), '', {'listen': '*:8008'})
|
||||
|
||||
@patch.object(MockRestApiServer, 'finish_request', Mock())
|
||||
def test_process_request_thread(self):
|
||||
mock_socket = Mock()
|
||||
self.srv.process_request_thread((mock_socket, 1), '2')
|
||||
mock_socket.context.wrap_socket.side_effect = socket.error
|
||||
self.srv.process_request_thread((mock_socket, 1), '2')
|
||||
|
||||
@patch.object(socket.socket, 'accept')
|
||||
def test_get_request(self, mock_accept):
|
||||
newsock = Mock()
|
||||
mock_accept.return_value = (newsock, '2')
|
||||
self.srv.socket = Mock()
|
||||
self.assertEqual(self.srv.get_request(), ((self.srv.socket, newsock), '2'))
|
||||
|
||||
@@ -13,7 +13,7 @@ class TestCancellableSubprocess(unittest.TestCase):
|
||||
|
||||
def test_call(self):
|
||||
self.c.cancel()
|
||||
self.assertRaises(PostgresException, self.c.call, communicate_input=None)
|
||||
self.assertRaises(PostgresException, self.c.call)
|
||||
|
||||
def test__kill_children(self):
|
||||
self.c._process_children = [Mock()]
|
||||
|
||||
+46
-1
@@ -1,9 +1,10 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import io
|
||||
|
||||
from mock import MagicMock, Mock, patch
|
||||
from patroni.config import Config
|
||||
from patroni.config import Config, ConfigParseError
|
||||
from six.moves import builtins
|
||||
|
||||
|
||||
@@ -55,6 +56,7 @@ class TestConfig(unittest.TestCase):
|
||||
'PATRONI_ZOOKEEPER_HOSTS': "'host1:2181','host2:2181'",
|
||||
'PATRONI_EXHIBITOR_HOSTS': 'host1,host2',
|
||||
'PATRONI_EXHIBITOR_PORT': '8181',
|
||||
'PATRONI_RAFT_PARTNER_ADDRS': "'host1:1234','host2:1234'",
|
||||
'PATRONI_foo_HOSTS': '[host1,host2', # Exception in parse_list
|
||||
'PATRONI_SUPERUSER_USERNAME': 'postgres',
|
||||
'PATRONI_SUPERUSER_PASSWORD': 'zalando',
|
||||
@@ -94,3 +96,46 @@ class TestConfig(unittest.TestCase):
|
||||
self.config.set_dynamic_configuration(dynamic_configuration)
|
||||
for name, value in dynamic_configuration['standby_cluster'].items():
|
||||
self.assertEqual(self.config['standby_cluster'][name], value)
|
||||
|
||||
@patch('os.path.exists', Mock(return_value=True))
|
||||
@patch('os.path.isfile', Mock(side_effect=lambda fname: fname != 'postgres0'))
|
||||
@patch('os.path.isdir', Mock(return_value=True))
|
||||
@patch('os.listdir', Mock(return_value=['01-specific.yml', '00-base.yml']))
|
||||
def test_configuration_directory(self):
|
||||
def open_mock(fname, *args, **kwargs):
|
||||
if fname.endswith('00-base.yml'):
|
||||
return io.StringIO(
|
||||
u'''
|
||||
test: True
|
||||
test2:
|
||||
child-1: somestring
|
||||
child-2: 5
|
||||
child-3: False
|
||||
test3: True
|
||||
test4:
|
||||
- abc: 3
|
||||
- abc: 4
|
||||
''')
|
||||
elif fname.endswith('01-specific.yml'):
|
||||
return io.StringIO(
|
||||
u'''
|
||||
test: False
|
||||
test2:
|
||||
child-2: 10
|
||||
child-3: !!null
|
||||
test4:
|
||||
- ab: 5
|
||||
new-attr: True
|
||||
''')
|
||||
|
||||
with patch.object(builtins, 'open', MagicMock(side_effect=open_mock)):
|
||||
config = Config('postgres0')
|
||||
self.assertEqual(config._local_configuration,
|
||||
{'test': False, 'test2': {'child-1': 'somestring', 'child-2': 10},
|
||||
'test3': True, 'test4': [{'ab': 5}], 'new-attr': True})
|
||||
|
||||
@patch('os.path.exists', Mock(return_value=True))
|
||||
@patch('os.path.isfile', Mock(return_value=False))
|
||||
@patch('os.path.isdir', Mock(return_value=False))
|
||||
def test_invalid_path(self):
|
||||
self.assertRaises(ConfigParseError, Config, 'postgres0')
|
||||
|
||||
@@ -122,6 +122,8 @@ class TestConsul(unittest.TestCase):
|
||||
self.c.refresh_session = Mock(return_value=True)
|
||||
for _ in range(0, 4):
|
||||
self.c.touch_member({'balbla': 'blabla'})
|
||||
self.c.refresh_session = Mock(side_effect=ConsulError('foo'))
|
||||
self.assertFalse(self.c.touch_member({'balbla': 'blabla'}))
|
||||
|
||||
@patch.object(consul.Consul.KV, 'put', Mock(side_effect=InvalidSession))
|
||||
def test_take_leader(self):
|
||||
|
||||
+76
-22
@@ -7,8 +7,8 @@ from datetime import datetime, timedelta
|
||||
from mock import patch, Mock
|
||||
from patroni.ctl import ctl, store_config, load_config, output_members, get_dcs, parse_dcs, \
|
||||
get_all_members, get_any_member, get_cursor, query_member, configure, PatroniCtlException, apply_config_changes, \
|
||||
format_config_for_editing, show_diff, invoke_editor, format_pg_version, find_executable
|
||||
from patroni.dcs.etcd import Client, Failover
|
||||
format_config_for_editing, show_diff, invoke_editor, format_pg_version, find_executable, CONFIG_FILE_PATH
|
||||
from patroni.dcs.etcd import AbstractEtcdClientWithFailover, Failover
|
||||
from patroni.utils import tzutc
|
||||
from psycopg2 import OperationalError
|
||||
from urllib3 import PoolManager
|
||||
@@ -18,31 +18,36 @@ from .test_etcd import etcd_read, socket_getaddrinfo
|
||||
from .test_ha import get_cluster_initialized_without_leader, get_cluster_initialized_with_leader, \
|
||||
get_cluster_initialized_with_only_leader, get_cluster_not_initialized_without_leader, get_cluster, Member
|
||||
|
||||
CONFIG_FILE_PATH = './test-ctl.yaml'
|
||||
|
||||
|
||||
def test_rw_config():
|
||||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
load_config(CONFIG_FILE_PATH + '/dummy', None)
|
||||
store_config({'etcd': {'host': 'localhost:2379'}}, CONFIG_FILE_PATH + '/dummy')
|
||||
load_config(CONFIG_FILE_PATH + '/dummy', '0.0.0.0')
|
||||
os.remove(CONFIG_FILE_PATH + '/dummy')
|
||||
os.rmdir(CONFIG_FILE_PATH)
|
||||
load_config(CONFIG_FILE_PATH, None)
|
||||
CONFIG_PATH = './test-ctl.yaml'
|
||||
store_config({'etcd': {'host': 'localhost:2379'}}, CONFIG_PATH + '/dummy')
|
||||
load_config(CONFIG_PATH + '/dummy', '0.0.0.0')
|
||||
os.remove(CONFIG_PATH + '/dummy')
|
||||
os.rmdir(CONFIG_PATH)
|
||||
|
||||
|
||||
@patch('patroni.ctl.load_config',
|
||||
Mock(return_value={'scope': 'alpha', 'postgresql': {'data_dir': '.', 'parameters': {}, 'retry_timeout': 5},
|
||||
'restapi': {'listen': '::', 'certfile': 'a'}, 'etcd': {'host': 'localhost:2379'}}))
|
||||
@patch('patroni.ctl.load_config', Mock(return_value={
|
||||
'scope': 'alpha', 'restapi': {'listen': '::', 'certfile': 'a'}, 'etcd': {'host': 'localhost:2379'},
|
||||
'postgresql': {'data_dir': '.', 'pgpass': './pgpass', 'parameters': {}, 'retry_timeout': 5}}))
|
||||
class TestCtl(unittest.TestCase):
|
||||
|
||||
@patch('socket.getaddrinfo', socket_getaddrinfo)
|
||||
def setUp(self):
|
||||
with patch.object(Client, 'machines') as mock_machines:
|
||||
with patch.object(AbstractEtcdClientWithFailover, 'machines') as mock_machines:
|
||||
mock_machines.__get__ = Mock(return_value=['http://remotehost:2379'])
|
||||
self.runner = CliRunner()
|
||||
self.e = get_dcs({'etcd': {'ttl': 30, 'host': 'ok:2379', 'retry_timeout': 10}}, 'foo')
|
||||
|
||||
def test_load_config(self):
|
||||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
self.assertRaises(PatroniCtlException, load_config, './non-existing-config-file', None)
|
||||
self.assertRaises(PatroniCtlException, load_config, './non-existing-config-file', None)
|
||||
|
||||
@patch('psycopg2.connect', psycopg2_connect)
|
||||
def test_get_cursor(self):
|
||||
self.assertIsNone(get_cursor(get_cluster_initialized_without_leader(), {}, role='master'))
|
||||
@@ -67,10 +72,9 @@ class TestCtl(unittest.TestCase):
|
||||
def test_output_members(self):
|
||||
scheduled_at = datetime.now(tzutc) + timedelta(seconds=600)
|
||||
cluster = get_cluster_initialized_with_leader(Failover(1, 'foo', 'bar', scheduled_at))
|
||||
self.assertIsNone(output_members(cluster, name='abc', fmt='pretty'))
|
||||
self.assertIsNone(output_members(cluster, name='abc', fmt='json'))
|
||||
self.assertIsNone(output_members(cluster, name='abc', fmt='yaml'))
|
||||
self.assertIsNone(output_members(cluster, name='abc', fmt='tsv'))
|
||||
del cluster.members[1].data['conn_url']
|
||||
for fmt in ('pretty', 'json', 'yaml', 'tsv', 'topology'):
|
||||
self.assertIsNone(output_members(cluster, name='abc', fmt=fmt))
|
||||
|
||||
@patch('patroni.ctl.get_dcs')
|
||||
@patch.object(PoolManager, 'request', Mock(return_value=MockResponse()))
|
||||
@@ -200,9 +204,6 @@ class TestCtl(unittest.TestCase):
|
||||
rows = query_member(None, None, None, 'master', 'SELECT pg_catalog.pg_is_in_recovery()', {})
|
||||
self.assertTrue('False' in str(rows))
|
||||
|
||||
rows = query_member(None, None, None, 'replica', 'SELECT pg_catalog.pg_is_in_recovery()', {})
|
||||
self.assertEqual(rows, (None, None))
|
||||
|
||||
with patch.object(MockCursor, 'execute', Mock(side_effect=OperationalError('bla'))):
|
||||
rows = query_member(None, None, None, 'replica', 'SELECT pg_catalog.pg_is_in_recovery()', {})
|
||||
|
||||
@@ -416,9 +417,39 @@ class TestCtl(unittest.TestCase):
|
||||
assert '2100' in result.output
|
||||
assert 'Scheduled restart' in result.output
|
||||
|
||||
@patch('patroni.ctl.get_dcs')
|
||||
def test_topology(self, mock_get_dcs):
|
||||
mock_get_dcs.return_value = self.e
|
||||
cluster = get_cluster_initialized_with_leader()
|
||||
cascade_member = Member(0, 'cascade', 28, {'conn_url': 'postgres://replicator:[email protected]:5437/postgres',
|
||||
'api_url': 'http://127.0.0.1:8012/patroni',
|
||||
'state': 'running',
|
||||
'tags': {'replicatefrom': 'other'},
|
||||
})
|
||||
cascade_member_wrong_tags = Member(0, 'wrong_cascade', 28,
|
||||
{'conn_url': 'postgres://replicator:[email protected]:5438/postgres',
|
||||
'api_url': 'http://127.0.0.1:8013/patroni',
|
||||
'state': 'running',
|
||||
'tags': {'replicatefrom': 'nonexistinghost'},
|
||||
})
|
||||
cluster.members.append(cascade_member)
|
||||
cluster.members.append(cascade_member_wrong_tags)
|
||||
mock_get_dcs.return_value.get_cluster = Mock(return_value=cluster)
|
||||
result = self.runner.invoke(ctl, ['topology', 'dummy'])
|
||||
assert '+\n| leader | 127.0.0.1:5435 | Leader |' in result.output
|
||||
assert '|\n| + other | 127.0.0.1:5436 | Replica |' in result.output
|
||||
assert '|\n| + cascade | 127.0.0.1:5437 | Replica |' in result.output
|
||||
assert '|\n| + wrong_cascade | 127.0.0.1:5438 | Replica |' in result.output
|
||||
|
||||
cluster = get_cluster_initialized_without_leader()
|
||||
mock_get_dcs.return_value.get_cluster = Mock(return_value=cluster)
|
||||
result = self.runner.invoke(ctl, ['topology', 'dummy'])
|
||||
assert '+\n| + leader | 127.0.0.1:5435 | Replica |' in result.output
|
||||
assert '|\n| + other | 127.0.0.1:5436 | Replica |' in result.output
|
||||
|
||||
@patch('patroni.ctl.get_dcs')
|
||||
@patch.object(PoolManager, 'request', Mock(return_value=MockResponse()))
|
||||
def test_flush(self, mock_get_dcs):
|
||||
def test_flush_restart(self, mock_get_dcs):
|
||||
mock_get_dcs.return_value = self.e
|
||||
mock_get_dcs.return_value.get_cluster = get_cluster_initialized_with_leader
|
||||
|
||||
@@ -431,6 +462,26 @@ class TestCtl(unittest.TestCase):
|
||||
result = self.runner.invoke(ctl, ['flush', 'dummy', 'restart', '--force'])
|
||||
assert 'Failed: flush scheduled restart' in result.output
|
||||
|
||||
@patch('patroni.ctl.get_dcs')
|
||||
@patch.object(PoolManager, 'request', Mock(return_value=MockResponse()))
|
||||
def test_flush_switchover(self, mock_get_dcs):
|
||||
mock_get_dcs.return_value = self.e
|
||||
|
||||
mock_get_dcs.return_value.get_cluster = get_cluster_initialized_with_leader
|
||||
result = self.runner.invoke(ctl, ['flush', 'dummy', 'switchover'])
|
||||
assert 'No pending scheduled switchover' in result.output
|
||||
|
||||
scheduled_at = datetime.now(tzutc) + timedelta(seconds=600)
|
||||
mock_get_dcs.return_value.get_cluster = Mock(
|
||||
return_value=get_cluster_initialized_with_leader(Failover(1, 'a', 'b', scheduled_at)))
|
||||
result = self.runner.invoke(ctl, ['flush', 'dummy', 'switchover'])
|
||||
assert result.output.startswith('Success: ')
|
||||
|
||||
mock_get_dcs.return_value.manual_failover = Mock()
|
||||
with patch.object(PoolManager, 'request', side_effect=[MockResponse(409), Exception]):
|
||||
result = self.runner.invoke(ctl, ['flush', 'dummy', 'switchover'])
|
||||
assert 'Could not find any accessible member of cluster' in result.output
|
||||
|
||||
@patch.object(PoolManager, 'request')
|
||||
@patch('patroni.ctl.get_dcs')
|
||||
@patch('patroni.ctl.polling_loop', Mock(return_value=[1]))
|
||||
@@ -510,7 +561,7 @@ class TestCtl(unittest.TestCase):
|
||||
self.assertRaises(PatroniCtlException, apply_config_changes, before_editing, config, ['a'])
|
||||
|
||||
@patch('sys.stdout.isatty', return_value=False)
|
||||
@patch('cdiff.markup_to_pager')
|
||||
@patch('patroni.ctl.markup_to_pager')
|
||||
def test_show_diff(self, mock_markup_to_pager, mock_isatty):
|
||||
show_diff("foo:\n bar: 1\n", "foo:\n bar: 2\n")
|
||||
mock_markup_to_pager.assert_not_called()
|
||||
@@ -519,6 +570,9 @@ class TestCtl(unittest.TestCase):
|
||||
show_diff("foo:\n bar: 1\n", "foo:\n bar: 2\n")
|
||||
mock_markup_to_pager.assert_called_once()
|
||||
|
||||
with patch('patroni.ctl.find_executable', Mock(return_value=None)):
|
||||
show_diff("foo:\n bar: 1\n", "foo:\n bar: 2\n")
|
||||
|
||||
# Test that unicode handling doesn't fail with an exception
|
||||
show_diff(b"foo:\n bar: \xc3\xb6\xc3\xb6\n".decode('utf-8'),
|
||||
b"foo:\n bar: \xc3\xbc\xc3\xbc\n".decode('utf-8'))
|
||||
|
||||
+40
-29
@@ -5,7 +5,7 @@ import unittest
|
||||
|
||||
from dns.exception import DNSException
|
||||
from mock import Mock, patch
|
||||
from patroni.dcs.etcd import AbstractDCS, Client, Cluster, Etcd, EtcdError, DnsCachingResolver
|
||||
from patroni.dcs.etcd import AbstractDCS, EtcdClient, Cluster, Etcd, EtcdError, DnsCachingResolver
|
||||
from patroni.exceptions import DCSError
|
||||
from patroni.utils import Retry
|
||||
from urllib3.exceptions import ReadTimeoutError
|
||||
@@ -17,7 +17,7 @@ def etcd_watch(self, key, index=None, timeout=None, recursive=None):
|
||||
if timeout == 2.0:
|
||||
raise etcd.EtcdWatchTimedOut
|
||||
elif timeout == 5.0:
|
||||
return etcd.EtcdResult('delete', {})
|
||||
return etcd.EtcdResult('compareAndSwap', {})
|
||||
elif 5 < timeout <= 10.0:
|
||||
raise etcd.EtcdException
|
||||
elif timeout == 20.0:
|
||||
@@ -123,19 +123,18 @@ class TestClient(unittest.TestCase):
|
||||
@patch('socket.getaddrinfo', socket_getaddrinfo)
|
||||
@patch('patroni.dcs.etcd.requests_get', requests_get)
|
||||
def setUp(self):
|
||||
with patch.object(Client, 'machines') as mock_machines:
|
||||
with patch.object(EtcdClient, 'machines') as mock_machines:
|
||||
mock_machines.__get__ = Mock(return_value=['http://localhost:2379', 'http://localhost:4001'])
|
||||
self.client = Client({'srv': 'test', 'retry_timeout': 3}, DnsCachingResolver())
|
||||
self.client = EtcdClient({'srv': 'test', 'retry_timeout': 3}, DnsCachingResolver())
|
||||
self.client.http.request = http_request
|
||||
self.client.http.request_encode_body = http_request
|
||||
|
||||
def test_machines(self):
|
||||
self.client._base_uri = 'http://localhost:4001'
|
||||
self.client._machines_cache = ['http://localhost:2379']
|
||||
self.client._base_uri = 'http://localhost:4002'
|
||||
self.client._machines_cache = ['http://localhost:4002', 'http://localhost:2379']
|
||||
self.assertIsNotNone(self.client.machines)
|
||||
self.client._base_uri = 'http://localhost:4001'
|
||||
self.client._machines_cache = []
|
||||
self.assertIsNotNone(self.client.machines)
|
||||
self.client._machines_cache = ['http://localhost:4001']
|
||||
self.client._update_machines_cache = True
|
||||
machines = None
|
||||
try:
|
||||
@@ -144,29 +143,31 @@ class TestClient(unittest.TestCase):
|
||||
except Exception:
|
||||
self.assertIsNone(machines)
|
||||
|
||||
@patch.object(Client, 'machines')
|
||||
@patch.object(EtcdClient, 'machines')
|
||||
def test_api_execute(self, mock_machines):
|
||||
mock_machines.__get__ = Mock(return_value=['http://localhost:2379'])
|
||||
self.assertRaises(ValueError, self.client.api_execute, '', '')
|
||||
mock_machines.__get__ = Mock(return_value=['http://localhost:4001', 'http://localhost:2379'])
|
||||
self.client._base_uri = 'http://localhost:4001'
|
||||
self.client._machines_cache = ['http://localhost:2379']
|
||||
self.assertRaises(etcd.EtcdException, self.client.api_execute, '/', 'POST', timeout=0)
|
||||
self.client._base_uri = 'http://localhost:4001'
|
||||
self.client._machines_cache = ['http://localhost:2379']
|
||||
rtry = Retry(deadline=10, max_delay=1, max_tries=-1, retry_exceptions=(etcd.EtcdLeaderElectionInProgress,))
|
||||
rtry(self.client.api_execute, '/', 'POST', timeout=0, params={'retry': rtry})
|
||||
mock_machines.__get__ = Mock(return_value=['http://localhost:2379'])
|
||||
self.client._machines_cache_updated = 0
|
||||
self.client.api_execute('/', 'POST', timeout=0)
|
||||
self.client._machines_cache = [self.client._base_uri]
|
||||
self.assertRaises(etcd.EtcdWatchTimedOut, self.client.api_execute, '/timeout', 'POST', params={'wait': 'true'})
|
||||
self.assertRaises(etcd.EtcdWatchTimedOut, self.client.api_execute, '/timeout', 'POST', params={'wait': 'true'})
|
||||
self.assertRaises(etcd.EtcdException, self.client.api_execute, '/', '')
|
||||
|
||||
with patch.object(Client, '_do_http_request', Mock(side_effect=etcd.EtcdConnectionFailed)):
|
||||
with patch.object(Client, '_calculate_timeouts', Mock(side_effect=[(1, 1, 0), (1, 1, 0), (0, 1, 0)])):
|
||||
self.assertRaises(etcd.EtcdException, rtry, self.client.api_execute, '/', 'GET', params={'retry': rtry})
|
||||
self.client._read_timeout = 0
|
||||
with patch.object(EtcdClient, '_calculate_timeouts', Mock(side_effect=[(1, 1, 0), (1, 1, 0), (0, 1, 0)])),\
|
||||
patch.object(EtcdClient, '_load_machines_cache', Mock(side_effect=Exception)):
|
||||
self.client.http.request = Mock(side_effect=socket.error)
|
||||
self.assertRaises(etcd.EtcdException, rtry, self.client.api_execute, '/', 'GET', params={'retry': rtry})
|
||||
|
||||
with patch.object(EtcdClient, '_calculate_timeouts', Mock(side_effect=[(1, 1, 0), (1, 1, 0), (0, 1, 0)])),\
|
||||
patch.object(EtcdClient, '_load_machines_cache', Mock(return_value=True)):
|
||||
self.assertRaises(etcd.EtcdException, rtry, self.client.api_execute, '/', 'GET', params={'retry': rtry})
|
||||
|
||||
with patch.object(EtcdClient, '_do_http_request', Mock(side_effect=etcd.EtcdException)):
|
||||
self.client._read_timeout = 0.01
|
||||
self.assertRaises(etcd.EtcdException, self.client.api_execute, '/', 'GET')
|
||||
|
||||
def test_get_srv_record(self):
|
||||
@@ -181,9 +182,13 @@ class TestClient(unittest.TestCase):
|
||||
def test__get_machines_cache_from_dns(self):
|
||||
self.client._get_machines_cache_from_dns('error', 2379)
|
||||
|
||||
@patch.object(Client, 'machines')
|
||||
def test__load_machines_cache(self, mock_machines):
|
||||
mock_machines.__get__ = Mock(return_value=['http://localhost:2379'])
|
||||
@patch.object(EtcdClient, 'machines')
|
||||
def test__refresh_machines_cache(self, mock_machines):
|
||||
mock_machines.__get__ = Mock(side_effect=etcd.EtcdConnectionFailed)
|
||||
self.assertIsNone(self.client._refresh_machines_cache())
|
||||
self.assertRaises(etcd.EtcdException, self.client._refresh_machines_cache, True)
|
||||
|
||||
def test__load_machines_cache(self):
|
||||
self.client._config = {}
|
||||
self.assertRaises(Exception, self.client._load_machines_cache)
|
||||
self.client._config = {'srv': 'blabla'}
|
||||
@@ -198,6 +203,10 @@ class TestClient(unittest.TestCase):
|
||||
timeout=1, source_address=('localhost', 53333),
|
||||
socket_options=[(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)])
|
||||
|
||||
def test___del__(self):
|
||||
self.client.http.clear = Mock(side_effect=TypeError)
|
||||
del self.client
|
||||
|
||||
|
||||
@patch('patroni.dcs.etcd.requests_get', requests_get)
|
||||
@patch('socket.getaddrinfo', socket_getaddrinfo)
|
||||
@@ -208,7 +217,7 @@ class TestEtcd(unittest.TestCase):
|
||||
|
||||
@patch('socket.getaddrinfo', socket_getaddrinfo)
|
||||
def setUp(self):
|
||||
with patch.object(Client, 'machines') as mock_machines:
|
||||
with patch.object(EtcdClient, 'machines') as mock_machines:
|
||||
mock_machines.__get__ = Mock(return_value=['http://localhost:2379', 'http://localhost:4001'])
|
||||
self.etcd = Etcd({'namespace': '/patroni/', 'ttl': 30, 'retry_timeout': 10,
|
||||
'host': 'localhost:2379', 'scope': 'test', 'name': 'foo'})
|
||||
@@ -219,17 +228,18 @@ class TestEtcd(unittest.TestCase):
|
||||
@patch('dns.resolver.query', dns_query)
|
||||
def test_get_etcd_client(self):
|
||||
with patch('time.sleep', Mock(side_effect=SleepException)),\
|
||||
patch.object(Client, 'machines') as mock_machines:
|
||||
patch.object(EtcdClient, 'machines') as mock_machines:
|
||||
mock_machines.__get__ = Mock(side_effect=etcd.EtcdException)
|
||||
self.assertRaises(SleepException, self.etcd.get_etcd_client,
|
||||
{'discovery_srv': 'test', 'retry_timeout': 10, 'cacert': '1', 'key': '1', 'cert': 1})
|
||||
{'discovery_srv': 'test', 'retry_timeout': 10, 'cacert': '1', 'key': '1', 'cert': 1},
|
||||
EtcdClient)
|
||||
self.assertRaises(SleepException, self.etcd.get_etcd_client,
|
||||
{'url': 'https://test:2379', 'retry_timeout': 10})
|
||||
{'url': 'https://test:2379', 'retry_timeout': 10}, EtcdClient)
|
||||
self.assertRaises(SleepException, self.etcd.get_etcd_client,
|
||||
{'hosts': 'foo:4001,bar', 'retry_timeout': 10})
|
||||
{'hosts': 'foo:4001,bar', 'retry_timeout': 10}, EtcdClient)
|
||||
mock_machines.__get__ = Mock(return_value=[])
|
||||
self.assertRaises(SleepException, self.etcd.get_etcd_client,
|
||||
{'proxy': 'https://user:password@test:2379', 'retry_timeout': 10})
|
||||
{'proxy': 'https://user:password@test:2379', 'retry_timeout': 10}, EtcdClient)
|
||||
|
||||
def test_get_cluster(self):
|
||||
cluster = self.etcd.get_cluster()
|
||||
@@ -278,7 +288,8 @@ class TestEtcd(unittest.TestCase):
|
||||
self.etcd.watch(None, 0)
|
||||
self.etcd.get_cluster()
|
||||
self.etcd.watch(20729, 1.5)
|
||||
self.etcd.watch(20729, 4.5)
|
||||
with patch('time.sleep', Mock()):
|
||||
self.etcd.watch(20729, 4.5)
|
||||
with patch.object(AbstractDCS, 'watch', Mock()):
|
||||
self.assertTrue(self.etcd.watch(20729, 19.5))
|
||||
self.assertRaises(SleepException, self.etcd.watch, 20729, 9.5)
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
import etcd
|
||||
import json
|
||||
import unittest
|
||||
import urllib3
|
||||
|
||||
from mock import Mock, patch
|
||||
from patroni.dcs.etcd3 import PatroniEtcd3Client, Cluster, Etcd3, Etcd3Error, Etcd3ClientError, RetryFailedError,\
|
||||
InvalidAuthToken, Unavailable, Unknown, UnsupportedEtcdVersion, UserEmpty, base64_encode
|
||||
from threading import Thread
|
||||
|
||||
from . import SleepException, MockResponse
|
||||
|
||||
|
||||
def mock_urlopen(self, method, url, **kwargs):
|
||||
ret = MockResponse()
|
||||
if method == 'GET' and url.endswith('/version'):
|
||||
ret.content = '{"etcdserver": "3.3.13", "etcdcluster": "3.3.0"}'
|
||||
elif method != 'POST':
|
||||
raise Exception('Unexpected request method: {0} {1} {2}'.format(method, url, kwargs))
|
||||
elif url.endswith('/cluster/member/list'):
|
||||
ret.content = '{"members":[{"clientURLs":["http://localhost:2379", "http://localhost:4001"]}]}'
|
||||
elif url.endswith('/auth/authenticate'):
|
||||
ret.content = '{"token":"authtoken"}'
|
||||
elif url.endswith('/lease/grant'):
|
||||
ret.content = '{"ID": "123"}'
|
||||
elif url.endswith('/lease/keepalive'):
|
||||
ret.content = '{"result":{"TTL":30}}'
|
||||
elif url.endswith('/kv/range'):
|
||||
ret.content = json.dumps({
|
||||
"header": {"revision": "1"},
|
||||
"kvs": [
|
||||
{"key": base64_encode('/patroni/test/leader'),
|
||||
"value": base64_encode('foo'), "lease": "bla", "mod_revision": '1'},
|
||||
{"key": base64_encode('/patroni/test/members/foo'),
|
||||
"value": base64_encode('{}'), "lease": "123", "mod_revision": '1'},
|
||||
{"key": base64_encode('/patroni/test/failover'), "value": base64_encode('{}'), "mod_revision": '1'}
|
||||
]
|
||||
})
|
||||
elif url.endswith('/watch'):
|
||||
key = base64_encode('/patroni/test/config')
|
||||
ret.read_chunked = Mock(return_value=[json.dumps({
|
||||
'result': {'events': [
|
||||
{'kv': {'key': key, 'value': base64_encode('bar'), 'mod_revision': '2'}},
|
||||
{'kv': {'key': key, 'value': base64_encode('buzz'), 'mod_revision': '3'}},
|
||||
{'type': 'DELETE', 'kv': {'key': key, 'mod_revision': '4'}},
|
||||
{'kv': {'key': base64_encode('/patroni/test/optime/leader'),
|
||||
'value': base64_encode('1234567'), 'mod_revision': '5'}},
|
||||
]}
|
||||
})[:-1].encode('utf-8'), b'}{"error":{"grpc_code":14,"message":"","http_code":503}}'])
|
||||
elif url.endswith('/kv/put') or url.endswith('/kv/txn'):
|
||||
ret.status_code = 400
|
||||
ret.content = '{"code":5,"error":"etcdserver: requested lease not found"}'
|
||||
elif not url.endswith('/kv/deleterange'):
|
||||
raise Exception('Unexpected url: {0} {1} {2}'.format(method, url, kwargs))
|
||||
return ret
|
||||
|
||||
|
||||
class BaseTestEtcd3(unittest.TestCase):
|
||||
|
||||
@patch.object(Thread, 'start', Mock())
|
||||
@patch.object(urllib3.PoolManager, 'urlopen', mock_urlopen)
|
||||
def setUp(self):
|
||||
self.etcd3 = Etcd3({'namespace': '/patroni/', 'ttl': 30, 'retry_timeout': 10,
|
||||
'host': 'localhost:2378', 'scope': 'test', 'name': 'foo',
|
||||
'username': 'etcduser', 'password': 'etcdpassword'})
|
||||
self.client = self.etcd3._client
|
||||
self.kv_cache = self.client._kv_cache
|
||||
|
||||
|
||||
class TestKVCache(BaseTestEtcd3):
|
||||
|
||||
def test__do_watch(self):
|
||||
self.client.watchprefix = Mock(return_value=False)
|
||||
self.assertRaises(AttributeError, self.kv_cache._do_watch, '1')
|
||||
|
||||
@patch('time.sleep', Mock(side_effect=SleepException))
|
||||
@patch('patroni.dcs.etcd3.KVCache._build_cache', Mock(side_effect=Exception))
|
||||
def test_run(self):
|
||||
self.assertRaises(SleepException, self.kv_cache.run)
|
||||
|
||||
@patch.object(urllib3.PoolManager, 'urlopen', mock_urlopen)
|
||||
def test_kill_stream(self):
|
||||
self.assertRaises(Unavailable, self.kv_cache._do_watch, '1')
|
||||
self.kv_cache.kill_stream()
|
||||
with patch.object(MockResponse, 'connection', create=True) as mock_conn:
|
||||
self.kv_cache.kill_stream()
|
||||
mock_conn.sock.close.side_effect = Exception
|
||||
self.kv_cache.kill_stream()
|
||||
|
||||
|
||||
class TestPatroniEtcd3Client(BaseTestEtcd3):
|
||||
|
||||
@patch('patroni.dcs.etcd3.Etcd3Client.authenticate', Mock(side_effect=Exception))
|
||||
def test__init__(self):
|
||||
self.assertRaises(SystemExit, self.setUp)
|
||||
|
||||
@patch.object(urllib3.PoolManager, 'urlopen')
|
||||
def test_call_rpc(self, mock_urlopen):
|
||||
request = {'key': base64_encode('/patroni/test/leader')}
|
||||
mock_urlopen.return_value = MockResponse()
|
||||
mock_urlopen.return_value.content = '{"succeeded":true,"header":{"revision":"1"}}'
|
||||
self.client.call_rpc('/kv/txn', {'success': [{'request_delete_range': request}]})
|
||||
self.client.call_rpc('/kv/put', request)
|
||||
self.client.call_rpc('/kv/deleterange', request)
|
||||
|
||||
@patch('time.time', Mock(side_effect=[1, 10.9, 100]))
|
||||
def test__wait_cache(self):
|
||||
with self.kv_cache.condition:
|
||||
self.assertRaises(RetryFailedError, self.client._wait_cache, 10)
|
||||
|
||||
@patch.object(urllib3.PoolManager, 'urlopen')
|
||||
def test__restart_watcher(self, mock_urlopen):
|
||||
mock_urlopen.return_value = MockResponse()
|
||||
mock_urlopen.return_value.status_code = 400
|
||||
mock_urlopen.return_value.content = '{"code":9,"error":"etcdserver: authentication is not enabled"}'
|
||||
self.client.authenticate()
|
||||
|
||||
@patch.object(urllib3.PoolManager, 'urlopen')
|
||||
def test__handle_auth_errors(self, mock_urlopen):
|
||||
mock_urlopen.return_value = MockResponse()
|
||||
mock_urlopen.return_value.content = '{"code":3,"error":"etcdserver: user name is empty"}'
|
||||
mock_urlopen.return_value.status_code = 403
|
||||
self.client._cluster_version = (3, 1, 5)
|
||||
self.assertRaises(UnsupportedEtcdVersion, self.client.deleteprefix, 'foo')
|
||||
self.client._cluster_version = (3, 3, 13)
|
||||
self.assertRaises(UserEmpty, self.client.deleteprefix, 'foo')
|
||||
mock_urlopen.return_value.content = '{"code":16,"error":"etcdserver: invalid auth token"}'
|
||||
self.assertRaises(InvalidAuthToken, self.client.deleteprefix, 'foo')
|
||||
with patch.object(PatroniEtcd3Client, 'authenticate', Mock(return_value=True)):
|
||||
self.assertRaises(InvalidAuthToken, self.client.deleteprefix, 'foo')
|
||||
self.client.username = None
|
||||
self.assertRaises(InvalidAuthToken, self.client.deleteprefix, 'foo')
|
||||
|
||||
def test__handle_server_response(self):
|
||||
response = MockResponse()
|
||||
response.content = '{"code":0,"error":"'
|
||||
self.assertRaises(etcd.EtcdException, self.client._handle_server_response, response)
|
||||
response.status_code = 400
|
||||
self.assertRaises(Unknown, self.client._handle_server_response, response)
|
||||
response.content = '{"error":{"grpc_code":0,"message":"","http_code":400}}'
|
||||
try:
|
||||
self.client._handle_server_response(response)
|
||||
except Unknown as e:
|
||||
self.assertEqual(e.as_dict(), {'code': 2, 'codeText': 'OK', 'error': u'', 'status': 400})
|
||||
|
||||
@patch.object(urllib3.PoolManager, 'urlopen')
|
||||
def test__ensure_version_prefix(self, mock_urlopen):
|
||||
self.client.version_prefix = None
|
||||
mock_urlopen.return_value = MockResponse()
|
||||
mock_urlopen.return_value.content = '{"etcdserver": "3.0.3", "etcdcluster": "3.0.0"}'
|
||||
self.assertRaises(UnsupportedEtcdVersion, self.client._ensure_version_prefix, '')
|
||||
mock_urlopen.return_value.content = '{"etcdserver": "3.0.4", "etcdcluster": "3.0.0"}'
|
||||
self.client._ensure_version_prefix('')
|
||||
self.assertEqual(self.client.version_prefix, '/v3alpha')
|
||||
mock_urlopen.return_value.content = '{"etcdserver": "3.4.4", "etcdcluster": "3.4.0"}'
|
||||
self.client._ensure_version_prefix('')
|
||||
self.assertEqual(self.client.version_prefix, '/v3')
|
||||
|
||||
|
||||
@patch.object(urllib3.PoolManager, 'urlopen', mock_urlopen)
|
||||
class TestEtcd3(BaseTestEtcd3):
|
||||
|
||||
@patch.object(Thread, 'start', Mock())
|
||||
@patch.object(urllib3.PoolManager, 'urlopen', mock_urlopen)
|
||||
def setUp(self):
|
||||
super(TestEtcd3, self).setUp()
|
||||
self.assertRaises(AttributeError, self.kv_cache._build_cache)
|
||||
self.kv_cache._is_ready = True
|
||||
self.etcd3.get_cluster()
|
||||
|
||||
def test_get_cluster(self):
|
||||
self.assertIsInstance(self.etcd3.get_cluster(), Cluster)
|
||||
self.client._kv_cache = None
|
||||
with patch.object(urllib3.PoolManager, 'urlopen') as mock_urlopen:
|
||||
mock_urlopen.side_effect = UnsupportedEtcdVersion('')
|
||||
self.assertRaises(UnsupportedEtcdVersion, self.etcd3.get_cluster)
|
||||
mock_urlopen.side_effect = SleepException()
|
||||
self.assertRaises(Etcd3Error, self.etcd3.get_cluster)
|
||||
|
||||
def test_touch_member(self):
|
||||
self.etcd3.touch_member({})
|
||||
self.etcd3._lease = 'bla'
|
||||
self.etcd3.touch_member({})
|
||||
with patch.object(PatroniEtcd3Client, 'lease_grant', Mock(side_effect=Etcd3ClientError)):
|
||||
self.etcd3.touch_member({})
|
||||
|
||||
def test__update_leader(self):
|
||||
self.etcd3._lease = None
|
||||
self.etcd3.update_leader('123')
|
||||
self.etcd3.update_leader('124')
|
||||
|
||||
def test_attempt_to_acquire_leader(self):
|
||||
self.etcd3._lease = None
|
||||
self.assertFalse(self.etcd3.attempt_to_acquire_leader())
|
||||
|
||||
def test_set_ttl(self):
|
||||
self.etcd3.set_ttl(20)
|
||||
|
||||
@patch.object(PatroniEtcd3Client, 'lease_keepalive', Mock(return_value=False))
|
||||
def test_refresh_lease(self):
|
||||
self.etcd3._last_lease_refresh = 0
|
||||
self.etcd3.refresh_lease()
|
||||
|
||||
@patch('time.sleep', Mock(side_effect=SleepException))
|
||||
@patch.object(PatroniEtcd3Client, 'lease_keepalive', Mock(return_value=False))
|
||||
@patch.object(PatroniEtcd3Client, 'lease_grant', Mock(side_effect=Etcd3ClientError))
|
||||
def test_create_lease(self):
|
||||
self.etcd3._lease = None
|
||||
self.etcd3._last_lease_refresh = 0
|
||||
self.assertRaises(SleepException, self.etcd3.create_lease)
|
||||
|
||||
def test_set_failover_value(self):
|
||||
self.etcd3.set_failover_value('', 1)
|
||||
|
||||
def test_set_config_value(self):
|
||||
self.etcd3.set_config_value('')
|
||||
|
||||
def test_initialize(self):
|
||||
self.etcd3.initialize()
|
||||
|
||||
def test_cancel_initialization(self):
|
||||
self.etcd3.cancel_initialization()
|
||||
|
||||
def test_delete_leader(self):
|
||||
self.etcd3.delete_leader()
|
||||
|
||||
def test_delete_cluster(self):
|
||||
self.etcd3.delete_cluster()
|
||||
|
||||
def test_set_history_value(self):
|
||||
self.etcd3.set_history_value('')
|
||||
|
||||
def test_set_sync_state_value(self):
|
||||
self.etcd3.set_sync_state_value('')
|
||||
|
||||
def test_delete_sync_state(self):
|
||||
self.etcd3.delete_sync_state()
|
||||
|
||||
def test_watch(self):
|
||||
self.etcd3.set_ttl(10)
|
||||
self.etcd3.watch(None, 0)
|
||||
self.etcd3.watch(None, 0)
|
||||
|
||||
def test_set_socket_options(self):
|
||||
with patch('socket.SIO_KEEPALIVE_VALS', 1, create=True):
|
||||
self.etcd3.set_socket_options(Mock(), None)
|
||||
+105
-23
@@ -3,11 +3,11 @@ import etcd
|
||||
import os
|
||||
import sys
|
||||
|
||||
from mock import Mock, MagicMock, PropertyMock, patch
|
||||
from mock import Mock, MagicMock, PropertyMock, patch, mock_open
|
||||
from patroni.config import Config
|
||||
from patroni.dcs import Cluster, ClusterConfig, Failover, Leader, Member, get_dcs, SyncState, TimelineHistory
|
||||
from patroni.dcs.etcd import Client
|
||||
from patroni.exceptions import DCSError, PostgresConnectionException, PatroniException
|
||||
from patroni.dcs.etcd import AbstractEtcdClientWithFailover
|
||||
from patroni.exceptions import DCSError, PostgresConnectionException, PatroniFatalException
|
||||
from patroni.ha import Ha, _MemberStatus
|
||||
from patroni.postgresql import Postgresql
|
||||
from patroni.postgresql.bootstrap import Bootstrap
|
||||
@@ -17,6 +17,7 @@ from patroni.postgresql.rewind import Rewind
|
||||
from patroni.postgresql.slots import SlotsHandler
|
||||
from patroni.utils import tzutc
|
||||
from patroni.watchdog import Watchdog
|
||||
from six.moves import builtins
|
||||
|
||||
from . import PostgresInit, MockPostmaster, psycopg2_connect, requests_get
|
||||
from .test_etcd import socket_getaddrinfo, etcd_read, etcd_write
|
||||
@@ -47,7 +48,7 @@ def get_cluster_not_initialized_without_leader(cluster_config=None):
|
||||
def get_cluster_initialized_without_leader(leader=False, failover=None, sync=None, cluster_config=None):
|
||||
m1 = Member(0, 'leader', 28, {'conn_url': 'postgres://replicator:[email protected]:5435/postgres',
|
||||
'api_url': 'http://127.0.0.1:8008/patroni', 'xlog_location': 4})
|
||||
leader = Leader(0, 0, m1) if leader else None
|
||||
leader = Leader(0, 0, m1 if leader else Member(0, '', 28, {}))
|
||||
m2 = Member(0, 'other', 28, {'conn_url': 'postgres://replicator:[email protected]:5436/postgres',
|
||||
'api_url': 'http://127.0.0.1:8011/patroni',
|
||||
'state': 'running',
|
||||
@@ -152,9 +153,11 @@ def run_async(self, func, args=()):
|
||||
@patch.object(Postgresql, 'is_leader', Mock(return_value=True))
|
||||
@patch.object(Postgresql, 'timeline_wal_position', Mock(return_value=(1, 10, 1)))
|
||||
@patch.object(Postgresql, '_cluster_info_state_get', Mock(return_value=3))
|
||||
@patch.object(Postgresql, 'call_nowait', Mock(return_value=True))
|
||||
@patch.object(Postgresql, 'data_directory_empty', Mock(return_value=False))
|
||||
@patch.object(Postgresql, 'controldata', Mock(return_value={'Database system identifier': SYSID}))
|
||||
@patch.object(Postgresql, 'controldata', Mock(return_value={
|
||||
'Database system identifier': SYSID,
|
||||
'Database cluster state': 'shut down',
|
||||
'Latest checkpoint location': '0/12345678'}))
|
||||
@patch.object(SlotsHandler, 'sync_replication_slots', Mock())
|
||||
@patch.object(ConfigHandler, 'append_pg_hba', Mock())
|
||||
@patch.object(ConfigHandler, 'write_pgpass', Mock(return_value={}))
|
||||
@@ -163,7 +166,7 @@ def run_async(self, func, args=()):
|
||||
@patch.object(Postgresql, 'query', Mock())
|
||||
@patch.object(Postgresql, 'checkpoint', Mock())
|
||||
@patch.object(CancellableSubprocess, 'call', Mock(return_value=0))
|
||||
@patch.object(Postgresql, 'get_local_timeline_lsn_from_replication_connection', Mock(return_value=[2, 10]))
|
||||
@patch.object(Postgresql, 'get_replica_timeline', Mock(return_value=2))
|
||||
@patch.object(Postgresql, 'get_master_timeline', Mock(return_value=2))
|
||||
@patch.object(ConfigHandler, 'restore_configuration_files', Mock())
|
||||
@patch.object(etcd.Client, 'write', etcd_write)
|
||||
@@ -172,6 +175,7 @@ def run_async(self, func, args=()):
|
||||
@patch('patroni.postgresql.polling_loop', Mock(return_value=range(1)))
|
||||
@patch('patroni.async_executor.AsyncExecutor.busy', PropertyMock(return_value=False))
|
||||
@patch('patroni.async_executor.AsyncExecutor.run_async', run_async)
|
||||
@patch('patroni.postgresql.rewind.Thread', Mock())
|
||||
@patch('subprocess.call', Mock(return_value=0))
|
||||
@patch('time.sleep', Mock())
|
||||
class TestHa(PostgresInit):
|
||||
@@ -181,7 +185,7 @@ class TestHa(PostgresInit):
|
||||
@patch.object(etcd.Client, 'read', etcd_read)
|
||||
def setUp(self):
|
||||
super(TestHa, self).setUp()
|
||||
with patch.object(Client, 'machines') as mock_machines:
|
||||
with patch.object(AbstractEtcdClientWithFailover, 'machines') as mock_machines:
|
||||
mock_machines.__get__ = Mock(return_value=['http://remotehost:2379'])
|
||||
self.p.set_state('running')
|
||||
self.p.set_role('replica')
|
||||
@@ -196,8 +200,10 @@ class TestHa(PostgresInit):
|
||||
|
||||
def test_update_lock(self):
|
||||
self.p.last_operation = Mock(side_effect=PostgresConnectionException(''))
|
||||
self.assertTrue(self.ha.update_lock(True))
|
||||
self.ha.dcs.update_leader = Mock(side_effect=Exception)
|
||||
self.assertFalse(self.ha.update_lock(True))
|
||||
|
||||
@patch.object(Postgresql, 'received_timeline', Mock(return_value=None))
|
||||
def test_touch_member(self):
|
||||
self.p.timeline_wal_position = Mock(return_value=(0, 1, 0))
|
||||
self.p.replica_cached_timeline = Mock(side_effect=Exception)
|
||||
@@ -253,11 +259,19 @@ class TestHa(PostgresInit):
|
||||
self.ha.cluster = get_cluster_initialized_with_leader()
|
||||
self.assertEqual(self.ha.run_cycle(), 'starting as readonly because i had the session lock')
|
||||
|
||||
@patch.object(Postgresql, 'fix_cluster_state', Mock())
|
||||
@patch.object(Rewind, 'ensure_clean_shutdown', Mock())
|
||||
def test_crash_recovery(self):
|
||||
self.ha.has_lock = true
|
||||
self.p.is_running = false
|
||||
self.p.controldata = lambda: {'Database cluster state': 'in production', 'Database system identifier': SYSID}
|
||||
self.assertEqual(self.ha.run_cycle(), 'doing crash recovery in a single user mode')
|
||||
with patch('patroni.async_executor.AsyncExecutor.busy', PropertyMock(return_value=True)),\
|
||||
patch.object(Ha, 'check_timeline', Mock(return_value=False)):
|
||||
self.ha._async_executor.schedule('doing crash recovery in a single user mode')
|
||||
self.ha.state_handler.cancellable._process = Mock()
|
||||
self.ha._crash_recovery_started -= 600
|
||||
self.ha.patroni.config.set_dynamic_configuration({'maximum_lag_on_failover': 10})
|
||||
self.assertEqual(self.ha.run_cycle(), 'terminated crash recovery because of startup timeout')
|
||||
|
||||
@patch.object(Rewind, 'rewind_or_reinitialize_needed_and_possible', Mock(return_value=True))
|
||||
@patch.object(Rewind, 'can_rewind', PropertyMock(return_value=True))
|
||||
@@ -297,6 +311,19 @@ class TestHa(PostgresInit):
|
||||
self.p.is_leader = false
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to leader by acquiring session lock')
|
||||
|
||||
def test_promotion_cancelled_after_pre_promote_failed(self):
|
||||
self.p.is_leader = false
|
||||
self.p._pre_promote = false
|
||||
self.ha._is_healthiest_node = true
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to leader by acquiring session lock')
|
||||
self.assertEqual(self.ha.run_cycle(), 'Promotion cancelled because the pre-promote script failed')
|
||||
self.assertEqual(self.ha.run_cycle(), 'following a different leader because i am not the healthiest node')
|
||||
|
||||
def test_lost_leader_lock_during_promote(self):
|
||||
with patch('patroni.async_executor.AsyncExecutor.busy', PropertyMock(return_value=True)):
|
||||
self.ha._async_executor.schedule('promote')
|
||||
self.assertEqual(self.ha.run_cycle(), 'lost leader before promote')
|
||||
|
||||
def test_long_promote(self):
|
||||
self.ha.cluster.is_unlocked = false
|
||||
self.ha.has_lock = true
|
||||
@@ -417,7 +444,7 @@ class TestHa(PostgresInit):
|
||||
self.e.initialize = true
|
||||
self.ha.bootstrap()
|
||||
self.p.is_running = false
|
||||
self.assertRaises(PatroniException, self.ha.post_bootstrap)
|
||||
self.assertRaises(PatroniFatalException, self.ha.post_bootstrap)
|
||||
|
||||
def test_bootstrap_release_initialize_key_on_watchdog_failure(self):
|
||||
self.ha.cluster = get_cluster_not_initialized_without_leader()
|
||||
@@ -427,7 +454,7 @@ class TestHa(PostgresInit):
|
||||
self.p.is_leader = true
|
||||
with patch.object(Watchdog, 'activate', Mock(return_value=False)):
|
||||
self.assertEqual(self.ha.post_bootstrap(), 'running post_bootstrap')
|
||||
self.assertRaises(PatroniException, self.ha.post_bootstrap)
|
||||
self.assertRaises(PatroniFatalException, self.ha.post_bootstrap)
|
||||
|
||||
@patch('psycopg2.connect', psycopg2_connect)
|
||||
def test_reinitialize(self):
|
||||
@@ -618,12 +645,17 @@ class TestHa(PostgresInit):
|
||||
member = Member(0, 'test', 1, {'api_url': 'http://localhost:8011/patroni'})
|
||||
self.ha.fetch_node_status(member)
|
||||
|
||||
@patch.object(Rewind, 'pg_rewind', true)
|
||||
@patch.object(Rewind, 'check_leader_is_not_in_recovery', true)
|
||||
def test_post_recover(self):
|
||||
self.p.is_running = false
|
||||
self.ha.has_lock = true
|
||||
self.p.set_role('master')
|
||||
self.assertEqual(self.ha.post_recover(), 'removed leader key after trying and failing to start postgres')
|
||||
self.ha.has_lock = false
|
||||
self.assertEqual(self.ha.post_recover(), 'failed to start postgres')
|
||||
leader = Leader(0, 0, Member(0, 'l', 2, {"version": "1.6", "conn_url": "postgres://a", "role": "master"}))
|
||||
self.ha._rewind.execute(leader)
|
||||
self.p.is_running = true
|
||||
self.assertIsNone(self.ha.post_recover())
|
||||
|
||||
@@ -684,13 +716,19 @@ class TestHa(PostgresInit):
|
||||
self.ha.cluster = get_cluster_initialized_with_leader(Failover(0, '', self.p.name, None))
|
||||
self.assertEqual(self.ha.run_cycle(), 'PAUSE: waiting to become master after promote...')
|
||||
|
||||
@patch('patroni.postgresql.mtime', Mock(return_value=1588316884))
|
||||
@patch.object(builtins, 'open', mock_open(read_data='1\t0/40159C0\tno recovery target specified\n'))
|
||||
def test_process_healthy_standby_cluster_as_standby_leader(self):
|
||||
self.p.is_leader = false
|
||||
self.p.name = 'leader'
|
||||
self.ha.cluster = get_standby_cluster_initialized_with_only_leader()
|
||||
self.p.config.check_recovery_conf = Mock(return_value=(False, False))
|
||||
self.ha._leader_timeline = 1
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to a standby leader because i had the session lock')
|
||||
self.assertEqual(self.ha.run_cycle(), 'no action. i am the standby leader with the lock')
|
||||
self.p.set_role('replica')
|
||||
self.p.config.check_recovery_conf = Mock(return_value=(True, False))
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to a standby leader because i had the session lock')
|
||||
|
||||
def test_process_healthy_standby_cluster_as_cascade_replica(self):
|
||||
self.p.is_leader = false
|
||||
@@ -812,6 +850,17 @@ class TestHa(PostgresInit):
|
||||
self.assertEqual(self.ha.run_cycle(), 'stopped PostgreSQL to fail over after a crash')
|
||||
demote.assert_called_once()
|
||||
|
||||
def test_master_stop_timeout(self):
|
||||
self.assertEqual(self.ha.master_stop_timeout(), None)
|
||||
self.ha.patroni.config.set_dynamic_configuration({'master_stop_timeout': 30})
|
||||
with patch.object(Ha, 'is_synchronous_mode', Mock(return_value=True)):
|
||||
self.assertEqual(self.ha.master_stop_timeout(), 30)
|
||||
self.ha.patroni.config.set_dynamic_configuration({'master_stop_timeout': 30})
|
||||
with patch.object(Ha, 'is_synchronous_mode', Mock(return_value=False)):
|
||||
self.assertEqual(self.ha.master_stop_timeout(), None)
|
||||
self.ha.patroni.config.set_dynamic_configuration({'master_stop_timeout': None})
|
||||
self.assertEqual(self.ha.master_stop_timeout(), None)
|
||||
|
||||
@patch('patroni.postgresql.Postgresql.follow')
|
||||
def test_demote_immediate(self, follow):
|
||||
self.ha.has_lock = true
|
||||
@@ -829,7 +878,7 @@ class TestHa(PostgresInit):
|
||||
with patch.object(self.ha.dcs, 'delete_sync_state') as mock_delete_sync:
|
||||
self.ha.run_cycle()
|
||||
mock_delete_sync.assert_called_once()
|
||||
mock_set_sync.assert_called_once_with(None)
|
||||
mock_set_sync.assert_called_once_with([])
|
||||
|
||||
mock_set_sync.reset_mock()
|
||||
# Test sync key not touched when not there
|
||||
@@ -837,14 +886,14 @@ class TestHa(PostgresInit):
|
||||
with patch.object(self.ha.dcs, 'delete_sync_state') as mock_delete_sync:
|
||||
self.ha.run_cycle()
|
||||
mock_delete_sync.assert_not_called()
|
||||
mock_set_sync.assert_called_once_with(None)
|
||||
mock_set_sync.assert_called_once_with([])
|
||||
|
||||
mock_set_sync.reset_mock()
|
||||
|
||||
self.ha.is_synchronous_mode = true
|
||||
|
||||
# Test sync standby not touched when picking the same node
|
||||
self.p.pick_synchronous_standby = Mock(return_value=('other', True))
|
||||
self.p.pick_synchronous_standby = Mock(return_value=(['other'], ['other']))
|
||||
self.ha.cluster = get_cluster_initialized_with_leader(sync=('leader', 'other'))
|
||||
self.ha.run_cycle()
|
||||
mock_set_sync.assert_not_called()
|
||||
@@ -852,10 +901,17 @@ class TestHa(PostgresInit):
|
||||
mock_set_sync.reset_mock()
|
||||
|
||||
# Test sync standby is replaced when switching standbys
|
||||
self.p.pick_synchronous_standby = Mock(return_value=('other2', False))
|
||||
self.p.pick_synchronous_standby = Mock(return_value=(['other2'], []))
|
||||
self.ha.dcs.write_sync_state = Mock(return_value=True)
|
||||
self.ha.run_cycle()
|
||||
mock_set_sync.assert_called_once_with('other2')
|
||||
mock_set_sync.assert_called_once_with(['other2'])
|
||||
|
||||
# Test sync standby is replaced when new standby is joined
|
||||
self.p.pick_synchronous_standby = Mock(return_value=(['other2', 'other3'], ['other2']))
|
||||
self.ha.dcs.write_sync_state = Mock(return_value=True)
|
||||
self.ha.run_cycle()
|
||||
self.assertEqual(mock_set_sync.call_args_list[0][0], (['other2'],))
|
||||
self.assertEqual(mock_set_sync.call_args_list[1][0], (['other2', 'other3'],))
|
||||
|
||||
mock_set_sync.reset_mock()
|
||||
# Test sync standby is not disabled when updating dcs fails
|
||||
@@ -868,7 +924,7 @@ class TestHa(PostgresInit):
|
||||
self.ha.dcs.write_sync_state = Mock(return_value=True)
|
||||
self.ha.dcs.get_cluster = Mock(return_value=get_cluster_initialized_with_leader(sync=('leader', 'other')))
|
||||
# self.ha.cluster = get_cluster_initialized_with_leader(sync=('leader', 'other'))
|
||||
self.p.pick_synchronous_standby = Mock(return_value=('other2', True))
|
||||
self.p.pick_synchronous_standby = Mock(return_value=(['other2'], ['other2']))
|
||||
self.ha.run_cycle()
|
||||
self.ha.dcs.get_cluster.assert_called_once()
|
||||
self.assertEqual(self.ha.dcs.write_sync_state.call_count, 2)
|
||||
@@ -891,9 +947,9 @@ class TestHa(PostgresInit):
|
||||
# Test sync set to '*' when synchronous_mode_strict is enabled
|
||||
mock_set_sync.reset_mock()
|
||||
self.ha.is_synchronous_mode_strict = true
|
||||
self.p.pick_synchronous_standby = Mock(return_value=(None, False))
|
||||
self.p.pick_synchronous_standby = Mock(return_value=([], []))
|
||||
self.ha.run_cycle()
|
||||
mock_set_sync.assert_called_once_with('*')
|
||||
mock_set_sync.assert_called_once_with(['*'])
|
||||
|
||||
def test_sync_replication_become_master(self):
|
||||
self.ha.is_synchronous_mode = true
|
||||
@@ -908,7 +964,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
# When we just became master nobody is sync
|
||||
self.assertEqual(self.ha.enforce_master_role('msg', 'promote msg'), 'promote msg')
|
||||
mock_set_sync.assert_called_once_with(None)
|
||||
mock_set_sync.assert_called_once_with([])
|
||||
mock_write_sync.assert_called_once_with('leader', None, index=0)
|
||||
|
||||
mock_set_sync.reset_mock()
|
||||
@@ -990,6 +1046,8 @@ class TestHa(PostgresInit):
|
||||
self.ha._disable_sync = False
|
||||
self.assertEqual(self.ha.get_effective_tags(), {'foo': 'bar'})
|
||||
|
||||
@patch('patroni.postgresql.mtime', Mock(return_value=1588316884))
|
||||
@patch.object(builtins, 'open', Mock(side_effect=Exception))
|
||||
def test_restore_cluster_config(self):
|
||||
self.ha.cluster.config.data.clear()
|
||||
self.ha.has_lock = true
|
||||
@@ -1005,7 +1063,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
def test_shutdown(self):
|
||||
self.p.is_running = false
|
||||
self.ha.has_lock = true
|
||||
self.ha.is_leader = true
|
||||
self.ha.shutdown()
|
||||
|
||||
@patch('time.sleep', Mock())
|
||||
@@ -1021,7 +1079,9 @@ class TestHa(PostgresInit):
|
||||
# will not say bootstrap from leader as replica can't self elect
|
||||
self.assertEqual(self.ha.run_cycle(), "trying to bootstrap from replica 'other'")
|
||||
|
||||
@patch('psycopg2.connect', psycopg2_connect)
|
||||
@patch('patroni.postgresql.mtime', Mock(return_value=1588316884))
|
||||
@patch.object(builtins, 'open', mock_open(read_data=('1\t0/40159C0\tno recovery target specified\n\n'
|
||||
'2\t1/40159C0\tno recovery target specified\n')))
|
||||
def test_update_cluster_history(self):
|
||||
self.ha.has_lock = true
|
||||
self.ha.cluster.is_unlocked = false
|
||||
@@ -1052,3 +1112,25 @@ class TestHa(PostgresInit):
|
||||
self.ha.cluster = get_cluster_initialized_without_leader(leader=True, cluster_config=config)
|
||||
self.ha.has_lock = true
|
||||
self.assertEqual(self.ha.run_cycle(), 'no action. i am the leader with the lock')
|
||||
|
||||
@patch.object(Cluster, 'has_member', true)
|
||||
def test_run_cycle(self):
|
||||
self.ha.dcs.touch_member = Mock(side_effect=DCSError('foo'))
|
||||
self.assertEqual(self.ha.run_cycle(), 'Unexpected exception raised, please report it as a BUG')
|
||||
self.ha.dcs.touch_member = Mock(side_effect=PatroniFatalException('foo'))
|
||||
self.assertRaises(PatroniFatalException, self.ha.run_cycle)
|
||||
|
||||
def test_empty_directory_in_pause(self):
|
||||
self.ha.is_paused = true
|
||||
self.p.data_directory_empty = true
|
||||
self.assertEqual(self.ha.run_cycle(), 'PAUSE: running with empty data directory')
|
||||
self.assertEqual(self.p.role, 'uninitialized')
|
||||
|
||||
@patch('patroni.ha.Ha.sysid_valid', MagicMock(return_value=True))
|
||||
def test_sysid_no_match_in_pause(self):
|
||||
self.ha.is_paused = true
|
||||
self.p.controldata = lambda: {'Database cluster state': 'in recovery', 'Database system identifier': '123'}
|
||||
self.assertEqual(self.ha.run_cycle(), 'PAUSE: continue to run as master without lock')
|
||||
|
||||
self.ha.has_lock = true
|
||||
self.assertEqual(self.ha.run_cycle(), 'PAUSE: released leader key voluntarily due to the system ID mismatch')
|
||||
|
||||
+216
-73
@@ -1,14 +1,18 @@
|
||||
import json
|
||||
import socket
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from mock import Mock, patch
|
||||
from patroni.dcs.kubernetes import Kubernetes, KubernetesError, k8s_client, RetryFailedError
|
||||
from mock import Mock, mock_open, patch
|
||||
from patroni.dcs.kubernetes import k8s_client, k8s_config, K8sConfig, K8sConnectionFailed,\
|
||||
K8sException, K8sObject, Kubernetes, KubernetesError, KubernetesRetriableException,\
|
||||
Retry, RetryFailedError, SERVICE_HOST_ENV_NAME, SERVICE_PORT_ENV_NAME
|
||||
from six.moves import builtins
|
||||
from threading import Thread
|
||||
from . import SleepException
|
||||
from . import MockResponse, SleepException
|
||||
|
||||
|
||||
def mock_list_namespaced_config_map(self, *args, **kwargs):
|
||||
def mock_list_namespaced_config_map(*args, **kwargs):
|
||||
metadata = {'resource_version': '1', 'labels': {'f': 'b'}, 'name': 'test-config',
|
||||
'annotations': {'initialize': '123', 'config': '{}'}}
|
||||
items = [k8s_client.V1ConfigMap(metadata=k8s_client.V1ObjectMeta(**metadata))]
|
||||
@@ -22,67 +26,176 @@ def mock_list_namespaced_config_map(self, *args, **kwargs):
|
||||
return k8s_client.V1ConfigMapList(metadata=metadata, items=items, kind='ConfigMapList')
|
||||
|
||||
|
||||
def mock_list_namespaced_pod(self, *args, **kwargs):
|
||||
metadata = k8s_client.V1ObjectMeta(resource_version='1', name='p-0', annotations={'status': '{}'})
|
||||
items = [k8s_client.V1Pod(metadata=metadata)]
|
||||
def mock_list_namespaced_endpoints(*args, **kwargs):
|
||||
target_ref = k8s_client.V1ObjectReference(kind='Pod', resource_version='10', name='p-0',
|
||||
namespace='default', uid='964dfeae-e79b-4476-8a5a-1920b5c2a69d')
|
||||
address0 = k8s_client.V1EndpointAddress(ip='10.0.0.0', target_ref=target_ref)
|
||||
address1 = k8s_client.V1EndpointAddress(ip='10.0.0.1')
|
||||
port = k8s_client.V1EndpointPort(port=5432, name='postgresql', protocol='TCP')
|
||||
subset = k8s_client.V1EndpointSubset(addresses=[address1, address0], ports=[port])
|
||||
metadata = k8s_client.V1ObjectMeta(resource_version='1', labels={'f': 'b'}, name='test',
|
||||
annotations={'optime': '1234', 'leader': 'p-0', 'ttl': '30s'})
|
||||
endpoint = k8s_client.V1Endpoints(subsets=[subset], metadata=metadata)
|
||||
metadata = k8s_client.V1ObjectMeta(resource_version='1')
|
||||
return k8s_client.V1EndpointsList(metadata=metadata, items=[endpoint], kind='V1EndpointsList')
|
||||
|
||||
|
||||
def mock_list_namespaced_pod(*args, **kwargs):
|
||||
metadata = k8s_client.V1ObjectMeta(resource_version='1', name='p-0', annotations={'status': '{}'},
|
||||
uid='964dfeae-e79b-4476-8a5a-1920b5c2a69d')
|
||||
status = k8s_client.V1PodStatus(pod_ip='10.0.0.0')
|
||||
spec = k8s_client.V1PodSpec(hostname='p-0', node_name='kind-control-plane', containers=[])
|
||||
items = [k8s_client.V1Pod(metadata=metadata, status=status, spec=spec)]
|
||||
return k8s_client.V1PodList(items=items, kind='PodList')
|
||||
|
||||
|
||||
def mock_config_map(*args, **kwargs):
|
||||
def mock_namespaced_kind(*args, **kwargs):
|
||||
mock = Mock()
|
||||
mock.metadata.resource_version = '2'
|
||||
return mock
|
||||
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_config_map', mock_config_map)
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_config_map', mock_config_map)
|
||||
@patch('kubernetes.client.api_client.ThreadPool', Mock(), create=True)
|
||||
@patch.object(Thread, 'start', Mock())
|
||||
class TestKubernetes(unittest.TestCase):
|
||||
def mock_load_k8s_config(self, *args, **kwargs):
|
||||
self._server = ''
|
||||
|
||||
@patch('kubernetes.config.load_kube_config', Mock())
|
||||
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_config_map', mock_list_namespaced_config_map)
|
||||
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_pod', mock_list_namespaced_pod)
|
||||
@patch('kubernetes.client.api_client.ThreadPool', Mock(), create=True)
|
||||
@patch.object(Thread, 'start', Mock())
|
||||
|
||||
class TestK8sConfig(unittest.TestCase):
|
||||
|
||||
def test_load_incluster_config(self):
|
||||
for env in ({}, {SERVICE_HOST_ENV_NAME: '', SERVICE_PORT_ENV_NAME: ''}):
|
||||
with patch('os.environ', env):
|
||||
self.assertRaises(k8s_config.ConfigException, k8s_config.load_incluster_config)
|
||||
|
||||
with patch('os.environ', {SERVICE_HOST_ENV_NAME: 'a', SERVICE_PORT_ENV_NAME: '1'}),\
|
||||
patch('os.path.isfile', Mock(side_effect=[False, True, True, False, True, True, True, True])),\
|
||||
patch.object(builtins, 'open', Mock(side_effect=[
|
||||
mock_open()(), mock_open(read_data='a')(), mock_open(read_data='a')(),
|
||||
mock_open()(), mock_open(read_data='a')(), mock_open(read_data='a')()])):
|
||||
for _ in range(0, 4):
|
||||
self.assertRaises(k8s_config.ConfigException, k8s_config.load_incluster_config)
|
||||
k8s_config.load_incluster_config()
|
||||
self.assertEqual(k8s_config.server, 'https://a:1')
|
||||
|
||||
def test_load_kube_config(self):
|
||||
config = {
|
||||
"current-context": "local",
|
||||
"contexts": [{"name": "local", "context": {"user": "local", "cluster": "local"}}],
|
||||
"clusters": [{"name": "local", "cluster": {"server": "https://a:1/", "certificate-authority": "a"}}],
|
||||
"users": [{"name": "local", "user": {"username": "a", "password": "b", "client-certificate": "c"}}]
|
||||
}
|
||||
with patch.object(builtins, 'open', mock_open(read_data=json.dumps(config))):
|
||||
k8s_config.load_kube_config()
|
||||
self.assertEqual(k8s_config.server, 'https://a:1')
|
||||
self.assertEqual(k8s_config.pool_config, {'ca_certs': 'a', 'cert_file': 'c', 'cert_reqs': 'CERT_REQUIRED',
|
||||
'maxsize': 10, 'num_pools': 10})
|
||||
|
||||
config["users"][0]["user"]["token"] = "token"
|
||||
with patch.object(builtins, 'open', mock_open(read_data=json.dumps(config))):
|
||||
k8s_config.load_kube_config()
|
||||
self.assertEqual(k8s_config.headers.get('authorization'), 'Bearer token')
|
||||
|
||||
|
||||
@patch('urllib3.PoolManager.request')
|
||||
class TestApiClient(unittest.TestCase):
|
||||
|
||||
@patch.object(K8sConfig, '_server', '', create=True)
|
||||
@patch('urllib3.PoolManager.request', Mock())
|
||||
def setUp(self):
|
||||
self.k = Kubernetes({'ttl': 30, 'scope': 'test', 'name': 'p-0',
|
||||
'loop_wait': 10, 'retry_timeout': 10, 'labels': {'f': 'b'}})
|
||||
self.a = k8s_client.ApiClient(True)
|
||||
self.mock_get_ep = MockResponse()
|
||||
self.mock_get_ep.content = '{"subsets":[{"ports":[{"name":"https","protocol":"TCP","port":443}],' +\
|
||||
'"addresses":[{"ip":"127.0.0.1"},{"ip":"127.0.0.2"}]}]}'
|
||||
|
||||
def test__do_http_request(self, mock_request):
|
||||
mock_request.side_effect = [self.mock_get_ep] + [socket.timeout]
|
||||
self.assertRaises(K8sException, self.a.call_api, 'GET', 'f')
|
||||
|
||||
@patch('time.sleep', Mock())
|
||||
def test_request(self, mock_request):
|
||||
retry = Retry(deadline=10, max_delay=1, max_tries=1, retry_exceptions=KubernetesRetriableException)
|
||||
mock_request.side_effect = [self.mock_get_ep] + 3 * [socket.timeout] + [k8s_client.rest.ApiException(500, '')]
|
||||
self.assertRaises(k8s_client.rest.ApiException, retry, self.a.call_api, 'GET', 'f', _retry=retry)
|
||||
mock_request.side_effect = [self.mock_get_ep, socket.timeout, Mock(), self.mock_get_ep]
|
||||
self.assertRaises(k8s_client.rest.ApiException, retry, self.a.call_api, 'GET', 'f', _retry=retry)
|
||||
retry.deadline = 0.0001
|
||||
mock_request.side_effect = [socket.timeout, socket.timeout, self.mock_get_ep]
|
||||
self.assertRaises(K8sConnectionFailed, retry, self.a.call_api, 'GET', 'f', _retry=retry)
|
||||
|
||||
def test__refresh_api_servers_cache(self, mock_request):
|
||||
mock_request.side_effect = k8s_client.rest.ApiException(403, '')
|
||||
self.a.refresh_api_servers_cache()
|
||||
|
||||
|
||||
class TestCoreV1Api(unittest.TestCase):
|
||||
|
||||
@patch('urllib3.PoolManager.request', Mock())
|
||||
@patch.object(K8sConfig, '_server', '', create=True)
|
||||
def setUp(self):
|
||||
self.a = k8s_client.CoreV1Api()
|
||||
self.a._api_client.pool_manager.request = Mock(return_value=MockResponse())
|
||||
|
||||
def test_create_namespaced_service(self):
|
||||
self.assertEqual(str(self.a.create_namespaced_service('default', {}, _request_timeout=2)), '{}')
|
||||
|
||||
def test_list_namespaced_endpoints(self):
|
||||
self.a._api_client.pool_manager.request.return_value.content = '{"items": [1,2,3]}'
|
||||
self.assertIsInstance(self.a.list_namespaced_endpoints('default'), K8sObject)
|
||||
|
||||
def test_patch_namespaced_config_map(self):
|
||||
self.assertEqual(str(self.a.patch_namespaced_config_map('foo', 'default', {}, _request_timeout=(1, 2))), '{}')
|
||||
|
||||
def test_list_namespaced_pod(self):
|
||||
self.a._api_client.pool_manager.request.return_value.status_code = 409
|
||||
self.a._api_client.pool_manager.request.return_value.content = 'foo'
|
||||
try:
|
||||
self.a.list_namespaced_pod('default', label_selector='foo=bar')
|
||||
self.assertFail()
|
||||
except k8s_client.rest.ApiException as e:
|
||||
self.assertTrue('Reason: ' in str(e))
|
||||
|
||||
def test_delete_namespaced_pod(self):
|
||||
self.assertEqual(str(self.a.delete_namespaced_pod('foo', 'default', _request_timeout=(1, 2), body={})), '{}')
|
||||
|
||||
|
||||
class BaseTestKubernetes(unittest.TestCase):
|
||||
|
||||
@patch('urllib3.PoolManager.request', Mock())
|
||||
@patch('socket.TCP_KEEPIDLE', 4, create=True)
|
||||
@patch('socket.TCP_KEEPINTVL', 5, create=True)
|
||||
@patch('socket.TCP_KEEPCNT', 6, create=True)
|
||||
@patch.object(Thread, 'start', Mock())
|
||||
@patch.object(K8sConfig, 'load_kube_config', mock_load_k8s_config)
|
||||
@patch.object(K8sConfig, 'load_incluster_config', Mock(side_effect=k8s_config.ConfigException))
|
||||
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_pod', mock_list_namespaced_pod, create=True)
|
||||
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_config_map', mock_list_namespaced_config_map, create=True)
|
||||
def setUp(self, config=None):
|
||||
config = config or {}
|
||||
config.update(ttl=30, scope='test', name='p-0', loop_wait=10,
|
||||
retry_timeout=10, labels={'f': 'b'}, bypass_api_service=True)
|
||||
self.k = Kubernetes(config)
|
||||
self.assertRaises(AttributeError, self.k._pods._build_cache)
|
||||
self.k._pods._is_ready = True
|
||||
self.assertRaises(AttributeError, self.k._kinds._build_cache)
|
||||
self.assertRaises(TypeError, self.k._kinds._build_cache)
|
||||
self.k._kinds._is_ready = True
|
||||
self.k.get_cluster()
|
||||
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_config_map', mock_namespaced_kind, create=True)
|
||||
class TestKubernetesConfigMaps(BaseTestKubernetes):
|
||||
|
||||
@patch('time.time', Mock(side_effect=[1, 10.9, 100]))
|
||||
def test__wait_caches(self):
|
||||
self.k._pods._is_ready = False
|
||||
with self.k._condition:
|
||||
self.assertRaises(RetryFailedError, self.k._wait_caches)
|
||||
self.assertRaises(RetryFailedError, self.k._wait_caches, time.time() + 10)
|
||||
|
||||
@patch('time.time', Mock(return_value=time.time() + 100))
|
||||
def test_get_cluster(self):
|
||||
with patch.object(k8s_client.CoreV1Api, 'list_namespaced_config_map', mock_list_namespaced_config_map), \
|
||||
patch.object(k8s_client.CoreV1Api, 'list_namespaced_pod', mock_list_namespaced_pod), \
|
||||
patch('time.time', Mock(return_value=time.time() + 31)):
|
||||
self.k.get_cluster()
|
||||
self.k.get_cluster()
|
||||
|
||||
with patch.object(Kubernetes, '_wait_caches', Mock(side_effect=Exception)):
|
||||
self.assertRaises(KubernetesError, self.k.get_cluster)
|
||||
|
||||
@patch('kubernetes.config.load_kube_config', Mock())
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_endpoints', Mock())
|
||||
def test_update_leader(self):
|
||||
k = Kubernetes({'ttl': 30, 'scope': 'test', 'name': 'p-0', 'loop_wait': 10, 'retry_timeout': 10,
|
||||
'labels': {'f': 'b'}, 'use_endpoints': True, 'pod_ip': '10.0.0.0'})
|
||||
self.assertIsNotNone(k.update_leader('123'))
|
||||
|
||||
@patch('kubernetes.config.load_kube_config', Mock())
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_endpoints', Mock())
|
||||
def test_update_leader_with_restricted_access(self):
|
||||
k = Kubernetes({'ttl': 30, 'scope': 'test', 'name': 'p-0', 'loop_wait': 10, 'retry_timeout': 10,
|
||||
'labels': {'f': 'b'}, 'use_endpoints': True, 'pod_ip': '10.0.0.0'})
|
||||
self.assertIsNotNone(k.update_leader('123', True))
|
||||
|
||||
def test_take_leader(self):
|
||||
self.k.take_leader()
|
||||
self.k._leader_observed_record['leader'] = 'test'
|
||||
@@ -90,14 +203,16 @@ class TestKubernetes(unittest.TestCase):
|
||||
self.k.take_leader()
|
||||
|
||||
def test_manual_failover(self):
|
||||
with patch.object(k8s_client.CoreV1Api, 'patch_namespaced_config_map', Mock(side_effect=RetryFailedError(''))):
|
||||
with patch.object(k8s_client.CoreV1Api, 'patch_namespaced_config_map',
|
||||
Mock(side_effect=RetryFailedError('')), create=True):
|
||||
self.k.manual_failover('foo', 'bar')
|
||||
|
||||
def test_set_config_value(self):
|
||||
self.k.set_config_value('{}')
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_pod', Mock(return_value=True))
|
||||
def test_touch_member(self):
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_pod', create=True)
|
||||
def test_touch_member(self, mock_patch_namespaced_pod):
|
||||
mock_patch_namespaced_pod.return_value.metadata.resource_version = '10'
|
||||
self.k.touch_member({'role': 'replica'})
|
||||
self.k._name = 'p-1'
|
||||
self.k.touch_member({'state': 'running', 'role': 'replica'})
|
||||
@@ -107,24 +222,16 @@ class TestKubernetes(unittest.TestCase):
|
||||
self.k.initialize()
|
||||
|
||||
def test_delete_leader(self):
|
||||
self.k.delete_leader()
|
||||
self.k.delete_leader(1)
|
||||
|
||||
def test_cancel_initialization(self):
|
||||
self.k.cancel_initialization()
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'delete_collection_namespaced_config_map',
|
||||
Mock(side_effect=k8s_client.rest.ApiException(403, '')))
|
||||
Mock(side_effect=k8s_client.rest.ApiException(403, '')), create=True)
|
||||
def test_delete_cluster(self):
|
||||
self.k.delete_cluster()
|
||||
|
||||
@patch('kubernetes.config.load_kube_config', Mock())
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_endpoints',
|
||||
Mock(side_effect=[k8s_client.rest.ApiException(502, ''), k8s_client.rest.ApiException(500, '')]))
|
||||
def test_delete_sync_state(self):
|
||||
k = Kubernetes({'ttl': 30, 'scope': 'test', 'name': 'p-0', 'loop_wait': 10, 'retry_timeout': 10,
|
||||
'labels': {'f': 'b'}, 'use_endpoints': True, 'pod_ip': '10.0.0.0'})
|
||||
self.assertFalse(k.delete_sync_state())
|
||||
|
||||
def test_watch(self):
|
||||
self.k.set_ttl(10)
|
||||
self.k.watch(None, 0)
|
||||
@@ -133,30 +240,61 @@ class TestKubernetes(unittest.TestCase):
|
||||
def test_set_history_value(self):
|
||||
self.k.set_history_value('{}')
|
||||
|
||||
@patch('kubernetes.config.load_kube_config', Mock())
|
||||
@patch('patroni.dcs.kubernetes.ObjectCache', Mock())
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_pod', Mock(return_value=True))
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_endpoints', Mock())
|
||||
|
||||
class TestKubernetesEndpoints(BaseTestKubernetes):
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_endpoints', mock_list_namespaced_endpoints, create=True)
|
||||
def setUp(self, config=None):
|
||||
super(TestKubernetesEndpoints, self).setUp({'use_endpoints': True, 'pod_ip': '10.0.0.0'})
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_endpoints', create=True)
|
||||
def test_update_leader(self, mock_patch_namespaced_endpoints):
|
||||
self.assertIsNotNone(self.k.update_leader('123'))
|
||||
args = mock_patch_namespaced_endpoints.call_args[0]
|
||||
self.assertEqual(args[2].subsets[0].addresses[0].target_ref.resource_version, '10')
|
||||
self.k._kinds._object_cache['test'].subsets[:] = []
|
||||
self.assertIsNotNone(self.k.update_leader('123'))
|
||||
self.k._kinds._object_cache['test'].metadata.annotations['leader'] = 'p-1'
|
||||
self.assertFalse(self.k.update_leader('123'))
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_endpoints', mock_namespaced_kind, create=True)
|
||||
def test_update_leader_with_restricted_access(self):
|
||||
self.assertIsNotNone(self.k.update_leader('123', True))
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_endpoints', create=True)
|
||||
def test__update_leader_with_retry(self, mock_patch):
|
||||
mock_patch.side_effect = k8s_client.rest.ApiException(502, '')
|
||||
self.assertFalse(self.k.update_leader('123'))
|
||||
mock_patch.side_effect = RetryFailedError('')
|
||||
self.assertFalse(self.k.update_leader('123'))
|
||||
mock_patch.side_effect = k8s_client.rest.ApiException(409, '')
|
||||
with patch('time.time', Mock(side_effect=[0, 100, 200])):
|
||||
self.assertFalse(self.k.update_leader('123'))
|
||||
with patch('time.sleep', Mock()):
|
||||
self.assertFalse(self.k.update_leader('123'))
|
||||
mock_patch.side_effect = [k8s_client.rest.ApiException(409, ''), mock_namespaced_kind()]
|
||||
self.k._kinds._object_cache['test'].metadata.resource_version = '2'
|
||||
self.assertIsNotNone(self.k._update_leader_with_retry({}, '1', []))
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_endpoints',
|
||||
Mock(side_effect=[k8s_client.rest.ApiException(500, ''),
|
||||
k8s_client.rest.ApiException(502, '')]), create=True)
|
||||
def test_delete_sync_state(self):
|
||||
self.assertFalse(self.k.delete_sync_state())
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'patch_namespaced_pod', mock_namespaced_kind, create=True)
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_endpoints', mock_namespaced_kind, create=True)
|
||||
@patch.object(k8s_client.CoreV1Api, 'create_namespaced_service',
|
||||
Mock(side_effect=[True, False, k8s_client.rest.ApiException(500, '')]))
|
||||
Mock(side_effect=[True, False, k8s_client.rest.ApiException(500, '')]), create=True)
|
||||
def test__create_config_service(self):
|
||||
k = Kubernetes({'ttl': 30, 'scope': 'test', 'name': 'p-0', 'loop_wait': 10, 'retry_timeout': 10,
|
||||
'labels': {'f': 'b'}, 'use_endpoints': True, 'pod_ip': '10.0.0.0'})
|
||||
self.assertIsNotNone(k.patch_or_create_config({'foo': 'bar'}))
|
||||
self.assertIsNotNone(k.patch_or_create_config({'foo': 'bar'}))
|
||||
k.touch_member({'state': 'running', 'role': 'replica'})
|
||||
self.assertIsNotNone(self.k.patch_or_create_config({'foo': 'bar'}))
|
||||
self.assertIsNotNone(self.k.patch_or_create_config({'foo': 'bar'}))
|
||||
self.k.touch_member({'state': 'running', 'role': 'replica'})
|
||||
|
||||
|
||||
class TestCacheBuilder(unittest.TestCase):
|
||||
class TestCacheBuilder(BaseTestKubernetes):
|
||||
|
||||
@patch('kubernetes.config.load_kube_config', Mock())
|
||||
@patch('kubernetes.client.api_client.ThreadPool', Mock(), create=True)
|
||||
@patch.object(Thread, 'start', Mock())
|
||||
def setUp(self):
|
||||
self.k = Kubernetes({'ttl': 30, 'scope': 'test', 'name': 'p-0',
|
||||
'loop_wait': 10, 'retry_timeout': 10, 'labels': {'f': 'b'}})
|
||||
|
||||
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_config_map', mock_list_namespaced_config_map)
|
||||
@patch.object(k8s_client.CoreV1Api, 'list_namespaced_config_map', mock_list_namespaced_config_map, create=True)
|
||||
@patch('patroni.dcs.kubernetes.ObjectCache._watch')
|
||||
def test__build_cache(self, mock_response):
|
||||
mock_response.return_value.read_chunked.return_value = [json.dumps(
|
||||
@@ -167,10 +305,15 @@ class TestCacheBuilder(unittest.TestCase):
|
||||
'name': self.k.config_path, 'resourceVersion': '3'}}}
|
||||
) + '\n' + json.dumps(
|
||||
{'type': 'MDIFIED', 'object': {'metadata': {'name': self.k.config_path}}}
|
||||
) + '\n' + json.dumps({'object': {'code': 410}}) + '\n').encode('utf-8')]
|
||||
) + '\n').encode('utf-8'), b'{"object":{', b'"code":410}}\n']
|
||||
self.k._kinds._build_cache()
|
||||
|
||||
@patch('patroni.dcs.kubernetes.logger.error', Mock(side_effect=SleepException))
|
||||
@patch('patroni.dcs.kubernetes.ObjectCache._build_cache', Mock(side_effect=Exception))
|
||||
def test_run(self):
|
||||
self.assertRaises(SleepException, self.k._pods.run)
|
||||
|
||||
@patch('time.sleep', Mock())
|
||||
def test__list(self):
|
||||
self.k._pods._func = Mock(side_effect=Exception)
|
||||
self.assertRaises(Exception, self.k._pods._list)
|
||||
|
||||
+2
-1
@@ -43,7 +43,8 @@ class TestPatroniLogger(unittest.TestCase):
|
||||
_LOG.exception('test')
|
||||
logger.start()
|
||||
|
||||
with patch.object(logging.Handler, 'format', Mock(side_effect=Exception)):
|
||||
with patch.object(logging.Handler, 'format', Mock(side_effect=Exception)),\
|
||||
patch('_pytest.logging.LogCaptureHandler.emit', Mock()):
|
||||
logging.error('test')
|
||||
|
||||
self.assertEqual(logger.log_handler.maxBytes, config['log']['file_size'])
|
||||
|
||||
@@ -9,7 +9,7 @@ import patroni.config as config
|
||||
from mock import Mock, PropertyMock, patch
|
||||
from patroni.api import RestApiServer
|
||||
from patroni.async_executor import AsyncExecutor
|
||||
from patroni.dcs.etcd import Client
|
||||
from patroni.dcs.etcd import AbstractEtcdClientWithFailover
|
||||
from patroni.exceptions import DCSError
|
||||
from patroni.postgresql import Postgresql
|
||||
from patroni.postgresql.config import ConfigHandler
|
||||
@@ -44,12 +44,16 @@ class TestPatroni(unittest.TestCase):
|
||||
def test_no_config(self):
|
||||
self.assertRaises(SystemExit, patroni_main)
|
||||
|
||||
@patch('pkgutil.get_importer', Mock(return_value=MockFrozenImporter()))
|
||||
@patch('sys.argv', ['patroni.py', '--validate-config', 'postgres0.yml'])
|
||||
def test_validate_config(self):
|
||||
self.assertRaises(SystemExit, patroni_main)
|
||||
|
||||
@patch('pkgutil.iter_importers', Mock(return_value=[MockFrozenImporter()]))
|
||||
@patch('sys.frozen', Mock(return_value=True), create=True)
|
||||
@patch.object(BaseHTTPServer.HTTPServer, '__init__', Mock())
|
||||
@patch.object(etcd.Client, 'read', etcd_read)
|
||||
@patch.object(Thread, 'start', Mock())
|
||||
@patch.object(Client, 'machines', PropertyMock(return_value=['http://remotehost:2379']))
|
||||
@patch.object(AbstractEtcdClientWithFailover, 'machines', PropertyMock(return_value=['http://remotehost:2379']))
|
||||
def setUp(self):
|
||||
self._handlers = logging.getLogger().handlers[:]
|
||||
RestApiServer._BaseServer__is_shut_down = Mock()
|
||||
@@ -71,7 +75,7 @@ class TestPatroni(unittest.TestCase):
|
||||
@patch('sys.argv', ['patroni.py', 'postgres0.yml'])
|
||||
@patch('time.sleep', Mock(side_effect=SleepException))
|
||||
@patch.object(etcd.Client, 'delete', Mock())
|
||||
@patch.object(Client, 'machines', PropertyMock(return_value=['http://remotehost:2379']))
|
||||
@patch.object(AbstractEtcdClientWithFailover, 'machines', PropertyMock(return_value=['http://remotehost:2379']))
|
||||
@patch.object(Thread, 'join', Mock())
|
||||
def test_patroni_patroni_main(self):
|
||||
with patch('subprocess.call', Mock(return_value=1)):
|
||||
@@ -163,7 +167,7 @@ class TestPatroni(unittest.TestCase):
|
||||
def test_reload_config(self):
|
||||
self.p.reload_config()
|
||||
self.p.get_tags = Mock(side_effect=Exception)
|
||||
self.p.reload_config()
|
||||
self.p.reload_config(local=True)
|
||||
|
||||
def test_nosync(self):
|
||||
self.p.tags['nosync'] = True
|
||||
|
||||
+106
-78
@@ -1,5 +1,6 @@
|
||||
import mock # for the mock.call method, importing it without a namespace breaks python3
|
||||
import os
|
||||
import psutil
|
||||
import psycopg2
|
||||
import re
|
||||
import subprocess
|
||||
@@ -92,12 +93,11 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
@patch('subprocess.call', Mock(return_value=0))
|
||||
@patch('os.rename', Mock())
|
||||
@patch('patroni.postgresql.CallbackExecutor', Mock())
|
||||
@patch.object(Postgresql, 'get_major_version', Mock(return_value=120000))
|
||||
@patch.object(Postgresql, 'get_major_version', Mock(return_value=130000))
|
||||
@patch.object(Postgresql, 'is_running', Mock(return_value=True))
|
||||
def setUp(self):
|
||||
super(TestPostgresql, self).setUp()
|
||||
self.p.config.write_postgresql_conf()
|
||||
self.p._callback_executor = Mock()
|
||||
|
||||
@patch('subprocess.Popen')
|
||||
@patch.object(Postgresql, 'wait_for_startup')
|
||||
@@ -134,6 +134,9 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
|
||||
self.p.cancellable.cancel()
|
||||
self.assertFalse(self.p.start())
|
||||
with patch('patroni.postgresql.config.ConfigHandler.effective_configuration',
|
||||
PropertyMock(side_effect=Exception)):
|
||||
self.assertIsNone(self.p.start())
|
||||
|
||||
@patch.object(Postgresql, 'pg_isready')
|
||||
@patch('patroni.postgresql.polling_loop', Mock(return_value=range(1)))
|
||||
@@ -174,6 +177,17 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
mock_callback.assert_called()
|
||||
mock_postmaster.signal_stop.assert_called()
|
||||
|
||||
# Timed out waiting for fast shutdown triggers immediate shutdown
|
||||
mock_postmaster.wait.side_effect = [psutil.TimeoutExpired(30), psutil.TimeoutExpired(30), Mock()]
|
||||
mock_callback.reset_mock()
|
||||
self.assertTrue(self.p.stop(on_safepoint=mock_callback, stop_timeout=30))
|
||||
mock_callback.assert_called()
|
||||
mock_postmaster.signal_stop.assert_called()
|
||||
|
||||
# Immediate shutdown succeeded
|
||||
mock_postmaster.wait.side_effect = [psutil.TimeoutExpired(30), Mock()]
|
||||
self.assertTrue(self.p.stop(on_safepoint=mock_callback, stop_timeout=30))
|
||||
|
||||
# Stop signal failed
|
||||
mock_postmaster.signal_stop.return_value = False
|
||||
self.assertFalse(self.p.stop())
|
||||
@@ -184,6 +198,11 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
self.assertTrue(self.p.stop(on_safepoint=mock_callback))
|
||||
mock_callback.assert_called()
|
||||
|
||||
# Fast shutdown is timed out but when immediate postmaster is already gone
|
||||
mock_postmaster.wait.side_effect = [psutil.TimeoutExpired(30), Mock()]
|
||||
mock_postmaster.signal_stop.side_effect = [None, True]
|
||||
self.assertTrue(self.p.stop(on_safepoint=mock_callback, stop_timeout=30))
|
||||
|
||||
def test_restart(self):
|
||||
self.p.start = Mock(return_value=False)
|
||||
self.assertFalse(self.p.restart())
|
||||
@@ -200,7 +219,7 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
self.assertEqual(self.p.checkpoint({'user': 'postgres'}), 'is_in_recovery=true')
|
||||
with patch.object(MockCursor, 'execute', Mock(return_value=None)):
|
||||
self.assertIsNone(self.p.checkpoint())
|
||||
self.assertEqual(self.p.checkpoint(), 'not accessible or not healty')
|
||||
self.assertEqual(self.p.checkpoint(timeout=10), 'not accessible or not healty')
|
||||
|
||||
@patch('patroni.postgresql.config.mtime', mock_mtime)
|
||||
@patch('patroni.postgresql.config.ConfigHandler._get_pg_settings')
|
||||
@@ -230,6 +249,10 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
self.p.config.write_recovery_conf({'standby_mode': 'on', 'primary_conninfo': conninfo.copy()})
|
||||
self.p.config.write_postgresql_conf()
|
||||
self.assertEqual(self.p.config.check_recovery_conf(None), (False, False))
|
||||
with patch.object(Postgresql, 'primary_conninfo', Mock(return_value='host=1')):
|
||||
mock_get_pg_settings.return_value['primary_slot_name'] = [
|
||||
'primary_slot_name', '', '', 'string', 'postmaster', self.p.config._postgresql_conf]
|
||||
self.assertEqual(self.p.config.check_recovery_conf(None), (True, True))
|
||||
|
||||
@patch.object(Postgresql, 'major_version', PropertyMock(return_value=120000))
|
||||
@patch.object(Postgresql, 'is_running', MockPostmaster)
|
||||
@@ -247,11 +270,12 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
self.assertEqual(self.p.config.check_recovery_conf(None), (True, True))
|
||||
|
||||
@patch.object(Postgresql, 'major_version', PropertyMock(return_value=100000))
|
||||
@patch.object(Postgresql, 'primary_conninfo', Mock(return_value='host=1'))
|
||||
def test__read_recovery_params_pre_v12(self):
|
||||
self.p.config.write_recovery_conf({'standby_mode': 'on', 'primary_conninfo': {'password': 'foo'}})
|
||||
self.p.config.write_recovery_conf({'standby_mode': 'off', 'primary_conninfo': {'password': 'foo'}})
|
||||
self.assertEqual(self.p.config.check_recovery_conf(None), (True, True))
|
||||
self.assertEqual(self.p.config.check_recovery_conf(None), (True, True))
|
||||
self.p.config.write_recovery_conf({'standby_mode': '\n'})
|
||||
self.p.config.write_recovery_conf({'restore_command': '\n'})
|
||||
with patch('patroni.postgresql.config.mtime', mock_mtime):
|
||||
self.assertEqual(self.p.config.check_recovery_conf(None), (True, True))
|
||||
|
||||
@@ -281,8 +305,8 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
@patch.object(Postgresql, 'is_running', Mock(return_value=True))
|
||||
def test_sync_replication_slots(self):
|
||||
self.p.start()
|
||||
config = ClusterConfig(1, {'slots': {'ls': {'database': 'a', 'plugin': 'b'},
|
||||
'A': 0, 'test_3': 0, 'b': {'type': 'logical', 'plugin': '1'}}}, 1)
|
||||
config = ClusterConfig(1, {'slots': {'test_3': {'database': 'a', 'plugin': 'b'},
|
||||
'A': 0, 'ls': 0, 'b': {'type': 'logical', 'plugin': '1'}}}, 1)
|
||||
cluster = Cluster(True, config, self.leader, 0, [self.me, self.other, self.leadermem], None, None, None)
|
||||
with mock.patch('patroni.postgresql.Postgresql._query', Mock(side_effect=psycopg2.OperationalError)):
|
||||
self.p.slots_handler.sync_replication_slots(cluster)
|
||||
@@ -318,6 +342,11 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
with patch.object(Postgresql, '_query', Mock(side_effect=RetryFailedError(''))):
|
||||
self.assertRaises(PostgresConnectionException, self.p.is_leader)
|
||||
|
||||
@patch.object(Postgresql, 'controldata',
|
||||
Mock(return_value={'Database cluster state': 'shut down', 'Latest checkpoint location': 'X/678'}))
|
||||
def test_latest_checkpoint_location(self):
|
||||
self.assertIsNone(self.p.latest_checkpoint_location())
|
||||
|
||||
def test_reload(self):
|
||||
self.assertTrue(self.p.reload())
|
||||
|
||||
@@ -328,10 +357,22 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
mock_is_running.return_value = False
|
||||
self.assertFalse(self.p.is_healthy())
|
||||
|
||||
def test_promote(self):
|
||||
@patch('psutil.Popen')
|
||||
def test_promote(self, mock_popen):
|
||||
mock_popen.return_value.wait.return_value = 0
|
||||
task = CriticalTask()
|
||||
self.assertTrue(self.p.promote(0, task))
|
||||
|
||||
self.p.set_role('replica')
|
||||
self.assertIsNone(self.p.promote(0))
|
||||
self.assertTrue(self.p.promote(0))
|
||||
self.p.config._config['pre_promote'] = 'test'
|
||||
with patch('patroni.postgresql.cancellable.CancellableSubprocess.is_cancelled', PropertyMock(return_value=1)):
|
||||
self.assertFalse(self.p.promote(0, task))
|
||||
|
||||
mock_popen.side_effect = Exception
|
||||
self.assertFalse(self.p.promote(0, task))
|
||||
task.reset()
|
||||
task.cancel()
|
||||
self.assertFalse(self.p.promote(0, task))
|
||||
|
||||
def test_timeline_wal_position(self):
|
||||
self.assertEqual(self.p.timeline_wal_position(), (1, 2, 1))
|
||||
@@ -372,6 +413,10 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
|
||||
@patch('os.rename', Mock())
|
||||
@patch('os.path.isdir', Mock(return_value=True))
|
||||
@patch('os.unlink', Mock())
|
||||
@patch('os.symlink', Mock())
|
||||
@patch('patroni.postgresql.Postgresql.pg_wal_realpath', Mock(return_value={'pg_wal': '/mnt/pg_wal'}))
|
||||
@patch('patroni.postgresql.Postgresql.pg_tblspc_realpaths', Mock(return_value={'42': '/mnt/tablespaces/archive'}))
|
||||
def test_move_data_directory(self):
|
||||
self.p.move_data_directory()
|
||||
with patch('os.rename', Mock(side_effect=OSError)):
|
||||
@@ -385,13 +430,15 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
|
||||
def test_remove_data_directory(self):
|
||||
def _symlink(src, dst):
|
||||
try:
|
||||
if os.name != 'nt': # os.symlink under Windows needs admin rights skip it
|
||||
os.symlink(src, dst)
|
||||
except OSError:
|
||||
if os.name == 'nt': # os.symlink under Windows needs admin rights skip it
|
||||
pass
|
||||
|
||||
os.makedirs(os.path.join(self.p.data_dir, 'foo'))
|
||||
_symlink('foo', os.path.join(self.p.data_dir, 'pg_wal'))
|
||||
os.makedirs(os.path.join(self.p.data_dir, 'foo_tsp'))
|
||||
pg_tblspc = os.path.join(self.p.data_dir, 'pg_tblspc')
|
||||
os.makedirs(pg_tblspc)
|
||||
_symlink('../foo_tsp', os.path.join(pg_tblspc, '12345'))
|
||||
self.p.remove_data_directory()
|
||||
open(self.p.data_dir, 'w').close()
|
||||
self.p.remove_data_directory()
|
||||
@@ -562,36 +609,43 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
def test_pick_sync_standby(self):
|
||||
cluster = Cluster(True, None, self.leader, 0, [self.me, self.other, self.leadermem], None,
|
||||
SyncState(0, self.me.name, self.leadermem.name), None)
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.fetchone.return_value = ('remote_apply',)
|
||||
|
||||
with patch.object(Postgresql, "query", return_value=[
|
||||
(self.leadermem.name, 'streaming', 'sync'),
|
||||
(self.me.name, 'streaming', 'async'),
|
||||
(self.other.name, 'streaming', 'async'),
|
||||
with patch.object(Postgresql, "query", side_effect=[
|
||||
mock_cursor,
|
||||
[(self.leadermem.name, 'streaming', 'sync'),
|
||||
(self.me.name, 'streaming', 'async'),
|
||||
(self.other.name, 'streaming', 'async')]
|
||||
]):
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), (self.leadermem.name, True))
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), ([self.leadermem.name], [self.leadermem.name]))
|
||||
|
||||
with patch.object(Postgresql, "query", return_value=[
|
||||
(self.me.name, 'streaming', 'async'),
|
||||
(self.leadermem.name, 'streaming', 'potential'),
|
||||
(self.other.name, 'streaming', 'async'),
|
||||
with patch.object(Postgresql, "query", side_effect=[
|
||||
mock_cursor,
|
||||
[(self.leadermem.name, 'streaming', 'potential'),
|
||||
(self.me.name, 'streaming', 'async'),
|
||||
(self.other.name, 'streaming', 'async')]
|
||||
]):
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), (self.leadermem.name, False))
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), ([self.leadermem.name], []))
|
||||
|
||||
with patch.object(Postgresql, "query", return_value=[
|
||||
(self.me.name, 'streaming', 'async'),
|
||||
(self.other.name, 'streaming', 'async'),
|
||||
with patch.object(Postgresql, "query", side_effect=[
|
||||
mock_cursor,
|
||||
[(self.me.name, 'streaming', 'async'),
|
||||
(self.other.name, 'streaming', 'async')]
|
||||
]):
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), (self.me.name, False))
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), ([self.me.name], []))
|
||||
|
||||
with patch.object(Postgresql, "query", return_value=[
|
||||
('missing', 'streaming', 'sync'),
|
||||
(self.me.name, 'streaming', 'async'),
|
||||
(self.other.name, 'streaming', 'async'),
|
||||
with patch.object(Postgresql, "query", side_effect=[
|
||||
mock_cursor,
|
||||
[('missing', 'streaming', 'sync'),
|
||||
(self.me.name, 'streaming', 'async'),
|
||||
(self.other.name, 'streaming', 'async')]
|
||||
]):
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), (self.me.name, False))
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), ([self.me.name], []))
|
||||
|
||||
with patch.object(Postgresql, "query", return_value=[]):
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), (None, False))
|
||||
with patch.object(Postgresql, "query", side_effect=[mock_cursor, []]):
|
||||
self.p._major_version = 90400
|
||||
self.assertEqual(self.p.pick_synchronous_standby(cluster), ([], []))
|
||||
|
||||
def test_set_sync_standby(self):
|
||||
def value_in_conf():
|
||||
@@ -601,21 +655,21 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
return line.strip()
|
||||
|
||||
mock_reload = self.p.reload = Mock()
|
||||
self.p.config.set_synchronous_standby('n1')
|
||||
self.p.config.set_synchronous_standby(['n1'])
|
||||
self.assertEqual(value_in_conf(), "synchronous_standby_names = 'n1'")
|
||||
mock_reload.assert_called()
|
||||
|
||||
mock_reload.reset_mock()
|
||||
self.p.config.set_synchronous_standby('n1')
|
||||
self.p.config.set_synchronous_standby(['n1'])
|
||||
mock_reload.assert_not_called()
|
||||
self.assertEqual(value_in_conf(), "synchronous_standby_names = 'n1'")
|
||||
|
||||
self.p.config.set_synchronous_standby('n2')
|
||||
self.p.config.set_synchronous_standby(['n1', 'n2'])
|
||||
mock_reload.assert_called()
|
||||
self.assertEqual(value_in_conf(), "synchronous_standby_names = 'n2'")
|
||||
self.assertEqual(value_in_conf(), "synchronous_standby_names = '2 (n1,n2)'")
|
||||
|
||||
mock_reload.reset_mock()
|
||||
self.p.config.set_synchronous_standby(None)
|
||||
self.p.config.set_synchronous_standby([])
|
||||
mock_reload.assert_called()
|
||||
self.assertEqual(value_in_conf(), None)
|
||||
|
||||
@@ -645,44 +699,8 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
mock_postmaster.signal_stop.assert_called()
|
||||
mock_postmaster.wait.assert_called()
|
||||
|
||||
def test_read_postmaster_opts(self):
|
||||
m = mock_open(read_data='/usr/lib/postgres/9.6/bin/postgres "-D" "data/postgresql0" \
|
||||
"--listen_addresses=127.0.0.1" "--port=5432" "--hot_standby=on" "--wal_level=hot_standby" \
|
||||
"--wal_log_hints=on" "--max_wal_senders=5" "--max_replication_slots=5"\n')
|
||||
with patch.object(builtins, 'open', m):
|
||||
data = self.p.read_postmaster_opts()
|
||||
self.assertEqual(data['wal_level'], 'hot_standby')
|
||||
self.assertEqual(int(data['max_replication_slots']), 5)
|
||||
self.assertEqual(data.get('D'), None)
|
||||
|
||||
m.side_effect = IOError
|
||||
data = self.p.read_postmaster_opts()
|
||||
self.assertEqual(data, dict())
|
||||
|
||||
@patch('psutil.Popen')
|
||||
def test_single_user_mode(self, subprocess_popen_mock):
|
||||
subprocess_popen_mock.return_value.wait.return_value = 0
|
||||
self.assertEqual(self.p.single_user_mode('CHECKPOINT', {'archive_mode': 'on'}), 0)
|
||||
|
||||
@patch('os.listdir', Mock(side_effect=[OSError, ['a', 'b']]))
|
||||
@patch('os.unlink', Mock(side_effect=OSError))
|
||||
@patch('os.remove', Mock())
|
||||
@patch('os.path.islink', Mock(side_effect=[True, False]))
|
||||
@patch('os.path.isfile', Mock(return_value=True))
|
||||
def test_cleanup_archive_status(self):
|
||||
self.p.cleanup_archive_status()
|
||||
self.p.cleanup_archive_status()
|
||||
|
||||
@patch('os.unlink', Mock())
|
||||
@patch('os.listdir', Mock(return_value=[]))
|
||||
@patch('os.path.isfile', Mock(return_value=True))
|
||||
@patch.object(Postgresql, 'read_postmaster_opts', Mock(return_value={}))
|
||||
@patch.object(Postgresql, 'single_user_mode', Mock(return_value=0))
|
||||
def test_fix_cluster_state(self):
|
||||
self.assertTrue(self.p.fix_cluster_state())
|
||||
|
||||
def test_replica_cached_timeline(self):
|
||||
self.assertEqual(self.p.replica_cached_timeline(1), 2)
|
||||
self.assertEqual(self.p.replica_cached_timeline(2), 3)
|
||||
|
||||
def test_get_master_timeline(self):
|
||||
self.assertEqual(self.p.get_master_timeline(), 1)
|
||||
@@ -692,7 +710,6 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
with patch.object(Postgresql, 'controldata',
|
||||
Mock(return_value={'max_connections setting': '200',
|
||||
'max_worker_processes setting': '20',
|
||||
'max_prepared_xacts setting': '100',
|
||||
'max_locks_per_xact setting': '100',
|
||||
'max_wal_senders setting': 10})):
|
||||
self.p.cancellable.cancel()
|
||||
@@ -703,3 +720,14 @@ class TestPostgresql(BaseTestPostgresql):
|
||||
@patch('os.path.isfile', Mock(return_value=False))
|
||||
def test_pgpass_is_dir(self):
|
||||
self.assertRaises(PatroniException, self.setUp)
|
||||
|
||||
@patch.object(Postgresql, '_query', Mock(side_effect=RetryFailedError('')))
|
||||
def test_received_timeline(self):
|
||||
self.p.set_role('standby_leader')
|
||||
self.p.reset_cluster_info_state()
|
||||
self.assertRaises(PostgresConnectionException, self.p.received_timeline)
|
||||
|
||||
def test__write_recovery_params(self):
|
||||
self.p.config._write_recovery_params(Mock(), {'pause_at_recovery_target': 'false'})
|
||||
with patch.object(Postgresql, 'major_version', PropertyMock(return_value=90400)):
|
||||
self.p.config._write_recovery_params(Mock(), {'recovery_target_action': 'PROMOTE'})
|
||||
|
||||
@@ -63,6 +63,37 @@ class TestPostmasterProcess(unittest.TestCase):
|
||||
mock_init.side_effect = None
|
||||
self.assertNotEqual(PostmasterProcess.from_pid(123), None)
|
||||
|
||||
@patch('psutil.Process.__init__', Mock())
|
||||
@patch('psutil.wait_procs', Mock())
|
||||
@patch('psutil.Process.suspend')
|
||||
@patch('psutil.Process.children')
|
||||
@patch('psutil.Process.kill')
|
||||
def test_signal_kill(self, mock_kill, mock_children, mock_suspend):
|
||||
proc = PostmasterProcess(123)
|
||||
|
||||
# all processes successfully stopped
|
||||
mock_children.return_value = [Mock()]
|
||||
mock_children.return_value[0].kill.side_effect = psutil.Error
|
||||
self.assertTrue(proc.signal_kill())
|
||||
|
||||
# postmaster has gone before suspend
|
||||
mock_suspend.side_effect = psutil.NoSuchProcess(123)
|
||||
self.assertTrue(proc.signal_kill())
|
||||
|
||||
# postmaster has gone before we got a list of children
|
||||
mock_suspend.side_effect = psutil.Error()
|
||||
mock_children.side_effect = psutil.NoSuchProcess(123)
|
||||
self.assertTrue(proc.signal_kill())
|
||||
|
||||
# postmaster has gone after we got a list of children
|
||||
mock_children.side_effect = psutil.Error()
|
||||
mock_kill.side_effect = psutil.NoSuchProcess(123)
|
||||
self.assertTrue(proc.signal_kill())
|
||||
|
||||
# failed to kill postmaster
|
||||
mock_kill.side_effect = psutil.AccessDenied(123)
|
||||
self.assertFalse(proc.signal_kill())
|
||||
|
||||
@patch('psutil.Process.__init__', Mock())
|
||||
@patch('psutil.Process.send_signal')
|
||||
@patch('psutil.Process.pid', Mock(return_value=123))
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
import os
|
||||
import unittest
|
||||
import time
|
||||
|
||||
from mock import Mock, patch
|
||||
from patroni.dcs.raft import DynMemberSyncObj, KVStoreTTL, Raft, SyncObjUtility
|
||||
from pysyncobj import SyncObjConf, FAIL_REASON
|
||||
|
||||
|
||||
def remove_files(prefix):
|
||||
for f in ('journal', 'dump'):
|
||||
f = prefix + f
|
||||
if os.path.isfile(f):
|
||||
for i in range(0, 15):
|
||||
try:
|
||||
if os.path.isfile(f):
|
||||
os.unlink(f)
|
||||
break
|
||||
else:
|
||||
break
|
||||
except Exception:
|
||||
time.sleep(1.0)
|
||||
|
||||
|
||||
@patch('pysyncobj.tcp_server.TcpServer.bind', Mock())
|
||||
class TestDynMemberSyncObj(unittest.TestCase):
|
||||
|
||||
@patch('pysyncobj.tcp_server.TcpServer.bind', Mock())
|
||||
def setUp(self):
|
||||
self.conf = SyncObjConf(appendEntriesUseBatch=False, dynamicMembershipChange=True, autoTick=False)
|
||||
self.so = DynMemberSyncObj('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
||||
|
||||
@patch.object(SyncObjUtility, 'sendMessage')
|
||||
def test_add_member(self, mock_send_message):
|
||||
mock_send_message.return_value = [{'addr': '127.0.0.1:1235'}, {'addr': '127.0.0.1:1236'}]
|
||||
mock_send_message.ver = 0
|
||||
DynMemberSyncObj('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
||||
self.conf.dynamicMembershipChange = False
|
||||
DynMemberSyncObj('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
||||
|
||||
def test___onUtilityMessage(self):
|
||||
self.so._SyncObj__encryptor = Mock()
|
||||
mock_conn = Mock()
|
||||
mock_conn.sendRandKey = None
|
||||
self.so._SyncObj__transport._onIncomingMessageReceived(mock_conn, 'randkey')
|
||||
self.so._SyncObj__transport._onIncomingMessageReceived(mock_conn, ['members'])
|
||||
self.so._SyncObj__transport._onIncomingMessageReceived(mock_conn, ['status'])
|
||||
|
||||
def test__SyncObj__doChangeCluster(self):
|
||||
self.so._SyncObj__doChangeCluster(['add', '127.0.0.1:1236'])
|
||||
|
||||
def test_utility(self):
|
||||
utility = SyncObjUtility(['127.0.0.1:1235'], self.conf)
|
||||
utility.setPartnerNode(list(utility._SyncObj__otherNodes)[0])
|
||||
utility.sendMessage(['members'])
|
||||
utility._onMessageReceived(0, '')
|
||||
|
||||
|
||||
class TestKVStoreTTL(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.conf = SyncObjConf(appendEntriesUseBatch=False, appendEntriesPeriod=0.001,
|
||||
raftMinTimeout=0.004, raftMaxTimeout=0.005, autoTickPeriod=0.001)
|
||||
callback = Mock()
|
||||
callback.replicated = False
|
||||
self.so = KVStoreTTL('127.0.0.1:1234', [], self.conf, on_set=callback, on_delete=callback)
|
||||
self.so.set_retry_timeout(10)
|
||||
|
||||
@staticmethod
|
||||
def destroy(so):
|
||||
so.destroy()
|
||||
so._SyncObj__thread.join()
|
||||
|
||||
def tearDown(self):
|
||||
if self.so:
|
||||
self.destroy(self.so)
|
||||
|
||||
def test_set(self):
|
||||
self.assertTrue(self.so.set('foo', 'bar', prevExist=False, ttl=30))
|
||||
self.assertFalse(self.so.set('foo', 'bar', prevExist=False, ttl=30))
|
||||
self.assertFalse(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1}, prevValue=''))
|
||||
self.assertTrue(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1}))
|
||||
|
||||
def test_delete(self):
|
||||
self.conf.autoTickPeriod = 0.1
|
||||
self.so.set('foo', 'bar')
|
||||
self.so.set('fooo', 'bar')
|
||||
self.assertFalse(self.so.delete('foo', prevValue='buz'))
|
||||
self.assertFalse(self.so.delete('foo', prevValue='bar', timeout=0.00001))
|
||||
self.assertFalse(self.so.delete('foo', prevValue='bar'))
|
||||
self.assertTrue(self.so.delete('foo', recursive=True))
|
||||
self.assertFalse(self.so.retry(self.so._delete, 'foo', prevValue=''))
|
||||
|
||||
def test_expire(self):
|
||||
self.so.set('foo', 'bar', ttl=0.001)
|
||||
time.sleep(1)
|
||||
self.assertIsNone(self.so.get('foo'))
|
||||
self.assertEqual(self.so.get('foo', recursive=True), {})
|
||||
|
||||
@patch('time.sleep', Mock())
|
||||
def test_retry(self):
|
||||
return_values = [FAIL_REASON.QUEUE_FULL, FAIL_REASON.SUCCESS, FAIL_REASON.REQUEST_DENIED]
|
||||
|
||||
def test(callback):
|
||||
callback(True, return_values.pop(0))
|
||||
self.assertTrue(self.so.retry(test))
|
||||
self.assertFalse(self.so.retry(test))
|
||||
|
||||
def test_on_ready_override(self):
|
||||
self.assertTrue(self.so.set('foo', 'bar'))
|
||||
self.destroy(self.so)
|
||||
self.so = None
|
||||
self.conf.onReady = Mock()
|
||||
self.conf.autoTick = False
|
||||
so = KVStoreTTL('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
||||
so.doTick(0)
|
||||
so.destroy()
|
||||
|
||||
|
||||
class TestRaft(unittest.TestCase):
|
||||
|
||||
def test_raft(self):
|
||||
raft = Raft({'ttl': 30, 'scope': 'test', 'name': 'pg', 'self_addr': '127.0.0.1:1234', 'retry_timeout': 10})
|
||||
raft.set_retry_timeout(20)
|
||||
raft.set_ttl(60)
|
||||
self.assertTrue(raft.touch_member(''))
|
||||
self.assertTrue(raft.initialize())
|
||||
self.assertTrue(raft.cancel_initialization())
|
||||
self.assertTrue(raft.set_config_value('{}'))
|
||||
self.assertTrue(raft.write_sync_state('foo', 'bar'))
|
||||
self.assertTrue(raft.update_leader('1'))
|
||||
self.assertTrue(raft.manual_failover('foo', 'bar'))
|
||||
raft.get_cluster()
|
||||
self.assertTrue(raft.delete_sync_state())
|
||||
self.assertTrue(raft.delete_leader())
|
||||
self.assertTrue(raft.set_history_value(''))
|
||||
self.assertTrue(raft.delete_cluster())
|
||||
raft.get_cluster()
|
||||
self.assertTrue(raft.take_leader())
|
||||
raft.watch(None, 0.001)
|
||||
raft._sync_obj.destroy()
|
||||
raft._sync_obj._SyncObj__thread.join()
|
||||
|
||||
def tearDown(self):
|
||||
remove_files('127.0.0.1:1234.')
|
||||
|
||||
def setUp(self):
|
||||
self.tearDown()
|
||||
|
||||
@patch('patroni.dcs.raft.KVStoreTTL')
|
||||
@patch('threading.Event')
|
||||
def test_init(self, mock_event, mock_kvstore):
|
||||
mock_kvstore.return_value.applied_local_log = False
|
||||
mock_event.return_value.isSet.side_effect = [False, True]
|
||||
self.assertIsNotNone(Raft({'ttl': 30, 'scope': 'test', 'name': 'pg', 'patronictl': True, 'self_addr': '1'}))
|
||||
@@ -0,0 +1,44 @@
|
||||
import logging
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from mock import Mock, patch
|
||||
from pysyncobj import SyncObj
|
||||
from patroni.config import Config
|
||||
from patroni.raft_controller import RaftController, main as _main
|
||||
|
||||
from . import SleepException
|
||||
from .test_raft import remove_files
|
||||
|
||||
|
||||
class TestPatroniRaftController(unittest.TestCase):
|
||||
|
||||
SELF_ADDR = '127.0.0.1:5360'
|
||||
|
||||
def remove_files(self):
|
||||
remove_files(self.SELF_ADDR + '.')
|
||||
|
||||
@patch('pysyncobj.tcp_server.TcpServer.bind', Mock())
|
||||
def setUp(self):
|
||||
self._handlers = logging.getLogger().handlers[:]
|
||||
self.remove_files()
|
||||
os.environ['PATRONI_RAFT_SELF_ADDR'] = self.SELF_ADDR
|
||||
config = Config('postgres0.yml', validator=None)
|
||||
self.rc = RaftController(config)
|
||||
|
||||
def tearDown(self):
|
||||
logging.getLogger().handlers[:] = self._handlers
|
||||
self.remove_files()
|
||||
|
||||
def test_reload_config(self):
|
||||
self.rc.reload_config()
|
||||
|
||||
@patch('logging.Logger.error', Mock(side_effect=SleepException))
|
||||
@patch.object(SyncObj, 'doTick', Mock(side_effect=Exception))
|
||||
def test_run(self):
|
||||
self.assertRaises(SleepException, self.rc.run)
|
||||
self.rc.shutdown()
|
||||
|
||||
@patch('sys.argv', ['patroni'])
|
||||
def test_patroni_raft_controller_main(self):
|
||||
self.assertRaises(SystemExit, _main)
|
||||
+140
-17
@@ -1,12 +1,45 @@
|
||||
from mock import Mock, PropertyMock, patch
|
||||
from mock import Mock, PropertyMock, patch, mock_open
|
||||
|
||||
from patroni.postgresql import Postgresql
|
||||
from patroni.postgresql.cancellable import CancellableSubprocess
|
||||
from patroni.postgresql.rewind import Rewind
|
||||
from six.moves import builtins
|
||||
|
||||
from . import BaseTestPostgresql, MockCursor, psycopg2_connect
|
||||
|
||||
|
||||
class MockThread(object):
|
||||
|
||||
def __init__(self, target, args):
|
||||
self._target = target
|
||||
self._args = args
|
||||
|
||||
def start(self):
|
||||
self._target(*self._args)
|
||||
|
||||
|
||||
def mock_cancellable_call(*args, **kwargs):
|
||||
communicate = kwargs.pop('communicate', None)
|
||||
if isinstance(communicate, dict):
|
||||
communicate.update(stdout=b'', stderr=b'pg_rewind: error: could not open file ' +
|
||||
b'"data/postgresql0/pg_xlog/000000010000000000000003": No such file')
|
||||
return 1
|
||||
|
||||
|
||||
def mock_cancellable_call0(*args, **kwargs):
|
||||
communicate = kwargs.pop('communicate', None)
|
||||
if isinstance(communicate, dict):
|
||||
communicate.update(stdout=b'', stderr=b'')
|
||||
return 0
|
||||
|
||||
|
||||
def mock_cancellable_call1(*args, **kwargs):
|
||||
communicate = kwargs.pop('communicate', None)
|
||||
if isinstance(communicate, dict):
|
||||
communicate.update(stdout=b'', stderr=b'')
|
||||
return 1
|
||||
|
||||
|
||||
@patch('subprocess.call', Mock(return_value=0))
|
||||
@patch('psycopg2.connect', psycopg2_connect)
|
||||
class TestRewind(BaseTestPostgresql):
|
||||
@@ -25,13 +58,23 @@ class TestRewind(BaseTestPostgresql):
|
||||
self.p.config._config['use_pg_rewind'] = False
|
||||
self.assertFalse(self.r.can_rewind)
|
||||
|
||||
@patch.object(CancellableSubprocess, 'call')
|
||||
def test_pg_rewind(self, mock_cancellable_subprocess_call):
|
||||
def test_pg_rewind(self):
|
||||
r = {'user': '', 'host': '', 'port': '', 'database': '', 'password': ''}
|
||||
mock_cancellable_subprocess_call.return_value = 0
|
||||
self.assertTrue(self.r.pg_rewind(r))
|
||||
mock_cancellable_subprocess_call.side_effect = OSError
|
||||
self.assertFalse(self.r.pg_rewind(r))
|
||||
with patch.object(Postgresql, 'major_version', PropertyMock(return_value=130000)),\
|
||||
patch.object(CancellableSubprocess, 'call', Mock(return_value=None)):
|
||||
with patch('subprocess.check_output', Mock(return_value=b'boo')):
|
||||
self.assertFalse(self.r.pg_rewind(r))
|
||||
with patch('subprocess.check_output', Mock(side_effect=Exception)):
|
||||
self.assertFalse(self.r.pg_rewind(r))
|
||||
|
||||
with patch.object(Postgresql, 'major_version', PropertyMock(return_value=120000)),\
|
||||
patch('subprocess.check_output', Mock(return_value=b'foo %f %p %r %% % %')):
|
||||
with patch.object(CancellableSubprocess, 'call', mock_cancellable_call):
|
||||
self.assertFalse(self.r.pg_rewind(r))
|
||||
with patch.object(CancellableSubprocess, 'call', mock_cancellable_call0):
|
||||
self.assertTrue(self.r.pg_rewind(r))
|
||||
with patch.object(CancellableSubprocess, 'call', mock_cancellable_call1):
|
||||
self.assertFalse(self.r.pg_rewind(r))
|
||||
|
||||
@patch.object(Rewind, 'can_rewind', PropertyMock(return_value=True))
|
||||
def test__get_local_timeline_lsn(self):
|
||||
@@ -39,14 +82,15 @@ class TestRewind(BaseTestPostgresql):
|
||||
with patch.object(Postgresql, 'controldata',
|
||||
Mock(return_value={'Database cluster state': 'shut down in recovery',
|
||||
'Minimum recovery ending location': '0/0',
|
||||
"Min recovery ending loc's timeline": '0'})):
|
||||
"Min recovery ending loc's timeline": '0',
|
||||
'Latest checkpoint location': '0/'})):
|
||||
self.r.rewind_or_reinitialize_needed_and_possible(self.leader)
|
||||
|
||||
with patch.object(Postgresql, 'is_running', Mock(return_value=True)):
|
||||
with patch.object(MockCursor, 'fetchone', Mock(side_effect=[(False, ), Exception])):
|
||||
with patch.object(MockCursor, 'fetchone', Mock(side_effect=[(0, 0, 1, 1,), Exception])):
|
||||
self.r.rewind_or_reinitialize_needed_and_possible(self.leader)
|
||||
|
||||
@patch.object(CancellableSubprocess, 'call', Mock(return_value=0))
|
||||
@patch.object(CancellableSubprocess, 'call', mock_cancellable_call)
|
||||
@patch.object(Postgresql, 'checkpoint', side_effect=['', '1'],)
|
||||
@patch.object(Postgresql, 'stop', Mock(return_value=False))
|
||||
@patch.object(Postgresql, 'start', Mock())
|
||||
@@ -72,11 +116,19 @@ class TestRewind(BaseTestPostgresql):
|
||||
with patch.object(Postgresql, 'is_running', Mock(return_value=True)):
|
||||
self.r.execute(self.leader)
|
||||
|
||||
@patch('patroni.postgresql.rewind.logger.info')
|
||||
def test__log_master_history(self, mock_logger):
|
||||
history = [[n, n, ''] for n in range(1, 10)]
|
||||
self.r._log_master_history(history, 1)
|
||||
expected = '\n'.join(['{0}\t0/{0}\t'.format(n) for n in range(1, 4)] + ['...', '9\t0/9\t'])
|
||||
self.assertEqual(mock_logger.call_args[0][1], expected)
|
||||
|
||||
@patch.object(Postgresql, 'start', Mock())
|
||||
@patch.object(Rewind, 'can_rewind', PropertyMock(return_value=True))
|
||||
@patch.object(Rewind, '_get_local_timeline_lsn', Mock(return_value=(2, '40159C1')))
|
||||
@patch.object(Rewind, '_get_local_timeline_lsn')
|
||||
@patch.object(Rewind, 'check_leader_is_not_in_recovery')
|
||||
def test__check_timeline_and_lsn(self, mock_check_leader_is_not_in_recovery):
|
||||
def test__check_timeline_and_lsn(self, mock_check_leader_is_not_in_recovery, mock_get_local_timeline_lsn):
|
||||
mock_get_local_timeline_lsn.return_value = (True, 2, 67197377)
|
||||
mock_check_leader_is_not_in_recovery.return_value = False
|
||||
self.r.trigger_check_diverged_lsn()
|
||||
self.assertFalse(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
@@ -88,20 +140,91 @@ class TestRewind(BaseTestPostgresql):
|
||||
with patch('psycopg2.connect', Mock(side_effect=Exception)):
|
||||
self.assertFalse(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
self.r.trigger_check_diverged_lsn()
|
||||
with patch.object(MockCursor, 'fetchone', Mock(side_effect=[('', 2, '0/0'), ('', b'3\t0/40159C0\tn\n')])):
|
||||
with patch.object(MockCursor, 'fetchone', Mock(side_effect=[('', 3, '0/0'), ('', b'3\t0/40159C0\tn\n')])):
|
||||
self.assertFalse(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
self.r.trigger_check_diverged_lsn()
|
||||
with patch.object(MockCursor, 'fetchone', Mock(return_value=('', 1, '0/0'))):
|
||||
with patch.object(Rewind, '_get_local_timeline_lsn', Mock(return_value=(1, '0/0'))):
|
||||
with patch.object(Rewind, '_get_local_timeline_lsn', Mock(return_value=(True, 1, '0/0'))):
|
||||
self.assertFalse(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
self.r.trigger_check_diverged_lsn()
|
||||
self.assertTrue(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
|
||||
self.r.reset_state()
|
||||
self.r.trigger_check_diverged_lsn()
|
||||
mock_get_local_timeline_lsn.return_value = (False, 2, 67296664)
|
||||
self.assertTrue(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
|
||||
with patch('subprocess.Popen') as mock_popen:
|
||||
mock_popen.return_value.communicate.return_value = (
|
||||
b'0, lsn: 0/040159C1, prev 0/\n',
|
||||
b'pg_waldump: fatal: error in WAL record at 0/40159C1: invalid record length at /: wanted 24, got 0\n'
|
||||
)
|
||||
self.r.reset_state()
|
||||
self.r.trigger_check_diverged_lsn()
|
||||
mock_get_local_timeline_lsn.return_value = (False, 2, 67197377)
|
||||
self.assertTrue(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
self.r.reset_state()
|
||||
self.r.trigger_check_diverged_lsn()
|
||||
mock_popen.side_effect = Exception
|
||||
self.assertTrue(self.r.rewind_or_reinitialize_needed_and_possible(self.leader))
|
||||
|
||||
@patch.object(MockCursor, 'fetchone', Mock(side_effect=[(True,), Exception]))
|
||||
def test_check_leader_is_not_in_recovery(self):
|
||||
self.r.check_leader_is_not_in_recovery()
|
||||
self.r.check_leader_is_not_in_recovery()
|
||||
|
||||
@patch.object(Postgresql, 'controldata', Mock(return_value={"Latest checkpoint's TimeLineID": 1}))
|
||||
def test_check_for_checkpoint_after_promote(self):
|
||||
self.r.check_for_checkpoint_after_promote()
|
||||
def test_read_postmaster_opts(self):
|
||||
m = mock_open(read_data='/usr/lib/postgres/9.6/bin/postgres "-D" "data/postgresql0" \
|
||||
"--listen_addresses=127.0.0.1" "--port=5432" "--hot_standby=on" "--wal_level=hot_standby" \
|
||||
"--wal_log_hints=on" "--max_wal_senders=5" "--max_replication_slots=5"\n')
|
||||
with patch.object(builtins, 'open', m):
|
||||
data = self.r.read_postmaster_opts()
|
||||
self.assertEqual(data['wal_level'], 'hot_standby')
|
||||
self.assertEqual(int(data['max_replication_slots']), 5)
|
||||
self.assertEqual(data.get('D'), None)
|
||||
|
||||
m.side_effect = IOError
|
||||
data = self.r.read_postmaster_opts()
|
||||
self.assertEqual(data, dict())
|
||||
|
||||
@patch('psutil.Popen')
|
||||
def test_single_user_mode(self, subprocess_popen_mock):
|
||||
subprocess_popen_mock.return_value.wait.return_value = 0
|
||||
subprocess_popen_mock.return_value.communicate.return_value = ('', '')
|
||||
self.assertEqual(self.r.single_user_mode({'input': 'CHECKPOINT'}, {'archive_mode': 'on'}), 0)
|
||||
|
||||
@patch('os.listdir', Mock(side_effect=[OSError, ['a', 'b']]))
|
||||
@patch('os.unlink', Mock(side_effect=OSError))
|
||||
@patch('os.remove', Mock())
|
||||
@patch('os.path.islink', Mock(side_effect=[True, False]))
|
||||
@patch('os.path.isfile', Mock(return_value=True))
|
||||
def test_cleanup_archive_status(self):
|
||||
self.r.cleanup_archive_status()
|
||||
self.r.cleanup_archive_status()
|
||||
|
||||
@patch('os.unlink', Mock())
|
||||
@patch('os.listdir', Mock(return_value=[]))
|
||||
@patch('os.path.isfile', Mock(return_value=True))
|
||||
@patch.object(Rewind, 'read_postmaster_opts', Mock(return_value={}))
|
||||
@patch.object(Rewind, 'single_user_mode', Mock(return_value=0))
|
||||
def test_ensure_clean_shutdown(self):
|
||||
self.assertTrue(self.r.ensure_clean_shutdown())
|
||||
|
||||
@patch('patroni.postgresql.rewind.Thread', MockThread)
|
||||
@patch.object(Postgresql, 'controldata')
|
||||
@patch.object(Postgresql, 'checkpoint')
|
||||
def test_ensure_checkpoint_after_promote(self, mock_checkpoint, mock_controldata):
|
||||
mock_checkpoint.return_value = None
|
||||
self.r.ensure_checkpoint_after_promote(Mock())
|
||||
self.r.ensure_checkpoint_after_promote(Mock())
|
||||
|
||||
self.r.reset_state()
|
||||
mock_controldata.return_value = {"Latest checkpoint's TimeLineID": 1}
|
||||
mock_checkpoint.side_effect = Exception
|
||||
self.r.ensure_checkpoint_after_promote(Mock())
|
||||
self.r.ensure_checkpoint_after_promote(Mock())
|
||||
|
||||
self.r.reset_state()
|
||||
mock_controldata.side_effect = TypeError
|
||||
self.r.ensure_checkpoint_after_promote(Mock())
|
||||
self.r.ensure_checkpoint_after_promote(Mock())
|
||||
|
||||
+9
-1
@@ -2,7 +2,7 @@ import unittest
|
||||
|
||||
from mock import Mock, patch
|
||||
from patroni.exceptions import PatroniException
|
||||
from patroni.utils import Retry, RetryFailedError, polling_loop, validate_directory
|
||||
from patroni.utils import Retry, RetryFailedError, enable_keepalive, polling_loop, validate_directory
|
||||
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
@@ -33,6 +33,14 @@ class TestUtils(unittest.TestCase):
|
||||
def test_validate_directory_is_not_a_directory(self):
|
||||
self.assertRaises(PatroniException, validate_directory, "/tmp")
|
||||
|
||||
def test_enable_keepalive(self):
|
||||
with patch('socket.SIO_KEEPALIVE_VALS', 1, create=True):
|
||||
self.assertIsNotNone(enable_keepalive(Mock(), 10, 5))
|
||||
with patch('socket.SIO_KEEPALIVE_VALS', None, create=True):
|
||||
for platform in ('linux2', 'darwin', 'other'):
|
||||
with patch('sys.platform', platform):
|
||||
self.assertIsNone(enable_keepalive(Mock(), 10, 5))
|
||||
|
||||
|
||||
@patch('time.sleep', Mock())
|
||||
class TestRetrySleeper(unittest.TestCase):
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
import copy
|
||||
import os
|
||||
import socket
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from mock import Mock, patch, mock_open
|
||||
from patroni.dcs import dcs_modules
|
||||
from patroni.validator import schema
|
||||
from six import StringIO
|
||||
|
||||
available_dcs = [m.split(".")[-1] for m in dcs_modules()]
|
||||
config = {
|
||||
"name": "string",
|
||||
"scope": "string",
|
||||
"restapi": {
|
||||
"listen": "127.0.0.2:800",
|
||||
"connect_address": "127.0.0.2:800"
|
||||
},
|
||||
"bootstrap": {
|
||||
"dcs": {
|
||||
"ttl": 1000,
|
||||
"loop_wait": 1000,
|
||||
"retry_timeout": 1000,
|
||||
"maximum_lag_on_failover": 1000
|
||||
},
|
||||
"pg_hba": ["string"],
|
||||
"initdb": ["string", {"key": "value"}]
|
||||
},
|
||||
"consul": {
|
||||
"host": "127.0.0.1:5000"
|
||||
},
|
||||
"etcd": {
|
||||
"hosts": "127.0.0.1:2379,127.0.0.1:2380"
|
||||
},
|
||||
"exhibitor": {
|
||||
"hosts": ["string"],
|
||||
"port": 4000,
|
||||
"pool_interval": 1000
|
||||
},
|
||||
"zookeeper": {
|
||||
"hosts": "127.0.0.1:3379,127.0.0.1:3380"
|
||||
},
|
||||
"kubernetes": {
|
||||
"namespace": "string",
|
||||
"labels": {},
|
||||
"scope_label": "string",
|
||||
"role_label": "string",
|
||||
"use_endpoints": False,
|
||||
"pod_ip": "127.0.0.1",
|
||||
"ports": [{"name": "string", "port": 1000}],
|
||||
},
|
||||
"postgresql": {
|
||||
"listen": "127.0.0.2,::1:543",
|
||||
"connect_address": "127.0.0.2:543",
|
||||
"authentication": {
|
||||
"replication": {"username": "user"},
|
||||
"superuser": {"username": "user"},
|
||||
"rewind": {"username": "user"},
|
||||
},
|
||||
"data_dir": os.path.join(tempfile.gettempdir(), "data_dir"),
|
||||
"bin_dir": os.path.join(tempfile.gettempdir(), "bin_dir"),
|
||||
"parameters": {
|
||||
"unix_socket_directories": "."
|
||||
},
|
||||
"pg_hba": [u"string"],
|
||||
"pg_ident": ["string"],
|
||||
"pg_ctl_timeout": 1000,
|
||||
"use_pg_rewind": False
|
||||
},
|
||||
"watchdog": {
|
||||
"mode": "off",
|
||||
"device": "string"
|
||||
},
|
||||
"tags": {
|
||||
"nofailover": False,
|
||||
"clonefrom": False,
|
||||
"noloadbalance": False,
|
||||
"nosync": False
|
||||
}
|
||||
}
|
||||
|
||||
directories = []
|
||||
files = []
|
||||
|
||||
|
||||
def isfile_side_effect(arg):
|
||||
if arg.endswith('.exe'):
|
||||
arg = arg[:-4]
|
||||
return arg in files
|
||||
|
||||
|
||||
def isdir_side_effect(arg):
|
||||
return arg in directories
|
||||
|
||||
|
||||
def exists_side_effect(arg):
|
||||
return isfile_side_effect(arg) or isdir_side_effect(arg)
|
||||
|
||||
|
||||
def connect_side_effect(host_port):
|
||||
_, port = host_port
|
||||
if port < 1000:
|
||||
return 1
|
||||
elif port < 10000:
|
||||
return 0
|
||||
else:
|
||||
raise socket.gaierror()
|
||||
|
||||
|
||||
def parse_output(output):
|
||||
result = []
|
||||
for s in output.split("\n"):
|
||||
x = s.split(" ")[0]
|
||||
if x and x not in result:
|
||||
result.append(x)
|
||||
result.sort()
|
||||
return result
|
||||
|
||||
|
||||
@patch('socket.socket.connect_ex', Mock(side_effect=connect_side_effect))
|
||||
@patch('os.path.exists', Mock(side_effect=exists_side_effect))
|
||||
@patch('os.path.isdir', Mock(side_effect=isdir_side_effect))
|
||||
@patch('os.path.isfile', Mock(side_effect=isfile_side_effect))
|
||||
@patch('sys.stderr', new_callable=StringIO)
|
||||
@patch('sys.stdout', new_callable=StringIO)
|
||||
class TestValidator(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
del files[:]
|
||||
del directories[:]
|
||||
|
||||
def test_empty_config(self, mock_out, mock_err):
|
||||
schema({})
|
||||
output = mock_out.getvalue()
|
||||
expected = list(sorted(['name', 'postgresql', 'restapi', 'scope'] + available_dcs))
|
||||
self.assertEqual(expected, parse_output(output))
|
||||
|
||||
def test_complete_config(self, mock_out, mock_err):
|
||||
schema(config)
|
||||
output = mock_out.getvalue()
|
||||
self.assertEqual(['postgresql.bin_dir'], parse_output(output))
|
||||
|
||||
def test_bin_dir_is_file(self, mock_out, mock_err):
|
||||
files.append(config["postgresql"]["data_dir"])
|
||||
files.append(config["postgresql"]["bin_dir"])
|
||||
c = copy.deepcopy(config)
|
||||
c["restapi"]["connect_address"] = 'False:blabla'
|
||||
c["etcd"]["hosts"] = ["127.0.0.1:2379", "1244.0.0.1:2379", "127.0.0.1:invalidport"]
|
||||
c["kubernetes"]["pod_ip"] = "127.0.0.1111"
|
||||
schema(c)
|
||||
output = mock_out.getvalue()
|
||||
self.assertEqual(['etcd.hosts.1', 'etcd.hosts.2', 'kubernetes.pod_ip', 'postgresql.bin_dir',
|
||||
'postgresql.data_dir', 'restapi.connect_address'], parse_output(output))
|
||||
|
||||
@patch('socket.inet_pton', Mock(), create=True)
|
||||
def test_bin_dir_is_empty(self, mock_out, mock_err):
|
||||
directories.append(config["postgresql"]["data_dir"])
|
||||
directories.append(config["postgresql"]["bin_dir"])
|
||||
files.append(os.path.join(config["postgresql"]["data_dir"], "global", "pg_control"))
|
||||
c = copy.deepcopy(config)
|
||||
c["restapi"]["connect_address"] = "127.0.0.1:8008"
|
||||
c["kubernetes"]["pod_ip"] = "::1"
|
||||
c["consul"]["host"] = "127.0.0.1:50000"
|
||||
c["etcd"]["host"] = "127.0.0.1:237"
|
||||
c["postgresql"]["listen"] = "127.0.0.1:5432"
|
||||
with patch('patroni.validator.open', mock_open(read_data='9')):
|
||||
schema(c)
|
||||
output = mock_out.getvalue()
|
||||
self.assertEqual(['consul.host', 'etcd.host', 'postgresql.bin_dir', 'postgresql.data_dir',
|
||||
'postgresql.listen', 'restapi.connect_address'], parse_output(output))
|
||||
|
||||
@patch('subprocess.check_output', Mock(return_value=b"postgres (PostgreSQL) 12.1"))
|
||||
def test_data_dir_contains_pg_version(self, mock_out, mock_err):
|
||||
directories.append(config["postgresql"]["data_dir"])
|
||||
directories.append(config["postgresql"]["bin_dir"])
|
||||
directories.append(os.path.join(config["postgresql"]["data_dir"], "pg_wal"))
|
||||
files.append(os.path.join(config["postgresql"]["data_dir"], "global", "pg_control"))
|
||||
files.append(os.path.join(config["postgresql"]["data_dir"], "PG_VERSION"))
|
||||
files.append(os.path.join(config["postgresql"]["bin_dir"], "pg_ctl"))
|
||||
files.append(os.path.join(config["postgresql"]["bin_dir"], "initdb"))
|
||||
files.append(os.path.join(config["postgresql"]["bin_dir"], "pg_controldata"))
|
||||
files.append(os.path.join(config["postgresql"]["bin_dir"], "pg_basebackup"))
|
||||
files.append(os.path.join(config["postgresql"]["bin_dir"], "postgres"))
|
||||
files.append(os.path.join(config["postgresql"]["bin_dir"], "pg_isready"))
|
||||
with patch('patroni.validator.open', mock_open(read_data='12')):
|
||||
schema(config)
|
||||
output = mock_out.getvalue()
|
||||
self.assertEqual([], parse_output(output))
|
||||
|
||||
@patch('subprocess.check_output', Mock(return_value=b"postgres (PostgreSQL) 12.1"))
|
||||
def test_pg_version_missmatch(self, mock_out, mock_err):
|
||||
directories.append(config["postgresql"]["data_dir"])
|
||||
directories.append(config["postgresql"]["bin_dir"])
|
||||
directories.append(os.path.join(config["postgresql"]["data_dir"], "pg_wal"))
|
||||
files.append(os.path.join(config["postgresql"]["data_dir"], "global", "pg_control"))
|
||||
files.append(os.path.join(config["postgresql"]["data_dir"], "PG_VERSION"))
|
||||
c = copy.deepcopy(config)
|
||||
c["etcd"]["hosts"] = []
|
||||
del c["postgresql"]["bin_dir"]
|
||||
with patch('patroni.validator.open', mock_open(read_data='11')):
|
||||
schema(c)
|
||||
output = mock_out.getvalue()
|
||||
self.assertEqual(['etcd.hosts', 'postgresql.data_dir'], parse_output(output))
|
||||
|
||||
@patch('subprocess.check_output', Mock(return_value=b"postgres (PostgreSQL) 12.1"))
|
||||
def test_pg_wal_doesnt_exist(self, mock_out, mock_err):
|
||||
directories.append(config["postgresql"]["data_dir"])
|
||||
directories.append(config["postgresql"]["bin_dir"])
|
||||
files.append(os.path.join(config["postgresql"]["data_dir"], "global", "pg_control"))
|
||||
files.append(os.path.join(config["postgresql"]["data_dir"], "PG_VERSION"))
|
||||
c = copy.deepcopy(config)
|
||||
del c["postgresql"]["bin_dir"]
|
||||
with patch('patroni.validator.open', mock_open(read_data='11')):
|
||||
schema(c)
|
||||
output = mock_out.getvalue()
|
||||
self.assertEqual(['postgresql.data_dir'], parse_output(output))
|
||||
|
||||
def test_data_dir_is_empty_string(self, mock_out, mock_err):
|
||||
directories.append(config["postgresql"]["data_dir"])
|
||||
directories.append(config["postgresql"]["bin_dir"])
|
||||
c = copy.deepcopy(config)
|
||||
c["kubernetes"] = False
|
||||
c["postgresql"]["pg_hba"] = ""
|
||||
c["postgresql"]["data_dir"] = ""
|
||||
c["postgresql"]["bin_dir"] = ""
|
||||
schema(c)
|
||||
output = mock_out.getvalue()
|
||||
self.assertEqual(['kubernetes', 'postgresql.bin_dir',
|
||||
'postgresql.data_dir', 'postgresql.pg_hba'], parse_output(output))
|
||||
+10
-1
@@ -37,6 +37,8 @@ class MockKazooClient(Mock):
|
||||
b'postgres://repuser:rep-pass@localhost:5434/postgres?application_name=http://127.0.0.1:8009/patroni',
|
||||
ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0 if self.exists else -1, 0, 0, 0)
|
||||
)
|
||||
elif path.endswith('/optime/leader'):
|
||||
return (b'500', ZnodeStat(0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0))
|
||||
elif path.endswith('/leader'):
|
||||
if self.leader:
|
||||
return (b'foo', ZnodeStat(0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0))
|
||||
@@ -150,9 +152,15 @@ class TestZooKeeper(unittest.TestCase):
|
||||
|
||||
def test_get_cluster(self):
|
||||
self.assertRaises(ZooKeeperError, self.zk.get_cluster)
|
||||
cluster = self.zk.get_cluster()
|
||||
cluster = self.zk.get_cluster(True)
|
||||
self.assertIsInstance(cluster.leader, Leader)
|
||||
self.zk.touch_member({'foo': 'foo'})
|
||||
self.zk._name = 'bar'
|
||||
self.zk.optime_watcher(None)
|
||||
with patch.object(ZooKeeper, 'get_node', Mock(side_effect=Exception)):
|
||||
self.zk.get_cluster()
|
||||
cluster = self.zk.get_cluster()
|
||||
self.assertEqual(cluster.last_leader_operation, 500)
|
||||
|
||||
def test_delete_leader(self):
|
||||
self.assertTrue(self.zk.delete_leader())
|
||||
@@ -213,6 +221,7 @@ class TestZooKeeper(unittest.TestCase):
|
||||
def test_watch(self):
|
||||
self.zk.watch(None, 0)
|
||||
self.zk.event.isSet = Mock(return_value=True)
|
||||
self.zk._fetch_optime = False
|
||||
self.zk.watch(None, 0)
|
||||
|
||||
def test__kazoo_connect(self):
|
||||
|
||||
Reference in New Issue
Block a user