Commit 99a9110c authored by Georgios Bitzes's avatar Georgios Bitzes
Browse files

Implement method to retrieve current holder of a lease

Along with some more information: last renewal time, and deadline.
parent a64f3beb
Pipeline #427239 passed with stages
in 33 minutes and 53 seconds
......@@ -68,6 +68,7 @@ add_library(XrdQuarkDB SHARED
storage/KeyDescriptor.hh
storage/KeyDescriptorBuilder.cc storage/KeyDescriptorBuilder.hh
storage/KeyLocators.hh
storage/LeaseInfo.hh
storage/PatternMatching.hh
storage/ReverseLocator.hh
storage/StagingArea.hh
......
......@@ -1058,6 +1058,30 @@ void StateMachine::advanceClock(StagingArea &stagingArea, ClockValue newValue) {
stagingArea.put(KeyConstants::kStateMachine_Clock, unsignedIntToBinaryString(newValue));
}
rocksdb::Status StateMachine::lease_get(StagingArea &stagingArea, const std::string &key, ClockValue clockUpdate, LeaseInfo &info) {
// Advance clock, and clear out any expired leases.
maybeAdvanceClock(stagingArea, clockUpdate);
KeyDescriptor keyinfo = getKeyDescriptor(stagingArea, key);
if(keyinfo.empty()) {
return rocksdb::Status::NotFound();
}
if(keyinfo.getKeyType() != KeyType::kLease) {
return wrong_type();
}
LeaseLocator locator(key);
std::string value;
THROW_ON_ERROR(stagingArea.get(locator.toSlice(), value));
info = LeaseInfo(value, keyinfo.getStartIndex(), keyinfo.getEndIndex());
return rocksdb::Status::OK();
}
void StateMachine::advanceClock(ClockValue newValue, LogIndex index) {
StagingArea stagingArea(*this);
advanceClock(stagingArea, newValue);
......@@ -1678,6 +1702,10 @@ rocksdb::Status StateMachine::lease_acquire(const std::string &key, const std::s
CHAIN(index, lease_acquire, key, value, clockUpdate, duration, acquired);
}
rocksdb::Status StateMachine::lease_get(const std::string &key, ClockValue clockUpdate, LeaseInfo &info, LogIndex index) {
CHAIN(index, lease_get, key, clockUpdate, info);
}
rocksdb::Status StateMachine::lease_release(const std::string &key, LogIndex index) {
CHAIN(index, lease_release, key);
}
......@@ -31,6 +31,7 @@
#include "storage/KeyLocators.hh"
#include "storage/ConsistencyScanner.hh"
#include "storage/KeyConstants.hh"
#include "storage/LeaseInfo.hh"
#include <rocksdb/db.h>
#include <rocksdb/utilities/write_batch_with_index.h>
#include <rocksdb/utilities/debug.h>
......@@ -81,6 +82,7 @@ public:
void advanceClock(StagingArea &stagingArea, ClockValue newValue);
rocksdb::Status lease_acquire(StagingArea &stagingArea, const std::string &key, const std::string &value, ClockValue clockUpdate, uint64_t duration, bool &acquired);
rocksdb::Status lease_release(StagingArea &stagingArea, const std::string &key);
rocksdb::Status lease_get(StagingArea &stagingArea, const std::string &key, ClockValue clockUpdate, LeaseInfo &info);
//----------------------------------------------------------------------------
// API for transactional reads. Can be part of a mixed read-write transaction.
......@@ -148,6 +150,7 @@ public:
rocksdb::Status rawGetAllVersions(const std::string &key, std::vector<rocksdb::KeyVersion> &versions);
rocksdb::Status lease_acquire(const std::string &key, const std::string &value, ClockValue clockUpdate, uint64_t duration, bool &acquired, LogIndex index = 0);
rocksdb::Status lease_release(const std::string &key, LogIndex index = 0);
rocksdb::Status lease_get(const std::string &key, ClockValue clockUpdate, LeaseInfo &info, LogIndex index = 0);
//----------------------------------------------------------------------------
// Internal configuration, not exposed to users through 'KEYS' and friends.
......
// ----------------------------------------------------------------------
// File: LeaseInfo.hh
// Author: Georgios Bitzes - CERN
// ----------------------------------------------------------------------
/************************************************************************
* quarkdb - a redis-like highly available key-value store *
* Copyright (C) 2018 CERN/Switzerland *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
************************************************************************/
#ifndef QUARKDB_LEASE_INFO_H
#define QUARKDB_LEASE_INFO_H
#include <string_view>
namespace quarkdb {
using ClockValue = uint64_t;
class LeaseInfo {
public:
LeaseInfo() {}
LeaseInfo(const std::string& val, ClockValue ren, ClockValue dl) :
value(val), lastRenewal(ren), deadline(dl) {}
ClockValue getLastRenewal() const {
return lastRenewal;
}
ClockValue getDeadline() const {
return deadline;
}
std::string_view getValue() const {
return value;
}
private:
std::string value;
ClockValue lastRenewal;
ClockValue deadline;
};
}
#endif
......@@ -835,6 +835,14 @@ TEST_F(State_Machine, Leases) {
iterator.next();
ASSERT_FALSE(iterator.valid());
}
LeaseInfo info;
ASSERT_OK(stateMachine()->lease_get("my-lease-4", ClockValue(25), info));
ASSERT_EQ(info.getLastRenewal(), ClockValue(25));
ASSERT_EQ(info.getDeadline(), ClockValue(35));
ASSERT_EQ(info.getValue(), "some-other-string");
ASSERT_NOTFOUND(stateMachine()->lease_get("does-not-exist", ClockValue(25), info));
}
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