Skip to content

Switch to Postgres storage #1234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 28, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api-server/api-server-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ logging = {path = '../../logging'}
serialization = { path = "../../serialization" }

async-trait.workspace = true
bb8-postgres = "0.8"
clap = { workspace = true, features = ["derive"] }
futures = { workspace = true, default-features = false }
parity-scale-codec.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["full"] }
tokio-postgres = "0.7"
bb8-postgres = "0.8"


[dev-dependencies]
chainstate-test-framework = { path = '../../chainstate/test-framework' }
Expand Down
35 changes: 35 additions & 0 deletions api-server/api-server-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,38 @@
// limitations under the License.

pub mod storage;

use clap::Parser;

#[derive(Parser, Debug)]
pub struct PostgresConfig {
/// Postgres host
#[clap(long, default_value = "localhost")]
pub postgres_host: String,

/// Postgres port
#[clap(long, default_value = "5432")]
pub postgres_port: u16,

/// Postgres user
#[clap(long, default_value = "postgres")]
pub postgres_user: String,

/// Postgres password
#[clap(long)]
pub postgres_password: Option<String>,

/// Postgres database
#[clap(long)]
pub postgres_database: Option<String>,

/// Postgres max connections
#[clap(long, default_value = "10")]
pub postgres_max_connections: u32,
}

impl Default for PostgresConfig {
fn default() -> Self {
Self::parse()
}
}
5 changes: 5 additions & 0 deletions api-server/scanner-daemon/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use api_server_common::PostgresConfig;
use clap::Parser;
use common::chain::config::ChainType;
use std::net::SocketAddr;
Expand Down Expand Up @@ -46,6 +47,10 @@ pub struct ApiServerScannerArgs {
/// RPC password (either provide a username and password, or use a cookie file. You cannot use both)
#[clap(long)]
pub rpc_password: Option<String>,

/// Postgres config values
#[clap(flatten)]
pub postgres_config: PostgresConfig,
}

impl From<Network> for ChainType {
Expand Down
42 changes: 36 additions & 6 deletions api-server/scanner-daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
use std::sync::Arc;

use api_server_common::storage::{
impls::in_memory::transactional::TransactionalApiServerInMemoryStorage,
impls::postgres::TransactionalApiServerPostgresStorage,
storage_api::{
ApiServerStorage, ApiServerStorageRead, ApiServerStorageWrite, ApiServerTransactionRw,
ApiServerStorage, ApiServerStorageError, ApiServerStorageRead, ApiServerStorageWrite,
ApiServerTransactionRw,
},
};
use blockchain_scanner_lib::blockchain_state::BlockchainState;
Expand All @@ -31,9 +32,26 @@ use rpc::RpcAuthData;
use utils::{cookie::COOKIE_FILENAME, default_data_dir::default_data_dir_for_chain};
mod config;

#[must_use]
pub fn make_in_memory_storage(chain_config: &ChainConfig) -> TransactionalApiServerInMemoryStorage {
TransactionalApiServerInMemoryStorage::new(chain_config)
pub async fn make_postgres_storage(
postgres_host: String,
postgres_port: u16,
postgres_user: String,
postgres_password: Option<String>,
postgres_database: Option<String>,
postgres_max_connections: u32,
chain_config: &ChainConfig,
) -> Result<TransactionalApiServerPostgresStorage, ApiServerScannerError> {
TransactionalApiServerPostgresStorage::new(
&postgres_host,
postgres_port,
&postgres_user,
postgres_password.as_deref(),
postgres_database.as_deref(),
postgres_max_connections,
chain_config,
)
.await
.map_err(ApiServerScannerError::PostgresConnectionError)
}

pub async fn run<S: ApiServerStorage>(
Expand Down Expand Up @@ -83,6 +101,8 @@ pub enum ApiServerScannerError {
RpcError(node_comm::rpc_client::NodeRpcError),
#[error("Invalid config: {0}")]
InvalidConfig(String),
#[error("Postgres connection error: {0}")]
PostgresConnectionError(ApiServerStorageError),
}

#[tokio::main]
Expand All @@ -104,6 +124,7 @@ async fn main() -> Result<(), ApiServerScannerError> {
rpc_cookie_file,
rpc_username,
rpc_password,
postgres_config,
} = args;

let chain_type: ChainType = network.into();
Expand Down Expand Up @@ -134,7 +155,16 @@ async fn main() -> Result<(), ApiServerScannerError> {
.await
.map_err(ApiServerScannerError::RpcError)?;

let storage = make_in_memory_storage(&chain_config);
let storage = make_postgres_storage(
postgres_config.postgres_host,
postgres_config.postgres_port,
postgres_config.postgres_user,
postgres_config.postgres_password,
postgres_config.postgres_database,
postgres_config.postgres_max_connections,
&chain_config,
)
.await?;

run(&chain_config, &rpc_client, storage).await?;

Expand Down
5 changes: 5 additions & 0 deletions api-server/web-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use api_server_common::PostgresConfig;
use clap::Parser;
use std::{net::SocketAddr, ops::Deref};

Expand All @@ -27,6 +28,10 @@ pub struct ApiServerWebServerConfig {
/// Default: `127.0.0.1:3000`
#[clap(long)]
pub address: Option<ListenAddress>,

/// Postgres config values
#[clap(flatten)]
pub postgres_config: PostgresConfig,
}

#[derive(Clone, Debug, Parser)]
Expand Down
20 changes: 17 additions & 3 deletions api-server/web-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod api;
mod config;
mod error;

use api_server_common::storage::impls::in_memory::transactional::TransactionalApiServerInMemoryStorage;
use api_server_common::storage::impls::postgres::TransactionalApiServerPostgresStorage;
use axum::{response::IntoResponse, routing::get, Json, Router};
use clap::Parser;
use common::chain::config::create_unit_test_config;
Expand All @@ -43,9 +43,23 @@ async fn main() {
// TODO: generalize network configuration
let chain_config = Arc::new(create_unit_test_config());

// TODO: point database to PostgreSQL from command line arguments
let storage = TransactionalApiServerPostgresStorage::new(
&args.postgres_config.postgres_host,
args.postgres_config.postgres_port,
&args.postgres_config.postgres_user,
args.postgres_config.postgres_password.as_deref(),
args.postgres_config.postgres_database.as_deref(),
args.postgres_config.postgres_max_connections,
&chain_config,
)
.await
.unwrap_or_else(|e| {
log::error!("Error creating Postgres storage: {}", e);
std::process::exit(1);
});

let state = APIServerWebServerState {
db: Arc::new(TransactionalApiServerInMemoryStorage::new(&chain_config)),
db: Arc::new(storage),
chain_config,
};

Expand Down