design.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. .. _design:
  2. ============
  3. Design Guide
  4. ============
  5. .. _design_architecture:
  6. ------------
  7. Architecture
  8. ------------
  9. The emqttd broker 1.0 is more like a network Switch or Router, not a traditional enterprise message queue. Compared to a network router that routes packets based on IP or MPLS label, the emqttd broker routes MQTT messages based on topic trie.
  10. .. image:: _static/images/concept.png
  11. Design Philosophy
  12. -----------------
  13. 1. Focus on handling millions of MQTT connections and routing MQTT messages between clustered nodes.
  14. 2. Embrace Erlang/OTP, The Soft-Realtime, Low-Latency, Concurrent and Fault-Tolerant Platform.
  15. 3. Layered Design: Connection, Session, PubSub and Router Layers.
  16. 4. Separate the Message Flow Plane and the Control/Management Plane.
  17. 5. Stream MQTT messages to various backends including MQ or databases.
  18. System Layers
  19. -------------
  20. 1. Connection Layer:
  21. Handle TCP and WebSocket connections, encode/decode MQTT packets.
  22. 2. Session Layer:
  23. Process MQTT PUBLISH/SUBSCRIBE Packets received from client, and deliver MQTT messages to client.
  24. 3. PubSub Layer:
  25. Dispatch MQTT messages to subscribers in a node.
  26. 4. Routing(Distributed) Layer:
  27. Route MQTT messages between clustered nodes.
  28. .. code::
  29. -------------- ----------- ---------- ----------
  30. Client --> | Connection | --> | Session | --> | PubSub | --> | Router |
  31. -------------- ----------- ---------- ----------
  32. ----------------
  33. Connection Layer
  34. ----------------
  35. This layer is built on the `eSockd`_ library which is a general Non-blocking TCP/SSL Socket Server:
  36. * Acceptor Pool and Asynchronous TCP Accept
  37. * Parameterized Connection Module
  38. * Max connections management
  39. * Allow/Deny by peer address or CIDR
  40. * Keepalive Support
  41. * Rate Limit based on The Leaky Bucket Algorithm
  42. * Fully Asynchronous TCP RECV/SEND
  43. This layer is also responsible for encoding/decoding MQTT frames:
  44. 1. Parse MQTT frame received from client
  45. 2. Serialize MQTT frame sent to client
  46. 3. MQTT Connection Keepalive
  47. Main modules of this layer:
  48. +------------------+--------------------------+
  49. | Module | Description |
  50. +==================+==========================+
  51. | emqttd_client | TCP Client |
  52. +------------------+--------------------------+
  53. | emqttd_ws_client | WebSocket Client |
  54. +------------------+--------------------------+
  55. | emqttd_protocol | MQTT Protocol Handler |
  56. +------------------+--------------------------+
  57. | emqttd_parser | MQTT Frame Parser |
  58. +------------------+--------------------------+
  59. | emqttd_serializer| MQTT Frame Serializer |
  60. +------------------+--------------------------+
  61. -------------
  62. Session Layer
  63. -------------
  64. The session layer processes MQTT packets received from client, and deliver PUBLISH packets to client.
  65. A MQTT session will store the subscriptions and inflight messages in memory:
  66. 1. The Client’s subscriptions.
  67. 2. Inflight qos1/2 messages sent to the client but unacked, QoS 2 messages which
  68. have been sent to the Client, but have not been completely acknowledged.
  69. 3. Inflight qos2 messages received from client and waiting for PUBREL. QoS 2
  70. messages which have been received from the Client, but have not been
  71. completely acknowledged.
  72. 4. All qos1, qos2 messages published to when client has been disconnected.
  73. MQueue and Inflight Window
  74. --------------------------
  75. Concept of Message Queue and Inflight Window::
  76. |<----------------- Max Len ----------------->|
  77. -----------------------------------------------
  78. IN -> | Messages Queue | Inflight Window | -> Out
  79. -----------------------------------------------
  80. |<--- Win Size --->|
  81. 1. Inflight Window to store the messages delivered and awaiting for PUBACK.
  82. 2. Enqueue messages when the inflight window is full.
  83. 3. If the queue is full, dropped qos0 messages if store_qos0 is true, otherwise dropped the oldest one.
  84. The larger the inflight window size, the higher the throughput. The smaller the window size, the more strict the message order.
  85. PacketId and MessageId
  86. ----------------------
  87. The 16bits PacketId is defined by MQTT Protocol Specification, used by client/server to PUBLISH/PUBACK packets. A GUID(128bits globally unique Id) will be generated by the broker and assigned to a MQTT message.
  88. Format of the globally unique message id:
  89. +------------------------+----------------+------------+
  90. | Timestamp | NodeID + PID | Sequence |
  91. +------------------------+----------------+------------+
  92. | <-------64bits-------> | <---48bits---> | <-16bits-> |
  93. +------------------------+----------------+------------+
  94. 1. Timestamp: erlang:system_time if Erlang >= R18, otherwise os:timestamp
  95. 2. NodeId: encode node() to 2 bytes integer
  96. 3. Pid: encode pid to 4 bytes integer
  97. 4. Sequence: 2 bytes sequence in one process
  98. The PacketId and MessageId in a End-to-End Message PubSub Sequence::
  99. PktId <-- Session --> MsgId <-- Router --> MsgId <-- Session --> PktId
  100. ------------
  101. PubSub Layer
  102. ------------
  103. The PubSub layer maintains a subscription table and responsible to dispatch MQTT messages to subscribers.
  104. .. image:: _static/images/dispatch.png
  105. MQTT messages will be dispatched to the subscriber's session, which finally delivers the message to client.
  106. -------------
  107. Routing Layer
  108. -------------
  109. The routing(distributed) layer maintains and replicates the global Topic Trie and Routing Table. The topic tire is composed of wildcard topics created by subscribers, and the Routing Table map a topic to nodes in the cluster.
  110. For example, if node1 subscribed 't/+/x' and 't/+/y', node2 subscribed 't/#' and node3 subscribed 't/a', there will be a topic trie and route table::
  111. -------------------------
  112. | t |
  113. | / \ |
  114. | + # |
  115. | / \ |
  116. | x y |
  117. -------------------------
  118. | t/+/x -> node1, node3 |
  119. | t/+/y -> node1 |
  120. | t/# -> node2 |
  121. | t/a -> node3 |
  122. -------------------------
  123. The routing layer would route MQTT messages between clustered nodes by topic trie match and routing table lookup:
  124. .. image:: _static/images/route.png
  125. The routing design follows the two rules:
  126. 1. A message only gets forwarded to other cluster nodes if a cluster node is interested in it. This reduces the network traffic tremendously, because it prevents nodes from forwarding unnecessary messages.
  127. 2. As soon as a client on a node subscribes to a topic it becomes known within the cluster. If one of the clients somewhere in the cluster is publishing to this topic, the message will be delivered to its subscriber no matter to which cluster node it is connected.
  128. .. _design_auth_acl:
  129. ----------------------
  130. Authentication and ACL
  131. ----------------------
  132. The emqttd broker supports an extensible authentication/ACL mechanism, which is implemented by emqttd_access_control, emqttd_auth_mod and emqttd_acl_mod modules.
  133. emqttd_access_control module provides two APIs that help register/unregister auth or ACL module::
  134. register_mod(auth | acl, atom(), list()) -> ok | {error, any()}.
  135. register_mod(auth | acl, atom(), list(), non_neg_integer()) -> ok | {error, any()}.
  136. Authentication Bahaviour
  137. -------------------------
  138. The emqttd_auth_mod defines an Erlang behaviour for authentication module::
  139. -module(emqttd_auth_mod).
  140. -ifdef(use_specs).
  141. -callback init(AuthOpts :: list()) -> {ok, State :: any()}.
  142. -callback check(Client, Password, State) -> ok | ignore | {error, string()} when
  143. Client :: mqtt_client(),
  144. Password :: binary(),
  145. State :: any().
  146. -callback description() -> string().
  147. -else.
  148. -export([behaviour_info/1]).
  149. behaviour_info(callbacks) ->
  150. [{init, 1}, {check, 3}, {description, 0}];
  151. behaviour_info(_Other) ->
  152. undefined.
  153. -endif.
  154. The authentication modules implemented by default:
  155. +-----------------------+--------------------------------+
  156. | Module | Authentication |
  157. +-----------------------+--------------------------------+
  158. | emqttd_auth_username | Username and Password |
  159. +-----------------------+--------------------------------+
  160. | emqttd_auth_clientid | ClientID |
  161. +-----------------------+--------------------------------+
  162. | emqttd_auth_ldap | LDAP |
  163. +-----------------------+--------------------------------+
  164. | emqttd_auth_anonymous | Anonymous |
  165. +-----------------------+--------------------------------+
  166. Authorization(ACL)
  167. ------------------
  168. The emqttd_acl_mod defines an Erlang behavihour for ACL module::
  169. -module(emqttd_acl_mod).
  170. -include("emqttd.hrl").
  171. -ifdef(use_specs).
  172. -callback init(AclOpts :: list()) -> {ok, State :: any()}.
  173. -callback check_acl({Client, PubSub, Topic}, State :: any()) -> allow | deny | ignore when
  174. Client :: mqtt_client(),
  175. PubSub :: pubsub(),
  176. Topic :: binary().
  177. -callback reload_acl(State :: any()) -> ok | {error, any()}.
  178. -callback description() -> string().
  179. -else.
  180. -export([behaviour_info/1]).
  181. behaviour_info(callbacks) ->
  182. [{init, 1}, {check_acl, 2}, {reload_acl, 1}, {description, 0}];
  183. behaviour_info(_Other) ->
  184. undefined.
  185. -endif.
  186. emqttd_acl_internal implements the default ACL based on etc/acl.config file::
  187. %%%-----------------------------------------------------------------------------
  188. %%%
  189. %%% -type who() :: all | binary() |
  190. %%% {ipaddr, esockd_access:cidr()} |
  191. %%% {client, binary()} |
  192. %%% {user, binary()}.
  193. %%%
  194. %%% -type access() :: subscribe | publish | pubsub.
  195. %%%
  196. %%% -type topic() :: binary().
  197. %%%
  198. %%% -type rule() :: {allow, all} |
  199. %%% {allow, who(), access(), list(topic())} |
  200. %%% {deny, all} |
  201. %%% {deny, who(), access(), list(topic())}.
  202. %%%
  203. %%%-----------------------------------------------------------------------------
  204. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
  205. {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
  206. {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
  207. {allow, all}.
  208. .. _design_hook:
  209. ------------
  210. Hooks Design
  211. ------------
  212. The emqttd broker implements a simple but powerful hooks mechanism to help users develop plugin. The broker would run the hooks when a client is connected/disconnected, a topic is subscribed/unsubscribed or a MQTT message is published/delivered/acked:
  213. Hooks defined by the emqttd 1.0 broker:
  214. +------------------------+------------------------------------------------------+
  215. | Hook | Description |
  216. +========================+======================================================+
  217. | client.connected | Run when client connected to the broker successfully |
  218. +------------------------+------------------------------------------------------+
  219. | client.subscribe | Run before client subscribes topics |
  220. +------------------------+------------------------------------------------------+
  221. | client.subscribe.after | Run After client subscribed topics |
  222. +------------------------+------------------------------------------------------+
  223. | client.unsubscribe | Run when client unsubscribes topics |
  224. +------------------------+------------------------------------------------------+
  225. | message.publish | Run when a MQTT message is published |
  226. +------------------------+------------------------------------------------------+
  227. | message.delivered | Run when a MQTT message is delivered |
  228. +------------------------+------------------------------------------------------+
  229. | message.acked | Run when a MQTT message is acked |
  230. +------------------------+------------------------------------------------------+
  231. | client.disconnected | Run when client disconnected from broker |
  232. +------------------------+------------------------------------------------------+
  233. The emqttd broker uses the `Chain-of-responsibility_pattern`_ to implement hook mechanism. The callback functions registered to hook will be executed one bye one::
  234. -------- ok | {ok, NewAcc} -------- ok | {ok, NewAcc} --------
  235. (Args, Acc) --> | Fun1 | -------------------> | Fun2 | -------------------> | Fun3 | --> {ok, Acc} | {stop, Acc}
  236. -------- -------- --------
  237. | | |
  238. stop | {stop, NewAcc} stop | {stop, NewAcc} stop | {stop, NewAcc}
  239. The callback function for hook should return:
  240. +-----------------+------------------------+
  241. | Return | Description |
  242. +=================+========================+
  243. | ok | Continue |
  244. +-----------------+------------------------+
  245. | {ok, NewAcc} | Return Acc and Continue|
  246. +-----------------+------------------------+
  247. | stop | Break |
  248. +-----------------+------------------------+
  249. | {stop, NewAcc} | Return Acc and Break |
  250. +-----------------+------------------------+
  251. The input arguments for a callback function is different depending on the type of hook. Clone the `emqttd_plugin_template`_ to check how to use hooks.
  252. Hook Implementation
  253. -------------------
  254. The hook APIs defined in emqttd module:
  255. .. code:: erlang
  256. -module(emqttd).
  257. %% Hooks API
  258. -export([hook/4, hook/3, unhook/2, run_hooks/3]).
  259. hook(Hook :: atom(), Callback :: function(), InitArgs :: list(any())) -> ok | {error, any()}.
  260. hook(Hook :: atom(), Callback :: function(), InitArgs :: list(any()), Priority :: integer()) -> ok | {error, any()}.
  261. unhook(Hook :: atom(), Callback :: function()) -> ok | {error, any()}.
  262. run_hooks(Hook :: atom(), Args :: list(any()), Acc :: any()) -> {ok | stop, any()}.
  263. And implemented in emqttd_hook module:
  264. .. code:: erlang
  265. -module(emqttd_hook).
  266. %% Hooks API
  267. -export([add/3, add/4, delete/2, run/3, lookup/1]).
  268. add(HookPoint :: atom(), Callback :: function(), InitArgs :: list(any())) -> ok.
  269. add(HookPoint :: atom(), Callback :: function(), InitArgs :: list(any()), Priority :: integer()) -> ok.
  270. delete(HookPoint :: atom(), Callback :: function()) -> ok.
  271. run(HookPoint :: atom(), Args :: list(any()), Acc :: any()) -> any().
  272. lookup(HookPoint :: atom()) -> [#callback{}].
  273. Hook Usage
  274. ----------
  275. `emqttd_plugin_template`_ provides the examples for hook usage:
  276. .. code:: erlang
  277. -module(emqttd_plugin_template).
  278. -export([load/1, unload/0]).
  279. -export([on_message_publish/2, on_message_delivered/3, on_message_acked/3]).
  280. load(Env) ->
  281. emqttd:hook('message.publish', fun ?MODULE:on_message_publish/2, [Env]),
  282. emqttd:hook('message.delivered', fun ?MODULE:on_message_delivered/3, [Env]),
  283. emqttd:hook('message.acked', fun ?MODULE:on_message_acked/3, [Env]).
  284. on_message_publish(Message, _Env) ->
  285. io:format("publish ~s~n", [emqttd_message:format(Message)]),
  286. {ok, Message}.
  287. on_message_delivered(ClientId, Message, _Env) ->
  288. io:format("delivered to client ~s: ~s~n", [ClientId, emqttd_message:format(Message)]),
  289. {ok, Message}.
  290. on_message_acked(ClientId, Message, _Env) ->
  291. io:format("client ~s acked: ~s~n", [ClientId, emqttd_message:format(Message)]),
  292. {ok, Message}.
  293. unload() ->
  294. emqttd:unhook('message.publish', fun ?MODULE:on_message_publish/2),
  295. emqttd:unhook('message.acked', fun ?MODULE:on_message_acked/3),
  296. emqttd:unhook('message.delivered', fun ?MODULE:on_message_delivered/3).
  297. .. _design_plugin:
  298. -------------
  299. Plugin Design
  300. -------------
  301. Plugin is a normal erlang application that could be started/stopped dynamically by a running emqttd broker.
  302. emqttd_plugins Module
  303. ---------------------
  304. The plugin mechanism is implemented by emqttd_plugins module::
  305. -module(emqttd_plugins).
  306. -export([load/1, unload/1]).
  307. %% @doc Load a Plugin
  308. load(PluginName :: atom()) -> ok | {error, any()}.
  309. %% @doc UnLoad a Plugin
  310. unload(PluginName :: atom()) -> ok | {error, any()}.
  311. Load a Plugin
  312. -------------
  313. Use './bin/emqttd_ctl' CLI to load/unload a plugin::
  314. ./bin/emqttd_ctl plugins load emqttd_plugin_redis
  315. ./bin/emqttd_ctl plugins unload emqttd_plugin_redis
  316. Plugin Template
  317. ---------------
  318. http://github.com/emqtt/emqttd_plugin_template
  319. .. _eSockd: https://github.com/emqtt/esockd
  320. .. _Chain-of-responsibility_pattern: https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
  321. .. _emqttd_plugin_template: https://github.com/emqtt/emqttd_plugin_template/blob/master/src/emqttd_plugin_template.erl