1
1
/*******************************************************************************
2
*Copyright (c) 2009 Eucalyptus Systems, Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, only version 3 of the License.
9
* This file is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Please contact Eucalyptus Systems, Inc., 130 Castilian
18
* Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
19
* if you need additional information or have any questions.
21
* This file may incorporate work covered under the following copyright and
24
* Software License Agreement (BSD License)
26
* Copyright (c) 2008, Regents of the University of California
27
* All rights reserved.
29
* Redistribution and use of this software in source and binary forms, with
30
* or without modification, are permitted provided that the following
33
* Redistributions of source code must retain the above copyright notice,
34
* this list of conditions and the following disclaimer.
36
* Redistributions in binary form must reproduce the above copyright
37
* notice, this list of conditions and the following disclaimer in the
38
* documentation and/or other materials provided with the distribution.
40
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
41
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
44
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
51
* THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
52
* LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
53
* SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
54
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
55
* BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
56
* THE REGENTSā DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
57
* OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
58
* WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
59
* ANY SUCH LICENSES OR RIGHTS.
60
*******************************************************************************/
2
*Copyright (c) 2009 Eucalyptus Systems, Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, only version 3 of the License.
9
* This file is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Please contact Eucalyptus Systems, Inc., 130 Castilian
18
* Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
19
* if you need additional information or have any questions.
21
* This file may incorporate work covered under the following copyright and
24
* Software License Agreement (BSD License)
26
* Copyright (c) 2008, Regents of the University of California
27
* All rights reserved.
29
* Redistribution and use of this software in source and binary forms, with
30
* or without modification, are permitted provided that the following
33
* Redistributions of source code must retain the above copyright notice,
34
* this list of conditions and the following disclaimer.
36
* Redistributions in binary form must reproduce the above copyright
37
* notice, this list of conditions and the following disclaimer in the
38
* documentation and/or other materials provided with the distribution.
40
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
41
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
44
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
51
* THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
52
* LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
53
* SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
54
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
55
* BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
56
* THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
57
* OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
58
* WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
59
* ANY SUCH LICENSES OR RIGHTS.
60
*******************************************************************************/
62
62
* Author: chris grzegorczyk <grze@eucalyptus.com>
64
64
package com.eucalyptus.cluster.callback;
66
import java.util.List;
67
import java.util.NoSuchElementException;
66
import java.util.concurrent.Callable;
67
import java.util.ArrayList;
68
import javax.persistence.EntityTransaction;
69
69
import org.apache.log4j.Logger;
70
70
import com.eucalyptus.address.Address;
71
71
import com.eucalyptus.address.Addresses;
72
import com.eucalyptus.blockstorage.Volume;
73
import com.eucalyptus.cloud.ResourceToken;
74
import com.eucalyptus.cloud.VmRunType;
75
import com.eucalyptus.cluster.Cluster;
72
76
import com.eucalyptus.cluster.Clusters;
73
import com.eucalyptus.cluster.Networks;
74
import com.eucalyptus.cluster.VmInstance;
75
import com.eucalyptus.cluster.VmInstances;
77
import com.eucalyptus.cluster.ResourceState.NoSuchTokenException;
78
import com.eucalyptus.component.ServiceConfiguration;
79
import com.eucalyptus.component.Topology;
80
import com.eucalyptus.component.id.ClusterController;
81
import com.eucalyptus.component.id.Eucalyptus;
82
import com.eucalyptus.component.id.Storage;
83
import com.eucalyptus.entities.Entities;
84
import com.eucalyptus.records.Logs;
85
import com.eucalyptus.system.Threads;
86
import com.eucalyptus.util.Callback;
76
87
import com.eucalyptus.util.EucalyptusClusterException;
77
88
import com.eucalyptus.util.Exceptions;
78
89
import com.eucalyptus.util.LogUtil;
79
import com.eucalyptus.vm.SystemState.Reason;
80
import com.eucalyptus.vm.VmState;
81
import com.google.common.collect.Lists;
82
import com.google.common.collect.Sets;
83
import edu.ucsb.eucalyptus.cloud.Network;
84
import edu.ucsb.eucalyptus.cloud.NetworkToken;
85
import edu.ucsb.eucalyptus.cloud.ResourceToken;
90
import com.eucalyptus.util.async.AsyncRequests;
91
import com.eucalyptus.util.async.CheckedListenableFuture;
92
import com.eucalyptus.util.async.MessageCallback;
93
import com.eucalyptus.vm.VmInstance;
94
import com.eucalyptus.vm.VmInstance.VmState;
95
import com.eucalyptus.vm.VmInstances;
96
import com.eucalyptus.vm.VmVolumeAttachment;
97
import com.eucalyptus.vm.VmVolumeAttachment.AttachmentState;
98
import com.google.common.base.Function;
99
import com.google.common.base.Functions;
100
import com.google.common.base.Predicate;
86
101
import edu.ucsb.eucalyptus.cloud.VmInfo;
87
102
import edu.ucsb.eucalyptus.cloud.VmRunResponseType;
88
import edu.ucsb.eucalyptus.cloud.VmRunType;
90
public class VmRunCallback extends QueuedEventCallback<VmRunType,VmRunResponseType> {
92
private static Logger LOG = Logger.getLogger( VmRunCallback.class );
94
private ResourceToken token;
103
import edu.ucsb.eucalyptus.msgs.AttachStorageVolumeResponseType;
104
import edu.ucsb.eucalyptus.msgs.AttachStorageVolumeType;
105
import edu.ucsb.eucalyptus.msgs.AttachVolumeType;
106
import edu.ucsb.eucalyptus.msgs.BaseMessage;
108
public class VmRunCallback extends MessageCallback<VmRunType, VmRunResponseType> {
110
private static Logger LOG = Logger.getLogger( VmRunCallback.class );
112
private final ResourceToken token;
96
114
public VmRunCallback( final VmRunType msg, final ResourceToken token ) {
97
116
this.token = token;
98
this.setRequest( msg );
117
LOG.debug( this.token );
101
public void prepare( final VmRunType msg ) throws Exception {
102
// LOG.trace( LogUtil.subheader( msg.toString( ) ) );
103
for( String vmId : msg.getInstanceIds( ) ) {
121
public void initialize( final VmRunType msg ) {
122
LOG.debug( this.token + ":" + msg );
124
this.token.submit( );
125
} catch ( final NoSuchTokenException e2 ) {
127
Logs.extreme( ).error( e2, e2 );
129
EntityTransaction db = Entities.get( VmInstance.class );
131
final VmInstance vm = VmInstances.lookup( msg.getInstanceId( ) );
132
msg.setUserId( vm.getOwnerUserId( ) );
133
msg.setOwnerId( vm.getOwnerUserId( ) );
134
msg.setAccountId( vm.getOwnerAccountNumber( ) );
135
if ( !VmState.PENDING.equals( vm.getState( ) ) ) {
136
throw new EucalyptusClusterException( "Intercepted a RunInstances request for an instance which has meanwhile been terminated." );
139
} catch ( final Exception e ) {
141
Logs.extreme( ).error( e, e );
105
VmInstance vm = VmInstances.getInstance( ).lookup( vmId );
106
if( !VmState.PENDING.equals( vm.getState( ) ) ) {
107
throw new EucalyptusClusterException("Intercepted a RunInstances request for an instance which has meanwhile been terminated." );
109
} catch ( Exception e ) {
145
} catch ( Exception ex ) {
147
Logs.extreme( ).error( ex, ex );
114
Clusters.getInstance().lookup( token.getCluster() ).getNodeState().submitToken( token );
115
} catch ( Exception e2 ) {
149
throw new EucalyptusClusterException( "Error while initializing request state: " + this.getRequest( ), e );
122
public void verify( VmRunResponseType reply ) throws Exception {
124
Clusters.getInstance().lookup( token.getCluster() ).getNodeState().redeemToken( token );
125
} catch ( Throwable e ) {
131
if ( reply != null && reply.getVms( ).isEmpty( ) ) {
132
this.fail( new EucalyptusClusterException( "Failed to run the requested instances because the request was rejected: " + token.toString( ) ) );
133
} else if ( reply != null && !reply.getVms( ).isEmpty( ) ) {
134
Set<String> liveIds = Sets.newHashSet( );
135
for ( VmInfo vmInfo : reply.getVms() ) {
136
liveIds.add( vmInfo.getInstanceId( ) );
138
VmInstance vm = VmInstances.getInstance().lookup( vmInfo.getInstanceId() );
139
vm.updateAddresses( vmInfo.getNetParams().getIpAddress(), vmInfo.getNetParams().getIgnoredPublicIp() );
141
} catch ( Throwable ex ) {
142
Exceptions.eat( "UNIDENTIFIED VM: " + vmInfo.toString( ) );
145
Set<String> requestIds = Sets.newHashSet( token.getInstanceIds( ) );
146
requestIds.removeAll( liveIds );
147
for( String id : requestIds ) {
148
int idx = token.getInstanceIds( ).indexOf( id );
150
VmInstance vm = VmInstances.getInstance().lookup( id );
152
vm.setState( VmState.TERMINATED, Reason.FAILED, "This instance failed to be run by the CC." );
153
LOG.error( "Failed virtual machine: " + vm.toString( ) );
154
} catch ( Throwable ex ) {
155
LOG.trace( "Failed virtual machine: " + id );
157
if( idx > 0 && token.getAddresses( ).size( ) > idx ) {
159
String addr = token.getAddresses( ).get( idx );
160
LOG.warn( "Failed virtual machine: " + id + " releasing address: " + addr );
161
Addresses.getInstance( ).lookup( addr ).release( );
162
} catch ( Throwable ex ) {
163
LOG.error( ex , ex );
154
public void fire( final VmRunResponseType reply ) {
155
Logs.extreme( ).error( reply );
157
this.token.redeem( );
158
} catch ( Exception ex ) {
159
LOG.error( this.token + ": " + ex );
160
Logs.extreme( ).error( this.token + ": " + ex, ex );
162
final Volume rootVolume = this.token.getRootVolume( );
163
final String rootVolumeId = ( rootVolume != null ? rootVolume.getDisplayName( ) : null );
164
Function<VmInfo, Boolean> updateInstance = new Function<VmInfo, Boolean>( ) {
166
public Boolean apply( final VmInfo input ) {
167
final VmInstance vm = VmInstances.lookup( input.getInstanceId( ) );
168
vm.updateAddresses( input.getNetParams( ).getIpAddress( ), input.getNetParams( ).getIgnoredPublicIp( ) );
170
vm.getRuntimeState( ).setServiceTag( input.getServiceTag( ) );
171
final Predicate<VmVolumeAttachment> attachVolumes = new Predicate<VmVolumeAttachment>( ) {
172
public boolean apply( VmVolumeAttachment input ) {
173
final String volumeId = input.getVolumeId( );
174
if ( !volumeId.equals( rootVolumeId ) ) {
176
if ( !AttachmentState.attached.equals( input.getAttachmentState( ) ) && !AttachmentState.attaching.equals( input.getAttachmentState( ) ) ) {
177
input.setStatus( AttachmentState.attaching.name( ) );
179
} catch ( Exception ex ) {
180
input.setStatus( AttachmentState.attaching_failed.name( ) );
181
LOG.error( VmRunCallback.this.token + ": " + ex );
182
Logs.extreme( ).error( ex, ex );
168
this.fail( new EucalyptusClusterException( "RunInstances returned false." + this.getRequest( ) ) );
170
} catch ( Exception e ) {
189
vm.eachVolumeAttachment( attachVolumes );
190
} catch ( Exception ex ) {
191
LOG.error( VmRunCallback.this.token + ": " + ex );
192
Logs.extreme( ).error( VmRunCallback.this.token + ": " + ex, ex );
194
} catch ( Exception ex ) {
195
LOG.error( VmRunCallback.this.token + ": " + ex );
196
Logs.extreme( ).error( VmRunCallback.this.token + ": " + ex, ex );
198
final Address addr = VmRunCallback.this.token.getAddress( );
199
if ( addr != null ) {
201
AsyncRequests.newRequest( addr.assign( vm ).getCallback( ) )
202
.then( new Callback.Success<BaseMessage>( ) {
204
public void fire( final BaseMessage response ) {
205
vm.updateAddresses( addr.getInstanceAddress( ), addr.getName( ) );
207
} ).dispatch( vm.getPartition( ) );
208
} catch ( Exception ex ) {
209
LOG.error( VmRunCallback.this.token + ": " + ex );
210
Logs.extreme( ).error( VmRunCallback.this.token + ": " + ex, ex );
211
Addresses.release( addr );
218
for ( final VmInfo vmInfo : reply.getVms( ) ) {
219
if ( this.token.getInstanceId( ).equals( vmInfo.getInstanceId( ) ) ) {
221
Entities.asTransaction( VmInstance.class, updateInstance, 10 ).apply( vmInfo );
223
} catch ( RuntimeException ex ) {
224
LOG.error( "Failed: " + this.token + " because of " + ex.getMessage( ), ex );
228
throw new EucalyptusClusterException( "ccRunInstancesResponse: does not contain requested instance information for: "
229
+ this.token.getInstanceId( )
230
+ " but return status is "
231
+ reply.get_return( )
232
+ "\nccRunInstancesResponse:vms="
178
public void fail( Throwable e ) {
238
public void fireException( final Throwable e ) {
179
239
LOG.debug( LogUtil.header( "Failing run instances because of: " + e.getMessage( ) ), e );
180
LOG.debug( LogUtil.subheader( this.getRequest( ).toString( ) ) );
181
for( String addr : this.token.getAddresses() ) {
240
LOG.debug( LogUtil.subheader( VmRunCallback.this.getRequest( ).toString( ) ) );
241
Predicate<Throwable> rollbackAddr = new Predicate<Throwable>( ) {
244
public boolean apply( Throwable input ) {
245
final Address addr = VmRunCallback.this.token.getAddress( );
183
246
LOG.debug( "-> Release addresses from failed vm run allocation: " + addr );
184
Addresses.getInstance().lookup( addr ).release( );
185
} catch ( NoSuchElementException e1 ) {
186
LOG.debug( "-> Failed to release addresses from failed vm run allocation: " + addr );
189
LOG.debug( "-> Release network index allocation." );
190
if( this.token.getPrimaryNetwork( ) != null ) {
192
NetworkToken net = this.token.getPrimaryNetwork( );
193
Network network = Networks.getInstance( ).lookup( net.getName( ) );
194
for( Integer index : this.token.getPrimaryNetwork( ).getIndexes( ) ) {
195
network.returnNetworkIndex( index );
197
} catch ( Throwable e2 ) {
201
LOG.debug( "-> Release resource tokens for unused resources." );
203
Clusters.getInstance().lookup( token.getCluster() ).getNodeState().releaseToken( token );
204
} catch ( Throwable e2 ) {
249
} catch ( final Exception ex ) {
250
LOG.error( ex.getMessage( ) );
251
Logs.extreme( ).error( ex, ex );
257
Entities.asTransaction( VmInstance.class, Functions.forPredicate( rollbackAddr ) ).apply( e );
258
} catch ( Exception ex ) {
259
Logs.extreme( ).error( ex, ex );
261
Predicate<Throwable> rollbackToken = new Predicate<Throwable>( ) {
264
public boolean apply( Throwable input ) {
265
LOG.debug( "-> Release resource tokens for unused resources." );
267
VmRunCallback.this.token.abort( );
268
} catch ( final Exception ex ) {
269
LOG.error( ex.getMessage( ) );
270
Logs.extreme( ).error( ex, ex );
276
Entities.asTransaction( VmInstance.class, Functions.forPredicate( rollbackToken ) ).apply( e );
277
} catch ( Exception ex ) {
278
Logs.extreme( ).error( ex, ex );
283
public String toString( ) {
284
return "VmRunCallback " + this.token;