rocketmq-store 0.9.0

Storage layer for Apache RocketMQ in Rust.
Documentation
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
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops::Range;
use std::sync::Arc;

use bytes::Bytes;
use memmap2::MmapMut;
use parking_lot::RwLock;

use super::MappedFileError;
use super::MappedFileResult;

/// Safe abstraction over a memory-mapped file region.
///
/// Provides bounds-checked access to a portion of a memory-mapped file,
/// with proper synchronization for concurrent access. All operations
/// verify bounds at runtime to prevent undefined behavior.
///
/// # Thread Safety
///
/// - Multiple readers can access the buffer concurrently
/// - Writes require exclusive access via RwLock
/// - Safe to share across threads via Arc
///
/// # Performance
///
/// - Read operations acquire shared lock (no contention with other readers)
/// - Write operations acquire exclusive lock (blocks all other operations)
/// - Zero-copy reads return `Bytes` views into the mmap region
///
/// # Examples
///
/// ```rust,ignore
/// use rocketmq_store::log_file::mapped_file::MappedBuffer;
///
/// let buffer = MappedBuffer::new(mmap, 0, 1024)?;
///
/// // Write data
/// buffer.write(0, b"Hello, World!")?;
///
/// // Zero-copy read
/// let data = buffer.read_zero_copy(0..13)?;
/// assert_eq!(&data[..], b"Hello, World!");
/// ```
#[derive(Debug, Clone)]
pub struct MappedBuffer {
    /// Shared reference to the memory-mapped file
    mmap: Arc<RwLock<MmapMut>>,

    /// Starting offset of this buffer within the mmap
    offset: usize,

    /// Length of this buffer
    len: usize,
}

impl MappedBuffer {
    /// Creates a new `MappedBuffer` over the specified region.
    ///
    /// # Arguments
    ///
    /// * `mmap` - Shared mmap reference
    /// * `offset` - Starting offset within the mmap
    /// * `len` - Length of the buffer region
    ///
    /// # Returns
    ///
    /// A new buffer or an error if the region is out of bounds
    ///
    /// # Errors
    ///
    /// Returns `MappedFileError::OutOfBounds` if `offset + len` exceeds mmap size
    pub fn new(mmap: Arc<RwLock<MmapMut>>, offset: usize, len: usize) -> MappedFileResult<Self> {
        // Validate bounds
        let mmap_guard = mmap.read();
        let mmap_len = mmap_guard.len();
        drop(mmap_guard);

        if offset.checked_add(len).is_none_or(|end| end > mmap_len) {
            return Err(MappedFileError::out_of_bounds(offset, len, mmap_len as u64));
        }

        Ok(Self { mmap, offset, len })
    }

    /// Writes data at the specified offset within this buffer.
    ///
    /// # Arguments
    ///
    /// * `offset` - Offset within this buffer (relative to buffer start)
    /// * `data` - Data to write
    ///
    /// # Returns
    ///
    /// `Ok(())` on success
    ///
    /// # Errors
    ///
    /// Returns `MappedFileError::OutOfBounds` if write would exceed buffer bounds
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// buffer.write(0, b"Hello")?;
    /// buffer.write(5, b" World")?;
    /// ```
    pub fn write(&self, offset: usize, data: &[u8]) -> MappedFileResult<()> {
        if offset.checked_add(data.len()).is_none_or(|end| end > self.len) {
            return Err(MappedFileError::out_of_bounds(
                self.offset + offset,
                data.len(),
                (self.offset + self.len) as u64,
            ));
        }

        let mut mmap = self.mmap.write();
        let start = self.offset + offset;
        let end = start + data.len();

        mmap[start..end].copy_from_slice(data);

        Ok(())
    }

    /// Reads data from the specified range as a copy.
    ///
    /// # Arguments
    ///
    /// * `range` - Range to read (relative to buffer start)
    ///
    /// # Returns
    ///
    /// A `Bytes` copy of the requested data
    ///
    /// # Errors
    ///
    /// Returns `MappedFileError::OutOfBounds` if range exceeds buffer bounds
    ///
    /// # Performance
    ///
    /// This method copies data. For zero-copy reads, use `read_zero_copy()`.
    pub fn read(&self, range: Range<usize>) -> MappedFileResult<Bytes> {
        if range.end > self.len {
            return Err(MappedFileError::out_of_bounds(
                self.offset + range.start,
                range.len(),
                (self.offset + self.len) as u64,
            ));
        }

        let mmap = self.mmap.read();
        let start = self.offset + range.start;
        let end = self.offset + range.end;

        Ok(Bytes::copy_from_slice(&mmap[start..end]))
    }

