3232import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
3333import org .apache .cloudstack .engine .subsystem .api .storage .EndPoint ;
3434import org .apache .cloudstack .engine .subsystem .api .storage .EndPointSelector ;
35- import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
35+ import org .apache .cloudstack .framework .config .ConfigKey ;
36+ import org .apache .cloudstack .framework .config .Configurable ;
3637import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
3738import org .apache .cloudstack .storage .command .UploadStatusAnswer ;
3839import org .apache .cloudstack .storage .command .UploadStatusCommand ;
40+ import org .apache .cloudstack .storage .command .UploadStatusCommand .EntityType ;
3941import org .apache .cloudstack .storage .datastore .db .VolumeDataStoreDao ;
4042import org .apache .cloudstack .storage .datastore .db .VolumeDataStoreVO ;
4143import org .apache .cloudstack .utils .identity .ManagementServerNode ;
6567 */
6668@ Component
6769@ Local (value = {ImageStoreUploadMonitor .class })
68- public class ImageStoreUploadMonitorImpl extends ManagerBase implements ImageStoreUploadMonitor , Listener {
70+ public class ImageStoreUploadMonitorImpl extends ManagerBase implements ImageStoreUploadMonitor , Listener , Configurable {
6971
7072 static final Logger s_logger = Logger .getLogger (ImageStoreUploadMonitorImpl .class );
7173
72- @ Inject
73- private ConfigurationDao _configDao ;
7474 @ Inject
7575 private VolumeDao _volumeDao ;
7676 @ Inject
@@ -87,12 +87,16 @@ public class ImageStoreUploadMonitorImpl extends ManagerBase implements ImageSto
8787 private int _monitoringInterval ;
8888 private long _uploadOperationTimeout ;
8989
90+ static final ConfigKey <Integer > UploadMonitoringInterval = new ConfigKey <Integer >("Advanced" , Integer .class , "upload.monitoring.interval" , "60" ,
91+ "Interval (in seconds) to check the status of volumes that are uploaded using HTTP POST request" , true );
92+ static final ConfigKey <Integer > UploadOperationTimeout = new ConfigKey <Integer >("Advanced" , Integer .class , "upload.operation.timeout" , "10" ,
93+ "Time (in minutes) to wait before abandoning volume upload using HTTP POST request" , true );
94+
9095 @ Override
9196 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
9297 _executor = Executors .newScheduledThreadPool (1 , new NamedThreadFactory ("Upload-Monitor" ));
93- //TODO: use config variable
94- _monitoringInterval = 60 ;
95- _uploadOperationTimeout = 10 * 60 * 1000 ; // 10 minutes
98+ _monitoringInterval = UploadMonitoringInterval .value ();
99+ _uploadOperationTimeout = UploadOperationTimeout .value () * 60 * 1000 ;
96100 _nodeId = ManagementServerNode .getManagementServerId ();
97101 return true ;
98102 }
@@ -176,18 +180,18 @@ protected void runInContext() {
176180 }
177181 Host host = _hostDao .findById (ep .getId ());
178182 if (host != null && host .getManagementServerId () != null && _nodeId == host .getManagementServerId ().longValue ()) {
179- UploadStatusCommand cmd = new UploadStatusCommand (volume .getId ());
183+ UploadStatusCommand cmd = new UploadStatusCommand (volume .getId (), EntityType . Volume );
180184 Answer answer = ep .sendMessage (cmd );
181185 if (answer == null || !(answer instanceof UploadStatusAnswer )) {
182186 s_logger .warn ("No or invalid answer corresponding to UploadStatusCommand for volume " + volumeDataStore .getVolumeId ());
183187 continue ;
184188 }
185- handleStatusResponse ((UploadStatusAnswer )answer , volume , volumeDataStore );
189+ handleVolumeStatusResponse ((UploadStatusAnswer )answer , volume , volumeDataStore );
186190 }
187191 }
188192 }
189193
190- private void handleStatusResponse (final UploadStatusAnswer answer , final VolumeVO volume , final VolumeDataStoreVO volumeDataStore ) {
194+ private void handleVolumeStatusResponse (final UploadStatusAnswer answer , final VolumeVO volume , final VolumeDataStoreVO volumeDataStore ) {
191195 final StateMachine2 <Volume .State , Event , Volume > stateMachine = Volume .State .getStateMachine ();
192196 Transaction .execute (new TransactionCallbackNoReturn () {
193197 @ Override
@@ -204,13 +208,13 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
204208 }
205209 break ;
206210 case IN_PROGRESS :
207- tmpVolumeDataStore . setDownloadState ( VMTemplateStorageResourceAssoc . Status . UPLOAD_IN_PROGRESS );
208- // check for timeout
209- if ( tmpVolumeDataStore . getDownloadState () == VMTemplateStorageResourceAssoc . Status . NOT_UPLOADED ||
210- tmpVolumeDataStore . getDownloadState () == VMTemplateStorageResourceAssoc . Status . UPLOAD_IN_PROGRESS ) {
211+ if ( tmpVolume . getState () == Volume . State . NotUploaded ) {
212+ tmpVolumeDataStore . setDownloadState ( VMTemplateStorageResourceAssoc . Status . UPLOAD_IN_PROGRESS );
213+ stateMachine . transitTo ( tmpVolume , Event . UploadRequested , null , _volumeDao );
214+ } else if ( tmpVolume . getState () == Volume . State . UploadInProgress ) { // check for timeout
211215 if (System .currentTimeMillis () - tmpVolumeDataStore .getCreated ().getTime () > _uploadOperationTimeout ) {
212- tmpVolumeDataStore .setDownloadState (VMTemplateStorageResourceAssoc .Status .DOWNLOAD_ERROR );
213- stateMachine .transitTo (tmpVolume , Event .OperationTimeout , null , _volumeDao );
216+ tmpVolumeDataStore .setDownloadState (VMTemplateStorageResourceAssoc .Status .UPLOAD_ERROR );
217+ stateMachine .transitTo (tmpVolume , Event .OperationFailed , null , _volumeDao );
214218 if (s_logger .isDebugEnabled ()) {
215219 s_logger .debug ("Volume " + tmpVolume .getUuid () + " failed to upload due to operation timed out" );
216220 }
@@ -226,7 +230,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
226230 break ;
227231 case UNKNOWN :
228232 // check for timeout
229- if (tmpVolumeDataStore . getDownloadState () == VMTemplateStorageResourceAssoc . Status . NOT_UPLOADED ) {
233+ if (tmpVolume . getState () == Volume . State . NotUploaded ) {
230234 if (System .currentTimeMillis () - tmpVolumeDataStore .getCreated ().getTime () > _uploadOperationTimeout ) {
231235 tmpVolumeDataStore .setDownloadState (VMTemplateStorageResourceAssoc .Status .ABANDONED );
232236 stateMachine .transitTo (tmpVolume , Event .OperationTimeout , null , _volumeDao );
@@ -246,5 +250,14 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
246250 }
247251 }
248252
253+ @ Override
254+ public String getConfigComponentName () {
255+ return ImageStoreUploadMonitor .class .getSimpleName ();
256+ }
257+
258+ @ Override
259+ public ConfigKey <?>[] getConfigKeys () {
260+ return new ConfigKey <?>[] {UploadMonitoringInterval , UploadOperationTimeout };
261+ }
249262
250263}
0 commit comments