rocketmq-runtime 1.0.0

Unofficial Rust implementation of Apache RocketMQ
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
452
453
454
455
// Copyright 2026 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::num::NonZeroUsize;
use std::process::Command;
use std::sync::mpsc::SyncSender;
use std::time::Duration;

use rocketmq_runtime::task::service_task::ServiceTask;
use rocketmq_runtime::task::service_task::ServiceTaskContext;
use rocketmq_runtime::task::ServiceManager;
use rocketmq_runtime::MissedTickPolicy;
use rocketmq_runtime::OperationContext;
use rocketmq_runtime::RuntimeConfig;
use rocketmq_runtime::RuntimeOwner;
use rocketmq_runtime::ScheduledExecutionPolicy;
use rocketmq_runtime::ScheduledTaskConfig;
use rocketmq_runtime::ScheduledTaskControl;
use rocketmq_runtime::ScheduledTaskGroup;
use rocketmq_runtime::TaskGroup;
use rocketmq_runtime::TaskKind;

const CHILD_MODE: &str = "ROCKETMQ_RUNTIME_STACK_TEST_CHILD";
const STACK_SIZE: usize = 1024 * 1024;
// A submitting thread whose stack is already mostly used, such as a Broker
// entrypoint starting services deep inside its startup future.
const SUBMITTER_STACK_SIZE: usize = 256 * 1024;
// Below the release inline boundary, like the Broker transaction check loop.
const MID_SIZE_PAYLOAD: usize = 12 * 1024;

#[test]
fn large_tasks_submit_on_one_mib_stack() {
    if std::env::var_os(CHILD_MODE).is_none() {
        // Stack overflow aborts the process; isolate it from the rest of the test suite.
        let output = Command::new(std::env::current_exe().expect("test executable"))
            .args(["--exact", "large_tasks_submit_on_one_mib_stack", "--nocapture"])
            .env(CHILD_MODE, "1")
            .output()
            .expect("run stack probe subprocess");
        assert!(
            output.status.success(),
            "stack probe failed: {}\nstdout: {}\nstderr: {}",
            output.status,
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        return;
    }

    std::thread::Builder::new()
        .name("runtime-submission-stack-probe".to_owned())
        .stack_size(STACK_SIZE)
        .spawn(run_stack_probe)
        .expect("start small-stack thread")
        .join()
        .expect("small-stack thread completed");
}

fn run_stack_probe() {
    let owner = RuntimeOwner::plan(RuntimeConfig {
        worker_threads: 2,
        ..RuntimeConfig::default()
    })
    .expect("valid runtime configuration")
    .build()
    .expect("runtime owner");
    let context = owner.root_context().component("stack-probe");
    let group = context.task_group();

    let (_, handle) = group
        .spawn_with_handle("large-task", TaskKind::Service, large_task())
        .expect("submit large task");
    owner.block_on(handle).expect("large task completed");

    let schedules = ScheduledTaskGroup::new(group.clone());
    let (completed, received) = std::sync::mpsc::sync_channel(1);
    schedules
        .schedule(
            ScheduledTaskConfig::fixed_delay("large-scheduled-task", Duration::from_secs(60)),
            ScheduledExecutionPolicy::default(),
            move || {
                let completed = completed.clone();
                async move {
                    large_task().await;
                    completed.send(()).expect("report scheduled completion");
                }
            },
        )
        .expect("submit large scheduled task");
    received
        .recv_timeout(Duration::from_secs(10))
        .expect("large scheduled task completed");

    let report = owner.block_on(owner.shutdown_tasks());
    assert!(report.is_healthy(), "{}", report.to_json());
    owner.shutdown_runtime_blocking().expect("runtime stopped");
}

async fn large_task() {
    let payload = [7_u8; 16 * 1024];
    tokio::task::yield_now().await;
    assert_eq!(std::hint::black_box(payload)[0], 7);
}

fn isolated_stack_probe(name: &str, probe: fn()) {
    isolated_stack_probe_on(name, STACK_SIZE, probe);
}

fn isolated_stack_probe_on(name: &str, stack_size: usize, probe: fn()) {
    if std::env::var_os(CHILD_MODE).is_none() {
        let output = Command::new(std::env::current_exe().unwrap())
            .args(["--exact", name, "--nocapture"])
            .env(CHILD_MODE, "1")
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "{name}: {}\n{}",
            output.status,
            String::from_utf8_lossy(&output.stderr)
        );
        return;
    }
    std::thread::Builder::new()
        .name(name.into())
        .stack_size(stack_size)
        .spawn(probe)
        .unwrap()
        .join()
        .unwrap();
}

fn small_stack_owner() -> RuntimeOwner {
    RuntimeOwner::plan(RuntimeConfig {
        worker_threads: 2,
        thread_stack_size: Some(STACK_SIZE),
        ..RuntimeConfig::default()
    })
    .unwrap()
    .build()
    .unwrap()
}

async fn payload_task<const N: usize>() {
    let payload = [7_u8; N];
    tokio::task::yield_now().await;
    assert_eq!(std::hint::black_box(&payload)[0], 7);
}

