From 6368bb51fcafe56f4be1cdaf1392d39d0fc89241 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 27 Jul 2017 12:51:41 +0200 Subject: [PATCH] F #4977: Move missing monitor map logic to monitor_action() --- include/MarketPlaceAppPool.h | 53 +++++++++--------------- src/market/MarketPlaceAppPool.cc | 57 ++++++++++++++++++++------ src/market/MarketPlaceManagerDriver.cc | 57 +++++++++++++------------- 3 files changed, 93 insertions(+), 74 deletions(-) diff --git a/include/MarketPlaceAppPool.h b/include/MarketPlaceAppPool.h index 01c80af4d9..d66f674a09 100644 --- a/include/MarketPlaceAppPool.h +++ b/include/MarketPlaceAppPool.h @@ -73,13 +73,14 @@ public: * @param template to generate app with the from_template64 function * @param mp_id of the MarketPlace to store de App * @param mp_name of the MarketPlace + * @param app_id of the imported app * @param error_str Returns the error reason, if any * * @return the oid assigned to the object, -1 in case of failure, -2 * already imported */ int import(const std::string& t64, int mp_id, const std::string& mp_name, - std::string& error_str); + int& app_id, std::string& error_str); /** * Function to get a MarketPlaceApp from the pool @@ -147,43 +148,29 @@ public: }; /** - * Erease map element + * Check an element into map + * @param map_id of the app + * @return true if the app has to be deleted */ - void drop_map_check(const std::string& name){ - if (map_check.find( name ) != map_check.end()){ - map::iterator it; - it=map_check.find(name); - map_check.erase (it); - } - } + bool test_map_check(int map_id); /** - * Check an element into map + * Resets the counter of missing monitors of an app + * @param app_id of the app */ - bool test_map_check(const std::string& name){ - map_check[name]++; - if (map_check[name] > 0){ - return true; - } - return false; - } -private: - map map_check; + void reset_map_check(int app_id); - - void insert_map_check(const std::string& name){ - map_check.insert(make_pair(name, -1)); - } - void reset_map_check(const std::string& name){ - if (name != "") { - if (map_check.find( name ) != map_check.end()){ - map_check[name] = -1; - } - else{ - insert_map_check(name); - } - } - } +private: + + /** + * Hash to store the number of times an app was missing from monitor data + */ + map map_check; + + /** + * Max number of monitor that an app may be missing before deleting it + */ + static const int MAX_MISSING_MONITORS; }; #endif /*MARKETPLACE_POOL_H_*/ diff --git a/src/market/MarketPlaceAppPool.cc b/src/market/MarketPlaceAppPool.cc index 68c2d64e7e..e80944b4ca 100644 --- a/src/market/MarketPlaceAppPool.cc +++ b/src/market/MarketPlaceAppPool.cc @@ -128,11 +128,6 @@ int MarketPlaceAppPool:: allocate( *oid = PoolSQL::allocate(mp, error_str); - // ------------------------------------------------------------------------ - // Insert id into map_check - // -------------------------------------------------------------------------- - insert_map_check(name); - return *oid; error_duplicated: @@ -188,8 +183,6 @@ int MarketPlaceAppPool::drop(PoolObjectSQL * objsql, std::string& error_msg) return 0; } - drop_map_check(objsql->get_name()); - return PoolSQL::drop(objsql, error_msg); } @@ -197,7 +190,7 @@ int MarketPlaceAppPool::drop(PoolObjectSQL * objsql, std::string& error_msg) /* -------------------------------------------------------------------------- */ int MarketPlaceAppPool::import(const std::string& t64, int mp_id, - const std::string& mp_name, std::string& error_str) + const std::string& mp_name, int& app_id, std::string& error_str) { // ------------------------------------------------------------------------- // Build the marketplace app object @@ -236,7 +229,8 @@ int MarketPlaceAppPool::import(const std::string& t64, int mp_id, if( mp_aux != 0 ) //Marketplace app already imported { - reset_map_check(app->name); + app_id = mp_aux->oid; + if ( mp_aux->version != app->version || mp_aux->md5 != app->md5 ) { mp_aux->from_template64(t64, error_str); @@ -256,15 +250,17 @@ int MarketPlaceAppPool::import(const std::string& t64, int mp_id, // ------------------------------------------------------------------------- if (Nebula::instance().is_federation_slave()) { - int oid = master_allocate(app, error_str); + app_id = master_allocate(app, error_str); app->lock(); delete app; - return oid; + return app_id; } - insert_map_check(app->name); - return PoolSQL::allocate(app, error_str); + + app_id = PoolSQL::allocate(app, error_str); + + return app_id; } /* -------------------------------------------------------------------------- */ @@ -316,4 +312,39 @@ int MarketPlaceAppPool::update(PoolObjectSQL * objsql) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +const int MarketPlaceAppPool::MAX_MISSING_MONITORS = 3; +bool MarketPlaceAppPool::test_map_check(int app_id) +{ + map::iterator it = map_check.find(app_id); + + if ( it == map_check.end() ) + { + return false; + } + + it->second++; + + bool to_delete = it->second >= MAX_MISSING_MONITORS; + + if ( to_delete ) + { + map_check.erase(it); + } + + return to_delete; +} + +void MarketPlaceAppPool::reset_map_check(int app_id) +{ + map::iterator it = map_check.find(app_id); + + if ( it == map_check.end() ) + { + map_check.insert(make_pair(app_id, -1)); + } + else + { + it->second = -1; + } +} diff --git a/src/market/MarketPlaceManagerDriver.cc b/src/market/MarketPlaceManagerDriver.cc index 15e3ea4e42..695391389d 100644 --- a/src/market/MarketPlaceManagerDriver.cc +++ b/src/market/MarketPlaceManagerDriver.cc @@ -120,7 +120,6 @@ static void monitor_action( return; } - std::string name; MarketPlace * market = marketpool->get(id, true); if (market == 0 ) @@ -128,7 +127,8 @@ static void monitor_action( return; } - name = market->get_name(); + set apps_mp = market->get_marketapp_ids(); + std::string name = market->get_name(); market->update_monitor(monitor_data); @@ -143,7 +143,8 @@ static void monitor_action( for (int i=0; i< num ; i++) { - int rc = apppool->import(apps[i]->value(), id, name, err); + int app_id; + int rc = apppool->import(apps[i]->value(), id, name, app_id, err); if ( rc == -1 ) { @@ -162,37 +163,37 @@ static void monitor_action( market->unlock(); } } + + apppool->reset_map_check(app_id); + + apps_mp.erase(app_id); } - MarketPlaceApp *mp_app = nullptr; - std::string error; - std::string source; - int rc_del; - market = marketpool->get(id, true); - set apps_mp = market->get_marketapp_ids(); - market->unlock(); - for (set::iterator i = apps_mp.begin(); i != apps_mp.end(); i++) { - mp_app = apppool->get(*i, true); - if ( mp_app != 0 ) - { - if(apppool->test_map_check(mp_app->get_name())){ //delete app - market = marketpool->get(id, true); + for (set::iterator i = apps_mp.begin(); i != apps_mp.end(); ++i) + { + if (apppool->test_map_check(*i)) //delete app + { + std::string error; - source = mp_app->get_source(); - rc_del = apppool->drop(mp_app, error); + MarketPlaceApp * app = apppool->get(*i, true); - market->del_marketapp(*i); - marketpool->update(market); - - market->unlock(); - if ( rc_del < 0 ) - { - oss << " Error removing app from DB: " << error - << ". Remove app manually, source is: " << source; - } + if ( app == 0 ) + { + continue; } + + rc = apppool->drop(app, error); + + app->unlock(); + + market = marketpool->get(id, true); + + market->del_marketapp(*i); + + marketpool->update(market); + + market->unlock(); } - mp_app->unlock(); } oss << "Marketplace " << name << " (" << id << ") successfully monitored.";