Skip to content
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

fix(filemanager): sequencer ordering #396

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/workload/stateless/stacks/filemanager/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Create an index for ordering `s3_object`s in ascending order.
-- TODO this should be created `concurrently` when running migrations without transactions is released:
-- https://github.com/launchbadge/sqlx/issues/767
create index sequencer_index on s3_object (sequencer);
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ aws_lambda_events = "0.15"

[dev-dependencies]
lazy_static = "1"
rand = "0.8"

aws-smithy-runtime-api = "1"
aws-smithy-mocks-experimental = "0.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Query builder involving list operations on the database.
//!

use sea_orm::{EntityTrait, FromQueryResult, PaginatorTrait, QuerySelect, Select};
use sea_orm::{EntityTrait, FromQueryResult, PaginatorTrait, QueryOrder, QuerySelect, Select};

use crate::database::entities::object::Entity as ObjectEntity;
use crate::database::entities::s3_object::Column as S3Column;
use crate::database::entities::s3_object::Entity as S3ObjectEntity;
use crate::database::Client;
use crate::error::Error::OverflowError;
Expand Down Expand Up @@ -46,7 +47,7 @@ impl<'a> ListQueryBuilder<'a, S3ObjectEntity> {

/// Build a select query for finding values from s3 objects.
pub fn build_object() -> Select<S3ObjectEntity> {
S3ObjectEntity::find()
S3ObjectEntity::find().order_by_asc(S3Column::Sequencer)
}
}

Expand Down Expand Up @@ -117,7 +118,7 @@ mod tests {

use crate::database::aws::migration::tests::MIGRATOR;
use crate::database::Client;
use crate::queries::tests::initialize_database;
use crate::queries::tests::{initialize_database, initialize_database_reorder};

use super::*;

Expand Down Expand Up @@ -175,7 +176,7 @@ mod tests {
#[sqlx::test(migrator = "MIGRATOR")]
async fn test_list_s3_objects(pool: PgPool) {
let client = Client::from_pool(pool);
let entries = initialize_database(&client, 10).await;
let entries = initialize_database_reorder(&client, 10).await;

let builder = ListQueryBuilder::<S3ObjectEntity>::new(&client);
let result = builder.all().await.unwrap();
Expand All @@ -192,7 +193,7 @@ mod tests {
#[sqlx::test(migrator = "MIGRATOR")]
async fn test_paginate_s3_objects(pool: PgPool) {
let client = Client::from_pool(pool);
let entries = initialize_database(&client, 10).await;
let entries = initialize_database_reorder(&client, 10).await;

let builder = ListQueryBuilder::<S3ObjectEntity>::new(&client);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub(crate) mod tests {
use std::ops::Add;

use chrono::{DateTime, Days};
use rand::seq::SliceRandom;
use rand::thread_rng;
use sea_orm::Set;
use sea_orm::{ActiveModelTrait, TryIntoModel};
use strum::EnumCount;
Expand All @@ -21,13 +23,39 @@ pub(crate) mod tests {
use crate::database::Client;
use crate::uuid::UuidGenerator;

/// Initialize the database state for testing and shuffle entries to simulate
/// out of order events.
pub(crate) async fn initialize_database_reorder(
client: &Client,
n: usize,
) -> Vec<(Object, S3Object)> {
let mut data = initialize_database_with_shuffle(client, n, true).await;

// Return the correct ordering for test purposes
data.sort_by(|(_, a), (_, b)| a.sequencer.cmp(&b.sequencer));

data
}

/// Initialize database state for testing.
pub(crate) async fn initialize_database(client: &Client, n: usize) -> Vec<(Object, S3Object)> {
initialize_database_with_shuffle(client, n, false).await
}

async fn initialize_database_with_shuffle(
client: &Client,
n: usize,
shuffle: bool,
) -> Vec<(Object, S3Object)> {
let mut output = vec![];

for index in 0..n {
let (object, s3_object) = generate_entry(index);
let mut entries: Vec<_> = (0..n).map(generate_entry).collect();

if shuffle {
entries.shuffle(&mut thread_rng());
}

for (object, s3_object) in entries {
object
.clone()
.insert(client.connection_ref())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ mod tests {
use crate::database::aws::migration::tests::MIGRATOR;
use crate::database::entities::object::Model as Object;
use crate::database::entities::s3_object::Model as S3Object;
use crate::queries::tests::initialize_database;
use crate::queries::tests::{initialize_database, initialize_database_reorder};
use crate::routes::list::{ListCount, ListResponse};
use crate::routes::{api_router, AppState};

Expand Down Expand Up @@ -331,7 +331,7 @@ mod tests {
#[sqlx::test(migrator = "MIGRATOR")]
async fn list_s3_objects_api(pool: PgPool) {
let state = AppState::from_pool(pool);
let entries = initialize_database(state.client(), 10).await;
let entries = initialize_database_reorder(state.client(), 10).await;

let app = api_router(state);
let response = app
Expand Down Expand Up @@ -365,7 +365,7 @@ mod tests {
#[sqlx::test(migrator = "MIGRATOR")]
async fn list_s3_objects_api_paginate(pool: PgPool) {
let state = AppState::from_pool(pool);
let entries = initialize_database(state.client(), 10).await;
let entries = initialize_database_reorder(state.client(), 10).await;

let app = api_router(state);
let response = app
Expand Down Expand Up @@ -399,7 +399,7 @@ mod tests {
#[sqlx::test(migrator = "MIGRATOR")]
async fn list_s3_objects_api_zero_page_size(pool: PgPool) {
let state = AppState::from_pool(pool);
let entries = initialize_database(state.client(), 10).await;
let entries = initialize_database_reorder(state.client(), 10).await;

let app = api_router(state);
let response = app
Expand Down