Filter out master-only nodes when sniffing

Fixes #106, thanks jolynch!
This commit is contained in:
Honza Král
2014-07-31 00:33:07 +02:00
parent 43b96e7a49
commit 8966902e40
2 changed files with 24 additions and 2 deletions
+9 -1
View File
@@ -18,11 +18,19 @@ def get_host_info(node_info, host):
Useful for filtering nodes (by proximity for example) or if additional
information needs to be provided for the :class:`~elasticsearch.Connection`
class.
class. By default master only nodes are filtered out since they shouldn't
typically be used for API operations.
:arg node_info: node information from `/_cluster/nodes`
:arg host: connection information (host, port) extracted from the node info
"""
attrs = node_info.get('attributes', {})
# ignore master only nodes
if (attrs.get('data', 'true') == 'false' and
attrs.get('client', 'false') == 'false' and
attrs.get('master', 'true') == 'true'):
return None
return host
class Transport(object):
+15 -1
View File
@@ -2,7 +2,7 @@
from __future__ import unicode_literals
import time
from elasticsearch.transport import Transport
from elasticsearch.transport import Transport, get_host_info
from elasticsearch.connection import Connection
from elasticsearch.exceptions import ConnectionError
@@ -37,6 +37,20 @@ CLUSTER_NODES = '''{
}
}'''
class TestHostsInfoCallback(TestCase):
def test_master_only_nodes_are_ignored(self):
nodes = [
{'attributes': {'data': 'false', 'client': 'true'}},
{'attributes': {'data': 'false'}},
{'attributes': {'data': 'false', 'master': 'true'}},
{'attributes': {'data': 'false', 'master': 'false'}},
{'attributes': {}},
{}
]
chosen = [ i for i, node_info in enumerate(nodes) if get_host_info(node_info, i) is not None]
self.assertEquals([0, 3, 4, 5], chosen)
class TestTransport(TestCase):
def test_request_timeout_extracted_from_params_and_passed(self):
t = Transport([{}], connection_class=DummyConnection)