trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject trafficserver git commit: TS-3977: Add cache-key-genid to experimental plugins. This closes #309.
Date Thu, 03 Mar 2016 20:16:08 GMT
Repository: trafficserver
Updated Branches:
  refs/heads/master 1d1cb8fae -> fbdf0221d


TS-3977: Add cache-key-genid to experimental plugins.
This closes #309.


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

Branch: refs/heads/master
Commit: fbdf0221df113029bbfb4b35b650c768c900e515
Parents: 1d1cb8f
Author: Steven Feltner <sfeltner@godaddy.com>
Authored: Thu Oct 22 10:55:05 2015 -0700
Committer: Alan M. Carroll <amc@apache.org>
Committed: Thu Mar 3 14:12:56 2016 -0600

----------------------------------------------------------------------
 NOTICE                                          |   7 +-
 configure.ac                                    |  14 ++
 plugins/experimental/Makefile.am                |   4 +
 .../experimental/cache_key_genid/Makefile.am    |  22 +++
 .../cache_key_genid/cache_key_genid.c           | 174 +++++++++++++++++++
 5 files changed, 218 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/fbdf0221/NOTICE
----------------------------------------------------------------------
diff --git a/NOTICE b/NOTICE
index 15d94b6..7ee263f 100644
--- a/NOTICE
+++ b/NOTICE
@@ -9,6 +9,7 @@ This product includes software developed at
   - Comcast
   - LinkedIn
   - Mike Pall
+  - GoDaddy
 
 ~~~
 
@@ -44,8 +45,8 @@ Copyright (C) 2012 Oregon Health & Science University
 
 ~~~
 
-healthcheck Plugin developed by GoDaddy.
-Copyright (C) 2012 GoDaddy.
+cache-key-genid Plugin developed by GoDaddy
+Copyright (C) 2013 GoDaddy Operating Company, LLC
 
 ~~~
 
@@ -84,7 +85,7 @@ Copyright (C) 2016 Yahoo! Inc.  All rights reserved.
 ~~~
 
 healthchecks: Plugin for ATS healthchecks.
