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();
}
}
}
}
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;
}
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();
}
}
}
}
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;
}
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;
}
Aggregations