Skip to content

Commit

Permalink
fix: mysql: Replaces the user_id%10 in batch_id with a global counter.
Browse files Browse the repository at this point in the history
Since the batch_uploads primary key already contains user_id, it does
nothing to increase time resolution. The counter may still see
conflicts with other processes, but the timing issue is mostly a
concern in E2E tests.
  • Loading branch information
tommie committed Dec 29, 2024
1 parent 41fe851 commit 36194a0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions syncstorage-mysql/src/batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use base64::Engine;
use std::collections::HashSet;
use std::{
collections::HashSet,
sync::atomic::{AtomicU32, Ordering},
};

use diesel::{
self,
Expand All @@ -21,6 +24,8 @@ use super::{

const MAXTTL: i32 = 2_100_000_000;

static COUNTER: AtomicU32 = AtomicU32::new(0);

pub fn create(db: &MysqlDb, params: params::CreateBatch) -> DbResult<results::CreateBatch> {
let user_id = params.user_id.legacy_id as i64;
let collection_id = db.get_collection_id(&params.collection)?;
Expand All @@ -36,7 +41,7 @@ pub fn create(db: &MysqlDb, params: params::CreateBatch) -> DbResult<results::Cr
// while still letting us treat these ids as millisecond timestamps. It's
// yuck, but it works and it keeps the weirdness contained to this single
// line of code.
let batch_id = db.timestamp().as_i64() + (user_id % 10);
let batch_id = db.timestamp().as_i64() + COUNTER.fetch_add(1, Ordering::Relaxed) as i64 % 10;
insert_into(batch_uploads::table)
.values((
batch_uploads::batch_id.eq(&batch_id),
Expand Down

0 comments on commit 36194a0

Please sign in to comment.