def _sendMessage(self, nodeId, msgBuffer, tryCount = -1): result = None if tryCount == -1 : tryCount = self._config.getTryCount() for i in range(tryCount) : if i > 0: maxSleep = i * ArakoonClientConfig.getBackoffInterval() self._sleep( random.randint(0, maxSleep) ) with self.__lock : try : connection = self._getConnection( nodeId ) connection.send( msgBuffer ) # Message sent correctly, return client connection so result # can be read result = connection break except Exception, ex: fmt = "Attempt %d to exchange message with node %s failed with error (%s: '%s')." ArakoonClientLogger.logWarning( fmt , i, nodeId, ex.__class__.__name__, ex ) # Get rid of the connection in case of an exception self._connections[nodeId].close() del self._connections[ nodeId ] self._masterId = None
def retrying_f (self,*args,**kwargs): start = time.time() tryCount = 0.0 backoffPeriod = 0.2 callSucceeded = False retryPeriod = ArakoonClientConfig.getNoMasterRetryPeriod () deadline = start + retryPeriod while( not callSucceeded and time.time() < deadline ): try : retVal = f(self,*args,**kwargs) callSucceeded = True except (ArakoonNoMaster, ArakoonNodeNotMaster, ArakoonSocketException, ArakoonNotConnected, ArakoonGoingDown) as ex: if not is_read_only and \ isinstance(ex, (ArakoonSocketException, ArakoonGoingDown)): raise if len( self._config.getNodes().keys()) == 0 : raise ArakoonInvalidConfig( "Empty client configuration" ) self._masterId = None self.dropConnections() sleepPeriod = backoffPeriod * tryCount if time.time() + sleepPeriod > deadline : raise tryCount += 1.0 ArakoonClientLogger.logWarning( "Master not found (%s). Retrying in %0.2f sec." % (ex, sleepPeriod) ) time.sleep( sleepPeriod ) return retVal
def __init__ (self, config=None): """ Constructor of an Arakoon client object. It takes one optional paramater 'config'. This parameter contains info on the arakoon server nodes. See the constructor of L{ArakoonClientConfig} for more details. @type config: L{ArakoonClientConfig} @param config: The L{ArakoonClientConfig} object to be used by the client. Defaults to None in which case a default L{ArakoonClientConfig} object will be created. """ if config is None: config = ArakoonClientConfig() self._initialize( config ) self.__lock = threading.RLock() self._masterId = None self._connections = dict() self._consistency = Consistent() nodeList = self._config.getNodes().keys() if len(nodeList) == 0: raise ArakoonInvalidConfig("Node list empty.") self._dirtyReadNode = random.choice( nodeList )