-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbackup_engine.rs
More file actions
executable file
·627 lines (574 loc) · 24.2 KB
/
Copy pathbackup_engine.rs
File metadata and controls
executable file
·627 lines (574 loc) · 24.2 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Copyright 2026 ExtendDB contributors
// SPDX-License-Identifier: Apache-2.0
//! Backup and point-in-time recovery implementation for PostgreSQL storage.
use extenddb_core::types::{
BackupDescription, BackupDetails, BackupSummary, ContinuousBackupsDescription,
PointInTimeRecoveryDescription, SourceTableDetails, TableDescription,
};
use extenddb_storage::BackupEngine;
use extenddb_storage::TableEngine;
use extenddb_storage::error::StorageError;
use futures::future::BoxFuture;
use crate::PostgresEngine;
use crate::data::data_table_name;
/// Current epoch milliseconds for unique ARN generation.
fn epoch_millis() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis()
}
/// Convert a PostgreSQL `TIMESTAMPTZ` to epoch seconds as `f64`.
#[allow(clippy::cast_precision_loss)]
fn pg_timestamp_to_epoch(ts: time::OffsetDateTime) -> f64 {
ts.unix_timestamp() as f64
}
impl BackupEngine for PostgresEngine {
fn create_backup(
&self,
account_id: &str,
table_name: &str,
backup_name: &str,
) -> BoxFuture<'_, Result<BackupDetails, StorageError>> {
let account_id = account_id.to_string();
let table_name = table_name.to_string();
let backup_name = backup_name.to_string();
Box::pin(async move {
// Verify table exists and get metadata.
let row: (
String,
String,
serde_json::Value,
serde_json::Value,
String,
i64,
i64,
String,
) = sqlx::query_as(
"SELECT table_id, table_arn, key_schema, attribute_definitions, \
billing_mode, table_size_bytes, item_count, \
COALESCE(provisioned_throughput::text, '{}') \
FROM tables WHERE account_id = $1 AND table_name = $2 AND table_status = 'ACTIVE'",
)
.bind(&account_id)
.bind(&table_name)
.fetch_optional(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?
.ok_or_else(|| StorageError::TableNotFound(format!("Table not found: {table_name}")))?;
let (
table_id,
_table_arn,
key_schema,
attr_defs,
billing_mode,
size_bytes,
_item_count,
_prov,
) = row;
let backup_arn = format!(
"arn:aws:dynamodb:{region}:{account_id}:table/{table_name}/backup/{ts}",
region = self.region,
ts = epoch_millis()
);
// Snapshot items from the data table.
let ddb_table = data_table_name(&table_id);
let ddb_table_unquoted = ddb_table.trim_matches('"');
let has_sk: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM information_schema.columns \
WHERE table_name = $1 AND column_name = 'sk')",
)
.bind(ddb_table_unquoted)
.fetch_one(&self.data_pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
let items: Vec<(String, Option<String>, serde_json::Value)> = if has_sk {
sqlx::query_as(&format!("SELECT pk, sk, item_data FROM {ddb_table}"))
.fetch_all(&self.data_pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?
} else {
sqlx::query_as::<_, (String, serde_json::Value)>(&format!(
"SELECT pk, item_data FROM {ddb_table}"
))
.fetch_all(&self.data_pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?
.into_iter()
.map(|(pk, data)| (pk, None, data))
.collect()
};
#[allow(clippy::cast_possible_wrap)]
let actual_count = items.len() as i64;
// Wrap all catalog-side writes in a single transaction so a crash
// cannot leave a backup marked AVAILABLE with partial items.
let mut tx = self
.pool
.begin()
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
sqlx::query(
"INSERT INTO backups (backup_arn, backup_name, table_id, table_name, account_id, \
backup_status, backup_size_bytes, item_count, key_schema, attribute_definitions, \
billing_mode) \
VALUES ($1, $2, $3, $4, $5, 'AVAILABLE', $6, $7, $8, $9, $10)",
)
.bind(&backup_arn)
.bind(&backup_name)
.bind(&table_id)
.bind(&table_name)
.bind(&account_id)
.bind(size_bytes)
.bind(actual_count)
.bind(&key_schema)
.bind(&attr_defs)
.bind(&billing_mode)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
for (pk, sk, item_data) in &items {
sqlx::query(
"INSERT INTO backup_items (backup_arn, pk, sk, item_data) \
VALUES ($1, $2, $3, $4)",
)
.bind(&backup_arn)
.bind(pk)
.bind(sk.as_deref())
.bind(item_data)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
}
// Read back the creation timestamp assigned by the database.
let created_at: time::OffsetDateTime =
sqlx::query_scalar("SELECT created_at FROM backups WHERE backup_arn = $1")
.bind(&backup_arn)
.fetch_one(&mut *tx)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
tx.commit()
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
Ok(BackupDetails {
backup_arn,
backup_name: backup_name.to_owned(),
backup_status: "AVAILABLE".to_owned(),
backup_type: "USER".to_owned(),
backup_size_bytes: size_bytes,
backup_creation_date_time: pg_timestamp_to_epoch(created_at),
})
})
}
fn describe_backup(
&self,
backup_arn: &str,
) -> BoxFuture<'_, Result<BackupDescription, StorageError>> {
let backup_arn = backup_arn.to_string();
Box::pin(async move {
let row: (
String,
String,
String,
String,
String,
i64,
i64,
serde_json::Value,
String,
String,
time::OffsetDateTime,
time::OffsetDateTime,
) = sqlx::query_as(
"SELECT b.backup_name, b.backup_status, b.table_id, b.table_name, b.account_id, \
b.backup_size_bytes, b.item_count, b.key_schema, b.billing_mode, \
COALESCE(t.table_arn, \
'arn:aws:dynamodb:' || $2 || ':' || b.account_id || ':table/' || b.table_name), \
b.created_at, \
COALESCE(t.creation_date_time, b.created_at) \
FROM backups b \
LEFT JOIN tables t ON t.table_id = b.table_id \
WHERE b.backup_arn = $1",
)
.bind(&backup_arn)
.bind(&self.region)
.fetch_optional(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?
.ok_or_else(|| StorageError::Validation(format!("Backup not found: {backup_arn}")))?;
let (
name,
status,
table_id,
table_name,
_account_id,
size,
count,
ks_json,
billing,
table_arn,
backup_created_at,
table_created_at,
) = row;
let key_schema: Vec<extenddb_core::types::KeySchemaElement> =
serde_json::from_value(ks_json)
.map_err(|e| StorageError::Internal(format!("Parse key schema: {e}")))?;
Ok(BackupDescription {
backup_details: BackupDetails {
backup_arn: backup_arn.to_owned(),
backup_name: name,
backup_status: status,
backup_type: "USER".to_owned(),
backup_size_bytes: size,
backup_creation_date_time: pg_timestamp_to_epoch(backup_created_at),
},
source_table_details: SourceTableDetails {
table_name,
table_id,
table_arn,
key_schema,
item_count: count,
table_size_bytes: size,
billing_mode: Some(billing),
table_creation_date_time: pg_timestamp_to_epoch(table_created_at),
},
})
})
}
fn list_backups(
&self,
account_id: &str,
table_name: Option<&str>,
) -> BoxFuture<'_, Result<Vec<BackupSummary>, StorageError>> {
let account_id = account_id.to_string();
let table_name = table_name.map(|s| s.to_string());
Box::pin(async move {
let rows: Vec<(
String,
String,
String,
String,
i64,
String,
time::OffsetDateTime,
)> = if let Some(tn) = table_name {
sqlx::query_as(
"SELECT b.backup_arn, b.backup_name, b.table_name, b.backup_status, \
b.backup_size_bytes, \
COALESCE(t.table_arn, \
'arn:aws:dynamodb:' || $3 || ':' || b.account_id || ':table/' || b.table_name), \
b.created_at \
FROM backups b \
LEFT JOIN tables t ON t.table_id = b.table_id \
WHERE b.account_id = $1 AND b.table_name = $2 AND b.backup_status != 'DELETED' \
ORDER BY b.created_at DESC",
)
.bind(&account_id)
.bind(tn)
.bind(&self.region)
.fetch_all(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?
} else {
sqlx::query_as(
"SELECT b.backup_arn, b.backup_name, b.table_name, b.backup_status, \
b.backup_size_bytes, \
COALESCE(t.table_arn, \
'arn:aws:dynamodb:' || $2 || ':' || b.account_id || ':table/' || b.table_name), \
b.created_at \
FROM backups b \
LEFT JOIN tables t ON t.table_id = b.table_id \
WHERE b.account_id = $1 AND b.backup_status != 'DELETED' \
ORDER BY b.created_at DESC",
)
.bind(&account_id)
.bind(&self.region)
.fetch_all(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?
};
Ok(rows
.into_iter()
.map(
|(arn, name, tn, status, size, table_arn, created_at)| BackupSummary {
backup_arn: arn,
backup_name: name,
table_name: tn,
table_arn,
backup_status: status,
backup_type: "USER".to_owned(),
backup_size_bytes: size,
backup_creation_date_time: pg_timestamp_to_epoch(created_at),
},
)
.collect())
})
}
fn delete_backup(
&self,
backup_arn: &str,
) -> BoxFuture<'_, Result<BackupDescription, StorageError>> {
let backup_arn = backup_arn.to_string();
Box::pin(async move {
let desc = self.describe_backup(&backup_arn).await?;
sqlx::query("DELETE FROM backup_items WHERE backup_arn = $1")
.bind(&backup_arn)
.execute(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
sqlx::query("UPDATE backups SET backup_status = 'DELETED' WHERE backup_arn = $1")
.bind(&backup_arn)
.execute(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
Ok(BackupDescription {
backup_details: BackupDetails {
backup_status: "DELETED".to_owned(),
..desc.backup_details
},
source_table_details: desc.source_table_details,
})
})
}
fn restore_table_from_backup(
&self,
account_id: &str,
target_table_name: &str,
backup_arn: &str,
) -> BoxFuture<'_, Result<TableDescription, StorageError>> {
let account_id = account_id.to_string();
let target_table_name = target_table_name.to_string();
let backup_arn = backup_arn.to_string();
Box::pin(async move {
let backup_row: (
String,
serde_json::Value,
serde_json::Value,
String,
Option<serde_json::Value>,
) = sqlx::query_as(
"SELECT table_name, key_schema, attribute_definitions, billing_mode, \
provisioned_throughput \
FROM backups WHERE backup_arn = $1 AND backup_status = 'AVAILABLE'",
)
.bind(&backup_arn)
.fetch_optional(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?
.ok_or_else(|| StorageError::Validation(format!("Backup not found: {backup_arn}")))?;
let (_orig_table, ks_json, ad_json, billing, _prov) = backup_row;
let key_schema: Vec<extenddb_core::types::KeySchemaElement> =
serde_json::from_value(ks_json)
.map_err(|e| StorageError::Internal(format!("Parse key schema: {e}")))?;
let attr_defs: Vec<extenddb_core::types::AttributeDefinition> =
serde_json::from_value(ad_json)
.map_err(|e| StorageError::Internal(format!("Parse attr defs: {e}")))?;
let billing_mode = if billing == "PAY_PER_REQUEST" {
Some(extenddb_core::types::BillingMode::PayPerRequest)
} else {
Some(extenddb_core::types::BillingMode::Provisioned)
};
let create_input = extenddb_core::types::CreateTableInput {
table_name: target_table_name.to_owned(),
key_schema,
attribute_definitions: attr_defs,
billing_mode,
provisioned_throughput: Some(extenddb_core::types::ProvisionedThroughput {
read_capacity_units: 5,
write_capacity_units: 5,
}),
global_secondary_indexes: None,
local_secondary_indexes: None,
stream_specification: None,
tags: None,
deletion_protection_enabled: None,
sse_specification: None,
table_class: None,
on_demand_throughput: None,
};
let desc = self.create_table(&account_id, create_input).await?;
let new_table_id = &desc.table_id;
let ddb_table = data_table_name(new_table_id);
let ddb_table_unquoted = ddb_table.trim_matches('"');
// Do NOT force ACTIVE — let the control plane handle the transition
// (steering rule D-2: tests run with control_plane_delay_seconds > 0).
// The table starts in CREATING and transitions to ACTIVE after the delay.
let has_sk: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM information_schema.columns \
WHERE table_name = $1 AND column_name = 'sk')",
)
.bind(ddb_table_unquoted)
.fetch_one(&self.data_pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
let items: Vec<(String, Option<String>, serde_json::Value)> =
sqlx::query_as("SELECT pk, sk, item_data FROM backup_items WHERE backup_arn = $1")
.bind(&backup_arn)
.fetch_all(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
for (pk, sk, item_data) in &items {
if has_sk {
sqlx::query(&format!(
"INSERT INTO {ddb_table} (pk, sk, item_data) VALUES ($1, $2, $3)"
))
.bind(pk)
.bind(sk.as_deref())
.bind(item_data)
.execute(&self.data_pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
} else {
sqlx::query(&format!(
"INSERT INTO {ddb_table} (pk, item_data) VALUES ($1, $2)"
))
.bind(pk)
.bind(item_data)
.execute(&self.data_pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
}
}
#[allow(clippy::cast_possible_wrap)]
let item_count = items.len() as i64;
sqlx::query(
"UPDATE tables SET item_count = $1 WHERE account_id = $2 AND table_name = $3",
)
.bind(item_count)
.bind(&account_id)
.bind(&target_table_name)
.execute(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
// Mark the restored table ACTIVE immediately — the data is fully
// populated and the table is ready to serve requests. This matches
// real DynamoDB behavior where restored tables become ACTIVE once
// the restore completes (the CREATING status is transient).
sqlx::query(
"UPDATE tables SET table_status = 'ACTIVE', status_transition_at = NULL \
WHERE account_id = $1 AND table_name = $2",
)
.bind(&account_id)
.bind(&target_table_name)
.execute(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
// Return CREATING — the API response shows the initial status,
// but the table is already ACTIVE by the time the caller polls.
Ok(desc)
})
}
fn describe_continuous_backups(
&self,
account_id: &str,
table_name: &str,
) -> BoxFuture<'_, Result<ContinuousBackupsDescription, StorageError>> {
let account_id = account_id.to_string();
let table_name = table_name.to_string();
Box::pin(async move {
let exists: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM tables WHERE account_id = $1 AND table_name = $2)",
)
.bind(&account_id)
.bind(&table_name)
.fetch_one(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
if !exists {
return Err(StorageError::TableNotFound(format!(
"Table not found: {table_name}"
)));
}
let pitr_row: Option<(bool,)> = sqlx::query_as(
"SELECT pitr_enabled FROM continuous_backups \
WHERE account_id = $1 AND table_name = $2",
)
.bind(&account_id)
.bind(&table_name)
.fetch_optional(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
let pitr_enabled = pitr_row.is_some_and(|r| r.0);
#[allow(clippy::cast_precision_loss)]
let now_epoch = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as f64;
Ok(ContinuousBackupsDescription {
continuous_backups_status: "ENABLED".to_owned(),
point_in_time_recovery_description: Some(PointInTimeRecoveryDescription {
point_in_time_recovery_status: if pitr_enabled {
"ENABLED".to_owned()
} else {
"DISABLED".to_owned()
},
earliest_restorable_date_time: if pitr_enabled {
Some(now_epoch - 35.0 * 24.0 * 3600.0)
} else {
None
},
latest_restorable_date_time: if pitr_enabled { Some(now_epoch) } else { None },
}),
})
})
}
fn update_continuous_backups(
&self,
account_id: &str,
table_name: &str,
pitr_enabled: bool,
) -> BoxFuture<'_, Result<ContinuousBackupsDescription, StorageError>> {
let account_id = account_id.to_string();
let table_name = table_name.to_string();
Box::pin(async move {
let exists: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM tables WHERE account_id = $1 AND table_name = $2)",
)
.bind(&account_id)
.bind(&table_name)
.fetch_one(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
if !exists {
return Err(StorageError::TableNotFound(format!(
"Table not found: {table_name}"
)));
}
sqlx::query(
"INSERT INTO continuous_backups (account_id, table_name, pitr_enabled) \
VALUES ($1, $2, $3) \
ON CONFLICT (account_id, table_name) DO UPDATE SET pitr_enabled = $3",
)
.bind(&account_id)
.bind(&table_name)
.bind(pitr_enabled)
.execute(&self.pool)
.await
.map_err(|e| StorageError::Internal(format!("Database error: {e}")))?;
self.describe_continuous_backups(&account_id, &table_name)
.await
})
}
// TODO(cleanup): This method is unreachable — the engine handler returns
// ValidationException("not yet supported") before calling storage. Remove
// when real PITR is implemented or during the next storage trait cleanup.
fn restore_table_to_point_in_time(
&self,
account_id: &str,
source_table_name: &str,
target_table_name: &str,
) -> BoxFuture<'_, Result<TableDescription, StorageError>> {
let account_id = account_id.to_string();
let source_table_name = source_table_name.to_string();
let target_table_name = target_table_name.to_string();
Box::pin(async move {
let backup = self
.create_backup(&account_id, &source_table_name, "__pitr_restore__")
.await?;
let desc = self
.restore_table_from_backup(&account_id, &target_table_name, &backup.backup_arn)
.await?;
let _ = self.delete_backup(&backup.backup_arn).await;
Ok(desc)
})
}
}