-
Notifications
You must be signed in to change notification settings - Fork 64
/
spiceai.rs
306 lines (272 loc) · 11.6 KB
/
spiceai.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
/*
Copyright 2024 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 arrow_flight::decode::DecodedPayload;
use async_stream::stream;
use async_trait::async_trait;
use flight_client::FlightClient;
use flight_datafusion::FlightTable;
use futures::StreamExt;
use futures_core::stream::BoxStream;
use ns_lookup::verify_endpoint_connection;
use secrets::Secret;
use snafu::prelude::*;
use spicepod::component::dataset::Dataset;
use std::borrow::Borrow;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use std::{collections::HashMap, future::Future};
use crate::datapublisher::{AddDataResult, DataPublisher};
use crate::dataupdate::{DataUpdate, UpdateType};
use crate::tracers::SpacedTracer;
use crate::{info_spaced, status};
use super::DataConnectorFactory;
use super::{flight::Flight, DataConnector};
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Unable to parse SpiceAI dataset path: {dataset_path}"))]
UnableToParseDatasetPath { dataset_path: String },
#[snafu(display("Unable to publish data to SpiceAI: {source}"))]
UnableToPublishData { source: flight_client::Error },
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Clone)]
pub struct SpiceAI {
flight: Flight,
spaced_trace: Arc<SpacedTracer>,
api_key: String,
}
impl DataConnectorFactory for SpiceAI {
fn create(
secret: Option<Secret>,
params: Arc<Option<HashMap<String, String>>>,
) -> Pin<Box<dyn Future<Output = super::NewDataConnectorResult> + Send>> {
let default_flight_url = if cfg!(feature = "dev") {
"https://dev-flight.spiceai.io".to_string()
} else {
"https://flight.spiceai.io".to_string()
};
Box::pin(async move {
let secret = secret.ok_or_else(|| super::Error::UnableToCreateDataConnector {
source: "Missing required secrets".into(),
})?;
let url: String = params
.as_ref() // &Option<HashMap<String, String>>
.as_ref() // Option<&HashMap<String, String>>
.and_then(|params| params.get("endpoint").cloned())
.unwrap_or(default_flight_url);
tracing::trace!("Connecting to SpiceAI with flight url: {url}");
verify_endpoint_connection(&url)
.await
.map_err(|e| super::Error::UnableToCreateDataConnector { source: e.into() })?;
let api_key = secret.get("key").unwrap_or_default();
let flight_client = FlightClient::new(url.as_str(), "", api_key)
.await
.map_err(|e| super::Error::UnableToCreateDataConnector { source: e.into() })?;
let flight = Flight::new(flight_client);
let spiceai = Self {
flight,
api_key: api_key.to_string(),
spaced_trace: Arc::new(SpacedTracer::new(Duration::from_secs(15))),
};
Ok(Box::new(spiceai) as Box<dyn DataConnector>)
})
}
}
#[async_trait]
impl DataConnector for SpiceAI {
fn supports_data_streaming(&self, _dataset: &Dataset) -> bool {
true
}
/// Returns a stream of `DataUpdates` for the given dataset.
fn stream_data_updates<'a>(&self, dataset: &Dataset) -> BoxStream<'a, DataUpdate> {
let flight = &self.flight.client;
let mut flight = flight.clone();
let spice_dataset_path = Self::spice_dataset_path(dataset);
let spice_dataset_name = dataset.name.clone();
Box::pin(stream! {
let mut initial_connect = true;
loop {
// On connection reset, clear the data by sending an empty overwrite update and re-subscribe.
if !initial_connect {
tracing::info!("Reconnecting to {spice_dataset_path}");
yield DataUpdate {
data: vec![],
update_type: UpdateType::Overwrite,
};
}
let mut stream = match flight.subscribe(&spice_dataset_path).await {
Ok(stream) => stream,
Err(error) => {
status::update_dataset(spice_dataset_name.clone(), status::ComponentStatus::Error);
tracing::error!("Unable to subscribe to {spice_dataset_path}: {error}");
return;
}
};
loop {
let result = stream.next().await;
status::update_dataset(spice_dataset_name.clone(), status::ComponentStatus::Refreshing);
match result {
Some(Ok(decoded_data)) => if let DecodedPayload::RecordBatch(batch) = decoded_data.payload {
status::update_dataset(spice_dataset_name.clone(), status::ComponentStatus::Ready);
yield DataUpdate {
data: vec![batch],
update_type: UpdateType::Append,
};
} else {
status::update_dataset(spice_dataset_name.clone(), status::ComponentStatus::Ready);
continue;
},
Some(Err(error)) => {
status::update_dataset(spice_dataset_name.clone(), status::ComponentStatus::Error);
tracing::debug!("Error in subscription to {spice_dataset_path}: {error}");
tracing::error!("Error in subscription to {spice_dataset_path}");
continue;
},
None => {
status::update_dataset(spice_dataset_name.clone(), status::ComponentStatus::Ready);
tracing::error!("Flight stream ended for {spice_dataset_path}");
break;
}
};
}
initial_connect = false;
}
})
}
fn get_all_data(
&self,
dataset: &Dataset,
) -> Pin<Box<dyn Future<Output = Vec<arrow::record_batch::RecordBatch>> + Send>> {
let spice_dataset_path = Self::spice_dataset_path(dataset);
self.flight.get_all_data(&spice_dataset_path)
}
fn get_data_publisher(&self) -> Option<Box<dyn DataPublisher>> {
Some(Box::new(self.clone()))
}
fn has_table_provider(&self) -> bool {
true
}
async fn get_table_provider(
&self,
dataset: &Dataset,
) -> std::result::Result<Arc<dyn datafusion::datasource::TableProvider>, super::Error> {
let dataset_path = Self::spice_dataset_path(dataset);
let provider = FlightTable::new(self.flight.client.clone(), dataset_path).await;
match provider {
Ok(provider) => Ok(Arc::new(provider)),
Err(error) => Err(super::Error::UnableToGetTableProvider {
source: error.into(),
}),
}
}
}
impl DataPublisher for SpiceAI {
/// Adds data ingested locally back to the source.
fn add_data(&self, dataset: Arc<Dataset>, data_update: DataUpdate) -> AddDataResult {
let dataset_path = Self::spice_dataset_path(dataset);
let mut client = self.flight.client.clone();
let spaced_trace = Arc::clone(&self.spaced_trace);
let api_key = self.api_key.clone();
Box::pin(async move {
tracing::debug!("Adding data to {dataset_path}");
if let Err(e) = client.publish(&dataset_path, data_update.data).await {
if let flight_client::Error::PermissionDenied {} = e {
tracing::error!("Permission denied when publishing data to Spice AI at '{dataset_path}' - does the API Key ('{api_key}') match the app that owns the dataset?");
} else {
tracing::error!("Error publishing data to Spice AI at '{dataset_path}': {e}");
Err(e).context(UnableToPublishDataSnafu)?;
}
};
info_spaced!(spaced_trace, "Data published to {}", &dataset_path);
Ok(())
})
}
fn name(&self) -> &str {
"SpiceAI"
}
}
impl SpiceAI {
/// Parses a dataset path from a Spice AI dataset definition.
///
/// Spice AI datasets have several possible formats for `dataset.path()`:
/// 1. `<org>/<app>/datasets/<dataset_name>` or `spice.ai/<org>/<app>/datasets/<dataset_name>`.
/// 2. `<org>/<app>` or `spice.ai/<org>/<app>`.
/// 3. `some.blessed.dataset` or `spice.ai/some.blessed.dataset`.
///
/// The second format is a shorthand for the first format, where the dataset name
/// is the same as the local table name specified in `name`.
///
/// The third format is a path to a "blessed" Spice AI dataset (i.e. a dataset that is
/// defined and provided by Spice). If the dataset path does not match the first two formats,
/// then it is assumed to be a path to a blessed dataset.
///
/// This function returns the full dataset path for the given dataset as you would query for it in Spice.
/// i.e. `<org>.<app>.<dataset_name>`
#[allow(clippy::match_same_arms)]
fn spice_dataset_path<T: Borrow<Dataset>>(dataset: T) -> String {
let dataset = dataset.borrow();
let path = dataset.path();
let path = path.trim_start_matches("spice.ai/");
let path_parts: Vec<&str> = path.split('/').collect();
match path_parts.as_slice() {
[org, app] => format!("{org}.{app}.{dataset_name}", dataset_name = dataset.name),
[org, app, "datasets", dataset_name] => format!("{org}.{app}.{dataset_name}"),
[org, app, dataset_name] => format!("{org}.{app}.{dataset_name}"),
_ => path.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_spice_dataset_path() {
let tests = vec![
(
"spiceai:spice.ai/lukekim/demo/datasets/my_data".to_string(),
"lukekim.demo.my_data",
),
(
"spiceai:spice.ai/lukekim/demo/my_data".to_string(),
"lukekim.demo.my_data",
),
(
"spiceai:lukekim/demo/datasets/my_data".to_string(),
"lukekim.demo.my_data",
),
(
"spiceai:lukekim/demo/my_data".to_string(),
"lukekim.demo.my_data",
),
(
"spice.ai/lukekim/demo/datasets/my_data".to_string(),
"lukekim.demo.my_data",
),
(
"spice.ai/lukekim/demo/my_data".to_string(),
"lukekim.demo.my_data",
),
(
"lukekim/demo/datasets/my_data".to_string(),
"lukekim.demo.my_data",
),
("lukekim/demo/my_data".to_string(), "lukekim.demo.my_data"),
("eth.recent_blocks".to_string(), "eth.recent_blocks"),
];
for (input, expected) in tests {
let dataset = Dataset::new(input.clone(), "bar".to_string());
assert_eq!(SpiceAI::spice_dataset_path(dataset), expected, "{input}");
}
}
}