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

feat(redis): Use parallel iterators for multi-write #4273

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions relay-redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ redis = { workspace = true, optional = true, features = [
relay-log = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
rayon = { workspace = true }

[features]
default = []
Expand Down
56 changes: 29 additions & 27 deletions relay-redis/src/real.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
use r2d2::{Builder, ManageConnection, Pool, PooledConnection};
use rayon::prelude::*;
use rayon::ThreadPool;
use rayon::ThreadPoolBuilder;
pub use redis;
use redis::ConnectionLike;
use std::error::Error;
use std::thread::Scope;
use std::fmt;
use std::sync::LazyLock;
use std::time::Duration;
use std::{fmt, thread};
use thiserror::Error;

use crate::config::RedisConfigOptions;

static REDIS_SECONDARIES_THREAD_POOL: LazyLock<ThreadPool> = LazyLock::new(|| {
ThreadPoolBuilder::new()
.num_threads(10)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arbitrary number for now.

.thread_name(|i| format!("redis-secondary-{}", i))
.build()
.expect("Failed to create Redis secondaries thread pool")
});

/// An error returned from `RedisPool`.
#[derive(Debug, Error)]
pub enum RedisError {
Expand All @@ -34,21 +45,6 @@ fn log_secondary_redis_error<T>(result: redis::RedisResult<T>) {
}
}

fn spawn_secondary_thread<'scope, 'env: 'scope, T>(
scope: &'scope Scope<'scope, 'env>,
block: impl FnOnce() -> redis::RedisResult<T> + Send + 'scope,
) {
let result = thread::Builder::new().spawn_scoped(scope, move || {
log_secondary_redis_error(block());
});
if let Err(error) = result {
relay_log::error!(
error = &error as &dyn Error,
"spawning the thread for the secondary Redis connection failed",
);
}
}

enum ConnectionInner<'a> {
Cluster(&'a mut redis::cluster::ClusterConnection),
MultiWrite {
Expand All @@ -66,12 +62,14 @@ impl ConnectionLike for ConnectionInner<'_> {
ConnectionInner::MultiWrite {
primary,
secondaries,
} => thread::scope(|s| {
for connection in secondaries {
spawn_secondary_thread(s, || connection.req_packed_command(cmd))
}
} => {
REDIS_SECONDARIES_THREAD_POOL.install(|| {
secondaries.par_iter_mut().for_each(|connection| {
log_secondary_redis_error(connection.req_packed_command(cmd));
});
});
primary.req_packed_command(cmd)
}),
}
}
}

Expand All @@ -87,12 +85,16 @@ impl ConnectionLike for ConnectionInner<'_> {
ConnectionInner::MultiWrite {
primary,
secondaries,
} => thread::scope(|s| {
for connection in secondaries {
spawn_secondary_thread(s, || connection.req_packed_commands(cmd, offset, count))
}
} => {
REDIS_SECONDARIES_THREAD_POOL.install(|| {
secondaries.par_iter_mut().for_each(|connection| {
log_secondary_redis_error(
connection.req_packed_commands(cmd, offset, count),
);
});
});
primary.req_packed_commands(cmd, offset, count)
}),
}
}
}

Expand Down
Loading