1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/* Gearman server and library
* Copyright (C) 2008 Brian Aker, Eric Day
* All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the COPYING file in the parent directory for full text.
*/
/**
* @file
* @brief libmemcached Queue Storage Definitions
*/
#include "common.h"
#include <libgearman-server/queue_libmemcached.h>
#include <libmemcached/memcached.h>
/**
* @addtogroup gearman_queue_libmemcached_static Static libmemcached Queue Storage Functions
* @ingroup gearman_queue_libmemcached
* @{
*/
/**
* Default values.
*/
#define GEARMAN_QUEUE_LIBMEMCACHED_DEFAULT_PREFIX "gear_"
/**
* Structure for libmemcached specific data.
*/
typedef struct
{
memcached_st memc;
} gearman_queue_libmemcached_st;
/* Queue callback functions. */
static gearman_return_t _libmemcached_add(gearman_server_st *server,
void *context, const void *unique,
size_t unique_size,
const void *function_name,
size_t function_name_size,
const void *data, size_t data_size,
gearman_job_priority_t priority);
static gearman_return_t _libmemcached_flush(gearman_server_st *server,
void *context);
static gearman_return_t _libmemcached_done(gearman_server_st *server,
void *context, const void *unique,
size_t unique_size,
const void *function_name,
size_t function_name_size);
static gearman_return_t _libmemcached_replay(gearman_server_st *server,
void *context,
gearman_queue_add_fn *add_fn,
void *add_context);
/** @} */
/*
* Public definitions
*/
gearman_return_t gearman_server_queue_libmemcached_conf(gearman_conf_st *conf)
{
gearman_conf_module_st *module;
module= gearman_conf_module_create(conf, NULL, "libmemcached");
if (module == NULL)
return GEARMAN_MEMORY_ALLOCATION_FAILURE;
gearman_conf_module_add_option(module, "servers", 0, "SERVER_LIST",
"List of Memcached servers to use.");
return gearman_conf_return(conf);
}
gearman_return_t
gearman_server_queue_libmemcached_init(gearman_server_st *server,
gearman_conf_st *conf)
{
gearman_queue_libmemcached_st *queue;
gearman_conf_module_st *module;
const char *name;
const char *value;
memcached_server_st *servers;
const char *opt_servers= NULL;
gearman_log_info(server->gearman, "Initializing libmemcached module");
queue= calloc(1, sizeof(gearman_queue_libmemcached_st));
if (queue == NULL)
{
gearman_log_error(server->gearman, "gearman_queue_libmemcached_init", "malloc");
return GEARMAN_MEMORY_ALLOCATION_FAILURE;
}
if (memcached_create(&(queue->memc)) == NULL)
{
free(queue);
gearman_log_error(server->gearman, "gearman_queue_libmemcached_init", "memcached_create");
return GEARMAN_QUEUE_ERROR;
}
/* Get module and parse the option values that were given. */
module= gearman_conf_module_find(conf, "libmemcached");
if (module == NULL)
{
gearman_log_error(server->gearman, "gearman_queue_libmemcached_init", "gearman_conf_module_find:NULL");
return GEARMAN_QUEUE_ERROR;
}
while (gearman_conf_module_value(module, &name, &value))
{
if (!strcmp(name, "servers"))
opt_servers= value;
else
{
memcached_free(&(queue->memc));
free(queue);
gearman_log_error(server->gearman, "gearman_queue_libmemcached_init", "Unknown argument: %s", name);
return GEARMAN_QUEUE_ERROR;
}
}
if (opt_servers == NULL)
{
gearman_log_error(server->gearman, "gearman_queue_libmemcached_init", "No --servers given");
return GEARMAN_QUEUE_ERROR;
}
servers= memcached_servers_parse(opt_servers);
if (servers == NULL)
{
memcached_free(&(queue->memc));
free(queue);
gearman_log_error(server->gearman, "gearman_queue_libmemcached_init", "memcached_servers_parse");
return GEARMAN_QUEUE_ERROR;
}
memcached_server_push(&queue->memc, servers);
memcached_server_list_free(servers);
gearman_server_set_queue_context(server, queue);
gearman_server_set_queue_add_fn(server, _libmemcached_add);
gearman_server_set_queue_flush_fn(server, _libmemcached_flush);
gearman_server_set_queue_done_fn(server, _libmemcached_done);
gearman_server_set_queue_replay_fn(server, _libmemcached_replay);
return GEARMAN_SUCCESS;
}
gearman_return_t
gearman_server_queue_libmemcached_deinit(gearman_server_st *server)
{
gearman_queue_libmemcached_st *queue;
gearman_log_info(server->gearman, "Shutting down libmemcached queue module");
queue= (gearman_queue_libmemcached_st *)gearman_server_queue_context(server);
gearman_server_set_queue_context(server, NULL);
memcached_free(&(queue->memc));
free(queue);
return GEARMAN_SUCCESS;
}
gearman_return_t gearmand_queue_libmemcached_init(gearmand_st *gearmand,
gearman_conf_st *conf)
{
return gearman_server_queue_libmemcached_init(&(gearmand->server), conf);
}
gearman_return_t gearmand_queue_libmemcached_deinit(gearmand_st *gearmand)
{
return gearman_server_queue_libmemcached_deinit(&(gearmand->server));
}
/*
* Static definitions
*/
static gearman_return_t _libmemcached_add(gearman_server_st *server,
void *context, const void *unique,
size_t unique_size,
const void *function_name,
size_t function_name_size,
const void *data, size_t data_size,
gearman_job_priority_t priority)
{
gearman_queue_libmemcached_st *queue= (gearman_queue_libmemcached_st *)context;
memcached_return rc;
char key[MEMCACHED_MAX_KEY];
size_t key_length;
gearman_log_debug(server->gearman, "libmemcached add: %.*s", (uint32_t)unique_size, (char *)unique);
key_length= (size_t)snprintf(key, MEMCACHED_MAX_KEY, "%s%.*s-%.*s",
GEARMAN_QUEUE_LIBMEMCACHED_DEFAULT_PREFIX,
(int)function_name_size,
(const char *)function_name, (int)unique_size,
(const char *)unique);
rc= memcached_set(&queue->memc, (const char *)key, key_length,
(const char *)data, data_size, 0, (uint32_t)priority);
if (rc != MEMCACHED_SUCCESS)
return GEARMAN_QUEUE_ERROR;
return GEARMAN_SUCCESS;
}
static gearman_return_t _libmemcached_flush(gearman_server_st *server,
void *context __attribute__((unused)))
{
gearman_log_debug(server->gearman, "libmemcached flush");
return GEARMAN_SUCCESS;
}
static gearman_return_t _libmemcached_done(gearman_server_st *server,
void *context, const void *unique,
size_t unique_size,
const void *function_name,
size_t function_name_size)
{
size_t key_length;
char key[MEMCACHED_MAX_KEY];
memcached_return rc;
gearman_queue_libmemcached_st *queue= (gearman_queue_libmemcached_st *)context;
gearman_log_debug(server->gearman, "libmemcached done: %.*s", (uint32_t)unique_size, (char *)unique);
key_length= (size_t)snprintf(key, MEMCACHED_MAX_KEY, "%s%.*s-%.*s",
GEARMAN_QUEUE_LIBMEMCACHED_DEFAULT_PREFIX,
(int)function_name_size,
(const char *)function_name, (int)unique_size,
(const char *)unique);
/* For the moment we will assume it happened */
rc= memcached_delete(&queue->memc, (const char *)key, key_length, 0);
if (rc != MEMCACHED_SUCCESS)
return GEARMAN_QUEUE_ERROR;
return GEARMAN_SUCCESS;
}
struct replay_context
{
memcached_st clone;
gearman_server_st *server;
gearman_queue_add_fn *add_fn;
void *add_context;
};
static memcached_return callback_loader(const memcached_st *ptr __attribute__((unused)),
memcached_result_st *result __attribute__((unused)),
void *context)
{
struct replay_context *container= (struct replay_context *)context;
const char *key;
const char *unique;
char *function;
size_t unique_len;
key= memcached_result_key_value(result);
if (strcmp(key, GEARMAN_QUEUE_LIBMEMCACHED_DEFAULT_PREFIX))
return MEMCACHED_SUCCESS;
unique= key + strlen(GEARMAN_QUEUE_LIBMEMCACHED_DEFAULT_PREFIX);
function= index(unique, '-');
unique_len= (size_t)(function - unique);
function++;
assert(unique);
assert(unique_len);
assert(function);
assert(strlen(function));
/* Currently not looking at failure cases */
(void)(*container->add_fn)(container->server, container->add_context,
unique, unique_len,
function, strlen(function),
memcached_result_value(result), memcached_result_length(result),
memcached_result_flags(result));
return MEMCACHED_SUCCESS;
}
/* Grab the object and load it into the loader */
static memcached_return callback_for_key(const memcached_st *ptr __attribute__((unused)),
const char *key, size_t key_length,
void *context)
{
memcached_return rc;
struct replay_context *container= (struct replay_context *)context;
memcached_execute_function callbacks[1];
char *passable[1];
callbacks[0]= &callback_loader;
passable[0]= (char *)key;
rc= memcached_mget(&container->clone, (void *)passable, &key_length, 1);
/* Just void errors for the moment, since other treads might have picked up the object. */
(void)memcached_fetch_execute(&container->clone, callbacks, context, 1);
return MEMCACHED_SUCCESS;
}
/*
If we have any failures for loading values back into replay we just ignore them.
*/
static gearman_return_t _libmemcached_replay(gearman_server_st *server, void *context,
gearman_queue_add_fn *add_fn,
void *add_context)
{
gearman_queue_libmemcached_st *queue= (gearman_queue_libmemcached_st *)context;
struct replay_context container;
memcached_st *check_for_failure;
memcached_dump_func callbacks[1];
callbacks[0]= &callback_for_key;
gearman_log_info(server->gearman, "libmemcached replay start");
memset(&container, 0, sizeof(struct replay_context));
check_for_failure= memcached_clone(&container.clone, &queue->memc);
container.server= server;
container.add_fn= add_fn;
container.add_context= add_context;
assert(check_for_failure);
(void)memcached_dump(&queue->memc, callbacks, (void *)&container, 1);
memcached_free(&container.clone);
return GEARMAN_SUCCESS;
}
|