memgraph/src/transactions/engine_worker.hpp

48 lines
1.6 KiB
C++
Raw Normal View History

#pragma once
#include <atomic>
#include <mutex>
#include "communication/rpc/client_pool.hpp"
#include "data_structures/concurrent/concurrent_map.hpp"
#include "io/network/endpoint.hpp"
#include "transactions/commit_log.hpp"
#include "transactions/engine.hpp"
#include "transactions/transaction.hpp"
namespace tx {
/** Distributed worker transaction engine. Connects to a MasterEngine (single
* source of truth) to obtain transactional info. Caches most info locally. Can
* begin/advance/end transactions on the master. */
class WorkerEngine : public Engine {
public:
WorkerEngine(const io::network::Endpoint &endpoint);
~WorkerEngine();
Transaction *Begin() override;
command_id_t Advance(transaction_id_t id) override;
void Commit(const Transaction &t) override;
void Abort(const Transaction &t) override;
CommitLog::Info Info(transaction_id_t tid) const override;
Snapshot GlobalGcSnapshot() override;
Snapshot GlobalActiveTransactions() override;
bool GlobalIsActive(transaction_id_t tid) const override;
tx::transaction_id_t LocalLast() const override;
void LocalForEachActiveTransaction(
std::function<void(Transaction &)> f) override;
tx::Transaction *RunningTransaction(tx::transaction_id_t tx_id) override;
private:
// Local caches.
ConcurrentMap<transaction_id_t, Transaction *> active_;
std::atomic<transaction_id_t> local_last_{0};
// Mutable because just getting info can cause a cache fill.
mutable CommitLog clog_;
// Communication to the transactional master.
mutable communication::rpc::ClientPool rpc_client_pool_;
};
} // namespace tx