Revision: 15693
Author: vladimir.ralev
Date: Thu Dec 9 18:04:20 2010
Log: Update Issue 1871
Patch + tests + docs
http://code.google.com/p/mobicents/source/detail?r=15693
Added:
/trunk/tools/sip-balancer/jar/src/main/java/org/mobicents/tools/sip/balancer/ClusterSubdomainAffinityAlgorithm.java
/trunk/tools/sip-balancer/jar/src/test/java/org/mobicents/tools/sip/balancer/ClusterSubdomainAffinityAlgorithmTest.java
Modified:
/trunk/tools/sip-balancer/docs/sources/src/main/resources/en-US/concept-section-SS_Load_Balancer.xml
=======================================
--- /dev/null
+++
/trunk/tools/sip-balancer/jar/src/main/java/org/mobicents/tools/sip/balancer/ClusterSubdomainAffinityAlgorithm.java
Thu Dec 9 18:04:20 2010
@@ -0,0 +1,362 @@
+package org.mobicents.tools.sip.balancer;
+
+import gov.nist.javax.sip.header.SIPHeader;
+import gov.nist.javax.sip.header.Via;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+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.ListeningPoint;
+import javax.sip.message.Request;
+import javax.sip.message.Response;
+
+public class ClusterSubdomainAffinityAlgorithm extends
DefaultBalancerAlgorithm {
+ private static Logger logger =
Logger.getLogger(ClusterSubdomainAffinityAlgorithm.class.getCanonicalName());
+
+ protected String headerName = "Call-ID";
+ protected ConcurrentHashMap<String, List<String>> nodeToNodeGroup = new
ConcurrentHashMap<String, List<String>>();
+ 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();
+ Integer port = via.getPort();
+ String transport = via.getTransport().toLowerCase();
+ boolean found = false;
+ for(SIPNode node : BalancerContext.balancerContext.nodes) {
+ if(node.getIp().equals(host)) {
+
if(port.equals(node.getProperties().get(transport+"Port"))) {
+ found = true;
+ }
+ }
+ }
+ if(logger.isLoggable(Level.FINEST)) {
+ logger.finest("external response node found ? " +
found);
+ }
+ 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);
+ String transportProperty = transport + "Port";
+ port = (Integer)
node.getProperties().get(transportProperty);
+ if(port == null) throw new RuntimeException("No transport found for
node " + node + " " + transportProperty);
+ if(logger.isLoggable(Level.FINEST)) {
+ logger.finest("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)
node.getProperties().get(transportProperty);
+ if(via.getHost().equalsIgnoreCase(node.getIp()) || via.getPort() !=
port) {
+ if(logger.isLoggable(Level.FINEST)) {
+ logger.finest("changing retransmission 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();
+ }
+ }
+ }
+ }
+ }
+
+ 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);
+ }
+ SIPNode oldNode = node;
+ List<String> alternativeNodes =
nodeToNodeGroup.get(oldNode.getIp());
+ for(SIPNode check : getBalancerContext().nodes) {
+ 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.fine("No alternatives found for " + oldNode + " from " +
alternativeNodes);
+
+ if(groupedFailover) {
+ // This will occur very rarely because we re-assign all calls from the
dead node in
+ // a single operation
+
+ node = leastBusyTargetNode(oldNode);
+ if(node == null) return null;
+ groupedFailover(oldNode, node);
+ } else {
+ node = nextAvailableNode();
+ if(node == null) {
+ if(logger.isLoggable(Level.FINEST)) {
+ logger.finest("no nodes available return 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 ClusterSubdomainAffinityAlgorithm 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);
+ }
+ String subclusterMap =
getProperties().getProperty("subclusterMap");
+ logger.info("Subcluster map: " + subclusterMap);
+ loadSubclusters(subclusterMap);
+ logger.info("Grouped failover is set to " +
this.groupedFailover);
+ }
+
+ public void loadSubclusters(String subclustersString) {
+ if(subclustersString != null) {
+ subclustersString = subclustersString.replaceAll(" ",
"");
+ String[] groups = subclustersString.split("\\)\\(");
+ for(int q=0; q<groups.length; q++) {
+ String group = groups[q];
+ group = group.replaceAll("\\(",
"").replaceAll("\\)","");
+ String[] hosts = group.split(",");
+ LinkedList<String> hostGroupList = new
LinkedList<String>();
+ for(String host:hosts) {
+ if(host.length()>0) {
+
if(hostGroupList.contains(host)) {
+ throw new RuntimeException("Duplicate host " + host + " in " +
hosts);
+ }
+ hostGroupList.add(host);
+ }
+ }
+ for(String host:hosts) {
+ List<String> tmp = new
LinkedList<String>(hostGroupList);
+ tmp.remove(host);
+ nodeToNodeGroup.put(host, tmp);
+ }
+ }
+ }
+ }
+
+ public String dumpSubcluster() {
+ String result = "";
+ for(String host:nodeToNodeGroup.keySet()) {
+ String mapped = host + ": " + nodeToNodeGroup.get(host);
+ result += mapped + "\n";
+ }
+ return result;
+ }
+
+ 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);
+ }
+ }
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/tools/sip-balancer/jar/src/test/java/org/mobicents/tools/sip/balancer/ClusterSubdomainAffinityAlgorithmTest.java
Thu Dec 9 18:04:20 2010
@@ -0,0 +1,85 @@
+package org.mobicents.tools.sip.balancer;
+
+import java.text.ParseException;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import javax.sip.SipFactory;
+import javax.sip.message.Request;
+
+import junit.framework.TestCase;
+
+
+public class ClusterSubdomainAffinityAlgorithmTest extends TestCase {
+ static final String failoverGroup = "(original,partner)";
+ static final String groups2 =
"(127.0.0.1,4.322.5345.5,234235235435345) (55345435345,546,345,23,52
63456,546546 ,46345
34,45)(3435345345345345,43543fdg,ffdgdfg.f.f.f.ff.f.f.f..f,f,f,5,gggderf,dfgafa)";
+ static final String groups = "(a,b)(c,d,g)";
+ static final String inviteRequest = "INVITE sip:[email protected]
SIP/2.0\r\n"+
+ "To: sip:[email protected]\r\n"+
+ "From: sip:[email protected] ;tag=1234\r\n"+
+ "Call-ID: cid\r\n"+
+ "CSeq: 9 INVITE\r\n"+
+ "Via: SIP/2.0/UDP 135.180.130.133\r\n"+
+ "Content-Type: application/sdp\r\n"+
+ "\r\n"+
+ "v=0\r\n"+
+ "o=mhandley 29739 7272939 IN IP4 126.5.4.3\r\n" +
+ "c=IN IP4 135.180.130.88\r\n" +
+ "m=video 3227 RTP/AVP 31\r\n" +
+ "m=audio 4921 RTP/AVP 12\r\n" +
+ "a=rtpmap:31 LPC\r\n\r\n";
+
+ // Test if groups are parsed correctly
+ public void testGroupsLoad() throws Exception, ParseException {
+ ClusterSubdomainAffinityAlgorithm algorithm = new
ClusterSubdomainAffinityAlgorithm();
+ algorithm.loadSubclusters(groups);
+ String data = algorithm.dumpSubcluster();
+ String[] lines = data.split("\n");
+ System.out.println(data);
+ assertEquals(5, lines.length);
+ assertTrue(data.contains("a:"));
+ assertTrue(data.contains("b:"));
+ assertTrue(data.contains("c:"));
+ assertTrue(data.contains("d:"));
+ assertTrue(data.contains("g:"));
+ }
+
+ // Test validation, no duplicate nodes allowed
+ public void testGroupsLoad2() throws Exception, ParseException {
+ ClusterSubdomainAffinityAlgorithm algorithm = new
ClusterSubdomainAffinityAlgorithm();
+ try {
+ algorithm.loadSubclusters(groups2);
+ } catch (Exception e) {
+ return;
+ }
+ fail("Excpeted excpetion");
+ }
+
+ // Test actual failover by adding a lot of noise nodes and only 1 partner
for the original
+ public void partnerFailoverTest() throws Exception, ParseException {
+ try {
+ BalancerContext.balancerContext.nodes = new
CopyOnWriteArrayList<SIPNode>();
+ ClusterSubdomainAffinityAlgorithm algorithm = new
ClusterSubdomainAffinityAlgorithm();
+ for(int q=0;q<100;q++) {
+ BalancerContext.balancerContext.nodes.add(new
SIPNode("alphabeticalNoise"+q, "alphabeticalNoise"+q));
+ }
+ for(int q=0;q<100;q++) {
+ BalancerContext.balancerContext.nodes.add(new
SIPNode(q+"alphabeticalNoise"+q, q+"alphabeticalNoise"+q));
+ }
+ SIPNode originalNode = new SIPNode("original",
"original");
+ SIPNode partnerNode = new SIPNode("partner", "partner");
+
+ // This is dead
BalancerContext.balancerContext.nodes.add(originalNode);
+ BalancerContext.balancerContext.nodes.add(partnerNode);
+ for(int q=0;q<100;q++) {
+ BalancerContext.balancerContext.nodes.add(new
SIPNode("nonParner"+q, "nonPartner"+q));
+ }
+ algorithm.callIdMap.put("cid", originalNode);
+ Request request =
SipFactory.getInstance().createMessageFactory().createRequest(inviteRequest);
+ algorithm.loadSubclusters(failoverGroup);
+ SIPNode resultNode =
algorithm.processExternalRequest(request);
+ assertEquals("partner", resultNode.getIp());
+ } finally {
+ BalancerContext.balancerContext.nodes = null;
+ }
+ }
+}
=======================================
---
/trunk/tools/sip-balancer/docs/sources/src/main/resources/en-US/concept-section-SS_Load_Balancer.xml
Wed Dec 1 17:55:07 2010
+++
/trunk/tools/sip-balancer/docs/sources/src/main/resources/en-US/concept-section-SS_Load_Balancer.xml
Thu Dec 9 18:04:20 2010
@@ -477,6 +477,19 @@
<listitem>
<para>This algorithm is distributable and is similar to the
previous algorithm, but it attempts to keep session affinity even when the
cluster nodes are removed or added, which would normally cause hash values
to point to different nodes.</para>
</listitem>
+ </varlistentry>
+ <varlistentry
id="sslb-binary-SIP_Load_Balancer-Configuration_Properties_File_3">
+
<term>org.mobicents.tools.sip.balancer.ClusterSubdomainAffinityAlgorithm
</term>
+ <listitem>
+ <para>This algorithm is not distributable, but supports
grouping server nodes to act
+ as a subcluster. Any call of a node that belongs to a cluster
group will be preferentially
+ failed over to a node from the same group. To configure a
group you can just add the
+ <literal>subclusterMap</literal> property in the load
balancer properties and listing
+ the IP addresses of the nodes. The groups are enclosed in
parentheses and the IP
+ addresses are separate by commas as follows:</para>
+ <screen>subclusterMap=( 192.168.1.1, 192.168.1.2 ) (
10.10.10.10, 20.20.20.20, 30.30.30.30)</screen>
+ <para>The nodes specified in a group do not have to alive
and nodes that are not specified are still allowed to join the cluster.
Otherwise the algorthim behaves exactly as the default Call-ID affinity
algorthim.</para>
+ </listitem>
</varlistentry>
</variablelist></para>
</section>