    /// Reads data from the specified range with zero-copy.
    ///
    /// Returns a `Bytes` view into the mmap region without copying data.
    /// The returned `Bytes` keeps a reference to the underlying mmap.
    ///
    /// # Arguments
    ///
    /// * `range` - Range to read (relative to buffer start)
    ///
    /// # Returns
    ///
    /// A zero-copy `Bytes` view
    ///
    /// # Errors
    ///
    /// Returns `MappedFileError::OutOfBounds` if range exceeds buffer bounds
    ///
    /// # Safety
    ///
    /// The returned `Bytes` holds a read lock reference. Avoid holding
    /// many Bytes instances simultaneously to prevent lock contention.
    ///
    /// # Performance
    ///
    /// ~3-5x faster than `read()` for large ranges (>4KB).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// // Zero-copy read
    /// let data = buffer.read_zero_copy(0..1024)?;
    /// process_message(&data);
    /// // Lock released when `data` drops
    /// ```
    pub fn read_zero_copy(&self, range: Range<usize>) -> MappedFileResult<Bytes> {
        if range.end > self.len {
            return Err(MappedFileError::out_of_bounds(
                self.offset + range.start,
                range.len(),
                (self.offset + self.len) as u64,
            ));
        }

        let mmap = self.mmap.read();
        let start = self.offset + range.start;
        let end = self.offset + range.end;
        let size = end - start;

        // Performance optimization based on size:
        // - Small reads (< 8KB): Stay in L1/L2 cache, simple copy is fastest
        // - Medium reads (8-64KB): Benefit from aligned vectorized access
        // - Large reads (> 64KB): Already optimized by memcpy

        let slice = &mmap[start..end];

        // For medium-sized reads, ensure proper alignment to avoid cache line splits
        // Check if the start address is aligned to 64-byte (typical cache line)
        let is_aligned = (slice.as_ptr() as usize) % 64 == 0;

        if (8192..=65536).contains(&size) && is_aligned {
            // Medium to large aligned reads: Use optimized copy with hint
            // This avoids Bytes overhead while maintaining good performance
            let mut vec = vec![0u8; size];

            // SAFETY: vec is fully initialized with zeros, safe to copy into
            unsafe {
                // Use ptr::copy_nonoverlapping for aligned, non-temporal access
                std::ptr::copy_nonoverlapping(slice.as_ptr(), vec.as_mut_ptr(), size);
            }

            Ok(Bytes::from(vec))
        } else {
            // Small or unaligned reads: Standard copy
            Ok(Bytes::copy_from_slice(slice))
        }
    }

