cnidarium/delta.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
use std::{any::Any, sync::Arc};
use futures::StreamExt;
use parking_lot::RwLock;
use tendermint::abci;
use crate::{
future::{
CacheFuture, StateDeltaNonconsensusPrefixRawStream, StateDeltaNonconsensusRangeRawStream,
StateDeltaPrefixKeysStream, StateDeltaPrefixRawStream,
},
utils, Cache, EscapedByteSlice, StateRead, StateWrite,
};
/// An arbitrarily-deeply nested stack of delta updates to an underlying state.
///
/// This API allows exploring a tree of possible execution paths concurrently,
/// before finally selecting one and applying it to the underlying state.
///
/// Using this API requires understanding its invariants.
///
/// On creation, `StateDelta::new` takes ownership of a `StateRead + StateWrite`
/// instance, acquiring a "write lock" over the underlying state (since `&mut S`
/// is `StateWrite` if `S: StateWrite`, it's possible to pass a unique
/// reference).
///
/// The resulting `StateDelta` instance is a "leaf" state, and can be used for
/// reads and writes, following the some execution path.
///
/// When two potential execution paths diverge, `delta.fork()` can be used to
/// fork the state update. The new forked `StateDelta` will include all
/// previous state writes made to the original (and its ancestors). Any writes
/// made to the original `StateDelta` after `fork()` is called will not be seen
/// by the forked state.
///
/// Finally, after some execution path has been selected, calling
/// `delta.apply()` on one of the possible state updates will commit the changes
/// to the underlying state instance, and invalidate all other delta updates in
/// the same family. It is a programming error to use the other delta updates
/// after `apply()` has been called, but ideally this should not be a problem in
/// practice: the API is intended to explore a tree of possible execution paths;
/// once one has been selected, the others should be discarded.
#[derive(Debug)]
pub struct StateDelta<S: StateRead> {
/// The underlying state instance.
///
/// The Arc<_> allows it to be shared between different stacks of delta updates,
/// and the RwLock<Option<_>> allows it to be taken out when it's time to commit
/// the changes from one of the stacks.
state: Arc<RwLock<Option<S>>>,
/// A stack of intermediate delta updates, with the "top" layers first.
///
/// We store all the layers directly, rather than using a recursive structure,
/// so that the type doesn't depend on how many layers are involved. We're only
/// duplicating the Arc<_>, so this should be cheap.
layers: Vec<Arc<RwLock<Option<Cache>>>>,
/// The final delta update in the stack, the one we're currently working on.
/// Storing this separately allows us to avoid lock contention during writes.
/// In fact, this data shouldn't usually be shared at all; the only reason it's
/// wrapped this way is so that prefix streams can have 'static lifetimes.
/// We option-wrap it so it can be chained with the layers; it will never be None.
leaf_cache: Arc<RwLock<Option<Cache>>>,
}
impl<S: StateRead> StateDelta<S> {
/// Create a new tree of possible updates to an underlying `state`.
pub fn new(state: S) -> Self {
Self {
state: Arc::new(RwLock::new(Some(state))),
layers: Vec::default(),
leaf_cache: Arc::new(RwLock::new(Some(Cache::default()))),
}
}
/// Fork execution, returning a new child state that includes all previous changes.
pub fn fork(&mut self) -> Self {
// If we have writes in the leaf cache, we'll move them to a new layer,
// ensuring that the new child only sees writes made to this state
// *before* fork was called, and not after.
//
// Doing this only when the leaf cache is dirty means that we don't
// add empty layers in repeated fork() calls without intervening writes.
if self
.leaf_cache
.read()
.as_ref()
.expect("unable to get ref to leaf cache, storage not initialized?")
.is_dirty()
{
let new_layer = std::mem::replace(
&mut self.leaf_cache,
Arc::new(RwLock::new(Some(Cache::default()))),
);
self.layers.push(new_layer);
}
Self {
state: self.state.clone(),
layers: self.layers.clone(),
leaf_cache: Arc::new(RwLock::new(Some(Cache::default()))),
}
}
/// Flatten all changes in this branch of the tree into a single [`Cache`],
/// invalidating all other branches of the tree and releasing the underlying
/// state back to the caller.
///
/// The [`apply`](Self::apply) method is a convenience wrapper around this
/// that applies the changes to the underlying state.
pub fn flatten(self) -> (S, Cache) {
tracing::trace!("flattening branch");
// Take ownership of the underlying state, immediately invalidating all
// other delta stacks in the same family.
let state = self
.state
.write()
.take()
.expect("apply must be called only once");
// Flatten the intermediate layers into a single cache, applying them from oldest
// (bottom) to newest (top), so that newer writes clobber old ones.
let mut changes = Cache::default();
for layer in self.layers {
let cache = layer
.write()
.take()
.expect("cache must not have already been applied");
changes.merge(cache);
}
// Last, apply the changes in the leaf cache.
changes.merge(
self.leaf_cache
.write()
.take()
.expect("unable to take leaf cache, was it already applied?"),
);
(state, changes)
}
}
impl<S: StateRead + StateWrite> StateDelta<S> {
/// Apply all changes in this branch of the tree to the underlying state,
/// releasing it back to the caller and invalidating all other branches of
/// the tree.
pub fn apply(self) -> (S, Vec<abci::Event>) {
let (mut state, mut changes) = self.flatten();
let events = changes.take_events();
// Apply the flattened changes to the underlying state.
changes.apply_to(&mut state);
// Finally, return ownership of the state back to the caller.
(state, events)
}
}
impl<S: StateRead + StateWrite> StateDelta<Arc<S>> {
pub fn try_apply(self) -> anyhow::Result<(S, Vec<abci::Event>)> {
let (arc_state, mut changes) = self.flatten();
let events = std::mem::take(&mut changes.events);
if let Ok(mut state) = Arc::try_unwrap(arc_state) {
// Apply the flattened changes to the underlying state.
changes.apply_to(&mut state);
// Finally, return ownership of the state back to the caller.
Ok((state, events))
} else {
Err(anyhow::anyhow!("did not have unique ownership of Arc<S>"))
}
}
}
impl<S: StateRead> StateRead for StateDelta<S> {
type GetRawFut = CacheFuture<S::GetRawFut>;
type PrefixRawStream = StateDeltaPrefixRawStream<S::PrefixRawStream>;
type PrefixKeysStream = StateDeltaPrefixKeysStream<S::PrefixKeysStream>;
type NonconsensusPrefixRawStream =
StateDeltaNonconsensusPrefixRawStream<S::NonconsensusPrefixRawStream>;
type NonconsensusRangeRawStream =
StateDeltaNonconsensusRangeRawStream<S::NonconsensusRangeRawStream>;
fn get_raw(&self, key: &str) -> Self::GetRawFut {
// Check if we have a cache hit in the leaf cache.
if let Some(entry) = self
.leaf_cache
.read()
.as_ref()
.expect("delta must not have been applied")
.unwritten_changes
.get(key)
{
return CacheFuture::hit(entry.clone());
}
// Iterate through the stack, top to bottom, to see if we have a cache hit.
for layer in self.layers.iter().rev() {
if let Some(entry) = layer
.read()
.as_ref()
.expect("delta must not have been applied")
.unwritten_changes
.get(key)
{
return CacheFuture::hit(entry.clone());
}
}
// If we got here, the key must be in the underlying state or not present at all.
CacheFuture::miss(
self.state
.read()
.as_ref()
.expect("delta must not have been applied")
.get_raw(key),
)
}
fn nonverifiable_get_raw(&self, key: &[u8]) -> Self::GetRawFut {
// Check if we have a cache hit in the leaf cache.
if let Some(entry) = self
.leaf_cache
.read()
.as_ref()
.expect("delta must not have been applied")
.nonverifiable_changes
.get(key)
{
return CacheFuture::hit(entry.clone());
}
// Iterate through the stack, top to bottom, to see if we have a cache hit.
for layer in self.layers.iter().rev() {
if let Some(entry) = layer
.read()
.as_ref()
.expect("delta must not have been applied")
.nonverifiable_changes
.get(key)
{
return CacheFuture::hit(entry.clone());
}
}
// If we got here, the key must be in the underlying state or not present at all.
CacheFuture::miss(
self.state
.read()
.as_ref()
.expect("delta must not have been applied")
.nonverifiable_get_raw(key),
)
}
fn object_type(&self, key: &'static str) -> Option<std::any::TypeId> {
// Check if we have a cache hit in the leaf cache.
if let Some(entry) = self
.leaf_cache
.read()
.as_ref()
.expect("delta must not have been applied")
.ephemeral_objects
.get(key)
{
// We have to explicitly call `Any::type_id(&**v)` here because this ensures that we are
// asking for the type of the `Any` *inside* the `Box<dyn Any>`, rather than the type of
// `Box<dyn Any>` itself.
return entry.as_ref().map(|v| std::any::Any::type_id(&**v));
}
// Iterate through the stack, top to bottom, to see if we have a cache hit.
for layer in self.layers.iter().rev() {
if let Some(entry) = layer
.read()
.as_ref()
.expect("delta must not have been applied")
.ephemeral_objects
.get(key)
{
// We have to explicitly call `Any::type_id(&**v)` here because this ensures that we are
// asking for the type of the `Any` *inside* the `Box<dyn Any>`, rather than the type of
// `Box<dyn Any>` itself.
return entry.as_ref().map(|v| std::any::Any::type_id(&**v));
}
}
// Fall through to the underlying store.
self.state
.read()
.as_ref()
.expect("delta must not have been applied")
.object_type(key)
}
fn object_get<T: std::any::Any + Send + Sync + Clone>(&self, key: &'static str) -> Option<T> {
// Check if we have a cache hit in the leaf cache.
if let Some(entry) = self
.leaf_cache
.read()
.as_ref()
.expect("delta must not have been applied")
.ephemeral_objects
.get(key)
{
return entry
.as_ref()
.map(|v| {
v.downcast_ref().unwrap_or_else(|| panic!("unexpected type for key \"{key}\" in `StateDelta::object_get`: expected type {}", std::any::type_name::<T>()))
})
.cloned();
}
// Iterate through the stack, top to bottom, to see if we have a cache hit.
for layer in self.layers.iter().rev() {
if let Some(entry) = layer
.read()
.as_ref()
.expect("delta must not have been applied")
.ephemeral_objects
.get(key)
{
return entry
.as_ref()
.map(|v| {
v.downcast_ref().unwrap_or_else(|| panic!("unexpected type for key \"{key}\" in `StateDelta::object_get`: expected type {}", std::any::type_name::<T>()))
}).cloned();
}
}
// Fall through to the underlying store.
self.state
.read()
.as_ref()
.expect("delta must not have been applied")
.object_get(key)
}
fn prefix_raw(&self, prefix: &str) -> Self::PrefixRawStream {
let underlying = self
.state
.read()
.as_ref()
.expect("delta must not have been applied")
.prefix_raw(prefix)
.peekable();
StateDeltaPrefixRawStream {
underlying,
layers: self.layers.clone(),
leaf_cache: self.leaf_cache.clone(),
last_key: None,
prefix: prefix.to_owned(),
}
}
fn prefix_keys(&self, prefix: &str) -> Self::PrefixKeysStream {
let underlying = self
.state
.read()
.as_ref()
.expect("delta must not have been applied")
.prefix_keys(prefix)
.peekable();
StateDeltaPrefixKeysStream {
underlying,
layers: self.layers.clone(),
leaf_cache: self.leaf_cache.clone(),
last_key: None,
prefix: prefix.to_owned(),
}
}
fn nonverifiable_prefix_raw(&self, prefix: &[u8]) -> Self::NonconsensusPrefixRawStream {
let underlying = self
.state
.read()
.as_ref()
.expect("delta must not have been applied")
.nonverifiable_prefix_raw(prefix)
.peekable();
StateDeltaNonconsensusPrefixRawStream {
underlying,
layers: self.layers.clone(),
leaf_cache: self.leaf_cache.clone(),
last_key: None,
prefix: prefix.to_vec(),
}
}
fn nonverifiable_range_raw(
&self,
prefix: Option<&[u8]>,
range: impl std::ops::RangeBounds<Vec<u8>>,
) -> anyhow::Result<Self::NonconsensusRangeRawStream> {
let (range, (start, end)) = utils::convert_bounds(range)?;
let underlying = self
.state
.read()
.as_ref()
.expect("delta must not have been applied")
.nonverifiable_range_raw(prefix, range)?
.peekable();
Ok(StateDeltaNonconsensusRangeRawStream {
underlying,
layers: self.layers.clone(),
leaf_cache: self.leaf_cache.clone(),
last_key: None,
prefix: prefix.map(|p| p.to_vec()),
range: (start, end),
})
}
}
impl<S: StateRead> StateWrite for StateDelta<S> {
fn put_raw(&mut self, key: String, value: jmt::OwnedValue) {
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.unwritten_changes
.insert(key, Some(value));
}
fn delete(&mut self, key: String) {
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.unwritten_changes
.insert(key, None);
}
fn nonverifiable_delete(&mut self, key: Vec<u8>) {
tracing::trace!(key = ?EscapedByteSlice(&key), "deleting key");
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.nonverifiable_changes
.insert(key, None);
}
fn nonverifiable_put_raw(&mut self, key: Vec<u8>, value: Vec<u8>) {
tracing::trace!(key = ?EscapedByteSlice(&key), value = ?EscapedByteSlice(&value), "insert nonverifiable change");
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.nonverifiable_changes
.insert(key, Some(value));
}
fn object_put<T: Clone + Any + Send + Sync>(&mut self, key: &'static str, value: T) {
if let Some(previous_type) = self.object_type(key) {
if std::any::TypeId::of::<T>() != previous_type {
panic!(
"unexpected type for key \"{key}\" in `StateDelta::object_put`: expected type {expected}",
expected = std::any::type_name::<T>(),
);
}
}
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.ephemeral_objects
.insert(key, Some(Box::new(value)));
}
fn object_delete(&mut self, key: &'static str) {
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.ephemeral_objects
.insert(key, None);
}
fn object_merge(
&mut self,
objects: std::collections::BTreeMap<&'static str, Option<Box<dyn Any + Send + Sync>>>,
) {
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.ephemeral_objects
.extend(objects);
}
fn record(&mut self, event: abci::Event) {
self.leaf_cache
.write()
.as_mut()
.expect("delta must not have been applied")
.events
.push(event)
}
}
/// Extension trait providing `try_begin_transaction()` on `Arc<StateDelta<S>>`.
pub trait ArcStateDeltaExt: Sized {
type S: StateRead;
/// Attempts to begin a transaction on this `Arc<State>`, returning `None` if the `Arc` is shared.
fn try_begin_transaction(&'_ mut self) -> Option<StateDelta<&'_ mut StateDelta<Self::S>>>;
}
impl<S: StateRead> ArcStateDeltaExt for Arc<StateDelta<S>> {
type S = S;
fn try_begin_transaction(&'_ mut self) -> Option<StateDelta<&'_ mut StateDelta<S>>> {
Arc::get_mut(self).map(StateDelta::new)
}
}