Skip to content

Commit

Permalink
Get tile store config from cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Mar 8, 2024
1 parent d0545b6 commit 8e02eba
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 123 deletions.
7 changes: 2 additions & 5 deletions bbox-tile-server/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@ seed-files-test: cleanup docker-is-ready
cargo run -- --config={{justfile_directory()}}/bbox-mvtbench.toml --loglevel=info seed --tileset=ne_countries --maxzoom=3

seed-pmtiles: docker-is-ready
@# BBOX_TILESET_0__CACHE=pmtilecache cargo run -- --config={{justfile_directory()}}/bbox-mvtbench.toml --loglevel=warn seed --tileset=ne_countries --maxzoom=3
sed 's/cache = "tilecache"/cache = "pmtilecache"/g' {{justfile_directory()}}/bbox-mvtbench.toml >/tmp/bbox-mvtbench.toml
cargo run -- --config=/tmp/bbox-mvtbench.toml --loglevel=debug seed --tileset=ne_countries --maxzoom=3
cargo run -- --config={{justfile_directory()}}/bbox-mvtbench.toml --loglevel=debug seed --pm-path=/tmp/tilecache.pmtiles --tileset=ne_countries --maxzoom=3

seed-mbtiles: docker-is-ready
sed 's/cache = "tilecache"/cache = "mbtilecache"/g' {{justfile_directory()}}/bbox-mvtbench.toml >/tmp/bbox-mvtbench.toml
rm -f /tmp/tilecache.mbtiles
cargo run -- --config=/tmp/bbox-mvtbench.toml --loglevel=info seed --tileset=ne_countries --maxzoom=3
cargo run -- --config={{justfile_directory()}}/bbox-mvtbench.toml --loglevel=info seed --mb-path=/tmp/tilecache.mbtiles --tileset=ne_countries --maxzoom=3

