|
| 1 | +use std::io::{BufRead, Write}; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use anyhow::{anyhow, bail, Result}; |
| 5 | +use function_runner::{ |
| 6 | + bluejay_schema_analyzer::BluejaySchemaAnalyzer, |
| 7 | + engine::{run, FunctionRunParams}, |
| 8 | + function_run_result::FunctionRunResult, |
| 9 | + BytesContainer, BytesContainerType, Codec, |
| 10 | +}; |
| 11 | +use serde::Serialize; |
| 12 | +use wasmtime::Module; |
| 13 | + |
| 14 | +type ScaleFactorFn<'a> = dyn Fn(&serde_json::Value) -> Result<f64> + 'a; |
| 15 | + |
| 16 | +pub(crate) struct ScaleLimitsSource<'a> { |
| 17 | + pub schema: &'a str, |
| 18 | + pub schema_path: Option<&'a str>, |
| 19 | + pub query: &'a str, |
| 20 | + pub query_path: Option<&'a str>, |
| 21 | +} |
| 22 | + |
| 23 | +pub(crate) struct BatchOptions<'a> { |
| 24 | + pub function_path: &'a Path, |
| 25 | + pub export: &'a str, |
| 26 | + pub codec: Codec, |
| 27 | + pub default_scale_factor: f64, |
| 28 | + pub continue_on_error: bool, |
| 29 | + pub full_output: bool, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Serialize)] |
| 33 | +struct MinimalRecord<'a> { |
| 34 | + line: usize, |
| 35 | + success: bool, |
| 36 | + instructions: u64, |
| 37 | + memory_usage: u64, |
| 38 | + logs: &'a str, |
| 39 | + output: Option<&'a serde_json::Value>, |
| 40 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 41 | + output_error: Option<&'a str>, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Serialize)] |
| 45 | +struct FullRecord<'a> { |
| 46 | + line: usize, |
| 47 | + #[serde(flatten)] |
| 48 | + result: &'a FunctionRunResult, |
| 49 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 50 | + output_error: Option<&'a str>, |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Serialize)] |
| 54 | +struct ErrorRecord<'a> { |
| 55 | + line: usize, |
| 56 | + success: bool, |
| 57 | + error: &'a str, |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Default)] |
| 61 | +struct Summary { |
| 62 | + processed: usize, |
| 63 | + successful: usize, |
| 64 | + failed: usize, |
| 65 | +} |
| 66 | + |
| 67 | +pub(crate) fn run_batch( |
| 68 | + input: impl BufRead, |
| 69 | + output: impl Write, |
| 70 | + module: &Module, |
| 71 | + scale_limits: Option<ScaleLimitsSource>, |
| 72 | + options: &BatchOptions, |
| 73 | +) -> Result<()> { |
| 74 | + match scale_limits { |
| 75 | + Some(source) => BluejaySchemaAnalyzer::with_analyzer( |
| 76 | + source.schema, |
| 77 | + source.schema_path, |
| 78 | + source.query, |
| 79 | + source.query_path, |
| 80 | + |analyze| run_lines(input, output, module, Some(analyze), options), |
| 81 | + )?, |
| 82 | + None => run_lines(input, output, module, None, options), |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn run_lines( |
| 87 | + mut input: impl BufRead, |
| 88 | + mut output: impl Write, |
| 89 | + module: &Module, |
| 90 | + analyze: Option<&ScaleFactorFn<'_>>, |
| 91 | + options: &BatchOptions, |
| 92 | +) -> Result<()> { |
| 93 | + let mut summary = Summary::default(); |
| 94 | + let mut line_bytes = Vec::new(); |
| 95 | + let mut record = Vec::new(); |
| 96 | + let mut line = 0; |
| 97 | + |
| 98 | + let outcome = loop { |
| 99 | + line_bytes.clear(); |
| 100 | + match input.read_until(b'\n', &mut line_bytes) { |
| 101 | + Ok(0) => break Ok(()), |
| 102 | + Ok(_) => {} |
| 103 | + Err(e) => break Err(anyhow!("Couldn't read input line {}: {}", line + 1, e)), |
| 104 | + } |
| 105 | + line += 1; |
| 106 | + |
| 107 | + if line_bytes.iter().all(u8::is_ascii_whitespace) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + summary.processed += 1; |
| 111 | + |
| 112 | + let failure = match run_line(std::mem::take(&mut line_bytes), module, analyze, options) { |
| 113 | + Ok(result) => match write_result_record(&mut record, line, &result, options.full_output) { |
| 114 | + Err(error) => Some(format!("Line {line}: {error}")), |
| 115 | + Ok(()) if result.success => None, |
| 116 | + Ok(()) => Some(format!( |
| 117 | + "The Function execution failed on line {line}. Review the logs for more information." |
| 118 | + )), |
| 119 | + }, |
| 120 | + Err(error) => { |
| 121 | + let error = format!("{error:#}"); |
| 122 | + write_error_record(&mut record, line, &error); |
| 123 | + Some(format!("Line {line}: {error}")) |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + if let Err(e) = output.write_all(&record) { |
| 128 | + break Err(e.into()); |
| 129 | + } |
| 130 | + |
| 131 | + match failure { |
| 132 | + None => summary.successful += 1, |
| 133 | + Some(reason) => { |
| 134 | + summary.failed += 1; |
| 135 | + if !options.continue_on_error { |
| 136 | + break Err(anyhow!(reason)); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + output.flush()?; |
| 143 | + |
| 144 | + let status = if outcome.is_ok() { |
| 145 | + "complete" |
| 146 | + } else { |
| 147 | + "stopped" |
| 148 | + }; |
| 149 | + eprintln!( |
| 150 | + "Batch {status}: {} inputs processed, {} successful, {} failed", |
| 151 | + summary.processed, summary.successful, summary.failed |
| 152 | + ); |
| 153 | + |
| 154 | + outcome?; |
| 155 | + |
| 156 | + if summary.failed > 0 { |
| 157 | + bail!("{} of {} inputs failed", summary.failed, summary.processed); |
| 158 | + } |
| 159 | + |
| 160 | + Ok(()) |
| 161 | +} |
| 162 | + |
| 163 | +fn run_line( |
| 164 | + line_bytes: Vec<u8>, |
| 165 | + module: &Module, |
| 166 | + analyze: Option<&ScaleFactorFn<'_>>, |
| 167 | + options: &BatchOptions, |
| 168 | +) -> Result<FunctionRunResult> { |
| 169 | + let input = BytesContainer::new(BytesContainerType::Input, options.codec, line_bytes)?; |
| 170 | + |
| 171 | + let scale_factor = match (analyze, input.json_value.as_ref()) { |
| 172 | + (Some(analyze), Some(json_value)) => analyze(json_value)?, |
| 173 | + _ => options.default_scale_factor, |
| 174 | + }; |
| 175 | + |
| 176 | + run(FunctionRunParams { |
| 177 | + function_path: options.function_path.to_path_buf(), |
| 178 | + input, |
| 179 | + export: options.export, |
| 180 | + profile_opts: None, |
| 181 | + scale_factor, |
| 182 | + module: module.clone(), |
| 183 | + engine: module.engine().clone(), |
| 184 | + }) |
| 185 | +} |
| 186 | + |
| 187 | +fn write_result_record( |
| 188 | + record: &mut Vec<u8>, |
| 189 | + line: usize, |
| 190 | + result: &FunctionRunResult, |
| 191 | + full_output: bool, |
| 192 | +) -> Result<(), String> { |
| 193 | + record.clear(); |
| 194 | + let output_error = result.output.encoding_error.as_deref(); |
| 195 | + let serialized = if full_output { |
| 196 | + serde_json::to_writer( |
| 197 | + &mut *record, |
| 198 | + &FullRecord { |
| 199 | + line, |
| 200 | + result, |
| 201 | + output_error, |
| 202 | + }, |
| 203 | + ) |
| 204 | + } else { |
| 205 | + serde_json::to_writer( |
| 206 | + &mut *record, |
| 207 | + &MinimalRecord { |
| 208 | + line, |
| 209 | + success: result.success, |
| 210 | + instructions: result.instructions, |
| 211 | + memory_usage: result.memory_usage, |
| 212 | + logs: &result.logs, |
| 213 | + output: result.output.json_value.as_ref(), |
| 214 | + output_error, |
| 215 | + }, |
| 216 | + ) |
| 217 | + }; |
| 218 | + |
| 219 | + match serialized { |
| 220 | + Ok(()) => { |
| 221 | + record.push(b'\n'); |
| 222 | + Ok(()) |
| 223 | + } |
| 224 | + Err(e) => { |
| 225 | + let error = format!("Couldn't serialize result: {e}"); |
| 226 | + write_error_record(record, line, &error); |
| 227 | + Err(error) |
| 228 | + } |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +fn write_error_record(record: &mut Vec<u8>, line: usize, error: &str) { |
| 233 | + record.clear(); |
| 234 | + serde_json::to_writer( |
| 235 | + &mut *record, |
| 236 | + &ErrorRecord { |
| 237 | + line, |
| 238 | + success: false, |
| 239 | + error, |
| 240 | + }, |
| 241 | + ) |
| 242 | + .expect("An error record contains only a number, a bool, and a string"); |
| 243 | + record.push(b'\n'); |
| 244 | +} |
0 commit comments