Skip to content

Async/await and FDB 6.1 support #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ matrix:
RUSTFMT=true
script:
- rustup component add rustfmt-preview
- cargo fmt --all -- --write-mode=diff
- cargo fmt --all -- --check

## bindingtester
- rust: stable
Expand All @@ -56,13 +56,13 @@ before_install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then scripts/install_foundationdb_linux.sh ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then scripts/install_foundationdb_macos.sh ; fi
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then scripts/install_foundationdb_win64.sh ; export PATH="${PATH}:/c/Program Files/foundationdb/bin" ; fi
- if [[ "$CLIPPY" == "true" ]]; then cargo install clippy --force ; fi
- if [[ "$CLIPPY" == "true" ]]; then rustup component add clippy-preview ; fi

script:
- cargo test --manifest-path foundationdb-sys/Cargo.toml
- cargo test --manifest-path foundationdb-gen/Cargo.toml
- cargo test --manifest-path foundationdb/Cargo.toml
- cargo test --manifest-path foundationdb/Cargo.toml --no-default-features --features fdb-6_0
- cargo test --manifest-path foundationdb/Cargo.toml --no-default-features --features fdb-6_1
- cargo test --manifest-path foundationdb-bench/Cargo.toml
- cargo run --manifest-path foundationdb/Cargo.toml --example class-scheduling

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ build_script:
- cargo test --manifest-path foundationdb-sys/Cargo.toml
- cargo test --manifest-path foundationdb-gen/Cargo.toml
- cargo test --manifest-path foundationdb/Cargo.toml
- cargo test --manifest-path foundationdb/Cargo.toml --no-default-features --features fdb-6_0
- cargo test --manifest-path foundationdb/Cargo.toml --no-default-features --features fdb-6_1
- cargo test --manifest-path foundationdb-bench/Cargo.toml
- cargo run --manifest-path foundationdb/Cargo.toml --example class-scheduling
3 changes: 2 additions & 1 deletion foundationdb-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "foundationdb-bench"
version = "0.1.0"
authors = ["Benjamin Fry <[email protected]>"]
edition = "2018"

