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

opt(torii-core): move off queryqueue for executing tx #2460

Merged
merged 55 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
01ce338
opt(torii-core): move off queryqueue for executing tx
Larkooo Sep 20, 2024
e0ec767
feat: replace queury queue by executor
Larkooo Sep 24, 2024
6097a60
fix: executor
Larkooo Sep 24, 2024
043f669
refactor: executor logic
Larkooo Sep 25, 2024
9d7d0e7
Merge remote-tracking branch 'upstream/main' into use-tx-executor
Larkooo Sep 25, 2024
9314438
fix: tests
Larkooo Sep 25, 2024
f9a136f
fmt
Larkooo Sep 25, 2024
b883343
executor inside of tokio select
Larkooo Sep 25, 2024
7771fdf
executor graceful exit
Larkooo Sep 25, 2024
60c9069
priv execute
Larkooo Sep 25, 2024
cd52f0f
contracts insertion shouldnt go through executor
Larkooo Sep 25, 2024
045eed0
clean code
Larkooo Sep 25, 2024
045e4ae
exec
Larkooo Sep 25, 2024
388ba1e
Merge branch 'main' into use-tx-executor
Larkooo Sep 25, 2024
b7acef5
fix: tests
Larkooo Sep 25, 2024
b94ad7a
oneshot channel for execution result
Larkooo Sep 25, 2024
c13ff59
fmt
Larkooo Sep 25, 2024
7fc27d5
clone shutdown tx
Larkooo Sep 25, 2024
260845c
fmt
Larkooo Sep 25, 2024
a7e4f1f
fix: test exec
Larkooo Sep 25, 2024
8cf4452
non bloking execute engine
Larkooo Sep 25, 2024
2bcf226
executor logs
Larkooo Sep 25, 2024
3242ac4
in memory head
Larkooo Sep 25, 2024
ef3e4ba
fmt
Larkooo Sep 25, 2024
299c0b9
fix: tests
Larkooo Sep 27, 2024
e4404f1
fixx: libp2p
Larkooo Sep 27, 2024
663234a
fmt
Larkooo Sep 27, 2024
c998428
Merge branch 'main' into use-tx-executor
Larkooo Sep 27, 2024
994abc5
try fix libp2p test
Larkooo Sep 27, 2024
65612fa
fix tests
Larkooo Sep 27, 2024
13b1ba7
fmt
Larkooo Sep 27, 2024
0c31327
use tempfile for tests
Larkooo Sep 27, 2024
afa2a0a
fix
Larkooo Sep 27, 2024
ef9fafc
c
Larkooo Sep 27, 2024
4ec379c
fix: sql tests
Larkooo Sep 27, 2024
d393896
clone
Larkooo Sep 27, 2024
1730bfc
fmt
Larkooo Sep 27, 2024
b708081
fmt
Larkooo Sep 27, 2024
7758cf9
no temp file
Larkooo Sep 27, 2024
607cd06
tmp file
Larkooo Sep 30, 2024
d48dd30
fix: lock issues
Larkooo Sep 30, 2024
6d4b99f
manuallyt use tmp file
Larkooo Sep 30, 2024
baf7f35
fix graphql tests
Larkooo Sep 30, 2024
c4f288a
fix: tests
Larkooo Sep 30, 2024
5dac220
clippy
Larkooo Sep 30, 2024
28633b4
fix torii bin
Larkooo Sep 30, 2024
fd3c377
engine executions
Larkooo Sep 30, 2024
4cabea5
use tmp file for db
Larkooo Sep 30, 2024
ee86042
fix: cursor
Larkooo Sep 30, 2024
43246b6
chore
Larkooo Sep 30, 2024
706c7fb
wip
Larkooo Oct 1, 2024
9c9e0a3
Merge branch 'main' into use-tx-executor
Larkooo Oct 1, 2024
6b6f5a6
cleaning code
Larkooo Oct 2, 2024
61f0a4b
refactor: handle errors without panic
Larkooo Oct 2, 2024
63cca75
use vec
Larkooo Oct 2, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::VecDeque;

use std::sync::Arc;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use anyhow::{Context, Result};
use dojo_types::schema::Ty;
use sqlx::{FromRow, Pool, Sqlite};
Expand Down Expand Up @@ -52,28 +53,29 @@ pub enum QueryType {
Other,
}

