Exemple #1
0
 def _connect( self ):
   self.__discoverExtraCredentials()
   if not self.__initStatus[ 'OK' ]:
     return self.__initStatus
   if self.__enableThreadCheck:
     self.__checkThreadID()
   gLogger.debug( "Connecting to: %s" % self.serviceURL )
   try:
     transport = gProtocolDict[ self.__URLTuple[0] ][ 'transport' ]( self.__URLTuple[1:3], **self.kwargs )
     retVal = transport.initAsClient()
     if not retVal[ 'OK' ]:
       if self.__retry < self.__nbOfRetry * self.__nbOfUrls - 1:
         url = "%s://%s:%d/%s" % ( self.__URLTuple[0], self.__URLTuple[1], int( self.__URLTuple[2] ), self.__URLTuple[3] )
         if url not in self.__bannedUrls: 
           gLogger.notice( "URL banned", "%s" % url )
           self.__bannedUrls += [url]   
         self.__retry += 1
         gLogger.info( "Retry connection: ", "%d" % self.__retry )
         if (len(self.__bannedUrls) == self.__nbOfUrls):
           self.__retryDelay = 3. / self.__nbOfUrls  if self.__nbOfUrls > 1 else 5  # we run only one service! In that case we increase the retry delay.
           gLogger.info( "Waiting %f  second before retry all service(s)" % self.__retryDelay )
           time.sleep( self.__retryDelay )
         self.__discoverURL()
         return self._connect()
       else:
         return S_ERROR( "Can't connect to %s: %s" % ( self.serviceURL, retVal ) )
   except Exception, e:
     return S_ERROR( "Can't connect to %s: %s" % ( self.serviceURL, e ) )
Exemple #2
0
 def _connect( self ):
   self.__discoverExtraCredentials()
   if not self.__initStatus[ 'OK' ]:
     return self.__initStatus
   if self.__enableThreadCheck:
     self.__checkThreadID()
   gLogger.debug( "Connecting to: %s" % self.serviceURL )
   try:
     transport = gProtocolDict[ self.__URLTuple[0] ][ 'transport' ]( self.__URLTuple[1:3], **self.kwargs )
     retVal = transport.initAsClient()
     if not retVal[ 'OK' ]:
       if self.__retry < 5:
         url = "%s://%s:%d/%s" % ( self.__URLTuple[0], self.__URLTuple[1], int( self.__URLTuple[2] ), self.__URLTuple[3] )
         if url not in self.__bannedUrls: 
           gLogger.notice( "URL banned", "%s" % url )
           self.__bannedUrls += [url]   
         self.__retry += 1
         gLogger.info( "Retry connection: ", "%d" % self.__retry )
         time.sleep( self.__retryDelay )
         self.__discoverURL()
         return self._connect()
       else:
         return S_ERROR( "Can't connect to %s: %s" % ( self.serviceURL, retVal ) )
   except Exception, e:
     return S_ERROR( "Can't connect to %s: %s" % ( self.serviceURL, e ) )
Exemple #3
0
  def __logRemoteQueryResponse( self, retVal, elapsedTime ):
    """
    Log the result of a query

    :type retVal: dictionary
    :param retVal: Return value of the query
    """
    if retVal[ 'OK' ]:
      argsString = "OK"
    else:
      argsString = "ERROR: %s" % retVal[ 'Message' ]
    gLogger.notice( "Returning response", "%s (%.2f secs) %s" % ( self.srv_getFormattedRemoteCredentials(),
                                                                elapsedTime, argsString ) )
Exemple #4
0
    def __logRemoteQuery(self, method, args):
        """
        Log the contents of a remote query

        :type method: string
        :param method: Method to log
        :type args: tuple
        :param args: Arguments of the method called
        """
        if self.srv_getCSOption("MaskRequestParams", True):
            argsString = "<masked>"
        else:
            argsString = "\n\t%s\n" % ",\n\t".join([str(arg)[:50] for arg in args])
        gLogger.notice("Executing action", "%s %s(%s)" % (self.srv_getFormattedRemoteCredentials(), method, argsString))
Exemple #5
0
    def __logRemoteQuery(self, method, args):
        """
    Log the contents of a remote query

    @type method: string
    @param method: Method to log
    @type args: tuple
    @param args: Arguments of the method called
    """
        if self.srv_getCSOption("MaskRequestParams", True):
            argsString = "<masked>"
        else:
            argsString = "\n\t%s\n" % ",\n\t".join([str(arg)[:50] for arg in args])
        gLogger.notice("Executing action", "%s %s(%s)" % (self.srv_getFormattedRemoteCredentials(), method, argsString))
Exemple #6
0
    def _connect(self):

        self.__discoverExtraCredentials()
        if not self.__initStatus['OK']:
            return self.__initStatus
        if self.__enableThreadCheck:
            self.__checkThreadID()
        gLogger.debug("Connecting to: %s" % self.serviceURL)
        try:
            transport = gProtocolDict[self.__URLTuple[0]]['transport'](
                self.__URLTuple[1:3], **self.kwargs)
            #the socket timeout is the default value which is 1.
            #later we increase to 5
            retVal = transport.initAsClient()
            if not retVal['OK']:
                if self.__retry < self.__nbOfRetry * self.__nbOfUrls - 1:
                    url = "%s://%s:%d/%s" % (
                        self.__URLTuple[0], self.__URLTuple[1],
                        int(self.__URLTuple[2]), self.__URLTuple[3])
                    if url not in self.__bannedUrls:
                        self.__bannedUrls += [url]
                        if len(self.__bannedUrls) < self.__nbOfUrls:
                            gLogger.notice(
                                "Non-responding URL temporarily banned",
                                "%s" % url)
                    self.__retry += 1
                    if self.__retryCounter == self.__nbOfRetry - 1:
                        transport.setSocketTimeout(
                            5
                        )  # we increase the socket timeout in case the network is not good
                    gLogger.info("Retry connection: ", "%d" % self.__retry)
                    if len(self.__bannedUrls) == self.__nbOfUrls:
                        self.__retryCounter += 1
                        self.__retryDelay = 3. / self.__nbOfUrls if self.__nbOfUrls > 1 else 2  # we run only one service! In that case we increase the retry delay.
                        gLogger.info(
                            "Waiting %f  second before retry all service(s)" %
                            self.__retryDelay)
                        time.sleep(self.__retryDelay)
                    self.__discoverURL()
                    return self._connect()
                else:
                    return retVal
        except Exception as e:
            return S_ERROR("Can't connect to %s: %s" %
                           (self.serviceURL, repr(e)))
        trid = getGlobalTransportPool().add(transport)
        return S_OK((trid, transport))
Exemple #7
0
    def __logRemoteQueryResponse(self, retVal, elapsedTime):
        """
        Log the result of a query

        :type retVal: dictionary
        :param retVal: Return value of the query
        """
        if retVal["OK"]:
            argsString = "OK"
        else:
            argsString = "ERROR: %s" % retVal["Message"]
            if "CallStack" in retVal:
                argsString += "\n" + "".join(retVal["CallStack"])
        gLogger.notice(
            "Returning response",
            "%s (%.2f secs) %s" % (self.srv_getFormattedRemoteCredentials(), elapsedTime, argsString),
        )
Exemple #8
0
  def _connect( self ):

    self.__discoverExtraCredentials()
    if not self.__initStatus[ 'OK' ]:
      return self.__initStatus
    if self.__enableThreadCheck:
      self.__checkThreadID()
    gLogger.debug( "Connecting to: %s" % self.serviceURL )
    try:
      transport = gProtocolDict[ self.__URLTuple[0] ][ 'transport' ]( self.__URLTuple[1:3], **self.kwargs )
      #the socket timeout is the default value which is 1.
      #later we increase to 5
      retVal = transport.initAsClient()
      if not retVal[ 'OK' ]:
        if self.__retry < self.__nbOfRetry * self.__nbOfUrls - 1:
          url = "%s://%s:%d/%s" % ( self.__URLTuple[0], self.__URLTuple[1], int( self.__URLTuple[2] ), self.__URLTuple[3] )
          if url not in self.__bannedUrls:
            self.__bannedUrls += [url]
            if len( self.__bannedUrls ) < self.__nbOfUrls:
              gLogger.notice( "Non-responding URL temporarily banned", "%s" % url )
          self.__retry += 1
          if self.__retryCounter == self.__nbOfRetry - 1:
            transport.setSocketTimeout( 5 ) # we increase the socket timeout in case the network is not good
          gLogger.info( "Retry connection: ", "%d" % self.__retry )
          if len(self.__bannedUrls) == self.__nbOfUrls:
            self.__retryCounter += 1
            self.__retryDelay = 3. / self.__nbOfUrls  if self.__nbOfUrls > 1 else 2  # we run only one service! In that case we increase the retry delay.
            gLogger.info( "Waiting %f  second before retry all service(s)" % self.__retryDelay )
            time.sleep( self.__retryDelay )
          self.__discoverURL()
          return self._connect()
        else:
          return retVal
    except Exception as e:
      return S_ERROR( "Can't connect to %s: %s" % ( self.serviceURL, repr( e ) ) )
    trid = getGlobalTransportPool().add( transport )
    return S_OK( ( trid, transport ) )
Exemple #9
0
  def _connect( self ):
    """ Establish the connection.
        It uses the URL discovered in __discoverURL.
        In case the connection cannot be established, __discoverURL
        is called again, and _connect calls itself.
        We stop after trying self.__nbOfRetry * self.__nbOfUrls

    """
    # Check if the useServerCertificate configuration changed
    # Note: I am not really sure that  all this block makes
    # any sense at all since all these variables are
    # evaluated in __discoverCredentialsToUse
    if gConfig.useServerCertificate() != self.__useCertificates:
      if self.__forceUseCertificates is None:
        self.__useCertificates = gConfig.useServerCertificate()
        self.kwargs[self.KW_USE_CERTIFICATES] = self.__useCertificates
        # The server certificate use context changed, rechecking the transport sanity
        result = self.__checkTransportSanity()
        if not result['OK']:
          return result

    # Take all the extra credentials
    self.__discoverExtraCredentials()
    if not self.__initStatus[ 'OK' ]:
      return self.__initStatus
    if self.__enableThreadCheck:
      self.__checkThreadID()

    gLogger.debug( "Connecting to: %s" % self.serviceURL )
    try:
      # Calls the transport method of the apropriate protocol.
      # self.__URLTuple[1:3] = [server name, port, System/Component]
      transport = gProtocolDict[ self.__URLTuple[0] ][ 'transport' ]( self.__URLTuple[1:3], **self.kwargs )
      #the socket timeout is the default value which is 1.
      #later we increase to 5
      retVal = transport.initAsClient()
      # If we have an issue connecting
      if not retVal[ 'OK' ]:
        # We try at most __nbOfRetry each URLs
        if self.__retry < self.__nbOfRetry * self.__nbOfUrls - 1:
          # Recompose the URL (why not using self.serviceURL ? )
          url = "%s://%s:%d/%s" % ( self.__URLTuple[0], self.__URLTuple[1], int( self.__URLTuple[2] ), self.__URLTuple[3] )
          # Add the url to the list of banned URLs if it is not already there. (Can it happen ? I don't think so)
          if url not in self.__bannedUrls:
            self.__bannedUrls += [url]
            # Why only printing in this case ?
            if len( self.__bannedUrls ) < self.__nbOfUrls:
              gLogger.notice( "Non-responding URL temporarily banned", "%s" % url )
          # Increment the retry couunter
          self.__retry += 1
          # If it is our last attempt for each URL, we increase the timeout
          if self.__retryCounter == self.__nbOfRetry - 1:
            transport.setSocketTimeout( 5 ) # we increase the socket timeout in case the network is not good
          gLogger.info( "Retry connection: ", "%d" % self.__retry )
          # If we tried all the URL, we increase the global counter (__retryCounter), and sleep
          if len(self.__bannedUrls) == self.__nbOfUrls:
            self.__retryCounter += 1
            self.__retryDelay = 3. / self.__nbOfUrls  if self.__nbOfUrls > 1 else 2  # we run only one service! In that case we increase the retry delay.
            gLogger.info( "Waiting %f seconds before retry all service(s)" % self.__retryDelay )
            time.sleep( self.__retryDelay )
          # rediscover the URL
          self.__discoverURL()
          # try to reconnect
          return self._connect()
        else:
          return retVal
    except Exception as e:
      gLogger.exception(lException = True, lExcInfo = True)
      return S_ERROR( "Can't connect to %s: %s" % ( self.serviceURL, repr( e ) ) )
    # We add the connection to the transport pool
    trid = getGlobalTransportPool().add( transport )

    return S_OK( ( trid, transport ) )
Exemple #10
0
  def _connect(self):
    """ Establish the connection.
        It uses the URL discovered in __discoverURL.
        In case the connection cannot be established, __discoverURL
        is called again, and _connect calls itself.
        We stop after trying self.__nbOfRetry * self.__nbOfUrls

    """
    # Check if the useServerCertificate configuration changed
    # Note: I am not really sure that  all this block makes
    # any sense at all since all these variables are
    # evaluated in __discoverCredentialsToUse
    if gConfig.useServerCertificate() != self.__useCertificates:
      if self.__forceUseCertificates is None:
        self.__useCertificates = gConfig.useServerCertificate()
        self.kwargs[self.KW_USE_CERTIFICATES] = self.__useCertificates
        # The server certificate use context changed, rechecking the transport sanity
        result = self.__checkTransportSanity()
        if not result['OK']:
          return result

    # Take all the extra credentials
    self.__discoverExtraCredentials()
    if not self.__initStatus['OK']:
      return self.__initStatus
    if self.__enableThreadCheck:
      self.__checkThreadID()

    gLogger.debug("Connecting to: %s" % self.serviceURL)
    try:
      # Calls the transport method of the apropriate protocol.
      # self.__URLTuple[1:3] = [server name, port, System/Component]
      transport = gProtocolDict[self.__URLTuple[0]]['transport'](self.__URLTuple[1:3], **self.kwargs)
      # the socket timeout is the default value which is 1.
      # later we increase to 5
      retVal = transport.initAsClient()
      # If we have an issue connecting
      if not retVal['OK']:
        # We try at most __nbOfRetry each URLs
        if self.__retry < self.__nbOfRetry * self.__nbOfUrls - 1:
          # Recompose the URL (why not using self.serviceURL ? )
          url = "%s://%s:%d/%s" % (self.__URLTuple[0], self.__URLTuple[1], int(self.__URLTuple[2]), self.__URLTuple[3])
          # Add the url to the list of banned URLs if it is not already there. (Can it happen ? I don't think so)
          if url not in self.__bannedUrls:
            self.__bannedUrls += [url]
            # Why only printing in this case ?
            if len(self.__bannedUrls) < self.__nbOfUrls:
              gLogger.notice("Non-responding URL temporarily banned", "%s" % url)
          # Increment the retry couunter
          self.__retry += 1
          # If it is our last attempt for each URL, we increase the timeout
          if self.__retryCounter == self.__nbOfRetry - 1:
            transport.setSocketTimeout(5)  # we increase the socket timeout in case the network is not good
          gLogger.info("Retry connection", ": %d to %s" % (self.__retry, self.serviceURL))
          # If we tried all the URL, we increase the global counter (__retryCounter), and sleep
          if len(self.__bannedUrls) == self.__nbOfUrls:
            self.__retryCounter += 1
            # we run only one service! In that case we increase the retry delay.
            self.__retryDelay = 3. / self.__nbOfUrls if self.__nbOfUrls > 1 else 2
            gLogger.info("Waiting %f seconds before retry all service(s)" % self.__retryDelay)
            time.sleep(self.__retryDelay)
          # rediscover the URL
          self.__discoverURL()
          # try to reconnect
          return self._connect()
        else:
          return retVal
    except Exception as e:
      gLogger.exception(lException=True, lExcInfo=True)
      return S_ERROR("Can't connect to %s: %s" % (self.serviceURL, repr(e)))
    # We add the connection to the transport pool
    trid = getGlobalTransportPool().add(transport)

    return S_OK((trid, transport))