This repository was archived by the owner on Jan 2, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 57
Send prepared statements to all nodes #320
Open
ghost
wants to merge
1
commit into
AlexPikalov:master
Choose a base branch
from
unknown repository
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
use std::cell::RefCell; | ||
|
||
use crate::cluster::{GetCompressor, GetConnection}; | ||
use crate::cluster::{GetCompressor, GetConnection, GetAllConnections}; | ||
use crate::error; | ||
use crate::frame::parser::from_connection; | ||
use crate::frame::{Flag, Frame}; | ||
use crate::transport::CDRSTransport; | ||
use crate::compression::Compression; | ||
use r2d2::PooledConnection; | ||
use std::error::Error; | ||
|
||
pub fn prepare_flags(with_tracing: bool, with_warnings: bool) -> Vec<Flag> { | ||
let mut flags = vec![]; | ||
|
@@ -41,6 +44,55 @@ where | |
.and_then(|transport_cell| from_connection(&transport_cell, compression)) | ||
} | ||
|
||
pub fn send_frame_to_all_connections<S, T, M>(sender: &S, frame_bytes: Vec<u8>) -> error::Result<Frame> | ||
where | ||
S: GetAllConnections<T, M> + GetCompressor<'static> + Sized, | ||
T: CDRSTransport + 'static, | ||
M: r2d2::ManageConnection<Connection=RefCell<T>, Error=error::Error> + Sized, | ||
{ | ||
let ref compression = sender.get_compressor(); | ||
|
||
let mut results: Vec<error::Result<Frame>> = Vec::new(); | ||
|
||
for connection in sender.get_all_connections() { | ||
match connection { | ||
Some(conn) => { | ||
results.push(write_frame_to_connection(&frame_bytes, compression, conn)); | ||
} | ||
None => { results.push(transform_error()); } | ||
} | ||
} | ||
|
||
let result = results.iter().find(|r| r.is_err()) | ||
.unwrap_or_else( ||get_any_valid_result(&results)); | ||
|
||
match result { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this statement can be simplified so that no clone is needed. Please consider following line to put instead the whole result.map_err(|err| error::Error::General(e.description().to_string()) |
||
Ok(frame) => Result::Ok(frame.clone()), | ||
Err(e) => Result::Err(error::Error::General(e.description().to_string())) | ||
} | ||
} | ||
|
||
fn get_any_valid_result(results: &Vec<error::Result<Frame>>) -> &error::Result<Frame> { | ||
results.iter().find(|r| r.is_ok()).unwrap() | ||
} | ||
|
||
fn transform_error() -> error::Result<Frame> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A small typo. Perhaps it had to be |
||
return error::Result::Err(error::Error::from("Unable to get transport")); | ||
} | ||
|
||
fn write_frame_to_connection<T, M>(frame_bytes: &Vec<u8>, compression: &Compression, conn: PooledConnection<M>) -> error::Result<Frame> | ||
where | ||
T: CDRSTransport + 'static, | ||
M: r2d2::ManageConnection<Connection=RefCell<T>, Error=error::Error> + Sized { | ||
let result = conn | ||
.borrow_mut() | ||
.write(frame_bytes.as_slice()) | ||
.map_err(error::Error::from); | ||
|
||
result.map(|_| conn) | ||
.and_then(|transport_cell| from_connection(&transport_cell, compression)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, getting any valid result may not work as expected. Each node will prepare a query with a different ID. That's why if we have a cluster with >1 nodes load balancer may select a "wrong" node which has the query prepared with a different ID.
What I had been thinking about is to have a map of prepared queries to nodes so that during preparing new entity is added to this map. Whenever
exec
-like call is made, the executor picks up a node where the prepared query ID is residing. However for that aLoadBalancingStrategy
should be able to return a node basing on providing criteria, e.g.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I will work on it.