Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions roapi/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn config_arg() -> clap::Arg {
.short('c')
}

fn get_cmd() -> clap::Command {
pub fn get_cmd() -> clap::Command {
clap::Command::new("roapi")
.version(env!("CARGO_PKG_VERSION"))
.author("QP Hou")
Expand All @@ -156,8 +156,8 @@ fn get_cmd() -> clap::Command {
])
}

pub fn get_configuration() -> Result<Config, Whatever> {
let matches = get_cmd().get_matches();
pub fn get_configuration(cmd: clap::Command) -> Result<Config, Whatever> {
let matches = cmd.get_matches();

let mut config: Config = match matches.get_one::<String>("config") {
None => Config::default(),
Expand Down
20 changes: 15 additions & 5 deletions roapi/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,48 @@ use columnq::error::QueryError;
use columnq::table::TableSource;
use columnq::ColumnQ;
use log::info;
use snafu::{whatever, Whatever};
use snafu::prelude::*;
use tokio::sync::RwLock;

use crate::config::Config;
use crate::error::ApiErrResp;

#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("No table nor kvstore found in config"))]
NoData,
#[snafu(display("Failed to load table: {source}"))]
LoadTable { source: ColumnQError },
#[snafu(display("Failed to load kvstore: {source}"))]
LoadKvstore { source: ColumnQError },
}

pub struct RawRoapiContext {
pub cq: ColumnQ,
pub response_format: encoding::ContentType,
// TODO: store pre serialized schema in handler context
}

impl RawRoapiContext {
pub async fn new(config: &Config, read_only: bool) -> Result<Self, Whatever> {
pub async fn new(config: &Config, read_only: bool) -> Result<Self, Error> {
let mut cq = match config.get_datafusion_config() {
Ok(df_cfg) => ColumnQ::new_with_config(df_cfg, read_only),
_ => ColumnQ::new_with_read_only(read_only),
};

if config.tables.is_empty() && config.kvstores.is_empty() {
whatever!("No table nor kvstore found in config");
return Err(Error::NoData);
}

for t in config.tables.iter() {
info!("loading `{}` as table `{}`", t.io_source, t.name);
whatever!(cq.load_table(t).await, "Failed to load table");
cq.load_table(t).await.context(LoadTableSnafu)?;
info!("registered `{}` as table `{}`", t.io_source, t.name);
}

for k in config.kvstores.iter() {
info!("loading `{}` as kv store `{}`", k.io_source, k.name);
whatever!(cq.load_kv(k.clone()).await, "Failed to load kvstore");
cq.load_kv(k.clone()).await.context(LoadKvstoreSnafu)?;
info!("registered `{}` as kv store `{}`", k.io_source, k.name);
}

Expand Down
28 changes: 16 additions & 12 deletions roapi/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#![deny(warnings)]

use roapi::config::get_configuration;
use roapi::config::{get_cmd, get_configuration};
use roapi::startup::Application;
use snafu::{whatever, Whatever};

#[cfg(feature = "snmalloc")]
#[global_allocator]
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;

#[tokio::main]
async fn main() -> Result<(), Whatever> {
async fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

let config = whatever!(get_configuration(), "Failed to load configuration");
let application = Application::build(config)
.await
.expect("Failed to build App");
application
.run_until_stopped()
.await
.expect("Failed to run App");
Ok(())
let mut cmd = get_cmd();

let re: Result<(), Box<dyn std::error::Error>> = async {
let cmd = cmd.clone();
let config = get_configuration(cmd)?;
let application = Application::build(config).await?;
application.run_until_stopped().await?;
Ok(())
}
.await;
if let Err(e) = re {
cmd.error(clap::error::ErrorKind::Io, format!("{}", e))
.exit();
}
}
4 changes: 3 additions & 1 deletion roapi/src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum Error {
BuildHttpServer { source: server::http::Error },
#[snafu(display("Failed to build FlightSQL server: {source}"))]
BuildFlightSqlServer { source: server::flight_sql::Error },
#[snafu(display("Failed to build context: {source}"))]
BuildContext { source: crate::context::Error },
}

// TODO: replace table reloader with the new concurrent refresh infra
Expand Down Expand Up @@ -61,7 +63,7 @@ impl Application {

let handler_ctx = RawRoapiContext::new(&config, !config.disable_read_only)
.await
.expect("Failed to create Roapi context");
.context(BuildContextSnafu)?;

let tables = config
.tables
Expand Down