Commit 57a22892 authored by Georgios Bitzes's avatar Georgios Bitzes
Browse files

Automatically release leases past the deadline when advancing the clock

parent a3cc3fd2
Pipeline #425401 passed with stages
in 37 minutes and 46 seconds
......@@ -29,6 +29,7 @@
#include "storage/StagingArea.hh"
#include "storage/KeyDescriptorBuilder.hh"
#include "storage/PatternMatching.hh"
#include "storage/ExpirationEventIterator.hh"
#include "utils/IntToBinaryString.hh"
#include "utils/TimeFormatting.hh"
#include <sys/stat.h>
......@@ -1046,6 +1047,13 @@ void StateMachine::advanceClock(StagingArea &stagingArea, ClockValue newValue) {
qdb_throw("Attempted to set state machine clock in the past: " << prevValue << " ==> " << newValue);
}
// Clear out any leases past the deadline
ExpirationEventIterator iter(stagingArea);
while(iter.valid() && iter.getDeadline() <= newValue) {
qdb_assert(lease_release(stagingArea, std::string(iter.getRedisKey())).ok());
iter.next();
}
// Update value
stagingArea.put(KeyConstants::kStateMachine_Clock, unsignedIntToBinaryString(newValue));
}
......@@ -1100,7 +1108,6 @@ rocksdb::Status StateMachine::lease_acquire(StagingArea &stagingArea, const std:
// Quick check that no-one else holds the lease right now.
// Could it be that the lease has actually expired? Not at this point.
// advanceClock() should have taken care of removing expired leases.
// TODO: Actually implement that in advanceClock ;>
LeaseLocator locator(key);
std::string oldLeaseHolder;
......
......@@ -789,6 +789,50 @@ TEST_F(State_Machine, Leases) {
iterator.next();
ASSERT_FALSE(iterator.valid());
}
stateMachine()->lease_acquire("my-lease-3", "some-other-string", ClockValue(18), 10, acquired);
ASSERT_TRUE(acquired);
stateMachine()->lease_acquire("my-lease-4", "some-other-string", ClockValue(18), 10, acquired);
ASSERT_TRUE(acquired);
stateMachine()->getClock(clk);
ASSERT_EQ(clk, 18u);
{
StagingArea stagingArea(*stateMachine());
ExpirationEventIterator iterator(stagingArea);
ASSERT_TRUE(iterator.valid());
ASSERT_EQ(iterator.getDeadline(), 19u);
ASSERT_EQ(iterator.getRedisKey(), "my-lease");
iterator.next();
ASSERT_TRUE(iterator.valid());
ASSERT_EQ(iterator.getDeadline(), 28u);
ASSERT_EQ(iterator.getRedisKey(), "my-lease-3");
iterator.next();
ASSERT_TRUE(iterator.valid());
ASSERT_EQ(iterator.getDeadline(), 28u);
ASSERT_EQ(iterator.getRedisKey(), "my-lease-4");
iterator.next();
ASSERT_FALSE(iterator.valid());
}
stateMachine()->lease_acquire("my-lease-4", "some-other-string", ClockValue(25), 10, acquired);
ASSERT_TRUE(acquired);
{
StagingArea stagingArea(*stateMachine());
ExpirationEventIterator iterator(stagingArea);
ASSERT_TRUE(iterator.valid());
ASSERT_EQ(iterator.getDeadline(), 28u);
ASSERT_EQ(iterator.getRedisKey(), "my-lease-3");
iterator.next();
ASSERT_TRUE(iterator.valid());
ASSERT_EQ(iterator.getDeadline(), 35u);
ASSERT_EQ(iterator.getRedisKey(), "my-lease-4");
iterator.next();
ASSERT_FALSE(iterator.valid());
}
}
static std::string sliceToString(const rocksdb::Slice &slice) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment