Skip to content

Commit

Permalink
indexer alt: integrate tempDB (#20115)
Browse files Browse the repository at this point in the history
## Description 

title to unblock committer related tests including some test utils for
those tests.

## Test plan 

- modified smoketest of tempdb
- added test for reset_database without running migrations

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
gegaowp authored Nov 1, 2024
1 parent 69d3653 commit ae9d809
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 148 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ members = [
"crates/sui-package-dump",
"crates/sui-package-management",
"crates/sui-package-resolver",
"crates/sui-pg-temp-db",
"crates/sui-proc-macros",
"crates/sui-protocol-config",
"crates/sui-protocol-config-macros",
Expand Down Expand Up @@ -666,6 +667,7 @@ sui-open-rpc-macros = { path = "crates/sui-open-rpc-macros" }
sui-package-dump = { path = "crates/sui-package-dump" }
sui-package-management = { path = "crates/sui-package-management" }
sui-package-resolver = { path = "crates/sui-package-resolver" }
sui-pg-temp-db = { path = "crates/sui-pg-temp-db" }
sui-proc-macros = { path = "crates/sui-proc-macros" }
sui-protocol-config = { path = "crates/sui-protocol-config" }
sui-protocol-config-macros = { path = "crates/sui-protocol-config-macros" }
Expand Down
1 change: 1 addition & 0 deletions crates/sui-bridge-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sui-bridge.workspace = true
sui-sdk.workspace = true
sui-json-rpc-types.workspace = true
sui-data-ingestion-core.workspace = true
sui-pg-temp-db.workspace = true
sui-types.workspace = true
telemetry-subscribers.workspace = true
tracing.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-bridge-indexer/tests/indexer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use sui_bridge_indexer::storage::PgBridgePersistent;
use sui_bridge_indexer::{create_sui_indexer, schema};
use sui_data_ingestion_core::DataIngestionMetrics;
use sui_indexer::database::Connection;
use sui_indexer::tempdb::TempDb;
use sui_indexer_builder::indexer_builder::IndexerProgressStore;
use sui_pg_temp_db::TempDb;

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("src/migrations");

Expand Down
1 change: 1 addition & 0 deletions crates/sui-cluster-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ regex.workspace = true
sui-indexer.workspace = true
sui-faucet.workspace = true
sui-graphql-rpc.workspace = true
sui-pg-temp-db.workspace = true
sui-swarm.workspace = true
sui-swarm-config.workspace = true
sui-json-rpc-types.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-cluster-test/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use sui_config::Config;
use sui_config::{PersistedConfig, SUI_KEYSTORE_FILENAME, SUI_NETWORK_CONFIG};
use sui_graphql_rpc::config::{ConnectionConfig, ServiceConfig};
use sui_graphql_rpc::test_infra::cluster::start_graphql_server_with_fn_rpc;
use sui_indexer::tempdb::TempDb;
use sui_indexer::test_utils::{
start_indexer_jsonrpc_for_testing, start_indexer_writer_for_testing,
};
use sui_keys::keystore::{AccountKeystore, FileBasedKeystore, Keystore};
use sui_pg_temp_db::TempDb;
use sui_sdk::sui_client_config::{SuiClientConfig, SuiEnv};
use sui_sdk::wallet_context::WalletContext;
use sui_swarm::memory::Swarm;
Expand Down
1 change: 1 addition & 0 deletions crates/sui-graphql-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ serde_with.workspace = true
serde_yaml.workspace = true
shared-crypto.workspace = true
similar.workspace = true
sui-pg-temp-db.workspace = true
sui-sdk.workspace = true
sui-types.workspace = true
tap.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-graphql-rpc/src/data/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ mod tests {
use sui_framework::BuiltInFramework;
use sui_indexer::{
database::Connection, db::reset_database, models::objects::StoredObject, schema::objects,
tempdb::TempDb, types::IndexedObject,
types::IndexedObject,
};
use sui_pg_temp_db::TempDb;

#[tokio::test]
async fn test_query_cost() {
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-graphql-rpc/src/server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ pub mod tests {
use serde_json::json;
use std::sync::Arc;
use std::time::Duration;
use sui_indexer::tempdb::get_available_port;
use sui_pg_temp_db::get_available_port;
use sui_sdk::SuiClient;
use sui_types::digests::get_mainnet_chain_identifier;
use sui_types::transaction::TransactionData;
Expand Down
3 changes: 1 addition & 2 deletions crates/sui-graphql-rpc/src/test_infra/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ pub use sui_indexer::config::RetentionConfig;
pub use sui_indexer::config::SnapshotLagConfig;
use sui_indexer::errors::IndexerError;
use sui_indexer::store::PgIndexerStore;
use sui_indexer::tempdb::get_available_port;
use sui_indexer::tempdb::TempDb;
use sui_indexer::test_utils::start_indexer_writer_for_testing;
use sui_pg_temp_db::{get_available_port, TempDb};
use sui_swarm_config::genesis_config::{AccountConfig, DEFAULT_GAS_AMOUNT};
use sui_types::storage::RestStateReader;
use tempfile::tempdir;
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-graphql-rpc/tests/move_registry_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use sui_graphql_rpc::{
},
};
use sui_graphql_rpc_client::simple_client::SimpleClient;
use sui_indexer::tempdb::get_available_port;
use sui_json_rpc::name_service::{Domain, DomainFormat};
use sui_json_rpc_types::ObjectChange;
use sui_move_build::BuildConfig;
use sui_pg_temp_db::get_available_port;
use sui_types::{
base_types::{ObjectID, SequenceNumber},
digests::ObjectDigest,
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-indexer-alt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ url.workspace = true

mysten-metrics.workspace = true
sui-field-count.workspace = true
sui-pg-temp-db.workspace = true
sui-storage.workspace = true
sui-types.workspace = true

[dev-dependencies]
rand.workspace = true
wiremock.workspace = true
tempfile.workspace = true
wiremock.workspace = true

sui-types = { workspace = true, features = ["test-utils"] }
85 changes: 83 additions & 2 deletions crates/sui-indexer-alt/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use tracing::info;
use url::Url;

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
const DEFAULT_POOL_SIZE: u32 = 100;
const DEFAULT_CONNECTION_TIMEOUT_SECS: u64 = 60;

#[derive(Clone)]
pub struct Db {
Expand All @@ -30,13 +32,13 @@ pub struct DbConfig {
database_url: Url,

/// Number of connections to keep in the pool.
#[arg(long, default_value_t = 100)]
#[arg(long, default_value_t = DEFAULT_POOL_SIZE)]
connection_pool_size: u32,

/// Time spent waiting for a connection from the pool to become available.
#[arg(
long,
default_value = "60",
default_value = stringify!(DEFAULT_CONNECTION_TIMEOUT_SECS),
value_name = "SECONDS",
value_parser = |s: &str| s.parse().map(Duration::from_secs)
)]
Expand Down Expand Up @@ -144,6 +146,21 @@ impl Db {
}
}

impl DbConfig {
pub fn new(
database_url: Url,
connection_pool_size: Option<u32>,
connection_timeout: Option<Duration>,
) -> Self {
Self {
database_url,
connection_pool_size: connection_pool_size.unwrap_or(DEFAULT_POOL_SIZE),
connection_timeout: connection_timeout
.unwrap_or(Duration::from_secs(DEFAULT_CONNECTION_TIMEOUT_SECS)),
}
}
}

/// Drop all tables and rerunning migrations.
pub async fn reset_database(
db_config: DbConfig,
Expand All @@ -156,3 +173,67 @@ pub async fn reset_database(
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::db::{Db, DbConfig};
use diesel::prelude::QueryableByName;
use diesel_async::RunQueryDsl;
use sui_pg_temp_db::TempDb;

#[tokio::test]
async fn temp_db_smoketest() {
telemetry_subscribers::init_for_testing();
let db = TempDb::new().unwrap();
let url = db.database().url();
println!("url: {}", url.as_str());
let db_config = DbConfig::new(url.clone(), None, None);
let db = Db::new(db_config).await.unwrap();
let mut connection = db.connect().await.unwrap();

// Run a simple query to verify the db can properly be queried
let resp = diesel::sql_query("SELECT datname FROM pg_database")
.execute(&mut connection)
.await
.unwrap();
println!("resp: {:?}", resp);
}

#[derive(QueryableByName)]
struct CountResult {
#[diesel(sql_type = diesel::sql_types::BigInt)]
cnt: i64,
}

#[tokio::test]
async fn test_reset_database_skip_migrations() {
let temp_db = TempDb::new().unwrap();
let url = temp_db.database().url();
let db_config = DbConfig::new(url.clone(), None, None);

let db = Db::new(db_config.clone()).await.unwrap();
let mut conn = db.connect().await.unwrap();
diesel::sql_query("CREATE TABLE test_table (id INTEGER PRIMARY KEY)")
.execute(&mut conn)
.await
.unwrap();
let cnt = diesel::sql_query(
"SELECT COUNT(*) as cnt FROM information_schema.tables WHERE table_name = 'test_table'",
)
.get_result::<CountResult>(&mut conn)
.await
.unwrap();
assert_eq!(cnt.cnt, 1);

reset_database(db_config, true).await.unwrap();

let mut conn = db.connect().await.unwrap();
let cnt = diesel::sql_query(
"SELECT COUNT(*) as cnt FROM information_schema.tables WHERE table_name = 'test_table'",
)
.get_result::<CountResult>(&mut conn)
.await
.unwrap();
assert_eq!(cnt.cnt, 0);
}
}
1 change: 1 addition & 0 deletions crates/sui-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sui-json-rpc.workspace = true
sui-json-rpc-api.workspace = true
sui-json-rpc-types.workspace = true
sui-open-rpc.workspace = true
sui-pg-temp-db.workspace = true
sui-sdk.workspace = true
sui-snapshot.workspace = true
sui-storage.workspace = true
Expand Down
23 changes: 22 additions & 1 deletion crates/sui-indexer/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ mod tests {
check_db_migration_consistency, check_db_migration_consistency_impl, reset_database,
ConnectionPoolConfig, MIGRATIONS,
};
use crate::tempdb::TempDb;
use diesel::migration::{Migration, MigrationSource};
use diesel::pg::Pg;
use diesel_migrations::MigrationHarness;
use sui_pg_temp_db::TempDb;

// Check that the migration records in the database created from the local schema
// pass the consistency check.
Expand Down Expand Up @@ -392,4 +392,25 @@ mod tests {
.await
.unwrap();
}

#[tokio::test]
async fn temp_db_smoketest() {
use crate::database::Connection;
use diesel_async::RunQueryDsl;
use sui_pg_temp_db::TempDb;

telemetry_subscribers::init_for_testing();

let db = TempDb::new().unwrap();
let url = db.database().url();
println!("url: {}", url.as_str());
let mut connection = Connection::dedicated(url).await.unwrap();

// Run a simple query to verify the db can properly be queried
let resp = diesel::sql_query("SELECT datname FROM pg_database")
.execute(&mut connection)
.await
.unwrap();
println!("resp: {:?}", resp);
}
}
1 change: 0 additions & 1 deletion crates/sui-indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub mod restorer;
pub mod schema;
pub mod store;
pub mod system_package_task;
pub mod tempdb;
pub mod test_utils;
pub mod types;

Expand Down
3 changes: 1 addition & 2 deletions crates/sui-indexer/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use sui_json_rpc_types::SuiTransactionBlockResponse;
use sui_pg_temp_db::{get_available_port, TempDb};

use crate::config::{IngestionConfig, RetentionConfig, SnapshotLagConfig, UploadOptions};
use crate::database::Connection;
Expand All @@ -19,8 +20,6 @@ use crate::db::ConnectionPoolConfig;
use crate::errors::IndexerError;
use crate::indexer::Indexer;
use crate::store::PgIndexerStore;
use crate::tempdb::get_available_port;
use crate::tempdb::TempDb;
use crate::IndexerMetrics;

/// Wrapper over `Indexer::start_reader` to make it easier to configure an indexer jsonrpc reader
Expand Down
16 changes: 16 additions & 0 deletions crates/sui-pg-temp-db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "sui-pg-temp-db"
version.workspace = true
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
publish = false
edition = "2021"

[dependencies]
anyhow.workspace = true
diesel.workspace = true
diesel-async = { workspace = true, features = ["bb8", "postgres", "async-connection-wrapper"] }
tempfile.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing.workspace = true
url.workspace = true
Loading

0 comments on commit ae9d809

Please sign in to comment.