@@ -37,15 +37,15 @@ use graph::{
37
37
info, lazy_static, o, warn, BlockNumber , BlockPtr , CheapClone , Logger , StoreError , ENV_VARS ,
38
38
} ,
39
39
schema:: EntityType ,
40
- slog:: { debug , error} ,
40
+ slog:: error,
41
41
tokio,
42
42
} ;
43
43
use itertools:: Itertools ;
44
44
45
45
use crate :: {
46
46
advisory_lock, catalog, deployment,
47
47
dynds:: DataSourcesTable ,
48
- primary:: { DeploymentId , Site } ,
48
+ primary:: { DeploymentId , Primary , Site } ,
49
49
relational:: index:: IndexList ,
50
50
vid_batcher:: { VidBatcher , VidRange } ,
51
51
} ;
@@ -104,46 +104,6 @@ table! {
104
104
}
105
105
}
106
106
107
- // This is the same as primary::active_copies, but mapped into each shard
108
- table ! {
109
- primary_public. active_copies( dst) {
110
- src -> Integer ,
111
- dst -> Integer ,
112
- cancelled_at -> Nullable <Date >,
113
- }
114
- }
115
-
116
- /// Return `true` if the site is the source of a copy operation. The copy
117
- /// operation might be just queued or in progress already. This method will
118
- /// block until a fdw connection becomes available.
119
- pub fn is_source ( logger : & Logger , pool : & ConnectionPool , site : & Site ) -> Result < bool , StoreError > {
120
- use active_copies as ac;
121
-
122
- // We use a fdw connection to check if the site is being copied. If we
123
- // used an ordinary connection and there are many calls to this method,
124
- // postgres_fdw might open an unmanageable number of connections into
125
- // the primary, which makes the primary run out of connections
126
- let mut last_log = Instant :: now ( ) ;
127
- let mut conn = pool. get_fdw ( & logger, || {
128
- if last_log. elapsed ( ) > LOG_INTERVAL {
129
- last_log = Instant :: now ( ) ;
130
- debug ! (
131
- logger,
132
- "Waiting for fdw connection to check if site {} is being copied" , site. namespace
133
- ) ;
134
- }
135
- false
136
- } ) ?;
137
-
138
- select ( diesel:: dsl:: exists (
139
- ac:: table
140
- . filter ( ac:: src. eq ( site. id ) )
141
- . filter ( ac:: cancelled_at. is_null ( ) ) ,
142
- ) )
143
- . get_result :: < bool > ( & mut conn)
144
- . map_err ( StoreError :: from)
145
- }
146
-
147
107
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
148
108
pub enum Status {
149
109
Finished ,
@@ -161,6 +121,7 @@ struct CopyState {
161
121
impl CopyState {
162
122
fn new (
163
123
conn : & mut PgConnection ,
124
+ primary : Primary ,
164
125
src : Arc < Layout > ,
165
126
dst : Arc < Layout > ,
166
127
target_block : BlockPtr ,
@@ -199,21 +160,22 @@ impl CopyState {
199
160
src. site. id
200
161
) ) ;
201
162
}
202
- Self :: load ( conn, src, dst, target_block)
163
+ Self :: load ( conn, primary , src, dst, target_block)
203
164
}
204
- None => Self :: create ( conn, src, dst, target_block) ,
165
+ None => Self :: create ( conn, primary . cheap_clone ( ) , src, dst, target_block) ,
205
166
} ?;
206
167
207
168
Ok ( state)
208
169
}
209
170
210
171
fn load (
211
172
conn : & mut PgConnection ,
173
+ primary : Primary ,
212
174
src : Arc < Layout > ,
213
175
dst : Arc < Layout > ,
214
176
target_block : BlockPtr ,
215
177
) -> Result < CopyState , StoreError > {
216
- let tables = TableState :: load ( conn, src. as_ref ( ) , dst. as_ref ( ) ) ?;
178
+ let tables = TableState :: load ( conn, primary , src. as_ref ( ) , dst. as_ref ( ) ) ?;
217
179
let ( finished, mut unfinished) : ( Vec < _ > , Vec < _ > ) =
218
180
tables. into_iter ( ) . partition ( |table| table. finished ( ) ) ;
219
181
unfinished. sort_by_key ( |table| table. dst . object . to_string ( ) ) ;
@@ -228,6 +190,7 @@ impl CopyState {
228
190
229
191
fn create (
230
192
conn : & mut PgConnection ,
193
+ primary : Primary ,
231
194
src : Arc < Layout > ,
232
195
dst : Arc < Layout > ,
233
196
target_block : BlockPtr ,
@@ -253,6 +216,7 @@ impl CopyState {
253
216
. map ( |src_table| {
254
217
TableState :: init (
255
218
conn,
219
+ primary. cheap_clone ( ) ,
256
220
dst. site . clone ( ) ,
257
221
& src,
258
222
src_table. clone ( ) ,
@@ -354,6 +318,7 @@ pub(crate) fn source(
354
318
/// transformation. See `CopyEntityBatchQuery` for the details of what
355
319
/// exactly that means
356
320
struct TableState {
321
+ primary : Primary ,
357
322
src : Arc < Table > ,
358
323
dst : Arc < Table > ,
359
324
dst_site : Arc < Site > ,
@@ -364,6 +329,7 @@ struct TableState {
364
329
impl TableState {
365
330
fn init (
366
331
conn : & mut PgConnection ,
332
+ primary : Primary ,
367
333
dst_site : Arc < Site > ,
368
334
src_layout : & Layout ,
369
335
src : Arc < Table > ,
@@ -373,6 +339,7 @@ impl TableState {
373
339
let vid_range = VidRange :: for_copy ( conn, & src, target_block) ?;
374
340
let batcher = VidBatcher :: load ( conn, & src_layout. site . namespace , src. as_ref ( ) , vid_range) ?;
375
341
Ok ( Self {
342
+ primary,
376
343
src,
377
344
dst,
378
345
dst_site,
@@ -387,6 +354,7 @@ impl TableState {
387
354
388
355
fn load (
389
356
conn : & mut PgConnection ,
357
+ primary : Primary ,
390
358
src_layout : & Layout ,
391
359
dst_layout : & Layout ,
392
360
) -> Result < Vec < TableState > , StoreError > {
@@ -450,6 +418,7 @@ impl TableState {
450
418
. with_batch_size ( size as usize ) ;
451
419
452
420
Ok ( TableState {
421
+ primary : primary. cheap_clone ( ) ,
453
422
src,
454
423
dst,
455
424
dst_site : dst_layout. site . clone ( ) ,
@@ -516,13 +485,8 @@ impl TableState {
516
485
}
517
486
518
487
fn is_cancelled ( & self , conn : & mut PgConnection ) -> Result < bool , StoreError > {
519
- use active_copies as ac;
520
-
521
488
let dst = self . dst_site . as_ref ( ) ;
522
- let canceled = ac:: table
523
- . filter ( ac:: dst. eq ( dst. id ) )
524
- . select ( ac:: cancelled_at. is_not_null ( ) )
525
- . get_result :: < bool > ( conn) ?;
489
+ let canceled = self . primary . is_copy_cancelled ( dst) ?;
526
490
if canceled {
527
491
use copy_state as cs;
528
492
@@ -893,6 +857,7 @@ pub struct Connection {
893
857
/// `self.transaction`
894
858
conn : Option < PooledPgConnection > ,
895
859
pool : ConnectionPool ,
860
+ primary : Primary ,
896
861
workers : usize ,
897
862
src : Arc < Layout > ,
898
863
dst : Arc < Layout > ,
@@ -910,6 +875,7 @@ impl Connection {
910
875
/// is available.
911
876
pub fn new (
912
877
logger : & Logger ,
878
+ primary : Primary ,
913
879
pool : ConnectionPool ,
914
880
src : Arc < Layout > ,
915
881
dst : Arc < Layout > ,
@@ -942,6 +908,7 @@ impl Connection {
942
908
logger,
943
909
conn,
944
910
pool,
911
+ primary,
945
912
workers : ENV_VARS . store . batch_workers ,
946
913
src,
947
914
dst,
@@ -1079,7 +1046,9 @@ impl Connection {
1079
1046
let src = self . src . clone ( ) ;
1080
1047
let dst = self . dst . clone ( ) ;
1081
1048
let target_block = self . target_block . clone ( ) ;
1082
- let mut state = self . transaction ( |conn| CopyState :: new ( conn, src, dst, target_block) ) ?;
1049
+ let primary = self . primary . cheap_clone ( ) ;
1050
+ let mut state =
1051
+ self . transaction ( |conn| CopyState :: new ( conn, primary, src, dst, target_block) ) ?;
1083
1052
1084
1053
let progress = Arc :: new ( CopyProgress :: new ( self . logger . cheap_clone ( ) , & state) ) ;
1085
1054
progress. start ( ) ;
0 commit comments