diff --git a/arangod/Agency/Agent.cpp b/arangod/Agency/Agent.cpp index fb51f353bb..4b012d3070 100644 --- a/arangod/Agency/Agent.cpp +++ b/arangod/Agency/Agent.cpp @@ -253,11 +253,20 @@ bool Agent::recvAppendEntriesRPC( MUTEX_LOCKER(mutexLocker, _ioLock); - if (this->term() > term) { - LOG_TOPIC(WARN, Logger::AGENCY) << "I have a higher term than RPC caller."; + if (this->term() > term) { // peer at higher term + if (leaderCommitIndex >= _lastCommitIndex) { // + _constituent.follow(term); + } else { + LOG_TOPIC(WARN, Logger::AGENCY) << "I have a higher term than RPC caller."; + return false; + } + } + + if (!_constituent.vote(term, leaderId, prevIndex, prevTerm, true)) { + LOG_TOPIC(WARN, Logger::AGENCY) << "Not voting for " << leaderId; return false; } - + if (!_constituent.vote(term, leaderId, prevIndex, prevTerm, true)) { return false; } diff --git a/arangod/Agency/v8-agency.cpp b/arangod/Agency/v8-agency.cpp new file mode 100644 index 0000000000..8f6437d9ec --- /dev/null +++ b/arangod/Agency/v8-agency.cpp @@ -0,0 +1,118 @@ +//////////////////////////////////////////////////////////////////////////////// +/// DISCLAIMER +/// +/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany +/// Copyright 2004-2014 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 ArangoDB GmbH, Cologne, Germany +/// +/// @author Kaveh Vahedipour +//////////////////////////////////////////////////////////////////////////////// + +#include "v8-agency.h" + +#include "Agency/AgencyFeature.h" +#include "Agency/Agent.h" +#include "ApplicationFeatures/ApplicationServer.h" +#include "V8/v8-buffer.h" +#include "V8/v8-conv.h" +#include "V8/v8-globals.h" +#include "V8/v8-utils.h" +#include "V8/v8-vpack.h" +#include "VocBase/server.h" + +#include +#include + +using namespace arangodb; +using namespace arangodb::application_features; +using namespace arangodb::basics; +using namespace arangodb::consensus; + +static void JS_LeadingVulpes(v8::FunctionCallbackInfo const& args) { + + v8::Isolate* isolate = args.GetIsolate(); + + Agent* agent = nullptr; + try { + AgencyFeature* feature = + ApplicationServer::getEnabledFeature("AgencyFeature"); + agent = feature->agent(); + + } catch (std::exception const& e) { + TRI_V8_THROW_EXCEPTION_MESSAGE( + TRI_ERROR_INTERNAL, + std::string("couldn't access agency feature: ") + e.what()); + } + + v8::Handle r = v8::Object::New(isolate); + + + r->Set(TRI_V8_ASCII_STRING("leading"), + v8::Boolean::New(isolate, agent->leading())); + + TRI_V8_RETURN(r); + + +} + +static void JS_ReadVulpes(v8::FunctionCallbackInfo const& args) { + +} + +static void JS_WriteVulpes(v8::FunctionCallbackInfo const& args) { + +} + + +void TRI_InitV8Agency(v8::Isolate* isolate, v8::Handle context) { + TRI_V8_CURRENT_GLOBALS_AND_SCOPE; + TRI_ASSERT(v8g != nullptr); + + v8::Handle rt; + v8::Handle ft; + + // ........................................................................... + // generate the agency template + // ........................................................................... + + ft = v8::FunctionTemplate::New(isolate); + ft->SetClassName(TRI_V8_ASCII_STRING("ArangoVulpes")); + + rt = ft->InstanceTemplate(); + rt->SetInternalFieldCount(2); + + TRI_AddMethodVocbase( + isolate, rt, TRI_V8_ASCII_STRING("leading"), JS_LeadingVulpes); + TRI_AddMethodVocbase( + isolate, rt, TRI_V8_ASCII_STRING("read"), JS_ReadVulpes); + TRI_AddMethodVocbase( + isolate, rt, TRI_V8_ASCII_STRING("write"), JS_WriteVulpes); + + v8g->VulpesTempl.Reset(isolate, rt); + ft->SetClassName(TRI_V8_ASCII_STRING("ArangoVuplesCtor")); + + TRI_AddGlobalFunctionVocbase( + isolate, context, TRI_V8_ASCII_STRING("ArangoVuplesCtor"), + ft->GetFunction(), true); + + // register the global object + v8::Handle aa = rt->NewInstance(); + if (!aa.IsEmpty()) { + TRI_AddGlobalVariableVocbase( + isolate, context, TRI_V8_ASCII_STRING("ArangoVuples"), aa); + } + +} diff --git a/arangod/Agency/v8-agency.h b/arangod/Agency/v8-agency.h new file mode 100644 index 0000000000..250fdb2bf7 --- /dev/null +++ b/arangod/Agency/v8-agency.h @@ -0,0 +1,37 @@ +//////////////////////////////////////////////////////////////////////////////// +/// DISCLAIMER +/// +/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany +/// Copyright 2004-2014 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 ArangoDB GmbH, Cologne, Germany +/// +/// @author Kaveh Vahedipour +//////////////////////////////////////////////////////////////////////////////// + +#ifndef ARANGOD_CLUSTER_V8_AGENCY_H +#define ARANGOD_CLUSTER_V8_AGENCY_H 1 + +#include "Basics/Common.h" + +#include "v8.h" + +//////////////////////////////////////////////////////////////////////////////// +/// @brief creates a global agency context +//////////////////////////////////////////////////////////////////////////////// + +void TRI_InitV8Agency(v8::Isolate* isolate, v8::Handle); + +#endif diff --git a/arangod/CMakeLists.txt b/arangod/CMakeLists.txt index 452696c201..4e8e06db34 100644 --- a/arangod/CMakeLists.txt +++ b/arangod/CMakeLists.txt @@ -95,6 +95,7 @@ add_executable(${BIN_ARANGOD} Agency/State.cpp Agency/Store.cpp Agency/StoreCallback.cpp + Agency/v8-agency.cpp Aql/Aggregator.cpp Aql/AqlItemBlock.cpp Aql/AqlItemBlockManager.cpp diff --git a/js/client/tests/agency/agency-test.js b/js/client/tests/agency/agency-test.js index 68731f2631..37004772f1 100644 --- a/js/client/tests/agency/agency-test.js +++ b/js/client/tests/agency/agency-test.js @@ -49,7 +49,7 @@ function agencyTestSuite () { var whoseTurn = 0; var request = require("@arangodb/request"); - wait(2); + wait(3.0); function readAgency(list) { // We simply try all agency servers in turn until one gives us an HTTP diff --git a/lib/V8/v8-globals.cpp b/lib/V8/v8-globals.cpp index cc2fb4de73..46bee5e46d 100644 --- a/lib/V8/v8-globals.cpp +++ b/lib/V8/v8-globals.cpp @@ -35,6 +35,7 @@ TRI_v8_global_s::TRI_v8_global_s(v8::Isolate* isolate) VPackTempl(), VocbaseColTempl(), VocbaseTempl(), + VulpesTempl(), BufferTempl(), diff --git a/lib/V8/v8-globals.h b/lib/V8/v8-globals.h index e004c56c08..77ea333a99 100644 --- a/lib/V8/v8-globals.h +++ b/lib/V8/v8-globals.h @@ -517,6 +517,12 @@ typedef struct TRI_v8_global_s { v8::Persistent VocbaseTempl; + ////////////////////////////////////////////////////////////////////////////// + /// @brief vulpes template + ////////////////////////////////////////////////////////////////////////////// + + v8::Persistent VulpesTempl; + ////////////////////////////////////////////////////////////////////////////// /// @brief TRI_vocbase_t template //////////////////////////////////////////////////////////////////////////////