-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathheader.rs
More file actions
290 lines (248 loc) · 8.66 KB
/
Copy pathheader.rs
File metadata and controls
290 lines (248 loc) · 8.66 KB
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
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH.
// All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.
use crate::CommandType;
use std::fmt;
const COMMAND_ENTRY_MASK: u16 = 0x0400;
const NOTIFICATION_ENTRY_MASK: u16 = 0x8000;
const CUSTOM_ENTRY_MASK: u16 = 0xFC00;
const REQUESTED_ACK_MASK: u64 = 0x8000_0000_0000;
type MessageTypeId = u16;
#[derive(Debug, thiserror::Error)]
#[error("unknown protocol.message code {0:#x}")]
pub struct UnknownMessageType(u16);
// This macro generates the MessageKind enum, together with the conversions back and forth to MessageTypeId
macro_rules! gen_message_type_enum {
(@gen_enum [] -> [$($body:tt)*]) => {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MessageType {
$($body)*
CustomEntry(u16)
}
};
(@gen_enum [$variant:ident = $id:literal, $($tail:tt)*] -> [$($body:tt)*]) => {
gen_message_type_enum!(@gen_enum [$($tail)*] -> [$variant, $($body)*]);
};
(@gen_id [] -> [$($variant:ident, $id:literal,)*]) => {
impl TryFrom<MessageTypeId> for MessageType {
type Error = UnknownMessageType;
fn try_from(value: MessageTypeId) -> Result<Self, UnknownMessageType> {
match value {
$($id => Ok(MessageType::$variant),)*
v if (v & CUSTOM_ENTRY_MASK) != 0 => Ok(MessageType::CustomEntry(v)),
v => Err(UnknownMessageType(v)),
}
}
}
impl From<MessageType> for MessageTypeId {
fn from(mt: MessageType) -> Self {
match mt {
$(MessageType::$variant => $id,)*
MessageType::CustomEntry(id) => id
}
}
}
};
(@gen_id [$variant:ident = $id:literal, $($tail:tt)*] -> [$($body:tt)*]) => {
gen_message_type_enum!(@gen_id [$($tail)*] -> [$variant, $id, $($body)*]);
};
// Entrypoint of the macro
($($tokens:tt)*) => {
gen_message_type_enum!(@gen_enum [$($tokens)*] -> []);
gen_message_type_enum!(@gen_id [$($tokens)*] -> []);
};
}
gen_message_type_enum!(
Start = 0x0000,
Suspension = 0x0001,
Error = 0x0002,
End = 0x0003,
ProposeRunCompletion = 0x0005,
AwaitingOn = 0x0006,
ProposeRunCompletionAck = 0x0007,
InputCommand = 0x0400,
OutputCommand = 0x0401,
GetLazyStateCommand = 0x0402,
GetLazyStateCompletionNotification = 0x8002,
SetStateCommand = 0x0403,
ClearStateCommand = 0x0404,
ClearAllStateCommand = 0x0405,
GetLazyStateKeysCommand = 0x0406,
GetLazyStateKeysCompletionNotification = 0x8006,
GetEagerStateCommand = 0x0407,
GetEagerStateKeysCommand = 0x0408,
GetPromiseCommand = 0x0409,
GetPromiseCompletionNotification = 0x8009,
PeekPromiseCommand = 0x040A,
PeekPromiseCompletionNotification = 0x800A,
CompletePromiseCommand = 0x040B,
CompletePromiseCompletionNotification = 0x800B,
SleepCommand = 0x040C,
SleepCompletionNotification = 0x800C,
CallCommand = 0x040D,
CallInvocationIdCompletionNotification = 0x800E,
CallCompletionNotification = 0x800D,
OneWayCallCommand = 0x040E,
SendSignalCommand = 0x0410,
RunCommand = 0x0411,
RunCompletionNotification = 0x8011,
AttachInvocationCommand = 0x0412,
AttachInvocationCompletionNotification = 0x8012,
GetInvocationOutputCommand = 0x0413,
GetInvocationOutputCompletionNotification = 0x8013,
CompleteAwakeableCommand = 0x0414,
SignalNotification = 0xFBFF,
);
impl MessageType {
fn id(&self) -> MessageTypeId {
MessageTypeId::from(*self)
}
pub fn is_command(&self) -> bool {
(COMMAND_ENTRY_MASK..NOTIFICATION_ENTRY_MASK).contains(&self.id())
}
pub fn is_notification(&self) -> bool {
(NOTIFICATION_ENTRY_MASK..CUSTOM_ENTRY_MASK).contains(&self.id())
}
/// Returns true if this message type carries the `requires_ack` flag in its header.
pub fn allows_ack(&self) -> bool {
matches!(self, MessageType::ProposeRunCompletion)
}
}
impl fmt::Display for MessageType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match CommandType::try_from(*self) {
Ok(ct) => write!(f, "{ct}"),
Err(mt) => write!(f, "{mt:?}"),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct MessageHeader {
ty: MessageType,
length: u32,
requested_ack_flag: Option<bool>,
}
impl MessageHeader {
#[inline]
pub fn new(ty: MessageType, length: u32) -> Self {
Self {
ty,
length,
requested_ack_flag: None,
}
}
#[inline]
pub fn new_with_requested_ack(ty: MessageType, requested_ack: bool, length: u32) -> Self {
Self {
ty,
length,
requested_ack_flag: Some(requested_ack),
}
}
#[inline]
pub fn message_type(&self) -> MessageType {
self.ty
}
#[inline]
pub fn message_length(&self) -> u32 {
self.length
}
/// `Some(_)` when the message type carries the `requires_ack` flag, `None` otherwise.
#[inline]
pub fn requested_ack(&self) -> Option<bool> {
self.requested_ack_flag
}
}
impl TryFrom<u64> for MessageHeader {
type Error = UnknownMessageType;
/// Deserialize the protocol header.
/// See https://github.com/restatedev/service-protocol/blob/main/service-invocation-protocol.md#message-header
fn try_from(value: u64) -> Result<Self, Self::Error> {
let ty_code = (value >> 48) as u16;
let ty: MessageType = ty_code.try_into()?;
let length = value as u32;
let requested_ack_flag = if ty.allows_ack() {
Some((value & REQUESTED_ACK_MASK) != 0)
} else {
None
};
Ok(MessageHeader {
ty,
length,
requested_ack_flag,
})
}
}
impl From<MessageHeader> for u64 {
/// Serialize the protocol header.
/// See https://github.com/restatedev/service-protocol/blob/main/service-invocation-protocol.md#message-header
fn from(message_header: MessageHeader) -> Self {
let mut res =
((u16::from(message_header.ty) as u64) << 48) | (message_header.length as u64);
if let Some(true) = message_header.requested_ack_flag {
res |= REQUESTED_ACK_MASK;
}
res
}
}
#[cfg(test)]
mod tests {
use super::{MessageType::*, *};
macro_rules! roundtrip_test {
($test_name:ident, $header:expr, $ty:expr, $len:expr) => {
#[test]
fn $test_name() {
let serialized: u64 = $header.into();
let header: MessageHeader = serialized.try_into().unwrap();
assert_eq!(header.message_type(), $ty);
assert_eq!(header.message_length(), $len);
}
};
}
roundtrip_test!(
get_state_empty,
MessageHeader::new(GetLazyStateCommand, 0),
GetLazyStateCommand,
0
);
roundtrip_test!(
get_state_with_length,
MessageHeader::new(GetLazyStateCommand, 22),
GetLazyStateCommand,
22
);
roundtrip_test!(
custom_entry,
MessageHeader::new(CustomEntry(0xFC01), 10341),
CustomEntry(0xFC01),
10341
);
#[test]
fn propose_run_completion_with_requested_ack_roundtrip() {
let original = MessageHeader::new_with_requested_ack(ProposeRunCompletion, true, 42);
let serialized: u64 = original.into();
// The flag bit must be set in the wire form.
assert_ne!(serialized & REQUESTED_ACK_MASK, 0);
let header: MessageHeader = serialized.try_into().unwrap();
assert_eq!(header.message_type(), ProposeRunCompletion);
assert_eq!(header.message_length(), 42);
assert_eq!(header.requested_ack(), Some(true));
}
#[test]
fn propose_run_completion_without_requested_ack_roundtrip() {
let original = MessageHeader::new(ProposeRunCompletion, 42);
let serialized: u64 = original.into();
assert_eq!(serialized & REQUESTED_ACK_MASK, 0);
let header: MessageHeader = serialized.try_into().unwrap();
assert_eq!(header.message_type(), ProposeRunCompletion);
assert_eq!(header.message_length(), 42);
// ProposeRunCompletion carries the flag; when not set on the wire it decodes as Some(false).
assert_eq!(header.requested_ack(), Some(false));
}
}