-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcreate_table.rs
More file actions
executable file
·425 lines (398 loc) · 17 KB
/
Copy pathcreate_table.rs
File metadata and controls
executable file
·425 lines (398 loc) · 17 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
// Copyright 2026 ExtendDB contributors
// SPDX-License-Identifier: Apache-2.0
//! `create_table` implementation for `PostgresEngine`.
use extenddb_core::types::{
BillingMode, BillingModeSummary, CreateTableInput, GsiDescription, LsiDescription,
ProvisionedThroughputDescription, SseDescription, SseType, TableDescription, TableStatus,
};
use extenddb_storage::error::StorageError;
use extenddb_storage::util::{index_arn, stream_arn, table_arn};
use crate::PostgresEngine;
impl PostgresEngine {
/// Core implementation of `create_table` (Fix #4: wrapped in a transaction).
pub(crate) async fn create_table_impl(
&self,
account_id: &str,
input: CreateTableInput,
) -> Result<TableDescription, StorageError> {
Self::validate_account_id(account_id)?;
let table_id = uuid::Uuid::new_v4().to_string();
let table_arn = table_arn(&self.region, account_id, &input.table_name);
let billing_mode = input.billing_mode.unwrap_or(BillingMode::Provisioned);
let key_schema_json = serde_json::to_value(&input.key_schema)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let attr_defs_json = serde_json::to_value(&input.attribute_definitions)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let billing_str = match billing_mode {
BillingMode::Provisioned => "PROVISIONED",
BillingMode::PayPerRequest => "PAY_PER_REQUEST",
};
// Fix #7: Use serde_json::to_value directly instead of redundant closures
let pt_json = input
.provisioned_throughput
.as_ref()
.map(serde_json::to_value)
.transpose()
.map_err(|e| StorageError::Internal(e.to_string()))?;
let stream_json = input
.stream_specification
.as_ref()
.map(serde_json::to_value)
.transpose()
.map_err(|e| StorageError::Internal(e.to_string()))?;
let deletion_protection = input.deletion_protection_enabled.unwrap_or(false);
let sse_spec_json = input.sse_specification.as_ref().cloned();
let on_demand_json = input
.on_demand_throughput
.as_ref()
.map(serde_json::to_value)
.transpose()
.map_err(|e| StorageError::Internal(e.to_string()))?;
let mut tx = self
.pool
.begin()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
// Insert table metadata, returning creation timestamp and actual status.
// Use PG error code 23505 for robust duplicate detection instead of string matching.
// H-5: Insert as CREATING with a scheduled transition to ACTIVE,
// or directly as ACTIVE when control_plane_delay_seconds=0 (no async
// transition needed). This lets external test suites that don't call
// waitForActive() work correctly.
let (creation_epoch, actual_status): (f64, String) = sqlx::query_as(
r"WITH delay AS (
SELECT COALESCE(
(SELECT value::FLOAT8 FROM settings WHERE key = 'control_plane_delay_seconds'), 0.25
) AS secs
)
INSERT INTO tables
(account_id, table_name, key_schema, attribute_definitions, billing_mode,
provisioned_throughput, stream_specification, table_status,
creation_date_time, table_arn, table_id, deletion_protection_enabled,
status_transition_at, table_class, sse_specification, on_demand_throughput)
VALUES ($1, $2, $3, $4, $5, $6, $7,
CASE WHEN (SELECT secs FROM delay) = 0
THEN 'ACTIVE' ELSE 'CREATING' END,
NOW(), $8, $9, $10,
CASE WHEN (SELECT secs FROM delay) = 0
THEN NULL
ELSE NOW() + make_interval(secs => (SELECT secs FROM delay))
END,
$11, $12, $13)
RETURNING EXTRACT(EPOCH FROM creation_date_time)::FLOAT8, table_status",
)
.bind(account_id)
.bind(&input.table_name)
.bind(&key_schema_json)
.bind(&attr_defs_json)
.bind(billing_str)
.bind(&pt_json)
.bind(&stream_json)
.bind(&table_arn)
.bind(&table_id)
.bind(deletion_protection)
.bind(&input.table_class)
.bind(&sse_spec_json)
.bind(&on_demand_json)
.fetch_one(&mut *tx)
.await
.map_err(|e| match &e {
sqlx::Error::Database(db_err) if db_err.code().as_deref() == Some("23505") => {
StorageError::TableAlreadyExists(input.table_name.clone())
}
_ => StorageError::Internal(e.to_string()),
})?;
// Insert GSI metadata
// F-1: Store full ProvisionedThroughputDescription (not the input
// ProvisionedThroughput) so DescribeTable can deserialize it without
// failing on the missing NumberOfDecreasesToday field.
let mut gsi_index_ids: Vec<String> = Vec::new();
if let Some(gsis) = &input.global_secondary_indexes {
for gsi in gsis {
let gsi_ks = serde_json::to_value(&gsi.key_schema)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let gsi_proj = serde_json::to_value(&gsi.projection)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let gsi_pt = gsi
.provisioned_throughput
.as_ref()
.map(|pt| {
serde_json::to_value(ProvisionedThroughputDescription {
read_capacity_units: pt.read_capacity_units,
write_capacity_units: pt.write_capacity_units,
number_of_decreases_today: 0,
last_increase_date_time: None,
last_decrease_date_time: None,
})
})
.transpose()
.map_err(|e| StorageError::Internal(e.to_string()))?;
let index_id = uuid::Uuid::new_v4().to_string();
sqlx::query(
r"INSERT INTO indexes
(table_id, index_name, index_id, index_type, key_schema, projection,
index_status, provisioned_throughput)
VALUES ($1, $2, $3, 'GSI', $4, $5, 'ACTIVE', $6)",
)
.bind(&table_id)
.bind(&gsi.index_name)
.bind(&index_id)
.bind(&gsi_ks)
.bind(&gsi_proj)
.bind(&gsi_pt)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
gsi_index_ids.push(index_id);
}
}
// Insert LSI metadata
let mut lsi_index_ids: Vec<String> = Vec::new();
if let Some(lsis) = &input.local_secondary_indexes {
for lsi in lsis {
let lsi_ks = serde_json::to_value(&lsi.key_schema)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let lsi_proj = serde_json::to_value(&lsi.projection)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let index_id = uuid::Uuid::new_v4().to_string();
sqlx::query(
r"INSERT INTO indexes
(table_id, index_name, index_id, index_type, key_schema, projection,
index_status, provisioned_throughput)
VALUES ($1, $2, $3, 'LSI', $4, $5, 'ACTIVE', NULL)",
)
.bind(&table_id)
.bind(&lsi.index_name)
.bind(&index_id)
.bind(&lsi_ks)
.bind(&lsi_proj)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
lsi_index_ids.push(index_id);
}
}
// Insert tags
if let Some(tags) = &input.tags {
for tag in tags {
sqlx::query(
"INSERT INTO tags (resource_arn, tag_key, tag_value) VALUES ($1, $2, $3)",
)
.bind(&table_arn)
.bind(&tag.key)
.bind(&tag.value)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
}
// Create the per-DynamoDB-table data table for item storage.
// P54 Bug 1: Data tables live in the data database, not the catalog.
// Commit catalog metadata first, then create data tables on data_pool.
// If data DDL fails, the catalog entry is cleaned up (see below).
// Initialize stream shards and label if streams are enabled on this table.
let stream_label = if input
.stream_specification
.as_ref()
.is_some_and(|s| s.stream_enabled)
{
let label = Self::init_stream_shards(
&mut tx,
&self.data_pool,
account_id,
&input.table_name,
&table_id,
)
.await?;
Some(label)
} else {
None
};
tx.commit()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
// P54 Bug 1: Create data tables on the data pool after catalog commit.
let data_ddl_result = async {
let mut data_tx = self
.data_pool
.begin()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
Self::create_data_table(
&mut data_tx,
&table_id,
&input.key_schema,
&input.attribute_definitions,
)
.await?;
if let Some(gsis) = &input.global_secondary_indexes {
for (i, gsi) in gsis.iter().enumerate() {
Self::create_index_data_table(
&mut data_tx,
&gsi_index_ids[i],
&gsi.key_schema,
&input.attribute_definitions,
&input.key_schema,
&input.attribute_definitions,
)
.await?;
}
}
if let Some(lsis) = &input.local_secondary_indexes {
for (i, lsi) in lsis.iter().enumerate() {
Self::create_index_data_table(
&mut data_tx,
&lsi_index_ids[i],
&lsi.key_schema,
&input.attribute_definitions,
&input.key_schema,
&input.attribute_definitions,
)
.await?;
}
}
data_tx
.commit()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
Ok::<(), StorageError>(())
}
.await;
if let Err(e) = data_ddl_result {
// Data table creation failed. Clean up the catalog entry so the
// table name is not permanently stuck in CREATING state.
tracing::error!(
"Failed to create data tables for '{}', cleaning up catalog: {e}",
input.table_name,
);
let _ = sqlx::query("DELETE FROM tables WHERE account_id = $1 AND table_name = $2")
.bind(account_id)
.bind(&input.table_name)
.execute(&self.pool)
.await;
return Err(e);
}
// F-3: Wake the control plane poller so it processes the CREATING →
// ACTIVE transition without waiting for the idle timeout.
// If the server crashes between commit and notify, the 60s defensive
// sweep recovers the transition.
self.control_plane_notify.notify_one();
// Build response from in-scope data — avoids post-commit read race
// (another request could delete the table between commit and read).
let (rcu, wcu) = input.provisioned_throughput.as_ref().map_or((0, 0), |pt| {
(pt.read_capacity_units, pt.write_capacity_units)
});
let gsis = input.global_secondary_indexes.as_ref().map(|gs| {
gs.iter()
.map(|g| GsiDescription {
index_name: g.index_name.clone(),
key_schema: g.key_schema.clone(),
projection: g.projection.clone(),
index_status: "ACTIVE".to_owned(),
provisioned_throughput: Some(ProvisionedThroughputDescription {
read_capacity_units: g
.provisioned_throughput
.as_ref()
.map_or(0, |pt| pt.read_capacity_units),
write_capacity_units: g
.provisioned_throughput
.as_ref()
.map_or(0, |pt| pt.write_capacity_units),
number_of_decreases_today: 0,
last_increase_date_time: None,
last_decrease_date_time: None,
}),
index_size_bytes: 0,
item_count: 0,
index_arn: index_arn(
&self.region,
account_id,
&input.table_name,
&g.index_name,
),
})
.collect()
});
let lsis = input.local_secondary_indexes.as_ref().map(|ls| {
ls.iter()
.map(|l| LsiDescription {
index_name: l.index_name.clone(),
key_schema: l.key_schema.clone(),
projection: l.projection.clone(),
index_size_bytes: 0,
item_count: 0,
index_arn: index_arn(
&self.region,
account_id,
&input.table_name,
&l.index_name,
),
})
.collect()
});
let billing_mode_summary = if billing_mode == BillingMode::PayPerRequest {
Some(BillingModeSummary {
billing_mode: BillingMode::PayPerRequest,
last_update_to_pay_per_request_date_time: Some(creation_epoch),
})
} else {
None
};
let latest_stream_arn = stream_label
.as_ref()
.map(|label| stream_arn(&self.region, account_id, &input.table_name, label));
let response_status = if actual_status == "ACTIVE" {
TableStatus::Active
} else {
TableStatus::Creating
};
Ok(TableDescription {
table_name: input.table_name,
key_schema: input.key_schema,
attribute_definitions: input.attribute_definitions,
table_status: response_status,
creation_date_time: creation_epoch,
table_size_bytes: 0,
item_count: 0,
table_arn,
table_id,
provisioned_throughput: ProvisionedThroughputDescription {
read_capacity_units: rcu,
write_capacity_units: wcu,
number_of_decreases_today: 0,
last_increase_date_time: None,
last_decrease_date_time: None,
},
billing_mode_summary,
global_secondary_indexes: gsis,
local_secondary_indexes: lsis,
stream_specification: input.stream_specification,
latest_stream_arn,
latest_stream_label: stream_label,
deletion_protection_enabled: input.deletion_protection_enabled.unwrap_or(false),
sse_description: input.sse_specification.as_ref().and_then(|spec| {
let enabled = spec
.get("Enabled")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if enabled {
Some(SseDescription {
status: "ENABLED".to_string(),
sse_type: Some(SseType::KMS),
kms_master_key_arn: Some(format!(
"arn:aws:kms:{}:{}:key/default",
self.region, account_id
)),
})
} else {
None
}
}),
table_class_summary: input
.table_class
.as_ref()
.map(|tc| serde_json::json!({ "TableClass": tc })),
on_demand_throughput: input.on_demand_throughput,
})
}
}