-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdata_processing.rs
More file actions
267 lines (231 loc) · 8.09 KB
/
data_processing.rs
File metadata and controls
267 lines (231 loc) · 8.09 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
use std::sync::Arc;
use alloy::primitives::{BlockHash, BlockNumber};
use anyhow::anyhow;
use arrow::array::RecordBatch;
use chrono::{DateTime, Utc};
use graph::{
amp::{
codec::{utils::auto_block_timestamp_decoder, DecodeOutput, DecodedEntity, Decoder},
stream_aggregator::{RecordBatchGroup, RecordBatchGroups, StreamRecordBatch},
},
blockchain::block_stream::FirehoseCursor,
cheap_clone::CheapClone,
components::store::{EntityCache, EntityLfuCache, ModificationsAndCache, SeqGenerator},
};
use slog::{debug, trace};
use super::{data_stream::TablePtr, Compat, Context, Error};
pub(super) async fn process_record_batch_groups<AC>(
cx: &mut Context<AC>,
mut entity_lfu_cache: EntityLfuCache,
record_batch_groups: RecordBatchGroups,
stream_table_ptr: Arc<[TablePtr]>,
latest_block: BlockNumber,
) -> Result<EntityLfuCache, Error> {
if record_batch_groups.is_empty() {
debug!(cx.logger, "Received no record batch groups");
return Ok(entity_lfu_cache);
}
let from_block = record_batch_groups
.first_key_value()
.map(|((block, _), _)| *block)
.unwrap();
let to_block = record_batch_groups
.last_key_value()
.map(|((block, _), _)| *block)
.unwrap();
debug!(cx.logger, "Processing record batch groups";
"from_block" => from_block,
"to_block" => to_block
);
for ((block_number, block_hash), record_batch_group) in record_batch_groups {
trace!(cx.logger, "Processing record batch group";
"block" => block_number,
"record_batches_count" => record_batch_group.record_batches.len()
);
entity_lfu_cache = process_record_batch_group(
cx,
entity_lfu_cache,
block_number,
block_hash,
record_batch_group,
&stream_table_ptr,
latest_block,
)
.await
.map_err(|e| {
e.context(format!(
"failed to process record batch group at block '{block_number}'"
))
})?;
cx.metrics.deployment_head.update(block_number);
cx.metrics.blocks_processed.record_one();
trace!(cx.logger, "Completed processing record batch group";
"block" => block_number
);
}
debug!(cx.logger, "Completed processing record batch groups";
"from_block" => from_block,
"to_block" => to_block
);
Ok(entity_lfu_cache)
}
async fn process_record_batch_group<AC>(
cx: &mut Context<AC>,
entity_lfu_cache: EntityLfuCache,
block_number: BlockNumber,
block_hash: BlockHash,
record_batch_group: RecordBatchGroup,
stream_table_ptr: &[TablePtr],
latest_block: BlockNumber,
) -> Result<EntityLfuCache, Error> {
let _section = cx
.metrics
.stopwatch
.start_section("process_record_batch_group");
let RecordBatchGroup { record_batches } = record_batch_group;
if record_batches.is_empty() {
debug!(cx.logger, "Record batch group is empty");
return Ok(entity_lfu_cache);
}
let mut entity_cache = EntityCache::with_current(
cx.store.cheap_clone(),
entity_lfu_cache,
SeqGenerator::new(block_number.compat()),
);
let block_timestamp = if cx.manifest.schema.has_aggregations() {
decode_block_timestamp(&record_batches)
.map_err(|e| e.context("failed to decode block timestamp"))?
} else {
// TODO: Block timestamp is only required for subgraph aggregations.
// Make it optional at the store level.
DateTime::<Utc>::MIN_UTC
};
for record_batch in record_batches {
let StreamRecordBatch {
stream_index,
record_batch,
} = record_batch;
process_record_batch(
cx,
&mut entity_cache,
record_batch,
stream_table_ptr[stream_index],
)
.await
.map_err(|e| {
e.context(format!(
"failed to process record batch for stream '{stream_index}'"
))
})?;
}
let section = cx.metrics.stopwatch.start_section("as_modifications");
let ModificationsAndCache {
modifications,
entity_lfu_cache,
evict_stats: _,
} = entity_cache
.as_modifications(block_number.compat(), &cx.metrics.stopwatch)
.await
.map_err(Error::from)
.map_err(|e| e.context("failed to extract entity modifications from the state"))?;
section.end();
let _section = cx.metrics.stopwatch.start_section("transact_block");
let is_close_to_chain_head = latest_block.saturating_sub(block_number) <= 100;
cx.store
.transact_block_operations(
(block_number, block_hash).compat(),
block_timestamp.compat(),
FirehoseCursor::None,
modifications,
&cx.metrics.stopwatch,
Vec::new(),
Vec::new(),
Vec::new(),
false,
is_close_to_chain_head,
)
.await
.map_err(Error::from)
.map_err(|e| e.context("failed to transact block operations"))?;
if is_close_to_chain_head {
cx.metrics.deployment_synced.record(true);
}
Ok(entity_lfu_cache)
}
async fn process_record_batch<AC>(
cx: &mut Context<AC>,
entity_cache: &mut EntityCache,
record_batch: RecordBatch,
(i, j): TablePtr,
) -> Result<(), Error> {
let _section = cx.metrics.stopwatch.start_section("process_record_batch");
let table = &cx.manifest.data_sources[i].transformer.tables[j];
let entity_name = &table.name;
let DecodeOutput {
entity_type,
id_type,
decoded_entities,
} = cx
.codec
.decode(record_batch, entity_name.as_str())
.map_err(|e| {
Error::Deterministic(
e.context(format!("failed to decode entities of type '{entity_name}'")),
)
})?;
for decoded_entity in decoded_entities {
let DecodedEntity {
key,
mut entity_data,
} = decoded_entity;
let key = match key {
Some(key) => key,
None => {
let entity_id = entity_cache.seq_gen.id(id_type).map_err(|e| {
Error::Deterministic(e.context(format!(
"failed to generate a new id for an entity of type '{entity_name}'"
)))
})?;
entity_data.push(("id".into(), entity_id.clone().into()));
entity_type.key(entity_id)
}
};
let entity_id = key.entity_id.clone();
let entity = cx.manifest.schema.make_entity(entity_data).map_err(|e| {
Error::Deterministic(anyhow!(e).context(format!(
"failed to create a new entity of type '{entity_name}' with id '{entity_id}'"
)))
})?;
entity_cache.set(key, entity, None).await.map_err(|e| {
Error::Deterministic(e.context(format!(
"failed to store a new entity of type '{entity_name}' with id '{entity_id}'"
)))
})?;
}
Ok(())
}
/// Decodes the block timestamp from the first matching column in `record_batches`.
///
/// Iterates through the provided record batches and returns the timestamp from
/// the first batch that contains a valid block timestamp column.
///
/// # Preconditions
///
/// All entries in `record_batches` must belong to the same record batch group.
fn decode_block_timestamp(record_batches: &[StreamRecordBatch]) -> Result<DateTime<Utc>, Error> {
let mut last_error: Option<Error> = None;
for record_batch in record_batches {
match auto_block_timestamp_decoder(&record_batch.record_batch) {
Ok((_, decoder)) => {
return decoder
.decode(0)
.map_err(Error::Deterministic)?
.ok_or_else(|| Error::Deterministic(anyhow!("block timestamp is empty")));
}
Err(e) => {
last_error = Some(Error::Deterministic(e));
}
}
}
Err(last_error.unwrap())
}