Search in sources :

Example 26 with Node

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

the class ServerControllerKube method startServer.

@Override
public void startServer() {
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            PodList pods = kube.pods().list();
            List<Pod> items = pods.getItems();
            for (Pod pod : items) {
                if (isValidPod(pod)) {
                    String currSessionId = getSessionId(pod);
                    Node currNode = activeNodes.get(currSessionId);
                    if (currNode == null) {
                        for (ContainerStatus status : pod.getStatus().getContainerStatuses()) {
                            if (status.getName().startsWith(nodeName) && status.getReady()) {
                                Node newNode = getNodeFromPod(pod);
                                JsonObject jsonObject = parser.parse(gson.toJson(new StartRequestPacket(newNode))).getAsJsonObject();
                                listener.startRequestReceived(null, jsonObject);
                                activeNodes.put(newNode.getProperties().get(Protocol.SESSION_ID), newNode);
                            }
                        }
                    } else if (!isGracefulShutdown(pod) || currNode.isGracefulShutdown()) {
                        JsonObject jsonObject = parser.parse(gson.toJson(new HeartbeatRequestPacket(currNode))).getAsJsonObject();
                        listener.heartbeatRequestReceived(null, jsonObject);
                    } else {
                        if (!currNode.isGracefulShutdown()) {
                            currNode.setGracefulShutdown(true);
                            JsonObject jsonObject = parser.parse(gson.toJson(new NodeShutdownRequestPacket(currNode))).getAsJsonObject();
                            listener.shutdownRequestReceived(null, jsonObject);
                        }
                    }
                }
            }
        }
    }, 2000, pullPeriod);
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Timer(java.util.Timer) TimerTask(java.util.TimerTask) Pod(io.fabric8.kubernetes.api.model.Pod) StartRequestPacket(org.mobicents.tools.heartbeat.api.StartRequestPacket) Node(org.mobicents.tools.heartbeat.api.Node) HeartbeatRequestPacket(org.mobicents.tools.heartbeat.api.HeartbeatRequestPacket) JsonObject(com.google.gson.JsonObject) List(java.util.List) PodList(io.fabric8.kubernetes.api.model.PodList) NodeShutdownRequestPacket(org.mobicents.tools.heartbeat.api.NodeShutdownRequestPacket)

Example 27 with Node

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

the class ClusterSubdomainAffinityAlgorithmTest method testPartnerFailover.

// Test actual failover by adding a lot of noise nodes and only 1 partner for the original
@Test
public void testPartnerFailover() throws Exception, ParseException {
    try {
        ClusterSubdomainAffinityAlgorithm algorithm = new ClusterSubdomainAffinityAlgorithm();
        algorithm.balancerContext = new BalancerContext();
        algorithm.balancerContext.lbConfig = new LoadBalancerConfiguration();
        algorithm.balancerContext.lbConfig.getSipConfiguration().getAlgorithmConfiguration().setSubclusterMap(failoverGroup);
        algorithm.balancerContext.smppToNodeAlgorithmClassName = algorithm.balancerContext.lbConfig.getSmppConfiguration().getSmppToNodeAlgorithmClass();
        algorithm.balancerContext.smppToProviderAlgorithmClassName = algorithm.balancerContext.lbConfig.getSmppConfiguration().getSmppToProviderAlgorithmClass();
        algorithm.balancerContext.algorithmClassName = ClusterSubdomainAffinityAlgorithm.class.getName();
        InvocationContext ctx = new InvocationContext("0", algorithm.balancerContext);
        // ctx.nodes = new CopyOnWriteArrayList<Node>();
        ctx.sipNodeMap(false).clear();
        for (int q = 0; q < 100; q++) {
            Node node = new Node("alphabeticalNoise" + q, "alphabeticalNoise" + q);
            ctx.sipNodeMap(false).put(new KeySip(node, false), node);
        }
        for (int q = 0; q < 100; q++) {
            Node node = new Node(q + "alphabeticalNoise" + q, q + "alphabeticalNoise" + q);
            ctx.sipNodeMap(false).put(new KeySip(node, false), node);
        }
        Node originalNode = new Node("original", "original");
        Node partnerNode = new Node("partner", "partner");
        // This is dead BalancerContext.balancerContext.nodes.add(originalNode);
        ctx.sipNodeMap(false).put(new KeySip(partnerNode, false), partnerNode);
        for (int q = 0; q < 100; q++) {
            Node node = new Node("nonParner" + q, "nonPartner" + q);
            ctx.sipNodeMap(false).put(new KeySip(node, false), node);
        }
        algorithm.callIdMap.put("cid", originalNode);
        Request request = SipFactory.getInstance().createMessageFactory().createRequest(inviteRequest);
        algorithm.loadSubclusters(failoverGroup);
        algorithm.invocationContext = ctx;
        Node resultNode = algorithm.processExternalRequest(request, false);
        assertEquals("partner", resultNode.getIp());
        originalNode = null;
        partnerNode = null;
        resultNode = null;
    } finally {
    // BalancerContext.balancerContext.nodes = null;
    }
}
Also used : Node(org.mobicents.tools.heartbeat.api.Node) LoadBalancerConfiguration(org.mobicents.tools.configuration.LoadBalancerConfiguration) Request(javax.sip.message.Request) Test(org.junit.Test)

