Search in sources :

Example 36 with Node

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

the class UserBasedAlgorithm method processInternalResponse.

@Override
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()))
        return;
    if (logger.isDebugEnabled()) {
        logger.debug("internal response node found ? " + found);
    }
    if (!found) {
        String headerKey = extractHeaderKey(response);
        Node node = userToMap.get(headerKey);
        // if(node == null || !invocationContext.nodes.contains(node)) {
        if (node == null || !invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
            node = selectNewNode(node, headerKey, 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) Node(org.mobicents.tools.heartbeat.api.Node) Via(gov.nist.javax.sip.header.Via)

Example 37 with Node

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

the class UserBasedAlgorithm method processExternalRequest.

@Override
public Node processExternalRequest(Request request, Boolean isIpV6) {
    String headerKey = extractHeaderKey(request);
    Node node = null;
    if (balancerContext.regexMap != null && balancerContext.regexMap.size() != 0) {
        if (logger.isDebugEnabled())
            logger.debug("regexMap is not empty : " + balancerContext.regexMap);
        for (Entry<String, KeySip> entry : balancerContext.regexMap.entrySet()) {
            Pattern r = Pattern.compile(entry.getKey());
            Matcher m = r.matcher(headerKey);
            if (m.find()) {
                node = invocationContext.sipNodeMap(isIpV6).get(entry.getValue());
                if (node != null) {
                    if (logger.isDebugEnabled())
                        logger.debug("Found node for pattern : " + entry.getKey() + " and key :" + entry.getValue());
                    return node;
                } else {
                    if (logger.isDebugEnabled())
                        logger.debug("Node not found in the map of nodes. It is null. For pattern: " + entry.getKey() + " and key :" + entry.getValue());
                }
            }
        }
    } else {
        node = userToMap.get(headerKey);
        headerToTimestamps.put(headerKey, System.currentTimeMillis());
    }
    if (node == null || invocationContext.sipNodeMap(isIpV6).get(new KeySip(node, isIpV6)).isGracefulShutdown()) {
        // 
        if (lbConfig.getSipConfiguration().getTrafficRampupCyclePeriod() != null && lbConfig.getSipConfiguration().getMaxWeightIndex() != null)
            node = getNextRampUpNode(isIpV6);
        else
            node = nextAvailableNode(isIpV6);
        if (node == null)
            return null;
        userToMap.put(headerKey, node);
        if (logger.isDebugEnabled()) {
            logger.debug("No node found in the affinity map. It is null. We select new node: " + node);
        }
    } else {
        // if(!invocationContext.nodes.contains(node)) { // If the assigned node is now dead
        if (!invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
            // If the assigned node is now dead
            node = selectNewNode(node, headerKey, 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 : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Node(org.mobicents.tools.heartbeat.api.Node)

Example 38 with Node

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

the class WorstCaseUdpTestAffinityAlgorithm method processExternalResponse.

public void processExternalResponse(Response response, Boolean isIpV6) {
    Via via = (Via) response.getHeader(Via.NAME);
    String transport = via.getTransport().toLowerCase();
    String host = via.getHost();
    boolean found = false;
    // for(Node node : invocationContext.nodes) {
    for (Node node : invocationContext.sipNodeMap(isIpV6).values()) {
        if (node.getIp().equals(host))
            found = true;
    }
    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);
            try {
                via.setHost(node.getIp());
                String transportProperty = transport.toLowerCase() + "Port";
                Integer port = Integer.parseInt(node.getProperties().get(transportProperty));
                if (port == null)
                    throw new RuntimeException("No transport found for node " + node + " " + transportProperty);
                via.setPort(port);
            } catch (Exception e) {
                throw new RuntimeException("Error", e);
            }
            if (!ListeningPoint.UDP.equalsIgnoreCase(transport)) {
                via.setRPort();
            }
        }
    } else {
        if (earlyDialogWorstCase && response.getStatusCode() > 100) {
            String callId = ((SIPHeader) response.getHeader(headerName)).getValue();
            Node node = callIdMap.get(callId);
            for (int q = 0; q < 3; q++) {
                Node other = selectNewNode(node, callId);
                if (other != null && !other.equals(node)) {
                    node = other;
                    break;
                }
            }
            try {
                via.setHost(node.getIp());
                String transportProperty = transport.toLowerCase() + "Port";
                Integer port = Integer.parseInt(node.getProperties().get(transportProperty));
                if (port == null)
                    throw new RuntimeException("No transport found for node " + node + " " + transportProperty);
                via.setPort(port);
            } catch (Exception e) {
                throw new RuntimeException("Error", e);
            }
            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) ParseException(java.text.ParseException) ListeningPoint(javax.sip.ListeningPoint) Via(gov.nist.javax.sip.header.Via)

Example 39 with Node

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

the class WorstCaseUdpTestAffinityAlgorithm 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;
        }
    }
    logger.info("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 40 with Node

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

the class WorstCaseUdpTestAffinityAlgorithm method processExternalRequest.

public Node processExternalRequest(Request request, Boolean isIpV6) {
    String callId = ((SIPHeader) request.getHeader(headerName)).getValue();
    Node node;
    CSeqHeader cs = (CSeqHeader) request.getHeader(CSeqHeader.NAME);
    long cseq = cs.getSeqNumber();
    node = callIdMap.get(callId);
    callIdTimestamps.put(callId, System.currentTimeMillis());
    if (node == null) {
        // 
        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.nodes.contains(node)) { // If the assigned node is now dead
        if (!invocationContext.sipNodeMap(isIpV6).containsValue(node)) {
            // If the assigned node is now dead
            node = selectNewNode(node, callId);
        } else {
            // .. and we just leave it like that
            if (logger.isDebugEnabled()) {
                logger.debug("The assigned node in the affinity map is still alive: " + node);
            }
            if (!request.getMethod().equals("ACK")) {
                // for(Node n:invocationContext.nodes) {
                for (Node n : invocationContext.sipNodeMap(isIpV6).values()) {
                    if (!n.equals(node))
                        node = n;
                    break;
                }
            }
        }
    }
    setNodeA(callId + cseq, node);
    callIdMap.put(callId, node);
    // }
    return node;
}
Also used : CSeqHeader(javax.sip.header.CSeqHeader) SIPHeader(gov.nist.javax.sip.header.SIPHeader) 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