1
0
Fork 0

introduced global server state

This commit is contained in:
Jan Steemann 2013-12-06 11:49:26 +01:00
parent 0dfe23fb52
commit b7dee51516
10 changed files with 385 additions and 175 deletions

View File

@ -76,8 +76,8 @@ bin_arangod_SOURCES = \
arangod/RestServer/arango.cpp \
arangod/Sharding/AgencyComm.cpp \
arangod/Sharding/ApplicationSharding.cpp \
arangod/Sharding/server-state.cpp \
arangod/Sharding/HeartbeatThread.cpp \
arangod/Sharding/ServerState.cpp \
arangod/SkipLists/skiplistIndex.c \
arangod/Utils/DocumentHelper.cpp \
arangod/V8Server/ApplicationV8.cpp \

View File

@ -110,6 +110,23 @@ void AgencyComm::setPrefix (std::string const& prefix) {
_prefix = prefix;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief generate a timestamp
////////////////////////////////////////////////////////////////////////////////
std::string AgencyComm::generateStamp () {
time_t tt = time(0);
struct tm tb;
char buffer[21];
// TODO: optimise this
TRI_gmtime(tt, &tb);
size_t len = ::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &tb);
return std::string(buffer, len);
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
@ -152,6 +169,8 @@ int AgencyComm::removeAgent (Agent agent) {
int AgencyComm::setValue (std::string const& key,
std::string const& value) {
// LOG_INFO("SETTING VALUE: %s TO %s", key.c_str(), value.c_str());
return 0;
}

View File

@ -87,7 +87,13 @@ namespace triagens {
/// @brief sets the global prefix for all operations
////////////////////////////////////////////////////////////////////////////////
void setPrefix (std::string const&);
static void setPrefix (std::string const&);
////////////////////////////////////////////////////////////////////////////////
/// @brief generate a timestamp
////////////////////////////////////////////////////////////////////////////////
static std::string generateStamp ();
// -----------------------------------------------------------------------------
// --SECTION-- public methods

View File

@ -28,6 +28,7 @@
#include "ApplicationSharding.h"
#include "Rest/Endpoint.h"
#include "Sharding/HeartbeatThread.h"
#include "Sharding/ServerState.h"
#include "BasicsC/logging.h"
using namespace triagens;
@ -90,26 +91,39 @@ void ApplicationSharding::setupOptions (map<string, basics::ProgramOptionsDescri
////////////////////////////////////////////////////////////////////////////////
bool ApplicationSharding::prepare () {
if (! _agencyPrefix.empty()) {
_enableCluster = true;
_enableCluster = (_agencyEndpoints.size() > 0 || ! _agencyPrefix.empty());
size_t found = _agencyPrefix.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if (! enabled()) {
return true;
}
if (found != std::string::npos) {
LOG_FATAL_AND_EXIT("invalid value specified for --cluster.agency-prefix");
// validate --cluster.agency-prefix
size_t found = _agencyPrefix.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if (_agencyPrefix.empty() || found != std::string::npos) {
LOG_FATAL_AND_EXIT("invalid value specified for --cluster.agency-prefix");
}
// register the prefix with the communicator
AgencyComm::setPrefix(_agencyPrefix);
// validate --cluster.agency-endpoint
if (_agencyEndpoints.size() == 0) {
LOG_FATAL_AND_EXIT("must at least specify one endpoint in --cluster.agency-endpoint");
}
for (size_t i = 0; i < _agencyEndpoints.size(); ++i) {
const string unified = triagens::rest::Endpoint::getUnifiedForm(_agencyEndpoints[i]);
if (unified.empty()) {
LOG_FATAL_AND_EXIT("invalid endpoint '%s' specified for --cluster.agency-endpoint", _agencyEndpoints[i].c_str());
}
}
if (_agencyEndpoints.size() > 0) {
_enableCluster = true;
for (size_t i = 0; i < _agencyEndpoints.size(); ++i) {
const string unified = triagens::rest::Endpoint::getUnifiedForm(_agencyEndpoints[i]);
if (unified.empty()) {
LOG_FATAL_AND_EXIT("invalid endpoint '%s' specified for --cluster.agency-endpoint", _agencyEndpoints[i].c_str());
}
}
// validate --cluster.my-id
if (_myId.empty()) {
LOG_FATAL_AND_EXIT("invalid value specified for --cluster.my-id");
}
return true;
@ -126,7 +140,9 @@ bool ApplicationSharding::start () {
LOG_INFO("using clustering");
_heartbeat = new HeartbeatThread();
ServerState::instance()->setCurrent(ServerState::STATE_STARTUP);
_heartbeat = new HeartbeatThread(_myId);
if (_heartbeat == 0) {
LOG_FATAL_AND_EXIT("unable to start cluster heartbeat thread");

View File

@ -28,6 +28,7 @@
#include "HeartbeatThread.h"
#include "Basics/ConditionLocker.h"
#include "BasicsC/logging.h"
#include "Sharding/ServerState.h"
using namespace triagens::basics;
using namespace triagens::arango;
@ -44,10 +45,11 @@ using namespace triagens::arango;
/// @brief constructs a heartbeat thread
////////////////////////////////////////////////////////////////////////////////
HeartbeatThread::HeartbeatThread ()
HeartbeatThread::HeartbeatThread (std::string const& myId)
: Thread("heartbeat"),
_agency(),
_condition(),
_myId(myId),
_interval(1000 * 1000),
_stop(0) {
@ -77,15 +79,32 @@ void HeartbeatThread::run () {
while (! _stop) {
LOG_TRACE("sending heartbeat");
sendState();
CONDITION_LOCKER(guard, _condition);
guard.wait(_interval);
}
LOG_TRACE("stopping heartbeat thread");
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief heartbeat main loop
////////////////////////////////////////////////////////////////////////////////
bool HeartbeatThread::sendState () {
// TODO: handle return value
_agency.setValue("/state/servers/state/" + _myId,
ServerState::stateToString(ServerState::instance()->getCurrent()) + ";" + AgencyComm::generateStamp());
return true;
}
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"

View File

@ -61,7 +61,7 @@ namespace triagens {
/// @brief constructs a heartbeat thread
////////////////////////////////////////////////////////////////////////////////
HeartbeatThread ();
HeartbeatThread (std::string const&);
////////////////////////////////////////////////////////////////////////////////
/// @brief destroys a heartbeat thread
@ -92,6 +92,18 @@ namespace triagens {
void run ();
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief send the state to the agency
////////////////////////////////////////////////////////////////////////////////
bool sendState ();
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
@ -110,6 +122,12 @@ namespace triagens {
triagens::basics::ConditionVariable _condition;
////////////////////////////////////////////////////////////////////////////////
/// @brief this server's id
////////////////////////////////////////////////////////////////////////////////
const std::string _myId;
////////////////////////////////////////////////////////////////////////////////
/// @brief heartbeat interval
////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,132 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief single-server state
///
/// @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
////////////////////////////////////////////////////////////////////////////////
#include "ServerState.h"
#include "Basics/ReadLocker.h"
#include "Basics/WriteLocker.h"
using namespace triagens::arango;
// -----------------------------------------------------------------------------
// --SECTION-- static variables
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief server state instance
////////////////////////////////////////////////////////////////////////////////
static ServerState* Instance = 0;
// -----------------------------------------------------------------------------
// --SECTION-- ServerState
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor
////////////////////////////////////////////////////////////////////////////////
ServerState::ServerState ()
: _lock(),
_state(STATE_OFFLINE) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destructor
////////////////////////////////////////////////////////////////////////////////
ServerState::~ServerState () {
}
// -----------------------------------------------------------------------------
// --SECTION-- public static methods
// -----------------------------------------------------------------------------
ServerState* ServerState::instance () {
if (Instance == 0) {
Instance = new ServerState();
}
return Instance;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the string representation of a state
////////////////////////////////////////////////////////////////////////////////
std::string ServerState::stateToString (StateEnum state) {
switch (state) {
case STATE_OFFLINE:
return "OFFLINE";
case STATE_STARTUP:
return "STARTUP";
case STATE_CONNECTED:
return "CONNECTED";
case STATE_STOPPING:
return "STOPPING";
case STATE_STOPPED:
return "STOPPED";
case STATE_PROBLEM:
return "PROBLEM";
case STATE_RECOVERING:
return "RECOVERING";
case STATE_RECOVERED:
return "RECOVERED";
case STATE_SHUTDOWN:
return "SHUTDOWN";
}
assert(false);
return "";
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief get the current state
////////////////////////////////////////////////////////////////////////////////
ServerState::StateEnum ServerState::getCurrent () {
READ_LOCKER(_lock);
return _state;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief set the current state
////////////////////////////////////////////////////////////////////////////////
void ServerState::setCurrent (StateEnum state) {
WRITE_LOCKER(_lock);
_state = state;
}
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End:

View File

@ -0,0 +1,154 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief single-server state
///
/// @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_SHARDING_SERVER_STATE_H
#define TRIAGENS_SHARDING_SERVER_STATE_H 1
#include "Basics/Common.h"
#include "Basics/ReadWriteLock.h"
#ifdef __cplusplus
extern "C" {
#endif
namespace triagens {
namespace arango {
// -----------------------------------------------------------------------------
// --SECTION-- ServerState
// -----------------------------------------------------------------------------
class ServerState {
// -----------------------------------------------------------------------------
// --SECTION-- public types
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief an enum describing the possible states a server can have
////////////////////////////////////////////////////////////////////////////////
typedef enum {
STATE_OFFLINE = 0,
STATE_STARTUP = 1,
STATE_CONNECTED = 2,
STATE_STOPPING = 3,
STATE_STOPPED = 4,
STATE_PROBLEM = 5,
STATE_RECOVERING = 6,
STATE_RECOVERED = 7,
STATE_SHUTDOWN = 8
}
StateEnum;
// -----------------------------------------------------------------------------
// --SECTION-- constructors / destructors
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief constructor
////////////////////////////////////////////////////////////////////////////////
ServerState ();
////////////////////////////////////////////////////////////////////////////////
/// @brief destructor
////////////////////////////////////////////////////////////////////////////////
~ServerState ();
// -----------------------------------------------------------------------------
// --SECTION-- public static methods
// -----------------------------------------------------------------------------
public:
static ServerState* instance ();
////////////////////////////////////////////////////////////////////////////////
/// @brief get the string representation of a state
////////////////////////////////////////////////////////////////////////////////
static std::string stateToString (StateEnum);
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief get the current state
////////////////////////////////////////////////////////////////////////////////
StateEnum getCurrent ();
////////////////////////////////////////////////////////////////////////////////
/// @brief set the current state
////////////////////////////////////////////////////////////////////////////////
void setCurrent (StateEnum);
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief r/w lock for state
////////////////////////////////////////////////////////////////////////////////
triagens::basics::ReadWriteLock _lock;
////////////////////////////////////////////////////////////////////////////////
/// @brief the current state
////////////////////////////////////////////////////////////////////////////////
StateEnum _state;
};
}
}
#ifdef __cplusplus
}
#endif
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End:

View File

@ -1,70 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief single-server states
///
/// @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
////////////////////////////////////////////////////////////////////////////////
#include "server-state.h"
using namespace triagens::arango::serverstate;
// -----------------------------------------------------------------------------
// --SECTION-- server states
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief get the string representation of a state
////////////////////////////////////////////////////////////////////////////////
std::string stateToString (StateEnum state) {
switch (state) {
case STATE_OFFLINE:
return "offline";
case STATE_STARTUP:
return "startup";
case STATE_CONNECTED:
return "connected";
case STATE_STOPPING:
return "stopping";
case STATE_STOPPED:
return "stopped";
case STATE_PROBLEM:
return "problem";
case STATE_RECOVERING:
return "recovering";
case STATE_RECOVERED:
return "recovered";
case STATE_SHUTDOWN:
return "shutdown";
}
assert(false);
return "";
}
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End:

View File

@ -1,84 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief single-server states
///
/// @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_SHARDING_SERVER_STATE_H
#define TRIAGENS_SHARDING_SERVER_STATE_H 1
#include "Basics/Common.h"
#ifdef __cplusplus
extern "C" {
#endif
namespace triagens {
namespace arango {
// -----------------------------------------------------------------------------
// --SECTION-- server states
// -----------------------------------------------------------------------------
namespace serverstate {
////////////////////////////////////////////////////////////////////////////////
/// @brief an enum describing the possible states a server can have
////////////////////////////////////////////////////////////////////////////////
enum StateEnum {
STATE_OFFLINE = 0,
STATE_STARTUP = 1,
STATE_CONNECTED = 2,
STATE_STOPPING = 3,
STATE_STOPPED = 4,
STATE_PROBLEM = 5,
STATE_RECOVERING = 6,
STATE_RECOVERED = 7,
STATE_SHUTDOWN = 8
};
////////////////////////////////////////////////////////////////////////////////
/// @brief get the string representation of a state
////////////////////////////////////////////////////////////////////////////////
std::string stateToString (StateEnum);
}
}
}
#ifdef __cplusplus
}
#endif
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End: