Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #417]✨Implement message persistence to disk during transmission🚀 #418

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rocketmq-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ log = "0.4.20"

parking_lot = { workspace = true }
once_cell = { workspace = true }
tempfile = "3.10.1"
tempfile = "3.10.1"
trait-variant.workspace = true
[dev-dependencies]
mockall = "0.12.1"
26 changes: 25 additions & 1 deletion rocketmq-common/src/common/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@
* limitations under the License.
*/

pub mod thread_service;
use std::any::Any;

use crate::common::thread::thread_service_std::ServiceThreadStd;

pub mod thread_service_std;
pub mod thread_service_tokio;

pub trait ServiceThread {
fn start(&mut self);
fn shutdown(&mut self);
fn make_stop(&mut self);
fn wakeup(&mut self);
fn wait_for_running(&mut self, interval: i64);
fn is_stopped(&self) -> bool;
fn get_service_name(&self) -> String;
}

pub trait Runnable: Send + Sync + 'static {
fn run(&mut self) {}
}

#[trait_variant::make(TokioRunnable: Send)]
pub trait RocketMQTokioRunnable: Sync + 'static {
async fn run(&mut self);
}
95 changes: 0 additions & 95 deletions rocketmq-common/src/common/thread/thread_service.rs

This file was deleted.

199 changes: 199 additions & 0 deletions rocketmq-common/src/common/thread/thread_service_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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::{
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread::JoinHandle,
};

use parking_lot::Mutex;
use tracing::info;

use crate::common::thread::{Runnable, ServiceThread};

pub struct ServiceThreadStd {
name: String,
runnable: Arc<Mutex<dyn Runnable>>,
thread: Option<JoinHandle<()>>,
stopped: Arc<AtomicBool>,
started: Arc<AtomicBool>,
notified: (parking_lot::Mutex<()>, parking_lot::Condvar),
}

impl ServiceThreadStd {
pub fn new<T: Runnable>(name: String, runnable: T) -> Self {
ServiceThreadStd {
name,
runnable: Arc::new(Mutex::new(runnable)),
thread: None,
stopped: Arc::new(AtomicBool::new(false)),
started: Arc::new(AtomicBool::new(false)),
notified: (Default::default(), Default::default()),
}
}
}

impl ServiceThreadStd {
pub fn start(&mut self) {
if let Ok(value) =
self.started
.compare_exchange(false, true, Ordering::SeqCst, Ordering::Relaxed)

Check warning on line 56 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L53-L56

Added lines #L53 - L56 were not covered by tests
{
if value {

Check warning on line 58 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L58

Added line #L58 was not covered by tests
return;
}
} else {
return;
}
let name = self.name.clone();
let runnable = self.runnable.clone();
let stopped = self.stopped.clone();
let thread = std::thread::Builder::new()
.name(name.clone())
.spawn(move || {
log::info!("Starting service thread: {}", name);
if stopped.load(std::sync::atomic::Ordering::Relaxed) {
log::info!("Service thread stopped: {}", name);

Check warning on line 72 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L64-L72

Added lines #L64 - L72 were not covered by tests
return;
}
runnable.lock().run();
})

Check warning on line 76 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L75-L76

Added lines #L75 - L76 were not covered by tests
.expect("Failed to start service thread");
self.thread = Some(thread);
}

Check warning on line 79 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L78-L79

Added lines #L78 - L79 were not covered by tests

pub fn shutdown(&mut self) {
self.shutdown_interrupt(false);
}

Check warning on line 83 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L81-L83

Added lines #L81 - L83 were not covered by tests

pub fn shutdown_interrupt(&mut self, interrupt: bool) {
if let Ok(value) =
self.started
.compare_exchange(true, false, Ordering::SeqCst, Ordering::Relaxed)

Check warning on line 88 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L85-L88

Added lines #L85 - L88 were not covered by tests
{
if !value {

Check warning on line 90 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L90

Added line #L90 was not covered by tests
return;
}
} else {
return;
}
self.stopped.store(true, Ordering::Relaxed);
self.wakeup();
if let Some(thread) = self.thread.take() {
if interrupt {
drop(thread);

Check warning on line 100 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L96-L100

Added lines #L96 - L100 were not covered by tests
} else {
thread.join().expect("Failed to join service thread");

Check warning on line 102 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L102

Added line #L102 was not covered by tests
}
}
}

Check warning on line 105 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L105

Added line #L105 was not covered by tests

pub fn make_stop(&mut self) {
if !self.started.load(Ordering::Acquire) {

Check warning on line 108 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L107-L108

Added lines #L107 - L108 were not covered by tests
return;
}
self.stopped.store(true, Ordering::Release);
}

Check warning on line 112 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L111-L112

Added lines #L111 - L112 were not covered by tests

pub fn wakeup(&mut self) {
self.notified.1.notify_all();
}

Check warning on line 116 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L114-L116

Added lines #L114 - L116 were not covered by tests

pub fn wait_for_running(&mut self, interval: i64) {
let mut guard = self.notified.0.lock();
self.notified.1.wait_for(

Check warning on line 120 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L118-L120

Added lines #L118 - L120 were not covered by tests
&mut guard,
std::time::Duration::from_millis(interval as u64),

Check warning on line 122 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L122

Added line #L122 was not covered by tests
);
}

Check warning on line 124 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L124

Added line #L124 was not covered by tests

pub fn is_stopped(&self) -> bool {
self.stopped.load(Ordering::Acquire)
}

Check warning on line 128 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L126-L128

Added lines #L126 - L128 were not covered by tests

pub fn get_service_name(&self) -> String {
self.name.clone()
}

Check warning on line 132 in rocketmq-common/src/common/thread/thread_service_std.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-common/src/common/thread/thread_service_std.rs#L130-L132

Added lines #L130 - L132 were not covered by tests
}

#[cfg(test)]
mod tests {
/*use mockall::{automock, predicate::*};

use super::*;

struct MockTestRunnable;
impl MockTestRunnable {
fn new() -> MockTestRunnable {
MockTestRunnable
}
}
impl Runnable for MockTestRunnable {
fn run(&mut self, service_thread: &dyn ServiceThread) {}
}

#[test]
fn test_start_and_shutdown() {
let mock_runnable = MockTestRunnable::new();

let mut service_thread =
ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

service_thread.start();
assert!(service_thread.started.load(Ordering::SeqCst));
assert!(!service_thread.stopped.load(Ordering::SeqCst));

service_thread.shutdown_interrupt(false);
assert!(!service_thread.started.load(Ordering::SeqCst));
assert!(service_thread.stopped.load(Ordering::SeqCst));
}

#[test]
fn test_make_stop() {
let mock_runnable = MockTestRunnable::new();
let mut service_thread =
ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

service_thread.start();
service_thread.make_stop();
assert!(service_thread.is_stopped());
}

#[test]
fn test_wait_for_running() {
let mock_runnable = MockTestRunnable::new();
let mut service_thread =
ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

service_thread.start();
service_thread.wait_for_running(100);
assert!(service_thread.started.load(Ordering::SeqCst));
}

#[test]
fn test_wakeup() {
let mock_runnable = MockTestRunnable::new();
let mut service_thread =
ServiceThreadStd::new("TestServiceThread".to_string(), mock_runnable);

service_thread.start();
service_thread.wakeup();
// We expect that the wakeup method is called successfully.
}*/
}
Loading
Loading