1
0
Fork 0

Bug fix/lock file cleanup (#5960)

* Added cleanup functions class that allows to execute functions on unexpected shutdown (like FATAL_EXIT). First use: delete the LOCK file. (#5218)

* Removed superfluous log message
This commit is contained in:
Michael Hackstein 2018-07-24 09:43:15 +02:00 committed by GitHub
parent b1fab04296
commit f7373fe103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 156 additions and 13 deletions

View File

@ -79,6 +79,12 @@ void LockfileFeature::start() {
<< _lockFilename << "': " << TRI_errno_string(res);
FATAL_ERROR_EXIT_CODE(TRI_EXIT_COULD_NOT_LOCK);
}
auto cleanup = std::make_unique<CleanupFunctions::CleanupFunction>(
[&] (int code, void* data) {
TRI_DestroyLockFile(_lockFilename.c_str());
});
CleanupFunctions::registerFunction(std::move(cleanup));
}
void LockfileFeature::unprepare() {

View File

@ -0,0 +1,44 @@
////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2018 ArangoDB 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 Michael Hackstein
////////////////////////////////////////////////////////////////////////////////
#include "CleanupFunctions.h"
#include "Basics/MutexLocker.h"
using namespace arangodb;
using namespace arangodb::basics;
// Init static class members
Mutex CleanupFunctions::_functionsMutex;
std::vector<std::unique_ptr<CleanupFunctions::CleanupFunction>> CleanupFunctions::_cleanupFunctions;
void CleanupFunctions::registerFunction(std::unique_ptr<CleanupFunctions::CleanupFunction> func) {
MUTEX_LOCKER(locker, _functionsMutex);
_cleanupFunctions.emplace_back(std::move(func));
}
void CleanupFunctions::run(int code, void* data) {
MUTEX_LOCKER(locker, _functionsMutex);
for (auto const& func : _cleanupFunctions) {
(*func)(code, data);
}
_cleanupFunctions.clear();
}

View File

@ -0,0 +1,88 @@
////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2018 ArangoDB 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 Michael Hackstein
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_BASICS_CLEANUPFUNCTIONS_H
#define ARANGODB_BASICS_CLEANUPFUNCTIONS_H 1
#include "Basics/Common.h"
#include "Basics/Mutex.h"
namespace arangodb {
namespace basics {
/**
* @brief A class to manage and handle cleanup functions on shutdown.
* Thread save execution. It is meant to collect functions that should
* be executed on every shutdown we can react to.
* e.g. FATAL errors
*/
class CleanupFunctions {
CleanupFunctions(CleanupFunctions const&) = delete;
CleanupFunctions& operator=(CleanupFunctions const&) = delete;
// Typedefs
public:
/**
* @brief A cleanup function, will be called with the exit code
* and some data that is generated during exit (just as TRI_ExitFunction_t).
*/
typedef std::function<void(int, void*)> CleanupFunction;
// Functions
public:
/**
* @brief Register a new function to be executed during
* any "expected" exit of the server (FATAL, CTRL+C, Shutdown)
*
* @param func The function to be executed.
*/
static void registerFunction(std::unique_ptr<CleanupFunction> func);
/**
* @brief Execute all functions in _cleanupFunctions
*
* @param code The exit code provided
* @param data data given during exit
*/
static void run(int code, void* data);
private:
/**
* @brief A lock for the cleanup functions.
* Used to make sure they are only executed once.
* This is NOT performance critical as those functions
* only kick in on startup (insert) and shutdown (execute)
*/
static Mutex _functionsMutex;
/**
* @brief A list of functions to be executed during cleanup
*/
static std::vector<std::unique_ptr<CleanupFunction>> _cleanupFunctions;
};
} // namespace basics
} // namespace arangodb
#endif

View File

@ -215,13 +215,14 @@ typedef long suseconds_t;
/// @brief aborts program execution, returning an error code
/// if backtraces are enabled, a backtrace will be printed before
#define FATAL_ERROR_EXIT_CODE(code) \
do { \
TRI_LogBacktrace(); \
arangodb::Logger::flush(); \
arangodb::Logger::shutdown(); \
TRI_EXIT_FUNCTION(code, nullptr); \
exit(code); \
#define FATAL_ERROR_EXIT_CODE(code) \
do { \
TRI_LogBacktrace(); \
arangodb::basics::CleanupFunctions::run(code, nullptr); \
arangodb::Logger::flush(); \
arangodb::Logger::shutdown(); \
TRI_EXIT_FUNCTION(code, nullptr); \
exit(code); \
} while (0)
/// @brief aborts program execution, returning an error code
@ -233,12 +234,13 @@ typedef long suseconds_t;
/// @brief aborts program execution, calling std::abort
/// if backtraces are enabled, a backtrace will be printed before
#define FATAL_ERROR_ABORT(...) \
do { \
TRI_LogBacktrace(); \
arangodb::Logger::flush(); \
arangodb::Logger::shutdown(); \
std::abort(); \
#define FATAL_ERROR_ABORT(...) \
do { \
TRI_LogBacktrace(); \
arangodb::basics::CleanupFunctions::run(500, nullptr); \
arangodb::Logger::flush(); \
arangodb::Logger::shutdown(); \
std::abort(); \
} while (0)
#ifdef _WIN32

View File

@ -166,6 +166,7 @@ add_library(${LIB_ARANGO} STATIC
Basics/VelocyPackDumper.cpp
Basics/VelocyPackHelper.cpp
Basics/application-exit.cpp
Basics/CleanupFunctions.cpp
Basics/conversions.cpp
Basics/csv.cpp
Basics/datetime.cpp

View File

@ -59,6 +59,8 @@
#ifndef ARANGODB_LOGGER_LOGGER_H
#define ARANGODB_LOGGER_LOGGER_H 1
#include "Basics/Common.h"
#include "Basics/CleanupFunctions.h"
#include "Basics/Mutex.h"
#include "Logger/LogLevel.h"
#include "Logger/LogMacros.h"