Skip to content

Commit

Permalink
WIP: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 4, 2024
1 parent a96143d commit 92aff18
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ impl TurboTasksBackend {

impl Backend for TurboTasksBackend {
fn startup(&self, turbo_tasks: &dyn TurboTasksBackendApi<Self>) {
self.backing_storage.startup();

// Continue all uncompleted operations
// They can't be interrupted by a snapshot since the snapshotting job has not been scheduled
// yet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
};

pub trait BackingStorage {
fn startup(&self);
fn next_free_task_id(&self) -> TaskId;
fn uncompleted_operations(&self) -> Vec<AnyOperation>;
fn save_snapshot(
Expand Down
26 changes: 25 additions & 1 deletion turbopack/crates/turbo-tasks-backend/src/lmdb_backing_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::{

use anyhow::{anyhow, Context, Result};
use bincode::Options;
use lmdb::{Database, DatabaseFlags, Environment, EnvironmentFlags, Transaction, WriteFlags};
use lmdb::{
Cursor, Database, DatabaseFlags, Environment, EnvironmentFlags, Transaction, WriteFlags,
};
use turbo_tasks::{backend::CachedTaskType, KeyValuePair, TaskId};

use crate::{
Expand Down Expand Up @@ -81,9 +83,29 @@ impl LmdbBackingStorage {
restored_cache_entries: AtomicUsize::new(0),
})
}

fn display_db(&self) -> Result<String> {
use std::fmt::Write;
let mut result = String::new();
let tx = self.env.begin_ro_txn()?;
let mut cursor = tx.open_ro_cursor(self.data_db)?;
for (key, value) in cursor.iter() {
let task_id = u32::from_be_bytes(key.try_into()?);
let data: Vec<CachedDataItem> = bincode::deserialize(value)?;
write!(result, "### Task {task_id}\n{data:#?}\n\n")?;
}
Ok(result)
}
}

impl BackingStorage for LmdbBackingStorage {
fn startup(&self) {
println!(
"Database content:\n{}",
self.display_db().unwrap_or_default()
);
}

fn next_free_task_id(&self) -> TaskId {
fn get(this: &LmdbBackingStorage) -> Result<u32> {
let tx = this.env.begin_rw_txn()?;
Expand Down Expand Up @@ -219,6 +241,7 @@ impl BackingStorage for LmdbBackingStorage {
.into_iter()
.map(|(key, value)| CachedDataItem::from_key_and_value(key, value))
.collect();
println!("Store {task_id}: {vec:?}");
let value = match bincode::serialize(&vec) {
// Ok(value) => value,
Ok(_) | Err(_) => {
Expand Down Expand Up @@ -339,6 +362,7 @@ impl BackingStorage for LmdbBackingStorage {
.inspect_err(|err| println!("Looking up data for {task_id} failed: {err:?}"))
.unwrap_or_default();
if !result.is_empty() {
println!("restored {task_id}");
self.restored_tasks
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
event_background: Event::new(|| "TurboTasks::event_background".to_string()),
program_start: Instant::now(),
});
this.backend.startup(&*this);
TURBO_TASKS.sync_scope(this.pin(), || this.backend.startup(&*this));
this
}

Expand Down

0 comments on commit 92aff18

Please sign in to comment.