mirror of https://gitee.com/bigwinds/arangodb
Added cleanup functions class that allows to execute functions on unexpected shutdown (like FATAL_EXIT). First use: delete the LOCK file. (#5218)
This commit is contained in:
parent
08b787d897
commit
a9e446a83d
|
@ -80,6 +80,13 @@ void LockfileFeature::start() {
|
||||||
<< _lockFilename << "': " << TRI_errno_string(res);
|
<< _lockFilename << "': " << TRI_errno_string(res);
|
||||||
FATAL_ERROR_EXIT_CODE(TRI_EXIT_COULD_NOT_LOCK);
|
FATAL_ERROR_EXIT_CODE(TRI_EXIT_COULD_NOT_LOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto cleanup = std::make_unique<CleanupFunctions::CleanupFunction>(
|
||||||
|
[&] (int code, void* data) {
|
||||||
|
LOG_TOPIC(ERR, arangodb::Logger::FIXME) << "Removing LOCK file";
|
||||||
|
TRI_DestroyLockFile(_lockFilename.c_str());
|
||||||
|
});
|
||||||
|
CleanupFunctions::registerFunction(std::move(cleanup));
|
||||||
}
|
}
|
||||||
|
|
||||||
void LockfileFeature::unprepare() {
|
void LockfileFeature::unprepare() {
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -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
|
|
@ -212,13 +212,14 @@ typedef long suseconds_t;
|
||||||
|
|
||||||
/// @brief aborts program execution, returning an error code
|
/// @brief aborts program execution, returning an error code
|
||||||
/// if backtraces are enabled, a backtrace will be printed before
|
/// if backtraces are enabled, a backtrace will be printed before
|
||||||
#define FATAL_ERROR_EXIT_CODE(code) \
|
#define FATAL_ERROR_EXIT_CODE(code) \
|
||||||
do { \
|
do { \
|
||||||
TRI_LogBacktrace(); \
|
TRI_LogBacktrace(); \
|
||||||
arangodb::Logger::flush(); \
|
arangodb::basics::CleanupFunctions::run(code, nullptr); \
|
||||||
arangodb::Logger::shutdown(); \
|
arangodb::Logger::flush(); \
|
||||||
TRI_EXIT_FUNCTION(code, nullptr); \
|
arangodb::Logger::shutdown(); \
|
||||||
exit(code); \
|
TRI_EXIT_FUNCTION(code, nullptr); \
|
||||||
|
exit(code); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/// @brief aborts program execution, returning an error code
|
/// @brief aborts program execution, returning an error code
|
||||||
|
@ -230,12 +231,13 @@ typedef long suseconds_t;
|
||||||
|
|
||||||
/// @brief aborts program execution, calling std::abort
|
/// @brief aborts program execution, calling std::abort
|
||||||
/// if backtraces are enabled, a backtrace will be printed before
|
/// if backtraces are enabled, a backtrace will be printed before
|
||||||
#define FATAL_ERROR_ABORT(...) \
|
#define FATAL_ERROR_ABORT(...) \
|
||||||
do { \
|
do { \
|
||||||
TRI_LogBacktrace(); \
|
TRI_LogBacktrace(); \
|
||||||
arangodb::Logger::flush(); \
|
arangodb::basics::CleanupFunctions::run(500, nullptr); \
|
||||||
arangodb::Logger::shutdown(); \
|
arangodb::Logger::flush(); \
|
||||||
std::abort(); \
|
arangodb::Logger::shutdown(); \
|
||||||
|
std::abort(); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
|
@ -156,6 +156,7 @@ add_library(${LIB_ARANGO} STATIC
|
||||||
Basics/VelocyPackHelper.cpp
|
Basics/VelocyPackHelper.cpp
|
||||||
Basics/WorkMonitor.cpp
|
Basics/WorkMonitor.cpp
|
||||||
Basics/application-exit.cpp
|
Basics/application-exit.cpp
|
||||||
|
Basics/CleanupFunctions.cpp
|
||||||
Basics/conversions.cpp
|
Basics/conversions.cpp
|
||||||
Basics/csv.cpp
|
Basics/csv.cpp
|
||||||
Basics/debugging.cpp
|
Basics/debugging.cpp
|
||||||
|
|
|
@ -61,6 +61,7 @@
|
||||||
|
|
||||||
#include "Basics/Common.h"
|
#include "Basics/Common.h"
|
||||||
|
|
||||||
|
#include "Basics/CleanupFunctions.h"
|
||||||
#include "Basics/Mutex.h"
|
#include "Basics/Mutex.h"
|
||||||
#include "Logger/LogLevel.h"
|
#include "Logger/LogLevel.h"
|
||||||
#include "Logger/LogMacros.h"
|
#include "Logger/LogMacros.h"
|
||||||
|
|
Loading…
Reference in New Issue