2017-11-29 23:03:42 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
2017-12-13 23:11:52 +08:00
|
|
|
#include <mutex>
|
2017-11-29 23:03:42 +08:00
|
|
|
|
2017-12-06 21:12:26 +08:00
|
|
|
#include "communication/messaging/distributed.hpp"
|
|
|
|
#include "communication/rpc/rpc.hpp"
|
2017-11-29 23:03:42 +08:00
|
|
|
#include "data_structures/concurrent/concurrent_map.hpp"
|
2017-12-19 19:40:30 +08:00
|
|
|
#include "io/network/network_endpoint.hpp"
|
2017-11-29 23:03:42 +08:00
|
|
|
#include "transactions/commit_log.hpp"
|
|
|
|
#include "transactions/engine.hpp"
|
|
|
|
#include "transactions/transaction.hpp"
|
|
|
|
|
|
|
|
namespace tx {
|
2017-12-22 21:36:25 +08:00
|
|
|
|
2018-01-10 22:10:22 +08:00
|
|
|
/** Distributed worker transaction engine. Connects to a MasterEngine (single
|
|
|
|
* source of truth) to obtain transactional info. Caches most info locally. */
|
2017-11-29 23:03:42 +08:00
|
|
|
class WorkerEngine : public Engine {
|
|
|
|
public:
|
2017-12-06 21:12:26 +08:00
|
|
|
WorkerEngine(communication::messaging::System &system,
|
2017-12-19 19:40:30 +08:00
|
|
|
const io::network::NetworkEndpoint &endpoint);
|
2017-11-29 23:03:42 +08:00
|
|
|
|
|
|
|
Transaction *LocalBegin(transaction_id_t tx_id);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Local caches.
|
|
|
|
ConcurrentMap<transaction_id_t, Transaction *> active_;
|
2017-12-06 21:12:26 +08:00
|
|
|
std::atomic<transaction_id_t> local_last_{0};
|
2017-11-29 23:03:42 +08:00
|
|
|
// Mutable because just getting info can cause a cache fill.
|
|
|
|
mutable CommitLog clog_;
|
2017-12-06 21:12:26 +08:00
|
|
|
|
|
|
|
// Communication to the transactional master.
|
|
|
|
mutable communication::rpc::Client rpc_client_;
|
2017-11-29 23:03:42 +08:00
|
|
|
};
|
2017-12-22 21:36:25 +08:00
|
|
|
|
2017-11-29 23:03:42 +08:00
|
|
|
} // namespace tx
|