-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathupdate_table.rs
More file actions
executable file
·423 lines (386 loc) · 18 KB
/
Copy pathupdate_table.rs
File metadata and controls
executable file
·423 lines (386 loc) · 18 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
// Copyright 2026 ExtendDB contributors
// SPDX-License-Identifier: Apache-2.0
//! `update_table` implementation for `PostgresEngine`.
use extenddb_core::types::{
AttributeDefinition, BillingMode, KeySchemaElement, TableDescription, UpdateTableInput,
};
use extenddb_storage::error::StorageError;
use crate::PostgresEngine;
impl PostgresEngine {
/// Core implementation of `update_table` (REQ-CTRL-003).
pub(crate) async fn update_table_impl(
&self,
account_id: &str,
input: UpdateTableInput,
) -> Result<TableDescription, StorageError> {
Self::validate_account_id(account_id)?;
let mut tx = self
.pool
.begin()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
// Lock the row and fetch table_id, key_schema, attribute_definitions.
let row: Option<(String, String, serde_json::Value, serde_json::Value)> = sqlx::query_as(
"SELECT table_status, table_id, key_schema, attribute_definitions FROM tables WHERE account_id = $1 AND table_name = $2 FOR UPDATE",
)
.bind(account_id)
.bind(&input.table_name)
.fetch_optional(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
let (status, table_id, ks_json, ad_json) =
row.ok_or_else(|| StorageError::TableNotFound(input.table_name.clone()))?;
if status != "ACTIVE" {
return Err(StorageError::TableNotActive(input.table_name.clone()));
}
// No-op rejection: setting same billing mode to PROVISIONED with same
// throughput values is rejected by DynamoDB. This check runs under the
// FOR UPDATE lock to eliminate the TOCTOU race that existed when the
// check was in the engine layer.
if matches!(input.billing_mode, Some(BillingMode::Provisioned))
&& let Some(ref pt) = input.provisioned_throughput
{
let current_row: Option<(Option<String>, Option<serde_json::Value>)> = sqlx::query_as(
"SELECT billing_mode, provisioned_throughput FROM tables \
WHERE account_id = $1 AND table_name = $2",
)
.bind(account_id)
.bind(&input.table_name)
.fetch_optional(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
if let Some((current_bm, current_pt_opt)) = current_row {
let current_pt =
current_pt_opt.unwrap_or(serde_json::Value::Object(Default::default()));
let is_provisioned =
current_bm.as_deref() == Some("PROVISIONED") || current_bm.is_none();
let current_rcu = current_pt
.get("ReadCapacityUnits")
.or_else(|| current_pt.get("read_capacity_units"))
.and_then(|v| v.as_i64())
.unwrap_or(0);
let current_wcu = current_pt
.get("WriteCapacityUnits")
.or_else(|| current_pt.get("write_capacity_units"))
.and_then(|v| v.as_i64())
.unwrap_or(0);
if is_provisioned
&& current_rcu == pt.read_capacity_units
&& current_wcu == pt.write_capacity_units
{
return Err(StorageError::NoOpUpdate(format!(
"The provisioned throughput for the table will not change. \
The requested value equals the current value. \
Current ReadCapacityUnits provisioned for the table: {}. \
Requested ReadCapacityUnits: {}. \
Current WriteCapacityUnits provisioned for the table: {}. \
Requested WriteCapacityUnits: {}.",
current_rcu, pt.read_capacity_units, current_wcu, pt.write_capacity_units
)));
}
}
}
// Apply billing mode change.
if let Some(bm) = &input.billing_mode {
let bm_str = match bm {
BillingMode::Provisioned => "PROVISIONED",
BillingMode::PayPerRequest => "PAY_PER_REQUEST",
};
sqlx::query(
"UPDATE tables SET billing_mode = $1 WHERE account_id = $2 AND table_name = $3",
)
.bind(bm_str)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
// Apply provisioned throughput change.
if let Some(pt) = &input.provisioned_throughput {
let pt_json =
serde_json::to_value(pt).map_err(|e| StorageError::Internal(e.to_string()))?;
sqlx::query("UPDATE tables SET provisioned_throughput = $1 WHERE account_id = $2 AND table_name = $3")
.bind(&pt_json)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
// Apply deletion protection change.
if let Some(dp) = input.deletion_protection_enabled {
sqlx::query("UPDATE tables SET deletion_protection_enabled = $1 WHERE account_id = $2 AND table_name = $3")
.bind(dp)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
// Apply stream specification change (enable/disable streams).
if let Some(spec) = &input.stream_specification {
let spec_json =
serde_json::to_value(spec).map_err(|e| StorageError::Internal(e.to_string()))?;
sqlx::query(
"UPDATE tables SET stream_specification = $1 \
WHERE account_id = $2 AND table_name = $3",
)
.bind(&spec_json)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
if spec.stream_enabled {
// Check if shards already exist (re-enabling streams on a table
// that previously had them). Query the data pool since
// stream_shards lives in the data database.
let existing: Option<(String,)> = sqlx::query_as(
"SELECT shard_id FROM stream_shards \
WHERE table_id = $1 \
LIMIT 1",
)
.bind(&table_id)
.fetch_optional(&self.data_pool)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
if existing.is_none() {
Self::init_stream_shards(
&mut tx,
&self.data_pool,
account_id,
&input.table_name,
&table_id,
)
.await?;
} else {
// Shards exist but stream_label may be NULL if streams were
// previously disabled and the disable path cleared the label.
// This is a defensive check — init_stream_shards sets the
// label on first enable, but re-enable after disable needs
// to restore it.
let current_label: Option<String> = sqlx::query_scalar(
"SELECT stream_label FROM tables \
WHERE account_id = $1 AND table_name = $2",
)
.bind(account_id)
.bind(&input.table_name)
.fetch_one(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
if current_label.is_none() {
sqlx::query(
"UPDATE tables SET stream_label = \
to_char(NOW(), 'YYYY-MM-DD\"T\"HH24:MI:SS') \
WHERE account_id = $1 AND table_name = $2",
)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
}
}
}
// Apply table class change.
if let Some(tc) = &input.table_class {
sqlx::query(
"UPDATE tables SET table_class = $1 WHERE account_id = $2 AND table_name = $3",
)
.bind(tc)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
// Apply on-demand throughput change.
if let Some(odt) = &input.on_demand_throughput {
let odt_json =
serde_json::to_value(odt).map_err(|e| StorageError::Internal(e.to_string()))?;
sqlx::query(
"UPDATE tables SET on_demand_throughput = $1 \
WHERE account_id = $2 AND table_name = $3",
)
.bind(&odt_json)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
// Apply GSI updates (create/delete).
let mut created_index_ids: Vec<String> = Vec::new();
let mut deleted_index_ids: Vec<String> = Vec::new();
if let Some(updates) = &input.global_secondary_index_updates {
for update in updates {
if let Some(create) = &update.create {
// Check for duplicate index name.
let existing: Option<(String,)> = sqlx::query_as(
"SELECT index_name FROM indexes WHERE table_id = $1 AND index_name = $2",
)
.bind(&table_id)
.bind(&create.index_name)
.fetch_optional(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
if existing.is_some() {
return Err(StorageError::IndexAlreadyExists(create.index_name.clone()));
}
let gsi_ks = serde_json::to_value(&create.key_schema)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let gsi_proj = serde_json::to_value(&create.projection)
.map_err(|e| StorageError::Internal(e.to_string()))?;
let gsi_pt = create
.provisioned_throughput
.as_ref()
.map(serde_json::to_value)
.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(&create.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()))?;
created_index_ids.push(index_id);
// Create the index data table on the data pool (P54 Bug 1).
// Catalog metadata is committed first; data DDL follows.
}
if let Some(delete) = &update.delete {
// Verify the index exists and fetch its index_id.
let existing: Option<(String, String)> = sqlx::query_as(
"SELECT index_name, index_id FROM indexes WHERE table_id = $1 AND index_name = $2",
)
.bind(&table_id)
.bind(&delete.index_name)
.fetch_optional(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
let (_, del_index_id) = existing
.ok_or_else(|| StorageError::IndexNotFound(delete.index_name.clone()))?;
// Delete the index metadata.
sqlx::query("DELETE FROM indexes WHERE table_id = $1 AND index_name = $2")
.bind(&table_id)
.bind(&delete.index_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
deleted_index_ids.push(del_index_id);
// Drop the index data table on the data pool after catalog commit.
}
}
// Update attribute_definitions on the table if new ones were provided.
if let Some(new_attr_defs) = &input.attribute_definitions {
let ad_json = serde_json::to_value(new_attr_defs)
.map_err(|e| StorageError::Internal(e.to_string()))?;
sqlx::query("UPDATE tables SET attribute_definitions = $1 WHERE account_id = $2 AND table_name = $3")
.bind(&ad_json)
.bind(account_id)
.bind(&input.table_name)
.execute(&mut *tx)
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
}
}
tx.commit()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
// P54 Bug 1: Execute data DDL on the data pool after catalog commit.
if let Some(updates) = &input.global_secondary_index_updates {
let base_key_schema: Vec<KeySchemaElement> = serde_json::from_value(ks_json.clone())
.map_err(|e| StorageError::Internal(e.to_string()))?;
let base_attr_defs: Vec<AttributeDefinition> = serde_json::from_value(ad_json.clone())
.map_err(|e| StorageError::Internal(e.to_string()))?;
let effective_attr_defs = input
.attribute_definitions
.as_deref()
.unwrap_or(&base_attr_defs);
let mut create_idx = 0usize;
let mut delete_idx = 0usize;
for update in updates {
if let Some(create) = &update.create {
let idx_id = &created_index_ids[create_idx];
create_idx += 1;
let data_result = async {
let mut data_tx = self
.data_pool
.begin()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
Self::create_index_data_table(
&mut data_tx,
idx_id,
&create.key_schema,
effective_attr_defs,
&base_key_schema,
&base_attr_defs,
)
.await?;
Self::backfill_gsi(
&mut data_tx,
&table_id,
idx_id,
&create.key_schema,
effective_attr_defs,
&base_key_schema,
&base_attr_defs,
&create.projection,
)
.await?;
data_tx
.commit()
.await
.map_err(|e| StorageError::Internal(e.to_string()))?;
Ok::<(), StorageError>(())
}
.await;
if let Err(e) = data_result {
tracing::error!(
"Failed to create data table for GSI '{}' on '{}', \
cleaning up catalog: {e}",
create.index_name,
input.table_name,
);
let _ = sqlx::query(
"DELETE FROM indexes WHERE table_id = $1 AND index_name = $2",
)
.bind(&table_id)
.bind(&create.index_name)
.execute(&self.pool)
.await;
return Err(e);
}
}
if update.delete.is_some() {
let idx_id = &deleted_index_ids[delete_idx];
delete_idx += 1;
let idx_table = Self::index_table_name_static(idx_id);
if let Err(e) = sqlx::query(&format!("DROP TABLE IF EXISTS {idx_table}"))
.execute(&self.data_pool)
.await
{
tracing::warn!(
"Failed to drop data table for deleted GSI on '{}': {e}",
input.table_name,
);
}
}
}
}
self.build_table_description(account_id, &input.table_name)
.await
}
}