1
package edu.ucsb.eucalyptus.cloud.cluster;
3
import com.google.common.collect.*;
4
import edu.ucsb.eucalyptus.cloud.*;
5
import edu.ucsb.eucalyptus.msgs.*;
6
import org.apache.log4j.Logger;
9
import java.util.concurrent.ConcurrentLinkedQueue;
10
import java.util.concurrent.atomic.AtomicBoolean;
12
class ClusterAllocator extends Thread {
14
private static Logger LOG = Logger.getLogger( ClusterAllocator.class );
17
private AtomicBoolean rollback;
18
protected Multimap<State, QueuedEvent> msgMap;
19
private Cluster cluster;
20
private ConcurrentLinkedQueue<QueuedEvent> pendingEvents;
21
private VmAllocationInfo vmAllocInfo;
23
public ClusterAllocator( ResourceToken vmToken, VmAllocationInfo vmAllocInfo ) {
24
this.msgMap = Multimaps.newHashMultimap();
25
this.vmAllocInfo = vmAllocInfo;
26
this.pendingEvents = new ConcurrentLinkedQueue<QueuedEvent>();
27
this.cluster = Clusters.getInstance().lookup( vmToken.getCluster() );
28
this.state = State.START;
29
this.rollback = new AtomicBoolean( false );
30
for ( NetworkToken networkToken : vmToken.getNetworkTokens() )
31
this.setupNetworkMessages( networkToken );
32
this.setupVmMessages( vmToken );
35
public void setupNetworkMessages( NetworkToken networkToken ) {
36
if ( networkToken != null ) {
37
StartNetworkType msg = new StartNetworkType( this.vmAllocInfo.getRequest(), networkToken.getVlan(), networkToken.getNetworkName() );
38
StartNetworkCallback callback = new StartNetworkCallback( this, networkToken );
39
QueuedEvent<StartNetworkType> event = new QueuedEvent<StartNetworkType>( callback, msg );
40
this.msgMap.put( State.CREATE_NETWORK, event );
43
Network network = Networks.getInstance().lookup( networkToken.getName() );
44
if ( network.getRules().isEmpty() ) return;
45
ConfigureNetworkCallback callback = new ConfigureNetworkCallback();
46
ConfigureNetworkType msg = new ConfigureNetworkType( this.vmAllocInfo.getRequest(), network.getRules() );
47
QueuedEvent event = new QueuedEvent<ConfigureNetworkType>( callback, msg );
48
this.msgMap.put( State.CREATE_NETWORK_RULES, event );
49
} catch ( NoSuchElementException e ) {}/* just added this network, shouldn't happen, if so just smile and nod */
52
public void setupVmMessages( ResourceToken token ) {
53
List<String> macs = new ArrayList<String>();
54
List<String> networkNames = new ArrayList<String>();
56
for ( String instanceId : token.getInstanceIds() )
57
macs.add( VmInstances.getAsMAC( instanceId ) );
60
for ( Network net : vmAllocInfo.getNetworks() ) {
61
networkNames.add( net.getNetworkName() );
62
if ( vlan < 0 ) vlan = Networks.getInstance().lookup( net.getName() ).getToken( token.getCluster() ).getVlan();
64
if ( vlan < 0 ) vlan = 9;
66
RunInstancesType request = this.vmAllocInfo.getRequest();
67
VmImageInfo imgInfo = this.vmAllocInfo.getImageInfo();
68
VmTypeInfo vmInfo = this.vmAllocInfo.getVmTypeInfo();
69
String rsvId = this.vmAllocInfo.getReservationId();
70
VmKeyInfo keyInfo = this.vmAllocInfo.getKeyInfo();
72
VmRunType run = new VmRunType( request, rsvId, request.getUserData(), token.getAmount(), imgInfo, vmInfo, keyInfo, token.getInstanceIds(), macs, vlan, networkNames );
73
this.msgMap.put( State.CREATE_VMS, new QueuedEvent<VmRunType>( new VmRunCallback( this, token ), run ) );
76
public void setState( final State state ) {
78
if ( this.rollback.get() && !State.ROLLBACK.equals( this.state ) )
79
this.state = State.ROLLBACK;
80
else if ( this.rollback.get() && State.ROLLBACK.equals( this.state ) )
81
this.state = State.FINISHED;
87
this.state = State.CREATE_NETWORK;
88
while ( !this.state.equals( State.FINISHED ) ) {
89
switch ( this.state ) {
92
this.setState( State.CREATE_NETWORK_RULES );
94
case CREATE_NETWORK_RULES:
96
this.setState( State.CREATE_VMS );
100
this.setState( State.FINISHED );
104
this.setState( State.FINISHED );
113
public void clearQueue() {
114
QueuedEvent event = null;
115
while ( ( event = this.pendingEvents.poll() ) != null )
116
event.getCallback().waitForEvent();
119
private void queueEvents() {
120
for ( QueuedEvent event : this.msgMap.get( this.state ) ) {
121
this.pendingEvents.add( event );
122
this.cluster.getMessageQueue().enqueue( event );
126
public AtomicBoolean getRollback() {
134
CREATE_NETWORK_RULES,