2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2015-09-23 16:03:22 +03:00
/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems */
2008-06-17 20:27:32 +04:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef DISPATCH_MANAGER_H_
# define DISPATCH_MANAGER_H_
# include "ActionManager.h"
# include "HostPool.h"
# include "VirtualMachinePool.h"
using namespace std ;
extern " C " void * dm_action_loop ( void * arg ) ;
2015-04-27 00:34:26 +03:00
//Forward definitions
class TransferManager ;
class LifeCycleManager ;
class VirtualMachineManager ;
class ImageManager ;
2008-06-17 20:27:32 +04:00
class DispatchManager : public ActionListener
{
public :
2015-04-27 00:34:26 +03:00
DispatchManager ( ) :
hpool ( 0 ) , vmpool ( 0 ) , tm ( 0 ) , vmm ( 0 ) , lcm ( 0 ) , imagem ( 0 )
2008-06-17 20:27:32 +04:00
{
am . addListener ( this ) ;
} ;
2015-04-27 00:34:26 +03:00
~ DispatchManager ( ) { } ;
/**
2015-07-01 22:15:40 +03:00
* Initializes internal pointers to other managers . Must be called when
* all the other managers exist in Nebula : : instance
*/
2015-04-27 00:34:26 +03:00
void init_managers ( ) ;
2008-06-17 20:27:32 +04:00
enum Actions
{
2010-12-31 19:48:50 +03:00
SUSPEND_SUCCESS , /**< Send by LCM when a VM is suspended*/
STOP_SUCCESS , /**< Send by LCM when a VM is stopped*/
2013-04-02 19:01:22 +04:00
UNDEPLOY_SUCCESS , /**< Send by LCM when a VM is undeployed and saved*/
2012-09-04 20:30:53 +04:00
POWEROFF_SUCCESS , /**< Send by LCM when a VM is powered off */
2010-12-31 19:48:50 +03:00
DONE , /**< Send by LCM when a VM is shut down*/
RESUBMIT , /**< Send by LCM when a VM is ready for resubmission*/
2008-06-17 20:27:32 +04:00
FINALIZE
} ;
/**
* Triggers specific actions to the Dispatch Manager . This function
* wraps the ActionManager trigger function .
* @ param action the DM action
2010-07-14 20:11:29 +04:00
* @ param vid VM unique id . This is the argument of the passed to the
2008-06-17 20:27:32 +04:00
* invoked action .
*/
void trigger (
Actions action ,
int _vid ) ;
/**
2010-07-14 20:11:29 +04:00
* This functions creates a new thread for the Dispatch Manager . This
2008-06-17 20:27:32 +04:00
* thread will wait in an action loop till it receives ACTION_FINALIZE .
* @ return 0 on success .
*/
int start ( ) ;
2010-07-14 20:11:29 +04:00
2008-06-17 20:27:32 +04:00
/**
* Gets the thread identification .
* @ return pthread_t for the manager thread ( that in the action loop ) .
*/
pthread_t get_thread_id ( ) const
{
return dm_thread ;
} ;
//--------------------------------------------------------------------------
// DM Actions, the RM and the Scheduler will invoke this methods
//--------------------------------------------------------------------------
/**
* Deploys a VM . A new history record MUST be added before calling this
* function . Also the VM MUST have its mutex locked . If the function fails
* the calling funtion is responsible for recovering from the error .
* @ param vm pointer to a VirtualMachine with its mutex locked .
2010-07-14 20:11:29 +04:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
int deploy (
VirtualMachine * vm ) ;
2015-02-06 00:49:33 +03:00
/**
* Sets an imported VM to RUNNING state , a history record MUST be added ,
* and the VM MUST be locked .
* @ param vm pointer to a VirtualMachine with its mutex locked .
* @ return 0 on success
*/
int import (
VirtualMachine * vm ) ;
2008-06-17 20:27:32 +04:00
/**
2010-07-14 20:11:29 +04:00
* Migrates a VM . The following actions must be performed before calling
* this function :
2008-06-17 20:27:32 +04:00
* - Lock the VM mutex .
* - Update the History statistics of the current host .
2010-07-14 20:11:29 +04:00
* - Add a new History record with the new host .
* If the function fails the calling funtion is responsible for recovering
2008-06-17 20:27:32 +04:00
* from the error .
* @ param vm pointer to a VirtualMachine with its mutex locked .
2010-07-14 20:11:29 +04:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
int migrate (
VirtualMachine * vm ) ;
/**
2010-07-14 20:11:29 +04:00
* Migrates a VM . The following actions must be performed before calling
* this function :
2008-06-17 20:27:32 +04:00
* - Lock the VM mutex .
* - Update the History statistics of the current host .
2010-07-14 20:11:29 +04:00
* - Add a new History record with the new host .
* If the function fails the calling funtion is responsible for recovering
2008-06-17 20:27:32 +04:00
* from the error .
* @ param vm pointer to a VirtualMachine with its mutex locked .
2010-07-14 20:11:29 +04:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
int live_migrate (
VirtualMachine * vm ) ;
2010-07-14 20:11:29 +04:00
2008-06-17 20:27:32 +04:00
/**
2016-05-02 19:34:42 +03:00
* Terminates a VM .
2008-06-17 20:27:32 +04:00
* @ param vid VirtualMachine identification
2015-04-24 16:25:47 +03:00
* @ param hard True to force the shutdown ( cancel instead of shutdown )
2010-07-14 20:11:29 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
2016-05-02 19:34:42 +03:00
int terminate (
2015-03-30 18:41:22 +03:00
int vid ,
2015-04-24 16:25:47 +03:00
bool hard ,
2015-03-30 18:41:22 +03:00
string & error_str ) ;
2008-06-17 20:27:32 +04:00
2013-03-27 21:15:53 +04:00
/**
* Shuts down a VM , but it is saved in the system DS instead of destroyed .
* @ param vid VirtualMachine identification
* @ param hard True to force the shutdown ( cancel instead of shutdown )
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
2013-04-02 19:01:22 +04:00
int undeploy (
2015-03-30 18:41:22 +03:00
int vid ,
bool hard ,
string & error_str ) ;
2013-03-27 21:15:53 +04:00
2012-09-04 20:30:53 +04:00
/**
* Powers off a VM .
* @ param vid VirtualMachine identification
2013-03-27 16:48:06 +04:00
* @ param hard True to force the poweroff ( cancel instead of shutdown )
2012-09-04 20:30:53 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
int poweroff (
2015-03-30 18:41:22 +03:00
int vid ,
bool hard ,
string & error_str ) ;
2012-09-04 20:30:53 +04:00
2008-06-17 20:27:32 +04:00
/**
* Holds a VM .
* @ param vid VirtualMachine identification
2010-07-14 20:11:29 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
2008-06-17 20:27:32 +04:00
int hold (
2015-03-30 18:41:22 +03:00
int vid ,
string & error_str ) ;
2008-06-17 20:27:32 +04:00
/**
* Releases a VM .
* @ param vid VirtualMachine identification
2010-07-14 20:11:29 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
2008-06-17 20:27:32 +04:00
int release (
2015-03-30 18:41:22 +03:00
int vid ,
string & error_str ) ;
2008-06-17 20:27:32 +04:00
/**
* Stops a VM .
* @ param vid VirtualMachine identification
2010-07-14 20:11:29 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
2008-06-17 20:27:32 +04:00
int stop (
2015-03-30 18:41:22 +03:00
int vid ,
string & error_str ) ;
2008-06-17 20:27:32 +04:00
/**
* Suspends a VM .
* @ param vid VirtualMachine identification
2010-07-14 20:11:29 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
2008-06-17 20:27:32 +04:00
int suspend (
2015-03-30 18:41:22 +03:00
int vid ,
string & error_str ) ;
2008-06-17 20:27:32 +04:00
/**
* Resumes a VM .
* @ param vid VirtualMachine identification
2010-07-14 20:11:29 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
2008-06-17 20:27:32 +04:00
int resume (
2015-03-30 18:41:22 +03:00
int vid ,
string & error_str ) ;
2010-07-14 20:11:29 +04:00
2008-06-17 20:27:32 +04:00
/**
* Ends a VM life cycle inside ONE .
2016-05-02 19:34:42 +03:00
* @ param vm VirtualMachine object
* @ return 0 on success , the VM mutex is unlocked
2010-07-14 20:11:29 +04:00
*/
2016-05-02 19:34:42 +03:00
int delete_vm ( VirtualMachine * vm , string & error_str ) ;
/**
* VM ID interface
*/
int delete_vm ( int vid , string & error_str )
{
VirtualMachine * vm ;
vm = vmpool - > get ( vid , true ) ;
if ( vm = = 0 )
{
error_str = " Virtual machine does not exist " ;
return - 1 ;
}
return delete_vm ( vm , error_str ) ;
}
2008-06-17 20:27:32 +04:00
2010-12-31 19:48:50 +03:00
/**
* Moves a VM to PENDING state preserving any resource ( i . e . leases ) and id
2016-05-02 19:34:42 +03:00
* @ param vm VirtualMachine object
* @ return 0 on success
2010-12-31 19:48:50 +03:00
*/
2016-05-02 19:34:42 +03:00
int delete_recreate ( VirtualMachine * vm , string & error_str ) ;
/**
* Recover the last operation on the VM
* @ param vm VirtualMachine object
* @ param success if the operation is assumed to succeed or not
* @ return 0 on success
*/
int recover ( VirtualMachine * vm , bool success , string & error_str ) ;
/**
* Retry the last operation on the VM
* @ param vm VirtualMachine object
* @ return 0 on success
*/
int retry ( VirtualMachine * vm , string & error_str ) ;
2010-12-31 19:48:50 +03:00
2011-12-26 02:46:19 +04:00
/**
* Reboots a VM preserving any resource and RUNNING state
* @ param vid VirtualMachine identification
2015-04-24 16:45:54 +03:00
* @ param hard True to force the shutdown ( cancel instead of shutdown )
2011-12-26 02:46:19 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
int reboot (
2015-03-30 18:41:22 +03:00
int vid ,
2015-04-24 16:45:54 +03:00
bool hard ,
2015-03-30 18:41:22 +03:00
string & error_str ) ;
2012-05-09 00:33:59 +04:00
2012-04-26 21:06:49 +04:00
/**
* Set the re - scheduling flag for the VM ( must be in RUNNING state )
* @ param vid VirtualMachine identification
* @ param do_resched set or unset the flag
2012-06-15 14:28:20 +04:00
*
2012-04-26 21:06:49 +04:00
* @ return 0 on success , - 1 if the VM does not exits or - 2 if the VM is
* in a wrong a state
*/
int resched (
2015-03-30 18:41:22 +03:00
int vid ,
bool do_resched ,
string & error_str ) ;
2012-04-26 21:06:49 +04:00
2012-06-13 20:47:04 +04:00
/**
2012-06-15 14:28:20 +04:00
* Starts the attach disk action .
* @ param vid VirtualMachine identification
* @ param tmpl Template containing the new DISK attribute .
* @ param error_str Error reason , if any
2012-06-13 20:47:04 +04:00
*
2012-06-15 14:28:20 +04:00
* @ return 0 on success , - 1 otherwise
2012-06-13 20:47:04 +04:00
*/
2012-06-15 18:28:30 +04:00
int attach (
2013-03-06 18:59:58 +04:00
int vid ,
VirtualMachineTemplate * tmpl ,
2012-06-15 18:28:30 +04:00
string & error_str ) ;
2012-06-13 20:47:04 +04:00
2012-06-14 19:45:41 +04:00
/**
* Starts the detach disk action .
2012-06-15 18:28:30 +04:00
* @ param vid VirtualMachine identification
* @ param disk_id Disk to detach
* @ param error_str Error reason , if any
2012-06-14 19:45:41 +04:00
*
2012-06-15 18:28:30 +04:00
* @ return 0 on success , - 1 otherwise
2012-06-14 19:45:41 +04:00
*/
int detach (
2012-06-15 18:28:30 +04:00
int id ,
int disk_id ,
string & error_str ) ;
2012-06-14 19:45:41 +04:00
2012-12-12 21:31:27 +04:00
/**
* Starts the attach NIC action .
* @ param vid VirtualMachine identification
* @ param tmpl Template containing the new NIC attribute .
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int attach_nic (
int vid ,
VirtualMachineTemplate * tmpl ,
string & error_str ) ;
/**
* Starts the detach NIC action .
* @ param vid VirtualMachine identification
* @ param nic_id NIC to detach
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int detach_nic (
int id ,
int nic_id ,
string & error_str ) ;
2013-02-19 18:21:33 +04:00
/**
* Starts the snapshot create action
*
* @ param vid VirtualMachine identification
* @ param name Name for the new snapshot
2013-02-20 21:11:58 +04:00
* @ param snap_id Will contain the new snapshot ID
2013-02-19 18:21:33 +04:00
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int snapshot_create (
int vid ,
string & name ,
2013-02-20 21:11:58 +04:00
int & snap_id ,
2013-02-19 18:21:33 +04:00
string & error_str ) ;
2013-02-20 19:04:09 +04:00
/**
* Starts the snapshot revert action
*
* @ param vid VirtualMachine identification
* @ param snap_id Snapshot to be restored
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int snapshot_revert (
int vid ,
int snap_id ,
string & error_str ) ;
2013-02-21 18:01:48 +04:00
/**
* Starts the snapshot delete action
*
* @ param vid VirtualMachine identification
* @ param snap_id Snapshot to be deleted
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int snapshot_delete (
int vid ,
int snap_id ,
string & error_str ) ;
2012-12-12 21:31:27 +04:00
2015-05-19 19:41:23 +03:00
/**
* Starts the disk snapshot create action
*
* @ param vid VirtualMachine identification
* @ param did DISK identification
2015-07-09 14:13:07 +03:00
* @ param name Description for the new snapshot
2015-05-19 19:41:23 +03:00
* @ param snap_id Will contain the new snapshot ID
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int disk_snapshot_create (
int vid ,
int did ,
2015-07-09 14:13:07 +03:00
const string & name ,
2015-05-19 19:41:23 +03:00
int & snap_id ,
string & error_str ) ;
/**
* Reverts the disk state to a previous snapshot
*
* @ param vid VirtualMachine identification
* @ param did DISK identification
* @ param snap_id Snapshot to be restored
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int disk_snapshot_revert (
int vid ,
int did ,
int snap_id ,
string & error_str ) ;
2015-05-27 17:29:41 +03:00
/**
* Deletes a disk snapshot
*
* @ param vid VirtualMachine identification
* @ param did DISK identification
* @ param snap_id Snapshot to be restored
* @ param error_str Error reason , if any
*
* @ return 0 on success , - 1 otherwise
*/
int disk_snapshot_delete (
int vid ,
int did ,
int snap_id ,
string & error_str ) ;
2016-05-02 19:34:42 +03:00
2008-06-17 20:27:32 +04:00
private :
/**
* Thread id for the Dispatch Manager
*/
pthread_t dm_thread ;
/**
* Pointer to the Host Pool , to access hosts
*/
HostPool * hpool ;
/**
2008-11-13 19:21:17 +03:00
* Pointer to the Virtual Machine Pool , to access hosts
2008-06-17 20:27:32 +04:00
*/
VirtualMachinePool * vmpool ;
2015-07-01 22:15:40 +03:00
/**
2015-04-27 00:34:26 +03:00
* Pointer to TransferManager
*/
2015-07-01 22:15:40 +03:00
TransferManager * tm ;
2015-04-27 00:34:26 +03:00
2015-07-01 22:15:40 +03:00
/**
* Pointer to VirtualMachineManager
*/
VirtualMachineManager * vmm ;
2015-04-27 00:34:26 +03:00
2015-07-01 22:15:40 +03:00
/**
* Pointer to LifeCycleManager
*/
LifeCycleManager * lcm ;
2015-04-27 00:34:26 +03:00
2015-07-01 22:15:40 +03:00
/**
* Pointer to ImageManager
*/
ImageManager * imagem ;
2015-04-27 00:34:26 +03:00
2008-06-17 20:27:32 +04:00
/**
* Action engine for the Manager
*/
ActionManager am ;
/**
2010-07-14 20:11:29 +04:00
* Function to execute the Manager action loop method within a new pthread
2008-06-17 20:27:32 +04:00
* ( requires C linkage )
*/
friend void * dm_action_loop ( void * arg ) ;
/**
* The action function executed when an action is triggered .
* @ param action the name of the action
* @ param arg arguments for the action function
*/
void do_action (
const string & action ,
void * arg ) ;
2012-06-27 20:50:19 +04:00
/**
2016-05-02 19:34:42 +03:00
* Frees the resources associated to a VM : disks , ip addresses and Quotas
2012-06-27 20:50:19 +04:00
*/
2016-05-02 19:34:42 +03:00
void free_vm_resources ( VirtualMachine * vm ) ;
2012-06-27 20:50:19 +04:00
2008-06-17 20:27:32 +04:00
//--------------------------------------------------------------------------
// DM Actions associated with a VM state transition
//--------------------------------------------------------------------------
2010-07-14 20:11:29 +04:00
2008-06-17 20:27:32 +04:00
void suspend_success_action ( int vid ) ;
void stop_success_action ( int vid ) ;
2013-04-02 19:01:22 +04:00
void undeploy_success_action ( int vid ) ;
2013-03-27 21:15:53 +04:00
2012-09-04 20:30:53 +04:00
void poweroff_success_action ( int vid ) ;
2008-06-17 20:27:32 +04:00
void done_action ( int vid ) ;
2010-12-31 19:48:50 +03:00
void resubmit_action ( int vid ) ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*DISPATCH_MANAGER_H*/