summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_stream/docs/PROTOCOL.adoc
blob: 14e149664c328a7b631f7486ca0267818bc524eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
= RabbitMQ Stream Protocol Reference

This is the reference of the RabbitMQ stream protocol. Note the protocol
is still under development and is subject to change.

The https://github.com/rabbitmq/rabbitmq-stream-java-client[RabbitMQ stream Java client]
is currently the reference implementation.

== Types

int8, int16, int32, int64 - Signed integers (big endian order)

uint8, uint16, uint32, uint64 - Unsigned integers (big endian order)

bytes - int32 for the length followed by the bytes of content, length of -1 indicates null.

string - int16 for the length followed by the bytes of content, length of -1 indicates null.

arrays - int32 for the length followed by the repetition of the structure, notation uses [], e.g.
[int32] for an array of int32.

== Frame Structure

```
Frame => Size (Request | Response | Command)
  Size => int32 (size without the 4 bytes of the size element)

Request => Key Version (CorrelationId) Content
  Key => int16
  Version => int16
  CorrelationId => int32
  Command => bytes // see command details below

Response => Key Version CorrelationId ResponseCode
  Key => int16
  Version => int16
  CorrelationId => int32
  ResponseCode => int16

Command => Key Version Content
  Key => int16
  Version => int16
  Content => bytes // see command details below
```

Most commands are request/reply, but some commands (e.g. `Deliver`) are one-direction only and thus
doest not contain a correlation ID.

== Commands

.Stream Protocol Commands
|===
|Command |From |Key | Expects response?

|<<publish>>
|Client
|0
|No

|<<publishconfirm>>
|Server
|1
|No

|<<subscribe>>
|Client
|2
|Yes

|<<deliver>>
|Server
|3
|No

|<<credit>>
|Client
|4
|No

|<<unsubscribe>>
|Client
|5
|Yes

|<<publisherror>>
|Server
|6
|No

|<<metadataupdate>>
|Server
|7
|No

|<<metadata>>
|Client
|8
|No

|<<saslhandshake>>
|Client
|9
|Yes

|<<saslauthenticate>>
|Client
|10
|Yes

|<<tune>>
|Server
|11
|Yes

|<<open>>
|Server
|12
|Yes

|<<close>>
|Client & Server
|13
|Yes

|<<heartbeat>>
|Client & Server
|14
|No

|<<peerproperties>>
|Client
|15
|Yes

|<<commitoffset>>
|Client
|16
|No

|<<queryoffset>>
|Client
|17
|Yes

|<<create>>
|Client
|998
|Yes

|<<delete>>
|Client
|999
|Yes
|===

=== Publish

```
Publish => Key Version Stream PublishedMessages
  Key => int16 // 0
  Version => int16
  Stream => string // the name of the stream
  PublisherId => uint8
  PublishedMessages => [PublishedMessage]
  PublishedMessage => PublishingId Message
  PublishingId => int64
  Message => bytes
```

=== PublishConfirm

```
PublishConfirm => Key Version PublishingIds
  Key => int16 // 1
  Version => int16
  PublisherId => uint8
  PublishingIds => [int64] // to correlate with the messages sent
```

=== Subscribe

```
Subscribe => Key Version CorrelationId SubscriptionId Stream OffsetSpecification Credit
  Key => int16 // 2
  Version => int16
  CorrelationId => int32 // correlation id to correlate the response
  SubscriptionId => uint8 // client-supplied id to identify the subscription
  Stream => string // the name of the stream
  OffsetSpecification => OffsetType Offset
  OffsetType => int16 // 0 (first), 1 (last), 2 (next), 3 (offset), 4 (timestamp)
  Offset => uint64 (for offset) | int64 (for timestamp)
  Credit => int16
```

=== Deliver

```
Deliver => Key Version SubscriptionId OsirisChunk
  Key => int16 // 3
  Version => int32
  SubscriptionId => uint8
  OsirisChunk => MagicVersion NumEntries NumRecords Epoch ChunkFirstOffset ChunkCrc DataLength Messages
  MagicVersion => int8
  NumEntries => uint16
  NumRecords => uint32
  Epoch => uint64
  ChunkFirstOffset => uint64
  ChunkCrc => int32
  DataLength => uint32
  Messages => [Message] // no int32 for the size for this array
  Message => EntryTypeAndSize
  Data => bytes
```

NB: See the https://github.com/rabbitmq/osiris/blob/348db0528986d6025b823bcf1ae0570aa63f5e25/src/osiris_log.erl#L49-L81[Osiris project]
for details on the structure of messages.

=== Credit

```
Credit => Key Version SubscriptionId Credit
  Key => int16 // 4
  Version => int16
  SubscriptionId => int8
  Credit => int16 // the number of chunks that can be sent

CreditResponse => Key Version ResponseCode SubscriptionId
  Key => int16 // 4
  Version => int16
  ResponseCode => int16
  SubscriptionId => int8
```

NB: the server sent a response only in case of problem, e.g. crediting an unknown subscription.

=== Unsubscribe

```
Unsubscribe => Key Version CorrelationId SubscriptionId
  Key => int16 // 5
  Version => int16
  CorrelationId => int32
  SubscriptionId => int8
```

=== PublishError

```
PublishError => Key Version [PublishingError]
  Key => int16 // 6
  Version => int16
  PublisherId => int8
  PublishingError => PublishingId Code
  PublishingId => int64
  Code => int16 // code to identify the problem
```

=== MetadataUpdate

```
MetadataUpdate => Key Version MetadataInfo
  Key => int16 // 7
  Version => int16
  MetadataInfo => Code Stream
  Code => int16 // code to identify the information
  Stream => string // the stream implied
```