-Copyright (C) 2012 Go Daddy Operating Company, LLC
+Copyright (C) 2012 GoDaddy Operating Company, LLC
 
 ~~~
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/fbdf0221/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index 530d8c7..26e446e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1454,6 +1454,19 @@ AC_CHECK_LIB([mysqlclient],[mysql_info],[AC_SUBST([LIB_MYSQLCLIENT],["-lmysqlcli
 AC_SUBST(has_mysql)
 AM_CONDITIONAL([HAS_MYSQL], [ test "x${has_mysql}" = "x1" ])
 
+AC_CHECK_HEADERS([kclangc.h], [
+  AC_CHECK_LIB([kyotocabinet], [kcdbopen], [
+    AC_SUBST([LIB_KYOTOCABINET], ["-lkyotocabinet"])
+    has_kyotocabinet=1
+  ], [
+    has_kyotocabinet=0
+  ])
+],
+[has_kyotocabinet=0]
+)
+AC_SUBST(has_kyotocabinet)
+AM_CONDITIONAL([HAS_KYOTOCABINET], [ test "x${has_kyotocabinet}" = "x1" ])
+
 # -----------------------------------------------------------------------------
 # 5. CHECK FOR HEADER FILES
 
@@ -1930,6 +1943,7 @@ AC_CONFIG_FILES([
   plugins/experimental/background_fetch/Makefile
   plugins/experimental/balancer/Makefile
   plugins/experimental/buffer_upload/Makefile
+  plugins/experimental/cache_key_genid/Makefile
   plugins/experimental/cache_promote/Makefile
   plugins/experimental/cache_range_requests/Makefile
   plugins/experimental/cachekey/Makefile

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/fbdf0221/plugins/experimental/Makefile.am
----------------------------------------------------------------------
diff --git a/plugins/experimental/Makefile.am b/plugins/experimental/Makefile.am
index 1a98e39..dd73193 100644
--- a/plugins/experimental/Makefile.am
+++ b/plugins/experimental/Makefile.am
@@ -61,3 +61,7 @@ endif
 if BUILD_LUAJIT
   SUBDIRS += ts_lua
 endif
+
+if HAS_KYOTOCABINET
+  SUBDIRS += cache_key_genid
+endif

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/fbdf0221/plugins/experimental/cache_key_genid/Makefile.am
----------------------------------------------------------------------
diff --git a/plugins/experimental/cache_key_genid/Makefile.am b/plugins/experimental/cache_key_genid/Makefile.am
new file mode 100644
index 0000000..41db138
--- /dev/null
+++ b/plugins/experimental/cache_key_genid/Makefile.am
@@ -0,0 +1,22 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+include $(top_srcdir)/build/plugins.mk
+
+pkglib_LTLIBRARIES = cache_key_genid.la
+cache_key_genid_la_SOURCES = cache_key_genid.c
+cache_key_genid_la_LDFLAGS = $(TS_PLUGIN_LDFLAGS) $(LIB_KYOTOCABINET)
+

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/fbdf0221/plugins/experimental/cache_key_genid/cache_key_genid.c
----------------------------------------------------------------------
diff --git a/plugins/experimental/cache_key_genid/cache_key_genid.c b/plugins/experimental/cache_key_genid/cache_key_genid.c
new file mode 100644
index 0000000..cfaf1ac
--- /dev/null
+++ b/plugins/experimental/cache_key_genid/cache_key_genid.c
@@ -0,0 +1,174 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* cache-key-genid.c - Plugin to modify the URL used as a cache key for
+ * requests, without modifying the URL used for actually fetching data from
+ * the origin server.
+ */
+
+#include <ts/ts.h>
+#include <stdio.h>
+#include <string.h>
+#include "kclangc.h"
+
+#define PLUGIN_NAME "cache-key-genid"
+
+static char genid_kyoto_db[PATH_MAX + 1];
+
+// Find the host in url and set host to it
+static void
+get_genid_host(char **host, char *url)
+{
+  char *pt1;
+  char *pt2;
+  size_t host_len;
+  unsigned num = 1;
+
+  pt1 = strstr(url, "//");
+
+  if (pt1) {
+    pt1 = pt1 + 2;
+    pt2 = strstr(pt1, "/");
+  }
+
+  if (pt1 && pt2 && pt2 > pt1) {
+    host_len = pt2 - pt1;
+    *host = calloc(num, host_len + 1);
+    strncpy(*host, pt1, host_len);
+  }
+}
+
+/* get_genid
+ * Looks up the host's genid in the host->genid database
+ */
+static int
+get_genid(char *host)
+{
+  KCDB *db;
+  char *vbuf;
+  size_t vsiz;
+  int answer = 0;
+  int host_size;
+
+  /* create the database object */
+  db = kcdbnew();
+
+  /* open the database */
+  if (!kcdbopen(db, genid_kyoto_db, KCOREADER | KCONOLOCK)) {
+    TSDebug(PLUGIN_NAME, "could not open the genid database %s", genid_kyoto_db);
+    TSError("[%s] could not open the genid database %s: %s", PLUGIN_NAME, genid_kyoto_db,
strerror(errno));
+    return 0;
+  }
+
+  vbuf = kcdbget(db, host, strlen(host), &vsiz);
+
+  if (vbuf) {
+    TSDebug(PLUGIN_NAME, "kcdbget(%s) = %s", host, vbuf);
+    answer = (int)strtol(vbuf, NULL, 10);
+    kcfree(vbuf);
+  } else {
+    host_size = strlen(host);
+    TSDebug(PLUGIN_NAME, "kcdbget(%s) - no record found, len(%d)", host, host_size);
+    answer = 0;
+  }
+
+  kcdbclose(db);
+  return answer;
+}
+
+/* handle_hook
+ * Fires on TS_EVENT_HTTP_READ_REQUEST_HDR events, gets the effectiveUrl
+ * finds the host, gets the generation ID, gen_id, for the host
+ * and runs TSCacheUrlSet to change the cache key for the read
+ */
+static int
+handle_hook(TSCont *contp, TSEvent event, void *edata)
+{
+  TSHttpTxn txnp = (TSHttpTxn)edata;
+  char *url = NULL, *host = NULL;
+  int url_length;
+  int gen_id;
+  int ok = 1;
+
+  switch (event) {
+  case TS_EVENT_HTTP_READ_REQUEST_HDR:
+    TSDebug(PLUGIN_NAME, "handling TS_EVENT_HTTP_READ_REQUEST_HDR");
+
+    if (ok) {
+      url = TSHttpTxnEffectiveUrlStringGet(txnp, &url_length);
+      if (!url) {
+        TSError("[%s] could not retrieve request url", PLUGIN_NAME);
+        ok = 0;
+      }
+    }
+
+    if (ok) {
+      get_genid_host(&host, url);
+      if (!host) {
+        TSError("[%s] could not retrieve request host", PLUGIN_NAME);
+        ok = 0;
+      }
+    }
+
+    if (ok) {
+      TSDebug(PLUGIN_NAME, "From url (%s) discovered host (%s)", url, host);
+      if ((gen_id = get_genid(host)) != 0) {
+        if (TSHttpTxnConfigIntSet(txnp, TS_CONFIG_HTTP_CACHE_GENERATION, gen_id) != TS_SUCCESS)
{
+          TSDebug(PLUGIN_NAME, "Error, unable to modify cache url");
+          TSError("[%s] Unable to set cache generation for %s to %d", PLUGIN_NAME, url, gen_id);
+          ok = 0;
+        }
+      }
+    }
+
+    /* Clean up */
+    if (url)
+      TSfree(url);
+    if (host)
+      TSfree(host);
+    TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
+    break;
+
+  default:
+    TSAssert(!"Unexpected event");
+    ok = 0;
+    break;
+  }
+
+  return ok;
+}
+
+void
+TSPluginInit(int argc, const char *argv[])
+{
+  TSPluginRegistrationInfo info;
+
+  info.plugin_name = (char *)PLUGIN_NAME;
+  info.vendor_name = (char *)"Apache Software Foundation";
+  info.support_email = (char *)"dev@trafficserver.apache.org";
+
+  if (argc > 1) {
+    TSstrlcpy(genid_kyoto_db, argv[1], sizeof(genid_kyoto_db));
+  } else {
+    TSError("[%s] plugin registration failed. check argv[1] for db path", PLUGIN_NAME);
+    return;
+  }
+
+  if (TSPluginRegister(&info) != TS_SUCCESS) {
+    TSError("[%s] plugin registration failed.  check version.", PLUGIN_NAME);
+    return;
+  }
+
+  TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TSContCreate((TSEventFunc)handle_hook, NULL));
+}


Mime
View raw message