Commit 587450c7 authored by Marek Vavruša's avatar Marek Vavruša Committed by Marek Vavruša

modules/kmemcached: memcached backend for resolver cache

memcached is a distributed caching system, it is a good fit
for building resolvers with shared and replicated cache
parent c9a50c50
......@@ -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)
......
......@@ -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);
......
......@@ -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)) {
......
......@@ -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 )
......@@ -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);
......
/* 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);
kmemcached_SOURCES := modules/kmemcached/kmemcached.c modules/kmemcached/namedb_memcached.c
kmemcached_LIBS := $(libkresolve_TARGET) $(libkresolve_LIBS) $(libmemcached_LIBS)
$(call make_c_module,kmemcached)
/* 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;
}
......@@ -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 \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment