Search in sources :

Example 56 with Node

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

the class CallIDAffinityBalancerAlgorithm method groupedFailover.

public synchronized void groupedFailover(Node oldNode, Node newNode) {
    try {
        if (oldNode != null && newNode != null) {
            int updatedRoutes = 0;
            for (String key : callIdMap.keySet()) {
                Node n = callIdMap.get(key);
                if (n.equals(oldNode)) {
                    callIdMap.replace(key, newNode);
                    updatedRoutes++;
                }
            }
            if (logger.isInfoEnabled()) {
                logger.info("Switchover occured where oldNode=" + oldNode + " and newNode=" + newNode + " with " + updatedRoutes + " updated routes.");
            }
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("Switchover failed where fromJvmRoute=" + oldNode + " and toJvmRoute=" + newNode);
            }
        }
    } catch (Throwable t) {
        if (logger.isInfoEnabled()) {
            logger.info("Switchover failed where fromJvmRoute=" + oldNode + " and toJvmRoute=" + newNode);
            logger.info("This is not a fatal failure, logging the reason for the failure ", t);
        }
    }
}
Also used : Node(org.mobicents.tools.heartbeat.api.Node) ListeningPoint(javax.sip.ListeningPoint)

Example 57 with Node

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

the class CallIDAffinityBalancerAlgorithm 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();
    Integer port = via.getPort();
    boolean found = false;
    if (invocationContext.sipNodeMap(isIpV6).containsKey(new KeySip(host, port, isIpV6)))
        found = true;
    if (logger.isDebugEnabled()) {
        logger.debug("external 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();
            }
        } else {
            String transportProperty = transport + "Port";
            port = Integer.parseInt(node.getProperties().get(transportProperty));
            if (via.getHost().equalsIgnoreCase(node.getIp()) || via.getPort() != port) {
                if (logger.isDebugEnabled()) {
                    logger.debug("changing retransmission via " + via + "setting new values " + node.getIp() + ":" + port);
                }
                try {
                    via.setHost(node.getIp());
                    via.setPort(port);
                    via.removeParameter("rport");
                    via.removeParameter("received");
                } 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 58 with Node

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

the class DefaultBalancerAlgorithm method processHttpRequest.

public synchronized Node processHttpRequest(HttpRequest request) {
    if (invocationContext.sipNodeMap(false).size() > 0) {
        String instanceId = getInstanceId(request);
        if (instanceId != null) {
            Node node = getNodeByInstanceId(instanceId);
            if (node != null)
                return node;
        }
        String httpSessionId = null;
        httpSessionId = getUrlParameters(request.getUri()).get("jsessionid");
        if (httpSessionId == null)
            httpSessionId = getParameterFromCookie(request, "jsessionid");
        if (httpSessionId != null) {
            int indexOfDot = httpSessionId.lastIndexOf('.');
            if (indexOfDot > 0 && indexOfDot < httpSessionId.length()) {
                String jvmRoute = httpSessionId.substring(indexOfDot + 1);
                Node node = balancerContext.jvmRouteToSipNode.get(jvmRoute);
                if (node != null) {
                    if (invocationContext.sipNodeMap(false).containsValue(node)) {
                        return node;
                    }
                }
            }
        }
        if (logger.isDebugEnabled())
            logger.debug("LB will send request to node accordingly RR algorithm");
        if (httpRequestIterator == null) {
            httpRequestIterator = invocationContext.sipNodeMap(false).values().iterator();
        }
        Node node = null;
        int count = invocationContext.sipNodeMap(false).size();
        while (count > 0) {
            while (httpRequestIterator.hasNext() && count > 0) {
                node = httpRequestIterator.next();
                if (node != null && !node.isGracefulShutdown() && !node.isBad()) {
                    return node;
                } else {
                    count--;
                }
            }
            if (!httpRequestIterator.hasNext())
                httpRequestIterator = invocationContext.sipNodeMap(false).values().iterator();
        }
        return null;
    } else {
        String unavailaleHost = getConfiguration().getHttpConfiguration().getUnavailableHost();
        if (unavailaleHost != null && unavailaleHost != "") {
            Node node = new Node(unavailaleHost, unavailaleHost);
            node.getProperties().put("httpPort", "" + 80);
            return node;
        } else {
            return null;
        }
    }
}
Also used : Node(org.mobicents.tools.heartbeat.api.Node)

Example 59 with Node

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

the class NodeRegisterImpl method forceRemovalInRegister.

/**
 * {@inheritDoc}
 */
public void forceRemovalInRegister(ArrayList<Node> ping) {
    for (Node pingNode : ping) {
        InvocationContext ctx = balancerRunner.getInvocationContext(pingNode.getProperties().get("version"));
        // ctx.nodes.remove(pingNode);
        String instanceId = pingNode.getProperties().get("Restcomm-Instance-Id");
        if (instanceId != null)
            ctx.httpNodeMap.remove(instanceId);
        Integer smppPort = null;
        if (pingNode.getProperties().get("smppPort") != null)
            smppPort = Integer.parseInt(pingNode.getProperties().get("smppPort"));
        if (smppPort != null)
            ctx.smppNodeMap.remove(new KeySmpp(pingNode));
        Boolean isIpV6 = LbUtils.isValidInet6Address(pingNode.getIp());
        ctx.sipNodeMap(isIpV6).remove(new KeySip(pingNode, isIpV6));
        boolean nodePresent = false;
        Iterator<Node> nodesIterator = balancerRunner.balancerContext.aliveNodes.iterator();
        while (nodesIterator.hasNext() && !nodePresent) {
            Node node = (Node) nodesIterator.next();
            if (node.equals(pingNode)) {
                nodePresent = true;
            }
        }
        // removal done afterwards to avoid ConcurrentModificationException when removing the node while goign through the iterator
        if (nodePresent) {
            balancerRunner.balancerContext.aliveNodes.remove(pingNode);
            ctx.balancerAlgorithm.nodeRemoved(pingNode);
            if (logger.isInfoEnabled()) {
                logger.info("NodeExpirationTimerTask Run NSync[" + pingNode + "] forcibly removed due to a clean shutdown of a node. Numbers of nodes present in the balancer : " + balancerRunner.balancerContext.aliveNodes.size());
            }
        }
    }
}
Also used : Node(org.mobicents.tools.heartbeat.api.Node)

Example 60 with Node

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

the class NodeRegisterImpl method stopRequestReceived.

@Override
public synchronized void stopRequestReceived(MessageEvent e, JsonObject json) {
    boolean isIpV6 = false;
    boolean was = false;
    logger.info("LB got stop request from Node : " + json);
    KeySession keySession = new KeySession(json.get(Protocol.SESSION_ID).toString());
    for (Entry<String, InvocationContext> ctxEntry : balancerRunner.contexts.entrySet()) {
        InvocationContext ctx = ctxEntry.getValue();
        Node nodePresent = null;
        Node nodePresentIpv4 = ctx.sessionNodeMap(true).get(keySession);
        Node nodePresentIpv6 = ctx.sessionNodeMap(false).get(keySession);
        if (nodePresentIpv4 != null) {
            isIpV6 = true;
            nodePresent = nodePresentIpv4;
        } else if (nodePresentIpv6 != null) {
            nodePresent = nodePresentIpv6;
        }
        if (nodePresent != null) {
            was = true;
            Node removedNode = ctx.sessionNodeMap(isIpV6).remove(keySession);
            KeySip keySip = new KeySip(removedNode, isIpV6);
            ctx.sipNodeMap(isIpV6).remove(keySip);
            String instanceId = nodePresent.getProperties().get(Protocol.RESTCOMM_INSTANCE_ID);
            if (instanceId != null)
                ctx.httpNodeMap.remove(instanceId);
            String smppPort = nodePresent.getProperties().get(Protocol.SMPP_PORT);
            if (smppPort != null)
                ctx.smppNodeMap.remove(new KeySmpp(nodePresent));
            balancerRunner.balancerContext.aliveNodes.remove(nodePresent);
            ctx.balancerAlgorithm.nodeRemoved(nodePresent);
            if (logger.isInfoEnabled())
                logger.info(" LB got STOP request from node : " + nodePresent + ". So it will be rmoved : " + balancerRunner.balancerContext.aliveNodes.size());
        }
    }
    if (!was) {
        logger.error("LB got shutdown request ( " + json + " ) from node which not pesent in maps : " + balancerRunner.getLatestInvocationContext().sipNodeMap(isIpV6));
    }
    if (e != null)
        writeResponse(e, HttpResponseStatus.OK, Protocol.STOP, Protocol.OK);
}
Also used : 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