cloudstack/api/src/com/cloud/vm/VirtualMachine.java
Murali Reddy c12ccbd06f Bug 9534 : implement CPU cap
Introducing new boolean flag in service offering to restrict the user VM's CPU utilization to what service offering it is entitled for.
2011-04-27 13:54:45 +05:30

255 lines
10 KiB
Java
Executable File

/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.vm;
import java.util.Date;
import com.cloud.acl.ControlledEntity;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.utils.fsm.StateMachine2;
import com.cloud.utils.fsm.StateObject;
/**
* VirtualMachine describes the properties held by a virtual machine
*
*/
public interface VirtualMachine extends RunningOn, ControlledEntity, StateObject<VirtualMachine.State> {
public enum State {
Starting(true, "VM is being started. At this state, you should find host id filled which means it's being started on that host."),
Running(false, "VM is running. host id has the host that it is running on."),
Stopping(true, "VM is being stopped. host id has the host that it is being stopped on."),
Stopped(false, "VM is stopped. host id should be null."),
Destroyed(false, "VM is marked for destroy."),
Expunging(true, "VM is being expunged."),
Migrating(true, "VM is being migrated. host id holds to from host"),
Error(false, "VM is in error"),
Unknown(false, "VM state is unknown."),
Shutdowned(false, "VM is shutdowned from inside");
private final boolean _transitional;
String _description;
private State(boolean transitional, String description) {
_transitional = transitional;
_description = description;
}
public String getDescription() {
return _description;
}
public boolean isTransitional() {
return _transitional;
}
public static StateMachine2<State, VirtualMachine.Event, VirtualMachine> getStateMachine() {
return s_fsm;
}
protected static final StateMachine2<State, VirtualMachine.Event, VirtualMachine> s_fsm = new StateMachine2<State, VirtualMachine.Event, VirtualMachine>();
static {
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.StartRequested, State.Starting);
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.DestroyRequested, State.Destroyed);
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.StopRequested, State.Stopped);
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.AgentReportStopped, State.Stopped);
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.OperationFailed, State.Error);
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.ExpungeOperation, State.Expunging);
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
s_fsm.addTransition(State.Starting, VirtualMachine.Event.OperationRetry, State.Starting);
s_fsm.addTransition(State.Starting, VirtualMachine.Event.OperationSucceeded, State.Running);
s_fsm.addTransition(State.Starting, VirtualMachine.Event.OperationFailed, State.Stopped);
s_fsm.addTransition(State.Starting, VirtualMachine.Event.AgentReportRunning, State.Running);
s_fsm.addTransition(State.Starting, VirtualMachine.Event.AgentReportStopped, State.Stopped);
s_fsm.addTransition(State.Starting, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
s_fsm.addTransition(State.Destroyed, VirtualMachine.Event.RecoveryRequested, State.Stopped);
s_fsm.addTransition(State.Destroyed, VirtualMachine.Event.ExpungeOperation, State.Expunging);
s_fsm.addTransition(State.Running, VirtualMachine.Event.MigrationRequested, State.Migrating);
s_fsm.addTransition(State.Running, VirtualMachine.Event.AgentReportRunning, State.Running);
s_fsm.addTransition(State.Running, VirtualMachine.Event.AgentReportStopped, State.Stopped);
s_fsm.addTransition(State.Running, VirtualMachine.Event.StopRequested, State.Stopping);
s_fsm.addTransition(State.Running, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.MigrationRequested, State.Migrating);
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.OperationSucceeded, State.Running);
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.OperationFailed, State.Running);
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.AgentReportRunning, State.Running);
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.AgentReportStopped, State.Stopped);
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.OperationSucceeded, State.Stopped);
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.OperationFailed, State.Running);
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.AgentReportRunning, State.Running);
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.AgentReportStopped, State.Stopped);
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.StopRequested, State.Stopping);
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
s_fsm.addTransition(State.Expunging, VirtualMachine.Event.OperationFailed, State.Expunging);
s_fsm.addTransition(State.Expunging, VirtualMachine.Event.ExpungeOperation, State.Expunging);
s_fsm.addTransition(State.Error, VirtualMachine.Event.DestroyRequested, State.Expunging);
s_fsm.addTransition(State.Error, VirtualMachine.Event.ExpungeOperation, State.Expunging);
}
public static boolean isVmStarted(State oldState, Event e, State newState) {
if (oldState == State.Starting && newState == State.Running) {
return true;
}
return false;
}
public static boolean isVmStopped(State oldState, Event e, State newState) {
if (oldState == State.Stopping && newState == State.Stopped) {
return true;
}
return false;
}
public static boolean isVmMigrated(State oldState, Event e, State newState) {
if (oldState == State.Migrating && newState == State.Running && (e == Event.OperationSucceeded || e == Event.AgentReportRunning)) {
return true;
}
return false;
}
}
public enum Event {
CreateRequested,
StartRequested,
StopRequested,
DestroyRequested,
RecoveryRequested,
AgentReportStopped,
AgentReportRunning,
MigrationRequested,
ExpungeOperation,
OperationSucceeded,
OperationFailed,
OperationRetry,
AgentReportShutdowned
};
public enum Type {
User,
DomainRouter,
ConsoleProxy,
SecondaryStorageVm,
/*
* UserBareMetal is only used for selecting VirtualMachineGuru, there is no
* VM with this type. UserBareMetal should treat exactly as User.
*/
UserBareMetal;
public static boolean isSystemVM(VirtualMachine.Type vmtype) {
if(DomainRouter.equals(vmtype)
|| ConsoleProxy.equals(vmtype)
|| SecondaryStorageVm.equals(vmtype)){
return true;
}
return false;
}
}
/**
* @return The name of the vm instance used by the cloud stack to uniquely
* reference this VM. You can build names that starts with this name and it
* guarantees uniqueness for things related to the VM.
*/
public String getInstanceName();
/**
* @return the id of this virtual machine. null means the id has not been set.
*/
public long getId();
/**
* @return the host name of the virtual machine. If the user did not
* specify the host name when creating the virtual machine then it is
* the name generated by cloud stack.
*/
public String getName();
/**
* @return the ip address of the virtual machine.
*/
public String getPrivateIpAddress();
/**
* @return mac address.
*/
public String getPrivateMacAddress();
/**
* @return password of the host for vnc purposes.
*/
public String getVncPassword();
/**
* @return the state of the virtual machine
*/
//public State getState();
/**
* @return template id.
*/
public long getTemplateId();
/**
* returns the guest OS ID
* @return guestOSId
*/
public long getGuestOSId();
/**
* @return pod id.
*/
public Long getPodId();
/**
* @return data center id.
*/
public long getDataCenterId();
/**
* @return id of the host it was assigned last time.
*/
public Long getLastHostId();
@Override
public Long getHostId();
public void setLastHostId(Long lastHostId);
/**
* @return should HA be enabled for this machine?
*/
public boolean isHaEnabled();
/**
* @return should limit CPU usage to the service offering?
*/
public boolean limitCpuUse();
/**
* @return date when machine was created
*/
public Date getCreated();
public long getServiceOfferingId();
Type getType();
HypervisorType getHypervisorType();
}