Skip to content

Commit

Permalink
feat(foundationdb,bindingtester): add get_estimated_range_size_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreZ committed Jul 5, 2021
1 parent 9723325 commit 67e8d23
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
32 changes: 32 additions & 0 deletions foundationdb-bindingtester/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ static GOT_COMMITTED_VERSION: Element =
static ERROR_NONE: Element = Element::Bytes(Bytes(Cow::Borrowed(b"ERROR: NONE")));
static ERROR_MULTIPLE: Element = Element::Bytes(Bytes(Cow::Borrowed(b"ERROR: MULTIPLE")));
static OK: Element = Element::Bytes(Bytes(Cow::Borrowed(b"OK")));
static ESTIMATE_RANGE_RESPONSE: Element =
Element::Bytes(Bytes(Cow::Borrowed(b"GOT_ESTIMATED_RANGE_SIZE")));

#[cfg(feature = "fdb-6_2")]
static GOT_APPROXIMATE_SIZE: Element =
Element::Bytes(Bytes(Cow::Borrowed(b"GOT_APPROXIMATE_SIZE")));

use crate::fdb::options::{MutationType, StreamingMode};
use tuple::VersionstampOffset;

fn mutation_from_str(s: &str) -> MutationType {
match s {
"ADD" => MutationType::Add,
Expand Down Expand Up @@ -171,6 +174,7 @@ enum InstrCode {
GetCommittedVersion,
GetApproximateSize,
WaitFuture,
GetEstimatedRangeSize,

TuplePack,
TuplePackWithVersionstamp,
Expand Down Expand Up @@ -247,6 +251,7 @@ impl Instr {
"GET_COMMITTED_VERSION" => GetCommittedVersion,
"GET_APPROXIMATE_SIZE" => GetApproximateSize,
"WAIT_FUTURE" => WaitFuture,
"GET_ESTIMATED_RANGE_SIZE" => GetEstimatedRangeSize,

"TUPLE_PACK" => TuplePack,
"TUPLE_PACK_WITH_VERSIONSTAMP" => TuplePackWithVersionstamp,
Expand Down Expand Up @@ -1230,6 +1235,33 @@ impl StackMachine {
let item = self.pop().await;
self.stack.push(item);
}

// Pops the top two items off of the stack as BEGIN_KEY and END_KEY to
// construct a key range. Then call the `getEstimatedRangeSize` API of
// the language binding. Make sure the API returns without error. Finally
// push the string "GOT_ESTIMATED_RANGE_SIZE" onto the stack.
GetEstimatedRangeSize => {
debug!("get estimated range size");
#[cfg(feature = "fdb-6_3")]
{
let begin = self.pop_bytes().await;
let end = self.pop_bytes().await;

if let Ok(estimate) = trx
.as_mut()
.get_estimated_range_size_bytes(&begin, &end)
.await
{
debug!("got an estimate of {} bytes", estimate);
self.push(number, ESTIMATE_RANGE_RESPONSE.clone().into_owned());
}
}
#[cfg(not(feature = "fdb-6_3"))]
{
unimplemented!("get_estimated_range_size requires fdb630+");
}
}

// Pops the top item off of the stack as N. Pops the next N items off of the
// stack and packs them as the tuple [item0,item1,...,itemN], and then pushes
// this single packed value onto the stack.
Expand Down
18 changes: 18 additions & 0 deletions foundationdb/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,24 @@ impl Transaction {
}
}

/// Get the estimated byte size of the key range based on the byte sample collected by FDB
#[cfg(feature = "fdb-6_3")]
pub fn get_estimated_range_size_bytes(
&self,
begin: &[u8],
end: &[u8],
) -> impl Future<Output = FdbResult<i64>> + Send + Sync + Unpin {
FdbFuture::<i64>::new(unsafe {
fdb_sys::fdb_transaction_get_estimated_range_size_bytes(
self.inner.as_ptr(),
begin.as_ptr(),
fdb_len(begin.len(), "begin"),
end.as_ptr(),
fdb_len(end.len(), "end"),
)
})
}

/// Attempts to commit the sets and clears previously applied to the database snapshot
/// represented by transaction to the actual database.
///
Expand Down
30 changes: 30 additions & 0 deletions foundationdb/tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ fn test_range() {
futures::executor::block_on(test_get_range_async()).expect("failed to run");
futures::executor::block_on(test_range_option_async()).expect("failed to run");
futures::executor::block_on(test_get_ranges_async()).expect("failed to run");
#[cfg(feature = "fdb-6_3")]
futures::executor::block_on(test_get_estimate_range()).expect("failed to run");
}

async fn test_get_range_async() -> FdbResult<()> {
Expand Down Expand Up @@ -203,3 +205,31 @@ async fn test_range_option_async() -> FdbResult<()> {

Ok(())
}
async fn test_get_estimate_range() -> FdbResult<()> {
const N: usize = 10000;

let db = common::database().await?;
let trx = db.create_trx()?;
let key_begin = "test-rangeoption-";
let key_end = "test-rangeoption.";
let k = |i: u32| format!("{}-{:010}", key_begin, i);

eprintln!("clearing...");
trx.clear_range(key_begin.as_bytes(), key_end.as_bytes());

eprintln!("inserting...");
for i in 0..10000 {
let value = common::random_str(10);
trx.set(k(i).as_bytes(), value.as_bytes());
}
trx.commit().await?;

let trx = db.create_trx()?;
let estimate = trx
.get_estimated_range_size_bytes(key_begin.as_bytes(), key_end.as_bytes())
.await?;
eprintln!("get an estimate of {} bytes", estimate);
assert!(estimate > 0);

Ok(())
}
8 changes: 4 additions & 4 deletions scripts/run_bindingtester.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ esac
cd ${fdb_builddir:?}

## Get foundationdb source
git clone --depth 1 https://github.com/apple/foundationdb.git -b release-6.1
git clone --depth 1 https://github.com/apple/foundationdb.git -b release-6.3
cd foundationdb
git checkout release-6.1
git checkout release-6.3

## need the python api bindings
make fdb_python
Expand All @@ -33,6 +33,6 @@ esac
echo "testers['rust'] = Tester('rust', '${bindingtester}', 2040, 23, MAX_API_VERSION, types=ALL_TYPES)
" >> ./bindings/bindingtester/known_testers.py
./bindings/bindingtester/bindingtester.py --test-name scripted rust
./bindings/bindingtester/bindingtester.py --num-ops 1000 --api-version 610 --test-name api --compare python rust
./bindings/bindingtester/bindingtester.py --num-ops 1000 --api-version 610 --test-name api --concurrency 5 rust
./bindings/bindingtester/bindingtester.py --num-ops 1000 --api-version 630 --test-name api --compare python rust
./bindings/bindingtester/bindingtester.py --num-ops 1000 --api-version 630 --test-name api --concurrency 5 rust
)

0 comments on commit 67e8d23

Please sign in to comment.