Merge pull request #29 from zalando/patroni

Rename governor into patroni
This commit is contained in:
Alexander Kukushkin
2015-07-08 11:24:31 +02:00
9 changed files with 32 additions and 32 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2015 Compose
Copyright (c) 2015 Compose, Zalando SE
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+5 -5
View File
@@ -51,21 +51,21 @@ class RestApiHandler(BaseHTTPRequestHandler):
}
except (psycopg2.OperationalError, psycopg2.InterfaceError):
logger.exception('get_postgresql_status')
return {'running': self.server.governor.postgresql.is_running()}
return {'running': self.server.patroni.postgresql.is_running()}
class RestApiServer(ThreadingMixIn, HTTPServer, Thread):
def __init__(self, governor, config):
self.connection_string = 'http://{}/governor'.format(config.get('connect_address', None) or config['listen'])
def __init__(self, patroni, config):
self.connection_string = 'http://{}/patroni'.format(config.get('connect_address', None) or config['listen'])
host, port = config['listen'].split(':')
HTTPServer.__init__(self, (host, int(port)), RestApiHandler)
Thread.__init__(self, target=self.serve_forever)
self.governor = governor
self.patroni = patroni
self.daemon = True
def query(self, sql, *params):
cursor = self.governor.postgresql.connection().cursor()
cursor = self.patroni.postgresql.connection().cursor()
cursor.execute(sql, params)
ret = [r for r in cursor]
cursor.close()
+1 -1
View File
@@ -21,7 +21,7 @@ def parseurl(url):
'host': r.hostname,
'port': r.port or 5432,
'database': r.path[1:],
'fallback_application_name': 'Governor',
'fallback_application_name': 'Patroni',
'connect_timeout': 3,
'options': '-c statement_timeout=2000',
}
+7 -7
View File
@@ -14,7 +14,7 @@ from helpers.utils import setup_signal_handlers, sleep
from helpers.zookeeper import ZooKeeper
class Governor:
class Patroni:
def __init__(self, config):
self.nap_time = config['loop_wait']
@@ -107,16 +107,16 @@ def main():
with open(sys.argv[1], 'r') as f:
config = yaml.load(f)
governor = Governor(config)
patroni = Patroni(config)
try:
governor.initialize()
governor.run()
patroni.initialize()
patroni.run()
except KeyboardInterrupt:
pass
finally:
governor.touch_member(governor.shutdown_member_ttl) # schedule member removal
governor.postgresql.stop()
governor.ha.dcs.delete_leader()
patroni.touch_member(patroni.shutdown_member_ttl) # schedule member removal
patroni.postgresql.stop()
patroni.ha.dcs.delete_leader()
if __name__ == '__main__':
+5 -5
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""
Setup file for governor
Setup file for patroni
"""
import sys
@@ -13,14 +13,14 @@ from setuptools.command.test import test as TestCommand
from setuptools import setup
if sys.version_info < (2, 7, 0):
sys.stderr.write('FATAL: governor needs to be run with Python 2.7+\n')
sys.stderr.write('FATAL: patroni needs to be run with Python 2.7+\n')
sys.exit(1)
__location__ = os.path.join(os.getcwd(), os.path.dirname(inspect.getfile(inspect.currentframe())))
NAME = 'governor'
MAIN_PACKAGE = 'governor.py'
NAME = 'patroni'
MAIN_PACKAGE = 'patroni.py'
HELPERS = 'helpers'
VERSION = '0.1'
DESCRIPTION = 'A Template for PostgreSQL HA with etcd'
@@ -118,7 +118,7 @@ def setup_package():
version=version,
description=DESCRIPTION,
license=LICENSE,
keywords='etcd governor postgresql postgres ha',
keywords='etcd governor patroni postgresql postgres ha zookeeper',
long_description=read('README.md'),
classifiers=CLASSIFIERS,
test_suite='tests',
+2 -2
View File
@@ -24,7 +24,7 @@ class MockPostgresql:
return True
class MockGovernor:
class MockPatroni:
def __init__(self):
self.postgresql = MockPostgresql()
@@ -42,7 +42,7 @@ class MockRequest:
class MockRestApiServer(RestApiServer):
def __init__(self, Handler, path, *args):
self.governor = MockGovernor()
self.patroni = MockPatroni()
if len(args) > 0:
self.query = args[0]
Handler(MockRequest(path), ('0.0.0.0', 8080), self)
+2 -2
View File
@@ -46,11 +46,11 @@ def requests_get(url, **kwargs):
elif url.startswith('http://local'):
raise requests.exceptions.RequestException()
elif url.startswith('http://remote') or url.startswith('http://127.0.0.1') or url.startswith('http://error'):
response.content = '{"action":"get","node":{"key":"/service/batman5","dir":true,"nodes":[{"key":"/service/batman5/initialize","value":"postgresql0","modifiedIndex":1582,"createdIndex":1582},{"key":"/service/batman5/leader","value":"postgresql1","expiration":"2015-05-15T09:11:00.037397538Z","ttl":21,"modifiedIndex":20728,"createdIndex":20434},{"key":"/service/batman5/optime","dir":true,"nodes":[{"key":"/service/batman5/optime/leader","value":"2164261704","modifiedIndex":20729,"createdIndex":20729}],"modifiedIndex":20437,"createdIndex":20437},{"key":"/service/batman5/members","dir":true,"nodes":[{"key":"/service/batman5/members/postgresql1","value":"postgres://replicator:[email protected]:5434/postgres?application_name=http://127.0.0.1:8009/governor","expiration":"2015-05-15T09:10:59.949384522Z","ttl":21,"modifiedIndex":20727,"createdIndex":20727},{"key":"/service/batman5/members/postgresql0","value":"postgres://replicator:[email protected]:5433/postgres?application_name=http://127.0.0.1:8008/governor","expiration":"2015-05-15T09:11:09.611860899Z","ttl":30,"modifiedIndex":20730,"createdIndex":20730}],"modifiedIndex":1581,"createdIndex":1581}],"modifiedIndex":1581,"createdIndex":1581}}'
response.content = '{"action":"get","node":{"key":"/service/batman5","dir":true,"nodes":[{"key":"/service/batman5/initialize","value":"postgresql0","modifiedIndex":1582,"createdIndex":1582},{"key":"/service/batman5/leader","value":"postgresql1","expiration":"2015-05-15T09:11:00.037397538Z","ttl":21,"modifiedIndex":20728,"createdIndex":20434},{"key":"/service/batman5/optime","dir":true,"nodes":[{"key":"/service/batman5/optime/leader","value":"2164261704","modifiedIndex":20729,"createdIndex":20729}],"modifiedIndex":20437,"createdIndex":20437},{"key":"/service/batman5/members","dir":true,"nodes":[{"key":"/service/batman5/members/postgresql1","value":"postgres://replicator:[email protected]:5434/postgres?application_name=http://127.0.0.1:8009/patroni","expiration":"2015-05-15T09:10:59.949384522Z","ttl":21,"modifiedIndex":20727,"createdIndex":20727},{"key":"/service/batman5/members/postgresql0","value":"postgres://replicator:[email protected]:5433/postgres?application_name=http://127.0.0.1:8008/patroni","expiration":"2015-05-15T09:11:09.611860899Z","ttl":30,"modifiedIndex":20730,"createdIndex":20730}],"modifiedIndex":1581,"createdIndex":1581}],"modifiedIndex":1581,"createdIndex":1581}}'
elif url.startswith('http://other'):
response.status_code = 404
elif url.startswith('http://noleader'):
response.content = '{"action":"get","node":{"key":"/service/batman5","dir":true,"nodes":[{"key":"/service/batman5/initialize","value":"postgresql0","modifiedIndex":1582,"createdIndex":1582},{"key":"/service/batman5/leader","value":"postgresql1","expiration":"2015-05-15T09:11:00.037397538Z","ttl":21,"modifiedIndex":20728,"createdIndex":20434},{"key":"/service/batman5/optime","dir":true,"nodes":[{"key":"/service/batman5/optime/leader","value":"2164261704","modifiedIndex":20729,"createdIndex":20729}],"modifiedIndex":20437,"createdIndex":20437},{"key":"/service/batman5/members","dir":true,"nodes":[{"key":"/service/batman5/members/postgresql0","value":"postgres://replicator:[email protected]:5433/postgres?application_name=http://127.0.0.1:8008/governor","expiration":"2015-05-15T09:11:09.611860899Z","ttl":30,"modifiedIndex":20730,"createdIndex":20730}],"modifiedIndex":1581,"createdIndex":1581}],"modifiedIndex":1581,"createdIndex":1581}}'
response.content = '{"action":"get","node":{"key":"/service/batman5","dir":true,"nodes":[{"key":"/service/batman5/initialize","value":"postgresql0","modifiedIndex":1582,"createdIndex":1582},{"key":"/service/batman5/leader","value":"postgresql1","expiration":"2015-05-15T09:11:00.037397538Z","ttl":21,"modifiedIndex":20728,"createdIndex":20434},{"key":"/service/batman5/optime","dir":true,"nodes":[{"key":"/service/batman5/optime/leader","value":"2164261704","modifiedIndex":20729,"createdIndex":20729}],"modifiedIndex":20437,"createdIndex":20437},{"key":"/service/batman5/members","dir":true,"nodes":[{"key":"/service/batman5/members/postgresql0","value":"postgres://replicator:[email protected]:5433/postgres?application_name=http://127.0.0.1:8008/patroni","expiration":"2015-05-15T09:11:09.611860899Z","ttl":30,"modifiedIndex":20730,"createdIndex":20730}],"modifiedIndex":1581,"createdIndex":1581}],"modifiedIndex":1581,"createdIndex":1581}}'
else:
response.status_code = 404
response.ok = False
@@ -8,7 +8,7 @@ import time
import unittest
import yaml
from governor import Governor, main
from patroni import Patroni, main
from helpers.dcs import Cluster, Member
from helpers.zookeeper import ZooKeeper
from test_etcd import requests_get, requests_put, requests_delete
@@ -30,12 +30,12 @@ def time_sleep(*args):
raise Exception()
class TestGovernor(unittest.TestCase):
class TestPatroni(unittest.TestCase):
def __init__(self, method_name='runTest'):
self.setUp = self.set_up
self.tearDown = self.tear_down
super(TestGovernor, self).__init__(method_name)
super(TestPatroni, self).__init__(method_name)
def set_up(self):
self.touched = False
@@ -53,7 +53,7 @@ class TestGovernor(unittest.TestCase):
BaseHTTPServer.HTTPServer.__init__ = nop
with open('postgres0.yml', 'r') as f:
config = yaml.load(f)
self.g = Governor(config)
self.g = Patroni(config)
def tear_down(self):
time.sleep = self.time_sleep
@@ -65,13 +65,13 @@ class TestGovernor(unittest.TestCase):
self.assertIsInstance(self.g.get_dcs('', {'zookeeper': {'scope': '', 'hosts': ''}}), ZooKeeper)
self.assertRaises(Exception, self.g.get_dcs, '', {})
def test_governor_main(self):
def test_patroni_main(self):
main()
sys.argv = ['governor.py', 'postgres0.yml']
sys.argv = ['patroni.py', 'postgres0.yml']
time.sleep = time_sleep
self.assertRaises(Exception, main)
def test_governor_run(self):
def test_patroni_run(self):
time.sleep = time_sleep
self.g.postgresql.is_leader = lambda: False
self.g.ha.state_handler.sync_replication_slots = time_sleep
@@ -90,7 +90,7 @@ class TestGovernor(unittest.TestCase):
self.g.ha.cluster = Cluster(True, member, 0, [member])
self.g.touch_member()
def test_governor_initialize(self):
def test_patroni_initialize(self):
self.g.postgresql.should_use_s3_to_create_replica = false
self.g.ha.dcs.client._base_uri = 'http://remote'
self.g.postgresql.data_directory_empty = true
+1 -1
View File
@@ -56,7 +56,7 @@ class MockKazooClient:
raise Exception()
elif '/members/' in path:
return (
'postgres://repuser:rep-pass@localhost:5434/postgres?application_name=http://127.0.0.1:8009/governor',
'postgres://repuser:rep-pass@localhost:5434/postgres?application_name=http://127.0.0.1:8009/patroni',
ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
)
elif path.endswith('/optime/leader'):