Skip to content

Commit 13449d8

Browse files
committed
Functional sharding
1 parent 3d1498b commit 13449d8

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

pgdog/src/backend/pool/cluster.rs

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ mod test {
210210
name: Some("sharded".into()),
211211
column: "id".into(),
212212
primary: true,
213+
shard: None,
213214
}]),
214215
shards: vec![Shard::default(), Shard::default()],
215216
..Default::default()

pgdog/src/config/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,17 @@ pub struct ShardedTable {
528528
/// This table is the primary sharding anchor (e.g. "users").
529529
#[serde(default)]
530530
pub primary: bool,
531+
/// Hardcoded shard number.
532+
pub shard: Option<usize>,
531533
}
532534

533535
/// Queries with manual routing rules.
534536
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
535537
pub struct ManualQuery {
538+
/// Query fingerprint.
536539
pub fingerprint: String,
540+
/// Which shard this query should go to.
541+
pub shard: Option<usize>,
537542
}
538543

539544
#[cfg(test)]

pgdog/src/frontend/router/parser/query.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,12 @@ impl QueryParser {
179179
let fingerprint = fingerprint(query).map_err(Error::PgQuery)?;
180180
let manual_route = databases().manual_query(&fingerprint.hex).cloned();
181181

182-
// TODO: check routing logic required by config.
183-
if manual_route.is_some() {
184-
route.set_shard(round_robin::next() % cluster.shards().len());
182+
if let Some(manual_route) = manual_route {
183+
if let Some(shard) = manual_route.shard {
184+
route.set_shard(shard);
185+
} else {
186+
route.set_shard(round_robin::next() % cluster.shards().len());
187+
}
185188
}
186189
}
187190
}

0 commit comments

Comments
 (0)