Skip to content

Commit

Permalink
Filter out master-only nodes when sniffing
Browse files Browse the repository at this point in the history
Fixes #106, thanks jolynch!
  • Loading branch information
honzakral committed Jul 30, 2014
1 parent 43b96e7 commit 8966902
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 9 additions & 1 deletion elasticsearch/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 15 additions & 1 deletion test_elasticsearch/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -37,6 +37,20 @@ def perform_request(self, *args, **kwargs):
}
}'''

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)
Expand Down

0 comments on commit 8966902

Please sign in to comment.