Skip to content
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

Add contains_globally_committed to kv::Set #5928

Merged
merged 5 commits into from
Jan 15, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

[5.0.0-dev12]: https://github.com/microsoft/CCF/releases/tag/ccf-5.0.0-dev12

### Added

- There is now a `contains_globally_committed(k)` method on `kv::Set<K>`, with the same semantics as `get_globally_committed(k)` on `kv::Map<K, V>` (#5928).

### Changed

- JS endpoints marked as `"mode": "readonly"` are prevented from writing to the KV. Attempting to call `map.set(k, v)`, `map.delete(k)`, or `map.clear()` on any KV table in such an endpoint will now result in an error being thrown (#5921).
Expand Down
15 changes: 15 additions & 0 deletions include/ccf/kv/set_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ namespace kv
return read_handle.has(KSerialiser::to_serialised(key));
}

/** Test whether a key's presence is globally committed, meaning it has been
* replciated and acknowledged by consensus protocol.
*
* @see kv::ReadableMapHandle::get_globally_committed
*
* @param key Key to test
*
* @return Boolean true iff key exists in globally committed state
*/
bool contains_globally_committed(const K& key)
{
return read_handle.has_globally_committed(
KSerialiser::to_serialised(key));
}

/** Get version when this key was last added to the set.
*
* Returns nullopt if the key is not present.
Expand Down
2 changes: 2 additions & 0 deletions include/ccf/kv/untyped_map_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace kv::untyped

bool has(const KeyType& key);

bool has_globally_committed(const KeyType& key);

void put(const KeyType& key, const ValueType& value);

void remove(const KeyType& key);
Expand Down
54 changes: 54 additions & 0 deletions src/kv/test/kv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,16 @@ TEST_CASE("sets and values")
REQUIRE(set_handle->contains(k2));
REQUIRE(set_handle->size() == 2);

REQUIRE(!set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));

set_handle->remove(k2);
REQUIRE(!set_handle->contains(k2));
REQUIRE(set_handle->size() == 1);

REQUIRE(!set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));

REQUIRE(tx.commit() == kv::CommitResult::SUCCESS);
}

Expand All @@ -212,6 +218,11 @@ TEST_CASE("sets and values")
auto set_handle = tx.ro(set);
REQUIRE(set_handle->contains(k1));
REQUIRE(!set_handle->contains(k2));

// NB: Previous transaction committed locally, but not yet globally
REQUIRE(!set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));

REQUIRE(set_handle->size() == 1);
std::set<std::string> std_set;
set_handle->foreach([&std_set](const std::string& entry) {
Expand All @@ -224,6 +235,26 @@ TEST_CASE("sets and values")
REQUIRE(tx.commit() == kv::CommitResult::SUCCESS);
}

{
INFO("Read committed writes");
kv_store.compact(kv_store.current_version());

auto tx = kv_store.create_tx();
auto set_handle = tx.rw(set);

REQUIRE(set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));

// Local modifications do not affect globally_committed
set_handle->remove(k1);
set_handle->insert(k2);

REQUIRE(set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));

// This tx is deliberately dropped, not committed
}

{
INFO("Remove keys");
auto tx = kv_store.create_tx();
Expand All @@ -240,9 +271,32 @@ TEST_CASE("sets and values")
REQUIRE(!set_handle->contains(k1));
REQUIRE(set_handle->size() == 0);

// Not even locally committed, so globally_committed is unaffected
REQUIRE(set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));

REQUIRE(tx.commit() == kv::CommitResult::SUCCESS);
}

{
INFO("Read committed removals");
{
auto tx = kv_store.create_tx();
auto set_handle = tx.ro(set);
// Removal is still only locally committed
REQUIRE(set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));
}
kv_store.compact(kv_store.current_version());
{
auto tx = kv_store.create_tx();
auto set_handle = tx.ro(set);
// Removal is now globally committed
REQUIRE(!set_handle->contains_globally_committed(k1));
REQUIRE(!set_handle->contains_globally_committed(k2));
}
}

{
INFO("Hooks");

Expand Down
6 changes: 6 additions & 0 deletions src/kv/untyped_map_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ namespace kv::untyped
return found;
}

bool MapHandle::has_globally_committed(const MapHandle::KeyType& key)
{
auto raw = tx_changes.committed.getp(key);
return raw != nullptr;
}

void MapHandle::put(
const MapHandle::KeyType& key, const MapHandle::ValueType& value)
{
Expand Down