2018-01-10 19:18:03 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
|
2018-01-25 21:28:14 +08:00
|
|
|
#include "communication/rpc/client_pool.hpp"
|
2018-01-24 19:16:14 +08:00
|
|
|
#include "communication/rpc/messages.hpp"
|
|
|
|
#include "communication/rpc/server.hpp"
|
2018-01-10 19:18:03 +08:00
|
|
|
#include "data_structures/concurrent/concurrent_map.hpp"
|
|
|
|
|
|
|
|
namespace database {
|
|
|
|
|
|
|
|
/** A set of counter that are guaranteed to produce unique, consecutive values
|
|
|
|
* on each call. */
|
|
|
|
class Counters {
|
|
|
|
public:
|
|
|
|
virtual ~Counters() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current value of the counter with the given name, and
|
|
|
|
* increments that counter. If the counter with the given name does not exist,
|
|
|
|
* a new counter is created and this function returns 0.
|
|
|
|
*/
|
|
|
|
virtual int64_t Get(const std::string &name) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the counter with the given name to the given value. Returns nothing.
|
|
|
|
* If the counter with the given name does not exist, a new counter is created
|
|
|
|
* and set to the given value.
|
|
|
|
*/
|
|
|
|
virtual void Set(const std::string &name, int64_t values) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Implementation for the single-node memgraph */
|
|
|
|
class SingleNodeCounters : public Counters {
|
|
|
|
public:
|
|
|
|
int64_t Get(const std::string &name) override;
|
|
|
|
void Set(const std::string &name, int64_t value) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ConcurrentMap<std::string, std::atomic<int64_t>> counters_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Implementation for distributed master. */
|
|
|
|
class MasterCounters : public SingleNodeCounters {
|
|
|
|
public:
|
2018-02-23 17:56:56 +08:00
|
|
|
MasterCounters(communication::rpc::Server &server);
|
2018-01-10 19:18:03 +08:00
|
|
|
|
|
|
|
private:
|
2018-02-23 17:56:56 +08:00
|
|
|
communication::rpc::Server &rpc_server_;
|
2018-01-10 19:18:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Implementation for distributed worker. */
|
|
|
|
class WorkerCounters : public Counters {
|
|
|
|
public:
|
2018-02-28 23:02:45 +08:00
|
|
|
WorkerCounters(communication::rpc::ClientPool &master_client_pool);
|
2018-01-10 19:18:03 +08:00
|
|
|
|
|
|
|
int64_t Get(const std::string &name) override;
|
|
|
|
void Set(const std::string &name, int64_t value) override;
|
|
|
|
|
|
|
|
private:
|
2018-02-28 23:02:45 +08:00
|
|
|
communication::rpc::ClientPool &master_client_pool_;
|
2018-01-10 19:18:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace database
|