Skip to content

Commit

Permalink
refactor: improve debug logging (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin authored Oct 12, 2021
1 parent 206307f commit a9cf508
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ DECLARE
BEGIN
SELECT INTO mvt ST_AsMVT(tile, 'public.function_source', 4096, 'geom') FROM (
SELECT
ST_AsMVTGeom(ST_Transform(geom, 3857), TileBBox(z, x, y, 3857), 4096, 64, true) AS geom
ST_AsMVTGeom(ST_Transform(ST_CurveToLine(geom), 3857), TileBBox(z, x, y, 3857), 4096, 64, true) AS geom
FROM public.table_source
WHERE geom && TileBBox(z, x, y, 4326)
) as tile WHERE geom IS NOT NULL;
Expand Down
13 changes: 7 additions & 6 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ pub fn generate_config(args: Args, pool: &Pool) -> io::Result<Config> {
}

fn setup_from_config(file_name: String) -> io::Result<(Config, Pool)> {
let config = read_config(&file_name).map_err(prettify_error("Can't read config"))?;
let config = read_config(&file_name).map_err(prettify_error("Can't read config".to_owned()))?;

let pool = setup_connection_pool(
&config.connection_string,
Some(config.pool_size),
config.danger_accept_invalid_certs,
)
.map_err(prettify_error("Can't setup connection pool"))?;
.map_err(prettify_error("Can't setup connection pool".to_owned()))?;

if let Some(table_sources) = &config.table_sources {
for table_source in table_sources.values() {
Expand Down Expand Up @@ -127,10 +127,11 @@ fn setup_from_args(args: Args) -> io::Result<(Config, Pool)> {
args.flag_pool_size,
args.flag_danger_accept_invalid_certs,
)
.map_err(prettify_error("Can't setup connection pool"))?;
.map_err(prettify_error("Can't setup connection pool".to_owned()))?;

info!("Scanning database");
let config = generate_config(args, &pool).map_err(prettify_error("Can't generate config"))?;
let config =
generate_config(args, &pool).map_err(prettify_error("Can't generate config".to_owned()))?;

Ok((config, pool))
}
Expand Down Expand Up @@ -168,7 +169,7 @@ fn start(args: Args) -> io::Result<actix::SystemRunner> {
};

let matches = check_postgis_version(REQUIRED_POSTGIS_VERSION, &pool)
.map_err(prettify_error("Can't check PostGIS version"))?;
.map_err(prettify_error("Can't check PostGIS version".to_owned()))?;

if !matches {
std::process::exit(-1);
Expand All @@ -187,7 +188,7 @@ fn main() -> io::Result<()> {

let args = Docopt::new(USAGE)
.and_then(|d| d.deserialize::<Args>())
.map_err(prettify_error("Can't parse CLI arguments"))?;
.map_err(prettify_error("Can't parse CLI arguments".to_owned()))?;

let args = parse_env(args);

Expand Down
2 changes: 1 addition & 1 deletion src/composite_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Source for CompositeSource {
let tile: Tile = conn
.query_one(tile_query.as_str(), &[])
.map(|row| row.get("tile"))
.map_err(prettify_error("Can't get composite source tile"))?;
.map_err(prettify_error("Can't get composite source tile".to_owned()))?;

Ok(tile)
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn read_config(file_name: &str) -> io::Result<Config> {
file.read_to_string(&mut contents)?;

let config_builder: ConfigBuilder = serde_yaml::from_str(contents.as_str())
.map_err(prettify_error("Can't read config file"))?;
.map_err(prettify_error("Can't read config file".to_owned()))?;

Ok(config_builder.finalize())
}
24 changes: 13 additions & 11 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn make_tls_connector(danger_accept_invalid_certs: bool) -> io::Result<MakeTlsCo
let connector = TlsConnector::builder()
.danger_accept_invalid_certs(danger_accept_invalid_certs)
.build()
.map_err(prettify_error("Can't build TLS connection"))?;
.map_err(prettify_error("Can't build TLS connection".to_owned()))?;

let tls_connector = MakeTlsConnector::new(connector);
Ok(tls_connector)
Expand All @@ -30,23 +30,23 @@ pub fn setup_connection_pool(
danger_accept_invalid_certs: bool,
) -> io::Result<Pool> {
let config = postgres::config::Config::from_str(cn_str)
.map_err(prettify_error("Can't parse connection string"))?;
.map_err(prettify_error("Can't parse connection string".to_owned()))?;

let tls_connector = make_tls_connector(danger_accept_invalid_certs)?;
let manager = PostgresConnectionManager::new(config, tls_connector);

let pool = r2d2::Pool::builder()
.max_size(pool_size.unwrap_or(20))
.build(manager)
.map_err(prettify_error("Can't build connection pool"))?;
.map_err(prettify_error("Can't build connection pool".to_owned()))?;

Ok(pool)
}

pub fn get_connection(pool: &Pool) -> io::Result<Connection> {
let connection = pool
.get()
.map_err(prettify_error("Can't retrieve connection from the pool"))?;
let connection = pool.get().map_err(prettify_error(
"Can't retrieve connection from the pool".to_owned(),
))?;

Ok(connection)
}
Expand All @@ -57,19 +57,21 @@ pub fn select_postgis_verion(pool: &Pool) -> io::Result<String> {
let version = connection
.query_one(include_str!("scripts/get_postgis_version.sql"), &[])
.map(|row| row.get::<_, String>("postgis_version"))
.map_err(prettify_error("Can't get PostGIS version"))?;
.map_err(prettify_error("Can't get PostGIS version".to_owned()))?;

Ok(version)
}

pub fn check_postgis_version(required_postgis_version: &str, pool: &Pool) -> io::Result<bool> {
let postgis_version = select_postgis_verion(pool)?;

let req = VersionReq::parse(required_postgis_version)
.map_err(prettify_error("Can't parse required PostGIS version"))?;
let req = VersionReq::parse(required_postgis_version).map_err(prettify_error(
"Can't parse required PostGIS version".to_owned(),
))?;

let version = Version::parse(postgis_version.as_str())
.map_err(prettify_error("Can't parse database PostGIS version"))?;
let version = Version::parse(postgis_version.as_str()).map_err(prettify_error(
"Can't parse database PostGIS version".to_owned(),
))?;

let matches = req.matches(&version);

Expand Down
9 changes: 6 additions & 3 deletions src/function_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ impl Source for FunctionSource {
&[Type::INT4, Type::INT4, Type::INT4, Type::JSON],
)
.map_err(prettify_error(
"Can't create prepared statement for the tile",
"Can't create prepared statement for the tile".to_owned(),
))?;

let tile = conn
.query_one(&query, &[&xyz.x, &xyz.y, &xyz.z, &query_json])
.map(|row| row.get(self.function.as_str()))
.map_err(prettify_error("Can't get function source tile"))?;
.map_err(prettify_error(format!(
"Can't get \"{}\" tile at /{}/{}/{} with {:?} params",
self.id, &xyz.z, &xyz.x, &xyz.z, &query_json
)))?;

Ok(tile)
}
Expand All @@ -84,7 +87,7 @@ pub fn get_function_sources(conn: &mut Connection) -> Result<FunctionSources, io

let rows = conn
.query(include_str!("scripts/get_function_sources.sql"), &[])
.map_err(prettify_error("Can't get function sources"))?;
.map_err(prettify_error("Can't get function sources".to_owned()))?;

for row in &rows {
let schema: String = row.get("specific_schema");
Expand Down
21 changes: 13 additions & 8 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ struct CompositeTileRequest {
format: String,
}

fn map_internal_error<T: std::fmt::Display>(e: T) -> Error {
error!("{}", e.to_string());
error::ErrorInternalServerError(e.to_string())
}

async fn get_health() -> Result<HttpResponse, Error> {
let response = HttpResponse::Ok().body("OK");
Ok(response)
Expand All @@ -77,8 +82,8 @@ async fn get_table_sources(state: web::Data<AppState>) -> Result<HttpResponse, E
.db
.send(messages::GetTableSources {})
.await
.map_err(|_| HttpResponse::InternalServerError())?
.map_err(|_| HttpResponse::InternalServerError())?;
.map_err(map_internal_error)?
.map_err(map_internal_error)?;

state.coordinator.do_send(messages::RefreshTableSources {
table_sources: Some(table_sources.clone()),
Expand Down Expand Up @@ -192,8 +197,8 @@ async fn get_composite_source_tile(
.db
.send(message)
.await
.map_err(|_| HttpResponse::InternalServerError())?
.map_err(|_| HttpResponse::InternalServerError())?;
.map_err(map_internal_error)?
.map_err(map_internal_error)?;

match tile.len() {
0 => Ok(HttpResponse::NoContent()
Expand All @@ -218,8 +223,8 @@ async fn get_function_sources(state: web::Data<AppState>) -> Result<HttpResponse
.db
.send(messages::GetFunctionSources {})
.await
.map_err(|_| HttpResponse::InternalServerError())?
.map_err(|_| HttpResponse::InternalServerError())?;
.map_err(map_internal_error)?
.map_err(map_internal_error)?;

state.coordinator.do_send(messages::RefreshFunctionSources {
function_sources: Some(function_sources.clone()),
Expand Down Expand Up @@ -310,8 +315,8 @@ async fn get_function_source_tile(
.db
.send(message)
.await
.map_err(|_| HttpResponse::InternalServerError())?
.map_err(|_| HttpResponse::InternalServerError())?;
.map_err(map_internal_error)?
.map_err(map_internal_error)?;

match tile.len() {
0 => Ok(HttpResponse::NoContent()
Expand Down
11 changes: 7 additions & 4 deletions src/table_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TableSource {
let mercator_bounds = utils::tilebbox(xyz);

let properties = if self.properties.is_empty() {
"".to_string()
String::new()
} else {
let properties = self
.properties
Expand Down Expand Up @@ -111,7 +111,10 @@ impl Source for TableSource {
let tile: Tile = conn
.query_one(tile_query.as_str(), &[])
.map(|row| row.get("st_asmvt"))
.map_err(utils::prettify_error("Can't get table source tile"))?;
.map_err(utils::prettify_error(format!(
"Can't get \"{}\" tile at /{}/{}/{}",
self.id, &xyz.z, &xyz.x, &xyz.z
)))?;

Ok(tile)
}
Expand All @@ -126,7 +129,7 @@ pub fn get_table_sources(conn: &mut Connection) -> Result<TableSources, io::Erro

let rows = conn
.query(include_str!("scripts/get_table_sources.sql"), &[])
.map_err(utils::prettify_error("Can't get table sources"))?;
.map_err(utils::prettify_error("Can't get table sources".to_owned()))?;

for row in &rows {
let schema: String = row.get("f_table_schema");
Expand Down Expand Up @@ -159,7 +162,7 @@ pub fn get_table_sources(conn: &mut Connection) -> Result<TableSources, io::Erro
let properties = utils::json_to_hashmap(&row.get("properties"));

let source = TableSource {
id: id.to_string(),
id: id.to_owned(),
schema,
table,
id_column: None,
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use postgis::{ewkb, LineString, Point, Polygon};
use postgres::types::Json;
use serde_json::Value;

pub fn prettify_error<E: std::fmt::Display>(message: &'static str) -> impl Fn(E) -> std::io::Error {
pub fn prettify_error<E: std::fmt::Display>(message: String) -> impl Fn(E) -> std::io::Error {
move |error| std::io::Error::new(std::io::ErrorKind::Other, format!("{}: {}", message, error))
}

Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn json_to_hashmap(value: &serde_json::Value) -> HashMap<String, String> {
let object = value.as_object().unwrap();
for (key, value) in object {
let string_value = value.as_str().unwrap();
hashmap.insert(key.to_string(), string_value.to_string());
hashmap.insert(key.to_owned(), string_value.to_owned());
}

hashmap
Expand Down

0 comments on commit a9cf508

Please sign in to comment.