struct PayloadService<const N: usize> {
    running: SyncSender<()>,
}

impl<const N: usize> ServiceTask for PayloadService<N> {
    fn get_service_name(&self) -> String {
        "payload-service".to_owned()
    }

    async fn run(&self, context: &ServiceTaskContext) {
        let payload = [7_u8; N];
        self.running.send(()).unwrap();
        while !context.is_stopped() {
            context.wait_for_running(Duration::from_millis(10)).await;
        }
        assert_eq!(std::hint::black_box(&payload)[0], 7);
    }
}

fn start_and_stop_payload_service<const N: usize>(owner: &RuntimeOwner, group: &TaskGroup) {
    let (running, started) = std::sync::mpsc::sync_channel(1);
    let service = ServiceManager::new_with_task_group(PayloadService::<N> { running }, group.clone());
    owner.block_on(service.start()).unwrap();
    started.recv_timeout(Duration::from_secs(5)).unwrap();
    owner.block_on(service.shutdown()).unwrap();
}

#[test]
fn large_operations_fit_on_one_mib_stack() {
    isolated_stack_probe("large_operations_fit_on_one_mib_stack", || {
        let owner = small_stack_owner();
        let context = owner.root_context().component("operation-stack");
        let operation = OperationContext::without_deadline(TaskKind::Worker);
        let id = context
            .task_group()
            .spawn_operation(&operation, "large", payload_task::<65536>())
            .unwrap();
        assert!(owner.block_on(context.task_group().wait_task(id, Duration::from_secs(5))));
        let id = context
            .task_group()
            .spawn_draining_operation(&operation, "large-drain", payload_task::<65536>())
            .unwrap();
        assert!(owner.block_on(context.task_group().wait_task(id, Duration::from_secs(5))));
        let id = context
            .task_group()
            .spawn_cancellable_service("large-cancellable", payload_task::<65536>())
            .unwrap();
        assert!(owner.block_on(context.task_group().wait_task(id, Duration::from_secs(5))));
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn service_and_task_submission_fit_on_a_small_submitter_stack() {
    isolated_stack_probe_on(
        "service_and_task_submission_fit_on_a_small_submitter_stack",
        SUBMITTER_STACK_SIZE,
        || {
            let owner = small_stack_owner();
            let context = owner.root_context().component("submitter-stack");
            let group = context.task_group();

            // Unoptimized builds copy an inline future into every wrapper frame.
            let id = group
                .spawn("mid-size", TaskKind::Worker, payload_task::<MID_SIZE_PAYLOAD>())
                .unwrap();
            assert!(owner.block_on(group.wait_task(id, Duration::from_secs(5))));

            // A service loop is built by the worker, not moved down from the caller.
            start_and_stop_payload_service::<MID_SIZE_PAYLOAD>(&owner, group);
            start_and_stop_payload_service::<65536>(&owner, group);
            assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
        },
    );
}

#[test]
fn scheduled_runs_fit_on_one_mib_worker_stack() {
    isolated_stack_probe("scheduled_runs_fit_on_one_mib_worker_stack", || {
        let owner = small_stack_owner();
        let context = owner.root_context().component("schedule-stack");
        let schedules = ScheduledTaskGroup::new(context.task_group().clone());
        let (sent, received) = std::sync::mpsc::sync_channel(1);
        schedules
            .schedule(
                ScheduledTaskConfig::fixed_delay("large-run", Duration::from_secs(60)),
                ScheduledExecutionPolicy::default(),
                move || {
                    let sent = sent.clone();
                    async move {
                        payload_task::<32768>().await;
                        sent.send(()).unwrap();
                    }
                },
            )
            .unwrap();
        received.recv_timeout(Duration::from_secs(5)).unwrap();
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn blocking_captures_fit_on_one_mib_stack() {
    isolated_stack_probe("blocking_captures_fit_on_one_mib_stack", || {
        let owner = small_stack_owner();
        let context = owner.root_context().component("blocking-stack");
        let payload = [7_u8; 65536];
        let future = context
            .storage_io()
            .spawn_io("large-capture", move || std::hint::black_box(&payload)[0]);
        // Prevent large captures from inflating the caller's own async state.
        assert!(std::mem::size_of_val(&future) < 4096);
        let result = owner.block_on(future);
        assert_eq!(result.unwrap(), 7);
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn blocking_results_fit_on_one_mib_stack() {
    isolated_stack_probe("blocking_results_fit_on_one_mib_stack", || {
        let owner = small_stack_owner();
        let context = owner.root_context().component("blocking-result-stack");
        let result = owner
            .block_on(context.storage_io().spawn_io("large-result", || [7_u8; 32768]))
            .unwrap();
        assert_eq!(std::hint::black_box(&result)[0], 7);
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn deep_task_tree_shuts_down_on_one_mib_stack() {
    isolated_stack_probe("deep_task_tree_shuts_down_on_one_mib_stack", || {
        let owner = small_stack_owner();
        let root = owner.root_context().component("tree");
        let mut leaf = root.clone();
        for _ in 0..512 {
            leaf = leaf.component("child");
        }
        let report = owner.block_on(root.task_group().shutdown(Duration::from_secs(5)));
        assert!(report.is_healthy());
        let mut node = &report;
        let mut depth = 0;
        while let Some(child) = node.children.first() {
            depth += 1;
            node = child;
        }
        assert_eq!(depth, 512);
        drop(leaf);
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn deep_idle_ancestors_drop_on_one_mib_stack() {
    isolated_stack_probe("deep_idle_ancestors_drop_on_one_mib_stack", || {
        let owner = small_stack_owner();
        let root = owner.root_context().component("tree");
        let mut leaf = root.clone();
        for _ in 0..4096 {
            leaf = leaf.component("child");
        }
        drop(leaf);
        assert_eq!(root.task_group().component_count(), 0);
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn deep_immediate_shutdown_fits_on_one_mib_stack() {
    isolated_stack_probe("deep_immediate_shutdown_fits_on_one_mib_stack", || {
        let owner = small_stack_owner();
        let root = owner.root_context().component("tree");
        let mut leaf = root.clone();
        for _ in 0..1024 {
            leaf = leaf.component("child");
        }
        let report = root.task_group().shutdown_now();
        assert!(report.is_healthy());
        drop(leaf);
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn large_scheduler_factories_fit_on_one_mib_stack() {
    isolated_stack_probe("large_scheduler_factories_fit_on_one_mib_stack", || {
        let owner = small_stack_owner();
        let context = owner.root_context().component("factory-stack");
        let schedules = ScheduledTaskGroup::new(context.task_group().clone());
        let payload = [7_u8; 65536];
        let (sent, received) = std::sync::mpsc::sync_channel(1);
        schedules
            .schedule(
                ScheduledTaskConfig::fixed_delay("large-factory", Duration::from_secs(60)),
                ScheduledExecutionPolicy::default(),
                move || {
                    let value = std::hint::black_box(&payload)[0];
                    let sent = sent.clone();
                    async move {
                        sent.send(value).unwrap();
                    }
                },
            )
            .unwrap();
        assert_eq!(received.recv_timeout(Duration::from_secs(5)).unwrap(), 7);
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}

#[test]
fn scheduling_adapters_fit_on_one_mib_worker_stack() {
    isolated_stack_probe("scheduling_adapters_fit_on_one_mib_worker_stack", || {
        let owner = small_stack_owner();
        let context = owner.root_context().component("schedule-adapter-stack");
        let schedules = ScheduledTaskGroup::new(context.task_group().clone());
        let operation = OperationContext::without_deadline(TaskKind::Worker);

        macro_rules! check_schedule {
            ($name:literal, $mode:ident, |$config:ident, $task:ident| $register:expr, $result:expr) => {{
                let (sent, received) = std::sync::mpsc::sync_channel(1);
                let mut $config = ScheduledTaskConfig::$mode($name, Duration::from_secs(60));
                $config.max_run_time = Some(Duration::from_secs(5));
                let $task = move || {
                    let sent = sent.clone();
                    async move {
                        payload_task::<32768>().await;
                        sent.send(()).unwrap();
                        $result
                    }
                };
                $register.unwrap();
                received.recv_timeout(Duration::from_secs(5)).unwrap();
            }};
        }

        let serial = ScheduledExecutionPolicy::default();
        let overlapping = ScheduledExecutionPolicy::bounded(NonZeroUsize::new(2).unwrap(), MissedTickPolicy::Skip);
        check_schedule!(
            "delay",
            fixed_delay,
            |config, task| schedules.schedule(config, serial, task),
            ()
        );
        check_schedule!(
            "delay-operation",
            fixed_delay,
            |config, task| schedules.schedule_operation(&operation, config, serial, task),
            ()
        );
        check_schedule!(
            "controlled",
            fixed_delay,
            |config, task| schedules.schedule_controlled(config, task),
            ScheduledTaskControl::Stop
        );
        check_schedule!(
            "no-overlap",
            fixed_rate_no_overlap,
            |config, task| schedules.schedule(config, serial, task),
            ()
        );
        check_schedule!(
            "no-overlap-operation",
            fixed_rate_no_overlap,
            |config, task| schedules.schedule_operation(&operation, config, serial, task),
            ()
        );
        check_schedule!(
            "overlap",
            fixed_rate,
            |config, task| schedules.schedule(config, overlapping, task),
            ()
        );

        // Exercise both boxing decisions together: a large factory and a large run.
        let payload = [7_u8; 65536];
        let (sent, received) = std::sync::mpsc::sync_channel(1);
        let mut config = ScheduledTaskConfig::fixed_rate_no_overlap("bounded", Duration::from_secs(60));
        config.max_run_time = Some(Duration::from_secs(5));
        schedules
            .schedule(config, serial, move || {
                let value = std::hint::black_box(&payload)[0];
                let sent = sent.clone();
                async move {
                    payload_task::<32768>().await;
                    sent.send(value).unwrap();
                }
            })
            .unwrap();
        assert_eq!(received.recv_timeout(Duration::from_secs(5)).unwrap(), 7);
        assert!(owner.shutdown_runtime_blocking().unwrap().is_healthy());
    });
}