Commit Graph

17 Commits

Author SHA1 Message Date
Chunyi Lyu 209f23fa2f
Revert "Format MQTT code with `erlfmt`" 2023-01-27 18:25:57 +00:00
Chunyi Lyu 1de9fcf582 Format mqtt files with erlfmt 2023-01-27 11:06:41 +00:00
David Ansari d651f87ea7 Share tests between MQTT and Web MQTT
New test suite deps/rabbitmq_mqtt/test/shared_SUITE contains tests that
are executed against both MQTT and Web MQTT.

This has two major advantages:
1. Eliminates test code duplication between rabbitmq_mqtt and
rabbitmq_web_mqtt making the tests easier to maintain and to understand.
2. Increases test coverage of Web MQTT.

It's acceptable to add a **test** dependency from rabbitmq_mqtt to
rabbitmq_web_mqtt. Obviously, there should be no such dependency
for non-test code.
2023-01-24 17:32:59 +00:00
David Ansari bd0acb33e4 Remove test helper util:connect_to_node/3
because this method is superfluous given that util:connect
already exists.
2023-01-24 17:30:10 +00:00
David Ansari 97fefff0fe Add overflow drop-head to rabbit_mqtt_qos_queue type
Traditionally, queue types implement flow control by keeping state in
both sending and receiving Erlang processes (for example credit based flow
control keeps the number of credits within the process dictionary).

The rabbit_mqtt_qos0_queue cannot keep such state in sending or receiving
Erlang process because such state would consume a large amount of memory
in case of large fan-ins or large fan-outs.
The whole idea of the rabbit_mqtt_qos_queue type is to not keep any
state in the rabbit_queue_type client. This makes this new queue
type scale so well.

Therefore the new queue type cannot (easily) implement flow control
throttling individual senders.

In this commit, we take a different approach:
Instead of implementing flow control throttling individual senders,
the receiving MQTT connection process drops QoS 0 messages from the
rabbit_mqtt_qos_queue if it is overflowed with messages AND its MQTT
client is not capable of receiving messages fast enough.

This is a simple and sufficient solution because it's better to drop QoS
0 (at most once) messages instead of causing cluster-wide memory alarms.

The user can opt out of dropping messages by setting the new env setting
mailbox_soft_limit to 0.

Additionally, we reduce the send_timeout from 30 seconds default in
Ranch to 15 seconds default in MQTT. This will detect hanging MQTT
clients faster causing the MQTT connection to be closed.
2023-01-24 17:30:10 +00:00
Chunyi Lyu de28560d8f Extract connect to node helper in rmq mqtt tests 2023-01-24 17:30:10 +00:00
Chunyi Lyu aea7ff8f8d Use helper to connect to node in mqtt cluster suite 2023-01-24 17:30:10 +00:00
Chunyi Lyu 30a9ea521e Use connect helper func in more mqtt tests
- reduce code duplication
- connect helper does not unlink the connection process by default
2023-01-24 17:30:10 +00:00
David Ansari 7bc8208a1b Remove local record definitions from header files
Record #state{} is purely local to rabbit_mqtt_reader.
Record #proc_state{} is purely local to rabbit_mqtt_processor.

Therefore, move these record definitions to the defining module.
This avoids unnecessarily exposing internal information.

Now, that #proc_state{} is defined in rabbit_mqtt_processor,
rename #proc_state to #state{}.
2023-01-24 17:30:10 +00:00
Chunyi Lyu c3779d9996 Implement message consuming counters in mqtt 2023-01-24 17:30:10 +00:00
Chunyi Lyu 0b43f002f5 Remove subscriptions map from proc state in mqtt
- subscriptions information can be retrieved directly from mnesia
- when unsubscribe, we check if there is binding between topic name
and queue (check for both qos0 queue name and qos1 queue name) to
unbind
- added a boolean value has_subs in proc state which will indicate
if connection has any active subscriptions. Used for setting consumer
global counter
2023-01-24 17:29:07 +00:00
David Ansari aad7e1cdf6 Add test for consuming MQTT classic queue going down 2023-01-24 17:29:07 +00:00
David Ansari bda52dbf64 Support consuming classic mirrored queue failover
Some users use classic mirrored queues for MQTT queues by
applying a policy.

Given that classic mirrored queues are deprecated, but still supported
in RabbitMQ 3.x, native MQTT must support classic mirrored queues.
2023-01-24 17:29:07 +00:00
David Ansari b97006c4b9 Output username in connection closed event 2023-01-24 17:29:07 +00:00
Chunyi Lyu 96854a8c4c Use emqtt:publish in mqtt tests
- rename publish_qos1 to publish_qos1_timeout
since it's only been used for handling publisher timeout
more gracefully in tests
2023-01-24 17:29:07 +00:00
David Ansari 33bf2150a5 Add test for publishing via MQTT to different queue types 2023-01-24 17:29:07 +00:00
David Ansari 199238d76e Use pg to track MQTT client IDs
Instead of tracking {Vhost, ClientId} to ConnectionPid mappings in our
custom process registry, i.e. custom local ETS table with a custom
gen_server process managing that ETS table, this commit uses the pg module
because pg is better tested.

To save memory with millions of MQTT client connections, we want to save
the mappings only locally on the node where the connection resides and
therfore not be replicated across all nodes.

According to Maxim Fedorov:
"The easy way to provide per-node unique pg scope is to start it like
pg:start_link(node()). At least that's what we've been doing to have
node-local scopes. It will still try to discover scopes on nodeup from
nodes joining the cluster, but since you cannot have nodes with the
same name in one cluster, using node() for local-only scopes worked
well for us."

So that's what we're doing in this commit.
2023-01-24 17:29:07 +00:00