This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
Add a client InfluxDBClusterClient
to handle a cluster of InfluxDB servers
#148
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e44f74e
Add client `InfluxDBClusterClient` to handle a cluster of InfluxDB se…
cannium e213ecd
tests for InfluxDBClusterClient
cannium 91eb6d4
docs for InfluxDBClusterClient
cannium 59ca6a6
Improvements based on code review.
cannium 4555e6a
Add `from_DSN` to InfluxDBClusterClient
cannium 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 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 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 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 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 |
---|---|---|
|
@@ -26,7 +26,8 @@ | |
import warnings | ||
import mock | ||
|
||
from influxdb import InfluxDBClient | ||
from influxdb import InfluxDBClient, InfluxDBClusterClient | ||
from influxdb.client import InfluxDBServerError | ||
|
||
|
||
def _build_response_object(status_code=200, content=""): | ||
|
@@ -534,3 +535,97 @@ def connection_error(self, *args, **kwargs): | |
|
||
with self.assertRaises(requests.exceptions.ConnectionError): | ||
cli.write_points(self.dummy_points) | ||
|
||
|
||
class FakeClient(InfluxDBClient): | ||
fail = False | ||
|
||
def query(self, | ||
query, | ||
params={}, | ||
expected_response_code=200, | ||
database=None): | ||
if query == 'Fail': | ||
raise Exception("Fail") | ||
|
||
if self.fail: | ||
raise Exception("Fail") | ||
else: | ||
return "Success" | ||
|
||
|
||
class TestInfluxDBClusterClient(unittest.TestCase): | ||
|
||
def setUp(self): | ||
# By default, raise exceptions on warnings | ||
warnings.simplefilter('error', FutureWarning) | ||
|
||
self.hosts = [('host1', 8086), ('host2', 8086), ('host3', 8086)] | ||
|
||
def test_init(self): | ||
cluster = InfluxDBClusterClient(hosts=self.hosts, | ||
username='username', | ||
password='password', | ||
database='database', | ||
shuffle=False, | ||
client_base_class=FakeClient) | ||
assert len(cluster.clients) == 3 | ||
assert len(cluster.bad_clients) == 0 | ||
for idx, client in enumerate(cluster.clients): | ||
assert client._host == self.hosts[idx][0] | ||
assert client._port == self.hosts[idx][1] | ||
|
||
def test_one_server_fails(self): | ||
cluster = InfluxDBClusterClient(hosts=self.hosts, | ||
database='database', | ||
shuffle=False, | ||
client_base_class=FakeClient) | ||
cluster.clients[0].fail = True | ||
assert cluster.query('') == 'Success' | ||
assert len(cluster.clients) == 2 | ||
assert len(cluster.bad_clients) == 1 | ||
|
||
def test_two_servers_fail(self): | ||
cluster = InfluxDBClusterClient(hosts=self.hosts, | ||
database='database', | ||
shuffle=False, | ||
client_base_class=FakeClient) | ||
cluster.clients[0].fail = True | ||
cluster.clients[1].fail = True | ||
assert cluster.query('') == 'Success' | ||
assert len(cluster.clients) == 1 | ||
assert len(cluster.bad_clients) == 2 | ||
|
||
def test_all_fail(self): | ||
cluster = InfluxDBClusterClient(hosts=self.hosts, | ||
database='database', | ||
shuffle=True, | ||
client_base_class=FakeClient) | ||
try: | ||
cluster.query('Fail') | ||
except InfluxDBServerError: | ||
pass | ||
assert len(cluster.clients) == 0 | ||
assert len(cluster.bad_clients) == 3 | ||
|
||
def test_all_good(self): | ||
cluster = InfluxDBClusterClient(hosts=self.hosts, | ||
database='database', | ||
shuffle=True, | ||
client_base_class=FakeClient) | ||
assert cluster.query('') == 'Success' | ||
assert len(cluster.clients) == 3 | ||
assert len(cluster.bad_clients) == 0 | ||
|
||
def test_recovery(self): | ||
cluster = InfluxDBClusterClient(hosts=self.hosts, | ||
database='database', | ||
shuffle=True, | ||
client_base_class=FakeClient) | ||
try: | ||
cluster.query('Fail') | ||
except InfluxDBServerError: | ||
pass | ||
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. Here as well |
||
assert cluster.query('') == 'Success' | ||
assert len(cluster.clients) == 1 | ||
assert len(cluster.bad_clients) == 2 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could/should also "assert the whole try/except block" with :
?
EDIT: the
as ctx
isn't required/necessary as you don't assert anything specifically on the exception instance itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done