-
Notifications
You must be signed in to change notification settings - Fork 832
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/js to cpp transaction handler (#2985)
* implement transaction handler in c++ * add fake request object * move general code into extra file * improve error messages for RestJobHandler * support cancel operation for async jobs with RestTransactionHandler * fix integration test and add some optional debugging output * fix returning of context * fix error type * fix code in cluster test * remove unused variable
- Loading branch information
Showing
16 changed files
with
816 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 52 additions & 54 deletions
106
UnitTests/HttpInterface/api-transactions-noncluster-spec.rb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 Jan Christoph Uhde | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "Basics/WriteLocker.h" | ||
#include "Basics/ReadLocker.h" | ||
#include "RestTransactionHandler.h" | ||
|
||
#include "ApplicationFeatures/ApplicationServer.h" | ||
#include "VocBase/Methods/Transactions.h" | ||
#include "Rest/HttpRequest.h" | ||
#include "Basics/voc-errors.h" | ||
#include "V8Server/V8Context.h" | ||
#include "V8Server/V8DealerFeature.h" | ||
|
||
#include <velocypack/Builder.h> | ||
#include <velocypack/velocypack-aliases.h> | ||
|
||
using namespace arangodb; | ||
using namespace arangodb::basics; | ||
using namespace arangodb::rest; | ||
|
||
RestTransactionHandler::RestTransactionHandler(GeneralRequest* request, GeneralResponse* response) | ||
: RestVocbaseBaseHandler(request, response) | ||
, _v8Context(nullptr) | ||
, _lock() | ||
{} | ||
|
||
void RestTransactionHandler::returnContext(){ | ||
WRITE_LOCKER(writeLock, _lock); | ||
V8DealerFeature::DEALER->exitContext(_v8Context); | ||
_v8Context = nullptr; | ||
} | ||
|
||
RestStatus RestTransactionHandler::execute() { | ||
if (_request->requestType() != rest::RequestType::POST) { | ||
generateError(rest::ResponseCode::METHOD_NOT_ALLOWED, 405); | ||
return RestStatus::DONE; | ||
} | ||
|
||
auto slice = _request->payload(); | ||
if(!slice.isObject()){ | ||
generateError(GeneralResponse::responseCode(TRI_ERROR_BAD_PARAMETER),TRI_ERROR_BAD_PARAMETER, "could not acquire v8 context"); | ||
return RestStatus::DONE; | ||
} | ||
|
||
std::string portType = _request->connectionInfo().portType(); | ||
|
||
_v8Context = V8DealerFeature::DEALER->enterContext(_vocbase, true /*allow use database*/); | ||
if (!_v8Context) { | ||
generateError(GeneralResponse::responseCode(TRI_ERROR_INTERNAL),TRI_ERROR_INTERNAL, "could not acquire v8 context"); | ||
return RestStatus::DONE; | ||
} | ||
|
||
TRI_DEFER(returnContext()); | ||
|
||
VPackBuilder result; | ||
try { | ||
{ | ||
WRITE_LOCKER(lock, _lock); | ||
if(_canceled){ | ||
generateCanceled(); | ||
return RestStatus::DONE; | ||
} | ||
} | ||
|
||
Result res = executeTransaction(_v8Context->_isolate, _lock, _canceled, slice , portType, result); | ||
|
||
if (res.ok()){ | ||
VPackSlice slice = result.slice(); | ||
if (slice.isNone()) { | ||
generateSuccess(rest::ResponseCode::OK, VPackSlice::nullSlice()); | ||
} else { | ||
generateSuccess(rest::ResponseCode::OK, slice); | ||
} | ||
} else { | ||
generateError(res); | ||
} | ||
} catch (arangodb::basics::Exception const& ex) { | ||
generateError(GeneralResponse::responseCode(ex.code()),ex.code(), ex.what()); | ||
} catch (std::exception const& ex) { | ||
generateError(GeneralResponse::responseCode(TRI_ERROR_INTERNAL), TRI_ERROR_INTERNAL, ex.what()); | ||
} catch (...) { | ||
generateError(GeneralResponse::responseCode(TRI_ERROR_INTERNAL), TRI_ERROR_INTERNAL); | ||
} | ||
|
||
return RestStatus::DONE; | ||
} | ||
|
||
bool RestTransactionHandler::cancel() { | ||
//cancel v8 transaction | ||
WRITE_LOCKER(writeLock, _lock); | ||
_canceled.store(true); | ||
auto isolate = _v8Context->_isolate; | ||
if (!v8::V8::IsExecutionTerminating(isolate)) { | ||
v8::V8::TerminateExecution(isolate); | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/// 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 Jan Christoph Uhde | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#ifndef ARANGOD_REST_HANDLER_REST_TRANSACTION_HANDLER_H | ||
#define ARANGOD_REST_HANDLER_REST_TRANSACTION_HANDLER_H 1 | ||
|
||
#include "Basics/ReadWriteLock.h" | ||
#include "RestHandler/RestVocbaseBaseHandler.h" | ||
|
||
namespace arangodb { | ||
|
||
class V8Context; | ||
|
||
class RestTransactionHandler : public arangodb::RestVocbaseBaseHandler { | ||
V8Context* _v8Context; | ||
basics::ReadWriteLock _lock; | ||
|
||
public: | ||
RestTransactionHandler(GeneralRequest*, GeneralResponse*); | ||
|
||
public: | ||
char const* name() const override final { return "RestTransactionHandler"; } | ||
bool isDirect() const override { return false; } | ||
RestStatus execute() override; | ||
bool cancel() override final; | ||
|
||
private: | ||
void returnContext(); | ||
}; | ||
} | ||
|
||
#endif |
Oops, something went wrong.