-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
273 lines (237 loc) · 8.8 KB
/
main.rs
File metadata and controls
273 lines (237 loc) · 8.8 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
/*
Copyright 2024-2025 The Spice.ai OSS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use std::sync::Arc;
use adbc_client::AdbcConnection;
use checkpointer::CheckpointStore;
use clap::Parser;
use data_generation::config::{TargetConfig, build_version_prefix};
use data_generation::storage::DataStorage;
use data_generation::storage::s3::S3Storage;
use data_generation::version::VersionMetadata;
use etl::sink::adbc::AdbcSink;
use etl::{DatasetSource, ETLPipeline, PipelineState, StopReason};
use test_framework::{anyhow, rustls};
use tracing::Level;
use tracing_subscriber::EnvFilter;
mod args;
mod commands;
mod metrics;
mod scenario;
use crate::args::CommonArgs;
use crate::commands::connect_system_adapter;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(flatten)]
common: args::CommonArgs,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum SystemAdapterExecutionMode {
AdapterCommand,
DirectQuery,
}
async fn run_benchmark(
common: &CommonArgs,
system_adapter_client: &mut system_adapter_protocol::Client,
run_id: uuid::Uuid,
adbc_driver: system_adapter_protocol::SetupResponse,
version_metadata: &VersionMetadata,
source: Arc<S3Storage>,
) -> anyhow::Result<()> {
// --- Download checkpoints from S3 ---
let scenario_name = common.scenario.to_string();
let checkpoint_dir = tempfile::tempdir()?;
let version_prefix =
build_version_prefix(&common.etl_prefix, &scenario_name, common.etl_version);
let checkpoint_store = CheckpointStore::new(
&common.etl_bucket,
&version_prefix,
common.etl_region.as_deref(),
common.etl_endpoint.as_deref(),
)?;
let manifest = checkpoint_store.download_manifest().await.map_err(|e| {
tracing::warn!("Failed to download checkpoint manifest - results validation will not be enabled: {e}");
e
}).ok();
let mut checkpoint_steps: Option<usize> = None;
if let Some(manifest) = manifest
&& let Some(scenario_info) = manifest.scenarios.get(&scenario_name)
{
tracing::info!(
scenario = %scenario_name,
num_checkpoints = scenario_info.checkpoint_indexes.len(),
num_queries = scenario_info.query_indexes.len(),
checkpoint_interval_steps = scenario_info.checkpoint_interval_steps,
path = %checkpoint_dir.path().display(),
"Downloading checkpoints"
);
if scenario_info.checkpoint_interval_steps > 0 {
checkpoint_steps = Some(scenario_info.checkpoint_interval_steps);
}
if let Err(e) = checkpoint_store
.download_checkpoints(&scenario_name, scenario_info, checkpoint_dir.path())
.await
{
tracing::warn!(
"Failed to download checkpoints - results validation will not be enabled: {e}"
);
} else {
tracing::info!(scenario = %scenario_name, "Checkpoints downloaded");
}
} else {
tracing::warn!(
scenario = %scenario_name,
"No checkpoints found for scenario in manifest"
);
}
let driver_name = adbc_driver.driver.to_string();
let sink_kwargs = adbc_driver.db_kwargs.clone();
let load_kwargs = adbc_driver.db_kwargs;
let adbc_conn = AdbcConnection::create(&driver_name, sink_kwargs).map_err(|e| {
anyhow::anyhow!(
"Failed to create ADBC connection for driver {}: {e}",
driver_name
)
})?;
println!("ADBC connection established (driver: {})", driver_name);
let target = Arc::new(AdbcSink::new(adbc_conn, None));
let dataset_source = DatasetSource::from_dataset_type(&version_metadata.dataset_type)?;
let generation_config = version_metadata.dataset_config();
let mutations = version_metadata.mutation_config();
let mut pipeline = ETLPipeline::new(
dataset_source,
&generation_config,
source,
target,
&mutations,
)?
.with_created_at(common.with_created_at);
let datasets = pipeline.create_tables_request_datasets();
if let Err(e) = system_adapter_client.create_tables(run_id, datasets).await {
pipeline.cancel();
return Err(anyhow::anyhow!(
"Failed to create tables via system adapter: {e}"
));
}
// --- Initialize: ETL the first batch so the target has data ---
tracing::info!("Initializing ETL pipeline (first batch)...");
pipeline.initialize().await?;
tracing::info!("ETL pipeline initialized");
let load_conn = match AdbcConnection::create(&driver_name, load_kwargs) {
Ok(conn) => conn,
Err(e) => {
pipeline.cancel();
return Err(anyhow::anyhow!(
"Failed to create benchmark ADBC connection for driver {}: {e}",
driver_name
));
}
};
commands::load::run(
&common.scenario,
common,
load_conn,
&mut pipeline,
checkpoint_steps,
)
.await?;
// --- Wait for ETL to finish ---
// If checkpoint_steps was set, the load runner already handled
// the pause/resume loop internally, so the pipeline should be
// in a stopped state by now. If it was started without checkpoints
// (.start()), the pipeline may still be running.
let final_state = pipeline.wait().await;
match &final_state {
PipelineState::Stopped(StopReason::Completed) => {
tracing::info!("ETL pipeline completed successfully");
}
PipelineState::Stopped(StopReason::Cancelled) => {
tracing::warn!("ETL pipeline was cancelled");
}
PipelineState::Stopped(StopReason::Error(e)) => {
tracing::error!(error = %e, "ETL pipeline stopped with error");
}
other => {
tracing::warn!("Unexpected final pipeline state: {other:?}");
}
}
Ok(())
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = rustls::crypto::CryptoProvider::install_default(
rustls::crypto::aws_lc_rs::default_provider(),
);
tracing_subscriber::fmt()
.with_max_level(Level::INFO)
.with_env_filter(EnvFilter::from_default_env())
.init();
let cli = Cli::parse();
// --- Connect to S3 and read version metadata ---
let source_config = TargetConfig {
bucket: cli.common.etl_bucket.clone(),
prefix: build_version_prefix(
&cli.common.etl_prefix,
&cli.common.scenario.to_string(),
cli.common.etl_version,
),
region: cli.common.etl_region.clone(),
endpoint: cli.common.etl_endpoint.clone(),
};
let source = Arc::new(S3Storage::new(&source_config)?);
// Read version metadata to derive dataset config and mutations.
let version_metadata = source.read_version_metadata().await?.ok_or_else(|| {
anyhow::anyhow!(
"No version.json found at {}. Was data generation run for this version?",
source_config.prefix,
)
})?;
// --- Connect to the system adapter ---
let mut system_adapter_client = match connect_system_adapter(&cli.common).await {
Ok(system_adapter_client) => system_adapter_client,
Err(e) => {
return Err(anyhow::anyhow!("Failed to connect to system adapter: {e}"));
}
};
let run_id = uuid::Uuid::new_v4();
let setup_metadata = std::collections::HashMap::from([
(
"executor_instance_type".to_string(),
serde_json::Value::String(cli.common.executor_instance_type.clone()),
),
(
"table_format".to_string(),
serde_json::Value::String(cli.common.table_format.to_string()),
),
]);
let adbc_driver = match system_adapter_client.setup(run_id, setup_metadata).await {
Ok(response) => response,
Err(e) => {
return Err(anyhow::anyhow!("Failed to setup system adapter: {e}"));
}
};
let result = run_benchmark(
&cli.common,
&mut system_adapter_client,
run_id,
adbc_driver,
&version_metadata,
source,
)
.await;
// After successful setup, always teardown even if there are errors in between.
if let Err(e) = system_adapter_client.teardown(run_id).await {
tracing::error!("Failed to teardown system adapter: {e}");
}
result
}