diff --git a/include/Cluster.h b/include/Cluster.h index c0528ed05a..128884ab27 100644 --- a/include/Cluster.h +++ b/include/Cluster.h @@ -29,6 +29,9 @@ class Cluster : public PoolObjectSQL { public: + + virtual ~Cluster() = default; + // ************************************************************************* // Object Collections (Public) // ************************************************************************* @@ -101,6 +104,7 @@ private: // ------------------------------------------------------------------------- friend class ClusterPool; + friend class PoolSQL; // ************************************************************************* // VNC Port management function @@ -137,8 +141,6 @@ private: Cluster(int id, const std::string& name, ClusterTemplate* cl_template, const VectorAttribute& vnc_conf); - virtual ~Cluster() = default; - // ************************************************************************* // Attributes (Private) // ************************************************************************* diff --git a/include/ClusterPool.h b/include/ClusterPool.h index 5b6e34873f..d657ff00fe 100644 --- a/include/ClusterPool.h +++ b/include/ClusterPool.h @@ -68,19 +68,15 @@ public: { int rc = -1; - Cluster * cluster = get(oid); - - if ( cluster != 0 ) + if ( auto cluster = get(oid) ) { - rc = cluster->get_vnc_port(vm_id, port); + rc = cluster->get_vnc_port(vm_id, port); - update_vnc_bitmap(cluster); - - cluster->unlock(); + update_vnc_bitmap(cluster.get()); } return rc; - }; + } /** * Release a previously allocated VNC port in the cluster @@ -89,15 +85,11 @@ public: */ void release_vnc_port(int oid, unsigned int port) { - Cluster * cluster = get(oid); - - if ( cluster != 0 ) + if ( auto cluster = get(oid) ) { cluster->release_vnc_port(port); - update_vnc_bitmap(cluster); - - cluster->unlock(); + update_vnc_bitmap(cluster.get()); } } @@ -112,15 +104,11 @@ public: { int rc = -1; - Cluster * cluster = get(oid); - - if ( cluster != 0 ) + if ( auto cluster = get(oid) ) { rc = cluster->set_vnc_port(port); - update_vnc_bitmap(cluster); - - cluster->unlock(); + update_vnc_bitmap(cluster.get()); } return rc; @@ -142,28 +130,26 @@ public: int allocate(std::string name, int * oid, std::string& error_str); /** - * Function to get a cluster from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid cluster unique id - * @param lock locks the cluster mutex - * @return a pointer to the cluster, 0 if the cluster could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Cluster unique identifier + * @return a pointer to the Cluster, nullptr in case of failure */ - Cluster * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read onlycluster from the pool, if the object is not - * in memory it is loaded from the DB - * @param oid cluster unique id - * - * @return a pointer to the cluster, 0 if the cluster could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the Cluster unique identifier + * @return a pointer to the Cluster, nullptr in case of failure */ - - Cluster * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); + return PoolSQL::get_ro(oid); } /** diff --git a/include/Datastore.h b/include/Datastore.h index ebae7ab31f..7d1aeb984e 100644 --- a/include/Datastore.h +++ b/include/Datastore.h @@ -88,6 +88,8 @@ public: } }; + virtual ~Datastore() = default; + /** * Function to print the Datastore object into a string in XML format * @param xml the resulting XML string @@ -344,8 +346,6 @@ private: DatastoreTemplate* ds_template, const std::set &cluster_ids); - virtual ~Datastore() = default; - /** * Sets the DISK_TYPE attribute for the datastore. This function will * check the type against the supported DiskTypes for each datastore type diff --git a/include/DatastorePool.h b/include/DatastorePool.h index f3f775659c..371f4da058 100644 --- a/include/DatastorePool.h +++ b/include/DatastorePool.h @@ -96,26 +96,26 @@ public: std::string& error_str); /** - * Function to get a Datastore from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Datastore unique id - * @param lock locks the Datastore mutex - * @return a pointer to the Datastore, 0 if the Datastore could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Datastore unique identifier + * @return a pointer to the Datastore, nullptr in case of failure */ - Datastore * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read only Datastore from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Datastore unique id - * @return a pointer to the Datastore, 0 if the Datastore could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the Datastore unique identifier + * @return a pointer to the Datastore, nullptr in case of failure */ - Datastore * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); + return PoolSQL::get_ro(oid); } /** diff --git a/include/DispatchManager.h b/include/DispatchManager.h index e6c67797bb..6f54af9a0b 100644 --- a/include/DispatchManager.h +++ b/include/DispatchManager.h @@ -70,7 +70,8 @@ public: * @param ra information about the API call request * @return 0 on success */ - int deploy(VirtualMachine * vm, const RequestAttributes& request); + int deploy(std::unique_ptr vm, + const RequestAttributes& request); /** * Sets an imported VM to RUNNING state, a history record MUST be added, @@ -79,7 +80,8 @@ public: * @param ra information about the API call request * @return 0 on success */ - int import(VirtualMachine * vm, const RequestAttributes& ra); + int import(std::unique_ptr vm, + const RequestAttributes& ra); /** * Migrates a VM. The following actions must be performed before calling @@ -194,8 +196,9 @@ public: * @param ra information about the API call request * @return 0 on success, the VM mutex is unlocked */ - int delete_vm(VirtualMachine * vm, const RequestAttributes& ra, - std::string& error_str); + int delete_vm(std::unique_ptr vm, + const RequestAttributes& ra, + std::string& error_str); /** * VM ID interface @@ -208,8 +211,9 @@ public: * @param ra information about the API call request * @return 0 on success */ - int delete_recreate(VirtualMachine * vm, const RequestAttributes& ra, - std::string& error_str); + int delete_recreate(std::unique_ptr vm, + const RequestAttributes& ra, + std::string& error_str); /** * Ends a VM life cycle inside ONE but let the VM running at the Hipervisor. @@ -217,8 +221,9 @@ public: * @param ra information about the API call request * @return 0 on success, the VM mutex is unlocked */ - int delete_vm_db(VirtualMachine * vm, const RequestAttributes& ra, - std::string& error_str); + int delete_vm_db(std::unique_ptr vm, + const RequestAttributes& ra, + std::string& error_str); /** * Recover the last operation on the VM @@ -227,8 +232,10 @@ public: * @param ra information about the API call request * @return 0 on success */ - int recover(VirtualMachine * vm, bool success, const RequestAttributes& ra, - std::string& error_str); + int recover(std::unique_ptr vm, + bool success, + const RequestAttributes& ra, + std::string& error_str); /** * Retry the last operation on the VM @@ -236,7 +243,7 @@ public: * @param ra information about the API call request * @return 0 on success */ - int retry(VirtualMachine * vm, const RequestAttributes& ra, + int retry(std::unique_ptr vm, const RequestAttributes& ra, std::string& error_str); /** @@ -416,8 +423,8 @@ public: * * @return 0 on success, -1 otherwise */ - int live_updateconf(int vid, const RequestAttributes& ra, - std::string& error_str); + int live_updateconf(std::unique_ptr vm, + const RequestAttributes& ra, std::string& error_str); //-------------------------------------------------------------------------- // DM Actions associated with a VM state transition @@ -484,7 +491,7 @@ private: /** * Frees the resources associated to a VM: disks, ip addresses and Quotas */ - void free_vm_resources(VirtualMachine * vm, bool check_images); + void free_vm_resources(std::unique_ptr vm, bool check_images); }; #endif /*DISPATCH_MANAGER_H*/ diff --git a/include/Document.h b/include/Document.h index cb60339177..fbdbb50092 100644 --- a/include/Document.h +++ b/include/Document.h @@ -30,6 +30,8 @@ class Document : public PoolObjectSQL { public: + virtual ~Document() = default; + /** * Function to print the Document object into a string in XML format * @param xml the resulting XML string @@ -127,8 +129,6 @@ protected: int type, Template * _template_contents); - virtual ~Document() = default; - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/DocumentPool.h b/include/DocumentPool.h index ffbecff021..3d4c3bac80 100644 --- a/include/DocumentPool.h +++ b/include/DocumentPool.h @@ -66,29 +66,26 @@ public: /** * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Document unique identifier + * @return a pointer to the Document, nullptr in case of failure */ - Document * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** * Gets a read only object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). No object lock, other threads may work with the same object. + * @param oid the Document unique identifier + * @return a pointer to the Document, nullptr in case of failure */ - Document * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** * Dumps the pool in XML format. A filter can be also added to the diff --git a/include/Group.h b/include/Group.h index f46c261501..b3de734cae 100644 --- a/include/Group.h +++ b/include/Group.h @@ -31,6 +31,8 @@ class Group : public PoolObjectSQL { public: + virtual ~Group() = default; + /** * Function to print the Group object into a string in XML format * @param xml the resulting XML string @@ -172,6 +174,7 @@ private: // ------------------------------------------------------------------------- friend class GroupPool; + friend class PoolSQL; // ************************************************************************* // Constructor @@ -179,8 +182,6 @@ private: Group(int id, const std::string& name); - virtual ~Group() = default; - // ************************************************************************* // Administrators // ************************************************************************* diff --git a/include/GroupPool.h b/include/GroupPool.h index 74b237bcca..519c5b2aa0 100644 --- a/include/GroupPool.h +++ b/include/GroupPool.h @@ -72,28 +72,27 @@ public: std::string& error_str); /** - * Function to get a group from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid group unique id - * @param lock locks the group mutex - * @return a pointer to the group, 0 if the group could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Group unique identifier + * @return a pointer to the Group, nullptr in case of failure */ - Group * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read only group from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid group unique id - * @param lock locks the group mutex - * @return a pointer to the group, 0 if the group could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the Group unique identifier + * @return a pointer to the Group, nullptr in case of failure */ - Group * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** * Returns the name of a group @@ -104,7 +103,7 @@ public: { static std::string error_str = ""; - Group * group = get_ro(gid); + auto group = get_ro(gid); if ( group == 0 ) { @@ -113,8 +112,6 @@ public: const std::string gname = group->get_name(); - group->unlock(); - return gname; } diff --git a/include/Hook.h b/include/Hook.h index fb84434831..ccdc22f0a3 100644 --- a/include/Hook.h +++ b/include/Hook.h @@ -29,6 +29,8 @@ class Hook : public PoolObjectSQL { public: + ~Hook(); + /** * Defines the hook type, so a whole hook class can be masked */ @@ -95,8 +97,6 @@ private: Hook(Template * tmpl); - ~Hook(); - /** * Set hook implementation attribute depending of the hook type. */ diff --git a/include/HookPool.h b/include/HookPool.h index 2dbddf20d4..49080c9112 100644 --- a/include/HookPool.h +++ b/include/HookPool.h @@ -41,25 +41,26 @@ public: int allocate (Template * tmpl, std::string& error_str); /** - * Function to get a Hook from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Hook unique id - * @return a pointer to the Hook, 0 if the Hook could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Hook unique identifier + * @return a pointer to the Hook, nullptr in case of failure */ - Hook * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read only Hook from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Hook unique id - * @return a pointer to the Hook, 0 if the Host could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the Hook unique identifier + * @return a pointer to the Hook, nullptr in case of failure */ - Hook * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); + return PoolSQL::get_ro(oid); } /** diff --git a/include/Host.h b/include/Host.h index 632e3d3b5a..ed7bffbdd6 100644 --- a/include/Host.h +++ b/include/Host.h @@ -112,6 +112,8 @@ public: return st; } + virtual ~Host() = default; + /** * Function to print the Host object into a string in XML format * @param xml the resulting XML string @@ -357,8 +359,6 @@ private: Host(int id, const std::string& hostname, const std::string& im_mad, const std::string& vmm_mad, int clusterid, const std::string& cluster); - virtual ~Host() = default; - // ************************************************************************* // Helper functions diff --git a/include/HostPool.h b/include/HostPool.h index 1511fbd9dc..5731140a13 100644 --- a/include/HostPool.h +++ b/include/HostPool.h @@ -60,53 +60,52 @@ public: virtual int update(PoolObjectSQL * objsql); /** - * Function to get a Host from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Host unique id - * @param lock locks the Host mutex - * @return a pointer to the Host, 0 if the Host could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Host unique identifier + * @return a pointer to the Host, nullptr in case of failure */ - Host * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read only Host from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Host unique id - * @return a pointer to the Host, 0 if the Host could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the Host unique identifier + * @return a pointer to the Host, nullptr in case of failure */ - Host * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** - * Function to get a Host from the pool, if the object is not in memory - * it is loaded from the DB + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param hostname - * @param lock locks the Host mutex - * @return a pointer to the Host, 0 if the Host could not be loaded + * @return a pointer to the Host, nullptr if the Host could not be loaded */ - Host * get(std::string name) + std::unique_ptr get(std::string name) { // The owner is set to -1, because it is not used in the key() method - return static_cast(PoolSQL::get(name,-1)); - }; + return PoolSQL::get(name, -1); + } /** - * Function to get a Host from the pool, if the object is not in memory - * it is loaded from the DB + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. * @param hostname - * @param lock locks the Host mutex * @return a pointer to the Host, 0 if the Host could not be loaded */ - Host * get_ro(std::string name) + std::unique_ptr get_ro(std::string name) { // The owner is set to -1, because it is not used in the key() method - return static_cast(PoolSQL::get_ro(name,-1)); - }; + return PoolSQL::get_ro(name, -1); + } /** * Generate an index key for the object @@ -152,15 +151,12 @@ public: int add_capacity(int oid, HostShareCapacity &sr) { int rc = 0; - Host * host = get(oid); - if ( host != 0 ) + if ( auto host = get(oid) ) { host->add_capacity(sr); - update(host); - - host->unlock(); + update(host.get()); } else { @@ -168,7 +164,7 @@ public: } return rc; - }; + } /** * De-Allocates a given capacity to the host @@ -181,17 +177,13 @@ public: */ void del_capacity(int oid, HostShareCapacity &sr) { - Host * host = get(oid); - - if ( host != 0 ) + if ( auto host = get(oid) ) { host->del_capacity(sr); - update(host); - - host->unlock(); + update(host.get()); } - }; + } int drop(PoolObjectSQL * objsql, std::string& error_msg) { diff --git a/include/Image.h b/include/Image.h index 80d2fb64d0..8c2088a8eb 100644 --- a/include/Image.h +++ b/include/Image.h @@ -164,6 +164,8 @@ public: // Image Public Methods // ************************************************************************* + virtual ~Image() = default; + /** * Function to print the Image object into a string in XML format * @param xml the resulting XML string @@ -718,8 +720,6 @@ protected: int umask, ImageTemplate* img_template); - virtual ~Image() = default; - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/ImageManager.h b/include/ImageManager.h index 6d4c494d68..3406c9119a 100644 --- a/include/ImageManager.h +++ b/include/ImageManager.h @@ -85,7 +85,8 @@ public: * @param attach true if attaching the image to a VM * @return pointer to the image or 0 if could not be acquired */ - Image * acquire_image(int vm_id, int image_id, bool attach, std::string& error); + std::unique_ptr acquire_image(int vm_id, int image_id, + bool attach, std::string& error); /** * Try to acquire an image from the repository for a VM. @@ -95,7 +96,7 @@ public: * @param attach true if attaching the image to a VM * @return pointer to the image or 0 if could not be acquired */ - Image * acquire_image(int vm_id, const std::string& name, + std::unique_ptr acquire_image(int vm_id, const std::string& name, int uid, bool attach, std::string& error); /** diff --git a/include/ImagePool.h b/include/ImagePool.h index 0cdc6d532b..041fc90289 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -19,7 +19,6 @@ #include "PoolSQL.h" #include "Image.h" -#include "NebulaLog.h" #include "Datastore.h" #include "OneDB.h" @@ -87,55 +86,55 @@ public: int * oid, std::string& error_str); - /** - ** Function to get a Image from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Image unique id - * @param lock locks the Image mutex - * @return a pointer to the Image, 0 if the Image could not be loaded - */ - Image * get(int oid) - { - return static_cast(PoolSQL::get(oid)); - }; - - /** - ** Function to get a read only Image from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Image unique id - * @return a pointer to the Image, 0 if the Image could not be loaded - */ - Image * get_ro(int oid) - { - return static_cast(PoolSQL::get(oid)); - }; - /** * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param name of the object - * @param uid id of owner - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Image unique identifier + * @return a pointer to the Image, nullptr in case of failure */ - Image * get(const std::string& name, int uid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(name,uid)); - }; + return PoolSQL::get(oid); + } /** * Gets a read only object from the pool (if needed the object is loaded from the - * database). + * database). No object lock, other threads may work with the same object. + * @param oid the Image unique identifier + * @return a pointer to the Image, nullptr in case of failure + */ + std::unique_ptr get_ro(int oid) + { + return PoolSQL::get_ro(oid); + } + + /** + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param name of the object * @param uid id of owner * * @return a pointer to the object, 0 in case of failure */ - Image * get_ro(const std::string& name, int uid) + std::unique_ptr get(const std::string& name, int uid) { - return static_cast(PoolSQL::get_ro(name,uid)); - }; + return PoolSQL::get(name,uid); + } + + /** + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param name of the object + * @param uid id of owner + * + * @return a pointer to the object, 0 in case of failure + */ + std::unique_ptr get_ro(const std::string& name, int uid) + { + return PoolSQL::get_ro(name, uid); + } /** * Bootstraps the database table(s) associated to the Image pool diff --git a/include/Listener.h b/include/Listener.h index e7ef825d04..95a423c341 100644 --- a/include/Listener.h +++ b/include/Listener.h @@ -34,8 +34,45 @@ class Timer { public: + /** + * Empty constructor, call start method to start the thread timer + **/ + Timer() = default; + + /** + * Starts async thread, which call 'timer' method every 's' seconds + **/ Timer(double s, std::function timer) { + start(s, timer); + } + + /** + * Destructor, stops the running thread + **/ + ~Timer() + { + stop(); + + if (timer_thread.joinable()) + { + timer_thread.join(); + } + } + + /** + * Starts async thread, which call 'timer' method every 's' seconds + * Can be called only if default constructor is used, to start the thread + * Can be called only once per class instance + **/ + void start(double s, std::function timer) + { + if (timer_thread.joinable()) + { + NebulaLog::error("TMR", "Trying to start Timer thread twice!"); + return; + } + end = false; timer_thread = std::thread([&, s, timer]{ @@ -58,15 +95,12 @@ public: } } }); - }; - - ~Timer() - { - stop(); - - timer_thread.join(); } + /** + * Stops the running thread. It's not necessery to call this method, + * the thread is stopped in destructor + **/ void stop() { std::unique_lock ul(lock); @@ -75,6 +109,7 @@ public: cond.notify_one(); } + private: std::atomic end; diff --git a/include/MarketPlace.h b/include/MarketPlace.h index 331bfd5c28..427cb41f8e 100644 --- a/include/MarketPlace.h +++ b/include/MarketPlace.h @@ -30,6 +30,8 @@ class MarketPlace : public PoolObjectSQL { public: + virtual ~MarketPlace() = default; + /** * Function to print the MarketPlace object into a string in XML format * @param xml the resulting XML string @@ -184,8 +186,6 @@ private: int umask, MarketPlaceTemplate* mp_template); - virtual ~MarketPlace() = default; - // ************************************************************************* // DataBase implementation (Private) // ************************************************************************* diff --git a/include/MarketPlaceApp.h b/include/MarketPlaceApp.h index 6870f4dc05..7d814ae0bd 100644 --- a/include/MarketPlaceApp.h +++ b/include/MarketPlaceApp.h @@ -127,6 +127,8 @@ public: */ static Type str_to_type(std::string& str_type); + virtual ~MarketPlaceApp() = default; + /** * Function to print the MarketPlaceApp object into a string in XML format * @param xml the resulting XML string @@ -355,8 +357,6 @@ private: int umask, MarketPlaceAppTemplate* app_template); - virtual ~MarketPlaceApp() = default; - // ************************************************************************* // DataBase implementation (Private) // ************************************************************************* diff --git a/include/MarketPlaceAppPool.h b/include/MarketPlaceAppPool.h index 9af9acc97b..96d155028b 100644 --- a/include/MarketPlaceAppPool.h +++ b/include/MarketPlaceAppPool.h @@ -83,53 +83,55 @@ public: int import(const std::string& t64, int mp_id, const std::string& mp_name, int& app_id, std::string& error_str); - /** - * Function to get a MarketPlaceApp from the pool - * @param oid MarketPlaceApp unique id - * @param lock locks the MarketPlaceApp mutex - * @return a pointer to the MarketPlaceApp, 0 if not loaded - */ - MarketPlaceApp * get(int oid) - { - return static_cast(PoolSQL::get(oid)); - }; - - /** - * Function to get a read only MarketPlaceApp from the pool - * @param oid MarketPlaceApp unique id - * @return a pointer to the MarketPlaceApp, 0 if not loaded - */ - MarketPlaceApp * get_ro(int oid) - { - return static_cast(PoolSQL::get_ro(oid)); - }; - /** * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param name of the object - * @param uid id of owner - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the MarketPlaceApp unique identifier + * @return a pointer to the MarketPlaceApp, nullptr in case of failure */ - MarketPlaceApp * get(const std::string& name, int uid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(name, uid)); - }; + return PoolSQL::get(oid); + } /** * Gets a read only object from the pool (if needed the object is loaded from the - * database). + * database). No object lock, other threads may work with the same object. + * @param oid the MarketPlaceApp unique identifier + * @return a pointer to the MarketPlaceApp, nullptr in case of failure + */ + std::unique_ptr get_ro(int oid) + { + return PoolSQL::get_ro(oid); + } + + /** + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param name of the object * @param uid id of owner * * @return a pointer to the object, 0 in case of failure */ - MarketPlaceApp * get_ro(const std::string& name, int uid) + std::unique_ptr get(const std::string& name, int uid) { - return static_cast(PoolSQL::get_ro(name, uid)); - }; + return PoolSQL::get(name, uid); + } + + /** + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param name of the object + * @param uid id of owner + * + * @return a pointer to the object, 0 in case of failure + */ + std::unique_ptr get_ro(const std::string& name, int uid) + { + return PoolSQL::get_ro(name, uid); + } /** * Bootstraps the database table(s) associated to the MarketPlace pool diff --git a/include/MarketPlacePool.h b/include/MarketPlacePool.h index b2c8b3e595..778e883fcf 100644 --- a/include/MarketPlacePool.h +++ b/include/MarketPlacePool.h @@ -60,28 +60,27 @@ public: std::string& error_str); /** - * Function to get a MarketPlace from the pool, the object is loaded if not - * in memory - * @param oid MarketPlace unique id - * @param lock locks the MarketPlace mutex - * @return a pointer to the MarketPlace, 0 if not loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the MarketPlace unique identifier + * @return a pointer to the MarketPlace, nullptr in case of failure */ - MarketPlace * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read only MarketPlace from the pool, the object is loaded if not - * in memory - * @param oid MarketPlace unique id - * @param lock locks the MarketPlace mutex - * @return a pointer to the MarketPlace, 0 if not loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the MarketPlace unique identifier + * @return a pointer to the MarketPlace, nullptr in case of failure */ - MarketPlace * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** Update a particular MarketPlace * @param objsql points to the market diff --git a/include/Nebula.h b/include/Nebula.h index c99ab48766..4665ca14db 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -500,51 +500,41 @@ public: { if ( uid != -1 ) { - User * user = upool->get_ro(uid); + auto user = upool->get_ro(uid); - if ( user == 0 ) + if (!user) { return -1; } - const VectorAttribute * uconf; + auto uconf = user->get_template_attribute("OPENNEBULA"); - uconf = user->get_template_attribute("OPENNEBULA"); - - if ( uconf != 0 ) + if ( uconf != nullptr ) { if ( uconf->vector_value(name, value) == 0 ) { - user->unlock(); return 0; } } - - user->unlock(); } - Group * group = gpool->get_ro(gid); + auto group = gpool->get_ro(gid); - if ( group == 0 ) + if (!group) { return -1; } - const VectorAttribute * gconf; + auto gconf = group->get_template_attribute("OPENNEBULA"); - gconf = group->get_template_attribute("OPENNEBULA"); - - if ( gconf != 0 ) + if ( gconf != nullptr ) { if ( gconf->vector_value(name, value) == 0 ) { - group->unlock(); return 0; } } - group->unlock(); - nebula_configuration->get(name, value); return 0; diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 8a5ea80ee6..9af53916ce 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -181,6 +181,7 @@ public: lock_owner(-1), lock_req_id(-1), lock_time(0), + ro(false), mutex(0), table(_table) { @@ -188,6 +189,11 @@ public: virtual ~PoolObjectSQL() { + if (!ro && mutex != 0) + { + pthread_mutex_unlock(mutex); + } + delete obj_template; }; @@ -318,21 +324,6 @@ public: int _other_a, std::string& error_str); - /* --------------------------------------------------------------------- */ - /** - * Function to unlock the object. It also frees associated resources. Object - * cannot be access after unlocking it - */ - void unlock() - { - if (!ro && mutex != 0) - { - pthread_mutex_unlock(mutex); - } - - delete this; - }; - /** * Function to print the object into a string in XML format * base64 encoded diff --git a/include/PoolSQL.h b/include/PoolSQL.h index 492c95616b..42a3465343 100644 --- a/include/PoolSQL.h +++ b/include/PoolSQL.h @@ -18,6 +18,7 @@ #define POOL_SQL_H_ #include +#include #include "SqlDB.h" #include "PoolObjectSQL.h" @@ -56,21 +57,68 @@ public: /** * Gets an object from the pool (if needed the object is loaded from the - * database). + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param oid the object unique identifier - * - * @return a pointer to the object, 0 in case of failure + * @return a pointer to the object, nullptr in case of failure */ - PoolObjectSQL * get(int oid); + template + std::unique_ptr get(int oid) + { + if ( oid < 0 ) + { + return nullptr; + } + + pthread_mutex_t * object_lock = cache.lock_line(oid); + + std::unique_ptr objectsql(static_cast(create())); + + objectsql->oid = oid; + + objectsql->ro = false; + + objectsql->mutex = object_lock; + + int rc = objectsql->select(db); + + if ( rc != 0 ) + { + return nullptr; + } + + return objectsql; + } /** * Gets a read only object from the pool (if needed the object is loaded from the - * database). + * database). No object lock, other threads may work with the same object. * @param oid the object unique identifier - * - * @return a pointer to the object, 0 in case of failure + * @return a pointer to the object, nullptr in case of failure */ - PoolObjectSQL * get_ro(int oid); + template + std::unique_ptr get_ro(int oid) + { + if ( oid < 0 ) + { + return nullptr; + } + + std::unique_ptr objectsql(static_cast(create())); + + objectsql->oid = oid; + + objectsql->ro = true; + + int rc = objectsql->select(db); + + if ( rc != 0 ) + { + return nullptr; + } + + return objectsql; + } /** * Check if there is an object with the same for a given user @@ -289,13 +337,25 @@ protected: /** * Gets an object from the pool (if needed the object is loaded from the - * database). + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param name of the object * @param uid id of owner * * @return a pointer to the object, 0 in case of failure */ - PoolObjectSQL * get(const std::string& name, int uid); + template + std::unique_ptr get(const std::string& name, int uid) + { + int oid = PoolObjectSQL::select_oid(db, table.c_str(), name, uid); + + if ( oid == -1 ) + { + return nullptr; + } + + return get(oid); + } /** * Gets a read only object from the pool (if needed the object is loaded from the @@ -305,7 +365,18 @@ protected: * * @return a pointer to the object, 0 in case of failure */ - PoolObjectSQL * get_ro(const std::string& name, int uid); + template + std::unique_ptr get_ro(const std::string& name, int uid) + { + int oid = PoolObjectSQL::select_oid(db, table.c_str(), name, uid); + + if ( oid == -1 ) + { + return nullptr; + } + + return get_ro(oid); + } /** * Pointer to the database. diff --git a/include/RaftManager.h b/include/RaftManager.h index c9f9efcf14..a4efa3aa09 100644 --- a/include/RaftManager.h +++ b/include/RaftManager.h @@ -385,6 +385,8 @@ private: */ Timer timer_thread; + Timer purge_thread; + //-------------------------------------------------------------------------- // Volatile log index variables // - commit, highest log known to be committed @@ -419,10 +421,15 @@ private: // Internal Raft functions // ------------------------------------------------------------------------- /** - * This function is executed periodically to purge the state log + * This function is executed periodically to vote leader */ void timer_action(); + /** + * This function is executed periodically to purge the state log + */ + void purge_action(); + /** * @param s the state to check * @return true if the server states matches the provided one diff --git a/include/RequestManagerChown.h b/include/RequestManagerChown.h index 598ea219e9..26404540cd 100644 --- a/include/RequestManagerChown.h +++ b/include/RequestManagerChown.h @@ -64,20 +64,21 @@ protected: void request_execute(xmlrpc_c::paramList const& _paramList, RequestAttributes& att) override; - PoolObjectSQL * get_and_quota(int oid, - int new_uid, - int new_gid, - RequestAttributes& att) + std::unique_ptr get_and_quota(int oid, + int new_uid, + int new_gid, + RequestAttributes& att) { return get_and_quota(oid, new_uid, new_gid, att, pool, auth_object); } - PoolObjectSQL * get_and_quota(int oid, - int new_uid, - int new_gid, - RequestAttributes& att, - PoolSQL * pool, - PoolObjectSQL::ObjectType auth_object); + std::unique_ptr get_and_quota( + int oid, + int new_uid, + int new_gid, + RequestAttributes& att, + PoolSQL * pool, + PoolObjectSQL::ObjectType auth_object); /** * Checks if the new owner cannot has other object with the same name (if diff --git a/include/RequestManagerClone.h b/include/RequestManagerClone.h index c87811eab2..b3a812a2d4 100644 --- a/include/RequestManagerClone.h +++ b/include/RequestManagerClone.h @@ -205,17 +205,15 @@ protected: int pool_allocate(int sid, Template * tmpl, int& id, RequestAttributes& att) override { DocumentPool * docpool = static_cast(pool); - Document * doc = docpool->get_ro(sid); + auto doc = docpool->get_ro(sid); - if ( doc == 0 ) + if (!doc) { return -1; } int dtype = doc->get_document_type(); - doc->unlock(); - return docpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, dtype, tmpl, &id, att.resp_msg); }; diff --git a/include/RequestManagerCluster.h b/include/RequestManagerCluster.h index 665510f051..945bec0f75 100644 --- a/include/RequestManagerCluster.h +++ b/include/RequestManagerCluster.h @@ -100,7 +100,8 @@ protected: virtual int del_object(Cluster* cluster, int id, std::string& error_msg) = 0; - virtual void get(int oid, PoolObjectSQL ** object, Clusterable ** cluster_obj) = 0; + virtual void get(int oid, std::unique_ptr& object, + Clusterable ** cluster_obj) = 0; }; /* ------------------------------------------------------------------------- */ @@ -200,26 +201,27 @@ public: ~RequestManagerClusterDatastore(){}; - virtual int add_object( + int add_object( Cluster* cluster, int id, std::string& error_msg) override { return clpool->add_to_cluster(PoolObjectSQL::DATASTORE, cluster, id, error_msg); - }; + } - virtual int del_object(Cluster* cluster, int id, std::string& error_msg) override + int del_object(Cluster* cluster, int id, std::string& error_msg) override { return clpool->del_from_cluster(PoolObjectSQL::DATASTORE, cluster, id, error_msg); - }; + } - virtual void get(int oid, PoolObjectSQL ** object, Clusterable ** cluster_obj) override + void get(int oid, std::unique_ptr& object, + Clusterable ** cluster_obj) override { - Datastore * ds = dspool->get(oid); + auto ds = dspool->get(oid); - *object = static_cast(ds); - *cluster_obj = static_cast(ds); - }; + *cluster_obj = static_cast(ds.get()); + object = std::move(ds); + } }; /* ------------------------------------------------------------------------- */ @@ -285,26 +287,27 @@ public: ~RequestManagerClusterVNet(){}; - virtual int add_object( + int add_object( Cluster* cluster, int id, std::string& error_msg) override { return clpool->add_to_cluster(PoolObjectSQL::NET, cluster, id, error_msg); - }; + } - virtual int del_object(Cluster* cluster, int id, std::string& error_msg) override + int del_object(Cluster* cluster, int id, std::string& error_msg) override { return clpool->del_from_cluster(PoolObjectSQL::NET, cluster, id, error_msg); - }; + } - virtual void get(int oid, PoolObjectSQL ** object, Clusterable ** cluster_obj) override + void get(int oid, std::unique_ptr& object, + Clusterable ** cluster_obj) override { - VirtualNetwork * vnet = vnpool->get(oid); + auto vnet = vnpool->get(oid); - *object = static_cast(vnet); - *cluster_obj = static_cast(vnet); - }; + *cluster_obj = static_cast(vnet.get()); + object = std::move(vnet); + } }; /* ------------------------------------------------------------------------- */ diff --git a/include/RequestManagerDelete.h b/include/RequestManagerDelete.h index 1632da4f9d..1ee3c684b3 100644 --- a/include/RequestManagerDelete.h +++ b/include/RequestManagerDelete.h @@ -49,7 +49,9 @@ protected: /* -------------------------------------------------------------------- */ - virtual int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att); + virtual int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att); virtual std::set get_cluster_ids(PoolObjectSQL * object) const { @@ -87,7 +89,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -129,7 +133,9 @@ protected: return clpool->del_from_cluster(PoolObjectSQL::NET, cluster, id, error_msg); }; - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -152,7 +158,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -181,7 +189,9 @@ protected: return clpool->del_from_cluster(PoolObjectSQL::HOST, cluster, id, error_msg); }; - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -196,7 +206,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -213,7 +225,9 @@ protected: GroupPool * gpool; - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -238,7 +252,9 @@ public: return clpool->del_from_cluster(PoolObjectSQL::DATASTORE, cluster, id, error_msg); }; - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -253,7 +269,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -279,7 +297,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -294,7 +314,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -319,7 +341,9 @@ public: ~VirtualRouterDelete() = default; protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -334,7 +358,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ @@ -349,7 +375,9 @@ public: protected: - int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att) override; + int drop(std::unique_ptr obj, + bool recursive, + RequestAttributes& att) override; }; /* ------------------------------------------------------------------------- */ diff --git a/include/RequestManagerDropDB.h b/include/RequestManagerDropDB.h index 5744405903..f0b11f9940 100644 --- a/include/RequestManagerDropDB.h +++ b/include/RequestManagerDropDB.h @@ -46,9 +46,9 @@ protected: return; } - PoolObjectSQL * object = pool->get(oid); + auto object = pool->get(oid); - if ( object == 0 ) + if (!object) { att.resp_id = oid; failure_response(NO_EXISTS, att); @@ -56,7 +56,7 @@ protected: return; } - if ( pool->drop(object, error) != 0 ) + if ( pool->drop(object.get(), error) != 0 ) { att.resp_msg = error; failure_response(ACTION, att); @@ -66,8 +66,6 @@ protected: success_response(oid, att); } - object->unlock(); - return; } }; diff --git a/include/RequestManagerUpdateDB.h b/include/RequestManagerUpdateDB.h index 6101b09dfe..4d329bd2a1 100644 --- a/include/RequestManagerUpdateDB.h +++ b/include/RequestManagerUpdateDB.h @@ -65,7 +65,7 @@ protected: ErrorCode request_execute(int oid, const std::string& xml, RequestAttributes& att) { - PoolObjectSQL * object = pool->get(oid); + auto object = pool->get(oid); if ( object == 0 ) { @@ -80,7 +80,6 @@ protected: if ( object->from_xml(xml) != 0 ) { object->from_xml(old_xml); - object->unlock(); att.resp_msg = "Cannot update object from XML"; return INTERNAL; @@ -89,15 +88,12 @@ protected: if ( object->get_oid() != oid ) { object->from_xml(old_xml); - object->unlock(); att.resp_msg = "Consistency check failed"; return INTERNAL; } - pool->update(object); - - object->unlock(); + pool->update(object.get()); return SUCCESS; } diff --git a/include/RequestManagerVirtualMachine.h b/include/RequestManagerVirtualMachine.h index 777df34a63..88fb4566bf 100644 --- a/include/RequestManagerVirtualMachine.h +++ b/include/RequestManagerVirtualMachine.h @@ -101,9 +101,9 @@ protected: int ds_id, RequestAttributes& att); - VirtualMachine * get_vm(int id, RequestAttributes& att); + std::unique_ptr get_vm(int id, RequestAttributes& att); - VirtualMachine * get_vm_ro(int id, RequestAttributes& att); + std::unique_ptr get_vm_ro(int id, RequestAttributes& att); }; /* ------------------------------------------------------------------------- */ diff --git a/include/SecurityGroup.h b/include/SecurityGroup.h index 8073ff24aa..0a751f013c 100644 --- a/include/SecurityGroup.h +++ b/include/SecurityGroup.h @@ -27,6 +27,8 @@ class SecurityGroup : public PoolObjectSQL { public: + virtual ~SecurityGroup() = default; + /** * Function to print the SecurityGroup object into a string in XML format * @param xml the resulting XML string @@ -171,8 +173,6 @@ private: int _umask, Template* sgroup_template); - virtual ~SecurityGroup() = default; - /** * Check that a rule is valid * @param rule as a VectorAttribute diff --git a/include/SecurityGroupPool.h b/include/SecurityGroupPool.h index 34c29126c8..eb7ac1d1f6 100644 --- a/include/SecurityGroupPool.h +++ b/include/SecurityGroupPool.h @@ -60,27 +60,27 @@ public: std::string& error_str); /** - * Function to get a SecurityGroup from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid SecurityGroup unique id - * @param lock locks the SecurityGroup mutex - * @return a pointer to the SecurityGroup, 0 if the SecurityGroup could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the SecurityGroup unique identifier + * @return a pointer to the SecurityGroup, nullptr in case of failure */ - SecurityGroup * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read only SecurityGroup from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid SecurityGroup unique id - * @return a pointer to the SecurityGroup, 0 if the SecurityGroup could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the SecurityGroup unique identifier + * @return a pointer to the SecurityGroup, nullptr in case of failure */ - SecurityGroup * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** Update a particular SecurityGroup * @param securitygroup pointer to SecurityGroup diff --git a/include/User.h b/include/User.h index 0a036da0cf..a7577484f9 100644 --- a/include/User.h +++ b/include/User.h @@ -35,6 +35,8 @@ class User : public PoolObjectSQL { public: + virtual ~User() = default; + /** * Characters that can not be in a name */ @@ -267,6 +269,7 @@ private: // ------------------------------------------------------------------------- friend class UserPool; + friend class PoolSQL; // ------------------------------------------------------------------------- // User Attributes @@ -326,6 +329,7 @@ private: return db->exec_local_wr(oss_user); } +protected: /** * Reads the User (identified with its OID) from the database. * @param db pointer to the db @@ -391,8 +395,6 @@ protected: obj_template = new UserTemplate; } - virtual ~User() = default; - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/UserPool.h b/include/UserPool.h index 7a927fe6ad..8d1b364ea7 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -70,80 +70,80 @@ public: int drop(PoolObjectSQL * objsql, std::string& error_msg); /** - * Function to get a User from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid User unique id - * @param lock locks the User mutex - * @return a pointer to the User, 0 if the User could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the User unique identifier + * @return a pointer to the User, nullptr in case of failure */ - User * get(int oid) + std::unique_ptr get(int oid) { - User * u = static_cast(PoolSQL::get(oid)); + auto u = PoolSQL::get(oid); - if ( u != 0 ) + if (u) { u->session = get_session_token(oid); } return u; - }; + } /** - * Function to get a read only User from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid User unique id - * @return a pointer to the User, 0 if the User could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the User unique identifier + * @return a pointer to the User, nullptr in case of failure */ - User * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - User * u = static_cast(PoolSQL::get_ro(oid)); + auto u = PoolSQL::get_ro(oid); - if ( u != 0 ) + if (u) { u->session = get_session_token(oid); } return u; - }; + } /** - * Function to get a User from the pool, if the object is not in memory - * it is loaded from the DB + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param username - * @param lock locks the User mutex * @return a pointer to the User, 0 if the User could not be loaded */ - User * get(std::string name) + std::unique_ptr get(std::string name) { // The owner is set to -1, because it is not used in the key() method - User * u = static_cast(PoolSQL::get(name,-1)); + auto u = PoolSQL::get(name,-1); - if ( u != 0 ) + if (u) { u->session = get_session_token(u->oid); } return u; - }; + } /** - * Function to get a read only User from the pool, if the object is not in memory - * it is loaded from the DB + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. * @param username * @return a pointer to the User, 0 if the User could not be loaded */ - User * get_ro(std::string name) + std::unique_ptr get_ro(std::string name) { // The owner is set to -1, because it is not used in the key() method - User * u = static_cast(PoolSQL::get_ro(name,-1)); + auto u = PoolSQL::get_ro(name, -1); - if ( u != 0 ) + if (u) { u->session = get_session_token(u->oid); } return u; - }; + } /** * Function to get the token password of an user from the pool @@ -275,7 +275,7 @@ private: /** * Function to authenticate internal (known) users */ - bool authenticate_internal(User * user, + bool authenticate_internal(std::unique_ptr user, const std::string& token, std::string& password, int& user_id, @@ -288,7 +288,7 @@ private: /** * Function to authenticate internal users using a server driver */ - bool authenticate_server(User * user, + bool authenticate_server(std::unique_ptr user, const std::string& token, std::string& password, int& user_id, diff --git a/include/VMGroup.h b/include/VMGroup.h index 64bc5a523f..1c04dc3018 100644 --- a/include/VMGroup.h +++ b/include/VMGroup.h @@ -51,6 +51,8 @@ enum class VMGroupPolicy; class VMGroup : public PoolObjectSQL { public: + virtual ~VMGroup() = default; + /** * Function to print the VMGroup object into a string in XML format * @param xml the resulting XML string @@ -114,8 +116,6 @@ private: const std::string& _uname, const std::string& _gname, int _umask, Template * group_template); - virtual ~VMGroup() = default; - // ------------------------------------------------------------------------- // Role Management // ------------------------------------------------------------------------- diff --git a/include/VMGroupPool.h b/include/VMGroupPool.h index ba4dbff659..f9b4900807 100644 --- a/include/VMGroupPool.h +++ b/include/VMGroupPool.h @@ -53,30 +53,30 @@ public: std::string& error_str); /** - * Function to get a VMGroup from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid VMGroup unique id - * @param lock locks the VMGroup mutex - * @return a pointer to the VMGroup, 0 if the VMGroup could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the VMGroup unique identifier + * @return a pointer to the VMGroup, nullptr in case of failure */ - VMGroup * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** * Gets an object from the pool (if needed the object is loaded from the - * database). + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param name of the object * @param uid id of owner - * @param lock locks the object if true * * @return a pointer to the object, 0 in case of failure */ - VMGroup * get(const std::string& name, int uid) + std::unique_ptr get(const std::string& name, int uid) { - return static_cast(PoolSQL::get(name, uid)); - }; + return PoolSQL::get(name, uid); + } /** Update a VMGroup * @param vmgroup pointer to VMGroup @@ -155,7 +155,8 @@ private: * @param va the VectorAttribute * @param _uid default uid to look for the VMGroup */ - VMGroup * get_from_attribute(const VectorAttribute *va, int _uid); + std::unique_ptr get_from_attribute(const VectorAttribute *va, + int _uid); }; #endif /*VMGROUP_POOL_H_*/ diff --git a/include/VMTemplate.h b/include/VMTemplate.h index 28dfc7ad6b..d2cff6ff58 100644 --- a/include/VMTemplate.h +++ b/include/VMTemplate.h @@ -30,6 +30,8 @@ class VMTemplate : public PoolObjectSQL { public: + virtual ~VMTemplate() = default; + /** * Function to print the VMTemplate object into a string in XML format * @param xml the resulting XML string @@ -179,8 +181,6 @@ protected: int umask, VirtualMachineTemplate * _template_contents); - virtual ~VMTemplate() = default; - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/VMTemplatePool.h b/include/VMTemplatePool.h index 29ce4cdf42..1d77910d85 100644 --- a/include/VMTemplatePool.h +++ b/include/VMTemplatePool.h @@ -57,28 +57,26 @@ public: /** * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the VMTemplate unique identifier + * @return a pointer to the VMTemplate, nullptr in case of failure */ - VMTemplate * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * - * @return a pointer to the object, 0 in case of failure + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the VMTemplate unique identifier + * @return a pointer to the VMTemplate, nullptr in case of failure */ - VMTemplate * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** * Dumps the pool in XML format. A filter can be also added to the diff --git a/include/VNTemplate.h b/include/VNTemplate.h index d36b276fd7..b6d307891e 100644 --- a/include/VNTemplate.h +++ b/include/VNTemplate.h @@ -30,6 +30,8 @@ class VNTemplate : public PoolObjectSQL { public: + virtual ~VNTemplate() = default; + /** * Function to print the VNTemplate object into a string in XML format * @param xml the resulting XML string @@ -115,8 +117,6 @@ protected: int umask, VirtualNetworkTemplate * _template_contents); - virtual ~VNTemplate() = default; - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/VNTemplatePool.h b/include/VNTemplatePool.h index d5f495adbb..83a9f80cc9 100644 --- a/include/VNTemplatePool.h +++ b/include/VNTemplatePool.h @@ -57,28 +57,26 @@ public: /** * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid theVNTemplate unique identifier + * @return a pointer to the VNTemplate, nullptr in case of failure */ - VNTemplate * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * - * @return a pointer to the object, 0 in case of failure + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the VNTemplate unique identifier + * @return a pointer to the VNTemplate, nullptr in case of failure */ - VNTemplate * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** * Dumps the pool in XML format. A filter can be also added to the diff --git a/include/Vdc.h b/include/Vdc.h index 4d90b96777..fb78171d47 100644 --- a/include/Vdc.h +++ b/include/Vdc.h @@ -155,6 +155,8 @@ class Vdc : public PoolObjectSQL { public: + virtual ~Vdc() = default; + /** * Function to print the Vdc object into a string in XML format * @param xml the resulting XML string @@ -322,8 +324,6 @@ private: Vdc(int id, Template* vdc_template); - virtual ~Vdc() = default; - // ************************************************************************* // Attributes (Private) // ************************************************************************* diff --git a/include/VdcPool.h b/include/VdcPool.h index c89f552dee..a4fd550fde 100644 --- a/include/VdcPool.h +++ b/include/VdcPool.h @@ -48,16 +48,16 @@ public: std::string& error_str); /** - * Function to get a Vdc from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Vdc unique id - * @param lock locks the Vdc mutex - * @return a pointer to the Vdc, 0 if the Vdc could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Vdc unique identifier + * @return a pointer to the Vdc, nullptr in case of failure */ - Vdc * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** Update a particular Vdc * @param vdc pointer to Vdc diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 4628dcc20c..0b170c04c4 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -149,6 +149,8 @@ public: static std::string& lcm_state_to_str(std::string& st, LcmState state); + virtual ~VirtualMachine(); + /** * Returns the VM state to string, using the lcm state if the current state * is ACTIVE. @@ -1301,9 +1303,9 @@ public: { VirtualMachineDisk * disk = disks.delete_attach(); - if (disk == 0) + if (disk == nullptr) { - return 0; + return nullptr; } obj_template->remove(disk->vector_attribute()); @@ -1642,6 +1644,7 @@ private: // Friends // ------------------------------------------------------------------------- friend class VirtualMachinePool; + friend class PoolSQL; // ************************************************************************* // Virtual Machine Attributes @@ -2147,8 +2150,6 @@ protected: int umask, VirtualMachineTemplate * _vm_template); - virtual ~VirtualMachine(); - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/VirtualMachineManagerDriver.h b/include/VirtualMachineManagerDriver.h index abb2a06696..350f966449 100644 --- a/include/VirtualMachineManagerDriver.h +++ b/include/VirtualMachineManagerDriver.h @@ -184,7 +184,7 @@ protected: template bool get_attribute(const VirtualMachine * vm, const Host * host, - const Cluster* cluster, + const Cluster * cluster, const std::string& name, const std::string& vname, T& value) const diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index e87696f4fd..da172bbf1d 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -67,34 +67,32 @@ public: bool on_hold = false); /** - * Function to get a VM from the pool, if the object is not in memory - * it is loade from the DB - * @param oid VM unique id - * @param lock locks the VM mutex - * @return a pointer to the VM, 0 if the VM could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the VM unique identifier + * @return a pointer to the VM, nullptr in case of failure */ - VirtualMachine * get( - int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** - * Function to get a read only VM from the pool, if the object is not - * in memory it is loade from the DB - * @param oid VM unique id - * @return a pointer to the VM, 0 if the VM could not be loaded + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the VM unique identifier + * @return a pointer to the VM, nullptr in case of failure */ - VirtualMachine * get_ro( - int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** * Function to get a VM from the pool, string version for VM ID */ - VirtualMachine * get(const std::string& oid_s) + std::unique_ptr get(const std::string& oid_s) { std::istringstream iss(oid_s); int oid; @@ -106,13 +104,13 @@ public: return 0; } - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** * Function to get a read only VM from the pool, string version for VM ID */ - VirtualMachine * get_ro(const std::string& oid_s) + std::unique_ptr get_ro(const std::string& oid_s) { std::istringstream iss(oid_s); int oid; @@ -124,8 +122,8 @@ public: return 0; } - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** * Updates a VM in the data base. The VM SHOULD be locked. It also updates @@ -375,16 +373,16 @@ public: * Deletes the DISK that was in the process of being attached. Releases * Images and updates usage quotas * - * @param vid VM id + * @param vm unique_ptr to VM, will be release in the method */ - void delete_attach_disk(int vid); + void delete_attach_disk(std::unique_ptr vm); /** * Deletes the NIC that was in the process of being attached/detached * - * @param vid VM id + * @param vm unique_ptr to VM, will be release in the method */ - void delete_attach_nic(int vid); + void delete_attach_nic(std::unique_ptr vm); /** * Deletes an entry in the HV-2-vmid mapping table for imported VMs diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index e4084081c4..5560656047 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -197,6 +197,8 @@ public: // Virtual Network Public Methods // ************************************************************************* + virtual ~VirtualNetwork() = default; + /** * Factory method for virtual network templates */ @@ -808,8 +810,6 @@ private: const std::set &_cluster_ids, VirtualNetworkTemplate * _vn_template = 0); - virtual ~VirtualNetwork() = default; - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index 70849a8c06..c41460f4d6 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -81,57 +81,55 @@ public: return PoolSQL::drop(vn, error_msg); }; - /** - * Function to get a VN from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid VN unique id - * @param lock locks the VN mutex - * @return a pointer to the VN, 0 if the VN could not be loaded - */ - VirtualNetwork * get(int oid) - { - return static_cast(PoolSQL::get(oid)); - }; - - /** - * Function to get a read only VN from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid VN unique id - * @param lock locks the VN mutex - * @return a pointer to the VN, 0 if the VN could not be loaded - */ - VirtualNetwork * get_ro(int oid) - { - return static_cast(PoolSQL::get_ro(oid)); - }; - /** * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param name of the object - * @param uid id of owner - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the VN unique identifier + * @return a pointer to the VN, nullptr in case of failure */ - VirtualNetwork * get(const std::string& name, int uid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(name,uid)); - }; + return PoolSQL::get(oid); + } /** * Gets a read only object from the pool (if needed the object is loaded from the - * database). + * database). No object lock, other threads may work with the same object. + * @param oid the VN unique identifier + * @return a pointer to the VN, nullptr in case of failure + */ + std::unique_ptr get_ro(int oid) + { + return PoolSQL::get_ro(oid); + } + + /** + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. * @param name of the object * @param uid id of owner - * @param lock locks the object if true * * @return a pointer to the object, 0 in case of failure */ - VirtualNetwork * get_ro(const std::string& name, int uid) + std::unique_ptr get(const std::string& name, int uid) { - return static_cast(PoolSQL::get_ro(name,uid)); - }; + return PoolSQL::get(name,uid); + } + + /** + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param name of the object + * @param uid id of owner + * + * @return a pointer to the object, 0 in case of failure + */ + std::unique_ptr get_ro(const std::string& name, int uid) + { + return PoolSQL::get_ro(name,uid); + } /** * Bootstraps the database table(s) associated to the VirtualNetwork pool @@ -322,16 +320,17 @@ private: * Function to get a VirtualNetwork by its name, as provided by a VM * template */ - VirtualNetwork * get_nic_by_name(VirtualMachineNic * nic, - const std::string& name, - int _uidi, - bool ro, - std::string& error); + std::unique_ptr get_nic_by_name(VirtualMachineNic * nic, + const std::string& name, + int _uidi, + bool ro, + std::string& error); /** * Function to get a VirtualNetwork by its id, as provided by a VM template */ - VirtualNetwork * get_nic_by_id(const std::string& id_s, bool ro, - std::string& error); + std::unique_ptr get_nic_by_id(const std::string& id_s, + bool ro, + std::string& error); //-------------------------------------------------------------------------- // VLAN ID management functions diff --git a/include/VirtualRouter.h b/include/VirtualRouter.h index 6b64bfff2f..b72c0262b7 100644 --- a/include/VirtualRouter.h +++ b/include/VirtualRouter.h @@ -34,6 +34,8 @@ class VirtualRouter : public PoolObjectSQL { public: + virtual ~VirtualRouter() = default; + /** * Function to print the VirtualRouter object into a string in XML format * @param xml the resulting XML string @@ -225,8 +227,6 @@ private: int umask, Template * _template_contents); - virtual ~VirtualRouter() = default; - // ************************************************************************* // DataBase implementation // ************************************************************************* diff --git a/include/VirtualRouterPool.h b/include/VirtualRouterPool.h index 2069ee886b..b127e60465 100644 --- a/include/VirtualRouterPool.h +++ b/include/VirtualRouterPool.h @@ -64,29 +64,26 @@ public: /** * Gets an object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the VirtualRouter unique identifier + * @return a pointer to the VirtualRouter, nullptr in case of failure */ - VirtualRouter * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } /** * Gets a read only object from the pool (if needed the object is loaded from the - * database). - * @param oid the object unique identifier - * @param lock locks the object if true - * - * @return a pointer to the object, 0 in case of failure + * database). No object lock, other threads may work with the same object. + * @param oid the VirtualRouter unique identifier + * @return a pointer to the VirtualRouter, nullptr in case of failure */ - VirtualRouter * get_ro(int oid) + std::unique_ptr get_ro(int oid) { - return static_cast(PoolSQL::get_ro(oid)); - }; + return PoolSQL::get_ro(oid); + } /** * Dumps the pool in XML format. A filter can be also added to the diff --git a/include/Zone.h b/include/Zone.h index a334004c29..30e0e25c6f 100644 --- a/include/Zone.h +++ b/include/Zone.h @@ -28,6 +28,7 @@ class ZoneServer; class Zone : public PoolObjectSQL { public: + virtual ~Zone(); /** * Function to print the Zone object into a string in XML format @@ -95,8 +96,6 @@ private: // ------------------------------------------------------------------------- Zone(int id, Template* zone_template); - virtual ~Zone(); - // ------------------------------------------------------------------------- // Zone servers // ------------------------------------------------------------------------- diff --git a/include/ZonePool.h b/include/ZonePool.h index 1588b344b9..b5f2a1e193 100644 --- a/include/ZonePool.h +++ b/include/ZonePool.h @@ -48,16 +48,27 @@ public: std::string& error_str); /** - * Function to get a Zone from the pool, if the object is not in memory - * it is loaded from the DB - * @param oid Zone unique id - * @param lock locks the Zone mutex - * @return a pointer to the Zone, 0 if the Zone could not be loaded + * Gets an object from the pool (if needed the object is loaded from the + * database). The object is locked, other threads can't access the same + * object. The lock is released by destructor. + * @param oid the Zone unique identifier + * @return a pointer to the Zone, nullptr in case of failure */ - Zone * get(int oid) + std::unique_ptr get(int oid) { - return static_cast(PoolSQL::get(oid)); - }; + return PoolSQL::get(oid); + } + + /** + * Gets a read only object from the pool (if needed the object is loaded from the + * database). No object lock, other threads may work with the same object. + * @param oid the Zone unique identifier + * @return a pointer to the Zone, nullptr in case of failure + */ + std::unique_ptr get_ro(int oid) + { + return PoolSQL::get_ro(oid); + } /** Update a particular Zone * @param zone pointer to Zone diff --git a/src/cluster/Cluster.cc b/src/cluster/Cluster.cc index 5699baaa38..9e7aa2b75c 100644 --- a/src/cluster/Cluster.cc +++ b/src/cluster/Cluster.cc @@ -99,25 +99,16 @@ int Cluster::get_default_system_ds(const set& ds_set) Nebula& nd = Nebula::instance(); DatastorePool* dspool = nd.get_dspool(); - Datastore* ds; - for (set::const_iterator it = ds_set.begin(); it != ds_set.end(); it++) + for (auto dsid : ds_set) { - ds = dspool->get_ro(*it); - - if (ds == 0) + if ( auto ds = dspool->get_ro(dsid) ) { - continue; + if (ds->get_type() == Datastore::SYSTEM_DS) + { + return dsid; + } } - - if (ds->get_type() == Datastore::SYSTEM_DS) - { - ds->unlock(); - - return *it; - } - - ds->unlock(); } return -1; diff --git a/src/cluster/ClusterPool.cc b/src/cluster/ClusterPool.cc index 63909458e9..b19275cc88 100644 --- a/src/cluster/ClusterPool.cc +++ b/src/cluster/ClusterPool.cc @@ -57,26 +57,24 @@ ClusterPool::ClusterPool(SqlDB * db, allocate(DEFAULT_CLUSTER_NAME, &rc, error_str); - if( rc != DEFAULT_CLUSTER_ID ) + if (rc != DEFAULT_CLUSTER_ID) { goto error_bootstrap; } - Cluster* cluster = get(DEFAULT_CLUSTER_ID); + auto cluster = get(DEFAULT_CLUSTER_ID); - if (cluster == 0) + if (!cluster) { goto error_bootstrap; } - add_to_cluster(PoolObjectSQL::DATASTORE, cluster, DatastorePool::SYSTEM_DS_ID, - error_str); - add_to_cluster(PoolObjectSQL::DATASTORE, cluster, DatastorePool::DEFAULT_DS_ID, - error_str); - add_to_cluster(PoolObjectSQL::DATASTORE, cluster, DatastorePool::FILE_DS_ID, - error_str); - - cluster->unlock(); + add_to_cluster(PoolObjectSQL::DATASTORE, cluster.get(), + DatastorePool::SYSTEM_DS_ID, error_str); + add_to_cluster(PoolObjectSQL::DATASTORE, cluster.get(), + DatastorePool::DEFAULT_DS_ID, error_str); + add_to_cluster(PoolObjectSQL::DATASTORE, cluster.get(), + DatastorePool::FILE_DS_ID, error_str); // User created clusters will start from ID 100 set_lastOID(99); diff --git a/src/datastore/DatastorePool.cc b/src/datastore/DatastorePool.cc index cdd936b43a..0e36e1d2b9 100644 --- a/src/datastore/DatastorePool.cc +++ b/src/datastore/DatastorePool.cc @@ -286,17 +286,15 @@ int DatastorePool::drop(PoolObjectSQL * objsql, string& error_msg) int DatastorePool::disk_attribute(int ds_id, VirtualMachineDisk * disk) { - Datastore * ds = get_ro(ds_id); + auto ds = get_ro(ds_id); - if (ds == 0) + if (!ds) { return -1; } ds->disk_attribute(disk, inherit_attrs); - ds->unlock(); - return 0; } diff --git a/src/dm/DispatchManagerActions.cc b/src/dm/DispatchManagerActions.cc index d48c98dfea..d1deb79dc7 100644 --- a/src/dm/DispatchManagerActions.cc +++ b/src/dm/DispatchManagerActions.cc @@ -34,7 +34,8 @@ using namespace std; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int DispatchManager::deploy(VirtualMachine * vm, const RequestAttributes& ra) +int DispatchManager::deploy(unique_ptr vm, + const RequestAttributes& ra) { ostringstream oss; int vid; @@ -66,7 +67,7 @@ int DispatchManager::deploy(VirtualMachine * vm, const RequestAttributes& ra) vm->set_state(VirtualMachine::ACTIVE); - vmpool->update(vm); + vmpool->update(vm.get()); if ( do_quotas ) { @@ -83,7 +84,7 @@ int DispatchManager::deploy(VirtualMachine * vm, const RequestAttributes& ra) goto error; } - vm->unlock(); + vm.reset(); //force unlock of vm mutex if ( do_quotas ) { @@ -98,15 +99,13 @@ error: << ", wrong state " << vm->state_str() << "."; NebulaLog::log("DiM",Log::ERROR,oss); - vm->unlock(); - return -1; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int DispatchManager::import(VirtualMachine * vm, const RequestAttributes& ra) +int DispatchManager::import(unique_ptr vm, const RequestAttributes& ra) { ostringstream oss; string import_state; @@ -168,11 +167,11 @@ int DispatchManager::import(VirtualMachine * vm, const RequestAttributes& ra) vm->set_running_stime(the_time); - vmpool->update_history(vm); + vmpool->update_history(vm.get()); - vmpool->update(vm); + vmpool->update(vm.get()); - vm->unlock(); + vm.reset(); //force unlock of vm mutex if ( do_quotas ) { @@ -282,7 +281,8 @@ error: /* ************************************************************************** */ /* ************************************************************************** */ -void DispatchManager::free_vm_resources(VirtualMachine * vm, bool check_images) +void DispatchManager::free_vm_resources(unique_ptr vm, + bool check_images) { vector