| 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 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/sorcery.h" |
| 35 |
#include "asterisk/astobj2.h" |
| 36 |
#include "asterisk/sched.h" |
| 37 |
#include "asterisk/test.h" |
| 38 |
#include "asterisk/heap.h" |
| 39 |
#include "asterisk/cli.h" |
| 40 |
#include "asterisk/manager.h" |
| 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 |
struct sorcery_memory_cache { |
| 143 |
|
| 144 |
char *name; |
| 145 |
|
| 146 |
struct ao2_container *objects; |
| 147 |
|
| 148 |
unsigned int maximum_objects; |
| 149 |
|
| 150 |
unsigned int object_lifetime_maximum; |
| 151 |
|
| 152 |
unsigned int object_lifetime_stale; |
| 153 |
|
| 154 |
unsigned int expire_on_reload; |
| 155 |
|
| 156 |
unsigned int full_backend_cache; |
| 157 |
|
| 158 |
struct ast_heap *object_heap; |
| 159 |
|
| 160 |
int expire_id; |
| 161 |
|
| 162 |
int stale_update_sched_id; |
| 163 |
|
| 164 |
const struct ast_sorcery *sorcery; |
| 165 |
|
| 166 |
char *object_type; |
| 167 |
|
| 168 |
ast_mutex_t populate_lock; |
| 169 |
|
| 170 |
unsigned int del_expire:1; |
| 171 |
#ifdef TEST_FRAMEWORK |
| 172 |
|
| 173 |
unsigned int cache_notify; |
| 174 |
|
| 175 |
ast_mutex_t lock; |
| 176 |
|
| 177 |
ast_cond_t cond; |
| 178 |
|
| 179 |
unsigned int cache_completed; |
| 180 |
#endif |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
struct sorcery_memory_cached_object { |
| 185 |
|
| 186 |
void *object; |
| 187 |
|
| 188 |
struct timeval created; |
| 189 |
|
| 190 |
ssize_t __heap_index; |
| 191 |
|
| 192 |
int stale_update_sched_id; |
| 193 |
|
| 194 |
struct ast_variable *objectset; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
struct sorcery_memory_cache_fields_cmp_params { |
| 199 |
|
| 200 |
const struct ast_sorcery *sorcery; |
| 201 |
|
| 202 |
struct sorcery_memory_cache *cache; |
| 203 |
|
| 204 |
const struct ast_variable *fields; |
| 205 |
|
| 206 |
regex_t *regex; |
| 207 |
|
| 208 |
const char *prefix; |
| 209 |
|
| 210 |
const size_t prefix_len; |
| 211 |
|
| 212 |
struct ao2_container *container; |
| 213 |
}; |
| 214 |
|
| 215 |
static void *sorcery_memory_cache_open(const char *data); |
| 216 |
static int sorcery_memory_cache_create(const struct ast_sorcery *sorcery, void *data, void *object); |
| 217 |
static void sorcery_memory_cache_load(void *data, const struct ast_sorcery *sorcery, const char *type); |
| 218 |
static void sorcery_memory_cache_reload(void *data, const struct ast_sorcery *sorcery, const char *type); |
| 219 |
static void *sorcery_memory_cache_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 220 |
const char *id); |
| 221 |
static void *sorcery_memory_cache_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 222 |
const struct ast_variable *fields); |
| 223 |
static void sorcery_memory_cache_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 224 |
struct ao2_container *objects, const struct ast_variable *fields); |
| 225 |
static void sorcery_memory_cache_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 226 |
struct ao2_container *objects, const char *regex); |
| 227 |
static void sorcery_memory_cache_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 228 |
struct ao2_container *objects, const char *prefix, const size_t prefix_len); |
| 229 |
static int sorcery_memory_cache_delete(const struct ast_sorcery *sorcery, void *data, void *object); |
| 230 |
static void sorcery_memory_cache_close(void *data); |
| 231 |
|
| 232 |
static struct ast_sorcery_wizard memory_cache_object_wizard = { |
| 233 |
.name = "memory_cache", |
| 234 |
.open = sorcery_memory_cache_open, |
| 235 |
.create = sorcery_memory_cache_create, |
| 236 |
.update = sorcery_memory_cache_create, |
| 237 |
.delete = sorcery_memory_cache_delete, |
| 238 |
.load = sorcery_memory_cache_load, |
| 239 |
.reload = sorcery_memory_cache_reload, |
| 240 |
.retrieve_id = sorcery_memory_cache_retrieve_id, |
| 241 |
.retrieve_fields = sorcery_memory_cache_retrieve_fields, |
| 242 |
.retrieve_multiple = sorcery_memory_cache_retrieve_multiple, |
| 243 |
.retrieve_regex = sorcery_memory_cache_retrieve_regex, |
| 244 |
.retrieve_prefix = sorcery_memory_cache_retrieve_prefix, |
| 245 |
.close = sorcery_memory_cache_close, |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
#define CACHES_CONTAINER_BUCKET_SIZE 53 |
| 250 |
|
| 251 |
|
| 252 |
#define CACHE_CONTAINER_BUCKET_SIZE 53 |
| 253 |
|
| 254 |
|
| 255 |
#define CACHE_HEAP_INIT_HEIGHT 5 |
| 256 |
|
| 257 |
|
| 258 |
static struct ao2_container *caches; |
| 259 |
|
| 260 |
|
| 261 |
static struct ast_sched_context *sched; |
| 262 |
|
| 263 |
#define PASSTHRU_UPDATE_THREAD_ID 0x5EED1E55 |
| 264 |
AST_THREADSTORAGE(passthru_update_id_storage); |
| 265 |
|
| 266 |
static int is_passthru_update(void) |
| 267 |
{ |
| 268 |
uint32_t *passthru_update_thread_id; |
| 269 |
|
| 270 |
passthru_update_thread_id = ast_threadstorage_get(&passthru_update_id_storage, |
| 271 |
sizeof(*passthru_update_thread_id)); |
| 272 |
if (!passthru_update_thread_id) { |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
return *passthru_update_thread_id == PASSTHRU_UPDATE_THREAD_ID; |
| 277 |
} |
| 278 |
|
| 279 |
static void set_passthru_update(uint32_t value) |
| 280 |
{ |
| 281 |
uint32_t *passthru_update_thread_id; |
| 282 |
|
| 283 |
passthru_update_thread_id = ast_threadstorage_get(&passthru_update_id_storage, |
| 284 |
sizeof(*passthru_update_thread_id)); |
| 285 |
if (!passthru_update_thread_id) { |
| 286 |
ast_log(LOG_ERROR, "Could not set passthru update ID for sorcery memory cache thread\n"); |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
*passthru_update_thread_id = value; |
| 291 |
} |
| 292 |
|
| 293 |
static void start_passthru_update(void) |
| 294 |
{ |
| 295 |
set_passthru_update(PASSTHRU_UPDATE_THREAD_ID); |
| 296 |
} |
| 297 |
|
| 298 |
static void end_passthru_update(void) |
| 299 |
{ |
| 300 |
set_passthru_update(0); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static int sorcery_memory_cache_hash(const void *obj, int flags) |
| 313 |
{ |
| 314 |
const struct sorcery_memory_cache *cache = obj; |
| 315 |
const char *name = obj; |
| 316 |
int hash; |
| 317 |
|
| 318 |
switch (flags & OBJ_SEARCH_MASK) { |
| 319 |
default: |
| 320 |
case OBJ_SEARCH_OBJECT: |
| 321 |
name = cache->name; |
| 322 |
|
| 323 |
case OBJ_SEARCH_KEY: |
| 324 |
hash = ast_str_hash(name); |
| 325 |
break; |
| 326 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 327 |
|
| 328 |
ast_assert(0); |
| 329 |
hash = 0; |
| 330 |
break; |
| 331 |
} |
| 332 |
return hash; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
static int sorcery_memory_cache_cmp(void *obj, void *arg, int flags) |
| 347 |
{ |
| 348 |
const struct sorcery_memory_cache *left = obj; |
| 349 |
const struct sorcery_memory_cache *right = arg; |
| 350 |
const char *right_name = arg; |
| 351 |
int cmp; |
| 352 |
|
| 353 |
switch (flags & OBJ_SEARCH_MASK) { |
| 354 |
default: |
| 355 |
case OBJ_SEARCH_OBJECT: |
| 356 |
right_name = right->name; |
| 357 |
|
| 358 |
case OBJ_SEARCH_KEY: |
| 359 |
cmp = strcmp(left->name, right_name); |
| 360 |
break; |
| 361 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 362 |
cmp = strncmp(left->name, right_name, strlen(right_name)); |
| 363 |
break; |
| 364 |
} |
| 365 |
return cmp ? 0 : CMP_MATCH; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
static int sorcery_memory_cached_object_hash(const void *obj, int flags) |
| 378 |
{ |
| 379 |
const struct sorcery_memory_cached_object *cached = obj; |
| 380 |
const char *name = obj; |
| 381 |
int hash; |
| 382 |
|
| 383 |
switch (flags & OBJ_SEARCH_MASK) { |
| 384 |
default: |
| 385 |
case OBJ_SEARCH_OBJECT: |
| 386 |
name = ast_sorcery_object_get_id(cached->object); |
| 387 |
|
| 388 |
case OBJ_SEARCH_KEY: |
| 389 |
hash = ast_str_hash(name); |
| 390 |
break; |
| 391 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 392 |
|
| 393 |
ast_assert(0); |
| 394 |
hash = 0; |
| 395 |
break; |
| 396 |
} |
| 397 |
return hash; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
static int sorcery_memory_cached_object_cmp(void *obj, void *arg, int flags) |
| 412 |
{ |
| 413 |
struct sorcery_memory_cached_object *left = obj; |
| 414 |
struct sorcery_memory_cached_object *right = arg; |
| 415 |
const char *right_name = arg; |
| 416 |
int cmp; |
| 417 |
|
| 418 |
switch (flags & OBJ_SEARCH_MASK) { |
| 419 |
default: |
| 420 |
case OBJ_SEARCH_OBJECT: |
| 421 |
right_name = ast_sorcery_object_get_id(right->object); |
| 422 |
|
| 423 |
case OBJ_SEARCH_KEY: |
| 424 |
cmp = strcmp(ast_sorcery_object_get_id(left->object), right_name); |
| 425 |
break; |
| 426 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 427 |
cmp = strncmp(ast_sorcery_object_get_id(left->object), right_name, strlen(right_name)); |
| 428 |
break; |
| 429 |
} |
| 430 |
return cmp ? 0 : CMP_MATCH; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
static void sorcery_memory_cache_destructor(void *obj) |
| 440 |
{ |
| 441 |
struct sorcery_memory_cache *cache = obj; |
| 442 |
|
| 443 |
ast_free(cache->name); |
| 444 |
if (cache->object_heap) { |
| 445 |
ast_heap_destroy(cache->object_heap); |
| 446 |
} |
| 447 |
ao2_cleanup(cache->objects); |
| 448 |
ast_free(cache->object_type); |
| 449 |
ast_mutex_destroy(&cache->populate_lock); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
static void sorcery_memory_cached_object_destructor(void *obj) |
| 459 |
{ |
| 460 |
struct sorcery_memory_cached_object *cached = obj; |
| 461 |
|
| 462 |
ao2_cleanup(cached->object); |
| 463 |
ast_variables_destroy(cached->objectset); |
| 464 |
} |
| 465 |
|
| 466 |
static int schedule_cache_expiration(struct sorcery_memory_cache *cache); |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
static int remove_from_cache(struct sorcery_memory_cache *cache, const char *id, int reschedule) |
| 484 |
{ |
| 485 |
struct sorcery_memory_cached_object *hash_object; |
| 486 |
struct sorcery_memory_cached_object *oldest_object; |
| 487 |
struct sorcery_memory_cached_object *heap_object; |
| 488 |
|
| 489 |
hash_object = ao2_find(cache->objects, id, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NOLOCK); |
| 490 |
if (!hash_object) { |
| 491 |
return -1; |
| 492 |
} |
| 493 |
|
| 494 |
ast_assert(!strcmp(ast_sorcery_object_get_id(hash_object->object), id)); |
| 495 |
|
| 496 |
oldest_object = ast_heap_peek(cache->object_heap, 1); |
| 497 |
heap_object = ast_heap_remove(cache->object_heap, hash_object); |
| 498 |
|
| 499 |
ast_assert(heap_object == hash_object); |
| 500 |
|
| 501 |
ao2_ref(hash_object, -1); |
| 502 |
|
| 503 |
if (reschedule && (oldest_object == heap_object)) { |
| 504 |
schedule_cache_expiration(cache); |
| 505 |
} |
| 506 |
|
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
static int expire_objects_from_cache(const void *data) |
| 517 |
{ |
| 518 |
struct sorcery_memory_cache *cache = (struct sorcery_memory_cache *)data; |
| 519 |
struct sorcery_memory_cached_object *cached; |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
while (ao2_trywrlock(cache->objects)) { |
| 529 |
if (cache->del_expire) { |
| 530 |
cache->expire_id = -1; |
| 531 |
ao2_ref(cache, -1); |
| 532 |
return 0; |
| 533 |
} |
| 534 |
sched_yield(); |
| 535 |
} |
| 536 |
|
| 537 |
cache->expire_id = -1; |
| 538 |
|
| 539 |
|
| 540 |
while ((cached = ast_heap_peek(cache->object_heap, 1))) { |
| 541 |
int expiration; |
| 542 |
|
| 543 |
expiration = ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(cache->object_lifetime_maximum, 1)), ast_tvnow()); |
| 544 |
|
| 545 |
|
| 546 |
if (expiration > 0) { |
| 547 |
break; |
| 548 |
} |
| 549 |
|
| 550 |
remove_from_cache(cache, ast_sorcery_object_get_id(cached->object), 0); |
| 551 |
} |
| 552 |
|
| 553 |
schedule_cache_expiration(cache); |
| 554 |
|
| 555 |
ao2_unlock(cache->objects); |
| 556 |
|
| 557 |
ao2_ref(cache, -1); |
| 558 |
|
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static void remove_all_from_cache(struct sorcery_memory_cache *cache) |
| 573 |
{ |
| 574 |
while (ast_heap_pop(cache->object_heap)) { |
| 575 |
} |
| 576 |
|
| 577 |
ao2_callback(cache->objects, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, |
| 578 |
NULL, NULL); |
| 579 |
|
| 580 |
cache->del_expire = 1; |
| 581 |
AST_SCHED_DEL_UNREF(sched, cache->expire_id, ao2_ref(cache, -1)); |
| 582 |
cache->del_expire = 0; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
static int object_stale_callback(void *obj, void *arg, int flags) |
| 596 |
{ |
| 597 |
struct sorcery_memory_cached_object *cached = obj; |
| 598 |
struct sorcery_memory_cache *cache = arg; |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
cached->created = ast_tvsub(cached->created, ast_samp2tv(cache->object_lifetime_stale + 1, 1)); |
| 605 |
|
| 606 |
return CMP_MATCH; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
static int mark_object_as_stale_in_cache(struct sorcery_memory_cache *cache, const char *id) |
| 624 |
{ |
| 625 |
struct sorcery_memory_cached_object *cached; |
| 626 |
|
| 627 |
cached = ao2_find(cache->objects, id, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 628 |
if (!cached) { |
| 629 |
return -1; |
| 630 |
} |
| 631 |
|
| 632 |
ast_assert(!strcmp(ast_sorcery_object_get_id(cached->object), id)); |
| 633 |
|
| 634 |
object_stale_callback(cached, cache, 0); |
| 635 |
ao2_ref(cached, -1); |
| 636 |
|
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
static void mark_all_as_stale_in_cache(struct sorcery_memory_cache *cache) |
| 651 |
{ |
| 652 |
ao2_callback(cache->objects, OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, object_stale_callback, cache); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
static int schedule_cache_expiration(struct sorcery_memory_cache *cache) |
| 667 |
{ |
| 668 |
struct sorcery_memory_cached_object *cached; |
| 669 |
int expiration = 0; |
| 670 |
|
| 671 |
if (!cache->object_lifetime_maximum) { |
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
cache->del_expire = 1; |
| 676 |
AST_SCHED_DEL_UNREF(sched, cache->expire_id, ao2_ref(cache, -1)); |
| 677 |
cache->del_expire = 0; |
| 678 |
|
| 679 |
cached = ast_heap_peek(cache->object_heap, 1); |
| 680 |
if (!cached) { |
| 681 |
#ifdef TEST_FRAMEWORK |
| 682 |
ast_mutex_lock(&cache->lock); |
| 683 |
cache->cache_completed = 1; |
| 684 |
ast_cond_signal(&cache->cond); |
| 685 |
ast_mutex_unlock(&cache->lock); |
| 686 |
#endif |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
expiration = MAX(ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(cache->object_lifetime_maximum, 1)), ast_tvnow()), |
| 691 |
1); |
| 692 |
|
| 693 |
cache->expire_id = ast_sched_add(sched, expiration, expire_objects_from_cache, ao2_bump(cache)); |
| 694 |
if (cache->expire_id < 0) { |
| 695 |
ao2_ref(cache, -1); |
| 696 |
return -1; |
| 697 |
} |
| 698 |
|
| 699 |
return 0; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
static int remove_oldest_from_cache(struct sorcery_memory_cache *cache) |
| 714 |
{ |
| 715 |
struct sorcery_memory_cached_object *heap_old_object; |
| 716 |
struct sorcery_memory_cached_object *hash_old_object; |
| 717 |
|
| 718 |
heap_old_object = ast_heap_pop(cache->object_heap); |
| 719 |
if (!heap_old_object) { |
| 720 |
return -1; |
| 721 |
} |
| 722 |
hash_old_object = ao2_find(cache->objects, heap_old_object, |
| 723 |
OBJ_SEARCH_OBJECT | OBJ_UNLINK | OBJ_NOLOCK); |
| 724 |
|
| 725 |
ast_assert(heap_old_object == hash_old_object); |
| 726 |
|
| 727 |
ao2_ref(hash_old_object, -1); |
| 728 |
|
| 729 |
schedule_cache_expiration(cache); |
| 730 |
|
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
static int add_to_cache(struct sorcery_memory_cache *cache, |
| 747 |
struct sorcery_memory_cached_object *cached_object) |
| 748 |
{ |
| 749 |
struct sorcery_memory_cached_object *front; |
| 750 |
|
| 751 |
if (!ao2_link_flags(cache->objects, cached_object, OBJ_NOLOCK)) { |
| 752 |
return -1; |
| 753 |
} |
| 754 |
|
| 755 |
if (cache->full_backend_cache && (front = ast_heap_peek(cache->object_heap, 1))) { |
| 756 |
|
| 757 |
cached_object->created = front->created; |
| 758 |
} |
| 759 |
|
| 760 |
if (ast_heap_push(cache->object_heap, cached_object)) { |
| 761 |
ao2_find(cache->objects, cached_object, |
| 762 |
OBJ_SEARCH_OBJECT | OBJ_UNLINK | OBJ_NODATA | OBJ_NOLOCK); |
| 763 |
return -1; |
| 764 |
} |
| 765 |
|
| 766 |
if (cache->expire_id == -1) { |
| 767 |
schedule_cache_expiration(cache); |
| 768 |
} |
| 769 |
|
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
static struct sorcery_memory_cached_object *sorcery_memory_cached_object_alloc(const struct ast_sorcery *sorcery, |
| 785 |
const struct sorcery_memory_cache *cache, void *object) |
| 786 |
{ |
| 787 |
struct sorcery_memory_cached_object *cached; |
| 788 |
|
| 789 |
cached = ao2_alloc(sizeof(*cached), sorcery_memory_cached_object_destructor); |
| 790 |
if (!cached) { |
| 791 |
return NULL; |
| 792 |
} |
| 793 |
|
| 794 |
cached->object = ao2_bump(object); |
| 795 |
cached->created = ast_tvnow(); |
| 796 |
cached->stale_update_sched_id = -1; |
| 797 |
|
| 798 |
if (cache->full_backend_cache) { |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
cached->objectset = ast_sorcery_objectset_create(sorcery, object); |
| 803 |
if (!cached->objectset) { |
| 804 |
ao2_ref(cached, -1); |
| 805 |
return NULL; |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
return cached; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
static int sorcery_memory_cache_create(const struct ast_sorcery *sorcery, void *data, void *object) |
| 824 |
{ |
| 825 |
struct sorcery_memory_cache *cache = data; |
| 826 |
struct sorcery_memory_cached_object *cached; |
| 827 |
|
| 828 |
cached = sorcery_memory_cached_object_alloc(sorcery, cache, object); |
| 829 |
if (!cached) { |
| 830 |
return -1; |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
ao2_wrlock(cache->objects); |
| 840 |
remove_from_cache(cache, ast_sorcery_object_get_id(object), 1); |
| 841 |
if (cache->maximum_objects && ao2_container_count(cache->objects) >= cache->maximum_objects) { |
| 842 |
if (remove_oldest_from_cache(cache)) { |
| 843 |
ast_log(LOG_ERROR, "Unable to make room in cache for sorcery object '%s'.\n", |
| 844 |
ast_sorcery_object_get_id(object)); |
| 845 |
ao2_unlock(cache->objects); |
| 846 |
ao2_ref(cached, -1); |
| 847 |
return -1; |
| 848 |
} |
| 849 |
ast_assert(ao2_container_count(cache->objects) != cache->maximum_objects); |
| 850 |
} |
| 851 |
if (add_to_cache(cache, cached)) { |
| 852 |
ast_log(LOG_ERROR, "Unable to add object '%s' to the cache\n", |
| 853 |
ast_sorcery_object_get_id(object)); |
| 854 |
ao2_unlock(cache->objects); |
| 855 |
ao2_ref(cached, -1); |
| 856 |
return -1; |
| 857 |
} |
| 858 |
ao2_unlock(cache->objects); |
| 859 |
|
| 860 |
ao2_ref(cached, -1); |
| 861 |
return 0; |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
static int object_add_to_cache_callback(void *obj, void *arg, void *data, int flags) |
| 874 |
{ |
| 875 |
struct sorcery_memory_cache *cache = data; |
| 876 |
struct sorcery_memory_cached_object *cached; |
| 877 |
|
| 878 |
cached = sorcery_memory_cached_object_alloc(arg, cache, obj); |
| 879 |
if (!cached) { |
| 880 |
return CMP_STOP; |
| 881 |
} |
| 882 |
|
| 883 |
add_to_cache(cache, cached); |
| 884 |
ao2_ref(cached, -1); |
| 885 |
|
| 886 |
return 0; |
| 887 |
} |
| 888 |
|
| 889 |
struct stale_cache_update_task_data { |
| 890 |
struct ast_sorcery *sorcery; |
| 891 |
struct sorcery_memory_cache *cache; |
| 892 |
char *type; |
| 893 |
}; |
| 894 |
|
| 895 |
static void stale_cache_update_task_data_destructor(void *obj) |
| 896 |
{ |
| 897 |
struct stale_cache_update_task_data *task_data = obj; |
| 898 |
|
| 899 |
ao2_cleanup(task_data->cache); |
| 900 |
ast_sorcery_unref(task_data->sorcery); |
| 901 |
ast_free(task_data->type); |
| 902 |
} |
| 903 |
|
| 904 |
static struct stale_cache_update_task_data *stale_cache_update_task_data_alloc(struct ast_sorcery *sorcery, |
| 905 |
struct sorcery_memory_cache *cache, const char *type) |
| 906 |
{ |
| 907 |
struct stale_cache_update_task_data *task_data; |
| 908 |
|
| 909 |
task_data = ao2_alloc_options(sizeof(*task_data), stale_cache_update_task_data_destructor, |
| 910 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 911 |
if (!task_data) { |
| 912 |
return NULL; |
| 913 |
} |
| 914 |
|
| 915 |
task_data->sorcery = ao2_bump(sorcery); |
| 916 |
task_data->cache = ao2_bump(cache); |
| 917 |
task_data->type = ast_strdup(type); |
| 918 |
if (!task_data->type) { |
| 919 |
ao2_ref(task_data, -1); |
| 920 |
return NULL; |
| 921 |
} |
| 922 |
|
| 923 |
return task_data; |
| 924 |
} |
| 925 |
|
| 926 |
static int stale_cache_update(const void *data) |
| 927 |
{ |
| 928 |
struct stale_cache_update_task_data *task_data = (struct stale_cache_update_task_data *) data; |
| 929 |
struct ao2_container *backend_objects; |
| 930 |
|
| 931 |
start_passthru_update(); |
| 932 |
backend_objects = ast_sorcery_retrieve_by_fields(task_data->sorcery, task_data->type, |
| 933 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 934 |
end_passthru_update(); |
| 935 |
|
| 936 |
if (!backend_objects) { |
| 937 |
task_data->cache->stale_update_sched_id = -1; |
| 938 |
ao2_ref(task_data, -1); |
| 939 |
return 0; |
| 940 |
} |
| 941 |
|
| 942 |
if (task_data->cache->maximum_objects && ao2_container_count(backend_objects) >= task_data->cache->maximum_objects) { |
| 943 |
ast_log(LOG_ERROR, "The backend contains %d objects while the sorcery memory cache '%s' is explicitly configured to only allow %d\n", |
| 944 |
ao2_container_count(backend_objects), task_data->cache->name, task_data->cache->maximum_objects); |
| 945 |
task_data->cache->stale_update_sched_id = -1; |
| 946 |
ao2_ref(task_data, -1); |
| 947 |
return 0; |
| 948 |
} |
| 949 |
|
| 950 |
ao2_wrlock(task_data->cache->objects); |
| 951 |
remove_all_from_cache(task_data->cache); |
| 952 |
ao2_callback_data(backend_objects, OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, object_add_to_cache_callback, |
| 953 |
task_data->sorcery, task_data->cache); |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
if (ao2_container_count(task_data->cache->objects) != ao2_container_count(backend_objects)) { |
| 960 |
ast_log(LOG_WARNING, "The backend contains %d objects while only %d could be added to sorcery memory cache '%s'\n", |
| 961 |
ao2_container_count(backend_objects), ao2_container_count(task_data->cache->objects), task_data->cache->name); |
| 962 |
remove_all_from_cache(task_data->cache); |
| 963 |
} |
| 964 |
|
| 965 |
ao2_unlock(task_data->cache->objects); |
| 966 |
ao2_ref(backend_objects, -1); |
| 967 |
|
| 968 |
task_data->cache->stale_update_sched_id = -1; |
| 969 |
ao2_ref(task_data, -1); |
| 970 |
|
| 971 |
return 0; |
| 972 |
} |
| 973 |
|
| 974 |
struct stale_update_task_data { |
| 975 |
struct ast_sorcery *sorcery; |
| 976 |
struct sorcery_memory_cache *cache; |
| 977 |
void *object; |
| 978 |
}; |
| 979 |
|
| 980 |
static void stale_update_task_data_destructor(void *obj) |
| 981 |
{ |
| 982 |
struct stale_update_task_data *task_data = obj; |
| 983 |
|
| 984 |
ao2_cleanup(task_data->cache); |
| 985 |
ao2_cleanup(task_data->object); |
| 986 |
ast_sorcery_unref(task_data->sorcery); |
| 987 |
} |
| 988 |
|
| 989 |
static struct stale_update_task_data *stale_update_task_data_alloc(struct ast_sorcery *sorcery, |
| 990 |
struct sorcery_memory_cache *cache, const char *type, void *object) |
| 991 |
{ |
| 992 |
struct stale_update_task_data *task_data; |
| 993 |
|
| 994 |
task_data = ao2_alloc_options(sizeof(*task_data), stale_update_task_data_destructor, |
| 995 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 996 |
if (!task_data) { |
| 997 |
return NULL; |
| 998 |
} |
| 999 |
|
| 1000 |
task_data->sorcery = ao2_bump(sorcery); |
| 1001 |
task_data->cache = ao2_bump(cache); |
| 1002 |
task_data->object = ao2_bump(object); |
| 1003 |
|
| 1004 |
return task_data; |
| 1005 |
} |
| 1006 |
|
| 1007 |
static int stale_item_update(const void *data) |
| 1008 |
{ |
| 1009 |
struct stale_update_task_data *task_data = (struct stale_update_task_data *) data; |
| 1010 |
void *object; |
| 1011 |
|
| 1012 |
start_passthru_update(); |
| 1013 |
|
| 1014 |
object = ast_sorcery_retrieve_by_id(task_data->sorcery, |
| 1015 |
ast_sorcery_object_get_type(task_data->object), |
| 1016 |
ast_sorcery_object_get_id(task_data->object)); |
| 1017 |
if (!object) { |
| 1018 |
ast_debug(1, "Backend no longer has object type '%s' ID '%s'. Removing from cache\n", |
| 1019 |
ast_sorcery_object_get_type(task_data->object), |
| 1020 |
ast_sorcery_object_get_id(task_data->object)); |
| 1021 |
sorcery_memory_cache_delete(task_data->sorcery, task_data->cache, |
| 1022 |
task_data->object); |
| 1023 |
} else { |
| 1024 |
ast_debug(1, "Refreshing stale cache object type '%s' ID '%s'\n", |
| 1025 |
ast_sorcery_object_get_type(task_data->object), |
| 1026 |
ast_sorcery_object_get_id(task_data->object)); |
| 1027 |
sorcery_memory_cache_create(task_data->sorcery, task_data->cache, |
| 1028 |
object); |
| 1029 |
ao2_ref(object, -1); |
| 1030 |
} |
| 1031 |
|
| 1032 |
ast_test_suite_event_notify("SORCERY_MEMORY_CACHE_REFRESHED", "Cache: %s\r\nType: %s\r\nName: %s\r\n", |
| 1033 |
task_data->cache->name, ast_sorcery_object_get_type(task_data->object), |
| 1034 |
ast_sorcery_object_get_id(task_data->object)); |
| 1035 |
|
| 1036 |
ao2_ref(task_data, -1); |
| 1037 |
end_passthru_update(); |
| 1038 |
|
| 1039 |
return 0; |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
static void memory_cache_populate_internal(const struct ast_sorcery *sorcery, const char *type, struct sorcery_memory_cache *cache) |
| 1056 |
{ |
| 1057 |
struct ao2_container *backend_objects; |
| 1058 |
|
| 1059 |
start_passthru_update(); |
| 1060 |
backend_objects = ast_sorcery_retrieve_by_fields(sorcery, type, AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 1061 |
end_passthru_update(); |
| 1062 |
|
| 1063 |
if (!backend_objects) { |
| 1064 |
|
| 1065 |
return; |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (cache->maximum_objects && ao2_container_count(backend_objects) >= cache->maximum_objects) { |
| 1069 |
ast_log(LOG_ERROR, "The backend contains %d objects while the sorcery memory cache '%s' is explicitly configured to only allow %d\n", |
| 1070 |
ao2_container_count(backend_objects), cache->name, cache->maximum_objects); |
| 1071 |
ao2_ref(backend_objects, -1); |
| 1072 |
return; |
| 1073 |
} |
| 1074 |
|
| 1075 |
ao2_callback_data(backend_objects, OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, object_add_to_cache_callback, |
| 1076 |
(struct ast_sorcery*)sorcery, cache); |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
if (ao2_container_count(cache->objects) != ao2_container_count(backend_objects)) { |
| 1083 |
ast_log(LOG_WARNING, "The backend contains %d objects while only %d could be added to sorcery memory cache '%s'\n", |
| 1084 |
ao2_container_count(backend_objects), ao2_container_count(cache->objects), cache->name); |
| 1085 |
remove_all_from_cache(cache); |
| 1086 |
} |
| 1087 |
|
| 1088 |
ao2_ref(backend_objects, -1); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
static int memory_cache_populate_external(const struct ast_sorcery *sorcery, const char *type, struct sorcery_memory_cache *cache) |
| 1108 |
{ |
| 1109 |
struct ao2_container *backend_objects; |
| 1110 |
struct ao2_iterator it_backend; |
| 1111 |
void *object; |
| 1112 |
AST_VECTOR(, struct sorcery_memory_cached_object *) cached_objects; |
| 1113 |
int i, num_cached; |
| 1114 |
|
| 1115 |
|
| 1116 |
start_passthru_update(); |
| 1117 |
backend_objects = ast_sorcery_retrieve_by_fields(sorcery, type, AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 1118 |
end_passthru_update(); |
| 1119 |
|
| 1120 |
if (!backend_objects) { |
| 1121 |
|
| 1122 |
return -1; |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (cache->maximum_objects && ao2_container_count(backend_objects) >= cache->maximum_objects) { |
| 1126 |
ast_log(LOG_ERROR, "The backend contains %d objects while the sorcery memory cache '%s' is explicitly configured to only allow %d\n", |
| 1127 |
ao2_container_count(backend_objects), cache->name, cache->maximum_objects); |
| 1128 |
ao2_ref(backend_objects, -1); |
| 1129 |
return -1; |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
if (AST_VECTOR_INIT(&cached_objects, ao2_container_count(backend_objects))) { |
| 1134 |
ast_log(LOG_ERROR, "Could not allocate vector for cached objects for sorcery memory cache '%s'\n", |
| 1135 |
cache->name); |
| 1136 |
ao2_ref(backend_objects, -1); |
| 1137 |
return -1; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
it_backend = ao2_iterator_init(backend_objects, 0); |
| 1142 |
|
| 1143 |
while ((object = ao2_iterator_next(&it_backend))) { |
| 1144 |
struct sorcery_memory_cached_object *cached; |
| 1145 |
|
| 1146 |
cached = sorcery_memory_cached_object_alloc(sorcery, cache, object); |
| 1147 |
ao2_ref(object, -1); |
| 1148 |
|
| 1149 |
if (!cached || AST_VECTOR_APPEND(&cached_objects, cached)) { |
| 1150 |
ao2_cleanup(cached); |
| 1151 |
ao2_iterator_destroy(&it_backend); |
| 1152 |
ao2_ref(backend_objects, -1); |
| 1153 |
|
| 1154 |
for (i = 0; i < AST_VECTOR_SIZE(&cached_objects); i++) { |
| 1155 |
ao2_ref(AST_VECTOR_GET(&cached_objects, i), -1); |
| 1156 |
} |
| 1157 |
AST_VECTOR_FREE(&cached_objects); |
| 1158 |
return -1; |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
ao2_iterator_destroy(&it_backend); |
| 1163 |
|
| 1164 |
|
| 1165 |
ao2_wrlock(cache->objects); |
| 1166 |
|
| 1167 |
|
| 1168 |
remove_all_from_cache(cache); |
| 1169 |
|
| 1170 |
num_cached = AST_VECTOR_SIZE(&cached_objects); |
| 1171 |
|
| 1172 |
|
| 1173 |
for (i = 0; i < num_cached; i++) { |
| 1174 |
struct sorcery_memory_cached_object *cached = AST_VECTOR_GET(&cached_objects, i); |
| 1175 |
add_to_cache(cache, cached); |
| 1176 |
} |
| 1177 |
|
| 1178 |
ao2_unlock(cache->objects); |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
for (i = 0; i < num_cached; i++) { |
| 1185 |
ao2_ref(AST_VECTOR_GET(&cached_objects, i), -1); |
| 1186 |
} |
| 1187 |
AST_VECTOR_FREE(&cached_objects); |
| 1188 |
|
| 1189 |
ao2_ref(backend_objects, -1); |
| 1190 |
return num_cached; |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
static void memory_cache_full_update(const struct ast_sorcery *sorcery, const char *type, struct sorcery_memory_cache *cache) |
| 1202 |
{ |
| 1203 |
if (!cache->full_backend_cache) { |
| 1204 |
return; |
| 1205 |
} |
| 1206 |
|
| 1207 |
ao2_wrlock(cache->objects); |
| 1208 |
if (!ao2_container_count(cache->objects)) { |
| 1209 |
memory_cache_populate_internal(sorcery, type, cache); |
| 1210 |
} |
| 1211 |
ao2_unlock(cache->objects); |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
static void memory_cache_stale_update_full(const struct ast_sorcery *sorcery, struct sorcery_memory_cache *cache, |
| 1223 |
const char *type) |
| 1224 |
{ |
| 1225 |
ao2_wrlock(cache->objects); |
| 1226 |
if (cache->stale_update_sched_id == -1) { |
| 1227 |
struct stale_cache_update_task_data *task_data; |
| 1228 |
|
| 1229 |
task_data = stale_cache_update_task_data_alloc((struct ast_sorcery *) sorcery, |
| 1230 |
cache, type); |
| 1231 |
if (task_data) { |
| 1232 |
cache->stale_update_sched_id = ast_sched_add(sched, 1, |
| 1233 |
stale_cache_update, task_data); |
| 1234 |
} |
| 1235 |
if (cache->stale_update_sched_id < 0) { |
| 1236 |
ao2_cleanup(task_data); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
ao2_unlock(cache->objects); |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
static void memory_cache_stale_update_object(const struct ast_sorcery *sorcery, struct sorcery_memory_cache *cache, |
| 1251 |
struct sorcery_memory_cached_object *cached) |
| 1252 |
{ |
| 1253 |
ao2_lock(cached); |
| 1254 |
if (cached->stale_update_sched_id == -1) { |
| 1255 |
struct stale_update_task_data *task_data; |
| 1256 |
|
| 1257 |
task_data = stale_update_task_data_alloc((struct ast_sorcery *) sorcery, |
| 1258 |
cache, ast_sorcery_object_get_type(cached->object), cached->object); |
| 1259 |
if (task_data) { |
| 1260 |
ast_debug(1, "Cached sorcery object type '%s' ID '%s' is stale. Refreshing\n", |
| 1261 |
ast_sorcery_object_get_type(cached->object), ast_sorcery_object_get_id(cached->object)); |
| 1262 |
cached->stale_update_sched_id = ast_sched_add(sched, 1, |
| 1263 |
stale_item_update, task_data); |
| 1264 |
} |
| 1265 |
if (cached->stale_update_sched_id < 0) { |
| 1266 |
ao2_cleanup(task_data); |
| 1267 |
ast_log(LOG_ERROR, "Unable to update stale cached object type '%s', ID '%s'.\n", |
| 1268 |
ast_sorcery_object_get_type(cached->object), ast_sorcery_object_get_id(cached->object)); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
ao2_unlock(cached); |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
static void memory_cache_stale_check_object(const struct ast_sorcery *sorcery, struct sorcery_memory_cache *cache, |
| 1283 |
struct sorcery_memory_cached_object *cached) |
| 1284 |
{ |
| 1285 |
struct timeval elapsed; |
| 1286 |
|
| 1287 |
if (!cache->object_lifetime_stale) { |
| 1288 |
return; |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
elapsed = ast_tvsub(ast_tvnow(), cached->created); |
| 1293 |
|
| 1294 |
if (elapsed.tv_sec < cache->object_lifetime_stale) { |
| 1295 |
return; |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (cache->full_backend_cache) { |
| 1299 |
memory_cache_stale_update_full(sorcery, cache, ast_sorcery_object_get_type(cached->object)); |
| 1300 |
} else { |
| 1301 |
memory_cache_stale_update_object(sorcery, cache, cached); |
| 1302 |
} |
| 1303 |
|
| 1304 |
} |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
static void memory_cache_stale_check(const struct ast_sorcery *sorcery, struct sorcery_memory_cache *cache) |
| 1316 |
{ |
| 1317 |
struct sorcery_memory_cached_object *cached; |
| 1318 |
|
| 1319 |
ao2_rdlock(cache->objects); |
| 1320 |
cached = ao2_bump(ast_heap_peek(cache->object_heap, 1)); |
| 1321 |
ao2_unlock(cache->objects); |
| 1322 |
|
| 1323 |
if (!cached) { |
| 1324 |
return; |
| 1325 |
} |
| 1326 |
|
| 1327 |
memory_cache_stale_check_object(sorcery, cache, cached); |
| 1328 |
ao2_ref(cached, -1); |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
static void *sorcery_memory_cache_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id) |
| 1344 |
{ |
| 1345 |
struct sorcery_memory_cache *cache = data; |
| 1346 |
struct sorcery_memory_cached_object *cached; |
| 1347 |
void *object; |
| 1348 |
|
| 1349 |
if (is_passthru_update()) { |
| 1350 |
return NULL; |
| 1351 |
} |
| 1352 |
|
| 1353 |
memory_cache_full_update(sorcery, type, cache); |
| 1354 |
|
| 1355 |
cached = ao2_find(cache->objects, id, OBJ_SEARCH_KEY); |
| 1356 |
if (!cached) { |
| 1357 |
return NULL; |
| 1358 |
} |
| 1359 |
|
| 1360 |
ast_assert(!strcmp(ast_sorcery_object_get_id(cached->object), id)); |
| 1361 |
|
| 1362 |
memory_cache_stale_check_object(sorcery, cache, cached); |
| 1363 |
|
| 1364 |
object = ao2_bump(cached->object); |
| 1365 |
ao2_ref(cached, -1); |
| 1366 |
|
| 1367 |
return object; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
static int sorcery_memory_cache_fields_cmp(void *obj, void *arg, int flags) |
| 1379 |
{ |
| 1380 |
struct sorcery_memory_cached_object *cached = obj; |
| 1381 |
const struct sorcery_memory_cache_fields_cmp_params *params = arg; |
| 1382 |
RAII_VAR(struct ast_variable *, diff, NULL, ast_variables_destroy); |
| 1383 |
|
| 1384 |
if (params->regex) { |
| 1385 |
|
| 1386 |
if (!regexec(params->regex, ast_sorcery_object_get_id(cached->object), 0, NULL, 0)) { |
| 1387 |
ao2_link(params->container, cached->object); |
| 1388 |
} |
| 1389 |
return 0; |
| 1390 |
} else if (params->prefix) { |
| 1391 |
if (!strncmp(params->prefix, ast_sorcery_object_get_id(cached->object), params->prefix_len)) { |
| 1392 |
ao2_link(params->container, cached->object); |
| 1393 |
} |
| 1394 |
return 0; |
| 1395 |
} else if (params->fields && |
| 1396 |
(!ast_variable_lists_match(cached->objectset, params->fields, 0))) { |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
return 0; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if (params->container) { |
| 1404 |
ao2_link(params->container, cached->object); |
| 1405 |
|
| 1406 |
|
| 1407 |
return 0; |
| 1408 |
} else { |
| 1409 |
|
| 1410 |
return CMP_MATCH | CMP_STOP; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
static void *sorcery_memory_cache_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 1424 |
const struct ast_variable *fields) |
| 1425 |
{ |
| 1426 |
struct sorcery_memory_cache *cache = data; |
| 1427 |
struct sorcery_memory_cache_fields_cmp_params params = { |
| 1428 |
.sorcery = sorcery, |
| 1429 |
.cache = cache, |
| 1430 |
.fields = fields, |
| 1431 |
}; |
| 1432 |
struct sorcery_memory_cached_object *cached; |
| 1433 |
void *object = NULL; |
| 1434 |
|
| 1435 |
if (is_passthru_update() || !cache->full_backend_cache || !fields) { |
| 1436 |
return NULL; |
| 1437 |
} |
| 1438 |
|
| 1439 |
cached = ao2_callback(cache->objects, 0, sorcery_memory_cache_fields_cmp, ¶ms); |
| 1440 |
|
| 1441 |
if (cached) { |
| 1442 |
memory_cache_stale_check_object(sorcery, cache, cached); |
| 1443 |
object = ao2_bump(cached->object); |
| 1444 |
ao2_ref(cached, -1); |
| 1445 |
} |
| 1446 |
|
| 1447 |
return object; |
| 1448 |
} |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
static void sorcery_memory_cache_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 1461 |
struct ao2_container *objects, const struct ast_variable *fields) |
| 1462 |
{ |
| 1463 |
struct sorcery_memory_cache *cache = data; |
| 1464 |
struct sorcery_memory_cache_fields_cmp_params params = { |
| 1465 |
.sorcery = sorcery, |
| 1466 |
.cache = cache, |
| 1467 |
.fields = fields, |
| 1468 |
.container = objects, |
| 1469 |
}; |
| 1470 |
|
| 1471 |
if (is_passthru_update() || !cache->full_backend_cache) { |
| 1472 |
return; |
| 1473 |
} |
| 1474 |
|
| 1475 |
memory_cache_full_update(sorcery, type, cache); |
| 1476 |
ao2_callback(cache->objects, 0, sorcery_memory_cache_fields_cmp, ¶ms); |
| 1477 |
|
| 1478 |
if (ao2_container_count(objects)) { |
| 1479 |
memory_cache_stale_check(sorcery, cache); |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
static void sorcery_memory_cache_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 1494 |
struct ao2_container *objects, const char *regex) |
| 1495 |
{ |
| 1496 |
struct sorcery_memory_cache *cache = data; |
| 1497 |
regex_t expression; |
| 1498 |
struct sorcery_memory_cache_fields_cmp_params params = { |
| 1499 |
.sorcery = sorcery, |
| 1500 |
.cache = cache, |
| 1501 |
.container = objects, |
| 1502 |
.regex = &expression, |
| 1503 |
}; |
| 1504 |
|
| 1505 |
if (is_passthru_update() || !cache->full_backend_cache || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) { |
| 1506 |
return; |
| 1507 |
} |
| 1508 |
|
| 1509 |
memory_cache_full_update(sorcery, type, cache); |
| 1510 |
ao2_callback(cache->objects, 0, sorcery_memory_cache_fields_cmp, ¶ms); |
| 1511 |
regfree(&expression); |
| 1512 |
|
| 1513 |
if (ao2_container_count(objects)) { |
| 1514 |
memory_cache_stale_check(sorcery, cache); |
| 1515 |
} |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
static void sorcery_memory_cache_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 1529 |
struct ao2_container *objects, const char *prefix, const size_t prefix_len) |
| 1530 |
{ |
| 1531 |
struct sorcery_memory_cache *cache = data; |
| 1532 |
struct sorcery_memory_cache_fields_cmp_params params = { |
| 1533 |
.sorcery = sorcery, |
| 1534 |
.cache = cache, |
| 1535 |
.container = objects, |
| 1536 |
.prefix = prefix, |
| 1537 |
.prefix_len = prefix_len, |
| 1538 |
}; |
| 1539 |
|
| 1540 |
if (is_passthru_update() || !cache->full_backend_cache) { |
| 1541 |
return; |
| 1542 |
} |
| 1543 |
|
| 1544 |
memory_cache_full_update(sorcery, type, cache); |
| 1545 |
ao2_callback(cache->objects, 0, sorcery_memory_cache_fields_cmp, ¶ms); |
| 1546 |
|
| 1547 |
if (ao2_container_count(objects)) { |
| 1548 |
memory_cache_stale_check(sorcery, cache); |
| 1549 |
} |
| 1550 |
} |
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
static void sorcery_memory_cache_load(void *data, const struct ast_sorcery *sorcery, const char *type) |
| 1561 |
{ |
| 1562 |
struct sorcery_memory_cache *cache = data; |
| 1563 |
|
| 1564 |
|
| 1565 |
if (ast_strlen_zero(cache->name)) { |
| 1566 |
ast_asprintf(&cache->name, "%s/%s", ast_sorcery_get_module(sorcery), type); |
| 1567 |
} |
| 1568 |
|
| 1569 |
ao2_link(caches, cache); |
| 1570 |
ast_debug(1, "Memory cache '%s' associated with sorcery instance '%p' of module '%s' with object type '%s'\n", |
| 1571 |
cache->name, sorcery, ast_sorcery_get_module(sorcery), type); |
| 1572 |
|
| 1573 |
cache->sorcery = sorcery; |
| 1574 |
cache->object_type = ast_strdup(type); |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
static void sorcery_memory_cache_reload(void *data, const struct ast_sorcery *sorcery, const char *type) |
| 1586 |
{ |
| 1587 |
struct sorcery_memory_cache *cache = data; |
| 1588 |
|
| 1589 |
if (!cache->expire_on_reload) { |
| 1590 |
return; |
| 1591 |
} |
| 1592 |
|
| 1593 |
ao2_wrlock(cache->objects); |
| 1594 |
remove_all_from_cache(cache); |
| 1595 |
ao2_unlock(cache->objects); |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
static int configuration_parse_unsigned_integer(const char *value, unsigned int *result) |
| 1609 |
{ |
| 1610 |
if (ast_strlen_zero(value) || !strncmp(value, "-", 1)) { |
| 1611 |
return 0; |
| 1612 |
} |
| 1613 |
|
| 1614 |
return sscanf(value, "%30u", result); |
| 1615 |
} |
| 1616 |
|
| 1617 |
static int age_cmp(void *a, void *b) |
| 1618 |
{ |
| 1619 |
return ast_tvcmp(((struct sorcery_memory_cached_object *) b)->created, |
| 1620 |
((struct sorcery_memory_cached_object *) a)->created); |
| 1621 |
} |
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
static void *sorcery_memory_cache_open(const char *data) |
| 1633 |
{ |
| 1634 |
char *options = ast_strdupa(data), *option; |
| 1635 |
RAII_VAR(struct sorcery_memory_cache *, cache, NULL, ao2_cleanup); |
| 1636 |
|
| 1637 |
cache = ao2_alloc_options(sizeof(*cache), sorcery_memory_cache_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 1638 |
if (!cache) { |
| 1639 |
return NULL; |
| 1640 |
} |
| 1641 |
|
| 1642 |
cache->expire_id = -1; |
| 1643 |
cache->stale_update_sched_id = -1; |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
while (!ast_strlen_zero(options) && (option = strsep(&options, ","))) { |
| 1649 |
char *name = strsep(&option, "="), *value = option; |
| 1650 |
|
| 1651 |
if (!strcasecmp(name, "name")) { |
| 1652 |
if (ast_strlen_zero(value)) { |
| 1653 |
ast_log(LOG_ERROR, "A name must be specified for the memory cache\n"); |
| 1654 |
return NULL; |
| 1655 |
} |
| 1656 |
ast_free(cache->name); |
| 1657 |
cache->name = ast_strdup(value); |
| 1658 |
} else if (!strcasecmp(name, "maximum_objects")) { |
| 1659 |
if (configuration_parse_unsigned_integer(value, &cache->maximum_objects) != 1) { |
| 1660 |
ast_log(LOG_ERROR, "Unsupported maximum objects value of '%s' used for memory cache\n", |
| 1661 |
value); |
| 1662 |
return NULL; |
| 1663 |
} |
| 1664 |
} else if (!strcasecmp(name, "object_lifetime_maximum")) { |
| 1665 |
if (configuration_parse_unsigned_integer(value, &cache->object_lifetime_maximum) != 1) { |
| 1666 |
ast_log(LOG_ERROR, "Unsupported object maximum lifetime value of '%s' used for memory cache\n", |
| 1667 |
value); |
| 1668 |
return NULL; |
| 1669 |
} |
| 1670 |
} else if (!strcasecmp(name, "object_lifetime_stale")) { |
| 1671 |
if (configuration_parse_unsigned_integer(value, &cache->object_lifetime_stale) != 1) { |
| 1672 |
ast_log(LOG_ERROR, "Unsupported object stale lifetime value of '%s' used for memory cache\n", |
| 1673 |
value); |
| 1674 |
return NULL; |
| 1675 |
} |
| 1676 |
} else if (!strcasecmp(name, "expire_on_reload")) { |
| 1677 |
cache->expire_on_reload = ast_true(value); |
| 1678 |
} else if (!strcasecmp(name, "full_backend_cache")) { |
| 1679 |
cache->full_backend_cache = ast_true(value); |
| 1680 |
} else { |
| 1681 |
ast_log(LOG_ERROR, "Unsupported option '%s' used for memory cache\n", name); |
| 1682 |
return NULL; |
| 1683 |
} |
| 1684 |
} |
| 1685 |
|
| 1686 |
cache->objects = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, |
| 1687 |
cache->maximum_objects ? cache->maximum_objects : CACHE_CONTAINER_BUCKET_SIZE, |
| 1688 |
sorcery_memory_cached_object_hash, NULL, sorcery_memory_cached_object_cmp); |
| 1689 |
if (!cache->objects) { |
| 1690 |
ast_log(LOG_ERROR, "Could not create a container to hold cached objects for memory cache\n"); |
| 1691 |
return NULL; |
| 1692 |
} |
| 1693 |
|
| 1694 |
cache->object_heap = ast_heap_create(CACHE_HEAP_INIT_HEIGHT, age_cmp, |
| 1695 |
offsetof(struct sorcery_memory_cached_object, __heap_index)); |
| 1696 |
if (!cache->object_heap) { |
| 1697 |
ast_log(LOG_ERROR, "Could not create heap to hold cached objects\n"); |
| 1698 |
return NULL; |
| 1699 |
} |
| 1700 |
|
| 1701 |
if (ast_mutex_init(&cache->populate_lock)) { |
| 1702 |
ast_log(LOG_ERROR, "Could not create populate lock for cache\n"); |
| 1703 |
return NULL; |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
return ao2_bump(cache); |
| 1714 |
} |
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
static int sorcery_memory_cache_delete(const struct ast_sorcery *sorcery, void *data, void *object) |
| 1728 |
{ |
| 1729 |
struct sorcery_memory_cache *cache = data; |
| 1730 |
int res; |
| 1731 |
|
| 1732 |
ao2_wrlock(cache->objects); |
| 1733 |
res = remove_from_cache(cache, ast_sorcery_object_get_id(object), 1); |
| 1734 |
ao2_unlock(cache->objects); |
| 1735 |
|
| 1736 |
if (res) { |
| 1737 |
ast_debug(1, "Unable to delete object '%s' from sorcery cache\n", ast_sorcery_object_get_id(object)); |
| 1738 |
} |
| 1739 |
|
| 1740 |
return res; |
| 1741 |
} |
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
static void sorcery_memory_cache_close(void *data) |
| 1750 |
{ |
| 1751 |
struct sorcery_memory_cache *cache = data; |
| 1752 |
|
| 1753 |
|
| 1754 |
if (!ast_strlen_zero(cache->name)) { |
| 1755 |
ao2_unlink(caches, cache); |
| 1756 |
} |
| 1757 |
|
| 1758 |
if (cache->object_lifetime_maximum) { |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
ao2_wrlock(cache->objects); |
| 1764 |
remove_all_from_cache(cache); |
| 1765 |
ao2_unlock(cache->objects); |
| 1766 |
} |
| 1767 |
|
| 1768 |
if (cache->full_backend_cache) { |
| 1769 |
ao2_wrlock(cache->objects); |
| 1770 |
cache->sorcery = NULL; |
| 1771 |
ao2_unlock(cache->objects); |
| 1772 |
} |
| 1773 |
|
| 1774 |
ao2_ref(cache, -1); |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
static char *sorcery_memory_cache_complete_name(const char *word, int state) |
| 1782 |
{ |
| 1783 |
struct sorcery_memory_cache *cache; |
| 1784 |
struct ao2_iterator it_caches; |
| 1785 |
int wordlen = strlen(word); |
| 1786 |
int which = 0; |
| 1787 |
char *result = NULL; |
| 1788 |
|
| 1789 |
it_caches = ao2_iterator_init(caches, 0); |
| 1790 |
while ((cache = ao2_iterator_next(&it_caches))) { |
| 1791 |
if (!strncasecmp(word, cache->name, wordlen) |
| 1792 |
&& ++which > state) { |
| 1793 |
result = ast_strdup(cache->name); |
| 1794 |
} |
| 1795 |
ao2_ref(cache, -1); |
| 1796 |
if (result) { |
| 1797 |
break; |
| 1798 |
} |
| 1799 |
} |
| 1800 |
ao2_iterator_destroy(&it_caches); |
| 1801 |
return result; |
| 1802 |
} |
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
static char *sorcery_memory_cache_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1809 |
{ |
| 1810 |
struct sorcery_memory_cache *cache; |
| 1811 |
|
| 1812 |
switch (cmd) { |
| 1813 |
case CLI_INIT: |
| 1814 |
e->command = "sorcery memory cache show"; |
| 1815 |
e->usage = |
| 1816 |
"Usage: sorcery memory cache show <name>\n" |
| 1817 |
" Show sorcery memory cache configuration and statistics.\n"; |
| 1818 |
return NULL; |
| 1819 |
case CLI_GENERATE: |
| 1820 |
if (a->pos == 4) { |
| 1821 |
return sorcery_memory_cache_complete_name(a->word, a->n); |
| 1822 |
} else { |
| 1823 |
return NULL; |
| 1824 |
} |
| 1825 |
} |
| 1826 |
|
| 1827 |
if (a->argc != 5) { |
| 1828 |
return CLI_SHOWUSAGE; |
| 1829 |
} |
| 1830 |
|
| 1831 |
cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY); |
| 1832 |
if (!cache) { |
| 1833 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]); |
| 1834 |
return CLI_FAILURE; |
| 1835 |
} |
| 1836 |
|
| 1837 |
ast_cli(a->fd, "Sorcery memory cache: %s\n", cache->name); |
| 1838 |
ast_cli(a->fd, "Number of objects within cache: %d\n", ao2_container_count(cache->objects)); |
| 1839 |
if (cache->maximum_objects) { |
| 1840 |
ast_cli(a->fd, "Maximum allowed objects: %d\n", cache->maximum_objects); |
| 1841 |
} else { |
| 1842 |
ast_cli(a->fd, "There is no limit on the maximum number of objects in the cache\n"); |
| 1843 |
} |
| 1844 |
if (cache->object_lifetime_maximum) { |
| 1845 |
ast_cli(a->fd, "Number of seconds before object expires: %d\n", cache->object_lifetime_maximum); |
| 1846 |
} else { |
| 1847 |
ast_cli(a->fd, "Object expiration is not enabled - cached objects will not expire\n"); |
| 1848 |
} |
| 1849 |
if (cache->object_lifetime_stale) { |
| 1850 |
ast_cli(a->fd, "Number of seconds before object becomes stale: %d\n", cache->object_lifetime_stale); |
| 1851 |
} else { |
| 1852 |
ast_cli(a->fd, "Object staleness is not enabled - cached objects will not go stale\n"); |
| 1853 |
} |
| 1854 |
ast_cli(a->fd, "Expire all objects on reload: %s\n", AST_CLI_ONOFF(cache->expire_on_reload)); |
| 1855 |
|
| 1856 |
ao2_ref(cache, -1); |
| 1857 |
|
| 1858 |
return CLI_SUCCESS; |
| 1859 |
} |
| 1860 |
|
| 1861 |
|
| 1862 |
struct print_object_details { |
| 1863 |
|
| 1864 |
struct sorcery_memory_cache *cache; |
| 1865 |
|
| 1866 |
struct ast_cli_args *a; |
| 1867 |
}; |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
static int sorcery_memory_cache_print_object(void *obj, void *arg, int flags) |
| 1874 |
{ |
| 1875 |
#define FORMAT "%-25.25s %-15u %-15u \n" |
| 1876 |
struct sorcery_memory_cached_object *cached = obj; |
| 1877 |
struct print_object_details *details = arg; |
| 1878 |
int seconds_until_expire = 0, seconds_until_stale = 0; |
| 1879 |
|
| 1880 |
if (details->cache->object_lifetime_maximum) { |
| 1881 |
seconds_until_expire = ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(details->cache->object_lifetime_maximum, 1)), ast_tvnow()) / 1000; |
| 1882 |
} |
| 1883 |
if (details->cache->object_lifetime_stale) { |
| 1884 |
seconds_until_stale = ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(details->cache->object_lifetime_stale, 1)), ast_tvnow()) / 1000; |
| 1885 |
} |
| 1886 |
|
| 1887 |
ast_cli(details->a->fd, FORMAT, ast_sorcery_object_get_id(cached->object), MAX(seconds_until_stale, 0), MAX(seconds_until_expire, 0)); |
| 1888 |
|
| 1889 |
return CMP_MATCH; |
| 1890 |
#undef FORMAT |
| 1891 |
} |
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
static char *sorcery_memory_cache_dump(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1898 |
{ |
| 1899 |
#define FORMAT "%-25.25s %-15.15s %-15.15s \n" |
| 1900 |
struct sorcery_memory_cache *cache; |
| 1901 |
struct print_object_details details; |
| 1902 |
|
| 1903 |
switch (cmd) { |
| 1904 |
case CLI_INIT: |
| 1905 |
e->command = "sorcery memory cache dump"; |
| 1906 |
e->usage = |
| 1907 |
"Usage: sorcery memory cache dump <name>\n" |
| 1908 |
" Dump a list of the objects within the cache, listed by object identifier.\n"; |
| 1909 |
return NULL; |
| 1910 |
case CLI_GENERATE: |
| 1911 |
if (a->pos == 4) { |
| 1912 |
return sorcery_memory_cache_complete_name(a->word, a->n); |
| 1913 |
} else { |
| 1914 |
return NULL; |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
if (a->argc != 5) { |
| 1919 |
return CLI_SHOWUSAGE; |
| 1920 |
} |
| 1921 |
|
| 1922 |
cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY); |
| 1923 |
if (!cache) { |
| 1924 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]); |
| 1925 |
return CLI_FAILURE; |
| 1926 |
} |
| 1927 |
|
| 1928 |
details.cache = cache; |
| 1929 |
details.a = a; |
| 1930 |
|
| 1931 |
ast_cli(a->fd, "Dumping sorcery memory cache '%s':\n", cache->name); |
| 1932 |
if (!cache->object_lifetime_stale) { |
| 1933 |
ast_cli(a->fd, " * Staleness is not enabled - objects will not go stale\n"); |
| 1934 |
} |
| 1935 |
if (!cache->object_lifetime_maximum) { |
| 1936 |
ast_cli(a->fd, " * Object lifetime is not enabled - objects will not expire\n"); |
| 1937 |
} |
| 1938 |
ast_cli(a->fd, FORMAT, "Object Name", "Stale In", "Expires In"); |
| 1939 |
ast_cli(a->fd, FORMAT, "-------------------------", "---------------", "---------------"); |
| 1940 |
ao2_callback(cache->objects, OBJ_NODATA | OBJ_MULTIPLE, sorcery_memory_cache_print_object, &details); |
| 1941 |
ast_cli(a->fd, FORMAT, "-------------------------", "---------------", "---------------"); |
| 1942 |
ast_cli(a->fd, "Total number of objects cached: %d\n", ao2_container_count(cache->objects)); |
| 1943 |
|
| 1944 |
ao2_ref(cache, -1); |
| 1945 |
|
| 1946 |
return CLI_SUCCESS; |
| 1947 |
#undef FORMAT |
| 1948 |
} |
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
static char *sorcery_memory_cache_complete_object_name(const char *cache_name, const char *word, int state) |
| 1955 |
{ |
| 1956 |
struct sorcery_memory_cache *cache; |
| 1957 |
struct sorcery_memory_cached_object *cached; |
| 1958 |
struct ao2_iterator it_cached; |
| 1959 |
int wordlen = strlen(word); |
| 1960 |
int which = 0; |
| 1961 |
char *result = NULL; |
| 1962 |
|
| 1963 |
cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY); |
| 1964 |
if (!cache) { |
| 1965 |
return NULL; |
| 1966 |
} |
| 1967 |
|
| 1968 |
it_cached = ao2_iterator_init(cache->objects, 0); |
| 1969 |
while ((cached = ao2_iterator_next(&it_cached))) { |
| 1970 |
if (!strncasecmp(word, ast_sorcery_object_get_id(cached->object), wordlen) |
| 1971 |
&& ++which > state) { |
| 1972 |
result = ast_strdup(ast_sorcery_object_get_id(cached->object)); |
| 1973 |
} |
| 1974 |
ao2_ref(cached, -1); |
| 1975 |
if (result) { |
| 1976 |
break; |
| 1977 |
} |
| 1978 |
} |
| 1979 |
ao2_iterator_destroy(&it_cached); |
| 1980 |
|
| 1981 |
ao2_ref(cache, -1); |
| 1982 |
|
| 1983 |
return result; |
| 1984 |
} |
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
static char *sorcery_memory_cache_expire(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1991 |
{ |
| 1992 |
struct sorcery_memory_cache *cache; |
| 1993 |
|
| 1994 |
switch (cmd) { |
| 1995 |
case CLI_INIT: |
| 1996 |
e->command = "sorcery memory cache expire"; |
| 1997 |
e->usage = |
| 1998 |
"Usage: sorcery memory cache expire <cache name> [object name]\n" |
| 1999 |
" Expire a specific object or ALL objects within a sorcery memory cache.\n"; |
| 2000 |
return NULL; |
| 2001 |
case CLI_GENERATE: |
| 2002 |
if (a->pos == 4) { |
| 2003 |
return sorcery_memory_cache_complete_name(a->word, a->n); |
| 2004 |
} else if (a->pos == 5) { |
| 2005 |
return sorcery_memory_cache_complete_object_name(a->argv[4], a->word, a->n); |
| 2006 |
} else { |
| 2007 |
return NULL; |
| 2008 |
} |
| 2009 |
} |
| 2010 |
|
| 2011 |
if (a->argc < 5 || a->argc > 6) { |
| 2012 |
return CLI_SHOWUSAGE; |
| 2013 |
} |
| 2014 |
|
| 2015 |
cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY); |
| 2016 |
if (!cache) { |
| 2017 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]); |
| 2018 |
return CLI_FAILURE; |
| 2019 |
} |
| 2020 |
|
| 2021 |
ao2_wrlock(cache->objects); |
| 2022 |
if (a->argc == 5) { |
| 2023 |
remove_all_from_cache(cache); |
| 2024 |
ast_cli(a->fd, "All objects have been removed from cache '%s'\n", a->argv[4]); |
| 2025 |
} else { |
| 2026 |
if (cache->full_backend_cache) { |
| 2027 |
ast_cli(a->fd, "Due to full backend caching per-object expiration is not available on cache '%s'\n", a->argv[4]); |
| 2028 |
} else if (!remove_from_cache(cache, a->argv[5], 1)) { |
| 2029 |
ast_cli(a->fd, "Successfully expired object '%s' from cache '%s'\n", a->argv[5], a->argv[4]); |
| 2030 |
} else { |
| 2031 |
ast_cli(a->fd, "Object '%s' was not expired from cache '%s' as it was not found\n", a->argv[5], |
| 2032 |
a->argv[4]); |
| 2033 |
} |
| 2034 |
} |
| 2035 |
ao2_unlock(cache->objects); |
| 2036 |
|
| 2037 |
ao2_ref(cache, -1); |
| 2038 |
|
| 2039 |
return CLI_SUCCESS; |
| 2040 |
} |
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
static char *sorcery_memory_cache_stale(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2047 |
{ |
| 2048 |
struct sorcery_memory_cache *cache; |
| 2049 |
int reload = 0; |
| 2050 |
|
| 2051 |
switch (cmd) { |
| 2052 |
case CLI_INIT: |
| 2053 |
e->command = "sorcery memory cache stale"; |
| 2054 |
e->usage = |
| 2055 |
"Usage: sorcery memory cache stale <cache name> [object name [reload]]\n" |
| 2056 |
" Mark a specific object or ALL objects as stale in a sorcery memory cache.\n" |
| 2057 |
" If \"reload\" is specified, then the object is marked stale and immediately\n" |
| 2058 |
" retrieved from backend storage to repopulate the cache\n"; |
| 2059 |
return NULL; |
| 2060 |
case CLI_GENERATE: |
| 2061 |
if (a->pos == 4) { |
| 2062 |
return sorcery_memory_cache_complete_name(a->word, a->n); |
| 2063 |
} else if (a->pos == 5) { |
| 2064 |
return sorcery_memory_cache_complete_object_name(a->argv[4], a->word, a->n); |
| 2065 |
} else if (a->pos == 6) { |
| 2066 |
static const char * const completions[] = { "reload", NULL }; |
| 2067 |
return ast_cli_complete(a->word, completions, a->n); |
| 2068 |
} else { |
| 2069 |
return NULL; |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
if (a->argc < 5 || a->argc > 7) { |
| 2074 |
return CLI_SHOWUSAGE; |
| 2075 |
} |
| 2076 |
|
| 2077 |
if (a->argc == 7) { |
| 2078 |
if (!strcasecmp(a->argv[6], "reload")) { |
| 2079 |
reload = 1; |
| 2080 |
} else { |
| 2081 |
return CLI_SHOWUSAGE; |
| 2082 |
} |
| 2083 |
} |
| 2084 |
|
| 2085 |
cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY); |
| 2086 |
if (!cache) { |
| 2087 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]); |
| 2088 |
return CLI_FAILURE; |
| 2089 |
} |
| 2090 |
|
| 2091 |
if (!cache->object_lifetime_stale) { |
| 2092 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' does not have staleness enabled\n", a->argv[4]); |
| 2093 |
ao2_ref(cache, -1); |
| 2094 |
return CLI_FAILURE; |
| 2095 |
} |
| 2096 |
|
| 2097 |
ao2_rdlock(cache->objects); |
| 2098 |
if (a->argc == 5) { |
| 2099 |
mark_all_as_stale_in_cache(cache); |
| 2100 |
ast_cli(a->fd, "Marked all objects in sorcery memory cache '%s' as stale\n", a->argv[4]); |
| 2101 |
} else { |
| 2102 |
if (!mark_object_as_stale_in_cache(cache, a->argv[5])) { |
| 2103 |
ast_cli(a->fd, "Successfully marked object '%s' in memory cache '%s' as stale\n", |
| 2104 |
a->argv[5], a->argv[4]); |
| 2105 |
if (reload) { |
| 2106 |
struct sorcery_memory_cached_object *cached; |
| 2107 |
|
| 2108 |
cached = ao2_find(cache->objects, a->argv[5], OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 2109 |
if (cached) { |
| 2110 |
memory_cache_stale_update_object(cache->sorcery, cache, cached); |
| 2111 |
ao2_ref(cached, -1); |
| 2112 |
} |
| 2113 |
} |
| 2114 |
} else { |
| 2115 |
ast_cli(a->fd, "Object '%s' in sorcery memory cache '%s' could not be marked as stale as it was not found\n", |
| 2116 |
a->argv[5], a->argv[4]); |
| 2117 |
} |
| 2118 |
} |
| 2119 |
ao2_unlock(cache->objects); |
| 2120 |
|
| 2121 |
ao2_ref(cache, -1); |
| 2122 |
|
| 2123 |
return CLI_SUCCESS; |
| 2124 |
} |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
static char *sorcery_memory_cache_populate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2131 |
{ |
| 2132 |
struct sorcery_memory_cache *cache; |
| 2133 |
struct ast_sorcery *sorcery; |
| 2134 |
const char *object_type; |
| 2135 |
int num_cached; |
| 2136 |
|
| 2137 |
switch (cmd) { |
| 2138 |
case CLI_INIT: |
| 2139 |
e->command = "sorcery memory cache populate"; |
| 2140 |
e->usage = |
| 2141 |
"Usage: sorcery memory cache populate <cache name>\n" |
| 2142 |
" Expire all objects in the cache and populate it with ALL objects from backend.\n"; |
| 2143 |
return NULL; |
| 2144 |
case CLI_GENERATE: |
| 2145 |
if (a->pos == 4) { |
| 2146 |
return sorcery_memory_cache_complete_name(a->word, a->n); |
| 2147 |
} else { |
| 2148 |
return NULL; |
| 2149 |
} |
| 2150 |
} |
| 2151 |
|
| 2152 |
if (a->argc != 5) { |
| 2153 |
return CLI_SHOWUSAGE; |
| 2154 |
} |
| 2155 |
|
| 2156 |
cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY); |
| 2157 |
if (!cache) { |
| 2158 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]); |
| 2159 |
return CLI_FAILURE; |
| 2160 |
} |
| 2161 |
|
| 2162 |
if (!cache->full_backend_cache) { |
| 2163 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' does not have full backend caching enabled\n", a->argv[4]); |
| 2164 |
ao2_ref(cache, -1); |
| 2165 |
return CLI_FAILURE; |
| 2166 |
} |
| 2167 |
|
| 2168 |
|
| 2169 |
ast_mutex_lock(&cache->populate_lock); |
| 2170 |
|
| 2171 |
|
| 2172 |
ao2_rdlock(cache->objects); |
| 2173 |
if (!cache->sorcery) { |
| 2174 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' is no longer active\n", a->argv[4]); |
| 2175 |
ao2_unlock(cache->objects); |
| 2176 |
ast_mutex_unlock(&cache->populate_lock); |
| 2177 |
ao2_ref(cache, -1); |
| 2178 |
return CLI_FAILURE; |
| 2179 |
} |
| 2180 |
|
| 2181 |
|
| 2182 |
sorcery = ao2_bump((struct ast_sorcery *)cache->sorcery); |
| 2183 |
object_type = cache->object_type; |
| 2184 |
|
| 2185 |
|
| 2186 |
ao2_unlock(cache->objects); |
| 2187 |
num_cached = memory_cache_populate_external(sorcery, object_type, cache); |
| 2188 |
|
| 2189 |
ao2_ref(sorcery, -1); |
| 2190 |
ast_mutex_unlock(&cache->populate_lock); |
| 2191 |
ao2_ref(cache, -1); |
| 2192 |
|
| 2193 |
if (num_cached < 0) { |
| 2194 |
ast_cli(a->fd, "An error occurred while populating sorcery memory cache '%s'\n", a->argv[4]); |
| 2195 |
} else { |
| 2196 |
ast_cli(a->fd, "Specified sorcery memory cache '%s' has been populated with '%d' objects from the backend\n", |
| 2197 |
a->argv[4], num_cached); |
| 2198 |
} |
| 2199 |
|
| 2200 |
return CLI_SUCCESS; |
| 2201 |
} |
| 2202 |
|
| 2203 |
static struct ast_cli_entry cli_memory_cache[] = { |
| 2204 |
AST_CLI_DEFINE(sorcery_memory_cache_show, "Show sorcery memory cache information"), |
| 2205 |
AST_CLI_DEFINE(sorcery_memory_cache_dump, "Dump all objects within a sorcery memory cache"), |
| 2206 |
AST_CLI_DEFINE(sorcery_memory_cache_expire, "Expire a specific object or ALL objects within a sorcery memory cache"), |
| 2207 |
AST_CLI_DEFINE(sorcery_memory_cache_stale, "Mark a specific object or ALL objects as stale within a sorcery memory cache"), |
| 2208 |
AST_CLI_DEFINE(sorcery_memory_cache_populate, "Clear and populate the sorcery memory cache with objects from the backend"), |
| 2209 |
}; |
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
static int sorcery_memory_cache_ami_expire_object(struct mansession *s, const struct message *m) |
| 2216 |
{ |
| 2217 |
const char *cache_name = astman_get_header(m, "Cache"); |
| 2218 |
const char *object_name = astman_get_header(m, "Object"); |
| 2219 |
struct sorcery_memory_cache *cache; |
| 2220 |
int res; |
| 2221 |
|
| 2222 |
if (ast_strlen_zero(cache_name)) { |
| 2223 |
astman_send_error(s, m, "SorceryMemoryCacheExpireObject requires that a cache name be provided.\n"); |
| 2224 |
return 0; |
| 2225 |
} else if (ast_strlen_zero(object_name)) { |
| 2226 |
astman_send_error(s, m, "SorceryMemoryCacheExpireObject requires that an object name be provided\n"); |
| 2227 |
return 0; |
| 2228 |
} |
| 2229 |
|
| 2230 |
cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY); |
| 2231 |
if (!cache) { |
| 2232 |
astman_send_error(s, m, "The provided cache does not exist\n"); |
| 2233 |
return 0; |
| 2234 |
} |
| 2235 |
|
| 2236 |
ao2_wrlock(cache->objects); |
| 2237 |
if (cache->full_backend_cache) { |
| 2238 |
res = 1; |
| 2239 |
} else { |
| 2240 |
res = remove_from_cache(cache, object_name, 1); |
| 2241 |
} |
| 2242 |
ao2_unlock(cache->objects); |
| 2243 |
|
| 2244 |
ao2_ref(cache, -1); |
| 2245 |
|
| 2246 |
if (res == 1) { |
| 2247 |
astman_send_error(s, m, "Due to full backend caching per-object expiration is not available, consider using SorceryMemoryCachePopulate or SorceryMemoryCacheExpire instead\n"); |
| 2248 |
} else if (!res) { |
| 2249 |
astman_send_ack(s, m, "The provided object was expired from the cache\n"); |
| 2250 |
} else { |
| 2251 |
astman_send_error(s, m, "The provided object could not be expired from the cache\n"); |
| 2252 |
} |
| 2253 |
|
| 2254 |
return 0; |
| 2255 |
} |
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
static int sorcery_memory_cache_ami_expire(struct mansession *s, const struct message *m) |
| 2262 |
{ |
| 2263 |
const char *cache_name = astman_get_header(m, "Cache"); |
| 2264 |
struct sorcery_memory_cache *cache; |
| 2265 |
|
| 2266 |
if (ast_strlen_zero(cache_name)) { |
| 2267 |
astman_send_error(s, m, "SorceryMemoryCacheExpire requires that a cache name be provided.\n"); |
| 2268 |
return 0; |
| 2269 |
} |
| 2270 |
|
| 2271 |
cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY); |
| 2272 |
if (!cache) { |
| 2273 |
astman_send_error(s, m, "The provided cache does not exist\n"); |
| 2274 |
return 0; |
| 2275 |
} |
| 2276 |
|
| 2277 |
ao2_wrlock(cache->objects); |
| 2278 |
remove_all_from_cache(cache); |
| 2279 |
ao2_unlock(cache->objects); |
| 2280 |
|
| 2281 |
ao2_ref(cache, -1); |
| 2282 |
|
| 2283 |
astman_send_ack(s, m, "All objects were expired from the cache\n"); |
| 2284 |
|
| 2285 |
return 0; |
| 2286 |
} |
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
static int sorcery_memory_cache_ami_stale_object(struct mansession *s, const struct message *m) |
| 2293 |
{ |
| 2294 |
const char *cache_name = astman_get_header(m, "Cache"); |
| 2295 |
const char *object_name = astman_get_header(m, "Object"); |
| 2296 |
const char *reload = astman_get_header(m, "Reload"); |
| 2297 |
struct sorcery_memory_cache *cache; |
| 2298 |
int res; |
| 2299 |
|
| 2300 |
if (ast_strlen_zero(cache_name)) { |
| 2301 |
astman_send_error(s, m, "SorceryMemoryCacheStaleObject requires that a cache name be provided.\n"); |
| 2302 |
return 0; |
| 2303 |
} else if (ast_strlen_zero(object_name)) { |
| 2304 |
astman_send_error(s, m, "SorceryMemoryCacheStaleObject requires that an object name be provided\n"); |
| 2305 |
return 0; |
| 2306 |
} |
| 2307 |
|
| 2308 |
cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY); |
| 2309 |
if (!cache) { |
| 2310 |
astman_send_error(s, m, "The provided cache does not exist\n"); |
| 2311 |
return 0; |
| 2312 |
} |
| 2313 |
|
| 2314 |
ao2_rdlock(cache->objects); |
| 2315 |
|
| 2316 |
res = mark_object_as_stale_in_cache(cache, object_name); |
| 2317 |
|
| 2318 |
if (ast_true(reload)) { |
| 2319 |
struct sorcery_memory_cached_object *cached; |
| 2320 |
|
| 2321 |
cached = ao2_find(cache->objects, object_name, OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 2322 |
if (cached) { |
| 2323 |
memory_cache_stale_update_object(cache->sorcery, cache, cached); |
| 2324 |
ao2_ref(cached, -1); |
| 2325 |
} |
| 2326 |
} |
| 2327 |
|
| 2328 |
ao2_unlock(cache->objects); |
| 2329 |
|
| 2330 |
ao2_ref(cache, -1); |
| 2331 |
|
| 2332 |
if (!res) { |
| 2333 |
astman_send_ack(s, m, "The provided object was marked as stale in the cache\n"); |
| 2334 |
} else { |
| 2335 |
astman_send_error(s, m, "The provided object could not be marked as stale in the cache\n"); |
| 2336 |
} |
| 2337 |
|
| 2338 |
return 0; |
| 2339 |
} |
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
static int sorcery_memory_cache_ami_stale(struct mansession *s, const struct message *m) |
| 2346 |
{ |
| 2347 |
const char *cache_name = astman_get_header(m, "Cache"); |
| 2348 |
struct sorcery_memory_cache *cache; |
| 2349 |
|
| 2350 |
if (ast_strlen_zero(cache_name)) { |
| 2351 |
astman_send_error(s, m, "SorceryMemoryCacheStale requires that a cache name be provided.\n"); |
| 2352 |
return 0; |
| 2353 |
} |
| 2354 |
|
| 2355 |
cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY); |
| 2356 |
if (!cache) { |
| 2357 |
astman_send_error(s, m, "The provided cache does not exist\n"); |
| 2358 |
return 0; |
| 2359 |
} |
| 2360 |
|
| 2361 |
ao2_rdlock(cache->objects); |
| 2362 |
mark_all_as_stale_in_cache(cache); |
| 2363 |
ao2_unlock(cache->objects); |
| 2364 |
|
| 2365 |
ao2_ref(cache, -1); |
| 2366 |
|
| 2367 |
astman_send_ack(s, m, "All objects were marked as stale in the cache\n"); |
| 2368 |
|
| 2369 |
return 0; |
| 2370 |
} |
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
static int sorcery_memory_cache_ami_populate(struct mansession *s, const struct message *m) |
| 2377 |
{ |
| 2378 |
const char *cache_name = astman_get_header(m, "Cache"); |
| 2379 |
struct sorcery_memory_cache *cache; |
| 2380 |
struct ast_sorcery *sorcery; |
| 2381 |
const char *object_type; |
| 2382 |
int cache_population_result; |
| 2383 |
|
| 2384 |
if (ast_strlen_zero(cache_name)) { |
| 2385 |
astman_send_error(s, m, "SorceryMemoryCachePopulate requires that a cache name be provided.\n"); |
| 2386 |
return 0; |
| 2387 |
} |
| 2388 |
|
| 2389 |
cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY); |
| 2390 |
if (!cache) { |
| 2391 |
astman_send_error(s, m, "The provided cache does not exist\n"); |
| 2392 |
return 0; |
| 2393 |
} |
| 2394 |
|
| 2395 |
if (!cache->full_backend_cache) { |
| 2396 |
astman_send_error(s, m, "The provided cache does not have full backend caching enabled\n"); |
| 2397 |
ao2_ref(cache, -1); |
| 2398 |
return 0; |
| 2399 |
} |
| 2400 |
|
| 2401 |
|
| 2402 |
ast_mutex_lock(&cache->populate_lock); |
| 2403 |
|
| 2404 |
|
| 2405 |
ao2_rdlock(cache->objects); |
| 2406 |
if (!cache->sorcery) { |
| 2407 |
astman_send_error(s, m, "The provided cache is no longer active\n"); |
| 2408 |
ao2_unlock(cache->objects); |
| 2409 |
ast_mutex_unlock(&cache->populate_lock); |
| 2410 |
ao2_ref(cache, -1); |
| 2411 |
return 0; |
| 2412 |
} |
| 2413 |
|
| 2414 |
|
| 2415 |
sorcery = ao2_bump((struct ast_sorcery *)cache->sorcery); |
| 2416 |
object_type = cache->object_type; |
| 2417 |
|
| 2418 |
|
| 2419 |
ao2_unlock(cache->objects); |
| 2420 |
cache_population_result = memory_cache_populate_external(sorcery, object_type, cache); |
| 2421 |
|
| 2422 |
ao2_ref(sorcery, -1); |
| 2423 |
ast_mutex_unlock(&cache->populate_lock); |
| 2424 |
ao2_ref(cache, -1); |
| 2425 |
|
| 2426 |
if (cache_population_result < 0) { |
| 2427 |
astman_send_error(s, m, "An error occurred while populating the cache\n"); |
| 2428 |
} else { |
| 2429 |
astman_send_ack(s, m, "Cache has been expired and populated\n"); |
| 2430 |
} |
| 2431 |
|
| 2432 |
return 0; |
| 2433 |
} |
| 2434 |
|
| 2435 |
#ifdef TEST_FRAMEWORK |
| 2436 |
|
| 2437 |
|
| 2438 |
struct test_sorcery_object { |
| 2439 |
SORCERY_OBJECT(details); |
| 2440 |
}; |
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
static void *test_sorcery_object_alloc(const char *id) |
| 2452 |
{ |
| 2453 |
return ast_sorcery_generic_alloc(sizeof(struct test_sorcery_object), NULL); |
| 2454 |
} |
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
static struct ast_sorcery *alloc_and_initialize_sorcery(void) |
| 2464 |
{ |
| 2465 |
struct ast_sorcery *sorcery; |
| 2466 |
|
| 2467 |
if (!(sorcery = ast_sorcery_open())) { |
| 2468 |
return NULL; |
| 2469 |
} |
| 2470 |
|
| 2471 |
if ((ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) || |
| 2472 |
ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) { |
| 2473 |
ast_sorcery_unref(sorcery); |
| 2474 |
return NULL; |
| 2475 |
} |
| 2476 |
|
| 2477 |
return sorcery; |
| 2478 |
} |
| 2479 |
|
| 2480 |
AST_TEST_DEFINE(open_with_valid_options) |
| 2481 |
{ |
| 2482 |
int res = AST_TEST_PASS; |
| 2483 |
struct sorcery_memory_cache *cache; |
| 2484 |
|
| 2485 |
switch (cmd) { |
| 2486 |
case TEST_INIT: |
| 2487 |
info->name = "open_with_valid_options"; |
| 2488 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 2489 |
info->summary = "Attempt to create sorcery memory caches using valid options"; |
| 2490 |
info->description = "This test performs the following:\n" |
| 2491 |
"\t* Creates a memory cache with default configuration\n" |
| 2492 |
"\t* Creates a memory cache with a maximum object count of 10 and verifies it\n" |
| 2493 |
"\t* Creates a memory cache with a maximum object lifetime of 60 and verifies it\n" |
| 2494 |
"\t* Creates a memory cache with a stale object lifetime of 90 and verifies it"; |
| 2495 |
return AST_TEST_NOT_RUN; |
| 2496 |
case TEST_EXECUTE: |
| 2497 |
break; |
| 2498 |
} |
| 2499 |
|
| 2500 |
cache = sorcery_memory_cache_open(""); |
| 2501 |
if (!cache) { |
| 2502 |
ast_test_status_update(test, "Failed to create a sorcery memory cache using default configuration\n"); |
| 2503 |
res = AST_TEST_FAIL; |
| 2504 |
} else { |
| 2505 |
sorcery_memory_cache_close(cache); |
| 2506 |
} |
| 2507 |
|
| 2508 |
cache = sorcery_memory_cache_open("maximum_objects=10"); |
| 2509 |
if (!cache) { |
| 2510 |
ast_test_status_update(test, "Failed to create a sorcery memory cache with a maximum object count of 10\n"); |
| 2511 |
res = AST_TEST_FAIL; |
| 2512 |
} else { |
| 2513 |
if (cache->maximum_objects != 10) { |
| 2514 |
ast_test_status_update(test, "Created a sorcery memory cache with a maximum object count of 10 but it has '%u'\n", |
| 2515 |
cache->maximum_objects); |
| 2516 |
} |
| 2517 |
sorcery_memory_cache_close(cache); |
| 2518 |
} |
| 2519 |
|
| 2520 |
cache = sorcery_memory_cache_open("object_lifetime_maximum=60"); |
| 2521 |
if (!cache) { |
| 2522 |
ast_test_status_update(test, "Failed to create a sorcery memory cache with a maximum object lifetime of 60\n"); |
| 2523 |
res = AST_TEST_FAIL; |
| 2524 |
} else { |
| 2525 |
if (cache->object_lifetime_maximum != 60) { |
| 2526 |
ast_test_status_update(test, "Created a sorcery memory cache with a maximum object lifetime of 60 but it has '%u'\n", |
| 2527 |
cache->object_lifetime_maximum); |
| 2528 |
} |
| 2529 |
sorcery_memory_cache_close(cache); |
| 2530 |
} |
| 2531 |
|
| 2532 |
cache = sorcery_memory_cache_open("object_lifetime_stale=90"); |
| 2533 |
if (!cache) { |
| 2534 |
ast_test_status_update(test, "Failed to create a sorcery memory cache with a stale object lifetime of 90\n"); |
| 2535 |
res = AST_TEST_FAIL; |
| 2536 |
} else { |
| 2537 |
if (cache->object_lifetime_stale != 90) { |
| 2538 |
ast_test_status_update(test, "Created a sorcery memory cache with a stale object lifetime of 90 but it has '%u'\n", |
| 2539 |
cache->object_lifetime_stale); |
| 2540 |
} |
| 2541 |
sorcery_memory_cache_close(cache); |
| 2542 |
} |
| 2543 |
|
| 2544 |
|
| 2545 |
return res; |
| 2546 |
} |
| 2547 |
|
| 2548 |
AST_TEST_DEFINE(open_with_invalid_options) |
| 2549 |
{ |
| 2550 |
int res = AST_TEST_PASS; |
| 2551 |
struct sorcery_memory_cache *cache; |
| 2552 |
|
| 2553 |
switch (cmd) { |
| 2554 |
case TEST_INIT: |
| 2555 |
info->name = "open_with_invalid_options"; |
| 2556 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 2557 |
info->summary = "Attempt to create sorcery memory caches using invalid options"; |
| 2558 |
info->description = "This test attempts to perform the following:\n" |
| 2559 |
"\t* Create a memory cache with an empty name\n" |
| 2560 |
"\t* Create a memory cache with a maximum object count of -1\n" |
| 2561 |
"\t* Create a memory cache with a maximum object count of toast\n" |
| 2562 |
"\t* Create a memory cache with a maximum object lifetime of -1\n" |
| 2563 |
"\t* Create a memory cache with a maximum object lifetime of toast\n" |
| 2564 |
"\t* Create a memory cache with a stale object lifetime of -1\n" |
| 2565 |
"\t* Create a memory cache with a stale object lifetime of toast"; |
| 2566 |
return AST_TEST_NOT_RUN; |
| 2567 |
case TEST_EXECUTE: |
| 2568 |
break; |
| 2569 |
} |
| 2570 |
|
| 2571 |
cache = sorcery_memory_cache_open("name="); |
| 2572 |
if (cache) { |
| 2573 |
ast_test_status_update(test, "Created a sorcery memory cache with an empty name\n"); |
| 2574 |
sorcery_memory_cache_close(cache); |
| 2575 |
res = AST_TEST_FAIL; |
| 2576 |
} |
| 2577 |
|
| 2578 |
cache = sorcery_memory_cache_open("maximum_objects=-1"); |
| 2579 |
if (cache) { |
| 2580 |
ast_test_status_update(test, "Created a sorcery memory cache with a maximum object count of -1\n"); |
| 2581 |
sorcery_memory_cache_close(cache); |
| 2582 |
res = AST_TEST_FAIL; |
| 2583 |
} |
| 2584 |
|
| 2585 |
cache = sorcery_memory_cache_open("maximum_objects=toast"); |
| 2586 |
if (cache) { |
| 2587 |
ast_test_status_update(test, "Created a sorcery memory cache with a maximum object count of toast\n"); |
| 2588 |
sorcery_memory_cache_close(cache); |
| 2589 |
res = AST_TEST_FAIL; |
| 2590 |
} |
| 2591 |
|
| 2592 |
cache = sorcery_memory_cache_open("object_lifetime_maximum=-1"); |
| 2593 |
if (cache) { |
| 2594 |
ast_test_status_update(test, "Created a sorcery memory cache with an object lifetime maximum of -1\n"); |
| 2595 |
sorcery_memory_cache_close(cache); |
| 2596 |
res = AST_TEST_FAIL; |
| 2597 |
} |
| 2598 |
|
| 2599 |
cache = sorcery_memory_cache_open("object_lifetime_maximum=toast"); |
| 2600 |
if (cache) { |
| 2601 |
ast_test_status_update(test, "Created a sorcery memory cache with an object lifetime maximum of toast\n"); |
| 2602 |
sorcery_memory_cache_close(cache); |
| 2603 |
res = AST_TEST_FAIL; |
| 2604 |
} |
| 2605 |
|
| 2606 |
cache = sorcery_memory_cache_open("object_lifetime_stale=-1"); |
| 2607 |
if (cache) { |
| 2608 |
ast_test_status_update(test, "Created a sorcery memory cache with a stale object lifetime of -1\n"); |
| 2609 |
sorcery_memory_cache_close(cache); |
| 2610 |
res = AST_TEST_FAIL; |
| 2611 |
} |
| 2612 |
|
| 2613 |
cache = sorcery_memory_cache_open("object_lifetime_stale=toast"); |
| 2614 |
if (cache) { |
| 2615 |
ast_test_status_update(test, "Created a sorcery memory cache with a stale object lifetime of toast\n"); |
| 2616 |
sorcery_memory_cache_close(cache); |
| 2617 |
res = AST_TEST_FAIL; |
| 2618 |
} |
| 2619 |
|
| 2620 |
cache = sorcery_memory_cache_open("tacos"); |
| 2621 |
if (cache) { |
| 2622 |
ast_test_status_update(test, "Created a sorcery memory cache with an invalid configuration option 'tacos'\n"); |
| 2623 |
sorcery_memory_cache_close(cache); |
| 2624 |
res = AST_TEST_FAIL; |
| 2625 |
} |
| 2626 |
|
| 2627 |
return res; |
| 2628 |
} |
| 2629 |
|
| 2630 |
AST_TEST_DEFINE(create_and_retrieve) |
| 2631 |
{ |
| 2632 |
int res = AST_TEST_FAIL; |
| 2633 |
struct ast_sorcery *sorcery = NULL; |
| 2634 |
struct sorcery_memory_cache *cache = NULL; |
| 2635 |
RAII_VAR(void *, object, NULL, ao2_cleanup); |
| 2636 |
RAII_VAR(void *, cached_object, NULL, ao2_cleanup); |
| 2637 |
|
| 2638 |
switch (cmd) { |
| 2639 |
case TEST_INIT: |
| 2640 |
info->name = "create"; |
| 2641 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 2642 |
info->summary = "Attempt to create an object in the cache"; |
| 2643 |
info->description = "This test performs the following:\n" |
| 2644 |
"\t* Creates a memory cache with default options\n" |
| 2645 |
"\t* Creates a sorcery instance with a test object\n" |
| 2646 |
"\t* Creates a test object with an id of test\n" |
| 2647 |
"\t* Pushes the test object into the memory cache\n" |
| 2648 |
"\t* Confirms that the test object is in the cache"; |
| 2649 |
return AST_TEST_NOT_RUN; |
| 2650 |
case TEST_EXECUTE: |
| 2651 |
break; |
| 2652 |
} |
| 2653 |
|
| 2654 |
cache = sorcery_memory_cache_open(""); |
| 2655 |
if (!cache) { |
| 2656 |
ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n"); |
| 2657 |
goto cleanup; |
| 2658 |
} |
| 2659 |
|
| 2660 |
if (ao2_container_count(cache->objects)) { |
| 2661 |
ast_test_status_update(test, "Memory cache contains cached objects before we added one\n"); |
| 2662 |
goto cleanup; |
| 2663 |
} |
| 2664 |
|
| 2665 |
sorcery = alloc_and_initialize_sorcery(); |
| 2666 |
if (!sorcery) { |
| 2667 |
ast_test_status_update(test, "Failed to create a test sorcery instance\n"); |
| 2668 |
goto cleanup; |
| 2669 |
} |
| 2670 |
|
| 2671 |
object = ast_sorcery_alloc(sorcery, "test", "test"); |
| 2672 |
if (!object) { |
| 2673 |
ast_test_status_update(test, "Failed to allocate a test object\n"); |
| 2674 |
goto cleanup; |
| 2675 |
} |
| 2676 |
|
| 2677 |
sorcery_memory_cache_create(sorcery, cache, object); |
| 2678 |
|
| 2679 |
if (!ao2_container_count(cache->objects)) { |
| 2680 |
ast_test_status_update(test, "Added test object to memory cache but cache remains empty\n"); |
| 2681 |
goto cleanup; |
| 2682 |
} |
| 2683 |
|
| 2684 |
cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test"); |
| 2685 |
if (!cached_object) { |
| 2686 |
ast_test_status_update(test, "Object placed into memory cache could not be retrieved\n"); |
| 2687 |
goto cleanup; |
| 2688 |
} |
| 2689 |
|
| 2690 |
if (cached_object != object) { |
| 2691 |
ast_test_status_update(test, "Object retrieved from memory cached is not the one we cached\n"); |
| 2692 |
goto cleanup; |
| 2693 |
} |
| 2694 |
|
| 2695 |
res = AST_TEST_PASS; |
| 2696 |
|
| 2697 |
cleanup: |
| 2698 |
if (cache) { |
| 2699 |
sorcery_memory_cache_close(cache); |
| 2700 |
} |
| 2701 |
if (sorcery) { |
| 2702 |
ast_sorcery_unref(sorcery); |
| 2703 |
} |
| 2704 |
|
| 2705 |
return res; |
| 2706 |
} |
| 2707 |
|
| 2708 |
AST_TEST_DEFINE(update) |
| 2709 |
{ |
| 2710 |
int res = AST_TEST_FAIL; |
| 2711 |
struct ast_sorcery *sorcery = NULL; |
| 2712 |
struct sorcery_memory_cache *cache = NULL; |
| 2713 |
RAII_VAR(void *, original_object, NULL, ao2_cleanup); |
| 2714 |
RAII_VAR(void *, updated_object, NULL, ao2_cleanup); |
| 2715 |
RAII_VAR(void *, cached_object, NULL, ao2_cleanup); |
| 2716 |
|
| 2717 |
switch (cmd) { |
| 2718 |
case TEST_INIT: |
| 2719 |
info->name = "create"; |
| 2720 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 2721 |
info->summary = "Attempt to create and then update an object in the cache"; |
| 2722 |
info->description = "This test performs the following:\n" |
| 2723 |
"\t* Creates a memory cache with default options\n" |
| 2724 |
"\t* Creates a sorcery instance with a test object\n" |
| 2725 |
"\t* Creates a test object with an id of test\n" |
| 2726 |
"\t* Pushes the test object into the memory cache\n" |
| 2727 |
"\t* Confirms that the test object is in the cache\n" |
| 2728 |
"\t* Creates a new test object with the same id of test\n" |
| 2729 |
"\t* Pushes the new test object into the memory cache\n" |
| 2730 |
"\t* Confirms that the new test object has replaced the old one"; |
| 2731 |
return AST_TEST_NOT_RUN; |
| 2732 |
case TEST_EXECUTE: |
| 2733 |
break; |
| 2734 |
} |
| 2735 |
|
| 2736 |
cache = sorcery_memory_cache_open(""); |
| 2737 |
if (!cache) { |
| 2738 |
ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n"); |
| 2739 |
goto cleanup; |
| 2740 |
} |
| 2741 |
|
| 2742 |
if (ao2_container_count(cache->objects)) { |
| 2743 |
ast_test_status_update(test, "Memory cache contains cached objects before we added one\n"); |
| 2744 |
goto cleanup; |
| 2745 |
} |
| 2746 |
|
| 2747 |
sorcery = alloc_and_initialize_sorcery(); |
| 2748 |
if (!sorcery) { |
| 2749 |
ast_test_status_update(test, "Failed to create a test sorcery instance\n"); |
| 2750 |
goto cleanup; |
| 2751 |
} |
| 2752 |
|
| 2753 |
original_object = ast_sorcery_alloc(sorcery, "test", "test"); |
| 2754 |
if (!original_object) { |
| 2755 |
ast_test_status_update(test, "Failed to allocate a test object\n"); |
| 2756 |
goto cleanup; |
| 2757 |
} |
| 2758 |
|
| 2759 |
sorcery_memory_cache_create(sorcery, cache, original_object); |
| 2760 |
|
| 2761 |
updated_object = ast_sorcery_alloc(sorcery, "test", "test"); |
| 2762 |
if (!updated_object) { |
| 2763 |
ast_test_status_update(test, "Failed to allocate an updated test object\n"); |
| 2764 |
goto cleanup; |
| 2765 |
} |
| 2766 |
|
| 2767 |
sorcery_memory_cache_create(sorcery, cache, updated_object); |
| 2768 |
|
| 2769 |
if (ao2_container_count(cache->objects) != 1) { |
| 2770 |
ast_test_status_update(test, "Added updated test object to memory cache but cache now contains %d objects instead of 1\n", |
| 2771 |
ao2_container_count(cache->objects)); |
| 2772 |
goto cleanup; |
| 2773 |
} |
| 2774 |
|
| 2775 |
cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test"); |
| 2776 |
if (!cached_object) { |
| 2777 |
ast_test_status_update(test, "Updated object placed into memory cache could not be retrieved\n"); |
| 2778 |
goto cleanup; |
| 2779 |
} |
| 2780 |
|
| 2781 |
if (cached_object == original_object) { |
| 2782 |
ast_test_status_update(test, "Updated object placed into memory cache but old one is being retrieved\n"); |
| 2783 |
goto cleanup; |
| 2784 |
} else if (cached_object != updated_object) { |
| 2785 |
ast_test_status_update(test, "Updated object placed into memory cache but different one is being retrieved\n"); |
| 2786 |
goto cleanup; |
| 2787 |
} |
| 2788 |
|
| 2789 |
res = AST_TEST_PASS; |
| 2790 |
|
| 2791 |
cleanup: |
| 2792 |
if (cache) { |
| 2793 |
sorcery_memory_cache_close(cache); |
| 2794 |
} |
| 2795 |
if (sorcery) { |
| 2796 |
ast_sorcery_unref(sorcery); |
| 2797 |
} |
| 2798 |
|
| 2799 |
return res; |
| 2800 |
} |
| 2801 |
|
| 2802 |
AST_TEST_DEFINE(delete) |
| 2803 |
{ |
| 2804 |
int res = AST_TEST_FAIL; |
| 2805 |
struct ast_sorcery *sorcery = NULL; |
| 2806 |
struct sorcery_memory_cache *cache = NULL; |
| 2807 |
RAII_VAR(void *, object, NULL, ao2_cleanup); |
| 2808 |
RAII_VAR(void *, cached_object, NULL, ao2_cleanup); |
| 2809 |
|
| 2810 |
switch (cmd) { |
| 2811 |
case TEST_INIT: |
| 2812 |
info->name = "delete"; |
| 2813 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 2814 |
info->summary = "Attempt to create and then delete an object in the cache"; |
| 2815 |
info->description = "This test performs the following:\n" |
| 2816 |
"\t* Creates a memory cache with default options\n" |
| 2817 |
"\t* Creates a sorcery instance with a test object\n" |
| 2818 |
"\t* Creates a test object with an id of test\n" |
| 2819 |
"\t* Pushes the test object into the memory cache\n" |
| 2820 |
"\t* Confirms that the test object is in the cache\n" |
| 2821 |
"\t* Deletes the test object from the cache\n" |
| 2822 |
"\t* Confirms that the test object is no longer in the cache"; |
| 2823 |
return AST_TEST_NOT_RUN; |
| 2824 |
case TEST_EXECUTE: |
| 2825 |
break; |
| 2826 |
} |
| 2827 |
|
| 2828 |
cache = sorcery_memory_cache_open(""); |
| 2829 |
if (!cache) { |
| 2830 |
ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n"); |
| 2831 |
goto cleanup; |
| 2832 |
} |
| 2833 |
|
| 2834 |
if (ao2_container_count(cache->objects)) { |
| 2835 |
ast_test_status_update(test, "Memory cache contains cached objects before we added one\n"); |
| 2836 |
goto cleanup; |
| 2837 |
} |
| 2838 |
|
| 2839 |
sorcery = alloc_and_initialize_sorcery(); |
| 2840 |
if (!sorcery) { |
| 2841 |
ast_test_status_update(test, "Failed to create a test sorcery instance\n"); |
| 2842 |
goto cleanup; |
| 2843 |
} |
| 2844 |
|
| 2845 |
object = ast_sorcery_alloc(sorcery, "test", "test"); |
| 2846 |
if (!object) { |
| 2847 |
ast_test_status_update(test, "Failed to allocate a test object\n"); |
| 2848 |
goto cleanup; |
| 2849 |
} |
| 2850 |
|
| 2851 |
sorcery_memory_cache_create(sorcery, cache, object); |
| 2852 |
|
| 2853 |
if (!ao2_container_count(cache->objects)) { |
| 2854 |
ast_test_status_update(test, "Added test object to memory cache but cache contains no objects\n"); |
| 2855 |
goto cleanup; |
| 2856 |
} |
| 2857 |
|
| 2858 |
cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test"); |
| 2859 |
if (!cached_object) { |
| 2860 |
ast_test_status_update(test, "Test object placed into memory cache could not be retrieved\n"); |
| 2861 |
goto cleanup; |
| 2862 |
} |
| 2863 |
|
| 2864 |
ao2_ref(cached_object, -1); |
| 2865 |
cached_object = NULL; |
| 2866 |
|
| 2867 |
sorcery_memory_cache_delete(sorcery, cache, object); |
| 2868 |
|
| 2869 |
cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test"); |
| 2870 |
if (cached_object) { |
| 2871 |
ast_test_status_update(test, "Test object deleted from memory cache can still be retrieved\n"); |
| 2872 |
goto cleanup; |
| 2873 |
} |
| 2874 |
|
| 2875 |
res = AST_TEST_PASS; |
| 2876 |
|
| 2877 |
cleanup: |
| 2878 |
if (cache) { |
| 2879 |
sorcery_memory_cache_close(cache); |
| 2880 |
} |
| 2881 |
if (sorcery) { |
| 2882 |
ast_sorcery_unref(sorcery); |
| 2883 |
} |
| 2884 |
|
| 2885 |
return res; |
| 2886 |
} |
| 2887 |
|
| 2888 |
static int check_cache_content(struct ast_test *test, struct ast_sorcery *sorcery, struct sorcery_memory_cache *cache, |
| 2889 |
const char **in_cache, size_t num_in_cache, const char **not_in_cache, size_t num_not_in_cache) |
| 2890 |
{ |
| 2891 |
int i; |
| 2892 |
int res = 0; |
| 2893 |
RAII_VAR(void *, cached_object, NULL, ao2_cleanup); |
| 2894 |
|
| 2895 |
for (i = 0; i < num_in_cache; ++i) { |
| 2896 |
cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", in_cache[i]); |
| 2897 |
if (!cached_object) { |
| 2898 |
ast_test_status_update(test, "Failed to retrieve '%s' object from the cache\n", |
| 2899 |
in_cache[i]); |
| 2900 |
res = -1; |
| 2901 |
} |
| 2902 |
ao2_ref(cached_object, -1); |
| 2903 |
} |
| 2904 |
|
| 2905 |
for (i = 0; i < num_not_in_cache; ++i) { |
| 2906 |
cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", not_in_cache[i]); |
| 2907 |
if (cached_object) { |
| 2908 |
ast_test_status_update(test, "Retrieved '%s' object from the cache unexpectedly\n", |
| 2909 |
not_in_cache[i]); |
| 2910 |
ao2_ref(cached_object, -1); |
| 2911 |
res = -1; |
| 2912 |
} |
| 2913 |
} |
| 2914 |
|
| 2915 |
return res; |
| 2916 |
} |
| 2917 |
|
| 2918 |
AST_TEST_DEFINE(maximum_objects) |
| 2919 |
{ |
| 2920 |
int res = AST_TEST_FAIL; |
| 2921 |
struct ast_sorcery *sorcery = NULL; |
| 2922 |
struct sorcery_memory_cache *cache = NULL; |
| 2923 |
RAII_VAR(void *, alice, NULL, ao2_cleanup); |
| 2924 |
RAII_VAR(void *, bob, NULL, ao2_cleanup); |
| 2925 |
RAII_VAR(void *, charlie, NULL, ao2_cleanup); |
| 2926 |
RAII_VAR(void *, cached_object, NULL, ao2_cleanup); |
| 2927 |
const char *in_cache[2]; |
| 2928 |
const char *not_in_cache[2]; |
| 2929 |
|
| 2930 |
switch (cmd) { |
| 2931 |
case TEST_INIT: |
| 2932 |
info->name = "maximum_objects"; |
| 2933 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 2934 |
info->summary = "Ensure that the 'maximum_objects' option works as expected"; |
| 2935 |
info->description = "This test performs the following:\n" |
| 2936 |
"\t* Creates a memory cache with maximum_objects=2\n" |
| 2937 |
"\t* Creates a sorcery instance\n" |
| 2938 |
"\t* Creates a three test objects: alice, bob, charlie, and david\n" |
| 2939 |
"\t* Pushes alice and bob into the memory cache\n" |
| 2940 |
"\t* Confirms that alice and bob are in the memory cache\n" |
| 2941 |
"\t* Pushes charlie into the memory cache\n" |
| 2942 |
"\t* Confirms that bob and charlie are in the memory cache\n" |
| 2943 |
"\t* Deletes charlie from the memory cache\n" |
| 2944 |
"\t* Confirms that only bob is in the memory cache\n" |
| 2945 |
"\t* Pushes alice into the memory cache\n" |
| 2946 |
"\t* Confirms that bob and alice are in the memory cache"; |
| 2947 |
return AST_TEST_NOT_RUN; |
| 2948 |
case TEST_EXECUTE: |
| 2949 |
break; |
| 2950 |
} |
| 2951 |
|
| 2952 |
cache = sorcery_memory_cache_open("maximum_objects=2"); |
| 2953 |
if (!cache) { |
| 2954 |
ast_test_status_update(test, "Failed to create a sorcery memory cache with maximum_objects=2\n"); |
| 2955 |
goto cleanup; |
| 2956 |
} |
| 2957 |
|
| 2958 |
if (ao2_container_count(cache->objects)) { |
| 2959 |
ast_test_status_update(test, "Memory cache contains cached objects before we added one\n"); |
| 2960 |
goto cleanup; |
| 2961 |
} |
| 2962 |
|
| 2963 |
sorcery = alloc_and_initialize_sorcery(); |
| 2964 |
if (!sorcery) { |
| 2965 |
ast_test_status_update(test, "Failed to create a test sorcery instance\n"); |
| 2966 |
goto cleanup; |
| 2967 |
} |
| 2968 |
|
| 2969 |
alice = ast_sorcery_alloc(sorcery, "test", "alice"); |
| 2970 |
bob = ast_sorcery_alloc(sorcery, "test", "bob"); |
| 2971 |
charlie = ast_sorcery_alloc(sorcery, "test", "charlie"); |
| 2972 |
|
| 2973 |
if (!alice || !bob || !charlie) { |
| 2974 |
ast_test_status_update(test, "Failed to allocate sorcery object(s)\n"); |
| 2975 |
goto cleanup; |
| 2976 |
} |
| 2977 |
|
| 2978 |
sorcery_memory_cache_create(sorcery, cache, alice); |
| 2979 |
in_cache[0] = "alice"; |
| 2980 |
in_cache[1] = NULL; |
| 2981 |
not_in_cache[0] = "bob"; |
| 2982 |
not_in_cache[1] = "charlie"; |
| 2983 |
if (check_cache_content(test, sorcery, cache, in_cache, 1, not_in_cache, 2)) { |
| 2984 |
goto cleanup; |
| 2985 |
} |
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
usleep(1000); |
| 2991 |
|
| 2992 |
sorcery_memory_cache_create(sorcery, cache, bob); |
| 2993 |
in_cache[0] = "alice"; |
| 2994 |
in_cache[1] = "bob"; |
| 2995 |
not_in_cache[0] = "charlie"; |
| 2996 |
not_in_cache[1] = NULL; |
| 2997 |
if (check_cache_content(test, sorcery, cache, in_cache, 2, not_in_cache, 1)) { |
| 2998 |
goto cleanup; |
| 2999 |
} |
| 3000 |
|
| 3001 |
usleep(1000); |
| 3002 |
|
| 3003 |
sorcery_memory_cache_create(sorcery, cache, charlie); |
| 3004 |
in_cache[0] = "bob"; |
| 3005 |
in_cache[1] = "charlie"; |
| 3006 |
not_in_cache[0] = "alice"; |
| 3007 |
not_in_cache[1] = NULL; |
| 3008 |
if (check_cache_content(test, sorcery, cache, in_cache, 2, not_in_cache, 1)) { |
| 3009 |
goto cleanup; |
| 3010 |
} |
| 3011 |
usleep(1000); |
| 3012 |
|
| 3013 |
sorcery_memory_cache_delete(sorcery, cache, charlie); |
| 3014 |
in_cache[0] = "bob"; |
| 3015 |
in_cache[1] = NULL; |
| 3016 |
not_in_cache[0] = "alice"; |
| 3017 |
not_in_cache[1] = "charlie"; |
| 3018 |
if (check_cache_content(test, sorcery, cache, in_cache, 1, not_in_cache, 2)) { |
| 3019 |
goto cleanup; |
| 3020 |
} |
| 3021 |
usleep(1000); |
| 3022 |
|
| 3023 |
sorcery_memory_cache_create(sorcery, cache, alice); |
| 3024 |
in_cache[0] = "bob"; |
| 3025 |
in_cache[1] = "alice"; |
| 3026 |
not_in_cache[0] = "charlie"; |
| 3027 |
not_in_cache[1] = NULL; |
| 3028 |
if (check_cache_content(test, sorcery, cache, in_cache, 2, not_in_cache, 1)) { |
| 3029 |
goto cleanup; |
| 3030 |
} |
| 3031 |
|
| 3032 |
res = AST_TEST_PASS; |
| 3033 |
|
| 3034 |
cleanup: |
| 3035 |
if (cache) { |
| 3036 |
sorcery_memory_cache_close(cache); |
| 3037 |
} |
| 3038 |
if (sorcery) { |
| 3039 |
ast_sorcery_unref(sorcery); |
| 3040 |
} |
| 3041 |
|
| 3042 |
return res; |
| 3043 |
} |
| 3044 |
|
| 3045 |
AST_TEST_DEFINE(expiration) |
| 3046 |
{ |
| 3047 |
int res = AST_TEST_FAIL; |
| 3048 |
struct ast_sorcery *sorcery = NULL; |
| 3049 |
struct sorcery_memory_cache *cache = NULL; |
| 3050 |
int i; |
| 3051 |
|
| 3052 |
switch (cmd) { |
| 3053 |
case TEST_INIT: |
| 3054 |
info->name = "expiration"; |
| 3055 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 3056 |
info->summary = "Add objects to a cache configured with maximum lifetime, confirm they are removed"; |
| 3057 |
info->description = "This test performs the following:\n" |
| 3058 |
"\t* Creates a memory cache with a maximum object lifetime of 5 seconds\n" |
| 3059 |
"\t* Pushes 10 objects into the memory cache\n" |
| 3060 |
"\t* Waits (up to) 10 seconds for expiration to occur\n" |
| 3061 |
"\t* Confirms that the objects have been removed from the cache"; |
| 3062 |
return AST_TEST_NOT_RUN; |
| 3063 |
case TEST_EXECUTE: |
| 3064 |
break; |
| 3065 |
} |
| 3066 |
|
| 3067 |
cache = sorcery_memory_cache_open("object_lifetime_maximum=5"); |
| 3068 |
if (!cache) { |
| 3069 |
ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n"); |
| 3070 |
goto cleanup; |
| 3071 |
} |
| 3072 |
|
| 3073 |
sorcery = alloc_and_initialize_sorcery(); |
| 3074 |
if (!sorcery) { |
| 3075 |
ast_test_status_update(test, "Failed to create a test sorcery instance\n"); |
| 3076 |
goto cleanup; |
| 3077 |
} |
| 3078 |
|
| 3079 |
cache->cache_notify = 1; |
| 3080 |
ast_mutex_init(&cache->lock); |
| 3081 |
ast_cond_init(&cache->cond, NULL); |
| 3082 |
|
| 3083 |
for (i = 0; i < 5; ++i) { |
| 3084 |
char uuid[AST_UUID_STR_LEN]; |
| 3085 |
void *object; |
| 3086 |
|
| 3087 |
object = ast_sorcery_alloc(sorcery, "test", ast_uuid_generate_str(uuid, sizeof(uuid))); |
| 3088 |
if (!object) { |
| 3089 |
ast_test_status_update(test, "Failed to allocate test object for expiration\n"); |
| 3090 |
goto cleanup; |
| 3091 |
} |
| 3092 |
|
| 3093 |
sorcery_memory_cache_create(sorcery, cache, object); |
| 3094 |
|
| 3095 |
ao2_ref(object, -1); |
| 3096 |
} |
| 3097 |
|
| 3098 |
ast_mutex_lock(&cache->lock); |
| 3099 |
while (!cache->cache_completed) { |
| 3100 |
struct timeval start = ast_tvnow(); |
| 3101 |
struct timespec end = { |
| 3102 |
.tv_sec = start.tv_sec + 10, |
| 3103 |
.tv_nsec = start.tv_usec * 1000, |
| 3104 |
}; |
| 3105 |
|
| 3106 |
if (ast_cond_timedwait(&cache->cond, &cache->lock, &end) == ETIMEDOUT) { |
| 3107 |
break; |
| 3108 |
} |
| 3109 |
} |
| 3110 |
ast_mutex_unlock(&cache->lock); |
| 3111 |
|
| 3112 |
if (ao2_container_count(cache->objects)) { |
| 3113 |
ast_test_status_update(test, "Objects placed into the memory cache did not expire and get removed\n"); |
| 3114 |
goto cleanup; |
| 3115 |
} |
| 3116 |
|
| 3117 |
res = AST_TEST_PASS; |
| 3118 |
|
| 3119 |
cleanup: |
| 3120 |
if (cache) { |
| 3121 |
if (cache->cache_notify) { |
| 3122 |
ast_cond_destroy(&cache->cond); |
| 3123 |
ast_mutex_destroy(&cache->lock); |
| 3124 |
} |
| 3125 |
sorcery_memory_cache_close(cache); |
| 3126 |
} |
| 3127 |
if (sorcery) { |
| 3128 |
ast_sorcery_unref(sorcery); |
| 3129 |
} |
| 3130 |
|
| 3131 |
return res; |
| 3132 |
} |
| 3133 |
|
| 3134 |
|
| 3135 |
|
| 3136 |
|
| 3137 |
static struct backend_data { |
| 3138 |
|
| 3139 |
int salt; |
| 3140 |
|
| 3141 |
int pepper; |
| 3142 |
|
| 3143 |
int exists; |
| 3144 |
} *real_backend_data; |
| 3145 |
|
| 3146 |
|
| 3147 |
|
| 3148 |
|
| 3149 |
struct test_data { |
| 3150 |
SORCERY_OBJECT(details); |
| 3151 |
|
| 3152 |
int salt; |
| 3153 |
|
| 3154 |
int pepper; |
| 3155 |
}; |
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
|
| 3160 |
static void *test_data_alloc(const char *id) { |
| 3161 |
return ast_sorcery_generic_alloc(sizeof(struct test_data), NULL); |
| 3162 |
} |
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
|
| 3171 |
|
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
|
| 3177 |
|
| 3178 |
|
| 3179 |
|
| 3180 |
static void *mock_retrieve_id(const struct ast_sorcery *sorcery, void *data, |
| 3181 |
const char *type, const char *id) |
| 3182 |
{ |
| 3183 |
struct test_data *b_data; |
| 3184 |
|
| 3185 |
if (!real_backend_data->exists) { |
| 3186 |
return NULL; |
| 3187 |
} |
| 3188 |
|
| 3189 |
b_data = ast_sorcery_alloc(sorcery, type, id); |
| 3190 |
if (!b_data) { |
| 3191 |
return NULL; |
| 3192 |
} |
| 3193 |
|
| 3194 |
b_data->salt = real_backend_data->salt; |
| 3195 |
b_data->pepper = real_backend_data->pepper; |
| 3196 |
return b_data; |
| 3197 |
} |
| 3198 |
|
| 3199 |
|
| 3200 |
|
| 3201 |
|
| 3202 |
|
| 3203 |
|
| 3204 |
|
| 3205 |
|
| 3206 |
|
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
|
| 3211 |
|
| 3212 |
static void mock_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, |
| 3213 |
const char *type, struct ao2_container *objects, const struct ast_variable *fields) |
| 3214 |
{ |
| 3215 |
int i; |
| 3216 |
|
| 3217 |
if (fields) { |
| 3218 |
return; |
| 3219 |
} |
| 3220 |
|
| 3221 |
for (i = 0; i < real_backend_data->exists; ++i) { |
| 3222 |
char uuid[AST_UUID_STR_LEN]; |
| 3223 |
struct test_data *b_data; |
| 3224 |
|
| 3225 |
b_data = ast_sorcery_alloc(sorcery, type, ast_uuid_generate_str(uuid, sizeof(uuid))); |
| 3226 |
if (!b_data) { |
| 3227 |
continue; |
| 3228 |
} |
| 3229 |
|
| 3230 |
b_data->salt = real_backend_data->salt; |
| 3231 |
b_data->pepper = real_backend_data->pepper; |
| 3232 |
|
| 3233 |
ao2_link(objects, b_data); |
| 3234 |
ao2_ref(b_data, -1); |
| 3235 |
} |
| 3236 |
} |
| 3237 |
|
| 3238 |
|
| 3239 |
|
| 3240 |
|
| 3241 |
static struct ast_sorcery_wizard mock_wizard = { |
| 3242 |
.name = "mock", |
| 3243 |
.retrieve_id = mock_retrieve_id, |
| 3244 |
.retrieve_multiple = mock_retrieve_multiple, |
| 3245 |
}; |
| 3246 |
|
| 3247 |
|
| 3248 |
|
| 3249 |
|
| 3250 |
|
| 3251 |
|
| 3252 |
|
| 3253 |
|
| 3254 |
|
| 3255 |
|
| 3256 |
|
| 3257 |
|
| 3258 |
|
| 3259 |
|
| 3260 |
|
| 3261 |
|
| 3262 |
static int wait_for_cache_update(const struct ast_sorcery *sorcery, |
| 3263 |
void *previous_object, struct test_data **new_object) |
| 3264 |
{ |
| 3265 |
struct timeval start = ast_tvnow(); |
| 3266 |
|
| 3267 |
while (ast_remaining_ms(start, 5000) > 0) { |
| 3268 |
void *object; |
| 3269 |
|
| 3270 |
object = ast_sorcery_retrieve_by_id(sorcery, "test", "test"); |
| 3271 |
if (object != previous_object) { |
| 3272 |
*new_object = object; |
| 3273 |
return 0; |
| 3274 |
} |
| 3275 |
ao2_cleanup(object); |
| 3276 |
} |
| 3277 |
|
| 3278 |
return -1; |
| 3279 |
} |
| 3280 |
|
| 3281 |
AST_TEST_DEFINE(stale) |
| 3282 |
{ |
| 3283 |
int res = AST_TEST_FAIL; |
| 3284 |
struct ast_sorcery *sorcery = NULL; |
| 3285 |
struct test_data *backend_object; |
| 3286 |
struct backend_data iterations[] = { |
| 3287 |
{ .salt = 1, .pepper = 2, .exists = 1 }, |
| 3288 |
{ .salt = 568729, .pepper = -234123, .exists = 1 }, |
| 3289 |
{ .salt = 0, .pepper = 0, .exists = 0 }, |
| 3290 |
}; |
| 3291 |
struct backend_data initial = { |
| 3292 |
.salt = 0, |
| 3293 |
.pepper = 0, |
| 3294 |
.exists = 1, |
| 3295 |
}; |
| 3296 |
int i; |
| 3297 |
|
| 3298 |
switch (cmd) { |
| 3299 |
case TEST_INIT: |
| 3300 |
info->name = "stale"; |
| 3301 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 3302 |
info->summary = "Ensure that stale objects are replaced with updated objects"; |
| 3303 |
info->description = "This test performs the following:\n" |
| 3304 |
"\t* Create a sorcery instance with two wizards" |
| 3305 |
"\t\t* The first is a memory cache that marks items stale after 3 seconds\n" |
| 3306 |
"\t\t* The second is a mock of a back-end\n" |
| 3307 |
"\t* Pre-populates the cache by retrieving some initial data from the backend.\n" |
| 3308 |
"\t* Performs iterations of the following:\n" |
| 3309 |
"\t\t* Update backend data with new values\n" |
| 3310 |
"\t\t* Retrieve item from the cache\n" |
| 3311 |
"\t\t* Ensure the retrieved item does not have the new backend values\n" |
| 3312 |
"\t\t* Wait for cached object to become stale\n" |
| 3313 |
"\t\t* Retrieve the stale cached object\n" |
| 3314 |
"\t\t* Ensure that the stale object retrieved is the same as the fresh one from earlier\n" |
| 3315 |
"\t\t* Wait for the cache to update with new data\n" |
| 3316 |
"\t\t* Ensure that new data in the cache matches backend data"; |
| 3317 |
return AST_TEST_NOT_RUN; |
| 3318 |
case TEST_EXECUTE: |
| 3319 |
break; |
| 3320 |
} |
| 3321 |
|
| 3322 |
ast_sorcery_wizard_register(&mock_wizard); |
| 3323 |
|
| 3324 |
sorcery = ast_sorcery_open(); |
| 3325 |
if (!sorcery) { |
| 3326 |
ast_test_status_update(test, "Failed to create sorcery instance\n"); |
| 3327 |
goto cleanup; |
| 3328 |
} |
| 3329 |
|
| 3330 |
ast_sorcery_apply_wizard_mapping(sorcery, "test", "memory_cache", |
| 3331 |
"object_lifetime_stale=3", 1); |
| 3332 |
ast_sorcery_apply_wizard_mapping(sorcery, "test", "mock", NULL, 0); |
| 3333 |
ast_sorcery_internal_object_register(sorcery, "test", test_data_alloc, NULL, NULL); |
| 3334 |
|
| 3335 |
|
| 3336 |
real_backend_data = &initial; |
| 3337 |
|
| 3338 |
backend_object = ast_sorcery_retrieve_by_id(sorcery, "test", "test"); |
| 3339 |
if (!backend_object) { |
| 3340 |
ast_test_status_update(test, "Unable to retrieve backend data and populate the cache\n"); |
| 3341 |
goto cleanup; |
| 3342 |
} |
| 3343 |
ao2_ref(backend_object, -1); |
| 3344 |
|
| 3345 |
for (i = 0; i < ARRAY_LEN(iterations); ++i) { |
| 3346 |
RAII_VAR(struct test_data *, cache_fresh, NULL, ao2_cleanup); |
| 3347 |
RAII_VAR(struct test_data *, cache_stale, NULL, ao2_cleanup); |
| 3348 |
RAII_VAR(struct test_data *, cache_new, NULL, ao2_cleanup); |
| 3349 |
|
| 3350 |
real_backend_data = &iterations[i]; |
| 3351 |
|
| 3352 |
ast_test_status_update(test, "Begininning iteration %d\n", i); |
| 3353 |
|
| 3354 |
cache_fresh = ast_sorcery_retrieve_by_id(sorcery, "test", "test"); |
| 3355 |
if (!cache_fresh) { |
| 3356 |
ast_test_status_update(test, "Unable to retrieve fresh cached object\n"); |
| 3357 |
goto cleanup; |
| 3358 |
} |
| 3359 |
|
| 3360 |
if (cache_fresh->salt == iterations[i].salt || cache_fresh->pepper == iterations[i].pepper) { |
| 3361 |
ast_test_status_update(test, "Fresh cached object has unexpected values. Did we hit the backend?\n"); |
| 3362 |
goto cleanup; |
| 3363 |
} |
| 3364 |
|
| 3365 |
sleep(5); |
| 3366 |
|
| 3367 |
cache_stale = ast_sorcery_retrieve_by_id(sorcery, "test", "test"); |
| 3368 |
if (!cache_stale) { |
| 3369 |
ast_test_status_update(test, "Unable to retrieve stale cached object\n"); |
| 3370 |
goto cleanup; |
| 3371 |
} |
| 3372 |
|
| 3373 |
if (cache_stale != cache_fresh) { |
| 3374 |
ast_test_status_update(test, "Stale cache hit retrieved different object than fresh cache hit\n"); |
| 3375 |
goto cleanup; |
| 3376 |
} |
| 3377 |
|
| 3378 |
if (wait_for_cache_update(sorcery, cache_stale, &cache_new)) { |
| 3379 |
ast_test_status_update(test, "Cache was not updated\n"); |
| 3380 |
goto cleanup; |
| 3381 |
} |
| 3382 |
|
| 3383 |
if (iterations[i].exists) { |
| 3384 |
if (!cache_new) { |
| 3385 |
ast_test_status_update(test, "Failed to retrieve item from cache when there should be one present\n"); |
| 3386 |
goto cleanup; |
| 3387 |
} else if (cache_new->salt != iterations[i].salt || |
| 3388 |
cache_new->pepper != iterations[i].pepper) { |
| 3389 |
ast_test_status_update(test, "New cached item has unexpected values\n"); |
| 3390 |
goto cleanup; |
| 3391 |
} |
| 3392 |
} else if (cache_new) { |
| 3393 |
ast_test_status_update(test, "Retrieved a cached item when there should not have been one present\n"); |
| 3394 |
goto cleanup; |
| 3395 |
} |
| 3396 |
} |
| 3397 |
|
| 3398 |
res = AST_TEST_PASS; |
| 3399 |
|
| 3400 |
cleanup: |
| 3401 |
if (sorcery) { |
| 3402 |
ast_sorcery_unref(sorcery); |
| 3403 |
} |
| 3404 |
ast_sorcery_wizard_unregister(&mock_wizard); |
| 3405 |
return res; |
| 3406 |
} |
| 3407 |
|
| 3408 |
AST_TEST_DEFINE(full_backend_cache_expiration) |
| 3409 |
{ |
| 3410 |
int res = AST_TEST_FAIL; |
| 3411 |
struct ast_sorcery *sorcery = NULL; |
| 3412 |
struct backend_data initial = { |
| 3413 |
.salt = 0, |
| 3414 |
.pepper = 0, |
| 3415 |
.exists = 4, |
| 3416 |
}; |
| 3417 |
struct ao2_container *objects; |
| 3418 |
ast_mutex_t lock; |
| 3419 |
ast_cond_t cond; |
| 3420 |
struct timeval start; |
| 3421 |
struct timespec end; |
| 3422 |
|
| 3423 |
switch (cmd) { |
| 3424 |
case TEST_INIT: |
| 3425 |
info->name = "full_backend_cache_expiration"; |
| 3426 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 3427 |
info->summary = "Ensure that the full backend cache actually caches the backend"; |
| 3428 |
info->description = "This test performs the following:\n" |
| 3429 |
"\t* Create a sorcery instance with two wizards" |
| 3430 |
"\t\t* The first is a memory cache that expires objects after 3 seconds and does full backend caching\n" |
| 3431 |
"\t\t* The second is a mock of a back-end\n" |
| 3432 |
"\t* Populates the cache by requesting all objects which returns 4.\n" |
| 3433 |
"\t* Updates the backend to contain a different number of objects, 8.\n" |
| 3434 |
"\t* Requests all objects and confirms the number returned is only 4.\n" |
| 3435 |
"\t* Wait for cached objects to expire.\n" |
| 3436 |
"\t* Requests all objects and confirms the number returned is 8."; |
| 3437 |
return AST_TEST_NOT_RUN; |
| 3438 |
case TEST_EXECUTE: |
| 3439 |
break; |
| 3440 |
} |
| 3441 |
|
| 3442 |
ast_sorcery_wizard_register(&mock_wizard); |
| 3443 |
|
| 3444 |
sorcery = ast_sorcery_open(); |
| 3445 |
if (!sorcery) { |
| 3446 |
ast_test_status_update(test, "Failed to create sorcery instance\n"); |
| 3447 |
goto cleanup; |
| 3448 |
} |
| 3449 |
|
| 3450 |
ast_sorcery_apply_wizard_mapping(sorcery, "test", "memory_cache", |
| 3451 |
"object_lifetime_maximum=3,full_backend_cache=yes", 1); |
| 3452 |
ast_sorcery_apply_wizard_mapping(sorcery, "test", "mock", NULL, 0); |
| 3453 |
ast_sorcery_internal_object_register(sorcery, "test", test_data_alloc, NULL, NULL); |
| 3454 |
ast_sorcery_object_field_register_nodoc(sorcery, "test", "salt", "0", OPT_UINT_T, 0, FLDSET(struct test_data, salt)); |
| 3455 |
ast_sorcery_object_field_register_nodoc(sorcery, "test", "pepper", "0", OPT_UINT_T, 0, FLDSET(struct test_data, pepper)); |
| 3456 |
|
| 3457 |
|
| 3458 |
real_backend_data = &initial; |
| 3459 |
|
| 3460 |
|
| 3461 |
objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 3462 |
if (!objects) { |
| 3463 |
ast_test_status_update(test, "Unable to retrieve all objects in backend and populate cache\n"); |
| 3464 |
goto cleanup; |
| 3465 |
} |
| 3466 |
ao2_ref(objects, -1); |
| 3467 |
|
| 3468 |
|
| 3469 |
initial.exists = 8; |
| 3470 |
|
| 3471 |
|
| 3472 |
objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 3473 |
if (!objects) { |
| 3474 |
ast_test_status_update(test, "Unable to retrieve all objects in backend and populate cache\n"); |
| 3475 |
goto cleanup; |
| 3476 |
} |
| 3477 |
|
| 3478 |
if (ao2_container_count(objects) == initial.exists) { |
| 3479 |
ast_test_status_update(test, "Number of objects returned is of the current backend and not the cache\n"); |
| 3480 |
ao2_ref(objects, -1); |
| 3481 |
goto cleanup; |
| 3482 |
} |
| 3483 |
|
| 3484 |
ao2_ref(objects, -1); |
| 3485 |
|
| 3486 |
ast_mutex_init(&lock); |
| 3487 |
ast_cond_init(&cond, NULL); |
| 3488 |
|
| 3489 |
start = ast_tvnow(); |
| 3490 |
end.tv_sec = start.tv_sec + 5; |
| 3491 |
end.tv_nsec = start.tv_usec * 1000; |
| 3492 |
|
| 3493 |
ast_mutex_lock(&lock); |
| 3494 |
while (ast_cond_timedwait(&cond, &lock, &end) != ETIMEDOUT) { |
| 3495 |
} |
| 3496 |
ast_mutex_unlock(&lock); |
| 3497 |
|
| 3498 |
ast_mutex_destroy(&lock); |
| 3499 |
ast_cond_destroy(&cond); |
| 3500 |
|
| 3501 |
|
| 3502 |
objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 3503 |
if (!objects) { |
| 3504 |
ast_test_status_update(test, "Unable to retrieve all objects in backend and populate cache\n"); |
| 3505 |
goto cleanup; |
| 3506 |
} |
| 3507 |
|
| 3508 |
if (ao2_container_count(objects) != initial.exists) { |
| 3509 |
ast_test_status_update(test, "Number of objects returned is NOT of the current backend when it should be\n"); |
| 3510 |
ao2_ref(objects, -1); |
| 3511 |
goto cleanup; |
| 3512 |
} |
| 3513 |
|
| 3514 |
ao2_ref(objects, -1); |
| 3515 |
|
| 3516 |
res = AST_TEST_PASS; |
| 3517 |
|
| 3518 |
cleanup: |
| 3519 |
if (sorcery) { |
| 3520 |
ast_sorcery_unref(sorcery); |
| 3521 |
} |
| 3522 |
ast_sorcery_wizard_unregister(&mock_wizard); |
| 3523 |
return res; |
| 3524 |
} |
| 3525 |
|
| 3526 |
AST_TEST_DEFINE(full_backend_cache_stale) |
| 3527 |
{ |
| 3528 |
int res = AST_TEST_FAIL; |
| 3529 |
struct ast_sorcery *sorcery = NULL; |
| 3530 |
struct backend_data initial = { |
| 3531 |
.salt = 0, |
| 3532 |
.pepper = 0, |
| 3533 |
.exists = 4, |
| 3534 |
}; |
| 3535 |
struct ao2_container *objects; |
| 3536 |
ast_mutex_t lock; |
| 3537 |
ast_cond_t cond; |
| 3538 |
struct timeval start; |
| 3539 |
struct timespec end; |
| 3540 |
|
| 3541 |
switch (cmd) { |
| 3542 |
case TEST_INIT: |
| 3543 |
info->name = "full_backend_cache_stale"; |
| 3544 |
info->category = "/res/res_sorcery_memory_cache/"; |
| 3545 |
info->summary = "Ensure that the full backend cache works with staleness"; |
| 3546 |
info->description = "This test performs the following:\n" |
| 3547 |
"\t* Create a sorcery instance with two wizards" |
| 3548 |
"\t\t* The first is a memory cache that stales objects after 1 second and does full backend caching\n" |
| 3549 |
"\t\t* The second is a mock of a back-end\n" |
| 3550 |
"\t* Populates the cache by requesting all objects which returns 4.\n" |
| 3551 |
"\t* Wait for objects to go stale.\n" |
| 3552 |
"\t* Updates the backend to contain a different number of objects, 8.\"" |
| 3553 |
"\t* Requests all objects and confirms the number returned is only 4.\n" |
| 3554 |
"\t* Wait for objects to be refreshed from backend.\n" |
| 3555 |
"\t* Requests all objects and confirms the number returned is 8."; |
| 3556 |
return AST_TEST_NOT_RUN; |
| 3557 |
case TEST_EXECUTE: |
| 3558 |
break; |
| 3559 |
} |
| 3560 |
|
| 3561 |
ast_sorcery_wizard_register(&mock_wizard); |
| 3562 |
|
| 3563 |
ast_mutex_init(&lock); |
| 3564 |
ast_cond_init(&cond, NULL); |
| 3565 |
|
| 3566 |
sorcery = ast_sorcery_open(); |
| 3567 |
if (!sorcery) { |
| 3568 |
ast_test_status_update(test, "Failed to create sorcery instance\n"); |
| 3569 |
goto cleanup; |
| 3570 |
} |
| 3571 |
|
| 3572 |
ast_sorcery_apply_wizard_mapping(sorcery, "test", "memory_cache", |
| 3573 |
"object_lifetime_stale=1,full_backend_cache=yes", 1); |
| 3574 |
ast_sorcery_apply_wizard_mapping(sorcery, "test", "mock", NULL, 0); |
| 3575 |
ast_sorcery_internal_object_register(sorcery, "test", test_data_alloc, NULL, NULL); |
| 3576 |
ast_sorcery_object_field_register_nodoc(sorcery, "test", "salt", "0", OPT_UINT_T, 0, FLDSET(struct test_data, salt)); |
| 3577 |
ast_sorcery_object_field_register_nodoc(sorcery, "test", "pepper", "0", OPT_UINT_T, 0, FLDSET(struct test_data, pepper)); |
| 3578 |
|
| 3579 |
|
| 3580 |
real_backend_data = &initial; |
| 3581 |
|
| 3582 |
|
| 3583 |
objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 3584 |
if (!objects) { |
| 3585 |
ast_test_status_update(test, "Unable to retrieve all objects in backend and populate cache\n"); |
| 3586 |
goto cleanup; |
| 3587 |
} |
| 3588 |
ao2_ref(objects, -1); |
| 3589 |
|
| 3590 |
start = ast_tvnow(); |
| 3591 |
end.tv_sec = start.tv_sec + 5; |
| 3592 |
end.tv_nsec = start.tv_usec * 1000; |
| 3593 |
|
| 3594 |
ast_mutex_lock(&lock); |
| 3595 |
while (ast_cond_timedwait(&cond, &lock, &end) != ETIMEDOUT) { |
| 3596 |
} |
| 3597 |
ast_mutex_unlock(&lock); |
| 3598 |
|
| 3599 |
initial.exists = 8; |
| 3600 |
|
| 3601 |
|
| 3602 |
objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 3603 |
if (!objects) { |
| 3604 |
ast_test_status_update(test, "Unable to retrieve all objects in backend and populate cache\n"); |
| 3605 |
goto cleanup; |
| 3606 |
} |
| 3607 |
|
| 3608 |
if (ao2_container_count(objects) == initial.exists) { |
| 3609 |
ast_test_status_update(test, "Number of objects returned is of the backend and not the cache\n"); |
| 3610 |
ao2_ref(objects, -1); |
| 3611 |
goto cleanup; |
| 3612 |
} |
| 3613 |
|
| 3614 |
ao2_ref(objects, -1); |
| 3615 |
|
| 3616 |
start = ast_tvnow(); |
| 3617 |
end.tv_sec = start.tv_sec + 5; |
| 3618 |
end.tv_nsec = start.tv_usec * 1000; |
| 3619 |
|
| 3620 |
ast_mutex_lock(&lock); |
| 3621 |
while (ast_cond_timedwait(&cond, &lock, &end) != ETIMEDOUT) { |
| 3622 |
} |
| 3623 |
ast_mutex_unlock(&lock); |
| 3624 |
|
| 3625 |
|
| 3626 |
objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 3627 |
if (!objects) { |
| 3628 |
ast_test_status_update(test, "Unable to retrieve all objects in backend and populate cache\n"); |
| 3629 |
goto cleanup; |
| 3630 |
} |
| 3631 |
|
| 3632 |
if (ao2_container_count(objects) != initial.exists) { |
| 3633 |
ast_test_status_update(test, "Number of objects returned is not of backend\n"); |
| 3634 |
ao2_ref(objects, -1); |
| 3635 |
goto cleanup; |
| 3636 |
} |
| 3637 |
|
| 3638 |
ao2_ref(objects, -1); |
| 3639 |
|
| 3640 |
start = ast_tvnow(); |
| 3641 |
end.tv_sec = start.tv_sec + 5; |
| 3642 |
end.tv_nsec = start.tv_usec * 1000; |
| 3643 |
|
| 3644 |
ast_mutex_lock(&lock); |
| 3645 |
while (ast_cond_timedwait(&cond, &lock, &end) != ETIMEDOUT) { |
| 3646 |
} |
| 3647 |
ast_mutex_unlock(&lock); |
| 3648 |
|
| 3649 |
res = AST_TEST_PASS; |
| 3650 |
|
| 3651 |
cleanup: |
| 3652 |
if (sorcery) { |
| 3653 |
ast_sorcery_unref(sorcery); |
| 3654 |
} |
| 3655 |
ast_sorcery_wizard_unregister(&mock_wizard); |
| 3656 |
ast_mutex_destroy(&lock); |
| 3657 |
ast_cond_destroy(&cond); |
| 3658 |
return res; |
| 3659 |
} |
| 3660 |
|
| 3661 |
#endif |
| 3662 |
|
| 3663 |
static int unload_module(void) |
| 3664 |
{ |
| 3665 |
AST_TEST_UNREGISTER(open_with_valid_options); |
| 3666 |
AST_TEST_UNREGISTER(open_with_invalid_options); |
| 3667 |
AST_TEST_UNREGISTER(create_and_retrieve); |
| 3668 |
AST_TEST_UNREGISTER(update); |
| 3669 |
AST_TEST_UNREGISTER(delete); |
| 3670 |
AST_TEST_UNREGISTER(maximum_objects); |
| 3671 |
AST_TEST_UNREGISTER(expiration); |
| 3672 |
AST_TEST_UNREGISTER(stale); |
| 3673 |
AST_TEST_UNREGISTER(full_backend_cache_expiration); |
| 3674 |
AST_TEST_UNREGISTER(full_backend_cache_stale); |
| 3675 |
|
| 3676 |
ast_manager_unregister("SorceryMemoryCacheExpireObject"); |
| 3677 |
ast_manager_unregister("SorceryMemoryCacheExpire"); |
| 3678 |
ast_manager_unregister("SorceryMemoryCacheStaleObject"); |
| 3679 |
ast_manager_unregister("SorceryMemoryCacheStale"); |
| 3680 |
ast_manager_unregister("SorceryMemoryCachePopulate"); |
| 3681 |
|
| 3682 |
ast_cli_unregister_multiple(cli_memory_cache, ARRAY_LEN(cli_memory_cache)); |
| 3683 |
|
| 3684 |
ast_sorcery_wizard_unregister(&memory_cache_object_wizard); |
| 3685 |
|
| 3686 |
|
| 3687 |
|
| 3688 |
|
| 3689 |
|
| 3690 |
if (sched) { |
| 3691 |
ast_sched_context_destroy(sched); |
| 3692 |
sched = NULL; |
| 3693 |
} |
| 3694 |
|
| 3695 |
ao2_cleanup(caches); |
| 3696 |
caches = NULL; |
| 3697 |
|
| 3698 |
return 0; |
| 3699 |
} |
| 3700 |
|
| 3701 |
static int load_module(void) |
| 3702 |
{ |
| 3703 |
int res; |
| 3704 |
|
| 3705 |
caches = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 3706 |
CACHES_CONTAINER_BUCKET_SIZE, sorcery_memory_cache_hash, NULL, |
| 3707 |
sorcery_memory_cache_cmp); |
| 3708 |
if (!caches) { |
| 3709 |
ast_log(LOG_ERROR, "Failed to create container for configured caches\n"); |
| 3710 |
unload_module(); |
| 3711 |
return AST_MODULE_LOAD_DECLINE; |
| 3712 |
} |
| 3713 |
|
| 3714 |
sched = ast_sched_context_create(); |
| 3715 |
if (!sched) { |
| 3716 |
ast_log(LOG_ERROR, "Failed to create scheduler for cache management\n"); |
| 3717 |
unload_module(); |
| 3718 |
return AST_MODULE_LOAD_DECLINE; |
| 3719 |
} |
| 3720 |
|
| 3721 |
if (ast_sched_start_thread(sched)) { |
| 3722 |
ast_log(LOG_ERROR, "Failed to create scheduler thread for cache management\n"); |
| 3723 |
unload_module(); |
| 3724 |
return AST_MODULE_LOAD_DECLINE; |
| 3725 |
} |
| 3726 |
|
| 3727 |
if (ast_sorcery_wizard_register(&memory_cache_object_wizard)) { |
| 3728 |
unload_module(); |
| 3729 |
return AST_MODULE_LOAD_DECLINE; |
| 3730 |
} |
| 3731 |
|
| 3732 |
res = ast_cli_register_multiple(cli_memory_cache, ARRAY_LEN(cli_memory_cache)); |
| 3733 |
res |= ast_manager_register_xml("SorceryMemoryCacheExpireObject", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_expire_object); |
| 3734 |
res |= ast_manager_register_xml("SorceryMemoryCacheExpire", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_expire); |
| 3735 |
res |= ast_manager_register_xml("SorceryMemoryCacheStaleObject", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_stale_object); |
| 3736 |
res |= ast_manager_register_xml("SorceryMemoryCacheStale", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_stale); |
| 3737 |
res |= ast_manager_register_xml("SorceryMemoryCachePopulate", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_populate); |
| 3738 |
|
| 3739 |
if (res) { |
| 3740 |
unload_module(); |
| 3741 |
return AST_MODULE_LOAD_DECLINE; |
| 3742 |
} |
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
|
| 3747 |
AST_TEST_REGISTER(stale); |
| 3748 |
AST_TEST_REGISTER(open_with_valid_options); |
| 3749 |
AST_TEST_REGISTER(open_with_invalid_options); |
| 3750 |
AST_TEST_REGISTER(create_and_retrieve); |
| 3751 |
AST_TEST_REGISTER(update); |
| 3752 |
AST_TEST_REGISTER(delete); |
| 3753 |
AST_TEST_REGISTER(maximum_objects); |
| 3754 |
AST_TEST_REGISTER(expiration); |
| 3755 |
AST_TEST_REGISTER(full_backend_cache_expiration); |
| 3756 |
AST_TEST_REGISTER(full_backend_cache_stale); |
| 3757 |
|
| 3758 |
return AST_MODULE_LOAD_SUCCESS; |
| 3759 |
} |
| 3760 |
|
| 3761 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Memory Cache Object Wizard", |
| 3762 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 3763 |
.load = load_module, |
| 3764 |
.unload = unload_module, |
| 3765 |
.load_pri = AST_MODPRI_REALTIME_DRIVER, |
| 3766 |
); |