fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
5.7 KiB
162 lines
5.7 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
7 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
10 years ago
|
|
||
|
#pragma once
|
||
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
4 years ago
|
#include <memory>
|
||
10 years ago
|
#include <string>
|
||
|
#include <unordered_map>
|
||
8 years ago
|
#include <utility>
|
||
10 years ago
|
#include <vector>
|
||
|
|
||
8 years ago
|
#include "monitoring/instrumented_mutex.h"
|
||
10 years ago
|
#include "rocksdb/utilities/transaction.h"
|
||
8 years ago
|
#include "util/autovector.h"
|
||
8 years ago
|
#include "util/hash_map.h"
|
||
10 years ago
|
#include "util/thread_local.h"
|
||
4 years ago
|
#include "utilities/transactions/lock/lock_manager.h"
|
||
|
#include "utilities/transactions/lock/point/point_lock_tracker.h"
|
||
10 years ago
|
|
||
5 years ago
|
namespace ROCKSDB_NAMESPACE {
|
||
10 years ago
|
|
||
|
class ColumnFamilyHandle;
|
||
|
struct LockInfo;
|
||
|
struct LockMap;
|
||
|
struct LockMapStripe;
|
||
|
|
||
7 years ago
|
struct DeadlockInfoBuffer {
|
||
|
private:
|
||
|
std::vector<DeadlockPath> paths_buffer_;
|
||
|
uint32_t buffer_idx_;
|
||
|
std::mutex paths_buffer_mutex_;
|
||
|
std::vector<DeadlockPath> Normalize();
|
||
|
|
||
|
public:
|
||
|
explicit DeadlockInfoBuffer(uint32_t n_latest_dlocks)
|
||
|
: paths_buffer_(n_latest_dlocks), buffer_idx_(0) {}
|
||
|
void AddNewPath(DeadlockPath path);
|
||
|
void Resize(uint32_t target_size);
|
||
|
std::vector<DeadlockPath> PrepareBuffer();
|
||
|
};
|
||
|
|
||
|
struct TrackedTrxInfo {
|
||
|
autovector<TransactionID> m_neighbors;
|
||
|
uint32_t m_cf_id;
|
||
|
bool m_exclusive;
|
||
6 years ago
|
std::string m_waiting_key;
|
||
7 years ago
|
};
|
||
|
|
||
4 years ago
|
class PointLockManager : public LockManager {
|
||
10 years ago
|
public:
|
||
4 years ago
|
PointLockManager(PessimisticTransactionDB* db,
|
||
|
const TransactionDBOptions& opt);
|
||
5 years ago
|
// No copying allowed
|
||
4 years ago
|
PointLockManager(const PointLockManager&) = delete;
|
||
|
PointLockManager& operator=(const PointLockManager&) = delete;
|
||
|
|
||
|
~PointLockManager() override;
|
||
|
|
||
|
bool IsPointLockSupported() const override { return true; }
|
||
|
|
||
|
bool IsRangeLockSupported() const override { return false; }
|
||
|
|
||
|
const LockTrackerFactory& GetLockTrackerFactory() const override {
|
||
|
return PointLockTrackerFactory::Get();
|
||
|
}
|
||
|
|
||
|
void AddColumnFamily(const ColumnFamilyHandle* cf) override;
|
||
|
void RemoveColumnFamily(const ColumnFamilyHandle* cf) override;
|
||
10 years ago
|
|
||
4 years ago
|
Status TryLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
|
||
|
const std::string& key, Env* env, bool exclusive) override;
|
||
|
Status TryLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
|
||
|
const Endpoint& start, const Endpoint& end, Env* env,
|
||
|
bool exclusive) override;
|
||
10 years ago
|
|
||
4 years ago
|
void UnLock(PessimisticTransaction* txn, const LockTracker& tracker,
|
||
|
Env* env) override;
|
||
|
void UnLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
|
||
|
const std::string& key, Env* env) override;
|
||
|
void UnLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
|
||
|
const Endpoint& start, const Endpoint& end, Env* env) override;
|
||
10 years ago
|
|
||
4 years ago
|
PointLockStatus GetPointLockStatus() override;
|
||
10 years ago
|
|
||
4 years ago
|
RangeLockStatus GetRangeLockStatus() override;
|
||
10 years ago
|
|
||
4 years ago
|
std::vector<DeadlockPath> GetDeadlockInfoBuffer() override;
|
||
10 years ago
|
|
||
4 years ago
|
void Resize(uint32_t new_size) override;
|
||
8 years ago
|
|
||
10 years ago
|
private:
|
||
7 years ago
|
PessimisticTransactionDB* txn_db_impl_;
|
||
9 years ago
|
|
||
10 years ago
|
// Default number of lock map stripes per column family
|
||
|
const size_t default_num_stripes_;
|
||
|
|
||
|
// Limit on number of keys locked per column family
|
||
|
const int64_t max_num_locks_;
|
||
|
|
||
8 years ago
|
// The following lock order must be satisfied in order to avoid deadlocking
|
||
|
// ourselves.
|
||
|
// - lock_map_mutex_
|
||
|
// - stripe mutexes in ascending cf id, ascending stripe order
|
||
|
// - wait_txn_map_mutex_
|
||
|
//
|
||
|
// Must be held when accessing/modifying lock_maps_.
|
||
10 years ago
|
InstrumentedMutex lock_map_mutex_;
|
||
|
|
||
|
// Map of ColumnFamilyId to locked key info
|
||
|
using LockMaps = std::unordered_map<uint32_t, std::shared_ptr<LockMap>>;
|
||
|
LockMaps lock_maps_;
|
||
|
|
||
|
// Thread-local cache of entries in lock_maps_. This is an optimization
|
||
|
// to avoid acquiring a mutex in order to look up a LockMap
|
||
|
std::unique_ptr<ThreadLocalPtr> lock_maps_cache_;
|
||
|
|
||
8 years ago
|
// Must be held when modifying wait_txn_map_ and rev_wait_txn_map_.
|
||
|
std::mutex wait_txn_map_mutex_;
|
||
|
|
||
|
// Maps from waitee -> number of waiters.
|
||
8 years ago
|
HashMap<TransactionID, int> rev_wait_txn_map_;
|
||
8 years ago
|
// Maps from waiter -> waitee.
|
||
7 years ago
|
HashMap<TransactionID, TrackedTrxInfo> wait_txn_map_;
|
||
|
DeadlockInfoBuffer dlock_buffer_;
|
||
8 years ago
|
|
||
|
// Used to allocate mutexes/condvars to use when locking keys
|
||
|
std::shared_ptr<TransactionDBMutexFactory> mutex_factory_;
|
||
|
|
||
8 years ago
|
bool IsLockExpired(TransactionID txn_id, const LockInfo& lock_info, Env* env,
|
||
|
uint64_t* wait_time);
|
||
10 years ago
|
|
||
|
std::shared_ptr<LockMap> GetLockMap(uint32_t column_family_id);
|
||
|
|
||
7 years ago
|
Status AcquireWithTimeout(PessimisticTransaction* txn, LockMap* lock_map,
|
||
8 years ago
|
LockMapStripe* stripe, uint32_t column_family_id,
|
||
10 years ago
|
const std::string& key, Env* env, int64_t timeout,
|
||
6 years ago
|
LockInfo&& lock_info);
|
||
10 years ago
|
|
||
|
Status AcquireLocked(LockMap* lock_map, LockMapStripe* stripe,
|
||
|
const std::string& key, Env* env,
|
||
6 years ago
|
LockInfo&& lock_info, uint64_t* wait_time,
|
||
8 years ago
|
autovector<TransactionID>* txn_ids);
|
||
10 years ago
|
|
||
4 years ago
|
void UnLockKey(PessimisticTransaction* txn, const std::string& key,
|
||
8 years ago
|
LockMapStripe* stripe, LockMap* lock_map, Env* env);
|
||
|
|
||
7 years ago
|
bool IncrementWaiters(const PessimisticTransaction* txn,
|
||
7 years ago
|
const autovector<TransactionID>& wait_ids,
|
||
|
const std::string& key, const uint32_t& cf_id,
|
||
6 years ago
|
const bool& exclusive, Env* const env);
|
||
7 years ago
|
void DecrementWaiters(const PessimisticTransaction* txn,
|
||
8 years ago
|
const autovector<TransactionID>& wait_ids);
|
||
7 years ago
|
void DecrementWaitersImpl(const PessimisticTransaction* txn,
|
||
8 years ago
|
const autovector<TransactionID>& wait_ids);
|
||
10 years ago
|
};
|
||
|
|
||
5 years ago
|
} // namespace ROCKSDB_NAMESPACE
|
||
10 years ago
|
#endif // ROCKSDB_LITE
|