]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
modules/kmemcached: memcached backend for resolver cache
authorMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 26 May 2015 07:37:22 +0000 (09:37 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Tue, 26 May 2015 22:11:12 +0000 (00:11 +0200)
memcached is a distributed caching system, it is a good fit
for building resolvers with shared and replicated cache

Makefile
daemon/bindings.c
daemon/engine.c
help.mk
lib/module.c
modules/kmemcached/kmemcached.c [new file with mode: 0644]
modules/kmemcached/kmemcached.mk [new file with mode: 0644]
modules/kmemcached/namedb_memcached.c [new file with mode: 0644]
modules/modules.mk

index 57fc55af6cc1725823aec83c7dcb06bd6e10e89f..40c52d924fc858e97e71c4bd19122ec4c61839b4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,6 +26,7 @@ $(eval $(call find_bin,doxygen))
 $(eval $(call find_bin,sphinx-build))
 $(eval $(call find_bin,gccgo))
 $(eval $(call find_python))
+$(eval $(call find_lib,libmemcached))
 
 # Work around luajit on OS X
 ifeq ($(PLATFORM), Darwin)
index 6bde1f1538054f71edb4fe32dbcab51fd0f6df30..e2fb35cec3db26126f68354eb194b5ce71191a7b 100644 (file)
@@ -364,13 +364,14 @@ static int cache_open(lua_State *L)
                format_error(L, "unsupported cache backend");
                lua_error(L);
        }
-       kr_cache_storage_set(storage->api);
 
        /* Close if already open */
        if (engine->resolver.cache != NULL) {
                kr_cache_close(engine->resolver.cache);
        }
+
        /* Reopen cache */
+       kr_cache_storage_set(storage->api);
        void *storage_opts = storage->opts_create(conf, cache_size);
        engine->resolver.cache = kr_cache_open(storage_opts, engine->pool);
        free(storage_opts);
index aad178d73ac93a09c5c4a5733b118dfdd25ee1e0..defd59a7ada4fba33f59b01244de2d3e4f27887f 100644 (file)
@@ -375,6 +375,7 @@ int engine_register(struct engine *engine, const char *name)
        if (!module) {
                return kr_error(ENOMEM);
        }
+       module->data = engine;
        int ret = kr_module_load(module, name, NULL);
        /* Load Lua module if not a binary */
        if (ret == kr_error(ENOENT)) {
diff --git a/help.mk b/help.mk
index 89e47c000afdadd8a1b628a5c1e56e8086e06b5f..9b8202db1f9279d8799eed957dcf70f72e365846 100644 (file)
--- a/help.mk
+++ b/help.mk
@@ -18,4 +18,5 @@ info:
        $(info [$(HAS_cmocka)] cmocka (tests/unit))
        $(info [$(HAS_python)] Python (tests/integration))
        $(info [$(HAS_gccgo)] GCCGO (modules/go))
+       $(info [$(HAS_libmemcached)] libmemcached (modules/memcached))
        $(info )
index 19c93bc9758315521b122a6f3be874a66e166a19..9464a46bb676ecf8e21b607dfede3e81976e6a8e 100644 (file)
@@ -150,8 +150,10 @@ int kr_module_load(struct kr_module *module, const char *name, const char *path)
                return kr_error(EINVAL);
        }
 
-       /* Initialize. */
+       /* Initialize, keep userdata */
+       void *data = module->data;
        memset(module, 0, sizeof(struct kr_module));
