예제 #1
0
def sendKeepAlive(conf):
    """
        Sends a message for each connected node in order to keep the
        connection alive
    """
    log.log(cpc.util.log.TRACE, "Starting keep alive thread")

    #first get the network topology. by doing this we know that the network
    # topology is fetched and resides in the cache.
    #since we are later on fetching all connections from the connection pool
    # there will be no connection left to do this call thus we must be sure
    # that its already cached.
    ServerToServerMessage.getNetworkTopology()

    while True:
        log.log(cpc.util.log.TRACE, "Starting to send keep alive")
        sentRequests = 0
        for node in conf.getNodes().nodes.itervalues():
            if node.isConnectedOutbound():
                try:
                    connections = ServerConnectionPool().getAllConnections(
                        node)

                    for conn in connections:
                        try:
                            message = DirectServerMessage(node, conf)
                            message.conn = conn
                            message.pingServer(node.getId())
                            log.log(cpc.util.log.TRACE,
                                    "keepAlive sent to %s" % node.toString())

                        except ServerConnectionError:
                            log.error("Connection to %s is broken" %
                                      node.toString())

                    sentRequests += 1

                except ConnectionPoolEmptyError:
                    #this just mean that no connections are available in the
                    # pool i.e they are used and communicated on thus we do
                    # not need to send keep alive messages on them
                    continue

        keepAliveInterval = conf.getKeepAliveInterval()
        log.log(
            cpc.util.log.TRACE, "sent keep alive to %s nodes "
            "will resend in %s seconds" % (sentRequests, keepAliveInterval))
        time.sleep(keepAliveInterval)
예제 #2
0
def sendKeepAlive(conf):
    """
        Sends a message for each connected node in order to keep the
        connection alive
    """
    log.log(cpc.util.log.TRACE,"Starting keep alive thread")

    #first get the network topology. by doing this we know that the network
    # topology is fetched and resides in the cache.
    #since we are later on fetching all connections from the connection pool
    # there will be no connection left to do this call thus we must be sure
    # that its already cached.
    ServerToServerMessage.getNetworkTopology()

    while True:
        log.log(cpc.util.log.TRACE,"Starting to send keep alive")
        sentRequests = 0
        for node in conf.getNodes().nodes.itervalues():
            if node.isConnectedOutbound():
                try:
                    connections = ServerConnectionPool().getAllConnections(node)

                    for conn in connections:
                        try:
                            message = DirectServerMessage(node,conf)
                            message.conn = conn
                            message.pingServer(node.getId())
                            log.log(cpc.util.log.TRACE,"keepAlive sent to %s"%node
                            .toString())

                        except ServerConnectionError:
                            log.error("Connection to %s is broken"%node.toString())

                    sentRequests+=1

                except ConnectionPoolEmptyError:
                    #this just mean that no connections are available in the
                    # pool i.e they are used and communicated on thus we do
                    # not need to send keep alive messages on them
                    continue

        keepAliveInterval = conf.getKeepAliveInterval()
        log.log(cpc.util.log.TRACE,"sent keep alive to %s nodes "
                                   "will resend in %s seconds"%(sentRequests,
                                                         keepAliveInterval))
        time.sleep(keepAliveInterval)
    def requestNetworkTopology(topology,serverState=None):
        """
        Asks each neigbouring node for their network topology

        inputs:
            topology:Nodes The list of the topology generated so far
            serverState:ServerState
                if provided worker states are fetched.
                since this method is called by getNetworkTopology() which in turn
                is called from places where we do not pass (and don't want) the serverState
                we provide this option. Also it is not needed as the calling server always
                knows the most up to date state of its own workers.

        """
        conf = ServerConf()
        thisNode = Node.getSelfNode(conf)
        thisNode.setNodes(conf.getNodes())
        topology.addNode(thisNode)
        if serverState:
            thisNode.workerStates = WorkerStateHandler.getConnectedWorkers(serverState.getWorkerStates())

        for node in thisNode.getNodes().nodes.itervalues():
            if topology.exists(node.getId()) == False:
                #connect to correct node
                if node.isConnected():
                    try:
                        clnt = DirectServerMessage(node,conf=conf)
                        #send along the current topology
                        rawresp = clnt.networkTopology(topology)
                        processedResponse = ProcessedResponse(rawresp)
                        topology = processedResponse.getData()
                    except ServerConnectionError as e:
                        #we cannot connect to the node,
                        # and its marked as unreachable
                        #we must still add it to the topology
                        log.error("node %s unreachable when asking for network "
                                  "topology: error was %s"%(node.getId(),e.__str__()))
                        topology.addNode(node)

                #todo notify in topology that this node is not connected?
        return topology