~kirkland/eucalyptus/label-metadata

« back to all changes in this revision

Viewing changes to clc/modules/cluster-manager/src/edu/ucsb/eucalyptus/cloud/cluster/ClusterAllocator.java

  • Committer: graziano obertelli
  • Date: 2009-01-07 03:32:35 UTC
  • Revision ID: graziano@cs.ucsb.edu-20090107033235-oxhuexp18v8hg0pw
Tags: 1.4
from CVS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package edu.ucsb.eucalyptus.cloud.cluster;
 
2
 
 
3
import com.google.common.collect.*;
 
4
import edu.ucsb.eucalyptus.cloud.*;
 
5
import edu.ucsb.eucalyptus.msgs.*;
 
6
import org.apache.log4j.Logger;
 
7
 
 
8
import java.util.*;
 
9
import java.util.concurrent.ConcurrentLinkedQueue;
 
10
import java.util.concurrent.atomic.AtomicBoolean;
 
11
 
 
12
class ClusterAllocator extends Thread {
 
13
 
 
14
  private static Logger LOG = Logger.getLogger( ClusterAllocator.class );
 
15
 
 
16
  private State state;
 
17
  private AtomicBoolean rollback;
 
18
  protected Multimap<State, QueuedEvent> msgMap;
 
19
  private Cluster cluster;
 
20
  private ConcurrentLinkedQueue<QueuedEvent> pendingEvents;
 
21
  private VmAllocationInfo vmAllocInfo;
 
22
 
 
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 );
 
33
  }
 
34
 
 
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 );
 
41
    }
 
42
    try {
 
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 */
 
50
  }
 
51
 
 
52
  public void setupVmMessages( ResourceToken token ) {
 
53
    List<String> macs = new ArrayList<String>();
 
54
    List<String> networkNames = new ArrayList<String>();
 
55
 
 
56
    for ( String instanceId : token.getInstanceIds() )
 
57
      macs.add( VmInstances.getAsMAC( instanceId ) );
 
58
 
 
59
    int vlan = -1;
 
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();
 
63
    }
 
64
    if ( vlan < 0 ) vlan = 9;
 
65
 
 
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();
 
71
 
 
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 ) );
 
74
  }
 
75
 
 
76
  public void setState( final State state ) {
 
77
    this.clearQueue();
 
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;
 
82
    else
 
83
      this.state = state;
 
84
  }
 
85
 
 
86
  public void run() {
 
87
    this.state = State.CREATE_NETWORK;
 
88
    while ( !this.state.equals( State.FINISHED ) ) {
 
89
      switch ( this.state ) {
 
90
        case CREATE_NETWORK:
 
91
          this.queueEvents();
 
92
          this.setState( State.CREATE_NETWORK_RULES );
 
93
          break;
 
94
        case CREATE_NETWORK_RULES:
 
95
          this.queueEvents();
 
96
          this.setState( State.CREATE_VMS );
 
97
          break;
 
98
        case CREATE_VMS:
 
99
          this.queueEvents();
 
100
          this.setState( State.FINISHED );
 
101
          break;
 
102
        case ROLLBACK:
 
103
          this.queueEvents();
 
104
          this.setState( State.FINISHED );
 
105
          break;
 
106
        case FINISHED:
 
107
          break;
 
108
      }
 
109
      this.clearQueue();
 
110
    }
 
111
  }
 
112
 
 
113
  public void clearQueue() {
 
114
    QueuedEvent event = null;
 
115
    while ( ( event = this.pendingEvents.poll() ) != null )
 
116
      event.getCallback().waitForEvent();
 
117
  }
 
118
 
 
119
  private void queueEvents() {
 
120
    for ( QueuedEvent event : this.msgMap.get( this.state ) ) {
 
121
      this.pendingEvents.add( event );
 
122
      this.cluster.getMessageQueue().enqueue( event );
 
123
    }
 
124
  }
 
125
 
 
126
  public AtomicBoolean getRollback() {
 
127
    return rollback;
 
128
  }
 
129
 
 
130
  enum State {
 
131
 
 
132
    START,
 
133
    CREATE_NETWORK,
 
134
    CREATE_NETWORK_RULES,
 
135
    CREATE_VMS,
 
136
    FINISHED,
 
137
    ROLLBACK
 
138
  }
 
139
 
 
140
}