+       module->data = data;
        module->name = strdup(name);
        if (module->name == NULL) {
                return kr_error(ENOMEM);
diff --git a/modules/kmemcached/kmemcached.c b/modules/kmemcached/kmemcached.c
new file mode 100644 (file)
index 0000000..873d88d
--- /dev/null
@@ -0,0 +1,59 @@
+/*  Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <libknot/internal/namedb/namedb.h>
+
+#include "daemon/engine.h"
+#include "lib/module.h"
+#include "lib/cache.h"
+
+/** @internal Memcached API */
+extern const namedb_api_t *namedb_memcached_api(void);
+
+/** @internal Make memcached options. */
+void *namedb_memcached_mkopts(const char *conf, size_t maxsize)
+{
+       return strdup(conf);
+}
+
+int kmemcached_init(struct kr_module *module)
+{
+       struct engine *engine = module->data;
+       /* Register new storage option */
+       static struct storage_api memcached = {
+               "memcached://", namedb_memcached_api, namedb_memcached_mkopts
+       };
+       array_push(engine->storage_registry, memcached);
+       return kr_ok();
+}
+
+int lmemcached_deinit(struct kr_module *module)
+{
+       struct engine *engine = module->data;
+       for (unsigned i = 0; i < engine->storage_registry.len; ++i) {
+               struct storage_api *storage = &engine->storage_registry.at[i];
+               if (strcmp(storage->prefix, "memcached://") == 0) {
+                       array_del(engine->storage_registry, i);
+                       break;
+               }
+       }
+       if (kr_cache_storage == namedb_memcached_api) {
+               kr_cache_storage_set(NULL);
+       }
+       return kr_ok();
+}
+
+KR_MODULE_EXPORT(kmemcached);
diff --git a/modules/kmemcached/kmemcached.mk b/modules/kmemcached/kmemcached.mk
new file mode 100644 (file)
index 0000000..58e0099
--- /dev/null
@@ -0,0 +1,3 @@
+kmemcached_SOURCES := modules/kmemcached/kmemcached.c modules/kmemcached/namedb_memcached.c
+kmemcached_LIBS := $(libkresolve_TARGET) $(libkresolve_LIBS) $(libmemcached_LIBS)
+$(call make_c_module,kmemcached)
diff --git a/modules/kmemcached/namedb_memcached.c b/modules/kmemcached/namedb_memcached.c
new file mode 100644 (file)
index 0000000..e9ea1f6
--- /dev/null
@@ -0,0 +1,205 @@
+/*  Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/** @file namedb_memcached.c
+ *  @brief Implemented all the things that the resolver cache needs,
+ *         it's not a general-purpose namedb implementation, and it can't
+ *         be since it's *cache* by principle and it doesn't guarantee persistence anyway.
+ *  @note The implemented functions are not thread-safe, see http://docs.libmemcached.org/libmemcachedutil.html
+ *  @note Write transactions can't be aborted.
+ *  @note No iteration support.
+ */
+
+#include <assert.h>
+#include <string.h>
+#include <libmemcached/memcached.h>
+#include <libknot/internal/namedb/namedb.h>
+#include <libknot/errcode.h>
+
+#include "lib/generic/array.h"
+#include "lib/cache.h"
+#include "lib/utils.h"
+
+/* Oh, the irony... */
+typedef array_t(char *) freelist_t;
+
+static int init(namedb_t **db, mm_ctx_t *mm, void *arg)
+{
+       if (!db || !arg) {
+               return KNOT_EINVAL;
+       }
+
+       /* Make sure we're running on binary protocol, as the
+        * textual protocol is broken for binary keys. */
+       auto_free char *config_str = kr_strcatdup(2, arg, " --BINARY-PROTOCOL");
+       memcached_st *handle = memcached(config_str, strlen(config_str));
+       if (!handle) {
+               return KNOT_ERROR;
+       }
+
+       *db = handle;
+       return KNOT_EOK;
+}
+
+static void deinit(namedb_t *db)
+{
+       memcached_free((memcached_st *)db);
+}
+
+static int txn_begin(namedb_t *db, namedb_txn_t *txn, unsigned flags)
+{
+       freelist_t *freelist = malloc(sizeof(*freelist));
+       if (!freelist) {
+               return KNOT_ENOMEM;
+       }
+       txn->txn = freelist;
+       txn->db  = db;
+       array_init(*freelist);
+       return KNOT_EOK;
+}
+
+static int txn_commit(namedb_txn_t *txn)
+{
+       freelist_t *freelist = txn->txn;
+       if (freelist) {
+               for (unsigned i = 0; i < freelist->len; ++i) {
+                       free(freelist->at[i]);
+               }
+               array_clear(*freelist);
+               txn->txn = NULL;
+       }
+       return KNOT_EOK;
+}
+
+static void txn_abort(namedb_txn_t *txn)
+{
+       /** @warning No real transactions here,
+         *          all the reads/writes are done synchronously.
+         *          If it is needed, we would need to buffer writes in
+         *          the freelist first and put on commit.
+         */
+       txn_commit(txn);
+}
+
+static int count(namedb_txn_t *txn)
+{
+       memcached_return_t error = 0;
+       memcached_stat_st *stats = memcached_stat(txn->db, NULL, &error);
+       if (error != 0) {
+               return KNOT_ERROR;
+       }
+       size_t ret = stats->curr_items;
+       free(stats);
+       return ret;
+}
+
+static int clear(namedb_txn_t *txn)
+{
+       memcached_return_t ret = memcached_flush(txn->db, 0);
+       if (ret != 0) {
+               return KNOT_ERROR;
+       }
+       return KNOT_EOK;
+}
+
+static int find(namedb_txn_t *txn, namedb_val_t *key, namedb_val_t *val, unsigned flags)
+{
+       uint32_t mc_flags = 0;
+       memcached_return_t error = 0;
+       char *ret = memcached_get(txn->db, key->data, key->len, &val->len, &mc_flags, &error);
+       if (error != 0) {
+               return KNOT_ENOENT;
+       }
+       freelist_t *freelist = txn->txn;
+       if (array_push(*freelist, ret) < 0) {
+               free(ret); /* Can't track this, must free */
+               return KNOT_ENOMEM;
+       }
+       val->data = ret;
+       return KNOT_EOK;
+}
+
+static int insert(namedb_txn_t *txn, namedb_val_t *key, namedb_val_t *val, unsigned flags)
+{
+       if (!txn || !key || !val) {
+               return KNOT_EINVAL;
+       }
+       /* @warning This expects usage only for recursor cache, if anyone
+        *          desires to port this somewhere else, TTL shouldn't be interpreted.
+        */
+       struct kr_cache_entry *entry = val->data;
+       memcached_return_t ret = memcached_set(txn->db, key->data, key->len, val->data, val->len, entry->ttl, 0);
+       if (ret != 0) {
+               return KNOT_ERROR;
+       }
+       return KNOT_EOK;
+}
+
+static int del(namedb_txn_t *txn, namedb_val_t *key)
+{
+       memcached_return_t ret = memcached_delete(txn->db, key->data, key->len, 0);
+       if (ret != 0) {
+               return KNOT_ERROR;
+       }
+       return KNOT_EOK;
+}
+
+static namedb_iter_t *iter_begin(namedb_txn_t *txn, unsigned flags)
+{
+       /* Iteration is not supported, pruning should be
+        * left on the memcached server */
+       return NULL;
+}
+
+static namedb_iter_t *iter_seek(namedb_iter_t *iter, namedb_val_t *key, unsigned flags)
+{
+       assert(0);
+       return NULL; /* ENOTSUP */
+}
+
+static namedb_iter_t *iter_next(namedb_iter_t *iter)
+{
+       assert(0);
+       return NULL;
+}
+
+static int iter_key(namedb_iter_t *iter, namedb_val_t *val)
+{
+       return KNOT_ENOTSUP;
+}
+
+static int iter_val(namedb_iter_t *iter, namedb_val_t *val)
+{
+       return KNOT_ENOTSUP;
+}
+
+static void iter_finish(namedb_iter_t *iter)
+{
+       assert(0);
+}
+
+const namedb_api_t *namedb_memcached_api(void)
+{
+       static const namedb_api_t api = {
+               "memcached",
+               init, deinit,
+               txn_begin, txn_commit, txn_abort,
+               count, clear, find, insert, del,
+               iter_begin, iter_seek, iter_next, iter_key, iter_val, iter_finish
+       };
+
+       return &api;
+}
index dfbf1d66b5706c7cb3c38304c64e1aa4660a0afa..37355e9da66d0ab67a9ba30802f4820eb87e16b7 100644 (file)
@@ -3,6 +3,11 @@ modules_TARGETS := hints \
                    stats \
                    cachectl
 
+# Memcached
+ifeq ($(HAS_libmemcached),yes)
+modules_TARGETS += kmemcached
+endif
+
 # List of Lua modules
 ifeq ($(HAS_lua),yes)
 modules_TARGETS += ketcd \