impl QueryQueue {
pub fn new(pool: Pool<Sqlite>) -> Self {
QueryQueue { pool, queue: VecDeque::new(), publish_queue: VecDeque::new() }
}
pub struct TxExecutor {
pool: Pool<Sqlite>,
rx: Receiver<QueryMessage>,
}

pub fn enqueue<S: Into<String>>(
&mut self,
statement: S,
arguments: Vec<Argument>,
query_type: QueryType,
) {
self.queue.push_back((statement.into(), arguments, query_type));
}
pub struct QueryMessage {
statement: String,
arguments: Vec<Argument>,
query_type: QueryType,
}

pub fn push_publish(&mut self, value: BrokerMessage) {
self.publish_queue.push_back(value);
impl TxExecutor {
pub fn new(pool: Pool<Sqlite>) -> (Self, Sender<QueryMessage>) {
let (tx, rx) = channel(100); // Adjust buffer size as needed
Larkooo marked this conversation as resolved.
Show resolved Hide resolved
(TxExecutor { pool, rx }, tx)
}

pub async fn execute_all(&mut self) -> Result<()> {
pub async fn run(&mut self) -> Result<()> {
let mut tx = self.pool.begin().await?;
Larkooo marked this conversation as resolved.
Show resolved Hide resolved
let mut publish_queue = Vec::new();

while let Some((statement, arguments, query_type)) = self.queue.pop_front() {
while let Some(msg) = self.rx.recv().await {
let QueryMessage { statement, arguments, query_type } = msg;
let mut query = sqlx::query(&statement);

for arg in &arguments {
Expand All @@ -95,7 +97,7 @@ impl QueryQueue {
entity_updated.updated_model = Some(entity);
entity_updated.deleted = false;
let broker_message = BrokerMessage::EntityUpdated(entity_updated);
self.push_publish(broker_message);
publish_queue.push(broker_message);
}
QueryType::DeleteEntity(entity) => {
let delete_model = query.execute(&mut *tx).await.with_context(|| {
Expand Down Expand Up @@ -134,7 +136,7 @@ impl QueryQueue {
}

let broker_message = BrokerMessage::EntityUpdated(entity_updated);
self.push_publish(broker_message);
publish_queue.push(broker_message);
}
QueryType::Other => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify entity deletion logic for clarity.

Ohayo, sensei! The conditionals around deleting entities when all models are deleted could be streamlined for better readability. Refactoring this logic can make the code more maintainable.

Consider this refactored snippet:

// After deleting the model, check if any models remain
let remaining_models = sqlx::query_scalar::<_, i64>(
    "SELECT count(*) FROM entity_model WHERE entity_id = ?",
)
.bind(entity_updated.id.clone())
.fetch_one(&mut *tx)
.await?;

// If no models remain, delete the entity
if remaining_models == 0 {
    sqlx::query("DELETE FROM entities WHERE id = ?")
        .bind(entity_updated.id.clone())
        .execute(&mut *tx)
        .await?;
    entity_updated.deleted = true;
}

query.execute(&mut *tx).await.with_context(|| {
Expand All @@ -146,7 +148,7 @@ impl QueryQueue {

tx.commit().await?;

while let Some(message) = self.publish_queue.pop_front() {
for message in publish_queue {
send_broker_message(message);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider asynchronous message publishing.

Ohayo, sensei! To improve performance, especially under heavy load, you might consider publishing broker messages asynchronously.

Here's how you could modify the loop:

for message in publish_queue {
    tokio::spawn(async move {
        send_broker_message(message);
    });
}


Expand Down
2 changes: 1 addition & 1 deletion crates/torii/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod engine;
pub mod error;
pub mod model;
pub mod processors;
pub mod query_queue;
pub mod executor;
pub mod simple_broker;
pub mod sql;
pub mod types;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use starknet_crypto::poseidon_hash_many;
use tracing::{debug, warn};

use crate::cache::{Model, ModelCache};
use crate::query_queue::{Argument, BrokerMessage, DeleteEntityQuery, QueryQueue, QueryType};
use crate::executor::{Argument, BrokerMessage, DeleteEntityQuery, QueryQueue, QueryType};
use crate::types::{
Event as EventEmitted, EventMessage as EventMessageUpdated, Model as ModelRegistered,
};
Expand Down
Loading