-
Notifications
You must be signed in to change notification settings - Fork 26
Multiple endpoints #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dariko
wants to merge
32
commits into
Revolution1:master
Choose a base branch
from
dariko:multiple_endpoints
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
dc2407e
Add support for multiple endpoints
dariko 4b1de04
python 3.7 compatibility
dariko e3defa3
Rework tests to run against an etcd cluster
dariko c744c1a
use etcd version from envs.py
dariko 7a61e92
Only include async fixtures when running on python3
dariko 7b33a71
clarify retry loop
dariko 41492ff
iterate over a copy of the endpoint, preventing concurrent ops to cha…
dariko cad609a
allow EtcdCluster.etcdctl to failover to a working node
dariko 4406119
more stable cluster, containers status detection
dariko ea4814f
more stable cluster and containers status detection
dariko a6c50b7
add delay before asserting callback was called
dariko cebbedb
allow test_snapshot to be self-consistent
dariko f4e46dd
write snapshot data in docker-shared directory
dariko 2e38ccb
test watch util during etcd cluster rolling restart
dariko 1951e1c
python 2 compat
dariko a66edf9
remove useless decorator
dariko 6998a42
use first endpoint data as default
dariko 78d0a65
create shared directory for containers with permissive mode
dariko 16be75c
allow aioclient to work without valid certificates
dariko aead4ea
disable certificate validation on tests
dariko 95c7d24
disable certificate validation on tests
dariko ba4ea4f
Construct cluster endpoints based on container addresses
dariko 339f4aa
move retry_all_hosts to utils.py, initial whitelist support
dariko ae94b43
remove validation preventing minimal call format
dariko 7a07e22
replace deprecated log.warn with log.warning
dariko ec49b1a
rework failover logic
dariko 681d39c
add `status` to failover_waitlist
dariko 0151244
cleanup watch util failover test
dariko d47f9ed
tests
dariko 709f74e
python 2 compatibility
dariko 3aa610e
prevent mutable in parameter default
dariko 8206a66
remove unused imports
dariko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,18 @@ | |
import sys | ||
import time | ||
import warnings | ||
import copy | ||
import inspect | ||
import socket | ||
from collections import namedtuple, OrderedDict, Hashable | ||
from subprocess import Popen, PIPE | ||
from threading import Lock | ||
from requests.exceptions import ChunkedEncodingError, ConnectTimeout | ||
from urllib3.exceptions import MaxRetryError | ||
try: | ||
from httplib import IncompleteRead | ||
except ImportError: | ||
from http.client import IncompleteRead | ||
|
||
try: # pragma: no cover | ||
from inspect import getfullargspec as getargspec | ||
|
@@ -382,3 +391,70 @@ def find_executable(executable, path=None): # pragma: no cover | |
f = os.path.join(p, execname) | ||
if os.path.isfile(f): | ||
return f | ||
|
||
failover_exceptions = (ChunkedEncodingError, | ||
IncompleteRead, | ||
ConnectTimeout, | ||
MaxRetryError, | ||
socket.timeout, | ||
) | ||
|
||
def retry_all_hosts(func): | ||
def current_caller_name(): | ||
previous_frame = inspect.currentframe().f_back.f_back | ||
return inspect.getframeinfo(previous_frame).function | ||
def wrapper(self, *args, **kwargs): | ||
calling_function = current_caller_name() | ||
if not calling_function in self.failover_whitelist: | ||
log.debug('%s not in failover waitlist(%s)' % | ||
(calling_function, self.failover_whitelist)) | ||
try: | ||
return func(self, *args, **kwargs) | ||
except failover_exceptions as e: | ||
if not calling_function in self.failover_whitelist: | ||
log.debug('%s not in failover waitlist(%s)' % | ||
(calling_function, self.failover_whitelist)) | ||
raise e | ||
errors = [] | ||
got_result = False | ||
with self.current_endpoint_lock: | ||
# to exclude the current endpoint it should be saved | ||
# before the call in the outmost `try`, and the | ||
# whole wrapper should depend on the lock | ||
for endpoint in self.endpoints: | ||
self.current_endpoint = endpoint | ||
try: | ||
self.current_endpoint = endpoint | ||
ret = func(self, *args, **kwargs) | ||
got_result = True | ||
break | ||
except failover_exceptions as e: | ||
errors.append(e) | ||
log.warning('Failed to call %s(args: %s, kwargs: %s) on' | ||
' endpoint %s (%s)' % | ||
(func.__name__, args, kwargs, endpoint, e)) | ||
except Exception as e: | ||
log.debug('received exception %s, not in ' | ||
'failover_exceptions(%s)' % | ||
(e, failover_exceptions)) | ||
if not got_result: | ||
log.error('Failed to call %s(args: %s, kwargs: %s) on all ' | ||
'endpoints: %s. Got errors: %s' % | ||
(func.__name__, args, kwargs, | ||
call_endpoints, errors)) | ||
exception_types = [x.__class__ for x in errors] | ||
if len(set(exception_types)) == 1: | ||
raise errors[0] | ||
else: | ||
raise Etcd3Exception('Failed failover') | ||
return ret | ||
return wrapper | ||
|
||
|
||
class EtcdEndpoint(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this class contains only host and port but made creating a client less friendly any further design on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better put this into |
||
def __init__(self, host='127.0.0.1', port=2379): | ||
self.host = host | ||
self.port = port | ||
|
||
def __repr__(self): | ||
return "EtcdEndpoint(host=%s, port=%s)" % (self.host, self.port) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ m2r==0.2.1 | |
codecov>=1.4.0 | ||
codacy-coverage==1.3.11 | ||
twine==1.13.0 | ||
docker==3.7.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import six | ||
from etcd3.client import Client | ||
import pytest | ||
from .etcd_cluster import EtcdTestCluster | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def etcd_cluster(request): | ||
# function_name = request.function.__name__ | ||
# function_name = re.sub(r"[^a-zA-Z0-9]+", "", function_name) | ||
cluster = EtcdTestCluster(ident='cleartext', size=3) | ||
|
||
def fin(): | ||
cluster.down() | ||
request.addfinalizer(fin) | ||
cluster.up() | ||
cluster.wait_ready() | ||
|
||
return cluster | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def etcd_cluster_ssl(request): | ||
# function_name = request.function.__name__ | ||
# function_name = re.sub(r"[^a-zA-Z0-9]+", "", function_name) | ||
cluster = EtcdTestCluster(ident='ssl', size=3, ssl=True) | ||
|
||
def fin(): | ||
cluster.down() | ||
request.addfinalizer(fin) | ||
cluster.up() | ||
cluster.wait_ready() | ||
|
||
return cluster | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def client(etcd_cluster): | ||
""" | ||
init Etcd3Client, close its connection-pool when teardown | ||
""" | ||
# _, p, _ = docker_run_etcd_main() | ||
c = Client(endpoints=etcd_cluster.get_endpoints(), | ||
protocol='https' if etcd_cluster.ssl else 'http') | ||
yield c | ||
c.close() | ||
|
||
|
||
@pytest.fixture | ||
def clear(etcd_cluster): | ||
def _clear(): | ||
etcd_cluster.etcdctl('del --from-key ""') | ||
return _clear | ||
|
||
|
||
def teardown_auth(etcd_cluster): # pragma: no cover | ||
""" | ||
disable auth, delete all users and roles | ||
""" | ||
etcd_cluster.etcdctl('--user root:root auth disable') | ||
etcd_cluster.etcdctl('--user root:changed auth disable') | ||
for i in (etcd_cluster.etcdctl('role list') or '').splitlines(): | ||
if six.PY3: # pragma: no cover | ||
i = six.text_type(i, encoding='utf-8') | ||
etcd_cluster.etcdctl('role delete %s' % i) | ||
for i in (etcd_cluster.etcdctl('user list') or '').splitlines(): | ||
if six.PY3: # pragma: no cover | ||
i = six.text_type(i, encoding='utf-8') | ||
etcd_cluster.etcdctl('user delete %s' % i) | ||
|
||
|
||
def enable_auth(etcd_cluster): # pragma: no cover | ||
etcd_cluster.etcdctl('user add root:root') | ||
etcd_cluster.etcdctl('role add root') | ||
etcd_cluster.etcdctl('user grant root root') | ||
etcd_cluster.etcdctl('auth enable') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.