export AWS_ACCESS_KEY_ID := "miniostorage"
export AWS_SECRET_ACCESS_KEY := "miniostorage"
Expand Down
41 changes: 18 additions & 23 deletions bbox-tile-server/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::config::{S3StoreCfg, TileStoreCfg};
use crate::config::TileStoreCfg;
use crate::service::{ServiceError, TileService};
use crate::store::{s3::S3Store, s3putfiles, BoxRead, CacheLayout, TileWriter};
use bbox_core::config::error_exit;
use crate::store::{s3putfiles, BoxRead, CacheLayout};
use clap::{Args, Parser};
use futures::{prelude::*, stream};
use indicatif::{ProgressBar, ProgressStyle};
Expand Down Expand Up @@ -58,12 +57,18 @@ pub struct SeedArgs {
/// Extent minx,miny,maxx,maxy (in grid reference system)
#[arg(long)]
pub extent: Option<String>,
/// Base directory for file store
#[arg(long, group = "store")]
pub tile_path: Option<String>,
/// S3 path to upload to (e.g. s3://tiles)
#[arg(long, group = "output_s3", conflicts_with = "output_files")]
#[arg(long, group = "store")]
pub s3_path: Option<String>,
/// Base directory for file output
#[arg(long, group = "output_files", conflicts_with = "output_s3")]
pub base_dir: Option<String>,
/// MBTiles path to store tiles
#[arg(long, group = "store")]
pub mb_path: Option<String>,
/// PMTiles path to store tiles
#[arg(long, group = "store")]
pub pm_path: Option<String>,
/// Number of threads to use, defaults to number of logical cores
#[arg(short, long)]
pub threads: Option<usize>,
Expand Down Expand Up @@ -156,9 +161,6 @@ By-Feature (https://github.com/onthegomap/planetiler/blob/main/ARCHITECTURE.md):

impl TileService {
pub async fn seed_by_grid(&self, args: &SeedArgs) -> anyhow::Result<()> {
let progress = progress_bar();
let progress_main = progress.clone();

let tileset_name = Arc::new(args.tileset.clone());
let tileset = self
.tileset(&args.tileset)
Expand All @@ -183,18 +185,12 @@ impl TileService {
tms.xy_bbox()
};

let Some(mut cache_cfg) = tileset.cache_config() else {
let Some(cache_cfg) = tileset.cache_config() else {
return Err(
ServiceError::TilesetNotFound("Cache configuration not found".to_string()).into(),
);
};
let s3_cache_cfg;
if let Some(s3_path) = &args.s3_path {
s3_cache_cfg = TileStoreCfg::S3(S3StoreCfg {
path: s3_path.to_string(),
});
cache_cfg = &s3_cache_cfg;
};
let tile_writer = Arc::new(tileset.store_writer.clone().unwrap());

// Number of worker threads (size >= #cores).
let threads = args.threads.unwrap_or(num_cpus::get());
Expand All @@ -209,6 +205,8 @@ impl TileService {
// map service source -> tile store writer
// map service source -> temporary file writer -> s3 store writer

let progress = progress_bar();
let progress_main = progress.clone();
let iter = griditer.map(move |xyz| {
let path = CacheLayout::Zxy.path_string(&PathBuf::new(), &xyz, &format);
progress.set_message(path.clone());
Expand All @@ -232,7 +230,6 @@ impl TileService {

match cache_cfg {
TileStoreCfg::Files(_cfg) => {
let tile_writer = Arc::new(tileset.store_writer.clone().unwrap());
par_stream = par_stream.par_then(threads, move |(xyz, tile)| {
let tile_writer = tile_writer.clone();
async move {
Expand All @@ -243,12 +240,10 @@ impl TileService {
});
}
TileStoreCfg::S3(cfg) => {
let s3_writer =
Arc::new(S3Store::from_config(cfg, &format).unwrap_or_else(error_exit));
info!("Writing tiles to {s3_writer:?}");
info!("Writing tiles to {}", &cfg.path);
let s3_writer_thread_count = args.tasks.unwrap_or(256);
par_stream = par_stream.par_then(s3_writer_thread_count, move |(xyz, tile)| {
let s3_writer = s3_writer.clone();
let s3_writer = tile_writer.clone();
async move {
let _ = s3_writer.put_tile(&xyz, tile).await;
let empty: Box<dyn Read + Send + Sync> = Box::new(std::io::empty());
Expand Down
28 changes: 28 additions & 0 deletions bbox-tile-server/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cli::Commands;
use crate::datasource::source_config_from_cli_arg;
use crate::t_rex::config as t_rex;
use bbox_core::cli::CommonCommands;
Expand Down Expand Up @@ -241,6 +242,33 @@ pub struct PmtilesStoreCfg {
pub path: PathBuf,
}

impl TileStoreCfg {
pub fn from_cli_args(cli: &ArgMatches) -> Option<Self> {
let Ok(Commands::Seed(args)) = Commands::from_arg_matches(cli) else {
return None;
};
if let Some(path) = &args.tile_path {
let cache_cfg = TileStoreCfg::Files(FileStoreCfg {
base_dir: path.into(),
});
Some(cache_cfg)
} else if let Some(s3_path) = &args.s3_path {
let cache_cfg = TileStoreCfg::S3(S3StoreCfg {
path: s3_path.to_string(),
});
Some(cache_cfg)
} else if let Some(path) = &args.mb_path {
let cache_cfg = TileStoreCfg::Mbtiles(MbtilesStoreCfg { path: path.into() });
Some(cache_cfg)
} else if let Some(path) = &args.pm_path {
let cache_cfg = TileStoreCfg::Pmtiles(PmtilesStoreCfg { path: path.into() });
Some(cache_cfg)
} else {
None
}
}
}

impl TileserverCfg {
pub fn from_config(cli: &ArgMatches) -> Self {
let mut cfg: TileserverCfg = from_config_root_or_exit();
Expand Down
12 changes: 7 additions & 5 deletions bbox-tile-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ impl OgcApiService for TileService {
.get(name)
.unwrap_or_else(|| error_exit(ServiceError::CacheNotFound(name.to_string())))
});
let store_writer = if let Some(config) = cache_cfg {
Some(store_writer_from_config(config, &ts.name, &format, metadata).await)
} else {
None
};

let store_writer =
if let Some(config) = TileStoreCfg::from_cli_args(cli).or(cache_cfg.cloned()) {
Some(store_writer_from_config(&config, &ts.name, &format, metadata).await)
} else {
None
};
let store_reader = if let Some(config) = cache_cfg {
Some(store_reader_from_config(config, &ts.name, &format).await)
} else {
Expand Down
15 changes: 1 addition & 14 deletions bbox-tile-server/src/store/files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cli::SeedArgs;
use crate::config::FileStoreCfg;
use crate::store::{BoxRead, CacheLayout, TileReader, TileStoreError, TileStoreType, TileWriter};
use crate::store::{BoxRead, CacheLayout, TileReader, TileStoreError, TileWriter};
use async_trait::async_trait;
use bbox_core::endpoints::TileResponse;
use bbox_core::Format;
Expand Down Expand Up @@ -31,18 +30,6 @@ impl FileStore {
}
}

#[async_trait]
impl TileStoreType for FileStore {
async fn from_args(args: &SeedArgs, format: &Format) -> Result<Self, TileStoreError> {
let base_dir = PathBuf::from(
args.base_dir
.as_ref()
.ok_or(TileStoreError::ArgMissing("base_dir".to_string()))?,
);
Ok(FileStore::new(base_dir, *format))
}
}

#[async_trait]
impl TileWriter for FileStore {
async fn exists(&self, tile: &Xyz) -> bool {
Expand Down
17 changes: 1 addition & 16 deletions bbox-tile-server/src/store/mbtiles.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::cli::SeedArgs;
use crate::config::MbtilesStoreCfg;
use crate::mbtiles_ds::{Error as MbtilesDsError, MbtilesDatasource};
use crate::store::{BoxRead, TileReader, TileStoreError, TileStoreType, TileWriter};
use crate::store::{BoxRead, TileReader, TileStoreError, TileWriter};
use async_trait::async_trait;
use bbox_core::endpoints::TileResponse;
use bbox_core::Format;
use flate2::{read::GzEncoder, Compression};
use log::info;
use martin_mbtiles::{CopyDuplicateMode, MbtType, Metadata};
Expand Down Expand Up @@ -46,19 +44,6 @@ impl MbtilesStore {
}
}

#[async_trait]
impl TileStoreType for MbtilesStore {
async fn from_args(args: &SeedArgs, _format: &Format) -> Result<Self, TileStoreError> {
let path = args
.base_dir
.as_ref()
.ok_or(TileStoreError::ArgMissing("base_dir".to_string()))?
.into();
let cfg = MbtilesStoreCfg { path };
MbtilesStore::from_config(&cfg).await.map_err(Into::into)
}
}

#[async_trait]
impl TileWriter for MbtilesStore {
async fn exists(&self, tile: &Xyz) -> bool {
Expand Down
8 changes: 0 additions & 8 deletions bbox-tile-server/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod pmtiles;
pub mod s3;
pub mod s3putfiles;

use crate::cli::SeedArgs;
use crate::config::TileStoreCfg;
use crate::mbtiles_ds::Error as MbtilesDsError;
use crate::store::files::FileStore;
Expand Down Expand Up @@ -48,13 +47,6 @@ pub enum TileStoreError {
PmtilesError(#[from] ::pmtiles::error::Error),
}

#[async_trait]
pub trait TileStoreType {
async fn from_args(args: &SeedArgs, format: &Format) -> Result<Self, TileStoreError>
where
Self: Clone + Sized;
}

#[async_trait]
pub trait TileWriter: DynClone + Send + Sync {
/// Check for existing tile
Expand Down
40 changes: 1 addition & 39 deletions bbox-tile-server/src/store/pmtiles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cli::SeedArgs;
use crate::config::PmtilesStoreCfg;
use crate::store::{BoxRead, TileReader, TileStoreError, TileStoreType, TileWriter};
use crate::store::{BoxRead, TileReader, TileStoreError, TileWriter};
use async_trait::async_trait;
use bbox_core::endpoints::TileResponse;
use bbox_core::Format;
Expand All @@ -15,7 +14,6 @@ use std::fs::File;
use std::io::{Cursor, Read};
use std::path::PathBuf;
use tile_grid::Xyz;
use tilejson::tilejson;

pub struct PmtilesStoreReader {
pub path: PathBuf,
Expand Down Expand Up @@ -60,18 +58,6 @@ impl PmtilesStoreReader {
}
}

#[async_trait]
impl TileStoreType for PmtilesStoreReader {
async fn from_args(args: &SeedArgs, _format: &Format) -> Result<Self, TileStoreError> {
let path = PathBuf::from(
args.base_dir
.as_ref()
.ok_or(TileStoreError::ArgMissing("base_dir".to_string()))?,
);
Self::create_reader(path).await
}
}

#[async_trait]
impl TileReader for PmtilesStoreReader {
async fn get_tile(&self, tile: &Xyz) -> Result<Option<TileResponse>, TileStoreError> {
Expand All @@ -88,30 +74,6 @@ impl TileReader for PmtilesStoreReader {
}
}

#[async_trait]
impl TileStoreType for PmtilesStoreWriter {
async fn from_args(args: &SeedArgs, format: &Format) -> Result<Self, TileStoreError> {
let path = PathBuf::from(
args.base_dir
.as_ref()
.ok_or(TileStoreError::ArgMissing("base_dir".to_string()))?,
);
let metadata = Metadata {
id: "pmtiles".to_string(),
tile_info: martin_tile_utils::TileInfo {
format: martin_tile_utils::Format::parse(format.file_suffix())
.unwrap_or(martin_tile_utils::Format::Mvt),
encoding: martin_tile_utils::Encoding::Uncompressed,
},
layer_type: None,
tilejson: tilejson! { tiles: vec![] },
json: None,
agg_tiles_hash: None,
};
Ok(Self::new(path, metadata, format))
}
}

#[async_trait]
impl TileWriter for PmtilesStoreWriter {
async fn exists(&self, _tile: &Xyz) -> bool {
Expand Down
14 changes: 1 addition & 13 deletions bbox-tile-server/src/store/s3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::cli::SeedArgs;
use crate::config::S3StoreCfg;
use crate::store::CacheLayout;
use crate::store::{BoxRead, TileReader, TileStoreError, TileStoreType, TileWriter};
use crate::store::{BoxRead, TileReader, TileStoreError, TileWriter};
use async_trait::async_trait;
use bbox_core::endpoints::TileResponse;
use bbox_core::Format;
Expand Down Expand Up @@ -62,17 +61,6 @@ impl S3Store {
}
}

#[async_trait]
impl TileStoreType for S3Store {
async fn from_args(args: &SeedArgs, format: &Format) -> Result<Self, TileStoreError> {
let s3_path = args
.s3_path
.as_ref()
.ok_or(TileStoreError::ArgMissing("s3_path".to_string()))?;
Self::from_s3_path(s3_path, *format).map_err(Into::into)
}
}

#[async_trait]
impl TileWriter for S3Store {
async fn exists(&self, _tile: &Xyz) -> bool {
Expand Down

0 comments on commit 8e02eba

Please sign in to comment.