description = """
Bindings to the C api for FoundationDB
Expand All @@ -21,7 +22,7 @@ travis-ci = { repository = "bluejekyll/foundationdb-rs" }

[dependencies]
rand = "0.7"
futures = "0.1"
futures = "0.3"
foundationdb = { version = "*", path = "../foundationdb" }
stopwatch = "0"
log = "0.4"
Expand Down
77 changes: 32 additions & 45 deletions foundationdb-bench/src/bin/fdb-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ extern crate structopt;
use std::sync::atomic::*;
use std::sync::Arc;

use futures::executor::block_on;
use futures::future::*;
use rand::prelude::*;
use rand::rngs::mock::StepRng;
use stopwatch::Stopwatch;
use structopt::StructOpt;

use fdb::error::*;
use fdb::*;
use crate::fdb::error::*;
use crate::fdb::*;

#[derive(Clone)]
struct Counter {
Expand Down Expand Up @@ -45,7 +46,6 @@ struct BenchRunner {
key_buf: Vec<u8>,
val_buf: Vec<u8>,
rng: StepRng,
trx: Option<Transaction>,
trx_batch_size: usize,
}

Expand All @@ -57,47 +57,42 @@ impl BenchRunner {
let mut val_buf = Vec::with_capacity(opt.val_len);
val_buf.resize(opt.val_len, 0u8);

let trx = db.create_trx().expect("failed to create trx");

Self {
db,
counter,
key_buf,
val_buf,

rng,
trx: Some(trx),
trx_batch_size: opt.trx_batch_size,
}
}

//TODO: impl future
fn run(self) -> Box<Future<Item = (), Error = Error>> {
Box::new(loop_fn(self, Self::step))
}

//TODO: impl future
fn step(mut self) -> Box<Future<Item = Loop<(), Self>, Error = Error>> {
let trx = self.trx.take().unwrap();

for _ in 0..self.trx_batch_size {
self.rng.fill_bytes(&mut self.key_buf);
self.rng.fill_bytes(&mut self.val_buf);
self.key_buf[0] = 0x01;
trx.set(&self.key_buf, &self.val_buf);
}
async fn run(mut self) -> Result<()> {
let trx = self.db.create_trx().expect("failed to create trx");

loop {
let trx = trx.clone();
for _ in 0..self.trx_batch_size {
self.rng.fill_bytes(&mut self.key_buf);
self.rng.fill_bytes(&mut self.val_buf);
self.key_buf[0] = 0x01;
trx.set(&self.key_buf, &self.val_buf);
}

let f = trx.commit().map(move |trx| {
trx.reset();
self.trx = Some(trx);
let trx2 = trx.clone();
trx.commit().await?;
trx2.reset();

if self.counter.decr(self.trx_batch_size) {
Loop::Continue(self)
continue;
} else {
Loop::Break(())
break;
}
});
Box::new(f)
}

Ok(())
}
}

Expand All @@ -124,7 +119,7 @@ impl Bench {
let range = start..end;
let counter = counter.clone();
let b = self.clone();
let handle = std::thread::spawn(move || b.run_range(range, counter).wait());
let handle = std::thread::spawn(move || block_on(b.run_range(range, counter)));
handles.push(handle);

start = end;
Expand All @@ -146,22 +141,22 @@ impl Bench {
);
}

fn run_range(
&self,
r: std::ops::Range<usize>,
counter: Counter,
) -> Box<Future<Item = (), Error = Error>> {
async fn run_range(&self, r: std::ops::Range<usize>, counter: Counter) -> Result<()> {
let runners = r
.into_iter()
.map(|n| {
// With deterministic Rng, benchmark with same parameters will overwrite same set
// of keys again, which makes benchmark result stable.
let rng = StepRng::new(n as u64, 1);
BenchRunner::new(self.db.clone(), rng, counter.clone(), &self.opt).run()
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();

let f = join_all(runners).map(|_| ());
Box::new(f)
for r in join_all(runners).await {
r.expect("didn't expect error in transaction");
}

Ok(())
}
}

Expand Down Expand Up @@ -204,15 +199,7 @@ fn main() {

network.wait();

let cluster_path = fdb::default_config_path();
let cluster = Cluster::new(cluster_path)
.wait()
.expect("failed to create cluster");

let db = cluster
.create_database()
.wait()
.expect("failed to get database");
let db = Database::new(foundationdb::default_config_path()).expect("db connect failed");

let bench = Bench { db, opt };
bench.run();
Expand Down
1 change: 1 addition & 0 deletions foundationdb-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "foundationdb-gen"
version = "0.1.0"
authors = ["Jihyun Yu <[email protected]>"]
edition = "2018"

description = """
Binding generation helper for FoundationDB.
Expand Down
8 changes: 5 additions & 3 deletions foundationdb-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl FdbScope {
for option in self.options.iter() {
let rs_name = match option.name.as_ref() {
"AppendIfFit" => "AppendIfFits",
s => s
s => s,
};

s += &format!("{}::{}", self.name, rs_name);
Expand Down Expand Up @@ -94,6 +94,7 @@ impl FdbScope {
("THREAD", "THREADS"),
("KEY", "KEYS"),
("FIT", "FITS"),
("PROXY", "PROXIES"),
];

for &(ref from, ref to) in tab.iter() {
Expand Down Expand Up @@ -352,7 +353,8 @@ const OPTIONS_DATA: &[u8] = include_bytes!("/usr/include/foundationdb/fdb.option
const OPTIONS_DATA: &[u8] = include_bytes!("/usr/local/include/foundationdb/fdb.options");

#[cfg(target_os = "windows")]
const OPTIONS_DATA: &[u8] = include_bytes!("C:/Program Files/foundationdb/include/foundationdb/fdb.options");
const OPTIONS_DATA: &[u8] =
include_bytes!("C:/Program Files/foundationdb/include/foundationdb/fdb.options");

pub fn emit() -> Result<String> {
let mut reader = OPTIONS_DATA.as_ref();
Expand Down Expand Up @@ -387,7 +389,7 @@ pub fn emit() -> Result<String> {

let mut result = format!(
"{}\n{}\n{}\n\n",
"use std;", "use error;", "use foundationdb_sys as fdb;"
"use std;", "use crate::error;", "use foundationdb_sys as fdb;"
);

for scope in scopes.iter() {
Expand Down
7 changes: 3 additions & 4 deletions foundationdb-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "foundationdb-sys"
version = "0.2.0"
authors = ["Benjamin Fry <[email protected]>"]
edition = "2018"

description = """
Bindings to the C api for FoundationDB
Expand All @@ -21,11 +22,9 @@ travis-ci = { repository = "bluejekyll/foundationdb-rs" }
# codecov = { repository = "bluejekyll/foundationdb-rs", branch = "master", service = "github" }

[features]
default = [ "fdb-6_0" ]
default = [ "fdb-6_1" ]

fdb-5_1 = [ ]
fdb-5_2 = [ ]
fdb-6_0 = [ ]
fdb-6_1 = [ ]

[dependencies]

Expand Down
12 changes: 2 additions & 10 deletions foundationdb-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,9 @@ fn main() {
// out a src/wrapper.h file with the chosen version instead.
let api_version;

#[cfg(feature = "fdb-5_1")]
#[cfg(feature = "fdb-6_1")]
{
api_version = 510;
}
#[cfg(feature = "fdb-5_2")]
{
api_version = 520;
}
#[cfg(feature = "fdb-6_0")]
{
api_version = 600;
api_version = 610;
}

// Sigh, bindgen only takes a String for its header path, but that's UTF-8 while
Expand Down
9 changes: 4 additions & 5 deletions foundationdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "foundationdb"
version = "0.3.0"
authors = ["Benjamin Fry <[email protected]>"]
edition = "2018"

description = """
High level client bindings for FoundationDB.
Expand All @@ -20,11 +21,9 @@ travis-ci = { repository = "bluejekyll/foundationdb-rs" }
# codecov = { repository = "bluejekyll/foundationdb-rs", branch = "master", service = "github" }

[features]
default = ["fdb-6_0", "uuid"]
default = ["fdb-6_1", "uuid"]

fdb-5_1 = [ "foundationdb-sys/fdb-5_1" ]
fdb-5_2 = [ "foundationdb-sys/fdb-5_2" ]
fdb-6_0 = [ "foundationdb-sys/fdb-6_0" ]
fdb-6_1 = [ "foundationdb-sys/fdb-6_1" ]

[build-dependencies]
foundationdb-gen = { version = "0.1.0", path = "../foundationdb-gen" }
Expand All @@ -33,7 +32,7 @@ foundationdb-gen = { version = "0.1.0", path = "../foundationdb-gen" }
failure = "0.1"
failure_derive = "0.1"
foundationdb-sys = { version = "0.2.0", path = "../foundationdb-sys", default-features = false }
futures = "0.1"
futures = "0.3"
lazy_static = "1.0"
byteorder = "1.2"
log = "0.4"
Expand Down
Loading