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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: MapCache tile caching support file: riak cache backend.
* Author: Michael Downey and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2013 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include "mapcache-config.h"
#ifdef USE_RIAK
#include "mapcache.h"
#include <apr_strings.h>
#include <apr_reslist.h>
#include <apr_hash.h>
#include <string.h>
#include <errno.h>
/*
* Since we don't construct the connection pool and store it in the cache object
* we have to store all the connections in a hash map in case there are multiple
* riak caches defined.
*/
struct riak_conn_params {
mapcache_cache_riak *cache;
};
void mapcache_riak_connection_constructor(mapcache_context *ctx, void **conn_, void *params) {
mapcache_cache_riak *cache = ((struct riak_conn_params*)params)->cache;
struct RIACK_CONNECTION_OPTIONS options;
struct RIACK_CLIENT *client = riack_new_client(0);
if (client == NULL) {
ctx->set_error(ctx,500,"failed to riack_new_client(0)");
return;
}
options.recv_timeout_ms = 2000;
options.send_timeout_ms = 2000;
if (riack_connect(client, cache->host, cache->port, &options) != RIACK_SUCCESS) {
riack_free(client);
ctx->set_error(ctx,500,"failed to riack_connect()");
return;
}
if (riack_ping(client) != RIACK_SUCCESS) {
riack_free(client);
ctx->set_error(ctx,500,"failed to riack_ping()");
return;
}
*conn_ = client;
}
void mapcache_riak_connection_destructor(void *conn_) {
struct RIACK_CLIENT *client = (struct RIACK_CLIENT *)conn_;
riack_free(client);
}
static mapcache_pooled_connection* _riak_get_connection(mapcache_context *ctx, mapcache_cache_riak *cache, mapcache_tile *tile)
{
mapcache_pooled_connection *pc;
struct riak_conn_params params;
params.cache = cache;
pc = mapcache_connection_pool_get_connection(ctx,cache->cache.name,mapcache_riak_connection_constructor,
mapcache_riak_connection_destructor, ¶ms);
return pc;
}
static int _mapcache_cache_riak_has_tile(mapcache_context *ctx, mapcache_cache *pcache, mapcache_tile *tile) {
int error;
int retries = 3;
RIACK_STRING key;
struct RIACK_GET_OBJECT obj;
struct RIACK_CLIENT *client;
mapcache_pooled_connection *pc;
mapcache_cache_riak *cache = (mapcache_cache_riak*)pcache;
key.value = mapcache_util_get_tile_key(ctx, tile, NULL, " \r\n\t\f\e\a\b", "#");
if (GC_HAS_ERROR(ctx)) {
return MAPCACHE_FALSE;
}
key.len = strlen(key.value);
pc = _riak_get_connection(ctx, cache, tile);
if (GC_HAS_ERROR(ctx)) {
return MAPCACHE_FALSE;
}
client = pc->connection;
do
{
error = riack_get(client, cache->bucket, key, 0, &obj);
if (error != RIACK_SUCCESS) {
ctx->log(ctx, MAPCACHE_WARN, "Retry %d in riak_has_tile for tile %s from cache %s due to error %d", (4-retries), key.value, cache->cache.name, error);
for (error = riack_reconnect(client);
error != RIACK_SUCCESS && retries > 0;
error = riack_reconnect(client))
{
--retries;
}
--retries;
}
}
while (error != RIACK_SUCCESS && retries >= 0);
if (error != RIACK_SUCCESS) {
riack_free_get_object(client, &obj); // riack_get allocates the returned object so we need to deallocate it.
mapcache_connection_pool_invalidate_connection(ctx,pc);
ctx->set_error(ctx, 500, "riak: failed to get key %s: %d", key, error);
return MAPCACHE_FALSE;
}
if (obj.object.content_count < 1 || obj.object.content[0].data_len == 0) {
error = MAPCACHE_FALSE;
} else {
error = MAPCACHE_TRUE;
}
riack_free_get_object(client, &obj); // riack_get allocates the returned object so we need to deallocate it.
mapcache_connection_pool_release_connection(ctx,pc);
return error;
}
static void _mapcache_cache_riak_delete(mapcache_context *ctx, mapcache_cache *pcache, mapcache_tile *tile) {
int error;
RIACK_STRING key;
struct RIACK_CLIENT *client;
struct RIACK_DEL_PROPERTIES properties;
mapcache_pooled_connection *pc;
mapcache_cache_riak *cache = (mapcache_cache_riak*)pcache;
memset(&properties, 0, sizeof(struct RIACK_DEL_PROPERTIES));
key.value = mapcache_util_get_tile_key(ctx, tile, NULL, " \r\n\t\f\e\a\b", "#");
GC_CHECK_ERROR(ctx);
key.len = strlen(key.value);
pc = _riak_get_connection(ctx, cache, tile);
GC_CHECK_ERROR(ctx);
client = pc->connection;
properties.rw_use = 1;
properties.rw = (4294967295 - 3); // Special value meaning "ALL"
error = riack_delete(client, cache->bucket, key, &properties);
mapcache_connection_pool_release_connection(ctx,pc);
if (error != RIACK_SUCCESS) {
ctx->set_error(ctx, 500, "riak: failed to delete key %s: %d", key, error);
}
}
/**
* \brief get content of given tile
*
* fills the mapcache_tile::data of the given tile with content stored on the riak server
* \private \memberof mapcache_cache_riak
* \sa mapcache_cache::tile_get()
*/
static int _mapcache_cache_riak_get(mapcache_context *ctx, mapcache_cache *pcache, mapcache_tile *tile) {
int error;
int connect_error = RIACK_SUCCESS;
int retries = 3;
RIACK_STRING key;
struct RIACK_GET_OBJECT obj;
struct RIACK_GET_PROPERTIES properties;
struct RIACK_CLIENT *client;
mapcache_pooled_connection *pc;
mapcache_cache_riak *cache = (mapcache_cache_riak*)pcache;
memset(&properties, 0, sizeof(struct RIACK_GET_PROPERTIES));
//Use Buckets defaults instead of setting the read/write attributes
/*
properties.r_use = 1;
properties.r = 1;
*/
key.value = mapcache_util_get_tile_key(ctx, tile, NULL, " \r\n\t\f\e\a\b", "#");
if (GC_HAS_ERROR(ctx)) {
return MAPCACHE_FAILURE;
}
key.len = strlen(key.value);
tile->encoded_data = mapcache_buffer_create(0, ctx->pool);
pc = _riak_get_connection(ctx, cache, tile);
if (GC_HAS_ERROR(ctx)) {
return MAPCACHE_FAILURE;
}
client = pc->connection;
// If we get an error it is advised that we call reconnect. It also appears
// that every now and then we get an error and need to retry once again to
// get it to work.
do
{
error = riack_get(client, cache->bucket, key, &properties, &obj);
if (error != RIACK_SUCCESS) {
ctx->log(ctx, MAPCACHE_WARN, "Retry %d in riak_get for tile %s from cache %s due to error %d", (4-retries), key.value, cache->cache.name, error);
for (connect_error = riack_reconnect(client);
connect_error != RIACK_SUCCESS && retries > 0;
connect_error = riack_reconnect(client))
{
--retries;
}
--retries;
}
}
while (error != RIACK_SUCCESS && retries >= 0);
if (error != RIACK_SUCCESS)
{
if (connect_error != RIACK_SUCCESS)
mapcache_connection_pool_invalidate_connection(ctx,pc);
else
mapcache_connection_pool_release_connection(ctx,pc);
ctx->set_error(ctx, 500, "Failed to get tile %s from cache %s due to error %d", key.value, cache->cache.name, error);
return MAPCACHE_FAILURE;
}
// Check if tile exists. If it doesn't we need to return CACHE_MISS or things go wrong.
// Mapcache doesn't appear to use the has_tile function and uses _get instead so we need
// to do this sort of test here instead of erroring.
if (obj.object.content_count < 1 || obj.object.content[0].data_len == 0) {
riack_free_get_object(client, &obj); // Need to free the object here as well.
mapcache_connection_pool_release_connection(ctx,pc);
return MAPCACHE_CACHE_MISS;
}
// Copy the data into the buffer
mapcache_buffer_append(tile->encoded_data, obj.object.content[0].data_len, obj.object.content[0].data);
riack_free_get_object(client, &obj); // riack_get allocates the returned object so we need to deallocate it.
mapcache_connection_pool_release_connection(ctx,pc);
return MAPCACHE_SUCCESS;
}
/**
* \brief push tile data to riak
*
* writes the content of mapcache_tile::data to the configured riak instance(s)
* \private \memberof mapcache_cache_riak
* \sa mapcache_cache::tile_set()
*/
static void _mapcache_cache_riak_set(mapcache_context *ctx, mapcache_cache *pcache, mapcache_tile *tile) {
char *key,*content_type;
int error;
int connect_error = RIACK_SUCCESS;
int retries = 3;
struct RIACK_OBJECT object;
struct RIACK_CONTENT content;
struct RIACK_PUT_PROPERTIES properties;
struct RIACK_CLIENT *client;
mapcache_pooled_connection *pc;
mapcache_cache_riak *cache = (mapcache_cache_riak*)pcache;
memset(&content, 0, sizeof(struct RIACK_CONTENT));
memset(&object, 0, sizeof(struct RIACK_OBJECT));
memset(&properties, 0, sizeof(struct RIACK_PUT_PROPERTIES));
//Use Buckets defaults instead of setting the read/write attributes
/*
properties.w_use = 1;
properties.w = 1;
properties.dw_use = 1;
properties.dw = 0;*/
key = mapcache_util_get_tile_key(ctx, tile, NULL, " \r\n\t\f\e\a\b", "#");
GC_CHECK_ERROR(ctx);
if (!tile->encoded_data) {
tile->encoded_data = tile->tileset->format->write(ctx, tile->raw_image, tile->tileset->format);
GC_CHECK_ERROR(ctx);
}
content_type = tile->tileset->format?(tile->tileset->format->mime_type?tile->tileset->format->mime_type:NULL):NULL;
if(!content_type) {
/* compute the content-type */
mapcache_image_format_type t = mapcache_imageio_header_sniff(ctx,tile->encoded_data);
if(t == GC_PNG)
content_type = "image/png";
else if(t == GC_JPEG)
content_type = "image/jpeg";
}
pc = _riak_get_connection(ctx, cache, tile);
GC_CHECK_ERROR(ctx);
client = pc->connection;
// Set up the riak object to put. Need to do this after we get the client connection
object.bucket.value = cache->bucket.value;
object.bucket.len = cache->bucket.len;
object.key.value = key;
object.key.len = strlen(key);
object.vclock.len = 0;
object.content_count = 1;
object.content = &content;
content.content_type.value = content_type;
content.content_type.len = content_type?strlen(content_type):0;
content.data = (uint8_t*)tile->encoded_data->buf;
content.data_len = tile->encoded_data->size;
// If we get an error it is advised that we call reconnect. It also appears
// that every now and then we get an error and need to retry once again to
// get it to work.
do
{
error = riack_put(client, object, 0, &properties);
if (error != RIACK_SUCCESS) {
ctx->log(ctx, MAPCACHE_WARN, "Retry %d in riak_set for tile %s from cache %s due to eror %d", (4 - retries), key, cache->cache.name, error);
for (connect_error = riack_reconnect(client);
connect_error != RIACK_SUCCESS && retries > 0;
connect_error = riack_reconnect(client))
{
--retries;
}
--retries;
}
}
while (error != RIACK_SUCCESS && retries >= 0);
if (connect_error != RIACK_SUCCESS)
mapcache_connection_pool_invalidate_connection(ctx,pc);
else
mapcache_connection_pool_release_connection(ctx,pc);
if (error != RIACK_SUCCESS)
{
ctx->set_error(ctx, 500, "failed to store tile %s to cache %s due to error %d.", key, cache->cache.name, error);
}
}
/**
* \private \memberof mapcache_cache_riak
*/
static void _mapcache_cache_riak_configuration_parse_xml(mapcache_context *ctx, ezxml_t node, mapcache_cache *cache, mapcache_cfg *config) {
ezxml_t cur_node,xhost,xport,xbucket;
mapcache_cache_riak *dcache = (mapcache_cache_riak*)cache;
int servercount = 0;
for (cur_node = ezxml_child(node,"server"); cur_node; cur_node = cur_node->next) {
servercount++;
}
if (!servercount) {
ctx->set_error(ctx, 400, "riak cache %s has no <server>s configured", cache->name);
return;
}
if (servercount > 1) {
ctx->set_error(ctx, 400, "riak cache %s has more than 1 server configured", cache->name);
return;
}
cur_node = ezxml_child(node, "server");
xhost = ezxml_child(cur_node, "host"); /* Host should contain just server */
xport = ezxml_child(cur_node, "port");
xbucket = ezxml_child(cur_node, "bucket");
if (!xhost || !xhost->txt || ! *xhost->txt) {
ctx->set_error(ctx, 400, "cache %s: <server> with no <host>", cache->name);
return;
} else {
dcache->host = apr_pstrdup(ctx->pool, xhost->txt);
if (dcache->host == NULL) {
ctx->set_error(ctx, 400, "cache %s: failed to allocate host string!", cache->name);
return;
}
}
if (!xport || !xport->txt || ! *xport->txt) {
ctx->set_error(ctx, 400, "cache %s: <server> with no <port>", cache->name);
return;
} else {
dcache->port = atoi(xport->txt);
}
if (!xbucket || !xbucket->txt || ! *xbucket->txt) {
ctx->set_error(ctx, 400, "cache %s: <server> with no <bucket>", cache->name);
return;
} else {
dcache->bucket.value = apr_pstrdup(ctx->pool, xbucket->txt);
if (dcache->bucket.value == NULL) {
ctx->set_error(ctx, 400, "cache %s: failed to allocate bucket string!", cache->name);
return;
}
dcache->bucket.len = strlen(dcache->bucket.value);
}
}
/**
* \private \memberof mapcache_cache_riak
*/
static void _mapcache_cache_riak_configuration_post_config(mapcache_context *ctx, mapcache_cache *cache, mapcache_cfg *cfg) {
riack_init();
}
/**
* \brief creates and initializes a mapcache_riak_cache
*/
mapcache_cache* mapcache_cache_riak_create(mapcache_context *ctx) {
mapcache_cache_riak *cache = apr_pcalloc(ctx->pool,sizeof(mapcache_cache_riak));
if (!cache) {
ctx->set_error(ctx, 500, "failed to allocate riak cache");
return NULL;
}
cache->cache.metadata = apr_table_make(ctx->pool, 3);
cache->cache.type = MAPCACHE_CACHE_RIAK;
cache->cache.tile_get = _mapcache_cache_riak_get;
cache->cache.tile_exists = _mapcache_cache_riak_has_tile;
cache->cache.tile_set = _mapcache_cache_riak_set;
cache->cache.tile_delete = _mapcache_cache_riak_delete;
cache->cache.configuration_parse_xml = _mapcache_cache_riak_configuration_parse_xml;
cache->cache.configuration_post_config = _mapcache_cache_riak_configuration_post_config;
cache->host = NULL;
cache->port = 8087; // Default RIAK port used for protobuf
return (mapcache_cache*)cache;
}
#endif
|