From d3e745ab773be42ecc7914405721aebf56a6a766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Sun, 4 Jan 2015 19:42:29 +0100 Subject: [PATCH] Make initial sniff (sniff_on_start) ignore sniff_timeout This is due to issue mentioned in #167 as part of discussion --- elasticsearch/transport.py | 15 ++++++++++----- test_elasticsearch/test_transport.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index 6f005bdb..bedd3357 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -61,7 +61,9 @@ class Transport(object): :arg sniff_on_connection_fail: flag controlling if connection failure triggers a sniff :arg sniff_timeout: timeout used for the sniff request - it should be a fast api call and we are talking potentially to more nodes so we want - to fail quickly. + to fail quickly. Not used during initial sniffing (if + ``sniff_on_start`` is on) when the connection still isn't + initialized. :arg serializer: serializer instance :arg serializers: optional dict of serializer instances that will be used for deserializing data coming from the server. (key is the mimetype) @@ -124,7 +126,7 @@ class Transport(object): self.host_info_callback = host_info_callback if sniff_on_start: - self.sniff_hosts() + self.sniff_hosts(True) def add_connection(self, host): """ @@ -182,12 +184,15 @@ class Transport(object): self.sniff_hosts() return self.connection_pool.get_connection() - def sniff_hosts(self): + def sniff_hosts(self, initial=False): """ Obtain a list of nodes from the cluster and create a new connection pool using the information retrieved. - To extract the node connection parameters use the `nodes_to_host_callback`. + To extract the node connection parameters use the ``nodes_to_host_callback``. + + :arg initial: flag indicating if this is during startup + (``sniff_on_start``), ignore the ``sniff_timeout`` if ``True`` """ previous_sniff = self.last_sniff try: @@ -199,7 +204,7 @@ class Transport(object): try: # use small timeout for the sniffing request, should be a fast api call _, headers, node_info = c.perform_request('GET', '/_nodes/_all/clear', - timeout=self.sniff_timeout) + timeout=self.sniff_timeout if not initial else None) node_info = self.deserializer.loads(node_info, headers.get('content-type')) break except (ConnectionError, SerializationError): diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 9e4c2ddb..eec7d855 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -168,6 +168,16 @@ class TestTransport(TestCase): self.assertEquals(1, len(t.connection_pool.connections)) self.assertEquals('http://1.1.1.1:123', t.get_connection().host) + def test_sniff_on_start_ignores_sniff_timeout(self): + t = Transport([{'data': CLUSTER_NODES}], connection_class=DummyConnection, sniff_on_start=True, sniff_timeout=12) + self.assertEquals((('GET', '/_nodes/_all/clear'), {'timeout': None}), t.seed_connections[0].calls[0]) + + def test_sniff_uses_sniff_timeout(self): + t = Transport([{'data': CLUSTER_NODES}], connection_class=DummyConnection, sniff_timeout=42) + t.sniff_hosts() + self.assertEquals((('GET', '/_nodes/_all/clear'), {'timeout': 42}), t.seed_connections[0].calls[0]) + + def test_sniff_reuses_connection_instances_if_possible(self): t = Transport([{'data': CLUSTER_NODES}, {"host": "1.1.1.1", "port": 123}], connection_class=DummyConnection, randomize_hosts=False) connection = t.connection_pool.connections[1]