@@ -18,11 +18,19 @@ def get_host_info(node_info, host):
|
|||||||
|
|
||||||
Useful for filtering nodes (by proximity for example) or if additional
|
Useful for filtering nodes (by proximity for example) or if additional
|
||||||
information needs to be provided for the :class:`~elasticsearch.Connection`
|
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 node_info: node information from `/_cluster/nodes`
|
||||||
:arg host: connection information (host, port) extracted from the node info
|
: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
|
return host
|
||||||
|
|
||||||
class Transport(object):
|
class Transport(object):
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from elasticsearch.transport import Transport
|
from elasticsearch.transport import Transport, get_host_info
|
||||||
from elasticsearch.connection import Connection
|
from elasticsearch.connection import Connection
|
||||||
from elasticsearch.exceptions import ConnectionError
|
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):
|
class TestTransport(TestCase):
|
||||||
def test_request_timeout_extracted_from_params_and_passed(self):
|
def test_request_timeout_extracted_from_params_and_passed(self):
|
||||||
t = Transport([{}], connection_class=DummyConnection)
|
t = Transport([{}], connection_class=DummyConnection)
|
||||||
|
|||||||
Reference in New Issue
Block a user