Skip to content

Commit

Permalink
util package (openzfs#480)
Browse files Browse the repository at this point in the history
Move all of our utility code to a new package, which can be used by both
zettacache and zettaobject.  The utility package has code that is not
directly related to the ZFS logic, e.g. extensions on the standard
library or other external crates.
  • Loading branch information
ahrens authored Oct 4, 2021
1 parent 70be5eb commit 89e2853
Show file tree
Hide file tree
Showing 32 changed files with 128 additions and 69 deletions.
23 changes: 19 additions & 4 deletions cmd/zfs_object_agent/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cmd/zfs_object_agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ incremental = true
[workspace]
members = [
"client",
"object_perf",
"server",
"util",
"zcdb",
"zettacache",
"zettaobject",
"object_perf",
"zoa",
]
2 changes: 1 addition & 1 deletion cmd/zfs_object_agent/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lib_LTLIBRARIES = libzoa.la

sbin_PROGRAMS = zcachedb zfs_object_agent zfs_object_perf zoa_test

common_sources = $(wildcard zettacache/src/*.rs zettacache/Cargo.toml zettaobject/src/*.rs zettaobject/Cargo.toml Cargo.lock Cargo.toml)
common_sources = $(wildcard util/src/*.rs util/Cargo.toml zettacache/src/*.rs zettacache/Cargo.toml zettaobject/src/*.rs zettaobject/Cargo.toml Cargo.lock Cargo.toml)

zcachedb_SOURCES = $(wildcard zcdb/src/*.rs zcdb/Cargo.toml) $(common_sources)

Expand Down
1 change: 1 addition & 0 deletions cmd/zfs_object_agent/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ edition = "2018"
chrono = "0.4"
clap = "2"
log = "0.4"
util = { path = "../util" }
zettacache = { path = "../zettacache" }
zettaobject = { path = "../zettaobject" }
2 changes: 1 addition & 1 deletion cmd/zfs_object_agent/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() {
);

if let Some(file_name) = matches.value_of("config-file") {
zettacache::read_tunable_config(file_name);
util::read_tunable_config(file_name);
}

error!(
Expand Down
18 changes: 18 additions & 0 deletions cmd/zfs_object_agent/util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "util"
version = "0.1.0"
authors = ["Delphix"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-trait = "0.1.51"
backtrace = "0.3"
config = "0.11"
lazy_static = "1.4.0"
log = "0.4"
more-asserts = "0.2.1"
rand = "0.8.3"
roaring = "0.7.0"
serde = { version = "1.0.125", features = ["derive"] }
tokio = { version = "1.4", features = ["full"] }
File renamed without changes.
12 changes: 12 additions & 0 deletions cmd/zfs_object_agent/util/src/from64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::convert::TryInto;

/// Conversions that are safe assuming that we are on LP64 (usize == u64)
pub trait From64<A> {
fn from64(a: A) -> Self;
}

impl From64<u64> for usize {
fn from64(a: u64) -> usize {
a.try_into().unwrap()
}
}
24 changes: 24 additions & 0 deletions cmd/zfs_object_agent/util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![warn(clippy::cast_lossless)]
#![warn(clippy::cast_possible_truncation)]
#![warn(clippy::cast_possible_wrap)]
#![warn(clippy::cast_sign_loss)]

mod bitmap_range_iterator;
mod die;
mod from64;
mod lock_set;
mod mutex_ext;
mod range_tree;
mod tunable;
mod vec_ext;

pub use bitmap_range_iterator::BitmapRangeIterator;
pub use die::maybe_die_with;
pub use from64::From64;
pub use lock_set::LockSet;
pub use lock_set::LockedItem;
pub use mutex_ext::MutexExt;
pub use range_tree::RangeTree;
pub use tunable::get_tunable;
pub use tunable::read_tunable_config;
pub use vec_ext::TerseVec;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions cmd/zfs_object_agent/util/src/vec_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::Deserialize;
use serde::Serialize;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result;

/// exists just to reduce Debug output on fields we don't really care about
#[derive(Serialize, Deserialize)]
pub struct TerseVec<T>(pub Vec<T>);
impl<T> Debug for TerseVec<T> {
fn fmt(&self, fmt: &mut Formatter) -> Result {
fmt.write_fmt(format_args!("[...{} elements...]", self.0.len()))
}
}
6 changes: 1 addition & 5 deletions cmd/zfs_object_agent/zettacache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ edition = "2018"
[dependencies]
anyhow = "1.0"
async-stream = "0.3.0"
async-trait = "0.1.51"
backtrace = "0.3"
bincode = "1.3.2"
chrono = "0.4"
config = "0.11"
conv = "0.3.3"
futures = "0.3.13"
futures-core = "0.3.13"
Expand All @@ -25,10 +22,9 @@ metered = "0.8"
more-asserts = "0.2.1"
nix = "0.22.0"
num = "0.4.0"
rand = "0.8.3"
roaring = "0.7.0"
seahash = "4.1.0"
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.64"
tokio = { version = "1.4", features = ["full"] }

util = { path = "../util" }
13 changes: 1 addition & 12 deletions cmd/zfs_object_agent/zettacache/src/base_types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use more_asserts::*;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::fmt::*;
use std::ops::Add;
use std::ops::Sub;
use util::From64;

/*
* Things that are stored on disk.
Expand Down Expand Up @@ -107,14 +107,3 @@ impl ReclaimLogId {
usize::from(self.0)
}
}

/// Conversions that are safe assuming that we are on LP64 (usize == u64)
pub trait From64<A> {
fn from64(a: A) -> Self;
}

impl From64<u64> for usize {
fn from64(a: u64) -> usize {
a.try_into().unwrap()
}
}
4 changes: 2 additions & 2 deletions cmd/zfs_object_agent/zettacache/src/block_access.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::base_types::DiskLocation;
use crate::base_types::Extent;
use crate::base_types::From64;
use crate::get_tunable;
use anyhow::{anyhow, Result};
use bincode::Options;
use lazy_static::lazy_static;
Expand All @@ -24,6 +22,8 @@ use std::time::Instant;
use tokio::fs::File;
use tokio::fs::OpenOptions;
use tokio::sync::Semaphore;
use util::get_tunable;
use util::From64;

lazy_static! {
static ref MIN_SECTOR_SIZE: usize = get_tunable("min_sector_size", 512);
Expand Down
8 changes: 4 additions & 4 deletions cmd/zfs_object_agent/zettacache/src/block_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::base_types::*;
use crate::base_types::{Extent, OnDisk};
use crate::bitmap_range_iterator::BitmapRangeIterator;
use crate::block_access::BlockAccess;
use crate::extent_allocator::ExtentAllocator;
use crate::get_tunable;
use crate::range_tree::RangeTree;
use crate::space_map::{SpaceMap, SpaceMapEntry, SpaceMapPhys};
use crate::zettacache::DEFAULT_SLAB_SIZE;
use lazy_static::lazy_static;
Expand All @@ -19,6 +15,10 @@ use std::convert::TryFrom;
use std::ops::Bound::*;
use std::sync::Arc;
use std::{iter, mem};
use util::get_tunable;
use util::BitmapRangeIterator;
use util::From64;
use util::RangeTree;

lazy_static! {
static ref DEFAULT_SLAB_BUCKETS: SlabAllocationBucketsPhys =
Expand Down
2 changes: 1 addition & 1 deletion cmd/zfs_object_agent/zettacache/src/block_based_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::base_types::*;
use crate::block_access::BlockAccess;
use crate::block_access::EncodeType;
use crate::extent_allocator::ExtentAllocator;
use crate::get_tunable;
use anyhow::Context;
use async_stream::stream;
use futures::stream::FuturesUnordered;
Expand All @@ -23,6 +22,7 @@ use std::ops::Bound::*;
use std::ops::Sub;
use std::sync::Arc;
use std::time::Instant;
use util::get_tunable;

lazy_static! {
// XXX maybe this is wasteful for the smaller logs?
Expand Down
2 changes: 1 addition & 1 deletion cmd/zfs_object_agent/zettacache/src/extent_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::base_types::DiskLocation;
use crate::base_types::Extent;
use crate::range_tree::RangeTree;
use log::*;
use more_asserts::*;
use serde::{Deserialize, Serialize};
use std::mem;
use util::RangeTree;

#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub struct ExtentAllocatorPhys {
Expand Down
9 changes: 0 additions & 9 deletions cmd/zfs_object_agent/zettacache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@
#![warn(clippy::cast_sign_loss)]

pub mod base_types;
mod bitmap_range_iterator;
mod block_access;
mod block_allocator;
mod block_based_log;
mod die;
mod extent_allocator;
mod index;
mod lock_set;
mod mutex_ext;
mod range_tree;
mod space_map;
mod tunable;
mod zcachedb;
mod zettacache;

pub use crate::zettacache::LookupResponse;
pub use crate::zettacache::ZettaCache;
pub use die::maybe_die_with;
pub use tunable::get_tunable;
pub use tunable::read_tunable_config;
pub use zcachedb::DumpStructuresOptions;
pub use zcachedb::ZettaCacheDBCommand;
11 changes: 6 additions & 5 deletions cmd/zfs_object_agent/zettacache/src/zettacache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ use crate::block_allocator::BlockAllocatorPhys;
use crate::block_based_log::*;
use crate::extent_allocator::ExtentAllocator;
use crate::extent_allocator::ExtentAllocatorPhys;
use crate::get_tunable;
use crate::index::*;
use crate::lock_set::LockSet;
use crate::lock_set::LockedItem;
use crate::maybe_die_with;
use crate::mutex_ext::MutexExt;
use crate::DumpStructuresOptions;
use anyhow::Result;
use conv::ConvUtil;
Expand All @@ -37,6 +32,12 @@ use std::time::Instant;
use tokio::sync::OwnedSemaphorePermit;
use tokio::sync::Semaphore;
use tokio::time::{sleep_until, timeout_at};
use util::get_tunable;
use util::maybe_die_with;
use util::From64;
use util::LockSet;
use util::LockedItem;
use util::MutexExt;

lazy_static! {
static ref SUPERBLOCK_SIZE: u64 = get_tunable("superblock_size", 4 * 1024);
Expand Down
1 change: 1 addition & 0 deletions cmd/zfs_object_agent/zettaobject/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ serde_json = "1.0.64"
stream-reduce = "0.1.0"
tokio = { version = "1.4", features = ["full"] }
tokio-stream = "0.1.5"
util = { path = "../util" }
uuid = {version = "0.8.2", features = ["v4", "serde"]}
zettacache = { path = "../zettacache" }
3 changes: 2 additions & 1 deletion cmd/zfs_object_agent/zettaobject/src/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use std::{
time::{Duration, Instant, SystemTime},
};
use tokio::sync::watch::{self, Receiver};
use util::get_tunable;
use util::maybe_die_with;
use uuid::Uuid;
use zettacache::{get_tunable, maybe_die_with};

lazy_static! {
pub static ref LEASE_DURATION: Duration =
Expand Down
8 changes: 6 additions & 2 deletions cmd/zfs_object_agent/zettaobject/src/object_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ use lru::LruCache;
use rand::prelude::*;
use rusoto_core::{ByteStream, RusotoError};
use rusoto_credential::{AutoRefreshingProvider, ChainProvider, ProfileProvider};
use rusoto_s3::*;
use rusoto_s3::{
Delete, DeleteObjectsRequest, GetObjectRequest, HeadObjectOutput, HeadObjectRequest,
ListObjectsV2Request, ObjectIdentifier, PutObjectError, PutObjectOutput, PutObjectRequest,
S3Client, S3,
};
use std::convert::TryFrom;
use std::error::Error;
use std::iter;
use std::sync::Arc;
use std::time::Instant;
use std::{collections::HashMap, fmt::Display};
use tokio::{sync::watch, time::error::Elapsed};
use zettacache::get_tunable;
use util::get_tunable;

struct ObjectCache {
// XXX cache key should include Bucket
Expand Down
2 changes: 1 addition & 1 deletion cmd/zfs_object_agent/zettaobject/src/object_based_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::marker::PhantomData;
use std::sync::Arc;
use std::time::Instant;
use tokio::task::JoinHandle;
use util::get_tunable;
use zettacache::base_types::*;
use zettacache::get_tunable;

lazy_static! {
pub static ref ENTRIES_PER_OBJECT: usize = get_tunable("entries_per_object", 100_000);
Expand Down
Loading

0 comments on commit 89e2853

Please sign in to comment.