mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-26 15:40:21 +00:00
Compare commits
388
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.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
@@ -0,0 +1,194 @@
|
|||||||
|
import inspect
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import stat
|
||||||
|
import sys
|
||||||
|
import tarfile
|
||||||
|
import time
|
||||||
|
import zipfile
|
||||||
|
|
||||||
|
|
||||||
|
def install_requirements(what):
|
||||||
|
old_path = sys.path[:]
|
||||||
|
w = os.path.join(os.getcwd(), os.path.dirname(inspect.getfile(inspect.currentframe())))
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(w)))
|
||||||
|
try:
|
||||||
|
from setup import EXTRAS_REQUIRE, read
|
||||||
|
finally:
|
||||||
|
sys.path = old_path
|
||||||
|
requirements = ['mock>=2.0.0', 'flake8', 'pytest', 'pytest-cov'] if what == 'all' else ['behave']
|
||||||
|
requirements += ['coverage']
|
||||||
|
# try to split tests between psycopg2 and psycopg3
|
||||||
|
requirements += ['psycopg[binary]'] if sys.version_info >= (3, 6, 0) and\
|
||||||
|
(sys.platform != 'darwin' or what == 'etcd3') else ['psycopg2-binary']
|
||||||
|
for r in read('requirements.txt').split('\n'):
|
||||||
|
r = r.strip()
|
||||||
|
if r != '':
|
||||||
|
extras = {e for e, v in EXTRAS_REQUIRE.items() if v and any(r.startswith(x) for x in v)}
|
||||||
|
if not extras or what == 'all' or what in extras:
|
||||||
|
requirements.append(r)
|
||||||
|
|
||||||
|
subprocess.call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'])
|
||||||
|
subprocess.call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'wheel'])
|
||||||
|
r = subprocess.call([sys.executable, '-m', 'pip', 'install'] + requirements)
|
||||||
|
s = subprocess.call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'setuptools'])
|
||||||
|
return s | r
|
||||||
|
|
||||||
|
|
||||||
|
def install_packages(what):
|
||||||
|
from mapping import versions
|
||||||
|
|
||||||
|
packages = {
|
||||||
|
'zookeeper': ['zookeeper', 'zookeeper-bin', 'zookeeperd'],
|
||||||
|
'consul': ['consul'],
|
||||||
|
}
|
||||||
|
packages['exhibitor'] = packages['zookeeper']
|
||||||
|
packages = packages.get(what, [])
|
||||||
|
ver = versions.get(what)
|
||||||
|
subprocess.call(['sudo', 'apt-get', 'update', '-y'])
|
||||||
|
subprocess.call(['sudo', 'apt-get', 'install', '-y', 'wget', 'ca-certificates', 'gnupg', 'expect-dev'])
|
||||||
|
subprocess.call(['sudo', 'sh', '-c', "wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc"
|
||||||
|
" | gpg --dearmor > /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg"])
|
||||||
|
subprocess.call(['sudo', 'sed', '-i', 's/pgdg main.*$/pgdg main {0}/'.format(ver),
|
||||||
|
'/etc/apt/sources.list.d/pgdg.list'])
|
||||||
|
subprocess.call(['sudo', 'apt-get', 'update', '-y'])
|
||||||
|
return subprocess.call(['sudo', 'apt-get', 'install', '-y', 'postgresql-' + ver] + packages)
|
||||||
|
|
||||||
|
|
||||||
|
def get_file(url, name):
|
||||||
|
try:
|
||||||
|
from urllib.request import urlretrieve
|
||||||
|
except ImportError:
|
||||||
|
from urllib import urlretrieve
|
||||||
|
|
||||||
|
print('Downloading ' + url)
|
||||||
|
urlretrieve(url, name)
|
||||||
|
|
||||||
|
|
||||||
|
def untar(archive, name):
|
||||||
|
with tarfile.open(archive) as tar:
|
||||||
|
f = tar.extractfile(name)
|
||||||
|
dest = os.path.basename(name)
|
||||||
|
with open(dest, 'wb') as d:
|
||||||
|
shutil.copyfileobj(f, d)
|
||||||
|
return dest
|
||||||
|
|
||||||
|
|
||||||
|
def unzip(archive, name):
|
||||||
|
with zipfile.ZipFile(archive, 'r') as z:
|
||||||
|
name = z.extract(name)
|
||||||
|
dest = os.path.basename(name)
|
||||||
|
shutil.move(name, dest)
|
||||||
|
return dest
|
||||||
|
|
||||||
|
|
||||||
|
def unzip_all(archive):
|
||||||
|
print('Extracting ' + archive)
|
||||||
|
with zipfile.ZipFile(archive, 'r') as z:
|
||||||
|
z.extractall()
|
||||||
|
|
||||||
|
|
||||||
|
def chmod_755(name):
|
||||||
|
os.chmod(name, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
|
||||||
|
stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
|
||||||
|
|
||||||
|
|
||||||
|
def unpack(archive, name):
|
||||||
|
print('Extracting {0} from {1}'.format(name, archive))
|
||||||
|
func = unzip if archive.endswith('.zip') else untar
|
||||||
|
name = func(archive, name)
|
||||||
|
chmod_755(name)
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
|
def install_etcd():
|
||||||
|
version = os.environ.get('ETCDVERSION', '3.3.13')
|
||||||
|
platform = {'linux2': 'linux', 'win32': 'windows', 'cygwin': 'windows'}.get(sys.platform, sys.platform)
|
||||||
|
dirname = 'etcd-v{0}-{1}-amd64'.format(version, platform)
|
||||||
|
ext = 'tar.gz' if platform == 'linux' else 'zip'
|
||||||
|
name = '{0}.{1}'.format(dirname, ext)
|
||||||
|
url = 'https://github.com/etcd-io/etcd/releases/download/v{0}/{1}'.format(version, name)
|
||||||
|
get_file(url, name)
|
||||||
|
ext = '.exe' if platform == 'windows' else ''
|
||||||
|
return int(unpack(name, '{0}/etcd{1}'.format(dirname, ext)) is None)
|
||||||
|
|
||||||
|
|
||||||
|
def install_postgres():
|
||||||
|
version = os.environ.get('PGVERSION', '14.1-1')
|
||||||
|
platform = {'darwin': 'osx', 'win32': 'windows-x64', 'cygwin': 'windows-x64'}[sys.platform]
|
||||||
|
name = 'postgresql-{0}-{1}-binaries.zip'.format(version, platform)
|
||||||
|
get_file('http://get.enterprisedb.com/postgresql/' + name, name)
|
||||||
|
unzip_all(name)
|
||||||
|
bin_dir = os.path.join('pgsql', 'bin')
|
||||||
|
for f in os.listdir(bin_dir):
|
||||||
|
chmod_755(os.path.join(bin_dir, f))
|
||||||
|
subprocess.call(['pgsql/bin/postgres', '-V'])
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def setup_kubernetes():
|
||||||
|
get_file('https://storage.googleapis.com/minikube/k8sReleases/v1.7.0/localkube-linux-amd64', 'localkube')
|
||||||
|
chmod_755('localkube')
|
||||||
|
|
||||||
|
devnull = open(os.devnull, 'w')
|
||||||
|
subprocess.Popen(['sudo', 'nohup', './localkube', '--logtostderr=true', '--enable-dns=false'],
|
||||||
|
stdout=devnull, stderr=devnull)
|
||||||
|
for _ in range(0, 120):
|
||||||
|
if subprocess.call(['wget', '-qO', '-', 'http://127.0.0.1:8080/'], stdout=devnull, stderr=devnull) == 0:
|
||||||
|
break
|
||||||
|
time.sleep(1)
|
||||||
|
else:
|
||||||
|
print('localkube did not start')
|
||||||
|
return 1
|
||||||
|
|
||||||
|
subprocess.call('sudo chmod 644 /var/lib/localkube/certs/*', shell=True)
|
||||||
|
print('Set up .kube/config')
|
||||||
|
kube = os.path.join(os.path.expanduser('~'), '.kube')
|
||||||
|
os.makedirs(kube)
|
||||||
|
with open(os.path.join(kube, 'config'), 'w') as f:
|
||||||
|
f.write("""apiVersion: v1
|
||||||
|
clusters:
|
||||||
|
- cluster:
|
||||||
|
certificate-authority: /var/lib/localkube/certs/ca.crt
|
||||||
|
server: https://127.0.0.1:8443
|
||||||
|
name: local
|
||||||
|
contexts:
|
||||||
|
- context:
|
||||||
|
cluster: local
|
||||||
|
user: myself
|
||||||
|
name: local
|
||||||
|
current-context: local
|
||||||
|
kind: Config
|
||||||
|
preferences: {}
|
||||||
|
users:
|
||||||
|
- name: myself
|
||||||
|
user:
|
||||||
|
client-certificate: /var/lib/localkube/certs/apiserver.crt
|
||||||
|
client-key: /var/lib/localkube/certs/apiserver.key
|
||||||
|
""")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
what = os.environ.get('DCS', sys.argv[1] if len(sys.argv) > 1 else 'all')
|
||||||
|
|
||||||
|
if what != 'all':
|
||||||
|
if sys.platform.startswith('linux'):
|
||||||
|
r = install_packages(what)
|
||||||
|
if r == 0 and what == 'kubernetes':
|
||||||
|
r = setup_kubernetes()
|
||||||
|
else:
|
||||||
|
r = install_postgres()
|
||||||
|
|
||||||
|
if r == 0 and what.startswith('etcd'):
|
||||||
|
r = install_etcd()
|
||||||
|
|
||||||
|
if r != 0:
|
||||||
|
return r
|
||||||
|
|
||||||
|
return install_requirements(what)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
versions = {'etcd': '9.6', 'etcd3': '14', 'consul': '13', 'exhibitor': '12', 'raft': '11', 'kubernetes': '15'}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
name: Publish Patroni distributions to PyPI and TestPyPI
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v[0-9]+.[0-9]+.[0-9]+'
|
||||||
|
release:
|
||||||
|
types:
|
||||||
|
- published
|
||||||
|
jobs:
|
||||||
|
build-n-publish:
|
||||||
|
name: Build and publish Patroni distributions to PyPI and TestPyPI
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@master
|
||||||
|
|
||||||
|
- name: Set up Python 3.9
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.9
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
|
||||||
|
- name: Run tests and flake8
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
|
||||||
|
- name: Build a binary wheel and a source tarball
|
||||||
|
run: python setup.py sdist bdist_wheel
|
||||||
|
|
||||||
|
- name: Publish distribution to Test PyPI
|
||||||
|
if: github.event_name == 'push'
|
||||||
|
uses: pypa/[email protected]
|
||||||
|
with:
|
||||||
|
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
||||||
|
repository_url: https://test.pypi.org/legacy/
|
||||||
|
|
||||||
|
- name: Publish distribution to PyPI
|
||||||
|
if: github.event_name == 'release'
|
||||||
|
uses: pypa/[email protected]
|
||||||
|
with:
|
||||||
|
password: ${{ secrets.PYPI_API_TOKEN }}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
what = os.environ.get('DCS', sys.argv[1] if len(sys.argv) > 1 else 'all')
|
||||||
|
|
||||||
|
if what == 'all':
|
||||||
|
flake8 = subprocess.call([sys.executable, 'setup.py', 'flake8'])
|
||||||
|
test = subprocess.call([sys.executable, 'setup.py', 'test'])
|
||||||
|
version = '.'.join(map(str, sys.version_info[:2]))
|
||||||
|
shutil.move('.coverage', os.path.join(tempfile.gettempdir(), '.coverage.' + version))
|
||||||
|
return flake8 | test
|
||||||
|
elif what == 'combine':
|
||||||
|
tmp = tempfile.gettempdir()
|
||||||
|
for name in os.listdir(tmp):
|
||||||
|
if name.startswith('.coverage.'):
|
||||||
|
shutil.move(os.path.join(tmp, name), name)
|
||||||
|
return subprocess.call([sys.executable, '-m', 'coverage', 'combine'])
|
||||||
|
|
||||||
|
env = os.environ.copy()
|
||||||
|
if sys.platform.startswith('linux'):
|
||||||
|
from mapping import versions
|
||||||
|
|
||||||
|
version = versions.get(what)
|
||||||
|
path = '/usr/lib/postgresql/{0}/bin:.'.format(version)
|
||||||
|
unbuffer = ['timeout', '900', 'unbuffer']
|
||||||
|
else:
|
||||||
|
path = os.path.abspath(os.path.join('pgsql', 'bin'))
|
||||||
|
if sys.platform == 'darwin':
|
||||||
|
path += ':.'
|
||||||
|
unbuffer = []
|
||||||
|
env['PATH'] = path + os.pathsep + env['PATH']
|
||||||
|
env['DCS'] = what
|
||||||
|
|
||||||
|
ret = subprocess.call(unbuffer + [sys.executable, '-m', 'behave'], env=env)
|
||||||
|
|
||||||
|
if ret != 0:
|
||||||
|
if subprocess.call('grep . features/output/*_failed/*postgres?.*', shell=True) != 0:
|
||||||
|
subprocess.call('grep . features/output/*/*postgres?.*', shell=True)
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
name: Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
tags:
|
||||||
|
- v.*
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
unit:
|
||||||
|
runs-on: ${{ matrix.os }}-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu, windows, macos]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: Set up Python 2.7
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 2.7
|
||||||
|
if: matrix.os != 'windows'
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
if: matrix.os != 'windows'
|
||||||
|
- name: Run tests and flake8
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
if: matrix.os != 'windows'
|
||||||
|
|
||||||
|
- name: Set up Python 3.6
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.6
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
- name: Run tests and flake8
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
|
||||||
|
- name: Set up Python 3.7
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.7
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
- name: Run tests and flake8
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
|
||||||
|
- name: Set up Python 3.8
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.8
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
- name: Run tests and flake8
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
|
||||||
|
- name: Set up Python 3.9
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.9
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
- name: Run tests and flake8
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
|
||||||
|
- name: Set up Python 3.10
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: '3.10'
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
- name: Run tests and flake8
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
|
||||||
|
- name: Combine coverage
|
||||||
|
run: python .github/workflows/run_tests.py combine
|
||||||
|
|
||||||
|
- name: Install coveralls
|
||||||
|
run: python -m pip install coveralls
|
||||||
|
|
||||||
|
- name: Upload Coverage
|
||||||
|
env:
|
||||||
|
COVERALLS_FLAG_NAME: unit-${{ matrix.os }}
|
||||||
|
COVERALLS_PARALLEL: 'true'
|
||||||
|
GITHUB_TOKEN: ${{ secrets.github_token }}
|
||||||
|
run: python -m coveralls --service=github
|
||||||
|
|
||||||
|
behave:
|
||||||
|
runs-on: ${{ matrix.os }}-latest
|
||||||
|
env:
|
||||||
|
DCS: ${{ matrix.dcs }}
|
||||||
|
ETCDVERSION: 3.3.13
|
||||||
|
PGVERSION: 12.1-1 # for windows and macos
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu]
|
||||||
|
python-version: [2.7, 3.6, 3.9]
|
||||||
|
dcs: [etcd, etcd3, consul, exhibitor, kubernetes, raft]
|
||||||
|
exclude:
|
||||||
|
- dcs: kubernetes
|
||||||
|
python-version: 2.7
|
||||||
|
include:
|
||||||
|
- os: macos
|
||||||
|
python-version: 3.7
|
||||||
|
dcs: raft
|
||||||
|
- os: macos
|
||||||
|
python-version: 3.8
|
||||||
|
dcs: etcd
|
||||||
|
- os: macos
|
||||||
|
python-version: '3.10'
|
||||||
|
dcs: etcd3
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
- name: Add postgresql apt repo
|
||||||
|
run: sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||||
|
if: matrix.os == 'ubuntu'
|
||||||
|
- name: Install dependencies
|
||||||
|
run: python .github/workflows/install_deps.py
|
||||||
|
- name: Run behave tests
|
||||||
|
run: python .github/workflows/run_tests.py
|
||||||
|
- uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: '3.10'
|
||||||
|
- name: Install coveralls
|
||||||
|
run: python -m pip install coveralls
|
||||||
|
- name: Upload Coverage
|
||||||
|
env:
|
||||||
|
COVERALLS_FLAG_NAME: behave-${{ matrix.os }}-${{ matrix.dcs }}-${{ matrix.python-version }}
|
||||||
|
COVERALLS_PARALLEL: 'true'
|
||||||
|
GITHUB_TOKEN: ${{ secrets.github_token }}
|
||||||
|
run: python -m coveralls --service=github
|
||||||
|
|
||||||
|
coveralls-finish:
|
||||||
|
name: Finalize coveralls.io
|
||||||
|
needs: [unit, behave]
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/setup-python@v4
|
||||||
|
- run: python -m pip install coveralls
|
||||||
|
- run: python -m coveralls --service=github --finish
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.github_token }}
|
||||||
-157
@@ -1,157 +0,0 @@
|
|||||||
sudo: true
|
|
||||||
dist: trusty
|
|
||||||
language: python
|
|
||||||
addons:
|
|
||||||
apt:
|
|
||||||
packages:
|
|
||||||
- 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"
|
|
||||||
- BOTO_CONFIG=/doesnotexist
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- python: "3.5"
|
|
||||||
env: TEST_SUITE="python setup.py"
|
|
||||||
- python: "3.6"
|
|
||||||
env: DCS="etcd" 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="kubernetes" TEST_SUITE="behave"
|
|
||||||
branches:
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
- /^v\d+\.\d+(\.\d+)?$/
|
|
||||||
cache:
|
|
||||||
directories:
|
|
||||||
- $HOME/mycache
|
|
||||||
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
|
|
||||||
done
|
|
||||||
install:
|
|
||||||
- |
|
|
||||||
set -e
|
|
||||||
|
|
||||||
if [[ $TEST_SUITE == "behave" ]]; then
|
|
||||||
function get_consul() {
|
|
||||||
CC=~/mycache/consul_${CONSULVERSION}
|
|
||||||
if [[ ! -x $CC ]]; then
|
|
||||||
curl -L https://releases.hashicorp.com/consul/${CONSULVERSION}/consul_${CONSULVERSION}_linux_amd64.zip \
|
|
||||||
| gunzip > $CC
|
|
||||||
[[ ${PIPESTATUS[0]} == 0 ]] || return 1
|
|
||||||
chmod +x $CC
|
|
||||||
fi
|
|
||||||
ln -s $CC consul
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_etcd() {
|
|
||||||
EC=~/mycache/etcd_${ETCDVERSION}
|
|
||||||
if [[ ! -x $EC ]]; then
|
|
||||||
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
|
|
||||||
mv etcd $EC
|
|
||||||
fi
|
|
||||||
ln -s $EC etcd
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_kubernetes() {
|
|
||||||
wget -O localkube "https://storage.googleapis.com/minikube/k8sReleases/v1.7.0/localkube-linux-amd64"
|
|
||||||
chmod +x localkube
|
|
||||||
sudo nohup ./localkube --logtostderr=true --enable-dns=false > localkube.log 2>&1 &
|
|
||||||
|
|
||||||
echo "Waiting for localkube to start..."
|
|
||||||
if ! timeout 120 sh -c "while ! curl -ks http://127.0.0.1:8080/ >/dev/null; do sleep 1; done"; then
|
|
||||||
sudo cat localkube.log
|
|
||||||
echo "localkube did not start"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "Check certificate permissions"
|
|
||||||
sudo chmod 644 /var/lib/localkube/certs/*
|
|
||||||
sudo ls -altr /var/lib/localkube/certs/
|
|
||||||
|
|
||||||
echo "Set up .kube/config"
|
|
||||||
mkdir ~/.kube
|
|
||||||
echo -e "apiVersion: v1\nclusters:\n- cluster:\n certificate-authority: /var/lib/localkube/certs/ca.crt\n server: https://127.0.0.1:8443\n name: local\ncontexts:\n- context:\n cluster: local\n user: myself\n name: local\ncurrent-context: local\nkind: Config\npreferences: {}\nusers:\n- name: myself\n user:\n client-certificate: /var/lib/localkube/certs/apiserver.crt\n client-key: /var/lib/localkube/certs/apiserver.key\n" > ~/.kube/config
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_exhibitor() {
|
|
||||||
ZC=~/mycache/zookeeper-${ZKVERSION}
|
|
||||||
if [[ ! -d $ZC ]]; then
|
|
||||||
curl -L http://www.apache.org/dist/zookeeper/zookeeper-${ZKVERSION}/zookeeper-${ZKVERSION}.tar.gz | tar xz
|
|
||||||
[[ ${PIPESTATUS[0]} == 0 ]] || return 1
|
|
||||||
mv zookeeper-${ZKVERSION}/conf/zoo_sample.cfg zookeeper-${ZKVERSION}/conf/zoo.cfg
|
|
||||||
mv zookeeper-${ZKVERSION} $ZC
|
|
||||||
fi
|
|
||||||
$ZC/bin/zkServer.sh start
|
|
||||||
# following lines are 'emulating' exhibitor REST API
|
|
||||||
while true; do
|
|
||||||
echo -e 'HTTP/1.0 200 OK\nContent-Type: application/json\n\n{"servers":["127.0.0.1"],"port":2181}' \
|
|
||||||
| nc -l 8181 &> /dev/null
|
|
||||||
done&
|
|
||||||
}
|
|
||||||
|
|
||||||
attempt_num=1
|
|
||||||
until get_${DCS}; do
|
|
||||||
[[ $attempt_num -ge 3 ]] && exit 1
|
|
||||||
echo "Attempt $attempt_num failed! Trying again in $attempt_num seconds..."
|
|
||||||
sleep $(( attempt_num++ ))
|
|
||||||
done
|
|
||||||
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
|
|
||||||
fi
|
|
||||||
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 ! 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
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
set +e
|
|
||||||
after_success:
|
|
||||||
# before_cache is executed earlier than after_success, so we need to restore one of virtualenv directories
|
|
||||||
- fpv=$(basename $(readlink $HOME/virtualenv/python3.6)) && mv $HOME/mycache/${fpv} $HOME/virtualenv/${fpv}
|
|
||||||
- coveralls
|
|
||||||
- if [[ $TEST_SUITE != "behave" ]]; then python-codacy-coverage -r coverage.xml; fi
|
|
||||||
- if [[ $DCS == "exhibitor" ]]; then ~/mycache/zookeeper-${ZKVERSION}/bin/zkServer.sh stop; fi
|
|
||||||
- sudo kill $(jobs -p)
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# global owners
|
||||||
|
* @CyberDem0n @hughcapet
|
||||||
+6
-4
@@ -1,6 +1,6 @@
|
|||||||
## This Dockerfile is meant to aid in the building and debugging patroni whilst developing on your local machine
|
## This Dockerfile is meant to aid in the building and debugging patroni whilst developing on your local machine
|
||||||
## It has all the necessary components to play/debug with a single node appliance, running etcd
|
## It has all the necessary components to play/debug with a single node appliance, running etcd
|
||||||
ARG PG_MAJOR=10
|
ARG PG_MAJOR=15
|
||||||
ARG COMPRESS=false
|
ARG COMPRESS=false
|
||||||
ARG PGHOME=/home/postgres
|
ARG PGHOME=/home/postgres
|
||||||
ARG PGDATA=$PGHOME/data
|
ARG PGDATA=$PGHOME/data
|
||||||
@@ -14,7 +14,7 @@ ARG PGDATA
|
|||||||
ARG LC_ALL
|
ARG LC_ALL
|
||||||
ARG LANG
|
ARG LANG
|
||||||
|
|
||||||
ENV ETCDVERSION=2.3.8 CONFDVERSION=0.16.0
|
ENV ETCDVERSION=3.3.13 CONFDVERSION=0.16.0
|
||||||
|
|
||||||
RUN set -ex \
|
RUN set -ex \
|
||||||
&& export DEBIAN_FRONTEND=noninteractive \
|
&& export DEBIAN_FRONTEND=noninteractive \
|
||||||
@@ -25,11 +25,12 @@ RUN set -ex \
|
|||||||
| grep -Ev '^python3-(sphinx|etcd|consul|kazoo|kubernetes)' \
|
| grep -Ev '^python3-(sphinx|etcd|consul|kazoo|kubernetes)' \
|
||||||
| xargs apt-get install -y vim curl less jq locales haproxy sudo \
|
| xargs apt-get install -y vim curl less jq locales haproxy sudo \
|
||||||
python3-etcd python3-kazoo python3-pip busybox \
|
python3-etcd python3-kazoo python3-pip busybox \
|
||||||
|
net-tools iputils-ping --fix-missing \
|
||||||
&& pip3 install dumb-init \
|
&& pip3 install dumb-init \
|
||||||
\
|
\
|
||||||
# Cleanup all locales but en_US.UTF-8
|
# Cleanup all locales but en_US.UTF-8
|
||||||
&& find /usr/share/i18n/charmaps/ -type f ! -name UTF-8.gz -delete \
|
&& 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 \
|
&& echo 'en_US.UTF-8 UTF-8' > /usr/share/i18n/SUPPORTED \
|
||||||
\
|
\
|
||||||
# Make sure we have a en_US.UTF-8 locale available
|
# Make sure we have a en_US.UTF-8 locale available
|
||||||
@@ -116,7 +117,7 @@ RUN if [ "$COMPRESS" = "true" ]; then \
|
|||||||
FROM scratch
|
FROM scratch
|
||||||
COPY --from=builder / /
|
COPY --from=builder / /
|
||||||
|
|
||||||
LABEL maintainer="Alexander Kukushkin <alexander.kukushkin@zalando.de>"
|
LABEL maintainer="Alexander Kukushkin <akukushkin@microsoft.com>"
|
||||||
|
|
||||||
ARG PG_MAJOR
|
ARG PG_MAJOR
|
||||||
ARG COMPRESS
|
ARG COMPRESS
|
||||||
@@ -149,6 +150,7 @@ RUN sed -i 's/env python/&3/' /patroni*.py \
|
|||||||
&& sed -i 's/^ \(replication\|superuser\|rewind\|unix_socket_directories\|\(\( \)\{0,1\}\(username\|password\)\)\):/#&/' postgres?.yml \
|
&& sed -i 's/^ \(replication\|superuser\|rewind\|unix_socket_directories\|\(\( \)\{0,1\}\(username\|password\)\)\):/#&/' postgres?.yml \
|
||||||
&& sed -i 's/^ parameters:/ pg_hba:\n - local all all trust\n - host replication all all md5\n - host all all all md5\n&\n max_connections: 100/' postgres?.yml \
|
&& sed -i 's/^ parameters:/ pg_hba:\n - local all all trust\n - host replication all all md5\n - host all all all md5\n&\n max_connections: 100/' postgres?.yml \
|
||||||
&& if [ "$COMPRESS" = "true" ]; then chmod u+s /usr/bin/sudo; fi \
|
&& if [ "$COMPRESS" = "true" ]; then chmod u+s /usr/bin/sudo; fi \
|
||||||
|
&& chmod +s /bin/ping \
|
||||||
&& chown -R postgres:postgres $PGHOME /run /etc/haproxy
|
&& chown -R postgres:postgres $PGHOME /run /etc/haproxy
|
||||||
|
|
||||||
USER postgres
|
USER postgres
|
||||||
|
|||||||
+2
-3
@@ -1,3 +1,2 @@
|
|||||||
Alexander Kukushkin <alexander.kukushkin@zalando.de>
|
Alexander Kukushkin <akukushkin@microsoft.com>
|
||||||
Feike Steenbergen <feike.steenbergen@zalando.de>
|
Polina Bungina <polina.bungina@zalando.de>
|
||||||
Oleksii Kliukin <[email protected]>
|
|
||||||
|
|||||||
+18
-8
@@ -1,4 +1,4 @@
|
|||||||
|Build Status| |Coverage Status|
|
|Tests Status| |Coverage Status|
|
||||||
|
|
||||||
Patroni: A Template for PostgreSQL HA with ZooKeeper, etcd or Consul
|
Patroni: A Template for PostgreSQL HA with ZooKeeper, etcd or Consul
|
||||||
--------------------------------------------------------------------
|
--------------------------------------------------------------------
|
||||||
@@ -12,6 +12,8 @@ Patroni is a template for you to create your own customized, high-availability s
|
|||||||
|
|
||||||
We call Patroni a "template" because it is far from being a one-size-fits-all or plug-and-play replication system. It will have its own caveats. Use wisely.
|
We call Patroni a "template" because it is far from being a one-size-fits-all or plug-and-play replication system. It will have its own caveats. Use wisely.
|
||||||
|
|
||||||
|
Currently supported PostgreSQL versions: 9.3 to 15.
|
||||||
|
|
||||||
**Note to Kubernetes users**: Patroni can run natively on top of Kubernetes. Take a look at the `Kubernetes <https://github.com/zalando/patroni/blob/master/docs/kubernetes.rst>`__ chapter of the Patroni documentation.
|
**Note to Kubernetes users**: Patroni can run natively on top of Kubernetes. Take a look at the `Kubernetes <https://github.com/zalando/patroni/blob/master/docs/kubernetes.rst>`__ chapter of the Patroni documentation.
|
||||||
|
|
||||||
.. contents::
|
.. contents::
|
||||||
@@ -59,7 +61,7 @@ To install requirements on a Mac, run the following:
|
|||||||
|
|
||||||
brew install postgresql etcd haproxy libyaml python
|
brew install postgresql etcd haproxy libyaml python
|
||||||
|
|
||||||
**Psycopg2**
|
**Psycopg**
|
||||||
|
|
||||||
Starting from `psycopg2-2.8 <http://initd.org/psycopg/articles/2019/04/04/psycopg-28-released/>`__ the binary version of psycopg2 will no longer be installed by default. Installing it from the source code requires C compiler and postgres+python dev packages.
|
Starting from `psycopg2-2.8 <http://initd.org/psycopg/articles/2019/04/04/psycopg-28-released/>`__ the binary version of psycopg2 will no longer be installed by default. Installing it from the source code requires C compiler and postgres+python dev packages.
|
||||||
Since in the python world it is not possible to specify dependency as ``psycopg2 OR psycopg2-binary`` you will have to decide how to install it.
|
Since in the python world it is not possible to specify dependency as ``psycopg2 OR psycopg2-binary`` you will have to decide how to install it.
|
||||||
@@ -86,6 +88,12 @@ There are a few options available:
|
|||||||
|
|
||||||
pip install psycopg2>=2.5.4
|
pip install psycopg2>=2.5.4
|
||||||
|
|
||||||
|
4. Use psycopg 3.0 instead of psycopg2
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
pip install psycopg[binary]
|
||||||
|
|
||||||
**General installation for pip**
|
**General installation for pip**
|
||||||
|
|
||||||
Patroni can be installed with pip:
|
Patroni can be installed with pip:
|
||||||
@@ -96,7 +104,7 @@ Patroni can be installed with pip:
|
|||||||
|
|
||||||
where dependencies can be either empty, or consist of one or more of the following:
|
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
|
`python-etcd` module in order to use Etcd as DCS
|
||||||
consul
|
consul
|
||||||
`python-consul` module in order to use Consul as DCS
|
`python-consul` module in order to use Consul as DCS
|
||||||
@@ -106,6 +114,8 @@ exhibitor
|
|||||||
`kazoo` module in order to use Exhibitor as DCS (same dependencies as for Zookeeper)
|
`kazoo` module in order to use Exhibitor as DCS (same dependencies as for Zookeeper)
|
||||||
kubernetes
|
kubernetes
|
||||||
`kubernetes` module in order to use Kubernetes as DCS in Patroni
|
`kubernetes` module in order to use Kubernetes as DCS in Patroni
|
||||||
|
raft
|
||||||
|
`pysyncobj` module in order to use python Raft implementation as DCS
|
||||||
aws
|
aws
|
||||||
`boto` in order to use AWS callbacks
|
`boto` in order to use AWS callbacks
|
||||||
|
|
||||||
@@ -115,7 +125,7 @@ For example, the command in order to install Patroni together with dependencies
|
|||||||
|
|
||||||
pip install patroni[etcd,aws]
|
pip install patroni[etcd,aws]
|
||||||
|
|
||||||
Note that external tools to call in the replica creation or custom bootstap scripts (i.e. WAL-E) should be installed independently of Patroni.
|
Note that external tools to call in the replica creation or custom bootstrap scripts (i.e. WAL-E) should be installed independently of Patroni.
|
||||||
|
|
||||||
=======================
|
=======================
|
||||||
Running and Configuring
|
Running and Configuring
|
||||||
@@ -124,7 +134,7 @@ Running and Configuring
|
|||||||
To get started, do the following from different terminals:
|
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 postgres0.yml
|
||||||
> ./patroni.py postgres1.yml
|
> ./patroni.py postgres1.yml
|
||||||
|
|
||||||
@@ -167,7 +177,7 @@ Applications Should Not Use Superusers
|
|||||||
|
|
||||||
When connecting from an application, always use a non-superuser. Patroni requires access to the database to function properly. By using a superuser from an application, you can potentially use the entire connection pool, including the connections reserved for superusers, with the ``superuser_reserved_connections`` setting. If Patroni cannot access the Primary because the connection pool is full, behavior will be undesirable.
|
When connecting from an application, always use a non-superuser. Patroni requires access to the database to function properly. By using a superuser from an application, you can potentially use the entire connection pool, including the connections reserved for superusers, with the ``superuser_reserved_connections`` setting. If Patroni cannot access the Primary because the connection pool is full, behavior will be undesirable.
|
||||||
|
|
||||||
.. |Build Status| image:: https://travis-ci.org/zalando/patroni.svg?branch=master
|
.. |Tests Status| image:: https://github.com/zalando/patroni/actions/workflows/tests.yaml/badge.svg
|
||||||
:target: https://travis-ci.org/zalando/patroni
|
:target: https://github.com/zalando/patroni/actions/workflows/tests.yaml?query=branch%3Amaster
|
||||||
.. |Coverage Status| image:: https://coveralls.io/repos/zalando/patroni/badge.svg?branch=master
|
.. |Coverage Status| image:: https://coveralls.io/repos/zalando/patroni/badge.svg?branch=master
|
||||||
:target: https://coveralls.io/r/zalando/patroni?branch=master
|
:target: https://coveralls.io/github/zalando/patroni?branch=master
|
||||||
|
|||||||
+43
-35
@@ -6,57 +6,31 @@ networks:
|
|||||||
demo:
|
demo:
|
||||||
|
|
||||||
services:
|
services:
|
||||||
etcd1:
|
etcd1: &etcd
|
||||||
image: patroni
|
image: patroni
|
||||||
networks: [ demo ]
|
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
|
container_name: demo-etcd1
|
||||||
hostname: etcd1
|
hostname: etcd1
|
||||||
command: etcd -name etcd1 -initial-advertise-peer-urls http://etcd1:2380
|
command: etcd -name etcd1 -initial-advertise-peer-urls http://etcd1:2380
|
||||||
|
|
||||||
etcd2:
|
etcd2:
|
||||||
image: patroni
|
<<: *etcd
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/etcd.env
|
|
||||||
container_name: demo-etcd2
|
container_name: demo-etcd2
|
||||||
hostname: etcd2
|
hostname: etcd2
|
||||||
command: etcd -name etcd2 -initial-advertise-peer-urls http://etcd2:2380
|
command: etcd -name etcd2 -initial-advertise-peer-urls http://etcd2:2380
|
||||||
|
|
||||||
etcd3:
|
etcd3:
|
||||||
image: patroni
|
<<: *etcd
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/etcd.env
|
|
||||||
container_name: demo-etcd3
|
container_name: demo-etcd3
|
||||||
hostname: etcd3
|
hostname: etcd3
|
||||||
command: etcd -name etcd3 -initial-advertise-peer-urls http://etcd3:2380
|
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:
|
haproxy:
|
||||||
image: patroni
|
image: patroni
|
||||||
networks: [ demo ]
|
networks: [ demo ]
|
||||||
@@ -67,3 +41,37 @@ services:
|
|||||||
- "5000:5000"
|
- "5000:5000"
|
||||||
- "5001:5001"
|
- "5001:5001"
|
||||||
command: haproxy
|
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
|
while ! etcdctl cluster-health 2> /dev/null; do
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
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
|
fi
|
||||||
;;
|
;;
|
||||||
etcd)
|
etcd)
|
||||||
@@ -37,7 +37,7 @@ case "$1" in
|
|||||||
esac
|
esac
|
||||||
|
|
||||||
## We start an etcd
|
## 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"
|
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 &
|
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
|
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_USERNAME=admin
|
||||||
PATRONI_RESTAPI_PASSWORD=admin
|
PATRONI_RESTAPI_PASSWORD=admin
|
||||||
PATRONI_SUPERUSER_USERNAME=postgres
|
PATRONI_SUPERUSER_USERNAME=postgres
|
||||||
@@ -9,6 +6,3 @@ PATRONI_REPLICATION_USERNAME=replicator
|
|||||||
PATRONI_REPLICATION_PASSWORD=replicate
|
PATRONI_REPLICATION_PASSWORD=replicate
|
||||||
PATRONI_admin_PASSWORD=admin
|
PATRONI_admin_PASSWORD=admin
|
||||||
PATRONI_admin_OPTIONS=createdb,createrole
|
PATRONI_admin_OPTIONS=createdb,createrole
|
||||||
|
|
||||||
# for etcdctl
|
|
||||||
ETCDCTL_ENDPOINTS=http://etcd1:2379,http://etcd2:2379,http://etcd3:2379
|
|
||||||
|
|||||||
@@ -10,6 +10,39 @@ Chatting
|
|||||||
|
|
||||||
Just want to chat with other Patroni users? Looking for interactive troubleshooting help? Join us on channel #patroni in the `PostgreSQL Slack <https://postgres-slack.herokuapp.com/>`__.
|
Just want to chat with other Patroni users? Looking for interactive troubleshooting help? Join us on channel #patroni in the `PostgreSQL Slack <https://postgres-slack.herokuapp.com/>`__.
|
||||||
|
|
||||||
|
Running tests
|
||||||
|
-------------
|
||||||
|
|
||||||
|
Requirements for running behave tests:
|
||||||
|
|
||||||
|
1. PostgreSQL packages need to be installed.
|
||||||
|
2. PostgreSQL binaries must be available in your `PATH`. You may need to add them to the path with something like `PATH=/usr/lib/postgresql/11/bin:$PATH python -m behave`.
|
||||||
|
3. If you'd like to test with external DCSs (e.g., Etcd, Consul, and Zookeeper) you'll need the packages installed and respective services running and accepting unencrypted/unprotected connections on localhost and default port. In the case of Etcd or Consul, the behave test suite could start them up if binaries are available in the `PATH`.
|
||||||
|
|
||||||
|
Install dependencies:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
# You may want to use Virtualenv or specify pip3.
|
||||||
|
pip install -r requirements.txt
|
||||||
|
pip install -r requirements.dev.txt
|
||||||
|
|
||||||
|
After you have all dependencies installed, you can run the various test suites:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
# You may want to use Virtualenv or specify python3.
|
||||||
|
|
||||||
|
# Run flake8 to check syntax and formatting:
|
||||||
|
python setup.py flake8
|
||||||
|
|
||||||
|
# Run the pytest suite in tests/:
|
||||||
|
python setup.py test
|
||||||
|
|
||||||
|
# Run the behave (https://behave.readthedocs.io/en/latest/) test suite in features/;
|
||||||
|
# modify DCS as desired (raft has no dependencies so is the easiest to start with):
|
||||||
|
DCS=raft python -m behave
|
||||||
|
|
||||||
Reporting issues
|
Reporting issues
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|||||||
+89
-11
@@ -11,7 +11,11 @@ Global/Universal
|
|||||||
- **PATRONI\_NAME**: name of the node where the current instance of Patroni is running. Must be unique for the cluster.
|
- **PATRONI\_NAME**: name of the node where the current instance of Patroni is running. Must be unique for the cluster.
|
||||||
- **PATRONI\_NAMESPACE**: path within the configuration store where Patroni will keep information about the cluster. Default value: "/service"
|
- **PATRONI\_NAMESPACE**: path within the configuration store where Patroni will keep information about the cluster. Default value: "/service"
|
||||||
- **PATRONI\_SCOPE**: cluster name
|
- **PATRONI\_SCOPE**: cluster name
|
||||||
|
|
||||||
|
Log
|
||||||
|
---
|
||||||
- **PATRONI\_LOG\_LEVEL**: sets the general logging level. Default value is **INFO** (see `the docs for Python logging <https://docs.python.org/3.6/library/logging.html#levels>`_)
|
- **PATRONI\_LOG\_LEVEL**: sets the general logging level. Default value is **INFO** (see `the docs for Python logging <https://docs.python.org/3.6/library/logging.html#levels>`_)
|
||||||
|
- **PATRONI\_LOG\_TRACEBACK\_LEVEL**: sets the level where tracebacks will be visible. Default value is **ERROR**. Set it to **DEBUG** if you want to see tracebacks only if you enable **PATRONI\_LOG\_LEVEL=DEBUG**.
|
||||||
- **PATRONI\_LOG\_FORMAT**: sets the log formatting string. Default value is **%(asctime)s %(levelname)s: %(message)s** (see `the LogRecord attributes <https://docs.python.org/3.6/library/logging.html#logrecord-attributes>`_)
|
- **PATRONI\_LOG\_FORMAT**: sets the log formatting string. Default value is **%(asctime)s %(levelname)s: %(message)s** (see `the LogRecord attributes <https://docs.python.org/3.6/library/logging.html#logrecord-attributes>`_)
|
||||||
- **PATRONI\_LOG\_DATEFORMAT**: sets the datetime formatting string. (see the `formatTime() documentation <https://docs.python.org/3.6/library/logging.html#logging.Formatter.formatTime>`_)
|
- **PATRONI\_LOG\_DATEFORMAT**: sets the datetime formatting string. (see the `formatTime() documentation <https://docs.python.org/3.6/library/logging.html#logging.Formatter.formatTime>`_)
|
||||||
- **PATRONI\_LOG\_MAX\_QUEUE\_SIZE**: Patroni is using two-step logging. Log records are written into the in-memory queue and there is a separate thread which pulls them from the queue and writes to stderr or file. The maximum size of the internal queue is limited by default by **1000** records, which is enough to keep logs for the past 1h20m.
|
- **PATRONI\_LOG\_MAX\_QUEUE\_SIZE**: Patroni is using two-step logging. Log records are written into the in-memory queue and there is a separate thread which pulls them from the queue and writes to stderr or file. The maximum size of the internal queue is limited by default by **1000** records, which is enough to keep logs for the past 1h20m.
|
||||||
@@ -19,8 +23,6 @@ Global/Universal
|
|||||||
- **PATRONI\_LOG\_FILE\_NUM**: The number of application logs to retain.
|
- **PATRONI\_LOG\_FILE\_NUM**: The number of application logs to retain.
|
||||||
- **PATRONI\_LOG\_FILE\_SIZE**: Size of patroni.log file (in bytes) that triggers a log rolling.
|
- **PATRONI\_LOG\_FILE\_SIZE**: Size of patroni.log file (in bytes) that triggers a log rolling.
|
||||||
- **PATRONI\_LOG\_LOGGERS**: Redefine logging level per python module. Example ``PATRONI_LOG_LOGGERS="{patroni.postmaster: WARNING, urllib3: DEBUG}"``
|
- **PATRONI\_LOG\_LOGGERS**: Redefine logging level per python module. Example ``PATRONI_LOG_LOGGERS="{patroni.postmaster: WARNING, urllib3: DEBUG}"``
|
||||||
- **PATRONI\_DEBUG\_MODE**: When set to a non-empty value, makes Patroni run in the special debug mode that enables stepping with a debugger through some execution paths within Patroni `
|
|
||||||
Example ``PATRONI_DEBUG_MODE="on"``
|
|
||||||
|
|
||||||
Bootstrap configuration
|
Bootstrap configuration
|
||||||
-----------------------
|
-----------------------
|
||||||
@@ -33,8 +35,8 @@ Example: defining ``PATRONI_admin_PASSWORD=strongpasswd`` and ``PATRONI_admin_OP
|
|||||||
|
|
||||||
Consul
|
Consul
|
||||||
------
|
------
|
||||||
- **PATRONI\_CONSUL\_HOST**: the host:port for the Consul endpoint.
|
- **PATRONI\_CONSUL\_HOST**: the host:port for the Consul local agent.
|
||||||
- **PATRONI\_CONSUL\_URL**: url for the Consul, in format: http(s)://host:port
|
- **PATRONI\_CONSUL\_URL**: url for the Consul local agent, in format: http(s)://host:port
|
||||||
- **PATRONI\_CONSUL\_PORT**: (optional) Consul port
|
- **PATRONI\_CONSUL\_PORT**: (optional) Consul port
|
||||||
- **PATRONI\_CONSUL\_SCHEME**: (optional) **http** or **https**, defaults to **http**
|
- **PATRONI\_CONSUL\_SCHEME**: (optional) **http** or **https**, defaults to **http**
|
||||||
- **PATRONI\_CONSUL\_TOKEN**: (optional) ACL token
|
- **PATRONI\_CONSUL\_TOKEN**: (optional) ACL token
|
||||||
@@ -44,9 +46,10 @@ Consul
|
|||||||
- **PATRONI\_CONSUL\_KEY**: (optional) File with the client key. Can be empty if the key is part of certificate.
|
- **PATRONI\_CONSUL\_KEY**: (optional) File with the client key. Can be empty if the key is part of certificate.
|
||||||
- **PATRONI\_CONSUL\_DC**: (optional) Datacenter to communicate with. By default the datacenter of the host is used.
|
- **PATRONI\_CONSUL\_DC**: (optional) Datacenter to communicate with. By default the datacenter of the host is used.
|
||||||
- **PATRONI\_CONSUL\_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/>`__)
|
- **PATRONI\_CONSUL\_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/>`__)
|
||||||
- **PATRONI\_CONSUL\_CHECKS**: (optional) list of Consul health checks used for the session. If not specified Consul will use "serfHealth" in additional to the TTL based check created by Patroni. Additional checks, in particular the "serfHealth", may cause the leader lock to expire faster than in `ttl` seconds when the leader instance becomes unavailable.
|
- **PATRONI\_CONSUL\_CHECKS**: (optional) list of Consul health checks used for the session. By default an empty list is used.
|
||||||
- **PATRONI\_CONSUL\_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**
|
- **PATRONI\_CONSUL\_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**
|
||||||
- **PATRONI\_CONSUL\_SERVICE\_CHECK\_INTERVAL**: (optional) how often to perform health check against registered url
|
- **PATRONI\_CONSUL\_SERVICE\_CHECK\_INTERVAL**: (optional) how often to perform health check against registered url
|
||||||
|
- **PATRONI\_CONSUL\_SERVICE\_CHECK\_TLS\_SERVER\_NAME**: (optional) overide SNI host when connecting via TLS, see also `consul agent check API reference <https://www.consul.io/api-docs/agent/check#tlsservername>`__.
|
||||||
|
|
||||||
Etcd
|
Etcd
|
||||||
----
|
----
|
||||||
@@ -57,13 +60,37 @@ Etcd
|
|||||||
- **PATRONI\_ETCD\_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 but stick to a fixed list of **hosts**.
|
- **PATRONI\_ETCD\_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 but stick to a fixed list of **hosts**.
|
||||||
- **PATRONI\_ETCD\_PROTOCOL**: http or https, if not specified http is used. If the **url** or **proxy** is specified - will take protocol from them.
|
- **PATRONI\_ETCD\_PROTOCOL**: http or https, if not specified http is used. If the **url** or **proxy** is specified - will take protocol from them.
|
||||||
- **PATRONI\_ETCD\_HOST**: the host:port for the etcd endpoint.
|
- **PATRONI\_ETCD\_HOST**: the host:port for the etcd endpoint.
|
||||||
- **PATRONI\_ETCD\_SRV**: Domain to search the SRV record(s) for cluster autodiscovery.
|
- **PATRONI\_ETCD\_SRV**: Domain to search the SRV record(s) for cluster autodiscovery. Patroni will try to query these SRV service names for specified domain (in that order until first success): ``_etcd-client-ssl``, ``_etcd-client``, ``_etcd-ssl``, ``_etcd``, ``_etcd-server-ssl``, ``_etcd-server``. If SRV records for ``_etcd-server-ssl`` or ``_etcd-server`` are retrieved then ETCD peer protocol is used do query ETCD for available members. Otherwise hosts from SRV records will be used.
|
||||||
|
- **PATRONI\_ETCD\_SRV\_SUFFIX**: Configures a suffix to the SRV name that is queried during discovery. Use this flag to differentiate between multiple etcd clusters under the same domain. Works only with conjunction with **PATRONI\_ETCD\_SRV**. For example, if ``PATRONI_ETCD_SRV_SUFFIX=foo`` and ``PATRONI_ETCD_SRV=example.org`` are set, the following DNS SRV query is made:``_etcd-client-ssl-foo._tcp.example.com`` (and so on for every possible ETCD SRV service name).
|
||||||
- **PATRONI\_ETCD\_USERNAME**: username for etcd authentication.
|
- **PATRONI\_ETCD\_USERNAME**: username for etcd authentication.
|
||||||
- **PATRONI\_ETCD\_PASSWORD**: password for etcd authentication.
|
- **PATRONI\_ETCD\_PASSWORD**: password for etcd authentication.
|
||||||
- **PATRONI\_ETCD\_CACERT**: The ca certificate. If present it will enable validation.
|
- **PATRONI\_ETCD\_CACERT**: The ca certificate. If present it will enable validation.
|
||||||
- **PATRONI\_ETCD\_CERT**: File with the client certificate.
|
- **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.
|
- **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\_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``.
|
||||||
|
- **PATRONI\_ZOOKEEPER\_SET\_ACLS**: (optional) If set, configure Kazoo to apply a default ACL to each ZNode that it creates. ACLs will assume 'x509' schema and should be specified as a dictionary with the principal as the key and one or more permissions as a list in the value. Permissions may be one of ``CREATE``, ``READ``, ``WRITE``, ``DELETE`` or ``ADMIN``. For example, ``set_acls: {CN=principal1: [CREATE, READ], CN=principal2: [ALL]}``.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
It is required to install ``kazoo>=2.6.0`` to support SSL.
|
||||||
|
|
||||||
|
|
||||||
Exhibitor
|
Exhibitor
|
||||||
---------
|
---------
|
||||||
- **PATRONI\_EXHIBITOR\_HOSTS**: initial list of Exhibitor (ZooKeeper) nodes in format: 'host1,host2,etc...'. This list updates automatically whenever the Exhibitor (ZooKeeper) cluster topology changes.
|
- **PATRONI\_EXHIBITOR\_HOSTS**: initial list of Exhibitor (ZooKeeper) nodes in format: 'host1,host2,etc...'. This list updates automatically whenever the Exhibitor (ZooKeeper) cluster topology changes.
|
||||||
@@ -73,28 +100,67 @@ Exhibitor
|
|||||||
|
|
||||||
Kubernetes
|
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\_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\_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`.
|
- **PATRONI\_KUBERNETES\_SCOPE\_LABEL**: (optional) name of the label containing cluster name. Default value is `cluster-name`.
|
||||||
- **PATRONI\_KUBERNETES\_ROLE\_LABEL**: (optional) name of the label containing Postgres role (`master` or `replica`). Patroni will set this label on the pod it is running in. Default value is `role`.
|
- **PATRONI\_KUBERNETES\_ROLE\_LABEL**: (optional) name of the label containing Postgres role (`master` or `replica`). Patroni will set this label on the pod it is running in. Default value is `role`.
|
||||||
- **PATRONI\_KUBERNETES\_USE\_ENDPOINTS**: (optional) if set to true, Patroni will use Endpoints instead of ConfigMaps to run leader elections and keep cluster state.
|
- **PATRONI\_KUBERNETES\_USE\_ENDPOINTS**: (optional) if set to true, Patroni will use Endpoints instead of ConfigMaps to run leader elections and keep cluster state.
|
||||||
- **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\_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.
|
- **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.
|
||||||
|
- **PATRONI\_KUBERNETES\_CACERT**: (optional) Specifies the file with the CA_BUNDLE file with certificates of trusted CAs to use while verifying Kubernetes API SSL certs. If not provided, patroni will use the value provided by the ServiceAccount secret.
|
||||||
|
|
||||||
|
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
|
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.
|
- **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.
|
||||||
- **PATRONI\_POSTGRESQL\_CONNECT\_ADDRESS**: IP address + port through which Postgres is accessible from other nodes and applications.
|
- **PATRONI\_POSTGRESQL\_CONNECT\_ADDRESS**: IP address + port through which Postgres is accessible from other nodes and applications.
|
||||||
|
- **PATRONI\_POSTGRESQL\_PROXY\_ADDRESS**: IP address + port through which a connection pool (e.g. pgbouncer) running next to Postgres is accessible. The value is written to the member key in DCS as ``proxy_url`` and could be used/useful for service discovery.
|
||||||
- **PATRONI\_POSTGRESQL\_DATA\_DIR**: The location of the Postgres data directory, either existing or to be initialized by Patroni.
|
- **PATRONI\_POSTGRESQL\_DATA\_DIR**: The location of the Postgres data directory, either existing or to be initialized by Patroni.
|
||||||
- **PATRONI\_POSTGRESQL\_CONFIG\_DIR**: The location of the Postgres configuration directory, defaults to the data directory. Must be writable by Patroni.
|
- **PATRONI\_POSTGRESQL\_CONFIG\_DIR**: The location of the Postgres configuration directory, defaults to the data directory. Must be writable by Patroni.
|
||||||
- **PATRONI\_POSTGRESQL\_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.
|
- **PATRONI\_POSTGRESQL\_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.
|
||||||
- **PATRONI\_POSTGRESQL\_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 and under some other circumstances. The location must be writable by Patroni.
|
- **PATRONI\_POSTGRESQL\_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 and under some other circumstances. The location must be writable by Patroni.
|
||||||
- **PATRONI\_REPLICATION\_USERNAME**: replication username; the user will be created during initialization. Replicas will use this user to access master via streaming replication
|
- **PATRONI\_REPLICATION\_USERNAME**: replication username; the user will be created during initialization. Replicas will use this user to access the replication source via streaming replication
|
||||||
- **PATRONI\_REPLICATION\_PASSWORD**: replication password; the user will be created during initialization.
|
- **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\_SSLCRLDIR**: (optional) maps to the `sslcrldir <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRLDIR>`__ connection parameter, which specifies the location of a directory with files 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\_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\_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\_SSLCRLDIR**: (optional) maps to the `sslcrldir <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRLDIR>`__ connection parameter, which specifies the location of a directory with files 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\_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\_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\_SSLCRLDIR**: (optional) maps to the `sslcrldir <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRLDIR>`__ connection parameter, which specifies the location of a directory with files 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
|
REST API
|
||||||
--------
|
--------
|
||||||
@@ -104,7 +170,19 @@ REST API
|
|||||||
- **PATRONI\_RESTAPI\_PASSWORD**: Basic-auth password to protect unsafe REST API endpoints.
|
- **PATRONI\_RESTAPI\_PASSWORD**: Basic-auth password to protect unsafe REST API endpoints.
|
||||||
- **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\_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\_KEYFILE**: Specifies the file with the secret key in the PEM format.
|
||||||
|
- **PATRONI\_RESTAPI\_KEYFILE\_PASSWORD**: Specifies a password for decrypting the keyfile.
|
||||||
|
- **PATRONI\_RESTAPI\_CAFILE**: Specifies the file with the CA_BUNDLE with certificates of trusted CAs to use while verifying client certs.
|
||||||
|
- **PATRONI\_RESTAPI\_CIPHERS**: (optional) Specifies the permitted cipher suites (e.g. "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:!SSLv1:!SSLv2:!SSLv3:!TLSv1:!TLSv1.1")
|
||||||
|
- **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\_ALLOWLIST**: (optional): Specifies the set of hosts that are allowed to call unsafe REST API endpoints. The single element could be a host name, an IP address or a network address using CIDR notation. By default ``allow all`` is used. In case if ``allowlist`` or ``allowlist_include_members`` are set, anything that is not included is rejected.
|
||||||
|
- **PATRONI\_RESTAPI\_ALLOWLIST\_INCLUDE\_MEMBERS**: (optional): If set to ``true`` it allows accessing unsafe REST API endpoints from other cluster members registered in DCS (IP address or hostname is taken from the members ``api_url``). Be careful, it might happen that OS will use a different IP for outgoing connections.
|
||||||
|
- **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``.
|
||||||
|
|
||||||
ZooKeeper
|
CTL
|
||||||
---------
|
---
|
||||||
- **PATRONI\_ZOOKEEPER\_HOSTS**: comma separated list of ZooKeeper cluster members: "'host1:port1','host2:port2','etc...'". It is important to quote every single entity!
|
- **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.
|
||||||
|
- **PATRONI\_CTL\_KEYFILE**: Specifies the file with the client secret key in the PEM format. If not provided patronictl will use the value provided for REST API "keyfile" parameter.
|
||||||
|
|||||||
+36
-4
@@ -35,7 +35,7 @@ To install requirements on a Mac, run the following:
|
|||||||
|
|
||||||
.. _psycopg2_install_options:
|
.. _psycopg2_install_options:
|
||||||
|
|
||||||
**Psycopg2**
|
**Psycopg**
|
||||||
|
|
||||||
Starting from `psycopg2-2.8 <http://initd.org/psycopg/articles/2019/04/04/psycopg-28-released/>`__ the binary version of psycopg2 will no longer be installed by default. Installing it from the source code requires C compiler and postgres+python dev packages.
|
Starting from `psycopg2-2.8 <http://initd.org/psycopg/articles/2019/04/04/psycopg-28-released/>`__ the binary version of psycopg2 will no longer be installed by default. Installing it from the source code requires C compiler and postgres+python dev packages.
|
||||||
Since in the python world it is not possible to specify dependency as ``psycopg2 OR psycopg2-binary`` you will have to decide how to install it.
|
Since in the python world it is not possible to specify dependency as ``psycopg2 OR psycopg2-binary`` you will have to decide how to install it.
|
||||||
@@ -62,6 +62,12 @@ There are a few options available:
|
|||||||
|
|
||||||
pip install psycopg2>=2.5.4
|
pip install psycopg2>=2.5.4
|
||||||
|
|
||||||
|
4. Use psycopg 3.0 instead of psycopg2
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
pip install psycopg[binary]>=3.0.0
|
||||||
|
|
||||||
**General installation for pip**
|
**General installation for pip**
|
||||||
|
|
||||||
Patroni can be installed with pip:
|
Patroni can be installed with pip:
|
||||||
@@ -72,8 +78,8 @@ Patroni can be installed with pip:
|
|||||||
|
|
||||||
where dependencies can be either empty, or consist of one or more of the following:
|
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
|
`python-etcd` module in order to use Etcd as Distributed Configuration Store (DCS)
|
||||||
consul
|
consul
|
||||||
`python-consul` module in order to use Consul as DCS
|
`python-consul` module in order to use Consul as DCS
|
||||||
zookeeper
|
zookeeper
|
||||||
@@ -82,6 +88,8 @@ exhibitor
|
|||||||
`kazoo` module in order to use Exhibitor as DCS (same dependencies as for Zookeeper)
|
`kazoo` module in order to use Exhibitor as DCS (same dependencies as for Zookeeper)
|
||||||
kubernetes
|
kubernetes
|
||||||
`kubernetes` module in order to use Kubernetes as DCS in Patroni
|
`kubernetes` module in order to use Kubernetes as DCS in Patroni
|
||||||
|
raft
|
||||||
|
`pysyncobj` module in order to use python Raft implementation as DCS
|
||||||
aws
|
aws
|
||||||
`boto` in order to use AWS callbacks
|
`boto` in order to use AWS callbacks
|
||||||
|
|
||||||
@@ -97,6 +105,13 @@ independently of Patroni.
|
|||||||
|
|
||||||
.. _running_configuring:
|
.. _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 primary and one standby is
|
||||||
|
perfectly fine. You can add more standby nodes later.
|
||||||
|
|
||||||
Running and Configuring
|
Running and Configuring
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
@@ -107,7 +122,7 @@ obtain those files from the git repository and replace `./patroni.py` below with
|
|||||||
To get started, do the following from different terminals:
|
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 postgres0.yml
|
||||||
> ./patroni.py postgres1.yml
|
> ./patroni.py postgres1.yml
|
||||||
|
|
||||||
@@ -154,3 +169,20 @@ When connecting from an application, always use a non-superuser. Patroni require
|
|||||||
:target: https://travis-ci.org/zalando/patroni
|
:target: https://travis-ci.org/zalando/patroni
|
||||||
.. |Coverage Status| image:: https://coveralls.io/repos/zalando/patroni/badge.svg?branch=master
|
.. |Coverage Status| image:: https://coveralls.io/repos/zalando/patroni/badge.svg?branch=master
|
||||||
:target: https://coveralls.io/r/zalando/patroni?branch=master
|
:target: https://coveralls.io/r/zalando/patroni?branch=master
|
||||||
|
|
||||||
|
Testing Your HA Solution
|
||||||
|
--------------------------------------
|
||||||
|
Testing an HA solution is a time consuming process, with many variables. This is particularly true considering a cross-platform application. You need a trained system administrator or a consultant to do this work. It is not something we can cover in depth in the documentation.
|
||||||
|
|
||||||
|
That said, here are some pieces of your infrastructure you should be sure to test:
|
||||||
|
|
||||||
|
* Network (the network in front of your system as well as the NICs [physical or virtual] themselves)
|
||||||
|
* Disk IO
|
||||||
|
* file limits (nofile in Linux)
|
||||||
|
* RAM. Even if you have oomkiller turned off as suggested, the unavailability of RAM could cause issues.
|
||||||
|
* CPU
|
||||||
|
* Virtualization Contention (overcommitting the hypervisor)
|
||||||
|
* Any cgroup limitation (likely to be related to the above)
|
||||||
|
* ``kill -9`` of any postgres process (except postmaster!). This is a decent simulation of a segfault.
|
||||||
|
|
||||||
|
One thing that you should not do is run ``kill -9`` on a postmaster process. This is because doing so does not mimic any real life scenario. If you are concerned your infrastructure is insecure and an attacker could run ``kill -9``, no amount of HA process is going to fix that. The attacker will simply kill the process again, or cause chaos in another way.
|
||||||
|
|||||||
+270
-80
@@ -4,6 +4,68 @@
|
|||||||
YAML Configuration Settings
|
YAML Configuration Settings
|
||||||
===========================
|
===========================
|
||||||
|
|
||||||
|
.. _dynamic_configuration_settings:
|
||||||
|
|
||||||
|
Dynamic configuration settings
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
Dynamic configuration is stored in the DCS (Distributed Configuration Store) and applied on all cluster nodes. Some parameters, like **loop_wait**, **ttl**, **postgresql.parameters.max_connections**, **postgresql.parameters.max_worker_processes** and so on could be set only in the dynamic configuration. Some other parameters like **postgresql.listen**, **postgresql.data_dir** could be set only locally, i.e. in the Patroni config file or via :ref:`configuration <environment>` variable. In most cases the local configuration will override the dynamic configuration. In order to change the dynamic configuration you can use either ``patronictl edit-config`` tool or Patroni :ref:`REST API <rest_api>`.
|
||||||
|
|
||||||
|
- **loop\_wait**: the number of seconds the loop will sleep. Default value: 10
|
||||||
|
- **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.
|
||||||
|
- **maximum\_lag\_on\_syncnode**: the maximum bytes a synchronous follower may lag before it is considered as an unhealthy candidate and swapped by healthy asynchronous follower. Patroni utilize the max replica lsn if there is more than one follower, otherwise it will use leader's current wal lsn. Default is -1, Patroni will not take action to swap synchronous unhealthy follower when the value is set to 0 or below. Please set the value high enough so Patroni won't swap synchrounous follower fequently during high transaction volume.
|
||||||
|
- **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 primary 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 primary 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 primary. 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+.
|
||||||
|
- **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.
|
||||||
|
- **host**: an address of remote node
|
||||||
|
- **port**: a port of remote node
|
||||||
|
- **primary\_slot\_name**: which slot on the remote node to use for replication. This parameter is optional, the default value is derived from the instance name (see function `slot_name_from_member_name`).
|
||||||
|
- **create\_replica\_methods**: an ordered list of methods that can be used to bootstrap standby leader from the remote primary, can be different from the list defined in :ref:`postgresql_settings`
|
||||||
|
- **restore\_command**: command to restore WAL records from the remote primary to nodes in a standby cluster, can be different from the list defined in :ref:`postgresql_settings`
|
||||||
|
- **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. The logical slots are copied from the primary to a standby with restart, and after that their position advanced every **loop_wait** seconds (if necessary). Copying logical slot files performed via ``libpq`` connection and using either rewind or superuser credentials (see **postgresql.authentication** section). There is always a chance that the logical slot position on the replica is a bit behind the former primary, therefore application should be prepared that some messages could be received the second time after the failover. The easiest way of doing so - tracking ``confirmed_flush_lsn``. Enabling permanent logical replication slots requires **postgresql.use_slots** to be set and will also automatically enable the ``hot_standby_feedback``. Since the failover of logical replication slots is unsafe on PostgreSQL 9.6 and older and PostgreSQL version 10 is missing some important functions, the feature only works with PostgreSQL 11+.
|
||||||
|
- **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.
|
||||||
|
- **ignore_slots**: list of sets of replication slot properties for which Patroni should ignore matching slots. This configuration/feature/etc. is useful when some replication slots are managed outside of Patroni. Any subset of matching properties will cause a slot to be ignored.
|
||||||
|
- **name**: the name of the replication slot.
|
||||||
|
- **type**: slot type. Can be ``physical`` or ``logical``. If the slot is logical, you may additionally define ``database`` and/or ``plugin``.
|
||||||
|
- **database**: the database name (when matching a ``logical`` slot).
|
||||||
|
- **plugin**: the logical decoding plugin (when matching a ``logical`` slot).
|
||||||
|
|
||||||
|
Note: **slots** is a hashmap while **ignore_slots** is an array. For example:
|
||||||
|
|
||||||
|
.. code:: YAML
|
||||||
|
|
||||||
|
slots:
|
||||||
|
permanent_logical_slot_name:
|
||||||
|
type: logical
|
||||||
|
database: my_db
|
||||||
|
plugin: test_decoding
|
||||||
|
permanent_physical_slot_name:
|
||||||
|
type: physical
|
||||||
|
...
|
||||||
|
ignore_slots:
|
||||||
|
- name: ignored_logical_slot_name
|
||||||
|
type: logical
|
||||||
|
database: my_db
|
||||||
|
plugin: test_decoding
|
||||||
|
- name: ignored_physical_slot_name
|
||||||
|
type: physical
|
||||||
|
...
|
||||||
|
|
||||||
Global/Universal
|
Global/Universal
|
||||||
----------------
|
----------------
|
||||||
- **name**: the name of the host. Must be unique for the cluster.
|
- **name**: the name of the host. Must be unique for the cluster.
|
||||||
@@ -13,6 +75,7 @@ Global/Universal
|
|||||||
Log
|
Log
|
||||||
---
|
---
|
||||||
- **level**: sets the general logging level. Default value is **INFO** (see `the docs for Python logging <https://docs.python.org/3.6/library/logging.html#levels>`_)
|
- **level**: sets the general logging level. Default value is **INFO** (see `the docs for Python logging <https://docs.python.org/3.6/library/logging.html#levels>`_)
|
||||||
|
- **traceback\_level**: sets the level where tracebacks will be visible. Default value is **ERROR**. Set it to **DEBUG** if you want to see tracebacks only if you enable **log.level=DEBUG**.
|
||||||
- **format**: sets the log formatting string. Default value is **%(asctime)s %(levelname)s: %(message)s** (see `the LogRecord attributes <https://docs.python.org/3.6/library/logging.html#logrecord-attributes>`_)
|
- **format**: sets the log formatting string. Default value is **%(asctime)s %(levelname)s: %(message)s** (see `the LogRecord attributes <https://docs.python.org/3.6/library/logging.html#logrecord-attributes>`_)
|
||||||
- **dateformat**: sets the datetime formatting string. (see the `formatTime() documentation <https://docs.python.org/3.6/library/logging.html#logging.Formatter.formatTime>`_)
|
- **dateformat**: sets the datetime formatting string. (see the `formatTime() documentation <https://docs.python.org/3.6/library/logging.html#logging.Formatter.formatTime>`_)
|
||||||
- **max\_queue\_size**: Patroni is using two-step logging. Log records are written into the in-memory queue and there is a separate thread which pulls them from the queue and writes to stderr or file. The maximum size of the internal queue is limited by default by **1000** records, which is enough to keep logs for the past 1h20m.
|
- **max\_queue\_size**: Patroni is using two-step logging. Log records are written into the in-memory queue and there is a separate thread which pulls them from the queue and writes to stderr or file. The maximum size of the internal queue is limited by default by **1000** records, which is enough to keep logs for the past 1h20m.
|
||||||
@@ -27,50 +90,26 @@ Log
|
|||||||
|
|
||||||
Bootstrap configuration
|
Bootstrap configuration
|
||||||
-----------------------
|
-----------------------
|
||||||
- **dcs**: This section will be written into `/<namespace>/<scope>/config` of a given configuration store after initializing of new cluster. This is the global configuration for the cluster. If you want to change some parameters for all cluster nodes - just do it in DCS (or via Patroni API) and all nodes will apply this configuration.
|
- **bootstrap**:
|
||||||
- **loop\_wait**: the number of seconds the loop will sleep. Default value: 10
|
- **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>`.
|
||||||
- **ttl**: the TTL to acquire the leader lock. Think of it as the length of time before initiation of the automatic failover process. Default value: 30
|
- **method**: custom script to use for bootstrapping this cluster.
|
||||||
- **retry\_timeout**: timeout for DCS and PostgreSQL operation retries. 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.
|
|
||||||
- **master\_start\_timeout**: the amount of time a master is allowed to recover from failures before failover is triggered. 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. Best 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.
|
|
||||||
- **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
|
|
||||||
- **use\_slots**: whether or not to use replication_slots. Must be False for PostgreSQL 9.3. You should comment out max_replication_slots before it becomes ineligible for leader status.
|
|
||||||
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
|
||||||
- **parameters**: list of configuration settings for Postgres. Many of these are required for replication to work.
|
|
||||||
- **standby\_cluster**: if this section is defined, we want to bootstrap a standby cluster.
|
|
||||||
- **host**: an address of remote master
|
|
||||||
- **port**: a port of remote master
|
|
||||||
- **primary\_slot\_name**: which slot on the remote master to use for replication. This parameter is optional, the default value is derived from the instance name (see function `slot_name_from_member_name`).
|
|
||||||
- **create\_replica\_methods**: an ordered list of methods that can be used to bootstrap standby leader from the remote master, can be different from the list defined in :ref:`postgresql_settings`
|
|
||||||
- **restore\_command**: command to restore WAL records from the remote master to standby leader, can be different from the list defined in :ref:`postgresql_settings`
|
|
||||||
- **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.
|
|
||||||
- **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.
|
|
||||||
- **method**: custom script to use for bootstrapping this cluster.
|
|
||||||
See :ref:`custom bootstrap methods documentation <custom_bootstrap>` for details.
|
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``
|
When ``initdb`` is specified revert to the default ``initdb`` command. ``initdb`` is also triggered when no ``method``
|
||||||
parameter is present in the configuration file.
|
parameter is present in the configuration file.
|
||||||
- **initdb**: List options to be passed on to initdb.
|
- **initdb**: List options to be passed on to initdb.
|
||||||
- **- data-checksums**: Must be enabled when pg_rewind is needed on 9.3.
|
- **- data-checksums**: Must be enabled when pg_rewind is needed on 9.3.
|
||||||
- **- encoding: UTF8**: default encoding for new databases.
|
- **- encoding: UTF8**: default encoding for new databases.
|
||||||
- **- locale: UTF8**: default locale for new databases.
|
- **- locale: UTF8**: default locale for new databases.
|
||||||
- **pg\_hba**: list of lines that you should add to pg\_hba.conf.
|
- **pg\_hba**: list of lines that you should add to pg\_hba.conf.
|
||||||
- **- host all all 0.0.0.0/0 md5**.
|
- **- 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.
|
- **- 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
|
- **users**: Some additional users which need to be created after initializing new cluster
|
||||||
- **admin**: the name of user
|
- **admin**: the name of user
|
||||||
- **password: zalando**:
|
- **password: zalando**:
|
||||||
- **options**: list of options for CREATE USER statement
|
- **options**: list of options for CREATE USER statement
|
||||||
- **- createrole**
|
- **- createrole**
|
||||||
- **- createdb**
|
- **- 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.
|
- **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:
|
.. _consul_settings:
|
||||||
|
|
||||||
@@ -78,20 +117,36 @@ Consul
|
|||||||
------
|
------
|
||||||
Most of the parameters are optional, but you have to specify one of the **host** or **url**
|
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
|
- **host**: the host:port for the Consul local agent.
|
||||||
- **url**: url for the Consul endpoint
|
- **url**: url for the Consul local agent, in format: http(s)://host:port.
|
||||||
- **port**: (optional) Consul port
|
- **port**: (optional) Consul port.
|
||||||
- **scheme**: (optional) **http** or **https**, defaults to **http**
|
- **scheme**: (optional) **http** or **https**, defaults to **http**.
|
||||||
- **token**: (optional) ACL token
|
- **token**: (optional) ACL token.
|
||||||
- **verify**: (optional) whether to verify the SSL certificate for HTTPS requests
|
- **verify**: (optional) whether to verify the SSL certificate for HTTPS requests.
|
||||||
- **cacert**: (optional) The ca certificate. If present it will enable validation.
|
- **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**.
|
- **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.
|
- **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/>`__)
|
- **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. If not specified Consul will use "serfHealth" in additional to the TTL based check created by Patroni. Additional checks, in particular the "serfHealth", may cause the leader lock to expire faster than in `ttl` seconds when the leader instance becomes unavailable
|
- **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**
|
- **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
|
- **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. Defaults to '5s'.
|
||||||
|
- **service\_check\_tls\_server\_name**: (optional) overide SNI host when connecting via TLS, see also `consul agent check API reference <https://www.consul.io/api-docs/agent/check#tlsservername>`__.
|
||||||
|
|
||||||
|
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
|
Etcd
|
||||||
----
|
----
|
||||||
@@ -100,9 +155,10 @@ Most of the parameters are optional, but you have to specify one of the **host**
|
|||||||
- **host**: the host:port for the etcd endpoint.
|
- **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.
|
- **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.
|
- **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
|
- **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**
|
- **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.
|
- **srv**: Domain to search the SRV record(s) for cluster autodiscovery. Patroni will try to query these SRV service names for specified domain (in that order until first success): ``_etcd-client-ssl``, ``_etcd-client``, ``_etcd-ssl``, ``_etcd``, ``_etcd-server-ssl``, ``_etcd-server``. If SRV records for ``_etcd-server-ssl`` or ``_etcd-server`` are retrieved then ETCD peer protocol is used do query ETCD for available members. Otherwise hosts from SRV records will be used.
|
||||||
|
- **srv\_suffix**: Configures a suffix to the SRV name that is queried during discovery. Use this flag to differentiate between multiple etcd clusters under the same domain. Works only with conjunction with **srv**. For example, if ``srv_suffix: foo`` and ``srv: example.org`` are set, the following DNS SRV query is made:``_etcd-client-ssl-foo._tcp.example.com`` (and so on for every possible ETCD SRV service name).
|
||||||
- **protocol**: (optional) http or https, if not specified http is used. If the **url** or **proxy** is specified - will take protocol from them.
|
- **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.
|
- **username**: (optional) username for etcd authentication.
|
||||||
- **password**: (optional) password for etcd authentication.
|
- **password**: (optional) password for etcd authentication.
|
||||||
@@ -110,98 +166,232 @@ Most of the parameters are optional, but you have to specify one of the **host**
|
|||||||
- **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**.
|
- **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...'].
|
||||||
|
- **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``.
|
||||||
|
- **set_acls**: (optional) If set, configure Kazoo to apply a default ACL to each ZNode that it creates. ACLs will assume 'x509' schema and should be specified as a dictionary with the principal as the key and one or more permissions as a list in the value. Permissions may be one of ``CREATE``, ``READ``, ``WRITE``, ``DELETE`` or ``ADMIN``. For example, ``set_acls: {CN=principal1: [CREATE, READ], CN=principal2: [ALL]}``.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
It is required to install ``kazoo>=2.6.0`` to support SSL.
|
||||||
|
|
||||||
|
|
||||||
Exhibitor
|
Exhibitor
|
||||||
---------
|
---------
|
||||||
- **hosts**: initial list of Exhibitor (ZooKeeper) nodes in format: 'host1,host2,etc...'. This list updates automatically whenever the Exhibitor (ZooKeeper) cluster topology changes.
|
- **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.
|
- **port**: Exhibitor port.
|
||||||
|
|
||||||
.. _kubernetes_settings:
|
.. _kubernetes_settings:
|
||||||
|
|
||||||
Kubernetes
|
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`.
|
- **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.
|
- **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`.
|
- **scope\_label**: (optional) name of the label containing cluster name. Default value is `cluster-name`.
|
||||||
- **role\_label**: (optional) name of the label containing role (master or replica). Patroni will set this label on the pod it runs in. Default value is ``role``.
|
- **role\_label**: (optional) name of the label containing role (master or replica). Patroni will set this label on the pod it runs in. Default value is ``role``.
|
||||||
- **use\_endpoints**: (optional) if set to true, Patroni will use Endpoints instead of ConfigMaps to run leader elections and keep cluster state.
|
- **use\_endpoints**: (optional) if set to true, Patroni will use Endpoints instead of ConfigMaps to run leader elections and keep cluster state.
|
||||||
- **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.
|
- **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.
|
- **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.
|
||||||
|
- **cacert**: (optional) Specifies the file with the CA_BUNDLE file with certificates of trusted CAs to use while verifying Kubernetes API SSL certs. If not provided, patroni will use the value provided by the ServiceAccount secret.
|
||||||
|
|
||||||
|
|
||||||
|
.. _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 dependency.
|
||||||
|
|
||||||
|
- 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 a setup, one can temporarily lose one node without affecting the primary.
|
||||||
|
|
||||||
|
|
||||||
.. _postgresql_settings:
|
.. _postgresql_settings:
|
||||||
|
|
||||||
PostgreSQL
|
PostgreSQL
|
||||||
----------
|
----------
|
||||||
- **authentication**:
|
- **postgresql**:
|
||||||
|
- **authentication**:
|
||||||
- **superuser**:
|
- **superuser**:
|
||||||
- **username**: name for the superuser, set during initialization (initdb) and later used by Patroni to connect to the postgres.
|
- **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).
|
- **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.
|
||||||
|
- **sslcrldir**: (optional) maps to the `sslcrldir <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRLDIR>`__ connection parameter, which specifies the location of a directory with files 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**:
|
- **replication**:
|
||||||
- **username**: replication username; the user will be created during initialization. Replicas will use this user to access master via streaming replication
|
- **username**: replication username; the user will be created during initialization. Replicas will use this user to access the replication source via streaming replication
|
||||||
- **password**: replication password; the user will be created during initialization.
|
- **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.
|
||||||
|
- **sslcrldir**: (optional) maps to the `sslcrldir <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRLDIR>`__ connection parameter, which specifies the location of a directory with files 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**:
|
- **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.
|
- **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.
|
- **password**: password for the user for ``pg_rewind``; the user will be created during initialization.
|
||||||
- **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.)
|
- **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.
|
||||||
|
- **sslcrldir**: (optional) maps to the `sslcrldir <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRLDIR>`__ connection parameter, which specifies the location of a directory with files 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\_reload**: run this script when configuration reload is triggered.
|
||||||
- **on\_restart**: run this script when the postgres restarts (without changing role).
|
- **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\_role\_change**: run this script when the postgres is being promoted or demoted.
|
||||||
- **on\_start**: run this script when the postgres starts.
|
- **on\_start**: run this script when the postgres starts.
|
||||||
- **on\_stop**: run this script when the postgres stops.
|
- **on\_stop**: run this script when the postgres stops.
|
||||||
- **connect\_address**: IP address + port through which Postgres is accessible from other nodes and applications.
|
- **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.
|
- **proxy\_address**: IP address + port through which a connection pool (e.g. pgbouncer) running next to Postgres is accessible. The value is written to the member key in DCS as ``proxy_url`` and could be used/useful for service discovery.
|
||||||
|
- **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
|
"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.
|
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.
|
- **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.
|
- **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.
|
- **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.
|
- **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.
|
- **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.
|
- **use\_unix\_socket\_repl**: specifies that Patroni should prefer to use unix sockets for replication user cluster connection. 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.
|
||||||
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
- **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.
|
||||||
- **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.
|
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
||||||
- **parameters**: list of configuration settings for Postgres. Many of these are required for replication to work.
|
- **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.
|
||||||
- **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``.
|
- **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 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.
|
- **- 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``.
|
- **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 systemname1 pguser1**.
|
||||||
- **- mapname1 systemname2 pguser2**.
|
- **- mapname1 systemname2 pguser2**.
|
||||||
- **pg\_ctl\_timeout**: How long should pg_ctl wait when doing ``start``, ``stop`` or ``restart``. Default value is 60 seconds.
|
- **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.
|
- **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\_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**.
|
- **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 primary can not start streaming from the new primary. This option is useful when ``pg_rewind`` can not be used. While performing timelines divergence check on PostgreSQL v10 and older Patroni will try to connect with replication credential to the "postgres" database. Hence, such access should be allowed in the pg_hba.conf. 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".
|
- **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
|
REST API
|
||||||
--------
|
--------
|
||||||
- **connect\_address**: IP address (or hostname) and port, to access the Patroni's 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 addres (ie: "localhost" or "127.0.0.1"). It can serve as a 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 leader 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**: (optional)
|
||||||
- **authentication**:
|
|
||||||
- **username**: Basic-auth username to protect unsafe REST API endpoints.
|
- **username**: Basic-auth username to protect unsafe REST API endpoints.
|
||||||
- **password**: Basic-auth password 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.
|
||||||
|
- **keyfile\_password**: (optional): Specifies a password for decrypting the keyfile.
|
||||||
|
- **cafile**: (optional): Specifies the file with the CA_BUNDLE with certificates of trusted CAs to use while verifying client certs.
|
||||||
|
- **ciphers**: (optional): Specifies the permitted cipher suites (e.g. "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:!SSLv1:!SSLv2:!SSLv3:!TLSv1:!TLSv1.1")
|
||||||
|
- **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.
|
||||||
|
- **allowlist**: (optional): Specifies the set of hosts that are allowed to call unsafe REST API endpoints. The single element could be a host name, an IP address or a network address using CIDR notation. By default ``allow all`` is used. In case if ``allowlist`` or ``allowlist_include_members`` are set, anything that is not included is rejected.
|
||||||
|
- **allowlist\_include\_members**: (optional): If set to ``true`` it allows accessing unsafe REST API endpoints from other cluster members registered in DCS (IP address or hostname is taken from the members ``api_url``). Be careful, it might happen that OS will use a different IP for outgoing connections.
|
||||||
|
- **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.
|
Here is an example of both **http_extra_headers** and **https_extra_headers**:
|
||||||
- **keyfile**: Specifies the file with the secret key in the PEM format.
|
|
||||||
|
.. 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:
|
.. _patronictl_settings:
|
||||||
|
|
||||||
CTL
|
CTL
|
||||||
---
|
---
|
||||||
- **Optional**:
|
- **ctl**: (optional)
|
||||||
- **insecure**: Allow connections to REST API without verifying SSL certs.
|
- **insecure**: Allow connections to REST API without verifying SSL certs.
|
||||||
- **cacert**: Specifices the file with the CA_BUNDLE file or directory with certificates of trusted CAs to use while verifying REST API 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 certificate in the PEM format to use while verifying REST API SSL certs. If not provided patronictl will use the value provided for REST API "certfile" 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.
|
||||||
|
- **keyfile**: Specifies the file with the client secret key in the PEM format. If not provided patronictl will use the value provided for REST API "keyfile" parameter.
|
||||||
ZooKeeper
|
- **keyfile\_password**: Specifies a password for decrypting the keyfile. If not provided patronictl will use the value provided for REST API "keyfile\_password" parameter.
|
||||||
----------
|
|
||||||
- **hosts**: list of ZooKeeper cluster members in format: ['host1:port1', 'host2:port2', 'etc...'].
|
|
||||||
|
|
||||||
Watchdog
|
Watchdog
|
||||||
--------
|
--------
|
||||||
- **mode**: ``off``, ``automatic`` or ``required``. When ``off`` watchdog is disabled. When ``automatic`` watchdog will be used if available, but ignored if it is not. When ``required`` the node will not become a leader unless watchdog can be successfully enabled.
|
- **mode**: ``off``, ``automatic`` or ``required``. When ``off`` watchdog is disabled. When ``automatic`` watchdog will be used if available, but ignored if it is not. When ``required`` the node will not become a leader unless watchdog can be successfully enabled.
|
||||||
- **device**: Path to watchdog device. Defaults to ``/dev/watchdog``.
|
- **device**: Path to watchdog device. Defaults to ``/dev/watchdog``.
|
||||||
- **safety_margin**: Number of seconds of safety margin between watchdog triggering and leader key expiration.
|
- **safety_margin**: Number of seconds of safety margin between watchdog triggering and leader key expiration.
|
||||||
|
|
||||||
|
.. _tags_settings:
|
||||||
|
|
||||||
|
Tags
|
||||||
|
----
|
||||||
|
- **nofailover**: ``true`` or ``false``, controls whether this node is allowed to participate in the leader race and become a leader. Defaults to ``false``
|
||||||
|
- **clonefrom**: ``true`` or ``false``. If set to ``true`` other nodes might prefer to use this node for bootstrap (take ``pg_basebackup`` from). If there are several nodes with ``clonefrom`` tag set to ``true`` the node to bootstrap from will be chosen randomly. The default value is ``false``.
|
||||||
|
- **noloadbalance**: ``true`` or ``false``. If set to ``true`` the node will return HTTP Status Code 503 for the ``GET /replica`` REST API health-check and therefore will be excluded from the load-balancing. Defaults to ``false``.
|
||||||
|
- **replicatefrom**: The IP address/hostname of another replica. Used to support cascading replication.
|
||||||
|
- **nosync**: ``true`` or ``false``. If set to ``true`` the node will never be selected as a synchronous replica.
|
||||||
|
|
||||||
|
In addition to these predefined tags, you can also add your own ones:
|
||||||
|
|
||||||
|
- **key1**: ``true``
|
||||||
|
- **key2**: ``false``
|
||||||
|
- **key3**: ``1.4``
|
||||||
|
- **key4**: ``"RandomString"``
|
||||||
|
|
||||||
|
Tags are visible in the :ref:`REST API <rest_api>` and ``patronictl list`` You can also check for an instance health using these tags. If the tag isn't defined for an instance, or if the respective value doesn't match the querying value, it will return HTTP Status Code 503.
|
||||||
|
|||||||
+4
-1
@@ -194,4 +194,7 @@ intersphinx_mapping = {'https://docs.python.org/': None}
|
|||||||
# A possibility to have an own stylesheet, to add new rules or override existing ones
|
# A possibility to have an own stylesheet, to add new rules or override existing ones
|
||||||
# For the latter case, the CSS specificity of the rules should be higher than the default ones
|
# For the latter case, the CSS specificity of the rules should be higher than the default ones
|
||||||
def setup(app):
|
def setup(app):
|
||||||
app.add_stylesheet("custom.css")
|
if hasattr(app, 'add_css_file'):
|
||||||
|
app.add_css_file('custom.css')
|
||||||
|
else:
|
||||||
|
app.add_stylesheet('custom.css')
|
||||||
|
|||||||
@@ -14,13 +14,15 @@ Patroni configuration is stored in the DCS (Distributed Configuration Store). Th
|
|||||||
|
|
||||||
- Local :ref:`configuration <settings>` (patroni.yml).
|
- Local :ref:`configuration <settings>` (patroni.yml).
|
||||||
These options are defined in the configuration file and take precedence over dynamic configuration.
|
These options are defined in the configuration file and take precedence over dynamic configuration.
|
||||||
patroni.yml could be changed and reload in runtime (without restart of Patroni) by sending SIGHUP to the Patroni process or by performing ``POST /reload`` REST-API request.
|
patroni.yml could be changed and reloaded in runtime (without restart of Patroni) by sending SIGHUP to the Patroni process, performing ``POST /reload`` REST-API request or executing ``patronictl reload``.
|
||||||
|
|
||||||
- Environment :ref:`configuration <environment>`.
|
- Environment :ref:`configuration <environment>`.
|
||||||
It is possible to set/override some of the "Local" configuration parameters with environment variables.
|
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``).
|
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``).
|
||||||
|
|
||||||
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:
|
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 primary 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
|
- max_connections: 100
|
||||||
- max_locks_per_transaction: 64
|
- max_locks_per_transaction: 64
|
||||||
@@ -30,11 +32,12 @@ Some of the PostgreSQL parameters must hold the same values on the master and th
|
|||||||
- wal_log_hints: on
|
- wal_log_hints: on
|
||||||
- track_commit_timestamp: off
|
- track_commit_timestamp: off
|
||||||
|
|
||||||
For the parameters below, PostgreSQL does not require equal values among the master and all the replicas. However, considering the possibility of a replica to become the master at any time, it doesn't really make sense to set them differently; therefore, Patroni restricts setting their values to the Dynamic configuration
|
For the parameters below, PostgreSQL does not require equal values among the primary and all the replicas. However, considering the possibility of a replica to become the primary at any time, it doesn't really make sense to set them differently; therefore, Patroni restricts setting their values to the Dynamic configuration
|
||||||
|
|
||||||
- max_wal_senders: 5
|
- max_wal_senders: 5
|
||||||
- max_replication_slots: 5
|
- max_replication_slots: 5
|
||||||
- wal_keep_segments: 8
|
- wal_keep_segments: 8
|
||||||
|
- wal_keep_size: 128MB
|
||||||
|
|
||||||
These parameters are validated to ensure they are sane, or meet a minimum value.
|
These parameters are validated to ensure they are sane, or meet a minimum value.
|
||||||
|
|
||||||
@@ -76,154 +79,11 @@ Also, the following Patroni configuration options can be changed only dynamicall
|
|||||||
- loop_wait: 10
|
- loop_wait: 10
|
||||||
- retry_timeouts: 10
|
- retry_timeouts: 10
|
||||||
- maximum_lag_on_failover: 1048576
|
- maximum_lag_on_failover: 1048576
|
||||||
|
- max_timelines_history: 0
|
||||||
- check_timeline: false
|
- check_timeline: false
|
||||||
- postgresql.use_slots: true
|
- postgresql.use_slots: true
|
||||||
|
|
||||||
Upon changing these options, Patroni will read the relevant section of the configuration stored in DCS and change its
|
Upon changing these options, Patroni will read the relevant section of the configuration stored in DCS and change its
|
||||||
run-time values.
|
run-time values.
|
||||||
|
|
||||||
Patroni nodes are dumping the state of the DCS options to disk upon for every change of the configuration into the file ``patroni.dynamic.json`` located in the Postgres data directory. Only the master is allowed to restore these options from the on-disk dump if these are completely absent from the DCS or if they are invalid.
|
Patroni nodes are dumping the state of the DCS options to disk upon for every change of the configuration into the file ``patroni.dynamic.json`` located in the Postgres data directory. Only the leader is allowed to restore these options from the on-disk dump if these are completely absent from the DCS or if they are invalid.
|
||||||
|
|
||||||
REST API
|
|
||||||
========
|
|
||||||
|
|
||||||
We provide a REST API endpoint for working with dynamic configuration.
|
|
||||||
|
|
||||||
GET /config
|
|
||||||
-----------
|
|
||||||
Get current version of dynamic configuration.
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
$ curl -s localhost:8008/config | jq .
|
|
||||||
{
|
|
||||||
"ttl": 30,
|
|
||||||
"loop_wait": 10,
|
|
||||||
"retry_timeout": 10,
|
|
||||||
"maximum_lag_on_failover": 1048576,
|
|
||||||
"postgresql": {
|
|
||||||
"use_slots": true,
|
|
||||||
"use_pg_rewind": true,
|
|
||||||
"parameters": {
|
|
||||||
"hot_standby": "on",
|
|
||||||
"wal_log_hints": "on",
|
|
||||||
"wal_keep_segments": 8,
|
|
||||||
"wal_level": "hot_standby",
|
|
||||||
"max_wal_senders": 5,
|
|
||||||
"max_replication_slots": 5,
|
|
||||||
"max_connections": "100"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PATCH /config
|
|
||||||
-------------
|
|
||||||
Change existing configuration.
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
$ curl -s -XPATCH -d \
|
|
||||||
'{"loop_wait":5,"ttl":20,"postgresql":{"parameters":{"max_connections":"101"}}}' \
|
|
||||||
http://localhost:8008/config | jq .
|
|
||||||
{
|
|
||||||
"ttl": 20,
|
|
||||||
"loop_wait": 5,
|
|
||||||
"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",
|
|
||||||
"max_wal_senders": 5,
|
|
||||||
"max_replication_slots": 5,
|
|
||||||
"max_connections": "101"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
The above REST API call patches the existing configuration and returns the new configuration.
|
|
||||||
|
|
||||||
Let's check that the node processed this configuration. First of all it should start printing log lines every 5 seconds (loop_wait=5). The change of "max_connections" requires a restart, so the "restart_pending" flag should be exposed:
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
$ curl -s http://localhost:8008/patroni | jq .
|
|
||||||
{
|
|
||||||
"pending_restart": true,
|
|
||||||
"database_system_identifier": "6287881213849985952",
|
|
||||||
"postmaster_start_time": "2016-06-13 13:13:05.211 CEST",
|
|
||||||
"xlog": {
|
|
||||||
"location": 2197818976
|
|
||||||
},
|
|
||||||
"patroni": {
|
|
||||||
"scope": "batman",
|
|
||||||
"version": "1.0"
|
|
||||||
},
|
|
||||||
"state": "running",
|
|
||||||
"role": "master",
|
|
||||||
"server_version": 90503
|
|
||||||
}
|
|
||||||
|
|
||||||
Removing parameters:
|
|
||||||
|
|
||||||
If you want to remove (reset) some setting just patch it with ``null``:
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
$ curl -s -XPATCH -d \
|
|
||||||
'{"postgresql":{"parameters":{"max_connections":null}}}' \
|
|
||||||
http://localhost:8008/config | jq .
|
|
||||||
{
|
|
||||||
"ttl": 20,
|
|
||||||
"loop_wait": 5,
|
|
||||||
"retry_timeout": 10,
|
|
||||||
"maximum_lag_on_failover": 1048576,
|
|
||||||
"postgresql": {
|
|
||||||
"use_slots": true,
|
|
||||||
"use_pg_rewind": true,
|
|
||||||
"parameters": {
|
|
||||||
"hot_standby": "on",
|
|
||||||
"unix_socket_directories": ".",
|
|
||||||
"wal_keep_segments": 8,
|
|
||||||
"wal_level": "hot_standby",
|
|
||||||
"wal_log_hints": "on",
|
|
||||||
"max_wal_senders": 5,
|
|
||||||
"max_replication_slots": 5
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Above call removes ``postgresql.parameters.max_connections`` from the dynamic configuration.
|
|
||||||
|
|
||||||
PUT /config
|
|
||||||
-----------
|
|
||||||
|
|
||||||
It's also possible to perform the full rewrite of an existing dynamic configuration unconditionally:
|
|
||||||
|
|
||||||
.. 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}' \
|
|
||||||
http://localhost:8008/config | jq .
|
|
||||||
{
|
|
||||||
"ttl": 20,
|
|
||||||
"maximum_lag_on_failover": 1048576,
|
|
||||||
"retry_timeout": 10,
|
|
||||||
"postgresql": {
|
|
||||||
"use_slots": true,
|
|
||||||
"parameters": {
|
|
||||||
"hot_standby": "on",
|
|
||||||
"unix_socket_directories": ".",
|
|
||||||
"wal_keep_segments": 8,
|
|
||||||
"wal_level": "hot_standby",
|
|
||||||
"wal_log_hints": "on",
|
|
||||||
"max_wal_senders": 5
|
|
||||||
},
|
|
||||||
"use_pg_rewind": true
|
|
||||||
},
|
|
||||||
"loop_wait": 3
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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.
|
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.
|
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 primary 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 primary 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
|
FAQ
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ Patroni is a template for you to create your own customized, high-availability s
|
|||||||
|
|
||||||
We call Patroni a "template" because it is far from being a one-size-fits-all or plug-and-play replication system. It will have its own caveats. Use wisely. There are many ways to run high availability with PostgreSQL; for a list, see the `PostgreSQL Documentation <https://wiki.postgresql.org/wiki/Replication,_Clustering,_and_Connection_Pooling>`__.
|
We call Patroni a "template" because it is far from being a one-size-fits-all or plug-and-play replication system. It will have its own caveats. Use wisely. There are many ways to run high availability with PostgreSQL; for a list, see the `PostgreSQL Documentation <https://wiki.postgresql.org/wiki/Replication,_Clustering,_and_Connection_Pooling>`__.
|
||||||
|
|
||||||
|
Currently supported PostgreSQL versions: 9.3 to 15.
|
||||||
|
|
||||||
**Note to Kubernetes users**: Patroni can run natively on top of Kubernetes. Take a look at the :ref:`Kubernetes <kubernetes>` chapter of the Patroni documentation.
|
**Note to Kubernetes users**: Patroni can run natively on top of Kubernetes. Take a look at the :ref:`Kubernetes <kubernetes>` chapter of the Patroni documentation.
|
||||||
|
|
||||||
|
|
||||||
@@ -19,8 +21,11 @@ We call Patroni a "template" because it is far from being a one-size-fits-all or
|
|||||||
|
|
||||||
README
|
README
|
||||||
dynamic_configuration
|
dynamic_configuration
|
||||||
|
rest_api
|
||||||
|
existing_data
|
||||||
ENVIRONMENT
|
ENVIRONMENT
|
||||||
SETTINGS
|
SETTINGS
|
||||||
|
security
|
||||||
replica_bootstrap
|
replica_bootstrap
|
||||||
replication_modes
|
replication_modes
|
||||||
pause
|
pause
|
||||||
|
|||||||
+1
-1
@@ -23,7 +23,7 @@ Use ConfigMaps
|
|||||||
In this mode, Patroni will create ConfigMaps instead of Endpoints and store keys inside meta-data of those ConfigMaps.
|
In this mode, Patroni will create ConfigMaps instead of Endpoints and store keys inside meta-data of those ConfigMaps.
|
||||||
Changing the leader takes at least two updates, one to the leader ConfigMap and another to the respective Endpoint.
|
Changing the leader takes at least two updates, one to the leader ConfigMap and another to the respective Endpoint.
|
||||||
|
|
||||||
There are two ways to direct the traffic to the Postgres master:
|
There are two ways to direct the traffic to the Postgres leader:
|
||||||
|
|
||||||
- use the `callback script <https://github.com/zalando/patroni/blob/master/kubernetes/callback.py>`_ provided by Patroni
|
- use the `callback script <https://github.com/zalando/patroni/blob/master/kubernetes/callback.py>`_ provided by Patroni
|
||||||
- configure the Kubernetes Postgres service to use the label selector with the `role_label` (configured in patroni configuration).
|
- configure the Kubernetes Postgres service to use the label selector with the `role_label` (configured in patroni configuration).
|
||||||
|
|||||||
+7
-5
@@ -6,7 +6,7 @@ Pause/Resume mode for the cluster
|
|||||||
The goal
|
The goal
|
||||||
--------
|
--------
|
||||||
|
|
||||||
Under certain circumstances Patroni needs to temporary step down from managing the cluster, while still retaining the cluster state in DCS. Possible use cases are uncommon activities on the cluster, such as major version upgrades or corruption recovery. During those activities nodes are often started and stopped for the reason unknown to Patroni, some nodes can be even temporary promoted, violating the assumption of running only one master. Therefore, Patroni needs to be able to "detach" from the running cluster, implementing an equivalent of the maintenance mode in Pacemaker.
|
Under certain circumstances Patroni needs to temporarily step down from managing the cluster, while still retaining the cluster state in DCS. Possible use cases are uncommon activities on the cluster, such as major version upgrades or corruption recovery. During those activities nodes are often started and stopped for reasons unknown to Patroni, some nodes can be even temporarily promoted, violating the assumption of running only one primary. Therefore, Patroni needs to be able to "detach" from the running cluster, implementing an equivalent of the maintenance mode in Pacemaker.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -17,16 +17,18 @@ When Patroni runs in a paused mode, it does not change the state of PostgreSQL,
|
|||||||
|
|
||||||
- For each node, the member key in DCS is updated with the current information about the cluster. This causes Patroni to run read-only queries on a member node if the member is running.
|
- For each node, the member key in DCS is updated with the current information about the cluster. This causes Patroni to run read-only queries on a member node if the member is running.
|
||||||
|
|
||||||
- For the Postgres master with the leader lock Patroni updates the lock. If the node with the leader lock stops being the master (i.e. is demoted manually), Patroni will release the lock instead of promoting the node back.
|
- For the Postgres primary with the leader lock Patroni updates the lock. If the node with the leader lock stops being the primary (i.e. is demoted manually), Patroni will release the lock instead of promoting the node back.
|
||||||
|
|
||||||
- Manual unscheduled restart, reinitialize and manual failover are allowed. Manual failover is only allowed if the node to failover to is specified. In the paused mode, manual failover does not require a running master node.
|
- Manual unscheduled restart, reinitialize and manual failover are allowed. Manual failover is only allowed if the node to failover to is specified. In the paused mode, manual failover does not require a running primary node.
|
||||||
|
|
||||||
- If 'parallel' masters are detected by Patroni, it emits a warning, but does not demote the masters without the leader lock.
|
- If 'parallel' primaries are detected by Patroni, it emits a warning, but does not demote the primary without the leader lock.
|
||||||
|
|
||||||
- If there is no leader lock in the cluster, the running master acquires the lock. If there is more than one master node, then the first master to acquire the lock wins. If there are no masters altogether, Patroni does not try to promote any replicas. There is an exception in this rule: if there is no leader lock because the old master has demoted itself due to the manual promotion, then only the candidate node mentioned in the promotion request may take the leader lock. When the new leader lock is granted (i.e. after promoting a replica manually), Patroni makes sure the replicas that were streaming from the previous leader will switch to the new one.
|
- If there is no leader lock in the cluster, the running primary acquires the lock. If there is more than one primary node, then the first primary to acquire the lock wins. If there are no primary altogether, Patroni does not try to promote any replicas. There is an exception in this rule: if there is no leader lock because the old primary has demoted itself due to the manual promotion, then only the candidate node mentioned in the promotion request may take the leader lock. When the new leader lock is granted (i.e. after promoting a replica manually), Patroni makes sure the replicas that were streaming from the previous leader will switch to the new one.
|
||||||
|
|
||||||
- When Postgres is stopped, Patroni does not try to start it. When Patroni is stopped, it does not try to stop the Postgres instance it is managing.
|
- When Postgres is stopped, Patroni does not try to start it. When Patroni is stopped, it does not try to stop the Postgres instance it is managing.
|
||||||
|
|
||||||
|
- Patroni will not try to remove replication slots that don't represent the other cluster member or are not listed in the configuration of the permanent slots.
|
||||||
|
|
||||||
User guide
|
User guide
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|||||||
+1220
-10
File diff suppressed because it is too large
Load Diff
@@ -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>:
|
<custom_bootstrap_method_name>:
|
||||||
command: <path_to_custom_bootstrap_script> [param1 [, ...]]
|
command: <path_to_custom_bootstrap_script> [param1 [, ...]]
|
||||||
keep_existing_recovery_conf: False
|
keep_existing_recovery_conf: False
|
||||||
|
no_params: False
|
||||||
recovery_conf:
|
recovery_conf:
|
||||||
recovery_target_action: promote
|
recovery_target_action: promote
|
||||||
recovery_target_timeline: latest
|
recovery_target_timeline: latest
|
||||||
@@ -40,6 +41,8 @@ in the configuration files, Patroni supplies two cluster-specific ones:
|
|||||||
--datadir
|
--datadir
|
||||||
Path to the data directory of the cluster instance to be bootstrapped
|
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
|
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,
|
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.
|
cleans up after itself and releases the initialize lock to give another node the opportunity to bootstrap.
|
||||||
@@ -60,7 +63,7 @@ Building replicas
|
|||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
Patroni uses tried and proven ``pg_basebackup`` in order to create new replicas. One downside of it is that it requires
|
Patroni uses tried and proven ``pg_basebackup`` in order to create new replicas. One downside of it is that it requires
|
||||||
a running master node. Another one is the lack of 'on-the-fly' compression for the backup data and no built-in cleanup
|
a running leader node. Another one is the lack of 'on-the-fly' compression for the backup data and no built-in cleanup
|
||||||
for outdated backup files. Some people prefer other backup solutions, such as ``WAL-E``, ``pgBackRest``, ``Barman`` and
|
for outdated backup files. Some people prefer other backup solutions, such as ``WAL-E``, ``pgBackRest``, ``Barman`` and
|
||||||
others, or simply roll their own scripts. In order to accommodate all those use-cases Patroni supports running custom
|
others, or simply roll their own scripts. In order to accommodate all those use-cases Patroni supports running custom
|
||||||
scripts to clone a new replica. Those are configured in the ``postgresql`` configuration block:
|
scripts to clone a new replica. Those are configured in the ``postgresql`` configuration block:
|
||||||
@@ -120,26 +123,30 @@ to execute and any custom parameters that should be passed to that command. All
|
|||||||
--role
|
--role
|
||||||
Always 'replica'
|
Always 'replica'
|
||||||
--connstring
|
--connstring
|
||||||
Connection string to connect to the cluster member to clone from (master or other replica). The user in the
|
Connection string to connect to the cluster member to clone from (primary or other replica). The user in the
|
||||||
connection string can execute SQL and replication protocol commands.
|
connection string can execute SQL and replication protocol commands.
|
||||||
|
|
||||||
A special ``no_master`` parameter, if defined, allows Patroni to call the replica creation method even if there is no
|
A special ``no_master`` parameter, if defined, allows Patroni to call the replica creation method even if there is no
|
||||||
running master or replicas. In that case, an empty string will be passed in a connection string. This is useful for
|
running leader or replicas. In that case, an empty string will be passed in a connection string. This is useful for
|
||||||
restoring the formerly running cluster from the binary backup.
|
restoring the formerly running cluster from the binary backup.
|
||||||
|
|
||||||
A special ``keep_data`` parameter, if defined, will instuct Patroni to not clean PGDATA folder before calling restore.
|
A special ``keep_data`` parameter, if defined, will instruct Patroni to not clean PGDATA folder before calling restore.
|
||||||
|
|
||||||
A special ``no_params`` parameter, if defined, restricts passing parameters to custom command.
|
A special ``no_params`` parameter, if defined, restricts passing parameters to custom command.
|
||||||
|
|
||||||
A ``basebackup`` method is a special case: it will be used if
|
A ``basebackup`` method is a special case: it will be used if
|
||||||
``create_replica_methods`` is empty, although it is possible
|
``create_replica_methods`` is empty, although it is possible
|
||||||
to list it explicitly among the ``create_replica_methods`` methods. This method initializes a new replica with the
|
to list it explicitly among the ``create_replica_methods`` methods. This method initializes a new replica with the
|
||||||
``pg_basebackup``, the base backup is taken from the master unless there are replicas with ``clonefrom`` tag, in which case one
|
``pg_basebackup``, the base backup is taken from the leader unless there are replicas with ``clonefrom`` tag, in which case one
|
||||||
of such replicas will be used as the origin for pg_basebackup. It works without any configuration; however, it is
|
of such replicas will be used as the origin for pg_basebackup. It works without any configuration; however, it is
|
||||||
possible to specify a ``basebackup`` configuration section. Same rules as with the other method configuration apply,
|
possible to specify a ``basebackup`` configuration section. Same rules as with the other method configuration apply,
|
||||||
namely, only long (with --) options should be specified there. Not all parameters make sense, if you override a connection
|
namely, only long (with --) options should be specified there. Not all parameters make sense, if you override a connection
|
||||||
string or provide an option to created tar-ed or compressed base backups, patroni won't be able to make a replica out
|
string or provide an option to created tar-ed or compressed base backups, patroni won't be able to make a replica out
|
||||||
of it. There is no validation performed on the names or values of the parameters passed to the ``basebackup`` section.
|
of it. There is no validation performed on the names or values of the parameters passed to the ``basebackup`` section.
|
||||||
|
Also note that in case symlinks are used for the WAL folder it is up to the user to specify the correct ``--waldir``
|
||||||
|
path as an option, so that after replica buildup or re-initialization the symlink would persist. This option is supported
|
||||||
|
only since v10 though.
|
||||||
|
|
||||||
You can specify basebackup parameters as either a map (key-value pairs) or a list of elements, where each element
|
You can specify basebackup parameters as either a map (key-value pairs) or a list of elements, where each element
|
||||||
could be either a key-value pair or a single key (for options that does not receive any values, for instance, ``--verbose``).
|
could be either a key-value pair or a single key (for options that does not receive any values, for instance, ``--verbose``).
|
||||||
Consider those 2 examples:
|
Consider those 2 examples:
|
||||||
@@ -159,6 +166,7 @@ and
|
|||||||
basebackup:
|
basebackup:
|
||||||
- verbose
|
- verbose
|
||||||
- max-rate: '100M'
|
- max-rate: '100M'
|
||||||
|
- waldir: /pg-wal-mount/external-waldir
|
||||||
|
|
||||||
If all replica creation methods fail, Patroni will try again all methods in order during the next event loop cycle.
|
If all replica creation methods fail, Patroni will try again all methods in order during the next event loop cycle.
|
||||||
|
|
||||||
@@ -168,10 +176,10 @@ Standby cluster
|
|||||||
---------------
|
---------------
|
||||||
|
|
||||||
Another available option is to run a "standby cluster", that contains only of
|
Another available option is to run a "standby cluster", that contains only of
|
||||||
standby nodes replicating from some remote master. This type of clusters has:
|
standby nodes replicating from some remote node. This type of clusters has:
|
||||||
|
|
||||||
* "standby leader", that behaves pretty much like a regular cluster leader,
|
* "standby leader", that behaves pretty much like a regular cluster leader,
|
||||||
except it replicates from a remote master.
|
except it replicates from a remote node.
|
||||||
|
|
||||||
* cascade replicas, that are replicating from standby leader.
|
* cascade replicas, that are replicating from standby leader.
|
||||||
|
|
||||||
@@ -179,6 +187,13 @@ Standby leader holds and updates a leader lock in DCS. If the leader lock
|
|||||||
expires, cascade replicas will perform an election to choose another leader
|
expires, cascade replicas will perform an election to choose another leader
|
||||||
from the standbys.
|
from the standbys.
|
||||||
|
|
||||||
|
There is no further relationship between the standby cluster and the primary
|
||||||
|
cluster it replicates from, in particular, they must not share the same DCS
|
||||||
|
scope if they use the same DCS. They do not know anything else from each other
|
||||||
|
apart from replication information. Also, the standby cluster is not being
|
||||||
|
displayed in ``patronictl list`` or ``patronictl topology`` output on the
|
||||||
|
primary cluster.
|
||||||
|
|
||||||
For the sake of flexibility, you can specify methods of creating a replica and
|
For the sake of flexibility, you can specify methods of creating a replica and
|
||||||
recovery WAL records when a cluster is in the "standby mode" by providing
|
recovery WAL records when a cluster is in the "standby mode" by providing
|
||||||
`create_replica_methods` key in `standby_cluster` section. It is distinct from
|
`create_replica_methods` key in `standby_cluster` section. It is distinct from
|
||||||
@@ -204,4 +219,9 @@ in a patroni configuration:
|
|||||||
Note, that these options will be applied only once during cluster bootstrap,
|
Note, that these options will be applied only once during cluster bootstrap,
|
||||||
and the only way to change them afterwards is through DCS.
|
and the only way to change them afterwards is through DCS.
|
||||||
|
|
||||||
If you use replication slots on the standby cluster, you must also create the corresponding replication slot on the primary cluster. It will not be done automatically by the standby cluster implementation. You can use Patroni's permenant replication slots feature on the primary cluster to maintain a replication slot with the same name as ``primary_slot_name``, or its default value if ``primary_slot_name`` is not provided.
|
Patroni expects to find `postgresql.conf` or `postgresql.conf.backup` in PGDATA
|
||||||
|
of the remote primary and will not start if it does not find it after a
|
||||||
|
basebackup. If the remote primary keeps its `postgresql.conf` elsewhere, it is
|
||||||
|
your responsibility to copy it to PGDATA.
|
||||||
|
|
||||||
|
If you use replication slots on the standby cluster, you must also create the corresponding replication slot on the primary cluster. It will not be done automatically by the standby cluster implementation. You can use Patroni's permanent replication slots feature on the primary cluster to maintain a replication slot with the same name as ``primary_slot_name``, or its default value if ``primary_slot_name`` is not provided.
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ In asynchronous mode the cluster is allowed to lose some committed transactions
|
|||||||
|
|
||||||
The amount of transactions that can be lost is controlled via ``maximum_lag_on_failover`` parameter. Because the primary transaction log position is not sampled in real time, in reality the amount of lost data on failover is worst case bounded by ``maximum_lag_on_failover`` bytes of transaction log plus the amount that is written in the last ``ttl`` seconds (``loop_wait``/2 seconds in the average case). However typical steady state replication delay is well under a second.
|
The amount of transactions that can be lost is controlled via ``maximum_lag_on_failover`` parameter. Because the primary transaction log position is not sampled in real time, in reality the amount of lost data on failover is worst case bounded by ``maximum_lag_on_failover`` bytes of transaction log plus the amount that is written in the last ``ttl`` seconds (``loop_wait``/2 seconds in the average case). However typical steady state replication delay is well under a second.
|
||||||
|
|
||||||
By default, when running leader elections, Patroni does not take into account the current timeline of replicas, what in some cases could be undesirable behavior. You can prevent the node not having the same timeline as a former master become the new leader by changing the value of ``check_timeline`` parameter to ``true``.
|
By default, when running leader elections, Patroni does not take into account the current timeline of replicas, what in some cases could be undesirable behavior. You can prevent the node not having the same timeline as a former primary become the new leader by changing the value of ``check_timeline`` parameter to ``true``.
|
||||||
|
|
||||||
PostgreSQL synchronous replication
|
PostgreSQL synchronous replication
|
||||||
----------------------------------
|
----------------------------------
|
||||||
@@ -46,7 +46,7 @@ When ``synchronous_mode`` is on and a standby crashes, commits will block until
|
|||||||
|
|
||||||
When it is absolutely necessary to guarantee that each write is stored durably
|
When it is absolutely necessary to guarantee that each write is stored durably
|
||||||
on at least two nodes, enable ``synchronous_mode_strict`` in addition to the
|
on at least two nodes, enable ``synchronous_mode_strict`` in addition to the
|
||||||
``synchronous_node``. This parameter prevents Patroni from switching off the
|
``synchronous_mode``. This parameter prevents Patroni from switching off the
|
||||||
synchronous replication on the primary when no synchronous standby candidates
|
synchronous replication on the primary when no synchronous standby candidates
|
||||||
are available. As a downside, the primary is not be available for writes
|
are available. As a downside, the primary is not be available for writes
|
||||||
(unless the Postgres transaction explicitly turns of ``synchronous_mode``),
|
(unless the Postgres transaction explicitly turns of ``synchronous_mode``),
|
||||||
@@ -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.
|
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
|
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.
|
- 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.
|
- 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.
|
.. [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.
|
||||||
|
|||||||
@@ -0,0 +1,394 @@
|
|||||||
|
.. _rest_api:
|
||||||
|
|
||||||
|
Patroni REST API
|
||||||
|
================
|
||||||
|
|
||||||
|
Patroni has a rich REST API, which 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. Below you will find the list of Patroni REST API endpoints.
|
||||||
|
|
||||||
|
Health check endpoints
|
||||||
|
----------------------
|
||||||
|
For all health check ``GET`` requests Patroni returns a JSON document with the status of the node, along with the HTTP status code. If you don't want or don't need the JSON document, you might consider using the ``HEAD`` or ``OPTIONS`` method instead of ``GET``.
|
||||||
|
|
||||||
|
- The following requests to Patroni REST API will return HTTP status code **200** only when the Patroni node is running as the primary with leader lock:
|
||||||
|
|
||||||
|
- ``GET /``
|
||||||
|
- ``GET /master``
|
||||||
|
- ``GET /primary``
|
||||||
|
- ``GET /read-write``
|
||||||
|
|
||||||
|
- ``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>`.
|
||||||
|
|
||||||
|
- ``GET /leader``: returns HTTP status code **200** when the Patroni node has the leader lock. The major difference from the two previous endpoints is that it doesn't take into account whether PostgreSQL is running as the ``primary`` or the ``standby_leader``.
|
||||||
|
|
||||||
|
- ``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 /replica?tag_key1=value1&tag_key2=value2``: replica check endpoint. In addition, It will also check for user defined tags ``key1`` and ``key2`` and their respective values in the **tags** section of the yaml configuration management. If the tag isn't defined for an instance, or if the value in the yaml configuration doesn't match the querying value, it will return HTTP Status Code 503.
|
||||||
|
|
||||||
|
In the following requests, since we are checking for the leader or standby-leader status, Patroni doesn't apply any of the user defined tags and they will be ignored.
|
||||||
|
- ``GET /?tag_key1=value1&tag_key2=value2``
|
||||||
|
- ``GET /master?tag_key1=value1&tag_key2=value2``
|
||||||
|
- ``GET /leader?tag_key1=value1&tag_key2=value2``
|
||||||
|
- ``GET /primary?tag_key1=value1&tag_key2=value2``
|
||||||
|
- ``GET /read-write?tag_key1=value1&tag_key2=value2``
|
||||||
|
- ``GET /standby_leader?tag_key1=value1&tag_key2=value2``
|
||||||
|
- ``GET /standby-leader?tag_key1=value1&tag_key2=value2``
|
||||||
|
|
||||||
|
- ``GET /read-only``: like the above endpoint, but also includes the primary.
|
||||||
|
|
||||||
|
- ``GET /synchronous`` or ``GET /sync``: returns HTTP status code **200** only when the Patroni node is running as a synchronous standby.
|
||||||
|
|
||||||
|
- ``GET /read-only-sync``: like the above endpoint, but also includes the primary.
|
||||||
|
|
||||||
|
- ``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``: returns HTTP status code **200** if Patroni heartbeat loop is properly running and **503** if the last run was more than ``ttl`` seconds ago on the primary or ``2*ttl`` on the replica. 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 Kubernetes 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
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
The ``GET /patroni`` is used by Patroni during the leader race. It also could be used by your monitoring system. The JSON document produced by this endpoint has the same structure as the JSON produced by the health check endpoints.
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s http://localhost:8008/patroni | jq .
|
||||||
|
{
|
||||||
|
"state": "running",
|
||||||
|
"postmaster_start_time": "2019-09-24 09:22:32.555 CEST",
|
||||||
|
"role": "master",
|
||||||
|
"server_version": 110005,
|
||||||
|
"cluster_unlocked": false,
|
||||||
|
"xlog": {
|
||||||
|
"location": 25624640
|
||||||
|
},
|
||||||
|
"timeline": 3,
|
||||||
|
"database_system_identifier": "6739877027151648096",
|
||||||
|
"patroni": {
|
||||||
|
"version": "1.6.0",
|
||||||
|
"scope": "batman"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Cluster status endpoints
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
- The ``GET /cluster`` endpoint generates a JSON document describing the current cluster topology and state:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s http://localhost:8008/cluster | jq .
|
||||||
|
{
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"name": "postgresql0",
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 5432,
|
||||||
|
"role": "leader",
|
||||||
|
"state": "running",
|
||||||
|
"api_url": "http://127.0.0.1:8008/patroni",
|
||||||
|
"timeline": 5,
|
||||||
|
"tags": {
|
||||||
|
"clonefrom": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "postgresql1",
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 5433,
|
||||||
|
"role": "replica",
|
||||||
|
"state": "running",
|
||||||
|
"api_url": "http://127.0.0.1:8009/patroni",
|
||||||
|
"timeline": 5,
|
||||||
|
"tags": {
|
||||||
|
"clonefrom": true
|
||||||
|
},
|
||||||
|
"lag": 0
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"scheduled_switchover": {
|
||||||
|
"at": "2019-09-24T10:36:00+02:00",
|
||||||
|
"from": "postgresql0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
- The ``GET /history`` endpoint provides a view on the history of cluster switchovers/failovers. The format is very similar to the content of history files in the ``pg_wal`` directory. The only difference is the timestamp field showing when the new timeline was created.
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s http://localhost:8008/history | jq .
|
||||||
|
[
|
||||||
|
[
|
||||||
|
1,
|
||||||
|
25623960,
|
||||||
|
"no recovery target specified",
|
||||||
|
"2019-09-23T16:57:57+02:00"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
2,
|
||||||
|
25624344,
|
||||||
|
"no recovery target specified",
|
||||||
|
"2019-09-24T09:22:33+02:00"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
3,
|
||||||
|
25624752,
|
||||||
|
"no recovery target specified",
|
||||||
|
"2019-09-24T09:26:15+02:00"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
4,
|
||||||
|
50331856,
|
||||||
|
"no recovery target specified",
|
||||||
|
"2019-09-24T09:35:52+02:00"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
Config endpoint
|
||||||
|
---------------
|
||||||
|
|
||||||
|
``GET /config``: Get the current version of the dynamic configuration:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s localhost:8008/config | jq .
|
||||||
|
{
|
||||||
|
"ttl": 30,
|
||||||
|
"loop_wait": 10,
|
||||||
|
"retry_timeout": 10,
|
||||||
|
"maximum_lag_on_failover": 1048576,
|
||||||
|
"postgresql": {
|
||||||
|
"use_slots": true,
|
||||||
|
"use_pg_rewind": true,
|
||||||
|
"parameters": {
|
||||||
|
"hot_standby": "on",
|
||||||
|
"wal_log_hints": "on",
|
||||||
|
"wal_level": "hot_standby",
|
||||||
|
"max_wal_senders": 5,
|
||||||
|
"max_replication_slots": 5,
|
||||||
|
"max_connections": "100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
``PATCH /config``: Change the existing configuration.
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s -XPATCH -d \
|
||||||
|
'{"loop_wait":5,"ttl":20,"postgresql":{"parameters":{"max_connections":"101"}}}' \
|
||||||
|
http://localhost:8008/config | jq .
|
||||||
|
{
|
||||||
|
"ttl": 20,
|
||||||
|
"loop_wait": 5,
|
||||||
|
"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",
|
||||||
|
"max_wal_senders": 5,
|
||||||
|
"max_replication_slots": 5,
|
||||||
|
"max_connections": "101"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
The above REST API call patches the existing configuration and returns the new configuration.
|
||||||
|
|
||||||
|
Let's check that the node processed this configuration. First of all it should start printing log lines every 5 seconds (loop_wait=5). The change of "max_connections" requires a restart, so the "pending_restart" flag should be exposed:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s http://localhost:8008/patroni | jq .
|
||||||
|
{
|
||||||
|
"pending_restart": true,
|
||||||
|
"database_system_identifier": "6287881213849985952",
|
||||||
|
"postmaster_start_time": "2016-06-13 13:13:05.211 CEST",
|
||||||
|
"xlog": {
|
||||||
|
"location": 2197818976
|
||||||
|
},
|
||||||
|
"patroni": {
|
||||||
|
"scope": "batman",
|
||||||
|
"version": "1.0"
|
||||||
|
},
|
||||||
|
"state": "running",
|
||||||
|
"role": "master",
|
||||||
|
"server_version": 90503
|
||||||
|
}
|
||||||
|
|
||||||
|
Removing parameters:
|
||||||
|
|
||||||
|
If you want to remove (reset) some setting just patch it with ``null``:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s -XPATCH -d \
|
||||||
|
'{"postgresql":{"parameters":{"max_connections":null}}}' \
|
||||||
|
http://localhost:8008/config | jq .
|
||||||
|
{
|
||||||
|
"ttl": 20,
|
||||||
|
"loop_wait": 5,
|
||||||
|
"retry_timeout": 10,
|
||||||
|
"maximum_lag_on_failover": 1048576,
|
||||||
|
"postgresql": {
|
||||||
|
"use_slots": true,
|
||||||
|
"use_pg_rewind": true,
|
||||||
|
"parameters": {
|
||||||
|
"hot_standby": "on",
|
||||||
|
"unix_socket_directories": ".",
|
||||||
|
"wal_level": "hot_standby",
|
||||||
|
"wal_log_hints": "on",
|
||||||
|
"max_wal_senders": 5,
|
||||||
|
"max_replication_slots": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
The above call removes ``postgresql.parameters.max_connections`` from the dynamic configuration.
|
||||||
|
|
||||||
|
``PUT /config``: It's also possible to perform the full rewrite of an existing dynamic configuration unconditionally:
|
||||||
|
|
||||||
|
.. 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_level":"hot_standby","unix_socket_directories":".","max_wal_senders":5}},"loop_wait":3,"ttl":20}' \
|
||||||
|
http://localhost:8008/config | jq .
|
||||||
|
{
|
||||||
|
"ttl": 20,
|
||||||
|
"maximum_lag_on_failover": 1048576,
|
||||||
|
"retry_timeout": 10,
|
||||||
|
"postgresql": {
|
||||||
|
"use_slots": true,
|
||||||
|
"parameters": {
|
||||||
|
"hot_standby": "on",
|
||||||
|
"unix_socket_directories": ".",
|
||||||
|
"wal_level": "hot_standby",
|
||||||
|
"wal_log_hints": "on",
|
||||||
|
"max_wal_senders": 5
|
||||||
|
},
|
||||||
|
"use_pg_rewind": true
|
||||||
|
},
|
||||||
|
"loop_wait": 3
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Switchover and failover endpoints
|
||||||
|
---------------------------------
|
||||||
|
|
||||||
|
``POST /switchover`` or ``POST /failover``. These endpoints are very similar to each other. There are a couple of minor differences though:
|
||||||
|
|
||||||
|
1. The failover endpoint allows to perform a manual failover when there are no healthy nodes, but at the same time it will not allow you to schedule a switchover.
|
||||||
|
|
||||||
|
2. The switchover endpoint is the opposite. It works only when the cluster is healthy (there is a leader) and allows to schedule a switchover at a given time.
|
||||||
|
|
||||||
|
|
||||||
|
In the JSON body of the ``POST`` request you must specify at least the ``leader`` or ``candidate`` fields and optionally the ``scheduled_at`` field if you want to schedule a switchover at a specific time.
|
||||||
|
|
||||||
|
|
||||||
|
Example: perform a failover to the specific node:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s http://localhost:8009/failover -XPOST -d '{"candidate":"postgresql1"}'
|
||||||
|
Successfully failed over to "postgresql1"
|
||||||
|
|
||||||
|
|
||||||
|
Example: schedule a switchover from the leader to any other healthy replica in the cluster at a specific time:
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
$ curl -s http://localhost:8008/switchover -XPOST -d \
|
||||||
|
'{"leader":"postgresql0","scheduled_at":"2019-09-24T12:00+00"}'
|
||||||
|
Switchover scheduled
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
- ``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
|
||||||
|
----------------
|
||||||
|
|
||||||
|
- ``POST /restart``: You can restart Postgres on the specific node by performing the ``POST /restart`` call. In the JSON body of ``POST`` request it is possible to optionally specify some restart conditions:
|
||||||
|
|
||||||
|
- **restart_pending**: boolean, if set to ``true`` Patroni will restart PostgreSQL only when restart is pending in order to apply some changes in the PostgreSQL config.
|
||||||
|
- **role**: perform restart only if the current role of the node matches with the role from the POST request.
|
||||||
|
- **postgres_version**: perform restart only if the current version of postgres is smaller than specified in the POST request.
|
||||||
|
- **timeout**: how long we should wait before PostgreSQL starts accepting connections. Overrides ``master_start_timeout``.
|
||||||
|
- **schedule**: timestamp with time zone, schedule the restart somewhere in the future.
|
||||||
|
|
||||||
|
- ``DELETE /restart``: delete the scheduled restart
|
||||||
|
|
||||||
|
``POST /restart`` and ``DELETE /restart`` endpoints are used by ``patronictl restart`` and ``patronictl flush <cluster-name> restart`` respectively.
|
||||||
|
|
||||||
|
|
||||||
|
Reload endpoint
|
||||||
|
---------------
|
||||||
|
|
||||||
|
The ``POST /reload`` call will order Patroni to re-read and apply the configuration file. This is the equivalent of sending the ``SIGHUP`` signal to the Patroni process. In case you changed some of the Postgres parameters which require a restart (like **shared_buffers**), you still have to explicitly do the restart of Postgres by either calling the ``POST /restart`` endpoint or with the help of ``patronictl restart``.
|
||||||
|
|
||||||
|
The reload endpoint is used by ``patronictl reload``.
|
||||||
|
|
||||||
|
|
||||||
|
Reinitialize endpoint
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
``POST /reinitialize``: reinitialize the PostgreSQL data directory on the specified node. It is allowed to be executed only on replicas. Once called, it will remove the data directory and start ``pg_basebackup`` or some alternative :ref:`replica creation method <custom_replica_creation>`.
|
||||||
|
|
||||||
|
The call might fail if Patroni is in a loop trying to recover (restart) a failed Postgres. In order to overcome this problem one can specify ``{"force":true}`` in the request body.
|
||||||
|
|
||||||
|
The reinitialize endpoint is used by ``patronictl reinit``.
|
||||||
@@ -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
|
||||||
+2
-2
@@ -3,7 +3,7 @@
|
|||||||
Watchdog support
|
Watchdog support
|
||||||
================
|
================
|
||||||
|
|
||||||
Having multiple PostgreSQL servers running as master can result in transactions lost due to diverging timelines. This situation is also called a split-brain problem. To avoid split-brain Patroni needs to ensure PostgreSQL will not accept any transaction commits after leader key expires in the DCS. Under normal circumstances Patroni will try to achieve this by stopping PostgreSQL when leader lock update fails for any reason. However, this may fail to happen due to various reasons:
|
Having multiple PostgreSQL servers running as primary can result in transactions lost due to diverging timelines. This situation is also called a split-brain problem. To avoid split-brain Patroni needs to ensure PostgreSQL will not accept any transaction commits after leader key expires in the DCS. Under normal circumstances Patroni will try to achieve this by stopping PostgreSQL when leader lock update fails for any reason. However, this may fail to happen due to various reasons:
|
||||||
|
|
||||||
- Patroni has crashed due to a bug, out-of-memory condition or by being accidentally killed by a system administrator.
|
- Patroni has crashed due to a bug, out-of-memory condition or by being accidentally killed by a system administrator.
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ Having multiple PostgreSQL servers running as master can result in transactions
|
|||||||
|
|
||||||
To guarantee correct behavior under these conditions Patroni supports watchdog devices. Watchdog devices are software or hardware mechanisms that will reset the whole system when they do not get a keepalive heartbeat within a specified timeframe. This adds an additional layer of fail safe in case usual Patroni split-brain protection mechanisms fail.
|
To guarantee correct behavior under these conditions Patroni supports watchdog devices. Watchdog devices are software or hardware mechanisms that will reset the whole system when they do not get a keepalive heartbeat within a specified timeframe. This adds an additional layer of fail safe in case usual Patroni split-brain protection mechanisms fail.
|
||||||
|
|
||||||
Patroni will try to activate the watchdog before promoting PostgreSQL to master. If watchdog activation fails and watchdog mode is ``required`` then the node will refuse to become master. When deciding to participate in leader election Patroni will also check that watchdog configuration will allow it to become leader at all. After demoting PostgreSQL (for example due to a manual failover) Patroni will disable the watchdog again. Watchdog will also be disabled while Patroni is in paused state.
|
Patroni will try to activate the watchdog before promoting PostgreSQL to primary. If watchdog activation fails and watchdog mode is ``required`` then the node will refuse to become leader. When deciding to participate in leader election Patroni will also check that watchdog configuration will allow it to become leader at all. After demoting PostgreSQL (for example due to a manual failover) Patroni will disable the watchdog again. Watchdog will also be disabled while Patroni is in paused state.
|
||||||
|
|
||||||
By default Patroni will set up the watchdog to expire 5 seconds before TTL expires. With the default setup of ``loop_wait=10`` and ``ttl=30`` this gives HA loop at least 15 seconds (``ttl`` - ``safety_margin`` - ``loop_wait``) to complete before the system gets forcefully reset. By default accessing DCS is configured to time out after 10 seconds. This means that when DCS is unavailable, for example due to network issues, Patroni and PostgreSQL will have at least 5 seconds (``ttl`` - ``safety_margin`` - ``loop_wait`` - ``retry_timeout``) to come to a state where all client connections are terminated.
|
By default Patroni will set up the watchdog to expire 5 seconds before TTL expires. With the default setup of ``loop_wait=10`` and ``ttl=30`` this gives HA loop at least 15 seconds (``ttl`` - ``safety_margin`` - ``loop_wait``) to complete before the system gets forcefully reset. By default accessing DCS is configured to time out after 10 seconds. This means that when DCS is unavailable, for example due to network issues, Patroni and PostgreSQL will have at least 5 seconds (``ttl`` - ``safety_margin`` - ``loop_wait`` - ``retry_timeout``) to come to a state where all client connections are terminated.
|
||||||
|
|
||||||
|
|||||||
@@ -18,14 +18,14 @@ listen stats
|
|||||||
|
|
||||||
listen master
|
listen master
|
||||||
bind *:5000
|
bind *:5000
|
||||||
option httpchk OPTIONS /master
|
option httpchk HEAD /master
|
||||||
http-check expect status 200
|
http-check expect status 200
|
||||||
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
|
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
|
||||||
{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} maxconn 100 check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
|
{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} maxconn 100 check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
|
||||||
{{end}}
|
{{end}}
|
||||||
listen replicas
|
listen replicas
|
||||||
bind *:5001
|
bind *:5001
|
||||||
option httpchk OPTIONS /replica
|
option httpchk HEAD /replica
|
||||||
http-check expect status 200
|
http-check expect status 200
|
||||||
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
|
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
|
||||||
{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} maxconn 100 check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
|
{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} maxconn 100 check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ Group=postgres
|
|||||||
# Read in configuration file if it exists, otherwise proceed
|
# Read in configuration file if it exists, otherwise proceed
|
||||||
EnvironmentFile=-/etc/patroni_env.conf
|
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
|
# Where to send early-startup messages from the server
|
||||||
# This is normally controlled by the global default set by systemd
|
# This is normally controlled by the global default set by systemd
|
||||||
@@ -31,14 +32,14 @@ ExecStart=/bin/patroni /etc/patroni.yml
|
|||||||
# Send HUP to reload from patroni.yml
|
# Send HUP to reload from patroni.yml
|
||||||
ExecReload=/bin/kill -s HUP $MAINPID
|
ExecReload=/bin/kill -s HUP $MAINPID
|
||||||
|
|
||||||
# only kill the patroni process, not it's children, so it will gracefully stop postgres
|
# Only kill the patroni process, not it's children, so it will gracefully stop postgres
|
||||||
KillMode=process
|
KillMode=process
|
||||||
|
|
||||||
# Give a reasonable amount of time for the server to start up/shut down
|
# Give a reasonable amount of time for the server to start up/shut down
|
||||||
TimeoutSec=30
|
TimeoutSec=30
|
||||||
|
|
||||||
# Do not restart the service if it crashes, we want to manually inspect database on failure
|
# Restart the service if it crashed
|
||||||
Restart=no
|
Restart=on-failure
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--dirname", required=True)
|
||||||
|
parser.add_argument("--pathname", required=True)
|
||||||
|
parser.add_argument("--filename", required=True)
|
||||||
|
parser.add_argument("--mode", required=True, choices=("archive", "restore"))
|
||||||
|
args, _ = parser.parse_known_args()
|
||||||
|
|
||||||
|
full_filename = os.path.join(args.dirname, args.filename)
|
||||||
|
if args.mode == "archive":
|
||||||
|
if not os.path.isdir(args.dirname):
|
||||||
|
os.makedirs(args.dirname)
|
||||||
|
if not os.path.exists(full_filename):
|
||||||
|
shutil.copy(args.pathname, full_filename)
|
||||||
|
else:
|
||||||
|
shutil.copy(full_filename, args.pathname)
|
||||||
Executable
+14
@@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import argparse
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--datadir", required=True)
|
||||||
|
parser.add_argument("--dbname", required=True)
|
||||||
|
parser.add_argument("--walmethod", required=True, choices=("fetch", "stream", "none"))
|
||||||
|
args, _ = parser.parse_known_args()
|
||||||
|
|
||||||
|
walmethod = ["-X", args.walmethod] if args.walmethod != "none" else []
|
||||||
|
sys.exit(subprocess.call(["pg_basebackup", "-D", args.datadir, "-c", "fast", "-d", args.dbname] + walmethod))
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
while getopts ":-:" optchar; do
|
|
||||||
[[ "${optchar}" == "-" ]] || continue
|
|
||||||
case "${OPTARG}" in
|
|
||||||
datadir=* )
|
|
||||||
PGDATA=${OPTARG#*=}
|
|
||||||
;;
|
|
||||||
dbname=* )
|
|
||||||
DBNAME=${OPTARG#*=}
|
|
||||||
;;
|
|
||||||
walmethod=* )
|
|
||||||
WALMETHOD=${OPTARG#*=}
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
[[ -z $PGDATA || -z $DBNAME || -z $WALMETHOD ]] && exit 1
|
|
||||||
|
|
||||||
[[ $WALMETHOD != "none" ]] && WALMETHOD="-X $WALMETHOD" || WALMETHOD=""
|
|
||||||
|
|
||||||
exec pg_basebackup -D $PGDATA $WALMETHOD -c fast -d $DBNAME
|
|
||||||
Executable
+11
@@ -0,0 +1,11 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import argparse
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--datadir", required=True)
|
||||||
|
parser.add_argument("--sourcedir", required=True)
|
||||||
|
args, _ = parser.parse_known_args()
|
||||||
|
|
||||||
|
shutil.copytree(args.sourcedir, args.datadir)
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -x
|
|
||||||
|
|
||||||
while getopts ":-:" optchar; do
|
|
||||||
[[ "${optchar}" == "-" ]] || continue
|
|
||||||
case "${OPTARG}" in
|
|
||||||
datadir=* )
|
|
||||||
PGDATA=${OPTARG#*=}
|
|
||||||
;;
|
|
||||||
sourcedir=* )
|
|
||||||
SOURCE=${OPTARG#*=}
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
[[ -z $PGDATA || -z $SOURCE ]] && exit 1
|
|
||||||
|
|
||||||
mkdir -p $(dirname $PGDATA)
|
|
||||||
|
|
||||||
exec cp -af $SOURCE $PGDATA
|
|
||||||
@@ -4,7 +4,8 @@ Feature: basic replication
|
|||||||
Scenario: check replication of a single table
|
Scenario: check replication of a single table
|
||||||
Given I start postgres0
|
Given I start postgres0
|
||||||
Then postgres0 is a leader after 10 seconds
|
Then postgres0 is a leader after 10 seconds
|
||||||
When I issue a PATCH request to http://127.0.0.1:8008/config with {"ttl": 20, "loop_wait": 2, "synchronous_mode": true}
|
And there is a non empty initialize key in DCS after 15 seconds
|
||||||
|
When I issue a PATCH request to http://127.0.0.1:8008/config with {"ttl": 20, "synchronous_mode": true}
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
When I start postgres1
|
When I start postgres1
|
||||||
And I configure and start postgres2 with a tag replicatefrom postgres0
|
And I configure and start postgres2 with a tag replicatefrom postgres0
|
||||||
@@ -27,6 +28,46 @@ Feature: basic replication
|
|||||||
When I issue a GET request to http://127.0.0.1:8009/async
|
When I issue a GET request to http://127.0.0.1:8009/async
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
|
|
||||||
|
Scenario: check stuck sync replica
|
||||||
|
Given I issue a PATCH request to http://127.0.0.1:8008/config with {"pause": true, "maximum_lag_on_syncnode": 15000000, "postgresql": {"parameters": {"synchronous_commit": "remote_apply"}}}
|
||||||
|
Then I receive a response code 200
|
||||||
|
And I create table on postgres0
|
||||||
|
And table mytest is present on postgres1 after 2 seconds
|
||||||
|
And table mytest is present on postgres2 after 2 seconds
|
||||||
|
When I pause wal replay on postgres2
|
||||||
|
And I load data on postgres0
|
||||||
|
Then "sync" key in DCS has sync_standby=postgres1 after 15 seconds
|
||||||
|
And I resume wal replay on postgres2
|
||||||
|
And I sleep for 2 seconds
|
||||||
|
And I issue a GET request to http://127.0.0.1:8009/sync
|
||||||
|
Then I receive a response code 200
|
||||||
|
When I issue a GET request to http://127.0.0.1:8010/async
|
||||||
|
Then I receive a response code 200
|
||||||
|
When I issue a PATCH request to http://127.0.0.1:8008/config with {"pause": null, "maximum_lag_on_syncnode": -1, "postgresql": {"parameters": {"synchronous_commit": "on"}}}
|
||||||
|
Then I receive a response code 200
|
||||||
|
And I drop table on postgres0
|
||||||
|
|
||||||
|
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
|
Scenario: check the basic failover in synchronous mode
|
||||||
Given I run patronictl.py pause batman
|
Given I run patronictl.py pause batman
|
||||||
Then I receive a response returncode 0
|
Then I receive a response returncode 0
|
||||||
@@ -35,10 +76,12 @@ Feature: basic replication
|
|||||||
And I run patronictl.py resume batman
|
And I run patronictl.py resume batman
|
||||||
Then I receive a response returncode 0
|
Then I receive a response returncode 0
|
||||||
And postgres2 role is the primary after 24 seconds
|
And postgres2 role is the primary after 24 seconds
|
||||||
|
And Response on GET http://127.0.0.1:8010/history contains recovery after 10 seconds
|
||||||
When I issue a PATCH request to http://127.0.0.1:8010/config with {"synchronous_mode": null, "master_start_timeout": 0}
|
When I issue a PATCH request to http://127.0.0.1:8010/config with {"synchronous_mode": null, "master_start_timeout": 0}
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
When I add the table bar to postgres2
|
When I add the table bar to postgres2
|
||||||
Then table bar is present on postgres1 after 20 seconds
|
Then table bar is present on postgres1 after 20 seconds
|
||||||
|
And Response on GET http://127.0.0.1:8010/config contains master_start_timeout after 10 seconds
|
||||||
|
|
||||||
Scenario: check immediate failover when master_start_timeout=0
|
Scenario: check immediate failover when master_start_timeout=0
|
||||||
Given I kill postmaster on postgres2
|
Given I kill postmaster on postgres2
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
[[ "$3" == "master" ]] || exit
|
|
||||||
|
|
||||||
PGPASSWORD=zalando psql -h localhost -U postgres -p $1 -w -tAc "SELECT slot_name FROM pg_replication_slots WHERE slot_type = 'logical'" >> data/postgres0/label
|
|
||||||
Executable
+5
@@ -0,0 +1,5 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
with open("data/{0}/{0}_cb.log".format(sys.argv[1]), "a+") as log:
|
||||||
|
log.write(" ".join(sys.argv[-3:]) + "\n")
|
||||||
@@ -13,5 +13,5 @@ Scenario: make a backup and do a restore into a new cluster
|
|||||||
Given I add the table bar to postgres1
|
Given I add the table bar to postgres1
|
||||||
And I do a backup of postgres1
|
And I do a backup of postgres1
|
||||||
When I start postgres2 in a cluster batman2 from backup
|
When I start postgres2 in a cluster batman2 from backup
|
||||||
Then postgres2 is a leader of batman2 after 10 seconds
|
Then postgres2 is a leader of batman2 after 30 seconds
|
||||||
And table bar is present on postgres2 after 10 seconds
|
And table bar is present on postgres2 after 10 seconds
|
||||||
|
|||||||
+201
-93
@@ -1,12 +1,6 @@
|
|||||||
import abc
|
import abc
|
||||||
import consul
|
|
||||||
import datetime
|
import datetime
|
||||||
import etcd
|
|
||||||
import kazoo.client
|
|
||||||
import kazoo.exceptions
|
|
||||||
import os
|
import os
|
||||||
import psutil
|
|
||||||
import psycopg2
|
|
||||||
import json
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
import signal
|
import signal
|
||||||
@@ -18,6 +12,11 @@ import threading
|
|||||||
import time
|
import time
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
import patroni.psycopg as psycopg
|
||||||
|
|
||||||
|
from patroni.request import PatroniRequest
|
||||||
|
from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
|
||||||
|
|
||||||
@six.add_metaclass(abc.ABCMeta)
|
@six.add_metaclass(abc.ABCMeta)
|
||||||
class AbstractController(object):
|
class AbstractController(object):
|
||||||
@@ -140,12 +139,24 @@ class PatroniController(AbstractController):
|
|||||||
def _start(self):
|
def _start(self):
|
||||||
if self.watchdog:
|
if self.watchdog:
|
||||||
self.watchdog.start()
|
self.watchdog.start()
|
||||||
|
env = os.environ.copy()
|
||||||
if isinstance(self._context.dcs_ctl, KubernetesController):
|
if isinstance(self._context.dcs_ctl, KubernetesController):
|
||||||
self._context.dcs_ctl.create_pod(self._name[8:], self._scope)
|
self._context.dcs_ctl.create_pod(self._name[8:], self._scope)
|
||||||
os.environ['PATRONI_KUBERNETES_POD_IP'] = '10.0.0.' + self._name[-1]
|
env['PATRONI_KUBERNETES_POD_IP'] = '10.0.0.' + self._name[-1]
|
||||||
return subprocess.Popen([sys.executable, '-m', 'coverage', 'run',
|
if os.name == 'nt':
|
||||||
'--source=patroni', '-p', 'patroni.py', self._config],
|
env['BEHAVE_DEBUG'] = 'true'
|
||||||
|
patroni = subprocess.Popen([sys.executable, '-m', 'coverage', 'run',
|
||||||
|
'--source=patroni', '-p', 'patroni.py', self._config], env=env,
|
||||||
stdout=self._log, stderr=subprocess.STDOUT, cwd=self._work_directory)
|
stdout=self._log, stderr=subprocess.STDOUT, cwd=self._work_directory)
|
||||||
|
if os.name == 'nt':
|
||||||
|
patroni.terminate = self.terminate
|
||||||
|
return patroni
|
||||||
|
|
||||||
|
def terminate(self):
|
||||||
|
try:
|
||||||
|
self._context.request_executor.request('POST', self._restapi_url + '/sigterm')
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
def stop(self, kill=False, timeout=15, postgres=False):
|
def stop(self, kill=False, timeout=15, postgres=False):
|
||||||
if postgres:
|
if postgres:
|
||||||
@@ -170,17 +181,26 @@ class PatroniController(AbstractController):
|
|||||||
config = yaml.safe_load(f)
|
config = yaml.safe_load(f)
|
||||||
config.pop('etcd', None)
|
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]
|
host = config['postgresql']['listen'].split(':')[0]
|
||||||
|
|
||||||
config['postgresql']['listen'] = config['postgresql']['connect_address'] = '{0}:{1}'.format(host, self.__PORT)
|
config['postgresql']['listen'] = config['postgresql']['connect_address'] = '{0}:{1}'.format(host, self.__PORT)
|
||||||
|
|
||||||
config['name'] = name
|
config['name'] = name
|
||||||
config['postgresql']['data_dir'] = self._data_dir
|
config['postgresql']['data_dir'] = self._data_dir.replace('\\', '/')
|
||||||
config['postgresql']['use_unix_socket'] = True
|
config['postgresql']['basebackup'] = [{'checkpoint': 'fast'}]
|
||||||
|
config['postgresql']['use_unix_socket'] = os.name != 'nt' # windows doesn't yet support unix-domain sockets
|
||||||
|
config['postgresql']['use_unix_socket_repl'] = os.name != 'nt'
|
||||||
|
config['postgresql']['pgpass'] = os.path.join(tempfile.gettempdir(), 'pgpass_' + name).replace('\\', '/')
|
||||||
config['postgresql']['parameters'].update({
|
config['postgresql']['parameters'].update({
|
||||||
'logging_collector': 'on', 'log_destination': 'csvlog', 'log_directory': self._output_dir,
|
'logging_collector': 'on', 'log_destination': 'csvlog',
|
||||||
|
'log_directory': self._output_dir.replace('\\', '/'),
|
||||||
'log_filename': name + '.log', 'log_statement': 'all', 'log_min_messages': 'debug1',
|
'log_filename': name + '.log', 'log_statement': 'all', 'log_min_messages': 'debug1',
|
||||||
'unix_socket_directories': self._data_dir})
|
'unix_socket_directories': tempfile.gettempdir().replace('\\', '/')})
|
||||||
|
|
||||||
if 'bootstrap' in config:
|
if 'bootstrap' in config:
|
||||||
config['bootstrap']['post_bootstrap'] = 'psql -w -c "SELECT 1"'
|
config['bootstrap']['post_bootstrap'] = 'psql -w -c "SELECT 1"'
|
||||||
@@ -190,6 +210,8 @@ class PatroniController(AbstractController):
|
|||||||
if custom_config is not None:
|
if custom_config is not None:
|
||||||
self.recursive_update(config, custom_config)
|
self.recursive_update(config, custom_config)
|
||||||
|
|
||||||
|
self.recursive_update(config, {
|
||||||
|
'bootstrap': {'dcs': {'loop_wait': 2, 'postgresql': {'parameters': {'wal_keep_segments': 100}}}}})
|
||||||
if config['postgresql'].get('callbacks', {}).get('on_role_change'):
|
if config['postgresql'].get('callbacks', {}).get('on_role_change'):
|
||||||
config['postgresql']['callbacks']['on_role_change'] += ' ' + str(self.__PORT)
|
config['postgresql']['callbacks']['on_role_change'] += ' ' + str(self.__PORT)
|
||||||
|
|
||||||
@@ -198,16 +220,17 @@ class PatroniController(AbstractController):
|
|||||||
|
|
||||||
user = config['postgresql'].get('authentication', config['postgresql']).get('superuser', {})
|
user = config['postgresql'].get('authentication', config['postgresql']).get('superuser', {})
|
||||||
self._connkwargs = {k: user[n] for n, k in [('username', 'user'), ('password', 'password')] if n in user}
|
self._connkwargs = {k: user[n] for n, k in [('username', 'user'), ('password', 'password')] if n in user}
|
||||||
self._connkwargs.update({'host': host, 'port': self.__PORT, 'database': 'postgres'})
|
self._connkwargs.update({'host': host, 'port': self.__PORT, 'dbname': 'postgres'})
|
||||||
|
|
||||||
self._replication = config['postgresql'].get('authentication', config['postgresql']).get('replication', {})
|
self._replication = config['postgresql'].get('authentication', config['postgresql']).get('replication', {})
|
||||||
self._replication.update({'host': host, 'port': self.__PORT, 'database': 'postgres'})
|
self._replication.update({'host': host, 'port': self.__PORT, 'dbname': 'postgres'})
|
||||||
|
self._restapi_url = 'http://{0}'.format(config['restapi']['connect_address'])
|
||||||
|
|
||||||
return patroni_config_path
|
return patroni_config_path
|
||||||
|
|
||||||
def _connection(self):
|
def _connection(self):
|
||||||
if not self._conn or self._conn.closed != 0:
|
if not self._conn or self._conn.closed != 0:
|
||||||
self._conn = psycopg2.connect(**self._connkwargs)
|
self._conn = psycopg.connect(**self._connkwargs)
|
||||||
self._conn.autocommit = True
|
self._conn.autocommit = True
|
||||||
return self._conn
|
return self._conn
|
||||||
|
|
||||||
@@ -221,7 +244,7 @@ class PatroniController(AbstractController):
|
|||||||
cursor = self._cursor()
|
cursor = self._cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
return cursor
|
return cursor
|
||||||
except psycopg2.Error:
|
except psycopg.Error:
|
||||||
if not fail_ok:
|
if not fail_ok:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@@ -249,57 +272,22 @@ class PatroniController(AbstractController):
|
|||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def database_is_running(self):
|
|
||||||
pid = self._get_pid()
|
|
||||||
if not pid:
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
os.kill(pid, 0)
|
|
||||||
except OSError:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def patroni_hang(self, timeout):
|
def patroni_hang(self, timeout):
|
||||||
hang = ProcessHang(self._handle.pid, timeout)
|
hang = ProcessHang(self._handle.pid, timeout)
|
||||||
self._closables.append(hang)
|
self._closables.append(hang)
|
||||||
hang.start()
|
hang.start()
|
||||||
|
|
||||||
def checkpoint_hang(self, timeout):
|
|
||||||
pid = self._get_pid()
|
|
||||||
if not pid:
|
|
||||||
return False
|
|
||||||
proc = psutil.Process(pid)
|
|
||||||
for child in proc.children():
|
|
||||||
if 'checkpoint' in child.cmdline()[0]:
|
|
||||||
checkpointer = child
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
hang = ProcessHang(checkpointer.pid, timeout)
|
|
||||||
self._closables.append(hang)
|
|
||||||
hang.start()
|
|
||||||
return True
|
|
||||||
|
|
||||||
def cancel_background(self):
|
def cancel_background(self):
|
||||||
for obj in self._closables:
|
for obj in self._closables:
|
||||||
obj.close()
|
obj.close()
|
||||||
self._closables = []
|
self._closables = []
|
||||||
|
|
||||||
def terminate_backends(self):
|
|
||||||
pid = self._get_pid()
|
|
||||||
if not pid:
|
|
||||||
return False
|
|
||||||
proc = psutil.Process(pid)
|
|
||||||
for p in proc.children():
|
|
||||||
if 'process' not in p.cmdline()[0]:
|
|
||||||
p.terminate()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def backup_source(self):
|
def backup_source(self):
|
||||||
return 'postgres://{username}:{password}@{host}:{port}/{database}'.format(**self._replication)
|
return 'postgres://{username}:{password}@{host}:{port}/{dbname}'.format(**self._replication)
|
||||||
|
|
||||||
def backup(self, dest='data/basebackup'):
|
def backup(self, dest=os.path.join('data', 'basebackup')):
|
||||||
subprocess.call([PatroniPoolController.BACKUP_SCRIPT, '--walmethod=none',
|
subprocess.call(PatroniPoolController.BACKUP_SCRIPT + ['--walmethod=none',
|
||||||
'--datadir=' + os.path.join(self._work_directory, dest),
|
'--datadir=' + os.path.join(self._work_directory, dest),
|
||||||
'--dbname=' + self.backup_source])
|
'--dbname=' + self.backup_source])
|
||||||
|
|
||||||
@@ -375,9 +363,11 @@ class ConsulController(AbstractDcsController):
|
|||||||
super(ConsulController, self).__init__(context)
|
super(ConsulController, self).__init__(context)
|
||||||
os.environ['PATRONI_CONSUL_HOST'] = 'localhost:8500'
|
os.environ['PATRONI_CONSUL_HOST'] = 'localhost:8500'
|
||||||
os.environ['PATRONI_CONSUL_REGISTER_SERVICE'] = 'on'
|
os.environ['PATRONI_CONSUL_REGISTER_SERVICE'] = 'on'
|
||||||
self._client = consul.Consul()
|
|
||||||
self._config_file = None
|
self._config_file = None
|
||||||
|
|
||||||
|
import consul
|
||||||
|
self._client = consul.Consul()
|
||||||
|
|
||||||
def _start(self):
|
def _start(self):
|
||||||
self._config_file = self._work_directory + '.json'
|
self._config_file = self._work_directory + '.json'
|
||||||
with open(self._config_file, 'wb') as f:
|
with open(self._config_file, 'wb') as f:
|
||||||
@@ -410,26 +400,45 @@ class ConsulController(AbstractDcsController):
|
|||||||
super(ConsulController, self).start(max_wait_limit)
|
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 """
|
""" handles all etcd related tasks, used for the tests setup and cleanup """
|
||||||
|
|
||||||
def __init__(self, context):
|
def __init__(self, context, client_cls):
|
||||||
super(EtcdController, self).__init__(context)
|
super(AbstractEtcdController, self).__init__(context)
|
||||||
os.environ['PATRONI_ETCD_HOST'] = 'localhost:2379'
|
self._client_cls = client_cls
|
||||||
self._client = etcd.Client(port=2379)
|
|
||||||
|
|
||||||
def _start(self):
|
def _start(self):
|
||||||
return subprocess.Popen(["etcd", "--debug", "--data-dir", self._work_directory],
|
return subprocess.Popen(["etcd", "--data-dir", self._work_directory],
|
||||||
stdout=self._log, stderr=subprocess.STDOUT)
|
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'):
|
def query(self, key, scope='batman'):
|
||||||
|
import etcd
|
||||||
try:
|
try:
|
||||||
return self._client.get(self.path(key, scope)).value
|
return self._client.get(self.path(key, scope)).value
|
||||||
except etcd.EtcdKeyNotFound:
|
except etcd.EtcdKeyNotFound:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def cleanup_service_tree(self):
|
def cleanup_service_tree(self):
|
||||||
|
import etcd
|
||||||
try:
|
try:
|
||||||
self._client.delete(self.path(scope=''), recursive=True)
|
self._client.delete(self.path(scope=''), recursive=True)
|
||||||
except (etcd.EtcdKeyNotFound, etcd.EtcdConnectionFailed):
|
except (etcd.EtcdKeyNotFound, etcd.EtcdConnectionFailed):
|
||||||
@@ -437,12 +446,25 @@ class EtcdController(AbstractDcsController):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
assert False, "exception when cleaning up etcd contents: {0}".format(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:
|
try:
|
||||||
return bool(self._client.machines)
|
self._client.deleteprefix(self.path(scope=''))
|
||||||
except Exception:
|
except Exception as e:
|
||||||
return False
|
assert False, "exception when cleaning up etcd contents: {0}".format(e)
|
||||||
|
|
||||||
|
|
||||||
class KubernetesController(AbstractDcsController):
|
class KubernetesController(AbstractDcsController):
|
||||||
@@ -454,8 +476,9 @@ class KubernetesController(AbstractDcsController):
|
|||||||
self._label_selector = ','.join('{0}={1}'.format(k, v) for k, v in self._labels.items())
|
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_LABELS'] = json.dumps(self._labels)
|
||||||
os.environ['PATRONI_KUBERNETES_USE_ENDPOINTS'] = 'true'
|
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')
|
k8s_config.load_kube_config(context='local')
|
||||||
self._client = k8s_client
|
self._client = k8s_client
|
||||||
self._api = self._client.CoreV1Api()
|
self._api = self._client.CoreV1Api()
|
||||||
@@ -473,13 +496,13 @@ class KubernetesController(AbstractDcsController):
|
|||||||
|
|
||||||
def delete_pod(self, name):
|
def delete_pod(self, name):
|
||||||
try:
|
try:
|
||||||
self._api.delete_namespaced_pod(name, self._namespace, self._client.V1DeleteOptions())
|
self._api.delete_namespaced_pod(name, self._namespace, body=self._client.V1DeleteOptions())
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
self._api.read_namespaced_pod(name, self._namespace)
|
self._api.read_namespaced_pod(name, self._namespace)
|
||||||
except:
|
except Exception:
|
||||||
break
|
break
|
||||||
|
|
||||||
def query(self, key, scope='batman'):
|
def query(self, key, scope='batman'):
|
||||||
@@ -488,22 +511,23 @@ class KubernetesController(AbstractDcsController):
|
|||||||
return (pod.metadata.annotations or {}).get('status', '')
|
return (pod.metadata.annotations or {}).get('status', '')
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
e = self._api.read_namespaced_endpoints(scope + ('' if key == 'leader' else '-' + key), self._namespace)
|
ep = scope + {'leader': '', 'history': '-config', 'initialize': '-config'}.get(key, '-' + key)
|
||||||
if key == 'leader':
|
e = self._api.read_namespaced_endpoints(ep, self._namespace)
|
||||||
|
if key != 'sync':
|
||||||
return e.metadata.annotations[key]
|
return e.metadata.annotations[key]
|
||||||
else:
|
else:
|
||||||
return json.dumps(e.metadata.annotations)
|
return json.dumps(e.metadata.annotations)
|
||||||
except:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def cleanup_service_tree(self):
|
def cleanup_service_tree(self):
|
||||||
try:
|
try:
|
||||||
self._api.delete_collection_namespaced_pod(self._namespace, label_selector=self._label_selector)
|
self._api.delete_collection_namespaced_pod(self._namespace, label_selector=self._label_selector)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
self._api.delete_collection_namespaced_endpoints(self._namespace, label_selector=self._label_selector)
|
self._api.delete_collection_namespaced_endpoints(self._namespace, label_selector=self._label_selector)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@@ -523,18 +547,22 @@ class ZooKeeperController(AbstractDcsController):
|
|||||||
super(ZooKeeperController, self).__init__(context, False)
|
super(ZooKeeperController, self).__init__(context, False)
|
||||||
if export_env:
|
if export_env:
|
||||||
os.environ['PATRONI_ZOOKEEPER_HOSTS'] = "'localhost:2181'"
|
os.environ['PATRONI_ZOOKEEPER_HOSTS'] = "'localhost:2181'"
|
||||||
|
|
||||||
|
import kazoo.client
|
||||||
self._client = kazoo.client.KazooClient()
|
self._client = kazoo.client.KazooClient()
|
||||||
|
|
||||||
def _start(self):
|
def _start(self):
|
||||||
pass # TODO: implement later
|
pass # TODO: implement later
|
||||||
|
|
||||||
def query(self, key, scope='batman'):
|
def query(self, key, scope='batman'):
|
||||||
|
import kazoo.exceptions
|
||||||
try:
|
try:
|
||||||
return self._client.get(self.path(key, scope))[0].decode('utf-8')
|
return self._client.get(self.path(key, scope))[0].decode('utf-8')
|
||||||
except kazoo.exceptions.NoNodeError:
|
except kazoo.exceptions.NoNodeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def cleanup_service_tree(self):
|
def cleanup_service_tree(self):
|
||||||
|
import kazoo.exceptions
|
||||||
try:
|
try:
|
||||||
self._client.delete(self.path(scope=''), recursive=True)
|
self._client.delete(self.path(scope=''), recursive=True)
|
||||||
except (kazoo.exceptions.NoNodeError):
|
except (kazoo.exceptions.NoNodeError):
|
||||||
@@ -552,16 +580,78 @@ class ZooKeeperController(AbstractDcsController):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class MockExhibitor(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(b'{"servers":["127.0.0.1"],"port":2181}')
|
||||||
|
|
||||||
|
def log_message(self, fmt, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ExhibitorController(ZooKeeperController):
|
class ExhibitorController(ZooKeeperController):
|
||||||
|
|
||||||
def __init__(self, context):
|
def __init__(self, context):
|
||||||
super(ExhibitorController, self).__init__(context, False)
|
super(ExhibitorController, self).__init__(context, False)
|
||||||
os.environ.update({'PATRONI_EXHIBITOR_HOSTS': 'localhost', 'PATRONI_EXHIBITOR_PORT': '8181'})
|
port = 8181
|
||||||
|
exhibitor = HTTPServer(('', port), MockExhibitor)
|
||||||
|
exhibitor.daemon_thread = True
|
||||||
|
exhibitor_thread = threading.Thread(target=exhibitor.serve_forever)
|
||||||
|
exhibitor_thread.daemon = True
|
||||||
|
exhibitor_thread.start()
|
||||||
|
os.environ.update({'PATRONI_EXHIBITOR_HOSTS': 'localhost', 'PATRONI_EXHIBITOR_PORT': str(port)})
|
||||||
|
|
||||||
|
|
||||||
|
class RaftController(AbstractDcsController):
|
||||||
|
|
||||||
|
CONTROLLER_ADDR = 'localhost:1234'
|
||||||
|
PASSWORD = '12345'
|
||||||
|
|
||||||
|
def __init__(self, context):
|
||||||
|
super(RaftController, self).__init__(context)
|
||||||
|
os.environ.update(PATRONI_RAFT_PARTNER_ADDRS="'" + self.CONTROLLER_ADDR + "'",
|
||||||
|
PATRONI_RAFT_PASSWORD=self.PASSWORD, 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
|
||||||
|
|
||||||
|
if self._raft:
|
||||||
|
self._raft.destroy()
|
||||||
|
self.stop()
|
||||||
|
os.makedirs(self._work_directory)
|
||||||
|
self.start()
|
||||||
|
|
||||||
|
ready_event = threading.Event()
|
||||||
|
self._raft = KVStoreTTL(ready_event.set, None, None,
|
||||||
|
partner_addrs=[self.CONTROLLER_ADDR], password=self.PASSWORD)
|
||||||
|
self._raft.startAutoTick()
|
||||||
|
ready_event.wait()
|
||||||
|
|
||||||
|
|
||||||
class PatroniPoolController(object):
|
class PatroniPoolController(object):
|
||||||
|
|
||||||
BACKUP_SCRIPT = 'features/backup_create.sh'
|
PYTHON = sys.executable.replace('\\', '/')
|
||||||
|
BACKUP_SCRIPT = [PYTHON, 'features/backup_create.py']
|
||||||
|
ARCHIVE_RESTORE_SCRIPT = ' '.join((PYTHON, os.path.abspath('features/archive-restore.py')))
|
||||||
|
|
||||||
def __init__(self, context):
|
def __init__(self, context):
|
||||||
self._context = context
|
self._context = context
|
||||||
@@ -587,16 +677,15 @@ class PatroniPoolController(object):
|
|||||||
def output_dir(self):
|
def output_dir(self):
|
||||||
return self._output_dir
|
return self._output_dir
|
||||||
|
|
||||||
def start(self, name, max_wait_limit=20, custom_config=None):
|
def start(self, name, max_wait_limit=40, custom_config=None):
|
||||||
if name not in self._processes:
|
if name not in self._processes:
|
||||||
self._processes[name] = PatroniController(self._context, name, self.patroni_path,
|
self._processes[name] = PatroniController(self._context, name, self.patroni_path,
|
||||||
self._output_dir, custom_config)
|
self._output_dir, custom_config)
|
||||||
self._processes[name].start(max_wait_limit)
|
self._processes[name].start(max_wait_limit)
|
||||||
|
|
||||||
def __getattr__(self, func):
|
def __getattr__(self, func):
|
||||||
if func not in ['stop', 'query', 'write_label', 'read_label', 'check_role_has_changed_to', 'add_tag_to_config',
|
if func not in ['stop', 'query', 'write_label', 'read_label', 'check_role_has_changed_to',
|
||||||
'get_watchdog', 'database_is_running', 'checkpoint_hang', 'patroni_hang',
|
'add_tag_to_config', 'get_watchdog', 'patroni_hang', 'backup']:
|
||||||
'terminate_backends', 'backup']:
|
|
||||||
raise AttributeError("PatroniPoolController instance has no attribute '{0}'".format(func))
|
raise AttributeError("PatroniPoolController instance has no attribute '{0}'".format(func))
|
||||||
|
|
||||||
def wrapper(name, *args, **kwargs):
|
def wrapper(name, *args, **kwargs):
|
||||||
@@ -610,7 +699,7 @@ class PatroniPoolController(object):
|
|||||||
self._processes.clear()
|
self._processes.clear()
|
||||||
|
|
||||||
def create_and_set_output_directory(self, feature_name):
|
def create_and_set_output_directory(self, feature_name):
|
||||||
feature_dir = os.path.join(self.patroni_path, 'features/output', feature_name.replace(' ', '_'))
|
feature_dir = os.path.join(self.patroni_path, 'features', 'output', feature_name.replace(' ', '_'))
|
||||||
if os.path.exists(feature_dir):
|
if os.path.exists(feature_dir):
|
||||||
shutil.rmtree(feature_dir)
|
shutil.rmtree(feature_dir)
|
||||||
os.makedirs(feature_dir)
|
os.makedirs(feature_dir)
|
||||||
@@ -623,7 +712,7 @@ class PatroniPoolController(object):
|
|||||||
'bootstrap': {
|
'bootstrap': {
|
||||||
'method': 'pg_basebackup',
|
'method': 'pg_basebackup',
|
||||||
'pg_basebackup': {
|
'pg_basebackup': {
|
||||||
'command': self.BACKUP_SCRIPT + ' --walmethod=stream --dbname=' + f.backup_source
|
'command': " ".join(self.BACKUP_SCRIPT) + ' --walmethod=stream --dbname=' + f.backup_source
|
||||||
},
|
},
|
||||||
'dcs': {
|
'dcs': {
|
||||||
'postgresql': {
|
'postgresql': {
|
||||||
@@ -636,8 +725,9 @@ class PatroniPoolController(object):
|
|||||||
'postgresql': {
|
'postgresql': {
|
||||||
'parameters': {
|
'parameters': {
|
||||||
'archive_mode': 'on',
|
'archive_mode': 'on',
|
||||||
'archive_command': 'mkdir -p {0} && test ! -f {0}/%f && cp %p {0}/%f'.format(
|
'archive_command': (self.ARCHIVE_RESTORE_SCRIPT + ' --mode archive ' +
|
||||||
os.path.join(self.patroni_path, 'data/wal_archive'))
|
'--dirname {} --filename %f --pathname %p').format(
|
||||||
|
os.path.join(self.patroni_path, 'data', 'wal_archive').replace('\\', '/'))
|
||||||
},
|
},
|
||||||
'authentication': {
|
'authentication': {
|
||||||
'superuser': {'password': 'zalando1'},
|
'superuser': {'password': 'zalando1'},
|
||||||
@@ -653,12 +743,14 @@ class PatroniPoolController(object):
|
|||||||
'bootstrap': {
|
'bootstrap': {
|
||||||
'method': 'backup_restore',
|
'method': 'backup_restore',
|
||||||
'backup_restore': {
|
'backup_restore': {
|
||||||
'command': 'features/backup_restore.sh --sourcedir=' + os.path.join(self.patroni_path,
|
'command': (self.PYTHON + ' features/backup_restore.py --sourcedir=' +
|
||||||
'data/basebackup'),
|
os.path.join(self.patroni_path, 'data', 'basebackup').replace('\\', '/')),
|
||||||
'recovery_conf': {
|
'recovery_conf': {
|
||||||
'recovery_target_action': 'promote',
|
'recovery_target_action': 'promote',
|
||||||
'recovery_target_timeline': 'latest',
|
'recovery_target_timeline': 'latest',
|
||||||
'restore_command': 'cp {0}/data/wal_archive/%f %p'.format(self.patroni_path)
|
'restore_command': (self.ARCHIVE_RESTORE_SCRIPT + ' --mode restore ' +
|
||||||
|
'--dirname {} --filename %f --pathname %p').format(
|
||||||
|
os.path.join(self.patroni_path, 'data', 'wal_archive').replace('\\', '/'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -795,11 +887,14 @@ class WatchdogMonitor(object):
|
|||||||
return triggered
|
return triggered
|
||||||
|
|
||||||
|
|
||||||
# actions to execute on start/stop of the tests and before running invidual features
|
# actions to execute on start/stop of the tests and before running individual features
|
||||||
def before_all(context):
|
def before_all(context):
|
||||||
os.environ.update({'PATRONI_RESTAPI_USERNAME': 'username', 'PATRONI_RESTAPI_PASSWORD': 'password'})
|
os.environ.update({'PATRONI_RESTAPI_USERNAME': 'username', 'PATRONI_RESTAPI_PASSWORD': 'password'})
|
||||||
context.ci = 'TRAVIS_BUILD_NUMBER' in os.environ or 'BUILD_NUMBER' in os.environ
|
context.request_executor = PatroniRequest({'ctl': {'auth': os.environ['PATRONI_RESTAPI_USERNAME'] +
|
||||||
context.timeout_multiplier = 2 if context.ci else 1
|
':' + os.environ['PATRONI_RESTAPI_PASSWORD']}})
|
||||||
|
context.ci = os.name == 'nt' or\
|
||||||
|
any(a in os.environ for a in ('TRAVIS_BUILD_NUMBER', 'BUILD_NUMBER', 'GITHUB_ACTIONS'))
|
||||||
|
context.timeout_multiplier = 5 if context.ci else 1 # MacOS sometimes is VERY slow
|
||||||
context.pctl = PatroniPoolController(context)
|
context.pctl = PatroniPoolController(context)
|
||||||
context.dcs_ctl = context.pctl.known_dcs[context.pctl.dcs](context)
|
context.dcs_ctl = context.pctl.known_dcs[context.pctl.dcs](context)
|
||||||
context.dcs_ctl.start()
|
context.dcs_ctl.start()
|
||||||
@@ -818,13 +913,26 @@ def after_all(context):
|
|||||||
|
|
||||||
def before_feature(context, feature):
|
def before_feature(context, feature):
|
||||||
""" create per-feature output directory to collect Patroni and PostgreSQL logs """
|
""" create per-feature output directory to collect Patroni and PostgreSQL logs """
|
||||||
|
if feature.name == 'watchdog' and os.name == 'nt':
|
||||||
|
feature.skip("Watchdog isn't supported on Windows")
|
||||||
|
else:
|
||||||
context.pctl.create_and_set_output_directory(feature.name)
|
context.pctl.create_and_set_output_directory(feature.name)
|
||||||
|
|
||||||
|
|
||||||
def after_feature(context, feature):
|
def after_feature(context, feature):
|
||||||
""" stop all Patronis, remove their data directory and cleanup the keys in etcd """
|
""" stop all Patronis, remove their data directory and cleanup the keys in etcd """
|
||||||
context.pctl.stop_all()
|
context.pctl.stop_all()
|
||||||
shutil.rmtree(os.path.join(context.pctl.patroni_path, 'data'))
|
data = os.path.join(context.pctl.patroni_path, 'data')
|
||||||
|
if os.path.exists(data):
|
||||||
|
shutil.rmtree(data)
|
||||||
context.dcs_ctl.cleanup_service_tree()
|
context.dcs_ctl.cleanup_service_tree()
|
||||||
if feature.status == 'failed':
|
if feature.status == 'failed':
|
||||||
shutil.copytree(context.pctl.output_dir, context.pctl.output_dir + '_failed')
|
shutil.copytree(context.pctl.output_dir, context.pctl.output_dir + '_failed')
|
||||||
|
|
||||||
|
|
||||||
|
def before_scenario(context, scenario):
|
||||||
|
if 'slot-advance' in scenario.effective_tags:
|
||||||
|
for p in context.pctl._processes.values():
|
||||||
|
if p._conn and p._conn.server_version < 110000:
|
||||||
|
scenario.skip('pg_replication_slot_advance() is not supported on {0}'.format(p._conn.server_version))
|
||||||
|
break
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
Feature: ignored slots
|
||||||
|
Scenario: check ignored slots aren't removed on failover/switchover
|
||||||
|
Given I start postgres1
|
||||||
|
Then postgres1 is a leader after 10 seconds
|
||||||
|
And there is a non empty initialize key in DCS after 15 seconds
|
||||||
|
When I issue a PATCH request to http://127.0.0.1:8009/config with {"ignore_slots": [{"name": "unmanaged_slot_0", "database": "postgres", "plugin": "test_decoding", "type": "logical"}, {"name": "unmanaged_slot_1", "database": "postgres", "plugin": "test_decoding"}, {"name": "unmanaged_slot_2", "database": "postgres"}, {"name": "unmanaged_slot_3"}], "postgresql": {"parameters": {"wal_level": "logical"}}}
|
||||||
|
Then I receive a response code 200
|
||||||
|
And Response on GET http://127.0.0.1:8009/config contains ignore_slots after 10 seconds
|
||||||
|
# Make sure the wal_level has been changed.
|
||||||
|
When I shut down postgres1
|
||||||
|
And I start postgres1
|
||||||
|
Then postgres1 is a leader after 10 seconds
|
||||||
|
And "members/postgres1" key in DCS has role=master after 3 seconds
|
||||||
|
# Make sure Patroni has finished telling Postgres it should be accepting writes.
|
||||||
|
And postgres1 role is the primary after 20 seconds
|
||||||
|
# 1. Create our test logical replication slot.
|
||||||
|
# Test that ny subset of attributes in the ignore slots matcher is enough to match a slot
|
||||||
|
# by using 3 different slots.
|
||||||
|
When I create a logical replication slot unmanaged_slot_0 on postgres1 with the test_decoding plugin
|
||||||
|
And I create a logical replication slot unmanaged_slot_1 on postgres1 with the test_decoding plugin
|
||||||
|
And I create a logical replication slot unmanaged_slot_2 on postgres1 with the test_decoding plugin
|
||||||
|
And I create a logical replication slot unmanaged_slot_3 on postgres1 with the test_decoding plugin
|
||||||
|
And I create a logical replication slot dummy_slot on postgres1 with the test_decoding plugin
|
||||||
|
# It seems like it'd be obvious that these slots exist since we just created them,
|
||||||
|
# but Patroni can actually end up dropping them almost immediately, so it's helpful
|
||||||
|
# to verify they exist before we begin testing whether they persist through failover
|
||||||
|
# cycles.
|
||||||
|
Then postgres1 has a logical replication slot named unmanaged_slot_0 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_1 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_2 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_3 with the test_decoding plugin
|
||||||
|
|
||||||
|
When I start postgres0
|
||||||
|
Then "members/postgres0" key in DCS has role=replica after 3 seconds
|
||||||
|
And postgres0 role is the secondary after 20 seconds
|
||||||
|
# Verify that the replica has advanced beyond the point in the WAL
|
||||||
|
# where we created the replication slot so that on the next failover
|
||||||
|
# cycle we don't accidentally rewind to before the slot creation.
|
||||||
|
And replication works from postgres1 to postgres0 after 20 seconds
|
||||||
|
When I shut down postgres1
|
||||||
|
Then "members/postgres0" key in DCS has role=master after 3 seconds
|
||||||
|
|
||||||
|
# 2. After a failover the server (now a replica) still has the slot.
|
||||||
|
When I start postgres1
|
||||||
|
Then postgres1 role is the secondary after 20 seconds
|
||||||
|
And "members/postgres1" key in DCS has role=replica after 3 seconds
|
||||||
|
# give Patroni time to sync replication slots
|
||||||
|
And I sleep for 2 seconds
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_0 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_1 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_2 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_3 with the test_decoding plugin
|
||||||
|
And postgres1 does not have a logical replication slot named dummy_slot
|
||||||
|
|
||||||
|
# 3. After a failover the server (now a master) still has the slot.
|
||||||
|
When I shut down postgres0
|
||||||
|
Then "members/postgres1" key in DCS has role=master after 3 seconds
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_0 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_1 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_2 with the test_decoding plugin
|
||||||
|
And postgres1 has a logical replication slot named unmanaged_slot_3 with the test_decoding plugin
|
||||||
@@ -8,6 +8,8 @@ Scenario: check API requests on a stand-alone server
|
|||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And I receive a response state running
|
And I receive a response state running
|
||||||
And I receive a response role master
|
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
|
When I issue a GET request to http://127.0.0.1:8008/health
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
When I issue a GET request to http://127.0.0.1:8008/replica
|
When I issue a GET request to http://127.0.0.1:8008/replica
|
||||||
@@ -28,30 +30,48 @@ Scenario: check API requests on a stand-alone server
|
|||||||
And I receive a response text "Failover could be performed only to a specific candidate"
|
And I receive a response text "Failover could be performed only to a specific candidate"
|
||||||
|
|
||||||
Scenario: check local configuration reload
|
Scenario: check local configuration reload
|
||||||
Given I issue an empty POST request to http://127.0.0.1:8008/reload
|
Given I add tag new_tag new_value to postgres0 config
|
||||||
Then I receive a response code 200
|
|
||||||
And I receive a response text nothing changed
|
|
||||||
When I add tag new_tag new_value to postgres0 config
|
|
||||||
And I issue an empty POST request to http://127.0.0.1:8008/reload
|
And I issue an empty POST request to http://127.0.0.1:8008/reload
|
||||||
Then I receive a response code 202
|
Then I receive a response code 202
|
||||||
|
|
||||||
Scenario: check dynamic configuration change via DCS
|
Scenario: check dynamic configuration change via DCS
|
||||||
Given I run patronictl.py edit-config -s 'ttl=10' -s 'loop_wait=2' -p 'max_connections=101' --force batman
|
Given I run patronictl.py edit-config -s 'ttl=10' -p 'max_connections=101' --force batman
|
||||||
Then I receive a response returncode 0
|
Then I receive a response returncode 0
|
||||||
And I receive a response output "+loop_wait: 2"
|
And I receive a response output "+ttl: 10"
|
||||||
And Response on GET http://127.0.0.1:8008/patroni contains pending_restart after 11 seconds
|
And Response on GET http://127.0.0.1:8008/patroni contains pending_restart after 11 seconds
|
||||||
When I issue a GET request to http://127.0.0.1:8008/config
|
When I issue a GET request to http://127.0.0.1:8008/config
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And I receive a response loop_wait 2
|
And I receive a response ttl 10
|
||||||
When I issue a GET request to http://127.0.0.1:8008/patroni
|
When I issue a GET request to http://127.0.0.1:8008/patroni
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And I receive a response tags {'new_tag': 'new_value'}
|
And I receive a response tags {'new_tag': 'new_value'}
|
||||||
|
And I sleep for 4 seconds
|
||||||
|
|
||||||
|
Scenario: check the scheduled restart
|
||||||
|
Given I issue a PATCH request to http://127.0.0.1:8008/config with {"postgresql": {"parameters": {"superuser_reserved_connections": "6"}}}
|
||||||
|
Then I receive a response code 200
|
||||||
|
And Response on GET http://127.0.0.1:8008/patroni contains pending_restart after 5 seconds
|
||||||
|
Given I issue a scheduled restart at http://127.0.0.1:8008 in 5 seconds with {"role": "replica"}
|
||||||
|
Then I receive a response code 202
|
||||||
|
And I sleep for 8 seconds
|
||||||
|
And Response on GET http://127.0.0.1:8008/patroni contains pending_restart after 10 seconds
|
||||||
|
Given I issue a scheduled restart at http://127.0.0.1:8008 in 5 seconds with {"restart_pending": "True"}
|
||||||
|
Then I receive a response code 202
|
||||||
|
And Response on GET http://127.0.0.1:8008/patroni does not contain pending_restart after 10 seconds
|
||||||
|
And postgres0 role is the primary after 10 seconds
|
||||||
|
|
||||||
Scenario: check API requests for the primary-replica pair in the pause mode
|
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 I receive a response returncode 0
|
|
||||||
When I start postgres1
|
|
||||||
Then replication works from postgres0 to postgres1 after 20 seconds
|
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
|
||||||
|
Then replication works from postgres0 to postgres1 after 20 seconds
|
||||||
|
And I sleep for 2 seconds
|
||||||
When I issue a GET request to http://127.0.0.1:8009/replica
|
When I issue a GET request to http://127.0.0.1:8009/replica
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And I receive a response state running
|
And I receive a response state running
|
||||||
@@ -73,6 +93,7 @@ Scenario: check the switchover via the API in the pause mode
|
|||||||
And postgres1 role is the primary after 10 seconds
|
And postgres1 role is the primary after 10 seconds
|
||||||
And postgres0 role is the secondary after 10 seconds
|
And postgres0 role is the secondary after 10 seconds
|
||||||
And replication works from postgres1 to postgres0 after 20 seconds
|
And replication works from postgres1 to postgres0 after 20 seconds
|
||||||
|
And "members/postgres0" key in DCS has state=running after 10 seconds
|
||||||
When I issue a GET request to http://127.0.0.1:8008/master
|
When I issue a GET request to http://127.0.0.1:8008/master
|
||||||
Then I receive a response code 503
|
Then I receive a response code 503
|
||||||
When I issue a GET request to http://127.0.0.1:8008/replica
|
When I issue a GET request to http://127.0.0.1:8008/replica
|
||||||
@@ -83,17 +104,18 @@ Scenario: check the switchover via the API in the pause mode
|
|||||||
Then I receive a response code 503
|
Then I receive a response code 503
|
||||||
|
|
||||||
Scenario: check the scheduled switchover
|
Scenario: check the scheduled switchover
|
||||||
Given I issue a scheduled switchover from postgres1 to postgres0 in 3 seconds
|
Given I issue a scheduled switchover from postgres1 to postgres0 in 10 seconds
|
||||||
Then I receive a response returncode 1
|
Then I receive a response returncode 1
|
||||||
And I receive a response output "Can't schedule switchover in the paused state"
|
And I receive a response output "Can't schedule switchover in the paused state"
|
||||||
When I run patronictl.py resume batman
|
When I run patronictl.py resume batman
|
||||||
Then I receive a response returncode 0
|
Then I receive a response returncode 0
|
||||||
Given I issue a scheduled switchover from postgres1 to postgres0 in 3 seconds
|
Given I issue a scheduled switchover from postgres1 to postgres0 in 10 seconds
|
||||||
Then I receive a response returncode 0
|
Then I receive a response returncode 0
|
||||||
And postgres0 is a leader after 20 seconds
|
And postgres0 is a leader after 20 seconds
|
||||||
And postgres0 role is the primary after 10 seconds
|
And postgres0 role is the primary after 10 seconds
|
||||||
And postgres1 role is the secondary after 10 seconds
|
And postgres1 role is the secondary after 10 seconds
|
||||||
And replication works from postgres0 to postgres1 after 25 seconds
|
And replication works from postgres0 to postgres1 after 25 seconds
|
||||||
|
And "members/postgres1" key in DCS has state=running after 10 seconds
|
||||||
When I issue a GET request to http://127.0.0.1:8008/master
|
When I issue a GET request to http://127.0.0.1:8008/master
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
When I issue a GET request to http://127.0.0.1:8008/replica
|
When I issue a GET request to http://127.0.0.1:8008/replica
|
||||||
@@ -102,16 +124,3 @@ Scenario: check the scheduled switchover
|
|||||||
Then I receive a response code 503
|
Then I receive a response code 503
|
||||||
When I issue a GET request to http://127.0.0.1:8009/replica
|
When I issue a GET request to http://127.0.0.1:8009/replica
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
|
|
||||||
Scenario: check the scheduled restart
|
|
||||||
Given I issue a PATCH request to http://127.0.0.1:8008/config with {"postgresql": {"parameters": {"superuser_reserved_connections": "6"}}}
|
|
||||||
Then I receive a response code 200
|
|
||||||
And Response on GET http://127.0.0.1:8008/patroni contains pending_restart after 5 seconds
|
|
||||||
Given I issue a scheduled restart at http://127.0.0.1:8008 in 3 seconds with {"role": "replica"}
|
|
||||||
Then I receive a response code 202
|
|
||||||
And I sleep for 4 seconds
|
|
||||||
And Response on GET http://127.0.0.1:8008/patroni contains pending_restart after 10 seconds
|
|
||||||
Given I issue a scheduled restart at http://127.0.0.1:8008 in 3 seconds with {"restart_pending": "True"}
|
|
||||||
Then I receive a response code 202
|
|
||||||
And Response on GET http://127.0.0.1:8008/patroni does not contain pending_restart after 10 seconds
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,46 +1,58 @@
|
|||||||
Feature: standby cluster
|
Feature: standby cluster
|
||||||
Scenario: check permanent logical slots are preserved on failover/switchover
|
Scenario: prepare the cluster with logical slots
|
||||||
Given I start postgres1
|
Given I start postgres1
|
||||||
Then postgres1 is a leader after 10 seconds
|
Then postgres1 is a leader after 10 seconds
|
||||||
And I sleep for 3 seconds
|
And there is a non empty initialize key in DCS after 15 seconds
|
||||||
When I issue a PATCH request to http://127.0.0.1:8009/config with {"loop_wait": 2, "slots": {"pm_1": {"type": "physical"}}, "postgresql": {"parameters": {"wal_level": "logical"}}}
|
When I issue a PATCH request to http://127.0.0.1:8009/config with {"slots": {"pm_1": {"type": "physical"}}, "postgresql": {"parameters": {"wal_level": "logical"}}}
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And Response on GET http://127.0.0.1:8009/config contains slots after 10 seconds
|
And Response on GET http://127.0.0.1:8009/config contains slots after 10 seconds
|
||||||
And I sleep for 3 seconds
|
And I sleep for 3 seconds
|
||||||
When I issue a PATCH request to http://127.0.0.1:8009/config with {"slots": {"test_logical": {"type": "logical", "database": "postgres", "plugin": "test_decoding"}}}
|
When I issue a PATCH request to http://127.0.0.1:8009/config with {"slots": {"test_logical": {"type": "logical", "database": "postgres", "plugin": "test_decoding"}}}
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And I do a backup of postgres1
|
And I do a backup of postgres1
|
||||||
When I start postgres0 with callback configured
|
When I start postgres0
|
||||||
Then "members/postgres0" key in DCS has state=running after 10 seconds
|
Then "members/postgres0" key in DCS has state=running after 10 seconds
|
||||||
And replication works from postgres1 to postgres0 after 15 seconds
|
And replication works from postgres1 to postgres0 after 15 seconds
|
||||||
|
|
||||||
|
@slot-advance
|
||||||
|
Scenario: check permanent logical slots are synced to the replica
|
||||||
|
Given I run patronictl.py restart batman postgres1 --force
|
||||||
|
Then Logical slot test_logical is in sync between postgres0 and postgres1 after 10 seconds
|
||||||
|
When I add the table replicate_me to postgres1
|
||||||
|
And I get all changes from logical slot test_logical on postgres1
|
||||||
|
Then Logical slot test_logical is in sync between postgres0 and postgres1 after 10 seconds
|
||||||
|
|
||||||
|
Scenario: Detach exiting node from the cluster
|
||||||
When I shut down postgres1
|
When I shut down postgres1
|
||||||
Then postgres0 is a leader after 10 seconds
|
Then postgres0 is a leader after 10 seconds
|
||||||
And "members/postgres0" key in DCS has role=master after 3 seconds
|
And "members/postgres0" key in DCS has role=master after 3 seconds
|
||||||
When I issue a GET request to http://127.0.0.1:8008/
|
When I issue a GET request to http://127.0.0.1:8008/
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And there is a label with "test_logical" in postgres0 data directory
|
|
||||||
|
|
||||||
Scenario: check replication of a single table in a standby cluster
|
Scenario: check replication of a single table in a standby cluster
|
||||||
Given I start postgres1 in a standby cluster batman1 as a clone of postgres0
|
Given I start postgres1 in a standby cluster batman1 as a clone of postgres0
|
||||||
Then postgres1 is a leader of batman1 after 10 seconds
|
Then postgres1 is a leader of batman1 after 10 seconds
|
||||||
When I add the table foo to postgres0
|
When I add the table foo to postgres0
|
||||||
Then table foo is present on postgres1 after 20 seconds
|
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
|
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
|
When I issue a GET request to http://127.0.0.1:8009/standby_leader
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And I receive a response role standby_leader
|
And I receive a response role standby_leader
|
||||||
And there is a postgres1_cb.log with "on_start replica batman1\non_role_change standby_leader batman1" in postgres1 data directory
|
And there is a postgres1_cb.log with "on_role_change standby_leader batman1" in postgres1 data directory
|
||||||
When I start postgres2 in a cluster batman1
|
When I start postgres2 in a cluster batman1
|
||||||
Then postgres2 role is the replica after 24 seconds
|
Then postgres2 role is the replica after 24 seconds
|
||||||
And table foo is present on postgres2 after 20 seconds
|
And table foo is present on postgres2 after 20 seconds
|
||||||
|
And postgres1 does not have a logical replication slot named test_logical
|
||||||
|
|
||||||
Scenario: check failover
|
Scenario: check failover
|
||||||
When I kill postgres1
|
When I kill postgres1
|
||||||
And I kill postmaster on postgres1
|
And I kill postmaster on postgres1
|
||||||
Then postgres2 is replicating from postgres0 after 32 seconds
|
Then postgres2 is replicating from postgres0 after 32 seconds
|
||||||
When I issue a GET request to http://127.0.0.1:8010/master
|
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
|
When I issue a GET request to http://127.0.0.1:8010/standby_leader
|
||||||
Then I receive a response code 200
|
Then I receive a response code 200
|
||||||
And I receive a response role standby_leader
|
And I receive a response role standby_leader
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import psycopg2 as pg
|
import patroni.psycopg as pg
|
||||||
|
|
||||||
from behave import step, then
|
from behave import step, then
|
||||||
from time import sleep, time
|
from time import sleep, time
|
||||||
@@ -33,6 +33,37 @@ def add_table(context, table_name, pg_name):
|
|||||||
assert False, "Error creating table {0} on {1}: {2}".format(table_name, pg_name, e)
|
assert False, "Error creating table {0} on {1}: {2}".format(table_name, pg_name, e)
|
||||||
|
|
||||||
|
|
||||||
|
@step('I {action:w} wal replay on {pg_name:w}')
|
||||||
|
def toggle_wal_replay(context, action, pg_name):
|
||||||
|
# pause or resume the wal replay process
|
||||||
|
try:
|
||||||
|
version = context.pctl.query(pg_name, "select pg_catalog.pg_read_file('PG_VERSION', 0, 2)").fetchone()
|
||||||
|
wal = version and version[0] and int(version[0].split('.')[0]) < 10 and "xlog" or "wal"
|
||||||
|
context.pctl.query(pg_name, "SELECT pg_{0}_replay_{1}()".format(wal, action))
|
||||||
|
except pg.Error as e:
|
||||||
|
assert False, "Error during {0} wal recovery on {1}: {2}".format(action, pg_name, e)
|
||||||
|
|
||||||
|
|
||||||
|
@step('I {action:w} table on {pg_name:w}')
|
||||||
|
def crdr_mytest(context, action, pg_name):
|
||||||
|
try:
|
||||||
|
if (action == "create"):
|
||||||
|
context.pctl.query(pg_name, "create table if not exists mytest(id Numeric)")
|
||||||
|
else:
|
||||||
|
context.pctl.query(pg_name, "drop table if exists mytest")
|
||||||
|
except pg.Error as e:
|
||||||
|
assert False, "Error {0} table mytest on {1}: {2}".format(action, pg_name, e)
|
||||||
|
|
||||||
|
|
||||||
|
@step('I load data on {pg_name:w}')
|
||||||
|
def initiate_load(context, pg_name):
|
||||||
|
# perform dummy load
|
||||||
|
try:
|
||||||
|
context.pctl.query(pg_name, "begin; insert into mytest select r::numeric from generate_series(1, 350000) r; commit;")
|
||||||
|
except pg.Error as e:
|
||||||
|
assert False, "Error loading test data on {0}: {1}".format(pg_name, e)
|
||||||
|
|
||||||
|
|
||||||
@then('Table {table_name:w} is present on {pg_name:w} after {max_replication_delay:d} seconds')
|
@then('Table {table_name:w} is present on {pg_name:w} after {max_replication_delay:d} seconds')
|
||||||
def table_is_present_on(context, table_name, pg_name, max_replication_delay):
|
def table_is_present_on(context, table_name, pg_name, max_replication_delay):
|
||||||
max_replication_delay *= context.timeout_multiplier
|
max_replication_delay *= context.timeout_multiplier
|
||||||
|
|||||||
@@ -12,8 +12,10 @@ def start_patroni_with_a_name_value_tag(context, name, tag_name, tag_value):
|
|||||||
@then('There is a {label} with "{content}" in {name:w} data directory')
|
@then('There is a {label} with "{content}" in {name:w} data directory')
|
||||||
def check_label(context, label, content, name):
|
def check_label(context, label, content, name):
|
||||||
label = context.pctl.read_label(name, label)
|
label = context.pctl.read_label(name, label)
|
||||||
|
if label is None:
|
||||||
|
label = ""
|
||||||
label = label.replace('\n', '\\n')
|
label = label.replace('\n', '\\n')
|
||||||
assert label == content, "{0} is not equal to {1}".format(label, content)
|
assert content in label, "\"{0}\" doesn't contain {1}".format(label, content)
|
||||||
|
|
||||||
|
|
||||||
@step('I create label with "{content:w}" in {name:w} data directory')
|
@step('I create label with "{content:w}" in {name:w} data directory')
|
||||||
@@ -21,16 +23,33 @@ def write_label(context, content, name):
|
|||||||
context.pctl.write_label(name, content)
|
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):
|
def check_member(context, name, key, value, time_limit):
|
||||||
time_limit *= context.timeout_multiplier
|
time_limit *= context.timeout_multiplier
|
||||||
max_time = time.time() + int(time_limit)
|
max_time = time.time() + int(time_limit)
|
||||||
|
dcs_value = None
|
||||||
while time.time() < max_time:
|
while time.time() < max_time:
|
||||||
try:
|
try:
|
||||||
response = json.loads(context.dcs_ctl.query(name))
|
response = json.loads(context.dcs_ctl.query(name))
|
||||||
if response.get(key) == value:
|
dcs_value = response.get(key)
|
||||||
|
if dcs_value == value:
|
||||||
return
|
return
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
assert False, "{0} does not have {1}={2} in dcs after {3} seconds".format(name, key, value, time_limit)
|
assert False, "{0} does not have {1}={2} (found {3}) in dcs after {4} seconds".format(name, key, value,
|
||||||
|
dcs_value, time_limit)
|
||||||
|
|
||||||
|
|
||||||
|
@step('there is a non empty {key:w} key in DCS after {time_limit:d} seconds')
|
||||||
|
def check_initialize(context, key, time_limit):
|
||||||
|
time_limit *= context.timeout_multiplier
|
||||||
|
max_time = time.time() + int(time_limit)
|
||||||
|
while time.time() < max_time:
|
||||||
|
try:
|
||||||
|
if context.dcs_ctl.query(key):
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
time.sleep(1)
|
||||||
|
assert False, "There is no {0} in dcs after {1} seconds".format(key, time_limit)
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import base64
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import parse
|
import parse
|
||||||
import requests
|
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
@@ -45,9 +43,9 @@ def sleep_for_n_seconds(context, value):
|
|||||||
|
|
||||||
|
|
||||||
def _set_response(context, response):
|
def _set_response(context, response):
|
||||||
context.status_code = response.status_code
|
context.status_code = response.status
|
||||||
data = response.content.decode('utf-8')
|
data = response.data.decode('utf-8')
|
||||||
ct = response.headers.get('content-type', '')
|
ct = response.getheader('content-type', '')
|
||||||
if ct.startswith('application/json') or\
|
if ct.startswith('application/json') or\
|
||||||
ct.startswith('text/yaml') or\
|
ct.startswith('text/yaml') or\
|
||||||
ct.startswith('text/x-yaml') or\
|
ct.startswith('text/x-yaml') or\
|
||||||
@@ -63,13 +61,7 @@ def _set_response(context, response):
|
|||||||
|
|
||||||
@step('I issue a GET request to {url:url}')
|
@step('I issue a GET request to {url:url}')
|
||||||
def do_get(context, url):
|
def do_get(context, url):
|
||||||
try:
|
do_request(context, 'GET', url, None)
|
||||||
r = requests.get(url)
|
|
||||||
except requests.exceptions.RequestException:
|
|
||||||
context.status_code = None
|
|
||||||
context.response = None
|
|
||||||
else:
|
|
||||||
_set_response(context, r)
|
|
||||||
|
|
||||||
|
|
||||||
@step('I issue an empty POST request to {url:url}')
|
@step('I issue an empty POST request to {url:url}')
|
||||||
@@ -79,17 +71,13 @@ def do_post_empty(context, url):
|
|||||||
|
|
||||||
@step('I issue a {request_method:w} request to {url:url} with {data}')
|
@step('I issue a {request_method:w} request to {url:url} with {data}')
|
||||||
def do_request(context, request_method, url, data):
|
def do_request(context, request_method, url, data):
|
||||||
data = data and json.loads(data) or {}
|
data = data and json.loads(data)
|
||||||
headers = {'Authorization': 'Basic ' + base64.b64encode('username:password'.encode('utf-8')).decode('utf-8'),
|
|
||||||
'Content-Type': 'application/json'}
|
|
||||||
try:
|
try:
|
||||||
if request_method == 'PATCH':
|
r = context.request_executor.request(request_method, url, data)
|
||||||
r = requests.patch(url, headers=headers, json=data)
|
if request_method == 'PATCH' and r.status == 409:
|
||||||
else:
|
r = context.request_executor.request(request_method, url, data)
|
||||||
r = requests.post(url, headers=headers, json=data)
|
except Exception:
|
||||||
except requests.exceptions.RequestException:
|
context.status_code = context.response = None
|
||||||
context.status_code = None
|
|
||||||
context.response = None
|
|
||||||
else:
|
else:
|
||||||
_set_response(context, r)
|
_set_response(context, r)
|
||||||
|
|
||||||
@@ -149,8 +137,8 @@ def add_tag_to_config(context, tag, value, pg_name):
|
|||||||
def check_http_response(context, url, value, timeout, negate=False):
|
def check_http_response(context, url, value, timeout, negate=False):
|
||||||
timeout *= context.timeout_multiplier
|
timeout *= context.timeout_multiplier
|
||||||
for _ in range(int(timeout)):
|
for _ in range(int(timeout)):
|
||||||
r = requests.get(url)
|
r = context.request_executor.request('GET', url)
|
||||||
if (value in r.content.decode('utf-8')) != negate:
|
if (value in r.data.decode('utf-8')) != negate:
|
||||||
break
|
break
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
from behave import step, then
|
||||||
|
import patroni.psycopg as pg
|
||||||
|
|
||||||
|
|
||||||
|
@step('I create a logical replication slot {slot_name} on {pg_name:w} with the {plugin:w} plugin')
|
||||||
|
def create_logical_replication_slot(context, slot_name, pg_name, plugin):
|
||||||
|
try:
|
||||||
|
output = context.pctl.query(pg_name, ("SELECT pg_create_logical_replication_slot('{0}', '{1}'),"
|
||||||
|
" current_database()").format(slot_name, plugin))
|
||||||
|
print(output.fetchone())
|
||||||
|
except pg.Error as e:
|
||||||
|
print(e)
|
||||||
|
assert False, "Error creating slot {0} on {1} with plugin {2}".format(slot_name, pg_name, plugin)
|
||||||
|
|
||||||
|
|
||||||
|
@then('{pg_name:w} has a logical replication slot named {slot_name} with the {plugin:w} plugin')
|
||||||
|
def has_logical_replication_slot(context, pg_name, slot_name, plugin):
|
||||||
|
try:
|
||||||
|
row = context.pctl.query(pg_name, ("SELECT slot_type, plugin FROM pg_replication_slots"
|
||||||
|
" WHERE slot_name = '{0}'").format(slot_name)).fetchone()
|
||||||
|
assert row, "Couldn't find replication slot named {0}".format(slot_name)
|
||||||
|
assert row[0] == "logical", "Found replication slot named {0} but wasn't a logical slot".format(slot_name)
|
||||||
|
assert row[1] == plugin, ("Found replication slot named {0} but was using plugin "
|
||||||
|
"{1} rather than {2}").format(slot_name, row[1], plugin)
|
||||||
|
except pg.Error:
|
||||||
|
assert False, "Error looking for slot {0} on {1} with plugin {2}".format(slot_name, pg_name, plugin)
|
||||||
|
|
||||||
|
|
||||||
|
@then('{pg_name:w} does not have a logical replication slot named {slot_name}')
|
||||||
|
def does_not_have_logical_replication_slot(context, pg_name, slot_name):
|
||||||
|
try:
|
||||||
|
row = context.pctl.query(pg_name, ("SELECT 1 FROM pg_replication_slots"
|
||||||
|
" WHERE slot_name = '{0}'").format(slot_name)).fetchone()
|
||||||
|
assert not row, "Found unexpected replication slot named {0}".format(slot_name)
|
||||||
|
except pg.Error:
|
||||||
|
assert False, "Error looking for slot {0} on {1}".format(slot_name, pg_name)
|
||||||
|
|
||||||
|
|
||||||
|
@step('Logical slot {slot_name:w} is in sync between {pg_name1:w} and {pg_name2:w} after {time_limit:d} seconds')
|
||||||
|
def logical_slots_in_sync(context, slot_name, pg_name1, pg_name2, time_limit):
|
||||||
|
time_limit *= context.timeout_multiplier
|
||||||
|
max_time = time.time() + int(time_limit)
|
||||||
|
while time.time() < max_time:
|
||||||
|
try:
|
||||||
|
query = "SELECT confirmed_flush_lsn FROM pg_replication_slots WHERE slot_name = '{0}'".format(slot_name)
|
||||||
|
slot1 = context.pctl.query(pg_name1, query).fetchone()
|
||||||
|
slot2 = context.pctl.query(pg_name2, query).fetchone()
|
||||||
|
if slot1[0] == slot2[0]:
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
time.sleep(1)
|
||||||
|
assert False, "Logical slot {0} is not in sync between {1} and {2}".format(slot_name, pg_name1, pg_name2)
|
||||||
|
|
||||||
|
|
||||||
|
@step('I get all changes from logical slot {slot_name:w} on {pg_name:w}')
|
||||||
|
def logical_slot_get_changes(context, slot_name, pg_name):
|
||||||
|
context.pctl.query(pg_name, "SELECT * FROM pg_logical_slot_get_changes('{0}', NULL, NULL)".format(slot_name))
|
||||||
@@ -4,23 +4,9 @@ import time
|
|||||||
from behave import step
|
from behave import step
|
||||||
|
|
||||||
|
|
||||||
select_replication_query = """
|
def callbacks(context, name):
|
||||||
SELECT * FROM pg_catalog.pg_stat_replication
|
return {c: '{0} features/callback2.py {1}'.format(context.pctl.PYTHON, name)
|
||||||
WHERE application_name = '{0}'
|
for c in ('on_start', 'on_stop', 'on_restart', 'on_role_change')}
|
||||||
"""
|
|
||||||
|
|
||||||
callback = "bash -c 'echo \"${*: -3:1} ${*: -2:1} ${*: -1:1}\" >> data/$1/$1_cb.log' -- "
|
|
||||||
|
|
||||||
|
|
||||||
@step('I start {name:w} with callback configured')
|
|
||||||
def start_patroni_with_callbacks(context, name):
|
|
||||||
return context.pctl.start(name, custom_config={
|
|
||||||
"postgresql": {
|
|
||||||
"callbacks": {
|
|
||||||
"on_role_change": "features/callback.sh"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
@step('I start {name:w} in a cluster {cluster_name:w}')
|
@step('I start {name:w} in a cluster {cluster_name:w}')
|
||||||
@@ -28,10 +14,10 @@ def start_patroni(context, name, cluster_name):
|
|||||||
return context.pctl.start(name, custom_config={
|
return context.pctl.start(name, custom_config={
|
||||||
"scope": cluster_name,
|
"scope": cluster_name,
|
||||||
"postgresql": {
|
"postgresql": {
|
||||||
"callbacks": {c: callback + name for c in ('on_start', 'on_stop', 'on_restart', 'on_role_change')},
|
"callbacks": callbacks(context, name),
|
||||||
"backup_restore": {
|
"backup_restore": {
|
||||||
"command": "features/backup_restore.sh --sourcedir=" + os.path.join(context.pctl.patroni_path,
|
"command": (context.pctl.PYTHON + " features/backup_restore.py --sourcedir=" +
|
||||||
"data/basebackup")}
|
os.path.join(context.pctl.patroni_path, 'data', 'basebackup').replace('\\', '/'))}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -53,11 +39,12 @@ def start_patroni_standby_cluster(context, name, cluster_name, name2):
|
|||||||
"port": port,
|
"port": port,
|
||||||
"primary_slot_name": "pm_1",
|
"primary_slot_name": "pm_1",
|
||||||
"create_replica_methods": ["backup_restore", "basebackup"]
|
"create_replica_methods": ["backup_restore", "basebackup"]
|
||||||
}
|
},
|
||||||
|
"postgresql": {"parameters": {"wal_level": "logical"}}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"postgresql": {
|
"postgresql": {
|
||||||
"callbacks": {c: callback + name for c in ('on_start', 'on_stop', 'on_restart', 'on_role_change')}
|
"callbacks": callbacks(context, name)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return context.pctl.start(name)
|
return context.pctl.start(name)
|
||||||
@@ -65,12 +52,12 @@ def start_patroni_standby_cluster(context, name, cluster_name, name2):
|
|||||||
|
|
||||||
@step('{pg_name1:w} is replicating from {pg_name2:w} after {timeout:d} seconds')
|
@step('{pg_name1:w} is replicating from {pg_name2:w} after {timeout:d} seconds')
|
||||||
def check_replication_status(context, pg_name1, pg_name2, timeout):
|
def check_replication_status(context, pg_name1, pg_name2, timeout):
|
||||||
bound_time = time.time() + timeout
|
bound_time = time.time() + timeout * context.timeout_multiplier
|
||||||
|
|
||||||
while time.time() < bound_time:
|
while time.time() < bound_time:
|
||||||
cur = context.pctl.query(
|
cur = context.pctl.query(
|
||||||
pg_name2,
|
pg_name2,
|
||||||
select_replication_query.format(pg_name1),
|
"SELECT * FROM pg_catalog.pg_stat_replication WHERE application_name = '{0}'".format(pg_name1),
|
||||||
fail_ok=True
|
fail_ok=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -44,31 +44,6 @@ def watchdog_was_triggered(context, name, timeout):
|
|||||||
assert False
|
assert False
|
||||||
|
|
||||||
|
|
||||||
@then('{name:w} watchdog was not triggered')
|
|
||||||
def watchdog_was_not_triggered(context, name):
|
|
||||||
assert not context.pctl.get_watchdog(name).was_triggered
|
|
||||||
|
|
||||||
|
|
||||||
@step('{name:w} checkpoint takes {timeout:d} seconds')
|
|
||||||
def checkpoint_hang(context, name, timeout):
|
|
||||||
assert context.pctl.checkpoint_hang(name, timeout)
|
|
||||||
|
|
||||||
|
|
||||||
@step('{name:w} hangs for {timeout:d} seconds')
|
@step('{name:w} hangs for {timeout:d} seconds')
|
||||||
def patroni_hang(context, name, timeout):
|
def patroni_hang(context, name, timeout):
|
||||||
return context.pctl.patroni_hang(name, timeout)
|
return context.pctl.patroni_hang(name, timeout)
|
||||||
|
|
||||||
|
|
||||||
@step('I terminate {name:w} user processes')
|
|
||||||
def terminate_backends(context, name):
|
|
||||||
return context.pctl.terminate_backends(name)
|
|
||||||
|
|
||||||
|
|
||||||
@step('Sleep for {timeout:d} seconds')
|
|
||||||
def dcs_connection_lost(context, timeout):
|
|
||||||
time.sleep(timeout)
|
|
||||||
|
|
||||||
|
|
||||||
@then('{name:w} database is running')
|
|
||||||
def database_is_running(context, name):
|
|
||||||
assert context.pctl.database_is_running(name)
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
FROM postgres:11
|
FROM postgres:15
|
||||||
MAINTAINER Alexander Kukushkin <alexander.kukushkin@zalando.de>
|
LABEL maintainer="Alexander Kukushkin <akukushkin@microsoft.com>"
|
||||||
|
|
||||||
RUN export DEBIAN_FRONTEND=noninteractive \
|
RUN export DEBIAN_FRONTEND=noninteractive \
|
||||||
&& echo 'APT::Install-Recommends "0";\nAPT::Install-Suggests "0";' > /etc/apt/apt.conf.d/01norecommend \
|
&& echo 'APT::Install-Recommends "0";\nAPT::Install-Suggests "0";' > /etc/apt/apt.conf.d/01norecommend \
|
||||||
|
|||||||
@@ -33,7 +33,5 @@ postgresql:
|
|||||||
__EOF__
|
__EOF__
|
||||||
|
|
||||||
unset PATRONI_SUPERUSER_PASSWORD PATRONI_REPLICATION_PASSWORD
|
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:
|
fieldRef:
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
fieldPath: metadata.namespace
|
fieldPath: metadata.namespace
|
||||||
|
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
||||||
|
value: 'true'
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
- name: PATRONI_KUBERNETES_LABELS
|
||||||
value: '{application: ${APPLICATION_NAME}, cluster-name: ${PATRONI_CLUSTER_NAME}}'
|
value: '{application: ${APPLICATION_NAME}, cluster-name: ${PATRONI_CLUSTER_NAME}}'
|
||||||
- name: PATRONI_SUPERUSER_USERNAME
|
- name: PATRONI_SUPERUSER_USERNAME
|
||||||
@@ -152,6 +154,16 @@ objects:
|
|||||||
image: docker-registry.default.svc:5000/${NAMESPACE}/patroni:latest
|
image: docker-registry.default.svc:5000/${NAMESPACE}/patroni:latest
|
||||||
imagePullPolicy: IfNotPresent
|
imagePullPolicy: IfNotPresent
|
||||||
name: ${APPLICATION_NAME}
|
name: ${APPLICATION_NAME}
|
||||||
|
readinessProbe:
|
||||||
|
httpGet:
|
||||||
|
scheme: HTTP
|
||||||
|
path: /readiness
|
||||||
|
port: 8008
|
||||||
|
initialDelaySeconds: 3
|
||||||
|
periodSeconds: 10
|
||||||
|
timeoutSeconds: 5
|
||||||
|
successThreshold: 1
|
||||||
|
failureThreshold: 3
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 8008
|
- containerPort: 8008
|
||||||
protocol: TCP
|
protocol: TCP
|
||||||
@@ -240,6 +252,34 @@ objects:
|
|||||||
subjects:
|
subjects:
|
||||||
- kind: ServiceAccount
|
- kind: ServiceAccount
|
||||||
name: ${SERVICE_ACCOUNT}
|
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:
|
parameters:
|
||||||
- description: The name of the application for labelling all artifacts.
|
- description: The name of the application for labelling all artifacts.
|
||||||
displayName: Application Name
|
displayName: Application Name
|
||||||
|
|||||||
@@ -5,11 +5,9 @@ metadata:
|
|||||||
annotations:
|
annotations:
|
||||||
description: |-
|
description: |-
|
||||||
Patroni Postgresql database cluster, with persistent storage.
|
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
|
iconClass: icon-postgresql
|
||||||
openshift.io/display-name: Patroni Postgresql (Persistent)
|
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
|
tags: postgresql
|
||||||
objects:
|
objects:
|
||||||
- apiVersion: v1
|
- apiVersion: v1
|
||||||
@@ -106,6 +104,20 @@ objects:
|
|||||||
application: ${APPLICATION_NAME}
|
application: ${APPLICATION_NAME}
|
||||||
cluster-name: ${PATRONI_CLUSTER_NAME}
|
cluster-name: ${PATRONI_CLUSTER_NAME}
|
||||||
spec:
|
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:
|
containers:
|
||||||
- env:
|
- env:
|
||||||
- name: PATRONI_KUBERNETES_POD_IP
|
- name: PATRONI_KUBERNETES_POD_IP
|
||||||
@@ -118,6 +130,8 @@ objects:
|
|||||||
fieldRef:
|
fieldRef:
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
fieldPath: metadata.namespace
|
fieldPath: metadata.namespace
|
||||||
|
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
||||||
|
value: 'true'
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
- name: PATRONI_KUBERNETES_LABELS
|
||||||
value: '{application: ${APPLICATION_NAME}, cluster-name: ${PATRONI_CLUSTER_NAME}}'
|
value: '{application: ${APPLICATION_NAME}, cluster-name: ${PATRONI_CLUSTER_NAME}}'
|
||||||
- name: PATRONI_SUPERUSER_USERNAME
|
- name: PATRONI_SUPERUSER_USERNAME
|
||||||
@@ -152,6 +166,16 @@ objects:
|
|||||||
image: docker-registry.default.svc:5000/${NAMESPACE}/patroni:latest
|
image: docker-registry.default.svc:5000/${NAMESPACE}/patroni:latest
|
||||||
imagePullPolicy: IfNotPresent
|
imagePullPolicy: IfNotPresent
|
||||||
name: ${APPLICATION_NAME}
|
name: ${APPLICATION_NAME}
|
||||||
|
readinessProbe:
|
||||||
|
httpGet:
|
||||||
|
scheme: HTTP
|
||||||
|
path: /readiness
|
||||||
|
port: 8008
|
||||||
|
initialDelaySeconds: 3
|
||||||
|
periodSeconds: 10
|
||||||
|
timeoutSeconds: 5
|
||||||
|
successThreshold: 1
|
||||||
|
failureThreshold: 3
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 8008
|
- containerPort: 8008
|
||||||
protocol: TCP
|
protocol: TCP
|
||||||
@@ -252,6 +276,34 @@ objects:
|
|||||||
subjects:
|
subjects:
|
||||||
- kind: ServiceAccount
|
- kind: ServiceAccount
|
||||||
name: ${SERVICE_ACCOUNT}
|
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:
|
parameters:
|
||||||
- description: The name of the application for labelling all artifacts.
|
- description: The name of the application for labelling all artifacts.
|
||||||
displayName: Application Name
|
displayName: Application Name
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ spec:
|
|||||||
clusterIP: None
|
clusterIP: None
|
||||||
|
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1beta1
|
apiVersion: apps/v1
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: &cluster_name patronidemo
|
name: &cluster_name patronidemo
|
||||||
@@ -20,6 +20,10 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 3
|
replicas: 3
|
||||||
serviceName: *cluster_name
|
serviceName: *cluster_name
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
application: patroni
|
||||||
|
cluster-name: *cluster_name
|
||||||
template:
|
template:
|
||||||
metadata:
|
metadata:
|
||||||
labels:
|
labels:
|
||||||
@@ -31,6 +35,16 @@ spec:
|
|||||||
- name: *cluster_name
|
- name: *cluster_name
|
||||||
image: patroni # docker build -t patroni .
|
image: patroni # docker build -t patroni .
|
||||||
imagePullPolicy: IfNotPresent
|
imagePullPolicy: IfNotPresent
|
||||||
|
readinessProbe:
|
||||||
|
httpGet:
|
||||||
|
scheme: HTTP
|
||||||
|
path: /readiness
|
||||||
|
port: 8008
|
||||||
|
initialDelaySeconds: 3
|
||||||
|
periodSeconds: 10
|
||||||
|
timeoutSeconds: 5
|
||||||
|
successThreshold: 1
|
||||||
|
failureThreshold: 3
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 8008
|
- containerPort: 8008
|
||||||
protocol: TCP
|
protocol: TCP
|
||||||
@@ -48,6 +62,8 @@ spec:
|
|||||||
valueFrom:
|
valueFrom:
|
||||||
fieldRef:
|
fieldRef:
|
||||||
fieldPath: metadata.namespace
|
fieldPath: metadata.namespace
|
||||||
|
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
||||||
|
value: 'true'
|
||||||
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
|
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
|
||||||
value: 'true'
|
value: 'true'
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
- name: PATRONI_KUBERNETES_LABELS
|
||||||
@@ -123,6 +139,25 @@ spec:
|
|||||||
- port: 5432
|
- port: 5432
|
||||||
targetPort: 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
|
apiVersion: v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
@@ -210,3 +245,37 @@ roleRef:
|
|||||||
subjects:
|
subjects:
|
||||||
- kind: ServiceAccount
|
- kind: ServiceAccount
|
||||||
name: patronidemo
|
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
|
||||||
|
|||||||
+1
-9
@@ -1,14 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from patroni import main
|
from patroni.__main__ import main
|
||||||
|
|
||||||
import os
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
if os.getenv("PATRONI_DEBUG_MODE"):
|
|
||||||
# XXX Visual Code specific https://github.com/microsoft/ptvsd/issues/1443
|
|
||||||
# create processes by spawning new Python interpreters instead of forking the current one
|
|
||||||
import multiprocessing
|
|
||||||
multiprocessing.set_start_method('spawn', True)
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
+22
-214
@@ -1,169 +1,8 @@
|
|||||||
import logging
|
|
||||||
import os
|
|
||||||
import signal
|
|
||||||
import sys
|
import sys
|
||||||
import time
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
PATRONI_ENV_PREFIX = 'PATRONI_'
|
||||||
|
KUBERNETES_ENV_PREFIX = 'KUBERNETES_'
|
||||||
|
MIN_PSYCOPG2 = (2, 5, 4)
|
||||||
class Patroni(object):
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
from patroni.api import RestApiServer
|
|
||||||
from patroni.config import Config
|
|
||||||
from patroni.dcs import get_dcs
|
|
||||||
from patroni.ha import Ha
|
|
||||||
from patroni.log import PatroniLogger
|
|
||||||
from patroni.postgresql import Postgresql
|
|
||||||
from patroni.version import __version__
|
|
||||||
from patroni.watchdog import Watchdog
|
|
||||||
|
|
||||||
self.setup_signal_handlers()
|
|
||||||
|
|
||||||
self.version = __version__
|
|
||||||
self.logger = PatroniLogger()
|
|
||||||
self.config = Config()
|
|
||||||
self.logger.reload_config(self.config.get('log', {}))
|
|
||||||
self.dcs = get_dcs(self.config)
|
|
||||||
self.watchdog = Watchdog(self.config)
|
|
||||||
self.load_dynamic_configuration()
|
|
||||||
|
|
||||||
self.postgresql = Postgresql(self.config['postgresql'])
|
|
||||||
self.api = RestApiServer(self, self.config['restapi'])
|
|
||||||
self.ha = Ha(self)
|
|
||||||
self.is_in_debug_mode = bool(os.getenv("PATRONI_DEBUG_MODE"))
|
|
||||||
|
|
||||||
self.tags = self.get_tags()
|
|
||||||
self.next_run = time.time()
|
|
||||||
self.scheduled_restart = {}
|
|
||||||
|
|
||||||
def load_dynamic_configuration(self):
|
|
||||||
from patroni.exceptions import DCSError
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
cluster = self.dcs.get_cluster()
|
|
||||||
if cluster and cluster.config and cluster.config.data:
|
|
||||||
if self.config.set_dynamic_configuration(cluster.config):
|
|
||||||
self.dcs.reload_config(self.config)
|
|
||||||
self.watchdog.reload_config(self.config)
|
|
||||||
elif not self.config.dynamic_configuration and 'bootstrap' in self.config:
|
|
||||||
if self.config.set_dynamic_configuration(self.config['bootstrap']['dcs']):
|
|
||||||
self.dcs.reload_config(self.config)
|
|
||||||
break
|
|
||||||
except DCSError:
|
|
||||||
logger.warning('Can not get cluster from dcs')
|
|
||||||
time.sleep(5)
|
|
||||||
|
|
||||||
def get_tags(self):
|
|
||||||
return {tag: value for tag, value in self.config.get('tags', {}).items()
|
|
||||||
if tag not in ('clonefrom', 'nofailover', 'noloadbalance', 'nosync') or value}
|
|
||||||
|
|
||||||
@property
|
|
||||||
def nofailover(self):
|
|
||||||
return bool(self.tags.get('nofailover', False))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def nosync(self):
|
|
||||||
return bool(self.tags.get('nosync', False))
|
|
||||||
|
|
||||||
def reload_config(self):
|
|
||||||
try:
|
|
||||||
self.tags = self.get_tags()
|
|
||||||
self.logger.reload_config(self.config.get('log', {}))
|
|
||||||
self.dcs.reload_config(self.config)
|
|
||||||
self.watchdog.reload_config(self.config)
|
|
||||||
self.api.reload_config(self.config['restapi'])
|
|
||||||
self.postgresql.reload_config(self.config['postgresql'])
|
|
||||||
except Exception:
|
|
||||||
logger.exception('Failed to reload config_file=%s', self.config.config_file)
|
|
||||||
|
|
||||||
@property
|
|
||||||
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))
|
|
||||||
|
|
||||||
def schedule_next_run(self):
|
|
||||||
self.next_run += self.dcs.loop_wait
|
|
||||||
current_time = time.time()
|
|
||||||
nap_time = self.next_run - current_time
|
|
||||||
if nap_time <= 0:
|
|
||||||
self.next_run = current_time
|
|
||||||
# Release the GIL so we don't starve anyone waiting on async_executor lock
|
|
||||||
time.sleep(0.001)
|
|
||||||
# Warn user that Patroni is not keeping up or runs in debug
|
|
||||||
msg = "Patroni runs in the debug mode: keys' TTL is infinite, loop wait disabled" if self.is_in_debug_mode else "Loop time exceeded, rescheduling immediately."
|
|
||||||
logger.warning(msg)
|
|
||||||
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.next_run = time.time()
|
|
||||||
|
|
||||||
while not self.received_sigterm:
|
|
||||||
if self._received_sighup:
|
|
||||||
self._received_sighup = False
|
|
||||||
if self.config.reload_local_configuration():
|
|
||||||
self.reload_config()
|
|
||||||
|
|
||||||
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.postgresql.role != 'uninitialized':
|
|
||||||
self.config.save_cache()
|
|
||||||
|
|
||||||
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
|
|
||||||
try:
|
|
||||||
self.api.shutdown()
|
|
||||||
except Exception:
|
|
||||||
logger.exception('Exception during RestApi.shutdown')
|
|
||||||
self.ha.shutdown()
|
|
||||||
self.logger.shutdown()
|
|
||||||
|
|
||||||
|
|
||||||
def patroni_main():
|
|
||||||
patroni = Patroni()
|
|
||||||
try:
|
|
||||||
patroni.run()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
pass
|
|
||||||
finally:
|
|
||||||
patroni.shutdown()
|
|
||||||
|
|
||||||
|
|
||||||
def fatal(string, *args):
|
def fatal(string, *args):
|
||||||
@@ -171,63 +10,32 @@ def fatal(string, *args):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def check_psycopg2():
|
def parse_version(version):
|
||||||
min_psycopg2 = (2, 5, 4)
|
def _parse_version(version):
|
||||||
min_psycopg2_str = '.'.join(map(str, min_psycopg2))
|
|
||||||
|
|
||||||
def parse_version(version):
|
|
||||||
for e in version.split('.'):
|
for e in version.split('.'):
|
||||||
try:
|
try:
|
||||||
yield int(e)
|
yield int(e)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
break
|
break
|
||||||
|
return tuple(_parse_version(version.split(' ')[0]))
|
||||||
|
|
||||||
|
|
||||||
|
# We pass MIN_PSYCOPG2 and parse_version as arguments to simplify usage of check_psycopg from the setup.py
|
||||||
|
def check_psycopg(_min_psycopg2=MIN_PSYCOPG2, _parse_version=parse_version):
|
||||||
|
min_psycopg2_str = '.'.join(map(str, _min_psycopg2))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import psycopg2
|
from psycopg2 import __version__
|
||||||
version_str = psycopg2.__version__.split(' ')[0]
|
if _parse_version(__version__) >= _min_psycopg2:
|
||||||
version = tuple(parse_version(version_str))
|
return
|
||||||
if version < min_psycopg2:
|
version_str = __version__.split(' ')[0]
|
||||||
fatal('Patroni requires psycopg2>={0}, but only {1} is available', min_psycopg2_str, version_str)
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
fatal('Patroni requires psycopg2>={0} or psycopg2-binary', min_psycopg2_str)
|
version_str = None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
check_psycopg2()
|
|
||||||
if os.getpid() != 1:
|
|
||||||
return patroni_main()
|
|
||||||
|
|
||||||
# Patroni started with PID=1, it looks like we are in the container
|
|
||||||
pid = 0
|
|
||||||
|
|
||||||
# Looks like we are in a docker, so we will act like init
|
|
||||||
def sigchld_handler(signo, stack_frame):
|
|
||||||
try:
|
try:
|
||||||
while True:
|
from psycopg import __version__
|
||||||
ret = os.waitpid(-1, os.WNOHANG)
|
except ImportError:
|
||||||
if ret == (0, 0):
|
error = 'Patroni requires psycopg2>={0}, psycopg2-binary, or psycopg>=3.0'.format(min_psycopg2_str)
|
||||||
break
|
if version_str:
|
||||||
elif ret[0] != pid:
|
error += ', but only psycopg2=={0} is available'.format(version_str)
|
||||||
logger.info('Reaped pid=%s, exit status=%s', *ret)
|
fatal(error)
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def passtochild(signo, stack_frame):
|
|
||||||
if pid:
|
|
||||||
os.kill(pid, signo)
|
|
||||||
|
|
||||||
if os.name != 'nt':
|
|
||||||
signal.signal(signal.SIGCHLD, sigchld_handler)
|
|
||||||
signal.signal(signal.SIGHUP, passtochild)
|
|
||||||
signal.signal(signal.SIGQUIT, passtochild)
|
|
||||||
signal.signal(signal.SIGUSR1, passtochild)
|
|
||||||
signal.signal(signal.SIGUSR2, passtochild)
|
|
||||||
signal.signal(signal.SIGINT, passtochild)
|
|
||||||
signal.signal(signal.SIGABRT, passtochild)
|
|
||||||
signal.signal(signal.SIGTERM, passtochild)
|
|
||||||
|
|
||||||
import multiprocessing
|
|
||||||
patroni = multiprocessing.Process(target=patroni_main)
|
|
||||||
patroni.start()
|
|
||||||
pid = patroni.pid
|
|
||||||
patroni.join()
|
|
||||||
|
|||||||
+178
-1
@@ -1,4 +1,181 @@
|
|||||||
from patroni import main
|
import logging
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
import time
|
||||||
|
|
||||||
|
from .daemon import AbstractPatroniDaemon, abstract_main
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Patroni(AbstractPatroniDaemon):
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
from .api import RestApiServer
|
||||||
|
from .dcs import get_dcs
|
||||||
|
from .ha import Ha
|
||||||
|
from .postgresql import Postgresql
|
||||||
|
from .request import PatroniRequest
|
||||||
|
from .version import __version__
|
||||||
|
from .watchdog import Watchdog
|
||||||
|
|
||||||
|
super(Patroni, self).__init__(config)
|
||||||
|
|
||||||
|
self.version = __version__
|
||||||
|
self.dcs = get_dcs(self.config)
|
||||||
|
self.watchdog = Watchdog(self.config)
|
||||||
|
self.load_dynamic_configuration()
|
||||||
|
|
||||||
|
self.postgresql = Postgresql(self.config['postgresql'])
|
||||||
|
self.api = RestApiServer(self, self.config['restapi'])
|
||||||
|
self.request = PatroniRequest(self.config, True)
|
||||||
|
self.ha = Ha(self)
|
||||||
|
|
||||||
|
self.tags = self.get_tags()
|
||||||
|
self.next_run = time.time()
|
||||||
|
self.scheduled_restart = {}
|
||||||
|
|
||||||
|
def load_dynamic_configuration(self):
|
||||||
|
from patroni.exceptions import DCSError
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
cluster = self.dcs.get_cluster()
|
||||||
|
if cluster and cluster.config and cluster.config.data:
|
||||||
|
if self.config.set_dynamic_configuration(cluster.config):
|
||||||
|
self.dcs.reload_config(self.config)
|
||||||
|
self.watchdog.reload_config(self.config)
|
||||||
|
elif not self.config.dynamic_configuration and 'bootstrap' in self.config:
|
||||||
|
if self.config.set_dynamic_configuration(self.config['bootstrap']['dcs']):
|
||||||
|
self.dcs.reload_config(self.config)
|
||||||
|
break
|
||||||
|
except DCSError:
|
||||||
|
logger.warning('Can not get cluster from dcs')
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
def get_tags(self):
|
||||||
|
return {tag: value for tag, value in self.config.get('tags', {}).items()
|
||||||
|
if tag not in ('clonefrom', 'nofailover', 'noloadbalance', 'nosync') or value}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nofailover(self):
|
||||||
|
return bool(self.tags.get('nofailover', False))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nosync(self):
|
||||||
|
return bool(self.tags.get('nosync', False))
|
||||||
|
|
||||||
|
def reload_config(self, sighup=False, local=False):
|
||||||
|
try:
|
||||||
|
super(Patroni, self).reload_config(sighup, local)
|
||||||
|
if local:
|
||||||
|
self.tags = self.get_tags()
|
||||||
|
self.request.reload_config(self.config)
|
||||||
|
if local or sighup and self.api.reload_local_certificate():
|
||||||
|
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:
|
||||||
|
logger.exception('Failed to reload config_file=%s', self.config.config_file)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def replicatefrom(self):
|
||||||
|
return self.tags.get('replicatefrom')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def noloadbalance(self):
|
||||||
|
return bool(self.tags.get('noloadbalance', False))
|
||||||
|
|
||||||
|
def schedule_next_run(self):
|
||||||
|
self.next_run += self.dcs.loop_wait
|
||||||
|
current_time = time.time()
|
||||||
|
nap_time = self.next_run - current_time
|
||||||
|
if nap_time <= 0:
|
||||||
|
self.next_run = current_time
|
||||||
|
# Release the GIL so we don't starve anyone waiting on async_executor lock
|
||||||
|
time.sleep(0.001)
|
||||||
|
# Warn user that Patroni is not keeping up
|
||||||
|
logger.warning("Loop time exceeded, rescheduling immediately.")
|
||||||
|
elif self.ha.watch(nap_time):
|
||||||
|
self.next_run = time.time()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.api.start()
|
||||||
|
self.next_run = time.time()
|
||||||
|
super(Patroni, self).run()
|
||||||
|
|
||||||
|
def _run_cycle(self):
|
||||||
|
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.postgresql.role != 'uninitialized':
|
||||||
|
self.config.save_cache()
|
||||||
|
|
||||||
|
self.schedule_next_run()
|
||||||
|
|
||||||
|
def _shutdown(self):
|
||||||
|
try:
|
||||||
|
self.api.shutdown()
|
||||||
|
except Exception:
|
||||||
|
logger.exception('Exception during RestApi.shutdown')
|
||||||
|
try:
|
||||||
|
self.ha.shutdown()
|
||||||
|
except Exception:
|
||||||
|
logger.exception('Exception during Ha.shutdown')
|
||||||
|
|
||||||
|
|
||||||
|
def patroni_main():
|
||||||
|
from multiprocessing import freeze_support
|
||||||
|
from patroni.validator import schema
|
||||||
|
|
||||||
|
freeze_support()
|
||||||
|
abstract_main(Patroni, schema)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if os.getpid() != 1:
|
||||||
|
from . import check_psycopg
|
||||||
|
|
||||||
|
check_psycopg()
|
||||||
|
return patroni_main()
|
||||||
|
|
||||||
|
# Patroni started with PID=1, it looks like we are in the container
|
||||||
|
pid = 0
|
||||||
|
|
||||||
|
# Looks like we are in a docker, so we will act like init
|
||||||
|
def sigchld_handler(signo, stack_frame):
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
ret = os.waitpid(-1, os.WNOHANG)
|
||||||
|
if ret == (0, 0):
|
||||||
|
break
|
||||||
|
elif ret[0] != pid:
|
||||||
|
logger.info('Reaped pid=%s, exit status=%s', *ret)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def passtochild(signo, stack_frame):
|
||||||
|
if pid:
|
||||||
|
os.kill(pid, signo)
|
||||||
|
|
||||||
|
if os.name != 'nt':
|
||||||
|
signal.signal(signal.SIGCHLD, sigchld_handler)
|
||||||
|
signal.signal(signal.SIGHUP, passtochild)
|
||||||
|
signal.signal(signal.SIGQUIT, passtochild)
|
||||||
|
signal.signal(signal.SIGUSR1, passtochild)
|
||||||
|
signal.signal(signal.SIGUSR2, passtochild)
|
||||||
|
signal.signal(signal.SIGINT, passtochild)
|
||||||
|
signal.signal(signal.SIGABRT, passtochild)
|
||||||
|
signal.signal(signal.SIGTERM, passtochild)
|
||||||
|
|
||||||
|
import multiprocessing
|
||||||
|
patroni = multiprocessing.Process(target=patroni_main)
|
||||||
|
patroni.start()
|
||||||
|
pid = patroni.pid
|
||||||
|
patroni.join()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
+428
-130
@@ -1,62 +1,72 @@
|
|||||||
import base64
|
import base64
|
||||||
|
import hmac
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import psycopg2
|
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import dateutil.parser
|
import dateutil.parser
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import six
|
||||||
import socket
|
import socket
|
||||||
|
import sys
|
||||||
|
|
||||||
from patroni.postgresql import PostgresConnectionException
|
from ipaddress import ip_address, ip_network as _ip_network
|
||||||
from patroni.postgresql.misc import postgres_version_to_int, PostgresException
|
|
||||||
from patroni.utils import deep_compare, parse_bool, patch_config, Retry, \
|
|
||||||
RetryFailedError, parse_int, split_host_port, tzutc, uri
|
|
||||||
from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||||
from six.moves.socketserver import ThreadingMixIn
|
from six.moves.socketserver import ThreadingMixIn
|
||||||
|
from six.moves.urllib_parse import urlparse, parse_qs
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
|
from . import psycopg
|
||||||
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def check_auth(func):
|
def ip_network(value):
|
||||||
"""Decorator function to check authorization header.
|
return _ip_network(value.decode('utf-8') if six.PY2 else value, False)
|
||||||
|
|
||||||
Usage example:
|
|
||||||
@check_auth
|
|
||||||
def do_PUT_foo():
|
|
||||||
pass
|
|
||||||
"""
|
|
||||||
def wrapper(handler, *args, **kwargs):
|
|
||||||
if handler.check_auth_header():
|
|
||||||
return func(handler, *args, **kwargs)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
class RestApiHandler(BaseHTTPRequestHandler):
|
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):
|
def _write_response(self, status_code, body, content_type='text/html', headers=None):
|
||||||
|
# TODO: try-catch ConnectionResetError: [Errno 104] Connection reset by peer and log it in DEBUG level
|
||||||
self.send_response(status_code)
|
self.send_response(status_code)
|
||||||
headers = headers or {}
|
headers = headers or {}
|
||||||
if content_type:
|
if content_type:
|
||||||
headers['Content-Type'] = content_type
|
headers['Content-Type'] = content_type
|
||||||
for name, value in headers.items():
|
for name, value in headers.items():
|
||||||
self.send_header(name, value)
|
self.send_header(name, value)
|
||||||
|
for name, value in self.server.http_extra_headers.items():
|
||||||
|
self.send_header(name, value)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(body.encode('utf-8'))
|
self.wfile.write(body.encode('utf-8'))
|
||||||
|
|
||||||
def _write_json_response(self, status_code, response):
|
def _write_json_response(self, status_code, response):
|
||||||
self._write_response(status_code, json.dumps(response), content_type='application/json')
|
self._write_response(status_code, json.dumps(response, default=str), content_type='application/json')
|
||||||
|
|
||||||
def send_auth_request(self, body):
|
def check_access(func):
|
||||||
headers = {'WWW-Authenticate': 'Basic realm="' + self.server.patroni.__class__.__name__ + '"'}
|
"""Decorator function to check the source ip, authorization header. or client certificates
|
||||||
self._write_response(401, body, headers=headers)
|
|
||||||
|
|
||||||
def check_auth_header(self):
|
Usage example:
|
||||||
auth_header = self.headers.get('Authorization')
|
@check_access
|
||||||
status = self.server.check_auth_header(auth_header)
|
def do_PUT_foo():
|
||||||
return not status or self.send_auth_request(status)
|
pass
|
||||||
|
"""
|
||||||
|
|
||||||
|
def wrapper(self, *args, **kwargs):
|
||||||
|
if self.server.check_access(self):
|
||||||
|
return func(self, *args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
def _write_status_response(self, status_code, response):
|
def _write_status_response(self, status_code, response):
|
||||||
patroni = self.server.patroni
|
patroni = self.server.patroni
|
||||||
@@ -87,59 +97,132 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
def do_GET(self, write_status_code_only=False):
|
def do_GET(self, write_status_code_only=False):
|
||||||
"""Default method for processing all GET requests which can not be routed to other methods"""
|
"""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
|
path = '/master' if self.path == '/' else self.path
|
||||||
response = self.get_postgresql_status()
|
response = self.get_postgresql_status()
|
||||||
|
|
||||||
patroni = self.server.patroni
|
patroni = self.server.patroni
|
||||||
cluster = patroni.dcs.cluster
|
cluster = patroni.dcs.cluster
|
||||||
|
|
||||||
if not cluster and patroni.ha.is_paused():
|
leader_optime = cluster and cluster.last_lsn or 0
|
||||||
primary_status_code = 200 if response['role'] == 'master' else 503
|
replayed_location = response.get('xlog', {}).get('replayed_location', 0)
|
||||||
else:
|
max_replica_lag = parse_int(self.path_query.get('lag', [sys.maxsize])[0], 'B')
|
||||||
primary_status_code = 200 if patroni.ha.is_leader() else 503
|
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
|
response.get('role') == 'replica' and response.get('state') == 'running' else 503
|
||||||
|
|
||||||
|
if not cluster and patroni.ha.is_paused():
|
||||||
|
leader_status_code = 200 if response.get('role') in ('master', 'standby_leader') else 503
|
||||||
|
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():
|
||||||
|
leader_status_code = 200
|
||||||
|
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:
|
||||||
|
leader_status_code = primary_status_code = standby_leader_status_code = 503
|
||||||
|
|
||||||
status_code = 503
|
status_code = 503
|
||||||
|
|
||||||
if patroni.ha.is_standby_cluster() and ('standby_leader' in path or 'standby-leader' in path):
|
ignore_tags = False
|
||||||
status_code = 200 if patroni.ha.is_leader() else 503
|
if 'standby_leader' in path or 'standby-leader' in path:
|
||||||
elif 'master' in path or 'leader' in path or 'primary' in path or 'read-write' in path:
|
status_code = standby_leader_status_code
|
||||||
|
ignore_tags = True
|
||||||
|
elif 'leader' in path:
|
||||||
|
status_code = leader_status_code
|
||||||
|
ignore_tags = True
|
||||||
|
elif 'master' in path or 'primary' in path or 'read-write' in path:
|
||||||
status_code = primary_status_code
|
status_code = primary_status_code
|
||||||
|
ignore_tags = True
|
||||||
elif 'replica' in path:
|
elif 'replica' in path:
|
||||||
status_code = replica_status_code
|
status_code = replica_status_code
|
||||||
elif 'read-only' in path:
|
elif 'read-only' in path and 'sync' not 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:
|
elif 'health' in path:
|
||||||
status_code = 200 if response.get('state') == 'running' else 503
|
status_code = 200 if response.get('state') == 'running' else 503
|
||||||
elif cluster: # dcs is available
|
elif cluster: # dcs is available
|
||||||
is_synchronous = cluster.is_synchronous_mode() and cluster.sync \
|
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:
|
if path in ('/sync', '/synchronous') and is_synchronous:
|
||||||
status_code = replica_status_code
|
status_code = replica_status_code
|
||||||
elif path in ('/async', '/asynchronous') and not is_synchronous:
|
elif path in ('/async', '/asynchronous') and not is_synchronous:
|
||||||
status_code = replica_status_code
|
status_code = replica_status_code
|
||||||
|
elif path in ('/read-only-sync', '/read-only-synchronous'):
|
||||||
|
if 200 in (primary_status_code, standby_leader_status_code):
|
||||||
|
status_code = 200
|
||||||
|
elif is_synchronous:
|
||||||
|
status_code = replica_status_code
|
||||||
|
|
||||||
|
# check for user defined tags in query params
|
||||||
|
if not ignore_tags and status_code == 200:
|
||||||
|
qs_tag_prefix = "tag_"
|
||||||
|
for qs_key, qs_value in self.path_query.items():
|
||||||
|
if not qs_key.startswith(qs_tag_prefix):
|
||||||
|
continue
|
||||||
|
qs_key = qs_key[len(qs_tag_prefix):]
|
||||||
|
qs_value = qs_value[0]
|
||||||
|
instance_tag_value = patroni.tags.get(qs_key)
|
||||||
|
# tag not registered for instance
|
||||||
|
if instance_tag_value is None:
|
||||||
|
status_code = 503
|
||||||
|
break
|
||||||
|
if not isinstance(instance_tag_value, six.string_types):
|
||||||
|
instance_tag_value = str(instance_tag_value).lower()
|
||||||
|
if instance_tag_value != qs_value:
|
||||||
|
status_code = 503
|
||||||
|
break
|
||||||
|
|
||||||
if write_status_code_only: # when haproxy sends OPTIONS request it reads only status code and nothing more
|
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._write_status_code_only(status_code)
|
||||||
self.wfile.write('{0} {1} {2}\r\n'.format(self.protocol_version, status_code, message).encode('utf-8'))
|
|
||||||
else:
|
else:
|
||||||
self._write_status_response(status_code, response)
|
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):
|
def do_OPTIONS(self):
|
||||||
self.do_GET(write_status_code_only=True)
|
self.do_GET(write_status_code_only=True)
|
||||||
|
|
||||||
|
def do_HEAD(self):
|
||||||
|
self.do_GET(write_status_code_only=True)
|
||||||
|
|
||||||
|
def do_GET_liveness(self):
|
||||||
|
patroni = self.server.patroni
|
||||||
|
is_primary = patroni.postgresql.role == 'master' and patroni.postgresql.is_running()
|
||||||
|
# We can tolerate Patroni problems longer on the replica.
|
||||||
|
# On the primary the liveness probe most likely will start failing only after the leader key expired.
|
||||||
|
# It should not be a big problem because replicas will see that the primary is still alive via REST API call.
|
||||||
|
liveness_threshold = patroni.dcs.ttl * (1 if is_primary else 2)
|
||||||
|
|
||||||
|
# In maintenance mode (pause) we are fine if heartbeat loop stuck.
|
||||||
|
status_code = 200 if patroni.ha.is_paused() or patroni.next_run + liveness_threshold > time.time() else 503
|
||||||
|
self._write_status_code_only(status_code)
|
||||||
|
|
||||||
|
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):
|
def do_GET_patroni(self):
|
||||||
response = self.get_postgresql_status(True)
|
response = self.get_postgresql_status(True)
|
||||||
self._write_status_response(200, response)
|
self._write_status_response(200, response)
|
||||||
|
|
||||||
|
def do_GET_cluster(self):
|
||||||
|
cluster = self.server.patroni.dcs.get_cluster(True)
|
||||||
|
self._write_json_response(200, cluster_as_json(cluster))
|
||||||
|
|
||||||
|
def do_GET_history(self):
|
||||||
|
cluster = self.server.patroni.dcs.cluster or self.server.patroni.dcs.get_cluster()
|
||||||
|
self._write_json_response(200, cluster.history and cluster.history.lines or [])
|
||||||
|
|
||||||
def do_GET_config(self):
|
def do_GET_config(self):
|
||||||
cluster = self.server.patroni.dcs.cluster or self.server.patroni.dcs.get_cluster()
|
cluster = self.server.patroni.dcs.cluster or self.server.patroni.dcs.get_cluster()
|
||||||
if cluster.config:
|
if cluster.config:
|
||||||
@@ -147,6 +230,99 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
else:
|
else:
|
||||||
self.send_error(502)
|
self.send_error(502)
|
||||||
|
|
||||||
|
def do_GET_metrics(self):
|
||||||
|
postgres = self.get_postgresql_status(True)
|
||||||
|
patroni = self.server.patroni
|
||||||
|
epoch = datetime.datetime(1970, 1, 1, tzinfo=tzutc)
|
||||||
|
|
||||||
|
metrics = []
|
||||||
|
|
||||||
|
scope_label = '{{scope="{0}"}}'.format(patroni.postgresql.scope)
|
||||||
|
metrics.append("# HELP patroni_version Patroni semver without periods.")
|
||||||
|
metrics.append("# TYPE patroni_version gauge")
|
||||||
|
padded_semver = ''.join([x.zfill(2) for x in patroni.version.split('.')]) # 2.0.2 => 020002
|
||||||
|
metrics.append("patroni_version{0} {1}".format(scope_label, padded_semver))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_postgres_running Value is 1 if Postgres is running, 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_postgres_running gauge")
|
||||||
|
metrics.append("patroni_postgres_running{0} {1}".format(scope_label, int(postgres['state'] == 'running')))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_postmaster_start_time Epoch seconds since Postgres started.")
|
||||||
|
metrics.append("# TYPE patroni_postmaster_start_time gauge")
|
||||||
|
postmaster_start_time = postgres.get('postmaster_start_time')
|
||||||
|
postmaster_start_time = (postmaster_start_time - epoch).total_seconds() if postmaster_start_time else 0
|
||||||
|
metrics.append("patroni_postmaster_start_time{0} {1}".format(scope_label, postmaster_start_time))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_master Value is 1 if this node is the leader, 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_master gauge")
|
||||||
|
metrics.append("patroni_master{0} {1}".format(scope_label, int(postgres['role'] == 'master')))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_xlog_location Current location of the Postgres"
|
||||||
|
" transaction log, 0 if this node is not the leader.")
|
||||||
|
metrics.append("# TYPE patroni_xlog_location counter")
|
||||||
|
metrics.append("patroni_xlog_location{0} {1}".format(scope_label, postgres.get('xlog', {}).get('location', 0)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_standby_leader Value is 1 if this node is the standby_leader, 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_standby_leader gauge")
|
||||||
|
metrics.append("patroni_standby_leader{0} {1}".format(scope_label, int(postgres['role'] == 'standby_leader')))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_replica Value is 1 if this node is a replica, 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_replica gauge")
|
||||||
|
metrics.append("patroni_replica{0} {1}".format(scope_label, int(postgres['role'] == 'replica')))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_xlog_received_location Current location of the received"
|
||||||
|
" Postgres transaction log, 0 if this node is not a replica.")
|
||||||
|
metrics.append("# TYPE patroni_xlog_received_location counter")
|
||||||
|
metrics.append("patroni_xlog_received_location{0} {1}"
|
||||||
|
.format(scope_label, postgres.get('xlog', {}).get('received_location', 0)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_xlog_replayed_location Current location of the replayed"
|
||||||
|
" Postgres transaction log, 0 if this node is not a replica.")
|
||||||
|
metrics.append("# TYPE patroni_xlog_replayed_location counter")
|
||||||
|
metrics.append("patroni_xlog_replayed_location{0} {1}"
|
||||||
|
.format(scope_label, postgres.get('xlog', {}).get('replayed_location', 0)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_xlog_replayed_timestamp Current timestamp of the replayed"
|
||||||
|
" Postgres transaction log, 0 if null.")
|
||||||
|
metrics.append("# TYPE patroni_xlog_replayed_timestamp gauge")
|
||||||
|
replayed_timestamp = postgres.get('xlog', {}).get('replayed_timestamp')
|
||||||
|
replayed_timestamp = (replayed_timestamp - epoch).total_seconds() if replayed_timestamp else 0
|
||||||
|
metrics.append("patroni_xlog_replayed_timestamp{0} {1}".format(scope_label, replayed_timestamp))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_xlog_paused Value is 1 if the Postgres xlog is paused, 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_xlog_paused gauge")
|
||||||
|
metrics.append("patroni_xlog_paused{0} {1}"
|
||||||
|
.format(scope_label, int(postgres.get('xlog', {}).get('paused', False) is True)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_postgres_server_version Version of Postgres (if running), 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_postgres_server_version gauge")
|
||||||
|
metrics.append("patroni_postgres_server_version {0} {1}".format(scope_label, postgres.get('server_version', 0)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_cluster_unlocked Value is 1 if the cluster is unlocked, 0 if locked.")
|
||||||
|
metrics.append("# TYPE patroni_cluster_unlocked gauge")
|
||||||
|
metrics.append("patroni_cluster_unlocked{0} {1}".format(scope_label, int(postgres.get('cluster_unlocked', 0))))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_postgres_timeline Postgres timeline of this node (if running), 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_postgres_timeline counter")
|
||||||
|
metrics.append("patroni_postgres_timeline{0} {1}".format(scope_label, postgres.get('timeline', 0)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_dcs_last_seen Epoch timestamp when DCS was last contacted successfully"
|
||||||
|
" by Patroni.")
|
||||||
|
metrics.append("# TYPE patroni_dcs_last_seen gauge")
|
||||||
|
metrics.append("patroni_dcs_last_seen{0} {1}".format(scope_label, postgres.get('dcs_last_seen', 0)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_pending_restart Value is 1 if the node needs a restart, 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_pending_restart gauge")
|
||||||
|
metrics.append("patroni_pending_restart{0} {1}"
|
||||||
|
.format(scope_label, int(patroni.postgresql.pending_restart)))
|
||||||
|
|
||||||
|
metrics.append("# HELP patroni_is_paused Value is 1 if auto failover is disabled, 0 otherwise.")
|
||||||
|
metrics.append("# TYPE patroni_is_paused gauge")
|
||||||
|
metrics.append("patroni_is_paused{0} {1}"
|
||||||
|
.format(scope_label, int(patroni.ha.is_paused())))
|
||||||
|
|
||||||
|
self._write_response(200, '\n'.join(metrics)+'\n', content_type='text/plain')
|
||||||
|
|
||||||
def _read_json_content(self, body_is_optional=False):
|
def _read_json_content(self, body_is_optional=False):
|
||||||
if 'content-length' not in self.headers:
|
if 'content-length' not in self.headers:
|
||||||
return self.send_error(411) if not body_is_optional else {}
|
return self.send_error(411) if not body_is_optional else {}
|
||||||
@@ -161,11 +337,13 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
logger.exception('Bad request')
|
logger.exception('Bad request')
|
||||||
self.send_error(400)
|
self.send_error(400)
|
||||||
|
|
||||||
@check_auth
|
@check_access
|
||||||
def do_PATCH_config(self):
|
def do_PATCH_config(self):
|
||||||
request = self._read_json_content()
|
request = self._read_json_content()
|
||||||
if request:
|
if request:
|
||||||
cluster = self.server.patroni.dcs.get_cluster()
|
cluster = self.server.patroni.dcs.get_cluster(True)
|
||||||
|
if not (cluster.config and cluster.config.modify_index):
|
||||||
|
return self.send_error(503)
|
||||||
data = cluster.config.data.copy()
|
data = cluster.config.data.copy()
|
||||||
if patch_config(data, request):
|
if patch_config(data, request):
|
||||||
value = json.dumps(data, separators=(',', ':'))
|
value = json.dumps(data, separators=(',', ':'))
|
||||||
@@ -174,7 +352,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
self.server.patroni.ha.wakeup()
|
self.server.patroni.ha.wakeup()
|
||||||
self._write_json_response(200, data)
|
self._write_json_response(200, data)
|
||||||
|
|
||||||
@check_auth
|
@check_access
|
||||||
def do_PUT_config(self):
|
def do_PUT_config(self):
|
||||||
request = self._read_json_content()
|
request = self._read_json_content()
|
||||||
if request:
|
if request:
|
||||||
@@ -185,20 +363,18 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
return self.send_error(502)
|
return self.send_error(502)
|
||||||
self._write_json_response(200, request)
|
self._write_json_response(200, request)
|
||||||
|
|
||||||
@check_auth
|
@check_access
|
||||||
def do_POST_reload(self):
|
def do_POST_reload(self):
|
||||||
try:
|
|
||||||
if self.server.patroni.config.reload_local_configuration(True):
|
|
||||||
status_code = 202
|
|
||||||
response = 'reload scheduled'
|
|
||||||
self.server.patroni.sighup_handler()
|
self.server.patroni.sighup_handler()
|
||||||
else:
|
self._write_response(202, 'reload scheduled')
|
||||||
status_code = 200
|
|
||||||
response = 'nothing changed'
|
@check_access
|
||||||
except Exception as e:
|
def do_POST_sigterm(self):
|
||||||
status_code = 500
|
"""Only for behave testing on windows"""
|
||||||
response = str(e)
|
|
||||||
self._write_response(status_code, response)
|
if os.name == 'nt' and os.getenv('BEHAVE_DEBUG'):
|
||||||
|
self.server.patroni.api_sigterm()
|
||||||
|
self._write_response(202, 'shutdown scheduled')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_schedule(schedule, action):
|
def parse_schedule(schedule, action):
|
||||||
@@ -221,7 +397,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
status_code = 422
|
status_code = 422
|
||||||
return (status_code, error, scheduled_at)
|
return (status_code, error, scheduled_at)
|
||||||
|
|
||||||
@check_auth
|
@check_access
|
||||||
def do_POST_restart(self):
|
def do_POST_restart(self):
|
||||||
status_code = 500
|
status_code = 500
|
||||||
data = 'restart failed'
|
data = 'restart failed'
|
||||||
@@ -282,7 +458,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
status_code = 409
|
status_code = 409
|
||||||
self._write_response(status_code, data)
|
self._write_response(status_code, data)
|
||||||
|
|
||||||
@check_auth
|
@check_access
|
||||||
def do_DELETE_restart(self):
|
def do_DELETE_restart(self):
|
||||||
if self.server.patroni.ha.delete_future_restart():
|
if self.server.patroni.ha.delete_future_restart():
|
||||||
data = "scheduled restart deleted"
|
data = "scheduled restart deleted"
|
||||||
@@ -292,7 +468,21 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
code = 404
|
code = 404
|
||||||
self._write_response(code, data)
|
self._write_response(code, data)
|
||||||
|
|
||||||
@check_auth
|
@check_access
|
||||||
|
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_access
|
||||||
def do_POST_reinitialize(self):
|
def do_POST_reinitialize(self):
|
||||||
request = self._read_json_content(body_is_optional=True)
|
request = self._read_json_content(body_is_optional=True)
|
||||||
|
|
||||||
@@ -324,20 +514,20 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
if not cluster.failover:
|
if not cluster.failover:
|
||||||
return 503, action.title() + ' failed'
|
return 503, action.title() + ' failed'
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug('Exception occured during polling %s result: %s', action, e)
|
logger.debug('Exception occurred during polling %s result: %s', action, e)
|
||||||
return 503, action.title() + ' status unknown'
|
return 503, action.title() + ' status unknown'
|
||||||
|
|
||||||
def is_failover_possible(self, cluster, leader, candidate, action):
|
def is_failover_possible(self, cluster, leader, candidate, action):
|
||||||
if leader and (not cluster.leader or cluster.leader.name != leader):
|
if leader and (not cluster.leader or cluster.leader.name != leader):
|
||||||
return 'leader name does not match'
|
return 'leader name does not match'
|
||||||
if candidate:
|
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'
|
return 'candidate name does not match with sync_standby'
|
||||||
members = [m for m in cluster.members if m.name == candidate]
|
members = [m for m in cluster.members if m.name == candidate]
|
||||||
if not members:
|
if not members:
|
||||||
return 'candidate does not exists'
|
return 'candidate does not exists'
|
||||||
elif cluster.is_synchronous_mode():
|
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:
|
if not members:
|
||||||
return action + ' is not possible: can not find sync_standby'
|
return action + ' is not possible: can not find sync_standby'
|
||||||
else:
|
else:
|
||||||
@@ -349,7 +539,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
return None
|
return None
|
||||||
return action + ' is not possible: no good candidates have been found'
|
return action + ' is not possible: no good candidates have been found'
|
||||||
|
|
||||||
@check_auth
|
@check_access
|
||||||
def do_POST_failover(self, action='failover'):
|
def do_POST_failover(self, action='failover'):
|
||||||
request = self._read_json_content()
|
request = self._read_json_content()
|
||||||
(status_code, data) = (400, '')
|
(status_code, data) = (400, '')
|
||||||
@@ -414,6 +604,9 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
ret = BaseHTTPRequestHandler.parse_request(self)
|
ret = BaseHTTPRequestHandler.parse_request(self)
|
||||||
if ret:
|
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.path.lstrip('/').split('/')[0]
|
||||||
mname = self.command + ('_' + mname if mname else '')
|
mname = self.command + ('_' + mname if mname else '')
|
||||||
if hasattr(self, 'do_' + mname):
|
if hasattr(self, 'do_' + mname):
|
||||||
@@ -427,77 +620,79 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
return retry(self.server.query, sql, *params)
|
return retry(self.server.query, sql, *params)
|
||||||
|
|
||||||
def get_postgresql_status(self, retry=False):
|
def get_postgresql_status(self, retry=False):
|
||||||
|
postgresql = self.server.patroni.postgresql
|
||||||
try:
|
try:
|
||||||
cluster = self.server.patroni.dcs.cluster
|
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('')
|
raise RetryFailedError('')
|
||||||
stmt = ("WITH replication_info AS ("
|
stmt = ("SELECT " + postgresql.POSTMASTER_START_TIME + ", " + postgresql.TL_LSN + ","
|
||||||
"SELECT usename, application_name, client_addr, state, sync_state, sync_priority"
|
" pg_catalog.pg_last_xact_replay_timestamp(),"
|
||||||
" FROM pg_catalog.pg_stat_replication) SELECT"
|
" pg_catalog.array_to_json(pg_catalog.array_agg(pg_catalog.row_to_json(ri))) "
|
||||||
" pg_catalog.to_char(pg_catalog.pg_postmaster_start_time(), 'YYYY-MM-DD HH24:MI:SS.MS TZ'),"
|
"FROM (SELECT (SELECT rolname FROM pg_authid WHERE oid = usesysid) AS usename,"
|
||||||
" CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0"
|
" application_name, client_addr, w.state, sync_state, sync_priority"
|
||||||
" ELSE ('x' || pg_catalog.substr(pg_catalog.pg_{0}file_name("
|
" FROM pg_catalog.pg_stat_get_wal_senders() w, pg_catalog.pg_stat_get_activity(pid)) AS ri")
|
||||||
"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,"
|
|
||||||
" 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(), "
|
|
||||||
"(SELECT pg_catalog.array_to_json(pg_catalog.array_agg("
|
|
||||||
"pg_catalog.row_to_json(ri))) FROM replication_info ri)")
|
|
||||||
|
|
||||||
row = self.query(stmt.format(self.server.patroni.postgresql.wal_name,
|
row = self.query(stmt.format(postgresql.wal_name, postgresql.lsn_name), retry=retry)[0]
|
||||||
self.server.patroni.postgresql.lsn_name), retry=retry)[0]
|
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
'state': self.server.patroni.postgresql.state,
|
'state': postgresql.state,
|
||||||
'postmaster_start_time': row[0],
|
'postmaster_start_time': row[0],
|
||||||
'role': 'replica' if row[1] == 0 else 'master',
|
'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': ({
|
'xlog': ({
|
||||||
'received_location': row[3],
|
'received_location': row[4] or row[3],
|
||||||
'replayed_location': row[4],
|
'replayed_location': row[3],
|
||||||
'replayed_timestamp': row[5],
|
'replayed_timestamp': row[6],
|
||||||
'paused': row[6]} if row[1] == 0 else {
|
'paused': row[5]} if row[1] == 0 else {
|
||||||
'location': row[2]
|
'location': row[2]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if result['role'] == 'replica' and self.server.patroni.ha.is_standby_cluster():
|
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:
|
if row[1] > 0:
|
||||||
result['timeline'] = row[1]
|
result['timeline'] = row[1]
|
||||||
else:
|
else:
|
||||||
leader_timeline = None if not cluster or cluster.is_unlocked() else cluster.leader.timeline
|
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]:
|
if row[7]:
|
||||||
result['replication'] = row[7]
|
result['replication'] = row[7]
|
||||||
|
|
||||||
return result
|
except (psycopg.Error, RetryFailedError, PostgresConnectionException):
|
||||||
except (psycopg2.Error, RetryFailedError, PostgresConnectionException):
|
state = postgresql.state
|
||||||
state = self.server.patroni.postgresql.state
|
|
||||||
if state == 'running':
|
if state == 'running':
|
||||||
logger.exception('get_postgresql_status')
|
logger.exception('get_postgresql_status')
|
||||||
state = 'unknown'
|
state = 'unknown'
|
||||||
return {'state': state, 'role': self.server.patroni.postgresql.role}
|
result = {'state': state, 'role': postgresql.role}
|
||||||
|
|
||||||
|
if not cluster or cluster.is_unlocked():
|
||||||
|
result['cluster_unlocked'] = True
|
||||||
|
result['dcs_last_seen'] = self.server.patroni.dcs.last_seen
|
||||||
|
return result
|
||||||
|
|
||||||
|
def handle_one_request(self):
|
||||||
|
self.__start_time = time.time()
|
||||||
|
BaseHTTPRequestHandler.handle_one_request(self)
|
||||||
|
|
||||||
def log_message(self, fmt, *args):
|
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):
|
class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
||||||
|
# On 3.7+ the `ThreadingMixIn` gathers all non-daemon worker threads in order to join on them at server close.
|
||||||
|
daemon_threads = True # Make worker threads "fire and forget" to prevent a memory leak.
|
||||||
|
|
||||||
def __init__(self, patroni, config):
|
def __init__(self, patroni, config):
|
||||||
self.patroni = patroni
|
self.patroni = patroni
|
||||||
self.__listen = None
|
self.__listen = None
|
||||||
self.__initialize(config)
|
self.__ssl_options = None
|
||||||
self.__set_config_parameters(config)
|
self.__ssl_serial_number = None
|
||||||
|
self._received_new_cert = False
|
||||||
|
self.reload_config(config)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
|
|
||||||
def query(self, sql, *params):
|
def query(self, sql, *params):
|
||||||
@@ -506,7 +701,7 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
|||||||
with self.patroni.postgresql.connection().cursor() as cursor:
|
with self.patroni.postgresql.connection().cursor() as cursor:
|
||||||
cursor.execute(sql, params)
|
cursor.execute(sql, params)
|
||||||
return [r for r in cursor]
|
return [r for r in cursor]
|
||||||
except psycopg2.Error as e:
|
except psycopg.Error as e:
|
||||||
if cursor and cursor.connection.closed == 0:
|
if cursor and cursor.connection.closed == 0:
|
||||||
raise e
|
raise e
|
||||||
raise PostgresConnectionException('connection problems')
|
raise PostgresConnectionException('connection problems')
|
||||||
@@ -519,7 +714,7 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
|||||||
fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
|
fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
|
||||||
|
|
||||||
def check_basic_auth_key(self, key):
|
def check_basic_auth_key(self, key):
|
||||||
return self.__auth_key == key
|
return hmac.compare_digest(self.__auth_key, key.encode('utf-8'))
|
||||||
|
|
||||||
def check_auth_header(self, auth_header):
|
def check_auth_header(self, auth_header):
|
||||||
if self.__auth_key:
|
if self.__auth_key:
|
||||||
@@ -529,12 +724,43 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
|||||||
return 'not authenticated'
|
return 'not authenticated'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __get_ssl_options(config):
|
def __resolve_ips(host, port):
|
||||||
return {option: config[option] for option in ['certfile', 'keyfile'] if option in config}
|
try:
|
||||||
|
for _, _, _, _, sa in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP):
|
||||||
|
yield ip_network(sa[0])
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Failed to resolve %s: %r', host, e)
|
||||||
|
|
||||||
def __set_config_parameters(self, config):
|
def __members_ips(self):
|
||||||
self.__auth_key = base64.b64encode(config['auth'].encode('utf-8')).decode('utf-8') if 'auth' in config else None
|
cluster = self.patroni.dcs.cluster
|
||||||
self.connection_string = uri(self.__protocol, config.get('connect_address') or self.__listen, 'patroni')
|
if self.__allowlist_include_members and cluster:
|
||||||
|
for member in cluster.members:
|
||||||
|
if member.api_url:
|
||||||
|
try:
|
||||||
|
r = urlparse(member.api_url)
|
||||||
|
host = r.hostname
|
||||||
|
port = r.port or (443 if r.scheme == 'https' else 80)
|
||||||
|
for ip in self.__resolve_ips(host, port):
|
||||||
|
yield ip
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug('Failed to parse url %s: %r', member.api_url, e)
|
||||||
|
|
||||||
|
def check_access(self, rh):
|
||||||
|
if self.__allowlist or self.__allowlist_include_members:
|
||||||
|
incoming_ip = rh.client_address[0]
|
||||||
|
incoming_ip = ip_address(incoming_ip.decode('utf-8') if six.PY2 else incoming_ip)
|
||||||
|
if not any(incoming_ip in net for net in self.__allowlist + tuple(self.__members_ips())):
|
||||||
|
return rh._write_response(403, 'Access is denied')
|
||||||
|
|
||||||
|
if not hasattr(rh.request, 'getpeercert') or not rh.request.getpeercert(): # valid client cert isn't present
|
||||||
|
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)
|
||||||
|
return True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __has_dual_stack():
|
def __has_dual_stack():
|
||||||
@@ -553,7 +779,7 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
|||||||
|
|
||||||
def __httpserver_init(self, host, port):
|
def __httpserver_init(self, host, port):
|
||||||
dual_stack = self.__has_dual_stack()
|
dual_stack = self.__has_dual_stack()
|
||||||
if host == '':
|
if host in ('', '*'):
|
||||||
host = None
|
host = None
|
||||||
|
|
||||||
info = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
|
info = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
|
||||||
@@ -561,48 +787,120 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
|||||||
info.sort(key=lambda x: x[0] == socket.AF_INET, reverse=not dual_stack)
|
info.sort(key=lambda x: x[0] == socket.AF_INET, reverse=not dual_stack)
|
||||||
|
|
||||||
self.address_family = info[0][0]
|
self.address_family = info[0][0]
|
||||||
HTTPServer.__init__(self, info[0][-1][:2], RestApiHandler)
|
|
||||||
|
|
||||||
def __initialize(self, config):
|
|
||||||
try:
|
try:
|
||||||
host, port = split_host_port(config['listen'], None)
|
HTTPServer.__init__(self, info[0][-1][:2], RestApiHandler)
|
||||||
|
except socket.error:
|
||||||
|
logger.error(
|
||||||
|
"Couldn't start a service on '%s:%s', please check your `restapi.listen` configuration", host, port)
|
||||||
|
raise
|
||||||
|
|
||||||
|
def __initialize(self, listen, ssl_options):
|
||||||
|
try:
|
||||||
|
host, port = split_host_port(listen, None)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise ValueError('Invalid "restapi" config: expected <HOST>:<PORT> for "listen", but got "{0}"'
|
raise ValueError('Invalid "restapi" config: expected <HOST>:<PORT> for "listen", but got "{0}"'
|
||||||
.format(config['listen']))
|
.format(listen))
|
||||||
|
|
||||||
if self.__listen is not None: # changing config in runtime
|
reloading_config = self.__listen is not None # changing config in runtime
|
||||||
|
if reloading_config:
|
||||||
self.shutdown()
|
self.shutdown()
|
||||||
|
# Rely on ThreadingMixIn.server_close() to have all requests terminate before we continue
|
||||||
|
self.server_close()
|
||||||
|
|
||||||
self.__listen = config['listen']
|
self.__listen = listen
|
||||||
self.__ssl_options = self.__get_ssl_options(config)
|
self.__ssl_options = ssl_options
|
||||||
|
self._received_new_cert = False # reset to False after reload_config()
|
||||||
|
|
||||||
self.__httpserver_init(host, port)
|
self.__httpserver_init(host, port)
|
||||||
Thread.__init__(self, target=self.serve_forever)
|
Thread.__init__(self, target=self.serve_forever)
|
||||||
self._set_fd_cloexec(self.socket)
|
self._set_fd_cloexec(self.socket)
|
||||||
|
|
||||||
self.__protocol = 'http'
|
|
||||||
|
|
||||||
# wrap socket with ssl if 'certfile' is defined in a config.yaml
|
# wrap socket with ssl if 'certfile' is defined in a config.yaml
|
||||||
# Sometime it's also needed to pass reference to a 'keyfile'.
|
# Sometime it's also needed to pass reference to a 'keyfile'.
|
||||||
|
self.__protocol = 'https' if ssl_options.get('certfile') else 'http'
|
||||||
|
if self.__protocol == 'https':
|
||||||
|
import ssl
|
||||||
|
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, cafile=ssl_options.get('cafile'))
|
||||||
|
if ssl_options.get('ciphers'):
|
||||||
|
ctx.set_ciphers(ssl_options['ciphers'])
|
||||||
|
ctx.load_cert_chain(certfile=ssl_options['certfile'], keyfile=ssl_options.get('keyfile'),
|
||||||
|
password=ssl_options.get('keyfile_password'))
|
||||||
|
verify_client = ssl_options.get('verify_client')
|
||||||
|
if verify_client:
|
||||||
|
modes = {'none': ssl.CERT_NONE, 'optional': ssl.CERT_OPTIONAL, 'required': ssl.CERT_REQUIRED}
|
||||||
|
if verify_client in modes:
|
||||||
|
ctx.verify_mode = modes[verify_client]
|
||||||
|
else:
|
||||||
|
logger.error('Bad value in the "restapi.verify_client": %s', verify_client)
|
||||||
|
self.__ssl_serial_number = self.get_certificate_serial_number()
|
||||||
|
self.socket = ctx.wrap_socket(self.socket, server_side=True, do_handshake_on_connect=False)
|
||||||
|
if reloading_config:
|
||||||
|
self.start()
|
||||||
|
|
||||||
|
def process_request_thread(self, request, client_address):
|
||||||
|
enable_keepalive(request, 10, 3)
|
||||||
|
if hasattr(request, 'context'): # SSLSocket
|
||||||
|
request.do_handshake()
|
||||||
|
super(RestApiServer, self).process_request_thread(request, client_address)
|
||||||
|
|
||||||
|
def shutdown_request(self, request):
|
||||||
|
if hasattr(request, 'context'): # SSLSocket
|
||||||
|
request.unwrap()
|
||||||
|
super(RestApiServer, self).shutdown_request(request)
|
||||||
|
|
||||||
|
def get_certificate_serial_number(self):
|
||||||
if self.__ssl_options.get('certfile'):
|
if self.__ssl_options.get('certfile'):
|
||||||
import ssl
|
import ssl
|
||||||
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
|
try:
|
||||||
ctx.load_cert_chain(**self.__ssl_options)
|
crt = ssl._ssl._test_decode_cert(self.__ssl_options['certfile'])
|
||||||
self.socket = ctx.wrap_socket(self.socket, server_side=True)
|
return crt.get('serialNumber')
|
||||||
self.__protocol = 'https'
|
except ssl.SSLError as e:
|
||||||
|
logger.error('Failed to get serial number from certificate %s: %r', self.__ssl_options['certfile'], e)
|
||||||
|
|
||||||
|
def reload_local_certificate(self):
|
||||||
|
if self.__protocol == 'https':
|
||||||
|
on_disk_cert_serial_number = self.get_certificate_serial_number()
|
||||||
|
if on_disk_cert_serial_number != self.__ssl_serial_number:
|
||||||
|
self._received_new_cert = True
|
||||||
|
self.__ssl_serial_number = on_disk_cert_serial_number
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _build_allowlist(self, value):
|
||||||
|
if isinstance(value, list):
|
||||||
|
for v in value:
|
||||||
|
if '/' in v: # netmask
|
||||||
|
try:
|
||||||
|
yield ip_network(v)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Invalid value "%s" in the allowlist: %r', v, e)
|
||||||
|
else: # ip or hostname, try to resolve it
|
||||||
|
for ip in self.__resolve_ips(v, 8080):
|
||||||
|
yield ip
|
||||||
|
|
||||||
def reload_config(self, config):
|
def reload_config(self, config):
|
||||||
if 'listen' not in config: # changing config in runtime
|
if 'listen' not in config: # changing config in runtime
|
||||||
raise ValueError('Can not find "restapi.listen" config')
|
raise ValueError('Can not find "restapi.listen" config')
|
||||||
|
|
||||||
elif (self.__listen != config['listen'] or self.__ssl_options != self.__get_ssl_options(config)) \
|
self.__allowlist = tuple(self._build_allowlist(config.get('allowlist')))
|
||||||
and self.__initialize(config):
|
self.__allowlist_include_members = config.get('allowlist_include_members')
|
||||||
self.start()
|
|
||||||
self.__set_config_parameters(config)
|
ssl_options = {n: config[n] for n in ('certfile', 'keyfile', 'keyfile_password',
|
||||||
|
'cafile', 'ciphers') 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()
|
||||||
|
|
||||||
|
if self.__listen != config['listen'] or self.__ssl_options != ssl_options or self._received_new_cert:
|
||||||
|
self.__initialize(config['listen'], ssl_options)
|
||||||
|
|
||||||
|
self.__auth_key = base64.b64encode(config['auth'].encode('utf-8')) if 'auth' in config else None
|
||||||
|
self.connection_string = uri(self.__protocol, config.get('connect_address') or self.__listen, 'patroni')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def handle_error(request, client_address):
|
def handle_error(request, client_address):
|
||||||
address, port = client_address
|
logger.warning('Exception happened during processing of request from %s:%s',
|
||||||
logger.warning('Exception happened during processing of request from {}:{}'.format(address, port))
|
client_address[0], client_address[1])
|
||||||
logger.warning(traceback.format_exc())
|
logger.warning(traceback.format_exc())
|
||||||
|
|||||||
@@ -110,6 +110,12 @@ class AsyncExecutor(object):
|
|||||||
def run_async(self, func, args=()):
|
def run_async(self, func, args=()):
|
||||||
Thread(target=self.run, args=(func, args)).start()
|
Thread(target=self.run, args=(func, args)).start()
|
||||||
|
|
||||||
|
def try_run_async(self, action, func, args=()):
|
||||||
|
prev = self.schedule(action)
|
||||||
|
if prev is None:
|
||||||
|
return self.run_async(func, args)
|
||||||
|
return 'Failed to run {0}, {1} is already in progress'.format(action, prev)
|
||||||
|
|
||||||
def cancel(self):
|
def cancel(self):
|
||||||
with self:
|
with self:
|
||||||
with self._scheduled_action_lock:
|
with self._scheduled_action_lock:
|
||||||
|
|||||||
+127
-57
@@ -2,19 +2,38 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
from patroni import PATRONI_ENV_PREFIX
|
||||||
|
from patroni.exceptions import ConfigParseError
|
||||||
from patroni.dcs import ClusterConfig
|
from patroni.dcs import ClusterConfig
|
||||||
from patroni.postgresql.config import ConfigHandler
|
from patroni.postgresql.config import CaseInsensitiveDict, ConfigHandler
|
||||||
from patroni.utils import deep_compare, parse_bool, parse_int, patch_config
|
from patroni.utils import deep_compare, parse_bool, parse_int, patch_config
|
||||||
from requests.structures import CaseInsensitiveDict
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_AUTH_ALLOWED_PARAMETERS = (
|
||||||
|
'username',
|
||||||
|
'password',
|
||||||
|
'sslmode',
|
||||||
|
'sslcert',
|
||||||
|
'sslkey',
|
||||||
|
'sslpassword',
|
||||||
|
'sslrootcert',
|
||||||
|
'sslcrl',
|
||||||
|
'sslcrldir',
|
||||||
|
'gssencmode',
|
||||||
|
'channel_binding'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def default_validator(conf):
|
||||||
|
if not conf:
|
||||||
|
raise ConfigParseError("Config is empty.")
|
||||||
|
|
||||||
|
|
||||||
class Config(object):
|
class Config(object):
|
||||||
"""
|
"""
|
||||||
@@ -36,17 +55,19 @@ class Config(object):
|
|||||||
to work with it as with the old `config` object.
|
to work with it as with the old `config` object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
PATRONI_ENV_PREFIX = 'PATRONI_'
|
|
||||||
PATRONI_CONFIG_VARIABLE = PATRONI_ENV_PREFIX + 'CONFIGURATION'
|
PATRONI_CONFIG_VARIABLE = PATRONI_ENV_PREFIX + 'CONFIGURATION'
|
||||||
|
|
||||||
__CACHE_FILENAME = 'patroni.dynamic.json'
|
__CACHE_FILENAME = 'patroni.dynamic.json'
|
||||||
__DEFAULT_CONFIG = {
|
__DEFAULT_CONFIG = {
|
||||||
'ttl': 30, 'loop_wait': 10, 'retry_timeout': 10,
|
'ttl': 30, 'loop_wait': 10, 'retry_timeout': 10,
|
||||||
'maximum_lag_on_failover': 1048576,
|
'maximum_lag_on_failover': 1048576,
|
||||||
|
'maximum_lag_on_syncnode': -1,
|
||||||
'check_timeline': False,
|
'check_timeline': False,
|
||||||
'master_start_timeout': 300,
|
'master_start_timeout': 300,
|
||||||
|
'master_stop_timeout': 0,
|
||||||
'synchronous_mode': False,
|
'synchronous_mode': False,
|
||||||
'synchronous_mode_strict': False,
|
'synchronous_mode_strict': False,
|
||||||
|
'synchronous_node_count': 1,
|
||||||
'standby_cluster': {
|
'standby_cluster': {
|
||||||
'create_replica_methods': '',
|
'create_replica_methods': '',
|
||||||
'host': '',
|
'host': '',
|
||||||
@@ -59,34 +80,34 @@ class Config(object):
|
|||||||
'postgresql': {
|
'postgresql': {
|
||||||
'bin_dir': '',
|
'bin_dir': '',
|
||||||
'use_slots': True,
|
'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': {
|
'watchdog': {
|
||||||
'mode': 'automatic',
|
'mode': 'automatic',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, configfile, validator=default_validator):
|
||||||
self._modify_index = -1
|
self._modify_index = -1
|
||||||
self._dynamic_configuration = {}
|
self._dynamic_configuration = {}
|
||||||
|
|
||||||
self.__environment_configuration = self._build_environment_configuration()
|
self.__environment_configuration = self._build_environment_configuration()
|
||||||
|
|
||||||
# Patroni reads the configuration from the command-line argument if it exists, otherwise from the environment
|
# Patroni reads the configuration from the command-line argument if it exists, otherwise from the environment
|
||||||
self._config_file = len(sys.argv) >= 2 and os.path.isfile(sys.argv[1]) and sys.argv[1]
|
self._config_file = configfile and os.path.exists(configfile) and configfile
|
||||||
if self._config_file:
|
if self._config_file:
|
||||||
self._local_configuration = self._load_config_file()
|
self._local_configuration = self._load_config_file()
|
||||||
else:
|
else:
|
||||||
config_env = os.environ.pop(self.PATRONI_CONFIG_VARIABLE, None)
|
config_env = os.environ.pop(self.PATRONI_CONFIG_VARIABLE, None)
|
||||||
self._local_configuration = config_env and yaml.safe_load(config_env) or self.__environment_configuration
|
self._local_configuration = config_env and yaml.safe_load(config_env) or self.__environment_configuration
|
||||||
if not self._local_configuration:
|
if validator:
|
||||||
print('Usage: {0} config.yml'.format(sys.argv[0]))
|
errors = validator(self._local_configuration)
|
||||||
print('\tPatroni may also read the configuration from the {0} environment variable'.
|
if errors:
|
||||||
format(self.PATRONI_CONFIG_VARIABLE))
|
raise ConfigParseError("\n".join(errors))
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
self.__effective_configuration = self._build_effective_configuration({}, self._local_configuration)
|
self.__effective_configuration = self._build_effective_configuration({}, self._local_configuration)
|
||||||
self._data_dir = self.__effective_configuration['postgresql']['data_dir']
|
self._data_dir = self.__effective_configuration.get('postgresql', {}).get('data_dir', "")
|
||||||
self._cache_file = os.path.join(self._data_dir, self.__CACHE_FILENAME)
|
self._cache_file = os.path.join(self._data_dir, self.__CACHE_FILENAME)
|
||||||
self._load_cache()
|
self._load_cache()
|
||||||
self._cache_needs_saving = False
|
self._cache_needs_saving = False
|
||||||
@@ -102,10 +123,30 @@ class Config(object):
|
|||||||
def check_mode(self, mode):
|
def check_mode(self, mode):
|
||||||
return bool(parse_bool(self._dynamic_configuration.get(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):
|
def _load_config_file(self):
|
||||||
"""Loads config.yaml from filesystem and applies some values which were set via ENV"""
|
"""Loads config.yaml from filesystem and applies some values which were set via ENV"""
|
||||||
with open(self._config_file) as f:
|
config = self._load_config_path(self._config_file)
|
||||||
config = yaml.safe_load(f)
|
|
||||||
patch_config(config, self.__environment_configuration)
|
patch_config(config, self.__environment_configuration)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
@@ -158,23 +199,19 @@ class Config(object):
|
|||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Exception when setting dynamic_configuration')
|
logger.exception('Exception when setting dynamic_configuration')
|
||||||
|
|
||||||
def reload_local_configuration(self, dry_run=False):
|
def reload_local_configuration(self):
|
||||||
if self.config_file:
|
if self.config_file:
|
||||||
try:
|
try:
|
||||||
configuration = self._load_config_file()
|
configuration = self._load_config_file()
|
||||||
if not deep_compare(self._local_configuration, configuration):
|
if not deep_compare(self._local_configuration, configuration):
|
||||||
new_configuration = self._build_effective_configuration(self._dynamic_configuration, configuration)
|
new_configuration = self._build_effective_configuration(self._dynamic_configuration, configuration)
|
||||||
if dry_run:
|
|
||||||
return not deep_compare(new_configuration, self.__effective_configuration)
|
|
||||||
self._local_configuration = configuration
|
self._local_configuration = configuration
|
||||||
self.__effective_configuration = new_configuration
|
self.__effective_configuration = new_configuration
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
logger.info('No configuration items changed, nothing to reload.')
|
logger.info('No local configuration items changed.')
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Exception when reloading local configuration from %s', self.config_file)
|
logger.exception('Exception when reloading local configuration from %s', self.config_file)
|
||||||
if dry_run:
|
|
||||||
raise
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _process_postgresql_parameters(parameters, is_local=False):
|
def _process_postgresql_parameters(parameters, is_local=False):
|
||||||
@@ -190,13 +227,14 @@ class Config(object):
|
|||||||
for name, value in (value or {}).items():
|
for name, value in (value or {}).items():
|
||||||
if name == 'parameters':
|
if name == 'parameters':
|
||||||
config['postgresql'][name].update(self._process_postgresql_parameters(value))
|
config['postgresql'][name].update(self._process_postgresql_parameters(value))
|
||||||
elif name not in ('connect_address', 'listen', 'data_dir', 'pgpass', 'authentication'):
|
elif name not in ('connect_address', 'proxy_address', 'listen',
|
||||||
|
'config_dir', 'data_dir', 'pgpass', 'authentication'):
|
||||||
config['postgresql'][name] = deepcopy(value)
|
config['postgresql'][name] = deepcopy(value)
|
||||||
elif name == 'standby_cluster':
|
elif name == 'standby_cluster':
|
||||||
for name, value in (value or {}).items():
|
for name, value in (value or {}).items():
|
||||||
if name in self.__DEFAULT_CONFIG['standby_cluster']:
|
if name in self.__DEFAULT_CONFIG['standby_cluster']:
|
||||||
config['standby_cluster'][name] = deepcopy(value)
|
config['standby_cluster'][name] = deepcopy(value)
|
||||||
elif name in config: # only variables present in __DEFAULT_CONFIG allowed to be overriden from DCS
|
elif name in config: # only variables present in __DEFAULT_CONFIG allowed to be overridden from DCS
|
||||||
if name in ('synchronous_mode', 'synchronous_mode_strict'):
|
if name in ('synchronous_mode', 'synchronous_mode_strict'):
|
||||||
config[name] = value
|
config[name] = value
|
||||||
else:
|
else:
|
||||||
@@ -208,7 +246,7 @@ class Config(object):
|
|||||||
ret = defaultdict(dict)
|
ret = defaultdict(dict)
|
||||||
|
|
||||||
def _popenv(name):
|
def _popenv(name):
|
||||||
return os.environ.pop(Config.PATRONI_ENV_PREFIX + name.upper(), None)
|
return os.environ.pop(PATRONI_ENV_PREFIX + name.upper(), None)
|
||||||
|
|
||||||
for param in ('name', 'namespace', 'scope'):
|
for param in ('name', 'namespace', 'scope'):
|
||||||
value = _popenv(param)
|
value = _popenv(param)
|
||||||
@@ -217,7 +255,7 @@ class Config(object):
|
|||||||
|
|
||||||
def _fix_log_env(name, oldname):
|
def _fix_log_env(name, oldname):
|
||||||
value = _popenv(oldname)
|
value = _popenv(oldname)
|
||||||
name = Config.PATRONI_ENV_PREFIX + 'LOG_' + name.upper()
|
name = PATRONI_ENV_PREFIX + 'LOG_' + name.upper()
|
||||||
if value and name not in os.environ:
|
if value and name not in os.environ:
|
||||||
os.environ[name] = value
|
os.environ[name] = value
|
||||||
|
|
||||||
@@ -230,10 +268,45 @@ class Config(object):
|
|||||||
if value:
|
if value:
|
||||||
ret[section][param] = value
|
ret[section][param] = value
|
||||||
|
|
||||||
_set_section_values('restapi', ['listen', 'connect_address', 'certfile', 'keyfile'])
|
_set_section_values('restapi', ['listen', 'connect_address', 'certfile', 'keyfile', 'keyfile_password',
|
||||||
_set_section_values('postgresql', ['listen', 'connect_address', 'config_dir', 'data_dir', 'pgpass', 'bin_dir'])
|
'cafile', 'ciphers', 'verify_client', 'http_extra_headers',
|
||||||
_set_section_values('log', ['level', 'format', 'dateformat', 'max_queue_size',
|
'https_extra_headers', 'allowlist', 'allowlist_include_members'])
|
||||||
|
_set_section_values('ctl', ['insecure', 'cacert', 'certfile', 'keyfile', 'keyfile_password'])
|
||||||
|
_set_section_values('postgresql', ['listen', 'connect_address', 'proxy_address',
|
||||||
|
'config_dir', 'data_dir', 'pgpass', 'bin_dir'])
|
||||||
|
_set_section_values('log', ['level', 'traceback_level', 'format', 'dateformat', 'max_queue_size',
|
||||||
'dir', 'file_size', 'file_num', 'loggers'])
|
'dir', 'file_size', 'file_num', 'loggers'])
|
||||||
|
_set_section_values('raft', ['data_dir', 'self_addr', 'partner_addrs', 'password', 'bind_addr'])
|
||||||
|
|
||||||
|
for first, second in (('restapi', 'allowlist_include_members'), ('ctl', 'insecure')):
|
||||||
|
value = ret.get(first, {}).pop(second, None)
|
||||||
|
if value:
|
||||||
|
value = parse_bool(value)
|
||||||
|
if value is not None:
|
||||||
|
ret[first][second] = value
|
||||||
|
|
||||||
|
for second in ('max_queue_size', 'file_size', 'file_num'):
|
||||||
|
value = ret.get('log', {}).pop(second, None)
|
||||||
|
if value:
|
||||||
|
value = parse_int(value)
|
||||||
|
if value is not None:
|
||||||
|
ret['log'][second] = value
|
||||||
|
|
||||||
|
def _parse_list(value):
|
||||||
|
if not (value.strip().startswith('-') or '[' in value):
|
||||||
|
value = '[{0}]'.format(value)
|
||||||
|
try:
|
||||||
|
return yaml.safe_load(value)
|
||||||
|
except Exception:
|
||||||
|
logger.exception('Exception when parsing list %s', value)
|
||||||
|
return None
|
||||||
|
|
||||||
|
for first, second in (('raft', 'partner_addrs'), ('restapi', 'allowlist')):
|
||||||
|
value = ret.get(first, {}).pop(second, None)
|
||||||
|
if value:
|
||||||
|
value = _parse_list(value)
|
||||||
|
if value:
|
||||||
|
ret[first][second] = value
|
||||||
|
|
||||||
def _parse_dict(value):
|
def _parse_dict(value):
|
||||||
if not value.strip().startswith('{'):
|
if not value.strip().startswith('{'):
|
||||||
@@ -244,15 +317,17 @@ class Config(object):
|
|||||||
logger.exception('Exception when parsing dict %s', value)
|
logger.exception('Exception when parsing dict %s', value)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
value = ret.get('log', {}).pop('loggers', None)
|
for first, params in (('restapi', ('http_extra_headers', 'https_extra_headers')), ('log', ('loggers',))):
|
||||||
|
for second in params:
|
||||||
|
value = ret.get(first, {}).pop(second, None)
|
||||||
if value:
|
if value:
|
||||||
value = _parse_dict(value)
|
value = _parse_dict(value)
|
||||||
if value:
|
if value:
|
||||||
ret['log']['loggers'] = value
|
ret[first][second] = value
|
||||||
|
|
||||||
def _get_auth(name):
|
def _get_auth(name, params=None):
|
||||||
ret = {}
|
ret = {}
|
||||||
for param in ('username', 'password'):
|
for param in params or _AUTH_ALLOWED_PARAMETERS[:2]:
|
||||||
value = _popenv(name + '_' + param)
|
value = _popenv(name + '_' + param)
|
||||||
if value:
|
if value:
|
||||||
ret[param] = value
|
ret[param] = value
|
||||||
@@ -264,47 +339,40 @@ class Config(object):
|
|||||||
|
|
||||||
authentication = {}
|
authentication = {}
|
||||||
for user_type in ('replication', 'superuser', 'rewind'):
|
for user_type in ('replication', 'superuser', 'rewind'):
|
||||||
entry = _get_auth(user_type)
|
entry = _get_auth(user_type, _AUTH_ALLOWED_PARAMETERS)
|
||||||
if entry:
|
if entry:
|
||||||
authentication[user_type] = entry
|
authentication[user_type] = entry
|
||||||
|
|
||||||
if authentication:
|
if authentication:
|
||||||
ret['postgresql']['authentication'] = authentication
|
ret['postgresql']['authentication'] = authentication
|
||||||
|
|
||||||
def _parse_list(value):
|
|
||||||
if not (value.strip().startswith('-') or '[' in value):
|
|
||||||
value = '[{0}]'.format(value)
|
|
||||||
try:
|
|
||||||
return yaml.safe_load(value)
|
|
||||||
except Exception:
|
|
||||||
logger.exception('Exception when parsing list %s', value)
|
|
||||||
return None
|
|
||||||
|
|
||||||
for param in list(os.environ.keys()):
|
for param in list(os.environ.keys()):
|
||||||
if param.startswith(Config.PATRONI_ENV_PREFIX):
|
if param.startswith(PATRONI_ENV_PREFIX):
|
||||||
# PATRONI_(ETCD|CONSUL|ZOOKEEPER|EXHIBITOR|...)_(HOSTS?|PORT|..)
|
# PATRONI_(ETCD|CONSUL|ZOOKEEPER|EXHIBITOR|...)_(HOSTS?|PORT|..)
|
||||||
name, suffix = (param[8:].split('_', 1) + [''])[:2]
|
name, suffix = (param[8:].split('_', 1) + [''])[:2]
|
||||||
if suffix in ('HOST', 'HOSTS', 'PORT', 'USE_PROXIES', 'PROTOCOL', 'SRV', 'URL', 'PROXY',
|
if suffix in ('HOST', 'HOSTS', 'PORT', 'USE_PROXIES', 'PROTOCOL', 'SRV', 'SRV_SUFFIX', 'URL', 'PROXY',
|
||||||
'CACERT', 'CERT', 'KEY', 'VERIFY', 'TOKEN', 'CHECKS', 'DC', 'CONSISTENCY',
|
'CACERT', 'CERT', 'KEY', 'VERIFY', 'TOKEN', 'CHECKS', 'DC', 'CONSISTENCY',
|
||||||
'REGISTER_SERVICE', 'SERVICE_CHECK_INTERVAL', 'NAMESPACE', 'CONTEXT',
|
'REGISTER_SERVICE', 'SERVICE_CHECK_INTERVAL', 'SERVICE_CHECK_TLS_SERVER_NAME',
|
||||||
'USE_ENDPOINTS', 'SCOPE_LABEL', 'ROLE_LABEL', 'POD_IP', 'PORTS', 'LABELS') and name:
|
'NAMESPACE', 'CONTEXT', 'USE_ENDPOINTS', 'SCOPE_LABEL', 'ROLE_LABEL', 'POD_IP',
|
||||||
|
'PORTS', 'LABELS', 'BYPASS_API_SERVICE', 'KEY_PASSWORD', 'USE_SSL', 'SET_ACLS') and name:
|
||||||
value = os.environ.pop(param)
|
value = os.environ.pop(param)
|
||||||
if suffix == 'PORT':
|
if suffix == 'PORT':
|
||||||
value = value and parse_int(value)
|
value = value and parse_int(value)
|
||||||
elif suffix in ('HOSTS', 'PORTS', 'CHECKS'):
|
elif suffix in ('HOSTS', 'PORTS', 'CHECKS'):
|
||||||
value = value and _parse_list(value)
|
value = value and _parse_list(value)
|
||||||
elif suffix == 'LABELS':
|
elif suffix in ('LABELS', 'SET_ACLS'):
|
||||||
value = _parse_dict(value)
|
value = _parse_dict(value)
|
||||||
elif suffix in ('USE_PROXIES', 'REGISTER_SERVICE'):
|
elif suffix in ('USE_PROXIES', 'REGISTER_SERVICE', 'USE_ENDPOINTS', 'BYPASS_API_SERVICE', 'VERIFY'):
|
||||||
value = parse_bool(value)
|
value = parse_bool(value)
|
||||||
if value:
|
if value:
|
||||||
ret[name.lower()][suffix.lower()] = value
|
ret[name.lower()][suffix.lower()] = value
|
||||||
if 'etcd' in ret:
|
for dcs in ('etcd', 'etcd3'):
|
||||||
ret['etcd'].update(_get_auth('etcd'))
|
if dcs in ret:
|
||||||
|
ret[dcs].update(_get_auth(dcs))
|
||||||
|
|
||||||
users = {}
|
users = {}
|
||||||
for param in list(os.environ.keys()):
|
for param in list(os.environ.keys()):
|
||||||
if param.startswith(Config.PATRONI_ENV_PREFIX):
|
if param.startswith(PATRONI_ENV_PREFIX):
|
||||||
name, suffix = (param[8:].rsplit('_', 1) + [''])[:2]
|
name, suffix = (param[8:].rsplit('_', 1) + [''])[:2]
|
||||||
# PATRONI_<username>_PASSWORD=<password>, PATRONI_<username>_OPTIONS=<option1,option2,...>
|
# PATRONI_<username>_PASSWORD=<password>, PATRONI_<username>_OPTIONS=<option1,option2,...>
|
||||||
# CREATE USER "<username>" WITH <OPTIONS> PASSWORD '<password>'
|
# CREATE USER "<username>" WITH <OPTIONS> PASSWORD '<password>'
|
||||||
@@ -333,13 +401,8 @@ class Config(object):
|
|||||||
elif name not in config or name in ['watchdog']:
|
elif name not in config or name in ['watchdog']:
|
||||||
config[name] = deepcopy(value) if value else {}
|
config[name] = deepcopy(value) if value else {}
|
||||||
|
|
||||||
if os.getenv("PATRONI_DEBUG_MODE"):
|
|
||||||
config['ttl'] = 24 * 60 * 60 # practical infinity for a debugging session
|
|
||||||
config['loop_wait'] = -1
|
|
||||||
|
|
||||||
|
|
||||||
# restapi server expects to get restapi.auth = 'username:password'
|
# restapi server expects to get restapi.auth = 'username:password'
|
||||||
if 'authentication' in config['restapi']:
|
if 'restapi' in config and 'authentication' in config['restapi']:
|
||||||
config['restapi']['auth'] = '{username}:{password}'.format(**config['restapi']['authentication'])
|
config['restapi']['auth'] = '{username}:{password}'.format(**config['restapi']['authentication'])
|
||||||
|
|
||||||
# special treatment for old config
|
# special treatment for old config
|
||||||
@@ -358,6 +421,11 @@ class Config(object):
|
|||||||
if 'superuser' not in pg_config['authentication'] and 'pg_rewind' in pg_config:
|
if 'superuser' not in pg_config['authentication'] and 'pg_rewind' in pg_config:
|
||||||
pg_config['authentication']['superuser'] = pg_config['pg_rewind']
|
pg_config['authentication']['superuser'] = pg_config['pg_rewind']
|
||||||
|
|
||||||
|
# handle setting additional connection parameters that may be available
|
||||||
|
# in the configuration file, such as SSL connection parameters
|
||||||
|
for name, value in pg_config['authentication'].items():
|
||||||
|
pg_config['authentication'][name] = {n: v for n, v in value.items() if n in _AUTH_ALLOWED_PARAMETERS}
|
||||||
|
|
||||||
# no 'name' in config
|
# no 'name' in config
|
||||||
if 'name' not in config and 'name' in pg_config:
|
if 'name' not in config and 'name' in pg_config:
|
||||||
config['name'] = pg_config['name']
|
config['name'] = pg_config['name']
|
||||||
@@ -368,6 +436,8 @@ class Config(object):
|
|||||||
'retry_timeout',
|
'retry_timeout',
|
||||||
'synchronous_mode',
|
'synchronous_mode',
|
||||||
'synchronous_mode_strict',
|
'synchronous_mode_strict',
|
||||||
|
'synchronous_node_count',
|
||||||
|
'maximum_lag_on_syncnode'
|
||||||
)
|
)
|
||||||
|
|
||||||
pg_config.update({p: config[p] for p in updated_fields if p in config})
|
pg_config.update({p: config[p] for p in updated_fields if p in config})
|
||||||
|
|||||||
+312
-236
@@ -2,12 +2,11 @@
|
|||||||
Patroni Control
|
Patroni Control
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import base64
|
|
||||||
import click
|
import click
|
||||||
import codecs
|
import codecs
|
||||||
import datetime
|
import datetime
|
||||||
import dateutil.parser
|
import dateutil.parser
|
||||||
import cdiff
|
import dateutil.tz
|
||||||
import copy
|
import copy
|
||||||
import difflib
|
import difflib
|
||||||
import io
|
import io
|
||||||
@@ -15,26 +14,31 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import requests
|
import six
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
import tzlocal
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from click import ClickException
|
from click import ClickException
|
||||||
|
from collections import defaultdict
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from patroni.config import Config
|
from prettytable import ALL, FRAME, PrettyTable
|
||||||
from patroni.dcs import get_dcs as _get_dcs
|
|
||||||
from patroni.exceptions import PatroniException
|
|
||||||
from patroni.postgresql import Postgresql
|
|
||||||
from patroni.postgresql.misc import postgres_version_to_int
|
|
||||||
from patroni.utils import patch_config, polling_loop
|
|
||||||
from patroni.version import __version__
|
|
||||||
from prettytable import PrettyTable
|
|
||||||
from six.moves.urllib_parse import urlparse
|
from six.moves.urllib_parse import urlparse
|
||||||
from six import text_type
|
|
||||||
|
try:
|
||||||
|
from ydiff import markup_to_pager, PatchStream
|
||||||
|
except ImportError: # pragma: no cover
|
||||||
|
from cdiff import markup_to_pager, PatchStream
|
||||||
|
|
||||||
|
from .dcs import get_dcs as _get_dcs
|
||||||
|
from .exceptions import PatroniException
|
||||||
|
from .postgresql import Postgresql
|
||||||
|
from .postgresql.misc import postgres_version_to_int
|
||||||
|
from .utils import cluster_as_json, find_executable, patch_config, polling_loop
|
||||||
|
from .request import PatroniRequest
|
||||||
|
from .version import __version__
|
||||||
|
|
||||||
CONFIG_DIR_PATH = click.get_app_dir('patroni')
|
CONFIG_DIR_PATH = click.get_app_dir('patroni')
|
||||||
CONFIG_FILE_PATH = os.path.join(CONFIG_DIR_PATH, 'patronictl.yaml')
|
CONFIG_FILE_PATH = os.path.join(CONFIG_DIR_PATH, 'patronictl.yaml')
|
||||||
@@ -48,14 +52,53 @@ class PatroniCtlException(ClickException):
|
|||||||
pass
|
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 __build_header(self, line):
|
||||||
|
header = self.__table_header[:len(line) - 2]
|
||||||
|
return "".join([line[0], header, line[1 + len(header):]])
|
||||||
|
|
||||||
|
def _stringify_hrule(self, *args, **kwargs):
|
||||||
|
ret = super(PatronictlPrettyTable, self)._stringify_hrule(*args, **kwargs)
|
||||||
|
where = args[1] if len(args) > 1 else kwargs.get('where')
|
||||||
|
if where == 'top_' and self.__table_header:
|
||||||
|
ret = self.__build_header(ret)
|
||||||
|
self.__hline_num += 1
|
||||||
|
return ret
|
||||||
|
|
||||||
|
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:
|
||||||
|
ret = self.__build_header(ret)
|
||||||
|
|
||||||
|
self.__hline_num += 1
|
||||||
|
return ret
|
||||||
|
|
||||||
|
_hrule = property(_get_hline, _set_hline)
|
||||||
|
|
||||||
|
|
||||||
def parse_dcs(dcs):
|
def parse_dcs(dcs):
|
||||||
if dcs is None:
|
if dcs is None:
|
||||||
return None
|
return None
|
||||||
|
elif '//' not in dcs:
|
||||||
|
dcs = '//' + dcs
|
||||||
|
|
||||||
parsed = urlparse(dcs)
|
parsed = urlparse(dcs)
|
||||||
scheme = parsed.scheme
|
scheme = parsed.scheme
|
||||||
if scheme == '' and parsed.netloc == '':
|
|
||||||
parsed = urlparse('//' + dcs)
|
|
||||||
port = int(parsed.port) if parsed.port else None
|
port = int(parsed.port) if parsed.port else None
|
||||||
|
|
||||||
if scheme == '':
|
if scheme == '':
|
||||||
@@ -68,21 +111,17 @@ def parse_dcs(dcs):
|
|||||||
|
|
||||||
|
|
||||||
def load_config(path, dcs):
|
def load_config(path, dcs):
|
||||||
|
from patroni.config import Config
|
||||||
|
|
||||||
if not (os.path.exists(path) and os.access(path, os.R_OK)):
|
if not (os.path.exists(path) and os.access(path, os.R_OK)):
|
||||||
|
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)
|
logging.debug('Ignoring configuration file "%s". It does not exists or is not readable.', path)
|
||||||
else:
|
else:
|
||||||
logging.debug('Loading configuration from file %s', path)
|
logging.debug('Loading configuration from file %s', path)
|
||||||
config = {}
|
config = Config(path, validator=None).copy()
|
||||||
old_argv = list(sys.argv)
|
|
||||||
try:
|
|
||||||
sys.argv[1] = path
|
|
||||||
if Config.PATRONI_CONFIG_VARIABLE not in os.environ:
|
|
||||||
for p in ('PATRONI_RESTAPI_LISTEN', 'PATRONI_POSTGRESQL_DATA_DIR'):
|
|
||||||
if p not in os.environ:
|
|
||||||
os.environ[p] = '.'
|
|
||||||
config = Config().copy()
|
|
||||||
finally:
|
|
||||||
sys.argv = old_argv
|
|
||||||
|
|
||||||
dcs = parse_dcs(dcs) or parse_dcs(config.get('dcs_api')) or {}
|
dcs = parse_dcs(dcs) or parse_dcs(config.get('dcs_api')) or {}
|
||||||
if dcs:
|
if dcs:
|
||||||
@@ -100,7 +139,7 @@ def store_config(config, path):
|
|||||||
yaml.dump(config, fd)
|
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_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_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')
|
option_force = click.option('--force', is_flag=True, help='Do not ask for confirmation at any point')
|
||||||
@@ -135,63 +174,42 @@ def get_dcs(config, scope):
|
|||||||
raise PatroniCtlException(str(e))
|
raise PatroniCtlException(str(e))
|
||||||
|
|
||||||
|
|
||||||
def auth_header(config):
|
def request_patroni(member, method='GET', endpoint=None, data=None):
|
||||||
if config.get('restapi', {}).get('auth', ''):
|
|
||||||
return {'Authorization': 'Basic ' + base64.b64encode(config['restapi']['auth'].encode('utf-8')).decode('utf-8')}
|
|
||||||
|
|
||||||
|
|
||||||
def request_patroni(member, request_type, endpoint, content=None, headers=None):
|
|
||||||
ctx = click.get_current_context() # the current click context
|
ctx = click.get_current_context() # the current click context
|
||||||
headers = headers or {}
|
request_executor = ctx.obj.get('__request_patroni')
|
||||||
url_parts = urlparse(member.api_url)
|
if not request_executor:
|
||||||
logging.debug(url_parts)
|
request_executor = ctx.obj['__request_patroni'] = PatroniRequest(ctx.obj)
|
||||||
if 'Content-Type' not in headers:
|
return request_executor(member, method, endpoint, data)
|
||||||
headers['Content-Type'] = 'application/json'
|
|
||||||
|
|
||||||
url = '{0}://{1}/{2}'.format(url_parts.scheme, url_parts.netloc, endpoint)
|
|
||||||
|
|
||||||
insecure = ctx.obj.get('ctl', {}).get('insecure', False)
|
|
||||||
# Get certfile if any from several configuration namespace
|
|
||||||
cert = ctx.obj.get('ctl', {}).get('cacert') or \
|
|
||||||
ctx.obj.get('restapi', {}).get('cacert') or \
|
|
||||||
ctx.obj.get('restapi', {}).get('certfile')
|
|
||||||
# In the case we specificaly disable SSL cert verification we don't want to have the warning
|
|
||||||
if insecure:
|
|
||||||
verify = False
|
|
||||||
elif cert:
|
|
||||||
verify = cert
|
|
||||||
else:
|
|
||||||
verify = True
|
|
||||||
return getattr(requests, request_type)(url, headers=headers,
|
|
||||||
data=json.dumps(content) if content else None, timeout=60,
|
|
||||||
verify=verify)
|
|
||||||
|
|
||||||
|
|
||||||
def print_output(columns, rows=None, alignment=None, fmt='pretty', header=True, delimiter='\t'):
|
def print_output(columns, rows, alignment=None, fmt='pretty', header=None, delimiter='\t'):
|
||||||
rows = rows or []
|
if fmt in {'json', 'yaml', 'yml'}:
|
||||||
if fmt == 'pretty':
|
elements = [{k: v for k, v in zip(columns, r) if not header or str(v)} for r in rows]
|
||||||
t = PrettyTable(columns)
|
func = json.dumps if fmt == 'json' else format_config_for_editing
|
||||||
for k, v in (alignment or {}).items():
|
click.echo(func(elements))
|
||||||
t.align[k] = v
|
elif fmt in {'pretty', 'tsv', 'topology'}:
|
||||||
for r in rows:
|
list_cluster = bool(header and columns and columns[0] == 'Cluster')
|
||||||
t.add_row(r)
|
if list_cluster and 'Tags' in columns: # we want to format member tags as YAML
|
||||||
click.echo(t)
|
i = columns.index('Tags')
|
||||||
return
|
for row in rows:
|
||||||
|
if row[i]:
|
||||||
if fmt in ['json', 'yaml', 'yml']:
|
row[i] = format_config_for_editing(row[i], fmt != 'pretty').strip()
|
||||||
elements = [dict(zip(columns, r)) for r in rows]
|
if list_cluster and fmt != 'tsv': # skip cluster name if pretty-printing
|
||||||
if fmt == 'json':
|
columns = columns[1:] if columns else []
|
||||||
click.echo(json.dumps(elements))
|
rows = [row[1:] for row in rows]
|
||||||
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 fmt == 'tsv':
|
||||||
if columns is not None and header:
|
for r in ([columns] if columns else []) + rows:
|
||||||
click.echo(delimiter.join(columns))
|
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:
|
for r in rows:
|
||||||
c = [str(c) for c in r]
|
table.add_row(r)
|
||||||
click.echo(delimiter.join(c))
|
click.echo(table)
|
||||||
|
|
||||||
|
|
||||||
def watching(w, watch, max_count=None, clear=True):
|
def watching(w, watch, max_count=None, clear=True):
|
||||||
@@ -224,7 +242,7 @@ def watching(w, watch, max_count=None, clear=True):
|
|||||||
|
|
||||||
def get_all_members(cluster, role='master'):
|
def get_all_members(cluster, role='master'):
|
||||||
if role == 'master':
|
if role == 'master':
|
||||||
if cluster.leader is not None:
|
if cluster.leader is not None and cluster.leader.name:
|
||||||
yield cluster.leader
|
yield cluster.leader
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -241,6 +259,15 @@ def get_any_member(cluster, role='master', member=None):
|
|||||||
return m
|
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):
|
def get_cursor(cluster, connect_parameters, role='master', member=None):
|
||||||
member = get_any_member(cluster, role=role, member=member)
|
member = get_any_member(cluster, role=role, member=member)
|
||||||
if member is None:
|
if member is None:
|
||||||
@@ -248,13 +275,13 @@ def get_cursor(cluster, connect_parameters, role='master', member=None):
|
|||||||
|
|
||||||
params = member.conn_kwargs(connect_parameters)
|
params = member.conn_kwargs(connect_parameters)
|
||||||
params.update({'fallback_application_name': 'Patroni ctl', 'connect_timeout': '5'})
|
params.update({'fallback_application_name': 'Patroni ctl', 'connect_timeout': '5'})
|
||||||
if 'database' in connect_parameters:
|
if 'dbname' in connect_parameters:
|
||||||
params['database'] = connect_parameters['database']
|
params['dbname'] = connect_parameters['dbname']
|
||||||
else:
|
else:
|
||||||
params.pop('database')
|
params.pop('dbname')
|
||||||
|
|
||||||
import psycopg2
|
from . import psycopg
|
||||||
conn = psycopg2.connect(**params)
|
conn = psycopg.connect(**params)
|
||||||
conn.autocommit = True
|
conn.autocommit = True
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
if role == 'any':
|
if role == 'any':
|
||||||
@@ -271,10 +298,12 @@ def get_cursor(cluster, connect_parameters, role='master', member=None):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_members(cluster, cluster_name, member_names, role, force, action, scheduled_at=None):
|
def get_members(cluster, cluster_name, member_names, role, force, action, ask_confirmation=True):
|
||||||
candidates = {m.name: m for m in cluster.members}
|
candidates = {m.name: m for m in cluster.members}
|
||||||
|
|
||||||
if not force or role:
|
if not force or role:
|
||||||
|
if not member_names and not candidates:
|
||||||
|
raise PatroniCtlException('{0} cluster doesn\'t have any members'.format(cluster_name))
|
||||||
output_members(cluster, cluster_name)
|
output_members(cluster, cluster_name)
|
||||||
|
|
||||||
if role:
|
if role:
|
||||||
@@ -294,21 +323,26 @@ def get_members(cluster, cluster_name, member_names, role, force, action, schedu
|
|||||||
if member_name not in candidates:
|
if member_name not in candidates:
|
||||||
raise PatroniCtlException('{0} is not a member of cluster'.format(member_name))
|
raise PatroniCtlException('{0} is not a member of cluster'.format(member_name))
|
||||||
|
|
||||||
|
members = [candidates[n] for n in member_names]
|
||||||
|
if ask_confirmation:
|
||||||
|
confirm_members_action(members, force, action)
|
||||||
|
return members
|
||||||
|
|
||||||
|
|
||||||
|
def confirm_members_action(members, force, action, scheduled_at=None):
|
||||||
if scheduled_at:
|
if scheduled_at:
|
||||||
if not force:
|
if not force:
|
||||||
confirm = click.confirm('Are you sure you want to schedule {0} of members {1} at {2}?'
|
confirm = click.confirm('Are you sure you want to schedule {0} of members {1} at {2}?'
|
||||||
.format(action, ', '.join(member_names), scheduled_at))
|
.format(action, ', '.join([m.name for m in members]), scheduled_at))
|
||||||
if not confirm:
|
if not confirm:
|
||||||
raise PatroniCtlException('Aborted scheduled {0}'.format(action))
|
raise PatroniCtlException('Aborted scheduled {0}'.format(action))
|
||||||
else:
|
else:
|
||||||
if not force:
|
if not force:
|
||||||
confirm = click.confirm('Are you sure you want to {0} members {1}?'
|
confirm = click.confirm('Are you sure you want to {0} members {1}?'
|
||||||
.format(action, ', '.join(member_names)))
|
.format(action, ', '.join([m.name for m in members])))
|
||||||
if not confirm:
|
if not confirm:
|
||||||
raise PatroniCtlException('Aborted {0}'.format(action))
|
raise PatroniCtlException('Aborted {0}'.format(action))
|
||||||
|
|
||||||
return [candidates[n] for n in member_names]
|
|
||||||
|
|
||||||
|
|
||||||
@ctl.command('dsn', help='Generate a dsn for the provided member, defaults to a dsn of the master')
|
@ctl.command('dsn', help='Generate a dsn for the provided member, defaults to a dsn of the master')
|
||||||
@click.option('--role', '-r', help='Give a dsn of any member with this role', type=click.Choice(['master', 'replica',
|
@click.option('--role', '-r', help='Give a dsn of any member with this role', type=click.Choice(['master', 'replica',
|
||||||
@@ -333,8 +367,7 @@ def dsn(obj, cluster_name, role, member):
|
|||||||
|
|
||||||
@ctl.command('query', help='Query a Patroni PostgreSQL member')
|
@ctl.command('query', help='Query a Patroni PostgreSQL member')
|
||||||
@arg_cluster_name
|
@arg_cluster_name
|
||||||
@option_format
|
@click.option('--format', 'fmt', help='Output format (pretty, tsv, json, yaml)', default='tsv')
|
||||||
@click.option('--format', 'fmt', help='Output format (pretty, json)', default='tsv')
|
|
||||||
@click.option('--file', '-f', 'p_file', help='Execute the SQL commands from this file', type=click.File('rb'))
|
@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('--password', help='force password prompt', is_flag=True)
|
||||||
@click.option('-U', '--username', help='database user name', type=str)
|
@click.option('-U', '--username', help='database user name', type=str)
|
||||||
@@ -379,7 +412,7 @@ def query(
|
|||||||
if password:
|
if password:
|
||||||
connect_parameters['password'] = click.prompt('Password', hide_input=True, type=str)
|
connect_parameters['password'] = click.prompt('Password', hide_input=True, type=str)
|
||||||
if dbname:
|
if dbname:
|
||||||
connect_parameters['database'] = dbname
|
connect_parameters['dbname'] = dbname
|
||||||
|
|
||||||
if p_file is not None:
|
if p_file is not None:
|
||||||
command = p_file.read()
|
command = p_file.read()
|
||||||
@@ -391,12 +424,12 @@ def query(
|
|||||||
if cursor is None:
|
if cursor is None:
|
||||||
cluster = dcs.get_cluster()
|
cluster = dcs.get_cluster()
|
||||||
|
|
||||||
output, cursor = query_member(cluster, cursor, member, role, command, connect_parameters)
|
output, header = query_member(cluster, cursor, member, role, command, connect_parameters)
|
||||||
print_output(None, output, fmt=fmt, delimiter=delimiter)
|
print_output(header, output, fmt=fmt, delimiter=delimiter)
|
||||||
|
|
||||||
|
|
||||||
def query_member(cluster, cursor, member, role, command, connect_parameters):
|
def query_member(cluster, cursor, member, role, command, connect_parameters):
|
||||||
import psycopg2
|
from . import psycopg
|
||||||
try:
|
try:
|
||||||
if cursor is None:
|
if cursor is None:
|
||||||
cursor = get_cursor(cluster, connect_parameters, role=role, member=member)
|
cursor = get_cursor(cluster, connect_parameters, role=role, member=member)
|
||||||
@@ -409,20 +442,13 @@ def query_member(cluster, cursor, member, role, command, connect_parameters):
|
|||||||
logging.debug(message)
|
logging.debug(message)
|
||||||
return [[timestamp(0), message]], None
|
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)
|
cursor.execute(command)
|
||||||
return cursor.fetchall(), cursor
|
return cursor.fetchall(), [d.name for d in cursor.description]
|
||||||
except (psycopg2.OperationalError, psycopg2.DatabaseError) as oe:
|
except psycopg.DatabaseError as de:
|
||||||
logging.debug(oe)
|
logging.debug(de)
|
||||||
if cursor is not None and not cursor.connection.closed:
|
if cursor is not None and not cursor.connection.closed:
|
||||||
cursor.connection.close()
|
cursor.connection.close()
|
||||||
message = oe.pgcode or oe.pgerror or str(oe)
|
message = de.diag.sqlstate or str(de)
|
||||||
message = message.replace('\n', ' ')
|
message = message.replace('\n', ' ')
|
||||||
return [[timestamp(0), 'ERROR, SQLSTATE: {0}'.format(message)]], None
|
return [[timestamp(0), 'ERROR, SQLSTATE: {0}'.format(message)]], None
|
||||||
|
|
||||||
@@ -457,9 +483,9 @@ def remove(obj, cluster_name, fmt):
|
|||||||
|
|
||||||
|
|
||||||
def check_response(response, member_name, action_name, silent_success=False):
|
def check_response(response, member_name, action_name, silent_success=False):
|
||||||
if response.status_code >= 400:
|
if response.status >= 400:
|
||||||
click.echo('Failed: {0} for member {1}, status code={2}, ({3})'.format(
|
click.echo('Failed: {0} for member {1}, status code={2}, ({3})'.format(
|
||||||
action_name, member_name, response.status_code, response.text
|
action_name, member_name, response.status, response.data.decode('utf-8')
|
||||||
))
|
))
|
||||||
return False
|
return False
|
||||||
elif not silent_success:
|
elif not silent_success:
|
||||||
@@ -472,7 +498,7 @@ def parse_scheduled(scheduled):
|
|||||||
try:
|
try:
|
||||||
scheduled_at = dateutil.parser.parse(scheduled)
|
scheduled_at = dateutil.parser.parse(scheduled)
|
||||||
if scheduled_at.tzinfo is None:
|
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):
|
except (ValueError, TypeError):
|
||||||
message = 'Unable to parse scheduled timestamp ({0}). It should be in an unambiguous format (e.g. ISO 8601)'
|
message = 'Unable to parse scheduled timestamp ({0}). It should be in an unambiguous format (e.g. ISO 8601)'
|
||||||
raise PatroniCtlException(message.format(scheduled))
|
raise PatroniCtlException(message.format(scheduled))
|
||||||
@@ -493,18 +519,17 @@ def reload(obj, cluster_name, member_names, force, role):
|
|||||||
|
|
||||||
members = get_members(cluster, cluster_name, member_names, role, force, 'reload')
|
members = get_members(cluster, cluster_name, member_names, role, force, 'reload')
|
||||||
|
|
||||||
content = {}
|
|
||||||
for member in members:
|
for member in members:
|
||||||
r = request_patroni(member, 'post', 'reload', content, auth_header(obj))
|
r = request_patroni(member, 'post', 'reload')
|
||||||
if r.status_code == 200:
|
if r.status == 200:
|
||||||
click.echo('No changes to apply on member {0}'.format(member.name))
|
click.echo('No changes to apply on member {0}'.format(member.name))
|
||||||
elif r.status_code == 202:
|
elif r.status == 202:
|
||||||
click.echo('Reload request received for member {0} and will be processed within {1} seconds'.format(
|
click.echo('Reload request received for member {0} and will be processed within {1} seconds'.format(
|
||||||
member.name, cluster.config.data.get('loop_wait'))
|
member.name, cluster.config.data.get('loop_wait'))
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
click.echo('Failed: reload for member {0}, status code={1}, ({2})'.format(
|
click.echo('Failed: reload for member {0}, status code={1}, ({2})'.format(
|
||||||
member.name, r.status_code, r.text)
|
member.name, r.status, r.data.decode('utf-8'))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -526,14 +551,15 @@ def reload(obj, cluster_name, member_names, force, role):
|
|||||||
def restart(obj, cluster_name, member_names, force, role, p_any, scheduled, version, pending, timeout):
|
def restart(obj, cluster_name, member_names, force, role, p_any, scheduled, version, pending, timeout):
|
||||||
cluster = get_dcs(obj, cluster_name).get_cluster()
|
cluster = get_dcs(obj, cluster_name).get_cluster()
|
||||||
|
|
||||||
|
members = get_members(cluster, cluster_name, member_names, role, force, 'restart', False)
|
||||||
if scheduled is None and not force:
|
if scheduled is None and not force:
|
||||||
next_hour = (datetime.datetime.now() + datetime.timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M')
|
next_hour = (datetime.datetime.now() + datetime.timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M')
|
||||||
scheduled = click.prompt('When should the restart take place (e.g. ' + next_hour + ') ',
|
scheduled = click.prompt('When should the restart take place (e.g. ' + next_hour + ') ',
|
||||||
type=str, default='now')
|
type=str, default='now')
|
||||||
|
|
||||||
scheduled_at = parse_scheduled(scheduled)
|
scheduled_at = parse_scheduled(scheduled)
|
||||||
|
confirm_members_action(members, force, 'restart', scheduled_at)
|
||||||
|
|
||||||
members = get_members(cluster, cluster_name, member_names, role, force, 'restart', scheduled_at)
|
|
||||||
if p_any:
|
if p_any:
|
||||||
random.shuffle(members)
|
random.shuffle(members)
|
||||||
members = members[:1]
|
members = members[:1]
|
||||||
@@ -565,19 +591,19 @@ def restart(obj, cluster_name, member_names, force, role, p_any, scheduled, vers
|
|||||||
for member in members:
|
for member in members:
|
||||||
if 'schedule' in content:
|
if 'schedule' in content:
|
||||||
if force and member.data.get('scheduled_restart'):
|
if force and member.data.get('scheduled_restart'):
|
||||||
r = request_patroni(member, 'delete', 'restart', headers=auth_header(obj))
|
r = request_patroni(member, 'delete', 'restart')
|
||||||
check_response(r, member.name, 'flush scheduled restart', True)
|
check_response(r, member.name, 'flush scheduled restart', True)
|
||||||
|
|
||||||
r = request_patroni(member, 'post', 'restart', content, auth_header(obj))
|
r = request_patroni(member, 'post', 'restart', content)
|
||||||
if r.status_code == 200:
|
if r.status == 200:
|
||||||
click.echo('Success: restart on member {0}'.format(member.name))
|
click.echo('Success: restart on member {0}'.format(member.name))
|
||||||
elif r.status_code == 202:
|
elif r.status == 202:
|
||||||
click.echo('Success: restart scheduled on member {0}'.format(member.name))
|
click.echo('Success: restart scheduled on member {0}'.format(member.name))
|
||||||
elif r.status_code == 409:
|
elif r.status == 409:
|
||||||
click.echo('Failed: another restart is already scheduled on member {0}'.format(member.name))
|
click.echo('Failed: another restart is already scheduled on member {0}'.format(member.name))
|
||||||
else:
|
else:
|
||||||
click.echo('Failed: restart for member {0}, status code={1}, ({2})'.format(
|
click.echo('Failed: restart for member {0}, status code={1}, ({2})'.format(
|
||||||
member.name, r.status_code, r.text)
|
member.name, r.status, r.data.decode('utf-8'))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -585,20 +611,39 @@ def restart(obj, cluster_name, member_names, force, role, p_any, scheduled, vers
|
|||||||
@click.argument('cluster_name')
|
@click.argument('cluster_name')
|
||||||
@click.argument('member_names', nargs=-1)
|
@click.argument('member_names', nargs=-1)
|
||||||
@option_force
|
@option_force
|
||||||
|
@click.option('--wait', help='Wait until reinitialization completes', is_flag=True)
|
||||||
@click.pass_obj
|
@click.pass_obj
|
||||||
def reinit(obj, cluster_name, member_names, force):
|
def reinit(obj, cluster_name, member_names, force, wait):
|
||||||
cluster = get_dcs(obj, cluster_name).get_cluster()
|
cluster = get_dcs(obj, cluster_name).get_cluster()
|
||||||
members = get_members(cluster, cluster_name, member_names, None, force, 'reinitialize')
|
members = get_members(cluster, cluster_name, member_names, None, force, 'reinitialize')
|
||||||
|
|
||||||
|
wait_on_members = []
|
||||||
for member in members:
|
for member in members:
|
||||||
body = {'force': force}
|
body = {'force': force}
|
||||||
while True:
|
while True:
|
||||||
r = request_patroni(member, 'post', 'reinitialize', body, auth_header(obj))
|
r = request_patroni(member, 'post', 'reinitialize', body)
|
||||||
if not check_response(r, member.name, 'reinitialize') and r.text.endswith(' already in progress') \
|
started = check_response(r, member.name, 'reinitialize')
|
||||||
|
if not started and r.data.endswith(b' already in progress') \
|
||||||
and not force and click.confirm('Do you want to cancel it and reinitialize anyway?'):
|
and not force and click.confirm('Do you want to cancel it and reinitialize anyway?'):
|
||||||
body['force'] = True
|
body['force'] = True
|
||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
|
if started and wait:
|
||||||
|
wait_on_members.append(member)
|
||||||
|
|
||||||
|
last_display = []
|
||||||
|
while wait_on_members:
|
||||||
|
if wait_on_members != last_display:
|
||||||
|
click.echo('Waiting for reinitialize to complete on: {0}'.format(
|
||||||
|
", ".join(member.name for member in wait_on_members))
|
||||||
|
)
|
||||||
|
last_display[:] = wait_on_members
|
||||||
|
time.sleep(2)
|
||||||
|
for member in wait_on_members:
|
||||||
|
data = json.loads(request_patroni(member, 'get', 'patroni').data.decode('utf-8'))
|
||||||
|
if data.get('state') != 'creating replica':
|
||||||
|
click.echo('Reinitialize is completed on: {0}'.format(member.name))
|
||||||
|
wait_on_members.remove(member)
|
||||||
|
|
||||||
|
|
||||||
def _do_failover_or_switchover(obj, action, cluster_name, master, candidate, force, scheduled=None):
|
def _do_failover_or_switchover(obj, action, cluster_name, master, candidate, force, scheduled=None):
|
||||||
@@ -612,7 +657,7 @@ def _do_failover_or_switchover(obj, action, cluster_name, master, candidate, for
|
|||||||
dcs = get_dcs(obj, cluster_name)
|
dcs = get_dcs(obj, cluster_name)
|
||||||
cluster = dcs.get_cluster()
|
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')
|
raise PatroniCtlException('This cluster has no master')
|
||||||
|
|
||||||
if master is None:
|
if master is None:
|
||||||
@@ -682,19 +727,19 @@ def _do_failover_or_switchover(obj, action, cluster_name, master, candidate, for
|
|||||||
try:
|
try:
|
||||||
member = cluster.leader.member if cluster.leader else cluster.get_member(candidate, False)
|
member = cluster.leader.member if cluster.leader else cluster.get_member(candidate, False)
|
||||||
|
|
||||||
r = request_patroni(member, 'post', action, failover_value, auth_header(obj))
|
r = request_patroni(member, 'post', action, failover_value)
|
||||||
|
|
||||||
# probably old patroni, which doesn't support switchover yet
|
# probably old patroni, which doesn't support switchover yet
|
||||||
if r.status_code == 501 and action == 'switchover' and 'Server does not support this operation' in r.text:
|
if r.status == 501 and action == 'switchover' and b'Server does not support this operation' in r.data:
|
||||||
r = request_patroni(member, 'post', 'failover', failover_value, auth_header(obj))
|
r = request_patroni(member, 'post', 'failover', failover_value)
|
||||||
|
|
||||||
if r.status_code in (200, 202):
|
if r.status in (200, 202):
|
||||||
logging.debug(r)
|
logging.debug(r)
|
||||||
cluster = dcs.get_cluster()
|
cluster = dcs.get_cluster()
|
||||||
logging.debug(cluster)
|
logging.debug(cluster)
|
||||||
click.echo('{0} {1}'.format(timestamp(), r.text))
|
click.echo('{0} {1}'.format(timestamp(), r.data.decode('utf-8')))
|
||||||
else:
|
else:
|
||||||
click.echo('{0} failed, details: {1}, {2}'.format(action.title(), r.status_code, r.text))
|
click.echo('{0} failed, details: {1}, {2}'.format(action.title(), r.status, r.data.decode('utf-8')))
|
||||||
return
|
return
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception(r)
|
logging.exception(r)
|
||||||
@@ -728,83 +773,84 @@ def switchover(obj, cluster_name, master, candidate, force, scheduled):
|
|||||||
_do_failover_or_switchover(obj, 'switchover', 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'):
|
def output_members(cluster, name, extended=False, fmt='pretty'):
|
||||||
rows = []
|
rows = []
|
||||||
logging.debug(cluster)
|
logging.debug(cluster)
|
||||||
leader_name = None
|
initialize = {None: 'uninitialized', '': 'initializing'}.get(cluster.initialize, cluster.initialize)
|
||||||
if cluster.leader:
|
cluster = cluster_as_json(cluster)
|
||||||
leader_name = cluster.leader.name
|
|
||||||
|
|
||||||
xlog_location_cluster = cluster.last_leader_operation or 0
|
|
||||||
|
|
||||||
# Mainly for consistent pretty printing and watching we sort the output
|
|
||||||
cluster.members.sort(key=lambda x: x.name)
|
|
||||||
|
|
||||||
has_scheduled_restarts = any(m.data.get('scheduled_restart') for m in cluster.members)
|
|
||||||
has_pending_restarts = any(m.data.get('pending_restart') for m in cluster.members)
|
|
||||||
|
|
||||||
# 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(str(m.conn_kwargs()['port']) != '5432' for m in cluster.members) or\
|
|
||||||
len(set(m.conn_kwargs()['host'] for m in cluster.members)) < len(cluster.members)
|
|
||||||
|
|
||||||
for m in cluster.members:
|
|
||||||
logging.debug(m)
|
|
||||||
|
|
||||||
role = ''
|
|
||||||
if m.name == leader_name:
|
|
||||||
role = 'Leader'
|
|
||||||
elif m.name == cluster.sync.sync_standby:
|
|
||||||
role = 'Sync standby'
|
|
||||||
|
|
||||||
xlog_location = m.data.get('xlog_location')
|
|
||||||
lag = ''
|
|
||||||
if xlog_location is None:
|
|
||||||
lag = 'unknown'
|
|
||||||
elif xlog_location_cluster >= xlog_location:
|
|
||||||
lag = round((xlog_location_cluster - xlog_location)/1024/1024)
|
|
||||||
|
|
||||||
host = m.conn_kwargs()['host']
|
|
||||||
if append_port:
|
|
||||||
host += ':{0}'.format(m.conn_kwargs()['port'])
|
|
||||||
|
|
||||||
row = [name, m.name, host, role, m.data.get('state', ''), m.data.get('timeline', ''), lag]
|
|
||||||
|
|
||||||
if extended or has_pending_restarts:
|
|
||||||
row.append('*' if m.data.get('pending_restart') else '')
|
|
||||||
|
|
||||||
if extended or has_scheduled_restarts:
|
|
||||||
value = ''
|
|
||||||
scheduled_restart = m.data.get('scheduled_restart')
|
|
||||||
if scheduled_restart:
|
|
||||||
value = scheduled_restart['schedule']
|
|
||||||
if 'postgres_version' in scheduled_restart:
|
|
||||||
value += ' if version < {0}'.format(scheduled_restart['postgres_version'])
|
|
||||||
|
|
||||||
row.append(value)
|
|
||||||
|
|
||||||
rows.append(row)
|
|
||||||
|
|
||||||
columns = ['Cluster', 'Member', 'Host', 'Role', 'State', 'TL', 'Lag in MB']
|
columns = ['Cluster', 'Member', 'Host', 'Role', 'State', 'TL', 'Lag in MB']
|
||||||
alignment = {'Lag in MB': 'r', 'TL': 'r'}
|
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)
|
||||||
|
|
||||||
if extended or has_pending_restarts:
|
# Show Host as 'host:port' if somebody is running on non-standard port or two nodes are running on the same host
|
||||||
columns.append('Pending restart')
|
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)
|
||||||
|
|
||||||
if extended or has_scheduled_restarts:
|
sort = topology_sort if fmt == 'topology' else iter
|
||||||
columns.append('Scheduled restart')
|
for m in sort(cluster['members']):
|
||||||
|
logging.debug(m)
|
||||||
|
|
||||||
print_output(columns, rows, alignment, fmt)
|
lag = m.get('lag', '')
|
||||||
|
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 and m['host'] and m.get('port'):
|
||||||
|
m['host'] = ':'.join([m['host'], str(m['port'])])
|
||||||
|
|
||||||
|
if 'scheduled_restart' in m:
|
||||||
|
value = m['scheduled_restart']['schedule']
|
||||||
|
if 'postgres_version' in m['scheduled_restart']:
|
||||||
|
value += ' if version < {0}'.format(m['scheduled_restart']['postgres_version'])
|
||||||
|
m['scheduled_restart'] = value
|
||||||
|
|
||||||
|
rows.append([m.get(n.lower().replace(' ', '_'), '') for n in columns])
|
||||||
|
|
||||||
|
print_output(columns, rows, {'Lag in MB': 'r', 'TL': 'r'}, fmt, ' Cluster: {0} ({1}) '.format(name, initialize))
|
||||||
|
|
||||||
|
if fmt not in ('pretty', 'topology'): # Omit service info when using machine-readable formats
|
||||||
|
return
|
||||||
|
|
||||||
service_info = []
|
service_info = []
|
||||||
if cluster.is_paused():
|
if cluster.get('pause'):
|
||||||
service_info.append('Maintenance mode: on')
|
service_info.append('Maintenance mode: on')
|
||||||
|
|
||||||
if cluster.failover and cluster.failover.scheduled_at:
|
if 'scheduled_switchover' in cluster:
|
||||||
info = 'Switchover scheduled at: ' + cluster.failover.scheduled_at.isoformat()
|
info = 'Switchover scheduled at: ' + cluster['scheduled_switchover']['at']
|
||||||
if cluster.failover.leader:
|
for name in ('from', 'to'):
|
||||||
info += '\n from: ' + cluster.failover.leader
|
if name in cluster['scheduled_switchover']:
|
||||||
if cluster.failover.candidate:
|
info += '\n{0:>24}: {1}'.format(name, cluster['scheduled_switchover'][name])
|
||||||
info += '\n to: ' + cluster.failover.candidate
|
|
||||||
service_info.append(info)
|
service_info.append(info)
|
||||||
|
|
||||||
if service_info:
|
if service_info:
|
||||||
@@ -837,6 +883,16 @@ def members(obj, cluster_names, fmt, watch, w, extended, ts):
|
|||||||
output_members(cluster, cluster_name, extended, fmt)
|
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):
|
def timestamp(precision=6):
|
||||||
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:precision - 7]
|
return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:precision - 7]
|
||||||
|
|
||||||
@@ -904,25 +960,45 @@ def scaffold(obj, cluster_name, sysid):
|
|||||||
click.echo("Cluster {0} has been created successfully".format(cluster_name))
|
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('cluster_name')
|
||||||
@click.argument('member_names', nargs=-1)
|
@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',
|
@click.option('--role', '-r', help='Flush only members with this role', default='any',
|
||||||
type=click.Choice(['master', 'replica', 'any']))
|
type=click.Choice(['master', 'replica', 'any']))
|
||||||
@option_force
|
@option_force
|
||||||
@click.pass_obj
|
@click.pass_obj
|
||||||
def flush(obj, cluster_name, member_names, force, role, target):
|
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'):
|
if member.data.get('scheduled_restart'):
|
||||||
r = request_patroni(member, 'delete', 'restart', None, auth_header(obj))
|
r = request_patroni(member, 'delete', 'restart')
|
||||||
check_response(r, member.name, 'flush scheduled restart')
|
check_response(r, member.name, 'flush scheduled restart')
|
||||||
else:
|
else:
|
||||||
click.echo('No scheduled restart for member {0}'.format(member.name))
|
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):
|
def wait_until_pause_is_applied(dcs, paused, old_cluster):
|
||||||
@@ -949,27 +1025,22 @@ def toggle_pause(config, cluster_name, paused, wait):
|
|||||||
if cluster.is_paused() == paused:
|
if cluster.is_paused() == paused:
|
||||||
raise PatroniCtlException('Cluster is {0} paused'.format(paused and 'already' or 'not'))
|
raise PatroniCtlException('Cluster is {0} paused'.format(paused and 'already' or 'not'))
|
||||||
|
|
||||||
members = []
|
for member in get_all_members_leader_first(cluster):
|
||||||
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:
|
|
||||||
try:
|
try:
|
||||||
r = request_patroni(member, 'patch', 'config', {'pause': paused or None}, auth_header(config))
|
r = request_patroni(member, 'patch', 'config', {'pause': paused or None})
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
logging.warning(str(err))
|
logging.warning(str(err))
|
||||||
logging.warning('Member %s is not accessible', member.name)
|
logging.warning('Member %s is not accessible', member.name)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if r.status_code == 200:
|
if r.status == 200:
|
||||||
if wait:
|
if wait:
|
||||||
wait_until_pause_is_applied(dcs, paused, cluster)
|
wait_until_pause_is_applied(dcs, paused, cluster)
|
||||||
else:
|
else:
|
||||||
click.echo('Success: cluster management is {0}'.format(paused and 'paused' or 'resumed'))
|
click.echo('Success: cluster management is {0}'.format(paused and 'paused' or 'resumed'))
|
||||||
else:
|
else:
|
||||||
click.echo('Failed: {0} cluster management status code={1}, ({2})'.format(
|
click.echo('Failed: {0} cluster management status code={1}, ({2})'.format(
|
||||||
paused and 'pause' or 'resume', r.status_code, r.text))
|
paused and 'pause' or 'resume', r.status, r.data.decode('utf-8')))
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise PatroniCtlException('Can not find accessible cluster member')
|
raise PatroniCtlException('Can not find accessible cluster member')
|
||||||
@@ -1016,7 +1087,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.
|
If the output is to a tty the diff will be colored. Inputs are expected to be unicode strings.
|
||||||
"""
|
"""
|
||||||
def listify(string):
|
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))
|
unified_diff = difflib.unified_diff(listify(before_editing), listify(after_editing))
|
||||||
|
|
||||||
@@ -1024,25 +1095,32 @@ def show_diff(before_editing, after_editing):
|
|||||||
buf = io.StringIO()
|
buf = io.StringIO()
|
||||||
for line in unified_diff:
|
for line in unified_diff:
|
||||||
# Force cast to unicode as difflib on Python 2.7 returns a mix of unicode and str.
|
# Force cast to unicode as difflib on Python 2.7 returns a mix of unicode and str.
|
||||||
buf.write(text_type(line))
|
buf.write(six.text_type(line))
|
||||||
buf.seek(0)
|
buf.seek(0)
|
||||||
|
|
||||||
class opts:
|
class opts:
|
||||||
side_by_side = False
|
side_by_side = False
|
||||||
width = 80
|
width = 80
|
||||||
tab_width = 8
|
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:
|
else:
|
||||||
for line in unified_diff:
|
for line in unified_diff:
|
||||||
click.echo(line.rstrip('\n'))
|
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.
|
"""Formats configuration as YAML for human consumption.
|
||||||
|
|
||||||
:param data: configuration as nested dictionaries
|
:param data: configuration as nested dictionaries
|
||||||
:returns unicode YAML of the configuration"""
|
: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):
|
def apply_config_changes(before_editing, data, kvpairs):
|
||||||
@@ -1106,24 +1184,6 @@ def apply_yaml_file(data, filename):
|
|||||||
return format_config_for_editing(changed_data), changed_data
|
return format_config_for_editing(changed_data), changed_data
|
||||||
|
|
||||||
|
|
||||||
def find_executable(executable, path=None):
|
|
||||||
_, ext = os.path.splitext(executable)
|
|
||||||
|
|
||||||
if (sys.platform == 'win32') and (ext != '.exe'):
|
|
||||||
executable = executable + '.exe'
|
|
||||||
|
|
||||||
if os.path.isfile(executable):
|
|
||||||
return executable
|
|
||||||
|
|
||||||
if path is None:
|
|
||||||
path = os.environ.get('PATH', os.defpath)
|
|
||||||
|
|
||||||
for p in path.split(os.pathsep):
|
|
||||||
f = os.path.join(p, executable)
|
|
||||||
if os.path.isfile(f):
|
|
||||||
return f
|
|
||||||
|
|
||||||
|
|
||||||
def invoke_editor(before_editing, cluster_name):
|
def invoke_editor(before_editing, cluster_name):
|
||||||
"""Starts editor command to edit configuration in human readable format
|
"""Starts editor command to edit configuration in human readable format
|
||||||
|
|
||||||
@@ -1233,8 +1293,8 @@ def version(obj, cluster_name, member_names):
|
|||||||
if m.api_url:
|
if m.api_url:
|
||||||
if not member_names or m.name in member_names:
|
if not member_names or m.name in member_names:
|
||||||
try:
|
try:
|
||||||
response = request_patroni(m, 'get', 'patroni')
|
response = request_patroni(m)
|
||||||
data = response.json()
|
data = json.loads(response.data.decode('utf-8'))
|
||||||
version = data.get('patroni', {}).get('version')
|
version = data.get('patroni', {}).get('version')
|
||||||
pg_version = data.get('server_version')
|
pg_version = data.get('server_version')
|
||||||
pg_version_str = " PostgreSQL {0}".format(format_pg_version(pg_version)) if pg_version else ""
|
pg_version_str = " PostgreSQL {0}".format(format_pg_version(pg_version)) if pg_version else ""
|
||||||
@@ -1243,6 +1303,22 @@ def version(obj, cluster_name, member_names):
|
|||||||
click.echo("{0}: failed to get version: {1}".format(m.name, e))
|
click.echo("{0}: failed to get version: {1}".format(m.name, e))
|
||||||
|
|
||||||
|
|
||||||
|
@ctl.command('history', help="Show the history of failovers/switchovers")
|
||||||
|
@arg_cluster_name
|
||||||
|
@option_format
|
||||||
|
@click.pass_obj
|
||||||
|
def history(obj, cluster_name, fmt):
|
||||||
|
cluster = get_dcs(obj, cluster_name).get_cluster()
|
||||||
|
history = cluster.history and cluster.history.lines or []
|
||||||
|
table_header_row = ['TL', 'LSN', 'Reason', 'Timestamp', 'New Leader']
|
||||||
|
for line in history:
|
||||||
|
if len(line) < len(table_header_row):
|
||||||
|
add_column_num = len(table_header_row) - len(line)
|
||||||
|
for _ in range(add_column_num):
|
||||||
|
line.append('')
|
||||||
|
print_output(table_header_row, history, {'TL': 'r', 'LSN': 'r'}, fmt)
|
||||||
|
|
||||||
|
|
||||||
def format_pg_version(version):
|
def format_pg_version(version):
|
||||||
if version < 100000:
|
if version < 100000:
|
||||||
return "{0}.{1}.{2}".format(version // 10000, version // 100 % 100, version % 100)
|
return "{0}.{1}.{2}".format(version // 10000, version // 100 % 100, version % 100)
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
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 api_sigterm(self):
|
||||||
|
with self._sigterm_lock:
|
||||||
|
if not self._received_sigterm:
|
||||||
|
self._received_sigterm = True
|
||||||
|
return True
|
||||||
|
|
||||||
|
def sigterm_handler(self, *args):
|
||||||
|
if self.api_sigterm():
|
||||||
|
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()
|
||||||
|
validate_config = validator and args.validate_config
|
||||||
|
try:
|
||||||
|
if validate_config:
|
||||||
|
Config(args.configfile, validator=validator)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
config = Config(args.configfile)
|
||||||
|
except ConfigParseError as e:
|
||||||
|
if e.value:
|
||||||
|
print(e.value, file=sys.stderr)
|
||||||
|
if not validate_config:
|
||||||
|
parser.print_help()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
controller = cls(config)
|
||||||
|
try:
|
||||||
|
controller.run()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
controller.shutdown()
|
||||||
+240
-80
@@ -1,5 +1,5 @@
|
|||||||
import abc
|
import abc
|
||||||
import dateutil
|
import dateutil.parser
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
@@ -13,12 +13,13 @@ import time
|
|||||||
|
|
||||||
from collections import defaultdict, namedtuple
|
from collections import defaultdict, namedtuple
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from patroni.exceptions import PatroniException
|
|
||||||
from patroni.utils import parse_bool, uri
|
|
||||||
from random import randint
|
from random import randint
|
||||||
from six.moves.urllib_parse import urlparse, urlunparse, parse_qsl
|
from six.moves.urllib_parse import urlparse, urlunparse, parse_qsl
|
||||||
from threading import Event, Lock
|
from threading import Event, Lock
|
||||||
|
|
||||||
|
from ..exceptions import PatroniFatalException
|
||||||
|
from ..utils import deep_compare, parse_bool, uri
|
||||||
|
|
||||||
slot_name_re = re.compile('^[a-z0-9_]{1,63}$')
|
slot_name_re = re.compile('^[a-z0-9_]{1,63}$')
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -66,33 +67,48 @@ def dcs_modules():
|
|||||||
module_prefix = __package__ + '.'
|
module_prefix = __package__ + '.'
|
||||||
|
|
||||||
if getattr(sys, 'frozen', False):
|
if getattr(sys, 'frozen', False):
|
||||||
importer = pkgutil.get_importer(dcs_dirname)
|
toc = set()
|
||||||
return [module for module in list(importer.toc) if module.startswith(module_prefix) and module.count('.') == 2]
|
# dcs_dirname may contain a dot, which causes pkgutil.iter_importers()
|
||||||
|
# to misinterpret the path as a package name. This can be avoided
|
||||||
|
# altogether by not passing a path at all, because PyInstaller's
|
||||||
|
# FrozenImporter is a singleton and registered as top-level finder.
|
||||||
|
for importer in pkgutil.iter_importers():
|
||||||
|
if hasattr(importer, 'toc'):
|
||||||
|
toc |= importer.toc
|
||||||
|
return [module for module in toc if module.startswith(module_prefix) and module.count('.') == 2]
|
||||||
else:
|
else:
|
||||||
return [module_prefix + name for _, name, is_pkg in pkgutil.iter_modules([dcs_dirname]) if not is_pkg]
|
return [module_prefix + name for _, name, is_pkg in pkgutil.iter_modules([dcs_dirname]) if not is_pkg]
|
||||||
|
|
||||||
|
|
||||||
def get_dcs(config):
|
def get_dcs(config):
|
||||||
available_implementations = set()
|
modules = dcs_modules()
|
||||||
for module_name in dcs_modules():
|
|
||||||
|
for module_name in modules:
|
||||||
|
name = module_name.split('.')[-1]
|
||||||
|
if name in config: # we will try to import only modules which have configuration section in the config file
|
||||||
try:
|
try:
|
||||||
module = importlib.import_module(module_name)
|
module = importlib.import_module(module_name)
|
||||||
for name in filter(lambda name: not name.startswith('__'), dir(module)): # iterate through module content
|
for key, item in module.__dict__.items(): # iterate through the module content
|
||||||
item = getattr(module, name)
|
|
||||||
name = name.lower()
|
|
||||||
# try to find implementation of AbstractDCS interface, class name must match with module_name
|
# try to find implementation of AbstractDCS interface, class name must match with module_name
|
||||||
if inspect.isclass(item) and issubclass(item, AbstractDCS) and __package__ + '.' + name == module_name:
|
if key.lower() == name and inspect.isclass(item) and issubclass(item, AbstractDCS):
|
||||||
available_implementations.add(name)
|
|
||||||
if name in config: # which has configuration section in the config file
|
|
||||||
# propagate some parameters
|
# propagate some parameters
|
||||||
config[name].update({p: config[p] for p in ('namespace', 'name', 'scope', 'loop_wait',
|
config[name].update({p: config[p] for p in ('namespace', 'name', 'scope', 'loop_wait',
|
||||||
'patronictl', 'ttl', 'retry_timeout') if p in config})
|
'patronictl', 'ttl', 'retry_timeout') if p in config})
|
||||||
return item(config[name])
|
return item(config[name])
|
||||||
except ImportError:
|
except ImportError:
|
||||||
if not config.get('patronictl'):
|
logger.debug('Failed to import %s', module_name)
|
||||||
|
|
||||||
|
available_implementations = []
|
||||||
|
for module_name in modules:
|
||||||
|
name = module_name.split('.')[-1]
|
||||||
|
try:
|
||||||
|
module = importlib.import_module(module_name)
|
||||||
|
available_implementations.extend(name for key, item in module.__dict__.items() if key.lower() == name
|
||||||
|
and inspect.isclass(item) and issubclass(item, AbstractDCS))
|
||||||
|
except ImportError:
|
||||||
logger.info('Failed to import %s', module_name)
|
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(available_implementations))
|
Available implementations: """ + ', '.join(sorted(set(available_implementations))))
|
||||||
|
|
||||||
|
|
||||||
class Member(namedtuple('Member', 'index,name,session,data')):
|
class Member(namedtuple('Member', 'index,name,session,data')):
|
||||||
@@ -122,6 +138,8 @@ class Member(namedtuple('Member', 'index,name,session,data')):
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
data = json.loads(data)
|
data = json.loads(data)
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
data = {}
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
data = {}
|
data = {}
|
||||||
return Member(index, name, session, data)
|
return Member(index, name, session, data)
|
||||||
@@ -129,10 +147,10 @@ class Member(namedtuple('Member', 'index,name,session,data')):
|
|||||||
@property
|
@property
|
||||||
def conn_url(self):
|
def conn_url(self):
|
||||||
conn_url = self.data.get('conn_url')
|
conn_url = self.data.get('conn_url')
|
||||||
conn_kwargs = self.data.get('conn_kwargs')
|
|
||||||
if conn_url:
|
if conn_url:
|
||||||
return conn_url
|
return conn_url
|
||||||
|
|
||||||
|
conn_kwargs = self.data.get('conn_kwargs')
|
||||||
if conn_kwargs:
|
if conn_kwargs:
|
||||||
conn_url = uri('postgresql', (conn_kwargs.get('host'), conn_kwargs.get('port', 5432)))
|
conn_url = uri('postgresql', (conn_kwargs.get('host'), conn_kwargs.get('port', 5432)))
|
||||||
self.data['conn_url'] = conn_url
|
self.data['conn_url'] = conn_url
|
||||||
@@ -140,28 +158,31 @@ class Member(namedtuple('Member', 'index,name,session,data')):
|
|||||||
|
|
||||||
def conn_kwargs(self, auth=None):
|
def conn_kwargs(self, auth=None):
|
||||||
defaults = {
|
defaults = {
|
||||||
"host": "",
|
"host": None,
|
||||||
"port": "",
|
"port": None,
|
||||||
"database": ""
|
"dbname": None
|
||||||
}
|
}
|
||||||
ret = self.data.get('conn_kwargs')
|
ret = self.data.get('conn_kwargs')
|
||||||
if ret:
|
if ret:
|
||||||
defaults.update(ret)
|
defaults.update(ret)
|
||||||
ret = defaults
|
ret = defaults
|
||||||
else:
|
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 = {
|
ret = {
|
||||||
'host': r.hostname,
|
'host': r.hostname,
|
||||||
'port': r.port or 5432,
|
'port': r.port or 5432,
|
||||||
'database': r.path[1:]
|
'dbname': r.path[1:]
|
||||||
}
|
}
|
||||||
self.data['conn_kwargs'] = ret.copy()
|
self.data['conn_kwargs'] = ret.copy()
|
||||||
|
|
||||||
|
# apply any remaining authentication parameters
|
||||||
if auth and isinstance(auth, dict):
|
if auth and isinstance(auth, dict):
|
||||||
|
ret.update({k: v for k, v in auth.items() if v is not None})
|
||||||
if 'username' in auth:
|
if 'username' in auth:
|
||||||
ret['user'] = auth['username']
|
ret['user'] = ret.pop('username')
|
||||||
if 'password' in auth:
|
|
||||||
ret['password'] = auth['password']
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -192,6 +213,15 @@ class Member(namedtuple('Member', 'index,name,session,data')):
|
|||||||
def is_running(self):
|
def is_running(self):
|
||||||
return self.state == 'running'
|
return self.state == 'running'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def version(self):
|
||||||
|
version = self.data.get('version')
|
||||||
|
if version:
|
||||||
|
try:
|
||||||
|
return tuple(map(int, version.split('.')))
|
||||||
|
except Exception:
|
||||||
|
logger.debug('Failed to parse Patroni version %s', version)
|
||||||
|
|
||||||
|
|
||||||
class RemoteMember(Member):
|
class RemoteMember(Member):
|
||||||
""" Represents a remote master for a standby cluster
|
""" Represents a remote master for a standby cluster
|
||||||
@@ -232,23 +262,23 @@ class Leader(namedtuple('Leader', 'index,session,member')):
|
|||||||
def conn_url(self):
|
def conn_url(self):
|
||||||
return self.member.conn_url
|
return self.member.conn_url
|
||||||
|
|
||||||
|
@property
|
||||||
|
def data(self):
|
||||||
|
return self.member.data
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def timeline(self):
|
def timeline(self):
|
||||||
return self.member.data.get('timeline')
|
return self.data.get('timeline')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def checkpoint_after_promote(self):
|
def checkpoint_after_promote(self):
|
||||||
"""
|
"""
|
||||||
>>> Leader(1, '', Member.from_node(1, '', '', '{"version":"z"}')).checkpoint_after_promote
|
>>> Leader(1, '', Member.from_node(1, '', '', '{"version":"z"}')).checkpoint_after_promote
|
||||||
"""
|
"""
|
||||||
version = self.member.data.get('version')
|
version = self.member.version
|
||||||
if version:
|
|
||||||
try:
|
|
||||||
# 1.5.6 is the last version which doesn't expose checkpoint_after_promote: false
|
# 1.5.6 is the last version which doesn't expose checkpoint_after_promote: false
|
||||||
if tuple(map(int, version.split('.'))) > (1, 5, 6):
|
if version and version > (1, 5, 6):
|
||||||
return self.member.data['role'] == 'master' and 'checkpoint_after_promote' not in self.member.data
|
return self.data.get('role') == 'master' and 'checkpoint_after_promote' not in self.data
|
||||||
except Exception:
|
|
||||||
logger.debug('Failed to parse Patroni version %s', version)
|
|
||||||
|
|
||||||
|
|
||||||
class Failover(namedtuple('Failover', 'index,leader,candidate,scheduled_at')):
|
class Failover(namedtuple('Failover', 'index,leader,candidate,scheduled_at')):
|
||||||
@@ -323,13 +353,21 @@ class ClusterConfig(namedtuple('ClusterConfig', 'index,data,modify_index')):
|
|||||||
self.data.get('permanent_slots') or self.data.get('slots')
|
self.data.get('permanent_slots') or self.data.get('slots')
|
||||||
) or {}
|
) or {}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ignore_slots_matchers(self):
|
||||||
|
return isinstance(self.data, dict) and self.data.get('ignore_slots') or []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def max_timelines_history(self):
|
||||||
|
return self.data.get('max_timelines_history', 0)
|
||||||
|
|
||||||
|
|
||||||
class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
||||||
"""Immutable object (namedtuple) which represents last observed synhcronous replication state
|
"""Immutable object (namedtuple) which represents last observed synhcronous replication state
|
||||||
|
|
||||||
:param index: modification index of a synchronization key in a Configuration Store
|
:param index: modification index of a synchronization key in a Configuration Store
|
||||||
:param leader: reference to member that was leader
|
: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
|
@staticmethod
|
||||||
@@ -361,15 +399,22 @@ class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
|||||||
data = {}
|
data = {}
|
||||||
return SyncState(index, data.get('leader'), data.get('sync_standby'))
|
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):
|
def matches(self, name):
|
||||||
"""
|
"""
|
||||||
Returns if a node name matches one of the nodes in the sync state
|
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')
|
>>> s.matches('foo')
|
||||||
True
|
True
|
||||||
>>> s.matches('bar')
|
>>> s.matches('bar')
|
||||||
True
|
True
|
||||||
|
>>> s.matches('zoo')
|
||||||
|
True
|
||||||
>>> s.matches('baz')
|
>>> s.matches('baz')
|
||||||
False
|
False
|
||||||
>>> s.matches(None)
|
>>> s.matches(None)
|
||||||
@@ -377,7 +422,7 @@ class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
|||||||
>>> SyncState(1, None, None).matches('foo')
|
>>> SyncState(1, None, None).matches('foo')
|
||||||
False
|
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')):
|
class TimelineHistory(namedtuple('TimelineHistory', 'index,value,lines')):
|
||||||
@@ -399,23 +444,28 @@ class TimelineHistory(namedtuple('TimelineHistory', 'index,value,lines')):
|
|||||||
return TimelineHistory(index, value, lines)
|
return TimelineHistory(index, value, lines)
|
||||||
|
|
||||||
|
|
||||||
class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_leader_operation,members,failover,sync,history')):
|
class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_lsn,members,failover,sync,history,slots')):
|
||||||
|
|
||||||
"""Immutable object (namedtuple) which represents PostgreSQL cluster.
|
"""Immutable object (namedtuple) which represents PostgreSQL cluster.
|
||||||
Consists of the following fields:
|
Consists of the following fields:
|
||||||
:param initialize: shows whether this cluster has initialization key stored in DC or not.
|
:param initialize: shows whether this cluster has initialization key stored in DC or not.
|
||||||
:param config: global dynamic configuration, reference to `ClusterConfig` object
|
:param config: global dynamic configuration, reference to `ClusterConfig` object
|
||||||
:param leader: `Leader` object which represents current leader of the cluster
|
:param leader: `Leader` object which represents current leader of the cluster
|
||||||
:param last_leader_operation: int or long object containing position of last known leader operation.
|
:param last_lsn: int or long object containing position of last known leader LSN.
|
||||||
This value is stored in `/optime/leader` key
|
This value is stored in the `/status` key or `/optime/leader` (legacy) key
|
||||||
:param members: list of Member object, all PostgreSQL cluster members including leader
|
:param members: list of Member object, all PostgreSQL cluster members including leader
|
||||||
:param failover: reference to `Failover` object
|
:param failover: reference to `Failover` object
|
||||||
:param sync: reference to `SyncState` object, last observed synchronous replication state.
|
:param sync: reference to `SyncState` object, last observed synchronous replication state.
|
||||||
:param history: reference to `TimelineHistory` object
|
:param history: reference to `TimelineHistory` object
|
||||||
|
:param slots: state of permanent logical replication slots on the primary in the format: {"slot_name": int}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def leader_name(self):
|
||||||
|
return self.leader and self.leader.name
|
||||||
|
|
||||||
def is_unlocked(self):
|
def is_unlocked(self):
|
||||||
return not (self.leader and self.leader.name)
|
return not self.leader_name
|
||||||
|
|
||||||
def has_member(self, member_name):
|
def has_member(self, member_name):
|
||||||
return any(m for m in self.members if m.name == member_name)
|
return any(m for m in self.members if m.name == member_name)
|
||||||
@@ -437,20 +487,41 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_leader_operat
|
|||||||
def is_synchronous_mode(self):
|
def is_synchronous_mode(self):
|
||||||
return self.check_mode('synchronous_mode')
|
return self.check_mode('synchronous_mode')
|
||||||
|
|
||||||
def get_replication_slots(self, name, role):
|
@property
|
||||||
|
def __permanent_slots(self):
|
||||||
|
return self.config and self.config.permanent_slots or {}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __permanent_physical_slots(self):
|
||||||
|
return {name: value for name, value in self.__permanent_slots.items()
|
||||||
|
if not value or isinstance(value, dict) and value.get('type', 'physical') == 'physical'}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __permanent_logical_slots(self):
|
||||||
|
return {name: value for name, value in self.__permanent_slots.items() if isinstance(value, dict)
|
||||||
|
and value.get('type', 'logical') == 'logical' and value.get('database') and value.get('plugin')}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def use_slots(self):
|
||||||
|
return self.config and (self.config.data.get('postgresql') or {}).get('use_slots', True)
|
||||||
|
|
||||||
|
def get_replication_slots(self, my_name, role, nofailover, major_version, show_error=False):
|
||||||
# if the replicatefrom tag is set on the member - we should not create the replication slot for it on
|
# 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 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
|
# 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
|
# master), or if replicatefrom destination member happens to be the current master
|
||||||
|
use_slots = self.use_slots
|
||||||
if role in ('master', 'standby_leader'):
|
if role in ('master', 'standby_leader'):
|
||||||
slot_members = [m.name for m in self.members if m.name != name and
|
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 == name or
|
(m.replicatefrom is None or m.replicatefrom == my_name or
|
||||||
not self.has_member(m.replicatefrom))]
|
not self.has_member(m.replicatefrom))]
|
||||||
permanent_slots = (self.config and self.config.permanent_slots or {}).copy()
|
permanent_slots = self.__permanent_slots if use_slots and \
|
||||||
|
role == 'master' else self.__permanent_physical_slots
|
||||||
else:
|
else:
|
||||||
# only manage slots for replicas that replicate from this one, except for the leader among them
|
# 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 m.replicatefrom == name and m.name != self.leader.name]
|
slot_members = [m.name for m in self.members if use_slots and
|
||||||
permanent_slots = {}
|
m.replicatefrom == my_name and m.name != self.leader_name]
|
||||||
|
permanent_slots = self.__permanent_logical_slots if use_slots and not nofailover else {}
|
||||||
|
|
||||||
slots = {slot_name_from_member_name(name): {'type': 'physical'} for name in slot_members}
|
slots = {slot_name_from_member_name(name): {'type': 'physical'} for name in slot_members}
|
||||||
|
|
||||||
@@ -464,46 +535,82 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_leader_operat
|
|||||||
for k, v in slot_conflicts.items() if len(v) > 1))
|
for k, v in slot_conflicts.items() if len(v) > 1))
|
||||||
|
|
||||||
# "merge" replication slots for members with permanent_replication_slots
|
# "merge" replication slots for members with permanent_replication_slots
|
||||||
|
disabled_permanent_logical_slots = []
|
||||||
for name, value in permanent_slots.items():
|
for name, value in permanent_slots.items():
|
||||||
if not slot_name_re.match(name):
|
if not slot_name_re.match(name):
|
||||||
logger.error("Invalid permanent replication slot name '%s'", name)
|
logger.error("Invalid permanent replication slot name '%s'", name)
|
||||||
logger.error("Slot name may only contain lower case letters, numbers, and the underscore chars")
|
logger.error("Slot name may only contain lower case letters, numbers, and the underscore chars")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if name in slots:
|
value = deepcopy(value) if value else {'type': 'physical'}
|
||||||
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'}
|
|
||||||
|
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
if 'type' not in value:
|
if 'type' not in value:
|
||||||
value['type'] = 'logical' if value.get('database') and value.get('plugin') else 'physical'
|
value['type'] = 'logical' if value.get('database') and value.get('plugin') else 'physical'
|
||||||
|
|
||||||
if value['type'] == 'physical' or value['type'] == 'logical' \
|
if value['type'] == 'physical':
|
||||||
and value.get('database') and value.get('plugin'):
|
# 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 major_version < 110000:
|
||||||
|
disabled_permanent_logical_slots.append(name)
|
||||||
|
elif 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
|
slots[name] = value
|
||||||
continue
|
continue
|
||||||
|
|
||||||
logger.error("Bad value for slot '%s' in permanent_slots: %s", name, permanent_slots[name])
|
logger.error("Bad value for slot '%s' in permanent_slots: %s", name, permanent_slots[name])
|
||||||
|
|
||||||
|
if disabled_permanent_logical_slots and show_error:
|
||||||
|
logger.error("Permanent logical replication slots supported by Patroni only starting from PostgreSQL 11. "
|
||||||
|
"Following slots will not be created: %s.", disabled_permanent_logical_slots)
|
||||||
|
|
||||||
return slots
|
return slots
|
||||||
|
|
||||||
def has_permanent_logical_slots(self, name):
|
def has_permanent_logical_slots(self, my_name, nofailover, major_version=110000):
|
||||||
slots = self.get_replication_slots(name, 'master').values()
|
if major_version < 110000:
|
||||||
|
return False
|
||||||
|
slots = self.get_replication_slots(my_name, 'replica', nofailover, major_version).values()
|
||||||
return any(v for v in slots if v.get("type") == "logical")
|
return any(v for v in slots if v.get("type") == "logical")
|
||||||
|
|
||||||
|
def should_enforce_hot_standby_feedback(self, my_name, nofailover, major_version):
|
||||||
|
"""
|
||||||
|
The hot_standby_feedback must be enabled if the current replica has logical slots
|
||||||
|
or it is working as a cascading replica for the other node that has logical slots.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if major_version < 110000:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if self.has_permanent_logical_slots(my_name, nofailover, major_version):
|
||||||
|
return True
|
||||||
|
|
||||||
|
if self.use_slots:
|
||||||
|
members = [m for m in self.members if m.replicatefrom == my_name and m.name != self.leader_name]
|
||||||
|
return any(self.should_enforce_hot_standby_feedback(m.name, m.nofailover, major_version) for m in members)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_my_slot_name_on_primary(self, my_name, replicatefrom):
|
||||||
|
"""
|
||||||
|
P <-- I <-- L
|
||||||
|
In case of cascading replication we have to check not our physical slot,
|
||||||
|
but slot of the replica that connects us to the primary.
|
||||||
|
"""
|
||||||
|
|
||||||
|
m = self.get_member(replicatefrom, False) if replicatefrom else None
|
||||||
|
return self.get_my_slot_name_on_primary(m.name, m.replicatefrom) if m else slot_name_from_member_name(my_name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def timeline(self):
|
def timeline(self):
|
||||||
"""
|
"""
|
||||||
>>> Cluster(0, 0, 0, 0, 0, 0, 0, 0).timeline
|
>>> Cluster(0, 0, 0, 0, 0, 0, 0, 0, 0).timeline
|
||||||
0
|
0
|
||||||
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[]')).timeline
|
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[]'), 0).timeline
|
||||||
1
|
1
|
||||||
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[["a"]]')).timeline
|
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[["a"]]'), 0).timeline
|
||||||
0
|
0
|
||||||
"""
|
"""
|
||||||
if self.history:
|
if self.history:
|
||||||
@@ -516,6 +623,10 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_leader_operat
|
|||||||
return 1
|
return 1
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def min_version(self):
|
||||||
|
return next(iter(sorted(filter(lambda v: v, [m.version for m in self.members])) + [None]))
|
||||||
|
|
||||||
|
|
||||||
@six.add_metaclass(abc.ABCMeta)
|
@six.add_metaclass(abc.ABCMeta)
|
||||||
class AbstractDCS(object):
|
class AbstractDCS(object):
|
||||||
@@ -527,7 +638,8 @@ class AbstractDCS(object):
|
|||||||
_HISTORY = 'history'
|
_HISTORY = 'history'
|
||||||
_MEMBERS = 'members/'
|
_MEMBERS = 'members/'
|
||||||
_OPTIME = 'optime'
|
_OPTIME = 'optime'
|
||||||
_LEADER_OPTIME = _OPTIME + '/' + _LEADER
|
_STATUS = 'status' # JSON, contains "leader_lsn" and confirmed_flush_lsn of logical "slots" on the leader
|
||||||
|
_LEADER_OPTIME = _OPTIME + '/' + _LEADER # legacy
|
||||||
_SYNC = 'sync'
|
_SYNC = 'sync'
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
@@ -543,7 +655,9 @@ class AbstractDCS(object):
|
|||||||
self._cluster = None
|
self._cluster = None
|
||||||
self._cluster_valid_till = 0
|
self._cluster_valid_till = 0
|
||||||
self._cluster_thread_lock = Lock()
|
self._cluster_thread_lock = Lock()
|
||||||
self._last_leader_operation = ''
|
self._last_lsn = ''
|
||||||
|
self._last_seen = 0
|
||||||
|
self._last_status = {}
|
||||||
self.event = Event()
|
self.event = Event()
|
||||||
|
|
||||||
def client_path(self, path):
|
def client_path(self, path):
|
||||||
@@ -577,6 +691,10 @@ class AbstractDCS(object):
|
|||||||
def history_path(self):
|
def history_path(self):
|
||||||
return self.client_path(self._HISTORY)
|
return self.client_path(self._HISTORY)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def status_path(self):
|
||||||
|
return self.client_path(self._STATUS)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def leader_optime_path(self):
|
def leader_optime_path(self):
|
||||||
return self.client_path(self._LEADER_OPTIME)
|
return self.client_path(self._LEADER_OPTIME)
|
||||||
@@ -609,6 +727,10 @@ class AbstractDCS(object):
|
|||||||
def loop_wait(self):
|
def loop_wait(self):
|
||||||
return self._loop_wait
|
return self._loop_wait
|
||||||
|
|
||||||
|
@property
|
||||||
|
def last_seen(self):
|
||||||
|
return self._last_seen
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def _load_cluster(self):
|
def _load_cluster(self):
|
||||||
"""Internally this method should build `Cluster` object which
|
"""Internally this method should build `Cluster` object which
|
||||||
@@ -619,13 +741,20 @@ class AbstractDCS(object):
|
|||||||
If the current node was running as a master and exception raised,
|
If the current node was running as a master and exception raised,
|
||||||
instance would be demoted."""
|
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:
|
try:
|
||||||
cluster = self._load_cluster()
|
cluster = self._load_cluster()
|
||||||
except Exception:
|
except Exception:
|
||||||
self.reset_cluster()
|
self.reset_cluster()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
self._last_seen = int(time.time())
|
||||||
|
|
||||||
with self._cluster_thread_lock:
|
with self._cluster_thread_lock:
|
||||||
self._cluster = cluster
|
self._cluster = cluster
|
||||||
self._cluster_valid_till = time.time() + self.ttl
|
self._cluster_valid_till = time.time() + self.ttl
|
||||||
@@ -642,14 +771,30 @@ class AbstractDCS(object):
|
|||||||
self._cluster_valid_till = 0
|
self._cluster_valid_till = 0
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def _write_leader_optime(self, last_operation):
|
def _write_leader_optime(self, last_lsn):
|
||||||
"""write current xlog location into `/optime/leader` key in DCS
|
"""write current WAL LSN into `/optime/leader` key in DCS
|
||||||
:param last_operation: absolute xlog location in bytes
|
|
||||||
|
:param last_lsn: absolute WAL LSN in bytes
|
||||||
:returns: `!True` on success."""
|
:returns: `!True` on success."""
|
||||||
|
|
||||||
def write_leader_optime(self, last_operation):
|
def write_leader_optime(self, last_lsn):
|
||||||
if self._last_leader_operation != last_operation and self._write_leader_optime(last_operation):
|
self.write_status({self._OPTIME: last_lsn})
|
||||||
self._last_leader_operation = last_operation
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _write_status(self, value):
|
||||||
|
"""write current WAL LSN and confirmed_flush_lsn of permanent slots into the `/status` key in DCS
|
||||||
|
|
||||||
|
:param value: status serialized in JSON forman
|
||||||
|
:returns: `!True` on success."""
|
||||||
|
|
||||||
|
def write_status(self, value):
|
||||||
|
if not deep_compare(self._last_status, value) and self._write_status(json.dumps(value, separators=(',', ':'))):
|
||||||
|
self._last_status = value
|
||||||
|
cluster = self.cluster
|
||||||
|
min_version = cluster and cluster.min_version
|
||||||
|
if min_version and min_version < (2, 1, 0) and self._last_lsn != value[self._OPTIME]:
|
||||||
|
self._last_lsn = value[self._OPTIME]
|
||||||
|
self._write_leader_optime(str(value[self._OPTIME]))
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
@@ -661,16 +806,20 @@ class AbstractDCS(object):
|
|||||||
You have to use CAS (Compare And Swap) operation in order to update leader key,
|
You have to use CAS (Compare And Swap) operation in order to update leader key,
|
||||||
for example for etcd `prevValue` parameter must be used."""
|
for example for etcd `prevValue` parameter must be used."""
|
||||||
|
|
||||||
def update_leader(self, last_operation, access_is_restricted=False):
|
def update_leader(self, last_lsn, slots=None):
|
||||||
"""Update leader key (or session) ttl and optime/leader
|
"""Update leader key (or session) ttl and optime/leader
|
||||||
|
|
||||||
:param last_operation: absolute xlog location in bytes
|
:param last_lsn: absolute WAL LSN in bytes
|
||||||
|
:param slots: dict with permanent slots confirmed_flush_lsn
|
||||||
:returns: `!True` if leader key (or session) has been updated successfully.
|
:returns: `!True` if leader key (or session) has been updated successfully.
|
||||||
If not, `!False` must be returned and current instance would be demoted."""
|
If not, `!False` must be returned and current instance would be demoted."""
|
||||||
|
|
||||||
ret = self._update_leader()
|
ret = self._update_leader()
|
||||||
if ret and last_operation:
|
if ret and last_lsn:
|
||||||
self.write_leader_optime(last_operation)
|
status = {self._OPTIME: last_lsn}
|
||||||
|
if slots:
|
||||||
|
status['slots'] = slots
|
||||||
|
self.write_status(status)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
@@ -735,10 +884,19 @@ class AbstractDCS(object):
|
|||||||
otherwise it should return `!False`"""
|
otherwise it should return `!False`"""
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def delete_leader(self):
|
def _delete_leader(self):
|
||||||
"""Voluntarily remove leader key from DCS
|
"""Remove leader key from DCS.
|
||||||
This method should remove leader key if current instance is the leader"""
|
This method should remove leader key if current instance is the leader"""
|
||||||
|
|
||||||
|
def delete_leader(self, last_lsn=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_lsn: latest checkpoint location in bytes"""
|
||||||
|
|
||||||
|
if last_lsn:
|
||||||
|
self.write_status({self._OPTIME: last_lsn})
|
||||||
|
return self._delete_leader()
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def cancel_initialization(self):
|
def cancel_initialization(self):
|
||||||
""" Removes the initialize key for a cluster """
|
""" Removes the initialize key for a cluster """
|
||||||
@@ -749,8 +907,10 @@ class AbstractDCS(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def sync_state(leader, sync_standby):
|
def sync_state(leader, sync_standby):
|
||||||
"""Build sync_state dict"""
|
"""Build sync_state dict
|
||||||
return {'leader': leader, 'sync_standby': sync_standby}
|
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):
|
def write_sync_state(self, leader, sync_standby, index=None):
|
||||||
sync_value = self.sync_state(leader, sync_standby)
|
sync_value = self.sync_state(leader, sync_standby)
|
||||||
@@ -777,4 +937,4 @@ class AbstractDCS(object):
|
|||||||
:returns: `!True` if you would like to reschedule the next run of ha cycle"""
|
:returns: `!True` if you would like to reschedule the next run of ha cycle"""
|
||||||
|
|
||||||
self.event.wait(timeout)
|
self.event.wait(timeout)
|
||||||
return self.event.isSet()
|
return self.event.is_set()
|
||||||
|
|||||||
+103
-34
@@ -8,14 +8,16 @@ import ssl
|
|||||||
import time
|
import time
|
||||||
import urllib3
|
import urllib3
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
from consul import ConsulException, NotFound, base
|
from consul import ConsulException, NotFound, base
|
||||||
from patroni.dcs import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
|
||||||
from patroni.exceptions import DCSError
|
|
||||||
from patroni.utils import deep_compare, parse_bool, Retry, RetryFailedError, split_host_port, uri
|
|
||||||
from urllib3.exceptions import HTTPError
|
from urllib3.exceptions import HTTPError
|
||||||
from six.moves.urllib.parse import urlencode, urlparse, quote
|
from six.moves.urllib.parse import urlencode, urlparse, quote
|
||||||
from six.moves.http_client import HTTPException
|
from six.moves.http_client import HTTPException
|
||||||
|
|
||||||
|
from . import AbstractDCS, Cluster, ClusterConfig, Failover, Leader, Member, SyncState, TimelineHistory
|
||||||
|
from ..exceptions import DCSError
|
||||||
|
from ..utils import deep_compare, parse_bool, Retry, RetryFailedError, split_host_port, uri, USER_AGENT
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -35,6 +37,9 @@ class InvalidSession(ConsulException):
|
|||||||
"""invalid session"""
|
"""invalid session"""
|
||||||
|
|
||||||
|
|
||||||
|
Response = namedtuple('Response', 'code,headers,body,content')
|
||||||
|
|
||||||
|
|
||||||
class HTTPClient(object):
|
class HTTPClient(object):
|
||||||
|
|
||||||
def __init__(self, host='127.0.0.1', port=8500, token=None, scheme='http', verify=True, cert=None, ca_cert=None):
|
def __init__(self, host='127.0.0.1', port=8500, token=None, scheme='http', verify=True, cert=None, ca_cert=None):
|
||||||
@@ -52,9 +57,8 @@ class HTTPClient(object):
|
|||||||
kwargs['cert_file'] = cert
|
kwargs['cert_file'] = cert
|
||||||
if ca_cert:
|
if ca_cert:
|
||||||
kwargs['ca_certs'] = ca_cert
|
kwargs['ca_certs'] = ca_cert
|
||||||
if verify or ca_cert:
|
kwargs['cert_reqs'] = ssl.CERT_REQUIRED if verify or ca_cert else ssl.CERT_NONE
|
||||||
kwargs['cert_reqs'] = ssl.CERT_REQUIRED
|
self.http = urllib3.PoolManager(num_pools=10, maxsize=10, **kwargs)
|
||||||
self.http = urllib3.PoolManager(num_pools=10, **kwargs)
|
|
||||||
self._ttl = None
|
self._ttl = None
|
||||||
|
|
||||||
def set_read_timeout(self, timeout):
|
def set_read_timeout(self, timeout):
|
||||||
@@ -71,16 +75,17 @@ class HTTPClient(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def response(response):
|
def response(response):
|
||||||
data = response.data.decode('utf-8')
|
content = response.data
|
||||||
|
body = content.decode('utf-8')
|
||||||
if response.status == 500:
|
if response.status == 500:
|
||||||
msg = '{0} {1}'.format(response.status, data)
|
msg = '{0} {1}'.format(response.status, body)
|
||||||
if data.startswith('Invalid Session TTL'):
|
if body.startswith('Invalid Session TTL'):
|
||||||
raise InvalidSessionTTL(msg)
|
raise InvalidSessionTTL(msg)
|
||||||
elif data.startswith('invalid session'):
|
elif body.startswith('invalid session'):
|
||||||
raise InvalidSession(msg)
|
raise InvalidSession(msg)
|
||||||
else:
|
else:
|
||||||
raise ConsulInternalError(msg)
|
raise ConsulInternalError(msg)
|
||||||
return base.Response(response.status, response.headers, data)
|
return Response(response.status, response.headers, body, content)
|
||||||
|
|
||||||
def uri(self, path, params=None):
|
def uri(self, path, params=None):
|
||||||
return '{0}{1}{2}'.format(self.base_uri, path, params and '?' + urlencode(params) or '')
|
return '{0}{1}{2}'.format(self.base_uri, path, params and '?' + urlencode(params) or '')
|
||||||
@@ -89,7 +94,7 @@ class HTTPClient(object):
|
|||||||
if method not in ('get', 'post', 'put', 'delete'):
|
if method not in ('get', 'post', 'put', 'delete'):
|
||||||
raise AttributeError("HTTPClient instance has no attribute '{0}'".format(method))
|
raise AttributeError("HTTPClient instance has no attribute '{0}'".format(method))
|
||||||
|
|
||||||
def wrapper(callback, path, params=None, data=''):
|
def wrapper(callback, path, params=None, data='', headers=None):
|
||||||
# python-consul doesn't allow to specify ttl smaller then 10 seconds
|
# python-consul doesn't allow to specify ttl smaller then 10 seconds
|
||||||
# because session_ttl_min defaults to 10s, so we have to do this ugly dirty hack...
|
# because session_ttl_min defaults to 10s, so we have to do this ugly dirty hack...
|
||||||
if method == 'put' and path == '/v1/session/create':
|
if method == 'put' and path == '/v1/session/create':
|
||||||
@@ -106,13 +111,15 @@ class HTTPClient(object):
|
|||||||
# According to the documentation a small random amount of additional wait time is added to the
|
# According to the documentation a small random amount of additional wait time is added to the
|
||||||
# supplied maximum wait time to spread out the wake up time of any concurrent requests. This adds
|
# supplied maximum wait time to spread out the wake up time of any concurrent requests. This adds
|
||||||
# up to wait / 16 additional time to the maximum duration. Since our goal is actually getting a
|
# up to wait / 16 additional time to the maximum duration. Since our goal is actually getting a
|
||||||
# response rather read timeout we will add to the timeout a sligtly bigger value.
|
# response rather read timeout we will add to the timeout a slightly bigger value.
|
||||||
kwargs['timeout'] = timeout + max(timeout/15.0, 1)
|
kwargs['timeout'] = timeout + max(timeout/15.0, 1)
|
||||||
else:
|
else:
|
||||||
kwargs['timeout'] = self._read_timeout
|
kwargs['timeout'] = self._read_timeout
|
||||||
|
kwargs['headers'] = (headers or {}).copy()
|
||||||
|
kwargs['headers'].update(urllib3.make_headers(user_agent=USER_AGENT))
|
||||||
token = params.pop('token', self.token) if isinstance(params, dict) else self.token
|
token = params.pop('token', self.token) if isinstance(params, dict) else self.token
|
||||||
if token:
|
if token:
|
||||||
kwargs['headers'] = {'X-Consul-Token': token}
|
kwargs['headers']['X-Consul-Token'] = token
|
||||||
return callback(self.response(self.http.request(method.upper(), self.uri(path, params), **kwargs)))
|
return callback(self.response(self.http.request(method.upper(), self.uri(path, params), **kwargs)))
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
@@ -125,7 +132,7 @@ class ConsulClient(base.Consul):
|
|||||||
self.token = kwargs.get('token')
|
self.token = kwargs.get('token')
|
||||||
super(ConsulClient, self).__init__(*args, **kwargs)
|
super(ConsulClient, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def connect(self, *args, **kwargs):
|
def http_connect(self, *args, **kwargs):
|
||||||
kwargs.update(dict(zip(['host', 'port', 'scheme', 'verify'], args)))
|
kwargs.update(dict(zip(['host', 'port', 'scheme', 'verify'], args)))
|
||||||
if self._cert:
|
if self._cert:
|
||||||
kwargs['cert'] = self._cert
|
kwargs['cert'] = self._cert
|
||||||
@@ -135,6 +142,9 @@ class ConsulClient(base.Consul):
|
|||||||
kwargs['token'] = self.token
|
kwargs['token'] = self.token
|
||||||
return HTTPClient(**kwargs)
|
return HTTPClient(**kwargs)
|
||||||
|
|
||||||
|
def connect(self, *args, **kwargs):
|
||||||
|
return self.http_connect(*args, **kwargs)
|
||||||
|
|
||||||
def reload_config(self, config):
|
def reload_config(self, config):
|
||||||
self.http.token = self.token = config.get('token')
|
self.http.token = self.token = config.get('token')
|
||||||
self.consistency = config.get('consistency', 'default')
|
self.consistency = config.get('consistency', 'default')
|
||||||
@@ -215,16 +225,18 @@ class Consul(AbstractDCS):
|
|||||||
self.set_retry_timeout(config['retry_timeout'])
|
self.set_retry_timeout(config['retry_timeout'])
|
||||||
self.set_ttl(config.get('ttl') or 30)
|
self.set_ttl(config.get('ttl') or 30)
|
||||||
self._last_session_refresh = 0
|
self._last_session_refresh = 0
|
||||||
self.__session_checks = config.get('checks')
|
self.__session_checks = config.get('checks', [])
|
||||||
self._register_service = config.get('register_service', False)
|
self._register_service = config.get('register_service', False)
|
||||||
|
self._previous_loop_register_service = self._register_service
|
||||||
|
self._service_tags = sorted(config.get('service_tags', []))
|
||||||
|
self._previous_loop_service_tags = self._service_tags
|
||||||
if self._register_service:
|
if self._register_service:
|
||||||
self._service_name = service_name_from_scope_name(self._scope)
|
self._set_service_name()
|
||||||
if self._scope != self._service_name:
|
|
||||||
logger.warning('Using %s as consul service name instead of scope name %s', self._service_name,
|
|
||||||
self._scope)
|
|
||||||
self._service_check_interval = config.get('service_check_interval', '5s')
|
self._service_check_interval = config.get('service_check_interval', '5s')
|
||||||
|
self._service_check_tls_server_name = config.get('service_check_tls_server_name', None)
|
||||||
if not self._ctl:
|
if not self._ctl:
|
||||||
self.create_session()
|
self.create_session()
|
||||||
|
self._previous_loop_token = self._client.token
|
||||||
|
|
||||||
def retry(self, *args, **kwargs):
|
def retry(self, *args, **kwargs):
|
||||||
return self._retry.copy()(*args, **kwargs)
|
return self._retry.copy()(*args, **kwargs)
|
||||||
@@ -239,7 +251,18 @@ class Consul(AbstractDCS):
|
|||||||
|
|
||||||
def reload_config(self, config):
|
def reload_config(self, config):
|
||||||
super(Consul, self).reload_config(config)
|
super(Consul, self).reload_config(config)
|
||||||
self._client.reload_config(config.get('consul', {}))
|
|
||||||
|
consul_config = config.get('consul', {})
|
||||||
|
self._client.reload_config(consul_config)
|
||||||
|
self._previous_loop_service_tags = self._service_tags
|
||||||
|
self._service_tags = sorted(consul_config.get('service_tags', []))
|
||||||
|
|
||||||
|
should_register_service = consul_config.get('register_service', False)
|
||||||
|
if should_register_service and not self._register_service:
|
||||||
|
self._set_service_name()
|
||||||
|
|
||||||
|
self._previous_loop_register_service = self._register_service
|
||||||
|
self._register_service = should_register_service
|
||||||
|
|
||||||
def set_ttl(self, ttl):
|
def set_ttl(self, ttl):
|
||||||
if self._client.http.set_ttl(ttl/2.0): # Consul multiplies the TTL by 2x
|
if self._client.http.set_ttl(ttl/2.0): # Consul multiplies the TTL by 2x
|
||||||
@@ -326,9 +349,24 @@ class Consul(AbstractDCS):
|
|||||||
history = nodes.get(self._HISTORY)
|
history = nodes.get(self._HISTORY)
|
||||||
history = history and TimelineHistory.from_node(history['ModifyIndex'], history['Value'])
|
history = history and TimelineHistory.from_node(history['ModifyIndex'], history['Value'])
|
||||||
|
|
||||||
# get last leader operation
|
# get last known leader lsn and slots
|
||||||
last_leader_operation = nodes.get(self._LEADER_OPTIME)
|
status = nodes.get(self._STATUS)
|
||||||
last_leader_operation = 0 if last_leader_operation is None else int(last_leader_operation['Value'])
|
if status:
|
||||||
|
try:
|
||||||
|
status = json.loads(status['Value'])
|
||||||
|
last_lsn = status.get(self._OPTIME)
|
||||||
|
slots = status.get('slots')
|
||||||
|
except Exception:
|
||||||
|
slots = last_lsn = None
|
||||||
|
else:
|
||||||
|
last_lsn = nodes.get(self._LEADER_OPTIME)
|
||||||
|
last_lsn = last_lsn and last_lsn['Value']
|
||||||
|
slots = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
last_lsn = int(last_lsn)
|
||||||
|
except Exception:
|
||||||
|
last_lsn = 0
|
||||||
|
|
||||||
# get list of members
|
# get list of members
|
||||||
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
||||||
@@ -355,9 +393,9 @@ class Consul(AbstractDCS):
|
|||||||
sync = nodes.get(self._SYNC)
|
sync = nodes.get(self._SYNC)
|
||||||
sync = SyncState.from_node(sync and sync['ModifyIndex'], sync and sync['Value'])
|
sync = SyncState.from_node(sync and sync['ModifyIndex'], sync and sync['Value'])
|
||||||
|
|
||||||
return Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
return Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots)
|
||||||
except NotFound:
|
except NotFound:
|
||||||
return Cluster(None, None, None, None, [], None, None, None)
|
return Cluster(None, None, None, None, [], None, None, None, None)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('get_cluster')
|
logger.exception('get_cluster')
|
||||||
raise ConsulError('Consul is not responding properly')
|
raise ConsulError('Consul is not responding properly')
|
||||||
@@ -366,20 +404,28 @@ class Consul(AbstractDCS):
|
|||||||
def touch_member(self, data, permanent=False):
|
def touch_member(self, data, permanent=False):
|
||||||
cluster = self.cluster
|
cluster = self.cluster
|
||||||
member = cluster and cluster.get_member(self._name, fallback_to_leader=False)
|
member = cluster and cluster.get_member(self._name, fallback_to_leader=False)
|
||||||
|
|
||||||
|
try:
|
||||||
create_member = not permanent and self.refresh_session()
|
create_member = not permanent and self.refresh_session()
|
||||||
|
except DCSError:
|
||||||
|
return False
|
||||||
|
|
||||||
if member and (create_member or member.session != self._session):
|
if member and (create_member or member.session != self._session):
|
||||||
self._client.kv.delete(self.member_path)
|
self._client.kv.delete(self.member_path)
|
||||||
create_member = True
|
create_member = True
|
||||||
|
|
||||||
|
if self._register_service or self._previous_loop_register_service:
|
||||||
|
try:
|
||||||
|
self.update_service(not create_member and member and member.data or {}, data)
|
||||||
|
except Exception:
|
||||||
|
logger.exception('update_service')
|
||||||
|
|
||||||
if not create_member and member and deep_compare(data, member.data):
|
if not create_member and member and deep_compare(data, member.data):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
args = {} if permanent else {'acquire': self._session}
|
args = {} if permanent else {'acquire': self._session}
|
||||||
self._client.kv.put(self.member_path, json.dumps(data, separators=(',', ':')), **args)
|
self._client.kv.put(self.member_path, json.dumps(data, separators=(',', ':')), **args)
|
||||||
if self._register_service:
|
|
||||||
self.update_service(not create_member and member and member.data or {}, data)
|
|
||||||
return True
|
return True
|
||||||
except InvalidSession:
|
except InvalidSession:
|
||||||
self._session = None
|
self._session = None
|
||||||
@@ -388,6 +434,11 @@ class Consul(AbstractDCS):
|
|||||||
logger.exception('touch_member')
|
logger.exception('touch_member')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def _set_service_name(self):
|
||||||
|
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, self._scope)
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
def register_service(self, service_name, **kwargs):
|
def register_service(self, service_name, **kwargs):
|
||||||
logger.info('Register service %s, params %s', service_name, kwargs)
|
logger.info('Register service %s, params %s', service_name, kwargs)
|
||||||
@@ -409,17 +460,27 @@ class Consul(AbstractDCS):
|
|||||||
conn_parts = urlparse(data['conn_url'])
|
conn_parts = urlparse(data['conn_url'])
|
||||||
check = base.Check.http(api_parts.geturl(), self._service_check_interval,
|
check = base.Check.http(api_parts.geturl(), self._service_check_interval,
|
||||||
deregister='{0}s'.format(self._client.http.ttl * 10))
|
deregister='{0}s'.format(self._client.http.ttl * 10))
|
||||||
|
if self._service_check_tls_server_name is not None:
|
||||||
|
check['TLSServerName'] = self._service_check_tls_server_name
|
||||||
|
tags = self._service_tags[:]
|
||||||
|
tags.append(role)
|
||||||
|
self._previous_loop_service_tags = self._service_tags
|
||||||
|
self._previous_loop_token = self._client.token
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
'service_id': '{0}/{1}'.format(self._scope, self._name),
|
'service_id': '{0}/{1}'.format(self._scope, self._name),
|
||||||
'address': conn_parts.hostname,
|
'address': conn_parts.hostname,
|
||||||
'port': conn_parts.port,
|
'port': conn_parts.port,
|
||||||
'check': check,
|
'check': check,
|
||||||
'tags': [role]
|
'tags': tags,
|
||||||
|
'enable_tag_override': True,
|
||||||
}
|
}
|
||||||
|
|
||||||
if state == 'stopped':
|
if state == 'stopped' or (not self._register_service and self._previous_loop_register_service):
|
||||||
|
self._previous_loop_register_service = self._register_service
|
||||||
return self.deregister_service(params['service_id'])
|
return self.deregister_service(params['service_id'])
|
||||||
|
|
||||||
|
self._previous_loop_register_service = self._register_service
|
||||||
if role in ['master', 'replica', 'standby-leader']:
|
if role in ['master', 'replica', 'standby-leader']:
|
||||||
if state != 'running':
|
if state != 'running':
|
||||||
return
|
return
|
||||||
@@ -438,7 +499,11 @@ class Consul(AbstractDCS):
|
|||||||
if old_data.get(key) != new_data[key]:
|
if old_data.get(key) != new_data[key]:
|
||||||
update = True
|
update = True
|
||||||
|
|
||||||
if force or update:
|
if (
|
||||||
|
force or update or self._register_service != self._previous_loop_register_service
|
||||||
|
or self._service_tags != self._previous_loop_service_tags
|
||||||
|
or self._client.token != self._previous_loop_token
|
||||||
|
):
|
||||||
return self._update_service(new_data)
|
return self._update_service(new_data)
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
@@ -474,8 +539,12 @@ class Consul(AbstractDCS):
|
|||||||
return self._client.kv.put(self.config_path, value, cas=index)
|
return self._client.kv.put(self.config_path, value, cas=index)
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
def _write_leader_optime(self, last_operation):
|
def _write_leader_optime(self, last_lsn):
|
||||||
return self._client.kv.put(self.leader_optime_path, last_operation)
|
return self._client.kv.put(self.leader_optime_path, last_lsn)
|
||||||
|
|
||||||
|
@catch_consul_errors
|
||||||
|
def _write_status(self, value):
|
||||||
|
return self._client.kv.put(self.status_path, value)
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
@@ -502,7 +571,7 @@ class Consul(AbstractDCS):
|
|||||||
return self._client.kv.put(self.history_path, value)
|
return self._client.kv.put(self.history_path, value)
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
def delete_leader(self):
|
def _delete_leader(self):
|
||||||
cluster = self.cluster
|
cluster = self.cluster
|
||||||
if cluster and isinstance(cluster.leader, Leader) and cluster.leader.name == self._name:
|
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)
|
return self._client.kv.delete(self.leader_path, cas=cluster.leader.index)
|
||||||
|
|||||||
+310
-145
@@ -1,30 +1,36 @@
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
import abc
|
||||||
import etcd
|
import etcd
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import urllib3.util.connection
|
import urllib3.util.connection
|
||||||
import random
|
import random
|
||||||
import requests
|
|
||||||
import six
|
import six
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from dns.exception import DNSException
|
from dns.exception import DNSException
|
||||||
from dns import resolver
|
from dns import resolver
|
||||||
from patroni.dcs import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
from urllib3 import Timeout
|
||||||
from patroni.exceptions import DCSError
|
from urllib3.exceptions import HTTPError, ReadTimeoutError, ProtocolError
|
||||||
from patroni.utils import Retry, RetryFailedError, split_host_port, uri
|
|
||||||
from urllib3.exceptions import HTTPError, ReadTimeoutError
|
|
||||||
from requests.exceptions import RequestException
|
|
||||||
from six.moves.queue import Queue
|
from six.moves.queue import Queue
|
||||||
from six.moves.http_client import HTTPException
|
from six.moves.http_client import HTTPException
|
||||||
from six.moves.urllib_parse import urlparse
|
from six.moves.urllib_parse import urlparse
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
|
from . import AbstractDCS, Cluster, ClusterConfig, Failover, Leader, Member, SyncState, TimelineHistory
|
||||||
|
from ..exceptions import DCSError
|
||||||
|
from ..request import get as requests_get
|
||||||
|
from ..utils import Retry, RetryFailedError, split_host_port, uri, USER_AGENT
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class EtcdRaftInternal(etcd.EtcdException):
|
||||||
|
"""Raft Internal Error"""
|
||||||
|
|
||||||
|
|
||||||
class EtcdError(DCSError):
|
class EtcdError(DCSError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -65,16 +71,20 @@ class DnsCachingResolver(Thread):
|
|||||||
def resolve_async(self, host, port, attempt=0):
|
def resolve_async(self, host, port, attempt=0):
|
||||||
self._resolve_queue.put(((host, port), attempt))
|
self._resolve_queue.put(((host, port), attempt))
|
||||||
|
|
||||||
|
def remove(self, host, port):
|
||||||
|
self._cache.pop((host, port), None)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _do_resolve(host, port):
|
def _do_resolve(host, port):
|
||||||
try:
|
try:
|
||||||
return socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
return socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
||||||
except socket.gaierror:
|
except Exception as e:
|
||||||
logger.warning('failed to resolve host %s', host)
|
logger.warning('failed to resolve host %s: %s', host, e)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
class Client(etcd.Client):
|
@six.add_metaclass(abc.ABCMeta)
|
||||||
|
class AbstractEtcdClientWithFailover(etcd.Client):
|
||||||
|
|
||||||
def __init__(self, config, dns_resolver, cache_ttl=300):
|
def __init__(self, config, dns_resolver, cache_ttl=300):
|
||||||
self._dns_resolver = dns_resolver
|
self._dns_resolver = dns_resolver
|
||||||
@@ -82,34 +92,81 @@ class Client(etcd.Client):
|
|||||||
self._machines_cache_updated = 0
|
self._machines_cache_updated = 0
|
||||||
args = {p: config.get(p) for p in ('host', 'port', 'protocol', 'use_proxies', 'username', 'password',
|
args = {p: config.get(p) for p in ('host', 'port', 'protocol', 'use_proxies', 'username', 'password',
|
||||||
'cert', 'ca_cert') if config.get(p)}
|
'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
|
# 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
|
# 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.http.connection_pool_kw.pop('ssl_version', None)
|
||||||
self._config = config
|
self._config = config
|
||||||
|
self._initial_machines_cache = []
|
||||||
self._load_machines_cache()
|
self._load_machines_cache()
|
||||||
self._allow_reconnect = True
|
self._allow_reconnect = True
|
||||||
|
# allow passing retry argument to api_execute in params
|
||||||
|
self._comparison_conditions.add('retry')
|
||||||
|
self._read_options.add('retry')
|
||||||
|
self._del_conditions.add('retry')
|
||||||
|
|
||||||
def _build_request_parameters(self):
|
def _calculate_timeouts(self, etcd_nodes, timeout=None):
|
||||||
kwargs = {'headers': self._get_headers(), 'redirect': self.allow_redirect}
|
"""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."""
|
||||||
|
|
||||||
# calculate the number of retries and timeout *per node*
|
per_node_timeout = timeout = float(timeout or self.read_timeout)
|
||||||
# actual number of retries depends on the number of nodes
|
|
||||||
etcd_nodes = len(self._machines_cache) + 1
|
|
||||||
kwargs['retries'] = 0 if etcd_nodes > 3 else (1 if etcd_nodes > 1 else 2)
|
|
||||||
|
|
||||||
# if etcd_nodes > 3:
|
max_retries = 4 - min(etcd_nodes, 3)
|
||||||
# kwargs.update({'retries': 0, 'timeout': float(self.read_timeout)/etcd_nodes})
|
per_node_retries = 1
|
||||||
# elif etcd_nodes > 1:
|
min_timeout = 1.0
|
||||||
# kwargs.update({'retries': 1, 'timeout': self.read_timeout/2.0/etcd_nodes})
|
|
||||||
# else:
|
while etcd_nodes > 0:
|
||||||
# kwargs.update({'retries': 2, 'timeout': self.read_timeout/3.0})
|
per_node_timeout = float(timeout) / etcd_nodes
|
||||||
kwargs['timeout'] = self.read_timeout/float(kwargs['retries'] + 1)/etcd_nodes
|
if per_node_timeout >= min_timeout:
|
||||||
|
# for small clusters we will try to do more than on 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
|
||||||
|
etcd_nodes -= 1
|
||||||
|
max_retries = 1
|
||||||
|
|
||||||
|
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 _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(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
|
return kwargs
|
||||||
|
|
||||||
def set_machines_cache_ttl(self, cache_ttl):
|
def set_machines_cache_ttl(self, cache_ttl):
|
||||||
self._machines_cache_ttl = 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
|
@property
|
||||||
def machines(self):
|
def machines(self):
|
||||||
"""Original `machines` method(property) of `etcd.Client` class raise exception
|
"""Original `machines` method(property) of `etcd.Client` class raise exception
|
||||||
@@ -122,100 +179,106 @@ class Client(etcd.Client):
|
|||||||
Also this method implements the same timeout-retry logic as `api_execute`, because
|
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."""
|
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:
|
try:
|
||||||
response = self.http.request(self._MGET, self._base_uri + self.version_prefix + '/machines', **kwargs)
|
machines = list(set(self._get_members(base_uri, **kwargs)))
|
||||||
data = self._handle_server_response(response).data.decode('utf-8')
|
|
||||||
machines = [m.strip() for m in data.split(',') if m.strip()]
|
|
||||||
logger.debug("Retrieved list of machines: %s", machines)
|
logger.debug("Retrieved list of machines: %s", machines)
|
||||||
if not machines:
|
if machines:
|
||||||
raise etcd.EtcdException
|
|
||||||
random.shuffle(machines)
|
random.shuffle(machines)
|
||||||
for url in machines:
|
if not self._use_proxies:
|
||||||
r = urlparse(url)
|
self._update_dns_cache(self._dns_resolver.resolve_async, machines)
|
||||||
port = r.port or (443 if r.scheme == 'https' else 80)
|
|
||||||
self._dns_resolver.resolve_async(r.hostname, port)
|
|
||||||
return machines
|
return machines
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# We can't get the list of machines, if one server is in the
|
self.http.clear()
|
||||||
# machines cache, try on it
|
logger.error("Failed to get list of machines from %s%s: %r", base_uri, self.version_prefix, e)
|
||||||
logger.error("Failed to get list of machines from %s%s: %r", self._base_uri, self.version_prefix, e)
|
|
||||||
if self._machines_cache:
|
raise etcd.EtcdConnectionFailed('No more machines in the cluster')
|
||||||
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 []
|
|
||||||
|
|
||||||
def set_read_timeout(self, timeout):
|
def set_read_timeout(self, timeout):
|
||||||
self._read_timeout = timeout
|
self._read_timeout = timeout
|
||||||
|
|
||||||
def _do_http_request(self, request_executor, method, url, fields=None, **kwargs):
|
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:
|
try:
|
||||||
response = request_executor(method, url, fields=fields, **kwargs)
|
response = request_executor(method, base_uri + path, **kwargs)
|
||||||
response.data.decode('utf-8')
|
response.data.decode('utf-8')
|
||||||
self._check_cluster_id(response)
|
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:
|
except (HTTPError, HTTPException, socket.error, socket.timeout) as e:
|
||||||
|
self.http.clear()
|
||||||
|
if not retry:
|
||||||
|
if len(machines_cache) == 1:
|
||||||
|
self.set_base_uri(self._base_uri) # trigger Etcd3 watcher restart
|
||||||
|
# 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.
|
||||||
|
elif i + 1 < len(machines_cache):
|
||||||
|
self.set_base_uri(machines_cache[i + 1])
|
||||||
if (isinstance(fields, dict) and fields.get("wait") == "true" and
|
if (isinstance(fields, dict) and fields.get("wait") == "true" and
|
||||||
isinstance(e, ReadTimeoutError)):
|
isinstance(e, (ReadTimeoutError, ProtocolError))):
|
||||||
logger.debug("Watch timed out.")
|
logger.debug("Watch timed out.")
|
||||||
raise etcd.EtcdWatchTimedOut("Watch timed out: {0}".format(e), cause=e)
|
raise etcd.EtcdWatchTimedOut("Watch timed out: {0}".format(e), cause=e)
|
||||||
logger.error("Request to server %s failed: %r", self._base_uri, e)
|
logger.error("Request to server %s failed: %r", base_uri, e)
|
||||||
logger.info("Reconnection allowed, looking for another server.")
|
logger.info("Reconnection allowed, looking for another server.")
|
||||||
self._base_uri = self._next_server(cause=e)
|
if not retry:
|
||||||
response = False
|
raise etcd.EtcdException('{0} {1} request failed'.format(method, path))
|
||||||
return response
|
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):
|
def api_execute(self, path, method, params=None, timeout=None):
|
||||||
if not path.startswith('/'):
|
retry = params.pop('retry', None) if isinstance(params, dict) else None
|
||||||
raise ValueError('Path does not start with /')
|
|
||||||
|
|
||||||
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
|
# Update machines_cache if previous attempt of update has failed
|
||||||
if self._update_machines_cache:
|
if self._update_machines_cache:
|
||||||
self._load_machines_cache()
|
self._load_machines_cache()
|
||||||
elif not self._use_proxies and time.time() - self._machines_cache_updated > self._machines_cache_ttl:
|
elif not self._use_proxies and time.time() - self._machines_cache_updated > self._machines_cache_ttl:
|
||||||
self._refresh_machines_cache()
|
self._refresh_machines_cache()
|
||||||
self._machines_cache_updated = time.time()
|
|
||||||
|
|
||||||
kwargs.update(self._build_request_parameters())
|
machines_cache = self.machines_cache
|
||||||
|
etcd_nodes = len(machines_cache)
|
||||||
|
|
||||||
if timeout is not None:
|
kwargs = self._prepare_common_parameters(etcd_nodes, timeout)
|
||||||
kwargs.update({'retries': 0, 'timeout': timeout})
|
request_executor = self._prepare_request(kwargs, params, method)
|
||||||
|
|
||||||
response = False
|
|
||||||
|
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
some_request_failed = False
|
response = self._do_http_request(retry, machines_cache, request_executor, method, path, **kwargs)
|
||||||
while not response:
|
|
||||||
response = self._do_http_request(request_executor, method, self._base_uri + path, **kwargs)
|
|
||||||
|
|
||||||
if response is False:
|
|
||||||
some_request_failed = True
|
|
||||||
if some_request_failed:
|
|
||||||
self._refresh_machines_cache()
|
|
||||||
except etcd.EtcdConnectionFailed as e:
|
|
||||||
if isinstance(e, etcd.EtcdWatchTimedOut) and self._machines_cache:
|
|
||||||
self._base_uri = self._next_server()
|
|
||||||
else:
|
|
||||||
self._update_machines_cache = True
|
|
||||||
if not response:
|
|
||||||
raise
|
|
||||||
return self._handle_server_response(response)
|
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(etcd_nodes, remaining_time)
|
||||||
|
if nodes == 0:
|
||||||
|
self._update_machines_cache = True
|
||||||
|
self.set_base_uri(self._base_uri) # trigger Etcd3 watcher restart
|
||||||
|
raise ex
|
||||||
|
retry.sleep_func(sleeptime)
|
||||||
|
retry.update_delay()
|
||||||
|
# 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
|
@staticmethod
|
||||||
def get_srv_record(host):
|
def get_srv_record(host):
|
||||||
@@ -224,25 +287,26 @@ class Client(etcd.Client):
|
|||||||
except DNSException:
|
except DNSException:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def _get_machines_cache_from_srv(self, srv):
|
def _get_machines_cache_from_srv(self, srv, srv_suffix=None):
|
||||||
"""Fetch list of etcd-cluster member by resolving _etcd-server._tcp. SRV record.
|
"""Fetch list of etcd-cluster member by resolving _etcd-server._tcp. SRV record.
|
||||||
This record should contain list of host and peer ports which could be used to run
|
This record should contain list of host and peer ports which could be used to run
|
||||||
'GET http://{host}:{port}/members' request (peer protocol)"""
|
'GET http://{host}:{port}/members' request (peer protocol)"""
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for r in ['-client-ssl', '-client', '-ssl', '', '-server-ssl', '-server']:
|
for r in ['-client-ssl', '-client', '-ssl', '', '-server-ssl', '-server']:
|
||||||
|
r = '{0}-{1}'.format(r, srv_suffix) if srv_suffix else r
|
||||||
protocol = 'https' if '-ssl' in r else 'http'
|
protocol = 'https' if '-ssl' in r else 'http'
|
||||||
endpoint = '/members' if '-server' in r else ''
|
endpoint = '/members' if '-server' in r else ''
|
||||||
for host, port in self.get_srv_record('_etcd{0}._tcp.{1}'.format(r, srv)):
|
for host, port in self.get_srv_record('_etcd{0}._tcp.{1}'.format(r, srv)):
|
||||||
url = uri(protocol, (host, port), endpoint)
|
url = uri(protocol, (host, port), endpoint)
|
||||||
if endpoint:
|
if endpoint:
|
||||||
try:
|
try:
|
||||||
response = requests.get(url, timeout=self.read_timeout, verify=False)
|
response = requests_get(url, timeout=self.read_timeout, verify=False)
|
||||||
if response.ok:
|
if response.status < 400:
|
||||||
for member in response.json():
|
for member in json.loads(response.data.decode('utf-8')):
|
||||||
ret.extend(member['clientURLs'])
|
ret.extend(member['clientURLs'])
|
||||||
break
|
break
|
||||||
except RequestException:
|
except Exception:
|
||||||
logger.exception('GET %s', url)
|
logger.exception('GET %s', url)
|
||||||
else:
|
else:
|
||||||
ret.append(url)
|
ret.append(url)
|
||||||
@@ -267,7 +331,7 @@ class Client(etcd.Client):
|
|||||||
|
|
||||||
machines_cache = []
|
machines_cache = []
|
||||||
if 'srv' in self._config:
|
if 'srv' in self._config:
|
||||||
machines_cache = self._get_machines_cache_from_srv(self._config['srv'])
|
machines_cache = self._get_machines_cache_from_srv(self._config['srv'], self._config.get('srv_suffix'))
|
||||||
|
|
||||||
if not machines_cache and 'hosts' in self._config:
|
if not machines_cache and 'hosts' in self._config:
|
||||||
machines_cache = list(self._config['hosts'])
|
machines_cache = list(self._config['hosts'])
|
||||||
@@ -276,6 +340,13 @@ class Client(etcd.Client):
|
|||||||
machines_cache = self._get_machines_cache_from_dns(self._config['host'], self._config['port'])
|
machines_cache = self._get_machines_cache_from_dns(self._config['host'], self._config['port'])
|
||||||
return machines_cache
|
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):
|
def _load_machines_cache(self):
|
||||||
"""This method should fill up `_machines_cache` from scratch.
|
"""This method should fill up `_machines_cache` from scratch.
|
||||||
It could happen only in two cases:
|
It could happen only in two cases:
|
||||||
@@ -287,40 +358,96 @@ class Client(etcd.Client):
|
|||||||
if 'srv' not in self._config and 'host' not in self._config and 'hosts' not in self._config:
|
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')
|
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
|
# Can not bootstrap list of etcd-cluster members, giving up
|
||||||
if not self._machines_cache:
|
if not machines_cache:
|
||||||
raise etcd.EtcdException
|
raise etcd.EtcdException
|
||||||
|
|
||||||
# After filling up initial list of machines_cache we should ask etcd-cluster about actual list
|
# enforce resolving dns name,they might get new ips
|
||||||
self._base_uri = self._next_server()
|
self._update_dns_cache(self._dns_resolver.remove, machines_cache)
|
||||||
self._refresh_machines_cache()
|
|
||||||
|
# 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
|
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()
|
self._machines_cache_updated = time.time()
|
||||||
|
|
||||||
def _refresh_machines_cache(self):
|
def set_base_uri(self, value):
|
||||||
self._machines_cache = self._get_machines_cache_from_config() if self._use_proxies else self.machines
|
if self._base_uri != value:
|
||||||
if self._base_uri in self._machines_cache:
|
logger.info('Selected new etcd server %s', value)
|
||||||
self._machines_cache.remove(self._base_uri)
|
self._base_uri = value
|
||||||
|
|
||||||
|
|
||||||
class Etcd(AbstractDCS):
|
class EtcdClient(AbstractEtcdClientWithFailover):
|
||||||
|
|
||||||
def __init__(self, config):
|
ERROR_CLS = EtcdError
|
||||||
super(Etcd, self).__init__(config)
|
|
||||||
self._ttl = int(config.get('ttl') or 30)
|
def __del__(self):
|
||||||
|
if self.http is not None:
|
||||||
|
try:
|
||||||
|
self.http.clear()
|
||||||
|
except (ReferenceError, TypeError, AttributeError):
|
||||||
|
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,
|
self._retry = Retry(deadline=config['retry_timeout'], max_delay=1, max_tries=-1,
|
||||||
retry_exceptions=(etcd.EtcdLeaderElectionInProgress,
|
retry_exceptions=retry_errors_cls)
|
||||||
etcd.EtcdWatcherCleared,
|
self._ttl = int(config.get('ttl') or 30)
|
||||||
etcd.EtcdEventIndexCleared))
|
self._client = self.get_etcd_client(config, client_cls)
|
||||||
self._client = self.get_etcd_client(config)
|
|
||||||
self.__do_not_watch = False
|
self.__do_not_watch = False
|
||||||
self._has_failed = 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):
|
def retry(self, *args, **kwargs):
|
||||||
return self._retry.copy()(*args, **kwargs)
|
retry = self._retry.copy()
|
||||||
|
kwargs['retry'] = retry
|
||||||
|
return retry(*args, **kwargs)
|
||||||
|
|
||||||
def _handle_exception(self, e, name='', do_sleep=False, raise_ex=None):
|
def _handle_exception(self, e, name='', do_sleep=False, raise_ex=None):
|
||||||
if not self._has_failed:
|
if not self._has_failed:
|
||||||
@@ -333,22 +460,13 @@ class Etcd(AbstractDCS):
|
|||||||
if isinstance(raise_ex, Exception):
|
if isinstance(raise_ex, Exception):
|
||||||
raise raise_ex
|
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
|
@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:
|
if 'proxy' in config:
|
||||||
config['use_proxies'] = True
|
config['use_proxies'] = True
|
||||||
config['url'] = config['proxy']
|
config['url'] = config['proxy']
|
||||||
@@ -368,7 +486,7 @@ class Etcd(AbstractDCS):
|
|||||||
config['hosts'] = []
|
config['hosts'] = []
|
||||||
for value in hosts:
|
for value in hosts:
|
||||||
if isinstance(value, six.string_types):
|
if isinstance(value, six.string_types):
|
||||||
config['hosts'].append(uri(protocol, split_host_port(value, default_port)))
|
config['hosts'].append(uri(protocol, split_host_port(value.strip(), default_port)))
|
||||||
elif 'host' in config:
|
elif 'host' in config:
|
||||||
host, port = split_host_port(config['host'], 2379)
|
host, port = split_host_port(config['host'], 2379)
|
||||||
config['host'] = host
|
config['host'] = host
|
||||||
@@ -397,9 +515,7 @@ class Etcd(AbstractDCS):
|
|||||||
sock = None
|
sock = None
|
||||||
try:
|
try:
|
||||||
sock = socket.socket(af, socktype, proto)
|
sock = socket.socket(af, socktype, proto)
|
||||||
if socket_options:
|
self.set_socket_options(sock, socket_options)
|
||||||
for opt in socket_options:
|
|
||||||
sock.setsockopt(*opt)
|
|
||||||
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
|
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
|
||||||
sock.settimeout(timeout)
|
sock.settimeout(timeout)
|
||||||
if source_address:
|
if source_address:
|
||||||
@@ -423,7 +539,7 @@ class Etcd(AbstractDCS):
|
|||||||
client = None
|
client = None
|
||||||
while not client:
|
while not client:
|
||||||
try:
|
try:
|
||||||
client = Client(config, dns_resolver)
|
client = client_cls(config, dns_resolver)
|
||||||
if 'use_proxies' in config and not client.machines:
|
if 'use_proxies' in config and not client.machines:
|
||||||
raise etcd.EtcdException
|
raise etcd.EtcdException
|
||||||
except etcd.EtcdException:
|
except etcd.EtcdException:
|
||||||
@@ -433,9 +549,10 @@ class Etcd(AbstractDCS):
|
|||||||
|
|
||||||
def set_ttl(self, ttl):
|
def set_ttl(self, ttl):
|
||||||
ttl = int(ttl)
|
ttl = int(ttl)
|
||||||
self.__do_not_watch = self._ttl != ttl
|
ret = self._ttl != ttl
|
||||||
self._ttl = ttl
|
self._ttl = ttl
|
||||||
self._client.set_machines_cache_ttl(ttl*10)
|
self._client.set_machines_cache_ttl(ttl*10)
|
||||||
|
return ret
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ttl(self):
|
def ttl(self):
|
||||||
@@ -445,6 +562,31 @@ class Etcd(AbstractDCS):
|
|||||||
self._retry.deadline = retry_timeout
|
self._retry.deadline = retry_timeout
|
||||||
self._client.set_read_timeout(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
|
@staticmethod
|
||||||
def member(node):
|
def member(node):
|
||||||
return Member.from_node(node.modifiedIndex, os.path.basename(node.key), node.ttl, node.value)
|
return Member.from_node(node.modifiedIndex, os.path.basename(node.key), node.ttl, node.value)
|
||||||
@@ -467,9 +609,24 @@ class Etcd(AbstractDCS):
|
|||||||
history = nodes.get(self._HISTORY)
|
history = nodes.get(self._HISTORY)
|
||||||
history = history and TimelineHistory.from_node(history.modifiedIndex, history.value)
|
history = history and TimelineHistory.from_node(history.modifiedIndex, history.value)
|
||||||
|
|
||||||
# get last leader operation
|
# get last know leader lsn and slots
|
||||||
last_leader_operation = nodes.get(self._LEADER_OPTIME)
|
status = nodes.get(self._STATUS)
|
||||||
last_leader_operation = 0 if last_leader_operation is None else int(last_leader_operation.value)
|
if status:
|
||||||
|
try:
|
||||||
|
status = json.loads(status.value)
|
||||||
|
last_lsn = status.get(self._OPTIME)
|
||||||
|
slots = status.get('slots')
|
||||||
|
except Exception:
|
||||||
|
slots = last_lsn = None
|
||||||
|
else:
|
||||||
|
last_lsn = nodes.get(self._LEADER_OPTIME)
|
||||||
|
last_lsn = last_lsn and last_lsn.value
|
||||||
|
slots = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
last_lsn = int(last_lsn)
|
||||||
|
except Exception:
|
||||||
|
last_lsn = 0
|
||||||
|
|
||||||
# get list of members
|
# get list of members
|
||||||
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
||||||
@@ -491,9 +648,9 @@ class Etcd(AbstractDCS):
|
|||||||
sync = nodes.get(self._SYNC)
|
sync = nodes.get(self._SYNC)
|
||||||
sync = SyncState.from_node(sync and sync.modifiedIndex, sync and sync.value)
|
sync = SyncState.from_node(sync and sync.modifiedIndex, sync and sync.value)
|
||||||
|
|
||||||
cluster = Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
cluster = Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots)
|
||||||
except etcd.EtcdKeyNotFound:
|
except etcd.EtcdKeyNotFound:
|
||||||
cluster = Cluster(None, None, None, None, [], None, None, None)
|
cluster = Cluster(None, None, None, None, [], None, None, None, None)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._handle_exception(e, 'get_cluster', raise_ex=EtcdError('Etcd is not responding properly'))
|
self._handle_exception(e, 'get_cluster', raise_ex=EtcdError('Etcd is not responding properly'))
|
||||||
self._has_failed = False
|
self._has_failed = False
|
||||||
@@ -506,7 +663,7 @@ class Etcd(AbstractDCS):
|
|||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def take_leader(self):
|
def take_leader(self):
|
||||||
return self.retry(self._client.set, self.leader_path, self._name, self._ttl)
|
return self.retry(self._client.write, self.leader_path, self._name, ttl=self._ttl)
|
||||||
|
|
||||||
def attempt_to_acquire_leader(self, permanent=False):
|
def attempt_to_acquire_leader(self, permanent=False):
|
||||||
try:
|
try:
|
||||||
@@ -530,19 +687,23 @@ class Etcd(AbstractDCS):
|
|||||||
return self._client.write(self.config_path, value, prevIndex=index or 0)
|
return self._client.write(self.config_path, value, prevIndex=index or 0)
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def _write_leader_optime(self, last_operation):
|
def _write_leader_optime(self, last_lsn):
|
||||||
return self._client.set(self.leader_optime_path, last_operation)
|
return self._client.set(self.leader_optime_path, last_lsn)
|
||||||
|
|
||||||
|
@catch_etcd_errors
|
||||||
|
def _write_status(self, value):
|
||||||
|
return self._client.set(self.status_path, value)
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
return self.retry(self._client.test_and_set, self.leader_path, self._name, self._name, self._ttl)
|
return self.retry(self._client.write, self.leader_path, self._name, prevValue=self._name, ttl=self._ttl)
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def initialize(self, create_new=True, sysid=""):
|
def initialize(self, create_new=True, sysid=""):
|
||||||
return self.retry(self._client.write, self.initialize_path, sysid, prevExist=(not create_new))
|
return self.retry(self._client.write, self.initialize_path, sysid, prevExist=(not create_new))
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def delete_leader(self):
|
def _delete_leader(self):
|
||||||
return self._client.delete(self.leader_path, prevValue=self._name)
|
return self._client.delete(self.leader_path, prevValue=self._name)
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
@@ -575,13 +736,14 @@ class Etcd(AbstractDCS):
|
|||||||
|
|
||||||
while timeout >= 1: # when timeout is too small urllib3 doesn't have enough time to connect
|
while timeout >= 1: # when timeout is too small urllib3 doesn't have enough time to connect
|
||||||
try:
|
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
|
self._has_failed = False
|
||||||
|
if result.action == 'compareAndSwap':
|
||||||
|
time.sleep(0.01)
|
||||||
# Synchronous work of all cluster members with etcd is less expensive
|
# Synchronous work of all cluster members with etcd is less expensive
|
||||||
# than reestablishing http connection every time from every replica.
|
# than reestablishing http connection every time from every replica.
|
||||||
return True
|
return True
|
||||||
except etcd.EtcdWatchTimedOut:
|
except etcd.EtcdWatchTimedOut:
|
||||||
self._client.http.clear()
|
|
||||||
self._has_failed = False
|
self._has_failed = False
|
||||||
return False
|
return False
|
||||||
except (etcd.EtcdEventIndexCleared, etcd.EtcdWatcherCleared): # Watch failed
|
except (etcd.EtcdEventIndexCleared, etcd.EtcdWatcherCleared): # Watch failed
|
||||||
@@ -596,3 +758,6 @@ class Etcd(AbstractDCS):
|
|||||||
return super(Etcd, self).watch(None, timeout)
|
return super(Etcd, self).watch(None, timeout)
|
||||||
finally:
|
finally:
|
||||||
self.event.clear()
|
self.event.clear()
|
||||||
|
|
||||||
|
|
||||||
|
etcd.EtcdError.error_exceptions[300] = EtcdRaftInternal
|
||||||
|
|||||||
@@ -0,0 +1,834 @@
|
|||||||
|
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 urllib3.exceptions import ReadTimeoutError, ProtocolError
|
||||||
|
|
||||||
|
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 AuthFailed(InvalidArgument):
|
||||||
|
error = "etcdserver: authentication failed, invalid user ID or password"
|
||||||
|
|
||||||
|
|
||||||
|
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 AuthFailed 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._use_proxies and self._cluster_version is None:
|
||||||
|
kwargs = self._prepare_common_parameters(1)
|
||||||
|
self._ensure_version_prefix(self._base_uri, **kwargs)
|
||||||
|
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)
|
||||||
|
|
||||||
|
@_handle_auth_errors
|
||||||
|
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, read_timeout=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'], read=read_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, read_timeout=None):
|
||||||
|
return self.watchrange(key, prefix_range_end(key), start_revision, filters, read_timeout)
|
||||||
|
|
||||||
|
|
||||||
|
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._status_key = base64_encode(dcs.status_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 in (self._optime_key, self._status_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 (or status key) was updated
|
||||||
|
if value_changed and (key not in (self._optime_key, self._status_key) or
|
||||||
|
(self.get(self._leader_key) or {}).get('value') != 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
|
||||||
|
# We do most of requests with timeouts. The only exception /watch requests to Etcd v3.
|
||||||
|
# In order to interrupt the /watch request we do socket.shutdown() from the main thread,
|
||||||
|
# which doesn't work on Windows. Therefore we want to use the last resort, `read_timeout`.
|
||||||
|
# Setting it to TTL will help to partially mitigate the problem.
|
||||||
|
# Setting it to lower value is not nice because for idling clusters it will increase
|
||||||
|
# the numbers of interrupts and reconnects.
|
||||||
|
read_timeout = self._dcs.ttl if os.name == 'nt' else None
|
||||||
|
response = self._client.watchprefix(self._dcs.cluster_prefix, revision, read_timeout=read_timeout)
|
||||||
|
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:
|
||||||
|
# Following exceptions are expected on Windows because the /watch request is done with `read_timeout`
|
||||||
|
if not (os.name == 'nt' and isinstance(e, (ReadTimeoutError, ProtocolError))):
|
||||||
|
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 to 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 know leader lsn and slots
|
||||||
|
status = nodes.get(self._STATUS)
|
||||||
|
if status:
|
||||||
|
try:
|
||||||
|
status = json.loads(status['value'])
|
||||||
|
last_lsn = status.get(self._OPTIME)
|
||||||
|
slots = status.get('slots')
|
||||||
|
except Exception:
|
||||||
|
slots = last_lsn = None
|
||||||
|
else:
|
||||||
|
last_lsn = nodes.get(self._LEADER_OPTIME)
|
||||||
|
last_lsn = last_lsn and last_lsn['value']
|
||||||
|
slots = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
last_lsn = int(last_lsn)
|
||||||
|
except Exception:
|
||||||
|
last_lsn = 0
|
||||||
|
|
||||||
|
# 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_lsn, members, failover, sync, history, slots)
|
||||||
|
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_lsn):
|
||||||
|
return self._client.put(self.leader_optime_path, last_lsn)
|
||||||
|
|
||||||
|
@catch_etcd_errors
|
||||||
|
def _write_status(self, value):
|
||||||
|
return self._client.put(self.status_path, value)
|
||||||
|
|
||||||
|
@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()
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import requests
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from patroni.dcs.zookeeper import ZooKeeper
|
from patroni.dcs.zookeeper import ZooKeeper
|
||||||
|
from patroni.request import get as requests_get
|
||||||
from patroni.utils import uri
|
from patroni.utils import uri
|
||||||
from requests.exceptions import RequestException
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -48,10 +48,10 @@ class ExhibitorEnsembleProvider(object):
|
|||||||
random.shuffle(exhibitors)
|
random.shuffle(exhibitors)
|
||||||
for host in exhibitors:
|
for host in exhibitors:
|
||||||
try:
|
try:
|
||||||
response = requests.get(uri('http', (host, self._exhibitor_port), self._uri_path), timeout=self.TIMEOUT)
|
response = requests_get(uri('http', (host, self._exhibitor_port), self._uri_path), timeout=self.TIMEOUT)
|
||||||
return response.json()
|
return json.loads(response.data.decode('utf-8'))
|
||||||
except RequestException:
|
except Exception:
|
||||||
pass
|
logging.debug('Request to %s failed', host)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
+827
-128
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,422 @@
|
|||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
from pysyncobj import SyncObj, SyncObjConf, replicated, FAIL_REASON
|
||||||
|
from pysyncobj.dns_resolver import globalDnsResolver
|
||||||
|
from pysyncobj.node import TCPNode
|
||||||
|
from pysyncobj.transport import TCPTransport, CONNECTION_STATE
|
||||||
|
from pysyncobj.utility import TcpUtility
|
||||||
|
|
||||||
|
from . import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
||||||
|
from ..utils import validate_directory
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class _TCPTransport(TCPTransport):
|
||||||
|
|
||||||
|
def __init__(self, syncObj, selfNode, otherNodes):
|
||||||
|
super(_TCPTransport, self).__init__(syncObj, selfNode, otherNodes)
|
||||||
|
self.setOnUtilityMessageCallback('members', syncObj.getMembers)
|
||||||
|
|
||||||
|
def _connectIfNecessarySingle(self, node):
|
||||||
|
try:
|
||||||
|
return super(_TCPTransport, self)._connectIfNecessarySingle(node)
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug('Connection to %s failed: %r', node, e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_host(self):
|
||||||
|
return globalDnsResolver().resolve(self.host)
|
||||||
|
|
||||||
|
|
||||||
|
setattr(TCPNode, 'ip', property(resolve_host))
|
||||||
|
|
||||||
|
|
||||||
|
class SyncObjUtility(object):
|
||||||
|
|
||||||
|
def __init__(self, otherNodes, conf, retry_timeout=10):
|
||||||
|
self._nodes = otherNodes
|
||||||
|
self._utility = TcpUtility(conf.password, retry_timeout/max(1, len(otherNodes)))
|
||||||
|
|
||||||
|
def executeCommand(self, command):
|
||||||
|
try:
|
||||||
|
return self._utility.executeCommand(self.__node, command)
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def getMembers(self):
|
||||||
|
for self.__node in self._nodes:
|
||||||
|
response = self.executeCommand(['members'])
|
||||||
|
if response:
|
||||||
|
return [member['addr'] for member in response]
|
||||||
|
|
||||||
|
|
||||||
|
class DynMemberSyncObj(SyncObj):
|
||||||
|
|
||||||
|
def __init__(self, selfAddress, partnerAddrs, conf, retry_timeout=10):
|
||||||
|
self.__early_apply_local_log = selfAddress is not None
|
||||||
|
self.applied_local_log = False
|
||||||
|
|
||||||
|
utility = SyncObjUtility(partnerAddrs, conf, retry_timeout)
|
||||||
|
members = utility.getMembers()
|
||||||
|
add_self = members and selfAddress not in members
|
||||||
|
|
||||||
|
partnerAddrs = [member for member in (members or partnerAddrs) if member != selfAddress]
|
||||||
|
|
||||||
|
super(DynMemberSyncObj, self).__init__(selfAddress, partnerAddrs, conf, transportClass=_TCPTransport)
|
||||||
|
|
||||||
|
if add_self:
|
||||||
|
thread = threading.Thread(target=utility.executeCommand, args=(['add', selfAddress],))
|
||||||
|
thread.daemon = True
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
def getMembers(self, args, callback):
|
||||||
|
callback([{'addr': node.id, 'leader': node == self._getLeader(), 'status': CONNECTION_STATE.CONNECTED
|
||||||
|
if self.isNodeConnected(node) else CONNECTION_STATE.DISCONNECTED} for node in self.otherNodes] +
|
||||||
|
[{'addr': self.selfNode.id, 'leader': self._isLeader(), 'status': CONNECTION_STATE.CONNECTED}], None)
|
||||||
|
|
||||||
|
def _onTick(self, timeToWait=0.0):
|
||||||
|
super(DynMemberSyncObj, 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.raftLastApplied == self.raftCommitIndex:
|
||||||
|
self.applied_local_log = True
|
||||||
|
|
||||||
|
|
||||||
|
class KVStoreTTL(DynMemberSyncObj):
|
||||||
|
|
||||||
|
def __init__(self, on_ready, on_set, on_delete, **config):
|
||||||
|
self.__thread = None
|
||||||
|
self.__on_set = on_set
|
||||||
|
self.__on_delete = on_delete
|
||||||
|
self.__limb = {}
|
||||||
|
self.set_retry_timeout(int(config.get('retry_timeout') or 10))
|
||||||
|
|
||||||
|
self_addr = config.get('self_addr')
|
||||||
|
partner_addrs = set(config.get('partner_addrs', []))
|
||||||
|
if config.get('patronictl'):
|
||||||
|
if self_addr:
|
||||||
|
partner_addrs.add(self_addr)
|
||||||
|
self_addr = None
|
||||||
|
|
||||||
|
# Create raft data_dir if necessary
|
||||||
|
raft_data_dir = config.get('data_dir', '')
|
||||||
|
if raft_data_dir != '':
|
||||||
|
validate_directory(raft_data_dir)
|
||||||
|
|
||||||
|
file_template = (self_addr or '')
|
||||||
|
file_template = file_template.replace(':', '_') if os.name == 'nt' else file_template
|
||||||
|
file_template = os.path.join(raft_data_dir, file_template)
|
||||||
|
conf = SyncObjConf(password=config.get('password'), autoTick=False, appendEntriesUseBatch=False,
|
||||||
|
bindAddress=config.get('bind_addr'), dnsFailCacheTime=(config.get('loop_wait') or 10),
|
||||||
|
dnsCacheTime=(config.get('ttl') or 30), commandsWaitLeader=config.get('commandsWaitLeader'),
|
||||||
|
fullDumpFile=(file_template + '.dump' if self_addr else None),
|
||||||
|
journalFile=(file_template + '.journal' if self_addr else None),
|
||||||
|
onReady=on_ready, dynamicMembershipChange=True)
|
||||||
|
|
||||||
|
super(KVStoreTTL, self).__init__(self_addr, partner_addrs, conf, self.__retry_timeout)
|
||||||
|
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.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):
|
||||||
|
super(KVStoreTTL, self)._onTick(timeToWait)
|
||||||
|
|
||||||
|
if self._isLeader():
|
||||||
|
self.__expire_keys()
|
||||||
|
else:
|
||||||
|
self.__limb.clear()
|
||||||
|
|
||||||
|
def _autoTickThread(self):
|
||||||
|
self.__destroying = False
|
||||||
|
while not self.__destroying:
|
||||||
|
self.doTick(self.conf.autoTickPeriod)
|
||||||
|
|
||||||
|
def startAutoTick(self):
|
||||||
|
self.__thread = threading.Thread(target=self._autoTickThread)
|
||||||
|
self.__thread.daemon = True
|
||||||
|
self.__thread.start()
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
if self.__thread:
|
||||||
|
self.__destroying = True
|
||||||
|
self.__thread.join()
|
||||||
|
super(KVStoreTTL, self).destroy()
|
||||||
|
|
||||||
|
|
||||||
|
class Raft(AbstractDCS):
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
super(Raft, self).__init__(config)
|
||||||
|
self._ttl = int(config.get('ttl') or 30)
|
||||||
|
|
||||||
|
ready_event = threading.Event()
|
||||||
|
self._sync_obj = KVStoreTTL(ready_event.set, self._on_set, self._on_delete, commandsWaitLeader=False, **config)
|
||||||
|
self._sync_obj.startAutoTick()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
ready_event.wait(5)
|
||||||
|
if ready_event.is_set() or self._sync_obj.applied_local_log:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
logger.info('waiting on raft')
|
||||||
|
|
||||||
|
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 in (self.leader_optime_path, self.status_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)
|
||||||
|
|
||||||
|
def reload_config(self, config):
|
||||||
|
super(Raft, self).reload_config(config)
|
||||||
|
globalDnsResolver().setTimeouts(self.ttl, self.loop_wait)
|
||||||
|
|
||||||
|
@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, 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 know leader lsn and slots
|
||||||
|
status = nodes.get(self._STATUS)
|
||||||
|
if status:
|
||||||
|
try:
|
||||||
|
status = json.loads(status['value'])
|
||||||
|
last_lsn = status.get(self._OPTIME)
|
||||||
|
slots = status.get('slots')
|
||||||
|
except Exception:
|
||||||
|
slots = last_lsn = None
|
||||||
|
else:
|
||||||
|
last_lsn = nodes.get(self._LEADER_OPTIME)
|
||||||
|
last_lsn = last_lsn and last_lsn['value']
|
||||||
|
slots = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
last_lsn = int(last_lsn)
|
||||||
|
except Exception:
|
||||||
|
last_lsn = 0
|
||||||
|
|
||||||
|
# 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_lsn, members, failover, sync, history, slots)
|
||||||
|
|
||||||
|
def _write_leader_optime(self, last_lsn):
|
||||||
|
return self._sync_obj.set(self.leader_optime_path, last_lsn, timeout=1)
|
||||||
|
|
||||||
|
def _write_status(self, value):
|
||||||
|
return self._sync_obj.set(self.status_path, value, 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()
|
||||||
+132
-35
@@ -1,14 +1,18 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import select
|
import select
|
||||||
|
import six
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from kazoo.client import KazooClient, KazooState, KazooRetry
|
from kazoo.client import KazooClient, KazooState, KazooRetry
|
||||||
from kazoo.exceptions import NoNodeError, NodeExistsError
|
from kazoo.exceptions import NoNodeError, NodeExistsError, SessionExpiredError
|
||||||
from kazoo.handlers.threading import SequentialThreadingHandler
|
from kazoo.handlers.threading import SequentialThreadingHandler
|
||||||
from patroni.dcs import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
from kazoo.protocol.states import KeeperState
|
||||||
from patroni.exceptions import DCSError
|
from kazoo.security import make_acl
|
||||||
from patroni.utils import deep_compare
|
|
||||||
|
from . import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
||||||
|
from ..exceptions import DCSError
|
||||||
|
from ..utils import deep_compare
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -47,11 +51,35 @@ class PatroniSequentialThreadingHandler(SequentialThreadingHandler):
|
|||||||
return super(PatroniSequentialThreadingHandler, self).create_connection(*args, **kwargs)
|
return super(PatroniSequentialThreadingHandler, self).create_connection(*args, **kwargs)
|
||||||
|
|
||||||
def select(self, *args, **kwargs):
|
def select(self, *args, **kwargs):
|
||||||
"""Python3 raises `ValueError` if socket is closed, because fd == -1"""
|
"""
|
||||||
|
Python 3.XY may raise following exceptions if select/poll are called with an invalid socket:
|
||||||
|
- `ValueError`: because fd == -1
|
||||||
|
- `TypeError`: Invalid file descriptor: -1 (starting from kazoo 2.9)
|
||||||
|
Python 2.7 may raise the `IOError` instead of `socket.error` (starting from kazoo 2.9)
|
||||||
|
|
||||||
|
When it is appropriate we map these exceptions to `socket.error`.
|
||||||
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return super(PatroniSequentialThreadingHandler, self).select(*args, **kwargs)
|
return super(PatroniSequentialThreadingHandler, self).select(*args, **kwargs)
|
||||||
except ValueError as e:
|
except IOError as e:
|
||||||
raise select.error(9, str(e))
|
raise (select.error(e.errno, e.strerror) if six.PY2 else e)
|
||||||
|
except (TypeError, ValueError) as e:
|
||||||
|
raise (e if six.PY2 and isinstance(e, TypeError) else select.error(9, str(e)))
|
||||||
|
|
||||||
|
|
||||||
|
class PatroniKazooClient(KazooClient):
|
||||||
|
|
||||||
|
def _call(self, request, async_object):
|
||||||
|
# Before kazoo==2.7.0 it wasn't possible to send requests to zookeeper if
|
||||||
|
# the connection is in the SUSPENDED state and Patroni was strongly relying on it.
|
||||||
|
# The https://github.com/python-zk/kazoo/pull/588 changed it, and now such requests are queued.
|
||||||
|
# We override the `_call()` method in order to keep the old behavior.
|
||||||
|
|
||||||
|
if self._state == KeeperState.CONNECTING:
|
||||||
|
async_object.set_exception(SessionExpiredError())
|
||||||
|
return False
|
||||||
|
return super(PatroniKazooClient, self)._call(request, async_object)
|
||||||
|
|
||||||
|
|
||||||
class ZooKeeper(AbstractDCS):
|
class ZooKeeper(AbstractDCS):
|
||||||
@@ -63,20 +91,39 @@ class ZooKeeper(AbstractDCS):
|
|||||||
if isinstance(hosts, list):
|
if isinstance(hosts, list):
|
||||||
hosts = ','.join(hosts)
|
hosts = ','.join(hosts)
|
||||||
|
|
||||||
self._client = KazooClient(hosts, handler=PatroniSequentialThreadingHandler(config['retry_timeout']),
|
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}
|
||||||
|
|
||||||
|
if 'set_acls' in config:
|
||||||
|
kwargs['default_acl'] = []
|
||||||
|
for principal, permissions in config['set_acls'].items():
|
||||||
|
normalizedPermissions = [p.upper() for p in permissions]
|
||||||
|
kwargs['default_acl'].append(make_acl(scheme='x509',
|
||||||
|
credential=principal,
|
||||||
|
read='READ' in normalizedPermissions,
|
||||||
|
write='WRITE' in normalizedPermissions,
|
||||||
|
create='CREATE' in normalizedPermissions,
|
||||||
|
delete='DELETE' in normalizedPermissions,
|
||||||
|
admin='ADMIN' in normalizedPermissions,
|
||||||
|
all='ALL' in normalizedPermissions))
|
||||||
|
|
||||||
|
self._client = PatroniKazooClient(hosts, handler=PatroniSequentialThreadingHandler(config['retry_timeout']),
|
||||||
timeout=config['ttl'], connection_retry=KazooRetry(max_delay=1, max_tries=-1,
|
timeout=config['ttl'], connection_retry=KazooRetry(max_delay=1, max_tries=-1,
|
||||||
sleep_func=time.sleep), command_retry=KazooRetry(deadline=config['retry_timeout'],
|
sleep_func=time.sleep), command_retry=KazooRetry(max_delay=1, max_tries=-1,
|
||||||
max_delay=1, max_tries=-1, sleep_func=time.sleep))
|
deadline=config['retry_timeout'], sleep_func=time.sleep), **kwargs)
|
||||||
self._client.add_listener(self.session_listener)
|
self._client.add_listener(self.session_listener)
|
||||||
|
|
||||||
self._fetch_cluster = True
|
self._fetch_cluster = True
|
||||||
|
self._fetch_status = True
|
||||||
|
self.__last_member_data = None
|
||||||
|
|
||||||
self._orig_kazoo_connect = self._client._connection._connect
|
self._orig_kazoo_connect = self._client._connection._connect
|
||||||
self._client._connection._connect = self._kazoo_connect
|
self._client._connection._connect = self._kazoo_connect
|
||||||
|
|
||||||
self._client.start()
|
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
|
"""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
|
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
|
connection dead and try to connect to another node. Without this "magic" it was taking
|
||||||
@@ -88,16 +135,20 @@ class ZooKeeper(AbstractDCS):
|
|||||||
than loop_wait, because we can spend up to 2 seconds when calling `touch_member()` and
|
than loop_wait, because we can spend up to 2 seconds when calling `touch_member()` and
|
||||||
`write_leader_optime()` methods, which also may hang..."""
|
`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]
|
return max(self.loop_wait - 2, 2)*1000, ret[1]
|
||||||
|
|
||||||
def session_listener(self, state):
|
def session_listener(self, state):
|
||||||
if state in [KazooState.SUSPENDED, KazooState.LOST]:
|
if state in [KazooState.SUSPENDED, KazooState.LOST]:
|
||||||
self.cluster_watcher(None)
|
self.cluster_watcher(None)
|
||||||
|
|
||||||
|
def status_watcher(self, event):
|
||||||
|
self._fetch_status = True
|
||||||
|
self.event.set()
|
||||||
|
|
||||||
def cluster_watcher(self, event):
|
def cluster_watcher(self, event):
|
||||||
self._fetch_cluster = True
|
self._fetch_cluster = True
|
||||||
self.event.set()
|
self.status_watcher(event)
|
||||||
|
|
||||||
def reload_config(self, config):
|
def reload_config(self, config):
|
||||||
self.set_retry_timeout(config['retry_timeout'])
|
self.set_retry_timeout(config['retry_timeout'])
|
||||||
@@ -114,13 +165,14 @@ class ZooKeeper(AbstractDCS):
|
|||||||
# the same time, set_ttl method will reestablish connection and return
|
# the same time, set_ttl method will reestablish connection and return
|
||||||
# `!True`, otherwise we will close existing connection and let kazoo
|
# `!True`, otherwise we will close existing connection and let kazoo
|
||||||
# open the new one.
|
# open the new one.
|
||||||
if not self.set_ttl(int(config['ttl'] * 1000)) and loop_wait_changed:
|
if not self.set_ttl(config['ttl']) and loop_wait_changed:
|
||||||
self._client._connection._socket.close()
|
self._client._connection._socket.close()
|
||||||
|
|
||||||
def set_ttl(self, ttl):
|
def set_ttl(self, ttl):
|
||||||
"""It is not possible to change ttl (session_timeout) in zookeeper without
|
"""It is not possible to change ttl (session_timeout) in zookeeper without
|
||||||
destroying old session and creating the new one. This method returns `!True`
|
destroying old session and creating the new one. This method returns `!True`
|
||||||
if session_timeout has been changed (`restart()` has been called)."""
|
if session_timeout has been changed (`restart()` has been called)."""
|
||||||
|
ttl = int(ttl * 1000)
|
||||||
if self._client._session_timeout != ttl:
|
if self._client._session_timeout != ttl:
|
||||||
self._client._session_timeout = ttl
|
self._client._session_timeout = ttl
|
||||||
self._client.restart()
|
self._client.restart()
|
||||||
@@ -128,7 +180,7 @@ class ZooKeeper(AbstractDCS):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def ttl(self):
|
def ttl(self):
|
||||||
return self._client._session_timeout
|
return self._client._session_timeout / 1000.0
|
||||||
|
|
||||||
def set_retry_timeout(self, retry_timeout):
|
def set_retry_timeout(self, retry_timeout):
|
||||||
retry = self._client.retry if isinstance(self._client.retry, KazooRetry) else self._client._retry
|
retry = self._client.retry if isinstance(self._client.retry, KazooRetry) else self._client._retry
|
||||||
@@ -141,6 +193,30 @@ class ZooKeeper(AbstractDCS):
|
|||||||
except NoNodeError:
|
except NoNodeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_status(self, leader):
|
||||||
|
watch = self.status_watcher if not leader or leader.name != self._name else None
|
||||||
|
|
||||||
|
status = self.get_node(self.status_path, watch)
|
||||||
|
if status:
|
||||||
|
try:
|
||||||
|
status = json.loads(status[0])
|
||||||
|
last_lsn = status.get(self._OPTIME)
|
||||||
|
slots = status.get('slots')
|
||||||
|
except Exception:
|
||||||
|
slots = last_lsn = None
|
||||||
|
else:
|
||||||
|
last_lsn = self.get_node(self.leader_optime_path, watch)
|
||||||
|
last_lsn = last_lsn and last_lsn[0]
|
||||||
|
slots = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
last_lsn = int(last_lsn)
|
||||||
|
except Exception:
|
||||||
|
last_lsn = 0
|
||||||
|
|
||||||
|
self._fetch_status = False
|
||||||
|
return last_lsn, slots
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def member(name, value, znode):
|
def member(name, value, znode):
|
||||||
return Member.from_node(znode.version, name, znode.ephemeralOwner, value)
|
return Member.from_node(znode.version, name, znode.ephemeralOwner, value)
|
||||||
@@ -151,11 +227,10 @@ class ZooKeeper(AbstractDCS):
|
|||||||
except NoNodeError:
|
except NoNodeError:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def load_members(self, sync_standby):
|
def load_members(self):
|
||||||
members = []
|
members = []
|
||||||
for member in self.get_children(self.members_path, self.cluster_watcher):
|
for member in self.get_children(self.members_path, self.cluster_watcher):
|
||||||
watch = member == sync_standby and self.cluster_watcher or None
|
data = self.get_node(self.members_path + member)
|
||||||
data = self.get_node(self.members_path + member, watch)
|
|
||||||
if data is not None:
|
if data is not None:
|
||||||
members.append(self.member(member, *data))
|
members.append(self.member(member, *data))
|
||||||
return members
|
return members
|
||||||
@@ -178,17 +253,12 @@ class ZooKeeper(AbstractDCS):
|
|||||||
history = self.get_node(self.history_path, watch=self.cluster_watcher) if self._HISTORY in nodes else None
|
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])
|
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
|
# get synchronization state
|
||||||
sync = self.get_node(self.sync_path, watch=self.cluster_watcher) if self._SYNC in nodes else None
|
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])
|
sync = SyncState.from_node(sync and sync[1].version, sync and sync[0])
|
||||||
|
|
||||||
# get list of members
|
# get list of members
|
||||||
sync_standby = sync.leader == self._name and sync.sync_standby or None
|
members = self.load_members() if self._MEMBERS[:-1] in nodes else []
|
||||||
members = self.load_members(sync_standby) if self._MEMBERS[:-1] in nodes else []
|
|
||||||
|
|
||||||
# get leader
|
# get leader
|
||||||
leader = self.get_node(self.leader_path) if self._LEADER in nodes else None
|
leader = self.get_node(self.leader_path) if self._LEADER in nodes else None
|
||||||
@@ -206,11 +276,14 @@ class ZooKeeper(AbstractDCS):
|
|||||||
leader = Leader(leader[1].version, leader[1].ephemeralOwner, member)
|
leader = Leader(leader[1].version, leader[1].ephemeralOwner, member)
|
||||||
self._fetch_cluster = member.index == -1
|
self._fetch_cluster = member.index == -1
|
||||||
|
|
||||||
|
# get last known leader lsn and slots
|
||||||
|
last_lsn, slots = self.get_status(leader)
|
||||||
|
|
||||||
# failover key
|
# failover key
|
||||||
failover = self.get_node(self.failover_path, watch=self.cluster_watcher) if self._FAILOVER in nodes else None
|
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])
|
failover = failover and Failover.from_node(failover[1].version, failover[0])
|
||||||
|
|
||||||
return Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
return Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots)
|
||||||
|
|
||||||
def _load_cluster(self):
|
def _load_cluster(self):
|
||||||
cluster = self.cluster
|
cluster = self.cluster
|
||||||
@@ -221,8 +294,25 @@ class ZooKeeper(AbstractDCS):
|
|||||||
logger.exception('get_cluster')
|
logger.exception('get_cluster')
|
||||||
self.cluster_watcher(None)
|
self.cluster_watcher(None)
|
||||||
raise ZooKeeperError('ZooKeeper in not responding properly')
|
raise ZooKeeperError('ZooKeeper in not responding properly')
|
||||||
|
# The /status ZNode was updated or doesn't exist
|
||||||
|
elif self._fetch_status and not self._fetch_cluster or not cluster.last_lsn \
|
||||||
|
or cluster.has_permanent_logical_slots(self._name, False) and not cluster.slots:
|
||||||
|
# If current node is the leader just clear the event without fetching anything (we are updating the /status)
|
||||||
|
if cluster.leader and cluster.leader.name == self._name:
|
||||||
|
self.event.clear()
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
last_lsn, slots = self.get_status(cluster.leader)
|
||||||
|
self.event.clear()
|
||||||
|
cluster = Cluster(cluster.initialize, cluster.config, cluster.leader, last_lsn,
|
||||||
|
cluster.members, cluster.failover, cluster.sync, cluster.history, slots)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return cluster
|
return cluster
|
||||||
|
|
||||||
|
def _bypass_caches(self):
|
||||||
|
self._fetch_cluster = True
|
||||||
|
|
||||||
def _create(self, path, value, retry=False, ephemeral=False):
|
def _create(self, path, value, retry=False, ephemeral=False):
|
||||||
try:
|
try:
|
||||||
if retry:
|
if retry:
|
||||||
@@ -273,11 +363,11 @@ class ZooKeeper(AbstractDCS):
|
|||||||
def touch_member(self, data, permanent=False):
|
def touch_member(self, data, permanent=False):
|
||||||
cluster = self.cluster
|
cluster = self.cluster
|
||||||
member = cluster and cluster.get_member(self._name, fallback_to_leader=False)
|
member = cluster and cluster.get_member(self._name, fallback_to_leader=False)
|
||||||
encoded_data = json.dumps(data, separators=(',', ':')).encode('utf-8')
|
member_data = self.__last_member_data or member and member.data
|
||||||
if member and (self._client.client_id is not None and member.session != self._client.client_id[0] or
|
if member and (self._client.client_id is not None and member.session != self._client.client_id[0] or
|
||||||
not (deep_compare(member.data.get('tags', {}), data.get('tags', {})) and
|
not (deep_compare(member_data.get('tags', {}), data.get('tags', {})) and
|
||||||
member.data.get('version') == data.get('version') and
|
member_data.get('version') == data.get('version') and
|
||||||
member.data.get('checkpoint_after_promote') == data.get('checkpoint_after_promote'))):
|
member_data.get('checkpoint_after_promote') == data.get('checkpoint_after_promote'))):
|
||||||
try:
|
try:
|
||||||
self._client.delete_async(self.member_path).get(timeout=1)
|
self._client.delete_async(self.member_path).get(timeout=1)
|
||||||
except NoNodeError:
|
except NoNodeError:
|
||||||
@@ -286,13 +376,15 @@ class ZooKeeper(AbstractDCS):
|
|||||||
return False
|
return False
|
||||||
member = None
|
member = None
|
||||||
|
|
||||||
|
encoded_data = json.dumps(data, separators=(',', ':')).encode('utf-8')
|
||||||
if member:
|
if member:
|
||||||
if deep_compare(data, member.data):
|
if deep_compare(data, member_data):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
self._client.create_async(self.member_path, encoded_data, makepath=True,
|
self._client.create_async(self.member_path, encoded_data, makepath=True,
|
||||||
ephemeral=not permanent).get(timeout=1)
|
ephemeral=not permanent).get(timeout=1)
|
||||||
|
self.__last_member_data = data
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not isinstance(e, NodeExistsError):
|
if not isinstance(e, NodeExistsError):
|
||||||
@@ -300,6 +392,7 @@ class ZooKeeper(AbstractDCS):
|
|||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
self._client.set_async(self.member_path, encoded_data).get(timeout=1)
|
self._client.set_async(self.member_path, encoded_data).get(timeout=1)
|
||||||
|
self.__last_member_data = data
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('touch_member')
|
logger.exception('touch_member')
|
||||||
@@ -309,13 +402,16 @@ class ZooKeeper(AbstractDCS):
|
|||||||
def take_leader(self):
|
def take_leader(self):
|
||||||
return self.attempt_to_acquire_leader()
|
return self.attempt_to_acquire_leader()
|
||||||
|
|
||||||
def _write_leader_optime(self, last_operation):
|
def _write_leader_optime(self, last_lsn):
|
||||||
return self._set_or_create(self.leader_optime_path, last_operation)
|
return self._set_or_create(self.leader_optime_path, last_lsn)
|
||||||
|
|
||||||
|
def _write_status(self, value):
|
||||||
|
return self._set_or_create(self.status_path, value)
|
||||||
|
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def delete_leader(self):
|
def _delete_leader(self):
|
||||||
self._client.restart()
|
self._client.restart()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -346,6 +442,7 @@ class ZooKeeper(AbstractDCS):
|
|||||||
return self.set_sync_state_value("{}", index)
|
return self.set_sync_state_value("{}", index)
|
||||||
|
|
||||||
def watch(self, leader_index, timeout):
|
def watch(self, leader_index, timeout):
|
||||||
if super(ZooKeeper, self).watch(leader_index, timeout):
|
ret = super(ZooKeeper, self).watch(leader_index, timeout + 0.5)
|
||||||
|
if ret and not self._fetch_status:
|
||||||
self._fetch_cluster = True
|
self._fetch_cluster = True
|
||||||
return self._fetch_cluster
|
return ret or self._fetch_cluster
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ class PatroniException(Exception):
|
|||||||
return repr(self.value)
|
return repr(self.value)
|
||||||
|
|
||||||
|
|
||||||
|
class PatroniFatalException(PatroniException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PostgresException(PatroniException):
|
class PostgresException(PatroniException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -27,3 +31,7 @@ class PostgresConnectionException(PostgresException):
|
|||||||
|
|
||||||
class WatchdogError(PatroniException):
|
class WatchdogError(PatroniException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigParseError(PatroniException):
|
||||||
|
pass
|
||||||
|
|||||||
+415
-198
File diff suppressed because it is too large
Load Diff
+64
-15
@@ -11,6 +11,20 @@ from threading import Lock, Thread
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def debug_exception(logger_obj, msg, *args, **kwargs):
|
||||||
|
kwargs.pop("exc_info", False)
|
||||||
|
if logger_obj.isEnabledFor(logging.DEBUG):
|
||||||
|
logger_obj.debug(msg, *args, exc_info=True, **kwargs)
|
||||||
|
else:
|
||||||
|
msg = "{0}, DETAIL: '{1}'".format(msg, sys.exc_info()[1])
|
||||||
|
logger_obj.error(msg, *args, exc_info=False, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def error_exception(logger_obj, msg, *args, **kwargs):
|
||||||
|
exc_info = kwargs.pop("exc_info", True)
|
||||||
|
logger_obj.error(msg, *args, exc_info=exc_info, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class QueueHandler(logging.Handler):
|
class QueueHandler(logging.Handler):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -48,9 +62,20 @@ class QueueHandler(logging.Handler):
|
|||||||
return self._records_lost
|
return self._records_lost
|
||||||
|
|
||||||
|
|
||||||
|
class ProxyHandler(logging.Handler):
|
||||||
|
|
||||||
|
def __init__(self, patroni_logger):
|
||||||
|
logging.Handler.__init__(self)
|
||||||
|
self.patroni_logger = patroni_logger
|
||||||
|
|
||||||
|
def emit(self, record):
|
||||||
|
self.patroni_logger.log_handler.handle(record)
|
||||||
|
|
||||||
|
|
||||||
class PatroniLogger(Thread):
|
class PatroniLogger(Thread):
|
||||||
|
|
||||||
DEFAULT_LEVEL = 'INFO'
|
DEFAULT_LEVEL = 'INFO'
|
||||||
|
DEFAULT_TRACEBACK_LEVEL = 'ERROR'
|
||||||
DEFAULT_FORMAT = '%(asctime)s %(levelname)s: %(message)s'
|
DEFAULT_FORMAT = '%(asctime)s %(levelname)s: %(message)s'
|
||||||
|
|
||||||
NORMAL_LOG_QUEUE_SIZE = 2 # When everything goes normal Patroni writes only 2 messages per HA loop
|
NORMAL_LOG_QUEUE_SIZE = 2 # When everything goes normal Patroni writes only 2 messages per HA loop
|
||||||
@@ -59,16 +84,18 @@ class PatroniLogger(Thread):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(PatroniLogger, self).__init__()
|
super(PatroniLogger, self).__init__()
|
||||||
self.daemon = True
|
|
||||||
self._queue_handler = QueueHandler()
|
self._queue_handler = QueueHandler()
|
||||||
self._root_logger = logging.getLogger()
|
self._root_logger = logging.getLogger()
|
||||||
self._root_logger.addHandler(self._queue_handler)
|
|
||||||
self._config = None
|
self._config = None
|
||||||
self._log_handler = None
|
self.log_handler = None
|
||||||
|
self.log_handler_lock = Lock()
|
||||||
self._old_handlers = []
|
self._old_handlers = []
|
||||||
self._log_handler_lock = Lock()
|
|
||||||
self.reload_config({'level': 'DEBUG'})
|
self.reload_config({'level': 'DEBUG'})
|
||||||
self.start()
|
# We will switch to the QueueHandler only when thread was started.
|
||||||
|
# This is necessary to protect from the cases when Patroni constructor
|
||||||
|
# failed and PatroniLogger thread remain running and prevent shutdown.
|
||||||
|
self._proxy_handler = ProxyHandler(self)
|
||||||
|
self._root_logger.addHandler(self._proxy_handler)
|
||||||
|
|
||||||
def update_loggers(self):
|
def update_loggers(self):
|
||||||
loggers = deepcopy(self._config.get('loggers') or {})
|
loggers = deepcopy(self._config.get('loggers') or {})
|
||||||
@@ -87,18 +114,22 @@ class PatroniLogger(Thread):
|
|||||||
self._queue_handler.queue.maxsize = config.get('max_queue_size', self.DEFAULT_MAX_QUEUE_SIZE)
|
self._queue_handler.queue.maxsize = config.get('max_queue_size', self.DEFAULT_MAX_QUEUE_SIZE)
|
||||||
|
|
||||||
self._root_logger.setLevel(config.get('level', PatroniLogger.DEFAULT_LEVEL))
|
self._root_logger.setLevel(config.get('level', PatroniLogger.DEFAULT_LEVEL))
|
||||||
|
if config.get('traceback_level', PatroniLogger.DEFAULT_TRACEBACK_LEVEL).lower() == 'debug':
|
||||||
|
logging.Logger.exception = debug_exception
|
||||||
|
else:
|
||||||
|
logging.Logger.exception = error_exception
|
||||||
|
|
||||||
new_handler = None
|
new_handler = None
|
||||||
if 'dir' in config:
|
if 'dir' in config:
|
||||||
if not isinstance(self._log_handler, RotatingFileHandler):
|
if not isinstance(self.log_handler, RotatingFileHandler):
|
||||||
new_handler = RotatingFileHandler(os.path.join(config['dir'], __name__))
|
new_handler = RotatingFileHandler(os.path.join(config['dir'], __name__))
|
||||||
handler = new_handler or self._log_handler
|
handler = new_handler or self.log_handler
|
||||||
handler.maxBytes = int(config.get('file_size', 25000000))
|
handler.maxBytes = int(config.get('file_size', 25000000))
|
||||||
handler.backupCount = int(config.get('file_num', 4))
|
handler.backupCount = int(config.get('file_num', 4))
|
||||||
else:
|
else:
|
||||||
if self._log_handler is None or isinstance(self._log_handler, RotatingFileHandler):
|
if self.log_handler is None or isinstance(self.log_handler, RotatingFileHandler):
|
||||||
new_handler = logging.StreamHandler()
|
new_handler = logging.StreamHandler()
|
||||||
handler = new_handler or self._log_handler
|
handler = new_handler or self.log_handler
|
||||||
|
|
||||||
oldlogformat = (self._config or {}).get('format', PatroniLogger.DEFAULT_FORMAT)
|
oldlogformat = (self._config or {}).get('format', PatroniLogger.DEFAULT_FORMAT)
|
||||||
logformat = config.get('format', PatroniLogger.DEFAULT_FORMAT)
|
logformat = config.get('format', PatroniLogger.DEFAULT_FORMAT)
|
||||||
@@ -110,17 +141,17 @@ class PatroniLogger(Thread):
|
|||||||
handler.setFormatter(logging.Formatter(logformat, dateformat))
|
handler.setFormatter(logging.Formatter(logformat, dateformat))
|
||||||
|
|
||||||
if new_handler:
|
if new_handler:
|
||||||
with self._log_handler_lock:
|
with self.log_handler_lock:
|
||||||
if self._log_handler:
|
if self.log_handler:
|
||||||
self._old_handlers.append(self._log_handler)
|
self._old_handlers.append(self.log_handler)
|
||||||
self._log_handler = new_handler
|
self.log_handler = new_handler
|
||||||
|
|
||||||
self._config = config.copy()
|
self._config = config.copy()
|
||||||
self.update_loggers()
|
self.update_loggers()
|
||||||
|
|
||||||
def _close_old_handlers(self):
|
def _close_old_handlers(self):
|
||||||
while True:
|
while True:
|
||||||
with self._log_handler_lock:
|
with self.log_handler_lock:
|
||||||
if not self._old_handlers:
|
if not self._old_handlers:
|
||||||
break
|
break
|
||||||
handler = self._old_handlers.pop()
|
handler = self._old_handlers.pop()
|
||||||
@@ -130,6 +161,13 @@ class PatroniLogger(Thread):
|
|||||||
_LOGGER.exception('Failed to close the old log handler %s', handler)
|
_LOGGER.exception('Failed to close the old log handler %s', handler)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
# switch to QueueHandler only when the thread was started
|
||||||
|
with self.log_handler_lock:
|
||||||
|
self._root_logger.addHandler(self._queue_handler)
|
||||||
|
self._root_logger.removeHandler(self._proxy_handler)
|
||||||
|
|
||||||
|
prev_record = None
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
self._close_old_handlers()
|
self._close_old_handlers()
|
||||||
|
|
||||||
@@ -137,7 +175,18 @@ class PatroniLogger(Thread):
|
|||||||
if record is None:
|
if record is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
self._log_handler.handle(record)
|
if self._root_logger.level == logging.INFO:
|
||||||
|
if record.msg.startswith('Lock owner: '):
|
||||||
|
prev_record, record = record, None
|
||||||
|
else:
|
||||||
|
if prev_record and prev_record.thread == record.thread:
|
||||||
|
if not (record.msg.startswith('no action. ') or record.msg.startswith('PAUSE: no action')):
|
||||||
|
self.log_handler.handle(prev_record)
|
||||||
|
prev_record = None
|
||||||
|
|
||||||
|
if record:
|
||||||
|
self.log_handler.handle(record)
|
||||||
|
|
||||||
self._queue_handler.queue.task_done()
|
self._queue_handler.queue.task_done()
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
|
|||||||
+449
-210
@@ -1,26 +1,31 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import psycopg2
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
|
import six
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from patroni.postgresql.callback_executor import CallbackExecutor
|
from datetime import datetime
|
||||||
from patroni.postgresql.bootstrap import Bootstrap
|
from dateutil import tz
|
||||||
from patroni.postgresql.cancellable import CancellableSubprocess
|
from psutil import TimeoutExpired
|
||||||
from patroni.postgresql.config import ConfigHandler
|
|
||||||
from patroni.postgresql.connection import Connection, get_connection_cursor
|
|
||||||
from patroni.postgresql.misc import parse_history, 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.dcs import slot_name_from_member_name, RemoteMember
|
|
||||||
from threading import current_thread, Lock
|
from threading import current_thread, Lock
|
||||||
|
|
||||||
|
from .bootstrap import Bootstrap
|
||||||
|
from .callback_executor import CallbackExecutor
|
||||||
|
from .cancellable import CancellableSubprocess
|
||||||
|
from .config import ConfigHandler, mtime
|
||||||
|
from .connection import Connection, get_connection_cursor
|
||||||
|
from .misc import parse_history, parse_lsn, postgres_major_version_to_int
|
||||||
|
from .postmaster import PostmasterProcess
|
||||||
|
from .slots import SlotsHandler
|
||||||
|
from .. import psycopg
|
||||||
|
from ..exceptions import PostgresConnectionException
|
||||||
|
from ..utils import Retry, RetryFailedError, polling_loop, data_directory_is_empty, parse_int
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -38,15 +43,6 @@ STATE_UNKNOWN = 'unknown'
|
|||||||
|
|
||||||
STOP_POLLING_INTERVAL = 1
|
STOP_POLLING_INTERVAL = 1
|
||||||
|
|
||||||
cluster_info_query = ("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")
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def null_context():
|
def null_context():
|
||||||
@@ -55,12 +51,23 @@ def null_context():
|
|||||||
|
|
||||||
class Postgresql(object):
|
class Postgresql(object):
|
||||||
|
|
||||||
|
POSTMASTER_START_TIME = "pg_catalog.pg_postmaster_start_time()"
|
||||||
|
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):
|
def __init__(self, config):
|
||||||
self.name = config['name']
|
self.name = config['name']
|
||||||
self.scope = config['scope']
|
self.scope = config['scope']
|
||||||
self._data_dir = config['data_dir']
|
self._data_dir = config['data_dir']
|
||||||
self._database = config.get('database', 'postgres')
|
self._database = config.get('database', 'postgres')
|
||||||
self._version_file = os.path.join(self._data_dir, 'PG_VERSION')
|
self._version_file = os.path.join(self._data_dir, 'PG_VERSION')
|
||||||
|
self._pg_control = os.path.join(self._data_dir, 'global', 'pg_control')
|
||||||
self._major_version = self.get_major_version()
|
self._major_version = self.get_major_version()
|
||||||
|
|
||||||
self._state_lock = Lock()
|
self._state_lock = Lock()
|
||||||
@@ -69,6 +76,7 @@ class Postgresql(object):
|
|||||||
self._pending_restart = False
|
self._pending_restart = False
|
||||||
self._connection = Connection()
|
self._connection = Connection()
|
||||||
self.config = ConfigHandler(self, config)
|
self.config = ConfigHandler(self, config)
|
||||||
|
self.config.check_directories()
|
||||||
|
|
||||||
self._bin_dir = config.get('bin_dir') or ''
|
self._bin_dir = config.get('bin_dir') or ''
|
||||||
self.bootstrap = Bootstrap(self)
|
self.bootstrap = Bootstrap(self)
|
||||||
@@ -77,7 +85,6 @@ class Postgresql(object):
|
|||||||
|
|
||||||
self.slots_handler = SlotsHandler(self)
|
self.slots_handler = SlotsHandler(self)
|
||||||
|
|
||||||
self._pgpass = config.get('pgpass') or os.path.join(os.path.expanduser('~'), 'pgpass')
|
|
||||||
self._callback_executor = CallbackExecutor()
|
self._callback_executor = CallbackExecutor()
|
||||||
self.__cb_called = False
|
self.__cb_called = False
|
||||||
self.__cb_pending = None
|
self.__cb_pending = None
|
||||||
@@ -97,15 +104,19 @@ class Postgresql(object):
|
|||||||
self._state_entry_timestamp = None
|
self._state_entry_timestamp = None
|
||||||
|
|
||||||
self._cluster_info_state = {}
|
self._cluster_info_state = {}
|
||||||
|
self._has_permanent_logical_slots = True
|
||||||
|
self._enforce_hot_standby_feedback = False
|
||||||
self._cached_replica_timeline = None
|
self._cached_replica_timeline = None
|
||||||
|
|
||||||
# Last known running process
|
# Last known running process
|
||||||
self._postmaster_proc = None
|
self._postmaster_proc = None
|
||||||
|
|
||||||
if self.is_running():
|
if self.is_running(): # we are "joining" already running postgres
|
||||||
self.set_state('running')
|
self.set_state('running')
|
||||||
self.set_role('master' if self.is_leader() else 'replica')
|
self.set_role('master' if self.is_leader() else 'replica')
|
||||||
self.config.write_postgresql_conf() # we are "joining" already running postgres
|
# postpone writing postgresql.conf for 12+ because recovery parameters are not yet known
|
||||||
|
if self.major_version < 120000 or self.is_leader():
|
||||||
|
self.config.write_postgresql_conf()
|
||||||
hba_saved = self.config.replace_pg_hba()
|
hba_saved = self.config.replace_pg_hba()
|
||||||
ident_saved = self.config.replace_pg_ident()
|
ident_saved = self.config.replace_pg_ident()
|
||||||
if hba_saved or ident_saved:
|
if hba_saved or ident_saved:
|
||||||
@@ -133,6 +144,10 @@ class Postgresql(object):
|
|||||||
def callback(self):
|
def callback(self):
|
||||||
return self.config.get('callbacks') or {}
|
return self.config.get('callbacks') or {}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def wal_dir(self):
|
||||||
|
return os.path.join(self._data_dir, 'pg_' + self.wal_name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wal_name(self):
|
def wal_name(self):
|
||||||
return 'wal' if self._major_version >= 100000 else 'xlog'
|
return 'wal' if self._major_version >= 100000 else 'xlog'
|
||||||
@@ -141,6 +156,24 @@ class Postgresql(object):
|
|||||||
def lsn_name(self):
|
def lsn_name(self):
|
||||||
return 'lsn' if self._major_version >= 100000 else 'location'
|
return 'lsn' if self._major_version >= 100000 else 'location'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cluster_info_query(self):
|
||||||
|
if self._major_version >= 90600:
|
||||||
|
extra = "(SELECT pg_catalog.json_agg(s.*) FROM (SELECT slot_name, slot_type as type, datoid::bigint, " +\
|
||||||
|
"plugin, catalog_xmin, pg_catalog.pg_wal_lsn_diff(confirmed_flush_lsn, '0/0')::bigint" + \
|
||||||
|
" AS confirmed_flush_lsn FROM pg_catalog.pg_get_replication_slots()) AS s)"\
|
||||||
|
if self._has_permanent_logical_slots and self._major_version >= 110000 else "NULL"
|
||||||
|
extra = (", CASE WHEN latest_end_lsn IS NULL THEN NULL ELSE received_tli END,"
|
||||||
|
" slot_name, conninfo, {0} FROM pg_catalog.pg_stat_get_wal_receiver()").format(extra)
|
||||||
|
if self.role == 'standby_leader':
|
||||||
|
extra = "timeline_id" + extra + ", pg_catalog.pg_control_checkpoint()"
|
||||||
|
else:
|
||||||
|
extra = "0" + extra
|
||||||
|
else:
|
||||||
|
extra = "0, NULL, NULL, NULL, NULL"
|
||||||
|
|
||||||
|
return ("SELECT " + self.TL_LSN + ", {2}").format(self.wal_name, self.lsn_name, extra)
|
||||||
|
|
||||||
def _version_file_exists(self):
|
def _version_file_exists(self):
|
||||||
return not self.data_directory_empty() and os.path.isfile(self._version_file)
|
return not self.data_directory_empty() and os.path.isfile(self._version_file)
|
||||||
|
|
||||||
@@ -188,8 +221,8 @@ class Postgresql(object):
|
|||||||
3: STATE_UNKNOWN}
|
3: STATE_UNKNOWN}
|
||||||
return return_codes.get(ret, STATE_UNKNOWN)
|
return return_codes.get(ret, STATE_UNKNOWN)
|
||||||
|
|
||||||
def reload_config(self, config):
|
def reload_config(self, config, sighup=False):
|
||||||
self.config.reload_config(config)
|
self.config.reload_config(config, sighup)
|
||||||
self._is_leader_retry.deadline = self.retry.deadline = config['retry_timeout']/2.0
|
self._is_leader_retry.deadline = self.retry.deadline = config['retry_timeout']/2.0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -207,7 +240,7 @@ class Postgresql(object):
|
|||||||
return self._sysid
|
return self._sysid
|
||||||
|
|
||||||
def get_postgres_role_from_data_directory(self):
|
def get_postgres_role_from_data_directory(self):
|
||||||
if self.data_directory_empty():
|
if self.data_directory_empty() or not self.controldata():
|
||||||
return 'uninitialized'
|
return 'uninitialized'
|
||||||
elif self.config.recovery_conf_exists():
|
elif self.config.recovery_conf_exists():
|
||||||
return 'replica'
|
return 'replica'
|
||||||
@@ -231,15 +264,15 @@ class Postgresql(object):
|
|||||||
cursor = None
|
cursor = None
|
||||||
try:
|
try:
|
||||||
cursor = self._connection.cursor()
|
cursor = self._connection.cursor()
|
||||||
cursor.execute(sql, params)
|
cursor.execute(sql, params or None)
|
||||||
return cursor
|
return cursor
|
||||||
except psycopg2.Error as e:
|
except psycopg.Error as e:
|
||||||
if cursor and cursor.connection.closed == 0:
|
if cursor and cursor.connection.closed == 0:
|
||||||
# When connected via unix socket, psycopg2 can't recoginze 'connection lost'
|
# When connected via unix socket, psycopg2 can't recoginze 'connection lost'
|
||||||
# and leaves `_cursor_holder.connection.closed == 0`, but psycopg2.OperationalError
|
# and leaves `_cursor_holder.connection.closed == 0`, but psycopg2.OperationalError
|
||||||
# is still raised (what is correct). It doesn't make sense to continiue with existing
|
# is still raised (what is correct). It doesn't make sense to continiue with existing
|
||||||
# connection and we will close it, to avoid its reuse by the `cursor` method.
|
# connection and we will close it, to avoid its reuse by the `cursor` method.
|
||||||
if isinstance(e, psycopg2.OperationalError):
|
if isinstance(e, psycopg.OperationalError):
|
||||||
self._connection.close()
|
self._connection.close()
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
@@ -255,21 +288,13 @@ class Postgresql(object):
|
|||||||
except RetryFailedError as e:
|
except RetryFailedError as e:
|
||||||
raise PostgresConnectionException(str(e))
|
raise PostgresConnectionException(str(e))
|
||||||
|
|
||||||
|
def pg_control_exists(self):
|
||||||
|
return os.path.isfile(self._pg_control)
|
||||||
|
|
||||||
def data_directory_empty(self):
|
def data_directory_empty(self):
|
||||||
return not os.path.exists(self._data_dir) or os.listdir(self._data_dir) == []
|
if self.pg_control_exists():
|
||||||
|
return False
|
||||||
def write_pgpass(self, record):
|
return data_directory_is_empty(self._data_dir)
|
||||||
if 'user' not in record or 'password' not in record:
|
|
||||||
return os.environ.copy()
|
|
||||||
|
|
||||||
with open(self._pgpass, 'w') as f:
|
|
||||||
if os.name != 'nt':
|
|
||||||
os.fchmod(f.fileno(), 0o600)
|
|
||||||
f.write('{host}:{port}:*:{user}:{password}\n'.format(**record))
|
|
||||||
|
|
||||||
env = os.environ.copy()
|
|
||||||
env['PGPASSFILE'] = self._pgpass
|
|
||||||
return env
|
|
||||||
|
|
||||||
def replica_method_options(self, method):
|
def replica_method_options(self, method):
|
||||||
return deepcopy(self.config.get(method, {}))
|
return deepcopy(self.config.get(method, {}))
|
||||||
@@ -285,15 +310,41 @@ class Postgresql(object):
|
|||||||
replica_methods = self.create_replica_methods
|
replica_methods = self.create_replica_methods
|
||||||
return any(self.replica_method_can_work_without_replication_connection(m) for m in replica_methods)
|
return any(self.replica_method_can_work_without_replication_connection(m) for m in replica_methods)
|
||||||
|
|
||||||
def reset_cluster_info_state(self):
|
@property
|
||||||
|
def enforce_hot_standby_feedback(self):
|
||||||
|
return self._enforce_hot_standby_feedback
|
||||||
|
|
||||||
|
def set_enforce_hot_standby_feedback(self, value):
|
||||||
|
# If we enable or disable the hot_standby_feedback we need to update postgresql.conf and reload
|
||||||
|
if self._enforce_hot_standby_feedback != value:
|
||||||
|
self._enforce_hot_standby_feedback = value
|
||||||
|
if self.is_running():
|
||||||
|
self.config.write_postgresql_conf()
|
||||||
|
self.reload()
|
||||||
|
|
||||||
|
def reset_cluster_info_state(self, cluster, nofailover=None):
|
||||||
self._cluster_info_state = {}
|
self._cluster_info_state = {}
|
||||||
|
if cluster and cluster.config and cluster.config.modify_index:
|
||||||
|
self._has_permanent_logical_slots =\
|
||||||
|
cluster.has_permanent_logical_slots(self.name, nofailover, self.major_version)
|
||||||
|
|
||||||
|
# We want to enable hot_standby_feedback if the replica is supposed
|
||||||
|
# to have a logical slot or in case if it is the cascading replica.
|
||||||
|
self.set_enforce_hot_standby_feedback(
|
||||||
|
self._has_permanent_logical_slots or
|
||||||
|
cluster.should_enforce_hot_standby_feedback(self.name, nofailover, self.major_version))
|
||||||
|
|
||||||
def _cluster_info_state_get(self, name):
|
def _cluster_info_state_get(self, name):
|
||||||
if not self._cluster_info_state:
|
if not self._cluster_info_state:
|
||||||
stmt = cluster_info_query.format(self.wal_name, self.lsn_name)
|
|
||||||
try:
|
try:
|
||||||
result = self._is_leader_retry(self._query, stmt).fetchone()
|
result = self._is_leader_retry(self._query, self.cluster_info_query).fetchone()
|
||||||
self._cluster_info_state = dict(zip(['timeline', 'wal_position'], result))
|
cluster_info_state = dict(zip(['timeline', 'wal_position', 'replayed_location',
|
||||||
|
'received_location', 'replay_paused', 'pg_control_timeline',
|
||||||
|
'received_tli', 'slot_name', 'conninfo', 'slots'], result))
|
||||||
|
if self._has_permanent_logical_slots:
|
||||||
|
cluster_info_state['slots'] =\
|
||||||
|
self.slots_handler.process_permanent_slots(cluster_info_state['slots'])
|
||||||
|
self._cluster_info_state = cluster_info_state
|
||||||
except RetryFailedError as e: # SELECT failed two times
|
except RetryFailedError as e: # SELECT failed two times
|
||||||
self._cluster_info_state = {'error': str(e)}
|
self._cluster_info_state = {'error': str(e)}
|
||||||
if not self.is_starting() and self.pg_isready() == STATE_REJECT:
|
if not self.is_starting() and self.pg_isready() == STATE_REJECT:
|
||||||
@@ -304,8 +355,84 @@ class Postgresql(object):
|
|||||||
|
|
||||||
return self._cluster_info_state.get(name)
|
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 slots(self):
|
||||||
|
return self._cluster_info_state_get('slots')
|
||||||
|
|
||||||
|
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):
|
def is_leader(self):
|
||||||
|
try:
|
||||||
return bool(self._cluster_info_state_get('timeline'))
|
return bool(self._cluster_info_state_get('timeline'))
|
||||||
|
except PostgresConnectionException:
|
||||||
|
logger.warning('Failed to determine PostgreSQL state from the connection, falling back to cached role')
|
||||||
|
return bool(self.is_running() and self.role == 'master')
|
||||||
|
|
||||||
|
def replay_paused(self):
|
||||||
|
return self._cluster_info_state_get('replay_paused')
|
||||||
|
|
||||||
|
def resume_wal_replay(self):
|
||||||
|
self._query('SELECT pg_catalog.pg_{0}_replay_resume()'.format(self.wal_name))
|
||||||
|
|
||||||
|
def handle_parameter_change(self):
|
||||||
|
if self.major_version >= 140000 and not self.is_starting() and self.replay_paused():
|
||||||
|
logger.info('Resuming paused WAL replay for PostgreSQL 14+')
|
||||||
|
self.resume_wal_replay()
|
||||||
|
|
||||||
|
def pg_control_timeline(self):
|
||||||
|
try:
|
||||||
|
|
||||||
|
return int(self.controldata().get("Latest checkpoint's TimeLineID"))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
logger.exception('Failed to parse timeline from pg_controldata output')
|
||||||
|
|
||||||
|
def parse_wal_record(self, timeline, lsn):
|
||||||
|
out, err = self.waldump(timeline, lsn, 1)
|
||||||
|
if out and not err:
|
||||||
|
match = re.match(r'^rmgr:\s+(.+?)\s+len \(rec/tot\):\s+\d+/\s+\d+, tx:\s+\d+, '
|
||||||
|
r'lsn: ([0-9A-Fa-f]+/[0-9A-Fa-f]+), prev ([0-9A-Fa-f]+/[0-9A-Fa-f]+), '
|
||||||
|
r'.*?desc: (.+)', out.decode('utf-8'))
|
||||||
|
if match:
|
||||||
|
return match.groups()
|
||||||
|
return None, None, None, None
|
||||||
|
|
||||||
|
def latest_checkpoint_location(self):
|
||||||
|
"""Returns checkpoint location for the cleanly shut down primary.
|
||||||
|
But, if we know that the checkpoint was written to the new WAL
|
||||||
|
due to the archive_mode=on, we will return the LSN of prev wal record (SWITCH)."""
|
||||||
|
|
||||||
|
data = self.controldata()
|
||||||
|
timeline = data.get("Latest checkpoint's TimeLineID")
|
||||||
|
lsn = checkpoint_lsn = data.get('Latest checkpoint location')
|
||||||
|
if data.get('Database cluster state') == 'shut down' and lsn and timeline:
|
||||||
|
try:
|
||||||
|
checkpoint_lsn = parse_lsn(checkpoint_lsn)
|
||||||
|
rm_name, lsn, prev, desc = self.parse_wal_record(timeline, lsn)
|
||||||
|
desc = desc.strip().lower()
|
||||||
|
if rm_name == 'XLOG' and parse_lsn(lsn) == checkpoint_lsn and prev and\
|
||||||
|
desc.startswith('checkpoint') and desc.endswith('shutdown'):
|
||||||
|
_, lsn, _, desc = self.parse_wal_record(timeline, prev)
|
||||||
|
prev = parse_lsn(prev)
|
||||||
|
# If the cluster is shutdown with archive_mode=on, WAL is switched before writing the checkpoint.
|
||||||
|
# In this case we want to take the LSN of previous record (switch) as the last known WAL location.
|
||||||
|
if parse_lsn(lsn) == prev and desc.strip() in ('xlog switch', 'SWITCH'):
|
||||||
|
return prev
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Exception when parsing WAL pg_%sdump output: %r', self.wal_name, e)
|
||||||
|
if isinstance(checkpoint_lsn, six.integer_types):
|
||||||
|
return checkpoint_lsn
|
||||||
|
|
||||||
def is_running(self):
|
def is_running(self):
|
||||||
"""Returns PostmasterProcess if one is running on the data directory or None. If most recently seen process
|
"""Returns PostmasterProcess if one is running on the data directory or None. If most recently seen process
|
||||||
@@ -315,7 +442,7 @@ class Postgresql(object):
|
|||||||
return self._postmaster_proc
|
return self._postmaster_proc
|
||||||
self._postmaster_proc = None
|
self._postmaster_proc = None
|
||||||
|
|
||||||
# we noticed that postgres was restarted, force syncing of replication
|
# we noticed that postgres was restarted, force syncing of replication slots and check of logical slots
|
||||||
self.slots_handler.schedule()
|
self.slots_handler.schedule()
|
||||||
|
|
||||||
self._postmaster_proc = PostmasterProcess.from_pidfile(self._data_dir)
|
self._postmaster_proc = PostmasterProcess.from_pidfile(self._data_dir)
|
||||||
@@ -411,14 +538,21 @@ class Postgresql(object):
|
|||||||
self.set_state('starting')
|
self.set_state('starting')
|
||||||
self._pending_restart = False
|
self._pending_restart = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not self._major_version:
|
||||||
|
self.configure_server_parameters()
|
||||||
configuration = self.config.effective_configuration
|
configuration = self.config.effective_configuration
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
self.config.check_directories()
|
||||||
self.config.write_postgresql_conf(configuration)
|
self.config.write_postgresql_conf(configuration)
|
||||||
self.config.resolve_connection_addresses()
|
self.config.resolve_connection_addresses()
|
||||||
self.config.replace_pg_hba()
|
self.config.replace_pg_hba()
|
||||||
self.config.replace_pg_ident()
|
self.config.replace_pg_ident()
|
||||||
|
|
||||||
options = ['--{0}={1}'.format(p, configuration[p]) for p in self.config.CMDLINE_OPTIONS
|
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:
|
if self.cancellable.is_cancelled:
|
||||||
return False
|
return False
|
||||||
@@ -455,11 +589,13 @@ class Postgresql(object):
|
|||||||
else:
|
else:
|
||||||
return None
|
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
|
check_not_is_in_recovery = connect_kwargs is not None
|
||||||
connect_kwargs = connect_kwargs or self.config.local_connect_kwargs
|
connect_kwargs = connect_kwargs or self.config.local_connect_kwargs
|
||||||
for p in ['connect_timeout', 'options']:
|
for p in ['connect_timeout', 'options']:
|
||||||
connect_kwargs.pop(p, None)
|
connect_kwargs.pop(p, None)
|
||||||
|
if timeout:
|
||||||
|
connect_kwargs['connect_timeout'] = timeout
|
||||||
try:
|
try:
|
||||||
with get_connection_cursor(**connect_kwargs) as cur:
|
with get_connection_cursor(**connect_kwargs) as cur:
|
||||||
cur.execute("SET statement_timeout = 0")
|
cur.execute("SET statement_timeout = 0")
|
||||||
@@ -467,12 +603,13 @@ class Postgresql(object):
|
|||||||
cur.execute('SELECT pg_catalog.pg_is_in_recovery()')
|
cur.execute('SELECT pg_catalog.pg_is_in_recovery()')
|
||||||
if cur.fetchone()[0]:
|
if cur.fetchone()[0]:
|
||||||
return 'is_in_recovery=true'
|
return 'is_in_recovery=true'
|
||||||
return cur.execute('CHECKPOINT')
|
cur.execute('CHECKPOINT')
|
||||||
except psycopg2.Error:
|
except psycopg.Error:
|
||||||
logger.exception('Exception during CHECKPOINT')
|
logger.exception('Exception during CHECKPOINT')
|
||||||
return 'not accessible or not healty'
|
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, on_shutdown=None, stop_timeout=None):
|
||||||
"""Stop PostgreSQL
|
"""Stop PostgreSQL
|
||||||
|
|
||||||
Supports a callback when a safepoint is reached. A safepoint is when no user backend can return a successful
|
Supports a callback when a safepoint is reached. A safepoint is when no user backend can return a successful
|
||||||
@@ -480,11 +617,12 @@ class Postgresql(object):
|
|||||||
could be added.
|
could be added.
|
||||||
|
|
||||||
:param on_safepoint: This callback is called when no user backends are running.
|
:param on_safepoint: This callback is called when no user backends are running.
|
||||||
|
:param on_shutdown: is called when pg_controldata starts reporting `Database cluster state: shut down`
|
||||||
"""
|
"""
|
||||||
if checkpoint is None:
|
if checkpoint is None:
|
||||||
checkpoint = False if mode == 'immediate' else True
|
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, on_shutdown, stop_timeout)
|
||||||
if success:
|
if success:
|
||||||
# block_callbacks is used during restart to avoid
|
# block_callbacks is used during restart to avoid
|
||||||
# running start/stop callbacks in addition to restart ones
|
# running start/stop callbacks in addition to restart ones
|
||||||
@@ -497,7 +635,7 @@ class Postgresql(object):
|
|||||||
self.set_state('stop failed')
|
self.set_state('stop failed')
|
||||||
return success
|
return success
|
||||||
|
|
||||||
def _do_stop(self, mode, block_callbacks, checkpoint, on_safepoint):
|
def _do_stop(self, mode, block_callbacks, checkpoint, on_safepoint, on_shutdown, stop_timeout):
|
||||||
postmaster = self.is_running()
|
postmaster = self.is_running()
|
||||||
if not postmaster:
|
if not postmaster:
|
||||||
if on_safepoint:
|
if on_safepoint:
|
||||||
@@ -505,13 +643,13 @@ class Postgresql(object):
|
|||||||
return True, False
|
return True, False
|
||||||
|
|
||||||
if checkpoint and not self.is_starting():
|
if checkpoint and not self.is_starting():
|
||||||
self.checkpoint()
|
self.checkpoint(timeout=stop_timeout)
|
||||||
|
|
||||||
if not block_callbacks:
|
if not block_callbacks:
|
||||||
self.set_state('stopping')
|
self.set_state('stopping')
|
||||||
|
|
||||||
# Send signal to postmaster to stop
|
# Send signal to postmaster to stop
|
||||||
success = postmaster.signal_stop(mode)
|
success = postmaster.signal_stop(mode, self.pgcommand('pg_ctl'))
|
||||||
if success is not None:
|
if success is not None:
|
||||||
if success and on_safepoint:
|
if success and on_safepoint:
|
||||||
on_safepoint()
|
on_safepoint()
|
||||||
@@ -521,18 +659,51 @@ class Postgresql(object):
|
|||||||
if on_safepoint:
|
if on_safepoint:
|
||||||
# Wait for our connection to terminate so we can be sure that no new connections are being initiated
|
# Wait for our connection to terminate so we can be sure that no new connections are being initiated
|
||||||
self._wait_for_connection_close(postmaster)
|
self._wait_for_connection_close(postmaster)
|
||||||
postmaster.wait_for_user_backends_to_close()
|
postmaster.wait_for_user_backends_to_close(stop_timeout)
|
||||||
on_safepoint()
|
on_safepoint()
|
||||||
|
|
||||||
|
if on_shutdown and mode in ('fast', 'smart'):
|
||||||
|
i = 0
|
||||||
|
# Wait for pg_controldata `Database cluster state:` to change to "shut down"
|
||||||
|
while postmaster.is_running():
|
||||||
|
data = self.controldata()
|
||||||
|
if data.get('Database cluster state', '') == 'shut down':
|
||||||
|
on_shutdown(self.latest_checkpoint_location())
|
||||||
|
break
|
||||||
|
elif data.get('Database cluster state', '').startswith('shut down'): # shut down in recovery
|
||||||
|
break
|
||||||
|
elif stop_timeout and i >= stop_timeout:
|
||||||
|
stop_timeout = 0
|
||||||
|
break
|
||||||
|
time.sleep(STOP_POLLING_INTERVAL)
|
||||||
|
i += STOP_POLLING_INTERVAL
|
||||||
|
|
||||||
|
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()
|
postmaster.wait()
|
||||||
|
|
||||||
return True, True
|
return True, True
|
||||||
|
|
||||||
@staticmethod
|
def terminate_postmaster(self, postmaster, mode, stop_timeout):
|
||||||
def terminate_starting_postmaster(postmaster):
|
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
|
"""Terminates a postmaster that has not yet opened ports or possibly even written a pid file. Blocks
|
||||||
until the process goes away."""
|
until the process goes away."""
|
||||||
postmaster.signal_stop('immediate')
|
postmaster.signal_stop('immediate', self.pgcommand('pg_ctl'))
|
||||||
postmaster.wait()
|
postmaster.wait()
|
||||||
|
|
||||||
def _wait_for_connection_close(self, postmaster):
|
def _wait_for_connection_close(self, postmaster):
|
||||||
@@ -541,12 +712,12 @@ class Postgresql(object):
|
|||||||
while postmaster.is_running(): # Need a timeout here?
|
while postmaster.is_running(): # Need a timeout here?
|
||||||
cur.execute("SELECT 1")
|
cur.execute("SELECT 1")
|
||||||
time.sleep(STOP_POLLING_INTERVAL)
|
time.sleep(STOP_POLLING_INTERVAL)
|
||||||
except psycopg2.Error:
|
except psycopg.Error:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def reload(self):
|
def reload(self, block_callbacks=False):
|
||||||
ret = self.pg_ctl('reload')
|
ret = self.pg_ctl('reload')
|
||||||
if ret:
|
if ret and not block_callbacks:
|
||||||
self.call_nowait(ACTION_ON_RELOAD)
|
self.call_nowait(ACTION_ON_RELOAD)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@@ -566,10 +737,12 @@ class Postgresql(object):
|
|||||||
if ready == STATE_REJECT:
|
if ready == STATE_REJECT:
|
||||||
return False
|
return False
|
||||||
elif ready == STATE_NO_RESPONSE:
|
elif ready == STATE_NO_RESPONSE:
|
||||||
|
ret = not self.is_running()
|
||||||
|
if ret:
|
||||||
self.set_state('start failed')
|
self.set_state('start failed')
|
||||||
self.slots_handler.schedule(False) # TODO: can remove this?
|
self.slots_handler.schedule(False) # TODO: can remove this?
|
||||||
self.config.save_configuration_files(True) # TODO: maybe remove this?
|
self.config.save_configuration_files(True) # TODO: maybe remove this?
|
||||||
return True
|
return ret
|
||||||
else:
|
else:
|
||||||
if ready != STATE_RUNNING:
|
if ready != STATE_RUNNING:
|
||||||
# Bad configuration or unexpected OS error. No idea of PostgreSQL status.
|
# Bad configuration or unexpected OS error. No idea of PostgreSQL status.
|
||||||
@@ -624,45 +797,61 @@ class Postgresql(object):
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def get_guc_value(self, name):
|
||||||
|
cmd = [self.pgcommand('postgres'), '-D', self._data_dir, '-C', name,
|
||||||
|
'--config-file={}'.format(self.config.postgresql_conf)]
|
||||||
|
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):
|
def controldata(self):
|
||||||
""" return the contents of pg_controldata, or non-True value if pg_controldata call failed """
|
""" 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
|
# Don't try to call pg_controldata during backup restore
|
||||||
if self._version_file_exists() and self.state != 'creating replica':
|
if self._version_file_exists() and self.state != 'creating replica':
|
||||||
try:
|
try:
|
||||||
env = {'LANG': 'C', 'LC_ALL': 'C', 'PATH': os.getenv('PATH')}
|
env = os.environ.copy()
|
||||||
if os.getenv('SYSTEMROOT') is not None:
|
env.update(LANG='C', LC_ALL='C')
|
||||||
env['SYSTEMROOT'] = os.getenv('SYSTEMROOT')
|
|
||||||
data = subprocess.check_output([self.pgcommand('pg_controldata'), self._data_dir], env=env)
|
data = subprocess.check_output([self.pgcommand('pg_controldata'), self._data_dir], env=env)
|
||||||
if data:
|
if data:
|
||||||
data = data.decode('utf-8').splitlines()
|
data = filter(lambda e: ':' in e, data.decode('utf-8').splitlines())
|
||||||
# pg_controldata output depends on major verion. Some of parameters are prefixed by 'Current '
|
# pg_controldata output depends on major version. Some of parameters are prefixed by 'Current '
|
||||||
result = {l.split(':')[0].replace('Current ', '', 1): l.split(':', 1)[1].strip() for l in data
|
return {k.replace('Current ', '', 1): v.strip() for k, v in map(lambda e: e.split(':', 1), data)}
|
||||||
if l and ':' in l}
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
logger.exception("Error when calling pg_controldata")
|
logger.exception("Error when calling pg_controldata")
|
||||||
return result
|
return {}
|
||||||
|
|
||||||
|
def waldump(self, timeline, lsn, limit):
|
||||||
|
cmd = self.pgcommand('pg_{0}dump'.format(self.wal_name))
|
||||||
|
env = os.environ.copy()
|
||||||
|
env.update(LANG='C', LC_ALL='C', PGDATA=self._data_dir)
|
||||||
|
try:
|
||||||
|
waldump = subprocess.Popen([cmd, '-t', str(timeline), '-s', str(lsn), '-n', str(limit)],
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
|
||||||
|
out, err = waldump.communicate()
|
||||||
|
waldump.wait()
|
||||||
|
return out, err
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Failed to execute `%s -t %s -s %s -n %s`: %r', cmd, timeline, lsn, limit, e)
|
||||||
|
return None, None
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def get_replication_connection_cursor(self, host='localhost', port=5432, database=None, **kwargs):
|
def get_replication_connection_cursor(self, host=None, port=5432, **kwargs):
|
||||||
replication = self.config.replication
|
conn_kwargs = self.config.replication.copy()
|
||||||
with get_connection_cursor(host=host, port=int(port), database=database or self._database, replication=1,
|
conn_kwargs.update(host=host, port=int(port) if port else None, user=conn_kwargs.pop('username'),
|
||||||
user=replication['username'], password=replication.get('password'),
|
connect_timeout=3, replication=1, options='-c statement_timeout=2000')
|
||||||
connect_timeout=3, options='-c statement_timeout=2000') as cur:
|
with get_connection_cursor(**conn_kwargs) as cur:
|
||||||
yield cur
|
yield cur
|
||||||
|
|
||||||
def get_local_timeline_lsn_from_replication_connection(self):
|
def get_replica_timeline(self):
|
||||||
timeline = lsn = None
|
|
||||||
try:
|
try:
|
||||||
with self.get_replication_connection_cursor(**self.config.local_replication_address) as cur:
|
with self.get_replication_connection_cursor(**self.config.local_replication_address) as cur:
|
||||||
cur.execute('IDENTIFY_SYSTEM')
|
cur.execute('IDENTIFY_SYSTEM')
|
||||||
timeline, lsn = cur.fetchone()[1:3]
|
return cur.fetchone()[1]
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Can not fetch local timeline and lsn from replication connection')
|
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):
|
def replica_cached_timeline(self, master_timeline):
|
||||||
if not self._cached_replica_timeline or not master_timeline or self._cached_replica_timeline != master_timeline:
|
if not self._cached_replica_timeline or not master_timeline or self._cached_replica_timeline != master_timeline:
|
||||||
@@ -673,64 +862,50 @@ class Postgresql(object):
|
|||||||
return self._cluster_info_state_get('timeline')
|
return self._cluster_info_state_get('timeline')
|
||||||
|
|
||||||
def get_history(self, timeline):
|
def get_history(self, timeline):
|
||||||
history_path = 'pg_{0}/{1:08X}.history'.format(self.wal_name, timeline)
|
history_path = os.path.join(self.wal_dir, '{0:08X}.history'.format(timeline))
|
||||||
|
history_mtime = mtime(history_path)
|
||||||
|
if history_mtime:
|
||||||
try:
|
try:
|
||||||
cursor = self._connection.cursor()
|
with open(history_path, 'r') as f:
|
||||||
cursor.execute('SELECT isdir, modification FROM pg_catalog.pg_stat_file(%s)', (history_path,))
|
history = f.read()
|
||||||
isdir, modification = cursor.fetchone()
|
history = list(parse_history(history))
|
||||||
if not isdir:
|
|
||||||
cursor.execute('SELECT pg_catalog.pg_read_file(%s)', (history_path,))
|
|
||||||
history = list(parse_history(cursor.fetchone()[0]))
|
|
||||||
if history[-1][0] == timeline - 1:
|
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())
|
||||||
|
history[-1].append(self.name)
|
||||||
return history
|
return history
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Failed to read and parse %s', (history_path,))
|
logger.exception('Failed to read and parse %s', (history_path,))
|
||||||
|
|
||||||
def follow(self, member, role='replica', timeout=None):
|
def follow(self, member, role='replica', timeout=None, do_reload=False):
|
||||||
is_remote_master = isinstance(member, RemoteMember)
|
recovery_params = self.config.build_recovery_params(member)
|
||||||
no_replication_slot = is_remote_master and member.no_replication_slot
|
|
||||||
restore_command = is_remote_master and member.restore_command
|
|
||||||
min_apply_delay = is_remote_master and member.recovery_min_apply_delay
|
|
||||||
archive_cleanup = is_remote_master and member.archive_cleanup_command
|
|
||||||
|
|
||||||
primary_conninfo = self.config.primary_conninfo(member)
|
|
||||||
change_role = self.cb_called and (self.role in ('master', 'demoted') or
|
|
||||||
not {'standby_leader', 'replica'} - {self.role, role})
|
|
||||||
|
|
||||||
recovery_params = self.config.get('recovery_conf', {}).copy()
|
|
||||||
recovery_params.update({'standby_mode': 'on', 'recovery_target_timeline': 'latest'})
|
|
||||||
if primary_conninfo:
|
|
||||||
recovery_params['primary_conninfo'] = primary_conninfo
|
|
||||||
if self.slots_handler.use_slots and not no_replication_slot:
|
|
||||||
required_name = is_remote_master and member.data.get('primary_slot_name')
|
|
||||||
name = required_name or slot_name_from_member_name(self.name)
|
|
||||||
recovery_params['primary_slot_name'] = name
|
|
||||||
if restore_command:
|
|
||||||
recovery_params['restore_command'] = restore_command
|
|
||||||
if min_apply_delay:
|
|
||||||
recovery_params['recovery_min_apply_delay'] = min_apply_delay
|
|
||||||
if archive_cleanup:
|
|
||||||
recovery_params['archive_cleanup_command'] = archive_cleanup
|
|
||||||
|
|
||||||
self.config.write_recovery_conf(recovery_params)
|
self.config.write_recovery_conf(recovery_params)
|
||||||
|
|
||||||
# When we demoting the master or standby_leader to replica or promoting replica to a standby_leader
|
# When we demoting the master or standby_leader to replica or promoting replica to a standby_leader
|
||||||
# and we know for sure that postgres was already running before, we will only execute on_role_change
|
# and we know for sure that postgres was already running before, we will only execute on_role_change
|
||||||
# callback and prevent execution of on_restart/on_start callback.
|
# callback and prevent execution of on_restart/on_start callback.
|
||||||
# If the role remains the same (replica or standby_leader), we will execute on_start or on_restart
|
# If the role remains the same (replica or standby_leader), we will execute on_start or on_restart
|
||||||
|
change_role = self.cb_called and (self.role in ('master', 'demoted') or
|
||||||
|
not {'standby_leader', 'replica'} - {self.role, role})
|
||||||
if change_role:
|
if change_role:
|
||||||
self.__cb_pending = ACTION_NOOP
|
self.__cb_pending = ACTION_NOOP
|
||||||
|
|
||||||
|
ret = True
|
||||||
if self.is_running():
|
if self.is_running():
|
||||||
self.restart(block_callbacks=change_role, role=role)
|
if do_reload:
|
||||||
|
self.config.write_postgresql_conf()
|
||||||
|
ret = self.reload(block_callbacks=change_role)
|
||||||
|
if ret and change_role:
|
||||||
|
self.set_role(role)
|
||||||
else:
|
else:
|
||||||
self.start(timeout=timeout, block_callbacks=change_role, role=role)
|
ret = self.restart(block_callbacks=change_role, role=role)
|
||||||
|
else:
|
||||||
|
ret = self.start(timeout=timeout, block_callbacks=change_role, role=role) or None
|
||||||
|
|
||||||
if change_role:
|
if change_role:
|
||||||
# TODO: postpone this until start completes, or maybe do even earlier
|
# TODO: postpone this until start completes, or maybe do even earlier
|
||||||
self.call_nowait(ACTION_ON_ROLE_CHANGE)
|
self.call_nowait(ACTION_ON_ROLE_CHANGE)
|
||||||
return True
|
return ret
|
||||||
|
|
||||||
def _wait_promote(self, wait_seconds):
|
def _wait_promote(self, wait_seconds):
|
||||||
for _ in polling_loop(wait_seconds):
|
for _ in polling_loop(wait_seconds):
|
||||||
@@ -738,49 +913,136 @@ class Postgresql(object):
|
|||||||
if data.get('Database cluster state') == 'in production':
|
if data.get('Database cluster state') == 'in production':
|
||||||
return True
|
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):
|
||||||
if self.role == 'master':
|
if self.role == 'master':
|
||||||
return True
|
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
|
||||||
|
|
||||||
|
self.slots_handler.on_promote()
|
||||||
|
|
||||||
ret = self.pg_ctl('promote', '-W')
|
ret = self.pg_ctl('promote', '-W')
|
||||||
if ret:
|
if ret:
|
||||||
self.set_role('master')
|
self.set_role('master')
|
||||||
if on_success is not None:
|
if on_success is not None:
|
||||||
on_success()
|
on_success()
|
||||||
if not access_is_restricted:
|
|
||||||
self.call_nowait(ACTION_ON_ROLE_CHANGE)
|
self.call_nowait(ACTION_ON_ROLE_CHANGE)
|
||||||
ret = self._wait_promote(wait_seconds)
|
ret = self._wait_promote(wait_seconds)
|
||||||
return ret
|
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):
|
def timeline_wal_position(self):
|
||||||
# This method could be called from different threads (simultaneously with some other `_query` calls).
|
# 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 it is called not from main thread we will create a new cursor to execute statement.
|
||||||
if current_thread().ident == self.__thread_ident:
|
if current_thread().ident == self.__thread_ident:
|
||||||
return self._cluster_info_state_get('timeline'), self._cluster_info_state_get('wal_position')
|
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:
|
with self.connection().cursor() as cursor:
|
||||||
cursor.execute(cluster_info_query.format(self.wal_name, self.lsn_name))
|
cursor.execute(self.cluster_info_query)
|
||||||
return cursor.fetchone()[:2]
|
(timeline, wal_position, replayed_location,
|
||||||
|
received_location, _, pg_control_timeline) = cursor.fetchone()[:6]
|
||||||
|
|
||||||
|
wal_position = self._wal_position(timeline, wal_position, received_location, replayed_location)
|
||||||
|
return (timeline, wal_position, pg_control_timeline)
|
||||||
|
|
||||||
def postmaster_start_time(self):
|
def postmaster_start_time(self):
|
||||||
try:
|
try:
|
||||||
cursor = self.query("SELECT pg_catalog.to_char(pg_catalog.pg_postmaster_start_time(),"
|
query = "SELECT " + self.POSTMASTER_START_TIME
|
||||||
" 'YYYY-MM-DD HH24:MI:SS.MS TZ')")
|
if current_thread().ident == self.__thread_ident:
|
||||||
return cursor.fetchone()[0]
|
return self.query(query).fetchone()[0].isoformat(sep=' ')
|
||||||
except psycopg2.Error:
|
with self.connection().cursor() as cursor:
|
||||||
|
cursor.execute(query)
|
||||||
|
return cursor.fetchone()[0].isoformat(sep=' ')
|
||||||
|
except psycopg.Error:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def last_operation(self):
|
def last_operation(self):
|
||||||
return str(self._cluster_info_state_get('wal_position'))
|
return self._wal_position(self.is_leader(), self._cluster_info_state_get('wal_position'),
|
||||||
|
self.received_location(), self.replayed_location())
|
||||||
|
|
||||||
def configure_server_parameters(self):
|
def configure_server_parameters(self):
|
||||||
self._major_version = self.get_major_version()
|
self._major_version = self.get_major_version()
|
||||||
self.config.setup_server_parameters()
|
self.config.setup_server_parameters()
|
||||||
return True
|
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):
|
def move_data_directory(self):
|
||||||
if os.path.isdir(self._data_dir) and not self.is_running():
|
if os.path.isdir(self._data_dir) and not self.is_running():
|
||||||
try:
|
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(new_name, source)
|
||||||
|
|
||||||
|
# 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(new_name, source)
|
||||||
|
|
||||||
|
new_name = '{0}_{1}'.format(self._data_dir, postfix)
|
||||||
logger.info('renaming data directory to %s', new_name)
|
logger.info('renaming data directory to %s', new_name)
|
||||||
os.rename(self._data_dir, new_name)
|
os.rename(self._data_dir, new_name)
|
||||||
except OSError:
|
except OSError:
|
||||||
@@ -798,105 +1060,82 @@ class Postgresql(object):
|
|||||||
os.remove(self._data_dir)
|
os.remove(self._data_dir)
|
||||||
elif os.path.isdir(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
|
# should clean the target
|
||||||
for pg_wal_dir in ('pg_xlog', 'pg_wal'):
|
for pg_wal_realpath in self.pg_wal_realpath().values():
|
||||||
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)
|
logger.info('Removing WAL directory: %s', pg_wal_realpath)
|
||||||
shutil.rmtree(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)
|
shutil.rmtree(self._data_dir)
|
||||||
except (IOError, OSError):
|
except (IOError, OSError):
|
||||||
logger.exception('Could not remove data directory %s', self._data_dir)
|
logger.exception('Could not remove data directory %s', self._data_dir)
|
||||||
self.move_data_directory()
|
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, sync_node_maxlag=-1):
|
||||||
"""Finds the best candidate to be the synchronous standby.
|
"""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
|
Current synchronous standby is always preferred, unless it has disconnected or does not want to be a
|
||||||
synchronous standby any longer.
|
synchronous standby any longer.
|
||||||
|
Parameter sync_node_maxlag(maximum_lag_on_syncnode) would help swapping unhealthy sync replica in case
|
||||||
|
if it stops responding (or hung). Please set the value high enough so it won't unncessarily swap sync
|
||||||
|
standbys during high loads. Any less or equal of 0 value keep the behavior backward compatible and
|
||||||
|
will not swap. Please note that it will not also swap sync standbys in case where all replicas are hung.
|
||||||
|
|
||||||
: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
|
if self._major_version < 90600:
|
||||||
current = current.lower() if current else current
|
sync_node_count = 1
|
||||||
members = {m.name.lower(): m for m in cluster.members}
|
members = {m.name.lower(): m for m in cluster.members}
|
||||||
candidates = []
|
candidates = []
|
||||||
# Pick candidates based on who has flushed WAL farthest.
|
sync_nodes = []
|
||||||
# TODO: for synchronous_commit = remote_write we actually want to order on write_location
|
replica_list = []
|
||||||
for app_name, state, sync_state in self.query(
|
# Pick candidates based on who has higher replay/remote_write/flush lsn.
|
||||||
"SELECT pg_catalog.lower(application_name), state, sync_state"
|
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, sync_state, replica_lsn in self.query(
|
||||||
|
"SELECT pg_catalog.lower(application_name), sync_state, pg_{2}_{1}_diff({0}_{1}, '0/0')::bigint"
|
||||||
" FROM pg_catalog.pg_stat_replication"
|
" FROM pg_catalog.pg_stat_replication"
|
||||||
" ORDER BY flush_{0} DESC".format(self.lsn_name)):
|
" WHERE state = 'streaming' AND {0}_{1} IS NOT NULL"
|
||||||
|
" ORDER BY sync_state DESC, {0}_{1} DESC".format(sort_col, self.lsn_name, self.wal_name)):
|
||||||
member = members.get(app_name)
|
member = members.get(app_name)
|
||||||
if state != 'streaming' or not member or member.tags.get('nosync', False):
|
if member and not member.tags.get('nosync', False):
|
||||||
continue
|
replica_list.append((member.name, sync_state, replica_lsn, bool(member.nofailover)))
|
||||||
if sync_state == 'sync':
|
|
||||||
return app_name, True
|
max_lsn = max(replica_list, key=lambda x: x[2])[2] if len(replica_list) > 1 else int(str(self.last_operation()))
|
||||||
if sync_state == 'potential' and app_name == current:
|
|
||||||
# Prefer current even if not the best one any more to avoid indecisivness and spurious swaps.
|
# Prefer members without nofailover tag. We are relying on the fact that sorts are guaranteed to be stable.
|
||||||
return current, False
|
for app_name, sync_state, replica_lsn, _ in sorted(replica_list, key=lambda x: x[3]):
|
||||||
if sync_state in ('async', 'potential'):
|
if sync_node_maxlag <= 0 or max_lsn - replica_lsn <= sync_node_maxlag:
|
||||||
candidates.append(app_name)
|
candidates.append(app_name)
|
||||||
|
if sync_state == 'sync':
|
||||||
|
sync_nodes.append(app_name)
|
||||||
|
if len(candidates) >= sync_node_count:
|
||||||
|
break
|
||||||
|
|
||||||
if candidates:
|
return candidates, sync_nodes
|
||||||
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
|
|
||||||
|
|
||||||
def schedule_sanity_checks_after_pause(self):
|
def schedule_sanity_checks_after_pause(self):
|
||||||
"""
|
"""
|
||||||
After coming out of pause we have to:
|
After coming out of pause we have to:
|
||||||
1. sync replication slots, because it might happen that slots were removed
|
1. configure server parameters if necessary
|
||||||
2. get new 'Database system identifier' to make sure that it wasn't changed
|
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.slots_handler.schedule()
|
||||||
self._sysid = None
|
self._sysid = None
|
||||||
|
|||||||
@@ -4,10 +4,11 @@ import shlex
|
|||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from patroni.dcs import RemoteMember
|
|
||||||
from patroni.utils import deep_compare, uri
|
|
||||||
from six import string_types
|
from six import string_types
|
||||||
from six.moves.urllib.parse import quote_plus
|
|
||||||
|
from ..dcs import RemoteMember
|
||||||
|
from ..psycopg import quote_ident, quote_literal
|
||||||
|
from ..utils import deep_compare
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -54,7 +55,7 @@ class Bootstrap(object):
|
|||||||
error_handler('Error when parsing {0} option {1}: value should be string value'
|
error_handler('Error when parsing {0} option {1}: value should be string value'
|
||||||
' or a single key-value pair'.format(tool, opt))
|
' or a single key-value pair'.format(tool, opt))
|
||||||
else:
|
else:
|
||||||
error_handler('{0} options must be list ot dict'.format(tool))
|
error_handler('{0} options must be list or dict'.format(tool))
|
||||||
return user_options
|
return user_options
|
||||||
|
|
||||||
def _initdb(self, config):
|
def _initdb(self, config):
|
||||||
@@ -91,15 +92,16 @@ class Bootstrap(object):
|
|||||||
self._postgresql.configure_server_parameters()
|
self._postgresql.configure_server_parameters()
|
||||||
|
|
||||||
# make sure there is no trigger file or postgres will be automatically promoted
|
# make sure there is no trigger file or postgres will be automatically promoted
|
||||||
trigger_file = 'promote_trigger_file' if self._postgresql.major_version >= 120000 else 'trigger_file'
|
trigger_file = self._postgresql.config.triggerfile_good_name
|
||||||
trigger_file = self._postgresql.config.get('recovery_conf', {}).get(trigger_file) or 'promote'
|
trigger_file = (self._postgresql.config.get('recovery_conf') or {}).get(trigger_file) or 'promote'
|
||||||
trigger_file = os.path.abspath(os.path.join(self._postgresql.data_dir, trigger_file))
|
trigger_file = os.path.abspath(os.path.join(self._postgresql.data_dir, trigger_file))
|
||||||
if os.path.exists(trigger_file):
|
if os.path.exists(trigger_file):
|
||||||
os.unlink(trigger_file)
|
os.unlink(trigger_file)
|
||||||
|
|
||||||
def _custom_bootstrap(self, config):
|
def _custom_bootstrap(self, config):
|
||||||
self._postgresql.set_state('running custom bootstrap script')
|
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:
|
try:
|
||||||
logger.info('Running custom bootstrap script: %s', config['command'])
|
logger.info('Running custom bootstrap script: %s', config['command'])
|
||||||
if self._postgresql.cancellable.call(shlex.split(config['command']) + params) != 0:
|
if self._postgresql.cancellable.call(shlex.split(config['command']) + params) != 0:
|
||||||
@@ -123,28 +125,15 @@ class Bootstrap(object):
|
|||||||
cmd = config.get('post_bootstrap') or config.get('post_init')
|
cmd = config.get('post_bootstrap') or config.get('post_init')
|
||||||
if cmd:
|
if cmd:
|
||||||
r = self._postgresql.config.local_connect_kwargs
|
r = self._postgresql.config.local_connect_kwargs
|
||||||
|
connstring = self._postgresql.config.format_dsn(r, True)
|
||||||
if 'host' in r:
|
if 'host' not in r:
|
||||||
# '/tmp' => '%2Ftmp' for unix socket path
|
|
||||||
host = quote_plus(r['host']) if r['host'].startswith('/') else r['host']
|
|
||||||
else:
|
|
||||||
host = ''
|
|
||||||
|
|
||||||
# https://www.postgresql.org/docs/current/static/libpq-pgpass.html
|
# https://www.postgresql.org/docs/current/static/libpq-pgpass.html
|
||||||
# A host name of localhost matches both TCP (host name localhost) and Unix domain socket
|
# A host name of localhost matches both TCP (host name localhost) and Unix domain socket
|
||||||
# (pghost empty or the default socket directory) connections coming from the local machine.
|
# (pghost empty or the default socket directory) connections coming from the local machine.
|
||||||
r['host'] = 'localhost' # set it to localhost to write into pgpass
|
r['host'] = 'localhost' # set it to localhost to write into pgpass
|
||||||
|
|
||||||
if 'user' in r:
|
env = self._postgresql.config.write_pgpass(r)
|
||||||
user = r['user']
|
env['PGOPTIONS'] = '-c synchronous_commit=local'
|
||||||
else:
|
|
||||||
user = ''
|
|
||||||
if 'password' in r:
|
|
||||||
import getpass
|
|
||||||
r.setdefault('user', os.environ.get('PGUSER', getpass.getuser()))
|
|
||||||
|
|
||||||
connstring = uri('postgres', (host, r['port']), r['database'], user)
|
|
||||||
env = self._postgresql.write_pgpass(r) if 'password' in r else None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret = self._postgresql.cancellable.call(shlex.split(cmd) + [connstring], env=env)
|
ret = self._postgresql.cancellable.call(shlex.split(cmd) + [connstring], env=env)
|
||||||
@@ -176,9 +165,9 @@ class Bootstrap(object):
|
|||||||
|
|
||||||
if clone_member and clone_member.conn_url:
|
if clone_member and clone_member.conn_url:
|
||||||
r = clone_member.conn_kwargs(self._postgresql.config.replication)
|
r = clone_member.conn_kwargs(self._postgresql.config.replication)
|
||||||
connstring = uri('postgres', (r['host'], r['port']), r['database'], r['user'])
|
|
||||||
# add the credentials to connect to the replica origin to pgpass.
|
# add the credentials to connect to the replica origin to pgpass.
|
||||||
env = self._postgresql.write_pgpass(r)
|
env = self._postgresql.config.write_pgpass(r)
|
||||||
|
connstring = self._postgresql.config.format_dsn(r, True)
|
||||||
else:
|
else:
|
||||||
connstring = ''
|
connstring = ''
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
@@ -310,22 +299,28 @@ class Bootstrap(object):
|
|||||||
if 'NOLOGIN' not in options and 'LOGIN' not in options:
|
if 'NOLOGIN' not in options and 'LOGIN' not in options:
|
||||||
options.append('LOGIN')
|
options.append('LOGIN')
|
||||||
|
|
||||||
params = [name]
|
|
||||||
if password:
|
if password:
|
||||||
options.extend(['PASSWORD', '%s'])
|
options.extend(['PASSWORD', quote_literal(password)])
|
||||||
params.extend([password, password])
|
|
||||||
|
|
||||||
sql = """DO $$
|
sql = """DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
SET local synchronous_commit = 'local';
|
SET local synchronous_commit = 'local';
|
||||||
PERFORM * FROM pg_authid WHERE rolname = %s;
|
PERFORM * FROM pg_catalog.pg_authid WHERE rolname = {0};
|
||||||
IF FOUND THEN
|
IF FOUND THEN
|
||||||
ALTER ROLE "{0}" WITH {1};
|
ALTER ROLE {1} WITH {2};
|
||||||
ELSE
|
ELSE
|
||||||
CREATE ROLE "{0}" WITH {1};
|
CREATE ROLE {1} WITH {2};
|
||||||
END IF;
|
END IF;
|
||||||
END;$$""".format(name, ' '.join(options))
|
END;$$""".format(quote_literal(name), quote_ident(name, self._postgresql.connection()), ' '.join(options))
|
||||||
self._postgresql.query(sql, *params)
|
self._postgresql.query('SET log_statement TO none')
|
||||||
|
self._postgresql.query('SET log_min_duration_statement TO -1')
|
||||||
|
self._postgresql.query("SET log_min_error_statement TO 'log'")
|
||||||
|
try:
|
||||||
|
self._postgresql.query(sql)
|
||||||
|
finally:
|
||||||
|
self._postgresql.query('RESET log_min_error_statement')
|
||||||
|
self._postgresql.query('RESET log_min_duration_statement')
|
||||||
|
self._postgresql.query('RESET log_statement')
|
||||||
|
|
||||||
def post_bootstrap(self, config, task):
|
def post_bootstrap(self, config, task):
|
||||||
try:
|
try:
|
||||||
@@ -347,8 +342,8 @@ END;$$""".format(name, ' '.join(options))
|
|||||||
sql = """DO $$
|
sql = """DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
SET local synchronous_commit = 'local';
|
SET local synchronous_commit = 'local';
|
||||||
GRANT EXECUTE ON function pg_catalog.{0} TO "{1}";
|
GRANT EXECUTE ON function pg_catalog.{0} TO {1};
|
||||||
END;$$""".format(f, rewind['username'])
|
END;$$""".format(f, quote_ident(rewind['username'], self._postgresql.connection()))
|
||||||
postgresql.query(sql)
|
postgresql.query(sql)
|
||||||
|
|
||||||
for name, value in (config.get('users') or {}).items():
|
for name, value in (config.get('users') or {}).items():
|
||||||
@@ -361,6 +356,7 @@ END;$$""".format(f, rewind['username'])
|
|||||||
self._running_custom_bootstrap = False
|
self._running_custom_bootstrap = False
|
||||||
# If we don't have custom configuration for pg_hba.conf we need to restore original file
|
# If we don't have custom configuration for pg_hba.conf we need to restore original file
|
||||||
if not postgresql.config.get('pg_hba'):
|
if not postgresql.config.get('pg_hba'):
|
||||||
|
if os.path.exists(postgresql.config.pg_hba_conf):
|
||||||
os.unlink(postgresql.config.pg_hba_conf)
|
os.unlink(postgresql.config.pg_hba_conf)
|
||||||
postgresql.config.restore_configuration_files()
|
postgresql.config.restore_configuration_files()
|
||||||
postgresql.config.write_postgresql_conf()
|
postgresql.config.write_postgresql_conf()
|
||||||
@@ -369,7 +365,7 @@ END;$$""".format(f, rewind['username'])
|
|||||||
# at this point there should be no recovery.conf
|
# at this point there should be no recovery.conf
|
||||||
postgresql.config.remove_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()
|
postgresql.restart()
|
||||||
else:
|
else:
|
||||||
postgresql.config.replace_pg_hba()
|
postgresql.config.replace_pg_hba()
|
||||||
|
|||||||
@@ -1,40 +1,36 @@
|
|||||||
import logging
|
import logging
|
||||||
import subprocess
|
|
||||||
from threading import Event, Lock, Thread
|
from patroni.postgresql.cancellable import CancellableExecutor
|
||||||
|
from threading import Condition, Thread
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class CallbackExecutor(Thread):
|
class CallbackExecutor(CancellableExecutor, Thread):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(CallbackExecutor, self).__init__()
|
CancellableExecutor.__init__(self)
|
||||||
|
Thread.__init__(self)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self._lock = Lock()
|
|
||||||
self._cmd = None
|
self._cmd = None
|
||||||
self._process = None
|
self._condition = Condition()
|
||||||
self._callback_event = Event()
|
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
def call(self, cmd):
|
def call(self, cmd):
|
||||||
with self._lock:
|
self._kill_process()
|
||||||
if self._process and self._process.poll() is None:
|
with self._condition:
|
||||||
try:
|
|
||||||
self._process.kill()
|
|
||||||
logger.warning('Killed the old callback process because it was still running: %s', self._cmd)
|
|
||||||
except OSError:
|
|
||||||
logger.exception('Failed to kill the old callback')
|
|
||||||
self._cmd = cmd
|
self._cmd = cmd
|
||||||
self._callback_event.set()
|
self._condition.notify()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
self._callback_event.wait()
|
with self._condition:
|
||||||
self._callback_event.clear()
|
if self._cmd is None:
|
||||||
|
self._condition.wait()
|
||||||
|
cmd, self._cmd = self._cmd, None
|
||||||
|
|
||||||
with self._lock:
|
with self._lock:
|
||||||
try:
|
if not self._start_process(cmd, close_fds=True):
|
||||||
self._process = subprocess.Popen(self._cmd, close_fds=True)
|
|
||||||
except Exception:
|
|
||||||
logger.exception('Failed to execute %s', self._cmd)
|
|
||||||
continue
|
continue
|
||||||
self._process.wait()
|
self._process.wait()
|
||||||
|
self._kill_children()
|
||||||
|
|||||||
@@ -1,36 +1,92 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import psutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from patroni.exceptions import PostgresException
|
from patroni.exceptions import PostgresException
|
||||||
from patroni.utils import polling_loop
|
from patroni.utils import polling_loop
|
||||||
from six import string_types
|
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class CancellableSubprocess(object):
|
class CancellableExecutor(object):
|
||||||
|
|
||||||
|
"""
|
||||||
|
There must be only one such process so that AsyncExecutor can easily cancel it.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._is_cancelled = False
|
|
||||||
self._process = None
|
self._process = None
|
||||||
|
self._process_cmd = None
|
||||||
|
self._process_children = []
|
||||||
self._lock = Lock()
|
self._lock = Lock()
|
||||||
|
|
||||||
|
def _start_process(self, cmd, *args, **kwargs):
|
||||||
|
"""This method must be executed only when the `_lock` is acquired"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._process_children = []
|
||||||
|
self._process_cmd = cmd
|
||||||
|
self._process = psutil.Popen(cmd, *args, **kwargs)
|
||||||
|
except Exception:
|
||||||
|
return logger.exception('Failed to execute %s', cmd)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _kill_process(self):
|
||||||
|
with self._lock:
|
||||||
|
if self._process is not None and self._process.is_running() and not self._process_children:
|
||||||
|
try:
|
||||||
|
self._process.suspend() # Suspend the process before getting list of children
|
||||||
|
except psutil.Error as e:
|
||||||
|
logger.info('Failed to suspend the process: %s', e.msg)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._process_children = self._process.children(recursive=True)
|
||||||
|
except psutil.Error:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._process.kill()
|
||||||
|
logger.warning('Killed %s because it was still running', self._process_cmd)
|
||||||
|
except psutil.NoSuchProcess:
|
||||||
|
pass
|
||||||
|
except psutil.AccessDenied as e:
|
||||||
|
logger.warning('Failed to kill the process: %s', e.msg)
|
||||||
|
|
||||||
|
def _kill_children(self):
|
||||||
|
waitlist = []
|
||||||
|
with self._lock:
|
||||||
|
for child in self._process_children:
|
||||||
|
try:
|
||||||
|
child.kill()
|
||||||
|
except psutil.NoSuchProcess:
|
||||||
|
continue
|
||||||
|
except psutil.AccessDenied as e:
|
||||||
|
logger.info('Failed to kill child process: %s', e.msg)
|
||||||
|
waitlist.append(child)
|
||||||
|
psutil.wait_procs(waitlist)
|
||||||
|
|
||||||
|
|
||||||
|
class CancellableSubprocess(CancellableExecutor):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(CancellableSubprocess, self).__init__()
|
||||||
|
self._is_cancelled = False
|
||||||
|
|
||||||
def call(self, *args, **kwargs):
|
def call(self, *args, **kwargs):
|
||||||
for s in ('stdin', 'stdout', 'stderr'):
|
for s in ('stdin', 'stdout', 'stderr'):
|
||||||
kwargs.pop(s, None)
|
kwargs.pop(s, None)
|
||||||
|
|
||||||
communicate_input = 'communicate_input' in kwargs
|
communicate = kwargs.pop('communicate', None)
|
||||||
if communicate_input:
|
if isinstance(communicate, dict):
|
||||||
input_data = kwargs.pop('communicate_input', None)
|
input_data = communicate.get('input')
|
||||||
if not isinstance(input_data, string_types):
|
if input_data:
|
||||||
input_data = ''
|
if input_data[-1] != '\n':
|
||||||
if input_data and input_data[-1] != '\n':
|
|
||||||
input_data += '\n'
|
input_data += '\n'
|
||||||
|
input_data = input_data.encode('utf-8')
|
||||||
kwargs['stdin'] = subprocess.PIPE
|
kwargs['stdin'] = subprocess.PIPE
|
||||||
kwargs['stdout'] = open(os.devnull, 'w')
|
kwargs['stdout'] = subprocess.PIPE
|
||||||
kwargs['stderr'] = subprocess.STDOUT
|
kwargs['stderr'] = subprocess.PIPE
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
@@ -38,17 +94,16 @@ class CancellableSubprocess(object):
|
|||||||
raise PostgresException('cancelled')
|
raise PostgresException('cancelled')
|
||||||
|
|
||||||
self._is_cancelled = False
|
self._is_cancelled = False
|
||||||
self._process = subprocess.Popen(*args, **kwargs)
|
started = self._start_process(*args, **kwargs)
|
||||||
|
|
||||||
if communicate_input:
|
|
||||||
if input_data:
|
|
||||||
self._process.communicate(input_data)
|
|
||||||
self._process.stdin.close()
|
|
||||||
|
|
||||||
|
if started:
|
||||||
|
if isinstance(communicate, dict):
|
||||||
|
communicate['stdout'], communicate['stderr'] = self._process.communicate(input_data)
|
||||||
return self._process.wait()
|
return self._process.wait()
|
||||||
finally:
|
finally:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
self._process = None
|
self._process = None
|
||||||
|
self._kill_children()
|
||||||
|
|
||||||
def reset_is_cancelled(self):
|
def reset_is_cancelled(self):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
@@ -59,18 +114,20 @@ class CancellableSubprocess(object):
|
|||||||
with self._lock:
|
with self._lock:
|
||||||
return self._is_cancelled
|
return self._is_cancelled
|
||||||
|
|
||||||
def cancel(self):
|
def cancel(self, kill=False):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
self._is_cancelled = True
|
self._is_cancelled = True
|
||||||
if self._process is None or self._process.returncode is not None:
|
if self._process is None or not self._process.is_running():
|
||||||
return
|
return
|
||||||
|
|
||||||
|
logger.info('Terminating %s', self._process_cmd)
|
||||||
self._process.terminate()
|
self._process.terminate()
|
||||||
|
|
||||||
for _ in polling_loop(10):
|
for _ in polling_loop(10):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if self._process is None or self._process.returncode is not None:
|
if self._process is None or not self._process.is_running():
|
||||||
return
|
return
|
||||||
|
if kill:
|
||||||
|
break
|
||||||
|
|
||||||
with self._lock:
|
self._kill_process()
|
||||||
if self._process is not None and self._process.returncode is None:
|
|
||||||
self._process.kill()
|
|
||||||
|
|||||||
+580
-197
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,10 @@
|
|||||||
import logging
|
import logging
|
||||||
import psycopg2
|
|
||||||
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
|
from .. import psycopg
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ class Connection(object):
|
|||||||
def get(self):
|
def get(self):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if not self._connection or self._connection.closed != 0:
|
if not self._connection or self._connection.closed != 0:
|
||||||
self._connection = psycopg2.connect(**self._conn_kwargs)
|
self._connection = psycopg.connect(**self._conn_kwargs)
|
||||||
self._connection.autocommit = True
|
self._connection.autocommit = True
|
||||||
self.server_version = self._connection.server_version
|
self.server_version = self._connection.server_version
|
||||||
return self._connection
|
return self._connection
|
||||||
@@ -40,7 +41,8 @@ class Connection(object):
|
|||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def get_connection_cursor(**kwargs):
|
def get_connection_cursor(**kwargs):
|
||||||
with psycopg2.connect(**kwargs) as conn:
|
conn = psycopg.connect(**kwargs)
|
||||||
conn.autocommit = True
|
conn.autocommit = True
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
yield cur
|
yield cur
|
||||||
|
conn.close()
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ def postgres_version_to_int(pg_version):
|
|||||||
raise PostgresException('Invalid PostgreSQL version format: X.Y or X.Y.Z is accepted: {0}'.format(pg_version))
|
raise PostgresException('Invalid PostgreSQL version format: X.Y or X.Y.Z is accepted: {0}'.format(pg_version))
|
||||||
|
|
||||||
if len(components) == 2:
|
if len(components) == 2:
|
||||||
# new style verion numbers, i.e. 10.1 becomes 100001
|
# new style version numbers, i.e. 10.1 becomes 100001
|
||||||
components.insert(1, 0)
|
components.insert(1, 0)
|
||||||
|
|
||||||
return int(''.join('{0:02d}'.format(c) for c in components))
|
return int(''.join('{0:02d}'.format(c) for c in components))
|
||||||
@@ -68,3 +68,8 @@ def parse_history(data):
|
|||||||
yield values
|
yield values
|
||||||
except (IndexError, ValueError):
|
except (IndexError, ValueError):
|
||||||
logger.exception('Exception when parsing timeline history line "%s"', values)
|
logger.exception('Exception when parsing timeline history line "%s"', values)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|||||||
@@ -5,13 +5,24 @@ import psutil
|
|||||||
import re
|
import re
|
||||||
import signal
|
import signal
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from patroni import PATRONI_ENV_PREFIX, KUBERNETES_ENV_PREFIX
|
||||||
|
|
||||||
|
# avoid spawning the resource tracker process
|
||||||
|
if sys.version_info >= (3, 8): # pragma: no cover
|
||||||
|
import multiprocessing.resource_tracker
|
||||||
|
multiprocessing.resource_tracker.getfd = lambda: 0
|
||||||
|
elif sys.version_info >= (3, 4): # pragma: no cover
|
||||||
|
import multiprocessing.semaphore_tracker
|
||||||
|
multiprocessing.semaphore_tracker.getfd = lambda: 0
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
STOP_SIGNALS = {
|
STOP_SIGNALS = {
|
||||||
'smart': signal.SIGTERM,
|
'smart': 'TERM',
|
||||||
'fast': signal.SIGINT,
|
'fast': 'INT',
|
||||||
'immediate': signal.SIGQUIT if os.name != 'nt' else signal.SIGABRT,
|
'immediate': 'QUIT',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -94,7 +105,43 @@ class PostmasterProcess(psutil.Process):
|
|||||||
except psutil.NoSuchProcess:
|
except psutil.NoSuchProcess:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def signal_stop(self, mode):
|
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
|
"""Signal postmaster process to stop
|
||||||
|
|
||||||
:returns None if signaled, True if process is already gone, False if error
|
:returns None if signaled, True if process is already gone, False if error
|
||||||
@@ -102,8 +149,10 @@ class PostmasterProcess(psutil.Process):
|
|||||||
if self.is_single_user:
|
if self.is_single_user:
|
||||||
logger.warning("Cannot stop server; single-user server is running (PID: {0})".format(self.pid))
|
logger.warning("Cannot stop server; single-user server is running (PID: {0})".format(self.pid))
|
||||||
return False
|
return False
|
||||||
|
if os.name != 'posix':
|
||||||
|
return self.pg_ctl_kill(mode, pg_ctl)
|
||||||
try:
|
try:
|
||||||
self.send_signal(STOP_SIGNALS[mode])
|
self.send_signal(getattr(signal, 'SIG' + STOP_SIGNALS[mode]))
|
||||||
except psutil.NoSuchProcess:
|
except psutil.NoSuchProcess:
|
||||||
return True
|
return True
|
||||||
except psutil.AccessDenied as e:
|
except psutil.AccessDenied as e:
|
||||||
@@ -112,8 +161,18 @@ class PostmasterProcess(psutil.Process):
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def wait_for_user_backends_to_close(self):
|
def pg_ctl_kill(self, mode, pg_ctl):
|
||||||
# These regexps are cross checked against versions PostgreSQL 9.1 .. 11
|
try:
|
||||||
|
status = subprocess.call([pg_ctl, "kill", STOP_SIGNALS[mode], str(self.pid)])
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
if status == 0:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return not self.is_running()
|
||||||
|
|
||||||
|
def wait_for_user_backends_to_close(self, stop_timeout):
|
||||||
|
# These regexps are cross checked against versions PostgreSQL 9.1 .. 15
|
||||||
aux_proc_re = re.compile("(?:postgres:)( .*:)? (?:(?:archiver|startup|autovacuum launcher|autovacuum worker|"
|
aux_proc_re = re.compile("(?:postgres:)( .*:)? (?:(?:archiver|startup|autovacuum launcher|autovacuum worker|"
|
||||||
"checkpointer|logger|stats collector|wal receiver|wal writer|writer)(?: process )?|"
|
"checkpointer|logger|stats collector|wal receiver|wal writer|writer)(?: process )?|"
|
||||||
"walreceiver|wal sender process|walsender|walwriter|background writer|"
|
"walreceiver|wal sender process|walsender|walwriter|background writer|"
|
||||||
@@ -125,18 +184,22 @@ class PostmasterProcess(psutil.Process):
|
|||||||
return logger.debug('Failed to get list of postmaster children')
|
return logger.debug('Failed to get list of postmaster children')
|
||||||
|
|
||||||
user_backends = []
|
user_backends = []
|
||||||
user_backends_cmdlines = []
|
user_backends_cmdlines = {}
|
||||||
for child in children:
|
for child in children:
|
||||||
try:
|
try:
|
||||||
cmdline = child.cmdline()[0]
|
cmdline = child.cmdline()
|
||||||
if not aux_proc_re.match(cmdline):
|
if cmdline and not aux_proc_re.match(cmdline[0]):
|
||||||
user_backends.append(child)
|
user_backends.append(child)
|
||||||
user_backends_cmdlines.append(cmdline)
|
user_backends_cmdlines[child.pid] = cmdline[0]
|
||||||
except psutil.NoSuchProcess:
|
except psutil.NoSuchProcess:
|
||||||
pass
|
pass
|
||||||
if user_backends:
|
if user_backends:
|
||||||
logger.debug('Waiting for user backends %s to close', ', '.join(user_backends_cmdlines))
|
logger.debug('Waiting for user backends %s to close', ', '.join(user_backends_cmdlines.values()))
|
||||||
psutil.wait_procs(user_backends)
|
gone, live = psutil.wait_procs(user_backends, stop_timeout)
|
||||||
|
if stop_timeout and live:
|
||||||
|
live = [user_backends_cmdlines[b.pid] for b in live]
|
||||||
|
logger.warning('Backends still alive after %s: %s', stop_timeout, ', '.join(live))
|
||||||
|
else:
|
||||||
logger.debug("Backends closed")
|
logger.debug("Backends closed")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -153,7 +216,8 @@ class PostmasterProcess(psutil.Process):
|
|||||||
# In order to make everything portable we can't use fork&exec approach here, so we will call
|
# 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.
|
# 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.
|
# 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 ('PATH', 'LD_LIBRARY_PATH', 'LC_ALL', 'LANG', 'SYSTEMROOT') if p in os.environ}
|
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:
|
try:
|
||||||
proc = PostmasterProcess._from_pidfile(data_dir)
|
proc = PostmasterProcess._from_pidfile(data_dir)
|
||||||
if proc and not proc._is_postmaster_process():
|
if proc and not proc._is_postmaster_process():
|
||||||
@@ -170,8 +234,9 @@ class PostmasterProcess(psutil.Process):
|
|||||||
pass
|
pass
|
||||||
cmdline = [pgcommand, '-D', data_dir, '--config-file={}'.format(conf)] + options
|
cmdline = [pgcommand, '-D', data_dir, '--config-file={}'.format(conf)] + options
|
||||||
logger.debug("Starting postgres: %s", " ".join(cmdline))
|
logger.debug("Starting postgres: %s", " ".join(cmdline))
|
||||||
parent_conn, child_conn = multiprocessing.Pipe(False)
|
ctx = multiprocessing.get_context('spawn') if sys.version_info >= (3, 4) else multiprocessing
|
||||||
proc = multiprocessing.Process(target=pg_ctl_start, args=(child_conn, cmdline, env))
|
parent_conn, child_conn = ctx.Pipe(False)
|
||||||
|
proc = ctx.Process(target=pg_ctl_start, args=(child_conn, cmdline, env))
|
||||||
proc.start()
|
proc.start()
|
||||||
pid = parent_conn.recv()
|
pid = parent_conn.recv()
|
||||||
proc.join()
|
proc.join()
|
||||||
|
|||||||
+374
-61
@@ -1,10 +1,17 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
import shlex
|
||||||
|
import shutil
|
||||||
|
import six
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from patroni.dcs import Leader
|
from threading import Lock, Thread
|
||||||
from patroni.postgresql.connection import get_connection_cursor
|
|
||||||
from patroni.postgresql.misc import parse_history, parse_lsn
|
from .connection import get_connection_cursor
|
||||||
|
from .misc import format_lsn, parse_history, parse_lsn
|
||||||
|
from ..async_executor import CriticalTask
|
||||||
|
from ..dcs import Leader
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -16,19 +23,24 @@ class Rewind(object):
|
|||||||
|
|
||||||
def __init__(self, postgresql):
|
def __init__(self, postgresql):
|
||||||
self._postgresql = postgresql
|
self._postgresql = postgresql
|
||||||
|
self._checkpoint_task_lock = Lock()
|
||||||
self.reset_state()
|
self.reset_state()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def configuration_allows_rewind(data):
|
def configuration_allows_rewind(data):
|
||||||
return data.get('wal_log_hints setting', 'off') == 'on' or data.get('Data page checksum version', '0') != '0'
|
return data.get('wal_log_hints setting', 'off') == 'on' or data.get('Data page checksum version', '0') != '0'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def enabled(self):
|
||||||
|
return self._postgresql.config.get('use_pg_rewind')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def can_rewind(self):
|
def can_rewind(self):
|
||||||
""" check if pg_rewind executable is there and that pg_controldata indicates
|
""" check if pg_rewind executable is there and that pg_controldata indicates
|
||||||
we have either wal_log_hints or checksums turned on
|
we have either wal_log_hints or checksums turned on
|
||||||
"""
|
"""
|
||||||
# low-hanging fruit: check if pg_rewind configuration is there
|
# low-hanging fruit: check if pg_rewind configuration is there
|
||||||
if not self._postgresql.config.get('use_pg_rewind'):
|
if not self.enabled:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
cmd = [self._postgresql.pgcommand('pg_rewind'), '--help']
|
cmd = [self._postgresql.pgcommand('pg_rewind'), '--help']
|
||||||
@@ -40,19 +52,22 @@ class Rewind(object):
|
|||||||
return False
|
return False
|
||||||
return self.configuration_allows_rewind(self._postgresql.controldata())
|
return self.configuration_allows_rewind(self._postgresql.controldata())
|
||||||
|
|
||||||
|
@property
|
||||||
|
def should_remove_data_directory_on_diverged_timelines(self):
|
||||||
|
return self._postgresql.config.get('remove_data_directory_on_diverged_timelines')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def can_rewind_or_reinitialize_allowed(self):
|
def can_rewind_or_reinitialize_allowed(self):
|
||||||
return self._postgresql.config.get('remove_data_directory_on_diverged_timelines') or self.can_rewind
|
return self.should_remove_data_directory_on_diverged_timelines or self.can_rewind
|
||||||
|
|
||||||
def trigger_check_diverged_lsn(self):
|
def trigger_check_diverged_lsn(self):
|
||||||
if self.can_rewind_or_reinitialize_allowed and self._state != REWIND_STATUS.NEED:
|
if self.can_rewind_or_reinitialize_allowed and self._state != REWIND_STATUS.NEED:
|
||||||
self._state = REWIND_STATUS.CHECK
|
self._state = REWIND_STATUS.CHECK
|
||||||
|
|
||||||
def check_leader_is_not_in_recovery(self, **kwargs):
|
@staticmethod
|
||||||
if not kwargs.get('database'):
|
def check_leader_is_not_in_recovery(conn_kwargs):
|
||||||
kwargs['database'] = self._postgresql.database
|
|
||||||
try:
|
try:
|
||||||
with get_connection_cursor(connect_timeout=3, options='-c statement_timeout=2000', **kwargs) as cur:
|
with get_connection_cursor(connect_timeout=3, options='-c statement_timeout=2000', **conn_kwargs) as cur:
|
||||||
cur.execute('SELECT pg_catalog.pg_is_in_recovery()')
|
cur.execute('SELECT pg_catalog.pg_is_in_recovery()')
|
||||||
if not cur.fetchone()[0]:
|
if not cur.fetchone()[0]:
|
||||||
return True
|
return True
|
||||||
@@ -60,40 +75,131 @@ class Rewind(object):
|
|||||||
except Exception:
|
except Exception:
|
||||||
return logger.exception('Exception when working with leader')
|
return logger.exception('Exception when working with leader')
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def check_leader_has_run_checkpoint(conn_kwargs):
|
||||||
|
try:
|
||||||
|
with get_connection_cursor(connect_timeout=3, options='-c statement_timeout=2000', **conn_kwargs) as cur:
|
||||||
|
cur.execute("SELECT NOT pg_catalog.pg_is_in_recovery()" +
|
||||||
|
" AND ('x' || pg_catalog.substr(pg_catalog.pg_walfile_name(" +
|
||||||
|
" pg_catalog.pg_current_wal_lsn()), 1, 8))::bit(32)::int = timeline_id" +
|
||||||
|
" FROM pg_catalog.pg_control_checkpoint()")
|
||||||
|
if not cur.fetchone()[0]:
|
||||||
|
return 'leader has not run a checkpoint yet'
|
||||||
|
except Exception:
|
||||||
|
logger.exception('Exception when working with leader')
|
||||||
|
return 'not accessible or not healty'
|
||||||
|
|
||||||
|
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 will 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."""
|
||||||
|
|
||||||
|
lsn8 = format_lsn(lsn, True)
|
||||||
|
lsn = format_lsn(lsn)
|
||||||
|
out, err = self._postgresql.waldump(timeline, lsn, 2)
|
||||||
|
if out is not None and err is not None:
|
||||||
|
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 pg_%sdump output', self._postgresql.wal_name)
|
||||||
|
logger.error(' stdout=%s', '\n'.join(out))
|
||||||
|
logger.error(' stderr=%s', '\n'.join(err))
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def _get_local_timeline_lsn_from_controldata(self):
|
def _get_local_timeline_lsn_from_controldata(self):
|
||||||
timeline = lsn = None
|
in_recovery = timeline = lsn = None
|
||||||
data = self._postgresql.controldata()
|
data = self._postgresql.controldata()
|
||||||
try:
|
try:
|
||||||
if data.get('Database cluster state') == 'shut down in recovery':
|
if data.get('Database cluster state') in ('shut down in recovery', 'in archive recovery'):
|
||||||
|
in_recovery = True
|
||||||
lsn = data.get('Minimum recovery ending location')
|
lsn = data.get('Minimum recovery ending location')
|
||||||
timeline = int(data.get("Min recovery ending loc's timeline"))
|
timeline = int(data.get("Min recovery ending loc's timeline"))
|
||||||
if lsn == '0/0' or timeline == 0: # it was a master when it crashed
|
if lsn == '0/0' or timeline == 0: # it was a master when it crashed
|
||||||
data['Database cluster state'] = 'shut down'
|
data['Database cluster state'] = 'shut down'
|
||||||
if data.get('Database cluster state') == 'shut down':
|
if data.get('Database cluster state') == 'shut down':
|
||||||
|
in_recovery = False
|
||||||
lsn = data.get('Latest checkpoint location')
|
lsn = data.get('Latest checkpoint location')
|
||||||
timeline = int(data.get("Latest checkpoint's TimeLineID"))
|
timeline = int(data.get("Latest checkpoint's TimeLineID"))
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
logger.exception('Failed to get local timeline and lsn from pg_controldata output')
|
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):
|
def _get_local_timeline_lsn(self):
|
||||||
if self._postgresql.is_running(): # if postgres is running - get timeline and lsn from replication connection
|
if self._postgresql.is_running(): # if postgres is running - get timeline from replication connection
|
||||||
timeline, lsn = self._postgresql.get_local_timeline_lsn_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
|
else: # otherwise analyze pg_controldata output
|
||||||
timeline, lsn = self._get_local_timeline_lsn_from_controldata()
|
in_recovery, timeline, lsn = self._get_local_timeline_lsn_from_controldata()
|
||||||
logger.info('Local timeline=%s lsn=%s', timeline, lsn)
|
|
||||||
return timeline, lsn
|
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 _conn_kwargs(self, member, auth):
|
||||||
|
ret = member.conn_kwargs(auth)
|
||||||
|
if not ret.get('dbname'):
|
||||||
|
ret['dbname'] = self._postgresql.database
|
||||||
|
# Add target_session_attrs in case more than one hostname is specified
|
||||||
|
# (libpq client-side failover) making sure we hit the primary
|
||||||
|
if 'target_session_attrs' not in ret and self._postgresql.major_version >= 100000:
|
||||||
|
ret['target_session_attrs'] = 'read-write'
|
||||||
|
return ret
|
||||||
|
|
||||||
def _check_timeline_and_lsn(self, leader):
|
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:
|
if local_timeline is None or local_lsn is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(leader, Leader):
|
if isinstance(leader, Leader) and leader.member.data.get('role') != 'master':
|
||||||
if leader.member.data.get('role') != 'master':
|
|
||||||
return
|
return
|
||||||
# standby cluster
|
|
||||||
elif not self.check_leader_is_not_in_recovery(**leader.conn_kwargs(self._postgresql.config.replication)):
|
# We want to use replication credentials when connecting to the "postgres" database in case if
|
||||||
|
# `use_pg_rewind` isn't enabled and only `remove_data_directory_on_diverged_timelines` is set
|
||||||
|
# for Postgresql older than v11 (where Patroni can't use a dedicated user for rewind).
|
||||||
|
# In all other cases we will use rewind or superuser credentials.
|
||||||
|
check_credentials = self._postgresql.config.replication if not self.enabled and\
|
||||||
|
self.should_remove_data_directory_on_diverged_timelines and\
|
||||||
|
self._postgresql.major_version < 110000 else self._postgresql.config.rewind_credentials
|
||||||
|
if not self.check_leader_is_not_in_recovery(self._conn_kwargs(leader, check_credentials)):
|
||||||
return
|
return
|
||||||
|
|
||||||
history = need_rewind = None
|
history = need_rewind = None
|
||||||
@@ -104,25 +210,37 @@ class Rewind(object):
|
|||||||
logger.info('master_timeline=%s', master_timeline)
|
logger.info('master_timeline=%s', master_timeline)
|
||||||
if local_timeline > master_timeline: # Not always supported by pg_rewind
|
if local_timeline > master_timeline: # Not always supported by pg_rewind
|
||||||
need_rewind = True
|
need_rewind = True
|
||||||
elif master_timeline > 1:
|
elif local_timeline == master_timeline:
|
||||||
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
|
need_rewind = False
|
||||||
|
elif master_timeline > 1:
|
||||||
|
cur.execute('TIMELINE_HISTORY {0}'.format(master_timeline))
|
||||||
|
history = cur.fetchone()[1]
|
||||||
|
if not isinstance(history, six.string_types):
|
||||||
|
history = bytes(history).decode('utf-8')
|
||||||
|
logger.debug('master: history=%s', history)
|
||||||
except Exception:
|
except Exception:
|
||||||
return logger.exception('Exception when working with master via replication connection')
|
return logger.exception('Exception when working with master via replication connection')
|
||||||
|
|
||||||
if history is not None:
|
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:
|
if parent_timeline == local_timeline:
|
||||||
try:
|
# We don't need to rewind when:
|
||||||
need_rewind = parse_lsn(local_lsn) >= switchpoint
|
# 1. for replica: replayed location is not ahead of switchpoint
|
||||||
except (IndexError, ValueError):
|
# 2. for the former primary: end of checkpoint record is the same as switchpoint
|
||||||
logger.exception('Exception when parsing lsn')
|
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
|
break
|
||||||
elif parent_timeline > local_timeline:
|
elif parent_timeline > local_timeline:
|
||||||
|
need_rewind = True
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
need_rewind = True
|
||||||
|
self._log_master_history(history, i)
|
||||||
|
|
||||||
self._state = need_rewind and REWIND_STATUS.NEED or REWIND_STATUS.NOT_NEED
|
self._state = need_rewind and REWIND_STATUS.NEED or REWIND_STATUS.NOT_NEED
|
||||||
|
|
||||||
@@ -131,78 +249,219 @@ class Rewind(object):
|
|||||||
self._check_timeline_and_lsn(leader)
|
self._check_timeline_and_lsn(leader)
|
||||||
return leader and leader.conn_url and self._state == REWIND_STATUS.NEED
|
return leader and leader.conn_url and self._state == REWIND_STATUS.NEED
|
||||||
|
|
||||||
def check_for_checkpoint_after_promote(self):
|
def __checkpoint(self, task, wakeup):
|
||||||
if self._state == REWIND_STATUS.INITIAL and self._postgresql.is_leader():
|
|
||||||
try:
|
try:
|
||||||
timeline = int(self._postgresql.controldata().get("Latest checkpoint's TimeLineID"))
|
result = self._postgresql.checkpoint()
|
||||||
if self._postgresql.get_master_timeline() == timeline:
|
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 is not None:
|
||||||
self._state = REWIND_STATUS.CHECKPOINT
|
self._state = REWIND_STATUS.CHECKPOINT
|
||||||
except (TypeError, ValueError):
|
self._checkpoint_task = None
|
||||||
logger.exception('Failed to parse timeline from pg_controldata output')
|
elif self._postgresql.get_master_timeline() == self._postgresql.pg_control_timeline():
|
||||||
|
self._state = REWIND_STATUS.CHECKPOINT
|
||||||
|
else:
|
||||||
|
self._checkpoint_task = CriticalTask()
|
||||||
|
Thread(target=self.__checkpoint, args=(self._checkpoint_task, wakeup)).start()
|
||||||
|
|
||||||
def checkpoint_after_promote(self):
|
def checkpoint_after_promote(self):
|
||||||
return self._state == REWIND_STATUS.CHECKPOINT
|
return self._state == REWIND_STATUS.CHECKPOINT
|
||||||
|
|
||||||
|
def _buid_archiver_command(self, command, wal_filename):
|
||||||
|
"""Replace placeholders in the given archiver command's template.
|
||||||
|
Applicable for archive_command and restore_command.
|
||||||
|
Can also be used for archive_cleanup_command and recovery_end_command,
|
||||||
|
however %r value is always set to 000000010000000000000001."""
|
||||||
|
cmd = ''
|
||||||
|
length = len(command)
|
||||||
|
i = 0
|
||||||
|
while i < length:
|
||||||
|
if command[i] == '%' and i + 1 < length:
|
||||||
|
i += 1
|
||||||
|
if command[i] == 'p':
|
||||||
|
cmd += os.path.join(self._postgresql.wal_dir, wal_filename)
|
||||||
|
elif command[i] == 'f':
|
||||||
|
cmd += wal_filename
|
||||||
|
elif command[i] == 'r':
|
||||||
|
cmd += '000000010000000000000001'
|
||||||
|
elif command[i] == '%':
|
||||||
|
cmd += '%'
|
||||||
|
else:
|
||||||
|
cmd += '%'
|
||||||
|
i -= 1
|
||||||
|
else:
|
||||||
|
cmd += command[i]
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
|
||||||
|
def _fetch_missing_wal(self, restore_command, wal_filename):
|
||||||
|
cmd = self._buid_archiver_command(restore_command, wal_filename)
|
||||||
|
|
||||||
|
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 _archive_ready_wals(self):
|
||||||
|
"""Try to archive WALs that have .ready files just in case
|
||||||
|
archive_mode was not set to 'always' before promote, while
|
||||||
|
after it the WALs were recycled on the promoted replica.
|
||||||
|
With this we prevent the entire loss of such WALs and the
|
||||||
|
consequent old leader's start failure."""
|
||||||
|
archive_mode = self._postgresql.get_guc_value('archive_mode')
|
||||||
|
archive_cmd = self._postgresql.get_guc_value('archive_command')
|
||||||
|
if archive_mode not in ('on', 'always') or not archive_cmd:
|
||||||
|
return
|
||||||
|
|
||||||
|
walseg_regex = re.compile(r'^[0-9A-F]{24}(\.partial){0,1}\.ready$')
|
||||||
|
status_dir = os.path.join(self._postgresql.wal_dir, 'archive_status')
|
||||||
|
try:
|
||||||
|
wals_to_archive = [f[:-6] for f in os.listdir(status_dir) if walseg_regex.match(f)]
|
||||||
|
except OSError as e:
|
||||||
|
return logger.error('Unable to list %s: %r', status_dir, e)
|
||||||
|
|
||||||
|
# skip fsync, as postgres --single or pg_rewind will anyway run it
|
||||||
|
for wal in sorted(wals_to_archive):
|
||||||
|
old_name = os.path.join(status_dir, wal + '.ready')
|
||||||
|
# wal file might have alredy been archived
|
||||||
|
if os.path.isfile(old_name) and os.path.isfile(os.path.join(self._postgresql.wal_dir, wal)):
|
||||||
|
cmd = self._buid_archiver_command(archive_cmd, wal)
|
||||||
|
# it is the author of archive_command, who is responsible
|
||||||
|
# for not overriding the WALs already present in archive
|
||||||
|
logger.info('Trying to archive %s: %s', wal, cmd)
|
||||||
|
if self._postgresql.cancellable.call(shlex.split(cmd)) == 0:
|
||||||
|
new_name = os.path.join(status_dir, wal + '.done')
|
||||||
|
try:
|
||||||
|
shutil.move(old_name, new_name)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Unable to rename %s to %s: %r', old_name, new_name, e)
|
||||||
|
else:
|
||||||
|
logger.info('Failed to archive WAL segment %s', wal)
|
||||||
|
|
||||||
def pg_rewind(self, r):
|
def pg_rewind(self, r):
|
||||||
# prepare pg_rewind connection
|
# prepare pg_rewind connection
|
||||||
env = self._postgresql.write_pgpass(r)
|
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_attrs = [
|
dsn = self._postgresql.config.format_dsn(r, True)
|
||||||
('user', r.get('user')),
|
|
||||||
('host', r.get('host')),
|
|
||||||
('port', r.get('port')),
|
|
||||||
('dbname', r.get('database') or self._postgresql.database),
|
|
||||||
('sslmode', 'prefer'),
|
|
||||||
('sslcompression', '1'),
|
|
||||||
]
|
|
||||||
dsn = " ".join("{0}={1}".format(k, v) for k, v in dsn_attrs if v is not None)
|
|
||||||
logger.info('running pg_rewind from %s', dsn)
|
logger.info('running pg_rewind from %s', dsn)
|
||||||
try:
|
|
||||||
return self._postgresql.cancellable.call([self._postgresql.pgcommand('pg_rewind'), '-D',
|
restore_command = self._postgresql.config.get('recovery_conf', {}).get('restore_command') \
|
||||||
self._postgresql.data_dir, '--source-server', dsn], env=env) == 0
|
if self._postgresql.major_version < 120000 else self._postgresql.get_guc_value('restore_command')
|
||||||
except OSError:
|
|
||||||
|
# Until v15 pg_rewind expected postgresql.conf to be inside $PGDATA, which is not the case on e.g. Debian
|
||||||
|
pg_rewind_can_restore = restore_command and (self._postgresql.major_version >= 150000 or
|
||||||
|
(self._postgresql.major_version >= 130000 and
|
||||||
|
self._postgresql.config._config_dir == self._postgresql.data_dir))
|
||||||
|
|
||||||
|
cmd = [self._postgresql.pgcommand('pg_rewind')]
|
||||||
|
if pg_rewind_can_restore:
|
||||||
|
cmd.append('--restore-target-wal')
|
||||||
|
if self._postgresql.major_version >= 150000 and\
|
||||||
|
self._postgresql.config._config_dir != self._postgresql.data_dir:
|
||||||
|
cmd.append('--config-file={0}'.format(self._postgresql.config.postgresql_conf))
|
||||||
|
|
||||||
|
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 pg_rewind_can_restore:
|
||||||
|
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
|
return False
|
||||||
|
|
||||||
def execute(self, leader):
|
def execute(self, leader):
|
||||||
if self._postgresql.is_running() and not self._postgresql.stop(checkpoint=False):
|
if self._postgresql.is_running() and not self._postgresql.stop(checkpoint=False):
|
||||||
return logger.warning('Can not run pg_rewind because postgres is still running')
|
return logger.warning('Can not run pg_rewind because postgres is still running')
|
||||||
|
|
||||||
|
self._archive_ready_wals()
|
||||||
|
|
||||||
# prepare pg_rewind connection
|
# prepare pg_rewind connection
|
||||||
r = leader.conn_kwargs(self._postgresql.config.rewind_credentials)
|
r = self._conn_kwargs(leader, self._postgresql.config.rewind_credentials)
|
||||||
|
|
||||||
# 1. make sure that we are really trying to rewind from the master
|
# 1. make sure that we are really trying to rewind from the master
|
||||||
# 2. make sure that pg_control contains the new timeline by:
|
# 2. make sure that pg_control contains the new timeline by:
|
||||||
# running a checkpoint or
|
# running a checkpoint or
|
||||||
# waiting until Patroni on the master will expose checkpoint_after_promote=True
|
# waiting until Patroni on the master will expose checkpoint_after_promote=True
|
||||||
checkpoint_status = leader.checkpoint_after_promote if isinstance(leader, Leader) else None
|
checkpoint_status = leader.checkpoint_after_promote if isinstance(leader, Leader) else None
|
||||||
if checkpoint_status is None: # master still runs the old Patroni
|
if checkpoint_status is None: # we are the standby-cluster leader or master still runs the old Patroni
|
||||||
leader_status = self._postgresql.checkpoint(leader.conn_kwargs(self._postgresql.config.superuser))
|
# superuser credentials match rewind_credentials if the latter are not provided or we run 10 or older
|
||||||
if leader_status:
|
if self._postgresql.config.superuser == self._postgresql.config.rewind_credentials:
|
||||||
|
leader_status = self._postgresql.checkpoint(
|
||||||
|
self._conn_kwargs(leader, self._postgresql.config.superuser))
|
||||||
|
else: # we run 11+ and have a dedicated pg_rewind user
|
||||||
|
leader_status = self.check_leader_has_run_checkpoint(r)
|
||||||
|
if leader_status: # we tried to run/check for a checkpoint on the remote leader, but it failed
|
||||||
return logger.warning('Can not use %s for rewind: %s', leader.name, leader_status)
|
return logger.warning('Can not use %s for rewind: %s', leader.name, leader_status)
|
||||||
elif not checkpoint_status:
|
elif not checkpoint_status:
|
||||||
return logger.info('Waiting for checkpoint on %s before rewind', leader.name)
|
return logger.info('Waiting for checkpoint on %s before rewind', leader.name)
|
||||||
elif not self.check_leader_is_not_in_recovery(**r):
|
elif not self.check_leader_is_not_in_recovery(r):
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.pg_rewind(r):
|
if self.pg_rewind(r):
|
||||||
self._state = REWIND_STATUS.SUCCESS
|
self._state = REWIND_STATUS.SUCCESS
|
||||||
elif not self.check_leader_is_not_in_recovery(**r):
|
else:
|
||||||
|
if not self.check_leader_is_not_in_recovery(r):
|
||||||
logger.warning('Failed to rewind because master %s become unreachable', leader.name)
|
logger.warning('Failed to rewind because master %s become unreachable', leader.name)
|
||||||
|
if not self.can_rewind: # It is possible that the previous attempt damaged pg_control file!
|
||||||
|
self._state = REWIND_STATUS.FAILED
|
||||||
else:
|
else:
|
||||||
logger.error('Failed to rewind from healty master: %s', leader.name)
|
logger.error('Failed to rewind from healty master: %s', leader.name)
|
||||||
|
self._state = REWIND_STATUS.FAILED
|
||||||
|
|
||||||
|
if self.failed:
|
||||||
for name in ('remove_data_directory_on_rewind_failure', 'remove_data_directory_on_diverged_timelines'):
|
for name in ('remove_data_directory_on_rewind_failure', 'remove_data_directory_on_diverged_timelines'):
|
||||||
if self._postgresql.config.get(name):
|
if self._postgresql.config.get(name):
|
||||||
logger.warning('%s is set. removing...', name)
|
logger.warning('%s is set. removing...', name)
|
||||||
self._postgresql.remove_data_directory()
|
self._postgresql.remove_data_directory()
|
||||||
self._state = REWIND_STATUS.INITIAL
|
self._state = REWIND_STATUS.INITIAL
|
||||||
break
|
break
|
||||||
else:
|
|
||||||
self._state = REWIND_STATUS.FAILED
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def reset_state(self):
|
def reset_state(self):
|
||||||
self._state = REWIND_STATUS.INITIAL
|
self._state = REWIND_STATUS.INITIAL
|
||||||
|
with self._checkpoint_task_lock:
|
||||||
|
self._checkpoint_task = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_needed(self):
|
def is_needed(self):
|
||||||
@@ -215,3 +474,57 @@ class Rewind(object):
|
|||||||
@property
|
@property
|
||||||
def failed(self):
|
def failed(self):
|
||||||
return self._state == REWIND_STATUS.FAILED
|
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._archive_ready_wals()
|
||||||
|
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()
|
||||||
|
output = {}
|
||||||
|
ret = self.single_user_mode(communicate=output, options=opts)
|
||||||
|
if ret != 0:
|
||||||
|
logger.error('Crash recovery finished with code=%s', ret)
|
||||||
|
logger.info(' stdout=%s', output['stdout'].decode('utf-8'))
|
||||||
|
logger.info(' stderr=%s', output['stderr'].decode('utf-8'))
|
||||||
|
return ret == 0 or None
|
||||||
|
|||||||
+350
-37
@@ -1,42 +1,195 @@
|
|||||||
|
import errno
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
from patroni.postgresql.connection import get_connection_cursor
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from threading import Condition, Thread
|
||||||
|
|
||||||
|
from .connection import get_connection_cursor
|
||||||
|
from .misc import format_lsn
|
||||||
|
from ..psycopg import OperationalError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def compare_slots(s1, s2):
|
def compare_slots(s1, s2, dbid='database'):
|
||||||
return s1['type'] == s2['type'] and (s1['type'] == 'physical' or
|
return s1['type'] == s2['type'] and (s1['type'] == 'physical' or
|
||||||
s1['database'] == s2['database'] and s1['plugin'] == s2['plugin'])
|
s1.get(dbid) == s2.get(dbid) and s1['plugin'] == s2['plugin'])
|
||||||
|
|
||||||
|
|
||||||
|
def fsync_dir(path):
|
||||||
|
if os.name != 'nt':
|
||||||
|
fd = os.open(path, os.O_DIRECTORY)
|
||||||
|
try:
|
||||||
|
os.fsync(fd)
|
||||||
|
except OSError as e:
|
||||||
|
# Some filesystems don't like fsyncing directories and raise EINVAL. Ignoring it is usually safe.
|
||||||
|
if e.errno != errno.EINVAL:
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
os.close(fd)
|
||||||
|
|
||||||
|
|
||||||
|
class SlotsAdvanceThread(Thread):
|
||||||
|
|
||||||
|
def __init__(self, slots_handler):
|
||||||
|
super(SlotsAdvanceThread, self).__init__()
|
||||||
|
self.daemon = True
|
||||||
|
self._slots_handler = slots_handler
|
||||||
|
|
||||||
|
# _copy_slots and _failed are used to asynchronously give some feedback to the main thread
|
||||||
|
self._copy_slots = []
|
||||||
|
self._failed = False
|
||||||
|
|
||||||
|
self._scheduled = defaultdict(dict) # {'dbname1': {'slot1': 100, 'slot2': 100}, 'dbname2': {'slot3': 100}}
|
||||||
|
self._condition = Condition() # protect self._scheduled from concurrent access and to wakeup the run() method
|
||||||
|
|
||||||
|
self.start()
|
||||||
|
|
||||||
|
def sync_slot(self, cur, database, slot, lsn):
|
||||||
|
failed = copy = False
|
||||||
|
try:
|
||||||
|
cur.execute("SELECT pg_catalog.pg_replication_slot_advance(%s, %s)", (slot, format_lsn(lsn)))
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Failed to advance logical replication slot '%s': %r", slot, e)
|
||||||
|
failed = True
|
||||||
|
copy = isinstance(e, OperationalError) and e.diag.sqlstate == '58P01' # WAL file is gone
|
||||||
|
with self._condition:
|
||||||
|
if self._scheduled and failed:
|
||||||
|
if copy and slot not in self._copy_slots:
|
||||||
|
self._copy_slots.append(slot)
|
||||||
|
self._failed = True
|
||||||
|
|
||||||
|
new_lsn = self._scheduled.get(database, {}).get(slot, 0)
|
||||||
|
# remove slot from the self._scheduled structure only if it wasn't changed
|
||||||
|
if new_lsn == lsn and database in self._scheduled:
|
||||||
|
self._scheduled[database].pop(slot)
|
||||||
|
if not self._scheduled[database]:
|
||||||
|
self._scheduled.pop(database)
|
||||||
|
|
||||||
|
def sync_slots_in_database(self, database, slots):
|
||||||
|
with self._slots_handler.get_local_connection_cursor(dbname=database, options='-c statement_timeout=0') as cur:
|
||||||
|
for slot in slots:
|
||||||
|
with self._condition:
|
||||||
|
lsn = self._scheduled.get(database, {}).get(slot, 0)
|
||||||
|
if lsn:
|
||||||
|
self.sync_slot(cur, database, slot, lsn)
|
||||||
|
|
||||||
|
def sync_slots(self):
|
||||||
|
with self._condition:
|
||||||
|
databases = list(self._scheduled.keys())
|
||||||
|
for database in databases:
|
||||||
|
with self._condition:
|
||||||
|
slots = list(self._scheduled.get(database, {}).keys())
|
||||||
|
if slots:
|
||||||
|
try:
|
||||||
|
self.sync_slots_in_database(database, slots)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Failed to advance replication slots in database %s: %r', database, e)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
with self._condition:
|
||||||
|
if not self._scheduled:
|
||||||
|
self._condition.wait()
|
||||||
|
|
||||||
|
self.sync_slots()
|
||||||
|
|
||||||
|
def schedule(self, advance_slots):
|
||||||
|
with self._condition:
|
||||||
|
for database, values in advance_slots.items():
|
||||||
|
self._scheduled[database].update(values)
|
||||||
|
ret = (self._failed, self._copy_slots)
|
||||||
|
self._copy_slots = []
|
||||||
|
self._failed = False
|
||||||
|
self._condition.notify()
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def on_promote(self):
|
||||||
|
with self._condition:
|
||||||
|
self._scheduled.clear()
|
||||||
|
self._failed = False
|
||||||
|
self._copy_slots = []
|
||||||
|
|
||||||
|
|
||||||
class SlotsHandler(object):
|
class SlotsHandler(object):
|
||||||
|
|
||||||
def __init__(self, postgresql):
|
def __init__(self, postgresql):
|
||||||
self._postgresql = postgresql
|
self._postgresql = postgresql
|
||||||
self._use_slots = postgresql.config.get('use_slots', True)
|
self._advance = None
|
||||||
self._replication_slots = {} # already existing replication slots
|
self._replication_slots = {} # already existing replication slots
|
||||||
|
self._unready_logical_slots = {}
|
||||||
self.schedule()
|
self.schedule()
|
||||||
|
|
||||||
@property
|
|
||||||
def use_slots(self):
|
|
||||||
return self._use_slots and self._postgresql.major_version >= 90400
|
|
||||||
|
|
||||||
def _query(self, sql, *params):
|
def _query(self, sql, *params):
|
||||||
return self._postgresql.query(sql, *params, retry=False)
|
return self._postgresql.query(sql, *params, retry=False)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _copy_items(src, dst, keys=None):
|
||||||
|
dst.update({key: src[key] for key in keys or ('datoid', 'catalog_xmin', 'confirmed_flush_lsn')})
|
||||||
|
|
||||||
|
def process_permanent_slots(self, slots):
|
||||||
|
"""This methods solves three problems at once (I know, it is weird).
|
||||||
|
|
||||||
|
The cluster_info_query from `Postgresql` is executed every HA loop and returns
|
||||||
|
information about all replication slots that exists on the current host.
|
||||||
|
Based on this information we perform the following actions:
|
||||||
|
1. For the primary we want to expose to DCS permanent logical slots, therefore the method
|
||||||
|
builds (and returns) a dict, that maps permanent logical slot names and confirmed_flush_lsns.
|
||||||
|
2. This method also detects if one of the previously known permanent slots got missing and schedules resync.
|
||||||
|
3. Updates the local cache with the fresh catalog_xmin and confirmed_flush_lsn for every known slot.
|
||||||
|
This info is used when performing the check of logical slot readiness on standbys.
|
||||||
|
"""
|
||||||
|
ret = {}
|
||||||
|
|
||||||
|
slots = {slot['slot_name']: slot for slot in slots or []}
|
||||||
|
if slots:
|
||||||
|
for name, value in slots.items():
|
||||||
|
if name in self._replication_slots:
|
||||||
|
if compare_slots(value, self._replication_slots[name], 'datoid'):
|
||||||
|
if value['type'] == 'logical':
|
||||||
|
ret[name] = value['confirmed_flush_lsn']
|
||||||
|
self._copy_items(value, self._replication_slots[name])
|
||||||
|
else:
|
||||||
|
self._schedule_load_slots = True
|
||||||
|
|
||||||
|
# It could happen that the slots was deleted in the background, we want to detect this case
|
||||||
|
if any(name not in slots for name in self._replication_slots.keys()):
|
||||||
|
self._schedule_load_slots = True
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
def load_replication_slots(self):
|
def load_replication_slots(self):
|
||||||
if self.use_slots and self._schedule_load_slots:
|
if self._postgresql.major_version >= 90400 and self._schedule_load_slots:
|
||||||
replication_slots = {}
|
replication_slots = {}
|
||||||
cursor = self._query('SELECT slot_name, slot_type, plugin, database FROM pg_catalog.pg_replication_slots')
|
extra = ", catalog_xmin, pg_catalog.pg_wal_lsn_diff(confirmed_flush_lsn, '0/0')::bigint"\
|
||||||
|
if self._postgresql.major_version >= 100000 else ""
|
||||||
|
skip_temp_slots = ' WHERE NOT temporary' if self._postgresql.major_version >= 100000 else ''
|
||||||
|
cursor = self._query('SELECT slot_name, slot_type, plugin, database, datoid'
|
||||||
|
'{0} FROM pg_catalog.pg_replication_slots{1}'.format(extra, skip_temp_slots))
|
||||||
for r in cursor:
|
for r in cursor:
|
||||||
value = {'type': r[1]}
|
value = {'type': r[1]}
|
||||||
if r[1] == 'logical':
|
if r[1] == 'logical':
|
||||||
value.update({'plugin': r[2], 'database': r[3]})
|
value.update(plugin=r[2], database=r[3], datoid=r[4])
|
||||||
|
if self._postgresql.major_version >= 100000:
|
||||||
|
value.update(catalog_xmin=r[5], confirmed_flush_lsn=r[6])
|
||||||
replication_slots[r[0]] = value
|
replication_slots[r[0]] = value
|
||||||
self._replication_slots = replication_slots
|
self._replication_slots = replication_slots
|
||||||
self._schedule_load_slots = False
|
self._schedule_load_slots = False
|
||||||
|
if self._force_readiness_check:
|
||||||
|
self._unready_logical_slots = {n: None for n, v in replication_slots.items() if v['type'] == 'logical'}
|
||||||
|
self._force_readiness_check = False
|
||||||
|
|
||||||
|
def ignore_replication_slot(self, cluster, name):
|
||||||
|
slot = self._replication_slots[name]
|
||||||
|
for matcher in cluster.config.ignore_slots_matchers:
|
||||||
|
if ((matcher.get("name") is None or matcher["name"] == name)
|
||||||
|
and all(not matcher.get(a) or matcher[a] == slot.get(a) for a in ('database', 'plugin', 'type'))):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def drop_replication_slot(self, name):
|
def drop_replication_slot(self, name):
|
||||||
cursor = self._query(('SELECT pg_catalog.pg_drop_replication_slot(%s) WHERE EXISTS (SELECT 1 ' +
|
cursor = self._query(('SELECT pg_catalog.pg_drop_replication_slot(%s) WHERE EXISTS (SELECT 1 ' +
|
||||||
@@ -44,33 +197,27 @@ class SlotsHandler(object):
|
|||||||
# In normal situation rowcount should be 1, otherwise either slot doesn't exists or it is still active
|
# In normal situation rowcount should be 1, otherwise either slot doesn't exists or it is still active
|
||||||
return cursor.rowcount == 1
|
return cursor.rowcount == 1
|
||||||
|
|
||||||
def sync_replication_slots(self, cluster):
|
def _drop_incorrect_slots(self, cluster, slots, paused):
|
||||||
if self.use_slots:
|
|
||||||
try:
|
|
||||||
self.load_replication_slots()
|
|
||||||
|
|
||||||
slots = cluster.get_replication_slots(self._postgresql.name, self._postgresql.role)
|
|
||||||
|
|
||||||
# drop old replication slots which are not presented in desired slots
|
# drop old replication slots which are not presented in desired slots
|
||||||
for name in set(self._replication_slots) - set(slots):
|
for name in set(self._replication_slots) - set(slots):
|
||||||
if not self.drop_replication_slot(name):
|
if not paused and not self.ignore_replication_slot(cluster, name) and not self.drop_replication_slot(name):
|
||||||
logger.error("Failed to drop replication slot '%s'", name)
|
logger.error("Failed to drop replication slot '%s'", name)
|
||||||
self._schedule_load_slots = True
|
self._schedule_load_slots = True
|
||||||
|
|
||||||
immediately_reserve = ', true' if self._postgresql.major_version >= 90600 else ''
|
|
||||||
|
|
||||||
logical_slots = defaultdict(dict)
|
|
||||||
for name, value in slots.items():
|
for name, value in slots.items():
|
||||||
if name in self._replication_slots and not compare_slots(value, self._replication_slots[name]):
|
if name in self._replication_slots and not compare_slots(value, self._replication_slots[name]):
|
||||||
logger.info("Trying to drop replication slot '%s' because value is changing from %s to %s",
|
logger.info("Trying to drop replication slot '%s' because value is changing from %s to %s",
|
||||||
name, self._replication_slots[name], value)
|
name, self._replication_slots[name], value)
|
||||||
if not self.drop_replication_slot(name):
|
if self.drop_replication_slot(name):
|
||||||
|
self._replication_slots.pop(name)
|
||||||
|
else:
|
||||||
logger.error("Failed to drop replication slot '%s'", name)
|
logger.error("Failed to drop replication slot '%s'", name)
|
||||||
self._schedule_load_slots = True
|
self._schedule_load_slots = True
|
||||||
continue
|
|
||||||
self._replication_slots.pop(name)
|
def _ensure_physical_slots(self, slots):
|
||||||
if name not in self._replication_slots:
|
immediately_reserve = ', true' if self._postgresql.major_version >= 90600 else ''
|
||||||
if value['type'] == 'physical':
|
for name, value in slots.items():
|
||||||
|
if name not in self._replication_slots and value['type'] == 'physical':
|
||||||
try:
|
try:
|
||||||
self._query(("SELECT pg_catalog.pg_create_physical_replication_slot(%s{0})" +
|
self._query(("SELECT pg_catalog.pg_create_physical_replication_slot(%s{0})" +
|
||||||
" WHERE NOT EXISTS (SELECT 1 FROM pg_catalog.pg_replication_slots" +
|
" WHERE NOT EXISTS (SELECT 1 FROM pg_catalog.pg_replication_slots" +
|
||||||
@@ -79,30 +226,196 @@ class SlotsHandler(object):
|
|||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to create physical replication slot '%s'", name)
|
logger.exception("Failed to create physical replication slot '%s'", name)
|
||||||
self._schedule_load_slots = True
|
self._schedule_load_slots = True
|
||||||
elif value['type'] == 'logical' and name not in self._replication_slots:
|
|
||||||
|
@contextmanager
|
||||||
|
def get_local_connection_cursor(self, **kwargs):
|
||||||
|
conn_kwargs = self._postgresql.config.local_connect_kwargs
|
||||||
|
conn_kwargs.update(kwargs)
|
||||||
|
with get_connection_cursor(**conn_kwargs) as cur:
|
||||||
|
yield cur
|
||||||
|
|
||||||
|
def _ensure_logical_slots_primary(self, slots):
|
||||||
|
# Group logical slots to be created by database name
|
||||||
|
logical_slots = defaultdict(dict)
|
||||||
|
for name, value in slots.items():
|
||||||
|
if value['type'] == 'logical':
|
||||||
|
# If the logical already exists, copy some information about it into the original structure
|
||||||
|
if self._replication_slots.get(name, {}).get('datoid'):
|
||||||
|
self._copy_items(self._replication_slots[name], value)
|
||||||
|
else:
|
||||||
logical_slots[value['database']][name] = value
|
logical_slots[value['database']][name] = value
|
||||||
|
|
||||||
# create new logical slots
|
# Create new logical slots
|
||||||
for database, values in logical_slots.items():
|
for database, values in logical_slots.items():
|
||||||
conn_kwargs = self._postgresql.config.local_connect_kwargs
|
with self.get_local_connection_cursor(dbname=database) as cur:
|
||||||
conn_kwargs['database'] = database
|
|
||||||
with get_connection_cursor(**conn_kwargs) as cur:
|
|
||||||
for name, value in values.items():
|
for name, value in values.items():
|
||||||
try:
|
try:
|
||||||
cur.execute("SELECT pg_catalog.pg_create_logical_replication_slot(%s, %s)" +
|
cur.execute("SELECT pg_catalog.pg_create_logical_replication_slot(%s, %s)" +
|
||||||
" WHERE NOT EXISTS (SELECT 1 FROM pg_catalog.pg_replication_slots" +
|
" WHERE NOT EXISTS (SELECT 1 FROM pg_catalog.pg_replication_slots" +
|
||||||
" WHERE slot_type = 'logical' AND slot_name = %s)",
|
" WHERE slot_type = 'logical' AND slot_name = %s)",
|
||||||
(name, value['plugin'], name))
|
(name, value['plugin'], name))
|
||||||
except Exception:
|
except Exception as e:
|
||||||
logger.exception("Failed to create logical replication slot '%s' plugin='%s'",
|
logger.error("Failed to create logical replication slot '%s' plugin='%s': %r",
|
||||||
name, value['plugin'])
|
name, value['plugin'], e)
|
||||||
|
slots.pop(name)
|
||||||
self._schedule_load_slots = True
|
self._schedule_load_slots = True
|
||||||
|
|
||||||
|
def schedule_advance_slots(self, slots):
|
||||||
|
if not self._advance:
|
||||||
|
self._advance = SlotsAdvanceThread(self)
|
||||||
|
return self._advance.schedule(slots)
|
||||||
|
|
||||||
|
def _ensure_logical_slots_replica(self, cluster, slots):
|
||||||
|
advance_slots = defaultdict(dict) # Group logical slots to be advanced by database name
|
||||||
|
create_slots = [] # And collect logical slots to be created on the replica
|
||||||
|
for name, value in slots.items():
|
||||||
|
if value['type'] == 'logical':
|
||||||
|
# If the logical already exists, copy some information about it into the original structure
|
||||||
|
if self._replication_slots.get(name, {}).get('datoid'):
|
||||||
|
self._copy_items(self._replication_slots[name], value)
|
||||||
|
if name in cluster.slots:
|
||||||
|
try: # Skip slots that doesn't need to be advanced
|
||||||
|
if value['confirmed_flush_lsn'] < int(cluster.slots[name]):
|
||||||
|
advance_slots[value['database']][name] = int(cluster.slots[name])
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Failed to parse "%s": %r', cluster.slots[name], e)
|
||||||
|
elif name in cluster.slots: # We want to copy only slots with feedback in a DCS
|
||||||
|
create_slots.append(name)
|
||||||
|
|
||||||
|
error, copy_slots = self.schedule_advance_slots(advance_slots)
|
||||||
|
if error:
|
||||||
|
self._schedule_load_slots = True
|
||||||
|
return create_slots + copy_slots
|
||||||
|
|
||||||
|
def sync_replication_slots(self, cluster, nofailover, replicatefrom=None, paused=False):
|
||||||
|
ret = None
|
||||||
|
if self._postgresql.major_version >= 90400 and cluster.config:
|
||||||
|
try:
|
||||||
|
self.load_replication_slots()
|
||||||
|
|
||||||
|
slots = cluster.get_replication_slots(self._postgresql.name, self._postgresql.role,
|
||||||
|
nofailover, self._postgresql.major_version, True)
|
||||||
|
|
||||||
|
self._drop_incorrect_slots(cluster, slots, paused)
|
||||||
|
|
||||||
|
self._ensure_physical_slots(slots)
|
||||||
|
|
||||||
|
if self._postgresql.is_leader():
|
||||||
|
self._unready_logical_slots.clear()
|
||||||
|
self._ensure_logical_slots_primary(slots)
|
||||||
|
elif cluster.slots and slots:
|
||||||
|
self.check_logical_slots_readiness(cluster, nofailover, replicatefrom)
|
||||||
|
|
||||||
|
ret = self._ensure_logical_slots_replica(cluster, slots)
|
||||||
|
|
||||||
self._replication_slots = slots
|
self._replication_slots = slots
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Exception when changing replication slots')
|
logger.exception('Exception when changing replication slots')
|
||||||
self._schedule_load_slots = True
|
self._schedule_load_slots = True
|
||||||
|
return ret
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def _get_leader_connection_cursor(self, leader):
|
||||||
|
conn_kwargs = leader.conn_kwargs(self._postgresql.config.rewind_credentials)
|
||||||
|
conn_kwargs['dbname'] = self._postgresql.database
|
||||||
|
with get_connection_cursor(connect_timeout=3, options="-c statement_timeout=2000", **conn_kwargs) as cur:
|
||||||
|
yield cur
|
||||||
|
|
||||||
|
def check_logical_slots_readiness(self, cluster, nofailover, replicatefrom):
|
||||||
|
if self._unready_logical_slots:
|
||||||
|
slot_name = cluster.get_my_slot_name_on_primary(self._postgresql.name, replicatefrom)
|
||||||
|
try:
|
||||||
|
with self._get_leader_connection_cursor(cluster.leader) as cur:
|
||||||
|
cur.execute("SELECT slot_name, catalog_xmin FROM pg_catalog.pg_get_replication_slots()"
|
||||||
|
" WHERE NOT pg_catalog.pg_is_in_recovery() AND slot_name = ANY(%s)",
|
||||||
|
([n for n, v in self._unready_logical_slots.items() if v is None] + [slot_name],))
|
||||||
|
slots = {row[0]: row[1] for row in cur}
|
||||||
|
if slot_name not in slots:
|
||||||
|
return logger.warning('Physical slot %s does not exist on the primary', slot_name)
|
||||||
|
catalog_xmin = slots.pop(slot_name)
|
||||||
|
except Exception as e:
|
||||||
|
return logger.error("Failed to check %s physical slot on the primary: %r", slot_name, e)
|
||||||
|
# Remember catalog_xmin of logical slots on the primary when catalog_xmin of
|
||||||
|
# the physical slot became valid. Logical slots on replica will be safe to use after
|
||||||
|
# promote when catalog_xmin of the physical slot overtakes these values.
|
||||||
|
if catalog_xmin:
|
||||||
|
for name, value in slots.items():
|
||||||
|
self._unready_logical_slots[name] = value
|
||||||
|
else: # Replica isn't streaming or the hot_standby_feedback isn't enabled
|
||||||
|
try:
|
||||||
|
cur = self._query("SELECT pg_catalog.current_setting('hot_standby_feedback')::boolean")
|
||||||
|
if not cur.fetchone()[0]:
|
||||||
|
logger.error('Logical slot failover requires "hot_standby_feedback".'
|
||||||
|
' Please check postgresql.auto.conf')
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Failed to check the hot_standby_feedback setting: %r', e)
|
||||||
|
return # since `catalog_xmin` isn't valid further checks don't make any sense
|
||||||
|
|
||||||
|
for name in list(self._unready_logical_slots):
|
||||||
|
value = self._replication_slots.get(name)
|
||||||
|
# The logical slot on a replica is safe to use when the physical replica slot on the primary:
|
||||||
|
# 1. has a nonzero/non-null catalog_xmin
|
||||||
|
# 2. has a catalog_xmin that is not newer (greater) than the catalog_xmin of any slot on the standby
|
||||||
|
# 3. overtook the catalog_xmin of remembered values of logical slots on the primary.
|
||||||
|
if not value or self._unready_logical_slots[name] <= catalog_xmin <= value['catalog_xmin']:
|
||||||
|
del self._unready_logical_slots[name]
|
||||||
|
if value:
|
||||||
|
logger.info('Logical slot %s is safe to be used after a failover', name)
|
||||||
|
|
||||||
|
def copy_logical_slots(self, cluster, create_slots):
|
||||||
|
leader = cluster.leader
|
||||||
|
slots = cluster.get_replication_slots(self._postgresql.name, 'replica', False, self._postgresql.major_version)
|
||||||
|
with self._get_leader_connection_cursor(leader) as cur:
|
||||||
|
try:
|
||||||
|
cur.execute("SELECT slot_name, slot_type, datname, plugin, catalog_xmin, "
|
||||||
|
"pg_catalog.pg_wal_lsn_diff(confirmed_flush_lsn, '0/0')::bigint, "
|
||||||
|
"pg_catalog.pg_read_binary_file('pg_replslot/' || slot_name || '/state')"
|
||||||
|
" FROM pg_catalog.pg_get_replication_slots() JOIN pg_catalog.pg_database ON datoid = oid"
|
||||||
|
" WHERE NOT pg_catalog.pg_is_in_recovery() AND slot_name = ANY(%s)", (create_slots,))
|
||||||
|
|
||||||
|
create_slots = {}
|
||||||
|
for r in cur:
|
||||||
|
if r[0] in slots: # slot_name is defined in the global configuration
|
||||||
|
slot = {'type': r[1], 'database': r[2], 'plugin': r[3],
|
||||||
|
'catalog_xmin': r[4], 'confirmed_flush_lsn': r[5], 'data': r[6]}
|
||||||
|
if compare_slots(slot, slots[r[0]]):
|
||||||
|
create_slots[r[0]] = slot
|
||||||
|
else:
|
||||||
|
logger.warning('Will not copy the logical slot "%s" due to the configuration mismatch: ' +
|
||||||
|
'configuration=%s, slot on the primary=%s', r[0], slots[r[0]], slot)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Failed to copy logical slots from the %s via postgresql connection: %r", leader.name, e)
|
||||||
|
|
||||||
|
if isinstance(create_slots, dict) and create_slots and self._postgresql.stop():
|
||||||
|
pg_replslot_dir = os.path.join(self._postgresql.data_dir, 'pg_replslot')
|
||||||
|
for name, value in create_slots.items():
|
||||||
|
slot_dir = os.path.join(pg_replslot_dir, name)
|
||||||
|
slot_tmp_dir = slot_dir + '.tmp'
|
||||||
|
if os.path.exists(slot_tmp_dir):
|
||||||
|
shutil.rmtree(slot_tmp_dir)
|
||||||
|
os.makedirs(slot_tmp_dir)
|
||||||
|
fsync_dir(slot_tmp_dir)
|
||||||
|
with open(os.path.join(slot_tmp_dir, 'state'), 'wb') as f:
|
||||||
|
f.write(value['data'])
|
||||||
|
f.flush()
|
||||||
|
os.fsync(f.fileno())
|
||||||
|
if os.path.exists(slot_dir):
|
||||||
|
shutil.rmtree(slot_dir)
|
||||||
|
os.rename(slot_tmp_dir, slot_dir)
|
||||||
|
fsync_dir(slot_dir)
|
||||||
|
self._unready_logical_slots[name] = None
|
||||||
|
fsync_dir(pg_replslot_dir)
|
||||||
|
self._postgresql.start()
|
||||||
|
|
||||||
def schedule(self, value=None):
|
def schedule(self, value=None):
|
||||||
if value is None:
|
if value is None:
|
||||||
value = self.use_slots
|
value = self._postgresql.major_version >= 90400
|
||||||
self._schedule_load_slots = value
|
self._schedule_load_slots = self._force_readiness_check = value
|
||||||
|
|
||||||
|
def on_promote(self):
|
||||||
|
if self._advance:
|
||||||
|
self._advance.on_promote()
|
||||||
|
|
||||||
|
if self._unready_logical_slots:
|
||||||
|
logger.warning('Logical replication slots that might be unsafe to use after promote: %s',
|
||||||
|
set(self._unready_logical_slots))
|
||||||
|
|||||||
@@ -0,0 +1,550 @@
|
|||||||
|
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_in_place_tablespaces': Bool(150000, None),
|
||||||
|
'allow_system_table_mods': Bool(90300, None),
|
||||||
|
'application_name': String(90300, None),
|
||||||
|
'archive_command': String(90300, None),
|
||||||
|
'archive_library': String(150000, 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_connection_check_interval': Integer(140000, None, 0, 2147483647, 'ms'),
|
||||||
|
'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),
|
||||||
|
'compute_query_id': (
|
||||||
|
EnumBool(140000, 150000, ('auto',)),
|
||||||
|
EnumBool(150000, None, ('auto', 'regress'))
|
||||||
|
),
|
||||||
|
'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_discard_caches': Integer(150000, None, 0, 0, None),
|
||||||
|
'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_toast_compression': Enum(140000, None, ('pglz', 'lz4')),
|
||||||
|
'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_async_append': Bool(140000, 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_memoize': Bool(150000, 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),
|
||||||
|
'extension_destdir': String(140000, 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',)),
|
||||||
|
'huge_page_size': Integer(140000, None, 0, 2147483647, 'kB'),
|
||||||
|
'ident_file': String(90300, None),
|
||||||
|
'idle_in_transaction_session_timeout': Integer(90600, None, 0, 2147483647, 'ms'),
|
||||||
|
'idle_session_timeout': Integer(140000, 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_recovery_conflict_waits': Bool(140000, 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_startup_progress_interval': Integer(150000, None, 0, 2147483647, 'ms'),
|
||||||
|
'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_dynamic_shared_memory': Integer(140000, None, 0, 2147483647, 'MB'),
|
||||||
|
'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, 140000),
|
||||||
|
'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),
|
||||||
|
'recovery_init_sync_method': Enum(140000, None, ('fsync', 'syncfs')),
|
||||||
|
'recovery_prefetch': EnumBool(150000, None, ('try',)),
|
||||||
|
'recursive_worktable_factor': Real(150000, None, 0.001, 1e+06, None),
|
||||||
|
'remove_temp_files_after_crash': Bool(140000, 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_dir': String(140000, 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_fetch_consistency': Enum(150000, None, ('none', 'cache', 'snapshot')),
|
||||||
|
'stats_temp_directory': String(90300, 150000),
|
||||||
|
'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),
|
||||||
|
'track_wal_io_timing': Bool(140000, 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, 140000, 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_failsafe_age': Integer(140000, None, 0, 2100000000, None),
|
||||||
|
'vacuum_freeze_min_age': Integer(90300, None, 0, 1000000000, None),
|
||||||
|
'vacuum_freeze_table_age': Integer(90300, None, 0, 2000000000, None),
|
||||||
|
'vacuum_multixact_failsafe_age': Integer(140000, None, 0, 2100000000, 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, 150000),
|
||||||
|
EnumBool(150000, None, ('pglz', 'lz4', 'zstd'))
|
||||||
|
),
|
||||||
|
'wal_consistency_checking': String(100000, None),
|
||||||
|
'wal_decode_buffer_size': Integer(150000, None, 65536, 1073741823, 'B'),
|
||||||
|
'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
|
||||||
|
if name in recovery_parameters:
|
||||||
|
return None
|
||||||
|
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,40 @@
|
|||||||
|
__all__ = ['connect', 'quote_ident', 'quote_literal', 'DatabaseError', 'Error', 'OperationalError', 'ProgrammingError']
|
||||||
|
|
||||||
|
_legacy = False
|
||||||
|
try:
|
||||||
|
from psycopg2 import __version__
|
||||||
|
from . import MIN_PSYCOPG2, parse_version
|
||||||
|
if parse_version(__version__) < MIN_PSYCOPG2:
|
||||||
|
raise ImportError
|
||||||
|
from psycopg2 import connect, Error, DatabaseError, OperationalError, ProgrammingError
|
||||||
|
from psycopg2.extensions import adapt
|
||||||
|
|
||||||
|
try:
|
||||||
|
from psycopg2.extensions import quote_ident as _quote_ident
|
||||||
|
except ImportError:
|
||||||
|
_legacy = True
|
||||||
|
|
||||||
|
def quote_literal(value, conn=None):
|
||||||
|
value = adapt(value)
|
||||||
|
if conn:
|
||||||
|
value.prepare(conn)
|
||||||
|
return value.getquoted().decode('utf-8')
|
||||||
|
except ImportError:
|
||||||
|
from psycopg import connect as _connect, sql, Error, DatabaseError, OperationalError, ProgrammingError
|
||||||
|
|
||||||
|
def connect(*args, **kwargs):
|
||||||
|
ret = _connect(*args, **kwargs)
|
||||||
|
ret.server_version = ret.pgconn.server_version # compatibility with psycopg2
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def _quote_ident(value, conn):
|
||||||
|
return sql.Identifier(value).as_string(conn)
|
||||||
|
|
||||||
|
def quote_literal(value, conn=None):
|
||||||
|
return sql.Literal(value).as_string(conn)
|
||||||
|
|
||||||
|
|
||||||
|
def quote_ident(value, conn=None):
|
||||||
|
if _legacy or conn is None:
|
||||||
|
return '"{0}"'.format(value.replace('"', '""'))
|
||||||
|
return _quote_ident(value, conn)
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from .daemon import AbstractPatroniDaemon, abstract_main
|
||||||
|
from .dcs.raft import KVStoreTTL
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class RaftController(AbstractPatroniDaemon):
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
super(RaftController, self).__init__(config)
|
||||||
|
|
||||||
|
config = self.config.get('raft')
|
||||||
|
assert 'self_addr' in config
|
||||||
|
self._raft = KVStoreTTL(None, None, None, **config)
|
||||||
|
|
||||||
|
def _run_cycle(self):
|
||||||
|
try:
|
||||||
|
self._raft.doTick(self._raft.conf.autoTickPeriod)
|
||||||
|
except Exception:
|
||||||
|
logger.exception('doTick')
|
||||||
|
|
||||||
|
def _shutdown(self):
|
||||||
|
self._raft.destroy()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
abstract_main(RaftController)
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import json
|
||||||
|
import urllib3
|
||||||
|
import six
|
||||||
|
|
||||||
|
from six.moves.urllib_parse import urlparse, urlunparse
|
||||||
|
|
||||||
|
from .utils import USER_AGENT
|
||||||
|
|
||||||
|
|
||||||
|
class PatroniRequest(object):
|
||||||
|
|
||||||
|
def __init__(self, config, insecure=False):
|
||||||
|
cert_reqs = 'CERT_NONE' if insecure or config.get('ctl', {}).get('insecure', False) else 'CERT_REQUIRED'
|
||||||
|
self._pool = urllib3.PoolManager(num_pools=10, maxsize=10, cert_reqs=cert_reqs)
|
||||||
|
self.reload_config(config)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_cfg_value(config, name):
|
||||||
|
return config.get('ctl', {}).get(name) or config.get('restapi', {}).get(name)
|
||||||
|
|
||||||
|
def _apply_pool_param(self, param, value):
|
||||||
|
if value:
|
||||||
|
self._pool.connection_pool_kw[param] = value
|
||||||
|
else:
|
||||||
|
self._pool.connection_pool_kw.pop(param, None)
|
||||||
|
|
||||||
|
def _apply_ssl_file_param(self, config, name):
|
||||||
|
value = self._get_cfg_value(config, name + 'file')
|
||||||
|
self._apply_pool_param(name + '_file', value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def reload_config(self, config):
|
||||||
|
self._pool.headers = urllib3.make_headers(basic_auth=self._get_cfg_value(config, 'auth'), user_agent=USER_AGENT)
|
||||||
|
|
||||||
|
if self._apply_ssl_file_param(config, 'cert'):
|
||||||
|
self._apply_ssl_file_param(config, 'key')
|
||||||
|
|
||||||
|
password = self._get_cfg_value(config, 'keyfile_password')
|
||||||
|
self._apply_pool_param('key_password', password)
|
||||||
|
else:
|
||||||
|
self._pool.connection_pool_kw.pop('key_file', None)
|
||||||
|
|
||||||
|
cacert = config.get('ctl', {}).get('cacert') or config.get('restapi', {}).get('cafile')
|
||||||
|
self._apply_pool_param('ca_certs', cacert)
|
||||||
|
|
||||||
|
def request(self, method, url, body=None, **kwargs):
|
||||||
|
if body is not None and not isinstance(body, six.string_types):
|
||||||
|
body = json.dumps(body)
|
||||||
|
return self._pool.request(method.upper(), url, body=body, **kwargs)
|
||||||
|
|
||||||
|
def __call__(self, member, method='GET', endpoint=None, data=None, **kwargs):
|
||||||
|
url = member.api_url
|
||||||
|
if endpoint:
|
||||||
|
scheme, netloc, _, _, _, _ = urlparse(url)
|
||||||
|
url = urlunparse((scheme, netloc, endpoint, '', '', ''))
|
||||||
|
return self.request(method, url, data, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def get(url, verify=True, **kwargs):
|
||||||
|
http = PatroniRequest({}, not verify)
|
||||||
|
return http.request('GET', url, **kwargs)
|
||||||
+19
-15
@@ -1,12 +1,14 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import requests
|
|
||||||
from requests.exceptions import RequestException
|
|
||||||
import sys
|
import sys
|
||||||
import boto.ec2
|
import boto3
|
||||||
|
|
||||||
from patroni.utils import Retry, RetryFailedError
|
from ..utils import Retry, RetryFailedError
|
||||||
|
from ..request import get as requests_get
|
||||||
|
|
||||||
|
from botocore.exceptions import ClientError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -16,17 +18,17 @@ class AWSConnection(object):
|
|||||||
def __init__(self, cluster_name):
|
def __init__(self, cluster_name):
|
||||||
self.available = False
|
self.available = False
|
||||||
self.cluster_name = cluster_name if cluster_name is not None else 'unknown'
|
self.cluster_name = cluster_name if cluster_name is not None else 'unknown'
|
||||||
self._retry = Retry(deadline=300, max_delay=30, max_tries=-1, retry_exceptions=(boto.exception.StandardError,))
|
self._retry = Retry(deadline=300, max_delay=30, max_tries=-1, retry_exceptions=(ClientError,))
|
||||||
try:
|
try:
|
||||||
# get the instance id
|
# get the instance id
|
||||||
r = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document', timeout=2.1)
|
r = requests_get('http://169.254.169.254/latest/dynamic/instance-identity/document', timeout=2.1)
|
||||||
except RequestException:
|
except Exception:
|
||||||
logger.error('cannot query AWS meta-data')
|
logger.error('cannot query AWS meta-data')
|
||||||
return
|
return
|
||||||
|
|
||||||
if r.ok:
|
if r.status < 400:
|
||||||
try:
|
try:
|
||||||
content = r.json()
|
content = json.loads(r.data.decode('utf-8'))
|
||||||
self.instance_id = content['instanceId']
|
self.instance_id = content['instanceId']
|
||||||
self.region = content['region']
|
self.region = content['region']
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -42,20 +44,22 @@ class AWSConnection(object):
|
|||||||
|
|
||||||
def _tag_ebs(self, conn, role):
|
def _tag_ebs(self, conn, role):
|
||||||
""" set tags, carrying the cluster name, instance role and instance id for the EBS storage """
|
""" set tags, carrying the cluster name, instance role and instance id for the EBS storage """
|
||||||
tags = {'Name': 'spilo_' + self.cluster_name, 'Role': role, 'Instance': self.instance_id}
|
tags = [{'Key': 'Name', 'Value': 'spilo_' + self.cluster_name},
|
||||||
volumes = conn.get_all_volumes(filters={'attachment.instance-id': self.instance_id})
|
{'Key': 'Role', 'Value': role},
|
||||||
conn.create_tags([v.id for v in volumes], tags)
|
{'Key': 'Instance', 'Value': self.instance_id}]
|
||||||
|
volumes = conn.volumes.filter(Filters=[{'Name': 'attachment.instance-id', 'Values': [self.instance_id]}])
|
||||||
|
conn.create_tags(Resources=[v.id for v in volumes], Tags=tags)
|
||||||
|
|
||||||
def _tag_ec2(self, conn, role):
|
def _tag_ec2(self, conn, role):
|
||||||
""" tag the current EC2 instance with a cluster role """
|
""" tag the current EC2 instance with a cluster role """
|
||||||
tags = {'Role': role}
|
tags = [{'Key': 'Role', 'Value': role}]
|
||||||
conn.create_tags([self.instance_id], tags)
|
conn.create_tags(Resources=[self.instance_id], Tags=tags)
|
||||||
|
|
||||||
def on_role_change(self, new_role):
|
def on_role_change(self, new_role):
|
||||||
if not self.available:
|
if not self.available:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
conn = self.retry(boto.ec2.connect_to_region, self.region)
|
conn = boto3.resource('ec2', region_name=self.region)
|
||||||
self.retry(self._tag_ec2, conn, new_role)
|
self.retry(self._tag_ec2, conn, new_role)
|
||||||
self.retry(self._tag_ebs, conn, new_role)
|
self.retry(self._tag_ebs, conn, new_role)
|
||||||
except RetryFailedError:
|
except RetryFailedError:
|
||||||
|
|||||||
@@ -27,13 +27,14 @@ import argparse
|
|||||||
import csv
|
import csv
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import psycopg2
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from .. import psycopg
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
RETRY_SLEEP_INTERVAL = 1
|
RETRY_SLEEP_INTERVAL = 1
|
||||||
@@ -215,7 +216,7 @@ class WALERestore(object):
|
|||||||
if self.master_connection:
|
if self.master_connection:
|
||||||
try:
|
try:
|
||||||
# get the difference in bytes between the current WAL location and the backup start offset
|
# get the difference in bytes between the current WAL location and the backup start offset
|
||||||
with psycopg2.connect(self.master_connection) as con:
|
with psycopg.connect(self.master_connection) as con:
|
||||||
if con.server_version >= 100000:
|
if con.server_version >= 100000:
|
||||||
wal_name = 'wal'
|
wal_name = 'wal'
|
||||||
lsn_name = 'lsn'
|
lsn_name = 'lsn'
|
||||||
@@ -233,7 +234,7 @@ class WALERestore(object):
|
|||||||
(backup_start_lsn, backup_start_lsn, backup_start_lsn))
|
(backup_start_lsn, backup_start_lsn, backup_start_lsn))
|
||||||
|
|
||||||
diff_in_bytes = int(cur.fetchone()[0])
|
diff_in_bytes = int(cur.fetchone()[0])
|
||||||
except psycopg2.Error:
|
except psycopg.Error:
|
||||||
logger.exception('could not determine difference with the master location')
|
logger.exception('could not determine difference with the master location')
|
||||||
if attempts_no < self.retries: # retry in case of a temporarily connection issue
|
if attempts_no < self.retries: # retry in case of a temporarily connection issue
|
||||||
attempts_no = attempts_no + 1
|
attempts_no = attempts_no + 1
|
||||||
@@ -250,7 +251,7 @@ class WALERestore(object):
|
|||||||
diff_in_bytes = 0
|
diff_in_bytes = 0
|
||||||
break
|
break
|
||||||
|
|
||||||
# if the size of the accumulated WAL segments is more than a certan percentage of the backup size
|
# if the size of the accumulated WAL segments is more than a certain percentage of the backup size
|
||||||
# or exceeds the pre-determined size - pg_basebackup is chosen instead.
|
# or exceeds the pre-determined size - pg_basebackup is chosen instead.
|
||||||
is_size_thresh_ok = diff_in_bytes < int(threshold_megabytes) * 1048576
|
is_size_thresh_ok = diff_in_bytes < int(threshold_megabytes) * 1048576
|
||||||
threshold_pct_bytes = backup_size * threshold_percent / 100.0
|
threshold_pct_bytes = backup_size * threshold_percent / 100.0
|
||||||
@@ -308,7 +309,7 @@ class WALERestore(object):
|
|||||||
try:
|
try:
|
||||||
os.mkdir(path)
|
os.mkdir(path)
|
||||||
except OSError:
|
except OSError:
|
||||||
logger.exception("coud not create missing %s directory path", dirname)
|
logger.exception("could not create missing %s directory path", dirname)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
+183
-3
@@ -1,15 +1,25 @@
|
|||||||
|
import errno
|
||||||
|
import json.decoder as json_decoder
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from dateutil import tz
|
from dateutil import tz
|
||||||
from patroni.exceptions import PatroniException
|
|
||||||
|
from .exceptions import PatroniException
|
||||||
|
from .version import __version__
|
||||||
|
|
||||||
tzutc = tz.tzutc()
|
tzutc = tz.tzutc()
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
USER_AGENT = 'Patroni/{0} Python/{1} {2}'.format(__version__, platform.python_version(), platform.system())
|
||||||
OCT_RE = re.compile(r'^[-+]?0[0-7]*')
|
OCT_RE = re.compile(r'^[-+]?0[0-7]*')
|
||||||
DEC_RE = re.compile(r'^[-+]?(0|[1-9][0-9]*)')
|
DEC_RE = re.compile(r'^[-+]?(0|[1-9][0-9]*)')
|
||||||
HEX_RE = re.compile(r'^[-+]?0x[0-9a-fA-F]+')
|
HEX_RE = re.compile(r'^[-+]?0x[0-9a-fA-F]+')
|
||||||
@@ -296,6 +306,17 @@ class Retry(object):
|
|||||||
max_jitter=self.max_jitter / 100.0, max_delay=self.max_delay, sleep_func=self.sleep_func,
|
max_jitter=self.max_jitter / 100.0, max_delay=self.max_delay, sleep_func=self.sleep_func,
|
||||||
deadline=self.deadline, retry_exceptions=self.retry_exceptions)
|
deadline=self.deadline, retry_exceptions=self.retry_exceptions)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sleeptime(self):
|
||||||
|
return self._cur_delay + (random.randint(0, self.max_jitter) / 100.0)
|
||||||
|
|
||||||
|
def update_delay(self):
|
||||||
|
self._cur_delay = min(self._cur_delay * self.backoff, self.max_delay)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stoptime(self):
|
||||||
|
return self._cur_stoptime
|
||||||
|
|
||||||
def __call__(self, func, *args, **kwargs):
|
def __call__(self, func, *args, **kwargs):
|
||||||
"""Call a function with arguments until it completes without throwing a `retry_exceptions`
|
"""Call a function with arguments until it completes without throwing a `retry_exceptions`
|
||||||
|
|
||||||
@@ -317,14 +338,14 @@ class Retry(object):
|
|||||||
logger.warning('Retry got exception: %s', e)
|
logger.warning('Retry got exception: %s', e)
|
||||||
raise RetryFailedError("Too many retry attempts")
|
raise RetryFailedError("Too many retry attempts")
|
||||||
self._attempts += 1
|
self._attempts += 1
|
||||||
sleeptime = self._cur_delay + (random.randint(0, self.max_jitter) / 100.0)
|
sleeptime = hasattr(e, 'sleeptime') and e.sleeptime or self.sleeptime
|
||||||
|
|
||||||
if self._cur_stoptime is not None and time.time() + sleeptime >= self._cur_stoptime:
|
if self._cur_stoptime is not None and time.time() + sleeptime >= self._cur_stoptime:
|
||||||
logger.warning('Retry got exception: %s', e)
|
logger.warning('Retry got exception: %s', e)
|
||||||
raise RetryFailedError("Exceeded retry deadline")
|
raise RetryFailedError("Exceeded retry deadline")
|
||||||
logger.debug('Retry got exception: %s', e)
|
logger.debug('Retry got exception: %s', e)
|
||||||
self.sleep_func(sleeptime)
|
self.sleep_func(sleeptime)
|
||||||
self._cur_delay = min(self._cur_delay * self.backoff, self.max_delay)
|
self.update_delay()
|
||||||
|
|
||||||
|
|
||||||
def polling_loop(timeout, interval=1):
|
def polling_loop(timeout, interval=1):
|
||||||
@@ -340,6 +361,8 @@ def polling_loop(timeout, interval=1):
|
|||||||
|
|
||||||
def split_host_port(value, default_port):
|
def split_host_port(value, default_port):
|
||||||
t = value.rsplit(':', 1)
|
t = value.rsplit(':', 1)
|
||||||
|
if ':' in t[0]:
|
||||||
|
t[0] = ','.join([h.strip().strip('[]') for h in t[0].split(',')])
|
||||||
t.append(default_port)
|
t.append(default_port)
|
||||||
return t[0], int(t[1])
|
return t[0], int(t[1])
|
||||||
|
|
||||||
@@ -352,3 +375,160 @@ def uri(proto, netloc, path='', user=None):
|
|||||||
path = '/{0}'.format(path) if path and not path.startswith('/') else path
|
path = '/{0}'.format(path) if path and not path.startswith('/') else path
|
||||||
user = '{0}@'.format(user) if user else ''
|
user = '{0}@'.format(user) if user else ''
|
||||||
return '{0}://{1}{2}{3}{4}'.format(proto, user, host, port, path)
|
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'))
|
||||||
|
|
||||||
|
|
||||||
|
def cluster_as_json(cluster):
|
||||||
|
leader_name = cluster.leader.name if cluster.leader else None
|
||||||
|
cluster_lsn = cluster.last_lsn or 0
|
||||||
|
|
||||||
|
ret = {'members': []}
|
||||||
|
for m in cluster.members:
|
||||||
|
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 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()
|
||||||
|
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})
|
||||||
|
|
||||||
|
if m.name != leader_name:
|
||||||
|
lsn = m.data.get('xlog_location')
|
||||||
|
if lsn is None:
|
||||||
|
member['lag'] = 'unknown'
|
||||||
|
elif cluster_lsn >= lsn:
|
||||||
|
member['lag'] = cluster_lsn - lsn
|
||||||
|
else:
|
||||||
|
member['lag'] = 0
|
||||||
|
|
||||||
|
ret['members'].append(member)
|
||||||
|
|
||||||
|
# sort members by name for consistency
|
||||||
|
ret['members'].sort(key=lambda m: m['name'])
|
||||||
|
if cluster.is_paused():
|
||||||
|
ret['pause'] = True
|
||||||
|
if cluster.failover and cluster.failover.scheduled_at:
|
||||||
|
ret['scheduled_switchover'] = {'at': cluster.failover.scheduled_at.isoformat()}
|
||||||
|
if cluster.failover.leader:
|
||||||
|
ret['scheduled_switchover']['from'] = cluster.failover.leader
|
||||||
|
if cluster.failover.candidate:
|
||||||
|
ret['scheduled_switchover']['to'] = cluster.failover.candidate
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def is_subpath(d1, d2):
|
||||||
|
real_d1 = os.path.realpath(d1) + os.path.sep
|
||||||
|
real_d2 = os.path.realpath(os.path.join(real_d1, d2))
|
||||||
|
return os.path.commonprefix([real_d1, real_d2 + os.path.sep]) == real_d1
|
||||||
|
|
||||||
|
|
||||||
|
def validate_directory(d, msg="{} {}"):
|
||||||
|
if not os.path.exists(d):
|
||||||
|
try:
|
||||||
|
os.makedirs(d)
|
||||||
|
except OSError as e:
|
||||||
|
logger.error(e)
|
||||||
|
if e.errno != errno.EEXIST:
|
||||||
|
raise PatroniException(msg.format(d, "couldn't create the directory"))
|
||||||
|
elif os.path.isdir(d):
|
||||||
|
try:
|
||||||
|
fd, tmpfile = tempfile.mkstemp(dir=d)
|
||||||
|
os.close(fd)
|
||||||
|
os.remove(tmpfile)
|
||||||
|
except OSError:
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def find_executable(executable, path=None):
|
||||||
|
_, ext = os.path.splitext(executable)
|
||||||
|
|
||||||
|
if (sys.platform == 'win32') and (ext != '.exe'):
|
||||||
|
executable = executable + '.exe'
|
||||||
|
|
||||||
|
if os.path.isfile(executable):
|
||||||
|
return executable
|
||||||
|
|
||||||
|
if path is None:
|
||||||
|
path = os.environ.get('PATH', os.defpath)
|
||||||
|
|
||||||
|
for p in path.split(os.pathsep):
|
||||||
|
f = os.path.join(p, executable)
|
||||||
|
if os.path.isfile(f):
|
||||||
|
return f
|
||||||
|
|||||||
@@ -0,0 +1,401 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from six import string_types
|
||||||
|
|
||||||
|
from .utils import find_executable, split_host_port, data_directory_is_empty
|
||||||
|
from .dcs import dcs_modules
|
||||||
|
from .exceptions import ConfigParseError
|
||||||
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
if "*" in hosts:
|
||||||
|
if len(hosts) != 1:
|
||||||
|
raise ConfigParseError("expecting '*' alone")
|
||||||
|
hosts = [p[-1][0] for p in socket.getaddrinfo(None, port, 0, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)]
|
||||||
|
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 validate_host_port_list(value):
|
||||||
|
assert all([validate_host_port(v) for v in value]), "didn't pass the validation"
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def comma_separated_host_port(string):
|
||||||
|
return validate_host_port_list([s.strip() for s in string.split(",")])
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
errors = []
|
||||||
|
for i in self.validate(data):
|
||||||
|
if not i.status:
|
||||||
|
errors.append(str(i))
|
||||||
|
return errors
|
||||||
|
|
||||||
|
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()]
|
||||||
|
validate_host_port_list.expected_type = list
|
||||||
|
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
|
||||||
|
validate_etcd = {
|
||||||
|
Or("host", "hosts", "srv", "srv_suffix", "url", "proxy"): Case({
|
||||||
|
"host": validate_host_port,
|
||||||
|
"hosts": Or(comma_separated_host_port, [validate_host_port]),
|
||||||
|
"srv": str,
|
||||||
|
"srv_suffix": str,
|
||||||
|
"url": str,
|
||||||
|
"proxy": str})
|
||||||
|
}
|
||||||
|
|
||||||
|
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": validate_etcd,
|
||||||
|
"etcd3": validate_etcd,
|
||||||
|
"exhibitor": {
|
||||||
|
"hosts": [str],
|
||||||
|
"port": lambda i: assert_(int(i) <= 65535),
|
||||||
|
Optional("pool_interval"): int
|
||||||
|
},
|
||||||
|
"raft": {
|
||||||
|
"self_addr": validate_connect_address,
|
||||||
|
Optional("bind_addr"): validate_host_port_listen,
|
||||||
|
"partner_addrs": validate_host_port_list,
|
||||||
|
Optional("data_dir"): str,
|
||||||
|
Optional("password"): str
|
||||||
|
},
|
||||||
|
"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,
|
||||||
|
"proxy_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.0'
|
__version__ = '2.1.5'
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class WatchdogConfig(object):
|
|||||||
return not self == other
|
return not self == other
|
||||||
|
|
||||||
def get_impl(self):
|
def get_impl(self):
|
||||||
if self.driver == 'testing':
|
if self.driver == 'testing': # pragma: no cover
|
||||||
from patroni.watchdog.linux import TestingWatchdogDevice
|
from patroni.watchdog.linux import TestingWatchdogDevice
|
||||||
return TestingWatchdogDevice.from_config(self.driver_config)
|
return TestingWatchdogDevice.from_config(self.driver_config)
|
||||||
elif platform.system() == 'Linux' and self.driver == 'default':
|
elif platform.system() == 'Linux' and self.driver == 'default':
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ IOC_DIRBITS = 2
|
|||||||
|
|
||||||
# Non-generic platform special cases
|
# Non-generic platform special cases
|
||||||
machine = platform.machine()
|
machine = platform.machine()
|
||||||
if machine in ['mips', 'sparc', 'powerpc', 'ppc64']:
|
if machine in ['mips', 'sparc', 'powerpc', 'ppc64', 'ppc64le']: # pragma: no cover
|
||||||
IOC_SIZEBITS = 13
|
IOC_SIZEBITS = 13
|
||||||
IOC_DIRBITS = 3
|
IOC_DIRBITS = 3
|
||||||
IOC_NONE, IOC_WRITE, IOC_READ = 1, 2, 4
|
IOC_NONE, IOC_WRITE, IOC_READ = 1, 4, 2
|
||||||
elif machine == 'parisc':
|
elif machine == 'parisc': # pragma: no cover
|
||||||
IOC_WRITE, IOC_READ = 2, 1
|
IOC_WRITE, IOC_READ = 2, 1
|
||||||
|
|
||||||
IOC_NRSHIFT = 0
|
IOC_NRSHIFT = 0
|
||||||
@@ -218,7 +218,7 @@ class LinuxWatchdogDevice(WatchdogBase):
|
|||||||
return timeout.value
|
return timeout.value
|
||||||
|
|
||||||
|
|
||||||
class TestingWatchdogDevice(LinuxWatchdogDevice):
|
class TestingWatchdogDevice(LinuxWatchdogDevice): # pragma: no cover
|
||||||
"""Converts timeout ioctls to regular writes that can be intercepted from a named pipe."""
|
"""Converts timeout ioctls to regular writes that can be intercepted from a named pipe."""
|
||||||
timeout = 60
|
timeout = 60
|
||||||
|
|
||||||
|
|||||||
Executable
+6
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
from patroni.raft_controller import main
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
+29
-2
@@ -17,7 +17,25 @@ restapi:
|
|||||||
# cacert: /etc/ssl/certs/ssl-cacert-snakeoil.pem
|
# cacert: /etc/ssl/certs/ssl-cacert-snakeoil.pem
|
||||||
|
|
||||||
etcd:
|
etcd:
|
||||||
|
#Provide host to do the initial discovery of the cluster topology:
|
||||||
host: 127.0.0.1:2379
|
host: 127.0.0.1:2379
|
||||||
|
#Or use "hosts" to provide multiple endpoints
|
||||||
|
#Could be a comma separated string:
|
||||||
|
#hosts: host1:port1,host2:port2
|
||||||
|
#or an actual yaml list:
|
||||||
|
#hosts:
|
||||||
|
#- host1:port1
|
||||||
|
#- host2:port2
|
||||||
|
#Once discovery is complete Patroni will use the list of advertised clientURLs
|
||||||
|
#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:
|
bootstrap:
|
||||||
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
|
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
|
||||||
@@ -39,10 +57,15 @@ bootstrap:
|
|||||||
parameters:
|
parameters:
|
||||||
# wal_level: hot_standby
|
# wal_level: hot_standby
|
||||||
# hot_standby: "on"
|
# hot_standby: "on"
|
||||||
|
# max_connections: 100
|
||||||
|
# max_worker_processes: 8
|
||||||
# wal_keep_segments: 8
|
# wal_keep_segments: 8
|
||||||
# max_wal_senders: 10
|
# max_wal_senders: 10
|
||||||
# max_replication_slots: 10
|
# max_replication_slots: 10
|
||||||
|
# max_prepared_transactions: 0
|
||||||
|
# max_locks_per_transaction: 64
|
||||||
# wal_log_hints: "on"
|
# wal_log_hints: "on"
|
||||||
|
# track_commit_timestamp: "off"
|
||||||
# archive_mode: "on"
|
# archive_mode: "on"
|
||||||
# archive_timeout: 1800s
|
# archive_timeout: 1800s
|
||||||
# archive_command: mkdir -p ../wal_archive && test ! -f ../wal_archive/%f && cp %p ../wal_archive/%f
|
# archive_command: mkdir -p ../wal_archive && test ! -f ../wal_archive/%f && cp %p ../wal_archive/%f
|
||||||
@@ -68,7 +91,7 @@ bootstrap:
|
|||||||
# Some additional users users which needs to be created after initializing new cluster
|
# Some additional users users which needs to be created after initializing new cluster
|
||||||
users:
|
users:
|
||||||
admin:
|
admin:
|
||||||
password: admin
|
password: admin%
|
||||||
options:
|
options:
|
||||||
- createrole
|
- createrole
|
||||||
- createdb
|
- createdb
|
||||||
@@ -76,6 +99,8 @@ bootstrap:
|
|||||||
postgresql:
|
postgresql:
|
||||||
listen: 127.0.0.1:5432
|
listen: 127.0.0.1:5432
|
||||||
connect_address: 127.0.0.1:5432
|
connect_address: 127.0.0.1:5432
|
||||||
|
|
||||||
|
# proxy_address: 127.0.0.1:5433 # The address of connection pool (e.g., pgbouncer) running next to Patroni/Postgres. Only for service discovery.
|
||||||
data_dir: data/postgresql0
|
data_dir: data/postgresql0
|
||||||
# bin_dir:
|
# bin_dir:
|
||||||
# config_dir:
|
# config_dir:
|
||||||
@@ -96,7 +121,9 @@ postgresql:
|
|||||||
# Fully qualified kerberos ticket file for the running user
|
# Fully qualified kerberos ticket file for the running user
|
||||||
# same as KRB5CCNAME used by the GSS
|
# same as KRB5CCNAME used by the GSS
|
||||||
# krb_server_keyfile: /var/spool/keytabs/postgres
|
# krb_server_keyfile: /var/spool/keytabs/postgres
|
||||||
unix_socket_directories: '.'
|
unix_socket_directories: '..' # parent directory of data_dir
|
||||||
|
# Additional fencing script executed after acquiring the leader lock but before promoting the replica
|
||||||
|
#pre_promote: /path/to/pre_promote.sh
|
||||||
|
|
||||||
#watchdog:
|
#watchdog:
|
||||||
# mode: automatic # Allowed values: off, automatic, required
|
# mode: automatic # Allowed values: off, automatic, required
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user