You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2014/09/17 21:13:53 UTC

git commit: TS-2945: add target port number support to the balancer plugin

Repository: trafficserver
Updated Branches:
  refs/heads/master c12a77cdf -> a9905459d


TS-2945: add target port number support to the balancer plugin


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/a9905459
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/a9905459
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/a9905459

Branch: refs/heads/master
Commit: a9905459d93127ce603baa69845d655dfc2d5afa
Parents: c12a77c
Author: James Peach <jp...@apache.org>
Authored: Tue Sep 16 12:01:42 2014 -0700
Committer: James Peach <jp...@apache.org>
Committed: Wed Sep 17 12:13:34 2014 -0700

----------------------------------------------------------------------
 CHANGES                                     |  2 +
 doc/reference/plugins/balancer.en.rst       |  5 ++
 lib/ts/ink_inet.h                           |  6 +--
 plugins/experimental/balancer/balancer.cc   | 68 ++++++++++++++++++++++--
 plugins/experimental/balancer/balancer.h    | 11 +++-
 plugins/experimental/balancer/hash.cc       | 15 +++---
 plugins/experimental/balancer/roundrobin.cc |  8 +--
 7 files changed, 94 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a9905459/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 96b694a..584c5b0 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache Traffic Server 5.2.0
 
+  *) [TS-2945] Add target port number support to the balancer plugin.
+
   *) [TS-3070] Make span configuration work consistently across platforms.
 
   *) [TS-3076] Fix minor strtok_r errors.

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a9905459/doc/reference/plugins/balancer.en.rst
----------------------------------------------------------------------
diff --git a/doc/reference/plugins/balancer.en.rst b/doc/reference/plugins/balancer.en.rst
index a31c1aa..239d1b2 100644
--- a/doc/reference/plugins/balancer.en.rst
+++ b/doc/reference/plugins/balancer.en.rst
@@ -33,6 +33,11 @@ to the ``--policy`` option is a comma-separated list of keywords.
 The first keyword is the name of a balancing policy. The subsequent
 keywords are used to refine the requested policy.
 
+The remaining plugin arguments are balancer targets. Typically,
+these will be the host names of origin servers that requests should
+be balanced across. The target name may contain a colon-separated
+port number.
+
 Hash Balancing Policy
 ---------------------
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a9905459/lib/ts/ink_inet.h
----------------------------------------------------------------------
diff --git a/lib/ts/ink_inet.h b/lib/ts/ink_inet.h
index bfc7ca5..c5cd248 100644
--- a/lib/ts/ink_inet.h
+++ b/lib/ts/ink_inet.h
@@ -27,10 +27,10 @@
 
 #include <netinet/in.h>
 #include <netdb.h>
-#include <ink_memory.h>
 #include <sys/socket.h>
-#include <ts/ink_apidefs.h>
-#include <ts/TsBuffer.h>
+#include "ink_memory.h"
+#include "ink_apidefs.h"
+#include "TsBuffer.h"
 
 #define INK_GETHOSTBYNAME_R_DATA_SIZE 1024
 #define INK_GETHOSTBYADDR_R_DATA_SIZE 1024

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a9905459/plugins/experimental/balancer/balancer.cc
----------------------------------------------------------------------
diff --git a/plugins/experimental/balancer/balancer.cc b/plugins/experimental/balancer/balancer.cc
index 88b1c21..f3226ca 100644
--- a/plugins/experimental/balancer/balancer.cc
+++ b/plugins/experimental/balancer/balancer.cc
@@ -28,6 +28,9 @@
 #include <string.h>
 #include <iterator>
 
+// Using ink_inet API is cheating, but I was too lazy to write new IPv6 address parsing routines ;)
+#include "ink_inet.h"
+
 // The policy type is the first comma-separated token.
 static BalancerInstance *
 MakeBalancerInstance(const char * opt)
@@ -43,7 +46,46 @@ MakeBalancerInstance(const char * opt)
     TSError("balancer: invalid balancing policy '%.*s'", (int)len, opt);
     return NULL;
   }
