sui_indexer_alt_framework/cluster.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use std::{
ops::{Deref, DerefMut},
sync::Arc,
};
use anyhow::Context;
use diesel_migrations::EmbeddedMigrations;
use prometheus::Registry;
use sui_indexer_alt_metrics::{MetricsArgs, MetricsService};
use tokio::{signal, task::JoinHandle};
use tokio_util::sync::CancellationToken;
use tracing::info;
use url::Url;
use crate::postgres::{Db, DbArgs};
use crate::{
ingestion::{ClientArgs, IngestionConfig},
Indexer, IndexerArgs, IndexerMetrics, Result,
};
/// Bundle of arguments for setting up an indexer cluster (an Indexer and its associated Metrics
/// service). This struct is offered as a convenience for the common case of parsing command-line
/// arguments for a binary running a standalone indexer and its metrics service.
#[derive(clap::Parser, Debug, Default)]
pub struct Args {
/// What to index and in what time range.
#[clap(flatten)]
pub indexer_args: IndexerArgs,
/// Where to get checkpoint data from.
#[clap(flatten)]
pub client_args: Option<ClientArgs>,
/// How to expose metrics.
#[clap(flatten)]
pub metrics_args: MetricsArgs,
}
/// An opinionated [IndexerCluster] that spins up an [Indexer] implementation using Postgres as its
/// store, along with a [MetricsService] and a tracing subscriber (outputting to stderr) to provide
/// observability. It is a useful starting point for an indexer binary.
pub struct IndexerCluster {
indexer: Indexer<Db>,
metrics: MetricsService,
/// Cancelling this token signals cancellation to both the indexer and metrics service.
cancel: CancellationToken,
}
/// Builder for creating an IndexerCluster with a fluent API
#[derive(Default)]
pub struct IndexerClusterBuilder {
database_url: Option<Url>,
db_args: DbArgs,
args: Args,
ingestion_config: IngestionConfig,
migrations: Option<&'static EmbeddedMigrations>,
metrics_prefix: Option<String>,
}
impl IndexerClusterBuilder {
/// Create a new builder instance
pub fn new() -> Self {
Self::default()
}
/// Set the PostgreSQL database connection URL (required).
///
/// This should be a valid PostgreSQL connection urls, e.g.:
/// - `postgres://user:password@host:5432/mydb`
pub fn with_database_url(mut self, url: Url) -> Self {
self.database_url = Some(url);
self
}
/// Configure database connection parameters such as pool size, connection timeout, etc.
///
/// Defaults to [`DbArgs::default()`] if not specified, which provides reasonable defaults
/// for most use cases.
pub fn with_db_args(mut self, args: DbArgs) -> Self {
self.db_args = args;
self
}
/// Set the main indexer cluster's configuration arguments (required).
///
/// This bundles all configuration needed for the indexer:
/// - `IndexerArgs`: Controls what to index (checkpoint range, which pipelines to run, watermark behavior)
/// - `ClientArgs`: Specifies where to fetch checkpoint data from (remote store, local path, or RPC)
/// - `MetricsArgs`: Configures how to expose Prometheus metrics (address to serve on)
///
/// This overwrites any previously set args.
pub fn with_args(mut self, args: Args) -> Self {
self.args = args;
self
}
/// Set indexer arguments (what to index and in what time range).
/// This overwrites any previously set indexer args.
pub fn with_indexer_args(mut self, args: IndexerArgs) -> Self {
self.args.indexer_args = args;
self
}
/// Set client arguments (where to get checkpoint data from).
/// This overwrites any previously set client args.
pub fn with_client_args(mut self, args: ClientArgs) -> Self {
self.args.client_args = Some(args);
self
}
/// Set metrics arguments (how to expose metrics).
/// This overwrites any previously set metrics args.
pub fn with_metrics_args(mut self, args: MetricsArgs) -> Self {
self.args.metrics_args = args;
self
}
/// Set the ingestion configuration, which controls how the ingestion service is
/// set-up (its concurrency, polling, intervals, etc).
pub fn with_ingestion_config(mut self, config: IngestionConfig) -> Self {
self.ingestion_config = config;
self
}
/// Set database migrations to run.
///
/// See the [Diesel migration guide](https://diesel.rs/guides/migration_guide.html) for more information.
pub fn with_migrations(mut self, migrations: &'static EmbeddedMigrations) -> Self {
self.migrations = Some(migrations);
self
}
/// Add a custom prefix to all metrics reported by this indexer instance.
pub fn with_metrics_prefix(mut self, label: impl Into<String>) -> Self {
self.metrics_prefix = Some(label.into());
self
}
/// Build the IndexerCluster instance.
///
/// Returns an error if:
/// - Required fields are missing
/// - Database connection cannot be established
/// - Metrics registry creation fails
pub async fn build(self) -> Result<IndexerCluster> {
let database_url = self.database_url.context("database_url is required")?;
tracing_subscriber::fmt::init();
let cancel = CancellationToken::new();
let registry = Registry::new();
let metrics = MetricsService::new(self.args.metrics_args, registry, cancel.child_token());
let client_args = self.args.client_args.context("client_args is required")?;
let indexer = Indexer::new_from_pg(
database_url,
self.db_args,
self.args.indexer_args,
client_args,
self.ingestion_config,
self.migrations,
self.metrics_prefix.as_deref(),
metrics.registry(),
cancel.child_token(),
)
.await?;
Ok(IndexerCluster {
indexer,
metrics,
cancel,
})
}
}
impl IndexerCluster {
/// Create a new builder for constructing an IndexerCluster.
pub fn builder() -> IndexerClusterBuilder {
IndexerClusterBuilder::new()
}
/// Access to the indexer's metrics. This can be cloned before a call to [Self::run], to retain
/// shared access to the underlying metrics.
pub fn metrics(&self) -> &Arc<IndexerMetrics> {
self.indexer.metrics()
}
/// This token controls stopping the indexer and metrics service. Clone it before calling
/// [Self::run] to retain the ability to stop the service after it has started.
pub fn cancel(&self) -> &CancellationToken {
&self.cancel
}
/// Starts the indexer and metrics service, returning a handle to `await` the service's exit.
/// The service will exit when the indexer has finished processing all the checkpoints it was
/// configured to process, or when it receives an interrupt signal.
pub async fn run(self) -> Result<JoinHandle<()>> {
let h_ctrl_c = tokio::spawn({
let cancel = self.cancel.clone();
async move {
tokio::select! {
_ = cancel.cancelled() => {}
_ = signal::ctrl_c() => {
info!("Received Ctrl-C, shutting down...");
cancel.cancel();
}
}
}
});
let h_metrics = self.metrics.run().await?;
let h_indexer = self.indexer.run().await?;
Ok(tokio::spawn(async move {
let _ = h_indexer.await;
self.cancel.cancel();
let _ = h_metrics.await;
let _ = h_ctrl_c.await;
}))
}
}
impl Deref for IndexerCluster {
type Target = Indexer<Db>;
fn deref(&self) -> &Self::Target {
&self.indexer
}
}
impl DerefMut for IndexerCluster {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.indexer
}
}
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use diesel::{Insertable, QueryDsl, Queryable};
use diesel_async::RunQueryDsl;
use sui_synthetic_ingestion::synthetic_ingestion;
use tempfile::tempdir;
use crate::ingestion::ClientArgs;
use crate::pipeline::concurrent::{self, ConcurrentConfig};
use crate::pipeline::Processor;
use crate::postgres::{
temp::{get_available_port, TempDb},
Connection, Db, DbArgs,
};
use crate::types::full_checkpoint_content::CheckpointData;
use crate::FieldCount;
use super::*;
diesel::table! {
/// Table for storing transaction counts per checkpoint.
tx_counts (cp_sequence_number) {
cp_sequence_number -> BigInt,
count -> BigInt,
}
}
#[derive(Insertable, Queryable, FieldCount)]
#[diesel(table_name = tx_counts)]
struct StoredTxCount {
cp_sequence_number: i64,
count: i64,
}
/// Test concurrent pipeline for populating [tx_counts].
struct TxCounts;
impl Processor for TxCounts {
const NAME: &'static str = "tx_counts";
type Value = StoredTxCount;
fn process(&self, checkpoint: &Arc<CheckpointData>) -> anyhow::Result<Vec<Self::Value>> {
Ok(vec![StoredTxCount {
cp_sequence_number: checkpoint.checkpoint_summary.sequence_number as i64,
count: checkpoint.transactions.len() as i64,
}])
}
}
#[async_trait::async_trait]
impl concurrent::Handler for TxCounts {
type Store = Db;
async fn commit<'a>(
values: &[Self::Value],
conn: &mut Connection<'a>,
) -> anyhow::Result<usize> {
Ok(diesel::insert_into(tx_counts::table)
.values(values)
.on_conflict_do_nothing()
.execute(conn)
.await?)
}
}
#[tokio::test]
async fn test_indexer_cluster() {
let db = TempDb::new().expect("Failed to create temporary database");
let url = db.database().url();
// Generate test transactions to ingest.
let checkpoint_dir = tempdir().unwrap();
synthetic_ingestion::generate_ingestion(synthetic_ingestion::Config {
ingestion_dir: checkpoint_dir.path().to_owned(),
starting_checkpoint: 0,
num_checkpoints: 10,
checkpoint_size: 2,
})
.await;
let reader = Db::for_read(url.clone(), DbArgs::default()).await.unwrap();
let writer = Db::for_write(url.clone(), DbArgs::default()).await.unwrap();
{
// Create the table we are going to write to. We have to do this manually, because this
// table is not handled by migrations.
let mut conn = writer.connect().await.unwrap();
diesel::sql_query(
r#"
CREATE TABLE tx_counts (
cp_sequence_number BIGINT PRIMARY KEY,
count BIGINT NOT NULL
)
"#,
)
.execute(&mut conn)
.await
.unwrap();
}
let metrics_address =
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), get_available_port());
let args = Args {
client_args: Some(ClientArgs {
local_ingestion_path: Some(checkpoint_dir.path().to_owned()),
remote_store_url: None,
rpc_api_url: None,
rpc_username: None,
rpc_password: None,
}),
indexer_args: IndexerArgs {
first_checkpoint: Some(0),
last_checkpoint: Some(9),
..Default::default()
},
metrics_args: MetricsArgs { metrics_address },
};
let mut indexer = IndexerCluster::builder()
.with_database_url(url.clone())
.with_args(args)
.build()
.await
.unwrap();
indexer
.concurrent_pipeline(TxCounts, ConcurrentConfig::default())
.await
.unwrap();
let metrics = indexer.metrics().clone();
// Run the indexer until it signals completion. We have configured it to stop after
// ingesting 10 checkpoints, so it should shut itself down.
indexer.run().await.unwrap().await.unwrap();
// Check that the results were all written out.
{
let mut conn = reader.connect().await.unwrap();
let counts: Vec<StoredTxCount> = tx_counts::table
.order_by(tx_counts::cp_sequence_number)
.load(&mut conn)
.await
.unwrap();
assert_eq!(counts.len(), 10);
for (i, count) in counts.iter().enumerate() {
assert_eq!(count.cp_sequence_number, i as i64);
assert_eq!(count.count, 2);
}
}
// Check that metrics were updated.
assert_eq!(metrics.total_ingested_checkpoints.get(), 10);
assert_eq!(metrics.total_ingested_transactions.get(), 20);
assert_eq!(metrics.latest_ingested_checkpoint.get(), 9);
macro_rules! assert_pipeline_metric {
($name:ident, $value:expr) => {
assert_eq!(
metrics
.$name
.get_metric_with_label_values(&["tx_counts"])
.unwrap()
.get(),
$value
);
};
}
assert_pipeline_metric!(total_handler_checkpoints_received, 10);
assert_pipeline_metric!(total_handler_checkpoints_processed, 10);
assert_pipeline_metric!(total_handler_rows_created, 10);
assert_pipeline_metric!(latest_processed_checkpoint, 9);
assert_pipeline_metric!(total_collector_checkpoints_received, 10);
assert_pipeline_metric!(total_collector_rows_received, 10);
assert_pipeline_metric!(latest_collected_checkpoint, 9);
// The watermark checkpoint is inclusive, but the transaction is exclusive
assert_pipeline_metric!(watermark_checkpoint, 9);
assert_pipeline_metric!(watermark_checkpoint_in_db, 9);
assert_pipeline_metric!(watermark_transaction, 20);
assert_pipeline_metric!(watermark_transaction_in_db, 20);
}
#[test]
fn test_individual_methods_override_bundled_args() {
let builder = IndexerClusterBuilder::new()
.with_args(Args {
indexer_args: IndexerArgs {
first_checkpoint: Some(100),
..Default::default()
},
client_args: Some(ClientArgs {
local_ingestion_path: Some("/bundled".into()),
..Default::default()
}),
metrics_args: MetricsArgs {
metrics_address: "127.0.0.1:8080".parse().unwrap(),
},
})
.with_indexer_args(IndexerArgs {
first_checkpoint: Some(200),
..Default::default()
})
.with_client_args(ClientArgs {
local_ingestion_path: Some("/individual".into()),
..Default::default()
})
.with_metrics_args(MetricsArgs {
metrics_address: "127.0.0.1:9090".parse().unwrap(),
});
assert_eq!(builder.args.indexer_args.first_checkpoint, Some(200));
assert_eq!(
builder
.args
.client_args
.unwrap()
.local_ingestion_path
.unwrap()
.to_string_lossy(),
"/individual"
);
assert_eq!(
builder.args.metrics_args.metrics_address.to_string(),
"127.0.0.1:9090"
);
}
#[test]
fn test_bundled_args_override_individual_methods() {
let builder = IndexerClusterBuilder::new()
.with_indexer_args(IndexerArgs {
first_checkpoint: Some(200),
..Default::default()
})
.with_client_args(ClientArgs {
local_ingestion_path: Some("/individual".into()),
..Default::default()
})
.with_metrics_args(MetricsArgs {
metrics_address: "127.0.0.1:9090".parse().unwrap(),
})
.with_args(Args {
indexer_args: IndexerArgs {
first_checkpoint: Some(100),
..Default::default()
},
client_args: Some(ClientArgs {
local_ingestion_path: Some("/bundled".into()),
..Default::default()
}),
metrics_args: MetricsArgs {
metrics_address: "127.0.0.1:8080".parse().unwrap(),
},
});
assert_eq!(builder.args.indexer_args.first_checkpoint, Some(100));
assert_eq!(
builder
.args
.client_args
.unwrap()
.local_ingestion_path
.unwrap()
.to_string_lossy(),
"/bundled"
);
assert_eq!(
builder.args.metrics_args.metrics_address.to_string(),
"127.0.0.1:8080"
);
}
}