Search in sources :

Example 21 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class CallIDAffinityBalancerAlgorithm method leastBusyTargetNode.

protected synchronized Node leastBusyTargetNode(Node deadNode) {
    HashMap<Node, Integer> nodeUtilization = new HashMap<Node, Integer>();
    for (Node node : callIdMap.values()) {
        Integer n = nodeUtilization.get(node);
        if (n == null) {
            nodeUtilization.put(node, 0);
        } else {
            nodeUtilization.put(node, n + 1);
        }
    }
    int minUtil = Integer.MAX_VALUE;
    Node minUtilNode = null;
    for (Node node : nodeUtilization.keySet()) {
        Integer util = nodeUtilization.get(node);
        if (!node.equals(deadNode) && (util < minUtil)) {
            minUtil = util;
            minUtilNode = node;
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Least busy node selected " + minUtilNode + " with " + minUtil + " calls");
    }
    return minUtilNode;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Node(org.mobicents.tools.heartbeat.api.Node) ListeningPoint(javax.sip.ListeningPoint)

Example 22 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class CallIDAffinityBalancerAlgorithm method processInternalResponse.

public void processInternalResponse(Response response, Boolean isIpV6) {
    Via via = (Via) response.getHeader(Via.NAME);
    String transport = via.getTransport().toLowerCase();
    String host = via.getHost();
    Integer port = via.getPort();
    boolean found = false;
    Node senderNode = (Node) ((ResponseExt) response).getApplicationData();
    if (logger.isDebugEnabled()) {
        logger.debug("internal response checking sendernode " + senderNode + " or Via host:port " + host + ":" + port);
    }
    if (senderNode != null && invocationContext.sipNodeMap(isIpV6).containsValue(senderNode))
        found = true;
    else if (invocationContext.sipNodeMap(isIpV6).containsKey(new KeySip(host, port, isIpV6)))
        found = true;
    else if (balancerContext.responsesStatusCodeNodeRemoval.contains(response.getStatusCode()))
        // && response.getReasonPhrase().equals(balancerContext.responsesReasonNodeRemoval))
        return;
    if (logger.isDebugEnabled()) {
        logger.debug("internal response node found ? " + found);
    }
    if (!found) {
        String callId = ((SIPHeader) response.getHeader(headerName)).getValue();
        Node node = callIdMap.get(callId);
        // if(node == null || !invocationContext.nodes.contains(node)) {
        if (node == null || !invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
            node = selectNewNode(node, callId, isIpV6);
            String transportProperty = transport + "Port";
            port = Integer.parseInt(node.getProperties().get(transportProperty));
            if (port == null)
                throw new RuntimeException("No transport found for node " + node + " " + transportProperty);
            if (logger.isDebugEnabled()) {
                logger.debug("changing via " + via + "setting new values " + node.getIp() + ":" + port);
            }
            try {
                via.setHost(node.getIp());
                via.setPort(port);
            } catch (Exception e) {
                throw new RuntimeException("Error setting new values " + node.getIp() + ":" + port + " on via " + via, e);
            }
            // need to reset the rport for reliable transports
            if (!ListeningPoint.UDP.equalsIgnoreCase(transport)) {
                via.setRPort();
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SIPHeader(gov.nist.javax.sip.header.SIPHeader) Node(org.mobicents.tools.heartbeat.api.Node) Via(gov.nist.javax.sip.header.Via)

Example 23 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class CallIDAffinityBalancerAlgorithm method processExternalRequest.

public Node processExternalRequest(Request request, Boolean isIpV6) {
    String callId = ((SIPHeader) request.getHeader(headerName)).getValue();
    Node node;
    node = callIdMap.get(callId);
    callIdTimestamps.put(callId, System.currentTimeMillis());
    if (node == null) {
        // 
        if (lbConfig.getSipConfiguration().getTrafficRampupCyclePeriod() != null && lbConfig.getSipConfiguration().getMaxWeightIndex() != null)
            node = getNextRampUpNode(isIpV6);
        else
            node = nextAvailableNode(isIpV6);
        if (node == null)
            return null;
        callIdMap.put(callId, node);
        if (logger.isDebugEnabled()) {
            logger.debug("No node found in the affinity map. It is null. We select new node: " + node);
        }
    } else {
        if (!invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
            // If the assigned node is now dead
            // if(!invocationContext.nodes.contains(node)) { // If the assigned node is now dead
            node = selectNewNode(node, callId, isIpV6);
        } else {
            // .. and we just leave it like that
            if (logger.isDebugEnabled()) {
                logger.debug("The assigned node in the affinity map is still alive: " + node);
            }
        }
    }
    // }
    return node;
}
Also used : SIPHeader(gov.nist.javax.sip.header.SIPHeader) Node(org.mobicents.tools.heartbeat.api.Node)

Example 24 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class ClusterSubdomainAffinityAlgorithm method selectNewNode.

protected Node selectNewNode(Node node, String callId, Boolean isIpV6) {
    if (logger.isDebugEnabled()) {
        logger.debug("The assigned node has died. This is the dead node: " + node);
    }
    Node oldNode = node;
    List<String> alternativeNodes = nodeToNodeGroup.get(oldNode.getIp());
    // for(Node check : invocationContext.nodes)  {
    for (Node check : invocationContext.sipNodeMap(isIpV6).values()) {
        for (String alt : alternativeNodes) if (check.getIp().equals(alt)) {
            groupedFailover(oldNode, check);
            logger.info("Grouped failover to partner node from " + oldNode + " to " + check);
            return check;
        }
    }
    logger.info("No alternatives found for " + oldNode + " from " + alternativeNodes);
    return super.selectNewNode(oldNode, callId, isIpV6);
}
Also used : Node(org.mobicents.tools.heartbeat.api.Node)

Example 25 with Node

use of org.mobicents.tools.heartbeat.api.Node in project load-balancer by RestComm.

the class Helper method getNode.

public static Node getNode() {
    Node node = new Node("TestNodeHeartbeat", "127.0.0.1");
    Map<String, String> map = new HashMap<>();
    map.put("httpPort", "8080");
    map.put("sslPort", "8081");
    map.put("udpPort", "5060");
    map.put("tcpPort", "5060");
    map.put("tlsPort", "5061");
    map.put("wsPort", "5062");
    map.put("wssPort", "5063");
    map.put("sessionId", "" + System.currentTimeMillis());
    map.put("Restcomm-Instance-Id", "q1w2e3r4t5y6");
    map.put("version", "0");
    map.put("heartbeatPort", "2222");
    node.getProperties().putAll(map);
    return node;
}
Also used : HashMap(java.util.HashMap) Node(org.mobicents.tools.heartbeat.api.Node)

Aggregations

Node (org.mobicents.tools.heartbeat.api.Node)70 ListeningPoint (javax.sip.ListeningPoint)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Via (gov.nist.javax.sip.header.Via)7 HashMap (java.util.HashMap)7 SIPHeader (gov.nist.javax.sip.header.SIPHeader)6 ParseException (java.text.ParseException)6 ArrayList (java.util.ArrayList)6 ViaHeader (javax.sip.header.ViaHeader)6 RouteHeader (javax.sip.header.RouteHeader)5 SIPResponse (gov.nist.javax.sip.message.SIPResponse)4 UnknownHostException (java.net.UnknownHostException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 InvalidArgumentException (javax.sip.InvalidArgumentException)4 SipException (javax.sip.SipException)4 SipURI (javax.sip.address.SipURI)4 RecordRouteHeader (javax.sip.header.RecordRouteHeader)4 ToHeader (javax.sip.header.ToHeader)4 Response (javax.sip.message.Response)4 Test (org.junit.Test)4