1
0
Fork 0

transactions

This commit is contained in:
Jan Steemann 2014-03-17 15:28:16 +01:00
parent fb3cea6c9b
commit 1651776719
7 changed files with 783 additions and 0 deletions

View File

@ -77,6 +77,9 @@ bin_arangod_SOURCES = \
arangod/RestServer/VocbaseContext.cpp \
arangod/RestServer/arango.cpp \
arangod/SkipLists/skiplistIndex.c \
arangod/Transaction/IdGenerator.cpp \
arangod/Transaction/Manager.cpp \
arangod/Transaction/Transaction.cpp \
arangod/Utils/DocumentHelper.cpp \
arangod/V8Server/ApplicationV8.cpp \
arangod/V8Server/V8PeriodicTask.cpp \

View File

@ -0,0 +1,79 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction id generator
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2013 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 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "IdGenerator.h"
#include "VocBase/server.h"
using namespace triagens::transaction;
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief create an id generator
////////////////////////////////////////////////////////////////////////////////
IdGenerator::IdGenerator (Transaction::IdType id) {
setLastId(id);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy an id generator
////////////////////////////////////////////////////////////////////////////////
IdGenerator::~IdGenerator () {
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief set minimal transaction id
////////////////////////////////////////////////////////////////////////////////
void IdGenerator::setLastId (Transaction::IdType id) {
TRI_UpdateTickServer(static_cast<TRI_voc_tick_t>(id));
}
////////////////////////////////////////////////////////////////////////////////
/// @brief create a transaction id
////////////////////////////////////////////////////////////////////////////////
Transaction::IdType IdGenerator::next () {
return static_cast<Transaction::IdType>(TRI_NewTickServer());
}
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

View File

@ -0,0 +1,91 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction id generator
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2013 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 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_TRANSACTION_IDGENERATOR_H
#define TRIAGENS_TRANSACTION_IDGENERATOR_H 1
#include "Basics/Common.h"
#include "Transaction/Transaction.h"
namespace triagens {
namespace transaction {
// -----------------------------------------------------------------------------
// --SECTION-- class IdGenerator
// -----------------------------------------------------------------------------
class IdGenerator {
////////////////////////////////////////////////////////////////////////////////
/// @brief Transaction
////////////////////////////////////////////////////////////////////////////////
private:
IdGenerator (IdGenerator const&);
IdGenerator& operator= (IdGenerator const&);
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief create an id generator
////////////////////////////////////////////////////////////////////////////////
IdGenerator (Transaction::IdType = 0);
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy an id generator
////////////////////////////////////////////////////////////////////////////////
~IdGenerator ();
////////////////////////////////////////////////////////////////////////////////
/// @brief set minimal transaction id
////////////////////////////////////////////////////////////////////////////////
void setLastId (Transaction::IdType id);
////////////////////////////////////////////////////////////////////////////////
/// @brief create a transaction id
////////////////////////////////////////////////////////////////////////////////
Transaction::IdType next ();
};
}
}
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

View File

@ -0,0 +1,164 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction manager
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2013 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 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "Manager.h"
#include "Basics/ReadLocker.h"
#include "Basics/WriteLocker.h"
#include "Transaction/IdGenerator.h"
#include "VocBase/vocbase.h"
using namespace triagens::transaction;
////////////////////////////////////////////////////////////////////////////////
/// @brief the transaction manager singleton
////////////////////////////////////////////////////////////////////////////////
static Manager* ManagerInstance = 0;
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief create the transaction manager
////////////////////////////////////////////////////////////////////////////////
Manager::Manager ()
: _generator(),
_lock(),
_transactions() {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the transaction manager
////////////////////////////////////////////////////////////////////////////////
Manager::~Manager () {
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief get the transaction manager instance
////////////////////////////////////////////////////////////////////////////////
Manager* Manager::instance () {
assert(ManagerInstance != 0);
return ManagerInstance;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief initialise the transaction manager instance
////////////////////////////////////////////////////////////////////////////////
void Manager::initialise () {
assert(ManagerInstance == 0);
ManagerInstance = new Manager();
}
////////////////////////////////////////////////////////////////////////////////
/// @brief shutdown the transaction manager instance
////////////////////////////////////////////////////////////////////////////////
void Manager::shutdown () {
if (ManagerInstance != 0) {
delete ManagerInstance;
ManagerInstance = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief create a transaction object
////////////////////////////////////////////////////////////////////////////////
Transaction* Manager::createTransaction (TRI_vocbase_t* vocbase) {
Transaction::IdType id = _generator.next();
Transaction* transaction = new Transaction(this, id, vocbase);
return transaction;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief begin a transaction
////////////////////////////////////////////////////////////////////////////////
int Manager::beginTransaction (Transaction* transaction) {
Transaction::IdType id = transaction->id();
WRITE_LOCKER(_lock);
auto it = _transactions.insert(make_pair(id, Transaction::STATE_BEGUN));
if (it.first == _transactions.end()) {
return TRI_ERROR_INTERNAL;
}
return TRI_ERROR_NO_ERROR;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief commit a transaction
////////////////////////////////////////////////////////////////////////////////
int Manager::commitTransaction (Transaction* transaction) {
Transaction::IdType id = transaction->id();
WRITE_LOCKER(_lock);
auto it = _transactions.find(id);
if (it == _transactions.end()) {
return TRI_ERROR_INTERNAL;
}
_transactions.erase(id);
return TRI_ERROR_NO_ERROR;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief abort a transaction
////////////////////////////////////////////////////////////////////////////////
int Manager::abortTransaction (Transaction* transaction) {
Transaction::IdType id = transaction->id();
WRITE_LOCKER(_lock);
auto it = _transactions.find(id);
if (it == _transactions.end()) {
return TRI_ERROR_INTERNAL;
}
(*it).second = Transaction::STATE_ABORTED;
return TRI_ERROR_NO_ERROR;
}
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

View File

@ -0,0 +1,141 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction manager
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2013 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 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_TRANSACTION_MANAGER_H
#define TRIAGENS_TRANSACTION_MANAGER_H 1
#include "Basics/Common.h"
#include "Basics/ReadWriteLock.h"
#include "Transaction/IdGenerator.h"
#include "Transaction/Transaction.h"
extern "C" {
struct TRI_vocbase_s;
}
namespace triagens {
namespace transaction {
// -----------------------------------------------------------------------------
// --SECTION-- class TransactionManager
// -----------------------------------------------------------------------------
class Manager {
////////////////////////////////////////////////////////////////////////////////
/// @brief TransactionManager
////////////////////////////////////////////////////////////////////////////////
private:
Manager (Manager const&);
Manager& operator= (Manager const&);
Manager ();
~Manager ();
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief get the transaction manager instance
////////////////////////////////////////////////////////////////////////////////
static Manager* instance ();
////////////////////////////////////////////////////////////////////////////////
/// @brief initialise the transaction manager instance
////////////////////////////////////////////////////////////////////////////////
void initialise ();
////////////////////////////////////////////////////////////////////////////////
/// @brief shutdown the transaction manager instance
////////////////////////////////////////////////////////////////////////////////
void shutdown ();
////////////////////////////////////////////////////////////////////////////////
/// @brief create a transaction object
////////////////////////////////////////////////////////////////////////////////
Transaction* createTransaction (struct TRI_vocbase_s*);
////////////////////////////////////////////////////////////////////////////////
/// @brief begin a transaction
////////////////////////////////////////////////////////////////////////////////
int beginTransaction (Transaction*);
////////////////////////////////////////////////////////////////////////////////
/// @brief commit a transaction
////////////////////////////////////////////////////////////////////////////////
int commitTransaction (Transaction*);
////////////////////////////////////////////////////////////////////////////////
/// @brief abort a transaction
////////////////////////////////////////////////////////////////////////////////
int abortTransaction (Transaction*);
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction id generator
////////////////////////////////////////////////////////////////////////////////
IdGenerator _generator;
////////////////////////////////////////////////////////////////////////////////
/// @brief R/W lock for transactions tables
////////////////////////////////////////////////////////////////////////////////
basics::ReadWriteLock _lock;
////////////////////////////////////////////////////////////////////////////////
/// @brief all running or aborted transactions
////////////////////////////////////////////////////////////////////////////////
map<Transaction::IdType, Transaction::StateType> _transactions;
};
}
}
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

View File

@ -0,0 +1,120 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2013 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 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "Transaction.h"
#include "Transaction/Manager.h"
#include "VocBase/vocbase.h"
using namespace triagens::transaction;
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief create the transaction
////////////////////////////////////////////////////////////////////////////////
Transaction::Transaction (Manager* manager,
IdType id,
TRI_vocbase_t* vocbase)
: _manager(manager),
_id(id),
_state(STATE_UNINITIALISED),
_vocbase(vocbase) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the transaction manager
////////////////////////////////////////////////////////////////////////////////
Transaction::~Transaction () {
if (state() != STATE_COMMITTED && state() != STATE_ABORTED) {
this->abort();
}
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief begin a transaction
////////////////////////////////////////////////////////////////////////////////
int Transaction::begin () {
if (state() == STATE_UNINITIALISED &&
_manager->beginTransaction(this)) {
_state = STATE_BEGUN;
return TRI_ERROR_NO_ERROR;
}
this->abort();
return TRI_ERROR_TRANSACTION_INTERNAL;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief commit a transaction
////////////////////////////////////////////////////////////////////////////////
int Transaction::commit () {
if (state() == STATE_BEGUN &&
_manager->commitTransaction(this)) {
_state = STATE_COMMITTED;
return TRI_ERROR_NO_ERROR;
}
this->abort();
return TRI_ERROR_TRANSACTION_INTERNAL;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief abort a transaction
////////////////////////////////////////////////////////////////////////////////
int Transaction::abort () {
if (state() == STATE_BEGUN &&
_manager->abortTransaction(this)) {
_state = STATE_ABORTED;
return TRI_ERROR_NO_ERROR;
}
_state = STATE_ABORTED;
return TRI_ERROR_TRANSACTION_INTERNAL;
}
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End:

View File

@ -0,0 +1,185 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2013 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 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_TRANSACTION_TRANSACTION_H
#define TRIAGENS_TRANSACTION_TRANSACTION_H 1
#include "Basics/Common.h"
extern "C" {
struct TRI_vocbase_s;
}
namespace triagens {
namespace transaction {
class Manager;
// -----------------------------------------------------------------------------
// --SECTION-- class Transaction
// -----------------------------------------------------------------------------
class Transaction {
// -----------------------------------------------------------------------------
// --SECTION-- typedefs
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction id type
////////////////////////////////////////////////////////////////////////////////
typedef uint64_t IdType;
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction state
////////////////////////////////////////////////////////////////////////////////
enum StateType {
STATE_UNINITIALISED = 0,
STATE_BEGUN = 1,
STATE_ABORTED = 2,
STATE_COMMITTED = 3
};
////////////////////////////////////////////////////////////////////////////////
/// @brief Transaction
////////////////////////////////////////////////////////////////////////////////
private:
Transaction (Transaction const&);
Transaction& operator= (Transaction const&);
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief create a transaction
////////////////////////////////////////////////////////////////////////////////
Transaction (Manager*,
IdType,
struct TRI_vocbase_s*);
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy a transaction
////////////////////////////////////////////////////////////////////////////////
~Transaction ();
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief get the transaction id
////////////////////////////////////////////////////////////////////////////////
inline IdType id () const {
return _id;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the transaction state
////////////////////////////////////////////////////////////////////////////////
inline StateType state () const {
return _state;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief begin a transaction
////////////////////////////////////////////////////////////////////////////////
int begin ();
////////////////////////////////////////////////////////////////////////////////
/// @brief commit a transaction
////////////////////////////////////////////////////////////////////////////////
int commit ();
////////////////////////////////////////////////////////////////////////////////
/// @brief abort a transaction
////////////////////////////////////////////////////////////////////////////////
int abort ();
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
private:
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief the transaction manager
////////////////////////////////////////////////////////////////////////////////
Manager* _manager;
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction id
////////////////////////////////////////////////////////////////////////////////
IdType const _id;
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction state
////////////////////////////////////////////////////////////////////////////////
StateType _state;
////////////////////////////////////////////////////////////////////////////////
/// @brief vocbase for the transaction
////////////////////////////////////////////////////////////////////////////////
struct TRI_vocbase_s* _vocbase;
};
}
}
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End: