# 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.## 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.importloggingimportrandomimportthreadingimporttimefromqueueimportEmpty,PriorityQueuefromtypingimportAny,Dict,Optional,Sequence,Tuple,Typefrom.connectionimportConnectionfrom.exceptionsimportImproperlyConfiguredlogger:logging.Logger=logging.getLogger("opensearch")
[docs]classConnectionSelector:""" Simple class used to select a connection from a list of currently live connection instances. In init time it is passed a dictionary containing all the connections' options which it can then use during the selection process. When the `select` method is called it is given a list of *currently* live connections to choose from. The options dictionary is the one that has been passed to :class:`~opensearchpy.Transport` as `hosts` param and the same that is used to construct the Connection object itself. When the Connection was created from information retrieved from the cluster via the sniffing process it will be the dictionary returned by the `host_info_callback`. Example of where this would be useful is a zone-aware selector that would only select connections from its own zones and only fall back to other connections where there would be none in its zones. """def__init__(self,opts:Sequence[Tuple[Connection,Any]])->None:""" :arg opts: dictionary of connection instances and their options """self.connection_opts=opts
[docs]defselect(self,connections:Sequence[Connection])->None:""" Select a connection from the given list. :arg connections: list of live connections to choose from """pass
classRandomSelector(ConnectionSelector):""" Select a connection at random """defselect(self,connections:Sequence[Connection])->Any:returnrandom.choice(connections)
[docs]classRoundRobinSelector(ConnectionSelector):""" Selector using round-robin. """def__init__(self,opts:Sequence[Tuple[Connection,Any]])->None:super().__init__(opts)self.data=threading.local()
[docs]classConnectionPool:""" Container holding the :class:`~opensearchpy.Connection` instances, managing the selection process (via a :class:`~opensearchpy.ConnectionSelector`) and dead connections. It's only interactions are with the :class:`~opensearchpy.Transport` class that drives all the actions within `ConnectionPool`. Initially connections are stored on the class as a list and, along with the connection options, get passed to the `ConnectionSelector` instance for future reference. Upon each request the `Transport` will ask for a `Connection` via the `get_connection` method. If the connection fails (its `perform_request` raises a `ConnectionError`) it will be marked as dead (via `mark_dead`) and put on a timeout (if it fails N times in a row the timeout is exponentially longer - the formula is `default_timeout * 2 ** (fail_count - 1)`). When the timeout is over the connection will be resurrected and returned to the live pool. A connection that has been previously marked as dead and succeeds will be marked as live (its fail count will be deleted). """connections_opts:Sequence[Tuple[Connection,Any]]connections:Anyorig_connections:Tuple[Connection,...]dead:Anydead_count:Dict[Any,int]dead_timeout:floattimeout_cutoff:intselector:Anydef__init__(self,connections:Any,dead_timeout:float=60,timeout_cutoff:int=5,selector_class:Type[ConnectionSelector]=RoundRobinSelector,randomize_hosts:bool=True,**kwargs:Any,)->None:""" :arg connections: list of tuples containing the :class:`~opensearchpy.Connection` instance and its options :arg dead_timeout: number of seconds a connection should be retired for after a failure, increases on consecutive failures :arg timeout_cutoff: number of consecutive failures after which the timeout doesn't increase :arg selector_class: :class:`~opensearchpy.ConnectionSelector` subclass to use if more than one connection is live :arg randomize_hosts: shuffle the list of connections upon arrival to avoid dog piling effect across processes """ifnotconnections:raiseImproperlyConfigured("No defined connections, you need to ""specify at least one host.")self.connection_opts=connectionsself.connections=[cfor(c,opts)inconnections]# remember original connection list for resurrect(force=True)self.orig_connections=tuple(self.connections)# PriorityQueue for thread safety and ease of timeout managementself.dead=PriorityQueue(len(self.connections))self.dead_count={}ifrandomize_hosts:# randomize the connection list to avoid all clients hitting same node# after startup/restartrandom.shuffle(self.connections)# default timeout after which to try resurrecting a connectionself.dead_timeout=dead_timeoutself.timeout_cutoff=timeout_cutoffself.selector=selector_class(dict(connections))# type: ignore
[docs]defmark_dead(self,connection:Any,now:Optional[float]=None)->None:""" Mark the connection as dead (failed). Remove it from the live pool and put it on a timeout. :arg connection: the failed instance """# allow inject for testing purposesnow=nowifnowelsetime.time()try:self.connections.remove(connection)exceptValueError:logger.info("Attempted to remove %r, but it does not exist in the connection pool.",connection,)# connection not alive or another thread marked it already, ignorereturnelse:dead_count=self.dead_count.get(connection,0)+1self.dead_count[connection]=dead_counttimeout=self.dead_timeout*2**min(dead_count-1,self.timeout_cutoff)self.dead.put((now+timeout,connection))logger.warning("Connection %r has failed for %i times in a row, putting on %i second timeout.",connection,dead_count,timeout,)
[docs]defmark_live(self,connection:Any)->None:""" Mark connection as healthy after a resurrection. Resets the fail counter for the connection. :arg connection: the connection to redeem """try:delself.dead_count[connection]exceptKeyError:# race condition, safe to ignorepass
[docs]defresurrect(self,force:bool=False)->Any:""" Attempt to resurrect a connection from the dead pool. It will try to locate one (not all) eligible (its timeout is over) connection to return to the live pool. Any resurrected connection is also returned. :arg force: resurrect a connection even if there is none eligible (used when we have no live connections). If force is specified resurrect always returns a connection. """# no dead connectionsifself.dead.empty():# we are forced to return a connection, take one from the original# list. This is to avoid a race condition where get_connection can# see no live connections but when it calls resurrect self.dead is# also empty. We assume that other threat has resurrected all# available connections so we can safely return one at random.ifforce:returnrandom.choice(self.orig_connections)returntry:# retrieve a connection to checktimeout,connection=self.dead.get(block=False)exceptEmpty:# other thread has been faster and the queue is now empty. If we# are forced, return a connection at random again.ifforce:returnrandom.choice(self.orig_connections)returnifnotforceandtimeout>time.time():# return it back if not eligible and not forcedself.dead.put((timeout,connection))return# either we were forced or the connection is eligible to be retriedself.connections.append(connection)logger.info("Resurrecting connection %r (force=%s).",connection,force)returnconnection
[docs]defget_connection(self)->Any:""" Return a connection from the pool using the `ConnectionSelector` instance. It tries to resurrect eligible connections, forces a resurrection when no connections are available and passes the list of live connections to the selector instance to choose from. Returns a connection instance and its current fail count. """self.resurrect()connections=self.connections[:]# no live nodes, resurrect one by force and return itifnotconnections:returnself.resurrect(True)# only call selector if we have a selectioniflen(connections)>1:returnself.selector.select(connections)# only one connection, no need for a selectorreturnconnections[0]
classDummyConnectionPool(ConnectionPool):def__init__(self,connections:Any,**kwargs:Any)->None:iflen(connections)!=1:raiseImproperlyConfigured("DummyConnectionPool needs exactly one ""connection defined.")# we need connection opts for sniffing logicself.connection_opts=connectionsself.connection:Any=connections[0][0]self.connections=(self.connection,)defget_connection(self)->Any:returnself.connectiondefclose(self)->None:""" Explicitly closes connections """self.connection.close()def_noop(self,*args:Any,**kwargs:Any)->Any:passmark_dead=mark_live=resurrect=_noopclassEmptyConnectionPool(ConnectionPool):"""A connection pool that is empty. Errors out if used."""def__init__(self,*_:Any,**__:Any)->None:self.connections=[]self.connection_opts=[]defget_connection(self)->Connection:raiseImproperlyConfigured("No connections were configured")def_noop(self,*args:Any,**kwargs:Any)->Any:passclose=mark_dead=mark_live=resurrect=_noop