Skip to content

Commit

Permalink
ResourceManager: Add configuration for killing server when low resources
Browse files Browse the repository at this point in the history
Change-Id: I46ecc8288cd38d9019e7337f8fab471c4539022e
  • Loading branch information
jcaden committed May 23, 2016
1 parent 8272a6a commit 4a21095
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions kurento.conf.info
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mediaServer
; {
; ; Resources usage limit for raising an exception when an object creation is attempted
; exceptionLimit 0.8
; ; Resources usage limit for restarting the server when no objects are alive
; killLimit 0.7
; }
net
{
Expand Down
3 changes: 3 additions & 0 deletions kurento.conf.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
; [mediaServer.resources]
; Resources usage limit for raising an exception when an object creation is attempted
; exceptionLimit=0.8
; Resources usage limit for restarting the server when no objects are alive
; killLimit=0.7

[mediaServer.net.websocket]
port=8888
path=kurento
Expand Down
2 changes: 2 additions & 0 deletions kurento.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//"resources": {
// //Resources usage limit for raising an exception when an object creation is attempted
// "exceptionLimit": "0.8",
// // Resources usage limit for restarting the server when no objects are alive
// "killLimit": "0.7"
//},
"net" : {
// Uncomment just one of them
Expand Down
17 changes: 17 additions & 0 deletions server/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <fstream>
#include <sstream>
#include <KurentoException.hpp>
#include <MediaSet.hpp>

#include <sys/resource.h>

Expand Down Expand Up @@ -135,6 +136,22 @@ checkResources (float limit_percent)
checkOpenFiles (limit_percent);
}

void killServerOnLowResources (float limit_percent)
{
MediaSet::getMediaSet()->signalEmptyLocked.connect ([limit_percent] () {
GST_DEBUG ("MediaSet empty, checking resources");

try {
checkResources (limit_percent);
} catch (KurentoException &e) {
if (e.getCode() == NOT_ENOUGH_RESOURCES) {
GST_ERROR ("Resources over the limit, server will be killed");
exit (1);
}
}
});
}

} /* kurento */

static void init_debug (void) __attribute__ ( (constructor) );
Expand Down
2 changes: 2 additions & 0 deletions server/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static const float DEFAULT_RESOURCE_LIMIT_PERCENT = 0.8;

void checkResources (float limit_percent);

void killServerOnLowResources (float limit_percent);

} /* kurento */

#endif /* __RESOURCE_MANAGER_H__ */
15 changes: 13 additions & 2 deletions server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
#include <boost/log/utility/setup/common_attributes.hpp>

#include "TransportFactory.hpp"
#include "ResourceManager.hpp"

#include <SignalHandler.hpp>
#include <ServerMethods.hpp>
#include <gst/gst.h>

#include <boost/program_options.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/optional.hpp>

#include "logging.hpp"
#include "modules.hpp"
Expand Down Expand Up @@ -148,7 +150,8 @@ main (int argc, char **argv)
boost::program_options::store (boost::program_options::parse_environment (desc,
[&desc] (std::string & input) -> std::string {
/* Look for KURENTO_ prefix and change to lower case */
if (input.find (ENV_PREFIX) == 0) {
if (input.find (ENV_PREFIX) == 0)
{
std::string aux = input.substr (ENV_PREFIX.size() );
std::transform (aux.begin(), aux.end(), aux.begin(), [] (int c) -> int {
return (c == '_') ? '-' : tolower (c);
Expand Down Expand Up @@ -201,7 +204,6 @@ main (int argc, char **argv)
print_version();
exit (0);
}

} catch (boost::program_options::error &e) {
std::cerr << "Error : " << e.what() << std::endl;
exit (1);
Expand All @@ -219,6 +221,15 @@ main (int argc, char **argv)
GST_INFO ("Kmsc version: %s", get_version () );

loadConfig (config, confFile, modulesConfigPath);

boost::optional<float> killResourceLimit =
config.get_optional<float> ("mediaServer.resources.killLimit");

if (killResourceLimit) {
GST_INFO ("Resource limit is: %f", *killResourceLimit);
killServerOnLowResources (*killResourceLimit);
}

transport = createTransportFromConfig (config);

/* Start transport */
Expand Down

0 comments on commit 4a21095

Please sign in to comment.