Skip to content

Commit

Permalink
feat: write cached segments to storage on log synced
Browse files Browse the repository at this point in the history
  • Loading branch information
posaggen committed Jan 22, 2024
1 parent 613c1a8 commit 0566286
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 38 deletions.
16 changes: 11 additions & 5 deletions node/chunk_pool/src/mem_pool/chunk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::time::{Duration, Instant};
/// retrieved from blockchain.
pub struct MemoryCachedFile {
pub id: FileID,
pub chunks_per_segment: usize,
/// Window to control the cache of each file
pub segments: HashMap<usize, ChunkArray>,
/// Total number of chunks for the cache file, which is updated from log entry.
Expand All @@ -22,12 +23,13 @@ pub struct MemoryCachedFile {
}

impl MemoryCachedFile {
fn new(root: DataRoot, timeout: Duration) -> Self {
fn new(root: DataRoot, timeout: Duration, chunks_per_segment: usize) -> Self {
MemoryCachedFile {
id: FileID {
root,
tx_id: Default::default(),
},
chunks_per_segment,
segments: HashMap::default(),
total_chunks: 0,
expired_at: Instant::now().add(timeout),
Expand Down Expand Up @@ -81,6 +83,7 @@ impl ChunkPoolCache {
self.files.get(root)
}

#[allow(unused)]
pub fn get_file_mut(&mut self, root: &DataRoot) -> Option<&mut MemoryCachedFile> {
self.files.get_mut(root)
}
Expand Down Expand Up @@ -126,10 +129,13 @@ impl ChunkPoolCache {
// always GC at first
self.garbage_collect();

let file = self
.files
.entry(seg_info.root)
.or_insert_with(|| MemoryCachedFile::new(seg_info.root, self.config.expiration_time()));
let file = self.files.entry(seg_info.root).or_insert_with(|| {
MemoryCachedFile::new(
seg_info.root,
self.config.expiration_time(),
seg_info.chunks_per_segment,
)
});

// Segment already cached in memory. Directly return OK
if file.segments.contains_key(&seg_info.seg_index) {
Expand Down
53 changes: 24 additions & 29 deletions node/chunk_pool/src/mem_pool/chunk_pool_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,33 +204,28 @@ impl MemoryChunkPool {

/// Updates the cached file info when log entry retrieved from blockchain.
pub async fn update_file_info(&self, tx: &Transaction) -> Result<bool> {
let mut inner = self.inner.lock().await;

// Do nothing if file not uploaded yet.
let file = match inner.segment_cache.get_file_mut(&tx.data_merkle_root) {
Some(f) => f,
None => return Ok(false),
};

// Update the file info with transaction.
file.update_with_tx(tx);

// File partially uploaded and it's up to user thread
// to write chunks into store and finalize transaction.
if file.cached_chunk_num < file.total_chunks {
return Ok(true);
}

// Otherwise, notify to write all memory cached chunks and finalize transaction.
let file_id = FileID {
root: tx.data_merkle_root,
tx_id: tx.id(),
};
if let Err(e) = self.sender.send(file_id) {
// Channel receiver will not be dropped until program exit.
bail!("channel send error: {}", e);
let maybe_file = self
.inner
.lock()
.await
.segment_cache
.remove_file(&tx.data_merkle_root);
if let Some(mut file) = maybe_file {
file.update_with_tx(tx);
for (seg_index, seg) in file.segments.into_iter() {
self.write_chunks(
SegmentInfo {
root: tx.data_merkle_root,
seg_data: seg.data.clone(),
seg_index,
chunks_per_segment: file.chunks_per_segment,
},
file.id,
file.total_chunks * CHUNK_SIZE,
)
.await?
}
}

Ok(true)
}

Expand All @@ -242,10 +237,10 @@ impl MemoryChunkPool {
Ok(LogSyncEvent::ReorgDetected { .. }) => {}
Ok(LogSyncEvent::Reverted { .. }) => {}
Ok(LogSyncEvent::TxSynced { tx }) => {
if let Err(_e) = chunk_pool.update_file_info(&tx).await {
if let Err(e) = chunk_pool.update_file_info(&tx).await {
error!(
"Failed to update file info. tx seq={}, tx_root={}",
tx.seq, tx.data_merkle_root
"Failed to update file info. tx seq={}, tx_root={}, error={}",
tx.seq, tx.data_merkle_root, e
);
}
}
Expand Down
1 change: 1 addition & 0 deletions node/chunk_pool/src/mem_pool/chunk_write_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum SlotStatus {
/// limit on writing threads per file. Meanwhile, the left_boundary field records
/// how many segments have been uploaded.
struct CtrlWindow {
#[allow(unused)]
size: usize,
left_boundary: usize,
slots: HashMap<usize, SlotStatus>,
Expand Down
2 changes: 1 addition & 1 deletion node/router/src/libp2p_event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl Libp2pEventHandler {
}

// TODO(qhz): check if there is better way to check existence of requested chunks.
let _ = match self
match self
.store
.get_chunks_by_tx_and_index_range(
msg.tx_id.seq,
Expand Down
2 changes: 1 addition & 1 deletion node/sync/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl SyncService {
let store = Store::new(store, executor.clone());

let manager =
AutoSyncManager::new(store.clone(), sync_send.clone(), config.clone()).await?;
AutoSyncManager::new(store.clone(), sync_send.clone(), config).await?;
if config.auto_sync_enabled {
manager.spwn(&executor, event_recv);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run_test(self):
self.contract.submit(submissions)
wait_until(lambda: self.contract.num_submissions() == 1)
wait_until(lambda: client.zgs_get_file_info(data_root) is not None)
wait_until(lambda: client.zgs_get_file_info(data_root)["isCached"])
wait_until(lambda: not client.zgs_get_file_info(data_root)["isCached"] and client.zgs_get_file_info(data_root)["uploadedSegNum"] == 1)
client.zgs_upload_segment(segments[1])
wait_until(lambda: client.zgs_get_file_info(data_root)["finalized"])

Expand Down
1 change: 1 addition & 0 deletions tests/crash_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def run_test(self):
self.log.info("segment: %s", segment)

for i in range(self.num_nodes):
self.nodes[i].admin_start_sync_file(0)
self.log.info("wait for node: %s", i)
wait_until(
lambda: self.nodes[i].zgs_get_file_info(data_root) is not None
Expand Down
2 changes: 1 addition & 1 deletion tests/random_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setup_params(self):
self.num_blockchain_nodes = 1
self.num_nodes = 4
for i in range(self.num_nodes):
self.zgs_node_configs[i] = {"find_peer_timeout_secs": 1, "confirmation_block_count": 1}
self.zgs_node_configs[i] = {"find_peer_timeout_secs": 1, "confirmation_block_count": 1, "sync": {"auto_sync_enabled": True}}

def run_test(self):
max_size = 256 * 1024 * 64
Expand Down

0 comments on commit 0566286

Please sign in to comment.