mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-26 15:40:21 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4cc095f913 |
@@ -1,97 +0,0 @@
|
|||||||
name: Bug Report
|
|
||||||
description: Create a report to help us improve
|
|
||||||
labels:
|
|
||||||
- bug
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
If you have a question please post it on channel [#patroni](https://postgresteam.slack.com/archives/C9XPYG92A) in the [PostgreSQL Slack](https://join.slack.com/t/postgresteam/shared_invite/zt-1qj14i9sj-E9WqIFlvcOiHsEk2yFEMjA).
|
|
||||||
Before reporting a bug please make sure to **reproduce it with the latest Patroni version**!
|
|
||||||
Please fill the form below and provide as much information as possible.
|
|
||||||
Not doing so may result in your bug not being addressed in a timely manner.
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: problem
|
|
||||||
attributes:
|
|
||||||
label: What happened?
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: repro
|
|
||||||
attributes:
|
|
||||||
label: How can we reproduce it (as minimally and precisely as possible)?
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: expected
|
|
||||||
attributes:
|
|
||||||
label: What did you expect to happen?
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: environment
|
|
||||||
attributes:
|
|
||||||
label: Patroni/PostgreSQL/DCS version
|
|
||||||
value: |
|
|
||||||
- Patroni version:
|
|
||||||
- PostgreSQL version:
|
|
||||||
- DCS (and its version):
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: patroniConfig
|
|
||||||
attributes:
|
|
||||||
label: Patroni configuration file
|
|
||||||
description: Please copy and paste Patroni configuration file here. This will be automatically formatted into code, so no need for backticks.
|
|
||||||
render: yaml
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: globalConfig
|
|
||||||
attributes:
|
|
||||||
label: patronictl show-config
|
|
||||||
description: Please copy and paste `patronictl show-config` output here. This will be automatically formatted into code, so no need for backticks.
|
|
||||||
render: yaml
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: patroniLogs
|
|
||||||
attributes:
|
|
||||||
label: Patroni log files
|
|
||||||
description: Please copy and paste any relevant Patroni log output. This will be automatically formatted into code, so no need for backticks.
|
|
||||||
render: shell
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: postgresLogs
|
|
||||||
attributes:
|
|
||||||
label: PostgreSQL log files
|
|
||||||
description: Please copy and paste any relevant PostgreSQL log output. This will be automatically formatted into code, so no need for backticks.
|
|
||||||
render: shell
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: checkboxes
|
|
||||||
id: issueSearch
|
|
||||||
attributes:
|
|
||||||
label: Have you tried to use GitHub issue search?
|
|
||||||
description: Maybe there is already a similar issue solved.
|
|
||||||
options:
|
|
||||||
- label: 'Yes'
|
|
||||||
required: true
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
|
|
||||||
- type: textarea
|
|
||||||
id: additional
|
|
||||||
attributes:
|
|
||||||
label: Anything else we need to know?
|
|
||||||
description: Add any other context about the problem here.
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
blank_issues_enabled: false
|
|
||||||
contact_links:
|
|
||||||
- name: Question
|
|
||||||
url: https://join.slack.com/t/postgresteam/shared_invite/zt-1qj14i9sj-E9WqIFlvcOiHsEk2yFEMjA
|
|
||||||
about: "Please ask questions on channel #patroni in the PostgreSQL Slack"
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
import inspect
|
|
||||||
import os
|
|
||||||
import shutil
|
|
||||||
import subprocess
|
|
||||||
import stat
|
|
||||||
import sys
|
|
||||||
import tarfile
|
|
||||||
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, 7, 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)
|
|
||||||
if float(ver) >= 15:
|
|
||||||
packages += ['postgresql-{0}-citus-11.2'.format(ver)]
|
|
||||||
subprocess.call(['sudo', 'apt-get', 'update', '-y'])
|
|
||||||
return subprocess.call(['sudo', 'apt-get', 'install', '-y', 'postgresql-' + ver, 'expect-dev'] + 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.4.23')
|
|
||||||
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', '15.1-1')
|
|
||||||
platform = {'darwin': 'osx', 'win32': 'windows-x64', 'cygwin': 'windows-x64'}[sys.platform]
|
|
||||||
if platform == 'osx':
|
|
||||||
return subprocess.call(['brew', 'install', 'expect', 'postgresql@{0}'.format(version.split('.')[0])])
|
|
||||||
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))
|
|
||||||
return subprocess.call(['pgsql/bin/postgres', '-V'])
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
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())
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
versions = {'etcd': '9.6', 'etcd3': '14', 'consul': '13', 'exhibitor': '12', 'raft': '11', 'kubernetes': '15'}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
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 }}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
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:
|
|
||||||
if sys.platform == 'darwin':
|
|
||||||
version = os.environ.get('PGVERSION', '15.1-1')
|
|
||||||
path = '/usr/local/opt/postgresql@{0}/bin:.'.format(version.split('.')[0])
|
|
||||||
unbuffer = ['unbuffer']
|
|
||||||
else:
|
|
||||||
path = os.path.abspath(os.path.join('pgsql', 'bin'))
|
|
||||||
unbuffer = []
|
|
||||||
env['PATH'] = path + os.pathsep + env['PATH']
|
|
||||||
env['DCS'] = what
|
|
||||||
if what == 'kubernetes':
|
|
||||||
env['PATRONI_KUBERNETES_CONTEXT'] = 'k3d-k3s-default'
|
|
||||||
|
|
||||||
return subprocess.call(unbuffer + [sys.executable, '-m', 'behave'], env=env)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
sys.exit(main())
|
|
||||||
@@ -1,159 +0,0 @@
|
|||||||
name: Tests
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
env:
|
|
||||||
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
|
|
||||||
SECRETS_AVAILABLE: ${{ secrets.CODACY_PROJECT_TOKEN != '' }}
|
|
||||||
|
|
||||||
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 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: Set up Python 3.11
|
|
||||||
uses: actions/setup-python@v4
|
|
||||||
with:
|
|
||||||
python-version: 3.11
|
|
||||||
- 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.4.23
|
|
||||||
PGVERSION: 15.1-1 # for windows and macos
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu]
|
|
||||||
python-version: [3.7, '3.10']
|
|
||||||
dcs: [etcd, etcd3, consul, exhibitor, kubernetes, raft]
|
|
||||||
include:
|
|
||||||
- os: macos
|
|
||||||
python-version: 3.8
|
|
||||||
dcs: raft
|
|
||||||
- os: macos
|
|
||||||
python-version: 3.9
|
|
||||||
dcs: etcd
|
|
||||||
- os: macos
|
|
||||||
python-version: 3.11
|
|
||||||
dcs: etcd3
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- name: Set up Python
|
|
||||||
uses: actions/setup-python@v4
|
|
||||||
with:
|
|
||||||
python-version: ${{ matrix.python-version }}
|
|
||||||
- uses: nolar/setup-k3d-k3s@v1
|
|
||||||
if: matrix.dcs == 'kubernetes'
|
|
||||||
- name: Add postgresql and citus apt repo
|
|
||||||
run: |
|
|
||||||
sudo apt-get update -y
|
|
||||||
sudo apt-get install -y wget ca-certificates gnupg debian-archive-keyring apt-transport-https
|
|
||||||
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
|
||||||
sudo sh -c 'wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg'
|
|
||||||
sudo sh -c 'echo "deb [signed-by=/etc/apt/trusted.gpg.d/citusdata_community.gpg] https://repos.citusdata.com/community/ubuntu/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/citusdata_community.list'
|
|
||||||
sudo sh -c 'wget -qO - https://repos.citusdata.com/community/gpgkey | gpg --dearmor > /etc/apt/trusted.gpg.d/citusdata_community.gpg'
|
|
||||||
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
|
|
||||||
- name: Upload logs if behave failed
|
|
||||||
uses: actions/upload-artifact@v3
|
|
||||||
if: failure()
|
|
||||||
with:
|
|
||||||
name: behave-${{ matrix.os }}-${{ matrix.dcs }}-${{ matrix.python-version }}-logs
|
|
||||||
path: |
|
|
||||||
features/output/*_failed/*postgres?.*
|
|
||||||
features/output/*.log
|
|
||||||
if-no-files-found: error
|
|
||||||
retention-days: 5
|
|
||||||
- name: Generate coverage xml report
|
|
||||||
run: python -m coverage xml -o cobertura.xml
|
|
||||||
- name: Upload coverage to Codacy
|
|
||||||
run: bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r cobertura.xml -l Python --partial
|
|
||||||
if: ${{ env.SECRETS_AVAILABLE == 'true' }}
|
|
||||||
|
|
||||||
coveralls-finish:
|
|
||||||
name: Finalize coveralls.io
|
|
||||||
needs: unit
|
|
||||||
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 }}
|
|
||||||
|
|
||||||
codacy-final:
|
|
||||||
name: Finalize Codacy
|
|
||||||
needs: behave
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: bash <(curl -Ls https://coverage.codacy.com/get.sh) final
|
|
||||||
if: ${{ env.SECRETS_AVAILABLE == 'true' }}
|
|
||||||
+157
@@ -0,0 +1,157 @@
|
|||||||
|
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)
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
# global owners
|
|
||||||
* @CyberDem0n @hughcapet
|
|
||||||
+12
-16
@@ -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=15
|
ARG PG_MAJOR=10
|
||||||
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=3.3.13 CONFDVERSION=0.16.0
|
ENV ETCDVERSION=2.3.8 CONFDVERSION=0.16.0
|
||||||
|
|
||||||
RUN set -ex \
|
RUN set -ex \
|
||||||
&& export DEBIAN_FRONTEND=noninteractive \
|
&& export DEBIAN_FRONTEND=noninteractive \
|
||||||
@@ -25,12 +25,11 @@ 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
|
||||||
@@ -43,18 +42,18 @@ RUN set -ex \
|
|||||||
&& echo 'syntax on\nfiletype plugin indent on\nset mouse-=a\nautocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab' > /etc/vim/vimrc.local \
|
&& echo 'syntax on\nfiletype plugin indent on\nset mouse-=a\nautocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab' > /etc/vim/vimrc.local \
|
||||||
\
|
\
|
||||||
# Prepare postgres/patroni/haproxy environment
|
# Prepare postgres/patroni/haproxy environment
|
||||||
&& mkdir -p "$PGHOME/.config/patroni" /patroni /run/haproxy \
|
&& mkdir -p $PGHOME/.config/patroni /patroni /run/haproxy \
|
||||||
&& ln -s ../../postgres0.yml "$PGHOME/.config/patroni/patronictl.yaml" \
|
&& ln -s ../../postgres0.yml $PGHOME/.config/patroni/patronictl.yaml \
|
||||||
&& ln -s /patronictl.py /usr/local/bin/patronictl \
|
&& ln -s /patronictl.py /usr/local/bin/patronictl \
|
||||||
&& sed -i "s|/var/lib/postgresql.*|$PGHOME:/bin/bash|" /etc/passwd \
|
&& sed -i "s|/var/lib/postgresql.*|$PGHOME:/bin/bash|" /etc/passwd \
|
||||||
&& chown -R postgres:postgres /var/log \
|
&& chown -R postgres:postgres /var/log \
|
||||||
\
|
\
|
||||||
# Download etcd
|
# Download etcd
|
||||||
&& curl -sL "https://github.com/coreos/etcd/releases/download/v$ETCDVERSION/etcd-v$ETCDVERSION-linux-$(dpkg --print-architecture).tar.gz" \
|
&& curl -sL https://github.com/coreos/etcd/releases/download/v${ETCDVERSION}/etcd-v${ETCDVERSION}-linux-amd64.tar.gz \
|
||||||
| tar xz -C /usr/local/bin --strip=1 --wildcards --no-anchored etcd etcdctl \
|
| tar xz -C /usr/local/bin --strip=1 --wildcards --no-anchored etcd etcdctl \
|
||||||
\
|
\
|
||||||
# Download confd
|
# Download confd
|
||||||
&& curl -sL "https://github.com/kelseyhightower/confd/releases/download/v$CONFDVERSION/confd-$CONFDVERSION-linux-$(dpkg --print-architecture)" \
|
&& curl -sL https://github.com/kelseyhightower/confd/releases/download/v${CONFDVERSION}/confd-${CONFDVERSION}-linux-amd64 \
|
||||||
> /usr/local/bin/confd && chmod +x /usr/local/bin/confd \
|
> /usr/local/bin/confd && chmod +x /usr/local/bin/confd \
|
||||||
\
|
\
|
||||||
# Clean up all useless packages and some files
|
# Clean up all useless packages and some files
|
||||||
@@ -90,7 +89,7 @@ RUN set -ex \
|
|||||||
&& find /usr/bin -xtype l -delete \
|
&& find /usr/bin -xtype l -delete \
|
||||||
&& find /var/log -type f -exec truncate --size 0 {} \; \
|
&& find /var/log -type f -exec truncate --size 0 {} \; \
|
||||||
&& find /usr/lib/python3/dist-packages -name '*test*' | xargs rm -fr \
|
&& find /usr/lib/python3/dist-packages -name '*test*' | xargs rm -fr \
|
||||||
&& find /lib/$(uname -m)-linux-gnu/security -type f ! -name pam_env.so ! -name pam_permit.so ! -name pam_unix.so -delete
|
&& find /lib/x86_64-linux-gnu/security -type f ! -name pam_env.so ! -name pam_permit.so ! -name pam_unix.so -delete
|
||||||
|
|
||||||
# perform compression if it is necessary
|
# perform compression if it is necessary
|
||||||
ARG COMPRESS
|
ARG COMPRESS
|
||||||
@@ -99,10 +98,8 @@ RUN if [ "$COMPRESS" = "true" ]; then \
|
|||||||
# Allow certain sudo commands from postgres
|
# Allow certain sudo commands from postgres
|
||||||
&& echo 'postgres ALL=(ALL) NOPASSWD: /bin/tar xpJf /a.tar.xz -C /, /bin/rm /a.tar.xz, /bin/ln -snf dash /bin/sh' >> /etc/sudoers \
|
&& echo 'postgres ALL=(ALL) NOPASSWD: /bin/tar xpJf /a.tar.xz -C /, /bin/rm /a.tar.xz, /bin/ln -snf dash /bin/sh' >> /etc/sudoers \
|
||||||
&& ln -snf busybox /bin/sh \
|
&& ln -snf busybox /bin/sh \
|
||||||
&& arch=$(uname -m) \
|
&& files="/bin/sh /usr/bin/sudo /usr/lib/sudo/sudoers.so /lib/x86_64-linux-gnu/security/pam_*.so" \
|
||||||
&& darch=$(uname -m | sed 's/_/-/') \
|
&& libs="$(ldd $files | awk '{print $3;}' | grep '^/' | sort -u) /lib/x86_64-linux-gnu/ld-linux-x86-64.so.* /lib/x86_64-linux-gnu/libnsl.so.* /lib/x86_64-linux-gnu/libnss_compat.so.*" \
|
||||||
&& files="/bin/sh /usr/bin/sudo /usr/lib/sudo/sudoers.so /lib/$arch-linux-gnu/security/pam_*.so" \
|
|
||||||
&& libs="$(ldd $files | awk '{print $3;}' | grep '^/' | sort -u) /lib/ld-linux-$darch.so.* /lib/$arch-linux-gnu/ld-linux-$darch.so.* /lib/$arch-linux-gnu/libnsl.so.* /lib/$arch-linux-gnu/libnss_compat.so.* /lib/$arch-linux-gnu/libnss_files.so.*" \
|
|
||||||
&& (echo /var/run $files $libs | tr ' ' '\n' && realpath $files $libs) | sort -u | sed 's/^\///' > /exclude \
|
&& (echo /var/run $files $libs | tr ' ' '\n' && realpath $files $libs) | sort -u | sed 's/^\///' > /exclude \
|
||||||
&& find /etc/alternatives -xtype l -delete \
|
&& find /etc/alternatives -xtype l -delete \
|
||||||
&& save_dirs="usr lib var bin sbin etc/ssl etc/init.d etc/alternatives etc/apt" \
|
&& save_dirs="usr lib var bin sbin etc/ssl etc/init.d etc/alternatives etc/apt" \
|
||||||
@@ -119,7 +116,7 @@ RUN if [ "$COMPRESS" = "true" ]; then \
|
|||||||
FROM scratch
|
FROM scratch
|
||||||
COPY --from=builder / /
|
COPY --from=builder / /
|
||||||
|
|
||||||
LABEL maintainer="Alexander Kukushkin <akukushkin@microsoft.com>"
|
LABEL maintainer="Alexander Kukushkin <alexander.kukushkin@zalando.de>"
|
||||||
|
|
||||||
ARG PG_MAJOR
|
ARG PG_MAJOR
|
||||||
ARG COMPRESS
|
ARG COMPRESS
|
||||||
@@ -152,8 +149,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
|
||||||
|
|
||||||
|
|||||||
@@ -1,173 +0,0 @@
|
|||||||
## 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
|
|
||||||
ARG PG_MAJOR=15
|
|
||||||
ARG COMPRESS=false
|
|
||||||
ARG PGHOME=/home/postgres
|
|
||||||
ARG PGDATA=$PGHOME/data
|
|
||||||
ARG LC_ALL=C.UTF-8
|
|
||||||
ARG LANG=C.UTF-8
|
|
||||||
|
|
||||||
FROM postgres:$PG_MAJOR as builder
|
|
||||||
|
|
||||||
ARG PGHOME
|
|
||||||
ARG PGDATA
|
|
||||||
ARG LC_ALL
|
|
||||||
ARG LANG
|
|
||||||
|
|
||||||
ENV ETCDVERSION=3.3.13 CONFDVERSION=0.16.0
|
|
||||||
|
|
||||||
RUN set -ex \
|
|
||||||
&& export DEBIAN_FRONTEND=noninteractive \
|
|
||||||
&& echo 'APT::Install-Recommends "0";\nAPT::Install-Suggests "0";' > /etc/apt/apt.conf.d/01norecommend \
|
|
||||||
&& apt-get update -y \
|
|
||||||
# postgres:10 is based on debian, which has the patroni package. We will install all required dependencies
|
|
||||||
&& apt-cache depends patroni | sed -n -e 's/.*Depends: \(python3-.\+\)$/\1/p' \
|
|
||||||
| grep -Ev '^python3-(sphinx|etcd|consul|kazoo|kubernetes)' \
|
|
||||||
| xargs apt-get install -y vim curl less jq locales haproxy sudo \
|
|
||||||
python3-etcd python3-kazoo python3-pip busybox \
|
|
||||||
net-tools iputils-ping --fix-missing \
|
|
||||||
&& curl https://install.citusdata.com/community/deb.sh | bash \
|
|
||||||
&& apt-get -y install postgresql-$PG_MAJOR-citus-11.2 \
|
|
||||||
&& pip3 install dumb-init \
|
|
||||||
\
|
|
||||||
# Cleanup all locales but en_US.UTF-8
|
|
||||||
&& find /usr/share/i18n/charmaps/ -type f ! -name UTF-8.gz -delete \
|
|
||||||
&& find /usr/share/i18n/locales/ -type f ! -name en_US ! -name en_GB ! -name i18n* ! -name iso14651_t1 ! -name iso14651_t1_common ! -name 'translit_*' -delete \
|
|
||||||
&& echo 'en_US.UTF-8 UTF-8' > /usr/share/i18n/SUPPORTED \
|
|
||||||
\
|
|
||||||
# Make sure we have a en_US.UTF-8 locale available
|
|
||||||
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
|
|
||||||
\
|
|
||||||
# haproxy dummy config
|
|
||||||
&& echo 'global\n stats socket /run/haproxy/admin.sock mode 660 level admin' > /etc/haproxy/haproxy.cfg \
|
|
||||||
\
|
|
||||||
# vim config
|
|
||||||
&& echo 'syntax on\nfiletype plugin indent on\nset mouse-=a\nautocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab' > /etc/vim/vimrc.local \
|
|
||||||
\
|
|
||||||
# Prepare postgres/patroni/haproxy environment
|
|
||||||
&& mkdir -p $PGHOME/.config/patroni /patroni /run/haproxy \
|
|
||||||
&& ln -s ../../postgres0.yml $PGHOME/.config/patroni/patronictl.yaml \
|
|
||||||
&& ln -s /patronictl.py /usr/local/bin/patronictl \
|
|
||||||
&& sed -i "s|/var/lib/postgresql.*|$PGHOME:/bin/bash|" /etc/passwd \
|
|
||||||
&& chown -R postgres:postgres /var/log \
|
|
||||||
\
|
|
||||||
# Download etcd
|
|
||||||
&& curl -sL https://github.com/coreos/etcd/releases/download/v${ETCDVERSION}/etcd-v${ETCDVERSION}-linux-$(dpkg --print-architecture).tar.gz \
|
|
||||||
| tar xz -C /usr/local/bin --strip=1 --wildcards --no-anchored etcd etcdctl \
|
|
||||||
\
|
|
||||||
# Download confd
|
|
||||||
&& curl -sL https://github.com/kelseyhightower/confd/releases/download/v${CONFDVERSION}/confd-${CONFDVERSION}-linux-$(dpkg --print-architecture) \
|
|
||||||
> /usr/local/bin/confd && chmod +x /usr/local/bin/confd \
|
|
||||||
# Prepare client cert for HAProxy
|
|
||||||
&& cat /etc/ssl/private/ssl-cert-snakeoil.key /etc/ssl/certs/ssl-cert-snakeoil.pem > /etc/ssl/private/ssl-cert-snakeoil.crt \
|
|
||||||
\
|
|
||||||
# Clean up all useless packages and some files
|
|
||||||
&& apt-get purge -y --allow-remove-essential python3-pip gzip bzip2 util-linux e2fsprogs \
|
|
||||||
libmagic1 bsdmainutils login ncurses-bin libmagic-mgc e2fslibs bsdutils \
|
|
||||||
exim4-config gnupg-agent dirmngr libpython2.7-stdlib libpython2.7-minimal \
|
|
||||||
&& apt-get autoremove -y \
|
|
||||||
&& apt-get clean -y \
|
|
||||||
&& rm -rf /var/lib/apt/lists/* \
|
|
||||||
/root/.cache \
|
|
||||||
/var/cache/debconf/* \
|
|
||||||
/etc/rc?.d \
|
|
||||||
/etc/systemd \
|
|
||||||
/docker-entrypoint* \
|
|
||||||
/sbin/pam* \
|
|
||||||
/sbin/swap* \
|
|
||||||
/sbin/unix* \
|
|
||||||
/usr/local/bin/gosu \
|
|
||||||
/usr/sbin/[acgipr]* \
|
|
||||||
/usr/sbin/*user* \
|
|
||||||
/usr/share/doc* \
|
|
||||||
/usr/share/man \
|
|
||||||
/usr/share/info \
|
|
||||||
/usr/share/i18n/locales/translit_hangul \
|
|
||||||
/usr/share/locale/?? \
|
|
||||||
/usr/share/locale/??_?? \
|
|
||||||
/usr/share/postgresql/*/man \
|
|
||||||
/usr/share/postgresql-common/pg_wrapper \
|
|
||||||
/usr/share/vim/vim80/doc \
|
|
||||||
/usr/share/vim/vim80/lang \
|
|
||||||
/usr/share/vim/vim80/tutor \
|
|
||||||
# /var/lib/dpkg/info/* \
|
|
||||||
&& find /usr/bin -xtype l -delete \
|
|
||||||
&& find /var/log -type f -exec truncate --size 0 {} \; \
|
|
||||||
&& find /usr/lib/python3/dist-packages -name '*test*' | xargs rm -fr \
|
|
||||||
&& find /lib/$(uname -m)-linux-gnu/security -type f ! -name pam_env.so ! -name pam_permit.so ! -name pam_unix.so -delete
|
|
||||||
|
|
||||||
# perform compression if it is necessary
|
|
||||||
ARG COMPRESS
|
|
||||||
RUN if [ "$COMPRESS" = "true" ]; then \
|
|
||||||
set -ex \
|
|
||||||
# Allow certain sudo commands from postgres
|
|
||||||
&& echo 'postgres ALL=(ALL) NOPASSWD: /bin/tar xpJf /a.tar.xz -C /, /bin/rm /a.tar.xz, /bin/ln -snf dash /bin/sh' >> /etc/sudoers \
|
|
||||||
&& ln -snf busybox /bin/sh \
|
|
||||||
&& arch=$(uname -m) \
|
|
||||||
&& darch=$(uname -m | sed 's/_/-/') \
|
|
||||||
&& files="/bin/sh /usr/bin/sudo /usr/lib/sudo/sudoers.so /lib/$arch-linux-gnu/security/pam_*.so" \
|
|
||||||
&& libs="$(ldd $files | awk '{print $3;}' | grep '^/' | sort -u) /lib/ld-linux-$darch.so.* /lib/$arch-linux-gnu/ld-linux-$darch.so.* /lib/$arch-linux-gnu/libnsl.so.* /lib/$arch-linux-gnu/libnss_compat.so.* /lib/$arch-linux-gnu/libnss_files.so.*" \
|
|
||||||
&& (echo /var/run $files $libs | tr ' ' '\n' && realpath $files $libs) | sort -u | sed 's/^\///' > /exclude \
|
|
||||||
&& find /etc/alternatives -xtype l -delete \
|
|
||||||
&& save_dirs="usr lib var bin sbin etc/ssl etc/init.d etc/alternatives etc/apt" \
|
|
||||||
&& XZ_OPT=-e9v tar -X /exclude -cpJf a.tar.xz $save_dirs \
|
|
||||||
# we call "cat /exclude" to avoid including files from the $save_dirs that are also among
|
|
||||||
# the exceptions listed in the /exclude, as "uniq -u" eliminates all non-unique lines.
|
|
||||||
# By calling "cat /exclude" a second time we guarantee that there will be at least two lines
|
|
||||||
# for each exception and therefore they will be excluded from the output passed to 'rm'.
|
|
||||||
&& /bin/busybox sh -c "(find $save_dirs -not -type d && cat /exclude /exclude && echo exclude) | sort | uniq -u | xargs /bin/busybox rm" \
|
|
||||||
&& /bin/busybox --install -s \
|
|
||||||
&& /bin/busybox sh -c "find $save_dirs -type d -depth -exec rmdir -p {} \; 2> /dev/null"; \
|
|
||||||
else \
|
|
||||||
/bin/busybox --install -s; \
|
|
||||||
fi
|
|
||||||
|
|
||||||
FROM scratch
|
|
||||||
COPY --from=builder / /
|
|
||||||
|
|
||||||
LABEL maintainer="Alexander Kukushkin <[email protected]>"
|
|
||||||
|
|
||||||
ARG PG_MAJOR
|
|
||||||
ARG COMPRESS
|
|
||||||
ARG PGHOME
|
|
||||||
ARG PGDATA
|
|
||||||
ARG LC_ALL
|
|
||||||
ARG LANG
|
|
||||||
|
|
||||||
ARG PGBIN=/usr/lib/postgresql/$PG_MAJOR/bin
|
|
||||||
|
|
||||||
ENV LC_ALL=$LC_ALL LANG=$LANG EDITOR=/usr/bin/editor
|
|
||||||
ENV PGDATA=$PGDATA PATH=$PATH:$PGBIN
|
|
||||||
|
|
||||||
COPY patroni /patroni/
|
|
||||||
COPY extras/confd/conf.d/haproxy.toml /etc/confd/conf.d/
|
|
||||||
COPY extras/confd/templates/haproxy-citus.tmpl /etc/confd/templates/haproxy.tmpl
|
|
||||||
COPY patroni*.py docker/entrypoint.sh /
|
|
||||||
COPY postgres?.yml $PGHOME/
|
|
||||||
|
|
||||||
WORKDIR $PGHOME
|
|
||||||
|
|
||||||
RUN sed -i 's/env python/&3/' /patroni*.py \
|
|
||||||
# "fix" patroni configs
|
|
||||||
&& sed -i 's/^\( connect_address:\| - host\)/#&/' postgres?.yml \
|
|
||||||
&& sed -i 's/^ listen: 127.0.0.1/ listen: 0.0.0.0/' postgres?.yml \
|
|
||||||
&& sed -i "s|^\( data_dir: \).*|\1$PGDATA|" postgres?.yml \
|
|
||||||
&& sed -i "s|^#\( bin_dir: \).*|\1$PGBIN|" postgres?.yml \
|
|
||||||
&& sed -i 's/^ - encoding: UTF8/ - locale: en_US.UTF-8\n&/' postgres?.yml \
|
|
||||||
&& sed -i 's/^scope:/log:\n loggers:\n patroni.postgresql.citus: DEBUG\n#&/' postgres?.yml \
|
|
||||||
&& sed -i 's/^\(name\|etcd\| host\| authentication\| pg_hba\| parameters\):/#&/' postgres?.yml \
|
|
||||||
&& sed -i 's/^ \(replication\|superuser\|rewind\|unix_socket_directories\|\(\( \)\{0,1\}\(username\|password\)\)\):/#&/' postgres?.yml \
|
|
||||||
&& sed -i 's/^postgresql:/&\n basebackup:\n checkpoint: fast/' postgres?.yml \
|
|
||||||
&& sed -i 's|^ parameters:| pg_hba:\n - local all all trust\n - hostssl replication all all md5 clientcert=verify-ca\n - hostssl all all all md5 clientcert=verify-ca\n&\n max_connections: 100\n shared_buffers: 16MB\n ssl: "on"\n ssl_ca_file: /etc/ssl/certs/ssl-cert-snakeoil.pem\n ssl_cert_file: /etc/ssl/certs/ssl-cert-snakeoil.pem\n ssl_key_file: /etc/ssl/private/ssl-cert-snakeoil.key\n citus.node_conninfo: "sslrootcert=/etc/ssl/certs/ssl-cert-snakeoil.pem sslkey=/etc/ssl/private/ssl-cert-snakeoil.key sslcert=/etc/ssl/certs/ssl-cert-snakeoil.pem sslmode=verify-ca"|' postgres?.yml \
|
|
||||||
&& sed -i 's/^#\(ctl\| certfile\| keyfile\)/\1/' postgres?.yml \
|
|
||||||
&& sed -i 's|^# cafile: .*$| verify_client: required\n cafile: /etc/ssl/certs/ssl-cert-snakeoil.pem|' postgres?.yml \
|
|
||||||
&& sed -i 's|^# cacert: .*$| cacert: /etc/ssl/certs/ssl-cert-snakeoil.pem|' postgres?.yml \
|
|
||||||
&& sed -i 's/^# insecure: .*/ insecure: on/' postgres?.yml \
|
|
||||||
# client cert for HAProxy to access Patroni REST API
|
|
||||||
&& if [ "$COMPRESS" = "true" ]; then chmod u+s /usr/bin/sudo; fi \
|
|
||||||
&& chmod +s /bin/ping \
|
|
||||||
&& chown -R postgres:postgres $PGHOME /run /etc/haproxy
|
|
||||||
|
|
||||||
USER postgres
|
|
||||||
|
|
||||||
ENTRYPOINT ["/bin/sh", "/entrypoint.sh"]
|
|
||||||
+3
-2
@@ -1,2 +1,3 @@
|
|||||||
Alexander Kukushkin <akukushkin@microsoft.com>
|
Alexander Kukushkin <alexander.kukushkin@zalando.de>
|
||||||
Polina Bungina <polina.bungina@zalando.de>
|
Feike Steenbergen <feike.steenbergen@zalando.de>
|
||||||
|
Oleksii Kliukin <[email protected]>
|
||||||
|
|||||||
+9
-21
@@ -1,4 +1,4 @@
|
|||||||
|Tests Status| |Coverage Status|
|
|Build 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,10 +12,6 @@ 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 Citus users**: Starting from 3.0 Patroni nicely integrates with the `Citus <https://github.com/citusdata/citus>`__ database extension to Postgres. Please check the `Citus support page <https://github.com/zalando/patroni/blob/master/docs/citus.rst>`__ in the Patroni documentation for more info about how to use Patroni high availability together with a Citus distributed cluster.
|
|
||||||
|
|
||||||
**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::
|
||||||
@@ -49,7 +45,7 @@ We report new releases information `here <https://github.com/zalando/patroni/rel
|
|||||||
Community
|
Community
|
||||||
=========
|
=========
|
||||||
|
|
||||||
There are two places to connect with the Patroni community: `on github <https://github.com/zalando/patroni>`__, via Issues and PRs, and on channel `#patroni <https://postgresteam.slack.com/archives/C9XPYG92A>`__ in the `PostgreSQL Slack <https://join.slack.com/t/postgresteam/shared_invite/zt-1qj14i9sj-E9WqIFlvcOiHsEk2yFEMjA>`__. If you're using Patroni, or just interested, please join us.
|
There are two places to connect with the Patroni community: `on github <https://github.com/zalando/patroni>`__, via Issues and PRs, and on channel #patroni in the `PostgreSQL Slack <https://postgres-slack.herokuapp.com/>`__. If you're using Patroni, or just interested, please join us.
|
||||||
|
|
||||||
===================================
|
===================================
|
||||||
Technical Requirements/Installation
|
Technical Requirements/Installation
|
||||||
@@ -63,7 +59,7 @@ To install requirements on a Mac, run the following:
|
|||||||
|
|
||||||
brew install postgresql etcd haproxy libyaml python
|
brew install postgresql etcd haproxy libyaml python
|
||||||
|
|
||||||
**Psycopg**
|
**Psycopg2**
|
||||||
|
|
||||||
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.
|
||||||
@@ -90,12 +86,6 @@ 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:
|
||||||
@@ -106,7 +96,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 or etcd3
|
etcd
|
||||||
`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
|
||||||
@@ -116,8 +106,6 @@ 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
|
||||||
|
|
||||||
@@ -127,7 +115,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 bootstrap scripts (i.e. WAL-E) should be installed independently of Patroni.
|
Note that external tools to call in the replica creation or custom bootstap scripts (i.e. WAL-E) should be installed independently of Patroni.
|
||||||
|
|
||||||
=======================
|
=======================
|
||||||
Running and Configuring
|
Running and Configuring
|
||||||
@@ -136,7 +124,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 --enable-v2=true
|
> etcd --data-dir=data/etcd
|
||||||
> ./patroni.py postgres0.yml
|
> ./patroni.py postgres0.yml
|
||||||
> ./patroni.py postgres1.yml
|
> ./patroni.py postgres1.yml
|
||||||
|
|
||||||
@@ -179,7 +167,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.
|
||||||
|
|
||||||
.. |Tests Status| image:: https://github.com/zalando/patroni/actions/workflows/tests.yaml/badge.svg
|
.. |Build Status| image:: https://travis-ci.org/zalando/patroni.svg?branch=master
|
||||||
:target: https://github.com/zalando/patroni/actions/workflows/tests.yaml?query=branch%3Amaster
|
: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/github/zalando/patroni?branch=master
|
:target: https://coveralls.io/r/zalando/patroni?branch=master
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
Failover
|
||||||
|
========
|
||||||
|
- When determining who should become master, include the minor version of PostgreSQL in the decision.
|
||||||
|
|
||||||
|
Configuration
|
||||||
|
==============
|
||||||
|
- Provide a way to change pg_hba.conf of a running cluster on the Patroni level, without changing individual nodes.
|
||||||
|
- Provide hooks to store and retrieve cluster-wide passwords without exposing them in a plain-text form to unauthorized users.
|
||||||
|
|
||||||
|
Documentation
|
||||||
|
==============
|
||||||
|
- Document how to run cascading replication and possibly initialize the cluster without an access to the master node.
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
# docker compose file for running a Citus cluster
|
|
||||||
# with 3-node etcd v3 cluster as the DCS and one haproxy node.
|
|
||||||
# The Citus cluster has a coordinator (3 nodes)
|
|
||||||
# and two worker clusters (2 nodes).
|
|
||||||
#
|
|
||||||
# Before starting it up you need to build the docker image:
|
|
||||||
# $ docker build -f Dockerfile.citus -t patroni-citus .
|
|
||||||
# The cluster could be started as:
|
|
||||||
# $ docker-compose -f docker-compose-citus.yml up -d
|
|
||||||
# You can read more about it in the:
|
|
||||||
# https://github.com/zalando/patroni/blob/master/docker/README.md#citus-cluster
|
|
||||||
version: "2"
|
|
||||||
|
|
||||||
networks:
|
|
||||||
demo:
|
|
||||||
|
|
||||||
services:
|
|
||||||
etcd1: &etcd
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
environment:
|
|
||||||
ETCDCTL_API: 3
|
|
||||||
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
|
|
||||||
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
|
|
||||||
ETCD_INITIAL_CLUSTER: etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
|
|
||||||
ETCD_INITIAL_CLUSTER_STATE: new
|
|
||||||
ETCD_INITIAL_CLUSTER_TOKEN: tutorial
|
|
||||||
container_name: demo-etcd1
|
|
||||||
hostname: etcd1
|
|
||||||
command: etcd -name etcd1 -initial-advertise-peer-urls http://etcd1:2380
|
|
||||||
|
|
||||||
etcd2:
|
|
||||||
<<: *etcd
|
|
||||||
container_name: demo-etcd2
|
|
||||||
hostname: etcd2
|
|
||||||
command: etcd -name etcd2 -initial-advertise-peer-urls http://etcd2:2380
|
|
||||||
|
|
||||||
etcd3:
|
|
||||||
<<: *etcd
|
|
||||||
container_name: demo-etcd3
|
|
||||||
hostname: etcd3
|
|
||||||
command: etcd -name etcd3 -initial-advertise-peer-urls http://etcd3:2380
|
|
||||||
|
|
||||||
haproxy:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: haproxy
|
|
||||||
container_name: demo-haproxy
|
|
||||||
ports:
|
|
||||||
- "5000:5000" # Access to the coorinator primary
|
|
||||||
- "5001:5001" # Load-balancing across workers primaries
|
|
||||||
command: haproxy
|
|
||||||
environment: &haproxy_env
|
|
||||||
ETCDCTL_API: 3
|
|
||||||
ETCDCTL_ENDPOINTS: http://etcd1:2379,http://etcd2:2379,http://etcd3:2379
|
|
||||||
PATRONI_ETCD3_HOSTS: "'etcd1:2379','etcd2:2379','etcd3:2379'"
|
|
||||||
PATRONI_SCOPE: demo
|
|
||||||
PATRONI_CITUS_GROUP: 0
|
|
||||||
PATRONI_CITUS_DATABASE: citus
|
|
||||||
PGSSLMODE: verify-ca
|
|
||||||
PGSSLKEY: /etc/ssl/private/ssl-cert-snakeoil.key
|
|
||||||
PGSSLCERT: /etc/ssl/certs/ssl-cert-snakeoil.pem
|
|
||||||
PGSSLROOTCERT: /etc/ssl/certs/ssl-cert-snakeoil.pem
|
|
||||||
|
|
||||||
coord1:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: coord1
|
|
||||||
container_name: demo-coord1
|
|
||||||
environment: &coord_env
|
|
||||||
<<: *haproxy_env
|
|
||||||
PATRONI_NAME: coord1
|
|
||||||
PATRONI_CITUS_GROUP: 0
|
|
||||||
|
|
||||||
coord2:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: coord2
|
|
||||||
container_name: demo-coord2
|
|
||||||
environment:
|
|
||||||
<<: *coord_env
|
|
||||||
PATRONI_NAME: coord2
|
|
||||||
|
|
||||||
coord3:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: coord3
|
|
||||||
container_name: demo-coord3
|
|
||||||
environment:
|
|
||||||
<<: *coord_env
|
|
||||||
PATRONI_NAME: coord3
|
|
||||||
|
|
||||||
|
|
||||||
work1-1:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: work1-1
|
|
||||||
container_name: demo-work1-1
|
|
||||||
environment: &work1_env
|
|
||||||
<<: *haproxy_env
|
|
||||||
PATRONI_NAME: work1-1
|
|
||||||
PATRONI_CITUS_GROUP: 1
|
|
||||||
|
|
||||||
work1-2:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: work1-2
|
|
||||||
container_name: demo-work1-2
|
|
||||||
environment:
|
|
||||||
<<: *work1_env
|
|
||||||
PATRONI_NAME: work1-2
|
|
||||||
|
|
||||||
|
|
||||||
work2-1:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: work2-1
|
|
||||||
container_name: demo-work2-1
|
|
||||||
environment: &work2_env
|
|
||||||
<<: *haproxy_env
|
|
||||||
PATRONI_NAME: work2-1
|
|
||||||
PATRONI_CITUS_GROUP: 2
|
|
||||||
|
|
||||||
work2-2:
|
|
||||||
image: patroni-citus
|
|
||||||
networks: [ demo ]
|
|
||||||
env_file: docker/patroni.env
|
|
||||||
hostname: work2-2
|
|
||||||
container_name: demo-work2-2
|
|
||||||
environment:
|
|
||||||
<<: *work2_env
|
|
||||||
PATRONI_NAME: work2-2
|
|
||||||
+35
-50
@@ -1,43 +1,62 @@
|
|||||||
# docker compose file for running a 3-node PostgreSQL cluster
|
# docker compose file for running a 3-node PostgreSQL cluster
|
||||||
# with 3-node etcd cluster as the DCS and one haproxy node
|
# with 3-node etcd cluster as the DCS and one haproxy node
|
||||||
#
|
|
||||||
# requires a patroni image build from the Dockerfile:
|
|
||||||
# $ docker build -t patroni .
|
|
||||||
# The cluster could be started as:
|
|
||||||
# $ docker-compose up -d
|
|
||||||
# You can read more about it in the:
|
|
||||||
# https://github.com/zalando/patroni/blob/master/docker/README.md
|
|
||||||
version: "2"
|
version: "2"
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
demo:
|
demo:
|
||||||
|
|
||||||
services:
|
services:
|
||||||
etcd1: &etcd
|
etcd1:
|
||||||
image: patroni
|
image: patroni
|
||||||
networks: [ demo ]
|
networks: [ demo ]
|
||||||
environment:
|
env_file: docker/etcd.env
|
||||||
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:
|
||||||
<<: *etcd
|
image: patroni
|
||||||
|
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:
|
||||||
<<: *etcd
|
image: patroni
|
||||||
|
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 ]
|
||||||
@@ -48,37 +67,3 @@ 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
|
|
||||||
|
|||||||
+7
-196
@@ -1,10 +1,10 @@
|
|||||||
# Dockerfile and Dockerfile.citus
|
# Patroni Dockerfile
|
||||||
You can run Patroni in a docker container using these Dockerfiles
|
You can run Patroni in a docker container using this Dockerfile
|
||||||
|
|
||||||
They are meant in aiding development of Patroni and quick testing of features and not a production-worthy!
|
This Dockerfile is meant in aiding development of Patroni and quick testing of features. It is not a production-worthy
|
||||||
|
Dockerfile
|
||||||
|
|
||||||
docker build -t patroni .
|
docker build -t patroni .
|
||||||
docker build -f Dockerfile.citus -t patroni-citus .
|
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
|
|
||||||
@@ -12,10 +12,7 @@ They are meant in aiding development of Patroni and quick testing of features an
|
|||||||
|
|
||||||
docker run -d patroni
|
docker run -d patroni
|
||||||
|
|
||||||
## Three-node Patroni cluster
|
## Three-node Patroni cluster with three-node etcd cluster and one haproxy container using docker-compose
|
||||||
|
|
||||||
In addition to three Patroni containers the stack starts three containers with etcd (forming a three-node cluster), and one container with haproxy.
|
|
||||||
The haproxy listens on ports 5000 (connects to the primary) and 5001 (does load-balancing between healthy standbys).
|
|
||||||
|
|
||||||
Example session:
|
Example session:
|
||||||
|
|
||||||
@@ -95,8 +92,7 @@ Example session:
|
|||||||
b2e169fcb8a34028: name=etcd1 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false
|
b2e169fcb8a34028: name=etcd1 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false
|
||||||
postgres@patroni1:~$ exit
|
postgres@patroni1:~$ exit
|
||||||
|
|
||||||
$ docker exec -ti demo-haproxy bash
|
$ psql -h localhost -p 5000 -U postgres -W
|
||||||
postgres@haproxy:~$ psql -h localhost -p 5000 -U postgres -W
|
|
||||||
Password: postgres
|
Password: postgres
|
||||||
psql (11.2 (Ubuntu 11.2-1.pgdg18.04+1), server 10.7 (Debian 10.7-1.pgdg90+1))
|
psql (11.2 (Ubuntu 11.2-1.pgdg18.04+1), server 10.7 (Debian 10.7-1.pgdg90+1))
|
||||||
Type "help" for help.
|
Type "help" for help.
|
||||||
@@ -109,7 +105,7 @@ Example session:
|
|||||||
|
|
||||||
localhost/postgres=# \q
|
localhost/postgres=# \q
|
||||||
|
|
||||||
$postgres@haproxy:~ psql -h localhost -p 5001 -U postgres -W
|
$ psql -h localhost -p 5001 -U postgres -W
|
||||||
Password: postgres
|
Password: postgres
|
||||||
psql (11.2 (Ubuntu 11.2-1.pgdg18.04+1), server 10.7 (Debian 10.7-1.pgdg90+1))
|
psql (11.2 (Ubuntu 11.2-1.pgdg18.04+1), server 10.7 (Debian 10.7-1.pgdg90+1))
|
||||||
Type "help" for help.
|
Type "help" for help.
|
||||||
@@ -119,188 +115,3 @@ Example session:
|
|||||||
───────────────────
|
───────────────────
|
||||||
t
|
t
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
## Citus cluster
|
|
||||||
|
|
||||||
The stack starts three containers with etcd (forming a three-node etcd cluster), seven containers with Patroni+PostgreSQL+Citus (three coordinator nodes, and two worker clusters with two nodes each), and one container with haproxy.
|
|
||||||
The haproxy listens on ports 5000 (connects to the coordinator primary) and 5001 (does load-balancing between worker primary nodes).
|
|
||||||
|
|
||||||
Example session:
|
|
||||||
|
|
||||||
$ docker-compose -f docker-compose-citus.yml up -d
|
|
||||||
Creating demo-work2-1 ... done
|
|
||||||
Creating demo-work1-1 ... done
|
|
||||||
Creating demo-etcd2 ... done
|
|
||||||
Creating demo-etcd1 ... done
|
|
||||||
Creating demo-coord3 ... done
|
|
||||||
Creating demo-etcd3 ... done
|
|
||||||
Creating demo-coord1 ... done
|
|
||||||
Creating demo-haproxy ... done
|
|
||||||
Creating demo-work2-2 ... done
|
|
||||||
Creating demo-coord2 ... done
|
|
||||||
Creating demo-work1-2 ... done
|
|
||||||
|
|
||||||
$ docker ps
|
|
||||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
||||||
852d8885a612 patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 3 seconds demo-coord3
|
|
||||||
cdd692f947ab patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 3 seconds demo-work1-2
|
|
||||||
9f4e340b36da patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 3 seconds demo-etcd3
|
|
||||||
d69c129a960a patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 4 seconds demo-etcd1
|
|
||||||
c5849689b8cd patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 4 seconds demo-coord1
|
|
||||||
c9d72bd6217d patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 3 seconds demo-work2-1
|
|
||||||
24b1b43efa05 patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 4 seconds demo-coord2
|
|
||||||
cb0cc2b4ca0a patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 3 seconds demo-work2-2
|
|
||||||
9796c6b8aad5 patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 5 seconds demo-work1-1
|
|
||||||
8baccd74dcae patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 4 seconds demo-etcd2
|
|
||||||
353ec62a0187 patroni-citus "/bin/sh /entrypoint…" 6 seconds ago Up 4 seconds 0.0.0.0:5000-5001->5000-5001/tcp demo-haproxy
|
|
||||||
|
|
||||||
$ docker logs demo-coord1
|
|
||||||
2023-01-05 15:09:31,295 INFO: Selected new etcd server http://172.27.0.4:2379
|
|
||||||
2023-01-05 15:09:31,388 INFO: Lock owner: None; I am coord1
|
|
||||||
2023-01-05 15:09:31,501 INFO: trying to bootstrap a new cluster
|
|
||||||
...
|
|
||||||
2023-01-05 15:09:45,096 INFO: postmaster pid=39
|
|
||||||
localhost:5432 - no response
|
|
||||||
2023-01-05 15:09:45.137 UTC [39] LOG: starting PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
|
|
||||||
2023-01-05 15:09:45.137 UTC [39] LOG: listening on IPv4 address "0.0.0.0", port 5432
|
|
||||||
2023-01-05 15:09:45.152 UTC [39] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
|
|
||||||
2023-01-05 15:09:45.177 UTC [43] LOG: database system was shut down at 2023-01-05 15:09:32 UTC
|
|
||||||
2023-01-05 15:09:45.193 UTC [39] LOG: database system is ready to accept connections
|
|
||||||
localhost:5432 - accepting connections
|
|
||||||
localhost:5432 - accepting connections
|
|
||||||
2023-01-05 15:09:46,139 INFO: establishing a new patroni connection to the postgres cluster
|
|
||||||
2023-01-05 15:09:46,208 INFO: running post_bootstrap
|
|
||||||
2023-01-05 15:09:47.209 UTC [55] LOG: starting maintenance daemon on database 16386 user 10
|
|
||||||
2023-01-05 15:09:47.209 UTC [55] CONTEXT: Citus maintenance daemon for database 16386 user 10
|
|
||||||
2023-01-05 15:09:47,215 WARNING: Could not activate Linux watchdog device: "Can't open watchdog device: [Errno 2] No such file or directory: '/dev/watchdog'"
|
|
||||||
2023-01-05 15:09:47.446 UTC [41] LOG: checkpoint starting: immediate force wait
|
|
||||||
2023-01-05 15:09:47,466 INFO: initialized a new cluster
|
|
||||||
2023-01-05 15:09:47,594 DEBUG: query(SELECT nodeid, groupid, nodename, nodeport, noderole FROM pg_catalog.pg_dist_node WHERE noderole = 'primary', ())
|
|
||||||
2023-01-05 15:09:47,594 INFO: establishing a new patroni connection to the postgres cluster
|
|
||||||
2023-01-05 15:09:47,467 INFO: Lock owner: coord1; I am coord1
|
|
||||||
2023-01-05 15:09:47,613 DEBUG: query(SELECT pg_catalog.citus_set_coordinator_host(%s, %s, 'primary', 'default'), ('172.27.0.6', 5432))
|
|
||||||
2023-01-05 15:09:47,924 INFO: no action. I am (coord1), the leader with the lock
|
|
||||||
2023-01-05 15:09:51.282 UTC [41] LOG: checkpoint complete: wrote 1086 buffers (53.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.029 s, sync=3.746 s, total=3.837 s; sync files=280, longest=0.028 s, average=0.014 s; distance=8965 kB, estimate=8965 kB
|
|
||||||
2023-01-05 15:09:51.283 UTC [41] LOG: checkpoint starting: immediate force wait
|
|
||||||
2023-01-05 15:09:51.495 UTC [41] LOG: checkpoint complete: wrote 18 buffers (0.9%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.044 s, sync=0.091 s, total=0.212 s; sync files=15, longest=0.015 s, average=0.007 s; distance=67 kB, estimate=8076 kB
|
|
||||||
2023-01-05 15:09:57,467 INFO: Lock owner: coord1; I am coord1
|
|
||||||
2023-01-05 15:09:57,569 INFO: Assigning synchronous standby status to ['coord3']
|
|
||||||
server signaled
|
|
||||||
2023-01-05 15:09:57.574 UTC [39] LOG: received SIGHUP, reloading configuration files
|
|
||||||
2023-01-05 15:09:57.580 UTC [39] LOG: parameter "synchronous_standby_names" changed to "coord3"
|
|
||||||
2023-01-05 15:09:59,637 INFO: Synchronous standby status assigned to ['coord3']
|
|
||||||
2023-01-05 15:09:59,638 DEBUG: query(SELECT pg_catalog.citus_add_node(%s, %s, %s, 'primary', 'default'), ('172.27.0.2', 5432, 1))
|
|
||||||
2023-01-05 15:09:59.690 UTC [67] LOG: standby "coord3" is now a synchronous standby with priority 1
|
|
||||||
2023-01-05 15:09:59.690 UTC [67] STATEMENT: START_REPLICATION SLOT "coord3" 0/3000000 TIMELINE 1
|
|
||||||
2023-01-05 15:09:59,694 INFO: no action. I am (coord1), the leader with the lock
|
|
||||||
2023-01-05 15:09:59,704 DEBUG: query(SELECT pg_catalog.citus_add_node(%s, %s, %s, 'primary', 'default'), ('172.27.0.8', 5432, 2))
|
|
||||||
2023-01-05 15:10:07,625 INFO: no action. I am (coord1), the leader with the lock
|
|
||||||
2023-01-05 15:10:17,579 INFO: no action. I am (coord1), the leader with the lock
|
|
||||||
|
|
||||||
$ docker exec -ti demo-haproxy bash
|
|
||||||
postgres@haproxy:~$ etcdctl member list
|
|
||||||
1bab629f01fa9065, started, etcd3, http://etcd3:2380, http://172.27.0.10:2379
|
|
||||||
8ecb6af518d241cc, started, etcd2, http://etcd2:2380, http://172.27.0.4:2379
|
|
||||||
b2e169fcb8a34028, started, etcd1, http://etcd1:2380, http://172.27.0.7:2379
|
|
||||||
|
|
||||||
postgres@haproxy:~$ etcdctl get --keys-only --prefix /service/demo
|
|
||||||
/service/demo/0/config
|
|
||||||
/service/demo/0/initialize
|
|
||||||
/service/demo/0/leader
|
|
||||||
/service/demo/0/members/coord1
|
|
||||||
/service/demo/0/members/coord2
|
|
||||||
/service/demo/0/members/coord3
|
|
||||||
/service/demo/0/status
|
|
||||||
/service/demo/0/sync
|
|
||||||
/service/demo/1/config
|
|
||||||
/service/demo/1/initialize
|
|
||||||
/service/demo/1/leader
|
|
||||||
/service/demo/1/members/work1-1
|
|
||||||
/service/demo/1/members/work1-2
|
|
||||||
/service/demo/1/status
|
|
||||||
/service/demo/1/sync
|
|
||||||
/service/demo/2/config
|
|
||||||
/service/demo/2/initialize
|
|
||||||
/service/demo/2/leader
|
|
||||||
/service/demo/2/members/work2-1
|
|
||||||
/service/demo/2/members/work2-2
|
|
||||||
/service/demo/2/status
|
|
||||||
/service/demo/2/sync
|
|
||||||
|
|
||||||
postgres@haproxy:~$ psql -h localhost -p 5000 -U postgres -d citus
|
|
||||||
Password for user postgres: postgres
|
|
||||||
psql (15.1 (Debian 15.1-1.pgdg110+1))
|
|
||||||
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
|
|
||||||
Type "help" for help.
|
|
||||||
|
|
||||||
citus=# select pg_is_in_recovery();
|
|
||||||
pg_is_in_recovery
|
|
||||||
-------------------
|
|
||||||
f
|
|
||||||
(1 row)
|
|
||||||
|
|
||||||
citus=# table pg_dist_node;
|
|
||||||
nodeid | groupid | nodename | nodeport | noderack | hasmetadata | isactive | noderole | nodecluster | metadatasynced | shouldhaveshards
|
|
||||||
--------+---------+------------+----------+----------+-------------+----------+----------+-------------+----------------+------------------
|
|
||||||
1 | 0 | 172.27.0.6 | 5432 | default | t | t | primary | default | t | f
|
|
||||||
2 | 1 | 172.27.0.2 | 5432 | default | t | t | primary | default | t | t
|
|
||||||
3 | 2 | 172.27.0.8 | 5432 | default | t | t | primary | default | t | t
|
|
||||||
(3 rows)
|
|
||||||
|
|
||||||
citus=# \q
|
|
||||||
|
|
||||||
postgres@haproxy:~$ patronictl list
|
|
||||||
+ Citus cluster: demo ----------+--------------+---------+----+-----------+
|
|
||||||
| Group | Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
| 0 | coord1 | 172.27.0.6 | Leader | running | 1 | |
|
|
||||||
| 0 | coord2 | 172.27.0.5 | Replica | running | 1 | 0 |
|
|
||||||
| 0 | coord3 | 172.27.0.9 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 1 | work1-1 | 172.27.0.2 | Leader | running | 1 | |
|
|
||||||
| 1 | work1-2 | 172.27.0.12 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | work2-1 | 172.27.0.11 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | work2-2 | 172.27.0.8 | Leader | running | 1 | |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
|
|
||||||
postgres@haproxy:~$ patronictl switchover --group 2 --force
|
|
||||||
Current cluster topology
|
|
||||||
+ Citus cluster: demo (group: 2, 7185185529556963355) +-----------+
|
|
||||||
| Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
| work2-1 | 172.27.0.11 | Sync Standby | running | 1 | 0 |
|
|
||||||
| work2-2 | 172.27.0.8 | Leader | running | 1 | |
|
|
||||||
+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
2023-01-05 15:29:29.54204 Successfully switched over to "work2-1"
|
|
||||||
+ Citus cluster: demo (group: 2, 7185185529556963355) -------+
|
|
||||||
| Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+---------+-------------+---------+---------+----+-----------+
|
|
||||||
| work2-1 | 172.27.0.11 | Leader | running | 1 | |
|
|
||||||
| work2-2 | 172.27.0.8 | Replica | stopped | | unknown |
|
|
||||||
+---------+-------------+---------+---------+----+-----------+
|
|
||||||
|
|
||||||
postgres@haproxy:~$ patronictl list
|
|
||||||
+ Citus cluster: demo ----------+--------------+---------+----+-----------+
|
|
||||||
| Group | Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
| 0 | coord1 | 172.27.0.6 | Leader | running | 1 | |
|
|
||||||
| 0 | coord2 | 172.27.0.5 | Replica | running | 1 | 0 |
|
|
||||||
| 0 | coord3 | 172.27.0.9 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 1 | work1-1 | 172.27.0.2 | Leader | running | 1 | |
|
|
||||||
| 1 | work1-2 | 172.27.0.12 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | work2-1 | 172.27.0.11 | Leader | running | 2 | |
|
|
||||||
| 2 | work2-2 | 172.27.0.8 | Sync Standby | running | 2 | 0 |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
|
|
||||||
postgres@haproxy:~$ psql -h localhost -p 5000 -U postgres -d citus
|
|
||||||
Password for user postgres: postgres
|
|
||||||
psql (15.1 (Debian 15.1-1.pgdg110+1))
|
|
||||||
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
|
|
||||||
Type "help" for help.
|
|
||||||
|
|
||||||
citus=# table pg_dist_node;
|
|
||||||
nodeid | groupid | nodename | nodeport | noderack | hasmetadata | isactive | noderole | nodecluster | metadatasynced | shouldhaveshards
|
|
||||||
--------+---------+-------------+----------+----------+-------------+----------+----------+-------------+----------------+------------------
|
|
||||||
1 | 0 | 172.27.0.6 | 5432 | default | t | t | primary | default | t | f
|
|
||||||
3 | 2 | 172.27.0.11 | 5432 | default | t | t | primary | default | t | t
|
|
||||||
2 | 1 | 172.27.0.2 | 5432 | default | t | t | primary | default | t | t
|
|
||||||
(3 rows)
|
|
||||||
|
|||||||
+12
-27
@@ -7,36 +7,29 @@ if [ -f /a.tar.xz ]; then
|
|||||||
sudo ln -snf dash /bin/sh
|
sudo ln -snf dash /bin/sh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
readonly PATRONI_SCOPE="${PATRONI_SCOPE:-batman}"
|
readonly PATRONI_SCOPE=${PATRONI_SCOPE:-batman}
|
||||||
PATRONI_NAMESPACE="${PATRONI_NAMESPACE:-/service}"
|
PATRONI_NAMESPACE=${PATRONI_NAMESPACE:-/service}
|
||||||
readonly PATRONI_NAMESPACE="${PATRONI_NAMESPACE%/}"
|
readonly PATRONI_NAMESPACE=${PATRONI_NAMESPACE%/}
|
||||||
DOCKER_IP=$(hostname --ip-address)
|
readonly DOCKER_IP=$(hostname --ip-address)
|
||||||
readonly DOCKER_IP
|
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
haproxy)
|
haproxy)
|
||||||
haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -D
|
haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -D
|
||||||
set -- confd "-prefix=$PATRONI_NAMESPACE/$PATRONI_SCOPE" -interval=10 -backend
|
CONFD="confd -prefix=$PATRONI_NAMESPACE/$PATRONI_SCOPE -interval=10 -backend"
|
||||||
if [ -n "$PATRONI_ZOOKEEPER_HOSTS" ]; then
|
if [ ! -z "$PATRONI_ZOOKEEPER_HOSTS" ]; then
|
||||||
while ! /usr/share/zookeeper/bin/zkCli.sh -server "$PATRONI_ZOOKEEPER_HOSTS" ls /; do
|
while ! /usr/share/zookeeper/bin/zkCli.sh -server $PATRONI_ZOOKEEPER_HOSTS ls /; do
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
set -- "$@" zookeeper -node "$PATRONI_ZOOKEEPER_HOSTS"
|
exec dumb-init $CONFD zookeeper -node $PATRONI_ZOOKEEPER_HOSTS
|
||||||
else
|
else
|
||||||
while ! etcdctl member list 2> /dev/null; do
|
while ! etcdctl cluster-health 2> /dev/null; do
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
set -- "$@" etcdv3
|
exec dumb-init $CONFD etcd -node $(echo $ETCDCTL_ENDPOINTS | sed 's/,/ -node /g')
|
||||||
while IFS='' read -r line; do
|
|
||||||
set -- "$@" -node "$line"
|
|
||||||
done <<-EOT
|
|
||||||
$(echo "$ETCDCTL_ENDPOINTS" | sed 's/,/\n/g')
|
|
||||||
EOT
|
|
||||||
fi
|
fi
|
||||||
exec dumb-init "$@"
|
|
||||||
;;
|
;;
|
||||||
etcd)
|
etcd)
|
||||||
exec "$@" -advertise-client-urls "http://$DOCKER_IP:2379"
|
exec "$@" -advertise-client-urls http://$DOCKER_IP:2379
|
||||||
;;
|
;;
|
||||||
zookeeper)
|
zookeeper)
|
||||||
exec /usr/share/zookeeper/bin/zkServer.sh start-foreground
|
exec /usr/share/zookeeper/bin/zkServer.sh start-foreground
|
||||||
@@ -44,7 +37,7 @@ EOT
|
|||||||
esac
|
esac
|
||||||
|
|
||||||
## We start an etcd
|
## We start an etcd
|
||||||
if [ -z "$PATRONI_ETCD3_HOSTS" ] && [ -z "$PATRONI_ZOOKEEPER_HOSTS" ]; then
|
if [ -z "$PATRONI_ETCD_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
|
||||||
@@ -63,13 +56,5 @@ export PATRONI_REPLICATION_USERNAME="${PATRONI_REPLICATION_USERNAME:-replicator}
|
|||||||
export PATRONI_REPLICATION_PASSWORD="${PATRONI_REPLICATION_PASSWORD:-replicate}"
|
export PATRONI_REPLICATION_PASSWORD="${PATRONI_REPLICATION_PASSWORD:-replicate}"
|
||||||
export PATRONI_SUPERUSER_USERNAME="${PATRONI_SUPERUSER_USERNAME:-postgres}"
|
export PATRONI_SUPERUSER_USERNAME="${PATRONI_SUPERUSER_USERNAME:-postgres}"
|
||||||
export PATRONI_SUPERUSER_PASSWORD="${PATRONI_SUPERUSER_PASSWORD:-postgres}"
|
export PATRONI_SUPERUSER_PASSWORD="${PATRONI_SUPERUSER_PASSWORD:-postgres}"
|
||||||
export PATRONI_REPLICATION_SSLMODE="${PATRONI_REPLICATION_SSLMODE:-$PGSSLMODE}"
|
|
||||||
export PATRONI_REPLICATION_SSLKEY="${PATRONI_REPLICATION_SSLKEY:-$PGSSLKEY}"
|
|
||||||
export PATRONI_REPLICATION_SSLCERT="${PATRONI_REPLICATION_SSLCERT:-$PGSSLCERT}"
|
|
||||||
export PATRONI_REPLICATION_SSLROOTCERT="${PATRONI_REPLICATION_SSLROOTCERT:-$PGSSLROOTCERT}"
|
|
||||||
export PATRONI_SUPERUSER_SSLMODE="${PATRONI_SUPERUSER_SSLMODE:-$PGSSLMODE}"
|
|
||||||
export PATRONI_SUPERUSER_SSLKEY="${PATRONI_SUPERUSER_SSLKEY:-$PGSSLKEY}"
|
|
||||||
export PATRONI_SUPERUSER_SSLCERT="${PATRONI_SUPERUSER_SSLCERT:-$PGSSLCERT}"
|
|
||||||
export PATRONI_SUPERUSER_SSLROOTCERT="${PATRONI_SUPERUSER_SSLROOTCERT:-$PGSSLROOTCERT}"
|
|
||||||
|
|
||||||
exec python3 /patroni.py postgres0.yml
|
exec python3 /patroni.py postgres0.yml
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
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,3 +1,6 @@
|
|||||||
|
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
|
||||||
@@ -6,3 +9,6 @@ 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
|
||||||
|
|||||||
+1
-34
@@ -8,40 +8,7 @@ Wanna contribute to Patroni? Yay - here is how!
|
|||||||
Chatting
|
Chatting
|
||||||
--------
|
--------
|
||||||
|
|
||||||
Just want to chat with other Patroni users? Looking for interactive troubleshooting help? Join us on channel `#patroni <https://postgresteam.slack.com/archives/C9XPYG92A>`__ in the `PostgreSQL Slack <https://join.slack.com/t/postgresteam/shared_invite/zt-1qj14i9sj-E9WqIFlvcOiHsEk2yFEMjA>`__.
|
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
|
||||||
----------------
|
----------------
|
||||||
|
|||||||
+12
-99
@@ -11,11 +11,7 @@ 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.
|
||||||
@@ -23,6 +19,8 @@ Log
|
|||||||
- **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,17 +31,10 @@ It is possible to create new database users right after the successful initializ
|
|||||||
|
|
||||||
Example: defining ``PATRONI_admin_PASSWORD=strongpasswd`` and ``PATRONI_admin_OPTIONS='createrole,createdb'`` will cause creation of the user **admin** with the password **strongpasswd** that is allowed to create other users and databases.
|
Example: defining ``PATRONI_admin_PASSWORD=strongpasswd`` and ``PATRONI_admin_OPTIONS='createrole,createdb'`` will cause creation of the user **admin** with the password **strongpasswd** that is allowed to create other users and databases.
|
||||||
|
|
||||||
Citus
|
|
||||||
-----
|
|
||||||
Enables integration Patroni with `Citus <https://docs.citusdata.com>`__. If configured, Patroni will take care of registering Citus worker nodes on the coordinator. You can find more information about Citus support :ref:`here <citus>`.
|
|
||||||
|
|
||||||
- **PATRONI\_CITUS\_GROUP**: the Citus group id, integer. Use ``0`` for coordinator and ``1``, ``2``, etc... for workers
|
|
||||||
- **PATRONI\_CITUS\_DATABASE**: the database where ``citus`` extension should be created. Must be the same on the coordinator and all workers. Currently only one database is supported.
|
|
||||||
|
|
||||||
Consul
|
Consul
|
||||||
------
|
------
|
||||||
- **PATRONI\_CONSUL\_HOST**: the host:port for the Consul local agent.
|
- **PATRONI\_CONSUL\_HOST**: the host:port for the Consul endpoint.
|
||||||
- **PATRONI\_CONSUL\_URL**: url for the Consul local agent, in format: http(s)://host:port
|
- **PATRONI\_CONSUL\_URL**: url for the Consul, 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
|
||||||
@@ -53,11 +44,9 @@ 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. By default an empty list is used.
|
- **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\_REGISTER\_SERVICE**: (optional) whether or not to register a service with the name defined by the scope parameter and the tag master, primary, 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\_TAGS**: (optional) additional static tags to add to the Consul service apart from the role (``master``/``primary``/``replica``/``standby-leader``). By default an empty list is used.
|
|
||||||
- **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
|
||||||
----
|
----
|
||||||
@@ -68,37 +57,13 @@ 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 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**: Domain to search the SRV record(s) for cluster autodiscovery.
|
||||||
- **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.
|
||||||
@@ -108,68 +73,28 @@ 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.
|
|
||||||
- **PATRONI\_RETRIABLE\_HTTP\_CODES**: (optional) list of HTTP status codes from K8s API to retry on. By default Patroni is retrying on ``500``, ``503``, and ``504``, or if K8s API response has ``retry-after`` HTTP header.
|
|
||||||
|
|
||||||
Raft (deprecated)
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
- **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 the replication source via streaming replication
|
- **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\_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
|
||||||
--------
|
--------
|
||||||
@@ -179,19 +104,7 @@ 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``.
|
|
||||||
|
|
||||||
CTL
|
ZooKeeper
|
||||||
---
|
---------
|
||||||
- **PATRONICTL\_CONFIG\_FILE**: location of the configuration file.
|
- **PATRONI\_ZOOKEEPER\_HOSTS**: comma separated list of ZooKeeper cluster members: "'host1:port1','host2:port2','etc...'". It is important to quote every single entity!
|
||||||
- **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.
|
|
||||||
|
|||||||
+4
-36
@@ -35,7 +35,7 @@ To install requirements on a Mac, run the following:
|
|||||||
|
|
||||||
.. _psycopg2_install_options:
|
.. _psycopg2_install_options:
|
||||||
|
|
||||||
**Psycopg**
|
**Psycopg2**
|
||||||
|
|
||||||
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,12 +62,6 @@ 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:
|
||||||
@@ -78,8 +72,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 or etcd3
|
etcd
|
||||||
`python-etcd` module in order to use Etcd as Distributed Configuration Store (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
|
||||||
zookeeper
|
zookeeper
|
||||||
@@ -88,8 +82,6 @@ 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
|
||||||
|
|
||||||
@@ -105,13 +97,6 @@ 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
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
@@ -122,7 +107,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 --enable-v2=true
|
> etcd --data-dir=data/etcd
|
||||||
> ./patroni.py postgres0.yml
|
> ./patroni.py postgres0.yml
|
||||||
> ./patroni.py postgres1.yml
|
> ./patroni.py postgres1.yml
|
||||||
|
|
||||||
@@ -169,20 +154,3 @@ 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.
|
|
||||||
|
|||||||
+112
-315
@@ -4,69 +4,6 @@
|
|||||||
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.
|
|
||||||
- **primary\_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 + primary\_start\_timeout + loop\_wait, unless primary\_start\_timeout is zero, in which case it's just loop\_wait. Set the value according to your durability/availability tradeoff.
|
|
||||||
- **primary\_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 primary\_stop\_timeout. Set the value according to your durability/availability tradeoff. If the parameter is not set or set <= 0, primary\_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.
|
|
||||||
- **failsafe\_mode**: Enables :ref:`DCS Failsafe Mode <dcs_failsafe_mode>`. Defaults to `false`.
|
|
||||||
- **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.
|
||||||
@@ -76,7 +13,6 @@ 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.
|
||||||
@@ -91,35 +27,50 @@ Log
|
|||||||
|
|
||||||
Bootstrap configuration
|
Bootstrap configuration
|
||||||
-----------------------
|
-----------------------
|
||||||
- **bootstrap**:
|
- **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.
|
||||||
- **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>`.
|
- **loop\_wait**: the number of seconds the loop will sleep. Default value: 10
|
||||||
- **method**: custom script to use for bootstrapping this cluster.
|
- **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
|
||||||
See :ref:`custom bootstrap methods documentation <custom_bootstrap>` for details.
|
- **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
|
||||||
When ``initdb`` is specified revert to the default ``initdb`` command. ``initdb`` is also triggered when no ``method``
|
- **maximum\_lag\_on\_failover**: the maximum bytes a follower may lag to be able to participate in leader election.
|
||||||
parameter is present in the configuration file.
|
- **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.
|
||||||
- **initdb**: List options to be passed on to initdb.
|
- **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.
|
||||||
- **- data-checksums**: Must be enabled when pg_rewind is needed on 9.3.
|
- **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.
|
||||||
- **- encoding: UTF8**: default encoding for new databases.
|
- **postgresql**:
|
||||||
- **- locale: UTF8**: default locale for new databases.
|
- **use\_pg\_rewind**: whether or not to use pg_rewind
|
||||||
- **pg\_hba**: list of lines that you should add to pg\_hba.conf.
|
- **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.
|
||||||
- **- host all all 0.0.0.0/0 md5**.
|
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
||||||
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for replication.
|
- **parameters**: list of configuration settings for Postgres. Many of these are required for replication to work.
|
||||||
- **users**: Some additional users which need to be created after initializing new cluster
|
- **standby\_cluster**: if this section is defined, we want to bootstrap a standby cluster.
|
||||||
- **admin**: the name of user
|
- **host**: an address of remote master
|
||||||
- **password: zalando**:
|
- **port**: a port of remote master
|
||||||
- **options**: list of options for CREATE USER statement
|
- **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`).
|
||||||
- **- createrole**
|
- **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`
|
||||||
- **- createdb**
|
- **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`
|
||||||
- **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.
|
- **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
|
||||||
.. _citus_settings:
|
- **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.
|
||||||
Citus
|
- **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.
|
||||||
Enables integration Patroni with `Citus <https://docs.citusdata.com>`__. If configured, Patroni will take care of registering Citus worker nodes on the coordinator. You can find more information about Citus support :ref:`here <citus>`.
|
- **plugin**: the plugin name for the logical slot.
|
||||||
|
- **method**: custom script to use for bootstrapping this cluster.
|
||||||
- **group**: the Citus group id, integer. Use ``0`` for coordinator and ``1``, ``2``, etc... for workers
|
See :ref:`custom bootstrap methods documentation <custom_bootstrap>` for details.
|
||||||
- **database**: the database where ``citus`` extension should be created. Must be the same on the coordinator and all workers. Currently only one database is supported.
|
When ``initdb`` is specified revert to the default ``initdb`` command. ``initdb`` is also triggered when no ``method``
|
||||||
|
parameter is present in the configuration file.
|
||||||
|
- **initdb**: List options to be passed on to initdb.
|
||||||
|
- **- data-checksums**: Must be enabled when pg_rewind is needed on 9.3.
|
||||||
|
- **- encoding: UTF8**: default encoding for new databases.
|
||||||
|
- **- locale: UTF8**: default locale for new databases.
|
||||||
|
- **pg\_hba**: list of lines that you should add to pg\_hba.conf.
|
||||||
|
- **- host all all 0.0.0.0/0 md5**.
|
||||||
|
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for replication.
|
||||||
|
- **users**: Some additional users which need to be created after initializing new cluster
|
||||||
|
- **admin**: the name of user
|
||||||
|
- **password: zalando**:
|
||||||
|
- **options**: list of options for CREATE USER statement
|
||||||
|
- **- createrole**
|
||||||
|
- **- createdb**
|
||||||
|
- **post\_bootstrap** or **post\_init**: An additional script that will be executed after initializing the cluster. The script receives a connection string URL (with the cluster superuser as a user name). The PGPASSFILE variable is set to the location of pgpass file.
|
||||||
|
|
||||||
.. _consul_settings:
|
.. _consul_settings:
|
||||||
|
|
||||||
@@ -127,36 +78,20 @@ 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 local agent.
|
- **host**: the host:port for the Consul endpoint, in format: http(s)://host:port
|
||||||
- **url**: url for the Consul local agent, in format: http(s)://host:port.
|
- **url**: url for the Consul endpoint
|
||||||
- **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. By default an empty list is used.
|
- **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
|
||||||
- **register\_service**: (optional) whether or not to register a service with the name defined by the scope parameter and the tag master, primary, 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\_tags**: (optional) additional static tags to add to the Consul service apart from the role (``master``/``primary``/``replica``/``standby-leader``). By default an empty list is used.
|
- **service\_check\_interval**: (optional) how often to perform health check against registered url
|
||||||
- **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
|
||||||
----
|
----
|
||||||
@@ -165,10 +100,9 @@ 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. 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**: Domain to search the SRV record(s) for cluster autodiscovery.
|
||||||
- **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.
|
||||||
@@ -176,235 +110,98 @@ 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.
|
|
||||||
- **retriable\_http\_codes**: (optional) list of HTTP status codes from K8s API to retry on. By default Patroni is retrying on ``500``, ``503``, and ``504``, or if K8s API response has ``retry-after`` HTTP header.
|
|
||||||
|
|
||||||
|
|
||||||
.. _raft_settings:
|
|
||||||
|
|
||||||
Raft (deprecated)
|
|
||||||
-----------------
|
|
||||||
- **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
|
||||||
----------
|
----------
|
||||||
- **postgresql**:
|
- **authentication**:
|
||||||
- **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).
|
- **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``.
|
- **username**: replication username; the user will be created during initialization. Replicas will use this user to access master via streaming 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.
|
- **password**: replication password; the user will be created during initialization.
|
||||||
- **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``.
|
- **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.
|
- **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.
|
||||||
- **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.
|
- **password**: password for the user for ``pg_rewind``; the user will be created during initialization.
|
||||||
- **sslcrl**: (optional) maps to the `sslcrl <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-SSLCRL>`__ connection parameter, which specifies the location of a file containing a certificate revocation list. A client will reject connecting to any server that has a certificate present in this list.
|
- **callbacks**: callback scripts to run on certain actions. Patroni will pass the action, role and cluster name. (See scripts/aws.py as an example of how to write them.)
|
||||||
- **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.
|
- **on\_reload**: run this script when configuration reload is triggered.
|
||||||
- **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
|
- **on\_restart**: run this script when the postgres restarts (without changing role).
|
||||||
- **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.
|
- **on\_role\_change**: run this script when the postgres is being promoted or demoted.
|
||||||
- **replication**:
|
- **on\_start**: run this script when the postgres starts.
|
||||||
- **username**: replication username; the user will be created during initialization. Replicas will use this user to access the replication source via streaming replication
|
- **on\_stop**: run this script when the postgres stops.
|
||||||
- **password**: replication password; the user will be created during initialization.
|
- **connect\_address**: IP address + port through which Postgres is accessible from other nodes and applications.
|
||||||
- **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``.
|
- **create\_replica\_methods**: an ordered list of the create methods for turning a Patroni node into a new replica.
|
||||||
- **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.
|
"basebackup" is the default method; other methods are assumed to refer to scripts, each of which is configured as its
|
||||||
- **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``.
|
own config item. See :ref:`custom replica creation methods documentation <custom_replica_creation>` for further explanation.
|
||||||
- **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.
|
- **data\_dir**: The location of the Postgres data directory, either :ref:`existing <existing_data>` or to be initialized by Patroni.
|
||||||
- **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.
|
- **config\_dir**: The location of the Postgres configuration directory, defaults to the data directory. Must be writable by Patroni.
|
||||||
- **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.
|
- **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.
|
||||||
- **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.
|
- **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.
|
||||||
- **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
|
- **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.
|
||||||
- **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.
|
- **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.
|
||||||
- **rewind**:
|
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
||||||
- **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.
|
- **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.
|
||||||
- **password**: password for the user for ``pg_rewind``; the user will be created during initialization.
|
- **parameters**: list of configuration settings for Postgres. Many of these are required for replication to work.
|
||||||
- **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``.
|
- **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``.
|
||||||
- **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.
|
- **- host all all 0.0.0.0/0 md5**.
|
||||||
- **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``.
|
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for 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.
|
- **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``.
|
||||||
- **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.
|
- **- mapname1 systemname1 pguser1**.
|
||||||
- **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.
|
- **- mapname1 systemname2 pguser2**.
|
||||||
- **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.
|
- **pg\_ctl\_timeout**: How long should pg_ctl wait when doing ``start``, ``stop`` or ``restart``. Default value is 60 seconds.
|
||||||
- **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
|
- **use\_pg\_rewind**: try to use pg\_rewind on the former leader when it joins cluster as a replica.
|
||||||
- **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.
|
- **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**.
|
||||||
- **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.)
|
- **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**.
|
||||||
- **on\_reload**: run this script when configuration reload is triggered.
|
- **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".
|
||||||
- **on\_restart**: run this script when the postgres restarts (without changing role).
|
|
||||||
- **on\_role\_change**: run this script when the postgres is being promoted or demoted.
|
|
||||||
- **on\_start**: run this script when the postgres starts.
|
|
||||||
- **on\_stop**: run this script when the postgres stops.
|
|
||||||
- **connect\_address**: IP address + port through which Postgres is accessible from other nodes and applications.
|
|
||||||
- **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
|
|
||||||
own config item. See :ref:`custom replica creation methods documentation <custom_replica_creation>` for further explanation.
|
|
||||||
- **data\_dir**: The location of the Postgres data directory, either :ref:`existing <existing_data>` or to be initialized by Patroni.
|
|
||||||
- **config\_dir**: The location of the Postgres configuration directory, defaults to the data directory. Must be writable by Patroni.
|
|
||||||
- **bin\_dir**: Path to PostgreSQL binaries (pg_ctl, pg_rewind, pg_basebackup, postgres). The default value is an empty string meaning that PATH environment variable will be used to find the executables.
|
|
||||||
- **listen**: IP address + port that Postgres listens to; must be accessible from other nodes in the cluster, if you're using streaming replication. Multiple comma-separated addresses are permitted, as long as the port component is appended after to the last one with a colon, i.e. ``listen: 127.0.0.1,127.0.0.2:5432``. Patroni will use the first address from this list to establish local connections to the PostgreSQL node.
|
|
||||||
- **use\_unix\_socket**: specifies that Patroni should prefer to use unix sockets to connect to the cluster. Default value is ``false``. If ``unix_socket_directories`` is defined, Patroni will use the first suitable value from it to connect to the cluster and fallback to tcp if nothing is suitable. If ``unix_socket_directories`` is not specified in ``postgresql.parameters``, Patroni will assume that the default value should be used and omit ``host`` from the connection parameters.
|
|
||||||
- **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.
|
|
||||||
- **pgpass**: path to the `.pgpass <https://www.postgresql.org/docs/current/static/libpq-pgpass.html>`__ password file. Patroni creates this file before executing pg\_basebackup, the post_init script and under some other circumstances. The location must be writable by Patroni.
|
|
||||||
- **recovery\_conf**: additional configuration settings written to recovery.conf when configuring follower.
|
|
||||||
- **custom\_conf** : path to an optional custom ``postgresql.conf`` file, that will be used in place of ``postgresql.base.conf``. The file must exist on all cluster nodes, be readable by PostgreSQL and will be included from its location on the real ``postgresql.conf``. Note that Patroni will not monitor this file for changes, nor backup it. However, its settings can still be overridden by Patroni's own configuration facilities - see :ref:`dynamic configuration <dynamic_configuration>` for details.
|
|
||||||
- **parameters**: list of configuration settings for Postgres. Many of these are required for replication to work.
|
|
||||||
- **pg\_hba**: list of lines that Patroni will use to generate ``pg_hba.conf``. This parameter has higher priority than ``bootstrap.pg_hba``. Together with :ref:`dynamic configuration <dynamic_configuration>` it simplifies management of ``pg_hba.conf``.
|
|
||||||
- **- host all all 0.0.0.0/0 md5**.
|
|
||||||
- **- host replication replicator 127.0.0.1/32 md5**: A line like this is required for replication.
|
|
||||||
- **pg\_ident**: list of lines that Patroni will use to generate ``pg_ident.conf``. Together with :ref:`dynamic configuration <dynamic_configuration>` it simplifies management of ``pg_ident.conf``.
|
|
||||||
- **- mapname1 systemname1 pguser1**.
|
|
||||||
- **- mapname1 systemname2 pguser2**.
|
|
||||||
- **pg\_ctl\_timeout**: How long should pg_ctl wait when doing ``start``, ``stop`` or ``restart``. Default value is 60 seconds.
|
|
||||||
- **use\_pg\_rewind**: try to use pg\_rewind on the former leader when it joins cluster as a replica.
|
|
||||||
- **remove\_data\_directory\_on\_rewind\_failure**: If this option is enabled, Patroni will remove the PostgreSQL data directory and recreate the replica. Otherwise it will try to follow the new leader. Default value is **false**.
|
|
||||||
- **remove\_data\_directory\_on\_diverged\_timelines**: Patroni will remove the PostgreSQL data directory and recreate the replica if it notices that timelines are diverging and the former 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".
|
|
||||||
- **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.
|
|
||||||
|
|
||||||
.. _restapi_settings:
|
|
||||||
|
|
||||||
REST API
|
REST API
|
||||||
--------
|
--------
|
||||||
- **restapi**:
|
- **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.
|
||||||
- **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).
|
||||||
|
|
||||||
- **authentication**: (optional)
|
- **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``.
|
|
||||||
|
|
||||||
Here is an example of both **http_extra_headers** and **https_extra_headers**:
|
- **certfile**: Specifies the file with the certificate in the PEM format. If the certfile is not specified or is left empty, the API server will work without SSL.
|
||||||
|
- **keyfile**: Specifies the file with the secret key in the PEM format.
|
||||||
.. 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
|
||||||
---
|
---
|
||||||
- **ctl**: (optional)
|
- **Optional**:
|
||||||
- **insecure**: Allow connections to REST API without verifying SSL certs.
|
- **insecure**: Allow connections to REST API without verifying SSL certs.
|
||||||
- **cacert**: Specifies the file with the CA_BUNDLE file or directory with certificates of trusted CAs to use while verifying REST API SSL certs. If not provided patronictl will use the value provided for REST API "cafile" parameter.
|
- **cacert**: Specifices the file with the CA_BUNDLE file or directory with certificates of trusted CAs to use while verifying REST API SSL certs.
|
||||||
- **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.
|
- **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.
|
||||||
- **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.
|
|
||||||
- **keyfile\_password**: Specifies a password for decrypting the keyfile. If not provided patronictl will use the value provided for REST API "keyfile\_password" parameter.
|
ZooKeeper
|
||||||
|
----------
|
||||||
|
- **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.
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
<mxfile host="app.diagrams.net" modified="2023-03-13T14:29:21.924Z" agent="5.0 (X11; Ubuntu)" etag="sukwsRuBYbiX8e-LLYnw" version="21.0.6" type="device"><diagram name="Page-1" id="Xu3tU9JEMeQEUPilRV_D">7Vxtb9s2EP41BrYPNfTmt4+Jk2bFOixrihXYF4O2aEkNLaoUZTv99SMlUhZF+i2RE8dVEsDiiTxKd88deXeMO+54sb4jIAn/wj5EHcfy1x33puM4tud67INTngrKkLc4ISCRLzptCA/RTyiIlqBmkQ9TpSPFGNEoUYkzHMdwRhUaIASv1G5zjNRZExBAjfAwA0infot8Ggqq3R9tbvwBoyCk8v0GxY0FkJ3Fm6Qh8PGqQnJvO+6YYEyLq8V6DBEXnpRLMe7jlrvlgxEY00MG/L3474v99d/Hb+jnn596d99vsjj7ILgsAcrEC9+MhYJS+iSFkOAoprkge9fsj80ztjo9dmfMW12nVyPU2wOVYOstzkMl1NsDlWDX2du1+e36A1YIWkthb9XmtyoPyP7ca5xRFMVwXELOYsSAAD9iqhhjhAmjxThm0rsO6QKxls0uV2FE4UMCZlyqK2YujDbHMRWgtx3ZFoLnXBmsKWBzEcEj1wQkt0tYKKTogxBI0mhajiJwlpE0WsIvMC2YcyoDYMKvF+uA22oXrFKvGxCcJfnjf2JzGe9O2OVkhnDmcyaU4EcoX7LjuOz3Iwfc9TxCqPbyS0hoxGzpCkUB500xnwqIFoJzyjkyiURx8Dlv3biWkIJpCh+kIfTF6+j4l2Bms8J1hSTs4Q7iBaTkiXURd3vSNoVz8kRztbF0T9LCipF7chwQ3iUoWW8MkF0IGzzCHh3NHu8Bk3gcaTZpELemm95VfzzsVwVnb9VKHXk1HZSsTCiugFzXyk6/c7CqbHvAzXq3shyrpyur7Ni4slxdWXd8TJxSEDP5OH3EAT4l7CrIoc7o/vRJ02X6COksFII3epdtFrHF6xxmpYw+z3/qpiUR8hlMIbrHaUSj3DdMMaV4sdewZ5D7KBUX+xwdSJPibefRGvrbvBWBKc7IDBa+ivm51OS1/OlE6mAiRX5CZI5UJzLQcdk3+JD+qVDpHYtKAoHPOhCYIKbTFpyvB04u+YmU+wkR2lMRar81RHstRFuIKhB13DODaF+D6O3X8Q0PNFGWcu04lh4nKTisKE8BR76BSmthgIoqK/8x4bDEWz0k61pWHmR1+24t+BLxVY06MlKLOK3Wc7SF8SAfze4bmNg1mjOs9c0Dqb12olmE2XDqWH/MppDEkIm5GxVIT2R4wxTkn8zPDlUQu44O4qEpnBieCMQDDcQtZFvIViHbPzPEDjWAQj+AcqHDhIY4wDFAtxvqNcFZ7JdY3fT5jLmkczR/h5Q+ieUTZBS/JGYtVtAd/URmkAISwF38xBLDX3CnqghEgEZLNSFpkrwYes/trLK01r2S56ksigcVo2r6Kx/j+SodtU6odUI7nRDT23l5IVl8eNduaPBWbuhlorcvQPT9A0U/Oi/R6475ckU/OC/R66nkG74WFHkQtujFil76PzJeNSyWxA9iTbziYiQwF6nsIPMn7JMtgqPiChWUjwVb2aEt+bUlv03Jb4ZJggmgcOIDCiblPmJ7iel05T+9itVQ+c9Ttx1vX/2zDbn7yyz/ucfqyrbPrfpnt1nsS89iH49Sr16lfvNEtq0nAVuY/uIwdZzzg+lQg6lWcNFDwzZxdFGJo+P97blVXOw229mC9p3VXJyzznYK8e5N/JSnw/dlfuRKc+l1lzIebl1R64reS+XFgNF36IvkLuANfNHLpO9ehPSHB0pfyvFcpO/9UtKXRvL60n+kftr/Z/jtGi7DW39l/0juoeEwv8yM+NGyUkepJUsq9RRDv5xkKNzwhMKHIk/Pyzb9ZN3ZUrY5fjqVxGc65BFsd88zHMzIa4pRrylG+8R7MKNBU4yGTTEaNcSIeYCGGNlNMXKaYtQUsp1tyL6vWGXBTDPWX5qsuKV6BJIHDPJfax11bapvk+cIr2YhILTLq5JTkPIV0FSROtmG2altmAc9bb/slrnV6o6510Di1Lhu6QfVj1x87KYMbSsjY73hQLic2as0xigh0QJw9e+UhnHhf4aVMXRT1bT2BqqLyPeLHSY/UAA2Jw3UQJ5HxXxTKQ4d5Far5ABEkcdQr65UWdjW95RVuUGt2OHqUe7IFOWeymbPZKNvOFO1a2u8d0fvntWGXi/PS2MJ3WearjVDIE2VQTSiCL6Cw9l6BixntBKo5axiTBYAGZk9UALBIooD1u1LUWbME1iO9RtIn+JZSHCMs/T3ildRD4nt80FcsltcEEdFnjY7nR/SEb7L+jT/UX6JiJikU/2eDpNfsbqW5wwV1yJt4Lm5Y9kFz+cpPDItzJqbbxMpum++k8W9/R8=</diagram></mxfile>
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 40 KiB |
@@ -1 +0,0 @@
|
|||||||
<mxfile host="app.diagrams.net" modified="2023-03-13T14:25:32.295Z" agent="5.0 (X11; Ubuntu)" etag="EcVEbU6F-AyuIGXoP3hl" version="21.0.6" type="device"><diagram id="SVgELWPNXIlR7V7eDs_m" name="Page-1">7Vtbc5s4FP41frQHgY3tx9hO2u5kZ9Km7c70xSODDGxlxAoRO/31K4FkgwS2k+DGTZxmpuggjsS5fOci0nGmq80HCpPwb+Ij3LEtf9NxZh3bBn2nz/8TlMeCMhyOCkJAI19O2hHuo19IEi1JzSIfpZWJjBDMoqRK9EgcI49VaJBSsq5OWxJcXTWBATII9x7EJvWfyGehpAJ3vLvxEUVBKJce2cPixgqqyfJN0hD6ZF0iOdcdZ0oJYcXVajNFWAhPyaV47qbh7nZjFMXsmAf8b7fdj9cAuzc/wOTz92/W3Y+vXafg8gBxJl94NrXlftmjEkJCopjlghxM+C9fZ2p1BvzOVIx69kAj6ONhlQDMkeBRJejjYZUAdPZAWx/oGywRjFGFvaWtb5U2yH+dCckYjmI03ZqcxYkBhX7EVTElmFBOi0nMpTcJ2QrzEeCX6zBi6D6BnpDqmrsLpy1JzKTRA1uNpeAFV27WDPK1qOSRawLR6wdUKKSYgzFM0mixfYoiL6Np9IC+oLRgLqjcABNxvdoEwld7cJ32ewElWZJv/xNfq/bunF/OPUwyXzBhlPxE6iU7tsP/3QiDmywjjLWXf0CURdyXrnAUCN6MiKWgHGG0ZIIjl0gUB7f5aOZYUgp1S/gwDZEvX8e0f+kSYlW0KZGkP3xAZIUYfeRT5N2+JX3zUY2L4Xrn6Y5y37Dk5I56Dkp0Cbasdw7IL6QPPsEf3b7hkHeQizyODKeskbehnMGVOx25ZcmBRrXopqcpYcuqzoxLVm6qZS/wHK0roOvKNnQF6nQFHPtEunKAoavrr9OZCEY4S7mX8itgqC39iZgXShmX5Fax7VzGqQYVJX13hAmKnzqlL/MfBRYl2O5ZVg7EPdfRAFpisEYd11ILLNdmjhsYD/On+f0aJkCj2SNtbg62ylhv4QLhO5JGLMpxakEYI6sSnHhIIGOjeevo9zNbIBojLuZelCPfJFEQyBXkn8yM7aoZ25aJOaMaM+6PXm7F6ch2HA96nxfjq1/hp+zT9C+vaxpxx3axAGY/euCXAcuduiAtqE7ha9bMy0klq3f/y0Sak2NKtwhJV3yCm2yKh+TtFy1XJYmVjtkCcA7s4WhG/bYYDdpidEi8RzMatsVo1BajcUuMuIu3xAi0xchui1Fblm03WfZdySsLZoazvmtyBZb0NCCP2qqktKu5gB6rlpisvRBS1vMhgwuYooY8rCEaHRvImqNWvxq1RMWlJ8rD3sAMW4MWEuXasGXvD1tHQEhbftbI6O6DeIk4ZTDmOnqatZzZq7TGKKHRCgr175VGbdx/hpNxL2BVzzqYLK4i3xeP8xqavw7c1dTVZFpkpjBjRJbXudNW8nBZkdUUaaWS3+6f0mfBoOKzwDEzzXFdpnmq2tYsbWeiBig0yYuduNaycpjrylpI2FZCUZON8uJnXLWOgm2DeVzaWu+6reURmhAKGZqLqDrfQkJzF+V0LS6zUdNai+vMOlxgbHa46gLlLgZQBHk5blGUYC7p/Q2VWhtuaic22PZxLc5yo6WitIMNi/0mszXDkosdcieYJsXbLqMN8pt8gkc0klEPFR4hAlydb/iLuVLFXEh+ruR+dGuv/1Qb3UYmaaSu2dpza2zUPZWJqu1cGnvvt7H3dCN+xcZe/VGCWSK9zaOEp6vq8LHPbz5KAGYXdjZ1LgnrJWF9F+ewQ/fsstThJUt921kqeLKVnl2a6hpWhvwAKZETykISkBji6x11woEk9rdWsJtzS4R+cz3+ixh7lIoUPa5nBWUl3kKZ+95CSlFsfa8WKMKQcSytYEydUOWjdwKid9rrAg1kOMxUeTBIA8TkY5putvt4gbrMfOxSVbyzquIZoHN2ZYX7TsqKZ+jq7D5RskfnESLUl7s5wu6T+fj3BIOBFsoNDRRh63SxwEwvVUYZOnlEgGmxE3XKwSKGUQsHc219pnDwqLDxQCdntJbmL1jFhK4grmV2/xh7IccWkqV84pcix8sRvemI55lH9ULqDadEwhbzaNI52UnikYikvLnmrFB+/i5X6ZS/MK9Dqi4P7mPHqfiA+hLsua6lppDlMkUn8Rp7/Ieh2fA3odn4ldFM2WUDmh0GE/MrrBNCYFuffLXH6ALKLZD/DAhXp58vhnCO4CN3WEVw59wR3DF72q+J4IebE9aRUK/+FqA9qH9Za6j/h8n52JCqFHIuch68VTm33pVrkDMf7v4EsoCZ3R+SOtf/Aw==</diagram></mxfile>
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 33 KiB |
-355
@@ -1,355 +0,0 @@
|
|||||||
.. _citus:
|
|
||||||
|
|
||||||
Citus support
|
|
||||||
=============
|
|
||||||
|
|
||||||
Patroni makes it extremely simple to deploy `Multi-Node Citus`__ clusters.
|
|
||||||
|
|
||||||
__ https://docs.citusdata.com/en/stable/installation/multi_node.html
|
|
||||||
|
|
||||||
TL;DR
|
|
||||||
-----
|
|
||||||
|
|
||||||
There are only a few simple rules you need to follow:
|
|
||||||
|
|
||||||
1. `Citus <https://github.com/citusdata/citus>`__ database extension to
|
|
||||||
PostgreSQL must be available on all nodes. Absolute minimum supported Citus
|
|
||||||
version is 10.0, but, to take all benefits from transparent switchovers and
|
|
||||||
restarts of workers we recommend using at least Citus 11.2.
|
|
||||||
2. Cluster name (``scope``) must be the same for all Citus nodes!
|
|
||||||
3. Superuser credentials must be the same on coordinator and all worker
|
|
||||||
nodes, and ``pg_hba.conf`` should allow superuser access between all nodes.
|
|
||||||
4. :ref:`REST API <restapi_settings>` access should be allowed from worker
|
|
||||||
nodes to the coordinator. E.g., credentials should be the same and if
|
|
||||||
configured, client certificates from worker nodes must be accepted by the
|
|
||||||
coordinator.
|
|
||||||
5. Add the following section to the ``patroni.yaml``:
|
|
||||||
|
|
||||||
.. code:: YAML
|
|
||||||
|
|
||||||
citus:
|
|
||||||
group: X # 0 for coordinator and 1, 2, 3, etc for workers
|
|
||||||
database: citus # must be the same on all nodes
|
|
||||||
|
|
||||||
|
|
||||||
After that you just need to start Patroni and it will handle the rest:
|
|
||||||
|
|
||||||
1. ``citus`` extension will be automatically added to ``shared_preload_libraries``.
|
|
||||||
2. If ``max_prepared_transactions`` isn't explicitly set in the global
|
|
||||||
:ref:`dynamic configuration <dynamic_configuration>` Patroni will
|
|
||||||
automatically set it to ``2*max_connections``.
|
|
||||||
3. The ``citus.database`` will be automatically created followed by ``CREATE EXTENSION citus``.
|
|
||||||
4. Current superuser :ref:`credentials <postgresql_settings>` will be added to the ``pg_dist_authinfo``
|
|
||||||
table to allow cross-node communication. Don't forget to update them if
|
|
||||||
later you decide to change superuser username/password/sslcert/sslkey!
|
|
||||||
5. The coordinator primary node will automatically discover worker primary
|
|
||||||
nodes and add them to the ``pg_dist_node`` table using the
|
|
||||||
``citus_add_node()`` function.
|
|
||||||
6. Patroni will also maintain ``pg_dist_node`` in case failover/switchover
|
|
||||||
on the coordinator or worker clusters occurs.
|
|
||||||
|
|
||||||
patronictl
|
|
||||||
----------
|
|
||||||
|
|
||||||
Coordinator and worker clusters are physically different PostgreSQL/Patroni
|
|
||||||
clusters that are just logically groupped together using the
|
|
||||||
`Citus <https://github.com/citusdata/citus>`__ database extension to
|
|
||||||
PostgreSQL. Therefore in most cases it is not possible to manage them as a
|
|
||||||
single entity.
|
|
||||||
|
|
||||||
It results in two major differences in ``patronictl`` behaviour when
|
|
||||||
``patroni.yaml`` has the ``citus`` section comparing with the usual:
|
|
||||||
|
|
||||||
1. The ``list`` and the ``topology`` by default output all members of the Citus
|
|
||||||
formation (coordinators and workers). The new column ``Group`` indicates
|
|
||||||
which Citus group they belong to.
|
|
||||||
2. For all ``patronictl`` commands the new option is introduced, named
|
|
||||||
``--group``. For some commands the default value for the group might be
|
|
||||||
taken from the ``patroni.yaml``. For example, ``patronictl pause`` will
|
|
||||||
enable the maintenance mode by default for the ``group`` that is set in the
|
|
||||||
``citus`` section, but for example for ``patronictl switchover`` or
|
|
||||||
``patronictl remove`` the group must be explicitly specified.
|
|
||||||
|
|
||||||
An example of ``patronictl list`` output for the Citus cluster::
|
|
||||||
|
|
||||||
postgres@coord1:~$ patronictl list demo
|
|
||||||
+ Citus cluster: demo ----------+--------------+---------+----+-----------+
|
|
||||||
| Group | Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
| 0 | coord1 | 172.27.0.10 | Replica | running | 1 | 0 |
|
|
||||||
| 0 | coord2 | 172.27.0.6 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 0 | coord3 | 172.27.0.4 | Leader | running | 1 | |
|
|
||||||
| 1 | work1-1 | 172.27.0.8 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 1 | work1-2 | 172.27.0.2 | Leader | running | 1 | |
|
|
||||||
| 2 | work2-1 | 172.27.0.5 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | work2-2 | 172.27.0.7 | Leader | running | 1 | |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
|
|
||||||
If we add the ``--group`` option, the output will change to::
|
|
||||||
|
|
||||||
postgres@coord1:~$ patronictl list demo --group 0
|
|
||||||
+ Citus cluster: demo (group: 0, 7179854923829112860) -----------+
|
|
||||||
| Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+--------+-------------+--------------+---------+----+-----------+
|
|
||||||
| coord1 | 172.27.0.10 | Replica | running | 1 | 0 |
|
|
||||||
| coord2 | 172.27.0.6 | Sync Standby | running | 1 | 0 |
|
|
||||||
| coord3 | 172.27.0.4 | Leader | running | 1 | |
|
|
||||||
+--------+-------------+--------------+---------+----+-----------+
|
|
||||||
|
|
||||||
postgres@coord1:~$ patronictl list demo --group 1
|
|
||||||
+ Citus cluster: demo (group: 1, 7179854923881963547) -----------+
|
|
||||||
| Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+---------+------------+--------------+---------+----+-----------+
|
|
||||||
| work1-1 | 172.27.0.8 | Sync Standby | running | 1 | 0 |
|
|
||||||
| work1-2 | 172.27.0.2 | Leader | running | 1 | |
|
|
||||||
+---------+------------+--------------+---------+----+-----------+
|
|
||||||
|
|
||||||
Citus worker switchover
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
When a switchover is orchestrated for a Citus worker node, Citus offers the
|
|
||||||
opportunity to make the switchover close to transparent for an application.
|
|
||||||
Because the application connects to the coordinator, which in turn connects to
|
|
||||||
the worker nodes, then it is possible with Citus to `pause` the SQL traffic on
|
|
||||||
the coordinator for the shards hosted on a worker node. The switchover then
|
|
||||||
happens while the traffic is kept on the coordinator, and resumes as soon as a
|
|
||||||
new primary worker node is ready to accept read-write queries.
|
|
||||||
|
|
||||||
An example of ``patronictl switchover`` on the worker cluster::
|
|
||||||
|
|
||||||
postgres@coord1:~$ patronictl switchover demo
|
|
||||||
+ Citus cluster: demo ----------+--------------+---------+----+-----------+
|
|
||||||
| Group | Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
| 0 | coord1 | 172.27.0.10 | Replica | running | 1 | 0 |
|
|
||||||
| 0 | coord2 | 172.27.0.6 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 0 | coord3 | 172.27.0.4 | Leader | running | 1 | |
|
|
||||||
| 1 | work1-1 | 172.27.0.8 | Leader | running | 1 | |
|
|
||||||
| 1 | work1-2 | 172.27.0.2 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | work2-1 | 172.27.0.5 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | work2-2 | 172.27.0.7 | Leader | running | 1 | |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
Citus group: 2
|
|
||||||
Primary [work2-2]:
|
|
||||||
Candidate ['work2-1'] []:
|
|
||||||
When should the switchover take place (e.g. 2022-12-22T08:02 ) [now]:
|
|
||||||
Current cluster topology
|
|
||||||
+ Citus cluster: demo (group: 2, 7179854924063375386) -----------+
|
|
||||||
| Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+---------+------------+--------------+---------+----+-----------+
|
|
||||||
| work2-1 | 172.27.0.5 | Sync Standby | running | 1 | 0 |
|
|
||||||
| work2-2 | 172.27.0.7 | Leader | running | 1 | |
|
|
||||||
+---------+------------+--------------+---------+----+-----------+
|
|
||||||
Are you sure you want to switchover cluster demo, demoting current primary work2-2? [y/N]: y
|
|
||||||
2022-12-22 07:02:40.33003 Successfully switched over to "work2-1"
|
|
||||||
+ Citus cluster: demo (group: 2, 7179854924063375386) ------+
|
|
||||||
| Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+---------+------------+---------+---------+----+-----------+
|
|
||||||
| work2-1 | 172.27.0.5 | Leader | running | 1 | |
|
|
||||||
| work2-2 | 172.27.0.7 | Replica | stopped | | unknown |
|
|
||||||
+---------+------------+---------+---------+----+-----------+
|
|
||||||
|
|
||||||
postgres@coord1:~$ patronictl list demo
|
|
||||||
+ Citus cluster: demo ----------+--------------+---------+----+-----------+
|
|
||||||
| Group | Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
| 0 | coord1 | 172.27.0.10 | Replica | running | 1 | 0 |
|
|
||||||
| 0 | coord2 | 172.27.0.6 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 0 | coord3 | 172.27.0.4 | Leader | running | 1 | |
|
|
||||||
| 1 | work1-1 | 172.27.0.8 | Leader | running | 1 | |
|
|
||||||
| 1 | work1-2 | 172.27.0.2 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | work2-1 | 172.27.0.5 | Leader | running | 2 | |
|
|
||||||
| 2 | work2-2 | 172.27.0.7 | Sync Standby | running | 2 | 0 |
|
|
||||||
+-------+---------+-------------+--------------+---------+----+-----------+
|
|
||||||
|
|
||||||
And this is how it looks on the coordinator side::
|
|
||||||
|
|
||||||
# The worker primary notifies the coordinator that it is going to execute "pg_ctl stop".
|
|
||||||
2022-12-22 07:02:38,636 DEBUG: query("BEGIN")
|
|
||||||
2022-12-22 07:02:38,636 DEBUG: query("SELECT pg_catalog.citus_update_node(3, '172.27.0.7-demoted', 5432, true, 10000)")
|
|
||||||
# From this moment all application traffic on the coordinator to the worker group 2 is paused.
|
|
||||||
|
|
||||||
# The future worker primary notifies the coordinator that it acquired the leader lock in DCS and about to run "pg_ctl promote".
|
|
||||||
2022-12-22 07:02:40,085 DEBUG: query("SELECT pg_catalog.citus_update_node(3, '172.27.0.5', 5432)")
|
|
||||||
|
|
||||||
# The new worker primary just finished promote and notifies coordinator that it is ready to accept read-write traffic.
|
|
||||||
2022-12-22 07:02:41,485 DEBUG: query("COMMIT")
|
|
||||||
# From this moment the application traffic on the coordinator to the worker group 2 is unblocked.
|
|
||||||
|
|
||||||
Peek into DCS
|
|
||||||
-------------
|
|
||||||
|
|
||||||
The Citus cluster (coordinator and workers) are stored in DCS as a fleet of
|
|
||||||
Patroni clusters logically grouped together::
|
|
||||||
|
|
||||||
/service/batman/ # scope=batman
|
|
||||||
/service/batman/0/ # citus.group=0, coordinator
|
|
||||||
/service/batman/0/initialize
|
|
||||||
/service/batman/0/leader
|
|
||||||
/service/batman/0/members/
|
|
||||||
/service/batman/0/members/m1
|
|
||||||
/service/batman/0/members/m2
|
|
||||||
/service/batman/1/ # citus.group=1, worker
|
|
||||||
/service/batman/1/initialize
|
|
||||||
/service/batman/1/leader
|
|
||||||
/service/batman/1/members/
|
|
||||||
/service/batman/1/members/m3
|
|
||||||
/service/batman/1/members/m4
|
|
||||||
...
|
|
||||||
|
|
||||||
Such an approach was chosen because for most DCS it becomes possible to fetch
|
|
||||||
the entire Citus cluster with a single recursive read request. Only Citus
|
|
||||||
coordinator nodes are reading the whole tree, because they have to discover
|
|
||||||
worker nodes. Worker nodes are reading only the subtree for their own group and
|
|
||||||
in some cases they could read the subtree of the coordinator group.
|
|
||||||
|
|
||||||
Citus on Kubernetes
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
Since Kubernetes doesn't support hierarchical structures we had to include the
|
|
||||||
citus group to all K8s objects Patroni creates::
|
|
||||||
|
|
||||||
batman-0-leader # the leader config map for the coordinator
|
|
||||||
batman-0-config # the config map holding initialize, config, and history "keys"
|
|
||||||
...
|
|
||||||
batman-1-leader # the leader config map for worker group 1
|
|
||||||
batman-1-config
|
|
||||||
...
|
|
||||||
|
|
||||||
I.e., the naming pattern is: ``${scope}-${citus.group}-${type}``.
|
|
||||||
|
|
||||||
All Kubernetes objects are discovered by Patroni using the `label selector`__,
|
|
||||||
therefore all Pods with Patroni&Citus and Endpoints/ConfigMaps must have
|
|
||||||
similar labels, and Patroni must be configured to use them using Kubernetes
|
|
||||||
:ref:`settings <kubernetes_settings>` or :ref:`environment variables
|
|
||||||
<kubernetes_environment>`.
|
|
||||||
|
|
||||||
__ https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
|
|
||||||
|
|
||||||
A couple of examples of Patroni configuration using Pods environment variables:
|
|
||||||
|
|
||||||
1. for the coordinator cluster
|
|
||||||
|
|
||||||
.. code:: YAML
|
|
||||||
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Pod
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
citus-group: "0"
|
|
||||||
citus-type: coordinator
|
|
||||||
cluster-name: citusdemo
|
|
||||||
name: citusdemo-0-0
|
|
||||||
namespace: default
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- env:
|
|
||||||
- name: PATRONI_SCOPE
|
|
||||||
value: citusdemo
|
|
||||||
- name: PATRONI_NAME
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
apiVersion: v1
|
|
||||||
fieldPath: metadata.name
|
|
||||||
- name: PATRONI_KUBERNETES_POD_IP
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
apiVersion: v1
|
|
||||||
fieldPath: status.podIP
|
|
||||||
- name: PATRONI_KUBERNETES_NAMESPACE
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
apiVersion: v1
|
|
||||||
fieldPath: metadata.namespace
|
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
|
||||||
value: '{application: patroni}'
|
|
||||||
- name: PATRONI_CITUS_DATABASE
|
|
||||||
value: citus
|
|
||||||
- name: PATRONI_CITUS_GROUP
|
|
||||||
value: "0"
|
|
||||||
|
|
||||||
2. for the worker cluster from the group 2
|
|
||||||
|
|
||||||
.. code:: YAML
|
|
||||||
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Pod
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
citus-group: "2"
|
|
||||||
citus-type: worker
|
|
||||||
cluster-name: citusdemo
|
|
||||||
name: citusdemo-2-0
|
|
||||||
namespace: default
|
|
||||||
spec:
|
|
||||||
containers:
|
|
||||||
- env:
|
|
||||||
- name: PATRONI_SCOPE
|
|
||||||
value: citusdemo
|
|
||||||
- name: PATRONI_NAME
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
apiVersion: v1
|
|
||||||
fieldPath: metadata.name
|
|
||||||
- name: PATRONI_KUBERNETES_POD_IP
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
apiVersion: v1
|
|
||||||
fieldPath: status.podIP
|
|
||||||
- name: PATRONI_KUBERNETES_NAMESPACE
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
apiVersion: v1
|
|
||||||
fieldPath: metadata.namespace
|
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
|
||||||
value: '{application: patroni}'
|
|
||||||
- name: PATRONI_CITUS_DATABASE
|
|
||||||
value: citus
|
|
||||||
- name: PATRONI_CITUS_GROUP
|
|
||||||
value: "2"
|
|
||||||
|
|
||||||
As you may noticed, both examples have ``citus-group`` label set. This label
|
|
||||||
allows Patroni to identify object as belonging to a certain Citus group. In
|
|
||||||
addition to that, there is also ``PATRONI_CITUS_GROUP`` environment variable,
|
|
||||||
which has the same value as the ``citus-group`` label. When Patroni creates
|
|
||||||
new Kubernetes objects ConfigMaps or Endpoints, it automatically puts the
|
|
||||||
``citus-group: ${env.PATRONI_CITUS_GROUP}`` label on them:
|
|
||||||
|
|
||||||
.. code:: YAML
|
|
||||||
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-0-leader # Is generated as ${env.PATRONI_SCOPE}-${env.PATRONI_CITUS_GROUP}-leader
|
|
||||||
labels:
|
|
||||||
application: patroni # Is set from the ${env.PATRONI_KUBERNETES_LABELS}
|
|
||||||
cluster-name: citusdemo # Is automatically set from the ${env.PATRONI_SCOPE}
|
|
||||||
citus-group: '0' # Is automatically set from the ${env.PATRONI_CITUS_GROUP}
|
|
||||||
|
|
||||||
You can find a complete example of Patroni deployment on Kubernetes with Citus
|
|
||||||
support in the `kubernetes`__ folder of the Patroni repository.
|
|
||||||
|
|
||||||
__ https://github.com/zalando/patroni/tree/master/kubernetes
|
|
||||||
|
|
||||||
There are two important files for you:
|
|
||||||
|
|
||||||
1. Dockerfile.citus
|
|
||||||
2. citus_k8s.yaml
|
|
||||||
|
|
||||||
Citus upgrades and PostgreSQL major upgrades
|
|
||||||
--------------------------------------------
|
|
||||||
|
|
||||||
First, please read about upgrading Citus version in the `documentation`__.
|
|
||||||
There is one minor change in the process. When executing upgrade, you have to
|
|
||||||
use ``patronictl restart`` instead of ``systemctl restart`` to restart
|
|
||||||
PostgreSQL.
|
|
||||||
|
|
||||||
__ https://docs.citusdata.com/en/latest/admin_guide/upgrading_citus.html
|
|
||||||
|
|
||||||
The PostgreSQL major upgrade with Citus is a bit more complex. You will have to
|
|
||||||
combine techniques used in the Citus documentation about major upgrades and
|
|
||||||
Patroni documentation about :ref:`PostgreSQL major upgrade<major_upgrade>`.
|
|
||||||
Please keep in mind that Citus cluster consists of many Patroni clusters
|
|
||||||
(coordinator and workers) and they all have to be upgraded independently.
|
|
||||||
+1
-4
@@ -194,7 +194,4 @@ 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):
|
||||||
if hasattr(app, 'add_css_file'):
|
app.add_stylesheet("custom.css")
|
||||||
app.add_css_file('custom.css')
|
|
||||||
else:
|
|
||||||
app.add_stylesheet('custom.css')
|
|
||||||
|
|||||||
@@ -1,63 +0,0 @@
|
|||||||
.. _dcs_failsafe_mode:
|
|
||||||
|
|
||||||
DCS Failsafe Mode
|
|
||||||
=================
|
|
||||||
|
|
||||||
The problem
|
|
||||||
-----------
|
|
||||||
|
|
||||||
Patroni is heavily relying on Distributed Configuration Store (DCS) to solve the task of leader elections and detect network partitioning. That is, the node is allowed to run Postgres as the primary only if it can update the leader lock in DCS. In case the update of the leader lock fails, Postgres is immediately demoted and started as read-only. Depending on which DCS is used, the chances of hitting the "problem" differ. For example, with Etcd which is only used for Patroni, chances are close to zero, while with K8s API (backed by Etcd) it could be observed more frequently.
|
|
||||||
|
|
||||||
|
|
||||||
Reasons for the current implementation
|
|
||||||
---------------------------------------
|
|
||||||
|
|
||||||
The leader lock update failure could be caused by two main reasons:
|
|
||||||
|
|
||||||
1. Network partitioning
|
|
||||||
2. DCS being down
|
|
||||||
|
|
||||||
In general, it is impossible to distinguish between these two from a single node, and therefore Patroni assumes the worst case - network partitioning. In the case of a partitioned network, other nodes of the Patroni cluster may successfully grab the leader lock and promote Postgres to primary. In order to avoid a split-brain, the old primary is demoted before the leader lock expires.
|
|
||||||
|
|
||||||
|
|
||||||
DCS Failsafe Mode
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
We introduce a new special option, the ``failsafe_mode``. It could be enabled only via global configuration stored in the DCS ``/config`` key. If the failsafe mode is enabled and the leader lock update in DCS failed due to reasons different from the version/value/index mismatch, Postgres may continue to run as a primary if it can access all known members of the cluster via Patroni REST API.
|
|
||||||
|
|
||||||
|
|
||||||
Low-level implementation details
|
|
||||||
--------------------------------
|
|
||||||
|
|
||||||
- We introduce a new, permanent key in DCS, named ``/failsafe``.
|
|
||||||
- The ``/failsafe`` key contains all known members of the given Patroni cluster at a given time.
|
|
||||||
- The current leader maintains the ``/failsafe`` key.
|
|
||||||
- The member is allowed to participate in the leader race and become the new leader only if it is present in the ``/failsafe`` key.
|
|
||||||
- If the cluster consists of a single node the ``/failsafe`` key will contain a single member.
|
|
||||||
- In the case of DCS "outage" the existing primary connects to all members presented in the ``/failsafe`` key via the ``POST /failsafe`` REST API and may continue to run as the primary if all replicas acknowledge it.
|
|
||||||
- If one of the members doesn't respond, the primary is demoted.
|
|
||||||
- Replicas are using incoming ``POST /failsafe`` REST API requests as an indicator that the primary is still alive. This information is cached for ``ttl`` seconds.
|
|
||||||
|
|
||||||
|
|
||||||
F.A.Q.
|
|
||||||
------
|
|
||||||
|
|
||||||
- Why MUST the current primary see ALL other members? Can’t we rely on quorum here?
|
|
||||||
|
|
||||||
This is a great question! The problem is that the view on the quorum might be different from the perspective of DCS and Patroni. While DCS nodes must be evenly distributed across availability zones, there is no such rule for Patroni, and more importantly, there is no mechanism for introducing and enforcing such a rule. If the majority of Patroni nodes ends up in the losing part of the partitioned network (including primary) while minority nodes are in the winning part, the primary must be demoted. Only checking ALL other members allows detecting such a situation.
|
|
||||||
|
|
||||||
- What if node/pod gets terminated while DCS is down?
|
|
||||||
|
|
||||||
If DCS isn’t accessible, the check “are ALL other cluster members accessible?” is executed every cycle of the heartbeat loop (every ``loop_wait`` seconds). If pod/node is terminated, the check will fail and Postgres will be demoted to a read-only and will not recover until DCS is restored.
|
|
||||||
|
|
||||||
- What if all members of the Patroni cluster are lost while DCS is down?
|
|
||||||
|
|
||||||
Patroni could be configured to create the new replica from the backup even when the cluster doesn't have a leader. But, if the new member isn't present in the ``/failsafe`` key, it will not be able to grab the leader lock and promote.
|
|
||||||
|
|
||||||
- What will happen if the primary lost access to DCS while replicas didn't?
|
|
||||||
|
|
||||||
The primary will execute the failsafe code and contact all known replicas. These replicas will use this information as an indicator that the primary is alive and will not start the leader race even if the leader lock in DCS has expired.
|
|
||||||
|
|
||||||
- How to enable the Failsafe Mode?
|
|
||||||
|
|
||||||
Before enabling the ``failsafe_mode`` please make sure that Patroni version on all members is up-to-date. After that, you can use either the ``PATCH /config`` :ref:`REST API <rest_api>` or ``patronictl edit-config -s failsafe_mode=true``
|
|
||||||
@@ -14,15 +14,13 @@ 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 reloaded in runtime (without restart of Patroni) by sending SIGHUP to the Patroni process, performing ``POST /reload`` REST-API request or executing ``patronictl reload``.
|
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.
|
||||||
|
|
||||||
- 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``).
|
||||||
|
|
||||||
The local configuration can be either a single YAML file or a directory. When it is a directory, all YAML files in that directory are loaded one by one in sorted order. In case a key is defined in multiple files, the occurrence in the last file takes precedence.
|
Some of the PostgreSQL parameters must hold the same values on the master and the replicas. For those, values set either in the local patroni configuration files or via the environment variables take no effect. To alter or set their values one must change the shared configuration in the DCS. Below is the actual list of such parameters together with the default values:
|
||||||
|
|
||||||
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
|
||||||
@@ -32,12 +30,11 @@ Some of the PostgreSQL parameters must hold the same values on the primary and t
|
|||||||
- 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 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
|
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
|
||||||
|
|
||||||
- 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.
|
||||||
|
|
||||||
@@ -79,11 +76,154 @@ 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 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.
|
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.
|
||||||
|
|
||||||
|
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,23 +23,6 @@ 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:
|
|
||||||
|
|
||||||
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
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
.. _ha_multi_dc:
|
|
||||||
|
|
||||||
=================
|
|
||||||
HA multi datacenter
|
|
||||||
=================
|
|
||||||
|
|
||||||
The high availability of a PostgreSQL cluster deployed in multiple data centers is based on replication, which can be synchronous or asynchronous (`replication_modes <replication_modes.rst>`_).
|
|
||||||
|
|
||||||
In both cases, it is important to be clear about the following concepts:
|
|
||||||
|
|
||||||
- Postgres can run as primary or standby leader only when it owns the leading key and can update the leading key.
|
|
||||||
- You should run the odd number of etcd, ZooKeeper or Consul nodes: 3 or 5!
|
|
||||||
|
|
||||||
Synchronous Replication
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
To have a multi DC cluster that can automatically tolerate a zone drop, a minimum of 3 is required.
|
|
||||||
|
|
||||||
The architecture diagram would be the following:
|
|
||||||
|
|
||||||
.. image:: _static/multi-dc-synchronous-replication.png
|
|
||||||
|
|
||||||
We must deploy a cluster of etcd, ZooKeeper or Consul through the different DC, with a minimum of 3 nodes, one in each zone.
|
|
||||||
|
|
||||||
Regarding postgres, we must deploy at least 2 nodes, in different DC. Then you have to set ``synchronous_mode: true`` in the global configuration (``patronictl edit-config``).
|
|
||||||
|
|
||||||
This enables sync replication and the primary node will choose one of the nodes as synchronous.
|
|
||||||
|
|
||||||
Asynchronous Replication
|
|
||||||
----------------------------------
|
|
||||||
|
|
||||||
With only two data centers it would be better to have two independent etcd clusters and run Patroni :ref:`standby cluster <standby_cluster>` in the second data center. If the first site is down, you can MANUALLY promote the ``standby_cluster``.
|
|
||||||
|
|
||||||
The architecture diagram would be the following:
|
|
||||||
|
|
||||||
.. image:: _static/multi-dc-asynchronous-replication.png
|
|
||||||
|
|
||||||
Automatic promotion is not possible, because DC2 will never able to figure out the state of DC1.
|
|
||||||
|
|
||||||
You should not use ``pg_ctl promote`` in this scenario, you need "manually promote" the healthy cluster with ``patronictl edit-config`` and remove ``standby_cluster`` section from there.
|
|
||||||
|
|
||||||
.. warning::
|
|
||||||
If the source cluster is still up and running and you promote the standby cluster you create a split-brain.
|
|
||||||
|
|
||||||
In case you want to return to the "initial" state, there are only two ways of resolving it:
|
|
||||||
|
|
||||||
- Add the standby_cluster section back and it will trigger pg_rewind, but there are chances that pg_rewind will fail.
|
|
||||||
- Rebuild the standby cluster from scratch.
|
|
||||||
|
|
||||||
Before promoting standby cluster one have to manually ensure that the source cluster is down (STONITH). When DC1 recovers, the cluster has to be converted to a standby cluster.
|
|
||||||
|
|
||||||
Before doing that you may manually examine the database and extract all changes that happened between the time when network between DC1 and DC2 has stopped working and the time when you manually stopped the cluster in DC1.
|
|
||||||
|
|
||||||
Once extracted, you may also manually apply these changes to the cluster in DC2.
|
|
||||||
@@ -10,10 +10,6 @@ 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 Citus users**: Starting from 3.0 Patroni nicely integrates with the `Citus <https://github.com/citusdata/citus>`__ database extension to Postgres. Please check the :ref:`Citus support page <citus>` in the Patroni documentation for more info about how to use Patroni high availability together with a Citus distributed cluster.
|
|
||||||
|
|
||||||
**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.
|
||||||
|
|
||||||
|
|
||||||
@@ -22,17 +18,11 @@ Currently supported PostgreSQL versions: 9.3 to 15.
|
|||||||
:caption: Contents:
|
:caption: Contents:
|
||||||
|
|
||||||
README
|
README
|
||||||
citus
|
|
||||||
dynamic_configuration
|
dynamic_configuration
|
||||||
dcs_failsafe_mode
|
|
||||||
rest_api
|
|
||||||
existing_data
|
|
||||||
ENVIRONMENT
|
ENVIRONMENT
|
||||||
SETTINGS
|
SETTINGS
|
||||||
security
|
|
||||||
replica_bootstrap
|
replica_bootstrap
|
||||||
replication_modes
|
replication_modes
|
||||||
ha_multi_dc
|
|
||||||
pause
|
pause
|
||||||
kubernetes
|
kubernetes
|
||||||
watchdog
|
watchdog
|
||||||
|
|||||||
+5
-2
@@ -23,7 +23,10 @@ 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.
|
||||||
|
|
||||||
To direct the traffic to the Postgres leader you need to configure the Kubernetes Postgres service to use the label selector with the `role_label` (configured in patroni configuration).
|
There are two ways to direct the traffic to the Postgres master:
|
||||||
|
|
||||||
|
- 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).
|
||||||
|
|
||||||
Note that in some cases, for instance, when running on OpenShift, there is no alternative to using ConfigMaps.
|
Note that in some cases, for instance, when running on OpenShift, there is no alternative to using ConfigMaps.
|
||||||
|
|
||||||
@@ -36,7 +39,7 @@ Examples
|
|||||||
--------
|
--------
|
||||||
|
|
||||||
- The `kubernetes <https://github.com/zalando/patroni/tree/master/kubernetes>`__ folder of the Patroni repository contains
|
- The `kubernetes <https://github.com/zalando/patroni/tree/master/kubernetes>`__ folder of the Patroni repository contains
|
||||||
examples of the Docker image, and the Kubernetes manifest to test Patroni Kubernetes setup.
|
examples of the Docker image, the Kubernetes manifest and the callback script in order to test Patroni Kubernetes setup.
|
||||||
Note that in the current state it will not be able to use PersistentVolumes because of permission issues.
|
Note that in the current state it will not be able to use PersistentVolumes because of permission issues.
|
||||||
|
|
||||||
- You can find the full-featured Docker image that can use Persistent Volumes in the
|
- You can find the full-featured Docker image that can use Persistent Volumes in the
|
||||||
|
|||||||
+5
-7
@@ -6,7 +6,7 @@ Pause/Resume mode for the cluster
|
|||||||
The goal
|
The goal
|
||||||
--------
|
--------
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -17,18 +17,16 @@ 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 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.
|
- 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.
|
||||||
|
|
||||||
- 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.
|
- 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.
|
||||||
|
|
||||||
- If 'parallel' primaries are detected by Patroni, it emits a warning, but does not demote the primary without the leader lock.
|
- If 'parallel' masters are detected by Patroni, it emits a warning, but does not demote the masters without the leader lock.
|
||||||
|
|
||||||
- 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.
|
- 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.
|
||||||
|
|
||||||
- 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
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|||||||
+10
-1401
File diff suppressed because it is too large
Load Diff
+11
-31
@@ -24,7 +24,6 @@ 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
|
||||||
@@ -41,8 +40,6 @@ 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.
|
||||||
@@ -63,7 +60,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 leader node. Another one is the lack of 'on-the-fly' compression for the backup data and no built-in cleanup
|
a running master 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:
|
||||||
@@ -77,7 +74,7 @@ scripts to clone a new replica. Those are configured in the ``postgresql`` confi
|
|||||||
command: <command name>
|
command: <command name>
|
||||||
keep_data: True
|
keep_data: True
|
||||||
no_params: True
|
no_params: True
|
||||||
no_leader: 1
|
no_master: 1
|
||||||
|
|
||||||
example: wal_e
|
example: wal_e
|
||||||
|
|
||||||
@@ -89,7 +86,7 @@ example: wal_e
|
|||||||
- basebackup
|
- basebackup
|
||||||
wal_e:
|
wal_e:
|
||||||
command: patroni_wale_restore
|
command: patroni_wale_restore
|
||||||
no_leader: 1
|
no_master: 1
|
||||||
envdir: {{WALE_ENV_DIR}}
|
envdir: {{WALE_ENV_DIR}}
|
||||||
use_iam: 1
|
use_iam: 1
|
||||||
basebackup:
|
basebackup:
|
||||||
@@ -123,30 +120,26 @@ 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 (primary or other replica). The user in the
|
Connection string to connect to the cluster member to clone from (master 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_leader`` 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 leader or replicas. In that case, an empty string will be passed in a connection string. This is useful for
|
running master 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 instruct Patroni to not clean PGDATA folder before calling restore.
|
A special ``keep_data`` parameter, if defined, will instuct 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 leader unless there are replicas with ``clonefrom`` tag, in which case one
|
``pg_basebackup``, the base backup is taken from the master 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:
|
||||||
@@ -166,7 +159,6 @@ 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.
|
||||||
|
|
||||||
@@ -176,10 +168,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 node. This type of clusters has:
|
standby nodes replicating from some remote master. 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 node.
|
except it replicates from a remote master.
|
||||||
|
|
||||||
* cascade replicas, that are replicating from standby leader.
|
* cascade replicas, that are replicating from standby leader.
|
||||||
|
|
||||||
@@ -187,13 +179,6 @@ 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
|
||||||
@@ -219,9 +204,4 @@ 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.
|
||||||
|
|
||||||
Patroni expects to find `postgresql.conf` or `postgresql.conf.backup` in 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 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.
|
||||||
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 primary 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 master 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_mode``. This parameter prevents Patroni from switching off the
|
``synchronous_node``. 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,16 +57,11 @@ 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 databases. 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. 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.
|
||||||
|
|
||||||
@@ -74,9 +69,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 assign one or more synchronous standby nodes based on ``synchronous_node_count`` parameter to ``synchronous_standby_names``.
|
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.
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
|
||||||
.. [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.
|
||||||
|
|||||||
@@ -1,455 +0,0 @@
|
|||||||
.. _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 /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 /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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Retrieve the Patroni metrics in Prometheus format through the ``GET /metrics`` endpoint.
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
$ curl http://localhost:8008/metrics
|
|
||||||
|
|
||||||
# HELP patroni_version Patroni semver without periods. \
|
|
||||||
# TYPE patroni_version gauge
|
|
||||||
patroni_version{scope="batman"} 020103
|
|
||||||
# HELP patroni_postgres_running Value is 1 if Postgres is running, 0 otherwise.
|
|
||||||
# TYPE patroni_postgres_running gauge
|
|
||||||
patroni_postgres_running{scope="batman"} 1
|
|
||||||
# HELP patroni_postmaster_start_time Epoch seconds since Postgres started.
|
|
||||||
# TYPE patroni_postmaster_start_time gauge
|
|
||||||
patroni_postmaster_start_time{scope="batman"} 1657656955.179243
|
|
||||||
# HELP patroni_master Value is 1 if this node is the leader, 0 otherwise.
|
|
||||||
# TYPE patroni_master gauge
|
|
||||||
patroni_master{scope="batman"} 1
|
|
||||||
# HELP patroni_xlog_location Current location of the Postgres transaction log, 0 if this node is not the leader.
|
|
||||||
# TYPE patroni_xlog_location counter
|
|
||||||
patroni_xlog_location{scope="batman"} 22320573386952
|
|
||||||
# HELP patroni_standby_leader Value is 1 if this node is the standby_leader, 0 otherwise.
|
|
||||||
# TYPE patroni_standby_leader gauge
|
|
||||||
patroni_standby_leader{scope="batman"} 0
|
|
||||||
# HELP patroni_replica Value is 1 if this node is a replica, 0 otherwise.
|
|
||||||
# TYPE patroni_replica gauge
|
|
||||||
patroni_replica{scope="batman"} 0
|
|
||||||
# HELP patroni_sync_standby Value is 1 if this node is a sync standby replica, 0 otherwise.
|
|
||||||
# TYPE patroni_sync_standby gauge
|
|
||||||
patroni_sync_standby{scope="batman"} 0
|
|
||||||
# HELP patroni_xlog_received_location Current location of the received Postgres transaction log, 0 if this node is not a replica.
|
|
||||||
# TYPE patroni_xlog_received_location counter
|
|
||||||
patroni_xlog_received_location{scope="batman"} 0
|
|
||||||
# HELP patroni_xlog_replayed_location Current location of the replayed Postgres transaction log, 0 if this node is not a replica.
|
|
||||||
# TYPE patroni_xlog_replayed_location counter
|
|
||||||
patroni_xlog_replayed_location{scope="batman"} 0
|
|
||||||
# HELP patroni_xlog_replayed_timestamp Current timestamp of the replayed Postgres transaction log, 0 if null.
|
|
||||||
# TYPE patroni_xlog_replayed_timestamp gauge
|
|
||||||
patroni_xlog_replayed_timestamp{scope="batman"} 0
|
|
||||||
# HELP patroni_xlog_paused Value is 1 if the Postgres xlog is paused, 0 otherwise.
|
|
||||||
# TYPE patroni_xlog_paused gauge
|
|
||||||
patroni_xlog_paused{scope="batman"} 0
|
|
||||||
# HELP patroni_postgres_server_version Version of Postgres (if running), 0 otherwise.
|
|
||||||
# TYPE patroni_postgres_server_version gauge
|
|
||||||
patroni_postgres_server_version {scope="batman"} 140004
|
|
||||||
# HELP patroni_cluster_unlocked Value is 1 if the cluster is unlocked, 0 if locked.
|
|
||||||
# TYPE patroni_cluster_unlocked gauge
|
|
||||||
patroni_cluster_unlocked{scope="batman"} 0
|
|
||||||
# HELP patroni_postgres_timeline Postgres timeline of this node (if running), 0 otherwise.
|
|
||||||
# TYPE patroni_postgres_timeline counter
|
|
||||||
patroni_postgres_timeline{scope="batman"} 24
|
|
||||||
# HELP patroni_dcs_last_seen Epoch timestamp when DCS was last contacted successfully by Patroni.
|
|
||||||
# TYPE patroni_dcs_last_seen gauge
|
|
||||||
patroni_dcs_last_seen{scope="batman"} 1677658321
|
|
||||||
# HELP patroni_pending_restart Value is 1 if the node needs a restart, 0 otherwise.
|
|
||||||
# TYPE patroni_pending_restart gauge
|
|
||||||
patroni_pending_restart{scope="batman"} 1
|
|
||||||
# HELP patroni_is_paused Value is 1 if auto failover is disabled, 0 otherwise.
|
|
||||||
# TYPE patroni_is_paused gauge
|
|
||||||
patroni_is_paused{scope="batman"} 1
|
|
||||||
|
|
||||||
|
|
||||||
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 ``primary_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``.
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
.. _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 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:
|
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:
|
||||||
|
|
||||||
- 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 primary 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 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.
|
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.
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
|
|||||||
@@ -9,5 +9,5 @@ check_cmd = "/usr/sbin/haproxy -c -f {{ .src }}"
|
|||||||
reload_cmd = "haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -D -sf $(cat /var/run/haproxy.pid)"
|
reload_cmd = "haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -D -sf $(cat /var/run/haproxy.pid)"
|
||||||
|
|
||||||
keys = [
|
keys = [
|
||||||
"/",
|
"/members/",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
global
|
|
||||||
maxconn 100
|
|
||||||
|
|
||||||
defaults
|
|
||||||
log global
|
|
||||||
mode tcp
|
|
||||||
retries 2
|
|
||||||
timeout client 30m
|
|
||||||
timeout connect 4s
|
|
||||||
timeout server 30m
|
|
||||||
timeout check 5s
|
|
||||||
|
|
||||||
listen stats
|
|
||||||
mode http
|
|
||||||
bind *:7000
|
|
||||||
stats enable
|
|
||||||
stats uri /
|
|
||||||
|
|
||||||
listen coordinator
|
|
||||||
bind *:5000
|
|
||||||
option httpchk HEAD /primary
|
|
||||||
http-check expect status 200
|
|
||||||
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
|
|
||||||
{{range gets "/0/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} maxconn 100 check check-ssl port {{index (split (index (split $data.api_url "/") 2) ":") 1}} verify required ca-file /etc/ssl/certs/ssl-cert-snakeoil.pem crt /etc/ssl/private/ssl-cert-snakeoil.crt
|
|
||||||
{{end}}
|
|
||||||
listen workers
|
|
||||||
bind *:5001
|
|
||||||
option httpchk HEAD /primary
|
|
||||||
http-check expect status 200
|
|
||||||
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
|
|
||||||
{{range gets "/*/members/*"}}{{$group := index (split .Key "/") 1}}{{if ne $group "0"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} maxconn 100 check check-ssl port {{index (split (index (split $data.api_url "/") 2) ":") 1}} verify required ca-file /etc/ssl/certs/ssl-cert-snakeoil.pem crt /etc/ssl/private/ssl-cert-snakeoil.crt
|
|
||||||
{{end}}{{end}}
|
|
||||||
@@ -16,16 +16,16 @@ listen stats
|
|||||||
stats enable
|
stats enable
|
||||||
stats uri /
|
stats uri /
|
||||||
|
|
||||||
listen primary
|
listen master
|
||||||
bind *:5000
|
bind *:5000
|
||||||
option httpchk HEAD /primary
|
option httpchk OPTIONS /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 HEAD /replica
|
option httpchk OPTIONS /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}}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# startup scripts for Patroni
|
# startup scripts for Patroni
|
||||||
|
|
||||||
This directory contains sample startup scripts for various OSes
|
This directory contains sample startup scripts for various OSes
|
||||||
and management tools for Patroni.
|
and management tools for Patroni.
|
||||||
|
|
||||||
Scripts supplied:
|
Scripts supplied:
|
||||||
@@ -10,7 +10,7 @@ Scripts supplied:
|
|||||||
Upstart job for Ubuntu 12.04 or 14.04. Requires Upstart > 1.4. Intended for systems where Patroni has been installed on a base system, rather than in Docker.
|
Upstart job for Ubuntu 12.04 or 14.04. Requires Upstart > 1.4. Intended for systems where Patroni has been installed on a base system, rather than in Docker.
|
||||||
|
|
||||||
### patroni.service
|
### patroni.service
|
||||||
Systemd service file, to be copied to /etc/systemd/system/patroni.service, tested on Centos 7.1 with Patroni installed from pip.
|
Systemd service file, to be copied to /etc/systemd/system/patroni.service, tested on Centos 7.1 with Patroni installed from pip.
|
||||||
|
|
||||||
### patroni
|
### patroni
|
||||||
Init.d service file for Debian-like distributions. Copy it to /etc/init.d/, make executable:
|
Init.d service file for Debian-like distributions. Copy it to /etc/init.d/, make executable:
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ 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
|
||||||
|
|
||||||
# The default is the user's home directory, and if you want to change it, you must provide an absolute path.
|
WorkingDirectory=~
|
||||||
# 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
|
||||||
@@ -32,14 +31,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
|
||||||
|
|
||||||
# Restart the service if it crashed
|
# Do not restart the service if it crashes, we want to manually inspect database on failure
|
||||||
Restart=on-failure
|
Restart=no
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
#!/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)
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#!/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))
|
|
||||||
Executable
+22
@@ -0,0 +1,22 @@
|
|||||||
|
#!/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
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#!/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)
|
|
||||||
Executable
+21
@@ -0,0 +1,21 @@
|
|||||||
|
#!/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,8 +4,7 @@ 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
|
||||||
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, "loop_wait": 2, "synchronous_mode": true}
|
||||||
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
|
||||||
@@ -21,40 +20,12 @@ Feature: basic replication
|
|||||||
And I shut down postgres1
|
And I shut down postgres1
|
||||||
Then "sync" key in DCS has sync_standby=postgres2 after 10 seconds
|
Then "sync" key in DCS has sync_standby=postgres2 after 10 seconds
|
||||||
When I start postgres1
|
When I start postgres1
|
||||||
Then "members/postgres1" key in DCS has state=running after 10 seconds
|
And "members/postgres1" key in DCS has state=running after 10 seconds
|
||||||
And Status code on GET http://127.0.0.1:8010/sync is 200 after 3 seconds
|
And I sleep for 2 seconds
|
||||||
And Status code on GET http://127.0.0.1:8009/async is 200 after 3 seconds
|
When I issue a GET request to http://127.0.0.1:8010/sync
|
||||||
|
|
||||||
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
|
Then I receive a response code 200
|
||||||
And I create table on postgres0
|
When I issue a GET request to http://127.0.0.1:8009/async
|
||||||
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 Status code on GET http://127.0.0.1:8009/sync is 200 after 3 seconds
|
|
||||||
And Status code on GET http://127.0.0.1:8010/async is 200 after 3 seconds
|
|
||||||
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
|
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
|
|
||||||
Then "sync" key in DCS has sync_standby=postgres1,postgres2 after 10 seconds
|
|
||||||
And Status code on GET http://127.0.0.1:8010/sync is 200 after 3 seconds
|
|
||||||
And Status code on GET http://127.0.0.1:8009/sync is 200 after 3 seconds
|
|
||||||
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
|
|
||||||
Then "sync" key in DCS has sync_standby=postgres2 after 10 seconds
|
|
||||||
When I start postgres1
|
|
||||||
Then "members/postgres1" key in DCS has state=running after 10 seconds
|
|
||||||
And Status code on GET http://127.0.0.1:8010/sync is 200 after 3 seconds
|
|
||||||
And Status code on GET http://127.0.0.1:8009/async is 200 after 3 seconds
|
|
||||||
|
|
||||||
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
|
||||||
@@ -64,20 +35,17 @@ 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
|
|
||||||
And there is a postgres2_cb.log with "on_role_change master batman" in postgres2 data directory
|
|
||||||
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
|
||||||
Then postgres1 is a leader after 10 seconds
|
Then postgres1 is a leader after 10 seconds
|
||||||
And postgres1 role is the primary after 10 seconds
|
And postgres1 role is the primary after 10 seconds
|
||||||
|
|
||||||
Scenario: check rejoin of the former primary with pg_rewind
|
Scenario: check rejoin of the former master with pg_rewind
|
||||||
Given I add the table splitbrain to postgres0
|
Given I add the table splitbrain to postgres0
|
||||||
And I start postgres0
|
And I start postgres0
|
||||||
Then postgres0 role is the secondary after 20 seconds
|
Then postgres0 role is the secondary after 20 seconds
|
||||||
|
|||||||
Executable
+5
@@ -0,0 +1,5 @@
|
|||||||
|
#!/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
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
#!/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")
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
Feature: citus
|
|
||||||
We should check that coordinator discovers and registers workers and clients don't have errors when worker cluster switches over
|
|
||||||
|
|
||||||
Scenario: check that worker cluster is registered in the coordinator
|
|
||||||
Given I start postgres0 in citus group 0
|
|
||||||
And I start postgres2 in citus group 1
|
|
||||||
Then postgres0 is a leader in a group 0 after 10 seconds
|
|
||||||
And postgres2 is a leader in a group 1 after 10 seconds
|
|
||||||
When I start postgres1 in citus group 0
|
|
||||||
And I start postgres3 in citus group 1
|
|
||||||
Then replication works from postgres0 to postgres1 after 15 seconds
|
|
||||||
Then replication works from postgres2 to postgres3 after 15 seconds
|
|
||||||
And postgres0 is registered in the postgres0 as the worker in group 0
|
|
||||||
And postgres2 is registered in the postgres0 as the worker in group 1
|
|
||||||
|
|
||||||
Scenario: coordinator failover updates pg_dist_node
|
|
||||||
Given I run patronictl.py failover batman --group 0 --candidate postgres1 --force
|
|
||||||
Then postgres1 role is the primary after 10 seconds
|
|
||||||
And replication works from postgres1 to postgres0 after 15 seconds
|
|
||||||
And "sync" key in a group 0 in DCS has sync_standby=postgres0 after 15 seconds
|
|
||||||
And postgres1 is registered in the postgres2 as the worker in group 0
|
|
||||||
When I run patronictl.py failover batman --group 0 --candidate postgres0 --force
|
|
||||||
Then postgres0 role is the primary after 10 seconds
|
|
||||||
And replication works from postgres0 to postgres1 after 15 seconds
|
|
||||||
And "sync" key in a group 0 in DCS has sync_standby=postgres1 after 15 seconds
|
|
||||||
And postgres0 is registered in the postgres2 as the worker in group 0
|
|
||||||
|
|
||||||
Scenario: worker switchover doesn't break client queries on the coordinator
|
|
||||||
Given I create a distributed table on postgres0
|
|
||||||
And I start a thread inserting data on postgres0
|
|
||||||
When I run patronictl.py switchover batman --group 1 --force
|
|
||||||
Then I receive a response returncode 0
|
|
||||||
And postgres3 role is the primary after 10 seconds
|
|
||||||
And replication works from postgres3 to postgres2 after 15 seconds
|
|
||||||
And "sync" key in a group 1 in DCS has sync_standby=postgres2 after 15 seconds
|
|
||||||
And postgres3 is registered in the postgres0 as the worker in group 1
|
|
||||||
And a thread is still alive
|
|
||||||
When I run patronictl.py switchover batman --group 1 --force
|
|
||||||
Then I receive a response returncode 0
|
|
||||||
And postgres2 role is the primary after 10 seconds
|
|
||||||
And replication works from postgres2 to postgres3 after 15 seconds
|
|
||||||
And "sync" key in a group 1 in DCS has sync_standby=postgres3 after 15 seconds
|
|
||||||
And postgres2 is registered in the postgres0 as the worker in group 1
|
|
||||||
And a thread is still alive
|
|
||||||
When I stop a thread
|
|
||||||
Then a distributed table on postgres0 has expected rows
|
|
||||||
|
|
||||||
Scenario: worker primary restart doesn't break client queries on the coordinator
|
|
||||||
Given I cleanup a distributed table on postgres0
|
|
||||||
And I start a thread inserting data on postgres0
|
|
||||||
When I run patronictl.py restart batman postgres2 --group 1 --force
|
|
||||||
Then I receive a response returncode 0
|
|
||||||
And postgres2 role is the primary after 10 seconds
|
|
||||||
And replication works from postgres2 to postgres3 after 15 seconds
|
|
||||||
And postgres2 is registered in the postgres0 as the worker in group 1
|
|
||||||
And a thread is still alive
|
|
||||||
When I stop a thread
|
|
||||||
Then a distributed table on postgres0 has expected rows
|
|
||||||
|
|
||||||
Scenario: check that in-flight transaction is rolled back after timeout when other workers need to change pg_dist_node
|
|
||||||
Given I start postgres4 in citus group 2
|
|
||||||
Then postgres4 is a leader in a group 2 after 10 seconds
|
|
||||||
And "members/postgres4" key in a group 2 in DCS has role=master after 3 seconds
|
|
||||||
When I run patronictl.py edit-config batman --group 2 -s ttl=20 --force
|
|
||||||
Then I receive a response returncode 0
|
|
||||||
And I receive a response output "+ttl: 20"
|
|
||||||
When I sleep for 2 seconds
|
|
||||||
Then postgres4 is registered in the postgres2 as the worker in group 2
|
|
||||||
When I shut down postgres4
|
|
||||||
Then There is a transaction in progress on postgres0 changing pg_dist_node
|
|
||||||
When I run patronictl.py restart batman postgres2 --group 1 --force
|
|
||||||
Then a transaction finishes in 20 seconds
|
|
||||||
@@ -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 30 seconds
|
Then postgres2 is a leader of batman2 after 10 seconds
|
||||||
And table bar is present on postgres2 after 10 seconds
|
And table bar is present on postgres2 after 10 seconds
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
Feature: dcs failsafe mode
|
|
||||||
We should check the basic dcs failsafe mode functioning
|
|
||||||
|
|
||||||
Scenario: check failsafe mode can be successfully enabled
|
|
||||||
Given I start postgres0
|
|
||||||
And postgres0 is a leader after 10 seconds
|
|
||||||
And I sleep for 3 seconds
|
|
||||||
When I issue a PATCH request to http://127.0.0.1:8008/config with {"loop_wait": 2, "ttl": 20, "retry_timeout": 5, "failsafe_mode": true}
|
|
||||||
Then I receive a response code 200
|
|
||||||
And Response on GET http://127.0.0.1:8008/failsafe contains postgres0 after 10 seconds
|
|
||||||
When I issue a GET request to http://127.0.0.1:8008/failsafe
|
|
||||||
Then I receive a response code 200
|
|
||||||
And I receive a response postgres0 http://127.0.0.1:8008/patroni
|
|
||||||
When I issue a PATCH request to http://127.0.0.1:8008/config with {"postgresql": {"parameters": {"wal_level": "logical"}}}
|
|
||||||
Then I receive a response code 200
|
|
||||||
When I issue a PATCH request to http://127.0.0.1:8008/config with {"slots": {"dcs_slot_0": {"type": "logical", "database": "postgres", "plugin": "test_decoding"}}}
|
|
||||||
Then I receive a response code 200
|
|
||||||
|
|
||||||
@dcs-failsafe
|
|
||||||
Scenario: check one-node cluster is functioning while DCS is down
|
|
||||||
Given DCS is down
|
|
||||||
Then Response on GET http://127.0.0.1:8008/primary contains failsafe_mode_is_active after 12 seconds
|
|
||||||
And postgres0 role is the primary after 10 seconds
|
|
||||||
|
|
||||||
@dcs-failsafe
|
|
||||||
Scenario: check new replica isn't promoted when leader is down and DCS is up
|
|
||||||
Given DCS is up
|
|
||||||
When I do a backup of postgres0
|
|
||||||
And I shut down postgres0
|
|
||||||
When I start postgres1 in a cluster batman from backup with no_leader
|
|
||||||
And I sleep for 2 seconds
|
|
||||||
Then postgres1 role is the replica after 12 seconds
|
|
||||||
|
|
||||||
Scenario: check leader and replica are both in /failsafe key after leader is back
|
|
||||||
Given I start postgres0
|
|
||||||
And I start postgres1
|
|
||||||
Then "members/postgres0" key in DCS has state=running after 10 seconds
|
|
||||||
And "members/postgres1" key in DCS has state=running after 2 seconds
|
|
||||||
And Response on GET http://127.0.0.1:8009/failsafe contains postgres1 after 10 seconds
|
|
||||||
When I issue a GET request to http://127.0.0.1:8009/failsafe
|
|
||||||
Then I receive a response code 200
|
|
||||||
And I receive a response postgres0 http://127.0.0.1:8008/patroni
|
|
||||||
And I receive a response postgres1 http://127.0.0.1:8009/patroni
|
|
||||||
|
|
||||||
@dcs-failsafe
|
|
||||||
@slot-advance
|
|
||||||
Scenario: check leader and replica are functioning while DCS is down
|
|
||||||
Given logical slot dcs_slot_0 is in sync between postgres0 and postgres1 after 10 seconds
|
|
||||||
And DCS is down
|
|
||||||
Then Response on GET http://127.0.0.1:8008/primary contains failsafe_mode_is_active after 12 seconds
|
|
||||||
Then postgres0 role is the primary after 10 seconds
|
|
||||||
And postgres1 role is the replica after 2 seconds
|
|
||||||
And replication works from postgres0 to postgres1 after 10 seconds
|
|
||||||
And I get all changes from logical slot dcs_slot_0 on postgres0
|
|
||||||
And logical slot dcs_slot_0 is in sync between postgres0 and postgres1 after 20 seconds
|
|
||||||
|
|
||||||
@dcs-failsafe
|
|
||||||
Scenario: check primary is demoted when one replica is shut down and DCS is down
|
|
||||||
Given DCS is down
|
|
||||||
And I kill postgres1
|
|
||||||
And I kill postmaster on postgres1
|
|
||||||
And I sleep for 2 seconds
|
|
||||||
Then postgres0 role is the replica after 12 seconds
|
|
||||||
|
|
||||||
@dcs-failsafe
|
|
||||||
Scenario: check known replica is promoted when leader is down and DCS is up
|
|
||||||
Given I shut down postgres0
|
|
||||||
And DCS is up
|
|
||||||
When I start postgres1
|
|
||||||
Then "members/postgres1" key in DCS has state=running after 10 seconds
|
|
||||||
And postgres1 role is the primary after 25 seconds
|
|
||||||
|
|
||||||
@dcs-failsafe
|
|
||||||
Scenario: check three-node cluster is functioning while DCS is down
|
|
||||||
Given I start postgres0
|
|
||||||
And I start postgres2
|
|
||||||
Then "members/postgres2" key in DCS has state=running after 10 seconds
|
|
||||||
And "members/postgres0" key in DCS has state=running after 20 seconds
|
|
||||||
And Response on GET http://127.0.0.1:8008/failsafe contains postgres2 after 10 seconds
|
|
||||||
And replication works from postgres1 to postgres0 after 10 seconds
|
|
||||||
Given DCS is down
|
|
||||||
Then Response on GET http://127.0.0.1:8008/primary contains failsafe_mode_is_active after 12 seconds
|
|
||||||
Then postgres1 role is the primary after 10 seconds
|
|
||||||
And postgres0 role is the replica after 2 seconds
|
|
||||||
And postgres2 role is the replica after 2 seconds
|
|
||||||
+133
-440
@@ -1,12 +1,16 @@
|
|||||||
import abc
|
import abc
|
||||||
|
import consul
|
||||||
import datetime
|
import datetime
|
||||||
import glob
|
import etcd
|
||||||
|
import kazoo.client
|
||||||
|
import kazoo.exceptions
|
||||||
import os
|
import os
|
||||||
import json
|
|
||||||
import psutil
|
import psutil
|
||||||
import re
|
import psycopg2
|
||||||
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
import signal
|
import signal
|
||||||
|
import six
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -14,13 +18,9 @@ import threading
|
|||||||
import time
|
import time
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
import patroni.psycopg as psycopg
|
|
||||||
|
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
@six.add_metaclass(abc.ABCMeta)
|
||||||
from patroni.request import PatroniRequest
|
class AbstractController(object):
|
||||||
|
|
||||||
|
|
||||||
class AbstractController(abc.ABC):
|
|
||||||
|
|
||||||
def __init__(self, context, name, work_directory, output_dir):
|
def __init__(self, context, name, work_directory, output_dir):
|
||||||
self._context = context
|
self._context = context
|
||||||
@@ -101,7 +101,6 @@ class PatroniController(AbstractController):
|
|||||||
self.watchdog = None
|
self.watchdog = None
|
||||||
|
|
||||||
self._scope = (custom_config or {}).get('scope', 'batman')
|
self._scope = (custom_config or {}).get('scope', 'batman')
|
||||||
self._citus_group = (custom_config or {}).get('citus', {}).get('group')
|
|
||||||
self._config = self._make_patroni_test_config(name, custom_config)
|
self._config = self._make_patroni_test_config(name, custom_config)
|
||||||
self._closables = []
|
self._closables = []
|
||||||
|
|
||||||
@@ -141,24 +140,12 @@ 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._citus_group)
|
self._context.dcs_ctl.create_pod(self._name[8:], self._scope)
|
||||||
env['PATRONI_KUBERNETES_POD_IP'] = '10.0.0.' + self._name[-1]
|
os.environ['PATRONI_KUBERNETES_POD_IP'] = '10.0.0.' + self._name[-1]
|
||||||
if os.name == 'nt':
|
return subprocess.Popen([sys.executable, '-m', 'coverage', 'run',
|
||||||
env['BEHAVE_DEBUG'] = 'true'
|
'--source=patroni', '-p', 'patroni.py', self._config],
|
||||||
patroni = subprocess.Popen([sys.executable, '-m', 'coverage', 'run',
|
stdout=self._log, stderr=subprocess.STDOUT, cwd=self._work_directory)
|
||||||
'--source=patroni', '-p', 'patroni.py', self._config], env=env,
|
|
||||||
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:
|
||||||
@@ -179,61 +166,21 @@ class PatroniController(AbstractController):
|
|||||||
patroni_config_name = self.PATRONI_CONFIG.format(name)
|
patroni_config_name = self.PATRONI_CONFIG.format(name)
|
||||||
patroni_config_path = os.path.join(self._output_dir, patroni_config_name)
|
patroni_config_path = os.path.join(self._output_dir, patroni_config_name)
|
||||||
|
|
||||||
with open('postgres0.yml') as f:
|
with open(patroni_config_name) as f:
|
||||||
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')
|
host = config['postgresql']['listen'].split(':')[0]
|
||||||
# If patroni_raft_controller is suspended two Patroni members is enough to get a quorum,
|
|
||||||
# therefore we don't want Patroni to join as a voting member when testing dcs_failsafe_mode.
|
|
||||||
if raft_port and not self._output_dir.endswith('dcs_failsafe_mode'):
|
|
||||||
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['restapi']['listen'].rsplit(':', 1)[0]
|
|
||||||
config['restapi']['listen'] = config['restapi']['connect_address'] = '{0}:{1}'.format(host, 8008+int(name[-1]))
|
|
||||||
|
|
||||||
host = config['postgresql']['listen'].rsplit(':', 1)[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.replace('\\', '/')
|
config['postgresql']['data_dir'] = self._data_dir
|
||||||
config['postgresql']['basebackup'] = [{'checkpoint': 'fast'}]
|
config['postgresql']['use_unix_socket'] = True
|
||||||
config['postgresql']['callbacks'] = {
|
|
||||||
'on_role_change': '{0} features/callback2.py {1}'.format(self._context.pctl.PYTHON, name)}
|
|
||||||
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',
|
'logging_collector': 'on', 'log_destination': 'csvlog', 'log_directory': self._output_dir,
|
||||||
'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',
|
||||||
'shared_buffers': '1MB', 'unix_socket_directories': tempfile.gettempdir().replace('\\', '/')})
|
'unix_socket_directories': self._data_dir})
|
||||||
config['postgresql']['pg_hba'] = [
|
|
||||||
'local all all trust',
|
|
||||||
'local replication all trust',
|
|
||||||
'host replication replicator all md5',
|
|
||||||
'host all all all md5'
|
|
||||||
]
|
|
||||||
|
|
||||||
if self._context.postgres_supports_ssl and self._context.certfile:
|
|
||||||
config['postgresql']['parameters'].update({
|
|
||||||
'ssl': 'on',
|
|
||||||
'ssl_ca_file': self._context.certfile.replace('\\', '/'),
|
|
||||||
'ssl_cert_file': self._context.certfile.replace('\\', '/'),
|
|
||||||
'ssl_key_file': self._context.keyfile.replace('\\', '/')
|
|
||||||
})
|
|
||||||
for user in config['postgresql'].get('authentication').keys():
|
|
||||||
config['postgresql'].get('authentication', {}).get(user, {}).update({
|
|
||||||
'sslmode': 'verify-ca',
|
|
||||||
'sslrootcert': self._context.certfile,
|
|
||||||
'sslcert': self._context.certfile,
|
|
||||||
'sslkey': self._context.keyfile
|
|
||||||
})
|
|
||||||
for i, line in enumerate(list(config['postgresql']['pg_hba'])):
|
|
||||||
if line.endswith('md5'):
|
|
||||||
# we want to verify client cert first and than password
|
|
||||||
config['postgresql']['pg_hba'][i] = 'hostssl' + line[4:] + ' clientcert=verify-ca'
|
|
||||||
|
|
||||||
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"'
|
||||||
@@ -243,44 +190,25 @@ 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,
|
|
||||||
'archive_mode': 'on',
|
|
||||||
'archive_command': (PatroniPoolController.ARCHIVE_RESTORE_SCRIPT +
|
|
||||||
' --mode archive ' +
|
|
||||||
'--dirname {} --filename %f --pathname %p').format(
|
|
||||||
os.path.join(self._work_directory, 'data', 'wal_archive'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
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)
|
||||||
|
|
||||||
with open(patroni_config_path, 'w') as f:
|
with open(patroni_config_path, 'w') as f:
|
||||||
yaml.safe_dump(config, f, default_flow_style=False)
|
yaml.safe_dump(config, f, default_flow_style=False)
|
||||||
|
|
||||||
self._connkwargs = config['postgresql'].get('authentication', config['postgresql']).get('superuser', {})
|
user = config['postgresql'].get('authentication', config['postgresql']).get('superuser', {})
|
||||||
self._connkwargs.update({'host': host, 'port': self.__PORT, 'dbname': 'postgres',
|
self._connkwargs = {k: user[n] for n, k in [('username', 'user'), ('password', 'password')] if n in user}
|
||||||
'user': self._connkwargs.pop('username', None)})
|
self._connkwargs.update({'host': host, 'port': self.__PORT, 'database': '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, 'user': self._replication.pop('username', None)})
|
self._replication.update({'host': host, 'port': self.__PORT, 'database': 'postgres'})
|
||||||
self._restapi_url = 'http://{0}'.format(config['restapi']['connect_address'])
|
|
||||||
if self._context.certfile:
|
|
||||||
self._restapi_url = self._restapi_url.replace('http://', 'https://')
|
|
||||||
|
|
||||||
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 = psycopg.connect(**self._connkwargs)
|
self._conn = psycopg2.connect(**self._connkwargs)
|
||||||
|
self._conn.autocommit = True
|
||||||
return self._conn
|
return self._conn
|
||||||
|
|
||||||
def _cursor(self):
|
def _cursor(self):
|
||||||
@@ -293,7 +221,7 @@ class PatroniController(AbstractController):
|
|||||||
cursor = self._cursor()
|
cursor = self._cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
return cursor
|
return cursor
|
||||||
except psycopg.Error:
|
except psycopg2.Error:
|
||||||
if not fail_ok:
|
if not fail_ok:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@@ -321,27 +249,59 @@ 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):
|
||||||
def escape(value):
|
return 'postgres://{username}:{password}@{host}:{port}/{database}'.format(**self._replication)
|
||||||
return re.sub(r'([\'\\ ])', r'\\\1', str(value))
|
|
||||||
|
|
||||||
return ' '.join('{0}={1}'.format(k, escape(v)) for k, v in self._replication.items())
|
def backup(self, dest='data/basebackup'):
|
||||||
|
subprocess.call([PatroniPoolController.BACKUP_SCRIPT, '--walmethod=none',
|
||||||
def backup(self, dest=os.path.join('data', 'basebackup')):
|
'--datadir=' + os.path.join(self._work_directory, dest),
|
||||||
subprocess.call(PatroniPoolController.BACKUP_SCRIPT + ['--walmethod=none',
|
'--dbname=' + self.backup_source])
|
||||||
'--datadir=' + os.path.join(self._work_directory, dest),
|
|
||||||
'--dbname=' + self.backup_source])
|
|
||||||
|
|
||||||
|
|
||||||
class ProcessHang(object):
|
class ProcessHang(object):
|
||||||
@@ -375,7 +335,6 @@ class AbstractDcsController(AbstractController):
|
|||||||
|
|
||||||
def __init__(self, context, mktemp=True):
|
def __init__(self, context, mktemp=True):
|
||||||
work_directory = mktemp and tempfile.mkdtemp() or None
|
work_directory = mktemp and tempfile.mkdtemp() or None
|
||||||
self._paused = False
|
|
||||||
super(AbstractDcsController, self).__init__(context, self.name(), work_directory, context.pctl.output_dir)
|
super(AbstractDcsController, self).__init__(context, self.name(), work_directory, context.pctl.output_dir)
|
||||||
|
|
||||||
def _is_accessible(self):
|
def _is_accessible(self):
|
||||||
@@ -387,22 +346,11 @@ class AbstractDcsController(AbstractController):
|
|||||||
if self._work_directory:
|
if self._work_directory:
|
||||||
shutil.rmtree(self._work_directory)
|
shutil.rmtree(self._work_directory)
|
||||||
|
|
||||||
def path(self, key=None, scope='batman', group=None):
|
def path(self, key=None, scope='batman'):
|
||||||
citus_group = '/{0}'.format(group) if group is not None else ''
|
return self._CLUSTER_NODE.format(scope) + (key and '/' + key or '')
|
||||||
return self._CLUSTER_NODE.format(scope) + citus_group + (key and '/' + key or '')
|
|
||||||
|
|
||||||
def start_outage(self):
|
|
||||||
if not self._paused and self._handle:
|
|
||||||
self._handle.suspend()
|
|
||||||
self._paused = True
|
|
||||||
|
|
||||||
def stop_outage(self):
|
|
||||||
if self._paused and self._handle:
|
|
||||||
self._handle.resume()
|
|
||||||
self._paused = False
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def query(self, key, scope='batman', group=None):
|
def query(self, key, scope='batman'):
|
||||||
""" query for a value of a given key """
|
""" query for a value of a given key """
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
@@ -427,17 +375,15 @@ 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._config_file = None
|
|
||||||
|
|
||||||
import consul
|
|
||||||
self._client = consul.Consul()
|
self._client = consul.Consul()
|
||||||
|
self._config_file = None
|
||||||
|
|
||||||
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:
|
||||||
f.write(b'{"session_ttl_min":"5s","server":true,"bootstrap":true,"advertise_addr":"127.0.0.1"}')
|
f.write(b'{"session_ttl_min":"5s","server":true,"bootstrap":true,"advertise_addr":"127.0.0.1"}')
|
||||||
return psutil.Popen(['consul', 'agent', '-config-file', self._config_file, '-data-dir',
|
return subprocess.Popen(['consul', 'agent', '-config-file', self._config_file, '-data-dir',
|
||||||
self._work_directory], stdout=self._log, stderr=subprocess.STDOUT)
|
self._work_directory], stdout=self._log, stderr=subprocess.STDOUT)
|
||||||
|
|
||||||
def stop(self, kill=False, timeout=15):
|
def stop(self, kill=False, timeout=15):
|
||||||
super(ConsulController, self).stop(kill=kill, timeout=timeout)
|
super(ConsulController, self).stop(kill=kill, timeout=timeout)
|
||||||
@@ -450,11 +396,11 @@ class ConsulController(AbstractDcsController):
|
|||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def path(self, key=None, scope='batman', group=None):
|
def path(self, key=None, scope='batman'):
|
||||||
return super(ConsulController, self).path(key, scope, group)[1:]
|
return super(ConsulController, self).path(key, scope)[1:]
|
||||||
|
|
||||||
def query(self, key, scope='batman', group=None):
|
def query(self, key, scope='batman'):
|
||||||
_, value = self._client.kv.get(self.path(key, scope, group))
|
_, value = self._client.kv.get(self.path(key, scope))
|
||||||
return value and value['Value'].decode('utf-8')
|
return value and value['Value'].decode('utf-8')
|
||||||
|
|
||||||
def cleanup_service_tree(self):
|
def cleanup_service_tree(self):
|
||||||
@@ -464,45 +410,26 @@ class ConsulController(AbstractDcsController):
|
|||||||
super(ConsulController, self).start(max_wait_limit)
|
super(ConsulController, self).start(max_wait_limit)
|
||||||
|
|
||||||
|
|
||||||
class AbstractEtcdController(AbstractDcsController):
|
class EtcdController(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, client_cls):
|
def __init__(self, context):
|
||||||
super(AbstractEtcdController, self).__init__(context)
|
super(EtcdController, self).__init__(context)
|
||||||
self._client_cls = client_cls
|
os.environ['PATRONI_ETCD_HOST'] = 'localhost:2379'
|
||||||
|
self._client = etcd.Client(port=2379)
|
||||||
|
|
||||||
def _start(self):
|
def _start(self):
|
||||||
return psutil.Popen(["etcd", "--enable-v2=true", "--data-dir", self._work_directory],
|
return subprocess.Popen(["etcd", "--debug", "--data-dir", self._work_directory],
|
||||||
stdout=self._log, stderr=subprocess.STDOUT)
|
stdout=self._log, stderr=subprocess.STDOUT)
|
||||||
|
|
||||||
def _is_running(self):
|
def query(self, key, scope='batman'):
|
||||||
from patroni.dcs.etcd import DnsCachingResolver
|
|
||||||
# if etcd is running, but we didn't start it
|
|
||||||
try:
|
try:
|
||||||
self._client = self._client_cls({'host': 'localhost', 'port': 2379, 'retry_timeout': 30,
|
return self._client.get(self.path(key, scope)).value
|
||||||
'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', group=None):
|
|
||||||
import etcd
|
|
||||||
try:
|
|
||||||
return self._client.get(self.path(key, scope, group)).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):
|
||||||
@@ -510,64 +437,15 @@ class EtcdController(AbstractEtcdController):
|
|||||||
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)
|
||||||
|
|
||||||
|
|
||||||
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', group=None):
|
|
||||||
import base64
|
|
||||||
response = self._client.range(self.path(key, scope, group))
|
|
||||||
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:
|
|
||||||
self._client.deleteprefix(self.path(scope=''))
|
|
||||||
except Exception as e:
|
|
||||||
assert False, "exception when cleaning up etcd contents: {0}".format(e)
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractExternalDcsController(AbstractDcsController):
|
|
||||||
|
|
||||||
def __init__(self, context, mktemp=True):
|
|
||||||
super(AbstractExternalDcsController, self).__init__(context, mktemp)
|
|
||||||
self._wrapper = ['sudo']
|
|
||||||
|
|
||||||
def _start(self):
|
|
||||||
return self._external_pid
|
|
||||||
|
|
||||||
def start_outage(self):
|
|
||||||
if not self._paused:
|
|
||||||
subprocess.call(self._wrapper + ['kill', '-SIGSTOP', self._external_pid])
|
|
||||||
self._paused = True
|
|
||||||
|
|
||||||
def stop_outage(self):
|
|
||||||
if self._paused:
|
|
||||||
subprocess.call(self._wrapper + ['kill', '-SIGCONT', self._external_pid])
|
|
||||||
self._paused = False
|
|
||||||
|
|
||||||
def _has_started(self):
|
|
||||||
return True
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def process_name():
|
|
||||||
"""process name to search with pgrep"""
|
|
||||||
|
|
||||||
def _is_running(self):
|
def _is_running(self):
|
||||||
if not self._handle:
|
# if etcd is running, but we didn't start it
|
||||||
self._external_pid = subprocess.check_output(['pgrep', '-nf', self.process_name()]).decode('utf-8').strip()
|
try:
|
||||||
|
return bool(self._client.machines)
|
||||||
|
except Exception:
|
||||||
return False
|
return False
|
||||||
return True
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class KubernetesController(AbstractExternalDcsController):
|
class KubernetesController(AbstractDcsController):
|
||||||
|
|
||||||
def __init__(self, context):
|
def __init__(self, context):
|
||||||
super(KubernetesController, self).__init__(context)
|
super(KubernetesController, self).__init__(context)
|
||||||
@@ -576,48 +454,18 @@ class KubernetesController(AbstractExternalDcsController):
|
|||||||
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.setdefault('PATRONI_KUBERNETES_BYPASS_API_SERVICE', 'true')
|
|
||||||
|
|
||||||
from patroni.dcs.kubernetes import k8s_client, k8s_config
|
from kubernetes import client as k8s_client, config as k8s_config
|
||||||
k8s_config.load_kube_config(context=os.environ.setdefault('PATRONI_KUBERNETES_CONTEXT', 'kind-kind'))
|
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()
|
||||||
|
|
||||||
def process_name(self):
|
def _start(self):
|
||||||
return "localkube"
|
pass
|
||||||
|
|
||||||
def _is_running(self):
|
def create_pod(self, name, scope):
|
||||||
if not self._handle:
|
|
||||||
context = os.environ.get('PATRONI_KUBERNETES_CONTEXT')
|
|
||||||
if context.startswith('kind-'):
|
|
||||||
container = '{0}-control-plane'.format(context[5:])
|
|
||||||
api_process = 'kube-apiserver'
|
|
||||||
elif context.startswith('k3d-'):
|
|
||||||
container = '{0}-server-0'.format(context)
|
|
||||||
api_process = 'k3s'
|
|
||||||
else:
|
|
||||||
return super(KubernetesController, self)._is_running()
|
|
||||||
try:
|
|
||||||
docker = 'docker'
|
|
||||||
with open(os.devnull, 'w') as null:
|
|
||||||
if subprocess.call([docker, 'info'], stdout=null, stderr=null) != 0:
|
|
||||||
raise Exception
|
|
||||||
except Exception:
|
|
||||||
docker = 'podman'
|
|
||||||
with open(os.devnull, 'w') as null:
|
|
||||||
if subprocess.call([docker, 'info'], stdout=null, stderr=null) != 0:
|
|
||||||
raise Exception
|
|
||||||
self._wrapper = [docker, 'exec', container]
|
|
||||||
self._external_pid = subprocess.check_output(self._wrapper + ['pidof', api_process]).decode('utf-8').strip()
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def create_pod(self, name, scope, group=None):
|
|
||||||
self.delete_pod(name)
|
|
||||||
labels = self._labels.copy()
|
labels = self._labels.copy()
|
||||||
labels['cluster-name'] = scope
|
labels['cluster-name'] = scope
|
||||||
if group is not None:
|
|
||||||
labels['citus-group'] = str(group)
|
|
||||||
metadata = self._client.V1ObjectMeta(namespace=self._namespace, name=name, labels=labels)
|
metadata = self._client.V1ObjectMeta(namespace=self._namespace, name=name, labels=labels)
|
||||||
spec = self._client.V1PodSpec(containers=[self._client.V1Container(name=name, image='empty')])
|
spec = self._client.V1PodSpec(containers=[self._client.V1Container(name=name, image='empty')])
|
||||||
body = self._client.V1Pod(metadata=metadata, spec=spec)
|
body = self._client.V1Pod(metadata=metadata, spec=spec)
|
||||||
@@ -625,40 +473,37 @@ class KubernetesController(AbstractExternalDcsController):
|
|||||||
|
|
||||||
def delete_pod(self, name):
|
def delete_pod(self, name):
|
||||||
try:
|
try:
|
||||||
self._api.delete_namespaced_pod(name, self._namespace, body=self._client.V1DeleteOptions())
|
self._api.delete_namespaced_pod(name, self._namespace, self._client.V1DeleteOptions())
|
||||||
except Exception:
|
except:
|
||||||
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 Exception:
|
except:
|
||||||
break
|
break
|
||||||
|
|
||||||
def query(self, key, scope='batman', group=None):
|
def query(self, key, scope='batman'):
|
||||||
if key.startswith('members/'):
|
if key.startswith('members/'):
|
||||||
pod = self._api.read_namespaced_pod(key[8:], self._namespace)
|
pod = self._api.read_namespaced_pod(key[8:], self._namespace)
|
||||||
return (pod.metadata.annotations or {}).get('status', '')
|
return (pod.metadata.annotations or {}).get('status', '')
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if group is not None:
|
e = self._api.read_namespaced_endpoints(scope + ('' if key == 'leader' else '-' + key), self._namespace)
|
||||||
scope = '{0}-{1}'.format(scope, group)
|
if key == 'leader':
|
||||||
ep = scope + {'leader': '', 'history': '-config', 'initialize': '-config'}.get(key, '-' + key)
|
|
||||||
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 Exception:
|
except:
|
||||||
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 Exception:
|
except:
|
||||||
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 Exception:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@@ -666,8 +511,11 @@ class KubernetesController(AbstractExternalDcsController):
|
|||||||
if len(result.items) < 1:
|
if len(result.items) < 1:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def _is_running(self):
|
||||||
|
return True
|
||||||
|
|
||||||
class ZooKeeperController(AbstractExternalDcsController):
|
|
||||||
|
class ZooKeeperController(AbstractDcsController):
|
||||||
|
|
||||||
""" handles all zookeeper related tasks, used for the tests setup and cleanup """
|
""" handles all zookeeper related tasks, used for the tests setup and cleanup """
|
||||||
|
|
||||||
@@ -675,22 +523,18 @@ class ZooKeeperController(AbstractExternalDcsController):
|
|||||||
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 process_name(self):
|
def _start(self):
|
||||||
return "zookeeper"
|
pass # TODO: implement later
|
||||||
|
|
||||||
def query(self, key, scope='batman', group=None):
|
def query(self, key, scope='batman'):
|
||||||
import kazoo.exceptions
|
|
||||||
try:
|
try:
|
||||||
return self._client.get(self.path(key, scope, group))[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):
|
||||||
@@ -699,9 +543,6 @@ class ZooKeeperController(AbstractExternalDcsController):
|
|||||||
assert False, "exception when cleaning up zookeeper contents: {0}".format(e)
|
assert False, "exception when cleaning up zookeeper contents: {0}".format(e)
|
||||||
|
|
||||||
def _is_running(self):
|
def _is_running(self):
|
||||||
if not super(ZooKeeperController, self)._is_running():
|
|
||||||
return False
|
|
||||||
|
|
||||||
# if zookeeper is running, but we didn't start it
|
# if zookeeper is running, but we didn't start it
|
||||||
if self._client.connected:
|
if self._client.connected:
|
||||||
return True
|
return True
|
||||||
@@ -711,79 +552,16 @@ class ZooKeeperController(AbstractExternalDcsController):
|
|||||||
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)
|
||||||
port = 8181
|
os.environ.update({'PATRONI_EXHIBITOR_HOSTS': 'localhost', 'PATRONI_EXHIBITOR_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 psutil.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', group=None):
|
|
||||||
ret = self._raft.get(self.path(key, scope, group))
|
|
||||||
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):
|
||||||
|
|
||||||
PYTHON = sys.executable.replace('\\', '/')
|
BACKUP_SCRIPT = 'features/backup_create.sh'
|
||||||
BACKUP_SCRIPT = [PYTHON, 'features/backup_create.py']
|
|
||||||
BACKUP_RESTORE_SCRIPT = ' '.join((PYTHON, os.path.abspath('features/backup_restore.py'))).replace('\\', '/')
|
|
||||||
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
|
||||||
@@ -792,17 +570,8 @@ class PatroniPoolController(object):
|
|||||||
self._patroni_path = None
|
self._patroni_path = None
|
||||||
self._processes = {}
|
self._processes = {}
|
||||||
self.create_and_set_output_directory('')
|
self.create_and_set_output_directory('')
|
||||||
self._check_postgres_ssl()
|
|
||||||
self.known_dcs = {subclass.name(): subclass for subclass in AbstractDcsController.get_subclasses()}
|
self.known_dcs = {subclass.name(): subclass for subclass in AbstractDcsController.get_subclasses()}
|
||||||
|
|
||||||
def _check_postgres_ssl(self):
|
|
||||||
try:
|
|
||||||
subprocess.check_output(['postgres', '-D', os.devnull, '-c', 'ssl=on'], stderr=subprocess.STDOUT)
|
|
||||||
raise Exception # this one should never happen because the previous line will always raise and exception
|
|
||||||
except Exception as e:
|
|
||||||
self._context.postgres_supports_ssl = isinstance(e, subprocess.CalledProcessError)\
|
|
||||||
and 'SSL is not supported by this build' not in e.output.decode()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def patroni_path(self):
|
def patroni_path(self):
|
||||||
if self._patroni_path is None:
|
if self._patroni_path is None:
|
||||||
@@ -818,15 +587,16 @@ 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=40, custom_config=None):
|
def start(self, name, max_wait_limit=20, 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',
|
if func not in ['stop', 'query', 'write_label', 'read_label', 'check_role_has_changed_to', 'add_tag_to_config',
|
||||||
'add_tag_to_config', 'get_watchdog', 'patroni_hang', 'backup']:
|
'get_watchdog', 'database_is_running', 'checkpoint_hang', 'patroni_hang',
|
||||||
|
'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):
|
||||||
@@ -840,7 +610,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)
|
||||||
@@ -853,8 +623,7 @@ class PatroniPoolController(object):
|
|||||||
'bootstrap': {
|
'bootstrap': {
|
||||||
'method': 'pg_basebackup',
|
'method': 'pg_basebackup',
|
||||||
'pg_basebackup': {
|
'pg_basebackup': {
|
||||||
'command': " ".join(self.BACKUP_SCRIPT +
|
'command': self.BACKUP_SCRIPT + ' --walmethod=stream --dbname=' + f.backup_source
|
||||||
['--walmethod=stream', '--dbname="{0}"'.format(f.backup_source)])
|
|
||||||
},
|
},
|
||||||
'dcs': {
|
'dcs': {
|
||||||
'postgresql': {
|
'postgresql': {
|
||||||
@@ -867,9 +636,8 @@ class PatroniPoolController(object):
|
|||||||
'postgresql': {
|
'postgresql': {
|
||||||
'parameters': {
|
'parameters': {
|
||||||
'archive_mode': 'on',
|
'archive_mode': 'on',
|
||||||
'archive_command': (self.ARCHIVE_RESTORE_SCRIPT + ' --mode archive ' +
|
'archive_command': 'mkdir -p {0} && test ! -f {0}/%f && cp %p {0}/%f'.format(
|
||||||
'--dirname {} --filename %f --pathname %p').format(
|
os.path.join(self.patroni_path, 'data/wal_archive'))
|
||||||
os.path.join(self.patroni_path, 'data', 'wal_archive_clone').replace('\\', '/'))
|
|
||||||
},
|
},
|
||||||
'authentication': {
|
'authentication': {
|
||||||
'superuser': {'password': 'zalando1'},
|
'superuser': {'password': 'zalando1'},
|
||||||
@@ -885,14 +653,12 @@ class PatroniPoolController(object):
|
|||||||
'bootstrap': {
|
'bootstrap': {
|
||||||
'method': 'backup_restore',
|
'method': 'backup_restore',
|
||||||
'backup_restore': {
|
'backup_restore': {
|
||||||
'command': (self.BACKUP_RESTORE_SCRIPT + ' --sourcedir=' +
|
'command': 'features/backup_restore.sh --sourcedir=' + os.path.join(self.patroni_path,
|
||||||
os.path.join(self.patroni_path, 'data', 'basebackup').replace('\\', '/')),
|
'data/basebackup'),
|
||||||
'recovery_conf': {
|
'recovery_conf': {
|
||||||
'recovery_target_action': 'promote',
|
'recovery_target_action': 'promote',
|
||||||
'recovery_target_timeline': 'latest',
|
'recovery_target_timeline': 'latest',
|
||||||
'restore_command': (self.ARCHIVE_RESTORE_SCRIPT + ' --mode restore ' +
|
'restore_command': 'cp {0}/data/wal_archive/%f %p'.format(self.patroni_path)
|
||||||
'--dirname {} --filename %f --pathname %p').format(
|
|
||||||
os.path.join(self.patroni_path, 'data', 'wal_archive_clone').replace('\\', '/'))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -905,25 +671,6 @@ class PatroniPoolController(object):
|
|||||||
}
|
}
|
||||||
self.start(name, custom_config=custom_config)
|
self.start(name, custom_config=custom_config)
|
||||||
|
|
||||||
def bootstrap_from_backup_no_leader(self, name, cluster_name):
|
|
||||||
custom_config = {
|
|
||||||
'scope': cluster_name,
|
|
||||||
'postgresql': {
|
|
||||||
'recovery_conf': {
|
|
||||||
'restore_command': (self.ARCHIVE_RESTORE_SCRIPT + ' --mode restore ' +
|
|
||||||
'--dirname {} --filename %f --pathname %p').format(
|
|
||||||
os.path.join(self.patroni_path, 'data', 'wal_archive').replace('\\', '/'))
|
|
||||||
},
|
|
||||||
'create_replica_methods': ['no_leader_bootstrap'],
|
|
||||||
'no_leader_bootstrap': {
|
|
||||||
'command': (self.BACKUP_RESTORE_SCRIPT + ' --sourcedir=' +
|
|
||||||
os.path.join(self.patroni_path, 'data', 'basebackup').replace('\\', '/')),
|
|
||||||
'no_leader': '1'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.start(name, custom_config=custom_config)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dcs(self):
|
def dcs(self):
|
||||||
if self._dcs is None:
|
if self._dcs is None:
|
||||||
@@ -1048,34 +795,12 @@ class WatchdogMonitor(object):
|
|||||||
return triggered
|
return triggered
|
||||||
|
|
||||||
|
|
||||||
# actions to execute on start/stop of the tests and before running individual features
|
# actions to execute on start/stop of the tests and before running invidual features
|
||||||
def before_all(context):
|
def before_all(context):
|
||||||
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.keyfile = os.path.join(context.pctl.output_dir, 'patroni.key')
|
|
||||||
context.certfile = os.path.join(context.pctl.output_dir, 'patroni.crt')
|
|
||||||
try:
|
|
||||||
with open(os.devnull, 'w') as null:
|
|
||||||
ret = subprocess.call(['openssl', 'req', '-nodes', '-new', '-x509', '-subj', '/CN=batman.patroni',
|
|
||||||
'-keyout', context.keyfile, '-out', context.certfile], stdout=null, stderr=null)
|
|
||||||
if ret != 0:
|
|
||||||
raise Exception
|
|
||||||
except Exception:
|
|
||||||
context.keyfile = context.certfile = None
|
|
||||||
|
|
||||||
os.environ.update({'PATRONI_RESTAPI_USERNAME': 'username', 'PATRONI_RESTAPI_PASSWORD': 'password'})
|
os.environ.update({'PATRONI_RESTAPI_USERNAME': 'username', 'PATRONI_RESTAPI_PASSWORD': 'password'})
|
||||||
ctl = {'auth': os.environ['PATRONI_RESTAPI_USERNAME'] + ':' + os.environ['PATRONI_RESTAPI_PASSWORD']}
|
context.ci = 'TRAVIS_BUILD_NUMBER' in os.environ or 'BUILD_NUMBER' in os.environ
|
||||||
if context.certfile:
|
context.timeout_multiplier = 2 if context.ci else 1
|
||||||
os.environ.update({'PATRONI_RESTAPI_CAFILE': context.certfile,
|
context.pctl = PatroniPoolController(context)
|
||||||
'PATRONI_RESTAPI_CERTFILE': context.certfile,
|
|
||||||
'PATRONI_RESTAPI_KEYFILE': context.keyfile,
|
|
||||||
'PATRONI_RESTAPI_VERIFY_CLIENT': 'required',
|
|
||||||
'PATRONI_CTL_INSECURE': 'on'})
|
|
||||||
ctl.update({'cacert': context.certfile, 'certfile': context.certfile, 'keyfile': context.keyfile})
|
|
||||||
context.request_executor = PatroniRequest({'ctl': ctl}, True)
|
|
||||||
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()
|
||||||
try:
|
try:
|
||||||
@@ -1093,45 +818,13 @@ 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':
|
|
||||||
return feature.skip("Watchdog isn't supported on Windows")
|
|
||||||
elif feature.name == 'citus':
|
|
||||||
lib = subprocess.check_output(['pg_config', '--pkglibdir']).decode('utf-8').strip()
|
|
||||||
if not os.path.exists(os.path.join(lib, 'citus.so')):
|
|
||||||
return feature.skip("Citus extenstion isn't available")
|
|
||||||
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):
|
||||||
""" send SIGCONT to a dcs if neccessary,
|
""" 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.dcs_ctl.stop_outage()
|
|
||||||
context.pctl.stop_all()
|
context.pctl.stop_all()
|
||||||
data = os.path.join(context.pctl.patroni_path, 'data')
|
shutil.rmtree(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':
|
||||||
found = False
|
|
||||||
logs = glob.glob(context.pctl.output_dir + '/patroni_*.log')
|
|
||||||
for log in logs:
|
|
||||||
with open(log) as f:
|
|
||||||
for line in f:
|
|
||||||
if 'please report it as a BUG' in line:
|
|
||||||
print(':'.join([log, line.rstrip()]))
|
|
||||||
found = True
|
|
||||||
|
|
||||||
if feature.status == 'failed' or found:
|
|
||||||
shutil.copytree(context.pctl.output_dir, context.pctl.output_dir + '_failed')
|
shutil.copytree(context.pctl.output_dir, context.pctl.output_dir + '_failed')
|
||||||
if found:
|
|
||||||
raise Exception('Unexpected errors in Patroni log files')
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
if 'dcs-failsafe' in scenario.effective_tags and not context.dcs_ctl._handle:
|
|
||||||
scenario.skip('it is not possible to control state of {0} from tests'.format(context.dcs_ctl.name()))
|
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
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 primary) 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,15 +8,13 @@ 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
|
||||||
Then I receive a response code 503
|
Then I receive a response code 503
|
||||||
When I issue a POST request to http://127.0.0.1:8008/reinitialize with {"force": true}
|
When I run patronictl.py reinit batman postgres0 --force
|
||||||
Then I receive a response code 503
|
Then I receive a response returncode 0
|
||||||
And I receive a response text I am the leader, can not reinitialize
|
And I receive a response output "Failed: reinitialize for member postgres0, status code=503, (I am the leader, can not reinitialize)"
|
||||||
When I run patronictl.py switchover batman --master postgres0 --force
|
When I run patronictl.py switchover batman --master postgres0 --force
|
||||||
Then I receive a response returncode 1
|
Then I receive a response returncode 1
|
||||||
And I receive a response output "Error: No candidates found to switchover to"
|
And I receive a response output "Error: No candidates found to switchover to"
|
||||||
@@ -30,48 +28,30 @@ 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 add tag new_tag new_value to postgres0 config
|
Given I issue an empty POST request to http://127.0.0.1:8008/reload
|
||||||
|
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' -p 'max_connections=101' --force batman
|
Given I run patronictl.py edit-config -s 'ttl=10' -s 'loop_wait=2' -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 "+ttl: 10"
|
And I receive a response output "+loop_wait: 2"
|
||||||
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 ttl 10
|
And I receive a response loop_wait 2
|
||||||
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 start postgres1
|
Given I run patronictl.py pause batman
|
||||||
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 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
|
||||||
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
|
||||||
@@ -93,34 +73,45 @@ 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/primary
|
|
||||||
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
|
||||||
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:8009/primary
|
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 200
|
||||||
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 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 10 seconds
|
Given I issue a scheduled switchover from postgres1 to postgres0 in 3 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 10 seconds
|
Given I issue a scheduled switchover from postgres1 to postgres0 in 3 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/primary
|
|
||||||
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
|
||||||
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/primary
|
When I issue a GET request to http://127.0.0.1:8009/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: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,58 +1,46 @@
|
|||||||
Feature: standby cluster
|
Feature: standby cluster
|
||||||
Scenario: prepare the cluster with logical slots
|
Scenario: check permanent logical slots are preserved on failover/switchover
|
||||||
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 there is a non empty initialize key in DCS after 15 seconds
|
And I sleep for 3 seconds
|
||||||
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"}}}
|
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"}}}
|
||||||
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
|
When I start postgres0 with callback configured
|
||||||
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/primary
|
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_role_change standby_leader batman1" in postgres1 data directory
|
And there is a postgres1_cb.log with "on_start replica batman1\non_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/primary
|
When I issue a GET request to http://127.0.0.1:8010/master
|
||||||
Then I receive a response code 503
|
Then I receive a response code 200
|
||||||
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 patroni.psycopg as pg
|
import psycopg2 as pg
|
||||||
|
|
||||||
from behave import step, then
|
from behave import step, then
|
||||||
from time import sleep, time
|
from time import sleep, time
|
||||||
@@ -28,47 +28,16 @@ def stop_postgres(context, name):
|
|||||||
def add_table(context, table_name, pg_name):
|
def add_table(context, table_name, pg_name):
|
||||||
# parse the configuration file and get the port
|
# parse the configuration file and get the port
|
||||||
try:
|
try:
|
||||||
context.pctl.query(pg_name, "CREATE TABLE public.{0}()".format(table_name))
|
context.pctl.query(pg_name, "CREATE TABLE {0}()".format(table_name))
|
||||||
except pg.Error as e:
|
except pg.Error as e:
|
||||||
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, "SHOW server_version_num").fetchone()[0]
|
|
||||||
wal_name = 'xlog' if int(version)/10000 < 10 else 'wal'
|
|
||||||
context.pctl.query(pg_name, "SELECT pg_{0}_replay_{1}()".format(wal_name, 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 public.mytest(id numeric)")
|
|
||||||
else:
|
|
||||||
context.pctl.query(pg_name, "drop table if exists public.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, "insert into public.mytest select r::numeric from generate_series(1, 350000) r")
|
|
||||||
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
|
||||||
for _ in range(int(max_replication_delay)):
|
for _ in range(int(max_replication_delay)):
|
||||||
if context.pctl.query(pg_name, "SELECT 1 FROM public.{0}".format(table_name), fail_ok=True) is not None:
|
if context.pctl.query(pg_name, "SELECT 1 FROM {0}".format(table_name), fail_ok=True) is not None:
|
||||||
break
|
break
|
||||||
sleep(1)
|
sleep(1)
|
||||||
else:
|
else:
|
||||||
@@ -83,10 +52,10 @@ def check_role(context, pg_name, pg_role, max_promotion_timeout):
|
|||||||
"{0} role didn't change to {1} after {2} seconds".format(pg_name, pg_role, max_promotion_timeout)
|
"{0} role didn't change to {1} after {2} seconds".format(pg_name, pg_role, max_promotion_timeout)
|
||||||
|
|
||||||
|
|
||||||
@step('replication works from {primary:w} to {replica:w} after {time_limit:d} seconds')
|
@step('replication works from {master:w} to {replica:w} after {time_limit:d} seconds')
|
||||||
@then('replication works from {primary:w} to {replica:w} after {time_limit:d} seconds')
|
@then('replication works from {master:w} to {replica:w} after {time_limit:d} seconds')
|
||||||
def replication_works(context, primary, replica, time_limit):
|
def replication_works(context, master, replica, time_limit):
|
||||||
context.execute_steps(u"""
|
context.execute_steps(u"""
|
||||||
When I add the table test_{0} to {1}
|
When I add the table test_{0} to {1}
|
||||||
Then table test_{0} is present on {2} after {3} seconds
|
Then table test_{0} is present on {2} after {3} seconds
|
||||||
""".format(int(time()), primary, replica, time_limit))
|
""".format(int(time()), master, replica, time_limit))
|
||||||
|
|||||||
@@ -11,8 +11,9 @@ 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):
|
||||||
value = (context.pctl.read_label(name, label) or '').replace('\n', '\\n')
|
label = context.pctl.read_label(name, label)
|
||||||
assert content in value, "\"{0}\" in {1} doesn't contain {2}".format(value, label, content)
|
label = label.replace('\n', '\\n')
|
||||||
|
assert label == content, "{0} is not equal to {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')
|
||||||
@@ -20,33 +21,16 @@ 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} after {time_limit:d} seconds')
|
@step('"{name}" key in DCS has {key:w}={value:w} 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))
|
||||||
dcs_value = response.get(key)
|
if response.get(key) == value:
|
||||||
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} (found {3}) in dcs after {4} seconds".format(name, key, value,
|
assert False, "{0} does not have {1}={2} in dcs after {3} seconds".format(name, key, value, time_limit)
|
||||||
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,117 +0,0 @@
|
|||||||
import json
|
|
||||||
import time
|
|
||||||
|
|
||||||
from behave import step, then
|
|
||||||
from dateutil import tz
|
|
||||||
from datetime import datetime
|
|
||||||
from functools import partial
|
|
||||||
from threading import Thread, Event
|
|
||||||
|
|
||||||
tzutc = tz.tzutc()
|
|
||||||
|
|
||||||
|
|
||||||
@step('{name:w} is a leader in a group {group:d} after {time_limit:d} seconds')
|
|
||||||
@then('{name:w} is a leader in a group {group:d} after {time_limit:d} seconds')
|
|
||||||
def is_a_group_leader(context, name, group, time_limit):
|
|
||||||
time_limit *= context.timeout_multiplier
|
|
||||||
max_time = time.time() + int(time_limit)
|
|
||||||
while (context.dcs_ctl.query("leader", group=group) != name):
|
|
||||||
time.sleep(1)
|
|
||||||
assert time.time() < max_time, "{0} is not a leader in dcs after {1} seconds".format(name, time_limit)
|
|
||||||
|
|
||||||
|
|
||||||
@step('"{name}" key in a group {group:d} in DCS has {key:w}={value} after {time_limit:d} seconds')
|
|
||||||
def check_group_member(context, name, group, key, value, time_limit):
|
|
||||||
time_limit *= context.timeout_multiplier
|
|
||||||
max_time = time.time() + int(time_limit)
|
|
||||||
dcs_value = None
|
|
||||||
response = None
|
|
||||||
while time.time() < max_time:
|
|
||||||
try:
|
|
||||||
response = json.loads(context.dcs_ctl.query(name, group=group))
|
|
||||||
dcs_value = response.get(key)
|
|
||||||
if dcs_value == value:
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
time.sleep(1)
|
|
||||||
assert False, ("{0} in a group {1} does not have {2}={3} (found {4}) in dcs" +
|
|
||||||
" after {5} seconds").format(name, group, key, value, response, time_limit)
|
|
||||||
|
|
||||||
|
|
||||||
@step('I start {name:w} in citus group {group:d}')
|
|
||||||
def start_citus(context, name, group):
|
|
||||||
return context.pctl.start(name, custom_config={"citus": {"database": "postgres", "group": int(group)}})
|
|
||||||
|
|
||||||
|
|
||||||
@step('{name1:w} is registered in the {name2:w} as the worker in group {group:d}')
|
|
||||||
def check_registration(context, name1, name2, group):
|
|
||||||
worker_port = int(context.pctl.query(name1, "SHOW port").fetchone()[0])
|
|
||||||
r = context.pctl.query(name2, "SELECT nodeport FROM pg_catalog.pg_dist_node WHERE groupid = {0}".format(group))
|
|
||||||
assert worker_port == r.fetchone()[0],\
|
|
||||||
"Worker {0} is not registered in pg_dist_node on the coordinator {1}".format(name1, name2)
|
|
||||||
|
|
||||||
|
|
||||||
@step('I create a distributed table on {name:w}')
|
|
||||||
def create_distributed_table(context, name):
|
|
||||||
context.pctl.query(name, 'CREATE TABLE public.d(id int not null)')
|
|
||||||
context.pctl.query(name, "SELECT create_distributed_table('public.d', 'id')")
|
|
||||||
|
|
||||||
|
|
||||||
@step('I cleanup a distributed table on {name:w}')
|
|
||||||
def cleanup_distributed_table(context, name):
|
|
||||||
context.pctl.query(name, 'TRUNCATE public.d')
|
|
||||||
|
|
||||||
|
|
||||||
def insert_thread(query_func, context):
|
|
||||||
while True:
|
|
||||||
if context.thread_stop_event.is_set():
|
|
||||||
break
|
|
||||||
|
|
||||||
context.insert_counter += 1
|
|
||||||
query_func('INSERT INTO public.d VALUES({0})'.format(context.insert_counter))
|
|
||||||
|
|
||||||
context.thread_stop_event.wait(0.01)
|
|
||||||
|
|
||||||
|
|
||||||
@step('I start a thread inserting data on {name:w}')
|
|
||||||
def start_insert_thread(context, name):
|
|
||||||
context.thread_stop_event = Event()
|
|
||||||
context.insert_counter = 0
|
|
||||||
query_func = partial(context.pctl.query, name)
|
|
||||||
thread_func = partial(insert_thread, query_func, context)
|
|
||||||
context.thread = Thread(target=thread_func)
|
|
||||||
context.thread.daemon = True
|
|
||||||
context.thread.start()
|
|
||||||
|
|
||||||
|
|
||||||
@then('a thread is still alive')
|
|
||||||
def thread_is_alive(context):
|
|
||||||
assert context.thread.is_alive(), "Thread is not alive"
|
|
||||||
|
|
||||||
|
|
||||||
@step("I stop a thread")
|
|
||||||
def stop_insert_thread(context):
|
|
||||||
context.thread_stop_event.set()
|
|
||||||
context.thread.join(1*context.timeout_multiplier)
|
|
||||||
assert not context.thread.is_alive(), "Thread is still alive"
|
|
||||||
|
|
||||||
|
|
||||||
@step("a distributed table on {name:w} has expected rows")
|
|
||||||
def count_rows(context, name):
|
|
||||||
rows = context.pctl.query(name, "SELECT COUNT(*) FROM public.d").fetchone()[0]
|
|
||||||
assert rows == context.insert_counter, "Distributed table doesn't have expected amount of rows"
|
|
||||||
|
|
||||||
|
|
||||||
@step("There is a transaction in progress on {name:w} changing pg_dist_node")
|
|
||||||
def check_transaction(context, name):
|
|
||||||
cur = context.pctl.query(name, "SELECT xact_start FROM pg_stat_activity WHERE pid <> pg_backend_pid()"
|
|
||||||
" AND state = 'idle in transaction' AND query ~ 'citus_update_node'")
|
|
||||||
assert cur.rowcount == 1, "There is no idle in transaction updating pg_dist_node"
|
|
||||||
context.xact_start = cur.fetchone()[0]
|
|
||||||
|
|
||||||
|
|
||||||
@step("a transaction finishes in {timeout:d} seconds")
|
|
||||||
def check_transaction_timeout(context, timeout):
|
|
||||||
assert (datetime.now(tzutc) - context.xact_start).seconds > timeout,\
|
|
||||||
"a transaction finished earlier than in {0} seconds".format(timeout)
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
from behave import step
|
|
||||||
|
|
||||||
|
|
||||||
@step('DCS is down')
|
|
||||||
def start_dcs_outage(context):
|
|
||||||
context.dcs_ctl.start_outage()
|
|
||||||
|
|
||||||
|
|
||||||
@step('DCS is up')
|
|
||||||
def stop_dcs_outage(context):
|
|
||||||
context.dcs_ctl.stop_outage()
|
|
||||||
|
|
||||||
|
|
||||||
@step('I start {name:w} in a cluster {cluster_name:w} from backup with no_leader')
|
|
||||||
def start_cluster_from_backup_no_leader(context, name, cluster_name):
|
|
||||||
context.pctl.bootstrap_from_backup_no_leader(name, cluster_name)
|
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import parse
|
import parse
|
||||||
|
import requests
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
@@ -42,9 +45,9 @@ def sleep_for_n_seconds(context, value):
|
|||||||
|
|
||||||
|
|
||||||
def _set_response(context, response):
|
def _set_response(context, response):
|
||||||
context.status_code = response.status
|
context.status_code = response.status_code
|
||||||
data = response.data.decode('utf-8')
|
data = response.content.decode('utf-8')
|
||||||
ct = response.getheader('content-type', '')
|
ct = response.headers.get('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\
|
||||||
@@ -60,7 +63,13 @@ 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):
|
||||||
do_request(context, 'GET', url, None)
|
try:
|
||||||
|
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}')
|
||||||
@@ -70,15 +79,17 @@ 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):
|
||||||
if context.certfile:
|
data = data and json.loads(data) or {}
|
||||||
url = url.replace('http://', 'https://')
|
headers = {'Authorization': 'Basic ' + base64.b64encode('username:password'.encode('utf-8')).decode('utf-8'),
|
||||||
data = data and json.loads(data)
|
'Content-Type': 'application/json'}
|
||||||
try:
|
try:
|
||||||
r = context.request_executor.request(request_method, url, data)
|
if request_method == 'PATCH':
|
||||||
if request_method == 'PATCH' and r.status == 409:
|
r = requests.patch(url, headers=headers, json=data)
|
||||||
r = context.request_executor.request(request_method, url, data)
|
else:
|
||||||
except Exception:
|
r = requests.post(url, headers=headers, json=data)
|
||||||
context.status_code = context.response = None
|
except requests.exceptions.RequestException:
|
||||||
|
context.status_code = None
|
||||||
|
context.response = None
|
||||||
else:
|
else:
|
||||||
_set_response(context, r)
|
_set_response(context, r)
|
||||||
|
|
||||||
@@ -87,7 +98,10 @@ def do_request(context, request_method, url, data):
|
|||||||
def do_run(context, cmd):
|
def do_run(context, cmd):
|
||||||
cmd = [sys.executable, '-m', 'coverage', 'run', '--source=patroni', '-p'] + shlex.split(cmd)
|
cmd = [sys.executable, '-m', 'coverage', 'run', '--source=patroni', '-p'] + shlex.split(cmd)
|
||||||
try:
|
try:
|
||||||
response = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
|
# XXX: Dirty hack! We need to take name/passwd from the config!
|
||||||
|
env = os.environ.copy()
|
||||||
|
env.update({'PATRONI_RESTAPI_USERNAME': 'username', 'PATRONI_RESTAPI_PASSWORD': 'password'})
|
||||||
|
response = subprocess.check_output(cmd, stderr=subprocess.STDOUT, env=env)
|
||||||
context.status_code = 0
|
context.status_code = 0
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
response = e.output
|
response = e.output
|
||||||
@@ -109,8 +123,6 @@ def check_response(context, component, data):
|
|||||||
assert data.strip('"') in context.response, "response {0} does not contain {1}".format(context.response, data)
|
assert data.strip('"') in context.response, "response {0} does not contain {1}".format(context.response, data)
|
||||||
else:
|
else:
|
||||||
assert component in context.response, "{0} is not part of the response".format(component)
|
assert component in context.response, "{0} is not part of the response".format(component)
|
||||||
if context.certfile:
|
|
||||||
data = data.replace('http://', 'https://')
|
|
||||||
assert str(context.response[component]) == str(data), "{0} does not contain {1}".format(component, data)
|
assert str(context.response[component]) == str(data), "{0} does not contain {1}".format(component, data)
|
||||||
|
|
||||||
|
|
||||||
@@ -133,28 +145,12 @@ def add_tag_to_config(context, tag, value, pg_name):
|
|||||||
context.pctl.add_tag_to_config(pg_name, tag, value)
|
context.pctl.add_tag_to_config(pg_name, tag, value)
|
||||||
|
|
||||||
|
|
||||||
@then('Status code on GET {url:url} is {code:d} after {timeout:d} seconds')
|
@then('Response on GET {url} contains {value} after {timeout:d} seconds')
|
||||||
def check_http_code(context, url, code, timeout):
|
|
||||||
if context.certfile:
|
|
||||||
url = url.replace('http://', 'https://')
|
|
||||||
timeout *= context.timeout_multiplier
|
|
||||||
for _ in range(int(timeout)):
|
|
||||||
r = context.request_executor.request('GET', url)
|
|
||||||
if int(code) == int(r.status):
|
|
||||||
break
|
|
||||||
time.sleep(1)
|
|
||||||
else:
|
|
||||||
assert False, "HTTP Status Code is not {0} after {1} seconds".format(code, timeout)
|
|
||||||
|
|
||||||
|
|
||||||
@then('Response on GET {url:url} contains {value} after {timeout:d} seconds')
|
|
||||||
def check_http_response(context, url, value, timeout, negate=False):
|
def check_http_response(context, url, value, timeout, negate=False):
|
||||||
if context.certfile:
|
|
||||||
url = url.replace('http://', 'https://')
|
|
||||||
timeout *= context.timeout_multiplier
|
timeout *= context.timeout_multiplier
|
||||||
for _ in range(int(timeout)):
|
for _ in range(int(timeout)):
|
||||||
r = context.request_executor.request('GET', url)
|
r = requests.get(url)
|
||||||
if (value in r.data.decode('utf-8')) != negate:
|
if (value in r.content.decode('utf-8')) != negate:
|
||||||
break
|
break
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
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,9 +4,23 @@ import time
|
|||||||
from behave import step
|
from behave import step
|
||||||
|
|
||||||
|
|
||||||
def callbacks(context, name):
|
select_replication_query = """
|
||||||
return {c: '{0} features/callback2.py {1}'.format(context.pctl.PYTHON, name)
|
SELECT * FROM pg_catalog.pg_stat_replication
|
||||||
for c in ('on_start', 'on_stop', 'on_restart', 'on_role_change')}
|
WHERE application_name = '{0}'
|
||||||
|
"""
|
||||||
|
|
||||||
|
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}')
|
||||||
@@ -14,10 +28,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": callbacks(context, name),
|
"callbacks": {c: callback + name for c in ('on_start', 'on_stop', 'on_restart', 'on_role_change')},
|
||||||
"backup_restore": {
|
"backup_restore": {
|
||||||
"command": (context.pctl.PYTHON + " features/backup_restore.py --sourcedir=" +
|
"command": "features/backup_restore.sh --sourcedir=" + os.path.join(context.pctl.patroni_path,
|
||||||
os.path.join(context.pctl.patroni_path, 'data', 'basebackup').replace('\\', '/'))}
|
"data/basebackup")}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -39,12 +53,11 @@ 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": callbacks(context, name)
|
"callbacks": {c: callback + name for c in ('on_start', 'on_stop', 'on_restart', 'on_role_change')}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return context.pctl.start(name)
|
return context.pctl.start(name)
|
||||||
@@ -52,12 +65,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 * context.timeout_multiplier
|
bound_time = time.time() + timeout
|
||||||
|
|
||||||
while time.time() < bound_time:
|
while time.time() < bound_time:
|
||||||
cur = context.pctl.query(
|
cur = context.pctl.query(
|
||||||
pg_name2,
|
pg_name2,
|
||||||
"SELECT * FROM pg_catalog.pg_stat_replication WHERE application_name = '{0}'".format(pg_name1),
|
select_replication_query.format(pg_name1),
|
||||||
fail_ok=True
|
fail_ok=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def polling_loop(timeout, interval=1):
|
|||||||
|
|
||||||
@step('I start {name:w} with watchdog')
|
@step('I start {name:w} with watchdog')
|
||||||
def start_patroni_with_watchdog(context, name):
|
def start_patroni_with_watchdog(context, name):
|
||||||
return context.pctl.start(name, custom_config={'watchdog': True, 'bootstrap': {'dcs': {'ttl': 20}}})
|
return context.pctl.start(name, custom_config={'watchdog': True})
|
||||||
|
|
||||||
|
|
||||||
@step('{name:w} watchdog has been pinged after {timeout:d} seconds')
|
@step('{name:w} watchdog has been pinged after {timeout:d} seconds')
|
||||||
@@ -31,11 +31,6 @@ def watchdog_was_closed(context, name):
|
|||||||
assert context.pctl.get_watchdog(name).was_closed
|
assert context.pctl.get_watchdog(name).was_closed
|
||||||
|
|
||||||
|
|
||||||
@step('{name:w} watchdog has a {timeout:d} second timeout')
|
|
||||||
def watchdog_has_timeout(context, name, timeout):
|
|
||||||
assert context.pctl.get_watchdog(name).timeout == timeout
|
|
||||||
|
|
||||||
|
|
||||||
@step('I reset {name:w} watchdog state')
|
@step('I reset {name:w} watchdog state')
|
||||||
def watchdog_reset_pinged(context, name):
|
def watchdog_reset_pinged(context, name):
|
||||||
context.pctl.get_watchdog(name).reset()
|
context.pctl.get_watchdog(name).reset()
|
||||||
@@ -49,6 +44,31 @@ 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)
|
||||||
|
|||||||
@@ -6,14 +6,6 @@ Feature: watchdog
|
|||||||
Then postgres0 is a leader after 10 seconds
|
Then postgres0 is a leader after 10 seconds
|
||||||
And postgres0 role is the primary after 10 seconds
|
And postgres0 role is the primary after 10 seconds
|
||||||
And postgres0 watchdog has been pinged after 10 seconds
|
And postgres0 watchdog has been pinged after 10 seconds
|
||||||
And postgres0 watchdog has a 15 second timeout
|
|
||||||
|
|
||||||
Scenario: watchdog is reconfigured after global ttl changed
|
|
||||||
Given I run patronictl.py edit-config batman -s ttl=30 --force
|
|
||||||
Then I receive a response returncode 0
|
|
||||||
And I receive a response output "+ttl: 30"
|
|
||||||
When I sleep for 4 seconds
|
|
||||||
Then postgres0 watchdog has a 25 second timeout
|
|
||||||
|
|
||||||
Scenario: watchdog is disabled during pause
|
Scenario: watchdog is disabled during pause
|
||||||
Given I run patronictl.py pause batman
|
Given I run patronictl.py pause batman
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
FROM postgres:15
|
FROM postgres:11
|
||||||
LABEL maintainer="Alexander Kukushkin <akukushkin@microsoft.com>"
|
MAINTAINER Alexander Kukushkin <alexander.kukushkin@zalando.de>
|
||||||
|
|
||||||
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 \
|
||||||
&& apt-get update -y \
|
&& apt-get update -y \
|
||||||
|
&& apt-get upgrade -y \
|
||||||
&& apt-cache depends patroni | sed -n -e 's/.* Depends: \(python3-.\+\)$/\1/p' \
|
&& apt-cache depends patroni | sed -n -e 's/.* Depends: \(python3-.\+\)$/\1/p' \
|
||||||
| grep -Ev '^python3-(sphinx|etcd|consul|kazoo|kubernetes)' \
|
| grep -Ev '^python3-(sphinx|etcd|consul|kazoo|kubernetes)' \
|
||||||
| xargs apt-get install -y vim-tiny curl jq locales git python3-pip python3-wheel \
|
| xargs apt-get install -y vim-tiny curl jq locales git python3-pip python3-wheel \
|
||||||
@@ -24,7 +25,7 @@ RUN export DEBIAN_FRONTEND=noninteractive \
|
|||||||
&& apt-get clean -y \
|
&& apt-get clean -y \
|
||||||
&& rm -rf /var/lib/apt/lists/* /root/.cache
|
&& rm -rf /var/lib/apt/lists/* /root/.cache
|
||||||
|
|
||||||
COPY entrypoint.sh /
|
ADD entrypoint.sh /
|
||||||
|
|
||||||
EXPOSE 5432 8008
|
EXPOSE 5432 8008
|
||||||
ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 EDITOR=/usr/bin/editor
|
ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 EDITOR=/usr/bin/editor
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
FROM postgres:15
|
|
||||||
LABEL maintainer="Alexander Kukushkin <[email protected]>"
|
|
||||||
|
|
||||||
RUN export DEBIAN_FRONTEND=noninteractive \
|
|
||||||
&& echo 'APT::Install-Recommends "0";\nAPT::Install-Suggests "0";' > /etc/apt/apt.conf.d/01norecommend \
|
|
||||||
&& apt-get update -y \
|
|
||||||
&& apt-get upgrade -y \
|
|
||||||
&& apt-cache depends patroni | sed -n -e 's/.* Depends: \(python3-.\+\)$/\1/p' \
|
|
||||||
| grep -Ev '^python3-(sphinx|etcd|consul|kazoo|kubernetes)' \
|
|
||||||
| xargs apt-get install -y busybox vim-tiny curl jq less locales git python3-pip python3-wheel \
|
|
||||||
## Make sure we have a en_US.UTF-8 locale available
|
|
||||||
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
|
|
||||||
&& curl https://install.citusdata.com/community/deb.sh | bash \
|
|
||||||
&& apt-get -y install postgresql-15-citus-11.2 \
|
|
||||||
&& pip3 install setuptools \
|
|
||||||
&& pip3 install 'git+https://github.com/zalando/patroni.git#egg=patroni[kubernetes]' \
|
|
||||||
&& PGHOME=/home/postgres \
|
|
||||||
&& mkdir -p $PGHOME \
|
|
||||||
&& chown postgres $PGHOME \
|
|
||||||
&& sed -i "s|/var/lib/postgresql.*|$PGHOME:/bin/bash|" /etc/passwd \
|
|
||||||
&& /bin/busybox --install -s \
|
|
||||||
# Set permissions for OpenShift
|
|
||||||
&& chmod 775 $PGHOME \
|
|
||||||
&& chmod 664 /etc/passwd \
|
|
||||||
# Clean up
|
|
||||||
&& apt-get remove -y git python3-pip python3-wheel \
|
|
||||||
&& apt-get autoremove -y \
|
|
||||||
&& apt-get clean -y \
|
|
||||||
&& rm -rf /var/lib/apt/lists/* /root/.cache
|
|
||||||
|
|
||||||
ADD entrypoint.sh /
|
|
||||||
ENV PGSSLMODE=verify-ca PGSSLKEY=/etc/ssl/private/ssl-cert-snakeoil.key PGSSLCERT=/etc/ssl/certs/ssl-cert-snakeoil.pem PGSSLROOTCERT=/etc/ssl/certs/ssl-cert-snakeoil.pem
|
|
||||||
|
|
||||||
RUN sed -i 's/^postgresql:/&\n basebackup:\n checkpoint: fast/' /entrypoint.sh \
|
|
||||||
&& sed -i "s|^ postgresql:|&\n pg_hba:\n - local all all trust\n - hostssl replication all all md5 clientcert=$PGSSLMODE\n - hostssl all all all md5 clientcert=$PGSSLMODE\n parameters:\n max_connections: 100\n shared_buffers: 16MB\n ssl: 'on'\n ssl_ca_file: $PGSSLROOTCERT\n ssl_cert_file: $PGSSLCERT\n ssl_key_file: $PGSSLKEY\n citus.node_conninfo: 'sslrootcert=$PGSSLROOTCERT sslkey=$PGSSLKEY sslcert=$PGSSLCERT sslmode=$PGSSLMODE'|" /entrypoint.sh \
|
|
||||||
&& sed -i "s#^ \(superuser\|replication\):#&\n sslmode: $PGSSLMODE\n sslkey: $PGSSLKEY\n sslcert: $PGSSLCERT\n sslrootcert: $PGSSLROOTCERT#" /entrypoint.sh
|
|
||||||
|
|
||||||
EXPOSE 5432 8008
|
|
||||||
ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 EDITOR=/usr/bin/editor
|
|
||||||
USER postgres
|
|
||||||
WORKDIR /home/postgres
|
|
||||||
CMD ["/bin/bash", "/entrypoint.sh"]
|
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
# Kubernetes deployment examples
|
|
||||||
Below you will find examples of Patroni deployments using [kind](https://kind.sigs.k8s.io/).
|
|
||||||
|
|
||||||
# Patroni on K8s
|
|
||||||
The Patroni cluster deployment with a StatefulSet consisting of three Pods.
|
|
||||||
|
|
||||||
Example session:
|
|
||||||
|
|
||||||
$ kind create cluster
|
|
||||||
Creating cluster "kind" ...
|
|
||||||
✓ Ensuring node image (kindest/node:v1.25.3) 🖼
|
|
||||||
✓ Preparing nodes 📦
|
|
||||||
✓ Writing configuration 📜
|
|
||||||
✓ Starting control-plane 🕹️
|
|
||||||
✓ Installing CNI 🔌
|
|
||||||
✓ Installing StorageClass 💾
|
|
||||||
Set kubectl context to "kind-kind"
|
|
||||||
You can now use your cluster with:
|
|
||||||
|
|
||||||
kubectl cluster-info --context kind-kind
|
|
||||||
|
|
||||||
Thanks for using kind! 😊
|
|
||||||
|
|
||||||
$ docker build -t patroni .
|
|
||||||
Sending build context to Docker daemon 138.8kB
|
|
||||||
Step 1/9 : FROM postgres:15
|
|
||||||
...
|
|
||||||
Successfully built e9bfe69c5d2b
|
|
||||||
Successfully tagged patroni:latest
|
|
||||||
|
|
||||||
$ kind load docker-image patroni
|
|
||||||
Image: "" with ID "sha256:e9bfe69c5d2b319dec0cf564fb895484537664775e18f37f9b707914cc5537e6" not yet present on node "kind-control-plane", loading...
|
|
||||||
|
|
||||||
$ kubectl apply -f patroni_k8s.yaml
|
|
||||||
service/patronidemo-config created
|
|
||||||
statefulset.apps/patronidemo created
|
|
||||||
endpoints/patronidemo created
|
|
||||||
service/patronidemo created
|
|
||||||
service/patronidemo-repl created
|
|
||||||
secret/patronidemo created
|
|
||||||
serviceaccount/patronidemo created
|
|
||||||
role.rbac.authorization.k8s.io/patronidemo created
|
|
||||||
rolebinding.rbac.authorization.k8s.io/patronidemo created
|
|
||||||
clusterrole.rbac.authorization.k8s.io/patroni-k8s-ep-access created
|
|
||||||
clusterrolebinding.rbac.authorization.k8s.io/patroni-k8s-ep-access created
|
|
||||||
|
|
||||||
$ kubectl get pods -L role
|
|
||||||
NAME READY STATUS RESTARTS AGE ROLE
|
|
||||||
patronidemo-0 1/1 Running 0 34s master
|
|
||||||
patronidemo-1 1/1 Running 0 30s replica
|
|
||||||
patronidemo-2 1/1 Running 0 26s replica
|
|
||||||
|
|
||||||
$ kubectl exec -ti patronidemo-0 -- bash
|
|
||||||
postgres@patronidemo-0:~$ patronictl list
|
|
||||||
+ Cluster: patronidemo (7186662553319358497) ----+----+-----------+
|
|
||||||
| Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+---------------+------------+---------+---------+----+-----------+
|
|
||||||
| patronidemo-0 | 10.244.0.5 | Leader | running | 1 | |
|
|
||||||
| patronidemo-1 | 10.244.0.6 | Replica | running | 1 | 0 |
|
|
||||||
| patronidemo-2 | 10.244.0.7 | Replica | running | 1 | 0 |
|
|
||||||
+---------------+------------+---------+---------+----+-----------+
|
|
||||||
|
|
||||||
# Citus on K8s
|
|
||||||
The Citus cluster with the StatefulSets, one coordinator with three Pods and two workers with two pods each.
|
|
||||||
|
|
||||||
Example session:
|
|
||||||
|
|
||||||
$ kind create cluster
|
|
||||||
Creating cluster "kind" ...
|
|
||||||
✓ Ensuring node image (kindest/node:v1.25.3) 🖼
|
|
||||||
✓ Preparing nodes 📦
|
|
||||||
✓ Writing configuration 📜
|
|
||||||
✓ Starting control-plane 🕹️
|
|
||||||
✓ Installing CNI 🔌
|
|
||||||
✓ Installing StorageClass 💾
|
|
||||||
Set kubectl context to "kind-kind"
|
|
||||||
You can now use your cluster with:
|
|
||||||
|
|
||||||
kubectl cluster-info --context kind-kind
|
|
||||||
|
|
||||||
Thanks for using kind! 😊
|
|
||||||
|
|
||||||
demo@localhost:~/git/patroni/kubernetes$ docker build -f Dockerfile.citus -t patroni-citus-k8s .
|
|
||||||
Sending build context to Docker daemon 138.8kB
|
|
||||||
Step 1/11 : FROM postgres:15
|
|
||||||
...
|
|
||||||
Successfully built 8cd73e325028
|
|
||||||
Successfully tagged patroni-citus-k8s:latest
|
|
||||||
|
|
||||||
$ kind load docker-image patroni-citus-k8s
|
|
||||||
Image: "" with ID "sha256:8cd73e325028d7147672494965e53453f5540400928caac0305015eb2c7027c7" not yet present on node "kind-control-plane", loading...
|
|
||||||
|
|
||||||
$ kubectl apply -f citus_k8s.yaml
|
|
||||||
service/citusdemo-0-config created
|
|
||||||
service/citusdemo-1-config created
|
|
||||||
service/citusdemo-2-config created
|
|
||||||
statefulset.apps/citusdemo-0 created
|
|
||||||
statefulset.apps/citusdemo-1 created
|
|
||||||
statefulset.apps/citusdemo-2 created
|
|
||||||
endpoints/citusdemo-0 created
|
|
||||||
service/citusdemo-0 created
|
|
||||||
endpoints/citusdemo-1 created
|
|
||||||
service/citusdemo-1 created
|
|
||||||
endpoints/citusdemo-2 created
|
|
||||||
service/citusdemo-2 created
|
|
||||||
service/citusdemo-workers created
|
|
||||||
secret/citusdemo created
|
|
||||||
serviceaccount/citusdemo created
|
|
||||||
role.rbac.authorization.k8s.io/citusdemo created
|
|
||||||
rolebinding.rbac.authorization.k8s.io/citusdemo created
|
|
||||||
clusterrole.rbac.authorization.k8s.io/patroni-k8s-ep-access created
|
|
||||||
clusterrolebinding.rbac.authorization.k8s.io/patroni-k8s-ep-access created
|
|
||||||
|
|
||||||
$ kubectl get sts
|
|
||||||
NAME READY AGE
|
|
||||||
citusdemo-0 1/3 6s # coodinator (group=0)
|
|
||||||
citusdemo-1 1/2 6s # worker (group=1)
|
|
||||||
citusdemo-2 1/2 6s # worker (group=2)
|
|
||||||
|
|
||||||
$ kubectl get pods -l cluster-name=citusdemo -L role
|
|
||||||
NAME READY STATUS RESTARTS AGE ROLE
|
|
||||||
citusdemo-0-0 1/1 Running 0 105s master
|
|
||||||
citusdemo-0-1 1/1 Running 0 101s replica
|
|
||||||
citusdemo-0-2 1/1 Running 0 96s replica
|
|
||||||
citusdemo-1-0 1/1 Running 0 105s master
|
|
||||||
citusdemo-1-1 1/1 Running 0 101s replica
|
|
||||||
citusdemo-2-0 1/1 Running 0 105s master
|
|
||||||
citusdemo-2-1 1/1 Running 0 101s replica
|
|
||||||
|
|
||||||
$ kubectl exec -ti citusdemo-0-0 -- bash
|
|
||||||
postgres@citusdemo-0-0:~$ patronictl list
|
|
||||||
+ Citus cluster: citusdemo -----------+--------------+---------+----+-----------+
|
|
||||||
| Group | Member | Host | Role | State | TL | Lag in MB |
|
|
||||||
+-------+---------------+-------------+--------------+---------+----+-----------+
|
|
||||||
| 0 | citusdemo-0-0 | 10.244.0.10 | Leader | running | 1 | |
|
|
||||||
| 0 | citusdemo-0-1 | 10.244.0.12 | Replica | running | 1 | 0 |
|
|
||||||
| 0 | citusdemo-0-2 | 10.244.0.14 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 1 | citusdemo-1-0 | 10.244.0.8 | Leader | running | 1 | |
|
|
||||||
| 1 | citusdemo-1-1 | 10.244.0.11 | Sync Standby | running | 1 | 0 |
|
|
||||||
| 2 | citusdemo-2-0 | 10.244.0.9 | Leader | running | 1 | |
|
|
||||||
| 2 | citusdemo-2-1 | 10.244.0.13 | Sync Standby | running | 1 | 0 |
|
|
||||||
+-------+---------------+-------------+--------------+---------+----+-----------+
|
|
||||||
|
|
||||||
postgres@citusdemo-0-0:~$ psql citus
|
|
||||||
psql (15.1 (Debian 15.1-1.pgdg110+1))
|
|
||||||
Type "help" for help.
|
|
||||||
|
|
||||||
citus=# table pg_dist_node;
|
|
||||||
nodeid | groupid | nodename | nodeport | noderack | hasmetadata | isactive | noderole | nodecluster | metadatasynced | shouldhaveshards
|
|
||||||
--------+---------+-------------+----------+----------+-------------+----------+----------+-------------+----------------+------------------
|
|
||||||
1 | 0 | 10.244.0.10 | 5432 | default | t | t | primary | default | t | f
|
|
||||||
2 | 1 | 10.244.0.8 | 5432 | default | t | t | primary | default | t | t
|
|
||||||
3 | 2 | 10.244.0.9 | 5432 | default | t | t | primary | default | t | t
|
|
||||||
(3 rows)
|
|
||||||
@@ -1,590 +0,0 @@
|
|||||||
# headless services to avoid deletion of citusdemo-*-config endpoints
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-0-config
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '0'
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-1-config
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '1'
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-2-config
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '2'
|
|
||||||
spec:
|
|
||||||
clusterIP: None
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: &cluster_name citusdemo-0
|
|
||||||
labels: &labels
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '0'
|
|
||||||
citus-type: coordinator
|
|
||||||
spec:
|
|
||||||
replicas: 3
|
|
||||||
serviceName: *cluster_name
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
<<: *labels
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
<<: *labels
|
|
||||||
spec:
|
|
||||||
serviceAccountName: citusdemo
|
|
||||||
containers:
|
|
||||||
- name: *cluster_name
|
|
||||||
image: patroni-citus-k8s # docker build -f Dockerfile.citus -t patroni-citus-k8s .
|
|
||||||
imagePullPolicy: IfNotPresent
|
|
||||||
readinessProbe:
|
|
||||||
httpGet:
|
|
||||||
scheme: HTTP
|
|
||||||
path: /readiness
|
|
||||||
port: 8008
|
|
||||||
initialDelaySeconds: 3
|
|
||||||
periodSeconds: 10
|
|
||||||
timeoutSeconds: 5
|
|
||||||
successThreshold: 1
|
|
||||||
failureThreshold: 3
|
|
||||||
ports:
|
|
||||||
- containerPort: 8008
|
|
||||||
protocol: TCP
|
|
||||||
- containerPort: 5432
|
|
||||||
protocol: TCP
|
|
||||||
volumeMounts:
|
|
||||||
- mountPath: /home/postgres/pgdata
|
|
||||||
name: pgdata
|
|
||||||
env:
|
|
||||||
- name: PATRONI_KUBERNETES_POD_IP
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: status.podIP
|
|
||||||
- name: PATRONI_KUBERNETES_NAMESPACE
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: metadata.namespace
|
|
||||||
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
|
||||||
value: 'true'
|
|
||||||
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
|
|
||||||
value: 'true'
|
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
|
||||||
value: '{application: patroni, cluster-name: citusdemo}'
|
|
||||||
- name: PATRONI_CITUS_DATABASE
|
|
||||||
value: citus
|
|
||||||
- name: PATRONI_CITUS_GROUP
|
|
||||||
value: '0'
|
|
||||||
- name: PATRONI_SUPERUSER_USERNAME
|
|
||||||
value: postgres
|
|
||||||
- name: PATRONI_SUPERUSER_PASSWORD
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: citusdemo
|
|
||||||
key: superuser-password
|
|
||||||
- name: PATRONI_REPLICATION_USERNAME
|
|
||||||
value: standby
|
|
||||||
- name: PATRONI_REPLICATION_PASSWORD
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: citusdemo
|
|
||||||
key: replication-password
|
|
||||||
- name: PATRONI_SCOPE
|
|
||||||
value: citusdemo
|
|
||||||
- name: PATRONI_NAME
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: metadata.name
|
|
||||||
- name: PATRONI_POSTGRESQL_DATA_DIR
|
|
||||||
value: /home/postgres/pgdata/pgroot/data
|
|
||||||
- name: PATRONI_POSTGRESQL_PGPASS
|
|
||||||
value: /tmp/pgpass
|
|
||||||
- name: PATRONI_POSTGRESQL_LISTEN
|
|
||||||
value: '0.0.0.0:5432'
|
|
||||||
- name: PATRONI_RESTAPI_LISTEN
|
|
||||||
value: '0.0.0.0:8008'
|
|
||||||
terminationGracePeriodSeconds: 0
|
|
||||||
volumes:
|
|
||||||
- name: pgdata
|
|
||||||
emptyDir: {}
|
|
||||||
# volumeClaimTemplates:
|
|
||||||
# - metadata:
|
|
||||||
# labels:
|
|
||||||
# application: spilo
|
|
||||||
# spilo-cluster: *cluster_name
|
|
||||||
# annotations:
|
|
||||||
# volume.alpha.kubernetes.io/storage-class: anything
|
|
||||||
# name: pgdata
|
|
||||||
# spec:
|
|
||||||
# accessModes:
|
|
||||||
# - ReadWriteOnce
|
|
||||||
# resources:
|
|
||||||
# requests:
|
|
||||||
# storage: 5Gi
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: &cluster_name citusdemo-1
|
|
||||||
labels: &labels
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '1'
|
|
||||||
citus-type: worker
|
|
||||||
spec:
|
|
||||||
replicas: 2
|
|
||||||
serviceName: *cluster_name
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
<<: *labels
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
<<: *labels
|
|
||||||
spec:
|
|
||||||
serviceAccountName: citusdemo
|
|
||||||
containers:
|
|
||||||
- name: *cluster_name
|
|
||||||
image: patroni-citus-k8s # docker build -f Dockerfile.citus -t patroni-citus-k8s .
|
|
||||||
imagePullPolicy: IfNotPresent
|
|
||||||
readinessProbe:
|
|
||||||
httpGet:
|
|
||||||
scheme: HTTP
|
|
||||||
path: /readiness
|
|
||||||
port: 8008
|
|
||||||
initialDelaySeconds: 3
|
|
||||||
periodSeconds: 10
|
|
||||||
timeoutSeconds: 5
|
|
||||||
successThreshold: 1
|
|
||||||
failureThreshold: 3
|
|
||||||
ports:
|
|
||||||
- containerPort: 8008
|
|
||||||
protocol: TCP
|
|
||||||
- containerPort: 5432
|
|
||||||
protocol: TCP
|
|
||||||
volumeMounts:
|
|
||||||
- mountPath: /home/postgres/pgdata
|
|
||||||
name: pgdata
|
|
||||||
env:
|
|
||||||
- name: PATRONI_KUBERNETES_POD_IP
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: status.podIP
|
|
||||||
- name: PATRONI_KUBERNETES_NAMESPACE
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: metadata.namespace
|
|
||||||
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
|
||||||
value: 'true'
|
|
||||||
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
|
|
||||||
value: 'true'
|
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
|
||||||
value: '{application: patroni, cluster-name: citusdemo}'
|
|
||||||
- name: PATRONI_CITUS_DATABASE
|
|
||||||
value: citus
|
|
||||||
- name: PATRONI_CITUS_GROUP
|
|
||||||
value: '1'
|
|
||||||
- name: PATRONI_SUPERUSER_USERNAME
|
|
||||||
value: postgres
|
|
||||||
- name: PATRONI_SUPERUSER_PASSWORD
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: citusdemo
|
|
||||||
key: superuser-password
|
|
||||||
- name: PATRONI_REPLICATION_USERNAME
|
|
||||||
value: standby
|
|
||||||
- name: PATRONI_REPLICATION_PASSWORD
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: citusdemo
|
|
||||||
key: replication-password
|
|
||||||
- name: PATRONI_SCOPE
|
|
||||||
value: citusdemo
|
|
||||||
- name: PATRONI_NAME
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: metadata.name
|
|
||||||
- name: PATRONI_POSTGRESQL_DATA_DIR
|
|
||||||
value: /home/postgres/pgdata/pgroot/data
|
|
||||||
- name: PATRONI_POSTGRESQL_PGPASS
|
|
||||||
value: /tmp/pgpass
|
|
||||||
- name: PATRONI_POSTGRESQL_LISTEN
|
|
||||||
value: '0.0.0.0:5432'
|
|
||||||
- name: PATRONI_RESTAPI_LISTEN
|
|
||||||
value: '0.0.0.0:8008'
|
|
||||||
terminationGracePeriodSeconds: 0
|
|
||||||
volumes:
|
|
||||||
- name: pgdata
|
|
||||||
emptyDir: {}
|
|
||||||
# volumeClaimTemplates:
|
|
||||||
# - metadata:
|
|
||||||
# labels:
|
|
||||||
# application: spilo
|
|
||||||
# spilo-cluster: *cluster_name
|
|
||||||
# annotations:
|
|
||||||
# volume.alpha.kubernetes.io/storage-class: anything
|
|
||||||
# name: pgdata
|
|
||||||
# spec:
|
|
||||||
# accessModes:
|
|
||||||
# - ReadWriteOnce
|
|
||||||
# resources:
|
|
||||||
# requests:
|
|
||||||
# storage: 5Gi
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: apps/v1
|
|
||||||
kind: StatefulSet
|
|
||||||
metadata:
|
|
||||||
name: &cluster_name citusdemo-2
|
|
||||||
labels: &labels
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '2'
|
|
||||||
citus-type: worker
|
|
||||||
spec:
|
|
||||||
replicas: 2
|
|
||||||
serviceName: *cluster_name
|
|
||||||
selector:
|
|
||||||
matchLabels:
|
|
||||||
<<: *labels
|
|
||||||
template:
|
|
||||||
metadata:
|
|
||||||
labels:
|
|
||||||
<<: *labels
|
|
||||||
spec:
|
|
||||||
serviceAccountName: citusdemo
|
|
||||||
containers:
|
|
||||||
- name: *cluster_name
|
|
||||||
image: patroni-citus-k8s # docker build -f Dockerfile.citus -t patroni-citus-k8s .
|
|
||||||
imagePullPolicy: IfNotPresent
|
|
||||||
readinessProbe:
|
|
||||||
httpGet:
|
|
||||||
scheme: HTTP
|
|
||||||
path: /readiness
|
|
||||||
port: 8008
|
|
||||||
initialDelaySeconds: 3
|
|
||||||
periodSeconds: 10
|
|
||||||
timeoutSeconds: 5
|
|
||||||
successThreshold: 1
|
|
||||||
failureThreshold: 3
|
|
||||||
ports:
|
|
||||||
- containerPort: 8008
|
|
||||||
protocol: TCP
|
|
||||||
- containerPort: 5432
|
|
||||||
protocol: TCP
|
|
||||||
volumeMounts:
|
|
||||||
- mountPath: /home/postgres/pgdata
|
|
||||||
name: pgdata
|
|
||||||
env:
|
|
||||||
- name: PATRONI_KUBERNETES_POD_IP
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: status.podIP
|
|
||||||
- name: PATRONI_KUBERNETES_NAMESPACE
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: metadata.namespace
|
|
||||||
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
|
||||||
value: 'true'
|
|
||||||
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
|
|
||||||
value: 'true'
|
|
||||||
- name: PATRONI_KUBERNETES_LABELS
|
|
||||||
value: '{application: patroni, cluster-name: citusdemo}'
|
|
||||||
- name: PATRONI_CITUS_DATABASE
|
|
||||||
value: citus
|
|
||||||
- name: PATRONI_CITUS_GROUP
|
|
||||||
value: '2'
|
|
||||||
- name: PATRONI_SUPERUSER_USERNAME
|
|
||||||
value: postgres
|
|
||||||
- name: PATRONI_SUPERUSER_PASSWORD
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: citusdemo
|
|
||||||
key: superuser-password
|
|
||||||
- name: PATRONI_REPLICATION_USERNAME
|
|
||||||
value: standby
|
|
||||||
- name: PATRONI_REPLICATION_PASSWORD
|
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: citusdemo
|
|
||||||
key: replication-password
|
|
||||||
- name: PATRONI_SCOPE
|
|
||||||
value: citusdemo
|
|
||||||
- name: PATRONI_NAME
|
|
||||||
valueFrom:
|
|
||||||
fieldRef:
|
|
||||||
fieldPath: metadata.name
|
|
||||||
- name: PATRONI_POSTGRESQL_DATA_DIR
|
|
||||||
value: /home/postgres/pgdata/pgroot/data
|
|
||||||
- name: PATRONI_POSTGRESQL_PGPASS
|
|
||||||
value: /tmp/pgpass
|
|
||||||
- name: PATRONI_POSTGRESQL_LISTEN
|
|
||||||
value: '0.0.0.0:5432'
|
|
||||||
- name: PATRONI_RESTAPI_LISTEN
|
|
||||||
value: '0.0.0.0:8008'
|
|
||||||
terminationGracePeriodSeconds: 0
|
|
||||||
volumes:
|
|
||||||
- name: pgdata
|
|
||||||
emptyDir: {}
|
|
||||||
# volumeClaimTemplates:
|
|
||||||
# - metadata:
|
|
||||||
# labels:
|
|
||||||
# application: spilo
|
|
||||||
# spilo-cluster: *cluster_name
|
|
||||||
# annotations:
|
|
||||||
# volume.alpha.kubernetes.io/storage-class: anything
|
|
||||||
# name: pgdata
|
|
||||||
# spec:
|
|
||||||
# accessModes:
|
|
||||||
# - ReadWriteOnce
|
|
||||||
# resources:
|
|
||||||
# requests:
|
|
||||||
# storage: 5Gi
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Endpoints
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-0
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '0'
|
|
||||||
citus-type: coordinator
|
|
||||||
subsets: []
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-0
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '0'
|
|
||||||
citus-type: coordinator
|
|
||||||
spec:
|
|
||||||
type: ClusterIP
|
|
||||||
ports:
|
|
||||||
- port: 5432
|
|
||||||
targetPort: 5432
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Endpoints
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-1
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '1'
|
|
||||||
citus-type: worker
|
|
||||||
subsets: []
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-1
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '1'
|
|
||||||
citus-type: worker
|
|
||||||
spec:
|
|
||||||
type: ClusterIP
|
|
||||||
ports:
|
|
||||||
- port: 5432
|
|
||||||
targetPort: 5432
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Endpoints
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-2
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '2'
|
|
||||||
citus-type: worker
|
|
||||||
subsets: []
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-2
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-group: '2'
|
|
||||||
citus-type: worker
|
|
||||||
spec:
|
|
||||||
type: ClusterIP
|
|
||||||
ports:
|
|
||||||
- port: 5432
|
|
||||||
targetPort: 5432
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Service
|
|
||||||
metadata:
|
|
||||||
name: citusdemo-workers
|
|
||||||
labels: &labels
|
|
||||||
application: patroni
|
|
||||||
cluster-name: citusdemo
|
|
||||||
citus-type: worker
|
|
||||||
role: master
|
|
||||||
spec:
|
|
||||||
type: ClusterIP
|
|
||||||
selector:
|
|
||||||
<<: *labels
|
|
||||||
ports:
|
|
||||||
- port: 5432
|
|
||||||
targetPort: 5432
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: Secret
|
|
||||||
metadata:
|
|
||||||
name: &cluster_name citusdemo
|
|
||||||
labels:
|
|
||||||
application: patroni
|
|
||||||
cluster-name: *cluster_name
|
|
||||||
type: Opaque
|
|
||||||
data:
|
|
||||||
superuser-password: emFsYW5kbw==
|
|
||||||
replication-password: cmVwLXBhc3M=
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: ServiceAccount
|
|
||||||
metadata:
|
|
||||||
name: citusdemo
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: rbac.authorization.k8s.io/v1
|
|
||||||
kind: Role
|
|
||||||
metadata:
|
|
||||||
name: citusdemo
|
|
||||||
rules:
|
|
||||||
- apiGroups:
|
|
||||||
- ""
|
|
||||||
resources:
|
|
||||||
- configmaps
|
|
||||||
verbs:
|
|
||||||
- create
|
|
||||||
- get
|
|
||||||
- list
|
|
||||||
- patch
|
|
||||||
- update
|
|
||||||
- watch
|
|
||||||
# delete and deletecollection are required only for 'patronictl remove'
|
|
||||||
- delete
|
|
||||||
- deletecollection
|
|
||||||
- apiGroups:
|
|
||||||
- ""
|
|
||||||
resources:
|
|
||||||
- endpoints
|
|
||||||
verbs:
|
|
||||||
- get
|
|
||||||
- patch
|
|
||||||
- update
|
|
||||||
# the following three privileges are necessary only when using endpoints
|
|
||||||
- create
|
|
||||||
- list
|
|
||||||
- watch
|
|
||||||
# delete and deletecollection are required only for for 'patronictl remove'
|
|
||||||
- delete
|
|
||||||
- deletecollection
|
|
||||||
- apiGroups:
|
|
||||||
- ""
|
|
||||||
resources:
|
|
||||||
- pods
|
|
||||||
verbs:
|
|
||||||
- get
|
|
||||||
- list
|
|
||||||
- patch
|
|
||||||
- update
|
|
||||||
- watch
|
|
||||||
# The following privilege is only necessary for creation of headless service
|
|
||||||
# for citusdemo-config endpoint, in order to prevent cleaning it up by the
|
|
||||||
# k8s master. You can avoid giving this privilege by explicitly creating the
|
|
||||||
# service like it is done in this manifest (lines 2..10)
|
|
||||||
- apiGroups:
|
|
||||||
- ""
|
|
||||||
resources:
|
|
||||||
- services
|
|
||||||
verbs:
|
|
||||||
- create
|
|
||||||
|
|
||||||
---
|
|
||||||
apiVersion: rbac.authorization.k8s.io/v1
|
|
||||||
kind: RoleBinding
|
|
||||||
metadata:
|
|
||||||
name: citusdemo
|
|
||||||
roleRef:
|
|
||||||
apiGroup: rbac.authorization.k8s.io
|
|
||||||
kind: Role
|
|
||||||
name: citusdemo
|
|
||||||
subjects:
|
|
||||||
- kind: ServiceAccount
|
|
||||||
name: citusdemo
|
|
||||||
|
|
||||||
# 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: citusdemo
|
|
||||||
# The namespace must be specified explicitly.
|
|
||||||
# If deploying to the different namespace you have to change it.
|
|
||||||
namespace: default
|
|
||||||
@@ -33,5 +33,7 @@ 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
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# Patroni OpenShift Configuration
|
# Patroni OpenShift Configuration
|
||||||
Patroni can be run in OpenShift. Based on the kubernetes configuration, the Dockerfile and Entrypoint has been modified to support the dynamic UID/GID configuration that is applied in OpenShift. This can be run under the standard `restricted` SCC.
|
Patroni can be run in OpenShift. Based on the kubernetes configuration, the Dockerfile and Entrypoint has been modified to support the dynamic UID/GID configuration that is applied in OpenShift. This can be run under the standard `restricted` SCC.
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
|
|
||||||
@@ -11,39 +11,39 @@ oc new-project patroni-test
|
|||||||
|
|
||||||
## Build the image
|
## Build the image
|
||||||
|
|
||||||
Note: Update the references when merged upstream.
|
Note: Update the references when merged upstream.
|
||||||
Note: If deploying as a template for multiple users, the following commands should be performed in a shared namespace like `openshift`.
|
Note: If deploying as a template for multiple users, the following commands should be performed in a shared namespace like `openshift`.
|
||||||
|
|
||||||
```
|
```
|
||||||
oc import-image postgres:10 --confirm -n openshift
|
oc import-image postgres:10 --confirm -n openshift
|
||||||
oc new-build https://github.com/zalando/patroni --context-dir=kubernetes -n openshift
|
oc new-build https://github.com/zalando/patroni --context-dir=kubernetes -n openshift
|
||||||
```
|
```
|
||||||
|
|
||||||
## Deploy the Image
|
## Deploy the Image
|
||||||
Two configuration templates exist in [templates](templates) directory:
|
Two configuration templates exist in [templates](templates) directory:
|
||||||
- Patroni Ephemeral
|
- Patroni Ephemeral
|
||||||
- Patroni Persistent
|
- Patroni Persistent
|
||||||
|
|
||||||
The only difference is whether or not the statefulset requests persistent storage.
|
The only difference is whether or not the statefulset requests persistent storage.
|
||||||
|
|
||||||
## Create the Template
|
## Create the Template
|
||||||
Install the template into the `openshift` namespace if this should be shared across projects:
|
Install the template into the `openshift` namespace if this should be shared across projects:
|
||||||
|
|
||||||
```
|
```
|
||||||
oc create -f templates/template_patroni_ephemeral.yml -n openshift
|
oc create -f templates/template_patroni_ephemeral.yml -n openshift
|
||||||
```
|
```
|
||||||
|
|
||||||
Then, from your own project:
|
Then, from your own project:
|
||||||
|
|
||||||
```
|
```
|
||||||
oc new-app patroni-pgsql-ephemeral
|
oc new-app patroni-pgsql-ephemeral
|
||||||
```
|
```
|
||||||
|
|
||||||
Once the pods are running, two configmaps should be available:
|
Once the pods are running, two configmaps should be available:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ oc get configmap
|
$ oc get configmap
|
||||||
NAME DATA AGE
|
NAME DATA AGE
|
||||||
patroniocp-config 0 1m
|
patroniocp-config 0 1m
|
||||||
patroniocp-leader 0 1m
|
patroniocp-leader 0 1m
|
||||||
```
|
```
|
||||||
@@ -118,8 +118,6 @@ 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
|
||||||
@@ -154,16 +152,6 @@ 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,34 +240,6 @@ 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,9 +5,11 @@ 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 with persistent storage.
|
openshift.io/long-description: This template deploys a a patroni postgresql HA cluster without persistent storage.
|
||||||
tags: postgresql
|
tags: postgresql
|
||||||
objects:
|
objects:
|
||||||
- apiVersion: v1
|
- apiVersion: v1
|
||||||
@@ -104,20 +106,6 @@ 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
|
||||||
@@ -130,8 +118,6 @@ 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
|
||||||
@@ -166,16 +152,6 @@ 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
|
||||||
@@ -276,34 +252,6 @@ 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
|
||||||
@@ -352,4 +300,4 @@ parameters:
|
|||||||
- description: The size of the persistent volume to create.
|
- description: The size of the persistent volume to create.
|
||||||
displayName: Persistent Volume Size
|
displayName: Persistent Volume Size
|
||||||
name: PVC_SIZE
|
name: PVC_SIZE
|
||||||
value: 5Gi
|
value: 5Gi
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
# Jenkins Test
|
# Jenkins Test
|
||||||
This pipeline test will create a separate deployment config for a pgbench pod and execute a test against the patroni cluster. This is a sample and should be customized.
|
This pipeline test will create a separate deployment config for a pgbench pod and execute a test against the patroni cluster. This is a sample and should be customized.
|
||||||
@@ -10,7 +10,7 @@ spec:
|
|||||||
clusterIP: None
|
clusterIP: None
|
||||||
|
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1beta1
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: &cluster_name patronidemo
|
name: &cluster_name patronidemo
|
||||||
@@ -20,10 +20,6 @@ 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:
|
||||||
@@ -35,16 +31,6 @@ 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
|
||||||
@@ -62,8 +48,6 @@ 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
|
||||||
@@ -139,25 +123,6 @@ 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
|
||||||
@@ -245,37 +210,3 @@ 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
|
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
pip install --ignore-installed pyinstaller
|
pip install --ignore-installed setuptools==19.2 pyinstaller
|
||||||
pyinstaller --clean patroni.spec
|
pyinstaller --clean --onefile patroni.spec
|
||||||
|
|||||||
+9
-1
@@ -1,6 +1,14 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from patroni.__main__ import main
|
from patroni 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()
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ def hiddenimports():
|
|||||||
sys.path.insert(0, '.')
|
sys.path.insert(0, '.')
|
||||||
try:
|
try:
|
||||||
import patroni.dcs
|
import patroni.dcs
|
||||||
return patroni.dcs.dcs_modules() + ['http.server']
|
return patroni.dcs.dcs_modules()
|
||||||
finally:
|
finally:
|
||||||
sys.path.pop(0)
|
sys.path.pop(0)
|
||||||
|
|
||||||
|
|||||||
+215
-23
@@ -1,8 +1,169 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
PATRONI_ENV_PREFIX = 'PATRONI_'
|
logger = logging.getLogger(__name__)
|
||||||
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):
|
||||||
@@ -10,32 +171,63 @@ def fatal(string, *args):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def parse_version(version):
|
def check_psycopg2():
|
||||||
def _parse_version(version):
|
min_psycopg2 = (2, 5, 4)
|
||||||
|
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:
|
||||||
from psycopg2 import __version__
|
import psycopg2
|
||||||
if _parse_version(__version__) >= _min_psycopg2:
|
version_str = psycopg2.__version__.split(' ')[0]
|
||||||
return
|
version = tuple(parse_version(version_str))
|
||||||
version_str = __version__.split(' ')[0]
|
if version < min_psycopg2:
|
||||||
|
fatal('Patroni requires psycopg2>={0}, but only {1} is available', min_psycopg2_str, version_str)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
version_str = None
|
fatal('Patroni requires psycopg2>={0} or psycopg2-binary', min_psycopg2_str)
|
||||||
|
|
||||||
try:
|
|
||||||
from psycopg import __version__
|
def main():
|
||||||
except ImportError:
|
check_psycopg2()
|
||||||
error = 'Patroni requires psycopg2>={0}, psycopg2-binary, or psycopg>=3.0'.format(min_psycopg2_str)
|
if os.getpid() != 1:
|
||||||
if version_str:
|
return patroni_main()
|
||||||
error += ', but only psycopg2=={0} is available'.format(version_str)
|
|
||||||
fatal(error)
|
# 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()
|
||||||
|
|||||||
+1
-179
@@ -1,182 +1,4 @@
|
|||||||
import logging
|
from patroni import main
|
||||||
import os
|
|
||||||
import signal
|
|
||||||
import time
|
|
||||||
|
|
||||||
from patroni.daemon import AbstractPatroniDaemon, abstract_main
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Patroni(AbstractPatroniDaemon):
|
|
||||||
|
|
||||||
def __init__(self, config):
|
|
||||||
from patroni.api import RestApiServer
|
|
||||||
from patroni.dcs import get_dcs
|
|
||||||
from patroni.ha import Ha
|
|
||||||
from patroni.postgresql import Postgresql
|
|
||||||
from patroni.request import PatroniRequest
|
|
||||||
from patroni.version import __version__
|
|
||||||
from patroni.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)
|
|
||||||
self.watchdog.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 patroni 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__':
|
||||||
|
|||||||
+138
-482
@@ -1,67 +1,62 @@
|
|||||||
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 socket
|
import socket
|
||||||
import sys
|
|
||||||
|
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from patroni.postgresql import PostgresConnectionException
|
||||||
from ipaddress import ip_address, ip_network
|
from patroni.postgresql.misc import postgres_version_to_int, PostgresException
|
||||||
from socketserver import ThreadingMixIn
|
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.socketserver import ThreadingMixIn
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from urllib.parse import urlparse, parse_qs
|
|
||||||
|
|
||||||
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):
|
||||||
|
"""Decorator function to check authorization header.
|
||||||
|
|
||||||
|
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, default=str), content_type='application/json')
|
self._write_response(status_code, json.dumps(response), content_type='application/json')
|
||||||
|
|
||||||
def check_access(func):
|
def send_auth_request(self, body):
|
||||||
"""Decorator function to check the source ip, authorization header. or client certificates
|
headers = {'WWW-Authenticate': 'Basic realm="' + self.server.patroni.__class__.__name__ + '"'}
|
||||||
|
self._write_response(401, body, headers=headers)
|
||||||
|
|
||||||
Usage example:
|
def check_auth_header(self):
|
||||||
@check_access
|
auth_header = self.headers.get('Authorization')
|
||||||
def do_PUT_foo():
|
status = self.server.check_auth_header(auth_header)
|
||||||
pass
|
return not status or self.send_auth_request(status)
|
||||||
"""
|
|
||||||
|
|
||||||
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
|
||||||
@@ -92,131 +87,59 @@ 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"""
|
||||||
|
|
||||||
path = '/primary' if self.path == '/' else self.path
|
time_start = time.time()
|
||||||
|
request_type = 'OPTIONS' if write_status_code_only else 'GET'
|
||||||
|
|
||||||
|
path = '/master' if self.path == '/' else self.path
|
||||||
response = self.get_postgresql_status()
|
response = self.get_postgresql_status()
|
||||||
|
|
||||||
patroni = self.server.patroni
|
patroni = self.server.patroni
|
||||||
cluster = patroni.dcs.cluster
|
cluster = patroni.dcs.cluster
|
||||||
|
|
||||||
leader_optime = cluster and cluster.last_lsn or 0
|
|
||||||
replayed_location = response.get('xlog', {}).get('replayed_location', 0)
|
|
||||||
max_replica_lag = parse_int(self.path_query.get('lag', [sys.maxsize])[0], 'B')
|
|
||||||
if max_replica_lag is None:
|
|
||||||
max_replica_lag = sys.maxsize
|
|
||||||
is_lagging = leader_optime and leader_optime > replayed_location + max_replica_lag
|
|
||||||
|
|
||||||
replica_status_code = 200 if not patroni.noloadbalance and not is_lagging and \
|
|
||||||
response.get('role') == 'replica' and response.get('state') == 'running' else 503
|
|
||||||
|
|
||||||
if not cluster and patroni.ha.is_paused():
|
if not cluster and patroni.ha.is_paused():
|
||||||
leader_status_code = 200 if response.get('role') in ('master', 'primary', 'standby_leader') else 503
|
primary_status_code = 200 if response['role'] == 'master' else 503
|
||||||
primary_status_code = 200 if response.get('role') in ('master', 'primary') 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:
|
else:
|
||||||
leader_status_code = primary_status_code = standby_leader_status_code = 503
|
primary_status_code = 200 if patroni.ha.is_leader() else 503
|
||||||
|
|
||||||
|
replica_status_code = 200 if not patroni.noloadbalance and \
|
||||||
|
response.get('role') == 'replica' and response.get('state') == 'running' else 503
|
||||||
status_code = 503
|
status_code = 503
|
||||||
|
|
||||||
ignore_tags = False
|
if patroni.ha.is_standby_cluster() and ('standby_leader' in path or 'standby-leader' in path):
|
||||||
if 'standby_leader' in path or 'standby-leader' in path:
|
status_code = 200 if patroni.ha.is_leader() else 503
|
||||||
status_code = standby_leader_status_code
|
elif 'master' in path or 'leader' in path or 'primary' in path or 'read-write' in path:
|
||||||
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 and 'sync' not in path:
|
elif 'read-only' in path:
|
||||||
status_code = 200 if 200 in (primary_status_code, standby_leader_status_code) else replica_status_code
|
status_code = 200 if primary_status_code == 200 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 = response.get('sync_standby')
|
is_synchronous = cluster.is_synchronous_mode() and cluster.sync \
|
||||||
|
and cluster.sync.sync_standby == patroni.postgresql.name
|
||||||
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, str):
|
|
||||||
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
|
||||||
self._write_status_code_only(status_code)
|
message = self.responses[status_code][0]
|
||||||
|
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 in ('master', 'primary') 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:
|
||||||
@@ -224,112 +147,6 @@ 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'] in ('master', 'primary'))))
|
|
||||||
|
|
||||||
metrics.append("# HELP patroni_primary Value is 1 if this node is the leader, 0 otherwise.")
|
|
||||||
metrics.append("# TYPE patroni_primary gauge")
|
|
||||||
metrics.append("patroni_primary{0} {1}".format(scope_label, int(postgres['role'] in ('master', 'primary'))))
|
|
||||||
|
|
||||||
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_sync_standby Value is 1 if this node is a sync standby replica, 0 otherwise.")
|
|
||||||
metrics.append("# TYPE patroni_sync_standby gauge")
|
|
||||||
metrics.append("patroni_sync_standby{0} {1}".format(scope_label, int(postgres.get('sync_standby', False))))
|
|
||||||
|
|
||||||
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_failsafe_mode_is_active Value is 1 if the cluster is unlocked, 0 if locked.")
|
|
||||||
metrics.append("# TYPE patroni_failsafe_mode_is_active gauge")
|
|
||||||
metrics.append("patroni_failsafe_mode_is_active{0} {1}"
|
|
||||||
.format(scope_label, int(postgres.get('failsafe_mode_is_active', 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 {}
|
||||||
@@ -344,13 +161,11 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
logger.exception('Bad request')
|
logger.exception('Bad request')
|
||||||
self.send_error(400)
|
self.send_error(400)
|
||||||
|
|
||||||
@check_access
|
@check_auth
|
||||||
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(True)
|
cluster = self.server.patroni.dcs.get_cluster()
|
||||||
if not (cluster.config and cluster.config.modify_index):
|
|
||||||
return self.send_error(503)
|
|
||||||
data = cluster.config.data.copy()
|
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=(',', ':'))
|
||||||
@@ -359,7 +174,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_access
|
@check_auth
|
||||||
def do_PUT_config(self):
|
def do_PUT_config(self):
|
||||||
request = self._read_json_content()
|
request = self._read_json_content()
|
||||||
if request:
|
if request:
|
||||||
@@ -370,36 +185,20 @@ 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_access
|
@check_auth
|
||||||
def do_POST_reload(self):
|
def do_POST_reload(self):
|
||||||
self.server.patroni.sighup_handler()
|
try:
|
||||||
self._write_response(202, 'reload scheduled')
|
if self.server.patroni.config.reload_local_configuration(True):
|
||||||
|
status_code = 202
|
||||||
def do_GET_failsafe(self):
|
response = 'reload scheduled'
|
||||||
failsafe = self.server.patroni.dcs.failsafe
|
self.server.patroni.sighup_handler()
|
||||||
if isinstance(failsafe, dict):
|
else:
|
||||||
self._write_json_response(200, failsafe)
|
status_code = 200
|
||||||
else:
|
response = 'nothing changed'
|
||||||
self.send_error(502)
|
except Exception as e:
|
||||||
|
status_code = 500
|
||||||
@check_access
|
response = str(e)
|
||||||
def do_POST_failsafe(self):
|
self._write_response(status_code, response)
|
||||||
if self.server.patroni.ha.is_failsafe_mode():
|
|
||||||
request = self._read_json_content()
|
|
||||||
if request:
|
|
||||||
message = self.server.patroni.ha.update_failsafe(request) or 'Accepted'
|
|
||||||
code = 200 if message == 'Accepted' else 500
|
|
||||||
self._write_response(code, message)
|
|
||||||
else:
|
|
||||||
self.send_error(502)
|
|
||||||
|
|
||||||
@check_access
|
|
||||||
def do_POST_sigterm(self):
|
|
||||||
"""Only for behave testing on windows"""
|
|
||||||
|
|
||||||
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):
|
||||||
@@ -422,7 +221,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
status_code = 422
|
status_code = 422
|
||||||
return (status_code, error, scheduled_at)
|
return (status_code, error, scheduled_at)
|
||||||
|
|
||||||
@check_access
|
@check_auth
|
||||||
def do_POST_restart(self):
|
def do_POST_restart(self):
|
||||||
status_code = 500
|
status_code = 500
|
||||||
data = 'restart failed'
|
data = 'restart failed'
|
||||||
@@ -445,9 +244,9 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
status_code = _
|
status_code = _
|
||||||
break
|
break
|
||||||
elif k == 'role':
|
elif k == 'role':
|
||||||
if request[k] not in ('master', 'primary', 'replica'):
|
if request[k] not in ('master', 'replica'):
|
||||||
status_code = 400
|
status_code = 400
|
||||||
data = "PostgreSQL role should be either primary or replica"
|
data = "PostgreSQL role should be either master or replica"
|
||||||
break
|
break
|
||||||
elif k == 'postgres_version':
|
elif k == 'postgres_version':
|
||||||
try:
|
try:
|
||||||
@@ -483,7 +282,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
status_code = 409
|
status_code = 409
|
||||||
self._write_response(status_code, data)
|
self._write_response(status_code, data)
|
||||||
|
|
||||||
@check_access
|
@check_auth
|
||||||
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"
|
||||||
@@ -493,21 +292,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
code = 404
|
code = 404
|
||||||
self._write_response(code, data)
|
self._write_response(code, data)
|
||||||
|
|
||||||
@check_access
|
@check_auth
|
||||||
def do_DELETE_switchover(self):
|
|
||||||
failover = self.server.patroni.dcs.get_cluster().failover
|
|
||||||
if failover and failover.scheduled_at:
|
|
||||||
if not self.server.patroni.dcs.manual_failover('', '', index=failover.index):
|
|
||||||
return self.send_error(409)
|
|
||||||
else:
|
|
||||||
data = "scheduled switchover deleted"
|
|
||||||
code = 200
|
|
||||||
else:
|
|
||||||
data = "no switchover is scheduled"
|
|
||||||
code = 404
|
|
||||||
self._write_response(code, data)
|
|
||||||
|
|
||||||
@check_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)
|
||||||
|
|
||||||
@@ -539,20 +324,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 occurred during polling %s result: %s', action, e)
|
logger.debug('Exception occured 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 candidate not in cluster.sync.members:
|
if action == 'switchover' and cluster.is_synchronous_mode() and cluster.sync.sync_standby != candidate:
|
||||||
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 in cluster.sync.members]
|
members = [m for m in cluster.members if m.name == cluster.sync.sync_standby]
|
||||||
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:
|
||||||
@@ -564,7 +349,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_access
|
@check_auth
|
||||||
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, '')
|
||||||
@@ -617,18 +402,6 @@ class RestApiHandler(BaseHTTPRequestHandler):
|
|||||||
def do_POST_switchover(self):
|
def do_POST_switchover(self):
|
||||||
self.do_POST_failover(action='switchover')
|
self.do_POST_failover(action='switchover')
|
||||||
|
|
||||||
@check_access
|
|
||||||
def do_POST_citus(self):
|
|
||||||
request = self._read_json_content()
|
|
||||||
if not request:
|
|
||||||
return
|
|
||||||
|
|
||||||
patroni = self.server.patroni
|
|
||||||
if patroni.postgresql.citus_handler.is_coordinator() and patroni.ha.is_leader():
|
|
||||||
cluster = patroni.dcs.get_cluster(True)
|
|
||||||
patroni.postgresql.citus_handler.handle_event(cluster, request)
|
|
||||||
self._write_response(200, 'OK')
|
|
||||||
|
|
||||||
def parse_request(self):
|
def parse_request(self):
|
||||||
"""Override parse_request method to enrich basic functionality of `BaseHTTPRequestHandler` class
|
"""Override parse_request method to enrich basic functionality of `BaseHTTPRequestHandler` class
|
||||||
|
|
||||||
@@ -641,9 +414,6 @@ 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):
|
||||||
@@ -657,85 +427,77 @@ 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 postgresql.state not in ('running', 'restarting', 'starting'):
|
if self.server.patroni.postgresql.state not in ('running', 'restarting', 'starting'):
|
||||||
raise RetryFailedError('')
|
raise RetryFailedError('')
|
||||||
stmt = ("SELECT " + postgresql.POSTMASTER_START_TIME + ", " + postgresql.TL_LSN + ","
|
stmt = ("WITH replication_info AS ("
|
||||||
" pg_catalog.pg_last_xact_replay_timestamp(),"
|
"SELECT usename, application_name, client_addr, state, sync_state, sync_priority"
|
||||||
" pg_catalog.array_to_json(pg_catalog.array_agg(pg_catalog.row_to_json(ri))) "
|
" FROM pg_catalog.pg_stat_replication) SELECT"
|
||||||
"FROM (SELECT (SELECT rolname FROM pg_catalog.pg_authid WHERE oid = usesysid) AS usename,"
|
" pg_catalog.to_char(pg_catalog.pg_postmaster_start_time(), 'YYYY-MM-DD HH24:MI:SS.MS TZ'),"
|
||||||
" application_name, client_addr, w.state, sync_state, sync_priority"
|
" CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0"
|
||||||
" FROM pg_catalog.pg_stat_get_wal_senders() w, pg_catalog.pg_stat_get_activity(pid)) AS ri")
|
" ELSE ('x' || pg_catalog.substr(pg_catalog.pg_{0}file_name("
|
||||||
|
"pg_catalog.pg_current_{0}_{1}()), 1, 8))::bit(32)::int END,"
|
||||||
|
" CASE WHEN pg_catalog.pg_is_in_recovery() THEN 0"
|
||||||
|
" ELSE pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_current_{0}_{1}(), '0/0')::bigint END,"
|
||||||
|
" pg_catalog.pg_{0}_{1}_diff(COALESCE(pg_catalog.pg_last_{0}_receive_{1}(),"
|
||||||
|
" pg_catalog.pg_last_{0}_replay_{1}()), '0/0')::bigint,"
|
||||||
|
" pg_catalog.pg_{0}_{1}_diff(pg_catalog.pg_last_{0}_replay_{1}(), '0/0')::bigint,"
|
||||||
|
" 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(postgresql.wal_name, postgresql.lsn_name), retry=retry)[0]
|
row = self.query(stmt.format(self.server.patroni.postgresql.wal_name,
|
||||||
|
self.server.patroni.postgresql.lsn_name), retry=retry)[0]
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
'state': postgresql.state,
|
'state': self.server.patroni.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': postgresql.server_version,
|
'server_version': self.server.patroni.postgresql.server_version,
|
||||||
|
'cluster_unlocked': bool(not cluster or cluster.is_unlocked()),
|
||||||
'xlog': ({
|
'xlog': ({
|
||||||
'received_location': row[4] or row[3],
|
'received_location': row[3],
|
||||||
'replayed_location': row[3],
|
'replayed_location': row[4],
|
||||||
'replayed_timestamp': row[6],
|
'replayed_timestamp': row[5],
|
||||||
'paused': row[5]} if row[1] == 0 else {
|
'paused': row[6]} 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'] = postgresql.role
|
result['role'] = self.server.patroni.postgresql.role
|
||||||
|
|
||||||
if result['role'] == 'replica' and cluster and cluster.is_synchronous_mode()\
|
|
||||||
and cluster.sync and postgresql.name in cluster.sync.members:
|
|
||||||
result['sync_standby'] = True
|
|
||||||
|
|
||||||
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'] = postgresql.replica_cached_timeline(leader_timeline)
|
result['timeline'] = self.server.patroni.postgresql.replica_cached_timeline(leader_timeline)
|
||||||
|
|
||||||
if row[7]:
|
if row[7]:
|
||||||
result['replication'] = row[7]
|
result['replication'] = row[7]
|
||||||
|
|
||||||
except (psycopg.Error, RetryFailedError, PostgresConnectionException):
|
return result
|
||||||
state = postgresql.state
|
except (psycopg2.Error, RetryFailedError, PostgresConnectionException):
|
||||||
|
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'
|
||||||
result = {'state': state, 'role': postgresql.role}
|
return {'state': state, 'role': self.server.patroni.postgresql.role}
|
||||||
|
|
||||||
if not cluster or cluster.is_unlocked():
|
|
||||||
result['cluster_unlocked'] = True
|
|
||||||
if self.server.patroni.ha.failsafe_is_active():
|
|
||||||
result['failsafe_mode_is_active'] = 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):
|
||||||
latency = 1000.0 * (time.time() - self.__start_time)
|
logger.debug("API thread: %s - - [%s] %s", self.client_address[0], self.log_date_time_string(), fmt % args)
|
||||||
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.__ssl_options = None
|
self.__initialize(config)
|
||||||
self.__ssl_serial_number = None
|
self.__set_config_parameters(config)
|
||||||
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):
|
||||||
@@ -744,7 +506,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 psycopg.Error as e:
|
except psycopg2.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')
|
||||||
@@ -757,7 +519,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 hmac.compare_digest(self.__auth_key, key.encode('utf-8'))
|
return self.__auth_key == key
|
||||||
|
|
||||||
def check_auth_header(self, auth_header):
|
def check_auth_header(self, auth_header):
|
||||||
if self.__auth_key:
|
if self.__auth_key:
|
||||||
@@ -767,43 +529,12 @@ class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
|
|||||||
return 'not authenticated'
|
return 'not authenticated'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __resolve_ips(host, port):
|
def __get_ssl_options(config):
|
||||||
try:
|
return {option: config[option] for option in ['certfile', 'keyfile'] if option in config}
|
||||||
for _, _, _, _, sa in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP):
|
|
||||||
yield ip_network(sa[0], False)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Failed to resolve %s: %r', host, e)
|
|
||||||
|
|
||||||
def __members_ips(self):
|
def __set_config_parameters(self, config):
|
||||||
cluster = self.patroni.dcs.cluster
|
self.__auth_key = base64.b64encode(config['auth'].encode('utf-8')).decode('utf-8') if 'auth' in config else None
|
||||||
if self.__allowlist_include_members and cluster:
|
self.connection_string = uri(self.__protocol, config.get('connect_address') or self.__listen, 'patroni')
|
||||||
for cluster in [cluster] + list(cluster.workers.values()):
|
|
||||||
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 = ip_address(rh.client_address[0])
|
|
||||||
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():
|
||||||
@@ -822,7 +553,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 in ('', '*'):
|
if host == '':
|
||||||
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)
|
||||||
@@ -830,123 +561,48 @@ 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]
|
||||||
try:
|
HTTPServer.__init__(self, info[0][-1][:2], RestApiHandler)
|
||||||
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):
|
def __initialize(self, config):
|
||||||
try:
|
try:
|
||||||
host, port = split_host_port(listen, None)
|
host, port = split_host_port(config['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(listen))
|
.format(config['listen']))
|
||||||
|
|
||||||
reloading_config = self.__listen is not None # changing config in runtime
|
if 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 = listen
|
self.__listen = config['listen']
|
||||||
self.__ssl_options = ssl_options
|
self.__ssl_options = self.__get_ssl_options(config)
|
||||||
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
|
|
||||||
try:
|
|
||||||
request.unwrap()
|
|
||||||
except Exception as e:
|
|
||||||
logger.debug('Failed to shutdown SSL connection: %r', e)
|
|
||||||
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
|
||||||
try:
|
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
|
||||||
crt = ssl._ssl._test_decode_cert(self.__ssl_options['certfile'])
|
ctx.load_cert_chain(**self.__ssl_options)
|
||||||
return crt.get('serialNumber')
|
self.socket = ctx.wrap_socket(self.socket, server_side=True)
|
||||||
except ssl.SSLError as e:
|
self.__protocol = 'https'
|
||||||
logger.error('Failed to get serial number from certificate %s: %r', self.__ssl_options['certfile'], e)
|
return True
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
def _build_allowlist(self, value):
|
|
||||||
if isinstance(value, list):
|
|
||||||
for v in value:
|
|
||||||
if '/' in v: # netmask
|
|
||||||
try:
|
|
||||||
yield ip_network(v, False)
|
|
||||||
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')
|
||||||
|
|
||||||
self.__allowlist = tuple(self._build_allowlist(config.get('allowlist')))
|
elif (self.__listen != config['listen'] or self.__ssl_options != self.__get_ssl_options(config)) \
|
||||||
self.__allowlist_include_members = config.get('allowlist_include_members')
|
and self.__initialize(config):
|
||||||
|
self.start()
|
||||||
ssl_options = {n: config[n] for n in ('certfile', 'keyfile', 'keyfile_password',
|
self.__set_config_parameters(config)
|
||||||
'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'), str):
|
|
||||||
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):
|
||||||
logger.warning('Exception happened during processing of request from %s:%s',
|
address, port = client_address
|
||||||
client_address[0], client_address[1])
|
logger.warning('Exception happened during processing of request from {}:{}'.format(address, port))
|
||||||
logger.warning(traceback.format_exc())
|
logger.warning(traceback.format_exc())
|
||||||
|
|||||||
@@ -110,12 +110,6 @@ 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:
|
||||||
|
|||||||
+68
-163
@@ -2,38 +2,19 @@ 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 CaseInsensitiveDict, ConfigHandler
|
from patroni.postgresql.config import 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):
|
||||||
"""
|
"""
|
||||||
@@ -55,24 +36,17 @@ 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'
|
||||||
__REMAP_KEYS = {
|
|
||||||
'master_start_timeout': 'primary_start_timeout',
|
|
||||||
'master_stop_timeout': 'primary_stop_timeout'
|
|
||||||
}
|
|
||||||
__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,
|
||||||
'primary_start_timeout': 300,
|
'master_start_timeout': 300,
|
||||||
'primary_stop_timeout': 0,
|
|
||||||
'synchronous_mode': False,
|
'synchronous_mode': False,
|
||||||
'synchronous_mode_strict': False,
|
'synchronous_mode_strict': False,
|
||||||
'synchronous_node_count': 1,
|
|
||||||
'failsafe_mode': False,
|
|
||||||
'standby_cluster': {
|
'standby_cluster': {
|
||||||
'create_replica_methods': '',
|
'create_replica_methods': '',
|
||||||
'host': '',
|
'host': '',
|
||||||
@@ -85,34 +59,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, configfile, validator=default_validator):
|
def __init__(self):
|
||||||
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 = configfile and os.path.exists(configfile) and configfile
|
self._config_file = len(sys.argv) >= 2 and os.path.isfile(sys.argv[1]) and sys.argv[1]
|
||||||
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 validator:
|
if not self._local_configuration:
|
||||||
errors = validator(self._local_configuration)
|
print('Usage: {0} config.yml'.format(sys.argv[0]))
|
||||||
if errors:
|
print('\tPatroni may also read the configuration from the {0} environment variable'.
|
||||||
raise ConfigParseError("\n".join(errors))
|
format(self.PATRONI_CONFIG_VARIABLE))
|
||||||
|
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.get('postgresql', {}).get('data_dir', "")
|
self._data_dir = self.__effective_configuration['postgresql']['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
|
||||||
@@ -128,32 +102,12 @@ 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"""
|
||||||
config = self._load_config_path(self._config_file)
|
with open(self._config_file) as f:
|
||||||
patch_config(config, self.__environment_configuration)
|
config = yaml.safe_load(f)
|
||||||
return config
|
patch_config(config, self.__environment_configuration)
|
||||||
|
return config
|
||||||
|
|
||||||
def _load_cache(self):
|
def _load_cache(self):
|
||||||
if os.path.isfile(self._cache_file):
|
if os.path.isfile(self._cache_file):
|
||||||
@@ -204,19 +158,23 @@ 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):
|
def reload_local_configuration(self, dry_run=False):
|
||||||
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 local configuration items changed.')
|
logger.info('No configuration items changed, nothing to reload.')
|
||||||
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):
|
||||||
@@ -228,22 +186,18 @@ class Config(object):
|
|||||||
config = deepcopy(self.__DEFAULT_CONFIG)
|
config = deepcopy(self.__DEFAULT_CONFIG)
|
||||||
|
|
||||||
for name, value in dynamic_configuration.items():
|
for name, value in dynamic_configuration.items():
|
||||||
# allow copying master_start_timeout->primary_start_timeout when the latter isn't in dynamic_configuration
|
|
||||||
if name in self.__REMAP_KEYS and self.__REMAP_KEYS[name] not in dynamic_configuration:
|
|
||||||
name = self.__REMAP_KEYS[name]
|
|
||||||
if name == 'postgresql':
|
if name == 'postgresql':
|
||||||
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', 'proxy_address', 'listen',
|
elif name not in ('connect_address', 'listen', 'data_dir', 'pgpass', 'authentication'):
|
||||||
'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 overridden from DCS
|
elif name in config: # only variables present in __DEFAULT_CONFIG allowed to be overriden from DCS
|
||||||
if name in ('synchronous_mode', 'synchronous_mode_strict', 'failsafe_mode'):
|
if name in ('synchronous_mode', 'synchronous_mode_strict'):
|
||||||
config[name] = value
|
config[name] = value
|
||||||
else:
|
else:
|
||||||
config[name] = int(value)
|
config[name] = int(value)
|
||||||
@@ -254,7 +208,7 @@ class Config(object):
|
|||||||
ret = defaultdict(dict)
|
ret = defaultdict(dict)
|
||||||
|
|
||||||
def _popenv(name):
|
def _popenv(name):
|
||||||
return os.environ.pop(PATRONI_ENV_PREFIX + name.upper(), None)
|
return os.environ.pop(Config.PATRONI_ENV_PREFIX + name.upper(), None)
|
||||||
|
|
||||||
for param in ('name', 'namespace', 'scope'):
|
for param in ('name', 'namespace', 'scope'):
|
||||||
value = _popenv(param)
|
value = _popenv(param)
|
||||||
@@ -263,7 +217,7 @@ class Config(object):
|
|||||||
|
|
||||||
def _fix_log_env(name, oldname):
|
def _fix_log_env(name, oldname):
|
||||||
value = _popenv(oldname)
|
value = _popenv(oldname)
|
||||||
name = PATRONI_ENV_PREFIX + 'LOG_' + name.upper()
|
name = Config.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
|
||||||
|
|
||||||
@@ -276,45 +230,10 @@ class Config(object):
|
|||||||
if value:
|
if value:
|
||||||
ret[section][param] = value
|
ret[section][param] = value
|
||||||
|
|
||||||
_set_section_values('restapi', ['listen', 'connect_address', 'certfile', 'keyfile', 'keyfile_password',
|
_set_section_values('restapi', ['listen', 'connect_address', 'certfile', 'keyfile'])
|
||||||
'cafile', 'ciphers', 'verify_client', 'http_extra_headers',
|
_set_section_values('postgresql', ['listen', 'connect_address', 'config_dir', 'data_dir', 'pgpass', 'bin_dir'])
|
||||||
'https_extra_headers', 'allowlist', 'allowlist_include_members'])
|
_set_section_values('log', ['level', 'format', 'dateformat', 'max_queue_size',
|
||||||
_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('{'):
|
||||||
@@ -325,17 +244,15 @@ class Config(object):
|
|||||||
logger.exception('Exception when parsing dict %s', value)
|
logger.exception('Exception when parsing dict %s', value)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
for first, params in (('restapi', ('http_extra_headers', 'https_extra_headers')), ('log', ('loggers',))):
|
value = ret.get('log', {}).pop('loggers', None)
|
||||||
for second in params:
|
if value:
|
||||||
value = ret.get(first, {}).pop(second, None)
|
value = _parse_dict(value)
|
||||||
if value:
|
if value:
|
||||||
value = _parse_dict(value)
|
ret['log']['loggers'] = value
|
||||||
if value:
|
|
||||||
ret[first][second] = value
|
|
||||||
|
|
||||||
def _get_auth(name, params=None):
|
def _get_auth(name):
|
||||||
ret = {}
|
ret = {}
|
||||||
for param in params or _AUTH_ALLOWED_PARAMETERS[:2]:
|
for param in ('username', 'password'):
|
||||||
value = _popenv(name + '_' + param)
|
value = _popenv(name + '_' + param)
|
||||||
if value:
|
if value:
|
||||||
ret[param] = value
|
ret[param] = value
|
||||||
@@ -347,46 +264,47 @@ class Config(object):
|
|||||||
|
|
||||||
authentication = {}
|
authentication = {}
|
||||||
for user_type in ('replication', 'superuser', 'rewind'):
|
for user_type in ('replication', 'superuser', 'rewind'):
|
||||||
entry = _get_auth(user_type, _AUTH_ALLOWED_PARAMETERS)
|
entry = _get_auth(user_type)
|
||||||
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(PATRONI_ENV_PREFIX):
|
if param.startswith(Config.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', 'SRV_SUFFIX', 'URL', 'PROXY',
|
if suffix in ('HOST', 'HOSTS', 'PORT', 'USE_PROXIES', 'PROTOCOL', 'SRV', 'URL', 'PROXY',
|
||||||
'CACERT', 'CERT', 'KEY', 'VERIFY', 'TOKEN', 'CHECKS', 'DC', 'CONSISTENCY',
|
'CACERT', 'CERT', 'KEY', 'VERIFY', 'TOKEN', 'CHECKS', 'DC', 'CONSISTENCY',
|
||||||
'REGISTER_SERVICE', 'SERVICE_CHECK_INTERVAL', 'SERVICE_CHECK_TLS_SERVER_NAME',
|
'REGISTER_SERVICE', 'SERVICE_CHECK_INTERVAL', 'NAMESPACE', 'CONTEXT',
|
||||||
'SERVICE_TAGS', 'NAMESPACE', 'CONTEXT', 'USE_ENDPOINTS', 'SCOPE_LABEL', 'ROLE_LABEL',
|
'USE_ENDPOINTS', 'SCOPE_LABEL', 'ROLE_LABEL', 'POD_IP', 'PORTS', 'LABELS') and name:
|
||||||
'POD_IP', 'PORTS', 'LABELS', 'BYPASS_API_SERVICE', 'RETRIABLE_HTTP_CODES', 'KEY_PASSWORD',
|
|
||||||
'USE_SSL', 'SET_ACLS', 'GROUP', 'DATABASE') and name:
|
|
||||||
value = os.environ.pop(param)
|
value = os.environ.pop(param)
|
||||||
if name == 'CITUS':
|
if suffix == 'PORT':
|
||||||
if suffix == 'GROUP':
|
|
||||||
value = parse_int(value)
|
|
||||||
elif suffix != 'DATABASE':
|
|
||||||
continue
|
|
||||||
elif suffix == 'PORT':
|
|
||||||
value = value and parse_int(value)
|
value = value and parse_int(value)
|
||||||
elif suffix in ('HOSTS', 'PORTS', 'CHECKS', 'SERVICE_TAGS', 'RETRIABLE_HTTP_CODES'):
|
elif suffix in ('HOSTS', 'PORTS', 'CHECKS'):
|
||||||
value = value and _parse_list(value)
|
value = value and _parse_list(value)
|
||||||
elif suffix in ('LABELS', 'SET_ACLS'):
|
elif suffix == 'LABELS':
|
||||||
value = _parse_dict(value)
|
value = _parse_dict(value)
|
||||||
elif suffix in ('USE_PROXIES', 'REGISTER_SERVICE', 'USE_ENDPOINTS', 'BYPASS_API_SERVICE', 'VERIFY'):
|
elif suffix in ('USE_PROXIES', 'REGISTER_SERVICE'):
|
||||||
value = parse_bool(value)
|
value = parse_bool(value)
|
||||||
if value is not None:
|
if value:
|
||||||
ret[name.lower()][suffix.lower()] = value
|
ret[name.lower()][suffix.lower()] = value
|
||||||
for dcs in ('etcd', 'etcd3'):
|
if 'etcd' in ret:
|
||||||
if dcs in ret:
|
ret['etcd'].update(_get_auth('etcd'))
|
||||||
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(PATRONI_ENV_PREFIX):
|
if param.startswith(Config.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>'
|
||||||
@@ -406,11 +324,7 @@ class Config(object):
|
|||||||
def _build_effective_configuration(self, dynamic_configuration, local_configuration):
|
def _build_effective_configuration(self, dynamic_configuration, local_configuration):
|
||||||
config = self._safe_copy_dynamic_configuration(dynamic_configuration)
|
config = self._safe_copy_dynamic_configuration(dynamic_configuration)
|
||||||
for name, value in local_configuration.items():
|
for name, value in local_configuration.items():
|
||||||
if name == 'citus': # remove invalid citus configuration
|
if name == 'postgresql':
|
||||||
if isinstance(value, dict) and isinstance(value.get('group'), int)\
|
|
||||||
and isinstance(value.get('database'), str):
|
|
||||||
config[name] = value
|
|
||||||
elif name == 'postgresql':
|
|
||||||
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, True))
|
config['postgresql'][name].update(self._process_postgresql_parameters(value, True))
|
||||||
@@ -418,9 +332,14 @@ class Config(object):
|
|||||||
config['postgresql'][name] = deepcopy(value)
|
config['postgresql'][name] = deepcopy(value)
|
||||||
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 'restapi' in config and 'authentication' in config['restapi']:
|
if '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
|
||||||
@@ -439,30 +358,16 @@ 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']
|
||||||
|
|
||||||
# when bootstrapping the new Citus cluster (coordinator/worker) enable sync replication in global configuration
|
|
||||||
if 'citus' in config:
|
|
||||||
bootstrap = config.setdefault('bootstrap', {})
|
|
||||||
dcs = bootstrap.setdefault('dcs', {})
|
|
||||||
dcs.setdefault('synchronous_mode', True)
|
|
||||||
|
|
||||||
updated_fields = (
|
updated_fields = (
|
||||||
'name',
|
'name',
|
||||||
'scope',
|
'scope',
|
||||||
'retry_timeout',
|
'retry_timeout',
|
||||||
'synchronous_mode',
|
'synchronous_mode',
|
||||||
'synchronous_mode_strict',
|
'synchronous_mode_strict',
|
||||||
'synchronous_node_count',
|
|
||||||
'maximum_lag_on_syncnode',
|
|
||||||
'citus'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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})
|
||||||
|
|||||||
+433
-518
File diff suppressed because it is too large
Load Diff
@@ -1,185 +0,0 @@
|
|||||||
"""Daemon processes abstraction module.
|
|
||||||
|
|
||||||
This module implements abstraction classes and functions for creating and managing daemon processes in Patroni.
|
|
||||||
Currently it is only used for the main "Thread" of ``patroni`` and ``patroni_raft_controller`` commands.
|
|
||||||
"""
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import abc
|
|
||||||
import os
|
|
||||||
import signal
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from threading import Lock
|
|
||||||
from typing import Any, Optional, Type
|
|
||||||
|
|
||||||
from .config import Config
|
|
||||||
from .validator import Schema
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractPatroniDaemon(abc.ABC):
|
|
||||||
"""A Patroni daemon process.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
When inheriting from :class:`AbstractPatroniDaemon` you are expected to define the methods :func:`_run_cycle`
|
|
||||||
to determine what it should do in each execution cycle, and :func:`_shutdown` to determine what it should do
|
|
||||||
when shutting down.
|
|
||||||
|
|
||||||
:ivar logger: log handler used by this daemon.
|
|
||||||
:ivar config: configuration options for this daemon.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, config: Config) -> None:
|
|
||||||
"""Set up signal handlers, logging handler and configuration.
|
|
||||||
|
|
||||||
:param config: configuration options for this daemon.
|
|
||||||
"""
|
|
||||||
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, *_: Any) -> None:
|
|
||||||
"""Handle SIGHUP signals.
|
|
||||||
|
|
||||||
Flag the daemon as "SIGHUP received".
|
|
||||||
"""
|
|
||||||
self._received_sighup = True
|
|
||||||
|
|
||||||
def api_sigterm(self) -> bool:
|
|
||||||
"""Guarantee only a single SIGTERM is being processed.
|
|
||||||
|
|
||||||
Flag the daemon as "SIGTERM received" with a lock-based approach.
|
|
||||||
|
|
||||||
:returns: ``True`` if the daemon was flagged as "SIGTERM received".
|
|
||||||
"""
|
|
||||||
ret = False
|
|
||||||
with self._sigterm_lock:
|
|
||||||
if not self._received_sigterm:
|
|
||||||
self._received_sigterm = True
|
|
||||||
ret = True
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def sigterm_handler(self, *_: Any) -> None:
|
|
||||||
"""Handle SIGTERM signals.
|
|
||||||
|
|
||||||
Terminate the daemon process through :func:`api_sigterm`.
|
|
||||||
"""
|
|
||||||
if self.api_sigterm():
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
def setup_signal_handlers(self) -> None:
|
|
||||||
"""Set up daemon signal handlers.
|
|
||||||
|
|
||||||
Set up SIGHUP and SIGTERM signal handlers.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
SIGHUP is only handled in non-Windows environments.
|
|
||||||
"""
|
|
||||||
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) -> bool:
|
|
||||||
"""If daemon was signaled with SIGTERM."""
|
|
||||||
with self._sigterm_lock:
|
|
||||||
return self._received_sigterm
|
|
||||||
|
|
||||||
def reload_config(self, sighup: Optional[bool] = False, local: Optional[bool] = False) -> None:
|
|
||||||
"""Reload configuration.
|
|
||||||
|
|
||||||
:param sighup: if it is related to a SIGHUP signal.
|
|
||||||
The sighup parameter could be used in the method overridden in a child class.
|
|
||||||
:param local: will be ``True`` if there are changes in the local configuration file.
|
|
||||||
"""
|
|
||||||
if local:
|
|
||||||
self.logger.reload_config(self.config.get('log', {}))
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def _run_cycle(self) -> None:
|
|
||||||
"""Define what the daemon should do in each execution cycle.
|
|
||||||
|
|
||||||
Keep being called in the daemon's main loop until the daemon is eventually terminated.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def run(self) -> None:
|
|
||||||
"""Run the daemon process.
|
|
||||||
|
|
||||||
Start the logger thread and keep running execution cycles until a SIGTERM is eventually received. Also reload
|
|
||||||
configuration uppon receiving SIGHUP.
|
|
||||||
"""
|
|
||||||
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) -> None:
|
|
||||||
"""Define what the daemon should do when shutting down."""
|
|
||||||
|
|
||||||
def shutdown(self) -> None:
|
|
||||||
"""Shut the daemon down when a SIGTERM is received.
|
|
||||||
|
|
||||||
Shut down the daemon process and the logger thread.
|
|
||||||
"""
|
|
||||||
with self._sigterm_lock:
|
|
||||||
self._received_sigterm = True
|
|
||||||
self._shutdown()
|
|
||||||
self.logger.shutdown()
|
|
||||||
|
|
||||||
|
|
||||||
def abstract_main(cls: Type[AbstractPatroniDaemon], validator: Optional[Schema] = None) -> None:
|
|
||||||
"""Create the main entry point of a given daemon process.
|
|
||||||
|
|
||||||
Expose a basic argument parser, parse the command-line arguments, and run the given daemon process.
|
|
||||||
|
|
||||||
:param cls: a class that should inherit from :class:`AbstractPatroniDaemon`.
|
|
||||||
:param validator: used to validate the daemon configuration schema, if requested by the user through
|
|
||||||
``--validate-config`` CLI option.
|
|
||||||
"""
|
|
||||||
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()
|
|
||||||
+133
-397
@@ -1,5 +1,5 @@
|
|||||||
import abc
|
import abc
|
||||||
import dateutil.parser
|
import dateutil
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
@@ -7,21 +7,18 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import re
|
import re
|
||||||
|
import six
|
||||||
import sys
|
import sys
|
||||||
import time
|
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 threading import Event, Lock
|
from threading import Event, Lock
|
||||||
from typing import Any, Dict, List, Optional, Union
|
|
||||||
from urllib.parse import urlparse, urlunparse, parse_qsl
|
|
||||||
|
|
||||||
from ..exceptions import PatroniFatalException
|
|
||||||
from ..utils import deep_compare, parse_bool, uri
|
|
||||||
|
|
||||||
CITUS_COORDINATOR_GROUP_ID = 0
|
|
||||||
citus_group_re = re.compile('^(0|[1-9][0-9]*)$')
|
|
||||||
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__)
|
||||||
|
|
||||||
@@ -69,51 +66,33 @@ def dcs_modules():
|
|||||||
module_prefix = __package__ + '.'
|
module_prefix = __package__ + '.'
|
||||||
|
|
||||||
if getattr(sys, 'frozen', False):
|
if getattr(sys, 'frozen', False):
|
||||||
toc = set()
|
importer = pkgutil.get_importer(dcs_dirname)
|
||||||
# dcs_dirname may contain a dot, which causes pkgutil.iter_importers()
|
return [module for module in list(importer.toc) if module.startswith(module_prefix) and module.count('.') == 2]
|
||||||
# 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):
|
||||||
modules = dcs_modules()
|
available_implementations = set()
|
||||||
|
for module_name in dcs_modules():
|
||||||
for module_name in modules:
|
try:
|
||||||
name = module_name.split('.')[-1]
|
module = importlib.import_module(module_name)
|
||||||
if name in config: # we will try to import only modules which have configuration section in the config file
|
for name in filter(lambda name: not name.startswith('__'), dir(module)): # iterate through module content
|
||||||
try:
|
item = getattr(module, name)
|
||||||
module = importlib.import_module(module_name)
|
name = name.lower()
|
||||||
for key, item in module.__dict__.items(): # iterate through the module content
|
# 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})
|
||||||
# From citus section we only need "group" parameter, but will propagate everything just in case.
|
|
||||||
if isinstance(config.get('citus'), dict):
|
|
||||||
config[name].update(config['citus'])
|
|
||||||
return item(config[name])
|
return item(config[name])
|
||||||
except ImportError:
|
|
||||||
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:
|
except ImportError:
|
||||||
logger.info('Failed to import %s', module_name)
|
if not config.get('patronictl'):
|
||||||
raise PatroniFatalException("""Can not find suitable configuration of distributed configuration store
|
logger.info('Failed to import %s', module_name)
|
||||||
Available implementations: """ + ', '.join(sorted(set(available_implementations))))
|
raise PatroniException("""Can not find suitable configuration of distributed configuration store
|
||||||
|
Available implementations: """ + ', '.join(available_implementations))
|
||||||
|
|
||||||
|
|
||||||
class Member(namedtuple('Member', 'index,name,session,data')):
|
class Member(namedtuple('Member', 'index,name,session,data')):
|
||||||
@@ -143,8 +122,6 @@ 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)
|
||||||
@@ -152,10 +129,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
|
||||||
@@ -163,31 +140,28 @@ class Member(namedtuple('Member', 'index,name,session,data')):
|
|||||||
|
|
||||||
def conn_kwargs(self, auth=None):
|
def conn_kwargs(self, auth=None):
|
||||||
defaults = {
|
defaults = {
|
||||||
"host": None,
|
"host": "",
|
||||||
"port": None,
|
"port": "",
|
||||||
"dbname": None
|
"database": ""
|
||||||
}
|
}
|
||||||
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:
|
||||||
conn_url = self.conn_url
|
r = urlparse(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,
|
||||||
'dbname': r.path[1:]
|
'database': 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'] = ret.pop('username')
|
ret['user'] = auth['username']
|
||||||
|
if 'password' in auth:
|
||||||
|
ret['password'] = auth['password']
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -218,18 +192,10 @@ 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 member (typically a primary) for a standby cluster"""
|
""" Represents a remote master for a standby cluster
|
||||||
|
"""
|
||||||
def __new__(cls, name, data):
|
def __new__(cls, name, data):
|
||||||
return super(RemoteMember, cls).__new__(cls, None, name, None, data)
|
return super(RemoteMember, cls).__new__(cls, None, name, None, data)
|
||||||
|
|
||||||
@@ -266,23 +232,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.data.get('timeline')
|
return self.member.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.version
|
version = self.member.data.get('version')
|
||||||
# 1.5.6 is the last version which doesn't expose checkpoint_after_promote: false
|
if version:
|
||||||
if version and version > (1, 5, 6):
|
try:
|
||||||
return self.data.get('role') in ('master', 'primary') and 'checkpoint_after_promote' not in self.data
|
# 1.5.6 is the last version which doesn't expose checkpoint_after_promote: false
|
||||||
|
if tuple(map(int, version.split('.'))) > (1, 5, 6):
|
||||||
|
return self.member.data['role'] == 'master' and 'checkpoint_after_promote' not in self.member.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')):
|
||||||
@@ -357,25 +323,17 @@ 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: synchronous standby list (comma delimited) which are last synchronized to leader
|
:param sync_standby: standby that was last synchronized to leader
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_node(index: Union[str, int], value: Union[str, Dict[str, Any]]) -> 'SyncState':
|
def from_node(index, value):
|
||||||
"""
|
"""
|
||||||
>>> SyncState.from_node(1, None).leader is None
|
>>> SyncState.from_node(1, None).leader is None
|
||||||
True
|
True
|
||||||
@@ -390,39 +348,28 @@ class SyncState(namedtuple('SyncState', 'index,leader,sync_standby')):
|
|||||||
>>> SyncState.from_node(1, {"leader": "leader"}).leader == "leader"
|
>>> SyncState.from_node(1, {"leader": "leader"}).leader == "leader"
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
try:
|
if isinstance(value, dict):
|
||||||
if value and isinstance(value, str):
|
data = value
|
||||||
value = json.loads(value)
|
elif value:
|
||||||
if not isinstance(value, dict):
|
try:
|
||||||
return SyncState.empty(index)
|
data = json.loads(value)
|
||||||
return SyncState(index, value.get('leader'), value.get('sync_standby'))
|
if not isinstance(data, dict):
|
||||||
except (TypeError, ValueError):
|
data = {}
|
||||||
return SyncState.empty(index)
|
except (TypeError, ValueError):
|
||||||
|
data = {}
|
||||||
|
else:
|
||||||
|
data = {}
|
||||||
|
return SyncState(index, data.get('leader'), data.get('sync_standby'))
|
||||||
|
|
||||||
@staticmethod
|
def matches(self, name):
|
||||||
def empty(index: Optional[Union[str, int]] = '') -> 'SyncState':
|
"""
|
||||||
return SyncState(index, None, '')
|
Returns if a node name matches one of the nodes in the sync state
|
||||||
|
|
||||||
@property
|
>>> s = SyncState(1, 'foo', 'bar')
|
||||||
def is_empty(self) -> bool:
|
|
||||||
""":returns: True if /sync key doesn't have a leader"""
|
|
||||||
return self.leader is None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def members(self) -> List[str]:
|
|
||||||
""":returns: sync_standby as list"""
|
|
||||||
return list(filter(lambda a: a, [s.strip() for s in self.sync_standby.split(',')])) if self.sync_standby else []
|
|
||||||
|
|
||||||
def matches(self, name: str) -> bool:
|
|
||||||
""":returns: True if a node name matches one of the nodes in the sync state (including leader)
|
|
||||||
|
|
||||||
>>> 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)
|
||||||
@@ -430,7 +377,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.members
|
return name is not None and name in (self.leader, self.sync_standby)
|
||||||
|
|
||||||
|
|
||||||
class TimelineHistory(namedtuple('TimelineHistory', 'index,value,lines')):
|
class TimelineHistory(namedtuple('TimelineHistory', 'index,value,lines')):
|
||||||
@@ -452,40 +399,23 @@ 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_lsn,members,'
|
class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_leader_operation,members,failover,sync,history')):
|
||||||
'failover,sync,history,slots,failsafe,workers')):
|
|
||||||
|
|
||||||
"""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_lsn: int or long object containing position of last known leader LSN.
|
:param last_leader_operation: int or long object containing position of last known leader operation.
|
||||||
This value is stored in the `/status` key or `/optime/leader` (legacy) key
|
This value is stored in `/optime/leader` 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}
|
"""
|
||||||
:param failsafe: failsafe topology. Node is allowed to become the leader only if its name is found in this list.
|
|
||||||
:param workers: workers of the Citus cluster, optional. Format: {int(group): Cluster()}"""
|
|
||||||
|
|
||||||
def __new__(cls, *args):
|
|
||||||
# Make workers argument optional
|
|
||||||
if len(cls._fields) == len(args) + 1:
|
|
||||||
args = args + ({},)
|
|
||||||
return super(Cluster, cls).__new__(cls, *args)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def empty():
|
|
||||||
return Cluster(None, None, None, 0, [], None, SyncState.empty(), None, None, None)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def leader_name(self):
|
|
||||||
return self.leader and self.leader.name
|
|
||||||
|
|
||||||
def is_unlocked(self):
|
def is_unlocked(self):
|
||||||
return not self.leader_name
|
return not (self.leader and 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)
|
||||||
@@ -507,41 +437,20 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_lsn,members,'
|
|||||||
def is_synchronous_mode(self):
|
def is_synchronous_mode(self):
|
||||||
return self.check_mode('synchronous_mode')
|
return self.check_mode('synchronous_mode')
|
||||||
|
|
||||||
@property
|
def get_replication_slots(self, name, role):
|
||||||
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 primary, 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
|
||||||
# primary), or if replicatefrom destination member happens to be the current primary
|
# 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', 'primary', '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.__permanent_slots if use_slots and \
|
permanent_slots = (self.config and self.config.permanent_slots or {}).copy()
|
||||||
role in ('master', 'primary') 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 use_slots and
|
slot_members = [m.name for m in self.members if m.replicatefrom == name and m.name != self.leader.name]
|
||||||
m.replicatefrom == my_name and m.name != self.leader_name]
|
permanent_slots = {}
|
||||||
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}
|
||||||
|
|
||||||
@@ -555,82 +464,46 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_lsn,members,'
|
|||||||
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
|
||||||
|
|
||||||
value = deepcopy(value) if value else {'type': 'physical'}
|
if name in slots:
|
||||||
|
logger.error("Permanent replication slot {'%s': %s} is conflicting with" +
|
||||||
|
" physical replication slot for cluster member", name, value)
|
||||||
|
continue
|
||||||
|
|
||||||
|
value = deepcopy(value)
|
||||||
|
if not value:
|
||||||
|
value = {'type': 'physical'}
|
||||||
|
|
||||||
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':
|
if value['type'] == 'physical' or value['type'] == 'logical' \
|
||||||
# Don't try to create permanent physical replication slot for yourself
|
and value.get('database') and value.get('plugin'):
|
||||||
if name != slot_name_from_member_name(my_name):
|
slots[name] = value
|
||||||
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
|
|
||||||
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, my_name, nofailover, major_version=110000):
|
def has_permanent_logical_slots(self, name):
|
||||||
if major_version < 110000:
|
slots = self.get_replication_slots(name, 'master').values()
|
||||||
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, 0, None).timeline
|
>>> Cluster(0, 0, 0, 0, 0, 0, 0, 0).timeline
|
||||||
0
|
0
|
||||||
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[]'), 0, None).timeline
|
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[]')).timeline
|
||||||
1
|
1
|
||||||
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[["a"]]'), 0, None).timeline
|
>>> Cluster(0, 0, 0, 0, 0, 0, 0, TimelineHistory.from_node(1, '[["a"]]')).timeline
|
||||||
0
|
0
|
||||||
"""
|
"""
|
||||||
if self.history:
|
if self.history:
|
||||||
@@ -643,26 +516,9 @@ class Cluster(namedtuple('Cluster', 'initialize,config,leader,last_lsn,members,'
|
|||||||
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)
|
||||||
class ReturnFalseException(Exception):
|
class AbstractDCS(object):
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def catch_return_false_exception(func):
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
try:
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
except ReturnFalseException:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractDCS(abc.ABC):
|
|
||||||
|
|
||||||
_INITIALIZE = 'initialize'
|
_INITIALIZE = 'initialize'
|
||||||
_CONFIG = 'config'
|
_CONFIG = 'config'
|
||||||
@@ -671,10 +527,8 @@ class AbstractDCS(abc.ABC):
|
|||||||
_HISTORY = 'history'
|
_HISTORY = 'history'
|
||||||
_MEMBERS = 'members/'
|
_MEMBERS = 'members/'
|
||||||
_OPTIME = 'optime'
|
_OPTIME = 'optime'
|
||||||
_STATUS = 'status' # JSON, contains "leader_lsn" and confirmed_flush_lsn of logical "slots" on the leader
|
_LEADER_OPTIME = _OPTIME + '/' + _LEADER
|
||||||
_LEADER_OPTIME = _OPTIME + '/' + _LEADER # legacy
|
|
||||||
_SYNC = 'sync'
|
_SYNC = 'sync'
|
||||||
_FAILSAFE = 'failsafe'
|
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
"""
|
"""
|
||||||
@@ -683,25 +537,17 @@ class AbstractDCS(abc.ABC):
|
|||||||
"""
|
"""
|
||||||
self._name = config['name']
|
self._name = config['name']
|
||||||
self._base_path = re.sub('/+', '/', '/'.join(['', config.get('namespace', 'service'), config['scope']]))
|
self._base_path = re.sub('/+', '/', '/'.join(['', config.get('namespace', 'service'), config['scope']]))
|
||||||
self._citus_group = str(config['group']) if isinstance(config.get('group'), int) else None
|
|
||||||
self._set_loop_wait(config.get('loop_wait', 10))
|
self._set_loop_wait(config.get('loop_wait', 10))
|
||||||
|
|
||||||
self._ctl = bool(config.get('patronictl', False))
|
self._ctl = bool(config.get('patronictl', False))
|
||||||
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_lsn = ''
|
self._last_leader_operation = ''
|
||||||
self._last_seen = 0
|
|
||||||
self._last_status = {}
|
|
||||||
self._last_failsafe = {}
|
|
||||||
self.event = Event()
|
self.event = Event()
|
||||||
|
|
||||||
def client_path(self, path):
|
def client_path(self, path):
|
||||||
components = [self._base_path]
|
return '/'.join([self._base_path, path.lstrip('/')])
|
||||||
if self._citus_group:
|
|
||||||
components.append(self._citus_group)
|
|
||||||
components.append(path.lstrip('/'))
|
|
||||||
return '/'.join(components)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def initialize_path(self):
|
def initialize_path(self):
|
||||||
@@ -731,10 +577,6 @@ class AbstractDCS(abc.ABC):
|
|||||||
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)
|
||||||
@@ -743,10 +585,6 @@ class AbstractDCS(abc.ABC):
|
|||||||
def sync_path(self):
|
def sync_path(self):
|
||||||
return self.client_path(self._SYNC)
|
return self.client_path(self._SYNC)
|
||||||
|
|
||||||
@property
|
|
||||||
def failsafe_path(self):
|
|
||||||
return self.client_path(self._FAILSAFE)
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def set_ttl(self, ttl):
|
def set_ttl(self, ttl):
|
||||||
"""Set the new ttl value for leader key"""
|
"""Set the new ttl value for leader key"""
|
||||||
@@ -771,75 +609,23 @@ class AbstractDCS(abc.ABC):
|
|||||||
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 _cluster_loader(self, path):
|
def _load_cluster(self):
|
||||||
"""Load and build the `Cluster` object from DCS, which
|
"""Internally this method should build `Cluster` object which
|
||||||
represents a single Patroni cluster.
|
represents current state and topology of the cluster in DCS.
|
||||||
|
this method supposed to be called only by `get_cluster` method.
|
||||||
|
|
||||||
:param path: the path in DCS where to load Cluster(s) from.
|
raise `~DCSError` in case of communication or other problems with DCS.
|
||||||
:returns: `Cluster`"""
|
If the current node was running as a master and exception raised,
|
||||||
|
instance would be demoted."""
|
||||||
|
|
||||||
def _citus_cluster_loader(self, path):
|
def get_cluster(self):
|
||||||
"""Load and build `Cluster` onjects from DCS that represent all
|
|
||||||
Patroni clusters from a single Citus cluster.
|
|
||||||
|
|
||||||
:param path: the path in DCS where to load Cluster(s) from.
|
|
||||||
:returns: all Citus groups as `dict`, with group ids as keys"""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def _load_cluster(self, path, loader):
|
|
||||||
"""Internally this method should call the `loader` method that
|
|
||||||
will build `Cluster` object which represents current state and
|
|
||||||
topology of the cluster in DCS. This method supposed to be
|
|
||||||
called only by `get_cluster` method.
|
|
||||||
|
|
||||||
:param path: the path in DCS where to load Cluster(s) from.
|
|
||||||
:param loader: one of `_cluster_loader` or `_citus_cluster_loader`
|
|
||||||
:raise: `~DCSError` in case of communication problems with DCS.
|
|
||||||
If the current node was running as a primary and exception
|
|
||||||
raised, instance would be demoted."""
|
|
||||||
|
|
||||||
def _bypass_caches(self):
|
|
||||||
"""Used only in zookeeper"""
|
|
||||||
|
|
||||||
def is_citus_coordinator(self):
|
|
||||||
return self._citus_group == str(CITUS_COORDINATOR_GROUP_ID)
|
|
||||||
|
|
||||||
def get_citus_coordinator(self):
|
|
||||||
try:
|
try:
|
||||||
path = '{0}/{1}/'.format(self._base_path, CITUS_COORDINATOR_GROUP_ID)
|
cluster = self._load_cluster()
|
||||||
return self._load_cluster(path, self._cluster_loader)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Failed to load Citus coordinator cluster from %s: %r', self.__class__.__name__, e)
|
|
||||||
|
|
||||||
def _get_citus_cluster(self):
|
|
||||||
groups = self._load_cluster(self._base_path + '/', self._citus_cluster_loader)
|
|
||||||
if isinstance(groups, Cluster): # Zookeeper could return a cached version
|
|
||||||
cluster = groups
|
|
||||||
else:
|
|
||||||
assert isinstance(groups, dict)
|
|
||||||
cluster = groups.pop(CITUS_COORDINATOR_GROUP_ID, Cluster.empty())
|
|
||||||
cluster.workers.update(groups)
|
|
||||||
return cluster
|
|
||||||
|
|
||||||
def get_cluster(self, force=False):
|
|
||||||
if force:
|
|
||||||
self._bypass_caches()
|
|
||||||
try:
|
|
||||||
cluster = self._get_citus_cluster() if self.is_citus_coordinator()\
|
|
||||||
else self._load_cluster(self.client_path(''), self._cluster_loader)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
self.reset_cluster()
|
self.reset_cluster()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
self._last_seen = int(time.time())
|
|
||||||
self._last_status = {self._OPTIME: cluster.last_lsn, 'slots': cluster.slots}
|
|
||||||
self._last_failsafe = cluster.failsafe
|
|
||||||
|
|
||||||
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
|
||||||
@@ -856,88 +642,47 @@ class AbstractDCS(abc.ABC):
|
|||||||
self._cluster_valid_till = 0
|
self._cluster_valid_till = 0
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def _write_leader_optime(self, last_lsn):
|
def _write_leader_optime(self, last_operation):
|
||||||
"""write current WAL LSN into `/optime/leader` key in DCS
|
"""write current xlog location 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_lsn):
|
def write_leader_optime(self, last_operation):
|
||||||
self.write_status({self._OPTIME: last_lsn})
|
if self._last_leader_operation != last_operation and self._write_leader_optime(last_operation):
|
||||||
|
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
|
|
||||||
def _write_failsafe(self, value):
|
|
||||||
"""Write current cluster topology to DCS that will be used by failsafe mechanism (if enabled).
|
|
||||||
|
|
||||||
:param value: failsafe topology serialized in JSON format
|
|
||||||
:returns: `!True` on success."""
|
|
||||||
|
|
||||||
def write_failsafe(self, value):
|
|
||||||
if not (isinstance(self._last_failsafe, dict) and deep_compare(self._last_failsafe, value))\
|
|
||||||
and self._write_failsafe(json.dumps(value, separators=(',', ':'))):
|
|
||||||
self._last_failsafe = value
|
|
||||||
|
|
||||||
@property
|
|
||||||
def failsafe(self):
|
|
||||||
return self._last_failsafe
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
"""Update leader key (or session) ttl
|
"""Update leader key (or session) ttl
|
||||||
|
|
||||||
: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.
|
||||||
|
|
||||||
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."""
|
||||||
If update fails due to DCS not being accessible or because it is not able to
|
|
||||||
process requests (hopefuly temporary), the ~DCSError exception should be raised."""
|
|
||||||
|
|
||||||
def update_leader(self, last_lsn, slots=None, failsafe=None):
|
def update_leader(self, last_operation, access_is_restricted=False):
|
||||||
"""Update leader key (or session) ttl and optime/leader
|
"""Update leader key (or session) ttl and optime/leader
|
||||||
|
|
||||||
:param last_lsn: absolute WAL LSN in bytes
|
:param last_operation: absolute xlog location 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."""
|
||||||
|
|
||||||
ret = self._update_leader()
|
ret = self._update_leader()
|
||||||
if ret and last_lsn:
|
if ret and last_operation:
|
||||||
status = {self._OPTIME: last_lsn}
|
self.write_leader_optime(last_operation)
|
||||||
if slots:
|
|
||||||
status['slots'] = slots
|
|
||||||
self.write_status(status)
|
|
||||||
|
|
||||||
if ret and failsafe is not None:
|
|
||||||
self.write_failsafe(failsafe)
|
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def attempt_to_acquire_leader(self):
|
def attempt_to_acquire_leader(self, permanent=False):
|
||||||
"""Attempt to acquire leader lock
|
"""Attempt to acquire leader lock
|
||||||
This method should create `/leader` key with value=`~self._name`
|
This method should create `/leader` key with value=`~self._name`
|
||||||
|
:param permanent: if set to `!True`, the leader key will never expire.
|
||||||
|
Used in patronictl for the external master
|
||||||
:returns: `!True` if key has been created successfully.
|
:returns: `!True` if key has been created successfully.
|
||||||
|
|
||||||
Key must be created atomically. In case if key already exists it should not be
|
Key must be created atomically. In case if key already exists it should not be
|
||||||
overwritten and `!False` must be returned.
|
overwritten and `!False` must be returned"""
|
||||||
|
|
||||||
If key creation fails due to DCS not being accessible or because it is not able to
|
|
||||||
process requests (hopefuly temporary), the ~DCSError exception should be raised"""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def set_failover_value(self, value, index=None):
|
def set_failover_value(self, value, index=None):
|
||||||
@@ -960,13 +705,15 @@ class AbstractDCS(abc.ABC):
|
|||||||
"""Create or update `/config` key"""
|
"""Create or update `/config` key"""
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def touch_member(self, data):
|
def touch_member(self, data, permanent=False):
|
||||||
"""Update member key in DCS.
|
"""Update member key in DCS.
|
||||||
This method should create or update key with the name = '/members/' + `~self._name`
|
This method should create or update key with the name = '/members/' + `~self._name`
|
||||||
and value = data in a given DCS.
|
and value = data in a given DCS.
|
||||||
|
|
||||||
:param data: information about instance (including connection strings)
|
:param data: information about instance (including connection strings)
|
||||||
:param ttl: ttl for member key, optional parameter. If it is None `~self.member_ttl will be used`
|
:param ttl: ttl for member key, optional parameter. If it is None `~self.member_ttl will be used`
|
||||||
|
:param permanent: if set to `!True`, the member key will never expire.
|
||||||
|
Used in patronictl for the external master.
|
||||||
:returns: `!True` on success otherwise `!False`
|
:returns: `!True` on success otherwise `!False`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -988,19 +735,10 @@ class AbstractDCS(abc.ABC):
|
|||||||
otherwise it should return `!False`"""
|
otherwise it should return `!False`"""
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def _delete_leader(self):
|
def delete_leader(self):
|
||||||
"""Remove leader key from DCS.
|
"""Voluntarily 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 """
|
||||||
@@ -1011,10 +749,8 @@ class AbstractDCS(abc.ABC):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def sync_state(leader, sync_standby):
|
def sync_state(leader, sync_standby):
|
||||||
"""Build sync_state dict
|
"""Build sync_state dict"""
|
||||||
sync_standby dictionary key being kept for backward compatibility
|
return {'leader': leader, 'sync_standby': sync_standby}
|
||||||
"""
|
|
||||||
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)
|
||||||
@@ -1033,7 +769,7 @@ class AbstractDCS(abc.ABC):
|
|||||||
""""""
|
""""""
|
||||||
|
|
||||||
def watch(self, leader_index, timeout):
|
def watch(self, leader_index, timeout):
|
||||||
"""If the current node is a leader it should just sleep.
|
"""If the current node is a master it should just sleep.
|
||||||
Any other node should watch for changes of leader key with a given timeout
|
Any other node should watch for changes of leader key with a given timeout
|
||||||
|
|
||||||
:param leader_index: index of a leader key
|
:param leader_index: index of a leader key
|
||||||
@@ -1041,4 +777,4 @@ class AbstractDCS(abc.ABC):
|
|||||||
: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.is_set()
|
return self.event.isSet()
|
||||||
|
|||||||
+106
-234
@@ -8,16 +8,13 @@ import ssl
|
|||||||
import time
|
import time
|
||||||
import urllib3
|
import urllib3
|
||||||
|
|
||||||
from collections import defaultdict, namedtuple
|
|
||||||
from consul import ConsulException, NotFound, base
|
from consul import ConsulException, NotFound, base
|
||||||
from http.client import HTTPException
|
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 urllib.parse import urlencode, urlparse, quote
|
from six.moves.urllib.parse import urlencode, urlparse, quote
|
||||||
|
from six.moves.http_client import HTTPException
|
||||||
from . import AbstractDCS, Cluster, ClusterConfig, Failover, Leader, Member, SyncState,\
|
|
||||||
TimelineHistory, ReturnFalseException, catch_return_false_exception, citus_group_re
|
|
||||||
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__)
|
||||||
|
|
||||||
@@ -38,9 +35,6 @@ 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):
|
||||||
@@ -58,8 +52,9 @@ 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
|
||||||
kwargs['cert_reqs'] = ssl.CERT_REQUIRED if verify or ca_cert else ssl.CERT_NONE
|
if verify or ca_cert:
|
||||||
self.http = urllib3.PoolManager(num_pools=10, maxsize=10, **kwargs)
|
kwargs['cert_reqs'] = ssl.CERT_REQUIRED
|
||||||
|
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):
|
||||||
@@ -76,17 +71,16 @@ class HTTPClient(object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def response(response):
|
def response(response):
|
||||||
content = response.data
|
data = response.data.decode('utf-8')
|
||||||
body = content.decode('utf-8')
|
|
||||||
if response.status == 500:
|
if response.status == 500:
|
||||||
msg = '{0} {1}'.format(response.status, body)
|
msg = '{0} {1}'.format(response.status, data)
|
||||||
if body.startswith('Invalid Session TTL'):
|
if data.startswith('Invalid Session TTL'):
|
||||||
raise InvalidSessionTTL(msg)
|
raise InvalidSessionTTL(msg)
|
||||||
elif body.startswith('invalid session'):
|
elif data.startswith('invalid session'):
|
||||||
raise InvalidSession(msg)
|
raise InvalidSession(msg)
|
||||||
else:
|
else:
|
||||||
raise ConsulInternalError(msg)
|
raise ConsulInternalError(msg)
|
||||||
return Response(response.status, response.headers, body, content)
|
return base.Response(response.status, response.headers, data)
|
||||||
|
|
||||||
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 '')
|
||||||
@@ -95,7 +89,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='', headers=None):
|
def wrapper(callback, path, params=None, data=''):
|
||||||
# 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':
|
||||||
@@ -112,15 +106,13 @@ 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 slightly bigger value.
|
# response rather read timeout we will add to the timeout a sligtly 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
|
||||||
|
|
||||||
@@ -133,7 +125,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 http_connect(self, *args, **kwargs):
|
def 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
|
||||||
@@ -143,9 +135,6 @@ 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')
|
||||||
@@ -190,7 +179,6 @@ class Consul(AbstractDCS):
|
|||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
super(Consul, self).__init__(config)
|
super(Consul, self).__init__(config)
|
||||||
self._base_path = self._base_path[1:]
|
|
||||||
self._scope = config['scope']
|
self._scope = config['scope']
|
||||||
self._session = None
|
self._session = None
|
||||||
self.__do_not_watch = False
|
self.__do_not_watch = False
|
||||||
@@ -227,18 +215,16 @@ 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._set_service_name()
|
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)
|
||||||
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)
|
||||||
@@ -253,18 +239,7 @@ 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
|
||||||
@@ -273,7 +248,7 @@ class Consul(AbstractDCS):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def ttl(self):
|
def ttl(self):
|
||||||
return self._client.http.ttl * 2 # we multiply the value by 2 because it was divided in the `set_ttl()` method
|
return self._client.http.ttl
|
||||||
|
|
||||||
def set_retry_timeout(self, retry_timeout):
|
def set_retry_timeout(self, retry_timeout):
|
||||||
self._retry.deadline = retry_timeout
|
self._retry.deadline = retry_timeout
|
||||||
@@ -288,9 +263,9 @@ class Consul(AbstractDCS):
|
|||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('adjust_ttl')
|
logger.exception('adjust_ttl')
|
||||||
|
|
||||||
def _do_refresh_session(self, force=False):
|
def _do_refresh_session(self):
|
||||||
""":returns: `!True` if it had to create new session"""
|
""":returns: `!True` if it had to create new session"""
|
||||||
if not force and self._session and self._last_session_refresh + self._loop_wait > time.time():
|
if self._session and self._last_session_refresh + self._loop_wait > time.time():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if self._session:
|
if self._session:
|
||||||
@@ -319,126 +294,92 @@ class Consul(AbstractDCS):
|
|||||||
logger.exception('refresh_session')
|
logger.exception('refresh_session')
|
||||||
raise ConsulError('Failed to renew/create session')
|
raise ConsulError('Failed to renew/create session')
|
||||||
|
|
||||||
|
def client_path(self, path):
|
||||||
|
return super(Consul, self).client_path(path)[1:]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def member(node):
|
def member(node):
|
||||||
return Member.from_node(node['ModifyIndex'], os.path.basename(node['Key']), node.get('Session'), node['Value'])
|
return Member.from_node(node['ModifyIndex'], os.path.basename(node['Key']), node.get('Session'), node['Value'])
|
||||||
|
|
||||||
def _cluster_from_nodes(self, nodes):
|
def _load_cluster(self):
|
||||||
# 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['ModifyIndex'], config['Value'])
|
|
||||||
|
|
||||||
# get timeline history
|
|
||||||
history = nodes.get(self._HISTORY)
|
|
||||||
history = history and TimelineHistory.from_node(history['ModifyIndex'], history['Value'])
|
|
||||||
|
|
||||||
# get last known 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:
|
try:
|
||||||
last_lsn = int(last_lsn)
|
path = self.client_path('/')
|
||||||
except Exception:
|
_, results = self.retry(self._client.kv.get, path, recurse=True)
|
||||||
last_lsn = 0
|
|
||||||
|
|
||||||
# get list of members
|
if results is None:
|
||||||
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
raise NotFound
|
||||||
|
|
||||||
# get leader
|
nodes = {}
|
||||||
leader = nodes.get(self._LEADER)
|
for node in results:
|
||||||
|
|
||||||
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['ModifyIndex'], leader.get('Session'), member)
|
|
||||||
|
|
||||||
# failover key
|
|
||||||
failover = nodes.get(self._FAILOVER)
|
|
||||||
if failover:
|
|
||||||
failover = Failover.from_node(failover['ModifyIndex'], failover['Value'])
|
|
||||||
|
|
||||||
# get synchronization state
|
|
||||||
sync = nodes.get(self._SYNC)
|
|
||||||
sync = SyncState.from_node(sync and sync['ModifyIndex'], sync and sync['Value'])
|
|
||||||
|
|
||||||
# get failsafe topology
|
|
||||||
failsafe = nodes.get(self._FAILSAFE)
|
|
||||||
try:
|
|
||||||
failsafe = json.loads(failsafe['Value']) if failsafe else None
|
|
||||||
except Exception:
|
|
||||||
failsafe = None
|
|
||||||
|
|
||||||
return Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots, failsafe)
|
|
||||||
|
|
||||||
def _cluster_loader(self, path):
|
|
||||||
_, results = self.retry(self._client.kv.get, path, recurse=True)
|
|
||||||
if results is None:
|
|
||||||
raise NotFound
|
|
||||||
nodes = {}
|
|
||||||
for node in results:
|
|
||||||
node['Value'] = (node['Value'] or b'').decode('utf-8')
|
|
||||||
nodes[node['Key'][len(path):]] = node
|
|
||||||
|
|
||||||
return self._cluster_from_nodes(nodes)
|
|
||||||
|
|
||||||
def _citus_cluster_loader(self, path):
|
|
||||||
_, results = self.retry(self._client.kv.get, path, recurse=True)
|
|
||||||
clusters = defaultdict(dict)
|
|
||||||
for node in results or []:
|
|
||||||
key = node['Key'][len(path):].split('/', 1)
|
|
||||||
if len(key) == 2 and citus_group_re.match(key[0]):
|
|
||||||
node['Value'] = (node['Value'] or b'').decode('utf-8')
|
node['Value'] = (node['Value'] or b'').decode('utf-8')
|
||||||
clusters[int(key[0])][key[1]] = node
|
nodes[node['Key'][len(path):].lstrip('/')] = node
|
||||||
return {group: self._cluster_from_nodes(nodes) for group, nodes in clusters.items()}
|
|
||||||
|
|
||||||
def _load_cluster(self, path, loader):
|
# get initialize flag
|
||||||
try:
|
initialize = nodes.get(self._INITIALIZE)
|
||||||
return loader(path)
|
initialize = initialize and initialize['Value']
|
||||||
|
|
||||||
|
# get global dynamic configuration
|
||||||
|
config = nodes.get(self._CONFIG)
|
||||||
|
config = config and ClusterConfig.from_node(config['ModifyIndex'], config['Value'])
|
||||||
|
|
||||||
|
# get timeline history
|
||||||
|
history = nodes.get(self._HISTORY)
|
||||||
|
history = history and TimelineHistory.from_node(history['ModifyIndex'], history['Value'])
|
||||||
|
|
||||||
|
# get last leader operation
|
||||||
|
last_leader_operation = nodes.get(self._LEADER_OPTIME)
|
||||||
|
last_leader_operation = 0 if last_leader_operation is None else int(last_leader_operation['Value'])
|
||||||
|
|
||||||
|
# get list of members
|
||||||
|
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
||||||
|
|
||||||
|
# get leader
|
||||||
|
leader = nodes.get(self._LEADER)
|
||||||
|
if not self._ctl and leader and leader['Value'] == self._name \
|
||||||
|
and self._session != leader.get('Session', 'x'):
|
||||||
|
logger.info('I am leader but not owner of the session. Removing leader node')
|
||||||
|
self._client.kv.delete(self.leader_path, cas=leader['ModifyIndex'])
|
||||||
|
leader = None
|
||||||
|
|
||||||
|
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['ModifyIndex'], leader.get('Session'), member)
|
||||||
|
|
||||||
|
# failover key
|
||||||
|
failover = nodes.get(self._FAILOVER)
|
||||||
|
if failover:
|
||||||
|
failover = Failover.from_node(failover['ModifyIndex'], failover['Value'])
|
||||||
|
|
||||||
|
# get synchronization state
|
||||||
|
sync = nodes.get(self._SYNC)
|
||||||
|
sync = SyncState.from_node(sync and sync['ModifyIndex'], sync and sync['Value'])
|
||||||
|
|
||||||
|
return Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
||||||
except NotFound:
|
except NotFound:
|
||||||
return Cluster.empty()
|
return Cluster(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')
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
def touch_member(self, data):
|
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)
|
||||||
|
create_member = not permanent and self.refresh_session()
|
||||||
try:
|
|
||||||
create_member = 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:
|
||||||
self._client.kv.put(self.member_path, json.dumps(data, separators=(',', ':')), acquire=self._session)
|
args = {} if permanent else {'acquire': self._session}
|
||||||
|
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
|
||||||
@@ -447,11 +388,6 @@ 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)
|
||||||
@@ -473,32 +409,18 @@ 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)
|
|
||||||
if role == 'master':
|
|
||||||
tags.append('primary')
|
|
||||||
elif role == 'primary':
|
|
||||||
tags.append('master')
|
|
||||||
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': tags,
|
'tags': [role]
|
||||||
'enable_tag_override': True,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if state == 'stopped' or (not self._register_service and self._previous_loop_register_service):
|
if state == 'stopped':
|
||||||
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', 'primary', 'replica', 'standby-leader']:
|
|
||||||
if state != 'running':
|
if state != 'running':
|
||||||
return
|
return
|
||||||
return self.register_service(service_name, **params)
|
return self.register_service(service_name, **params)
|
||||||
@@ -516,39 +438,25 @@ class Consul(AbstractDCS):
|
|||||||
if old_data.get(key) != new_data[key]:
|
if old_data.get(key) != new_data[key]:
|
||||||
update = True
|
update = True
|
||||||
|
|
||||||
if (
|
if force or update:
|
||||||
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)
|
||||||
|
|
||||||
def _do_attempt_to_acquire_leader(self, retry):
|
@catch_consul_errors
|
||||||
|
def _do_attempt_to_acquire_leader(self, permanent):
|
||||||
try:
|
try:
|
||||||
return retry(self._client.kv.put, self.leader_path, self._name, acquire=self._session)
|
kwargs = {} if permanent else {'acquire': self._session}
|
||||||
|
return self.retry(self._client.kv.put, self.leader_path, self._name, **kwargs)
|
||||||
except InvalidSession:
|
except InvalidSession:
|
||||||
logger.error('Our session disappeared from Consul. Will try to get a new one and retry attempt')
|
|
||||||
self._session = None
|
self._session = None
|
||||||
retry.deadline = retry.stoptime - time.time()
|
logger.error('Our session disappeared from Consul. Will try to get a new one and retry attempt')
|
||||||
|
self.refresh_session()
|
||||||
|
return self.retry(self._client.kv.put, self.leader_path, self._name, acquire=self._session)
|
||||||
|
|
||||||
retry(self._do_refresh_session)
|
def attempt_to_acquire_leader(self, permanent=False):
|
||||||
|
if not self._session and not permanent:
|
||||||
|
self.refresh_session()
|
||||||
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
ret = self._do_attempt_to_acquire_leader(permanent)
|
||||||
if retry.deadline < 1:
|
|
||||||
raise ConsulError('_do_attempt_to_acquire_leader timeout')
|
|
||||||
|
|
||||||
return retry(self._client.kv.put, self.leader_path, self._name, acquire=self._session)
|
|
||||||
|
|
||||||
@catch_return_false_exception
|
|
||||||
def attempt_to_acquire_leader(self):
|
|
||||||
retry = self._retry.copy()
|
|
||||||
self._run_and_handle_exceptions(self._do_refresh_session, retry=retry)
|
|
||||||
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
|
||||||
if retry.deadline < 1:
|
|
||||||
raise ConsulError('attempt_to_acquire_leader timeout')
|
|
||||||
|
|
||||||
ret = self._run_and_handle_exceptions(self._do_attempt_to_acquire_leader, retry, retry=None)
|
|
||||||
if not ret:
|
if not ret:
|
||||||
logger.info('Could not take out TTL lock')
|
logger.info('Could not take out TTL lock')
|
||||||
|
|
||||||
@@ -566,50 +474,14 @@ 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_lsn):
|
def _write_leader_optime(self, last_operation):
|
||||||
return self._client.kv.put(self.leader_optime_path, last_lsn)
|
return self._client.kv.put(self.leader_optime_path, last_operation)
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
def _write_status(self, value):
|
|
||||||
return self._client.kv.put(self.status_path, value)
|
|
||||||
|
|
||||||
@catch_consul_errors
|
|
||||||
def _write_failsafe(self, value):
|
|
||||||
return self._client.kv.put(self.failsafe_path, value)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _run_and_handle_exceptions(method, *args, **kwargs):
|
|
||||||
retry = kwargs.pop('retry', None)
|
|
||||||
try:
|
|
||||||
return retry(method, *args, **kwargs) if retry else method(*args, **kwargs)
|
|
||||||
except (RetryFailedError, InvalidSession, HTTPException, HTTPError, socket.error, socket.timeout) as e:
|
|
||||||
raise ConsulError(e)
|
|
||||||
except ConsulException:
|
|
||||||
raise ReturnFalseException
|
|
||||||
|
|
||||||
@catch_return_false_exception
|
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
retry = self._retry.copy()
|
|
||||||
|
|
||||||
self._run_and_handle_exceptions(self._do_refresh_session, True, retry=retry)
|
|
||||||
|
|
||||||
if self._session:
|
if self._session:
|
||||||
cluster = self.cluster
|
self.retry(self._client.session.renew, self._session)
|
||||||
leader_session = cluster and isinstance(cluster.leader, Leader) and cluster.leader.session
|
self._last_session_refresh = time.time()
|
||||||
if leader_session != self._session:
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
|
||||||
if retry.deadline < 1:
|
|
||||||
raise ConsulError('update_leader timeout')
|
|
||||||
logger.warning('Recreating the leader key due to session mismatch')
|
|
||||||
if cluster.leader:
|
|
||||||
self._run_and_handle_exceptions(self._client.kv.delete, self.leader_path, cas=cluster.leader.index)
|
|
||||||
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
|
||||||
if retry.deadline < 0.5:
|
|
||||||
raise ConsulError('update_leader timeout')
|
|
||||||
self._run_and_handle_exceptions(self._client.kv.put, self.leader_path,
|
|
||||||
self._name, acquire=self._session)
|
|
||||||
|
|
||||||
return bool(self._session)
|
return bool(self._session)
|
||||||
|
|
||||||
@catch_consul_errors
|
@catch_consul_errors
|
||||||
@@ -630,7 +502,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)
|
||||||
|
|||||||
+213
-441
@@ -1,39 +1,30 @@
|
|||||||
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 socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from collections import defaultdict
|
|
||||||
from copy import deepcopy
|
|
||||||
from dns.exception import DNSException
|
from dns.exception import DNSException
|
||||||
from dns import resolver
|
from dns import resolver
|
||||||
from http.client import HTTPException
|
from patroni.dcs import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
||||||
from queue import Queue
|
from patroni.exceptions import DCSError
|
||||||
|
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.http_client import HTTPException
|
||||||
|
from six.moves.urllib_parse import urlparse
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from typing import List, Optional
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
from urllib3 import Timeout
|
|
||||||
from urllib3.exceptions import HTTPError, ReadTimeoutError, ProtocolError
|
|
||||||
|
|
||||||
from . import AbstractDCS, Cluster, ClusterConfig, Failover, Leader, Member, SyncState,\
|
|
||||||
TimelineHistory, ReturnFalseException, catch_return_false_exception, citus_group_re
|
|
||||||
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
|
||||||
|
|
||||||
@@ -74,19 +65,16 @@ 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 Exception as e:
|
except socket.gaierror:
|
||||||
logger.warning('failed to resolve host %s: %s', host, e)
|
logger.warning('failed to resolve host %s', host)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
class AbstractEtcdClientWithFailover(abc.ABC, etcd.Client):
|
class Client(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
|
||||||
@@ -94,105 +82,36 @@ class AbstractEtcdClientWithFailover(abc.ABC, 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(AbstractEtcdClientWithFailover, self).__init__(read_timeout=config['retry_timeout'], **args)
|
super(Client, 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._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 _calculate_timeouts(self, etcd_nodes, timeout=None):
|
def _build_request_parameters(self):
|
||||||
"""Calculate a request timeout and number of retries per single etcd node.
|
kwargs = {'headers': self._get_headers(), 'redirect': self.allow_redirect}
|
||||||
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."""
|
|
||||||
|
|
||||||
per_node_timeout = timeout = float(timeout or self.read_timeout)
|
# calculate the number of retries and timeout *per node*
|
||||||
|
# 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)
|
||||||
|
|
||||||
max_retries = 4 - min(etcd_nodes, 3)
|
# if etcd_nodes > 3:
|
||||||
per_node_retries = 1
|
# kwargs.update({'retries': 0, 'timeout': float(self.read_timeout)/etcd_nodes})
|
||||||
min_timeout = 1.0
|
# elif etcd_nodes > 1:
|
||||||
|
# kwargs.update({'retries': 1, 'timeout': self.read_timeout/2.0/etcd_nodes})
|
||||||
while etcd_nodes > 0:
|
# else:
|
||||||
per_node_timeout = float(timeout) / etcd_nodes
|
# kwargs.update({'retries': 2, 'timeout': self.read_timeout/3.0})
|
||||||
if per_node_timeout >= min_timeout:
|
kwargs['timeout'] = self.read_timeout/float(kwargs['retries'] + 1)/etcd_nodes
|
||||||
# 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
|
@property
|
||||||
def machines_cache(self):
|
def machines(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]
|
|
||||||
|
|
||||||
def _get_machines_list(self, machines_cache: List[str]) -> List[str]:
|
|
||||||
"""Gets list of members from Etcd cluster using API
|
|
||||||
|
|
||||||
:param machines_cache: initial list of Etcd members
|
|
||||||
:returns: list of clientURLs retrieved from Etcd cluster
|
|
||||||
:raises EtcdConnectionFailed: if failed"""
|
|
||||||
kwargs = self._prepare_get_members(len(machines_cache))
|
|
||||||
|
|
||||||
for base_uri in machines_cache:
|
|
||||||
try:
|
|
||||||
machines = list(set(self._get_members(base_uri, **kwargs)))
|
|
||||||
logger.debug("Retrieved list of machines: %s", machines)
|
|
||||||
if machines:
|
|
||||||
random.shuffle(machines)
|
|
||||||
if not self._use_proxies:
|
|
||||||
self._update_dns_cache(self._dns_resolver.resolve_async, machines)
|
|
||||||
return machines
|
|
||||||
except Exception as e:
|
|
||||||
self.http.clear()
|
|
||||||
logger.error("Failed to get list of machines from %s%s: %r", base_uri, self.version_prefix, e)
|
|
||||||
|
|
||||||
raise etcd.EtcdConnectionFailed('No more machines in the cluster')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def machines(self) -> List[str]:
|
|
||||||
"""Original `machines` method(property) of `etcd.Client` class raise exception
|
"""Original `machines` method(property) of `etcd.Client` class raise exception
|
||||||
when it failed to get list of etcd cluster members. This method is being called
|
when it failed to get list of etcd cluster members. This method is being called
|
||||||
only when request failed on one of the etcd members during `api_execute` call.
|
only when request failed on one of the etcd members during `api_execute` call.
|
||||||
@@ -201,94 +120,102 @@ class AbstractEtcdClientWithFailover(abc.ABC, etcd.Client):
|
|||||||
Later, during next `api_execute` call we will forcefully update machines_cache.
|
Later, during next `api_execute` call we will forcefully update machines_cache.
|
||||||
|
|
||||||
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."""
|
||||||
|
|
||||||
After the next refactoring the whole logic was moved to the _get_machines_list() method."""
|
kwargs = self._build_request_parameters()
|
||||||
|
|
||||||
return self._get_machines_list(self.machines_cache)
|
while True:
|
||||||
|
try:
|
||||||
|
response = self.http.request(self._MGET, self._base_uri + self.version_prefix + '/machines', **kwargs)
|
||||||
|
data = self._handle_server_response(response).data.decode('utf-8')
|
||||||
|
machines = [m.strip() for m in data.split(',') if m.strip()]
|
||||||
|
logger.debug("Retrieved list of machines: %s", machines)
|
||||||
|
if not machines:
|
||||||
|
raise etcd.EtcdException
|
||||||
|
random.shuffle(machines)
|
||||||
|
for url in machines:
|
||||||
|
r = urlparse(url)
|
||||||
|
port = r.port or (443 if r.scheme == 'https' else 80)
|
||||||
|
self._dns_resolver.resolve_async(r.hostname, port)
|
||||||
|
return machines
|
||||||
|
except Exception as e:
|
||||||
|
# We can't get the list of machines, if one server is in the
|
||||||
|
# machines cache, try on it
|
||||||
|
logger.error("Failed to get list of machines from %s%s: %r", self._base_uri, self.version_prefix, e)
|
||||||
|
if self._machines_cache:
|
||||||
|
self._base_uri = self._machines_cache.pop(0)
|
||||||
|
logger.info("Retrying on %s", self._base_uri)
|
||||||
|
elif self._update_machines_cache:
|
||||||
|
raise etcd.EtcdException("Could not get the list of servers, "
|
||||||
|
"maybe you provided the wrong "
|
||||||
|
"host(s) to connect to?")
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
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, retry, machines_cache, request_executor, method, path, fields=None, **kwargs):
|
def _do_http_request(self, request_executor, method, url, fields=None, **kwargs):
|
||||||
if fields is not None:
|
try:
|
||||||
kwargs['fields'] = fields
|
response = request_executor(method, url, fields=fields, **kwargs)
|
||||||
some_request_failed = False
|
response.data.decode('utf-8')
|
||||||
for i, base_uri in enumerate(machines_cache):
|
self._check_cluster_id(response)
|
||||||
if i > 0:
|
except (HTTPError, HTTPException, socket.error, socket.timeout) as e:
|
||||||
logger.info("Retrying on %s", base_uri)
|
if (isinstance(fields, dict) and fields.get("wait") == "true" and
|
||||||
try:
|
isinstance(e, ReadTimeoutError)):
|
||||||
response = request_executor(method, base_uri + path, **kwargs)
|
logger.debug("Watch timed out.")
|
||||||
response.data.decode('utf-8')
|
raise etcd.EtcdWatchTimedOut("Watch timed out: {0}".format(e), cause=e)
|
||||||
if some_request_failed:
|
logger.error("Request to server %s failed: %r", self._base_uri, e)
|
||||||
self.set_base_uri(base_uri)
|
logger.info("Reconnection allowed, looking for another server.")
|
||||||
self._refresh_machines_cache()
|
self._base_uri = self._next_server(cause=e)
|
||||||
return response
|
response = False
|
||||||
except (HTTPError, HTTPException, socket.error, socket.timeout) as e:
|
return response
|
||||||
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
|
|
||||||
isinstance(e, (ReadTimeoutError, ProtocolError))):
|
|
||||||
logger.debug("Watch timed out.")
|
|
||||||
raise etcd.EtcdWatchTimedOut("Watch timed out: {0}".format(e), cause=e)
|
|
||||||
logger.error("Request to server %s failed: %r", base_uri, e)
|
|
||||||
logger.info("Reconnection allowed, looking for another server.")
|
|
||||||
if not retry:
|
|
||||||
raise etcd.EtcdException('{0} {1} request failed'.format(method, path))
|
|
||||||
some_request_failed = True
|
|
||||||
|
|
||||||
raise etcd.EtcdConnectionFailed('No more machines in the cluster')
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def _prepare_request(self, kwargs, params=None, method=None):
|
|
||||||
"""returns: request_executor"""
|
|
||||||
|
|
||||||
def api_execute(self, path, method, params=None, timeout=None):
|
def api_execute(self, path, method, params=None, timeout=None):
|
||||||
retry = params.pop('retry', None) if isinstance(params, dict) else None
|
if not path.startswith('/'):
|
||||||
|
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()
|
||||||
|
|
||||||
machines_cache = self.machines_cache
|
kwargs.update(self._build_request_parameters())
|
||||||
etcd_nodes = len(machines_cache)
|
|
||||||
|
|
||||||
kwargs = self._prepare_common_parameters(etcd_nodes, timeout)
|
if timeout is not None:
|
||||||
request_executor = self._prepare_request(kwargs, params, method)
|
kwargs.update({'retries': 0, 'timeout': timeout})
|
||||||
|
|
||||||
while True:
|
response = False
|
||||||
try:
|
|
||||||
response = self._do_http_request(retry, machines_cache, request_executor, method, path, **kwargs)
|
try:
|
||||||
return self._handle_server_response(response)
|
some_request_failed = False
|
||||||
except etcd.EtcdWatchTimedOut:
|
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
|
raise
|
||||||
except etcd.EtcdConnectionFailed as ex:
|
return self._handle_server_response(response)
|
||||||
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):
|
||||||
@@ -297,26 +224,25 @@ class AbstractEtcdClientWithFailover(abc.ABC, etcd.Client):
|
|||||||
except DNSException:
|
except DNSException:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def _get_machines_cache_from_srv(self, srv, srv_suffix=None):
|
def _get_machines_cache_from_srv(self, srv):
|
||||||
"""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.status < 400:
|
if response.ok:
|
||||||
for member in json.loads(response.data.decode('utf-8')):
|
for member in response.json():
|
||||||
ret.extend(member['clientURLs'])
|
ret.extend(member['clientURLs'])
|
||||||
break
|
break
|
||||||
except Exception:
|
except RequestException:
|
||||||
logger.exception('GET %s', url)
|
logger.exception('GET %s', url)
|
||||||
else:
|
else:
|
||||||
ret.append(url)
|
ret.append(url)
|
||||||
@@ -341,7 +267,7 @@ class AbstractEtcdClientWithFailover(abc.ABC, 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'], self._config.get('srv_suffix'))
|
machines_cache = self._get_machines_cache_from_srv(self._config['srv'])
|
||||||
|
|
||||||
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'])
|
||||||
@@ -350,13 +276,6 @@ class AbstractEtcdClientWithFailover(abc.ABC, 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:
|
||||||
@@ -368,106 +287,40 @@ class AbstractEtcdClientWithFailover(abc.ABC, 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')
|
||||||
|
|
||||||
machines_cache = self._get_machines_cache_from_config()
|
self._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 machines_cache:
|
if not self._machines_cache:
|
||||||
raise etcd.EtcdException
|
raise etcd.EtcdException
|
||||||
|
|
||||||
# enforce resolving dns name,they might get new ips
|
# After filling up initial list of machines_cache we should ask etcd-cluster about actual list
|
||||||
self._update_dns_cache(self._dns_resolver.remove, machines_cache)
|
self._base_uri = self._next_server()
|
||||||
|
self._refresh_machines_cache()
|
||||||
# after filling up the initial list of machines_cache we should ask etcd-cluster about actual list
|
|
||||||
ret = self._refresh_machines_cache(machines_cache)
|
|
||||||
|
|
||||||
self._update_machines_cache = False
|
self._update_machines_cache = False
|
||||||
return ret
|
|
||||||
|
|
||||||
def _refresh_machines_cache(self, machines_cache: Optional[List[str]] = None) -> bool:
|
|
||||||
"""Get etcd cluster topology using Etcd API and put it to self._machines_cache
|
|
||||||
|
|
||||||
:param machines_cache: the list of nodes we want to run through executing API request
|
|
||||||
in addition to values stored in the self._machines_cache
|
|
||||||
:returns: `True` if self._machines_cache was updated with new values
|
|
||||||
:raises EtcdException: if failed to get topology and `machines_cache` was specified.
|
|
||||||
|
|
||||||
The self._machines_cache will not be updated if nodes from the list are
|
|
||||||
not accessible or if they are not returning correct results."""
|
|
||||||
|
|
||||||
if self._use_proxies:
|
|
||||||
value = self._get_machines_cache_from_config()
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
# we want to go through the list obtained from the config file + last known health topology
|
|
||||||
value = self._get_machines_list(list(set((machines_cache or []) + self.machines_cache)))
|
|
||||||
except etcd.EtcdConnectionFailed:
|
|
||||||
value = []
|
|
||||||
|
|
||||||
if value:
|
|
||||||
ret = set(self._machines_cache) != set(value)
|
|
||||||
self._machines_cache = value
|
|
||||||
elif machines_cache: # we are just starting or all nodes were not available at some point
|
|
||||||
raise etcd.EtcdException("Could not get the list of servers, "
|
|
||||||
"maybe you provided the wrong "
|
|
||||||
"host(s) to connect to?")
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
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()
|
||||||
return ret
|
|
||||||
|
|
||||||
def set_base_uri(self, value):
|
def _refresh_machines_cache(self):
|
||||||
if self._base_uri != value:
|
self._machines_cache = self._get_machines_cache_from_config() if self._use_proxies else self.machines
|
||||||
logger.info('Selected new etcd server %s', value)
|
if self._base_uri in self._machines_cache:
|
||||||
self._base_uri = value
|
self._machines_cache.remove(self._base_uri)
|
||||||
|
|
||||||
|
|
||||||
class EtcdClient(AbstractEtcdClientWithFailover):
|
class Etcd(AbstractDCS):
|
||||||
|
|
||||||
ERROR_CLS = EtcdError
|
def __init__(self, config):
|
||||||
|
super(Etcd, self).__init__(config)
|
||||||
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,
|
|
||||||
retry_exceptions=retry_errors_cls)
|
|
||||||
self._ttl = int(config.get('ttl') or 30)
|
self._ttl = int(config.get('ttl') or 30)
|
||||||
self._client = self.get_etcd_client(config, client_cls)
|
self._retry = Retry(deadline=config['retry_timeout'], max_delay=1, max_tries=-1,
|
||||||
|
retry_exceptions=(etcd.EtcdLeaderElectionInProgress,
|
||||||
|
etcd.EtcdWatcherCleared,
|
||||||
|
etcd.EtcdEventIndexCleared))
|
||||||
|
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):
|
||||||
retry = self._retry.copy()
|
return self._retry.copy()(*args, **kwargs)
|
||||||
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:
|
||||||
@@ -480,26 +333,22 @@ class AbstractEtcd(AbstractDCS):
|
|||||||
if isinstance(raise_ex, Exception):
|
if isinstance(raise_ex, Exception):
|
||||||
raise raise_ex
|
raise raise_ex
|
||||||
|
|
||||||
def _run_and_handle_exceptions(self, method, *args, **kwargs):
|
def catch_etcd_errors(func):
|
||||||
retry = kwargs.pop('retry', self.retry)
|
def wrapper(self, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
return retry(method, *args, **kwargs) if retry else method(*args, **kwargs)
|
retval = func(self, *args, **kwargs) is not None
|
||||||
except (RetryFailedError, etcd.EtcdConnectionFailed) as e:
|
self._has_failed = False
|
||||||
raise self._client.ERROR_CLS(e)
|
return retval
|
||||||
except etcd.EtcdException as e:
|
except (RetryFailedError, etcd.EtcdException) as e:
|
||||||
self._handle_exception(e)
|
self._handle_exception(e)
|
||||||
raise ReturnFalseException
|
return False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._handle_exception(e, raise_ex=self._client.ERROR_CLS('unexpected error'))
|
self._handle_exception(e, raise_ex=EtcdError('unexpected error'))
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def set_socket_options(sock, socket_options):
|
def get_etcd_client(config):
|
||||||
if socket_options:
|
|
||||||
for opt in socket_options:
|
|
||||||
sock.setsockopt(*opt)
|
|
||||||
|
|
||||||
def get_etcd_client(self, config, client_cls):
|
|
||||||
config = deepcopy(config)
|
|
||||||
if 'proxy' in config:
|
if 'proxy' in config:
|
||||||
config['use_proxies'] = True
|
config['use_proxies'] = True
|
||||||
config['url'] = config['proxy']
|
config['url'] = config['proxy']
|
||||||
@@ -513,13 +362,13 @@ class AbstractEtcd(AbstractDCS):
|
|||||||
default_port = config.pop('port', 2379)
|
default_port = config.pop('port', 2379)
|
||||||
protocol = config.get('protocol', 'http')
|
protocol = config.get('protocol', 'http')
|
||||||
|
|
||||||
if isinstance(hosts, str):
|
if isinstance(hosts, six.string_types):
|
||||||
hosts = hosts.split(',')
|
hosts = hosts.split(',')
|
||||||
|
|
||||||
config['hosts'] = []
|
config['hosts'] = []
|
||||||
for value in hosts:
|
for value in hosts:
|
||||||
if isinstance(value, str):
|
if isinstance(value, six.string_types):
|
||||||
config['hosts'].append(uri(protocol, split_host_port(value.strip(), default_port)))
|
config['hosts'].append(uri(protocol, split_host_port(value, 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
|
||||||
@@ -548,7 +397,9 @@ class AbstractEtcd(AbstractDCS):
|
|||||||
sock = None
|
sock = None
|
||||||
try:
|
try:
|
||||||
sock = socket.socket(af, socktype, proto)
|
sock = socket.socket(af, socktype, proto)
|
||||||
self.set_socket_options(sock, socket_options)
|
if 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:
|
||||||
@@ -572,7 +423,7 @@ class AbstractEtcd(AbstractDCS):
|
|||||||
client = None
|
client = None
|
||||||
while not client:
|
while not client:
|
||||||
try:
|
try:
|
||||||
client = client_cls(config, dns_resolver)
|
client = Client(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:
|
||||||
@@ -582,10 +433,9 @@ class AbstractEtcd(AbstractDCS):
|
|||||||
|
|
||||||
def set_ttl(self, ttl):
|
def set_ttl(self, ttl):
|
||||||
ttl = int(ttl)
|
ttl = int(ttl)
|
||||||
ret = self._ttl != ttl
|
self.__do_not_watch = 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):
|
||||||
@@ -595,140 +445,81 @@ class AbstractEtcd(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)
|
||||||
|
|
||||||
def _cluster_from_nodes(self, etcd_index, nodes):
|
def _load_cluster(self):
|
||||||
# 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.modifiedIndex, config.value)
|
|
||||||
|
|
||||||
# get timeline history
|
|
||||||
history = nodes.get(self._HISTORY)
|
|
||||||
history = history and TimelineHistory.from_node(history.modifiedIndex, 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 leader:
|
|
||||||
member = Member(-1, leader.value, None, {})
|
|
||||||
member = ([m for m in members if m.name == leader.value] or [member])[0]
|
|
||||||
index = etcd_index if etcd_index > leader.modifiedIndex else leader.modifiedIndex + 1
|
|
||||||
leader = Leader(index, leader.ttl, member)
|
|
||||||
|
|
||||||
# failover key
|
|
||||||
failover = nodes.get(self._FAILOVER)
|
|
||||||
if failover:
|
|
||||||
failover = Failover.from_node(failover.modifiedIndex, failover.value)
|
|
||||||
|
|
||||||
# get synchronization state
|
|
||||||
sync = nodes.get(self._SYNC)
|
|
||||||
sync = SyncState.from_node(sync and sync.modifiedIndex, sync and sync.value)
|
|
||||||
|
|
||||||
# get failsafe topology
|
|
||||||
failsafe = nodes.get(self._FAILSAFE)
|
|
||||||
try:
|
|
||||||
failsafe = json.loads(failsafe.value) if failsafe else None
|
|
||||||
except Exception:
|
|
||||||
failsafe = None
|
|
||||||
|
|
||||||
return Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots, failsafe)
|
|
||||||
|
|
||||||
def _cluster_loader(self, path):
|
|
||||||
result = self.retry(self._client.read, path, recursive=True)
|
|
||||||
nodes = {node.key[len(result.key):].lstrip('/'): node for node in result.leaves}
|
|
||||||
return self._cluster_from_nodes(result.etcd_index, nodes)
|
|
||||||
|
|
||||||
def _citus_cluster_loader(self, path):
|
|
||||||
clusters = defaultdict(dict)
|
|
||||||
result = self.retry(self._client.read, path, recursive=True)
|
|
||||||
for node in result.leaves:
|
|
||||||
key = node.key[len(result.key):].lstrip('/').split('/', 1)
|
|
||||||
if len(key) == 2 and citus_group_re.match(key[0]):
|
|
||||||
clusters[int(key[0])][key[1]] = node
|
|
||||||
return {group: self._cluster_from_nodes(result.etcd_index, nodes) for group, nodes in clusters.items()}
|
|
||||||
|
|
||||||
def _load_cluster(self, path, loader):
|
|
||||||
cluster = None
|
cluster = None
|
||||||
try:
|
try:
|
||||||
cluster = loader(path)
|
result = self.retry(self._client.read, self.client_path(''), recursive=True)
|
||||||
|
nodes = {node.key[len(result.key):].lstrip('/'): node for node in result.leaves}
|
||||||
|
|
||||||
|
# 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.modifiedIndex, config.value)
|
||||||
|
|
||||||
|
# get timeline history
|
||||||
|
history = nodes.get(self._HISTORY)
|
||||||
|
history = history and TimelineHistory.from_node(history.modifiedIndex, history.value)
|
||||||
|
|
||||||
|
# get last leader operation
|
||||||
|
last_leader_operation = nodes.get(self._LEADER_OPTIME)
|
||||||
|
last_leader_operation = 0 if last_leader_operation is None else int(last_leader_operation.value)
|
||||||
|
|
||||||
|
# get list of members
|
||||||
|
members = [self.member(n) for k, n in nodes.items() if k.startswith(self._MEMBERS) and k.count('/') == 1]
|
||||||
|
|
||||||
|
# get leader
|
||||||
|
leader = nodes.get(self._LEADER)
|
||||||
|
if leader:
|
||||||
|
member = Member(-1, leader.value, None, {})
|
||||||
|
member = ([m for m in members if m.name == leader.value] or [member])[0]
|
||||||
|
index = result.etcd_index if result.etcd_index > leader.modifiedIndex else leader.modifiedIndex + 1
|
||||||
|
leader = Leader(index, leader.ttl, member)
|
||||||
|
|
||||||
|
# failover key
|
||||||
|
failover = nodes.get(self._FAILOVER)
|
||||||
|
if failover:
|
||||||
|
failover = Failover.from_node(failover.modifiedIndex, failover.value)
|
||||||
|
|
||||||
|
# get synchronization state
|
||||||
|
sync = nodes.get(self._SYNC)
|
||||||
|
sync = SyncState.from_node(sync and sync.modifiedIndex, sync and sync.value)
|
||||||
|
|
||||||
|
cluster = Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
||||||
except etcd.EtcdKeyNotFound:
|
except etcd.EtcdKeyNotFound:
|
||||||
cluster = Cluster.empty()
|
cluster = Cluster(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
|
||||||
return cluster
|
return cluster
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def touch_member(self, data):
|
def touch_member(self, data, permanent=False):
|
||||||
data = json.dumps(data, separators=(',', ':'))
|
data = json.dumps(data, separators=(',', ':'))
|
||||||
return self._client.set(self.member_path, data, self._ttl)
|
return self._client.set(self.member_path, data, None if permanent else self._ttl)
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def take_leader(self):
|
def take_leader(self):
|
||||||
return self.retry(self._client.write, self.leader_path, self._name, ttl=self._ttl)
|
return self.retry(self._client.set, self.leader_path, self._name, self._ttl)
|
||||||
|
|
||||||
def _do_attempt_to_acquire_leader(self):
|
def attempt_to_acquire_leader(self, permanent=False):
|
||||||
try:
|
try:
|
||||||
return bool(self.retry(self._client.write, self.leader_path, self._name, ttl=self._ttl, prevExist=False))
|
return bool(self.retry(self._client.write,
|
||||||
|
self.leader_path,
|
||||||
|
self._name,
|
||||||
|
ttl=None if permanent else self._ttl,
|
||||||
|
prevExist=False))
|
||||||
except etcd.EtcdAlreadyExist:
|
except etcd.EtcdAlreadyExist:
|
||||||
logger.info('Could not take out TTL lock')
|
logger.info('Could not take out TTL lock')
|
||||||
return False
|
except (RetryFailedError, etcd.EtcdException):
|
||||||
|
pass
|
||||||
@catch_return_false_exception
|
return False
|
||||||
def attempt_to_acquire_leader(self):
|
|
||||||
return self._run_and_handle_exceptions(self._do_attempt_to_acquire_leader, retry=None)
|
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def set_failover_value(self, value, index=None):
|
def set_failover_value(self, value, index=None):
|
||||||
@@ -739,34 +530,19 @@ class Etcd(AbstractEtcd):
|
|||||||
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_lsn):
|
def _write_leader_optime(self, last_operation):
|
||||||
return self._client.set(self.leader_optime_path, last_lsn)
|
return self._client.set(self.leader_optime_path, last_operation)
|
||||||
|
|
||||||
@catch_etcd_errors
|
@catch_etcd_errors
|
||||||
def _write_status(self, value):
|
|
||||||
return self._client.set(self.status_path, value)
|
|
||||||
|
|
||||||
def _do_update_leader(self):
|
|
||||||
try:
|
|
||||||
return self.retry(self._client.write, self.leader_path, self._name,
|
|
||||||
prevValue=self._name, ttl=self._ttl) is not None
|
|
||||||
except etcd.EtcdKeyNotFound:
|
|
||||||
return self._do_attempt_to_acquire_leader()
|
|
||||||
|
|
||||||
@catch_etcd_errors
|
|
||||||
def _write_failsafe(self, value):
|
|
||||||
return self._client.set(self.failsafe_path, value)
|
|
||||||
|
|
||||||
@catch_return_false_exception
|
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
return self._run_and_handle_exceptions(self._do_update_leader, retry=None)
|
return self.retry(self._client.test_and_set, self.leader_path, self._name, self._name, 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
|
||||||
@@ -799,14 +575,13 @@ class Etcd(AbstractEtcd):
|
|||||||
|
|
||||||
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:
|
||||||
result = self._client.watch(self.leader_path, index=leader_index, timeout=timeout + 0.5)
|
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
|
||||||
@@ -821,6 +596,3 @@ class Etcd(AbstractEtcd):
|
|||||||
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
|
|
||||||
|
|||||||
@@ -1,876 +0,0 @@
|
|||||||
from __future__ import absolute_import
|
|
||||||
import base64
|
|
||||||
import etcd
|
|
||||||
import json
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
import socket
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
import urllib3
|
|
||||||
|
|
||||||
from collections import defaultdict
|
|
||||||
from threading import Condition, Lock, Thread
|
|
||||||
from urllib3.exceptions import ReadTimeoutError, ProtocolError
|
|
||||||
|
|
||||||
from . import ClusterConfig, Cluster, Failover, Leader, Member, SyncState,\
|
|
||||||
TimelineHistory, ReturnFalseException, catch_return_false_exception, citus_group_re
|
|
||||||
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/commits/main/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)
|
|
||||||
|
|
||||||
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, path):
|
|
||||||
if self._kv_cache and path.startswith(self._etcd3.cluster_prefix):
|
|
||||||
with self._kv_cache.condition:
|
|
||||||
self._wait_cache(self._etcd3._retry.deadline)
|
|
||||||
ret = self._kv_cache.copy()
|
|
||||||
else:
|
|
||||||
ret = self._etcd3.retry(self.prefix, path).get('kvs', [])
|
|
||||||
for node in ret:
|
|
||||||
node.update({'key': base64_decode(node['key']),
|
|
||||||
'value': base64_decode(node.get('value', '')),
|
|
||||||
'lease': node.get('lease')})
|
|
||||||
return ret
|
|
||||||
|
|
||||||
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, force=False, retry=None):
|
|
||||||
if not force and 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._base_path + '/' if self.is_citus_coordinator() else self.client_path('')
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def member(node):
|
|
||||||
return Member.from_node(node['mod_revision'], os.path.basename(node['key']), node['lease'], node['value'])
|
|
||||||
|
|
||||||
def _cluster_from_nodes(self, nodes):
|
|
||||||
# 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'])
|
|
||||||
|
|
||||||
# get failsafe topology
|
|
||||||
failsafe = nodes.get(self._FAILSAFE)
|
|
||||||
try:
|
|
||||||
failsafe = json.loads(failsafe['value']) if failsafe else None
|
|
||||||
except Exception:
|
|
||||||
failsafe = None
|
|
||||||
|
|
||||||
return Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots, failsafe)
|
|
||||||
|
|
||||||
def _cluster_loader(self, path):
|
|
||||||
nodes = {node['key'][len(path):]: node
|
|
||||||
for node in self._client.get_cluster(path)
|
|
||||||
if node['key'].startswith(path)}
|
|
||||||
return self._cluster_from_nodes(nodes)
|
|
||||||
|
|
||||||
def _citus_cluster_loader(self, path):
|
|
||||||
clusters = defaultdict(dict)
|
|
||||||
path = self._base_path + '/'
|
|
||||||
for node in self._client.get_cluster(path):
|
|
||||||
key = node['key'][len(path):].split('/', 1)
|
|
||||||
if len(key) == 2 and citus_group_re.match(key[0]):
|
|
||||||
clusters[int(key[0])][key[1]] = node
|
|
||||||
return {group: self._cluster_from_nodes(nodes) for group, nodes in clusters.items()}
|
|
||||||
|
|
||||||
def _load_cluster(self, path, loader):
|
|
||||||
cluster = None
|
|
||||||
try:
|
|
||||||
cluster = loader(path)
|
|
||||||
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):
|
|
||||||
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, 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)
|
|
||||||
|
|
||||||
def _do_attempt_to_acquire_leader(self, retry):
|
|
||||||
def _retry(*args, **kwargs):
|
|
||||||
kwargs['retry'] = retry
|
|
||||||
return retry(*args, **kwargs)
|
|
||||||
|
|
||||||
try:
|
|
||||||
return _retry(self._client.put, self.leader_path, self._name, self._lease, 0)
|
|
||||||
except LeaseNotFound:
|
|
||||||
logger.error('Our lease disappeared from Etcd. Will try to get a new one and retry attempt')
|
|
||||||
self._lease = None
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
|
||||||
|
|
||||||
_retry(self._do_refresh_lease)
|
|
||||||
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
|
||||||
if retry.deadline < 1:
|
|
||||||
raise Etcd3Error('_do_attempt_to_acquire_leader timeout')
|
|
||||||
|
|
||||||
return _retry(self._client.put, self.leader_path, self._name, self._lease, 0)
|
|
||||||
|
|
||||||
@catch_return_false_exception
|
|
||||||
def attempt_to_acquire_leader(self):
|
|
||||||
retry = self._retry.copy()
|
|
||||||
|
|
||||||
def _retry(*args, **kwargs):
|
|
||||||
kwargs['retry'] = retry
|
|
||||||
return retry(*args, **kwargs)
|
|
||||||
|
|
||||||
self._run_and_handle_exceptions(self._do_refresh_lease, retry=_retry)
|
|
||||||
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
|
||||||
if retry.deadline < 1:
|
|
||||||
raise Etcd3Error('attempt_to_acquire_leader timeout')
|
|
||||||
|
|
||||||
ret = self._run_and_handle_exceptions(self._do_attempt_to_acquire_leader, retry, retry=None)
|
|
||||||
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 _write_failsafe(self, value):
|
|
||||||
return self._client.put(self.failsafe_path, value)
|
|
||||||
|
|
||||||
@catch_return_false_exception
|
|
||||||
def _update_leader(self):
|
|
||||||
retry = self._retry.copy()
|
|
||||||
|
|
||||||
def _retry(*args, **kwargs):
|
|
||||||
kwargs['retry'] = retry
|
|
||||||
return retry(*args, **kwargs)
|
|
||||||
|
|
||||||
self._run_and_handle_exceptions(self._do_refresh_lease, True, retry=_retry)
|
|
||||||
|
|
||||||
if self._lease:
|
|
||||||
cluster = self.cluster
|
|
||||||
leader_lease = cluster and isinstance(cluster.leader, Leader) and cluster.leader.session
|
|
||||||
if leader_lease != self._lease:
|
|
||||||
retry.deadline = retry.stoptime - time.time()
|
|
||||||
if retry.deadline < 1:
|
|
||||||
raise Etcd3Error('update_leader timeout')
|
|
||||||
|
|
||||||
try:
|
|
||||||
self._run_and_handle_exceptions(self._client.put, self.leader_path,
|
|
||||||
self._name, self._lease, retry=_retry)
|
|
||||||
except ReturnFalseException:
|
|
||||||
pass
|
|
||||||
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.client_path(''))
|
|
||||||
|
|
||||||
@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()
|
|
||||||
+13
-11
@@ -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__)
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ class ExhibitorEnsembleProvider(object):
|
|||||||
self._uri_path = uri_path
|
self._uri_path = uri_path
|
||||||
self._poll_interval = poll_interval
|
self._poll_interval = poll_interval
|
||||||
self._exhibitors = hosts
|
self._exhibitors = hosts
|
||||||
self._boot_exhibitors = hosts
|
self._master_exhibitors = hosts
|
||||||
self._zookeeper_hosts = ''
|
self._zookeeper_hosts = ''
|
||||||
self._next_poll = None
|
self._next_poll = None
|
||||||
while not self.poll():
|
while not self.poll():
|
||||||
@@ -32,7 +32,7 @@ class ExhibitorEnsembleProvider(object):
|
|||||||
|
|
||||||
json = self._query_exhibitors(self._exhibitors)
|
json = self._query_exhibitors(self._exhibitors)
|
||||||
if not json:
|
if not json:
|
||||||
json = self._query_exhibitors(self._boot_exhibitors)
|
json = self._query_exhibitors(self._master_exhibitors)
|
||||||
|
|
||||||
if isinstance(json, dict) and 'servers' in json and 'port' in json:
|
if isinstance(json, dict) and 'servers' in json and 'port' in json:
|
||||||
self._next_poll = time.time() + self._poll_interval
|
self._next_poll = time.time() + self._poll_interval
|
||||||
@@ -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 json.loads(response.data.decode('utf-8'))
|
return response.json()
|
||||||
except Exception:
|
except RequestException:
|
||||||
logging.debug('Request to %s failed', host)
|
pass
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -64,9 +64,11 @@ class Exhibitor(ZooKeeper):
|
|||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
interval = config.get('poll_interval', 300)
|
interval = config.get('poll_interval', 300)
|
||||||
self._ensemble_provider = ExhibitorEnsembleProvider(config['hosts'], config['port'], poll_interval=interval)
|
self._ensemble_provider = ExhibitorEnsembleProvider(config['hosts'], config['port'], poll_interval=interval)
|
||||||
super(Exhibitor, self).__init__({**config, 'hosts': self._ensemble_provider.zookeeper_hosts})
|
config = config.copy()
|
||||||
|
config['hosts'] = self._ensemble_provider.zookeeper_hosts
|
||||||
|
super(Exhibitor, self).__init__(config)
|
||||||
|
|
||||||
def _load_cluster(self, path, loader):
|
def _load_cluster(self):
|
||||||
if self._ensemble_provider.poll():
|
if self._ensemble_provider.poll():
|
||||||
self._client.set_hosts(self._ensemble_provider.zookeeper_hosts)
|
self._client.set_hosts(self._ensemble_provider.zookeeper_hosts)
|
||||||
return super(Exhibitor, self)._load_cluster(path, loader)
|
return super(Exhibitor, self)._load_cluster()
|
||||||
|
|||||||
+203
-1045
File diff suppressed because it is too large
Load Diff
@@ -1,459 +0,0 @@
|
|||||||
import json
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
|
|
||||||
from collections import defaultdict
|
|
||||||
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, citus_group_re
|
|
||||||
from ..exceptions import DCSError
|
|
||||||
from ..utils import validate_directory
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class RaftError(DCSError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
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:
|
|
||||||
raise RaftError('timeout')
|
|
||||||
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, handle_raft_error=True, **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
|
|
||||||
try:
|
|
||||||
return self.retry(self._set, key, value, **kwargs)
|
|
||||||
except RaftError:
|
|
||||||
if not handle_raft_error:
|
|
||||||
raise
|
|
||||||
return False
|
|
||||||
|
|
||||||
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
|
|
||||||
try:
|
|
||||||
return self.retry(self._delete, key, recursive=recursive, **kwargs)
|
|
||||||
except RaftError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
@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 _cluster_from_nodes(self, nodes):
|
|
||||||
# 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'])
|
|
||||||
|
|
||||||
# get failsafe topology
|
|
||||||
failsafe = nodes.get(self._FAILSAFE)
|
|
||||||
try:
|
|
||||||
failsafe = json.loads(failsafe['value']) if failsafe else None
|
|
||||||
except Exception:
|
|
||||||
failsafe = None
|
|
||||||
|
|
||||||
return Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots, failsafe)
|
|
||||||
|
|
||||||
def _cluster_loader(self, path):
|
|
||||||
response = self._sync_obj.get(path, recursive=True)
|
|
||||||
if not response:
|
|
||||||
return Cluster.empty()
|
|
||||||
nodes = {key[len(path):]: value for key, value in response.items()}
|
|
||||||
return self._cluster_from_nodes(nodes)
|
|
||||||
|
|
||||||
def _citus_cluster_loader(self, path):
|
|
||||||
clusters = defaultdict(dict)
|
|
||||||
response = self._sync_obj.get(path, recursive=True)
|
|
||||||
for key, value in response.items():
|
|
||||||
key = key[len(path):].split('/', 1)
|
|
||||||
if len(key) == 2 and citus_group_re.match(key[0]):
|
|
||||||
clusters[int(key[0])][key[1]] = value
|
|
||||||
return {group: self._cluster_from_nodes(nodes) for group, nodes in clusters.items()}
|
|
||||||
|
|
||||||
def _load_cluster(self, path, loader):
|
|
||||||
return loader(path)
|
|
||||||
|
|
||||||
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 _write_failsafe(self, value):
|
|
||||||
return self._sync_obj.set(self.failsafe_path, value, timeout=1)
|
|
||||||
|
|
||||||
def _update_leader(self):
|
|
||||||
ret = self._sync_obj.set(self.leader_path, self._name, ttl=self._ttl,
|
|
||||||
handle_raft_error=False, 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):
|
|
||||||
return self._sync_obj.set(self.leader_path, self._name, ttl=self._ttl, handle_raft_error=False, prevExist=False)
|
|
||||||
|
|
||||||
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):
|
|
||||||
data = json.dumps(data, separators=(',', ':'))
|
|
||||||
return self._sync_obj.set(self.member_path, data, 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()
|
|
||||||
+66
-210
@@ -4,15 +4,11 @@ import select
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from kazoo.client import KazooClient, KazooState, KazooRetry
|
from kazoo.client import KazooClient, KazooState, KazooRetry
|
||||||
from kazoo.exceptions import ConnectionClosedError, NoNodeError, NodeExistsError, SessionExpiredError
|
from kazoo.exceptions import NoNodeError, NodeExistsError
|
||||||
from kazoo.handlers.threading import SequentialThreadingHandler
|
from kazoo.handlers.threading import SequentialThreadingHandler
|
||||||
from kazoo.protocol.states import KeeperState
|
from patroni.dcs import AbstractDCS, ClusterConfig, Cluster, Failover, Leader, Member, SyncState, TimelineHistory
|
||||||
from kazoo.retry import RetryFailedError
|
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, citus_group_re
|
|
||||||
from ..exceptions import DCSError
|
|
||||||
from ..utils import deep_compare
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -51,35 +47,13 @@ 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 (TypeError, ValueError) as e:
|
except ValueError as e:
|
||||||
raise select.error(9, str(e))
|
raise 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):
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
@@ -89,39 +63,20 @@ class ZooKeeper(AbstractDCS):
|
|||||||
if isinstance(hosts, list):
|
if isinstance(hosts, list):
|
||||||
hosts = ','.join(hosts)
|
hosts = ','.join(hosts)
|
||||||
|
|
||||||
mapping = {'use_ssl': 'use_ssl', 'verify': 'verify_certs', 'cacert': 'ca',
|
self._client = KazooClient(hosts, handler=PatroniSequentialThreadingHandler(config['retry_timeout']),
|
||||||
'cert': 'certfile', 'key': 'keyfile', 'key_password': 'keyfile_password'}
|
timeout=config['ttl'], connection_retry=KazooRetry(max_delay=1, max_tries=-1,
|
||||||
kwargs = {v: config[k] for k, v in mapping.items() if k in config}
|
sleep_func=time.sleep), command_retry=KazooRetry(deadline=config['retry_timeout'],
|
||||||
|
max_delay=1, max_tries=-1, sleep_func=time.sleep))
|
||||||
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,
|
|
||||||
sleep_func=time.sleep), command_retry=KazooRetry(max_delay=1, max_tries=-1,
|
|
||||||
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, *args):
|
def _kazoo_connect(self, host, port):
|
||||||
"""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
|
||||||
@@ -133,24 +88,16 @@ 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(*args)
|
ret = self._orig_kazoo_connect(host, port)
|
||||||
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
|
||||||
if not event or event.state != KazooState.CONNECTED or event.path.startswith(self.client_path('')):
|
self.event.set()
|
||||||
self.status_watcher(event)
|
|
||||||
|
|
||||||
def members_watcher(self, event):
|
|
||||||
self._fetch_cluster = True
|
|
||||||
|
|
||||||
def reload_config(self, config):
|
def reload_config(self, config):
|
||||||
self.set_retry_timeout(config['retry_timeout'])
|
self.set_retry_timeout(config['retry_timeout'])
|
||||||
@@ -167,14 +114,13 @@ 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(config['ttl']) and loop_wait_changed:
|
if not self.set_ttl(int(config['ttl'] * 1000)) 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()
|
||||||
@@ -182,7 +128,7 @@ class ZooKeeper(AbstractDCS):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def ttl(self):
|
def ttl(self):
|
||||||
return self._client._session_timeout / 1000.0
|
return self._client._session_timeout
|
||||||
|
|
||||||
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
|
||||||
@@ -195,30 +141,6 @@ class ZooKeeper(AbstractDCS):
|
|||||||
except NoNodeError:
|
except NoNodeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_status(self, path, leader):
|
|
||||||
watch = self.status_watcher if not leader or leader.name != self._name else None
|
|
||||||
|
|
||||||
status = self.get_node(path + self._STATUS, 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(path + self._LEADER_OPTIME, 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)
|
||||||
@@ -229,103 +151,78 @@ class ZooKeeper(AbstractDCS):
|
|||||||
except NoNodeError:
|
except NoNodeError:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def load_members(self, path):
|
def load_members(self, sync_standby):
|
||||||
members = []
|
members = []
|
||||||
for member in self.get_children(path + self._MEMBERS, self.cluster_watcher):
|
for member in self.get_children(self.members_path, self.cluster_watcher):
|
||||||
data = self.get_node(path + self._MEMBERS + member)
|
watch = member == sync_standby and self.cluster_watcher or None
|
||||||
|
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
|
||||||
|
|
||||||
def _cluster_loader(self, path):
|
def _inner_load_cluster(self):
|
||||||
self._fetch_cluster = False
|
self._fetch_cluster = False
|
||||||
self.event.clear()
|
self.event.clear()
|
||||||
nodes = set(self.get_children(path, self.cluster_watcher))
|
nodes = set(self.get_children(self.client_path(''), self.cluster_watcher))
|
||||||
if not nodes:
|
if not nodes:
|
||||||
self._fetch_cluster = True
|
self._fetch_cluster = True
|
||||||
|
|
||||||
# get initialize flag
|
# get initialize flag
|
||||||
initialize = (self.get_node(path + self._INITIALIZE) or [None])[0] if self._INITIALIZE in nodes else None
|
initialize = (self.get_node(self.initialize_path) or [None])[0] if self._INITIALIZE in nodes else None
|
||||||
|
|
||||||
# get global dynamic configuration
|
# get global dynamic configuration
|
||||||
config = self.get_node(path + self._CONFIG, watch=self.cluster_watcher) if self._CONFIG in nodes else None
|
config = self.get_node(self.config_path, watch=self.cluster_watcher) if self._CONFIG in nodes else None
|
||||||
config = config and ClusterConfig.from_node(config[1].version, config[0], config[1].mzxid)
|
config = config and ClusterConfig.from_node(config[1].version, config[0], config[1].mzxid)
|
||||||
|
|
||||||
# get timeline history
|
# get timeline history
|
||||||
history = self.get_node(path + self._HISTORY, 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(path + self._SYNC, 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
|
||||||
members = self.load_members(path) if self._MEMBERS[:-1] in nodes else []
|
sync_standby = sync.leader == self._name and sync.sync_standby or None
|
||||||
|
members = self.load_members(sync_standby) if self._MEMBERS[:-1] in nodes else []
|
||||||
|
|
||||||
# get leader
|
# get leader
|
||||||
leader = self.get_node(path + self._LEADER) if self._LEADER in nodes else None
|
leader = self.get_node(self.leader_path) if self._LEADER in nodes else None
|
||||||
if leader:
|
if leader:
|
||||||
member = Member(-1, leader[0], None, {})
|
client_id = self._client.client_id
|
||||||
member = ([m for m in members if m.name == leader[0]] or [member])[0]
|
if not self._ctl and leader[0] == self._name and client_id is not None \
|
||||||
leader = Leader(leader[1].version, leader[1].ephemeralOwner, member)
|
and client_id[0] != leader[1].ephemeralOwner:
|
||||||
self._fetch_cluster = member.index == -1
|
logger.info('I am leader but not owner of the session. Removing leader node')
|
||||||
|
self._client.delete(self.leader_path)
|
||||||
|
leader = None
|
||||||
|
|
||||||
# get last known leader lsn and slots
|
if leader:
|
||||||
last_lsn, slots = self.get_status(path, leader)
|
member = Member(-1, leader[0], None, {})
|
||||||
|
member = ([m for m in members if m.name == leader[0]] or [member])[0]
|
||||||
|
leader = Leader(leader[1].version, leader[1].ephemeralOwner, member)
|
||||||
|
self._fetch_cluster = member.index == -1
|
||||||
|
|
||||||
# failover key
|
# failover key
|
||||||
failover = self.get_node(path + self._FAILOVER, 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])
|
||||||
|
|
||||||
# get failsafe topology
|
return Cluster(initialize, config, leader, last_leader_operation, members, failover, sync, history)
|
||||||
failsafe = self.get_node(path + self._FAILSAFE, watch=self.cluster_watcher) if self._FAILSAFE in nodes else None
|
|
||||||
try:
|
|
||||||
failsafe = json.loads(failsafe[0]) if failsafe else None
|
|
||||||
except Exception:
|
|
||||||
failsafe = None
|
|
||||||
|
|
||||||
return Cluster(initialize, config, leader, last_lsn, members, failover, sync, history, slots, failsafe)
|
def _load_cluster(self):
|
||||||
|
cluster = self.cluster
|
||||||
def _citus_cluster_loader(self, path):
|
|
||||||
fetch_cluster = False
|
|
||||||
ret = {}
|
|
||||||
for node in self.get_children(path, self.cluster_watcher):
|
|
||||||
if citus_group_re.match(node):
|
|
||||||
ret[int(node)] = self._cluster_loader(path + node + '/')
|
|
||||||
fetch_cluster = fetch_cluster or self._fetch_cluster
|
|
||||||
self._fetch_cluster = fetch_cluster
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def _load_cluster(self, path, loader):
|
|
||||||
cluster = self.cluster if path == self._base_path + '/' else None
|
|
||||||
if self._fetch_cluster or cluster is None:
|
if self._fetch_cluster or cluster is None:
|
||||||
try:
|
try:
|
||||||
cluster = self._client.retry(loader, path)
|
cluster = self._client.retry(self._inner_load_cluster)
|
||||||
except Exception:
|
except Exception:
|
||||||
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(self.client_path(''), cluster.leader)
|
|
||||||
self.event.clear()
|
|
||||||
cluster = list(cluster)
|
|
||||||
cluster[3] = last_lsn
|
|
||||||
cluster[8] = slots
|
|
||||||
cluster = Cluster(*cluster)
|
|
||||||
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:
|
||||||
@@ -337,18 +234,11 @@ class ZooKeeper(AbstractDCS):
|
|||||||
logger.exception('Failed to create %s', path)
|
logger.exception('Failed to create %s', path)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def attempt_to_acquire_leader(self):
|
def attempt_to_acquire_leader(self, permanent=False):
|
||||||
try:
|
ret = self._create(self.leader_path, self._name.encode('utf-8'), retry=True, ephemeral=not permanent)
|
||||||
self._client.retry(self._client.create, self.leader_path, self._name.encode('utf-8'),
|
if not ret:
|
||||||
makepath=True, ephemeral=True)
|
logger.info('Could not take out TTL lock')
|
||||||
return True
|
return ret
|
||||||
except (ConnectionClosedError, RetryFailedError) as e:
|
|
||||||
raise ZooKeeperError(e)
|
|
||||||
except Exception as e:
|
|
||||||
if not isinstance(e, NodeExistsError):
|
|
||||||
logger.error('Failed to create %s: %r', self.leader_path, e)
|
|
||||||
logger.info('Could not take out TTL lock')
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _set_or_create(self, key, value, index=None, retry=False, do_not_create_empty=False):
|
def _set_or_create(self, key, value, index=None, retry=False, do_not_create_empty=False):
|
||||||
value = value.encode('utf-8')
|
value = value.encode('utf-8')
|
||||||
@@ -380,17 +270,14 @@ class ZooKeeper(AbstractDCS):
|
|||||||
return self._create(self.initialize_path, sysid, retry=True) if create_new \
|
return self._create(self.initialize_path, sysid, retry=True) if create_new \
|
||||||
else self._client.retry(self._client.set, self.initialize_path, sysid)
|
else self._client.retry(self._client.set, self.initialize_path, sysid)
|
||||||
|
|
||||||
def touch_member(self, data):
|
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)
|
||||||
member_data = self.__last_member_data or member and member.data
|
encoded_data = json.dumps(data, separators=(',', ':')).encode('utf-8')
|
||||||
# We want to notify leader if some important fields in the member key changed by removing ZNode
|
|
||||||
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('state') == data.get('state') or
|
member.data.get('version') == data.get('version') and
|
||||||
'running' not in (member_data.get('state'), data.get('state'))) and
|
member.data.get('checkpoint_after_promote') == data.get('checkpoint_after_promote'))):
|
||||||
member_data.get('version') == data.get('version') and
|
|
||||||
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:
|
||||||
@@ -399,14 +286,13 @@ 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, ephemeral=True).get(timeout=1)
|
self._client.create_async(self.member_path, encoded_data, makepath=True,
|
||||||
self.__last_member_data = data
|
ephemeral=not permanent).get(timeout=1)
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not isinstance(e, NodeExistsError):
|
if not isinstance(e, NodeExistsError):
|
||||||
@@ -414,7 +300,6 @@ 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')
|
||||||
@@ -424,41 +309,13 @@ 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_lsn):
|
def _write_leader_optime(self, last_operation):
|
||||||
return self._set_or_create(self.leader_optime_path, last_lsn)
|
return self._set_or_create(self.leader_optime_path, last_operation)
|
||||||
|
|
||||||
def _write_status(self, value):
|
|
||||||
return self._set_or_create(self.status_path, value)
|
|
||||||
|
|
||||||
def _write_failsafe(self, value):
|
|
||||||
return self._set_or_create(self.failsafe_path, value)
|
|
||||||
|
|
||||||
def _update_leader(self):
|
def _update_leader(self):
|
||||||
cluster = self.cluster
|
|
||||||
session = cluster and isinstance(cluster.leader, Leader) and cluster.leader.session
|
|
||||||
if self._client.client_id and self._client.client_id[0] != session:
|
|
||||||
logger.warning('Recreating the leader ZNode due to ownership mismatch')
|
|
||||||
try:
|
|
||||||
self._client.retry(self._client.delete, self.leader_path)
|
|
||||||
except NoNodeError:
|
|
||||||
pass
|
|
||||||
except (ConnectionClosedError, RetryFailedError) as e:
|
|
||||||
raise ZooKeeperError(e)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Failed to remove %s: %r', self.leader_path, e)
|
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
|
||||||
self._client.retry(self._client.create, self.leader_path,
|
|
||||||
self._name.encode('utf-8'), makepath=True, ephemeral=True)
|
|
||||||
except (ConnectionClosedError, RetryFailedError) as e:
|
|
||||||
raise ZooKeeperError(e)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Failed to create %s: %r', self.leader_path, e)
|
|
||||||
return False
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _delete_leader(self):
|
def delete_leader(self):
|
||||||
self._client.restart()
|
self._client.restart()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -489,7 +346,6 @@ 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):
|
||||||
ret = super(ZooKeeper, self).watch(leader_index, timeout + 0.5)
|
if super(ZooKeeper, self).watch(leader_index, timeout):
|
||||||
if ret and not self._fetch_status:
|
|
||||||
self._fetch_cluster = True
|
self._fetch_cluster = True
|
||||||
return ret or self._fetch_cluster
|
return self._fetch_cluster
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user