Search in sources :

Example 1 with StickinessPolicyTO

use of com.cloud.legacymodel.to.LoadBalancerTO.StickinessPolicyTO in project cosmic by MissionCriticalCloud.

the class HAProxyConfigurator method getRulesForPool.

private List<String> getRulesForPool(final LoadBalancerTO lbTO, final boolean keepAliveEnabled) {
    StringBuilder sb = new StringBuilder();
    final String poolName = sb.append(lbTO.getSrcIp().replace(".", "_")).append('-').append(lbTO.getSrcPort()).toString();
    final String publicIP = lbTO.getSrcIp();
    final int publicPort = lbTO.getSrcPort();
    final String algorithm = lbTO.getAlgorithm();
    final List<String> result = new ArrayList<>();
    // Add line like this: "listen  65_37_141_30-80 65.37.141.30:80"
    sb = new StringBuilder();
    sb.append("listen ").append(poolName).append(" ").append(publicIP).append(":").append(publicPort);
    result.add(sb.toString());
    sb = new StringBuilder();
    sb.append("\t").append("balance ").append(algorithm);
    sb.append("\n\t").append("timeout client ").append(lbTO.getClientTimeout());
    sb.append("\n\t").append("timeout server ").append(lbTO.getServerTimeout());
    result.add(sb.toString());
    int i = 0;
    Boolean destsAvailable = false;
    final String stickinessSubRule = getLbSubRuleForStickiness(lbTO);
    final List<String> dstSubRule = new ArrayList<>();
    final List<String> dstWithCookieSubRule = new ArrayList<>();
    for (final DestinationTO dest : lbTO.getDestinations()) {
        // Add line like this: "server  65_37_141_30-80_3 10.1.1.4:80 check"
        if (dest.isRevoked()) {
            continue;
        }
        sb = new StringBuilder();
        sb.append("\t").append("server ").append(poolName).append("_").append(Integer.toString(i++)).append(" ").append(dest.getDestIp()).append(":").append(dest.getDestPort()).append(" check");
        if (lbTO.getLbProtocol() != null && lbTO.getLbProtocol().equals("tcp-proxy")) {
            sb.append(" send-proxy");
        }
        dstSubRule.add(sb.toString());
        if (stickinessSubRule != null) {
            sb.append(" cookie ").append(dest.getDestIp().replace(".", "_")).append('-').append(dest.getDestPort()).toString();
            dstWithCookieSubRule.add(sb.toString());
        }
        destsAvailable = true;
    }
    Boolean httpbasedStickiness = false;
    // Attach stickiness sub rule only if the destinations are available
    if (stickinessSubRule != null && destsAvailable == true) {
        for (final StickinessPolicyTO stickinessPolicy : lbTO.getStickinessPolicies()) {
            if (stickinessPolicy == null) {
                continue;
            }
            if (StickinessMethodType.LBCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName()) || StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
                httpbasedStickiness = true;
            }
        }
        if (httpbasedStickiness) {
            result.addAll(dstWithCookieSubRule);
        } else {
            result.addAll(dstSubRule);
        }
        result.add(stickinessSubRule);
    } else {
        result.addAll(dstSubRule);
    }
    if (stickinessSubRule != null && !destsAvailable) {
        s_logger.warn("Haproxy stickiness policy for lb rule: " + lbTO.getSrcIp() + ":" + lbTO.getSrcPort() + ": Not Applied, cause:  backends are unavailable");
    }
    if (publicPort == NetUtils.HTTP_PORT && !keepAliveEnabled || httpbasedStickiness) {
        sb = new StringBuilder();
        sb.append("\t").append("mode http");
        result.add(sb.toString());
        sb = new StringBuilder();
        sb.append("\t").append("option httpclose");
        result.add(sb.toString());
    }
    result.add(blankLine);
    return result;
}
Also used : ArrayList(java.util.ArrayList) DestinationTO(com.cloud.legacymodel.to.LoadBalancerTO.DestinationTO) StickinessPolicyTO(com.cloud.legacymodel.to.LoadBalancerTO.StickinessPolicyTO)

Example 2 with StickinessPolicyTO

use of com.cloud.legacymodel.to.LoadBalancerTO.StickinessPolicyTO in project cosmic by MissionCriticalCloud.

the class HAProxyConfigurator method getLbSubRuleForStickiness.

