You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by so...@apache.org on 2016/05/20 23:11:18 UTC

[trafficserver] 22/28: TS-4020 Use cache-key URL for some plugins

This is an automated email from the ASF dual-hosted git repository.

sorber pushed a commit to branch 6.2.x
in repository https://git-dual.apache.org/repos/asf/trafficserver.git

commit fcc6f4cdaeb8065ff0bbcbfa10e97fcc1db3d05c
Author: Leif Hedstrom <zw...@apache.org>
AuthorDate: Thu Apr 28 16:09:48 2016 -0600

    TS-4020 Use cache-key URL for some plugins
    
    cache_promote and background_fetch both use the pristine URL
    as the hash for various internal lookup features. This works
    well, except when it doesn't. For example, when mixed with
    using the cachekey.so plugin.
    
    This changes the behavior to use the cachekey URL. This is not
    awesomely efficient at this point, but a future fix will use a
    new API that allows introspection into the actual cache key used
    by the ATS core instead.
    
    (cherry picked from commit 3511a6630d00029d60e8c1728d7f06decabc0362)
---
 .../background_fetch/background_fetch.cc           | 62 +++++++++++++---------
 .../experimental/cache_promote/cache_promote.cc    | 20 ++++++-
 2 files changed, 56 insertions(+), 26 deletions(-)