+}
+
+static BalancerTarget
+MakeBalancerTarget(const char * strval)
+{
+  BalancerTarget target = BalancerTarget();
+
+  union {
+    struct sockaddr_storage storage;
+    struct sockaddr sa;
+  } address;
+
+  memset(&address, 0, sizeof(address));
+
+  // First, check whether we have an address literal.
+  if (ats_ip_pton(strval, &address.sa) == 0) {
+    char namebuf[INET6_ADDRSTRLEN];
+
+    target.port = ats_ip_port_host_order(&address.sa);
+    target.name =ats_ip_ntop(&address.sa, namebuf, sizeof(namebuf));
+  } else {
+    const char * colon = strrchr(strval, ':');
+
+    if (colon) {
+      size_t len = std::distance(strval, colon);
+
+      target.port = atoi(colon + 1);
+      target.name = std::string(strval, len);
+    } else {
+      target.port = 0;
+      target.name = strval;
+    }
+  }
+
+  if (target.port > INT16_MAX) {
+    TSError("balancer: ignoring invalid port number for target '%s'", strval);
+    target.port = 0;
+  }
 
+  return target;
 }
 
 TSReturnCode
@@ -100,7 +142,14 @@ TSRemapNewInstance(int argc, char * argv[], void ** instance, char * errbuf, int
 
   // Pick up the remaining options as balance targets.
   for (int i = optind; i < argc; ++i) {
-    balancer->push_target(argv[i]);
+    BalancerTarget target = MakeBalancerTarget(argv[i]);
+
+    balancer->push_target(target);
+    if (target.port) {
+      TSDebug("balancer", "added target -> %s:%u", target.name.c_str(), target.port);
+    } else {
+      TSDebug("balancer", "added target -> %s", target.name.c_str());
+    }
   }
 
   *instance = balancer;
@@ -117,18 +166,27 @@ TSRemapStatus
 TSRemapDoRemap(void * instance, TSHttpTxn txn, TSRemapRequestInfo * rri)
 {
   BalancerInstance * balancer = (BalancerInstance *)instance;
-  const char * target;
+  const BalancerTarget& target = balancer->balance(txn, rri);
 
-  target = balancer->balance(txn, rri);
   if (TSIsDebugTagSet("balancer")) {
     char * url;
     int len;
 
     url = TSHttpTxnEffectiveUrlStringGet(txn, &len);
-    TSDebug("balancer", "%s <- %.*s", target, len, url);
+    if (target.port) {
+      TSDebug("balancer", "%s:%u <- %.*s", target.name.c_str(), target.port, len, url);
+    } else {
+      TSDebug("balancer", "%s <- %.*s", target.name.c_str(), len, url);
+    }
+
     TSfree(url);
   }
 
-  TSUrlHostSet(rri->requestBufp, rri->requestUrl, target, -1);
+  TSUrlHostSet(rri->requestBufp, rri->requestUrl, target.name.data(), target.name.size());
+
+  if (target.port) {
+    TSUrlPortSet(rri->requestBufp, rri->requestUrl, target.port);
+  }
+
   return TSREMAP_DID_REMAP;
 }

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a9905459/plugins/experimental/balancer/balancer.h
----------------------------------------------------------------------
diff --git a/plugins/experimental/balancer/balancer.h b/plugins/experimental/balancer/balancer.h
index 710e629..f7cf3f1 100644
--- a/plugins/experimental/balancer/balancer.h
+++ b/plugins/experimental/balancer/balancer.h
@@ -26,6 +26,7 @@
 
 #include <ts/ts.h>
 #include <ts/remap.h>
+#include <string>
 
 // Return the length of a string literal.
 template <int N> unsigned
@@ -33,11 +34,17 @@ lengthof(const char (&)[N]) {
   return N - 1;
 }
 
+struct BalancerTarget
+{
+  std::string name;
+  unsigned    port;
+};
+
 struct BalancerInstance
 {
   virtual ~BalancerInstance() {}
-  virtual void push_target(const char *) = 0;
-  virtual const char * balance(TSHttpTxn, TSRemapRequestInfo *) = 0;
+  virtual void push_target(const BalancerTarget&) = 0;
+  virtual const BalancerTarget& balance(TSHttpTxn, TSRemapRequestInfo *) = 0;
 };
 
 BalancerInstance * MakeHashBalancer(const char *);

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a9905459/plugins/experimental/balancer/hash.cc
----------------------------------------------------------------------
diff --git a/plugins/experimental/balancer/hash.cc b/plugins/experimental/balancer/hash.cc
index 9d2a204..d90a02c 100644
--- a/plugins/experimental/balancer/hash.cc
+++ b/plugins/experimental/balancer/hash.cc
@@ -46,11 +46,12 @@ struct md5_key {
 
   md5_key() {}
 
-  md5_key(const std::string& str, unsigned i) {
+  md5_key(const BalancerTarget& target, unsigned i) {
     MD5_CTX ctx;
 
     MD5_Init(&ctx);
-    MD5_Update(&ctx, str.data(), str.size());
+    MD5_Update(&ctx, target.name.data(), target.name.size());
+    MD5_Update(&ctx, &target.port, sizeof(target.port));
     MD5_Update(&ctx, &i, sizeof(i));
     MD5_Final(this->key, &ctx);
   }
@@ -126,8 +127,8 @@ done:
 
 struct HashBalancer : public BalancerInstance
 {
-  typedef std::map<md5_key, std::string>  hash_ring_type;
-  typedef std::vector<HashComponent>      hash_part_type;
+  typedef std::map<md5_key, BalancerTarget> hash_ring_type;
+  typedef std::vector<HashComponent>        hash_part_type;
 
   enum { iterations = 10 };
 
@@ -135,13 +136,13 @@ struct HashBalancer : public BalancerInstance
     this->hash_parts.push_back(HashTxnUrl);
   }
 
-  void push_target(const char * target) {
+  void push_target(const BalancerTarget& target) {
     for (unsigned i = 0; i < iterations; ++i) {
       this->hash_ring.insert(std::make_pair(md5_key(target, i), target));
     }
   }
 
-  const char * balance(TSHttpTxn txn, TSRemapRequestInfo * rri) {
+  const BalancerTarget& balance(TSHttpTxn txn, TSRemapRequestInfo * rri) {
     md5_key key;
     MD5_CTX ctx;
     hash_ring_type::const_iterator loc;
@@ -166,7 +167,7 @@ struct HashBalancer : public BalancerInstance
       loc = this->hash_ring.begin();
     }
 
-    return loc->second.c_str();
+    return loc->second;
   }
 
   hash_ring_type hash_ring;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a9905459/plugins/experimental/balancer/roundrobin.cc
----------------------------------------------------------------------
diff --git a/plugins/experimental/balancer/roundrobin.cc b/plugins/experimental/balancer/roundrobin.cc
index fa8900f..9bf0d6f 100644
--- a/plugins/experimental/balancer/roundrobin.cc
+++ b/plugins/experimental/balancer/roundrobin.cc
@@ -35,15 +35,15 @@ struct RoundRobinBalancer : public BalancerInstance
   RoundRobinBalancer() : targets(), next(0) {
   }
 
-  void push_target(const char * target) {
+  void push_target(const BalancerTarget& target) {
     this->targets.push_back(target);
   }
 
-  const char * balance(TSHttpTxn, TSRemapRequestInfo *) {
-    return this->targets[++next % this->targets.size()].c_str();
+  const BalancerTarget& balance(TSHttpTxn, TSRemapRequestInfo *) {
+    return this->targets[++next % this->targets.size()];
   }
 
-  std::vector<std::string> targets;
+  std::vector<BalancerTarget> targets;
   unsigned next;
 };