-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from garious/add-accounting-stage
TPU cleanup
- Loading branch information
Showing
12 changed files
with
592 additions
and
470 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! The `entry_writer` module helps implement the TPU's write stage. | ||
use entry::Entry; | ||
use event_processor::EventProcessor; | ||
use ledger; | ||
use packet; | ||
use request_stage::RequestProcessor; | ||
use result::Result; | ||
use serde_json; | ||
use std::collections::VecDeque; | ||
use std::io::Write; | ||
use std::io::sink; | ||
use std::sync::mpsc::Receiver; | ||
use std::sync::{Arc, Mutex}; | ||
use std::time::Duration; | ||
use streamer; | ||
|
||
pub struct EntryWriter<'a> { | ||
event_processor: &'a EventProcessor, | ||
request_processor: &'a RequestProcessor, | ||
} | ||
|
||
impl<'a> EntryWriter<'a> { | ||
/// Create a new Tpu that wraps the given Accountant. | ||
pub fn new( | ||
event_processor: &'a EventProcessor, | ||
request_processor: &'a RequestProcessor, | ||
) -> Self { | ||
EntryWriter { | ||
event_processor, | ||
request_processor, | ||
} | ||
} | ||
|
||
fn write_entry<W: Write>(&self, writer: &Mutex<W>, entry: &Entry) { | ||
trace!("write_entry entry"); | ||
self.event_processor.accountant.register_entry_id(&entry.id); | ||
writeln!( | ||
writer.lock().expect("'writer' lock in fn fn write_entry"), | ||
"{}", | ||
serde_json::to_string(&entry).expect("'entry' to_strong in fn write_entry") | ||
).expect("writeln! in fn write_entry"); | ||
self.request_processor.notify_entry_info_subscribers(&entry); | ||
} | ||
|
||
fn write_entries<W: Write>( | ||
&self, | ||
writer: &Mutex<W>, | ||
entry_receiver: &Receiver<Entry>, | ||
) -> Result<Vec<Entry>> { | ||
//TODO implement a serialize for channel that does this without allocations | ||
let mut l = vec![]; | ||
let entry = entry_receiver.recv_timeout(Duration::new(1, 0))?; | ||
self.write_entry(writer, &entry); | ||
l.push(entry); | ||
while let Ok(entry) = entry_receiver.try_recv() { | ||
self.write_entry(writer, &entry); | ||
l.push(entry); | ||
} | ||
Ok(l) | ||
} | ||
|
||
/// Process any Entry items that have been published by the Historian. | ||
/// continuosly broadcast blobs of entries out | ||
pub fn write_and_send_entries<W: Write>( | ||
&self, | ||
broadcast: &streamer::BlobSender, | ||
blob_recycler: &packet::BlobRecycler, | ||
writer: &Mutex<W>, | ||
entry_receiver: &Receiver<Entry>, | ||
) -> Result<()> { | ||
let mut q = VecDeque::new(); | ||
let list = self.write_entries(writer, entry_receiver)?; | ||
trace!("New blobs? {}", list.len()); | ||
ledger::process_entry_list_into_blobs(&list, blob_recycler, &mut q); | ||
if !q.is_empty() { | ||
broadcast.send(q)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Process any Entry items that have been published by the Historian. | ||
/// continuosly broadcast blobs of entries out | ||
pub fn drain_entries(&self, entry_receiver: &Receiver<Entry>) -> Result<()> { | ||
self.write_entries(&Arc::new(Mutex::new(sink())), entry_receiver)?; | ||
Ok(()) | ||
} | ||
} |
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
Oops, something went wrong.