Skip to content

Commit 342df3c

Browse files
authored
Fix: delete pg auction (#576)
* add tx_hash to bid analytics tables * delete from auction * add auction creation time index * create auction buffer * concurrent index * upd * bump version * new sqlx
1 parent 81c5ba3 commit 342df3c

11 files changed

+91
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auction-server/.sqlx/query-d9c19f7767f197f7fea6dc0ebc34e1a1262b540aa8ecd8296ea03cc672a71602.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auction-server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "auction-server"
3-
version = "0.34.3"
3+
version = "0.35.0"
44
edition = "2021"
55
license-file = "license.txt"
66

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE bid_swap ADD COLUMN tx_hash Nullable(String);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX IF EXISTS auction_creation_time_idx;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX CONCURRENTLY auction_creation_time_idx ON auction (creation_time);

auction-server/src/auction/repository/add_bid_analytics.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ impl Repository {
3737
decimals: HashMap<Pubkey, u8>,
3838
) -> anyhow::Result<()> {
3939
let transaction = STANDARD.encode(bincode::serialize(&bid.chain_data.transaction.clone())?);
40+
let tx_hash = match &bid.status {
41+
entities::BidStatusSvm::Pending => None,
42+
entities::BidStatusSvm::AwaitingSignature { auction } => {
43+
Some(auction.tx_hash.to_string())
44+
}
45+
entities::BidStatusSvm::SentToUserForSubmission { auction } => {
46+
Some(auction.tx_hash.to_string())
47+
}
48+
entities::BidStatusSvm::Submitted { auction } => Some(auction.tx_hash.to_string()),
49+
entities::BidStatusSvm::Lost { auction } => {
50+
auction.as_ref().map(|a| a.tx_hash.to_string())
51+
}
52+
entities::BidStatusSvm::Won { auction } => Some(auction.tx_hash.to_string()),
53+
entities::BidStatusSvm::Failed { auction, reason: _ } => {
54+
Some(auction.tx_hash.to_string())
55+
}
56+
entities::BidStatusSvm::Expired { auction } => Some(auction.tx_hash.to_string()),
57+
entities::BidStatusSvm::Cancelled { auction } => Some(auction.tx_hash.to_string()),
58+
entities::BidStatusSvm::SubmissionFailed { auction, reason: _ } => {
59+
Some(auction.tx_hash.to_string())
60+
}
61+
};
4062
let bid_analytics = match data {
4163
entities::BidTransactionData::SubmitBid(transaction_data) => {
4264
let SubmitBidArgs {
@@ -53,6 +75,7 @@ impl Repository {
5375
bid_amount: bid.amount,
5476

5577
auction_id: bid.status.get_auction_id(),
78+
tx_hash,
5679
submission_time: bid.submission_time,
5780
conclusion_time: bid.conclusion_time,
5881

@@ -113,6 +136,7 @@ impl Repository {
113136
bid_amount: bid.amount,
114137

115138
auction_id: bid.status.get_auction_id(),
139+
tx_hash,
116140
opportunity_id: bid.opportunity_id,
117141
conclusion_time: bid.conclusion_time,
118142

auction-server/src/auction/repository/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ pub struct BidAnalyticsSwap {
858858

859859
#[serde(with = "clickhouse::serde::uuid::option")]
860860
pub auction_id: Option<Uuid>,
861+
pub tx_hash: Option<String>,
861862
#[serde(with = "clickhouse::serde::time::datetime64::micros::option")]
862863
pub submission_time: Option<OffsetDateTime>,
863864
#[serde(with = "clickhouse::serde::uuid::option")]
@@ -905,6 +906,7 @@ pub struct BidAnalyticsLimo {
905906

906907
#[serde(with = "clickhouse::serde::uuid::option")]
907908
pub auction_id: Option<Uuid>,
909+
pub tx_hash: Option<String>,
908910
#[serde(with = "clickhouse::serde::time::datetime64::micros::option")]
909911
pub submission_time: Option<OffsetDateTime>,
910912
#[serde(with = "clickhouse::serde::time::datetime64::micros::option")]

auction-server/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ pub struct DeletePgRowsOptions {
102102
#[arg(env = "DELETE_THRESHOLD_SECONDS")]
103103
#[arg(default_value = "172800")] // 2 days in seconds
104104
pub delete_threshold_secs: u64,
105+
106+
/// The buffer time to account for bids that may still exist in the db. We cannot delete auctions with ids that are still referenced by bids, so we wait an additional buffer time before deleting auctions.
107+
#[arg(long = "delete-buffer-auction-seconds")]
108+
#[arg(env = "DELETE_BUFFER_AUCTION_SECONDS")]
109+
#[arg(default_value = "3600")] // 1 hour in seconds
110+
pub delete_buffer_auction_secs: u64,
105111
}
106112

107113
#[derive(Args, Clone, Debug)]

auction-server/src/kernel/workers.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ pub async fn run_delete_pg_db_history(
122122
}
123123
});
124124
futures::future::try_join_all(futures).await?;
125+
126+
delete_pg_db_auction_history(
127+
db,
128+
delete_threshold_secs + delete_pg_rows_options.delete_buffer_auction_secs,
129+
)
130+
.await?;
125131
}
126132
}
127133
}
@@ -205,3 +211,35 @@ pub async fn delete_pg_db_opportunity_history(
205211

206212
Ok(())
207213
}
214+
215+
#[instrument(
216+
target = "metrics",
217+
name = "db_delete_pg_auction_history"
218+
fields(category = "db_queries", result = "success", name = "delete_pg_auction_history", tracing_enabled),
219+
skip_all
220+
)]
221+
pub async fn delete_pg_db_auction_history(
222+
db: &PgPool,
223+
delete_threshold_secs: u64,
224+
) -> anyhow::Result<()> {
225+
let threshold = OffsetDateTime::now_utc() - Duration::from_secs(delete_threshold_secs);
226+
let n_auctions_deleted = sqlx::query!(
227+
"WITH rows_to_delete AS (
228+
SELECT id FROM auction WHERE creation_time < $1 LIMIT $2
229+
) DELETE FROM auction WHERE id IN (SELECT id FROM rows_to_delete)",
230+
PrimitiveDateTime::new(threshold.date(), threshold.time()),
231+
DELETE_BATCH_SIZE as i64,
232+
)
233+
.execute(db)
234+
.await
235+
.map_err(|e| {
236+
tracing::Span::current().record("result", "error");
237+
tracing::error!("Failed to delete PG DB auction history: {}", e);
238+
e
239+
})?
240+
.rows_affected();
241+
242+
metrics::histogram!("db_delete_pg_auction_count").record(n_auctions_deleted as f64);
243+
244+
Ok(())
245+
}

0 commit comments

Comments
 (0)