//////////////////////////////////////////////////////////////////////////////// /// @brief heartbeat thread /// /// @file /// /// DISCLAIMER /// /// Copyright 2010-2012 triagens GmbH, Cologne, Germany /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is triAGENS GmbH, Cologne, Germany /// /// @author Jan Steemann /// @author Copyright 2013, triagens GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// #ifndef TRIAGENS_CLUSTER_HEARTBEAT_THREAD_H #define TRIAGENS_CLUSTER_HEARTBEAT_THREAD_H 1 #include "Basics/Common.h" #include "Basics/ConditionVariable.h" #include "Basics/Thread.h" #include "BasicsC/logging.h" #include "Cluster/AgencyComm.h" extern "C" { struct TRI_server_s; } namespace triagens { namespace rest { class ApplicationDispatcher; } namespace arango { class ApplicationV8; // ----------------------------------------------------------------------------- // --SECTION-- HeartbeatThread // ----------------------------------------------------------------------------- class HeartbeatThread : public basics::Thread { // ----------------------------------------------------------------------------- // --SECTION-- constructors and destructors // ----------------------------------------------------------------------------- private: HeartbeatThread (HeartbeatThread const&); HeartbeatThread& operator= (HeartbeatThread const&); public: //////////////////////////////////////////////////////////////////////////////// /// @brief constructs a heartbeat thread //////////////////////////////////////////////////////////////////////////////// HeartbeatThread (struct TRI_server_s*, triagens::rest::ApplicationDispatcher*, ApplicationV8*, uint64_t, uint64_t); //////////////////////////////////////////////////////////////////////////////// /// @brief destroys a heartbeat thread //////////////////////////////////////////////////////////////////////////////// ~HeartbeatThread (); // ----------------------------------------------------------------------------- // --SECTION-- public methods // ----------------------------------------------------------------------------- public: //////////////////////////////////////////////////////////////////////////////// /// @brief initialises the heartbeat //////////////////////////////////////////////////////////////////////////////// bool init (); //////////////////////////////////////////////////////////////////////////////// /// @brief stops the heartbeat //////////////////////////////////////////////////////////////////////////////// void stop () { if (_stop > 0) { return; } LOG_TRACE("stopping heartbeat thread"); _stop = 1; _condition.signal(); while (_stop != 2) { usleep(1000); } } //////////////////////////////////////////////////////////////////////////////// /// @brief sets the ready flag //////////////////////////////////////////////////////////////////////////////// void ready (bool value) { if (value) { _ready = 1; } else { _ready = 0; } } //////////////////////////////////////////////////////////////////////////////// /// @brief fetches the ready flag //////////////////////////////////////////////////////////////////////////////// bool ready () const { return _ready > 0; } // ----------------------------------------------------------------------------- // --SECTION-- Thread methods // ----------------------------------------------------------------------------- protected: //////////////////////////////////////////////////////////////////////////////// /// @brief heartbeat main loop //////////////////////////////////////////////////////////////////////////////// void run (); // ----------------------------------------------------------------------------- // --SECTION-- private methods // ----------------------------------------------------------------------------- private: //////////////////////////////////////////////////////////////////////////////// /// @brief handles a plan change, coordinator case //////////////////////////////////////////////////////////////////////////////// bool handlePlanChangeCoordinator (uint64_t, uint64_t&); //////////////////////////////////////////////////////////////////////////////// /// @brief handles a plan change, DBServer case //////////////////////////////////////////////////////////////////////////////// bool handlePlanChangeDBServer (uint64_t, uint64_t&); //////////////////////////////////////////////////////////////////////////////// /// @brief handles a state change //////////////////////////////////////////////////////////////////////////////// bool handleStateChange (AgencyCommResult&, uint64_t&); //////////////////////////////////////////////////////////////////////////////// /// @brief fetch the last value of Sync/Commands/my-id from the agency //////////////////////////////////////////////////////////////////////////////// uint64_t getLastCommandIndex (); //////////////////////////////////////////////////////////////////////////////// /// @brief sends the current server's state to the agency //////////////////////////////////////////////////////////////////////////////// bool sendState (); // ----------------------------------------------------------------------------- // --SECTION-- private variables // ----------------------------------------------------------------------------- private: //////////////////////////////////////////////////////////////////////////////// /// @brief server //////////////////////////////////////////////////////////////////////////////// struct TRI_server_s* _server; //////////////////////////////////////////////////////////////////////////////// /// @brief Job dispatcher //////////////////////////////////////////////////////////////////////////////// triagens::rest::ApplicationDispatcher* _dispatcher; //////////////////////////////////////////////////////////////////////////////// /// @brief v8 dispatcher //////////////////////////////////////////////////////////////////////////////// ApplicationV8* _applicationV8; //////////////////////////////////////////////////////////////////////////////// /// @brief AgencyComm instance //////////////////////////////////////////////////////////////////////////////// AgencyComm _agency; //////////////////////////////////////////////////////////////////////////////// /// @brief condition variable for heartbeat //////////////////////////////////////////////////////////////////////////////// triagens::basics::ConditionVariable _condition; //////////////////////////////////////////////////////////////////////////////// /// @brief this server's id //////////////////////////////////////////////////////////////////////////////// const std::string _myId; //////////////////////////////////////////////////////////////////////////////// /// @brief heartbeat interval //////////////////////////////////////////////////////////////////////////////// uint64_t _interval; //////////////////////////////////////////////////////////////////////////////// /// @brief number of fails in a row before a warning is issued //////////////////////////////////////////////////////////////////////////////// uint64_t _maxFailsBeforeWarning; //////////////////////////////////////////////////////////////////////////////// /// @brief current number of fails in a row //////////////////////////////////////////////////////////////////////////////// uint64_t _numFails; //////////////////////////////////////////////////////////////////////////////// /// @brief stop flag //////////////////////////////////////////////////////////////////////////////// volatile sig_atomic_t _stop; //////////////////////////////////////////////////////////////////////////////// /// @brief whether or not the thread is ready //////////////////////////////////////////////////////////////////////////////// volatile sig_atomic_t _ready; }; } } #endif // Local Variables: // mode: outline-minor // outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)" // End: