Skip to content

Commit 4555e6a

Browse files
committed
Add from_DSN to InfluxDBClusterClient
1 parent 59ca6a6 commit 4555e6a

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

influxdb/client.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
except NameError:
1919
xrange = range
2020

21-
if version_info.major == 3:
21+
if version_info[0] == 3:
2222
from urllib.parse import urlparse
2323
else:
2424
from urlparse import urlparse
@@ -469,6 +469,9 @@ class InfluxDBClusterClient(object):
469469
:param hosts: A list of hosts, where a host should be in format
470470
(address, port)
471471
e.g. [('127.0.0.1', 8086), ('127.0.0.1', 9096)]
472+
:param shuffle: If true, queries will hit servers evenly(randomly)
473+
:param client_base_class: In order to support different clients,
474+
default to InfluxDBClient
472475
"""
473476

474477
def __init__(self,
@@ -482,11 +485,11 @@ def __init__(self,
482485
use_udp=False,
483486
udp_port=4444,
484487
shuffle=True,
485-
client_base_class=InfluxDBClient, # For simpler test code
488+
client_base_class=InfluxDBClient,
486489
):
487490
self.clients = []
488491
self.bad_clients = [] # Corresponding server has failures in history
489-
self.shuffle = shuffle # if true, queries will hit servers evenly
492+
self.shuffle = shuffle
490493
for h in hosts:
491494
self.clients.append(client_base_class(host=h[0], port=h[1],
492495
username=username,
@@ -505,6 +508,40 @@ def __init__(self,
505508
continue
506509
setattr(self, method, self._make_func(orig_func))
507510

511+
@staticmethod
512+
def from_DSN(dsn, client_base_class=InfluxDBClient,
513+
shuffle=True, **kwargs):
514+
"""
515+
Same as InfluxDBClient.from_DSN, and supports multiple servers.
516+
517+
Example DSN:
518+
influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db_name
519+
udp+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db_name
520+
https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db_name
521+
522+
:param shuffle: If true, queries will hit servers evenly(randomly)
523+
:param client_base_class: In order to support different clients,
524+
default to InfluxDBClient
525+
"""
526+
dsn = dsn.lower()
527+
conn_params = urlparse(dsn)
528+
netlocs = conn_params.netloc.split(',')
529+
cluster_client = InfluxDBClusterClient(
530+
hosts=[],
531+
client_base_class=client_base_class,
532+
shuffle=shuffle,
533+
**kwargs)
534+
for netloc in netlocs:
535+
single_dsn = '%(scheme)s://%(netloc)s%(path)s' % (
536+
{'scheme': conn_params.scheme,
537+
'netloc': netloc,
538+
'path': conn_params.path}
539+
)
540+
cluster_client.clients.append(client_base_class.from_DSN(
541+
single_dsn,
542+
**kwargs))
543+
return cluster_client
544+
508545
def _make_func(self, orig_func):
509546

510547
@wraps(orig_func)

tests/influxdb/client_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,55 @@ def test_recovery(self):
623623
self.assertEqual('Success', cluster.query(''))
624624
self.assertEqual(1, len(cluster.clients))
625625
self.assertEqual(2, len(cluster.bad_clients))
626+
627+
def test_dsn(self):
628+
cli = InfluxDBClusterClient.from_DSN(
629+
'influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db')
630+
self.assertEqual(2, len(cli.clients))
631+
self.assertEqual('http://host1:8086', cli.clients[0]._baseurl)
632+
self.assertEqual('usr', cli.clients[0]._username)
633+
self.assertEqual('pwd', cli.clients[0]._password)
634+
self.assertEqual('db', cli.clients[0]._database)
635+
self.assertFalse(cli.clients[0].use_udp)
636+
self.assertEqual('http://host2:8086', cli.clients[1]._baseurl)
637+
self.assertEqual('usr', cli.clients[1]._username)
638+
self.assertEqual('pwd', cli.clients[1]._password)
639+
self.assertEqual('db', cli.clients[1]._database)
640+
self.assertFalse(cli.clients[1].use_udp)
641+
642+
cli = InfluxDBClusterClient.from_DSN(
643+
'udp+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db')
644+
self.assertTrue(cli.clients[0].use_udp)
645+
self.assertTrue(cli.clients[1].use_udp)
646+
647+
cli = InfluxDBClusterClient.from_DSN(
648+
'https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db')
649+
self.assertEqual('https://host1:8086', cli.clients[0]._baseurl)
650+
self.assertEqual('https://host2:8086', cli.clients[1]._baseurl)
651+
652+
cli = InfluxDBClusterClient.from_DSN(
653+
'https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db',
654+
**{'ssl': False})
655+
self.assertEqual('http://host1:8086', cli.clients[0]._baseurl)
656+
self.assertEqual('http://host2:8086', cli.clients[1]._baseurl)
657+
658+
def test_dsn_single_client(self):
659+
cli = InfluxDBClusterClient.from_DSN('influxdb://usr:pwd@host:8086/db')
660+
self.assertEqual('http://host:8086', cli.clients[0]._baseurl)
661+
self.assertEqual('usr', cli.clients[0]._username)
662+
self.assertEqual('pwd', cli.clients[0]._password)
663+
self.assertEqual('db', cli.clients[0]._database)
664+
self.assertFalse(cli.clients[0].use_udp)
665+
666+
cli = InfluxDBClusterClient.from_DSN(
667+
'udp+influxdb://usr:pwd@host:8086/db')
668+
self.assertTrue(cli.clients[0].use_udp)
669+
670+
cli = InfluxDBClusterClient.from_DSN(
671+
'https+influxdb://usr:pwd@host:8086/db')
672+
self.assertEqual('https://host:8086', cli.clients[0]._baseurl)
673+
674+
cli = InfluxDBClusterClient.from_DSN(
675+
'https+influxdb://usr:pwd@host:8086/db',
676+
**{'ssl': False})
677+
self.assertEqual('http://host:8086', cli.clients[0]._baseurl)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy