示例#1
0
def load_keyfunc():
  if settings.CARBONLINK_HASHING_KEYFUNC:
    module_path, func_name = settings.CARBONLINK_HASHING_KEYFUNC.rsplit(':', 1)
    log.cache("Using keyfunc %s found in %s" % (str(func_name), str(module_path)))
    return load_module(module_path, member=func_name)
  else:
    return lambda x: x
示例#2
0
 def query(self, metric):
     request = dict(type='cache-query', metric=metric)
     results = self.send_request(request)
     log.cache(
         "CarbonLink cache-query request for %s returned %d datapoints" %
         (metric, len(results['datapoints'])))
     return results['datapoints']
示例#3
0
 def set_metadata(self, metric, key, value):
     request = dict(type='set-metadata',
                    metric=metric,
                    key=key,
                    value=value)
     results = self.send_request(request)
     log.cache("CarbonLink set-metadata request received for %s:%s" %
               (metric, key))
     return results
示例#4
0
    def get_connection(self, host):
        # First try to take one out of the pool for this host
        (server, instance) = host
        port = self.ports[host]
        connectionPool = self.connections[host]
        try:
            return connectionPool.pop()
        except KeyError:
            pass #nothing left in the pool, gotta make a new connection

        log.cache("CarbonLink creating a new socket for %s" % str(host))
        connection = socket.socket()
        connection.settimeout(self.timeout)
        try:
            connection.connect( (server, port) )
        except:
            self.last_failure[host] = time.time()
            raise
        else:
            connection.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1 )
            return connection
示例#5
0
  def get_connection(self, host):
    # First try to take one out of the pool for this host
    (server, instance) = host
    port = self.ports[host]
    connectionPool = self.connections[host]
    try:
      return connectionPool.pop()
    except KeyError:
      pass #nothing left in the pool, gotta make a new connection

    log.cache("CarbonLink creating a new socket for %s" % str(host))
    connection = socket.socket()
    connection.settimeout(self.timeout)
    try:
      connection.connect( (server, port) )
    except:
      self.last_failure[host] = time.time()
      raise
    else:
      connection.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1 )
      return connection
示例#6
0
    def send_request(self, request):
        metric = request['metric']
        serialized_request = pickle.dumps(request, protocol=-1)
        len_prefix = struct.pack("!L", len(serialized_request))
        request_packet = len_prefix + serialized_request

        host = self.select_host(metric)
        conn = self.get_connection(host)
        log.cache("CarbonLink sending request for %s to %s" % (metric, str(host)))
        try:
            conn.sendall(request_packet)
            result = self.recv_response(conn)
        except:
            self.last_failure[host] = time.time()
            raise
        else:
            self.connections[host].add(conn)
            if 'error' in result:
                log.cache("Error getting data from cache: %s" % result['error'])
                raise CarbonLinkRequestError(result['error'])
            else:
                return result
示例#7
0
  def send_request(self, request):
    metric = request['metric']
    serialized_request = pickle.dumps(request, protocol=-1)
    len_prefix = struct.pack("!L", len(serialized_request))
    request_packet = len_prefix + serialized_request

    host = self.select_host(metric)
    conn = self.get_connection(host)
    log.cache("CarbonLink sending request for %s to %s" % (metric, str(host)))
    try:
      conn.sendall(request_packet)
      result = self.recv_response(conn)
    except:
      self.last_failure[host] = time.time()
      raise
    else:
      self.connections[host].add(conn)
      if 'error' in result:
        log.cache("Error getting data from cache: %s" % result['error'])
        raise CarbonLinkRequestError(result['error'])
      else:
        return result
示例#8
0
 def get_metadata(self, metric, key):
     request = dict(type='get-metadata', metric=metric, key=key)
     results = self.send_request(request)
     log.cache("CarbonLink get-metadata request received for %s:%s" % (metric, key))
     return results['value']
示例#9
0
 def query(self, metric):
     request = dict(type='cache-query', metric=metric)
     results = self.send_request(request)
     log.cache("CarbonLink cache-query request for %s returned %d datapoints" % (metric, len(results['datapoints'])))
     return results['datapoints']