2021-08-06 12:59:39 +05:30
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
#
|
|
|
|
|
# The OpenSearch Contributors require contributions made to
|
|
|
|
|
# this file be licensed under the Apache-2.0 license or a
|
|
|
|
|
# compatible open source license.
|
|
|
|
|
#
|
|
|
|
|
# Modifications Copyright OpenSearch Contributors. See
|
|
|
|
|
# GitHub history for details.
|
|
|
|
|
#
|
2020-07-02 13:15:25 -05:00
|
|
|
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
|
|
|
# license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright
|
|
|
|
|
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
|
|
|
# the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
|
# under the License.
|
2020-04-23 11:22:08 -05:00
|
|
|
|
2022-10-04 00:15:18 +05:30
|
|
|
|
2013-05-02 17:54:26 +02:00
|
|
|
import time
|
2023-11-09 10:51:20 -05:00
|
|
|
from typing import Any
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2021-09-16 14:59:29 +05:30
|
|
|
from opensearchpy.connection import Connection
|
|
|
|
|
from opensearchpy.connection_pool import (
|
2019-05-10 09:16:33 -06:00
|
|
|
ConnectionPool,
|
|
|
|
|
DummyConnectionPool,
|
2021-01-13 14:21:04 -06:00
|
|
|
RoundRobinSelector,
|
2019-05-10 09:16:33 -06:00
|
|
|
)
|
2021-09-16 14:59:29 +05:30
|
|
|
from opensearchpy.exceptions import ImproperlyConfigured
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2013-08-28 19:11:28 +02:00
|
|
|
from .test_cases import TestCase
|
|
|
|
|
|
2019-05-10 09:16:33 -06:00
|
|
|
|
2013-05-02 17:54:26 +02:00
|
|
|
class TestConnectionPool(TestCase):
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_dummy_cp_raises_exception_on_more_connections(self) -> None:
|
2014-12-20 00:35:24 +01:00
|
|
|
self.assertRaises(ImproperlyConfigured, DummyConnectionPool, [])
|
2019-05-10 09:16:33 -06:00
|
|
|
self.assertRaises(
|
|
|
|
|
ImproperlyConfigured, DummyConnectionPool, [object(), object()]
|
|
|
|
|
)
|
2014-12-20 00:35:24 +01:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_raises_exception_when_no_connections_defined(self) -> None:
|
2014-12-08 14:32:52 +01:00
|
|
|
self.assertRaises(ImproperlyConfigured, ConnectionPool, [])
|
|
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_default_round_robin(self) -> None:
|
2013-05-02 17:54:26 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(100)])
|
|
|
|
|
|
|
|
|
|
connections = set()
|
|
|
|
|
for _ in range(100):
|
2013-06-14 16:46:58 +02:00
|
|
|
connections.add(pool.get_connection())
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(connections, set(range(100)))
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def test_disable_shuffling(self) -> None:
|
2013-05-02 17:54:26 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(100)], randomize_hosts=False)
|
|
|
|
|
|
|
|
|
|
connections = []
|
|
|
|
|
for _ in range(100):
|
2013-06-14 16:46:58 +02:00
|
|
|
connections.append(pool.get_connection())
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(connections, list(range(100)))
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def test_selectors_have_access_to_connection_opts(self) -> None:
|
2013-05-02 17:54:26 +02:00
|
|
|
class MySelector(RoundRobinSelector):
|
2023-11-09 10:51:20 -05:00
|
|
|
def select(self, connections: Any) -> Any:
|
2024-07-20 23:19:20 +03:00
|
|
|
return self.connection_opts[super().select(connections)]["actual"]
|
2019-05-10 09:16:33 -06:00
|
|
|
|
|
|
|
|
pool = ConnectionPool(
|
|
|
|
|
[(x, {"actual": x * x}) for x in range(100)],
|
|
|
|
|
selector_class=MySelector,
|
|
|
|
|
randomize_hosts=False,
|
|
|
|
|
)
|
2013-05-02 17:54:26 +02:00
|
|
|
|
|
|
|
|
connections = []
|
|
|
|
|
for _ in range(100):
|
2013-06-14 16:46:58 +02:00
|
|
|
connections.append(pool.get_connection())
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(connections, [x * x for x in range(100)])
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_dead_nodes_are_removed_from_active_connections(self) -> None:
|
2013-05-02 17:54:26 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(100)])
|
|
|
|
|
|
|
|
|
|
now = time.time()
|
2013-06-14 16:46:58 +02:00
|
|
|
pool.mark_dead(42, now=now)
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(99, len(pool.connections))
|
|
|
|
|
self.assertEqual(1, pool.dead.qsize())
|
|
|
|
|
self.assertEqual((now + 60, 42), pool.dead.get())
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_connection_is_skipped_when_dead(self) -> None:
|
2013-05-02 17:54:26 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(2)])
|
2013-06-14 16:46:58 +02:00
|
|
|
pool.mark_dead(0)
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(
|
2019-05-10 09:16:33 -06:00
|
|
|
[1, 1, 1],
|
|
|
|
|
[pool.get_connection(), pool.get_connection(), pool.get_connection()],
|
|
|
|
|
)
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_new_connection_is_not_marked_dead(self) -> None:
|
2019-10-29 11:46:18 -04:00
|
|
|
# Create 10 connections
|
|
|
|
|
pool = ConnectionPool([(Connection(), {}) for _ in range(10)])
|
|
|
|
|
|
|
|
|
|
# Pass in a new connection that is not in the pool to mark as dead
|
|
|
|
|
new_connection = Connection()
|
|
|
|
|
pool.mark_dead(new_connection)
|
|
|
|
|
|
|
|
|
|
# Nothing should be marked dead
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(0, len(pool.dead_count))
|
2019-10-29 11:46:18 -04:00
|
|
|
|
2024-01-19 13:36:05 -05:00
|
|
|
def test_connection_is_forcibly_resurrected_when_no_live_ones_are_available(
|
2023-11-06 13:08:19 -05:00
|
|
|
self,
|
|
|
|
|
) -> None:
|
2013-05-02 17:54:26 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(2)])
|
2013-06-14 16:46:58 +02:00
|
|
|
pool.dead_count[0] = 1
|
2019-05-10 09:16:33 -06:00
|
|
|
pool.mark_dead(0) # failed twice, longer timeout
|
|
|
|
|
pool.mark_dead(1) # failed the first time, first to be resurrected
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual([], pool.connections)
|
|
|
|
|
self.assertEqual(1, pool.get_connection())
|
|
|
|
|
self.assertEqual([1], pool.connections)
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_connection_is_resurrected_after_its_timeout(self) -> None:
|
2013-05-02 17:54:26 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(100)])
|
|
|
|
|
|
|
|
|
|
now = time.time()
|
2019-05-10 09:16:33 -06:00
|
|
|
pool.mark_dead(42, now=now - 61)
|
2013-05-03 03:19:49 +02:00
|
|
|
pool.get_connection()
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(42, pool.connections[-1])
|
|
|
|
|
self.assertEqual(100, len(pool.connections))
|
2013-05-02 17:54:26 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_force_resurrect_always_returns_a_connection(self) -> None:
|
2014-12-20 00:34:42 +01:00
|
|
|
pool = ConnectionPool([(0, {})])
|
|
|
|
|
|
|
|
|
|
pool.connections = []
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(0, pool.get_connection())
|
|
|
|
|
self.assertEqual([], pool.connections)
|
2014-12-20 00:34:42 +01:00
|
|
|
self.assertTrue(pool.dead.empty())
|
|
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_already_failed_connection_has_longer_timeout(self) -> None:
|
2013-05-05 01:00:10 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(100)])
|
|
|
|
|
now = time.time()
|
2013-06-14 16:46:58 +02:00
|
|
|
pool.dead_count[42] = 2
|
|
|
|
|
pool.mark_dead(42, now=now)
|
2013-05-05 01:00:10 +02:00
|
|
|
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(3, pool.dead_count[42])
|
|
|
|
|
self.assertEqual((now + 4 * 60, 42), pool.dead.get())
|
2013-05-05 01:00:10 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_timeout_for_failed_connections_is_limitted(self) -> None:
|
2013-06-14 16:59:36 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(100)])
|
|
|
|
|
now = time.time()
|
|
|
|
|
pool.dead_count[42] = 245
|
|
|
|
|
pool.mark_dead(42, now=now)
|
|
|
|
|
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(246, pool.dead_count[42])
|
|
|
|
|
self.assertEqual((now + 32 * 60, 42), pool.dead.get())
|
2013-06-14 16:59:36 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def test_dead_count_is_wiped_clean_for_connection_if_marked_live(self) -> None:
|
2013-05-05 01:00:10 +02:00
|
|
|
pool = ConnectionPool([(x, {}) for x in range(100)])
|
|
|
|
|
now = time.time()
|
2013-06-14 16:46:58 +02:00
|
|
|
pool.dead_count[42] = 2
|
|
|
|
|
pool.mark_dead(42, now=now)
|
2013-05-05 01:00:10 +02:00
|
|
|
|
2020-05-08 16:07:52 -05:00
|
|
|
self.assertEqual(3, pool.dead_count[42])
|
2013-05-05 01:00:10 +02:00
|
|
|
pool.mark_live(42)
|
2013-08-28 19:02:42 +02:00
|
|
|
self.assertNotIn(42, pool.dead_count)
|