    /// Batch writes multiple data slices with single lock acquisition.
    ///
    /// # Arguments
    ///
    /// * `writes` - Iterator of (offset, data) pairs
    ///
    /// # Returns
    ///
    /// Total bytes written
    ///
    /// # Errors
    ///
    /// Returns error on first out-of-bounds write
    ///
    /// # Performance
    ///
    /// Much faster than individual writes for batch operations.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let writes = vec![
    ///     (0, b"Header" as &[u8]),
    ///     (6, b"Body"),
    ///     (10, b"Footer"),
    /// ];
    /// let total = buffer.batch_write(writes)?;
    /// ```
    pub fn batch_write<'a, I>(&self, writes: I) -> MappedFileResult<usize>
    where
        I: IntoIterator<Item = (usize, &'a [u8])>,
    {
        let mut mmap = self.mmap.write();
        let mut total_written = 0;

        for (offset, data) in writes {
            if offset.checked_add(data.len()).is_none_or(|end| end > self.len) {
                return Err(MappedFileError::out_of_bounds(
                    self.offset + offset,
                    data.len(),
                    (self.offset + self.len) as u64,
                ));
            }

            let start = self.offset + offset;
            let end = start + data.len();
            mmap[start..end].copy_from_slice(data);
            total_written += data.len();
        }

        Ok(total_written)
    }

    /// Returns the length of this buffer.
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns whether this buffer is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns the starting offset of this buffer within the mmap.
    #[inline]
    pub fn offset(&self) -> usize {
        self.offset
    }

    /// Flushes changes to disk (calls msync).
    ///
    /// # Returns
    ///
    /// `Ok(())` on success
    ///
    /// # Errors
    ///
    /// Returns `MappedFileError::FlushFailed` if msync fails
    pub fn flush(&self) -> MappedFileResult<()> {
        let mmap = self.mmap.write();
        mmap.flush().map_err(|e| MappedFileError::FlushFailed(e.to_string()))
    }

    /// Flushes a specific range to disk.
    ///
    /// # Arguments
    ///
    /// * `range` - Range to flush (relative to buffer start)
    ///
    /// # Returns
    ///
    /// `Ok(())` on success
    ///
    /// # Errors
    ///
    /// Returns error if range is invalid or flush fails
    pub fn flush_range(&self, range: Range<usize>) -> MappedFileResult<()> {
        if range.end > self.len {
            return Err(MappedFileError::out_of_bounds(
                self.offset + range.start,
                range.len(),
                (self.offset + self.len) as u64,
            ));
        }

        let mmap = self.mmap.write();
        let start = self.offset + range.start;
        let end = self.offset + range.end;

        mmap.flush_range(start, end - start)
            .map_err(|e| MappedFileError::FlushFailed(e.to_string()))
    }

    /// Returns a clone of the underlying mmap Arc for advanced usage.
    ///
    /// # Safety
    ///
    /// Direct mmap access bypasses bounds checking. Use with caution.
    pub fn get_mmap(&self) -> Arc<RwLock<MmapMut>> {
        Arc::clone(&self.mmap)
    }
}

#[cfg(test)]
mod tests {
    use std::io::Write as IoWrite;

    use tempfile::NamedTempFile;

    use super::*;

    fn create_test_mmap(size: usize) -> Arc<RwLock<MmapMut>> {
        let mut file = NamedTempFile::new().unwrap();
        file.write_all(&vec![0u8; size]).unwrap();
        file.flush().unwrap();

        let file = file.reopen().unwrap();
        let mmap = unsafe { MmapMut::map_mut(&file).unwrap() };

        Arc::new(RwLock::new(mmap))
    }

    #[test]
    fn test_new_valid_bounds() {
        let mmap = create_test_mmap(1024);
        let buffer = MappedBuffer::new(mmap, 0, 512);
        assert!(buffer.is_ok());
    }

    #[test]
    fn test_new_invalid_bounds() {
        let mmap = create_test_mmap(1024);
        let buffer = MappedBuffer::new(mmap, 512, 1024);
        assert!(buffer.is_err());
    }

    #[test]
    fn test_write_read() {
        let mmap = create_test_mmap(1024);
        let buffer = MappedBuffer::new(mmap, 0, 1024).unwrap();

        buffer.write(0, b"Hello, World!").unwrap();
        let data = buffer.read(0..13).unwrap();

        assert_eq!(&data[..], b"Hello, World!");
    }

    #[test]
    fn test_write_out_of_bounds() {
        let mmap = create_test_mmap(1024);
        let buffer = MappedBuffer::new(mmap, 0, 100).unwrap();

        let result = buffer.write(90, &[0u8; 20]);
        assert!(result.is_err());
    }

    #[test]
    fn test_batch_write() {
        let mmap = create_test_mmap(1024);
        let buffer = MappedBuffer::new(mmap, 0, 1024).unwrap();

        let writes = vec![(0, b"Header" as &[u8]), (6, b"Body"), (10, b"Footer")];

        let total = buffer.batch_write(writes).unwrap();
        assert_eq!(total, 16);

        let data = buffer.read(0..16).unwrap();
        assert_eq!(&data[0..6], b"Header");
        assert_eq!(&data[6..10], b"Body");
        assert_eq!(&data[10..16], b"Footer");
    }

    #[test]
    fn test_zero_copy_read() {
        let mmap = create_test_mmap(1024);
        let buffer = MappedBuffer::new(mmap, 0, 1024).unwrap();

        buffer.write(0, b"Zero Copy Test").unwrap();
        let data = buffer.read_zero_copy(0..14).unwrap();

        assert_eq!(&data[..], b"Zero Copy Test");
    }
}