-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[Indexer] SQL Backfill command #19359
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::database::ConnectionPool; | ||
use diesel_async::RunQueryDsl; | ||
use futures::{stream, StreamExt}; | ||
use std::time::Duration; | ||
use std::time::Instant; | ||
|
||
const CHUNK_SIZE: u64 = 10000; | ||
const MAX_CONCURRENCY: usize = 100; | ||
const SLEEP_BETWEEN_BATCHES_MS: u64 = 100; | ||
|
||
pub async fn run_sql_backfill( | ||
sql: &str, | ||
checkpoint_column_name: &str, | ||
first_checkpoint: u64, | ||
last_checkpoint: u64, | ||
pool: ConnectionPool, | ||
) { | ||
let cur_time = Instant::now(); | ||
let chunks: Vec<(u64, u64)> = (first_checkpoint..=last_checkpoint) | ||
.step_by(CHUNK_SIZE as usize) | ||
.map(|chunk_start| { | ||
let chunk_end = std::cmp::min(chunk_start + CHUNK_SIZE - 1, last_checkpoint); | ||
(chunk_start, chunk_end) | ||
}) | ||
.collect(); | ||
|
||
stream::iter(chunks) | ||
.for_each_concurrent(MAX_CONCURRENCY, |(start_id, end_id)| { | ||
let pool_clone = pool.clone(); // Clone the pool for async operation | ||
async move { | ||
// Run the copy in a batch and add a delay | ||
backfill_data_batch(sql, checkpoint_column_name, start_id, end_id, pool_clone) | ||
.await; | ||
println!("Finished checkpoint range: {} - {}", start_id, end_id); | ||
tokio::time::sleep(Duration::from_millis(SLEEP_BETWEEN_BATCHES_MS)).await; | ||
} | ||
}) | ||
.await; | ||
println!("Finished backfilling in {:?}", cur_time.elapsed()); | ||
} | ||
|
||
async fn backfill_data_batch( | ||
sql: &str, | ||
checkpoint_column_name: &str, | ||
first_checkpoint: u64, | ||
last_checkpoint: u64, | ||
pool: ConnectionPool, | ||
) { | ||
let mut conn = pool.get().await.unwrap(); | ||
|
||
let query = format!( | ||
"{} WHERE {} BETWEEN {} AND {}", | ||
sql, checkpoint_column_name, first_checkpoint, last_checkpoint | ||
); | ||
|
||
// Execute the SQL query using Diesel's async connection | ||
diesel::sql_query(query).execute(&mut conn).await.unwrap(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.