11//! A collection of replicas and a primary.
22
33use parking_lot:: { Mutex , RwLock } ;
4+ use pgdog_config:: { PreparedStatements , Rewrite } ;
45use std:: sync:: {
56 atomic:: { AtomicBool , Ordering } ,
67 Arc ,
@@ -52,6 +53,12 @@ pub struct Cluster {
5253 two_phase_commit : bool ,
5354 two_phase_commit_auto : bool ,
5455 online : Arc < AtomicBool > ,
56+ rewrite : Rewrite ,
57+ prepared_statements : PreparedStatements ,
58+ dry_run : bool ,
59+ expanded_explain : bool ,
60+ pub_sub_channel_size : usize ,
61+ query_parser_enabled : bool ,
5562}
5663
5764/// Sharding configuration from the cluster.
@@ -94,6 +101,12 @@ pub struct ClusterConfig<'a> {
94101 pub two_pc : bool ,
95102 pub two_pc_auto : bool ,
96103 pub sharded_schemas : ShardedSchemas ,
104+ pub rewrite : & ' a Rewrite ,
105+ pub prepared_statements : & ' a PreparedStatements ,
106+ pub dry_run : bool ,
107+ pub expanded_explain : bool ,
108+ pub pub_sub_channel_size : usize ,
109+ pub query_parser_enabled : bool ,
97110}
98111
99112impl < ' a > ClusterConfig < ' a > {
@@ -104,6 +117,7 @@ impl<'a> ClusterConfig<'a> {
104117 sharded_tables : ShardedTables ,
105118 multi_tenant : & ' a Option < MultiTenant > ,
106119 sharded_schemas : ShardedSchemas ,
120+ rewrite : & ' a Rewrite ,
107121 ) -> Self {
108122 Self {
109123 name : & user. database ,
@@ -126,6 +140,12 @@ impl<'a> ClusterConfig<'a> {
126140 . two_phase_commit_auto
127141 . unwrap_or ( general. two_phase_commit_auto . unwrap_or ( false ) ) , // Disable by default.
128142 sharded_schemas,
143+ rewrite,
144+ prepared_statements : & general. prepared_statements ,
145+ dry_run : general. dry_run ,
146+ expanded_explain : general. expanded_explain ,
147+ pub_sub_channel_size : general. pub_sub_channel_size ,
148+ query_parser_enabled : general. query_parser_enabled ,
129149 }
130150 }
131151}
@@ -150,6 +170,12 @@ impl Cluster {
150170 two_pc,
151171 two_pc_auto,
152172 sharded_schemas,
173+ rewrite,
174+ prepared_statements,
175+ dry_run,
176+ expanded_explain,
177+ pub_sub_channel_size,
178+ query_parser_enabled,
153179 } = config;
154180
155181 Self {
@@ -175,9 +201,23 @@ impl Cluster {
175201 two_phase_commit : two_pc && shards. len ( ) > 1 ,
176202 two_phase_commit_auto : two_pc_auto && shards. len ( ) > 1 ,
177203 online : Arc :: new ( AtomicBool :: new ( false ) ) ,
204+ rewrite : rewrite. clone ( ) ,
205+ prepared_statements : prepared_statements. clone ( ) ,
206+ dry_run,
207+ expanded_explain,
208+ pub_sub_channel_size,
209+ query_parser_enabled,
178210 }
179211 }
180212
213+ /// Change config to work with logical replication streaming.
214+ pub fn logical_stream ( & self ) -> Self {
215+ let mut cluster = self . clone ( ) ;
216+ // Disable rewrites, we are only sending valid statements.
217+ cluster. rewrite . enabled = false ;
218+ cluster
219+ }
220+
181221 /// Get a connection to a primary of the given shard.
182222 pub async fn primary ( & self , shard : usize , request : & Request ) -> Result < Guard , Error > {
183223 let shard = self . shards . get ( shard) . ok_or ( Error :: NoShard ( shard) ) ?;
@@ -251,6 +291,31 @@ impl Cluster {
251291 self . sharded_tables . tables ( )
252292 }
253293
294+ /// Get query rewrite config.
295+ pub fn rewrite ( & self ) -> & Rewrite {
296+ & self . rewrite
297+ }
298+
299+ pub fn query_parser_enabled ( & self ) -> bool {
300+ self . query_parser_enabled
301+ }
302+
303+ pub fn prepared_statements ( & self ) -> & PreparedStatements {
304+ & self . prepared_statements
305+ }
306+
307+ pub fn dry_run ( & self ) -> bool {
308+ self . dry_run
309+ }
310+
311+ pub fn expanded_explain ( & self ) -> bool {
312+ self . expanded_explain
313+ }
314+
315+ pub fn pub_sub_enabled ( & self ) -> bool {
316+ self . pub_sub_channel_size > 0
317+ }
318+
254319 /// Find sharded column position, if the table and columns match the configuration.
255320 pub fn sharded_column ( & self , table : & str , columns : & [ & str ] ) -> Option < ShardedColumn > {
256321 self . sharded_tables . sharded_column ( table, columns)
@@ -412,10 +477,12 @@ mod test {
412477 use std:: sync:: Arc ;
413478
414479 use crate :: {
415- backend:: pool:: { Address , Config , PoolConfig } ,
416- backend:: { Shard , ShardedTables } ,
480+ backend:: {
481+ pool:: { Address , Config , PoolConfig } ,
482+ Shard , ShardedTables ,
483+ } ,
417484 config:: {
418- DataType , Hasher , LoadBalancingStrategy , ReadWriteSplit , ReadWriteStrategy ,
485+ config , DataType , Hasher , LoadBalancingStrategy , ReadWriteSplit , ReadWriteStrategy ,
419486 ShardedTable ,
420487 } ,
421488 } ;
@@ -424,6 +491,7 @@ mod test {
424491
425492 impl Cluster {
426493 pub fn new_test ( ) -> Self {
494+ let config = config ( ) ;
427495 Cluster {
428496 sharded_tables : ShardedTables :: new (
429497 vec ! [ ShardedTable {
@@ -470,6 +538,13 @@ mod test {
470538 user : "pgdog" . into ( ) ,
471539 database : "pgdog" . into ( ) ,
472540 } ) ,
541+ prepared_statements : config. config . general . prepared_statements ,
542+ dry_run : config. config . general . dry_run ,
543+ expanded_explain : config. config . general . expanded_explain ,
544+ query_parser_enabled : config. config . general . query_parser_enabled ,
545+ rewrite : config. config . rewrite . clone ( ) ,
546+ two_phase_commit : config. config . general . two_phase_commit ,
547+ two_phase_commit_auto : config. config . general . two_phase_commit_auto . unwrap_or ( false ) ,
473548 ..Default :: default ( )
474549 }
475550 }
0 commit comments