stasis_cache.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Stasis Message API.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/astobj2.h"
  30. #include "asterisk/hashtab.h"
  31. #include "asterisk/stasis_internal.h"
  32. #include "asterisk/stasis.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/vector.h"
  35. #ifdef LOW_MEMORY
  36. #define NUM_CACHE_BUCKETS 17
  37. #else
  38. #define NUM_CACHE_BUCKETS 563
  39. #endif
  40. /*! \internal */
  41. struct stasis_cache {
  42. struct ao2_container *entries;
  43. snapshot_get_id id_fn;
  44. cache_aggregate_calc_fn aggregate_calc_fn;
  45. cache_aggregate_publish_fn aggregate_publish_fn;
  46. int registered;
  47. };
  48. /*! \internal */
  49. struct stasis_caching_topic {
  50. struct stasis_cache *cache;
  51. struct stasis_topic *topic;
  52. struct stasis_topic *original_topic;
  53. struct stasis_subscription *sub;
  54. };
  55. static void stasis_caching_topic_dtor(void *obj)
  56. {
  57. struct stasis_caching_topic *caching_topic = obj;
  58. /* Caching topics contain subscriptions, and must be manually
  59. * unsubscribed. */
  60. ast_assert(!stasis_subscription_is_subscribed(caching_topic->sub));
  61. /* If there are any messages in flight to this subscription; that would
  62. * be bad. */
  63. ast_assert(stasis_subscription_is_done(caching_topic->sub));
  64. ao2_container_unregister(stasis_topic_name(caching_topic->topic));
  65. ao2_cleanup(caching_topic->sub);
  66. caching_topic->sub = NULL;
  67. ao2_cleanup(caching_topic->cache);
  68. caching_topic->cache = NULL;
  69. ao2_cleanup(caching_topic->topic);
  70. caching_topic->topic = NULL;
  71. ao2_cleanup(caching_topic->original_topic);
  72. caching_topic->original_topic = NULL;
  73. }
  74. struct stasis_topic *stasis_caching_get_topic(struct stasis_caching_topic *caching_topic)
  75. {
  76. return caching_topic->topic;
  77. }
  78. int stasis_caching_accept_message_type(struct stasis_caching_topic *caching_topic,
  79. struct stasis_message_type *type)
  80. {
  81. int res;
  82. if (!caching_topic) {
  83. return -1;
  84. }
  85. /* We wait to accept the stasis specific message types until now so that by default everything
  86. * will flow to us.
  87. */
  88. res = stasis_subscription_accept_message_type(caching_topic->sub, stasis_cache_clear_type());
  89. res |= stasis_subscription_accept_message_type(caching_topic->sub, stasis_subscription_change_type());
  90. res |= stasis_subscription_accept_message_type(caching_topic->sub, type);
  91. return res;
  92. }
  93. int stasis_caching_set_filter(struct stasis_caching_topic *caching_topic,
  94. enum stasis_subscription_message_filter filter)
  95. {
  96. if (!caching_topic) {
  97. return -1;
  98. }
  99. return stasis_subscription_set_filter(caching_topic->sub, filter);
  100. }
  101. struct stasis_caching_topic *stasis_caching_unsubscribe(struct stasis_caching_topic *caching_topic)
  102. {
  103. if (!caching_topic) {
  104. return NULL;
  105. }
  106. /*
  107. * The subscription may hold the last reference to this caching
  108. * topic, but we want to make sure the unsubscribe finishes
  109. * before kicking of the caching topic's dtor.
  110. */
  111. ao2_ref(caching_topic, +1);
  112. if (stasis_subscription_is_subscribed(caching_topic->sub)) {
  113. /*
  114. * Increment the reference to hold on to it past the
  115. * unsubscribe. Will be cleaned up in dtor.
  116. */
  117. ao2_ref(caching_topic->sub, +1);
  118. stasis_unsubscribe(caching_topic->sub);
  119. } else {
  120. ast_log(LOG_ERROR, "stasis_caching_topic unsubscribed multiple times\n");
  121. }
  122. ao2_cleanup(caching_topic);
  123. return NULL;
  124. }
  125. struct stasis_caching_topic *stasis_caching_unsubscribe_and_join(struct stasis_caching_topic *caching_topic)
  126. {
  127. if (!caching_topic) {
  128. return NULL;
  129. }
  130. /* Hold a ref past the unsubscribe */
  131. ao2_ref(caching_topic, +1);
  132. stasis_caching_unsubscribe(caching_topic);
  133. stasis_subscription_join(caching_topic->sub);
  134. ao2_cleanup(caching_topic);
  135. return NULL;
  136. }
  137. /*!
  138. * \brief The key for an entry in the cache
  139. * \note The items in this struct must be immutable for the item in the cache
  140. */
  141. struct cache_entry_key {
  142. /*! The message type of the item stored in the cache */
  143. struct stasis_message_type *type;
  144. /*! The unique ID of the item stored in the cache */
  145. const char *id;
  146. /*! The hash, computed from \c type and \c id */
  147. unsigned int hash;
  148. };
  149. struct stasis_cache_entry {
  150. struct cache_entry_key key;
  151. /*! Aggregate snapshot of the stasis cache. */
  152. struct stasis_message *aggregate;
  153. /*! Local entity snapshot of the stasis event. */
  154. struct stasis_message *local;
  155. /*! Remote entity snapshots of the stasis event. */
  156. AST_VECTOR(, struct stasis_message *) remote;
  157. };
  158. static void cache_entry_dtor(void *obj)
  159. {
  160. struct stasis_cache_entry *entry = obj;
  161. size_t idx;
  162. entry->key.type = NULL;
  163. ast_free((char *) entry->key.id);
  164. entry->key.id = NULL;
  165. ao2_cleanup(entry->aggregate);
  166. entry->aggregate = NULL;
  167. ao2_cleanup(entry->local);
  168. entry->local = NULL;
  169. for (idx = 0; idx < AST_VECTOR_SIZE(&entry->remote); ++idx) {
  170. struct stasis_message *remote;
  171. remote = AST_VECTOR_GET(&entry->remote, idx);
  172. ao2_cleanup(remote);
  173. }
  174. AST_VECTOR_FREE(&entry->remote);
  175. }
  176. static void cache_entry_compute_hash(struct cache_entry_key *key)
  177. {
  178. key->hash = stasis_message_type_hash(key->type);
  179. key->hash += ast_hashtab_hash_string(key->id);
  180. }
  181. static struct stasis_cache_entry *cache_entry_create(struct stasis_message_type *type, const char *id, struct stasis_message *snapshot)
  182. {
  183. struct stasis_cache_entry *entry;
  184. int is_remote;
  185. ast_assert(id != NULL);
  186. ast_assert(snapshot != NULL);
  187. if (!type) {
  188. return NULL;
  189. }
  190. entry = ao2_alloc_options(sizeof(*entry), cache_entry_dtor,
  191. AO2_ALLOC_OPT_LOCK_NOLOCK);
  192. if (!entry) {
  193. return NULL;
  194. }
  195. entry->key.id = ast_strdup(id);
  196. if (!entry->key.id) {
  197. ao2_cleanup(entry);
  198. return NULL;
  199. }
  200. /*
  201. * Normal ao2 ref counting rules says we should increment the message
  202. * type ref here and decrement it in cache_entry_dtor(). However, the
  203. * stasis message snapshot is cached here, will always have the same type
  204. * as the cache entry, and can legitimately cause the type ref count to
  205. * hit the excessive ref count assertion. Since the cache entry will
  206. * always have a snapshot we can get away with not holding a ref here.
  207. */
  208. ast_assert(type == stasis_message_type(snapshot));
  209. entry->key.type = type;
  210. cache_entry_compute_hash(&entry->key);
  211. is_remote = ast_eid_cmp(&ast_eid_default, stasis_message_eid(snapshot)) ? 1 : 0;
  212. if (AST_VECTOR_INIT(&entry->remote, is_remote)) {
  213. ao2_cleanup(entry);
  214. return NULL;
  215. }
  216. if (is_remote) {
  217. if (AST_VECTOR_APPEND(&entry->remote, snapshot)) {
  218. ao2_cleanup(entry);
  219. return NULL;
  220. }
  221. } else {
  222. entry->local = snapshot;
  223. }
  224. ao2_bump(snapshot);
  225. return entry;
  226. }
  227. static int cache_entry_hash(const void *obj, int flags)
  228. {
  229. const struct stasis_cache_entry *object;
  230. const struct cache_entry_key *key;
  231. switch (flags & OBJ_SEARCH_MASK) {
  232. case OBJ_SEARCH_KEY:
  233. key = obj;
  234. break;
  235. case OBJ_SEARCH_OBJECT:
  236. object = obj;
  237. key = &object->key;
  238. break;
  239. default:
  240. /* Hash can only work on something with a full key. */
  241. ast_assert(0);
  242. return 0;
  243. }
  244. return (int)key->hash;
  245. }
  246. static int cache_entry_cmp(void *obj, void *arg, int flags)
  247. {
  248. const struct stasis_cache_entry *object_left = obj;
  249. const struct stasis_cache_entry *object_right = arg;
  250. const struct cache_entry_key *right_key = arg;
  251. int cmp;
  252. switch (flags & OBJ_SEARCH_MASK) {
  253. case OBJ_SEARCH_OBJECT:
  254. right_key = &object_right->key;
  255. /* Fall through */
  256. case OBJ_SEARCH_KEY:
  257. cmp = object_left->key.type != right_key->type
  258. || strcmp(object_left->key.id, right_key->id);
  259. break;
  260. case OBJ_SEARCH_PARTIAL_KEY:
  261. /* Not supported by container */
  262. ast_assert(0);
  263. cmp = -1;
  264. break;
  265. default:
  266. /*
  267. * What arg points to is specific to this traversal callback
  268. * and has no special meaning to astobj2.
  269. */
  270. cmp = 0;
  271. break;
  272. }
  273. if (cmp) {
  274. return 0;
  275. }
  276. /*
  277. * At this point the traversal callback is identical to a sorted
  278. * container.
  279. */
  280. return CMP_MATCH;
  281. }
  282. static void cache_dtor(void *obj)
  283. {
  284. struct stasis_cache *cache = obj;
  285. ao2_cleanup(cache->entries);
  286. cache->entries = NULL;
  287. }
  288. struct stasis_cache *stasis_cache_create_full(snapshot_get_id id_fn,
  289. cache_aggregate_calc_fn aggregate_calc_fn,
  290. cache_aggregate_publish_fn aggregate_publish_fn)
  291. {
  292. struct stasis_cache *cache;
  293. cache = ao2_alloc_options(sizeof(*cache), cache_dtor,
  294. AO2_ALLOC_OPT_LOCK_NOLOCK);
  295. if (!cache) {
  296. return NULL;
  297. }
  298. cache->entries = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0,
  299. NUM_CACHE_BUCKETS, cache_entry_hash, NULL, cache_entry_cmp);
  300. if (!cache->entries) {
  301. ao2_cleanup(cache);
  302. return NULL;
  303. }
  304. cache->id_fn = id_fn;
  305. cache->aggregate_calc_fn = aggregate_calc_fn;
  306. cache->aggregate_publish_fn = aggregate_publish_fn;
  307. return cache;
  308. }
  309. struct stasis_cache *stasis_cache_create(snapshot_get_id id_fn)
  310. {
  311. return stasis_cache_create_full(id_fn, NULL, NULL);
  312. }
  313. struct stasis_message *stasis_cache_entry_get_aggregate(struct stasis_cache_entry *entry)
  314. {
  315. return entry->aggregate;
  316. }
  317. struct stasis_message *stasis_cache_entry_get_local(struct stasis_cache_entry *entry)
  318. {
  319. return entry->local;
  320. }
  321. struct stasis_message *stasis_cache_entry_get_remote(struct stasis_cache_entry *entry, int idx)
  322. {
  323. if (idx < AST_VECTOR_SIZE(&entry->remote)) {
  324. return AST_VECTOR_GET(&entry->remote, idx);
  325. }
  326. return NULL;
  327. }
  328. /*!
  329. * \internal
  330. * \brief Find the cache entry in the cache entries container.
  331. *
  332. * \param entries Container of cached entries.
  333. * \param type Type of message to retrieve the cache entry.
  334. * \param id Identity of the snapshot to retrieve the cache entry.
  335. *
  336. * \note The entries container is already locked.
  337. *
  338. * \retval Cache-entry on success.
  339. * \retval NULL Not in cache.
  340. */
  341. static struct stasis_cache_entry *cache_find(struct ao2_container *entries, struct stasis_message_type *type, const char *id)
  342. {
  343. struct cache_entry_key search_key;
  344. struct stasis_cache_entry *entry;
  345. search_key.type = type;
  346. search_key.id = id;
  347. cache_entry_compute_hash(&search_key);
  348. entry = ao2_find(entries, &search_key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  349. /* Ensure that what we looked for is what we found. */
  350. ast_assert(!entry
  351. || (!strcmp(stasis_message_type_name(entry->key.type),
  352. stasis_message_type_name(type)) && !strcmp(entry->key.id, id)));
  353. return entry;
  354. }
  355. /*!
  356. * \internal
  357. * \brief Remove the stasis snapshot in the cache entry determined by eid.
  358. *
  359. * \param entries Container of cached entries.
  360. * \param cached_entry The entry to remove the snapshot from.
  361. * \param eid Which snapshot in the cached entry.
  362. *
  363. * \note The entries container is already locked.
  364. *
  365. * \return Previous stasis entry snapshot.
  366. */
  367. static struct stasis_message *cache_remove(struct ao2_container *entries, struct stasis_cache_entry *cached_entry, const struct ast_eid *eid)
  368. {
  369. struct stasis_message *old_snapshot;
  370. int is_remote;
  371. is_remote = ast_eid_cmp(eid, &ast_eid_default);
  372. if (!is_remote) {
  373. old_snapshot = cached_entry->local;
  374. cached_entry->local = NULL;
  375. } else {
  376. int idx;
  377. old_snapshot = NULL;
  378. for (idx = 0; idx < AST_VECTOR_SIZE(&cached_entry->remote); ++idx) {
  379. struct stasis_message *cur;
  380. cur = AST_VECTOR_GET(&cached_entry->remote, idx);
  381. if (!ast_eid_cmp(eid, stasis_message_eid(cur))) {
  382. old_snapshot = AST_VECTOR_REMOVE_UNORDERED(&cached_entry->remote, idx);
  383. break;
  384. }
  385. }
  386. }
  387. if (!cached_entry->local && !AST_VECTOR_SIZE(&cached_entry->remote)) {
  388. ao2_unlink_flags(entries, cached_entry, OBJ_NOLOCK);
  389. }
  390. return old_snapshot;
  391. }
  392. /*!
  393. * \internal
  394. * \brief Update the stasis snapshot in the cache entry determined by eid.
  395. *
  396. * \param cached_entry The entry to remove the snapshot from.
  397. * \param eid Which snapshot in the cached entry.
  398. * \param new_snapshot Snapshot to replace the old snapshot.
  399. *
  400. * \return Previous stasis entry snapshot.
  401. */
  402. static struct stasis_message *cache_udpate(struct stasis_cache_entry *cached_entry, const struct ast_eid *eid, struct stasis_message *new_snapshot)
  403. {
  404. struct stasis_message *old_snapshot;
  405. int is_remote;
  406. int idx;
  407. is_remote = ast_eid_cmp(eid, &ast_eid_default);
  408. if (!is_remote) {
  409. old_snapshot = cached_entry->local;
  410. cached_entry->local = ao2_bump(new_snapshot);
  411. return old_snapshot;
  412. }
  413. old_snapshot = NULL;
  414. for (idx = 0; idx < AST_VECTOR_SIZE(&cached_entry->remote); ++idx) {
  415. struct stasis_message *cur;
  416. cur = AST_VECTOR_GET(&cached_entry->remote, idx);
  417. if (!ast_eid_cmp(eid, stasis_message_eid(cur))) {
  418. old_snapshot = AST_VECTOR_REMOVE_UNORDERED(&cached_entry->remote, idx);
  419. break;
  420. }
  421. }
  422. if (!AST_VECTOR_APPEND(&cached_entry->remote, new_snapshot)) {
  423. ao2_bump(new_snapshot);
  424. }
  425. return old_snapshot;
  426. }
  427. struct cache_put_snapshots {
  428. /*! Old cache eid snapshot. */
  429. struct stasis_message *old;
  430. /*! Old cache aggregate snapshot. */
  431. struct stasis_message *aggregate_old;
  432. /*! New cache aggregate snapshot. */
  433. struct stasis_message *aggregate_new;
  434. };
  435. static struct cache_put_snapshots cache_put(struct stasis_cache *cache,
  436. struct stasis_message_type *type, const char *id, const struct ast_eid *eid,
  437. struct stasis_message *new_snapshot)
  438. {
  439. struct stasis_cache_entry *cached_entry;
  440. struct cache_put_snapshots snapshots;
  441. ast_assert(cache->entries != NULL);
  442. ast_assert(eid != NULL);/* Aggregate snapshots not allowed to be put directly. */
  443. ast_assert(new_snapshot == NULL ||
  444. type == stasis_message_type(new_snapshot));
  445. memset(&snapshots, 0, sizeof(snapshots));
  446. ao2_wrlock(cache->entries);
  447. cached_entry = cache_find(cache->entries, type, id);
  448. /* Update the eid snapshot. */
  449. if (!new_snapshot) {
  450. /* Remove snapshot from cache */
  451. if (cached_entry) {
  452. snapshots.old = cache_remove(cache->entries, cached_entry, eid);
  453. }
  454. } else if (cached_entry) {
  455. /* Update snapshot in cache */
  456. snapshots.old = cache_udpate(cached_entry, eid, new_snapshot);
  457. } else {
  458. /* Insert into the cache */
  459. cached_entry = cache_entry_create(type, id, new_snapshot);
  460. if (cached_entry) {
  461. ao2_link_flags(cache->entries, cached_entry, OBJ_NOLOCK);
  462. }
  463. }
  464. /* Update the aggregate snapshot. */
  465. if (cache->aggregate_calc_fn && cached_entry) {
  466. snapshots.aggregate_new = cache->aggregate_calc_fn(cached_entry, new_snapshot);
  467. snapshots.aggregate_old = cached_entry->aggregate;
  468. cached_entry->aggregate = ao2_bump(snapshots.aggregate_new);
  469. }
  470. ao2_unlock(cache->entries);
  471. ao2_cleanup(cached_entry);
  472. return snapshots;
  473. }
  474. /*!
  475. * \internal
  476. * \brief Dump all entity snapshots in the cache entry into the given container.
  477. *
  478. * \param snapshots Container to put all snapshots in the cache entry.
  479. * \param entry Cache entry to use.
  480. *
  481. * \retval 0 on success.
  482. * \retval non-zero on error.
  483. */
  484. static int cache_entry_dump(struct ao2_container *snapshots, const struct stasis_cache_entry *entry)
  485. {
  486. int idx;
  487. int err = 0;
  488. ast_assert(snapshots != NULL);
  489. ast_assert(entry != NULL);
  490. /* The aggregate snapshot is not a snapshot from an entity. */
  491. if (entry->local) {
  492. err |= !ao2_link(snapshots, entry->local);
  493. }
  494. for (idx = 0; !err && idx < AST_VECTOR_SIZE(&entry->remote); ++idx) {
  495. struct stasis_message *snapshot;
  496. snapshot = AST_VECTOR_GET(&entry->remote, idx);
  497. err |= !ao2_link(snapshots, snapshot);
  498. }
  499. return err;
  500. }
  501. struct ao2_container *stasis_cache_get_all(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
  502. {
  503. struct stasis_cache_entry *cached_entry;
  504. struct ao2_container *found;
  505. ast_assert(cache != NULL);
  506. ast_assert(cache->entries != NULL);
  507. ast_assert(id != NULL);
  508. if (!type) {
  509. return NULL;
  510. }
  511. found = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  512. if (!found) {
  513. return NULL;
  514. }
  515. ao2_rdlock(cache->entries);
  516. cached_entry = cache_find(cache->entries, type, id);
  517. if (cached_entry && cache_entry_dump(found, cached_entry)) {
  518. ao2_cleanup(found);
  519. found = NULL;
  520. }
  521. ao2_unlock(cache->entries);
  522. ao2_cleanup(cached_entry);
  523. return found;
  524. }
  525. /*!
  526. * \internal
  527. * \brief Retrieve an item from the cache entry for a specific eid.
  528. *
  529. * \param entry Cache entry to use.
  530. * \param eid Specific entity id to retrieve. NULL for aggregate.
  531. *
  532. * \note The returned snapshot has not had its reference bumped.
  533. *
  534. * \retval Snapshot from the cache.
  535. * \retval \c NULL if snapshot is not found.
  536. */
  537. static struct stasis_message *cache_entry_by_eid(const struct stasis_cache_entry *entry, const struct ast_eid *eid)
  538. {
  539. int is_remote;
  540. int idx;
  541. if (!eid) {
  542. /* Get aggregate. */
  543. return entry->aggregate;
  544. }
  545. /* Get snapshot with specific eid. */
  546. is_remote = ast_eid_cmp(eid, &ast_eid_default);
  547. if (!is_remote) {
  548. return entry->local;
  549. }
  550. for (idx = 0; idx < AST_VECTOR_SIZE(&entry->remote); ++idx) {
  551. struct stasis_message *cur;
  552. cur = AST_VECTOR_GET(&entry->remote, idx);
  553. if (!ast_eid_cmp(eid, stasis_message_eid(cur))) {
  554. return cur;
  555. }
  556. }
  557. return NULL;
  558. }
  559. struct stasis_message *stasis_cache_get_by_eid(struct stasis_cache *cache, struct stasis_message_type *type, const char *id, const struct ast_eid *eid)
  560. {
  561. struct stasis_cache_entry *cached_entry;
  562. struct stasis_message *snapshot = NULL;
  563. ast_assert(cache != NULL);
  564. ast_assert(cache->entries != NULL);
  565. ast_assert(id != NULL);
  566. if (!type) {
  567. return NULL;
  568. }
  569. ao2_rdlock(cache->entries);
  570. cached_entry = cache_find(cache->entries, type, id);
  571. if (cached_entry) {
  572. snapshot = cache_entry_by_eid(cached_entry, eid);
  573. ao2_bump(snapshot);
  574. }
  575. ao2_unlock(cache->entries);
  576. ao2_cleanup(cached_entry);
  577. return snapshot;
  578. }
  579. struct stasis_message *stasis_cache_get(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
  580. {
  581. return stasis_cache_get_by_eid(cache, type, id, &ast_eid_default);
  582. }
  583. struct cache_dump_data {
  584. struct ao2_container *container;
  585. struct stasis_message_type *type;
  586. const struct ast_eid *eid;
  587. };
  588. static int cache_dump_by_eid_cb(void *obj, void *arg, int flags)
  589. {
  590. struct cache_dump_data *cache_dump = arg;
  591. struct stasis_cache_entry *entry = obj;
  592. if (!cache_dump->type || entry->key.type == cache_dump->type) {
  593. struct stasis_message *snapshot;
  594. snapshot = cache_entry_by_eid(entry, cache_dump->eid);
  595. if (snapshot) {
  596. if (!ao2_link(cache_dump->container, snapshot)) {
  597. ao2_cleanup(cache_dump->container);
  598. cache_dump->container = NULL;
  599. return CMP_STOP;
  600. }
  601. }
  602. }
  603. return 0;
  604. }
  605. struct ao2_container *stasis_cache_dump_by_eid(struct stasis_cache *cache, struct stasis_message_type *type, const struct ast_eid *eid)
  606. {
  607. struct cache_dump_data cache_dump;
  608. ast_assert(cache != NULL);
  609. ast_assert(cache->entries != NULL);
  610. cache_dump.eid = eid;
  611. cache_dump.type = type;
  612. cache_dump.container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  613. if (!cache_dump.container) {
  614. return NULL;
  615. }
  616. ao2_callback(cache->entries, OBJ_MULTIPLE | OBJ_NODATA, cache_dump_by_eid_cb, &cache_dump);
  617. return cache_dump.container;
  618. }
  619. struct ao2_container *stasis_cache_dump(struct stasis_cache *cache, struct stasis_message_type *type)
  620. {
  621. return stasis_cache_dump_by_eid(cache, type, &ast_eid_default);
  622. }
  623. static int cache_dump_all_cb(void *obj, void *arg, int flags)
  624. {
  625. struct cache_dump_data *cache_dump = arg;
  626. struct stasis_cache_entry *entry = obj;
  627. if (!cache_dump->type || entry->key.type == cache_dump->type) {
  628. if (cache_entry_dump(cache_dump->container, entry)) {
  629. ao2_cleanup(cache_dump->container);
  630. cache_dump->container = NULL;
  631. return CMP_STOP;
  632. }
  633. }
  634. return 0;
  635. }
  636. struct ao2_container *stasis_cache_dump_all(struct stasis_cache *cache, struct stasis_message_type *type)
  637. {
  638. struct cache_dump_data cache_dump;
  639. ast_assert(cache != NULL);
  640. ast_assert(cache->entries != NULL);
  641. cache_dump.eid = NULL;
  642. cache_dump.type = type;
  643. cache_dump.container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
  644. if (!cache_dump.container) {
  645. return NULL;
  646. }
  647. ao2_callback(cache->entries, OBJ_MULTIPLE | OBJ_NODATA, cache_dump_all_cb, &cache_dump);
  648. return cache_dump.container;
  649. }
  650. STASIS_MESSAGE_TYPE_DEFN(stasis_cache_clear_type);
  651. STASIS_MESSAGE_TYPE_DEFN(stasis_cache_update_type);
  652. struct stasis_message *stasis_cache_clear_create(struct stasis_message *id_message)
  653. {
  654. return stasis_message_create(stasis_cache_clear_type(), id_message);
  655. }
  656. static void stasis_cache_update_dtor(void *obj)
  657. {
  658. struct stasis_cache_update *update = obj;
  659. ao2_cleanup(update->old_snapshot);
  660. update->old_snapshot = NULL;
  661. ao2_cleanup(update->new_snapshot);
  662. update->new_snapshot = NULL;
  663. ao2_cleanup(update->type);
  664. update->type = NULL;
  665. }
  666. static struct stasis_message *update_create(struct stasis_message *old_snapshot, struct stasis_message *new_snapshot)
  667. {
  668. struct stasis_cache_update *update;
  669. struct stasis_message *msg;
  670. ast_assert(old_snapshot != NULL || new_snapshot != NULL);
  671. if (!stasis_cache_update_type()) {
  672. return NULL;
  673. }
  674. update = ao2_alloc_options(sizeof(*update), stasis_cache_update_dtor,
  675. AO2_ALLOC_OPT_LOCK_NOLOCK);
  676. if (!update) {
  677. return NULL;
  678. }
  679. if (old_snapshot) {
  680. ao2_ref(old_snapshot, +1);
  681. update->old_snapshot = old_snapshot;
  682. if (!new_snapshot) {
  683. ao2_ref(stasis_message_type(old_snapshot), +1);
  684. update->type = stasis_message_type(old_snapshot);
  685. }
  686. }
  687. if (new_snapshot) {
  688. ao2_ref(new_snapshot, +1);
  689. update->new_snapshot = new_snapshot;
  690. ao2_ref(stasis_message_type(new_snapshot), +1);
  691. update->type = stasis_message_type(new_snapshot);
  692. }
  693. msg = stasis_message_create(stasis_cache_update_type(), update);
  694. ao2_cleanup(update);
  695. return msg;
  696. }
  697. static void caching_topic_exec(void *data, struct stasis_subscription *sub,
  698. struct stasis_message *message)
  699. {
  700. struct stasis_caching_topic *caching_topic_needs_unref;
  701. struct stasis_caching_topic *caching_topic = data;
  702. struct stasis_message *msg;
  703. struct stasis_message *msg_put;
  704. struct stasis_message_type *msg_type;
  705. const struct ast_eid *msg_eid;
  706. const char *msg_id;
  707. ast_assert(caching_topic != NULL);
  708. ast_assert(caching_topic->topic != NULL);
  709. ast_assert(caching_topic->cache != NULL);
  710. ast_assert(caching_topic->cache->id_fn != NULL);
  711. if (stasis_subscription_final_message(sub, message)) {
  712. caching_topic_needs_unref = caching_topic;
  713. } else {
  714. caching_topic_needs_unref = NULL;
  715. }
  716. msg_type = stasis_message_type(message);
  717. if (stasis_subscription_change_type() == msg_type) {
  718. struct stasis_subscription_change *change = stasis_message_data(message);
  719. /*
  720. * If this change type is an unsubscribe, we need to find the original
  721. * subscribe and remove it from the cache otherwise the cache will
  722. * continue to grow unabated.
  723. */
  724. if (strcmp(change->description, "Unsubscribe") == 0) {
  725. struct stasis_cache_entry *sub;
  726. ao2_wrlock(caching_topic->cache->entries);
  727. sub = cache_find(caching_topic->cache->entries, stasis_subscription_change_type(), change->uniqueid);
  728. if (sub) {
  729. cache_remove(caching_topic->cache->entries, sub, stasis_message_eid(message));
  730. ao2_cleanup(sub);
  731. }
  732. ao2_unlock(caching_topic->cache->entries);
  733. ao2_cleanup(caching_topic_needs_unref);
  734. return;
  735. }
  736. msg_put = message;
  737. msg = message;
  738. } else if (stasis_cache_clear_type() == msg_type) {
  739. /* Cache clear event. */
  740. msg_put = NULL;
  741. msg = stasis_message_data(message);
  742. msg_type = stasis_message_type(msg);
  743. } else {
  744. /* Normal cache update event. */
  745. msg_put = message;
  746. msg = message;
  747. }
  748. ast_assert(msg_type != NULL);
  749. msg_eid = stasis_message_eid(msg);/* msg_eid is NULL for aggregate message. */
  750. msg_id = caching_topic->cache->id_fn(msg);
  751. if (msg_id && msg_eid) {
  752. struct stasis_message *update;
  753. struct cache_put_snapshots snapshots;
  754. /* Update the cache */
  755. snapshots = cache_put(caching_topic->cache, msg_type, msg_id, msg_eid, msg_put);
  756. if (snapshots.old || msg_put) {
  757. if (stasis_topic_subscribers(caching_topic->topic)) {
  758. update = update_create(snapshots.old, msg_put);
  759. if (update) {
  760. stasis_publish(caching_topic->topic, update);
  761. ao2_ref(update, -1);
  762. }
  763. }
  764. } else {
  765. ast_debug(1,
  766. "Attempting to remove an item from the %s cache that isn't there: %s %s\n",
  767. stasis_topic_name(caching_topic->topic),
  768. stasis_message_type_name(msg_type), msg_id);
  769. }
  770. if (snapshots.aggregate_old != snapshots.aggregate_new) {
  771. if (snapshots.aggregate_new && caching_topic->cache->aggregate_publish_fn) {
  772. caching_topic->cache->aggregate_publish_fn(caching_topic->original_topic,
  773. snapshots.aggregate_new);
  774. }
  775. if (stasis_topic_subscribers(caching_topic->topic)) {
  776. update = update_create(snapshots.aggregate_old, snapshots.aggregate_new);
  777. if (update) {
  778. stasis_publish(caching_topic->topic, update);
  779. ao2_ref(update, -1);
  780. }
  781. }
  782. }
  783. ao2_cleanup(snapshots.old);
  784. ao2_cleanup(snapshots.aggregate_old);
  785. ao2_cleanup(snapshots.aggregate_new);
  786. }
  787. ao2_cleanup(caching_topic_needs_unref);
  788. }
  789. static void print_cache_entry(void *v_obj, void *where, ao2_prnt_fn *prnt)
  790. {
  791. struct stasis_cache_entry *entry = v_obj;
  792. if (!entry) {
  793. return;
  794. }
  795. prnt(where, "Type: %s ID: %s Hash: %u", stasis_message_type_name(entry->key.type),
  796. entry->key.id, entry->key.hash);
  797. }
  798. struct stasis_caching_topic *stasis_caching_topic_create(struct stasis_topic *original_topic, struct stasis_cache *cache)
  799. {
  800. struct stasis_caching_topic *caching_topic;
  801. char *new_name;
  802. int ret;
  803. ret = ast_asprintf(&new_name, "%s-cached", stasis_topic_name(original_topic));
  804. if (ret < 0) {
  805. return NULL;
  806. }
  807. caching_topic = ao2_alloc_options(sizeof(*caching_topic),
  808. stasis_caching_topic_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  809. if (caching_topic == NULL) {
  810. ast_free(new_name);
  811. return NULL;
  812. }
  813. caching_topic->topic = stasis_topic_create(new_name);
  814. if (caching_topic->topic == NULL) {
  815. ao2_ref(caching_topic, -1);
  816. ast_free(new_name);
  817. return NULL;
  818. }
  819. ao2_ref(cache, +1);
  820. caching_topic->cache = cache;
  821. if (!cache->registered) {
  822. if (ao2_container_register(new_name, cache->entries, print_cache_entry)) {
  823. ast_log(LOG_ERROR, "Stasis cache container '%p' for '%s' did not register\n",
  824. cache->entries, new_name);
  825. } else {
  826. cache->registered = 1;
  827. }
  828. }
  829. ast_free(new_name);
  830. caching_topic->sub = internal_stasis_subscribe(original_topic, caching_topic_exec, caching_topic, 0, 0, __FILE__, __LINE__, __PRETTY_FUNCTION__);
  831. if (caching_topic->sub == NULL) {
  832. ao2_ref(caching_topic, -1);
  833. return NULL;
  834. }
  835. ao2_ref(original_topic, +1);
  836. caching_topic->original_topic = original_topic;
  837. /* The subscription holds the reference, so no additional ref bump. */
  838. return caching_topic;
  839. }
  840. static void stasis_cache_cleanup(void)
  841. {
  842. STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_clear_type);
  843. STASIS_MESSAGE_TYPE_CLEANUP(stasis_cache_update_type);
  844. }
  845. int stasis_cache_init(void)
  846. {
  847. ast_register_cleanup(stasis_cache_cleanup);
  848. if (STASIS_MESSAGE_TYPE_INIT(stasis_cache_clear_type) != 0) {
  849. return -1;
  850. }
  851. if (STASIS_MESSAGE_TYPE_INIT(stasis_cache_update_type) != 0) {
  852. return -1;
  853. }
  854. return 0;
  855. }