ipc-high.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. The IPC protocol
  2. ================
  3. While the cc-protocol.txt describes the low-level primitives, here we
  4. describe how the whole IPC should work and how to use it.
  5. Definitions
  6. -----------
  7. system::
  8. The system that moves data between the users and does bookkeeping.
  9. In our current implementation, it is implemented as the MsgQ daemon,
  10. which the users connect to and it routes the data.
  11. user::
  12. Usually a process; generally an entity that wants to communicate
  13. with the other users.
  14. session::
  15. Session is the interface by which the user communicates with the
  16. system. Single user may have multiple sessions, a session belongs to
  17. single user.
  18. message::
  19. A data blob sent by one user. The recipient might be the system
  20. itself, other session or set of sessions (called group, see below,
  21. it is possibly empty). Message is either a response or an original
  22. message (TODO: Better name?).
  23. group::
  24. A named set of sessions. Conceptually, all the possible groups
  25. exist, there's no explicit creation and deletion of groups.
  26. session id::
  27. Unique identifier of a session. It is not reused for the whole
  28. lifetime of the system. Historically called `lname` in the code.
  29. undelivery signal::
  30. While sending an original message, a client may request an
  31. undelivery signal. If the recipient specification yields no
  32. sessions to deliver the message to, the system informs user about
  33. the situation.
  34. sequence number::
  35. Each message sent through the system carries a sequence number. The
  36. number should be unique per sender. It can be used to pair a
  37. response to the original message, since the response specifies which
  38. sequence number had the message it response to. Even responses and
  39. messages not expecting answer have their sequence number, but it is
  40. generally unused.
  41. non-blocking operation::
  42. Operation that will complete without waiting for anything.
  43. fast operation::
  44. Operation that may wait for other process, but only for a very short
  45. time. Generally, this includes communication between the user and
  46. system, but not between two clients. It can be expected to be fast
  47. enough to use this inside an interactive session, but may be too
  48. heavy in the middle of query processing, for example. Every
  49. non-blocking operation is considered fast.
  50. The session
  51. -----------
  52. The session interface allows for several operations interacting with
  53. the system. In the code, it is represented by a class.
  54. Possible operations include:
  55. Opening a session::
  56. The session is created and connects to the system. This operation is
  57. fast. The session receives session id from the system.
  58. Group management::
  59. A user may subscribe (become member) of a group, or unsubscribe from
  60. a group. These are fast operations.
  61. Send::
  62. A user may send a message, addressed to the system, or other
  63. session(s). This operation is expected to be non-blocking
  64. (current implementation is based on assumption of how OS handles the
  65. sends, which may need to be revisited if it turns out to be false).
  66. Receive synchronously::
  67. User may wait for an incoming message in blocking mode. It is
  68. possible to specify the kind of message to wait for, either original
  69. message or response to a message. This interface has a timeout.
  70. Receive asynchronously::
  71. Similar to previous, but non-blocking. It terminates immediately.
  72. The user provides a callback that is invoked when the requested
  73. message arrives.
  74. Terminate::
  75. A session may be terminated. No more messages are sent or received
  76. over it, the session is automatically unsubscribed from all the
  77. groups. This operation is non-blocking. A session is terminated
  78. automatically if the user exits.
  79. Assumptions
  80. -----------
  81. We assume reliability and order of delivery. Messages sent from user A
  82. to B are all delivered unchanged in original order as long as B
  83. exists.
  84. All above operations are expected to always succeed. If there's an
  85. error reported, it should be considered fatal and user should
  86. exit. In case a user still wants to continue, the session must be
  87. considered terminated and a new one must be created. Care must be
  88. taken not to use any information obtained from the previous session,
  89. since the state in other users and the system may have changed during
  90. the reconnect.
  91. Addressing
  92. ----------
  93. Addressing happens in three ways:
  94. By group name::
  95. The message is routed to all the sessions subscribed to this group.
  96. It is legal to address an empty group; such message is then
  97. delivered to no sessions.
  98. By session ID::
  99. The message is sent to the single session, if it is still alive.
  100. By an alias::
  101. A session may have any number of aliases - well known names. Only
  102. single session may hold given alias (but it is not yet enforced by
  103. the system). The message is delivered to the one session owning the
  104. alias, if any. Internally, the aliases are implemented as groups
  105. with single subscribed session, so it is the same as the first
  106. option on the protocol level, but semantically it is different.
  107. The system
  108. ----------
  109. The system performs these goals:
  110. * Maintains the open sessions and allows creating new ones.
  111. * Keeps information about groups and which sessions are subscribed to
  112. which group.
  113. * Routes the messages between users.
  114. Also, the system itself is a user of the system. It can be reached by
  115. the alias `Msgq` and provides following high-level services (see
  116. below):
  117. Notifications about sessions::
  118. When a session is opened to the system or when a session is
  119. terminated, a notification is sent to interested users. The
  120. notification contains the session ID of the session in question.
  121. The termination notification is probably more useful (if a user
  122. communicated with a given session before, it might be interested it
  123. is no longer available), the opening notification is provided mostly
  124. for completeness.
  125. Notifications about group subscriptions::
  126. When a session subscribes to a group or unsubscribes from a group, a
  127. notification is sent to interested users. The notification contains
  128. both the session ID of the session subscribing/unsubscribing and
  129. name of the group. This includes notifications about aliases (since
  130. aliases are groups internally).
  131. Commands to list sessions::
  132. There's a command to list session IDs of all currently opened sessions
  133. and a command to list session IDs of all sessions subscribed to a
  134. given group. Note that using these lists might need some care, as
  135. the information might be outdated at the time it is delivered to the
  136. user.
  137. User shows interest in notifications about sessions and group
  138. subscriptions by subscribing to a group with well-known name (as with
  139. any notification).
  140. Note that due to implementation details, the `Msgq` alias is not yet
  141. available during early stage of the bootstrap of bind10 system. This
  142. means some very core services can't rely on the above services of the
  143. system. The alias is guaranteed to be working before the first
  144. non-core module is started.
  145. Higher-level services
  146. ---------------------
  147. While the system is able to send any kind of data, the payload sent by
  148. users in bind10 is structured data encoded as JSON. The messages sent
  149. are of three general types:
  150. Command::
  151. A message sent to single destination, with the undeliverable
  152. signal turned on and expecting an answer. This is a request
  153. to perform some operation on the recipient (it can have side effects
  154. or not). The command is identified by a name and it can have
  155. parameters. A command with the same name may behave differently (or
  156. have different parameters) on different receiving users.
  157. Reply::
  158. An answer to the `Command`. It is sent directly to the session where
  159. the command originated from, does not expect further answer and the
  160. undeliverable notification is not set. It either confirms the
  161. command was run successfully and contains an optional result, or
  162. notifies the sender of failure to run the command. Success and
  163. failure differ only in the payload sent through the system, not in
  164. the way it is sent. The undeliverable signal is failure
  165. reply sent by the system on behalf of the missing recipient.
  166. Notification::
  167. A message sent to any number of destinations (eg. sent to a group),
  168. not expecting an answer. It notifies other users about an event or
  169. change of state.
  170. Details of the higher-level
  171. ---------------------------
  172. While there are libraries implementing the communication in convenient
  173. way, it is useful to know what happens inside.
  174. The notifications are probably the simplest. Users interested in
  175. receiving notifications of some family subscribe to corresponding
  176. group. Then, a client sends a message to the group. For example, if
  177. clients `receiver-A` and `receiver-B` want to receive notifications
  178. about changes to zone data, they'd subscribe to the
  179. `Notifications/ZoneUpdates` group. Then, other client (let's say
  180. `XfrIn`, with session ID `s12345`) would send something like:
  181. s12345 -> Notifications/ZoneUpdates
  182. {"notification": ["zone-update", {
  183. "class": "IN",
  184. "origin": "example.org.",
  185. "serial": 123456
  186. }]}
  187. Both receivers would receive the message and know that the
  188. `example.org` zone is now at version 123456. Note that multiple users
  189. may produce the same kind of notification. Also, single group may be
  190. used to send multiple notification names (but they should be related;
  191. in our example, the `Notifications/ZoneUpdates` could be used for
  192. `zone-update`, `zone-available` and `zone-unavailable` notifications
  193. for change in zone data, configuration of new zone in the system and
  194. removal of a zone from configuration).
  195. Sending a command to single recipient is slightly more complex. The
  196. sending user sends a message to the receiving one, addressed either by
  197. session ID or by an alias (group to which at most one session may be
  198. subscribed). The message contains the name of the command and
  199. parameters. It is sent with the undeliverable signals turned on.
  200. The user also starts a timer (with reasonably long timeout). The
  201. sender also subscribes to notifications about terminated sessions or
  202. unsubscription from the alias group.
  203. The receiving user gets the message, runs the command and sends a
  204. response back, with the result. The response has the undeliverable
  205. signal turned off and it is marked as response to the message
  206. containing the command. The sending user receives the answer and pairs
  207. it with the command.
  208. There are several things that may go wrong.
  209. * There might be an error on the receiving user (bad parameters, the
  210. operation failed, the recipient doesn't know command of that name).
  211. The receiving side sends the response as previous, the only
  212. difference is the content of the payload. The sending user is
  213. notified about it, without delays.
  214. * The recipient user doesn't exist (either the session ID is wrong or
  215. terminated already, or the alias is empty). The system sends a
  216. failure response and the sending user knows immediately the command
  217. failed.
  218. * The recipient disconnects while processing the command (possibly
  219. crashes). The sender gets a notification about disconnection or
  220. unsubscription from the alias group and knows the answer won't come.
  221. * The recipient ``blackholes'' the command. It receives it, but never
  222. answers. The timeout in sender times out. As this is a serious
  223. programmer error in the recipient and should be rare, the sender
  224. should at least log an error to notify about the case.
  225. One example would be asking the question of life, universe and
  226. everything (all the examples assume the sending user is already
  227. subscribed to the notifications):
  228. s12345 -> DeepThought
  229. {"command": ["question", {
  230. "what": ["Life", "Universe", "*"]
  231. }]}
  232. s23456 -> s12345
  233. {"reply": [0, 42]}
  234. The deep thought had an alias. But the answer is sent from its session
  235. ID. The `0` in the reply means ``success''.
  236. Another example might be asking for some data at a bureau and getting
  237. an error:
  238. s12345 -> Burreau
  239. {"command": ["provide-information", {
  240. "about": "me",
  241. "topic": "taxes"
  242. }]}
  243. s23456 -> s12345
  244. {"reply": [1, "You need to fill in other form"]}
  245. And, in this example, the sender is trying to reach an non-existent
  246. session. The `msgq` here is not the alias `Msgq`, but a special
  247. ``phantom'' session ID that is not listed anywhere.
  248. s12345 -> s0
  249. {"command": ["ping"]}
  250. msgq -> s12345
  251. {"reply": [-1, "No such recipient"]}
  252. Last, an example when the other user disconnects while processing the
  253. command.
  254. s12345 -> s23456
  255. {"command": ["shutdown"]}
  256. msgq -> s12345
  257. {"notification": ["disconnected", {
  258. "lname": "s23456"
  259. }]}
  260. The system does not support sending a command to multiple users
  261. directly. It can be accomplished as this:
  262. * The sending user calls a command on the system to get list of
  263. sessions in given group. This is command to alias, so it can be done
  264. by the previous way.
  265. * After receiving the list of session IDs, multiple copies of the
  266. command are sent by the sending user, one to each of the session
  267. IDs.
  268. * Successes and failures are handled the same as above, since these
  269. are just single-recipient commands.
  270. So, this would be an example with unhelpful war council.
  271. s12345 -> Msgq
  272. {"command": ["get-subscriptions", {
  273. "group": "WarCouncil"
  274. }]}
  275. msgq -> s12345
  276. {"reply": [0, ["s1", "s2", "s3"]]}
  277. s12345 -> s1
  278. {"command": ["advice", {
  279. "topic": "Should we attack?"
  280. }]}
  281. s12345 -> s2
  282. {"command": ["advice", {
  283. "topic": "Should we attack?"
  284. }]}
  285. s12345 -> s3
  286. {"command": ["advice", {
  287. "topic": "Should we attack?"
  288. }]}
  289. s1 -> s12345
  290. {"reply": [0, true]}
  291. s2 -> s12345
  292. {"reply": [0, false]}
  293. s3 -> s12345
  294. {"reply": [1, "Advice feature not implemented"]}
  295. Users
  296. -----
  297. While there's a lot of flexibility for the behaviour of a user, it
  298. usually comes to something like this (during the lifetime of the
  299. user):
  300. * The user starts up.
  301. * Then it creates one or more sessions (there may be technical reasons
  302. to have more than one session, such as threads, but it is not
  303. required by the system).
  304. * It subscribes to some groups to receive notifications in future.
  305. * It binds to some aliases if it wants to be reachable by others by a
  306. nice name.
  307. * It invokes some start-up commands (to get the configuration, for
  308. example).
  309. * During the lifetime, it listens for notifications and answers
  310. commands. It also invokes remote commands and sends notifications
  311. about things that are happening.
  312. * Eventually, the user terminates, closing all the sessions it had
  313. opened.
  314. Known limitations
  315. -----------------
  316. It is meant mostly as signalling protocol. Sending millions of
  317. messages or messages of several tens of megabytes is probably a bad
  318. idea. While there's no architectural limitation with regards of the
  319. number of transferred messages and the maximum size of message is 4GB,
  320. the code is not optimised and it would probably be very slow.
  321. We currently expect the system not to be at heavy load. Therefore, we
  322. expect the system to keep up with users sending messages. The
  323. libraries write in blocking mode, which is no problem if the
  324. expectation is true, as the write buffers will generally be empty and
  325. the write wouldn't block, but if it turns out it is not the case, we
  326. might need to reconsider.