Debian Codegraph @

crt/aws-c-mqtt/source/request-response/subscription_manager.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/mqtt/private/request-response/subscription_manager.h>
7
8 #include <aws/common/logging.h>
9 #include <aws/mqtt/private/client_impl_shared.h>
10 #include <aws/mqtt/private/request-response/protocol_adapter.h>
11
12 #include <inttypes.h>
13
14 enum aws_rr_subscription_status_type {
15 ARRSST_SUBSCRIBED,
16 ARRSST_NOT_SUBSCRIBED,
17 };
18
19 /*
20 * Invariant: subscriptions can only transition from nothing -> {subscribing, unsubscribing}
21 *
22 * In particular, the logic blocks subscribing while unsubscribing and unsubscribing while subscribing (unless
23 * shutting down).
24 */
25 enum aws_rr_subscription_pending_action_type {
26 ARRSPAT_NOTHING,
27 ARRSPAT_SUBSCRIBING,
28 ARRSPAT_UNSUBSCRIBING,
29 };
30
31 struct aws_rr_subscription_listener {
32 struct aws_allocator *allocator;
33 uint64_t operation_id;
34 };
35
36 static uint64_t s_aws_hash_subscription_listener(const void *item) {
37 const struct aws_rr_subscription_listener *listener = item;
38
39 return listener->operation_id;
40 }
41
42 static bool s_aws_subscription_listener_hash_equality(const void *a, const void *b) {
43 const struct aws_rr_subscription_listener *a_listener = a;
44 const struct aws_rr_subscription_listener *b_listener = b;
45
46 return a_listener->operation_id == b_listener->operation_id;
47 }
48
49 static void s_aws_subscription_listener_destroy(void *element) {
50 struct aws_rr_subscription_listener *listener = element;
51
52 aws_mem_release(listener->allocator, listener);
53 }
54
55 struct aws_rr_subscription_record {
56 struct aws_allocator *allocator;
57
58 struct aws_byte_buf topic_filter;
59 struct aws_byte_cursor topic_filter_cursor;
60
61 struct aws_hash_table listeners;
62
63 enum aws_rr_subscription_status_type status;
64 enum aws_rr_subscription_pending_action_type pending_action;
65
66 enum aws_rr_subscription_type type;
67
68 /*
69 * A poisoned record represents a subscription that we will never try to subscribe to because a previous
70 * attempt resulted in a failure that we judge to be "terminal." Terminal failures include permission failures
71 * and validation failures. To remove a poisoned record, all listeners must be removed. For request-response
72 * operations this will happen naturally. For streaming operations, the operation must be closed by the user (in
73 * response to the user-facing event we emit on the streaming operation when the failure that poisons the
74 * record occurs).
75 */
76 bool poisoned;
77 };
78
79 static void s_aws_rr_subscription_record_destroy(void *element) {
80 struct aws_rr_subscription_record *record = element;
81
82 aws_byte_buf_clean_up(&record->topic_filter);
83 aws_hash_table_clean_up(&record->listeners);
84
85 aws_mem_release(record->allocator, record);
86 }
87
88 static struct aws_rr_subscription_record *s_aws_rr_subscription_new(
89 struct aws_allocator *allocator,
90 struct aws_byte_cursor topic_filter,
91 enum aws_rr_subscription_type type) {
92 struct aws_rr_subscription_record *record = aws_mem_calloc(allocator, 1, sizeof(struct aws_rr_subscription_record));
93 record->allocator = allocator;
94
95 aws_byte_buf_init_copy_from_cursor(&record->topic_filter, allocator, topic_filter);
96 record->topic_filter_cursor = aws_byte_cursor_from_buf(&record->topic_filter);
97
98 aws_hash_table_init(
99 &record->listeners,
100 allocator,
101 4,
102 s_aws_hash_subscription_listener,
103 s_aws_subscription_listener_hash_equality,
104 NULL,
105 s_aws_subscription_listener_destroy);
106
107 record->status = ARRSST_NOT_SUBSCRIBED;
108 record->pending_action = ARRSPAT_NOTHING;
109
110 record->type = type;
111
112 return record;
113 }
114
115 static void s_subscription_record_unsubscribe(
116 struct aws_rr_subscription_manager *manager,
117 struct aws_rr_subscription_record *record,
118 bool shutdown) {
119
120 bool currently_subscribed = record->status == ARRSST_SUBSCRIBED;
121 bool currently_subscribing = record->pending_action == ARRSPAT_SUBSCRIBING;
122 bool currently_unsubscribing = record->pending_action == ARRSPAT_UNSUBSCRIBING;
123
124 /*
125 * The difference between a shutdown unsubscribe and a normal unsubscribe is that on a shutdown we will "chase"
126 * a pending subscribe with an unsubscribe (breaking the invariant of never having multiple MQTT operations
127 * pending on a subscription).
128 */
129 bool should_unsubscribe = currently_subscribed && !currently_unsubscribing;
130 if (shutdown) {
131 should_unsubscribe = should_unsubscribe || currently_subscribing;
132 }
133
134 if (!should_unsubscribe) {
135 AWS_LOGF_DEBUG(
136 AWS_LS_MQTT_REQUEST_RESPONSE,
137 "request-response subscription manager - subscription ('" PRInSTR
138 "') has no listeners but is not in a state that allows unsubscribe yet",
139 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor));
140 return;
141 }
142
143 struct aws_protocol_adapter_unsubscribe_options unsubscribe_options = {
144 .topic_filter = record->topic_filter_cursor,
145 .ack_timeout_seconds = manager->config.operation_timeout_seconds,
146 };
147
148 if (aws_mqtt_protocol_adapter_unsubscribe(manager->protocol_adapter, &unsubscribe_options)) {
149 int error_code = aws_last_error();
150 AWS_LOGF_ERROR(
151 AWS_LS_MQTT_REQUEST_RESPONSE,
152 "request-response subscription manager - sync unsubscribe failure for ('" PRInSTR "'), ec %d(%s)",
153 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor),
154 error_code,
155 aws_error_debug_str(error_code));
156 return;
157 }
158
159 AWS_LOGF_DEBUG(
160 AWS_LS_MQTT_REQUEST_RESPONSE,
161 "request-response subscription manager - unsubscribe submitted for ('" PRInSTR "')",
162 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor));
163
164 record->pending_action = ARRSPAT_UNSUBSCRIBING;
165 }
166
167 /* Only called by the request-response client when shutting down */
168 static int s_rr_subscription_clean_up_foreach_wrap(void *context, struct aws_hash_element *elem) {
169 struct aws_rr_subscription_manager *manager = context;
170 struct aws_rr_subscription_record *subscription = elem->value;
171
172 s_subscription_record_unsubscribe(manager, subscription, true);
173 s_aws_rr_subscription_record_destroy(subscription);
174
175 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE | AWS_COMMON_HASH_TABLE_ITER_DELETE;
176 }
177
178 static struct aws_rr_subscription_record *s_get_subscription_record(
179 struct aws_rr_subscription_manager *manager,
180 struct aws_byte_cursor topic_filter) {
181 struct aws_rr_subscription_record *subscription = NULL;
182 struct aws_hash_element *element = NULL;
183 if (aws_hash_table_find(&manager->subscriptions, &topic_filter, &element)) {
184 return NULL;
185 }
186
187 if (element != NULL) {
188 subscription = element->value;
189 }
190
191 return subscription;
192 }
193
194 struct aws_subscription_stats {
195 size_t request_response_subscriptions;
196 size_t event_stream_subscriptions;
197 size_t unsubscribing_event_stream_subscriptions;
198 };
199
200 static int s_rr_subscription_count_foreach_wrap(void *context, struct aws_hash_element *elem) {
201 const struct aws_rr_subscription_record *subscription = elem->value;
202 struct aws_subscription_stats *stats = context;
203
204 if (subscription->type == ARRST_EVENT_STREAM) {
205 ++stats->event_stream_subscriptions;
206 if (subscription->pending_action == ARRSPAT_UNSUBSCRIBING) {
207 ++stats->unsubscribing_event_stream_subscriptions;
208 }
209 } else {
210 ++stats->request_response_subscriptions;
211 }
212
213 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
214 }
215
216 static void s_get_subscription_stats(
217 struct aws_rr_subscription_manager *manager,
218 struct aws_subscription_stats *stats) {
219 AWS_ZERO_STRUCT(*stats);
220
221 aws_hash_table_foreach(&manager->subscriptions, s_rr_subscription_count_foreach_wrap, stats);
222
223 AWS_LOGF_DEBUG(
224 AWS_LS_MQTT_REQUEST_RESPONSE,
225 "request-response subscription manager current stats: %d event stream sub records, %d request-response sub "
226 "records, %d unsubscribing event stream subscriptions",
227 (int)stats->event_stream_subscriptions,
228 (int)stats->request_response_subscriptions,
229 (int)stats->unsubscribing_event_stream_subscriptions);
230 }
231
232 static void s_remove_listener_from_subscription_record(
233 struct aws_rr_subscription_manager *manager,
234 struct aws_byte_cursor topic_filter,
235 uint64_t operation_id) {
236 struct aws_rr_subscription_record *record = s_get_subscription_record(manager, topic_filter);
237 if (record == NULL) {
238 return;
239 }
240
241 struct aws_rr_subscription_listener listener = {
242 .operation_id = operation_id,
243 };
244
245 aws_hash_table_remove(&record->listeners, &listener, NULL, NULL);
246
247 size_t listener_count = aws_hash_table_get_entry_count(&record->listeners);
248
249 AWS_LOGF_DEBUG(
250 AWS_LS_MQTT_REQUEST_RESPONSE,
251 "request-response subscription manager - removed listener %" PRIu64 " from subscription ('" PRInSTR
252 "'), %zu listeners left",
253 operation_id,
254 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor),
255 listener_count);
256
257 if (listener_count == 0) {
258 struct aws_rr_subscription_status_event event = {
259 .type = ARRSET_SUBSCRIPTION_EMPTY,
260 .topic_filter = record->topic_filter_cursor,
261 .operation_id = 0,
262 };
263
264 (*manager->config.subscription_status_callback)(&event, manager->config.userdata);
265 }
266 }
267
268 static void s_add_listener_to_subscription_record(struct aws_rr_subscription_record *record, uint64_t operation_id) {
269 struct aws_rr_subscription_listener *listener =
270 aws_mem_calloc(record->allocator, 1, sizeof(struct aws_rr_subscription_listener));
271 listener->allocator = record->allocator;
272 listener->operation_id = operation_id;
273
274 aws_hash_table_put(&record->listeners, listener, listener, NULL);
275
276 AWS_LOGF_DEBUG(
277 AWS_LS_MQTT_REQUEST_RESPONSE,
278 "request-response subscription manager - added listener %" PRIu64 " to subscription ('" PRInSTR
279 "'), %zu listeners total",
280 operation_id,
281 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor),
282 aws_hash_table_get_entry_count(&record->listeners));
283 }
284
285 static int s_rr_subscription_purge_unused_subscriptions_wrapper(void *context, struct aws_hash_element *elem) {
286 struct aws_rr_subscription_record *record = elem->value;
287 struct aws_rr_subscription_manager *manager = context;
288
289 if (aws_hash_table_get_entry_count(&record->listeners) == 0) {
290 AWS_LOGF_DEBUG(
291 AWS_LS_MQTT_REQUEST_RESPONSE,
292 "request-response subscription manager - checking subscription ('" PRInSTR "') for removal",
293 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor));
294
295 if (manager->is_protocol_client_connected) {
296 s_subscription_record_unsubscribe(manager, record, false);
297 }
298
299 if (record->status == ARRSST_NOT_SUBSCRIBED && record->pending_action == ARRSPAT_NOTHING) {
300 AWS_LOGF_DEBUG(
301 AWS_LS_MQTT_REQUEST_RESPONSE,
302 "request-response subscription manager - deleting subscription ('" PRInSTR "')",
303 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor));
304
305 s_aws_rr_subscription_record_destroy(record);
306 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE | AWS_COMMON_HASH_TABLE_ITER_DELETE;
307 }
308 }
309
310 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
311 }
312
313 void aws_rr_subscription_manager_purge_unused(struct aws_rr_subscription_manager *manager) {
314 AWS_LOGF_DEBUG(
315 AWS_LS_MQTT_REQUEST_RESPONSE, "request-response subscription manager - purging unused subscriptions");
316 aws_hash_table_foreach(&manager->subscriptions, s_rr_subscription_purge_unused_subscriptions_wrapper, manager);
317 }
318
319 static const char *s_rr_subscription_event_type_to_c_str(enum aws_rr_subscription_event_type type) {
320 switch (type) {
321 case ARRSET_REQUEST_SUBSCRIBE_SUCCESS:
322 return "RequestSubscribeSuccess";
323
324 case ARRSET_REQUEST_SUBSCRIBE_FAILURE:
325 return "RequestSubscribeFailure";
326
327 case ARRSET_REQUEST_SUBSCRIPTION_ENDED:
328 return "RequestSubscriptionEnded";
329
330 case ARRSET_STREAMING_SUBSCRIPTION_ESTABLISHED:
331 return "StreamingSubscriptionEstablished";
332
333 case ARRSET_STREAMING_SUBSCRIPTION_LOST:
334 return "StreamingSubscriptionLost";
335
336 case ARRSET_STREAMING_SUBSCRIPTION_HALTED:
337 return "StreamingSubscriptionHalted";
338
339 case ARRSET_UNSUBSCRIBE_COMPLETE:
340 return "UnsubscribeComplete";
341
342 case ARRSET_SUBSCRIPTION_EMPTY:
343 return "SubscriptionEmpty";
344 }
345
346 return "Unknown";
347 }
348
349 static bool s_subscription_type_matches_event_type(
350 enum aws_rr_subscription_type subscription_type,
351 enum aws_rr_subscription_event_type event_type) {
352 switch (event_type) {
353 case ARRSET_REQUEST_SUBSCRIBE_SUCCESS:
354 case ARRSET_REQUEST_SUBSCRIBE_FAILURE:
355 case ARRSET_REQUEST_SUBSCRIPTION_ENDED:
356 return subscription_type == ARRST_REQUEST_RESPONSE;
357
358 case ARRSET_STREAMING_SUBSCRIPTION_ESTABLISHED:
359 case ARRSET_STREAMING_SUBSCRIPTION_LOST:
360 case ARRSET_STREAMING_SUBSCRIPTION_HALTED:
361 return subscription_type == ARRST_EVENT_STREAM;
362
363 default:
364 return true;
365 }
366 }
367
368 static void s_emit_subscription_event(
369 const struct aws_rr_subscription_manager *manager,
370 const struct aws_rr_subscription_record *record,
371 enum aws_rr_subscription_event_type type) {
372
373 AWS_FATAL_ASSERT(s_subscription_type_matches_event_type(record->type, type));
374
375 for (struct aws_hash_iter iter = aws_hash_iter_begin(&record->listeners); !aws_hash_iter_done(&iter);
376 aws_hash_iter_next(&iter)) {
377
378 struct aws_rr_subscription_listener *listener = iter.element.value;
379 struct aws_rr_subscription_status_event event = {
380 .type = type,
381 .topic_filter = record->topic_filter_cursor,
382 .operation_id = listener->operation_id,
383 };
384
385 (*manager->config.subscription_status_callback)(&event, manager->config.userdata);
386
387 AWS_LOGF_DEBUG(
388 AWS_LS_MQTT_REQUEST_RESPONSE,
389 "request-response subscription manager - subscription event for ('" PRInSTR
390 "'), type: %s, operation: %" PRIu64 "",
391 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor),
392 s_rr_subscription_event_type_to_c_str(type),
393 listener->operation_id);
394 }
395 }
396
397 static int s_rr_activate_idle_subscription(
398 struct aws_rr_subscription_manager *manager,
399 struct aws_rr_subscription_record *record) {
400 int result = AWS_OP_SUCCESS;
401
402 if (record->poisoned) {
403 /*
404 * Don't try and establish poisoned subscriptions. This is not an error or a loggable event, it just means
405 * we hit a "try and make subscriptions" event when a poisoned subscription still hadn't been fully released.
406 */
407 return AWS_OP_SUCCESS;
408 }
409
410 if (manager->is_protocol_client_connected && aws_hash_table_get_entry_count(&record->listeners) > 0) {
411 if (record->status == ARRSST_NOT_SUBSCRIBED && record->pending_action == ARRSPAT_NOTHING) {
412 struct aws_protocol_adapter_subscribe_options subscribe_options = {
413 .topic_filter = record->topic_filter_cursor,
414 .ack_timeout_seconds = manager->config.operation_timeout_seconds,
415 };
416
417 result = aws_mqtt_protocol_adapter_subscribe(manager->protocol_adapter, &subscribe_options);
418 if (result == AWS_OP_SUCCESS) {
419 AWS_LOGF_DEBUG(
420 AWS_LS_MQTT_REQUEST_RESPONSE,
421 "request-response subscription manager - initiating subscribe operation for ('" PRInSTR "')",
422 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor));
423 record->pending_action = ARRSPAT_SUBSCRIBING;
424 } else {
425 int error_code = aws_last_error();
426 AWS_LOGF_ERROR(
427 AWS_LS_MQTT_REQUEST_RESPONSE,
428 "request-response subscription manager - synchronous failure subscribing to ('" PRInSTR
429 "'), ec %d(%s)",
430 AWS_BYTE_CURSOR_PRI(record->topic_filter_cursor),
431 error_code,
432 aws_error_debug_str(error_code));
433
434 if (record->type == ARRST_REQUEST_RESPONSE) {
435 s_emit_subscription_event(manager, record, ARRSET_REQUEST_SUBSCRIBE_FAILURE);
436 } else {
437 record->poisoned = true;
438 s_emit_subscription_event(manager, record, ARRSET_STREAMING_SUBSCRIPTION_HALTED);
439 }
440 }
441 }
442 }
443
444 return result;
445 }
446
447 enum aws_acquire_subscription_result_type aws_rr_subscription_manager_acquire_subscription(
448 struct aws_rr_subscription_manager *manager,
449 const struct aws_rr_acquire_subscription_options *options) {
450
451 if (options->topic_filter_count == 0) {
452 AWS_LOGF_ERROR(
453 AWS_LS_MQTT_REQUEST_RESPONSE,
454 "request-response subscription manager - acquire_subscription for operation %" PRIu64
455 " with no topic filters",
456 options->operation_id);
457 return AASRT_FAILURE;
458 }
459
460 /*
461 * Check for poisoned or mismatched records. This has precedence over the following unsubscribing check,
462 * and so we put them in separate loops
463 */
464 for (size_t i = 0; i < options->topic_filter_count; ++i) {
465 struct aws_byte_cursor topic_filter = options->topic_filters[i];
466 struct aws_rr_subscription_record *existing_record = s_get_subscription_record(manager, topic_filter);
467 if (existing_record == NULL) {
468 continue;
469 }
470
471 if (existing_record->poisoned) {
472 AWS_LOGF_ERROR(
473 AWS_LS_MQTT_REQUEST_RESPONSE,
474 "request-response subscription manager - acquire subscription for ('" PRInSTR "'), operation %" PRIu64
475 " failed - existing subscription is poisoned and has not been released",
476 AWS_BYTE_CURSOR_PRI(topic_filter),
477 options->operation_id);
478 return AASRT_FAILURE;
479 }
480
481 if (existing_record->type != options->type) {
482 AWS_LOGF_ERROR(
483 AWS_LS_MQTT_REQUEST_RESPONSE,
484 "request-response subscription manager - acquire subscription for ('" PRInSTR "'), operation %" PRIu64
485 " failed - conflicts with subscription type of existing subscription",
486 AWS_BYTE_CURSOR_PRI(topic_filter),
487 options->operation_id);
488 return AASRT_FAILURE;
489 }
490 }
491
492 /* blocked if an existing record is unsubscribing; also compute how many subscriptions are needed */
493 size_t subscriptions_needed = 0;
494 for (size_t i = 0; i < options->topic_filter_count; ++i) {
495 struct aws_byte_cursor topic_filter = options->topic_filters[i];
496 struct aws_rr_subscription_record *existing_record = s_get_subscription_record(manager, topic_filter);
497 if (existing_record != NULL) {
498 if (existing_record->pending_action == ARRSPAT_UNSUBSCRIBING) {
499 AWS_LOGF_DEBUG(
500 AWS_LS_MQTT_REQUEST_RESPONSE,
501 "request-response subscription manager - acquire subscription for ('" PRInSTR
502 "'), operation %" PRIu64 " blocked - existing subscription is unsubscribing",
503 AWS_BYTE_CURSOR_PRI(topic_filter),
504 options->operation_id);
505 return AASRT_BLOCKED;
506 }
507 } else {
508 ++subscriptions_needed;
509 }
510 }
511
512 /* Check for space and fail or block as appropriate */
513 if (subscriptions_needed > 0) {
514 /* how much of the budget are we using? */
515 struct aws_subscription_stats stats;
516 s_get_subscription_stats(manager, &stats);
517
518 if (options->type == ARRST_REQUEST_RESPONSE) {
519 if (subscriptions_needed >
520 manager->config.max_request_response_subscriptions - stats.request_response_subscriptions) {
521 AWS_LOGF_DEBUG(
522 AWS_LS_MQTT_REQUEST_RESPONSE,
523 "request-response subscription manager - acquire subscription for request operation %" PRIu64
524 " blocked - no room currently",
525 options->operation_id);
526 return AASRT_BLOCKED;
527 }
528 } else {
529 /*
530 * Streaming subscriptions have more complicated space-checking logic. Under certain conditions, we may
531 * block rather than failing
532 */
533 if (subscriptions_needed + stats.event_stream_subscriptions > manager->config.max_streaming_subscriptions) {
534 if (subscriptions_needed + stats.event_stream_subscriptions <=
535 manager->config.max_streaming_subscriptions + stats.unsubscribing_event_stream_subscriptions) {
536 /* If enough subscriptions are in the process of going away then wait in the blocked state */
537 AWS_LOGF_DEBUG(
538 AWS_LS_MQTT_REQUEST_RESPONSE,
539 "request-response subscription manager - acquire subscription for streaming operation %" PRIu64
540 " blocked - no room currently",
541 options->operation_id);
542 return AASRT_BLOCKED;
543 } else {
544 /* Otherwise, there's no hope, fail */
545 AWS_LOGF_DEBUG(
546 AWS_LS_MQTT_REQUEST_RESPONSE,
547 "request-response subscription manager - acquire subscription for operation %" PRIu64
548 " failed - no room",
549 options->operation_id);
550 return AASRT_NO_CAPACITY;
551 }
552 }
553 }
554 }
555
556 bool is_fully_subscribed = true;
557 for (size_t i = 0; i < options->topic_filter_count; ++i) {
558 struct aws_byte_cursor topic_filter = options->topic_filters[i];
559 struct aws_rr_subscription_record *existing_record = s_get_subscription_record(manager, topic_filter);
560
561 if (existing_record == NULL) {
562 existing_record = s_aws_rr_subscription_new(manager->allocator, topic_filter, options->type);
563 aws_hash_table_put(&manager->subscriptions, &existing_record->topic_filter_cursor, existing_record, NULL);
564 }
565
566 s_add_listener_to_subscription_record(existing_record, options->operation_id);
567 if (existing_record->status != ARRSST_SUBSCRIBED) {
568 is_fully_subscribed = false;
569 }
570 }
571
572 if (is_fully_subscribed) {
573 AWS_LOGF_DEBUG(
574 AWS_LS_MQTT_REQUEST_RESPONSE,
575 "request-response subscription manager - acquire subscription for operation %" PRIu64
576 " fully subscribed - all required subscriptions are active",
577 options->operation_id);
578 return AASRT_SUBSCRIBED;
579 }
580
581 for (size_t i = 0; i < options->topic_filter_count; ++i) {
582 struct aws_byte_cursor topic_filter = options->topic_filters[i];
583 struct aws_rr_subscription_record *existing_record = s_get_subscription_record(manager, topic_filter);
584
585 if (s_rr_activate_idle_subscription(manager, existing_record)) {
586 AWS_LOGF_ERROR(
587 AWS_LS_MQTT_REQUEST_RESPONSE,
588 "request-response subscription manager - acquire subscription for operation %" PRIu64
589 " failed - synchronous subscribe failure",
590 options->operation_id);
591 return AASRT_FAILURE;
592 }
593 }
594
595 AWS_LOGF_DEBUG(
596 AWS_LS_MQTT_REQUEST_RESPONSE,
597 "request-response subscription manager - acquire subscription for operation %" PRIu64
598 " subscribing - waiting on one or more subscribes to complete",
599 options->operation_id);
600
601 return AASRT_SUBSCRIBING;
602 }
603
604 void aws_rr_subscription_manager_release_subscription(
605 struct aws_rr_subscription_manager *manager,
606 const struct aws_rr_release_subscription_options *options) {
607 for (size_t i = 0; i < options->topic_filter_count; ++i) {
608 struct aws_byte_cursor topic_filter = options->topic_filters[i];
609 s_remove_listener_from_subscription_record(manager, topic_filter, options->operation_id);
610 }
611 }
612
613 static void s_handle_protocol_adapter_request_subscription_event(
614 struct aws_rr_subscription_manager *manager,
615 struct aws_rr_subscription_record *record,
616 const struct aws_protocol_adapter_subscription_event *event) {
617 if (event->event_type == AWS_PASET_SUBSCRIBE) {
618 AWS_FATAL_ASSERT(record->pending_action == ARRSPAT_SUBSCRIBING);
619 record->pending_action = ARRSPAT_NOTHING;
620
621 if (event->error_code == AWS_ERROR_SUCCESS) {
622 record->status = ARRSST_SUBSCRIBED;
623 s_emit_subscription_event(manager, record, ARRSET_REQUEST_SUBSCRIBE_SUCCESS);
624 } else {
625 s_emit_subscription_event(manager, record, ARRSET_REQUEST_SUBSCRIBE_FAILURE);
626 }
627 } else {
628 AWS_FATAL_ASSERT(event->event_type == AWS_PASET_UNSUBSCRIBE);
629 AWS_FATAL_ASSERT(record->pending_action == ARRSPAT_UNSUBSCRIBING);
630 record->pending_action = ARRSPAT_NOTHING;
631
632 if (event->error_code == AWS_ERROR_SUCCESS) {
633 record->status = ARRSST_NOT_SUBSCRIBED;
634
635 struct aws_rr_subscription_status_event unsubscribe_event = {
636 .type = ARRSET_UNSUBSCRIBE_COMPLETE,
637 .topic_filter = record->topic_filter_cursor,
638 .operation_id = 0,
639 };
640
641 (*manager->config.subscription_status_callback)(&unsubscribe_event, manager->config.userdata);
642 }
643 }
644 }
645
646 static void s_handle_protocol_adapter_streaming_subscription_event(
647 struct aws_rr_subscription_manager *manager,
648 struct aws_rr_subscription_record *record,
649 const struct aws_protocol_adapter_subscription_event *event) {
650 if (event->event_type == AWS_PASET_SUBSCRIBE) {
651 AWS_FATAL_ASSERT(record->pending_action == ARRSPAT_SUBSCRIBING);
652 record->pending_action = ARRSPAT_NOTHING;
653
654 if (event->error_code == AWS_ERROR_SUCCESS) {
655 record->status = ARRSST_SUBSCRIBED;
656 s_emit_subscription_event(manager, record, ARRSET_STREAMING_SUBSCRIPTION_ESTABLISHED);
657 } else {
658 if (event->retryable) {
659 s_rr_activate_idle_subscription(manager, record);
660 } else {
661 record->poisoned = true;
662 s_emit_subscription_event(manager, record, ARRSET_STREAMING_SUBSCRIPTION_HALTED);
663 }
664 }
665 } else {
666 AWS_FATAL_ASSERT(event->event_type == AWS_PASET_UNSUBSCRIBE);
667 AWS_FATAL_ASSERT(record->pending_action == ARRSPAT_UNSUBSCRIBING);
668 record->pending_action = ARRSPAT_NOTHING;
669
670 if (event->error_code == AWS_ERROR_SUCCESS) {
671 record->status = ARRSST_NOT_SUBSCRIBED;
672
673 struct aws_rr_subscription_status_event unsubscribe_event = {
674 .type = ARRSET_UNSUBSCRIBE_COMPLETE,
675 .topic_filter = record->topic_filter_cursor,
676 .operation_id = 0,
677 };
678
679 (*manager->config.subscription_status_callback)(&unsubscribe_event, manager->config.userdata);
680 }
681 }
682 }
683
684 void aws_rr_subscription_manager_on_protocol_adapter_subscription_event(
685 struct aws_rr_subscription_manager *manager,
686 const struct aws_protocol_adapter_subscription_event *event) {
687 struct aws_rr_subscription_record *record = s_get_subscription_record(manager, event->topic_filter);
688 if (record == NULL) {
689 return;
690 }
691
692 AWS_LOGF_DEBUG(
693 AWS_LS_MQTT_REQUEST_RESPONSE,
694 "request-response subscription manager - received a protocol adapter subscription event for ('" PRInSTR
695 "'), type %s, error_code %d(%s)",
696 AWS_BYTE_CURSOR_PRI(event->topic_filter),
697 aws_protocol_adapter_subscription_event_type_to_c_str(event->event_type),
698 event->error_code,
699 aws_error_debug_str(event->error_code));
700
701 if (record->type == ARRST_REQUEST_RESPONSE) {
702 s_handle_protocol_adapter_request_subscription_event(manager, record, event);
703 } else {
704 s_handle_protocol_adapter_streaming_subscription_event(manager, record, event);
705 }
706 }
707
708 static int s_rr_activate_idle_subscriptions_wrapper(void *context, struct aws_hash_element *elem) {
709 struct aws_rr_subscription_record *record = elem->value;
710 struct aws_rr_subscription_manager *manager = context;
711
712 s_rr_activate_idle_subscription(manager, record);
713
714 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
715 }
716
717 static void s_activate_idle_subscriptions(struct aws_rr_subscription_manager *manager) {
718 aws_hash_table_foreach(&manager->subscriptions, s_rr_activate_idle_subscriptions_wrapper, manager);
719 }
720
721 static int s_apply_session_lost_wrapper(void *context, struct aws_hash_element *elem) {
722 struct aws_rr_subscription_record *record = elem->value;
723 struct aws_rr_subscription_manager *manager = context;
724
725 if (record->status == ARRSST_SUBSCRIBED) {
726 record->status = ARRSST_NOT_SUBSCRIBED;
727
728 if (record->type == ARRST_REQUEST_RESPONSE) {
729 s_emit_subscription_event(manager, record, ARRSET_REQUEST_SUBSCRIPTION_ENDED);
730
731 if (record->pending_action != ARRSPAT_UNSUBSCRIBING) {
732 s_aws_rr_subscription_record_destroy(record);
733 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE | AWS_COMMON_HASH_TABLE_ITER_DELETE;
734 }
735 } else {
736 s_emit_subscription_event(manager, record, ARRSET_STREAMING_SUBSCRIPTION_LOST);
737 }
738 }
739
740 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
741 }
742
743 static int s_apply_streaming_resubscribe_wrapper(void *context, struct aws_hash_element *elem) {
744 struct aws_rr_subscription_record *record = elem->value;
745 struct aws_rr_subscription_manager *manager = context;
746
747 if (record->type == ARRST_EVENT_STREAM) {
748 s_rr_activate_idle_subscription(manager, record);
749 }
750
751 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
752 }
753
754 static void s_apply_session_lost(struct aws_rr_subscription_manager *manager) {
755 aws_hash_table_foreach(&manager->subscriptions, s_apply_session_lost_wrapper, manager);
756 aws_hash_table_foreach(&manager->subscriptions, s_apply_streaming_resubscribe_wrapper, manager);
757 }
758
759 void aws_rr_subscription_manager_on_protocol_adapter_connection_event(
760 struct aws_rr_subscription_manager *manager,
761 const struct aws_protocol_adapter_connection_event *event) {
762
763 if (event->event_type == AWS_PACET_CONNECTED) {
764 AWS_LOGF_DEBUG(
765 AWS_LS_MQTT_REQUEST_RESPONSE,
766 "request-response subscription manager - received a protocol adapter connection event, joined_session: "
767 "%d",
768 (int)(event->joined_session ? 1 : 0));
769
770 manager->is_protocol_client_connected = true;
771 if (!event->joined_session) {
772 s_apply_session_lost(manager);
773 }
774
775 aws_rr_subscription_manager_purge_unused(manager);
776 s_activate_idle_subscriptions(manager);
777 } else {
778 AWS_LOGF_DEBUG(
779 AWS_LS_MQTT_REQUEST_RESPONSE,
780 "request-response subscription manager - received a protocol adapter disconnection event");
781
782 manager->is_protocol_client_connected = false;
783 }
784 }
785
786 bool aws_rr_subscription_manager_are_options_valid(const struct aws_rr_subscription_manager_options *options) {
787 if (options == NULL || options->max_request_response_subscriptions < 2 || options->operation_timeout_seconds == 0) {
788 return false;
789 }
790
791 return true;
792 }
793
794 void aws_rr_subscription_manager_init(
795 struct aws_rr_subscription_manager *manager,
796 struct aws_allocator *allocator,
797 struct aws_mqtt_protocol_adapter *protocol_adapter,
798 const struct aws_rr_subscription_manager_options *options) {
799 AWS_ZERO_STRUCT(*manager);
800
801 AWS_FATAL_ASSERT(aws_rr_subscription_manager_are_options_valid(options));
802
803 manager->allocator = allocator;
804 manager->config = *options;
805 manager->protocol_adapter = protocol_adapter;
806
807 aws_hash_table_init(
808 &manager->subscriptions,
809 allocator,
810 options->max_request_response_subscriptions + options->max_streaming_subscriptions,
811 aws_hash_byte_cursor_ptr,
812 aws_mqtt_byte_cursor_hash_equality,
813 NULL,
814 s_aws_rr_subscription_record_destroy);
815
816 manager->is_protocol_client_connected = aws_mqtt_protocol_adapter_is_connected(protocol_adapter);
817 }
818
819 void aws_rr_subscription_manager_clean_up(struct aws_rr_subscription_manager *manager) {
820 aws_hash_table_foreach(&manager->subscriptions, s_rr_subscription_clean_up_foreach_wrap, manager);
821 aws_hash_table_clean_up(&manager->subscriptions);
822 }