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

Use Value::Map to aggregate cluster responses. #72

Merged
merged 1 commit into from
Dec 11, 2023
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
7 changes: 2 additions & 5 deletions redis/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,18 +586,15 @@ where
}
Some(ResponsePolicy::Special) | None => {
// This is our assumption - if there's no coherent way to aggregate the responses, we just map each response to the sender, and pass it to the user.
// TODO - once RESP3 is merged, return a map value here.
// TODO - once Value::Error is merged, we can use join_all and report separate errors and also pass successes.
let results = results
.enumerate()
.map(|(index, result)| {
let addr = addresses[index];
result.map(|val| {
Value::Array(vec![Value::BulkString(addr.as_bytes().to_vec()), val])
})
result.map(|val| (Value::BulkString(addr.as_bytes().to_vec()), val))
})
.collect::<RedisResult<Vec<_>>>()?;
Ok(Value::Array(results))
Ok(Value::Map(results))
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions redis/src/cluster_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,17 +737,13 @@ where
}
Some(ResponsePolicy::Special) | None => {
// This is our assumption - if there's no coherent way to aggregate the responses, we just map each response to the sender, and pass it to the user.
// TODO - once RESP3 is merged, return a map value here.
// TODO - once Value::Error is merged, we can use join_all and report separate errors and also pass successes.
future::try_join_all(receivers.into_iter().map(|(addr, receiver)| async move {
let result = convert_result(receiver.await)?;
Ok(Value::Array(vec![
Value::BulkString(addr.as_bytes().to_vec()),
result,
]))
Ok((Value::BulkString(addr.as_bytes().to_vec()), result))
}))
.await
.map(Value::Array)
.map(Value::Map)
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions redis/tests/test_cluster_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ fn test_async_cluster_route_info_to_nodes() {
let cluster = TestClusterContext::new(12, 1);

let split_to_addresses_and_info = |res| -> (Vec<String>, Vec<String>) {
if let Value::Array(values) = res {
if let Value::Map(values) = res {
let mut pairs: Vec<_> = values
.into_iter()
.map(|value| redis::from_redis_value::<(String, String)>(&value).unwrap())
.map(|(key, value)| {
(
redis::from_redis_value::<String>(&key).unwrap(),
redis::from_redis_value::<String>(&value).unwrap(),
)
})
.collect();
pairs.sort_by(|(address1, _), (address2, _)| address1.cmp(address2));
pairs.into_iter().unzip()
Expand Down Expand Up @@ -1472,18 +1477,17 @@ fn test_cluster_fan_out_and_return_map_of_results_for_special_response_policy()
},
);

// TODO once RESP3 is in, return this as a map
let mut result = runtime
.block_on(cmd.query_async::<_, Vec<Vec<String>>>(&mut connection))
.block_on(cmd.query_async::<_, Vec<(String, String)>>(&mut connection))
.unwrap();
result.sort();
assert_eq!(
result,
vec![
vec![format!("{name}:6379"), "latency: 6379".to_string()],
vec![format!("{name}:6380"), "latency: 6380".to_string()],
vec![format!("{name}:6381"), "latency: 6381".to_string()],
vec![format!("{name}:6382"), "latency: 6382".to_string()]
(format!("{name}:6379"), "latency: 6379".to_string()),
(format!("{name}:6380"), "latency: 6380".to_string()),
(format!("{name}:6381"), "latency: 6381".to_string()),
(format!("{name}:6382"), "latency: 6382".to_string())
],
"{result:?}"
);
Expand Down