diff --git a/plugins/experimental/background_fetch/background_fetch.cc b/plugins/experimental/background_fetch/background_fetch.cc
index 013e4d5..b2a4bd5 100644
--- a/plugins/experimental/background_fetch/background_fetch.cc
+++ b/plugins/experimental/background_fetch/background_fetch.cc
@@ -219,9 +219,11 @@ private:
 bool
 BgFetchData::initialize(TSMBuffer request, TSMLoc req_hdr, TSHttpTxn txnp)
 {
+  struct sockaddr const *ip = TSHttpTxnClientAddrGet(txnp);
+  bool ret = false;
+
   TSAssert(TS_NULL_MLOC == hdr_loc);
   TSAssert(TS_NULL_MLOC == url_loc);
-  struct sockaddr const *ip = TSHttpTxnClientAddrGet(txnp);
 
   if (ip) {
     if (ip->sa_family == AF_INET) {
@@ -238,39 +240,51 @@ BgFetchData::initialize(TSMBuffer request, TSMLoc req_hdr, TSHttpTxn txnp)
 
   hdr_loc = TSHttpHdrCreate(mbuf);
   if (TS_SUCCESS == TSHttpHdrCopy(mbuf, hdr_loc, request, req_hdr)) {
-    TSMLoc purl;
-    int len;
+    TSMLoc p_url;
 
     // Now copy the pristine request URL into our MBuf
-    if ((TS_SUCCESS == TSHttpTxnPristineUrlGet(txnp, &request, &purl)) &&
-        (TS_SUCCESS == TSUrlClone(mbuf, request, purl, &url_loc))) {
-      char *url = TSUrlStringGet(mbuf, url_loc, &len);
-
-      _url.append(url, len); // Save away the URL for later use when acquiring lock
+    if (TS_SUCCESS == TSHttpTxnPristineUrlGet(txnp, &request, &p_url)) {
+      if (TS_SUCCESS == TSUrlClone(mbuf, request, p_url, &url_loc)) {
+        TSMLoc c_url = TS_NULL_MLOC;
+        int len;
+        char *url = NULL;
+
+        // Get the cache key URL (for now), since this has better lookup behavior when using
+        // e.g. the cachekey plugin.
+        if (TS_SUCCESS == TSUrlCreate(request, &c_url)) {
+          if (TS_SUCCESS == TSHttpTxnCacheLookupUrlGet(txnp, request, c_url)) {
+            url = TSUrlStringGet(request, c_url, &len);
+            TSHandleMLocRelease(request, TS_NULL_MLOC, c_url);
+            TSDebug(PLUGIN_NAME, "Cache URL is %.*s", len, url);
+          }
+        }
 
-      TSfree(static_cast<void *>(url));
-      TSHandleMLocRelease(request, TS_NULL_MLOC, purl);
+        if (url) {
+          _url.assign(url, len); // Save away the cache URL for later use when acquiring lock
+          TSfree(static_cast<void *>(url));
 
-      if (TS_SUCCESS == TSHttpHdrUrlSet(mbuf, hdr_loc, url_loc)) {
-        // Make sure we have the correct Host: header for this request.
-        const char *hostp = TSUrlHostGet(mbuf, url_loc, &len);
+          if (TS_SUCCESS == TSHttpHdrUrlSet(mbuf, hdr_loc, url_loc)) {
+            // Make sure we have the correct Host: header for this request.
+            const char *hostp = TSUrlHostGet(mbuf, url_loc, &len);
 
-        if (set_header(mbuf, hdr_loc, TS_MIME_FIELD_HOST, TS_MIME_LEN_HOST, hostp, len)) {
-          TSDebug(PLUGIN_NAME, "Set header Host: %.*s", len, hostp);
-        }
+            if (set_header(mbuf, hdr_loc, TS_MIME_FIELD_HOST, TS_MIME_LEN_HOST, hostp, len)) {
+              TSDebug(PLUGIN_NAME, "Set header Host: %.*s", len, hostp);
+            }
 
-        // Next, remove any Range: headers from our request.
-        if (remove_header(mbuf, hdr_loc, TS_MIME_FIELD_RANGE, TS_MIME_LEN_RANGE) > 0) {
-          TSDebug(PLUGIN_NAME, "Removed the Range: header from request");
+            // Next, remove any Range: headers from our request.
+            if (remove_header(mbuf, hdr_loc, TS_MIME_FIELD_RANGE, TS_MIME_LEN_RANGE) > 0) {
+              TSDebug(PLUGIN_NAME, "Removed the Range: header from request");
+            }
+            // Everything went as planned, so we can return true
+            ret = true;
+          }
         }
-
-        return true;
       }
+      TSHandleMLocRelease(request, TS_NULL_MLOC, p_url);
     }
   }
 
-  // Something failed.
-  return false;
+  return ret;
 }
 
 static int cont_bg_fetch(TSCont contp, TSEvent event, void *edata);
@@ -362,7 +376,7 @@ cont_bg_fetch(TSCont contp, TSEvent event, void * /* edata ATS_UNUSED */)
         TSError("[%s] Unknown address family %d", PLUGIN_NAME, sockaddress->sa_family);
         break;
       }
-      TSDebug(PLUGIN_NAME, "Starting bg fetch on: %s", data->getUrl());
+      TSDebug(PLUGIN_NAME, "Starting background fetch, replaying:");
       dump_headers(data->mbuf, data->hdr_loc);
     }
 
diff --git a/plugins/experimental/cache_promote/cache_promote.cc b/plugins/experimental/cache_promote/cache_promote.cc
index 73a409f..ca236b0 100644
--- a/plugins/experimental/cache_promote/cache_promote.cc
+++ b/plugins/experimental/cache_promote/cache_promote.cc
@@ -238,16 +238,32 @@ public:
   {
     LRUHash hash;
     LRUMap::iterator map_it;
+    char *url = NULL;
     int url_len = 0;
-    char *url = TSHttpTxnEffectiveUrlStringGet(txnp, &url_len);
     bool ret = false;
+    TSMBuffer request;
+    TSMLoc req_hdr;
+
+    if (TS_SUCCESS == TSHttpTxnClientReqGet(txnp, &request, &req_hdr)) {
+      TSMLoc c_url = TS_NULL_MLOC;
+
+      // Get the cache key URL (for now), since this has better lookup behavior when using
+      // e.g. the cachekey plugin.
+      if (TS_SUCCESS == TSUrlCreate(request, &c_url)) {
+        if (TS_SUCCESS == TSHttpTxnCacheLookupUrlGet(txnp, request, c_url)) {
+          url = TSUrlStringGet(request, c_url, &url_len);
+          TSHandleMLocRelease(request, TS_NULL_MLOC, c_url);
+        }
+      }
+      TSHandleMLocRelease(request, TS_NULL_MLOC, req_hdr);
+    }
 
     // Generally shouldn't happen ...
     if (!url) {
       return false;
     }
 
-    TSDebug(PLUGIN_NAME, "LRUPolicy::doPromote(%.*s ...)", url_len > 30 ? 30 : url_len, url);
+    TSDebug(PLUGIN_NAME, "LRUPolicy::doPromote(%.*s%s)", url_len > 100 ? 100 : url_len, url, url_len > 100 ? "..." : "");
     hash.init(url, url_len);
     TSfree(url);
 

-- 
To stop receiving notification emails like this one, please contact
"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>.