From 116425fc993d0dc0185305dc392c8f6fb90e1b63 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 20 Apr 2017 17:11:56 +0200 Subject: [PATCH] F #4809: Template for the LogDBManager --- include/LogDBManager.h | 78 +++++++++++++--------- src/logdb/LogDBManager.cc | 137 ++++++++++++++++---------------------- src/logdb/SConstruct | 3 +- 3 files changed, 105 insertions(+), 113 deletions(-) diff --git a/include/LogDBManager.h b/include/LogDBManager.h index 06b666222b..8ee761ed60 100644 --- a/include/LogDBManager.h +++ b/include/LogDBManager.h @@ -18,11 +18,12 @@ #define LOG_DB_MANAGER_H_ #include "ActionManager.h" -#include "LogDBRecord.h" #include "ZoneServer.h" extern "C" void * logdb_manager_loop(void *arg); +extern "C" void * replication_thread(void *arg); + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -31,35 +32,29 @@ class LogDBAction : public ActionRequest public: enum Actions { - NEW_LOGDB_RECORD, + START, + STOP, + REPLICATE, DELETE_SERVER - } + }; - LogDBAction(Actions a, LogDBRequest * r):ActionRequest(ActionRequest::USER), - _action(a), _request(r){}; + LogDBAction(Actions a):ActionRequest(ActionRequest::USER), _action(a){}; LogDBAction(const LogDBAction& o):ActionRequest(o._type), - _action(o._action), _request(o._request){}; + _action(o._action){}; Actions action() const { return _action; } - LogDBRequest * request() const - { - return _request; - } - ActionRequest * clone() const { return new LogDBAction(*this); } private: - Action _action; - - LogDBRequest * _request; + Actions _action; }; // ----------------------------------------------------------------------------- @@ -68,17 +63,30 @@ private: class LogDBManager : public ActionListener { private: - class LogDBManagerThread + + friend void * logdb_manager_loop(void *arg); + + friend void * replication_thread(void *arg); + + /** + * Event engine for the LogDBManager + */ + ActionManager am; + + // ------------------------------------------------------------------------- + // Replication thread class + // ------------------------------------------------------------------------- + class ReplicaThread { public: - LogDBManagerThread(ZoneServer * z):replicate(false), zserver(z) + ReplicaThread(ZoneServer * z):zserver(z) { pthread_mutex_init(&mutex, 0); pthread_cond_init(&cond, 0); }; - virtual ~LogDBManagerThread(){}; + virtual ~ReplicaThread(){}; void do_replication(); @@ -89,27 +97,35 @@ private: pthread_cond_t cond; - bool replicate; - ZoneServer * zserver; - } - - /** - * LogDB records being replicated on followers - */ - std::map log; + }; // ------------------------------------------------------------------------- // Action Listener interface // ------------------------------------------------------------------------- - void finalize_action(const ActionRequest& ar) - { - NebulaLog::log("DBM",Log::INFO,"Stopping LogDB Manager..."); - }; + void finalize_action(const ActionRequest& ar); + /** + * Start the replication threads, one for each server in the zone + */ + void start(const ActionRequest& ar); + + /** + * Stop the replication threads (leader becomes follower) + */ + void stop(const ActionRequest& ar); + + /** + * Notify threads there is a new log entry to replicate on followers + */ + void replicate(const ActionRequest& ar); + + /** + * Event dispatcher function + */ void user_action(const ActionRequest& ar); -} +}; -#endif /*LOG_DB_H_*/ +#endif /*LOG_DB_MANAGER_H_*/ diff --git a/src/logdb/LogDBManager.cc b/src/logdb/LogDBManager.cc index 06b666222b..e5725503f4 100644 --- a/src/logdb/LogDBManager.cc +++ b/src/logdb/LogDBManager.cc @@ -14,102 +14,77 @@ /* limitations under the License. */ /* -------------------------------------------------------------------------- */ -#ifndef LOG_DB_MANAGER_H_ -#define LOG_DB_MANAGER_H_ +#include "LogDBManager.h" +#include "Nebula.h" +#include "NebulaLog.h" -#include "ActionManager.h" -#include "LogDBRecord.h" -#include "ZoneServer.h" - -extern "C" void * logdb_manager_loop(void *arg); - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -class LogDBAction : public ActionRequest +// ---------------------------------------------------------------------------- +// Thread wrapper functions +// ---------------------------------------------------------------------------- +extern "C" void * logdb_manager_loop(void *arg) { -public: - enum Actions + LogDBManager * dbm; + + if ( arg == 0 ) { - NEW_LOGDB_RECORD, - DELETE_SERVER + return 0; } - LogDBAction(Actions a, LogDBRequest * r):ActionRequest(ActionRequest::USER), - _action(a), _request(r){}; + dbm = static_cast(arg); - LogDBAction(const LogDBAction& o):ActionRequest(o._type), - _action(o._action), _request(o._request){}; + NebulaLog::log("DBM",Log::INFO,"LogDB Replication Manager started."); - Actions action() const + dbm->am.loop(); + + NebulaLog::log("DBM",Log::INFO,"LogDB Replication Manager stopped."); + + return 0; +} + +extern "C" void * replication_thread(void *arg) +{ + LogDBManager::ReplicaThread * rt; + + if ( arg == 0 ) { - return _action; + return 0; } - LogDBRequest * request() const - { - return _request; - } + rt = static_cast(arg); - ActionRequest * clone() const - { - return new LogDBAction(*this); - } + rt->do_replication(); -private: - Action _action; + return 0; +} + +// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- + +void LogDBManager::finalize_action(const ActionRequest& ar) +{ + NebulaLog::log("DBM",Log::INFO,"Stopping LogDB Manager..."); +}; + +void LogDBManager::user_action(const ActionRequest& ar) +{ + +}; + +void LogDBManager::start(const ActionRequest& ar) +{ + +}; + +void LogDBManager::stop(const ActionRequest& ar) +{ + +}; + +void LogDBManager::replicate(const ActionRequest& ar) +{ - LogDBRequest * _request; }; // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- -class LogDBManager : public ActionListener -{ -private: - class LogDBManagerThread - { - public: - LogDBManagerThread(ZoneServer * z):replicate(false), zserver(z) - { - pthread_mutex_init(&mutex, 0); - - pthread_cond_init(&cond, 0); - }; - - virtual ~LogDBManagerThread(){}; - - void do_replication(); - - private: - pthread_t thread_id; - - pthread_mutex_t mutex; - - pthread_cond_t cond; - - bool replicate; - - ZoneServer * zserver; - } - - /** - * LogDB records being replicated on followers - */ - std::map log; - - // ------------------------------------------------------------------------- - // Action Listener interface - // ------------------------------------------------------------------------- - void finalize_action(const ActionRequest& ar) - { - NebulaLog::log("DBM",Log::INFO,"Stopping LogDB Manager..."); - }; - - void user_action(const ActionRequest& ar); -} - - -#endif /*LOG_DB_H_*/ - diff --git a/src/logdb/SConstruct b/src/logdb/SConstruct index 9c42e6cc35..8cddaa2331 100644 --- a/src/logdb/SConstruct +++ b/src/logdb/SConstruct @@ -23,7 +23,8 @@ lib_name='nebula_logdb' # Sources to generate the library source_files=[ 'LogDB.cc', - 'LogDBRequest.cc' + 'LogDBRequest.cc', + 'LogDBManager.cc' ] # Build library