From 8966902e40b9bb910352b8324ba4ff42ba5a7322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Thu, 31 Jul 2014 00:33:07 +0200 Subject: [PATCH] Filter out master-only nodes when sniffing Fixes #106, thanks jolynch! --- elasticsearch/transport.py | 10 +++++++++- test_elasticsearch/test_transport.py | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index 475401b6..ca1a9c33 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -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): diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 7204abed..9946784f 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -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)