-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Pipeline for writing out the set of latest packages. This is used to do type resolution during reads, and also to answer queries about package versions. Originally, this was called `kv_packages`, but I realised that because of system packages, it is not append-only, so I renamed it to match the other summary tables. ## Test plan Ran the indexer with the new pipeline on the first 1.2M checkpoints. This includes multiple system package upgrades and the first user package publish. Inspect the contents of the table at that point: ``` sui$ cargo run -p sui-indexer-alt --release -- \ --database-url "postgres://postgres:postgrespw@localhost:5432/sui_indexer_alt" \ indexer --remote-store-url https://checkpoints.mainnet.sui.io \ --last-checkpoint 1200000 --pipeline sum_packages ``` ``` sui_indexer_alt=# SELECT package_id, original_id, package_version FROM sum_packages; package_id | original_id | package_version --------------------------------------------------------------------+--------------------------------------------------------------------+----------------- \x0000000000000000000000000000000000000000000000000000000000000001 | \x0000000000000000000000000000000000000000000000000000000000000001 | 1 \x0000000000000000000000000000000000000000000000000000000000000002 | \x0000000000000000000000000000000000000000000000000000000000000002 | 2 \x0000000000000000000000000000000000000000000000000000000000000003 | \x0000000000000000000000000000000000000000000000000000000000000003 | 3 \x000000000000000000000000000000000000000000000000000000000000dee9 | \x000000000000000000000000000000000000000000000000000000000000dee9 | 2 \x39ac04c24dbedf422abb8d582973ee733dbbab07a597fc98300666abe7982034 | \x39ac04c24dbedf422abb8d582973ee733dbbab07a597fc98300666abe7982034 | 1 ```
- Loading branch information
Showing
8 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
crates/sui-indexer-alt/migrations/2024-10-31-000319_sum_packages/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS sum_packages; |
14 changes: 14 additions & 0 deletions
14
crates/sui-indexer-alt/migrations/2024-10-31-000319_sum_packages/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE TABLE IF NOT EXISTS sum_packages | ||
( | ||
package_id BYTEA PRIMARY KEY, | ||
original_id BYTEA NOT NULL, | ||
package_version BIGINT NOT NULL, | ||
move_package BYTEA NOT NULL, | ||
cp_sequence_number BIGINT NOT NULL | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS sum_packages_cp_id_version | ||
ON sum_packages (cp_sequence_number, original_id, package_version); | ||
|
||
CREATE INDEX IF NOT EXISTS sum_packages_id_version_cp | ||
ON sum_packages (original_id, package_version, cp_sequence_number); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::{collections::BTreeMap, sync::Arc}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use diesel::{upsert::excluded, ExpressionMethods}; | ||
use diesel_async::RunQueryDsl; | ||
use futures::future::try_join_all; | ||
use sui_types::full_checkpoint_content::CheckpointData; | ||
|
||
use crate::{ | ||
db, | ||
models::packages::StoredPackage, | ||
pipeline::{sequential::Handler, Processor}, | ||
schema::sum_packages, | ||
}; | ||
|
||
const CHUNK_ROWS: usize = i16::MAX as usize / 5; | ||
|
||
pub struct SumPackages; | ||
|
||
impl Processor for SumPackages { | ||
const NAME: &'static str = "sum_packages"; | ||
|
||
type Value = StoredPackage; | ||
|
||
fn process(checkpoint: &Arc<CheckpointData>) -> Result<Vec<Self::Value>> { | ||
let CheckpointData { | ||
checkpoint_summary, | ||
transactions, | ||
.. | ||
} = checkpoint.as_ref(); | ||
|
||
let cp_sequence_number = checkpoint_summary.sequence_number as i64; | ||
let mut values = vec![]; | ||
for tx in transactions { | ||
for obj in &tx.output_objects { | ||
let Some(package) = obj.data.try_as_package() else { | ||
continue; | ||
}; | ||
|
||
values.push(StoredPackage { | ||
package_id: obj.id().to_vec(), | ||
original_id: package.original_package_id().to_vec(), | ||
package_version: obj.version().value() as i64, | ||
move_package: bcs::to_bytes(package) | ||
.map_err(|e| anyhow!("Error serializing package {}: {e}", obj.id()))?, | ||
cp_sequence_number, | ||
}); | ||
} | ||
} | ||
|
||
Ok(values) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Handler for SumPackages { | ||
type Batch = BTreeMap<Vec<u8>, StoredPackage>; | ||
|
||
fn batch(batch: &mut Self::Batch, values: Vec<Self::Value>) { | ||
for value in values { | ||
batch.insert(value.package_id.clone(), value); | ||
} | ||
} | ||
|
||
async fn commit(batch: &Self::Batch, conn: &mut db::Connection<'_>) -> Result<usize> { | ||
let values: Vec<_> = batch.values().cloned().collect(); | ||
let updates = values.chunks(CHUNK_ROWS).map(|chunk| { | ||
diesel::insert_into(sum_packages::table) | ||
.values(chunk) | ||
.on_conflict(sum_packages::package_id) | ||
.do_update() | ||
.set(( | ||
sum_packages::package_version.eq(excluded(sum_packages::package_version)), | ||
sum_packages::move_package.eq(excluded(sum_packages::move_package)), | ||
sum_packages::cp_sequence_number.eq(excluded(sum_packages::cp_sequence_number)), | ||
)) | ||
.execute(conn) | ||
}); | ||
|
||
Ok(try_join_all(updates).await?.into_iter().sum()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
pub mod checkpoints; | ||
pub mod events; | ||
pub mod objects; | ||
pub mod packages; | ||
pub mod transactions; | ||
pub mod watermarks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use diesel::prelude::*; | ||
|
||
use crate::schema::sum_packages; | ||
|
||
#[derive(Insertable, Debug, Clone)] | ||
#[diesel(table_name = sum_packages, primary_key(package_id))] | ||
pub struct StoredPackage { | ||
pub package_id: Vec<u8>, | ||
pub original_id: Vec<u8>, | ||
pub package_version: i64, | ||
pub move_package: Vec<u8>, | ||
pub cp_sequence_number: i64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters