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
use crate::api::ChannelError;
use crate::api::ChannelError::{IncompatibleVersion, InvalidCapacity, InvalidMaxMessageLength, InvalidSignature};
use crate::tick::TickUnit;
use crate::utils::{align, is_aligned, REC_HEADER_LEN};
use crate::version::Version;
use std::cmp::max;
use std::cmp::min;
const MIN_CAPACITY: u32 = 1024 * 16;
const HEADER_LEN: usize = 128;
const SIGNATURE: u64 = 0x2A54_4942_4B45_4B2A;
#[inline]
const fn compute_max_msg_len(capacity: u32) -> u32 {
(capacity >> 7) - (REC_HEADER_LEN as u32)
}
#[derive(PartialEq, Eq, Debug)]
pub struct Header {
writer_id: u64,
channel_id: u64,
capacity: u32,
max_msg_len: u32,
timeout: u64,
creation_time: u64,
tick_unit: TickUnit,
version: Version,
}
#[allow(clippy::len_without_is_empty)]
impl Header {
#[inline]
pub fn new(
writer_id: u64,
channel_id: u64,
capacity_hint: u32,
max_msg_len_hint: u32,
timeout: u64,
tick_unit: TickUnit,
) -> Header {
let capacity = max(MIN_CAPACITY, align(capacity_hint));
let max_msg_len = align(min(max_msg_len_hint + REC_HEADER_LEN, compute_max_msg_len(capacity)) as u32);
let creation_time = tick_unit.nix_time();
Header {
writer_id,
channel_id,
capacity,
max_msg_len,
timeout,
creation_time,
tick_unit,
version: Version::latest(),
}
}
pub fn read(header: &[u8]) -> Result<Header, ChannelError> {
assert!(header.len() >= HEADER_LEN);
let mut offset = 0;
let signature = Header::read_u64(header, offset);
if signature != SIGNATURE {
return Err(InvalidSignature {
expected: SIGNATURE,
actual: signature,
});
}
offset += 8;
let version: Version = Header::read_u64(header, 8).into();
let latest = Version::latest();
if !latest.is_compatible(version) {
return Err(IncompatibleVersion {
expected: latest.into(),
actual: version.into(),
});
}
offset += 8;
let writer_id = Header::read_u64(header, offset);
offset += 8;
let channel_id = Header::read_u64(header, offset);
offset += 8;
let capacity = Header::read_u32(header, offset);
if capacity < MIN_CAPACITY {
return Err(InvalidCapacity {
capacity,
msg: "Capacity below minimum allowed of 10KB",
});
}
if !is_aligned(MIN_CAPACITY) {
return Err(InvalidCapacity {
capacity,
msg: "Capacity is not 8 bytes aligned",
});
}
offset += 4;
let max_msg_len = Header::read_u32(header, offset);
if max_msg_len > align(compute_max_msg_len(capacity)) {
return Err(InvalidMaxMessageLength {
msg_len: max_msg_len,
msg: "Max message lenght is too large",
});
}
if !is_aligned(max_msg_len) {
return Err(InvalidMaxMessageLength {
msg_len: max_msg_len,
msg: "Max message length is not 8 bytes aligned",
});
}
offset += 4;
let timeout = Header::read_u64(header, offset);
offset += 8;
let creation_time = Header::read_u64(header, offset);
offset += 8;
let tick_unit = TickUnit::from_id(header[offset]);
Ok(Header {
version,
writer_id,
channel_id,
capacity,
max_msg_len,
timeout,
creation_time,
tick_unit,
})
}
#[inline]
pub fn write_to(&self, header: &mut [u8]) -> usize {
assert!(self.len() <= header.len());
header[0..8].clone_from_slice(&SIGNATURE.to_le_bytes());
let latest_v: u64 = Version::latest().into();
header[8..16].clone_from_slice(&latest_v.to_le_bytes());
header[16..24].clone_from_slice(&self.writer_id.to_le_bytes());
header[24..32].clone_from_slice(&self.channel_id.to_le_bytes());
header[32..36].clone_from_slice(&self.capacity.to_le_bytes());
header[36..40].clone_from_slice(&self.max_msg_len.to_le_bytes());
header[40..48].clone_from_slice(&self.timeout.to_le_bytes());
header[48..56].clone_from_slice(&self.creation_time.to_le_bytes());
header[56] = self.tick_unit.id();
let last = 57;
for item in header.iter_mut().take(HEADER_LEN).skip(last) {
*item = 0u8;
}
self.len()
}
#[inline]
fn read_u64(header: &[u8], offset: usize) -> u64 {
assert!(offset + 8 < HEADER_LEN);
u64::from_le_bytes([
header[offset],
header[offset + 1],
header[offset + 2],
header[offset + 3],
header[offset + 4],
header[offset + 5],
header[offset + 6],
header[offset + 7],
])
}
#[inline]
fn read_u32(header: &[u8], offset: usize) -> u32 {
assert!(offset + 4 < HEADER_LEN);
u32::from_le_bytes([header[offset], header[offset + 1], header[offset + 2], header[offset + 3]])
}
#[inline]
pub fn version(&self) -> String {
self.version.to_string()
}
#[inline]
pub fn channel_id(&self) -> u64 {
self.channel_id
}
#[inline]
pub fn writer_id(&self) -> u64 {
self.writer_id
}
#[inline]
pub fn capacity(&self) -> u32 {
self.capacity
}
#[inline]
pub fn max_msg_len(&self) -> u32 {
self.max_msg_len
}
#[inline]
pub fn timeout(&self) -> u64 {
self.timeout
}
#[inline]
pub fn creation_time(&self) -> u64 {
self.creation_time
}
#[inline]
pub fn tick_unit(&self) -> TickUnit {
self.tick_unit
}
#[inline]
pub const fn len(&self) -> usize {
HEADER_LEN
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_read_write_header() {
let producer_id: u64 = 111;
let channel_id: u64 = 101;
let capacity: u32 = 10_001;
let max_msg_len: u32 = 100;
let timeout: u64 = 10_000;
let tick_unit = TickUnit::Nanos;
let head = Header::new(producer_id, channel_id, capacity, max_msg_len, timeout, tick_unit);
let mut data = vec![0u8; HEADER_LEN];
assert!(head.write_to(&mut data) == HEADER_LEN);
assert!(Header::read(&data).unwrap() == head);
assert_eq!(head.tick_unit(), TickUnit::Nanos);
assert_eq!(head.timeout(), timeout);
assert_eq!(head.version(), Version::latest().to_string());
assert!(head.creation_time() < tick_unit.nix_time());
assert_eq!(head.len(), 128);
assert_eq!(head.writer_id(), producer_id);
}
}