Summary: Prototype of API to allow MyRocks to override default Mutex/CondVar used by transactions with their own implementations. They would simply need to pass their own implementations of Mutex/CondVar to the templated TransactionDB::Open(). Default implementation of TransactionDBMutex/TransactionDBCondVar provided (but the code is not currently changed to use this). Let me know if this API makes sense or if it should be changed Test Plan: n/a Reviewers: yhchiang, rven, igor, sdong, spetrunia Reviewed By: spetrunia Subscribers: maykov, dhruba, leveldb Differential Revision: https://reviews.facebook.net/D43761main
parent
0ccf2db385
commit
5e94f68f35
@ -0,0 +1,92 @@ |
||||
// Copyright (c) 2015, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
#pragma once |
||||
#ifndef ROCKSDB_LITE |
||||
|
||||
#include <memory> |
||||
|
||||
#include "rocksdb/status.h" |
||||
|
||||
namespace rocksdb { |
||||
|
||||
// TransactionDBMutex and TransactionDBCondVar APIs allows applications to
|
||||
// implement custom mutexes and condition variables to be used by a
|
||||
// TransactionDB when locking keys.
|
||||
//
|
||||
// To open a TransactionDB with a custom TransactionDBMutexFactory, set
|
||||
// TransactionDBOptions.custom_mutex_factory.
|
||||
|
||||
class TransactionDBMutex { |
||||
public: |
||||
virtual ~TransactionDBMutex() {} |
||||
|
||||
// Attempt to acquire lock. Return OK on success, or other Status on failure.
|
||||
// If returned status is OK, TransactionDB will eventually call UnLock().
|
||||
virtual Status Lock() = 0; |
||||
|
||||
// Attempt to acquire lock. If timeout is non-negative, operation should be
|
||||
// failed after this many microseconds.
|
||||
// Returns OK on success,
|
||||
// TimedOut if timed out,
|
||||
// or other Status on failure.
|
||||
// If returned status is OK, TransactionDB will eventually call UnLock().
|
||||
virtual Status TryLockFor(int64_t timeout_time) = 0; |
||||
|
||||
// Unlock Mutex that was successfully locked by Lock() or TryLockUntil()
|
||||
virtual void UnLock() = 0; |
||||
}; |
||||
|
||||
class TransactionDBCondVar { |
||||
public: |
||||
virtual ~TransactionDBCondVar() {} |
||||
|
||||
// Block current thread until condition variable is notified by a call to
|
||||
// Notify() or NotifyAll(). Wait() will be called with mutex locked.
|
||||
// Returns OK if notified.
|
||||
// Returns non-OK if TransactionDB should stop waiting and fail the operation.
|
||||
// May return OK spuriously even if not notified.
|
||||
virtual Status Wait(std::shared_ptr<TransactionDBMutex> mutex) = 0; |
||||
|
||||
// Block current thread until condition variable is notified by a call to
|
||||
// Notify() or NotifyAll(), or if the timeout is reached.
|
||||
// Wait() will be called with mutex locked.
|
||||
//
|
||||
// If timeout is non-negative, operation should be failed after this many
|
||||
// microseconds.
|
||||
// If implementing a custom version of this class, the implementation may
|
||||
// choose to ignore the timeout.
|
||||
//
|
||||
// Returns OK if notified.
|
||||
// Returns TimedOut if timeout is reached.
|
||||
// Returns other status if TransactionDB should otherwis stop waiting and
|
||||
// fail the operation.
|
||||
// May return OK spuriously even if not notified.
|
||||
virtual Status WaitFor(std::shared_ptr<TransactionDBMutex> mutex, |
||||
int64_t timeout_time) = 0; |
||||
|
||||
// If any threads are waiting on *this, unblock at least one of the
|
||||
// waiting threads.
|
||||
virtual void Notify() = 0; |
||||
|
||||
// Unblocks all threads waiting on *this.
|
||||
virtual void NotifyAll() = 0; |
||||
}; |
||||
|
||||
// Factory class that can allocate mutexes and condition variables.
|
||||
class TransactionDBMutexFactory { |
||||
public: |
||||
// Create a TransactionDBMutex object.
|
||||
virtual std::shared_ptr<TransactionDBMutex> AllocateMutex() = 0; |
||||
|
||||
// Create a TransactionDBCondVar object.
|
||||
virtual std::shared_ptr<TransactionDBCondVar> AllocateCondVar() = 0; |
||||
|
||||
virtual ~TransactionDBMutexFactory() {} |
||||
}; |
||||
|
||||
} // namespace rocksdb
|
||||
|
||||
#endif // ROCKSDB_LITE
|
@ -0,0 +1,121 @@ |
||||
// Copyright (c) 2015, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
#ifndef ROCKSDB_LITE |
||||
|
||||
#include "utilities/transactions/transaction_db_mutex_impl.h" |
||||
|
||||
#include <chrono> |
||||
#include <condition_variable> |
||||
#include <functional> |
||||
#include <mutex> |
||||
|
||||
#include "rocksdb/utilities/transaction_db_mutex.h" |
||||
|
||||
namespace rocksdb { |
||||
|
||||
class TransactionDBMutexImpl : public TransactionDBMutex { |
||||
public: |
||||
TransactionDBMutexImpl() {} |
||||
~TransactionDBMutexImpl() {} |
||||
|
||||
Status Lock() override; |
||||
|
||||
Status TryLockFor(int64_t timeout_time) override; |
||||
|
||||
void UnLock() override { mutex_.unlock(); } |
||||
|
||||
friend class TransactionDBCondVarImpl; |
||||
|
||||
private: |
||||
std::timed_mutex mutex_; |
||||
}; |
||||
|
||||
class TransactionDBCondVarImpl : public TransactionDBCondVar { |
||||
public: |
||||
TransactionDBCondVarImpl() {} |
||||
~TransactionDBCondVarImpl() {} |
||||
|
||||
Status Wait(std::shared_ptr<TransactionDBMutex> mutex) override; |
||||
|
||||
Status WaitFor(std::shared_ptr<TransactionDBMutex> mutex, |
||||
int64_t timeout_time) override; |
||||
|
||||
void Notify() override { cv_.notify_one(); } |
||||
|
||||
void NotifyAll() override { cv_.notify_all(); } |
||||
|
||||
private: |
||||
std::condition_variable_any cv_; |
||||
}; |
||||
|
||||
std::shared_ptr<TransactionDBMutex> |
||||
TransactionDBMutexFactoryImpl::AllocateMutex() { |
||||
return std::shared_ptr<TransactionDBMutex>(new TransactionDBMutexImpl()); |
||||
} |
||||
|
||||
std::shared_ptr<TransactionDBCondVar> |
||||
TransactionDBMutexFactoryImpl::AllocateCondVar() { |
||||
return std::shared_ptr<TransactionDBCondVar>(new TransactionDBCondVarImpl()); |
||||
} |
||||
|
||||
Status TransactionDBMutexImpl::Lock() { |
||||
mutex_.lock(); |
||||
return Status::OK(); |
||||
} |
||||
|
||||
Status TransactionDBMutexImpl::TryLockFor(int64_t timeout_time) { |
||||
bool locked = true; |
||||
|
||||
if (timeout_time < 0) { |
||||
// If timeout is negative, we wait indefinitely to acquire the lock
|
||||
mutex_.lock(); |
||||
} else if (timeout_time == 0) { |
||||
locked = mutex_.try_lock(); |
||||
} else { |
||||
// Attempt to acquire the lock unless we timeout
|
||||
auto duration = std::chrono::microseconds(timeout_time); |
||||
locked = mutex_.try_lock_for(duration); |
||||
} |
||||
|
||||
if (!locked) { |
||||
// timeout acquiring mutex
|
||||
return Status::TimedOut(Status::SubCode::kMutexTimeout); |
||||
} |
||||
|
||||
return Status::OK(); |
||||
} |
||||
|
||||
Status TransactionDBCondVarImpl::Wait( |
||||
std::shared_ptr<TransactionDBMutex> mutex) { |
||||
auto mutex_impl = reinterpret_cast<TransactionDBMutexImpl*>(mutex.get()); |
||||
cv_.wait(mutex_impl->mutex_); |
||||
return Status::OK(); |
||||
} |
||||
|
||||
Status TransactionDBCondVarImpl::WaitFor( |
||||
std::shared_ptr<TransactionDBMutex> mutex, int64_t timeout_time) { |
||||
auto mutex_impl = reinterpret_cast<TransactionDBMutexImpl*>(mutex.get()); |
||||
|
||||
if (timeout_time < 0) { |
||||
// If timeout is negative, do not use a timeout
|
||||
cv_.wait(mutex_impl->mutex_); |
||||
} else { |
||||
auto duration = std::chrono::microseconds(timeout_time); |
||||
auto cv_status = cv_.wait_for(mutex_impl->mutex_, duration); |
||||
|
||||
// Check if the wait stopped due to timing out.
|
||||
if (cv_status == std::cv_status::timeout) { |
||||
return Status::TimedOut(Status::SubCode::kMutexTimeout); |
||||
} |
||||
} |
||||
|
||||
// CV was signaled, or we spuriously woke up (but didn't time out)
|
||||
return Status::OK(); |
||||
} |
||||
|
||||
} // namespace rocksdb
|
||||
|
||||
#endif // ROCKSDB_LITE
|
@ -0,0 +1,26 @@ |
||||
// Copyright (c) 2015, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
|
||||
#pragma once |
||||
#ifndef ROCKSDB_LITE |
||||
|
||||
#include "rocksdb/utilities/transaction_db_mutex.h" |
||||
|
||||
namespace rocksdb { |
||||
|
||||
class TransactionDBMutex; |
||||
class TransactionDBCondVar; |
||||
|
||||
// Default implementation of TransactionDBMutexFactory. May be overridden
|
||||
// by TransactionDBOptions.custom_mutex_factory.
|
||||
class TransactionDBMutexFactoryImpl : public TransactionDBMutexFactory { |
||||
public: |
||||
std::shared_ptr<TransactionDBMutex> AllocateMutex() override; |
||||
std::shared_ptr<TransactionDBCondVar> AllocateCondVar() override; |
||||
}; |
||||
|
||||
} // namespace rocksdb
|
||||
|
||||
#endif // ROCKSDB_LITE
|
Loading…
Reference in new issue