-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcopy.rs
More file actions
386 lines (347 loc) · 11.9 KB
/
copy.rs
File metadata and controls
386 lines (347 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use diesel::{ExpressionMethods, JoinOnDsl, OptionalExtension, QueryDsl, RunQueryDsl};
use std::{collections::HashMap, sync::Arc, time::SystemTime};
use graph::{
components::store::{BlockStore as _, DeploymentId, DeploymentLocator},
data::query::QueryTarget,
prelude::{
anyhow::{anyhow, bail, Error},
chrono::{DateTime, Duration, SecondsFormat, Utc},
BlockPtr, ChainStore, DeploymentHash, NodeId, QueryStoreManager,
},
};
use graph_store_postgres::{
command_support::{
catalog::{self, copy_state, copy_table_state},
on_sync, OnSync,
},
PRIMARY_SHARD,
};
use graph_store_postgres::{connection_pool::ConnectionPool, Shard, Store, SubgraphStore};
use crate::manager::deployment::DeploymentSearch;
use crate::manager::display::List;
type UtcDateTime = DateTime<Utc>;
#[derive(Queryable, QueryableByName, Debug)]
#[diesel(table_name = copy_state)]
struct CopyState {
src: i32,
dst: i32,
#[allow(dead_code)]
target_block_hash: Vec<u8>,
target_block_number: i32,
started_at: UtcDateTime,
finished_at: Option<UtcDateTime>,
cancelled_at: Option<UtcDateTime>,
}
#[derive(Queryable, QueryableByName, Debug)]
#[diesel(table_name = copy_table_state)]
struct CopyTableState {
#[allow(dead_code)]
id: i32,
entity_type: String,
#[allow(dead_code)]
dst: i32,
next_vid: i64,
target_vid: i64,
batch_size: i64,
#[allow(dead_code)]
started_at: UtcDateTime,
finished_at: Option<UtcDateTime>,
duration_ms: i64,
}
impl CopyState {
fn find(
pools: &HashMap<Shard, ConnectionPool>,
shard: &Shard,
dst: i32,
) -> Result<Option<(CopyState, Vec<CopyTableState>, OnSync)>, Error> {
use copy_state as cs;
use copy_table_state as cts;
let dpool = pools
.get(shard)
.ok_or_else(|| anyhow!("can not find pool for shard {}", shard))?;
let mut dconn = dpool.get()?;
let tables = cts::table
.filter(cts::dst.eq(dst))
.order_by(cts::entity_type)
.load::<CopyTableState>(&mut dconn)?;
let on_sync = on_sync(&mut dconn, DeploymentId(dst))?;
Ok(cs::table
.filter(cs::dst.eq(dst))
.get_result::<CopyState>(&mut dconn)
.optional()?
.map(|state| (state, tables, on_sync)))
}
}
async fn create_inner(
store: Arc<Store>,
src: &DeploymentLocator,
shard: String,
shards: Vec<String>,
node: String,
block_offset: u32,
activate: bool,
replace: bool,
) -> Result<(), Error> {
let block_offset = block_offset as i32;
let on_sync = match (activate, replace) {
(true, true) => bail!("--activate and --replace can't both be specified"),
(true, false) => OnSync::Activate,
(false, true) => OnSync::Replace,
(false, false) => OnSync::None,
};
let subgraph_store = store.subgraph_store();
let query_store = store
.query_store(QueryTarget::Deployment(
src.hash.clone(),
Default::default(),
))
.await?;
let network = query_store.network_name();
let src_ptr = query_store.block_ptr().await?.ok_or_else(|| anyhow!("subgraph {} has not indexed any blocks yet and can not be used as the source of a copy", src))?;
let src_number = if src_ptr.number <= block_offset {
bail!("subgraph {} has only indexed up to block {}, but we need at least block {} before we can copy from it", src, src_ptr.number, block_offset);
} else {
src_ptr.number - block_offset
};
let chain_store = store
.block_store()
.chain_store(network)
.ok_or_else(|| anyhow!("could not find chain store for network {}", network))?;
let mut hashes = chain_store.block_hashes_by_block_number(src_number)?;
let hash = match hashes.len() {
0 => bail!(
"could not find a block with number {} in our cache",
src_number
),
1 => hashes.pop().unwrap(),
n => bail!(
"the cache contains {} hashes for block number {}",
n,
src_number
),
};
let base_ptr = BlockPtr::new(hash, src_number);
if !shards.contains(&shard) {
bail!(
"unknown shard {shard}, only shards {} are configured",
shards.join(", ")
)
}
let shard = Shard::new(shard)?;
let node = NodeId::new(node.clone()).map_err(|()| anyhow!("invalid node id `{}`", node))?;
let dst = subgraph_store.copy_deployment(&src, shard, node, base_ptr, on_sync)?;
println!("created deployment {} as copy of {}", dst, src);
Ok(())
}
pub async fn create(
store: Arc<Store>,
primary: ConnectionPool,
src: DeploymentSearch,
shard: String,
shards: Vec<String>,
node: String,
block_offset: u32,
activate: bool,
replace: bool,
) -> Result<(), Error> {
let src = src.locate_unique(&primary)?;
create_inner(
store,
&src,
shard,
shards,
node,
block_offset,
activate,
replace,
)
.await
.map_err(|e| anyhow!("cannot copy {src}: {e}"))
}
pub fn activate(store: Arc<SubgraphStore>, deployment: String, shard: String) -> Result<(), Error> {
let shard = Shard::new(shard)?;
let deployment =
DeploymentHash::new(deployment).map_err(|s| anyhow!("illegal deployment hash `{}`", s))?;
let deployment = store
.locate_in_shard(&deployment, shard.clone())?
.ok_or_else(|| {
anyhow!(
"could not find a copy for {} in shard {}",
deployment,
shard
)
})?;
store.activate(&deployment)?;
println!("activated copy {}", deployment);
Ok(())
}
pub fn list(pools: HashMap<Shard, ConnectionPool>) -> Result<(), Error> {
use catalog::active_copies as ac;
use catalog::deployment_schemas as ds;
let primary = pools.get(&*PRIMARY_SHARD).expect("there is a primary pool");
let mut conn = primary.get()?;
let copies = ac::table
.inner_join(ds::table.on(ds::id.eq(ac::dst)))
.select((
ac::src,
ac::dst,
ac::cancelled_at,
ac::queued_at,
ds::subgraph,
ds::shard,
))
.load::<(i32, i32, Option<UtcDateTime>, UtcDateTime, String, Shard)>(&mut conn)?;
if copies.is_empty() {
println!("no active copies");
} else {
fn status(name: &str, at: UtcDateTime) {
println!(
"{:20} | {}",
name,
at.to_rfc3339_opts(SecondsFormat::Secs, false)
);
}
for (src, dst, cancelled_at, queued_at, deployment_hash, shard) in copies {
println!("{:-<78}", "");
println!("{:20} | {}", "deployment", deployment_hash);
println!("{:20} | sgd{} -> sgd{} ({})", "action", src, dst, shard);
match CopyState::find(&pools, &shard, dst)? {
Some((state, tables, _)) => match cancelled_at {
Some(cancel_requested) => match state.cancelled_at {
Some(cancelled_at) => status("cancelled", cancelled_at),
None => status("cancel requested", cancel_requested),
},
None => match state.finished_at {
Some(finished_at) => status("finished", finished_at),
None => {
let target: i64 = tables.iter().map(|table| table.target_vid).sum();
let next: i64 = tables.iter().map(|table| table.next_vid).sum();
let done = next as f64 / target as f64 * 100.0;
status("started", state.started_at);
println!("{:20} | {:.2}% done, {}/{}", "progress", done, next, target)
}
},
},
None => status("queued", queued_at),
};
}
}
Ok(())
}
pub fn status(pools: HashMap<Shard, ConnectionPool>, dst: &DeploymentSearch) -> Result<(), Error> {
const CHECK: &str = "✓";
use catalog::active_copies as ac;
use catalog::deployment_schemas as ds;
fn duration(start: &UtcDateTime, end: &Option<UtcDateTime>) -> String {
let start = *start;
let end = *end;
let end = end.unwrap_or(UtcDateTime::from(SystemTime::now()));
let duration = end - start;
human_duration(duration)
}
fn human_duration(duration: Duration) -> String {
if duration.num_seconds() < 5 {
format!("{}ms", duration.num_milliseconds())
} else if duration.num_minutes() < 5 {
format!("{}s", duration.num_seconds())
} else {
format!("{}m", duration.num_minutes())
}
}
let primary = pools
.get(&*PRIMARY_SHARD)
.ok_or_else(|| anyhow!("can not find deployment with id {}", dst))?;
let mut pconn = primary.get()?;
let dst = dst.locate_unique(primary)?.id.0;
let (shard, deployment) = ds::table
.filter(ds::id.eq(dst))
.select((ds::shard, ds::subgraph))
.get_result::<(Shard, String)>(&mut pconn)?;
let (active, cancelled_at) = ac::table
.filter(ac::dst.eq(dst))
.select((ac::src, ac::cancelled_at))
.get_result::<(i32, Option<UtcDateTime>)>(&mut pconn)
.optional()?
.map(|(_, cancelled_at)| (true, cancelled_at))
.unwrap_or((false, None));
let (state, tables, on_sync) = match CopyState::find(&pools, &shard, dst)? {
Some((state, tables, on_sync)) => (state, tables, on_sync),
None => {
if active {
println!("copying is queued but has not started");
return Ok(());
} else {
bail!("no copy operation for {} exists", dst);
}
}
};
let progress = match &state.finished_at {
Some(_) => CHECK.to_string(),
None => {
let target: i64 = tables.iter().map(|table| table.target_vid).sum();
let next: i64 = tables.iter().map(|table| table.next_vid).sum();
let pct = next as f64 / target as f64 * 100.0;
format!("{:.2}% done, {}/{}", pct, next, target)
}
};
let mut lst = vec![
"deployment",
"src",
"dst",
"target block",
"on sync",
"duration",
"status",
];
let mut vals = vec![
deployment,
state.src.to_string(),
state.dst.to_string(),
state.target_block_number.to_string(),
on_sync.to_str().to_string(),
duration(&state.started_at, &state.finished_at),
progress,
];
match (cancelled_at, state.cancelled_at) {
(Some(c), None) => {
lst.push("cancel");
vals.push(format!("requested at {}", c));
}
(_, Some(c)) => {
lst.push("cancel");
vals.push(format!("cancelled at {}", c));
}
(None, None) => {}
}
let mut lst = List::new(lst);
lst.append(vals);
lst.render();
println!();
println!(
"{:^30} | {:^8} | {:^8} | {:^8} | {:^8}",
"entity type", "next", "target", "batch", "duration"
);
println!("{:-<74}", "-");
for table in tables {
let status = match &table.finished_at {
// table finished
Some(_) => CHECK,
// empty source table
None if table.target_vid < 0 => CHECK,
// copying in progress
None if table.duration_ms > 0 => ">",
// not started
None => ".",
};
println!(
"{} {:<28} | {:>8} | {:>8} | {:>8} | {:>8}",
status,
table.entity_type,
table.next_vid,
table.target_vid,
table.batch_size,
human_duration(Duration::milliseconds(table.duration_ms)),
);
}
Ok(())
}