private String getLbSubRuleForStickiness(final LoadBalancerTO lbTO) {
    int i = 0;
    if (lbTO.getStickinessPolicies() == null) {
        return null;
    }
    final StringBuilder sb = new StringBuilder();
    for (final StickinessPolicyTO stickinessPolicy : lbTO.getStickinessPolicies()) {
        if (stickinessPolicy == null) {
            continue;
        }
        final List<Pair<String, String>> paramsList = stickinessPolicy.getParams();
        i++;
        if (StickinessMethodType.LBCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
            /* Default Values */
            // optional
            String cookieName = null;
            // optional
            String mode = "insert ";
            // optional
            Boolean indirect = false;
            // optional
            Boolean nocache = false;
            // optional
            Boolean postonly = false;
            // optional
            StringBuilder domainSb = null;
            for (final Pair<String, String> paramKV : paramsList) {
                final String key = paramKV.first();
                final String value = paramKV.second();
                if ("cookie-name".equalsIgnoreCase(key)) {
                    cookieName = value;
                }
                if ("mode".equalsIgnoreCase(key)) {
                    mode = value;
                }
                if ("domain".equalsIgnoreCase(key)) {
                    if (domainSb == null) {
                        domainSb = new StringBuilder();
                    }
                    domainSb = domainSb.append("domain ");
                    domainSb.append(value).append(" ");
                }
                if ("indirect".equalsIgnoreCase(key)) {
                    indirect = true;
                }
                if ("nocache".equalsIgnoreCase(key)) {
                    nocache = true;
                }
                if ("postonly".equalsIgnoreCase(key)) {
                    postonly = true;
                }
            }
            if (cookieName == null) {
                // re-check all haproxy mandatory params
                final StringBuilder tempSb = new StringBuilder();
                String srcip = lbTO.getSrcIp();
                if (srcip == null) {
                    srcip = "TESTCOOKIE";
                }
                tempSb.append("lbcooki_").append(srcip.hashCode()).append("_").append(lbTO.getSrcPort());
                cookieName = tempSb.toString();
            }
            sb.append("\t").append("cookie ").append(cookieName).append(" ").append(mode).append(" ");
            if (indirect) {
                sb.append("indirect ");
            }
            if (nocache) {
                sb.append("nocache ");
            }
            if (postonly) {
                sb.append("postonly ");
            }
            if (domainSb != null) {
                sb.append(domainSb).append(" ");
            }
        } else if (StickinessMethodType.SourceBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
            // Default Values
            // optional
            String tablesize = "200k";
            // optional
            String expire = "30m";
            // Overwrite default values with the stick parameters
            for (final Pair<String, String> paramKV : paramsList) {
                final String key = paramKV.first();
                final String value = paramKV.second();
                if ("tablesize".equalsIgnoreCase(key)) {
                    tablesize = value;
                }
                if ("expire".equalsIgnoreCase(key)) {
                    expire = value;
                }
            }
            sb.append("\t").append("stick-table type ip size ").append(tablesize).append(" expire ").append(expire);
            sb.append("\n\t").append("stick on src");
        } else if (StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
            // optional
            String cookieName = null;
            // optional
            String length = "52";
            // optional
            String holdtime = "3h";
            // optional
            String mode = null;
            // optional
            Boolean requestlearn = false;
            // optional
            Boolean prefix = false;
            for (final Pair<String, String> paramKV : paramsList) {
                final String key = paramKV.first();
                final String value = paramKV.second();
                if ("cookie-name".equalsIgnoreCase(key)) {
                    cookieName = value;
                }
                if ("length".equalsIgnoreCase(key)) {
                    length = value;
                }
                if ("holdtime".equalsIgnoreCase(key)) {
                    holdtime = value;
                }
                if ("mode".equalsIgnoreCase(key)) {
                    mode = value;
                }
                if ("request-learn".equalsIgnoreCase(key)) {
                    requestlearn = true;
                }
                if ("prefix".equalsIgnoreCase(key)) {
                    prefix = true;
                }
            }
            if (cookieName == null) {
                // Re-check all haproxy mandatory params
                final StringBuilder tempSb = new StringBuilder();
                String srcip = lbTO.getSrcIp();
                if (srcip == null) {
                    srcip = "TESTCOOKIE";
                }
                tempSb.append("appcookie_").append(srcip.hashCode()).append("_").append(lbTO.getSrcPort());
                cookieName = tempSb.toString();
            }
            sb.append("\t").append("appsession ").append(cookieName).append(" len ").append(length).append(" timeout ").append(holdtime).append(" ");
            if (prefix) {
                sb.append("prefix ");
            }
            if (requestlearn) {
                sb.append("request-learn").append(" ");
            }
            if (mode != null) {
                sb.append("mode ").append(mode).append(" ");
            }
        } else {
            // Error is silently swallowed. Not supposed to reach here, validation of methods are done at the higher layer
            s_logger.warn("Haproxy stickiness policy for lb rule: " + lbTO.getSrcIp() + ":" + lbTO.getSrcPort() + ": Not Applied, cause:invalid method ");
            return null;
        }
    }
    if (i == 0) {
        return null;
    }
    return sb.toString();
}
Also used : StickinessPolicyTO(com.cloud.legacymodel.to.LoadBalancerTO.StickinessPolicyTO) Pair(com.cloud.legacymodel.utils.Pair)

Aggregations

StickinessPolicyTO (com.cloud.legacymodel.to.LoadBalancerTO.StickinessPolicyTO)2 DestinationTO (com.cloud.legacymodel.to.LoadBalancerTO.DestinationTO)1 Pair (com.cloud.legacymodel.utils.Pair)1 ArrayList (java.util.ArrayList)1