-
Notifications
You must be signed in to change notification settings - Fork 172
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
Introduce least-outstanding-connections load balancing #282
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f3d4ff3
Introduce least-outstanding-queries load balancing
drdrsh 110df88
fmt
drdrsh 7abdbd2
more slow queries
drdrsh 42518d5
Add startup logs
drdrsh 911e02e
LOC
drdrsh 492b9e1
fmt
drdrsh 532c487
use pool_state
drdrsh 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 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 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ use std::collections::{HashMap, HashSet}; | |
use std::sync::Arc; | ||
use std::time::Instant; | ||
|
||
use crate::config::{get_config, Address, General, PoolMode, Role, User}; | ||
use crate::config::{get_config, Address, General, LoadBalancingMode, PoolMode, Role, User}; | ||
use crate::errors::Error; | ||
|
||
use crate::server::Server; | ||
|
@@ -62,6 +62,9 @@ pub struct PoolSettings { | |
/// Transaction or Session. | ||
pub pool_mode: PoolMode, | ||
|
||
/// Random or LeastOutstandingConnections. | ||
pub load_balancing_mode: LoadBalancingMode, | ||
|
||
// Number of shards. | ||
pub shards: usize, | ||
|
||
|
@@ -94,6 +97,7 @@ impl Default for PoolSettings { | |
fn default() -> PoolSettings { | ||
PoolSettings { | ||
pool_mode: PoolMode::Transaction, | ||
load_balancing_mode: LoadBalancingMode::Random, | ||
shards: 1, | ||
user: User::default(), | ||
default_role: None, | ||
|
@@ -257,6 +261,7 @@ impl ConnectionPool { | |
server_info: BytesMut::new(), | ||
settings: PoolSettings { | ||
pool_mode: pool_config.pool_mode, | ||
load_balancing_mode: pool_config.load_balancing_mode, | ||
// shards: pool_config.shards.clone(), | ||
shards: shard_ids.len(), | ||
user: user.clone(), | ||
|
@@ -356,8 +361,17 @@ impl ConnectionPool { | |
.filter(|address| address.role == role) | ||
.collect(); | ||
|
||
// Random load balancing | ||
// We shuffle even if least_outstanding_queries is used to avoid imbalance | ||
// in cases where all candidates have more or less the same number of outstanding | ||
// queries | ||
candidates.shuffle(&mut thread_rng()); | ||
if self.settings.load_balancing_mode == LoadBalancingMode::LeastOutstandingConnections { | ||
candidates.sort_by(|a, b| { | ||
self.busy_connection_count(b) | ||
.partial_cmp(&self.busy_connection_count(a)) | ||
.unwrap() | ||
}); | ||
} | ||
|
||
while !candidates.is_empty() { | ||
// Get the next candidate | ||
|
@@ -565,6 +579,20 @@ impl ConnectionPool { | |
pub fn server_info(&self) -> BytesMut { | ||
self.server_info.clone() | ||
} | ||
|
||
fn busy_connection_count(&self, address: &Address) -> u32 { | ||
let state = self.databases[address.shard][address.address_index].state(); | ||
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. |
||
let idle = state.idle_connections; | ||
let provisioned = state.connections; | ||
|
||
if idle > provisioned { | ||
// Unlikely but avoids an overflow panic if this ever happens | ||
return 0; | ||
} | ||
let busy = provisioned - idle; | ||
debug!("{:?} has {:?} busy connections", address, busy); | ||
return busy; | ||
} | ||
} | ||
|
||
/// Wrapper for the bb8 connection pool. | ||
|
This file contains 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 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 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
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.
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.
This is actually, the least_checked_out_connections which can be skewed by clients that are idle in transactions, I think we should be fine though.