-
Notifications
You must be signed in to change notification settings - Fork 82
/
default_processor.rs
407 lines (375 loc) · 13 KB
/
default_processor.rs
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
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
use super::{DefaultProcessingResult, ProcessorName, ProcessorTrait};
use crate::{
db::common::models::default_models::{
block_metadata_transactions::BlockMetadataTransactionModel,
move_tables::{CurrentTableItem, TableItem, TableMetadata},
},
gap_detectors::ProcessingResult,
schema,
utils::{
counters::PROCESSOR_UNKNOWN_TYPE_COUNT,
database::{execute_in_chunks, get_config_table_chunk_size, ArcDbPool},
},
worker::TableFlags,
};
use ahash::AHashMap;
use anyhow::bail;
use aptos_protos::transaction::v1::{
transaction::TxnData, write_set_change::Change as WriteSetChangeEnum, Transaction,
};
use async_trait::async_trait;
use diesel::{
pg::{upsert::excluded, Pg},
query_builder::QueryFragment,
ExpressionMethods,
};
use std::fmt::Debug;
use tokio::join;
use tracing::error;
pub struct DefaultProcessor {
connection_pool: ArcDbPool,
per_table_chunk_sizes: AHashMap<String, usize>,
deprecated_tables: TableFlags,
}
impl DefaultProcessor {
pub fn new(
connection_pool: ArcDbPool,
per_table_chunk_sizes: AHashMap<String, usize>,
deprecated_tables: TableFlags,
) -> Self {
Self {
connection_pool,
per_table_chunk_sizes,
deprecated_tables,
}
}
}
impl Debug for DefaultProcessor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let state = &self.connection_pool.state();
write!(
f,
"DefaultTransactionProcessor {{ connections: {:?} idle_connections: {:?} }}",
state.connections, state.idle_connections
)
}
}
async fn insert_to_db(
conn: ArcDbPool,
name: &'static str,
start_version: u64,
end_version: u64,
block_metadata_transactions: &[BlockMetadataTransactionModel],
(table_items, current_table_items, table_metadata): (
&[TableItem],
&[CurrentTableItem],
&[TableMetadata],
),
per_table_chunk_sizes: &AHashMap<String, usize>,
) -> Result<(), diesel::result::Error> {
tracing::trace!(
name = name,
start_version = start_version,
end_version = end_version,
"Inserting to db",
);
let bmt_res = execute_in_chunks(
conn.clone(),
insert_block_metadata_transactions_query,
block_metadata_transactions,
get_config_table_chunk_size::<BlockMetadataTransactionModel>(
"block_metadata_transactions",
per_table_chunk_sizes,
),
);
let ti_res = execute_in_chunks(
conn.clone(),
insert_table_items_query,
table_items,
get_config_table_chunk_size::<TableItem>("table_items", per_table_chunk_sizes),
);
let cti_res = execute_in_chunks(
conn.clone(),
insert_current_table_items_query,
current_table_items,
get_config_table_chunk_size::<CurrentTableItem>(
"current_table_items",
per_table_chunk_sizes,
),
);
let tm_res = execute_in_chunks(
conn.clone(),
insert_table_metadata_query,
table_metadata,
get_config_table_chunk_size::<TableMetadata>("table_metadatas", per_table_chunk_sizes),
);
let (bmt_res, ti_res, cti_res, tm_res) = join!(bmt_res, ti_res, cti_res, tm_res);
for res in [bmt_res, ti_res, cti_res, tm_res] {
res?;
}
Ok(())
}
pub fn insert_block_metadata_transactions_query(
items_to_insert: Vec<BlockMetadataTransactionModel>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Option<&'static str>,
) {
use schema::block_metadata_transactions::dsl::*;
(
diesel::insert_into(schema::block_metadata_transactions::table)
.values(items_to_insert)
.on_conflict(version)
.do_nothing(),
None,
)
}
pub fn insert_table_items_query(
items_to_insert: Vec<TableItem>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Option<&'static str>,
) {
use schema::table_items::dsl::*;
(
diesel::insert_into(schema::table_items::table)
.values(items_to_insert)
.on_conflict((transaction_version, write_set_change_index))
.do_nothing(),
None,
)
}
pub fn insert_current_table_items_query(
items_to_insert: Vec<CurrentTableItem>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Option<&'static str>,
) {
use schema::current_table_items::dsl::*;
(
diesel::insert_into(schema::current_table_items::table)
.values(items_to_insert)
.on_conflict((table_handle, key_hash))
.do_update()
.set((
key.eq(excluded(key)),
decoded_key.eq(excluded(decoded_key)),
decoded_value.eq(excluded(decoded_value)),
is_deleted.eq(excluded(is_deleted)),
last_transaction_version.eq(excluded(last_transaction_version)),
inserted_at.eq(excluded(inserted_at)),
)),
Some(" WHERE current_table_items.last_transaction_version <= excluded.last_transaction_version "),
)
}
pub fn insert_table_metadata_query(
items_to_insert: Vec<TableMetadata>,
) -> (
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Option<&'static str>,
) {
use schema::table_metadatas::dsl::*;
(
diesel::insert_into(schema::table_metadatas::table)
.values(items_to_insert)
.on_conflict(handle)
.do_nothing(),
None,
)
}
#[async_trait]
impl ProcessorTrait for DefaultProcessor {
fn name(&self) -> &'static str {
ProcessorName::DefaultProcessor.into()
}
async fn process_transactions(
&self,
transactions: Vec<Transaction>,
start_version: u64,
end_version: u64,
_: Option<u64>,
) -> anyhow::Result<ProcessingResult> {
let processing_start = std::time::Instant::now();
let last_transaction_timestamp = transactions.last().unwrap().timestamp.clone();
let flags = self.deprecated_tables;
let (block_metadata_transactions, table_items, current_table_items, table_metadata) =
tokio::task::spawn_blocking(move || process_transactions(transactions, flags))
.await
.expect("Failed to spawn_blocking for TransactionModel::from_transactions");
let processing_duration_in_secs = processing_start.elapsed().as_secs_f64();
let db_insertion_start = std::time::Instant::now();
let tx_result = insert_to_db(
self.get_pool(),
self.name(),
start_version,
end_version,
&block_metadata_transactions,
(&table_items, ¤t_table_items, &table_metadata),
&self.per_table_chunk_sizes,
)
.await;
// These vectors could be super large and take a lot of time to drop, move to background to
// make it faster.
tokio::task::spawn(async move {
drop(block_metadata_transactions);
drop(table_items);
drop(current_table_items);
drop(table_metadata);
});
let db_insertion_duration_in_secs = db_insertion_start.elapsed().as_secs_f64();
match tx_result {
Ok(_) => Ok(ProcessingResult::DefaultProcessingResult(
DefaultProcessingResult {
start_version,
end_version,
processing_duration_in_secs,
db_insertion_duration_in_secs,
last_transaction_timestamp,
},
)),
Err(e) => {
error!(
start_version = start_version,
end_version = end_version,
processor_name = self.name(),
error = ?e,
"[Parser] Error inserting transactions to db",
);
bail!(e)
},
}
}
fn connection_pool(&self) -> &ArcDbPool {
&self.connection_pool
}
}
/// Processes a list of transactions and extracts relevant data into different models.
///
/// This function iterates over a list of transactions, extracting block metadata transactions,
/// table items, current table items, and table metadata. It handles different types of
/// transactions and write set changes, converting them into appropriate models. The function
/// also sorts the extracted data to avoid PostgreSQL deadlocks during multi-threaded database
/// writes.
///
/// # Arguments
///
/// * `transactions` - A vector of `Transaction` objects to be processed.
/// * `flags` - A `TableFlags` object that determines which tables to clear after processing.
///
/// # Returns
///
/// A tuple containing:
/// * `Vec<BlockMetadataTransactionModel>` - A vector of block metadata transaction models.
/// * `Vec<TableItem>` - A vector of table items.
/// * `Vec<CurrentTableItem>` - A vector of current table items, sorted by primary key.
/// * `Vec<TableMetadata>` - A vector of table metadata, sorted by primary key.
pub fn process_transactions(
transactions: Vec<Transaction>,
flags: TableFlags,
) -> (
Vec<BlockMetadataTransactionModel>,
Vec<TableItem>,
Vec<CurrentTableItem>,
Vec<TableMetadata>,
) {
let mut block_metadata_transactions = vec![];
let mut table_items = vec![];
let mut current_table_items = AHashMap::new();
let mut table_metadata = AHashMap::new();
for transaction in transactions {
let version = transaction.version as i64;
let block_height = transaction.block_height as i64;
let epoch = transaction.epoch as i64;
let timestamp = transaction
.timestamp
.as_ref()
.expect("Transaction timestamp doesn't exist!");
let transaction_info = transaction
.info
.as_ref()
.expect("Transaction info doesn't exist!");
let txn_data = match transaction.txn_data.as_ref() {
Some(txn_data) => txn_data,
None => {
PROCESSOR_UNKNOWN_TYPE_COUNT
.with_label_values(&["Transaction"])
.inc();
tracing::warn!(
transaction_version = transaction.version,
"Transaction data doesn't exist",
);
continue;
},
};
if let TxnData::BlockMetadata(block_metadata_txn) = txn_data {
let bmt = BlockMetadataTransactionModel::from_bmt_transaction(
block_metadata_txn,
version,
block_height,
epoch,
timestamp,
);
block_metadata_transactions.push(bmt);
}
for (index, wsc) in transaction_info.changes.iter().enumerate() {
match wsc
.change
.as_ref()
.expect("WriteSetChange must have a change")
{
WriteSetChangeEnum::WriteTableItem(inner) => {
let (ti, cti) = TableItem::from_write_table_item(
inner,
index as i64,
version,
block_height,
);
table_items.push(ti);
current_table_items.insert(
(cti.table_handle.clone(), cti.key_hash.clone()),
cti.clone(),
);
table_metadata.insert(
cti.table_handle.clone(),
TableMetadata::from_write_table_item(inner),
);
},
WriteSetChangeEnum::DeleteTableItem(inner) => {
let (ti, cti) = TableItem::from_delete_table_item(
inner,
index as i64,
version,
block_height,
);
table_items.push(ti);
current_table_items
.insert((cti.table_handle.clone(), cti.key_hash.clone()), cti);
},
_ => {},
};
}
}
// Getting list of values and sorting by pk in order to avoid postgres deadlock since we're doing multi threaded db writes
let mut current_table_items = current_table_items
.into_values()
.collect::<Vec<CurrentTableItem>>();
let mut table_metadata = table_metadata.into_values().collect::<Vec<TableMetadata>>();
// Sort by PK
current_table_items
.sort_by(|a, b| (&a.table_handle, &a.key_hash).cmp(&(&b.table_handle, &b.key_hash)));
table_metadata.sort_by(|a, b| a.handle.cmp(&b.handle));
if flags.contains(TableFlags::TABLE_ITEMS) {
table_items.clear();
}
if flags.contains(TableFlags::TABLE_METADATAS) {
table_metadata.clear();
}
(
block_metadata_transactions,
table_items,
current_table_items,
table_metadata,
)
}