This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(concurrency): add execute method to the thread logic (#1882)
- Loading branch information
Showing
8 changed files
with
91 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,10 @@ | ||
use std::fmt::Debug; | ||
use std::sync::{Mutex, MutexGuard}; | ||
|
||
use crate::concurrency::TxIndex; | ||
|
||
pub fn lock_mutex_in_array<T: Debug>(array: &[Mutex<T>], tx_index: TxIndex) -> MutexGuard<'_, T> { | ||
array[tx_index].lock().unwrap_or_else(|error| { | ||
panic!("Cell of transaction index {} is poisoned. Data: {:?}.", tx_index, *error.get_ref()) | ||
}) | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -1,58 +1,94 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
use std::sync::{Arc, Mutex}; | ||
use std::fmt::Debug; | ||
use std::sync::Mutex; | ||
|
||
use starknet_api::core::ClassHash; | ||
|
||
use crate::concurrency::scheduler::{Scheduler, Task}; | ||
use crate::concurrency::utils::lock_mutex_in_array; | ||
use crate::concurrency::versioned_state_proxy::ThreadSafeVersionedState; | ||
use crate::concurrency::TxIndex; | ||
use crate::context::BlockContext; | ||
use crate::state::cached_state::StateMaps; | ||
use crate::state::cached_state::{CachedState, StateMaps}; | ||
use crate::state::state_api::StateReader; | ||
use crate::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult}; | ||
use crate::transaction::transaction_execution::Transaction; | ||
use crate::transaction::transactions::ExecutableTransaction; | ||
|
||
pub struct ConcurrentExecutionContext<S: StateReader> { | ||
pub scheduler: Scheduler, | ||
pub state: ThreadSafeVersionedState<S>, | ||
pub chunk: Box<[Transaction]>, | ||
pub execution_outputs: Box<[Mutex<ExecutionTaskOutput>]>, | ||
pub block_context: BlockContext, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ExecutionTaskOutput { | ||
pub reads: StateMaps, | ||
pub writes: StateMaps, | ||
pub visited_pcs: HashMap<ClassHash, HashSet<usize>>, | ||
pub result: TransactionExecutionResult<TransactionExecutionInfo>, | ||
} | ||
|
||
// TODO(Noa, 15/05/2024): Re-consider the necessity of the Arc (as opposed to a reference), given | ||
// concurrent code. | ||
pub fn run<S: StateReader>(execution_context: Arc<ConcurrentExecutionContext<S>>) { | ||
let scheduler = &execution_context.scheduler; | ||
let mut task = Task::NoTask; | ||
loop { | ||
task = match task { | ||
Task::ExecutionTask(tx_index) => { | ||
execute(&execution_context, tx_index); | ||
Task::NoTask | ||
} | ||
Task::ValidationTask(tx_index) => validate(&execution_context, tx_index), | ||
Task::NoTask => scheduler.next_task(), | ||
Task::Done => break, | ||
}; | ||
} | ||
pub struct WorkerExecutor<S: StateReader> { | ||
pub scheduler: Scheduler, | ||
pub state: ThreadSafeVersionedState<S>, | ||
pub chunk: Box<[Transaction]>, | ||
pub execution_outputs: Box<[Mutex<Option<ExecutionTaskOutput>>]>, | ||
pub block_context: BlockContext, | ||
} | ||
impl<S: StateReader> WorkerExecutor<S> { | ||
pub fn run(&self) { | ||
let mut task = Task::NoTask; | ||
loop { | ||
task = match task { | ||
Task::ExecutionTask(tx_index) => { | ||
self.execute(tx_index); | ||
Task::NoTask | ||
} | ||
Task::ValidationTask(tx_index) => self.validate(tx_index), | ||
Task::NoTask => self.scheduler.next_task(), | ||
Task::Done => break, | ||
}; | ||
} | ||
} | ||
|
||
fn execute<S: StateReader>(_execution_context: &ConcurrentExecutionContext<S>, _tx_index: TxIndex) { | ||
// TODO(Noa, 15/05/2024): share code with `try_commit`. | ||
todo!(); | ||
} | ||
fn execute(&self, tx_index: TxIndex) { | ||
self.execute_tx(tx_index); | ||
self.scheduler.finish_execution(tx_index) | ||
} | ||
|
||
fn execute_tx(&self, tx_index: TxIndex) { | ||
let tx_versioned_state = self.state.pin_version(tx_index); | ||
let tx = &self.chunk[tx_index]; | ||
// TODO(Noa, 15/05/2024): remove the redundant cached state. | ||
let mut tx_state = CachedState::new(tx_versioned_state); | ||
let mut transactional_state = CachedState::create_transactional(&mut tx_state); | ||
let validate = true; | ||
let charge_fee = true; | ||
|
||
let execution_result = | ||
tx.execute_raw(&mut transactional_state, &self.block_context, charge_fee, validate); | ||
|
||
fn validate<S: StateReader>( | ||
_execution_context: &ConcurrentExecutionContext<S>, | ||
_tx_index: TxIndex, | ||
) -> Task { | ||
todo!(); | ||
if execution_result.is_ok() { | ||
let class_hash_to_class = transactional_state.class_hash_to_class.borrow(); | ||
// TODO(Noa, 15/05/2024): use `tx_versioned_state` when we add support to transactional | ||
// versioned state. | ||
self.state | ||
.pin_version(tx_index) | ||
.apply_writes(&transactional_state.cache.borrow().writes, &class_hash_to_class); | ||
} | ||
|
||
// Write the transaction execution outputs. | ||
let tx_reads_writes = transactional_state.cache.take(); | ||
// In case of a failed transaction, we don't record its writes and visited pcs. | ||
let (writes, visited_pcs) = match execution_result { | ||
Ok(_) => (tx_reads_writes.writes, transactional_state.visited_pcs), | ||
Err(_) => (StateMaps::default(), HashMap::default()), | ||
}; | ||
let mut execution_output = lock_mutex_in_array(&self.execution_outputs, tx_index); | ||
*execution_output = Some(ExecutionTaskOutput { | ||
reads: tx_reads_writes.initial_reads, | ||
writes, | ||
visited_pcs, | ||
result: execution_result, | ||
}); | ||
} | ||
|
||
fn validate(&self, _tx_index: TxIndex) -> Task { | ||
todo!(); | ||
} | ||
} |