Revision: 15082
Author: vladimir.ralev
Date: Sun Nov  7 19:01:10 2010
Log: Update Issue 2082
Implemenations. The WorstCaseAffitity algorithm will send each new request to a new node in a live cluster to test the replication under switchover conditions.
http://code.google.com/p/mobicents/source/detail?r=15082

Added:
/trunk/tools/sip-balancer/jar/src/main/java/org/mobicents/tools/sip/balancer/UDPPacketForwarder.java /trunk/tools/sip-balancer/jar/src/main/java/org/mobicents/tools/sip/balancer/WorstCaseUdpTestAffinityAlgorithm.java

=======================================
--- /dev/null
+++ /trunk/tools/sip-balancer/jar/src/main/java/org/mobicents/tools/sip/balancer/UDPPacketForwarder.java Sun Nov 7 19:01:10 2010
@@ -0,0 +1,57 @@
+package org.mobicents.tools.sip.balancer;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+
+public class UDPPacketForwarder {
+       int fromPort;
+       int toPort;
+       String bindAddress;
+       boolean running;
+       DatagramSocket fromSocket;
+       Thread worker;
+       public UDPPacketForwarder(int fromPort, int toPort, String bind) {
+               this.fromPort = fromPort;
+               this.toPort = toPort;
+               this.bindAddress = bind;
+       }
+
+       public void start() {
+               try {
+ fromSocket = new DatagramSocket(fromPort, InetAddress.getByName(bindAddress));
+                       running = true;
+                       worker = new Thread() {
+                               @Override
+                               public void run() {
+                                       while(running) {
+                                               DatagramPacket packet = new 
DatagramPacket(new byte[3000], 3000);
+                                               try {
+                                                       
fromSocket.receive(packet);
+                                                       packet.setPort(toPort);
+                                                       fromSocket.send(packet);
+                                               } catch (IOException e) {
+                                                       e.printStackTrace();
+                                               }
+                                       }
+
+                               }
+                       };
+                       worker.start();
+               } catch (Exception e) {
+                       throw new RuntimeException("Error", e);
+               }
+       }
+
+       public void stop() {
+               running = false;
+               try {
+                       worker.interrupt();
+                       fromSocket.close();
+               } catch (Exception e) {
+                       //e.printStackTrace();
+               }
+
+       }
+}
=======================================
--- /dev/null
+++ /trunk/tools/sip-balancer/jar/src/main/java/org/mobicents/tools/sip/balancer/WorstCaseUdpTestAffinityAlgorithm.java Sun Nov 7 19:01:10 2010
@@ -0,0 +1,306 @@
+package org.mobicents.tools.sip.balancer;
+
+import gov.nist.javax.sip.header.SIPHeader;
+import gov.nist.javax.sip.header.Via;
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.sip.address.SipURI;
+import javax.sip.header.RouteHeader;
+import javax.sip.message.Request;
+import javax.sip.message.Response;
+
+/**
+ * This algorthim will send each new transaction to a new node triggering all replication corner cases
+ *
+ * @author vladimirralev
+ *
+ */
+public class WorstCaseUdpTestAffinityAlgorithm extends DefaultBalancerAlgorithm {
+       public SIPNode processAssignedExternalRequest(Request request,
+                       SIPNode assignedNode) {
+               if(request.getMethod().contains("ACK")) return assignedNode;
+               RouteHeader route = (RouteHeader) 
request.getHeader(RouteHeader.NAME);
+               SipURI uri = null;
+               if(route != null) {
+                       uri = (SipURI) route.getAddress().getURI();
+               } else {
+                       uri = (SipURI) request.getRequestURI();
+               }
+
+               for(SIPNode node:getBalancerContext().nodes) {
+                       if(!node.equals(assignedNode)) {
+                               try {
+                                       uri.setHost(node.getIp());
+                               } catch (ParseException e) {
+                                       throw new RuntimeException("Error chainging 
assiged node", e);
+                               }
+                               Integer port = (Integer) 
node.getProperties().get("udpPort");
+                               uri.setPort(port);
+                               String callId = ((SIPHeader) 
request.getHeader(headerName))
+                                       .getValue();
+                               callIdMap.put(callId, node);
+                               return node;
+                       }
+               }
+               return assignedNode;
+       }
+
+ private static Logger logger = Logger.getLogger(WorstCaseUdpTestAffinityAlgorithm.class.getCanonicalName());
+
+       protected String headerName = "Call-ID";
+ protected ConcurrentHashMap<String, SIPNode> callIdMap = new ConcurrentHashMap<String, SIPNode>(); + protected ConcurrentHashMap<String, Long> callIdTimestamps = new ConcurrentHashMap<String, Long>();
+       protected AtomicInteger nextNodeCounter = new AtomicInteger(0);
+       protected int maxCallIdleTime = 500;
+       protected boolean groupedFailover = false;
+
+       protected Timer cacheEvictionTimer = new Timer();
+
+       public void processInternalRequest(Request request) {
+               logger.fine("internal request");
+       }
+
+       public void processInternalResponse(Response request) {
+               logger.fine("internal response");
+       }
+
+       public void processExternalResponse(Response response) {
+               BalancerContext balancerContext = getBalancerContext();
+               Via via = (Via) response.getHeader(Via.NAME);
+               String host = via.getHost();
+               boolean found = false;
+               for(SIPNode node : BalancerContext.balancerContext.nodes) {
+                       if(node.getIp().equals(host)) found = true;
+               }
+               if(!found) {
+                       String callId = ((SIPHeader) 
response.getHeader(headerName))
+                       .getValue();
+                       SIPNode node = callIdMap.get(callId);
+                       if(node == null || 
!balancerContext.nodes.contains(node)) {
+                               node = selectNewNode(node, callId);
+                               try {
+                                       via.setHost(node.getIp());
+                                       String transportProperty = 
via.getTransport().toLowerCase() + "Port";
+                                       Integer port = (Integer) 
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);
+                               }
+                       }
+               }
+       }
+
+       public SIPNode processExternalRequest(Request request) {
+               String callId = ((SIPHeader) request.getHeader(headerName))
+               .getValue();
+               SIPNode node;
+               node = callIdMap.get(callId);
+               callIdTimestamps.put(callId, System.currentTimeMillis());
+
+               BalancerContext balancerContext = getBalancerContext();
+
+               if(node == null) { //
+                       node = nextAvailableNode();
+                       if(node == null) return null;
+                       callIdMap.put(callId, node);
+                       if(logger.isLoggable(Level.FINEST)) {
+ logger.finest("No node found in the affinity map. It is null. We select new node: " + node);
+               }
+               } else {
+ if(!balancerContext.nodes.contains(node)) { // If the assigned node is now dead
+                               node = selectNewNode(node, callId);
+                       } else { // ..else it's alive and we can route there
+                               //.. and we just leave it like that
+                               if(logger.isLoggable(Level.FINEST)) {
+ logger.finest("The assigned node in the affinity map is still alive: " + node);
+                       }
+                       }
+               }
+
+// Don't try to be smart here, the retransmissions of BYE will come and will not know where to go.
+//             if(request.getMethod().equals("BYE")) {
+//                     callIdMap.remove(callId);
+//                     callIdTimestamps.remove(callId);
+//             }
+               return node;
+
+       }
+
+       protected SIPNode selectNewNode(SIPNode node, String callId) {
+               if(logger.isLoggable(Level.FINEST)) {
+ logger.finest("The assigned node has died. This is the dead node: " + node);
+       }
+               if(groupedFailover) {
+ // This will occur very rarely because we re-assign all calls from the dead node in
+                       // a single operation
+                       SIPNode oldNode = node;
+                       node = leastBusyTargetNode(oldNode);
+                       if(node == null) return null;
+                       groupedFailover(oldNode, node);
+               } else {
+                       node = nextAvailableNode();
+                       if(node == null) return null;
+                       callIdMap.put(callId, node);
+               }
+
+               if(logger.isLoggable(Level.FINEST)) {
+               logger.finest("So, we must select new node: " + node);
+       }
+               return node;
+       }
+
+       protected synchronized SIPNode nextAvailableNode() {
+               BalancerContext balancerContext = getBalancerContext();
+               if(balancerContext.nodes.size() == 0) return null;
+               int nextNode = nextNodeCounter.incrementAndGet();
+               nextNode %= balancerContext.nodes.size();
+               return balancerContext.nodes.get(nextNode);
+       }
+
+       protected synchronized SIPNode leastBusyTargetNode(SIPNode deadNode) {
+               BalancerContext balancerContext = getBalancerContext();
+ HashMap<SIPNode, Integer> nodeUtilization = new HashMap<SIPNode, Integer>();
+               for(SIPNode 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;
+               SIPNode minUtilNode = null;
+               for(SIPNode 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;
+       }
+
+       public void init() {
+ String maxTimeInCacheString = getProperties().getProperty("callIdAffinityMaxTimeInCache");
+               if(maxTimeInCacheString != null) {
+                       this.maxCallIdleTime = 
Integer.parseInt(maxTimeInCacheString);
+               }
+ logger.info("Call Idle Time is " + this.maxCallIdleTime + " seconds. Inactive calls will be evicted.");
+
+               final WorstCaseUdpTestAffinityAlgorithm thisAlgorithm = this;
+               this.cacheEvictionTimer.schedule(new TimerTask() {
+
+                       @Override
+                       public void run() {
+                               try {
+                                       synchronized (thisAlgorithm) {
+                                               ArrayList<String> oldCalls = new 
ArrayList<String>();
+                                               Iterator<String> keys = 
callIdTimestamps.keySet().iterator();
+                                               while(keys.hasNext()) {
+                                                       String key = 
keys.next();
+                                                       long time = 
callIdTimestamps.get(key);
+                                                       
if(System.currentTimeMillis() - time > 1000*maxCallIdleTime) {
+                                                               
oldCalls.add(key);
+                                                       }
+                                               }
+                                               for(String key : oldCalls) {
+                                                       callIdMap.remove(key);
+                                                       
callIdTimestamps.remove(key);
+                                               }
+                                               if(oldCalls.size()>0) {
+ logger.info("Reaping idle calls... Evicted " + oldCalls.size() + " calls.");
+                                               }}
+                               } catch (Exception e) {
+ logger.log(Level.WARNING, "Failed to clean up old calls. If you continue to se this message frequestly and the memory is growing, report this problem.", e);
+                               }
+
+                       }
+               }, 0, 6000);
+
+ String groupFailoverProperty = getProperties().getProperty("callIdAffinityGroupFailover");
+               if(groupFailoverProperty != null) {
+                       this.groupedFailover = 
Boolean.parseBoolean(groupFailoverProperty);
+               }
+               logger.info("Grouped failover is set to " + 
this.groupedFailover);
+       }
+
+       public void assignToNode(String id, SIPNode node) {
+               callIdMap.put(id, node);
+               callIdTimestamps.put(id, System.currentTimeMillis());
+       }
+
+       @Override
+       public void jvmRouteSwitchover(String fromJvmRoute, String toJvmRoute) {
+               try {
+ SIPNode oldNode = getBalancerContext().jvmRouteToSipNode.get(fromJvmRoute); + SIPNode newNode = getBalancerContext().jvmRouteToSipNode.get(toJvmRoute);
+                       if(oldNode != null && newNode != null) {
+                               int updatedRoutes = 0;
+                               for(String key : callIdMap.keySet()) {
+                                       SIPNode n = callIdMap.get(key);
+                                       if(n.equals(oldNode)) {
+                                               callIdMap.replace(key, newNode);
+                                               updatedRoutes++;
+                                       }
+                               }
+                               if(logger.isLoggable(Level.INFO)) {
+ logger.info("Switchover occured where fromJvmRoute=" + fromJvmRoute + " and toJvmRoute=" + toJvmRoute + " with " +
+                                                       updatedRoutes + " updated 
routes.");
+                               }
+                       } else {
+                               if(logger.isLoggable(Level.INFO)) {
+ logger.info("Switchover failed where fromJvmRoute=" + fromJvmRoute + " and toJvmRoute=" + toJvmRoute);
+                               }
+                       }
+               } catch (Throwable t) {
+                       if(logger.isLoggable(Level.INFO)) {
+ logger.info("Switchover failed where fromJvmRoute=" + fromJvmRoute + " and toJvmRoute=" + toJvmRoute); + logger.log(Level.INFO, "This is not a fatal failure, logging the reason for the failure ", t);
+                       }
+               }
+       }
+
+ synchronized public void groupedFailover(SIPNode oldNode, SIPNode newNode) {
+               try {
+                       if(oldNode != null && newNode != null) {
+                               int updatedRoutes = 0;
+                               for(String key : callIdMap.keySet()) {
+                                       SIPNode n = callIdMap.get(key);
+                                       if(n.equals(oldNode)) {
+                                               callIdMap.replace(key, newNode);
+                                               updatedRoutes++;
+                                       }
+                               }
+                               if(logger.isLoggable(Level.INFO)) {
+ logger.info("Switchover occured where oldNode=" + oldNode + " and newNode=" + newNode + " with " +
+                                                       updatedRoutes + " updated 
routes.");
+                               }
+                       } else {
+                               if(logger.isLoggable(Level.INFO)) {
+ logger.info("Switchover failed where fromJvmRoute=" + oldNode + " and toJvmRoute=" + newNode);
+                               }
+                       }
+               } catch (Throwable t) {
+                       if(logger.isLoggable(Level.INFO)) {
+ logger.info("Switchover failed where fromJvmRoute=" + oldNode + " and toJvmRoute=" + newNode); + logger.log(Level.INFO, "This is not a fatal failure, logging the reason for the failure ", t);
+                       }
+               }
+       }
+
+}

Reply via email to