Example 28 with Node

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

the class BalancerDispatcher method bindRequested.

@Override
public void bindRequested(Long sessionId, ServerConnectionImpl serverConnection, Pdu packet) {
    // InvocationContext invocationContext = balancerRunner.getLatestInvocationContext();
    balancerRunner.balancerContext.smppRequestsToServer.getAndIncrement();
    balancerRunner.incMessages();
    balancerRunner.balancerContext.smppRequestsProcessedById.get(packet.getCommandId()).incrementAndGet();
    balancerRunner.balancerContext.smppBytesToServer.addAndGet(packet.getCommandLength());
    serverSessions.put(sessionId, serverConnection);
    SmppSessionConfiguration sessionConfig = serverConnection.getConfig();
    if (!serverConnection.getConfig().isUseSsl())
        sessionConfig.setUseSsl(false);
    else
        sessionConfig.setUseSsl(!balancerRunner.balancerContext.terminateTLSTraffic);
    counterConnections.compareAndSet(Integer.MAX_VALUE, 0);
    Node currNode = ctx.smppToProviderBalancerAlgorithm.processBindToProvider();
    sessionConfig.setHost(currNode.getIp());
    sessionConfig.setPort(Integer.parseInt((String) currNode.getProperties().get("smppPort")));
    clientSessions.put(sessionId, new ClientConnectionImpl(sessionId, sessionConfig, this, monitorExecutor, balancerRunner, packet, currNode));
    handlerService.execute(new BinderRunnable(sessionId, packet, serverSessions, clientSessions, currNode, balancerRunner));
}
Also used : SmppSessionConfiguration(com.cloudhopper.smpp.SmppSessionConfiguration) BinderRunnable(org.mobicents.tools.smpp.balancer.impl.BinderRunnable) Node(org.mobicents.tools.heartbeat.api.Node) ClientConnectionImpl(org.mobicents.tools.smpp.balancer.impl.ClientConnectionImpl)

Example 29 with Node

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

the class BlackholeAppServer method start.

public void start() {
    timer = new Timer();
    try {
        socket = new DatagramSocket(port, InetAddress.getByName(lbAddress));
        try {
            tcpSocket = new ServerSocket(port);
            tcpThread = new Thread() {

                public void run() {
                    while (true) {
                        try {
                            sock = tcpSocket.accept();
                            new Thread() {

                                public void run() {
                                    while (true) {
                                        try {
                                            numUnitsReceived += sock.getInputStream().read(temp);
                                        } catch (IOException e) {
                                            return;
                                        }
                                    }
                                }
                            }.start();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            };
            tcpThread.start();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        thread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        socket.receive(packet);
                        numUnitsReceived++;
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    appServerNode = new Node(name, "127.0.0.1");
    appServerNode.getProperties().put("udpPort", "" + port);
    appServerNode.getProperties().put("tcpPort", "" + port);
}
Also used : SocketException(java.net.SocketException) Timer(java.util.Timer) UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) Node(org.mobicents.tools.heartbeat.api.Node) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Example 30 with Node

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

the class NodeTest method testEquals.

@Test
public void testEquals() throws Exception {
    Node node1 = new Node("mobicents.org", "192.168.0.10");
    node1.getProperties().put(Protocol.UDP_PORT, "5060");
    Node node2 = node1;
    assertTrue(node1.equals(node2));
    node2 = new Node("mobicents.org", "192.168.0.20");
    node2.getProperties().put(Protocol.UDP_PORT, "5060");
    assertFalse(node1.equals(node2));
    node2 = new Node("mobicents.org", "192.168.0.10");
    node2.getProperties().put(Protocol.UDP_PORT, "5060");
    assertTrue(node1.equals(node2));
    node1.getProperties().put(Protocol.TCP_PORT, "5060");
    node2.getProperties().put(Protocol.TCP_PORT, "5060");
    assertTrue(node1.equals(node2));
    node1.getProperties().put(Protocol.TLS_PORT, "5061");
    assertFalse(node1.equals(node2));
    node2.getProperties().put(Protocol.TLS_PORT, "5061");
    node2.getProperties().put(Protocol.WS_PORT, "5062");
    assertFalse(node1.equals(node2));
}
Also used : Node(org.mobicents.tools.heartbeat.api.Node) Test(org.junit.Test)

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