=== Metadata

```
MetadataQuery => Key Version CorrelationId [Stream]
  Key => int16 // 8
  Version => int16
  CorrelationId => int32
  Stream => string

MetadataResponse => Key Version CorrelationId [Broker] [StreamMetadata]
  Key => int16 // 8
  Version => int16
  CorrelationId => int32
  Broker => Reference Host Port
    Reference => int16
    Host => string
    Port => int32
  StreamMetadata => StreamName LeaderReference ReplicasReferences
     StreamName => string
     ResponseCode => int16
     LeaderReference => int16
     ReplicasReferences => [int16]
```

=== SaslHandshake

```
SaslHandshakeRequest => Key Version CorrelationId Mechanism
  Key => int16 // 9
  Version => int16
  CorrelationId => int32

SaslHandshakeResponse => Key Version CorrelationId ResponseCode [Mechanism]
  Key => int16 // 9
  Version => int16
  CorrelationId => int32
  ResponseCode => int16
  Mechanism => string
```

=== SaslAuthenticate

```
SaslAuthenticateRequest => Key Version CorrelationId Mechanism SaslOpaqueData
  Key => int16 // 10
  Version => int16
  CorrelationId => int32
  Mechanism => string
  SaslOpaqueData => bytes

SaslAuthenticateResponse => Key Version CorrelationId ResponseCode SaslOpaqueData
  Key => int16 // 10
  Version => int16
  CorrelationId => int32
  ResponseCode => int16
  SaslOpaqueData => bytes
```

=== Tune

```
TuneRequest => Key Version FrameMax Heartbeat
  Key => int16 // 11, to identify the command
  Version => int16
  FrameMax => int32 // in bytes, 0 means no limit
  Heartbeat => int32 // in seconds, 0 means no heartbeat

TuneResponse => TuneRequest
```

=== Open

```
OpenRequest => Key Version CorrelationId VirtualHost
  Key => int16 // 12
  Version => int16
  CorrelationId => int32
  VirtualHost => string

OpenResponse => Key Version CorrelationId ResponseCode
  Key => int16 // 12
  Version => int16
  CorrelationId => int32
  ResponseCode => int16
```

=== Close

```
CloseRequest => Key Version CorrelationId ClosingCode ClosingReason
  Key => int16 // 13
  Version => int16
  CorrelationId => int32
  ClosingCode => int16
  ClosingReason => string

CloseResponse => Key Version CorrelationId ResponseCode
  Key => int16 // 13
  Version => int16
  CorrelationId => int32
  ResponseCode => int16
```

=== Heartbeat

```
Heartbeat => Key Version
  Key => int16 // 14
  Version => int16
```

=== PeerProperties

```
PeerPropertiesRequest => Key Version PeerProperties
  Key => int16 // 15
  Version => int16
  CorrelationId => int32
  PeerProperties => [PeerProperty]
  PeerProperty => Key Value
  Key => string
  Value => string

SaslAuthenticateResponse => Key Version CorrelationId ResponseCode PeerProperties
  Key => int16 // 15
  Version => int16
  CorrelationId => int32
  ResponseCode => int16
  PeerProperties => [PeerProperty]
  PeerProperty => Key Value
  Key => string
  Value => string
```

=== CommitOffset

```
CommitOffset => Key Version Reference Stream Offset
  Key => int16 // 16
  Version => int16
  CorrelationId => int32 // not used yet
  Reference => string // max 256 characters
  SubscriptionId => uint8
  Offset => int64
```

=== QueryOffset

```
QueryOffsetRequest => Key Version CorrelationId Reference Stream
  Key => int16 // 17
  Version => int16
  CorrelationId => int32
  Reference => string // max 256 characters
  Stream => string

QueryOffsetResponse => Key Version CorrelationId Reference Stream
  Key => int16 // 17
  Version => int16
  CorrelationId => int32
  ResponseCode => int16
  Offset => int64
```

=== Create

```
Create => Key Version CorrelationId Stream Arguments
  Key => int16 // 998
  Version => int16
  CorrelationId => int32
  Stream => string
  Arguments => [Argument]
  Argument => Key Value
  Key => string
  Value => string
```

=== Delete

```
Delete => Key Version CorrelationId Stream
  Key => int16 // 999
  Version => int16
  CorrelationId => int32
  Stream => string
```

== Authentication

Once a client is connected to the server, it initiates an authentication
sequence. The next figure shows the steps of the sequence:

[ditaa]
.Authentication Sequence
....
Client                      Server
  +                           +
  | Peer Properties Exchange  |
  |-------------------------->|
  |<--------------------------|
  |                           |
  |      SASL Handshake       |
  |-------------------------->|
  |<--------------------------|
  |                           |
  |     SASL Authenticate     |
  |-------------------------->|
  |<--------------------------|
  |                           |
  |           Tune            |
  |<--------------------------|
  |-------------------------->|
  |                           |
  |           Open            |
  |-------------------------->|
  |<--------------------------|
  |                           |
  +                           +
....

* SaslHandshake: the client asks about the SASL mechanisms the server supports. It
can then pick one from the list the server returns.
* SaslAuthenticate: the client answers to the server's challenge(s), using the
SASL mechanism it picked. The server will send a `Tune` frame once it is satisfied
with the client authentication response.
* Tune: the server sends a `Tune` frame to suggest some settings (max frame size, heartbeat).
The client answers with a `Tune` frame with the settings he agrees on, possibly adjusted
from the server's suggestions.
* Open: the client sends an `Open` frame to pick a virtual host to connect to. The server
answers whether it accepts the access or not.