Skip to content

Commit faeddc1

Browse files
committed
CLOUDSTACK-3659
Implement the download url expiration functionality for volumes Signed off by : nitin mehta<nitin.mehta@citrix.com>
1 parent f831875 commit faeddc1

9 files changed

Lines changed: 127 additions & 19 deletions

File tree

‎engine/api/src/org/apache/cloudstack/storage/datastore/db/VolumeDataStoreDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ public interface VolumeDataStoreDao extends GenericDao<VolumeDataStoreVO, Long>,
4040
VolumeDataStoreVO findByStoreVolume(long storeId, long volumeId, boolean lock);
4141

4242
List<VolumeDataStoreVO> listDestroyed(long storeId);
43+
44+
List<VolumeDataStoreVO> listVolumeDownloadUrls();
4345
}

‎engine/api/src/org/apache/cloudstack/storage/image/datastore/ImageStoreEntity.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ public interface ImageStoreEntity extends DataStore, ImageStore {
4343
String getMountPoint(); // get the mount point on ssvm.
4444

4545
String createEntityExtractUrl(String installPath, ImageFormat format, DataObject dataObject); // get the entity download URL
46+
47+
void deleteExtractUrl(String installPath, String url, ImageFormat format);
4648
}

‎engine/storage/image/src/org/apache/cloudstack/storage/image/store/ImageStoreImpl.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,9 @@ public String createEntityExtractUrl(String installPath, ImageFormat format, Dat
194194
return driver.createEntityExtractUrl(this, installPath, format, dataObject);
195195
}
196196

197+
@Override
198+
public void deleteExtractUrl(String installPath, String url, ImageFormat format) {
199+
driver.deleteEntityExtractUrl(this, installPath, url, format);
200+
}
197201

198202
}

‎engine/storage/src/org/apache/cloudstack/storage/image/BaseImageStoreDriverImpl.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.cloud.agent.api.to.DataObjectType;
2525
import com.cloud.agent.api.to.DataTO;
2626
import com.cloud.configuration.dao.ConfigurationDao;
27+
import com.cloud.storage.Storage;
2728
import com.cloud.storage.VMTemplateStorageResourceAssoc;
2829
import com.cloud.storage.VMTemplateVO;
2930
import com.cloud.storage.VolumeVO;
@@ -246,4 +247,9 @@ public boolean canCopy(DataObject srcData, DataObject destData) {
246247
@Override
247248
public void resize(DataObject data, AsyncCompletionCallback<CreateCmdResult> callback) {
248249
}
250+
251+
@Override
252+
public void deleteEntityExtractUrl(DataStore store, String installPath, String url, Storage.ImageFormat format){
253+
}
254+
249255
}

‎engine/storage/src/org/apache/cloudstack/storage/image/ImageStoreDriver.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@
2525
import com.cloud.storage.Storage.ImageFormat;
2626

2727
public interface ImageStoreDriver extends DataStoreDriver {
28+
2829
String createEntityExtractUrl(DataStore store, String installPath, ImageFormat format, DataObject dataObject);
30+
31+
void deleteEntityExtractUrl(DataStore store, String installPath, String url, ImageFormat format);
32+
2933
}

‎engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class VolumeDataStoreDaoImpl extends GenericDaoBase<VolumeDataStoreVO, Lo
4545
private SearchBuilder<VolumeDataStoreVO> storeSearch;
4646
private SearchBuilder<VolumeDataStoreVO> cacheSearch;
4747
private SearchBuilder<VolumeDataStoreVO> storeVolumeSearch;
48+
private SearchBuilder<VolumeDataStoreVO> downloadVolumeSearch;
4849

4950
@Override
5051
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@@ -77,6 +78,12 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
7778
updateStateSearch.and("state", updateStateSearch.entity().getState(), Op.EQ);
7879
updateStateSearch.and("updatedCount", updateStateSearch.entity().getUpdatedCount(), Op.EQ);
7980
updateStateSearch.done();
81+
82+
downloadVolumeSearch = createSearchBuilder();
83+
downloadVolumeSearch.and("download_url", downloadVolumeSearch.entity().getExtractUrl(), Op.NNULL);
84+
downloadVolumeSearch.and("destroyed", downloadVolumeSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
85+
downloadVolumeSearch.done();
86+
8087
return true;
8188
}
8289

@@ -186,4 +193,11 @@ public List<VolumeDataStoreVO> listDestroyed(long id) {
186193
sc.setParameters("destroyed", true);
187194
return listIncludingRemovedBy(sc);
188195
}
196+
197+
@Override
198+
public List<VolumeDataStoreVO> listVolumeDownloadUrls() {
199+
SearchCriteria<VolumeDataStoreVO> sc = downloadVolumeSearch.create();
200+
sc.setParameters("destroyed", false);
201+
return listBy(sc);
202+
}
189203
}

‎plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import javax.inject.Inject;
2424

2525
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
26+
27+
import com.cloud.agent.api.storage.DeleteEntityDownloadURLCommand;
28+
import com.cloud.storage.Storage;
29+
import com.cloud.storage.Upload;
2630
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
2731
import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint;
2832
import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector;
@@ -76,6 +80,23 @@ public String createEntityExtractUrl(DataStore store, String installPath, ImageF
7680
return generateCopyUrl(ep.getPublicAddr(), uuid);
7781
}
7882

83+
@Override
84+
public void deleteEntityExtractUrl(DataStore store, String installPath, String downloadUrl, ImageFormat format) {
85+
// find an endpoint to send command
86+
EndPoint ep = _epSelector.select(store);
87+
// Create Symlink at ssvm
88+
//CreateEntityDownloadURLCommand cmd = new CreateEntityDownloadURLCommand(((ImageStoreEntity) store).getMountPoint(), installPath, uuid);
89+
DeleteEntityDownloadURLCommand cmd = new DeleteEntityDownloadURLCommand(installPath, Upload.Type.VOLUME, downloadUrl, ((ImageStoreEntity) store).getMountPoint());
90+
91+
Answer ans = ep.sendMessage(cmd);
92+
if (ans == null || !ans.getResult()) {
93+
String errorString = "Unable to delete the url " + downloadUrl + " for path " + installPath + " on ssvm, " + ans.getDetails();
94+
s_logger.error(errorString);
95+
throw new CloudRuntimeException(errorString);
96+
}
97+
98+
}
99+
79100
private String generateCopyUrl(String ipAddress, String uuid){
80101

81102
String hostname = ipAddress;

‎server/src/com/cloud/storage/StorageManagerImpl.java‎

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import javax.inject.Inject;
4242
import javax.naming.ConfigurationException;
4343

44+
import com.cloud.agent.api.storage.DeleteEntityDownloadURLCommand;
45+
import com.cloud.utils.DateUtil;
4446
import org.apache.cloudstack.api.command.admin.storage.AddImageStoreCmd;
4547
import org.apache.cloudstack.api.command.admin.storage.CancelPrimaryStorageMaintenanceCmd;
4648
import org.apache.cloudstack.api.command.admin.storage.CreateSecondaryStagingStoreCmd;
@@ -49,26 +51,8 @@
4951
import org.apache.cloudstack.api.command.admin.storage.DeleteImageStoreCmd;
5052
import org.apache.cloudstack.api.command.admin.storage.DeletePoolCmd;
5153
import org.apache.cloudstack.api.command.admin.storage.UpdateStoragePoolCmd;
52-
import org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope;
53-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
54-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreLifeCycle;
55-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
56-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider;
57-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProviderManager;
58-
import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector;
59-
import org.apache.cloudstack.engine.subsystem.api.storage.HostScope;
60-
import org.apache.cloudstack.engine.subsystem.api.storage.HypervisorHostListener;
61-
import org.apache.cloudstack.engine.subsystem.api.storage.ImageStoreProvider;
62-
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo;
63-
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotDataFactory;
64-
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
65-
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
66-
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateDataFactory;
67-
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateService;
68-
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
69-
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeService;
54+
import org.apache.cloudstack.engine.subsystem.api.storage.*;
7055
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult;
71-
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
7256
import org.apache.cloudstack.framework.async.AsyncCallFuture;
7357
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
7458
import org.apache.cloudstack.storage.datastore.db.ImageStoreDetailsDao;
@@ -81,6 +65,7 @@
8165
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
8266
import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreDao;
8367
import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO;
68+
import org.apache.cloudstack.storage.image.datastore.ImageStoreEntity;
8469
import org.apache.log4j.Logger;
8570
import org.springframework.stereotype.Component;
8671

@@ -257,6 +242,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
257242
DataStoreManager _dataStoreMgr;
258243
@Inject
259244
DataStoreProviderManager _dataStoreProviderMgr;
245+
260246
@Inject
261247
private TemplateService _imageSrv;
262248
@Inject
@@ -291,6 +277,8 @@ public void setDiscoverers(List<StoragePoolDiscoverer> _discoverers) {
291277
boolean _storageCleanupEnabled;
292278
boolean _templateCleanupEnabled = true;
293279
int _storageCleanupInterval;
280+
int _downloadUrlCleanupInterval;
281+
int _downloadUrlExpirationInterval;
294282
private int _createVolumeFromSnapshotWait;
295283
private int _copyvolumewait;
296284
int _storagePoolAcquisitionWaitSeconds = 1800; // 30 minutes
@@ -510,6 +498,12 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
510498
int wrks = NumbersUtil.parseInt(workers, 10);
511499
_executor = Executors.newScheduledThreadPool(wrks, new NamedThreadFactory("StorageManager-Scavenger"));
512500

501+
String cleanupInterval = configs.get("extract.url.cleanup.interval");
502+
_downloadUrlCleanupInterval = NumbersUtil.parseInt(cleanupInterval, 7200);
503+
504+
String urlExpirationInterval = configs.get("extract.url.expiration.interval");
505+
_downloadUrlExpirationInterval = NumbersUtil.parseInt(urlExpirationInterval, 14400);
506+
513507
_agentMgr.registerForHostEvents(ComponentContext.inject(LocalStoragePoolListener.class), true, false, false);
514508

515509
String maxVolumeSizeInGbString = _configDao.getValue("storage.max.volume.size");
@@ -571,6 +565,8 @@ public boolean start() {
571565
} else {
572566
s_logger.debug("Storage cleanup is not enabled, so the storage cleanup thread is not being scheduled.");
573567
}
568+
569+
_executor.scheduleWithFixedDelay(new DownloadURLGarbageCollector(), _downloadUrlCleanupInterval, _downloadUrlCleanupInterval, TimeUnit.SECONDS);
574570
return true;
575571
}
576572

@@ -1097,6 +1093,35 @@ List<String> findAllSnapshotForVolume(Long volumeId) {
10971093
}
10981094
}
10991095

1096+
1097+
public void cleanupDownloadUrls(){
1098+
// find volumesOnImageStoreList with download url
1099+
List<VolumeDataStoreVO> volumesOnImageStoreList = _volumeStoreDao.listVolumeDownloadUrls();
1100+
1101+
for(VolumeDataStoreVO volumeOnImageStore : volumesOnImageStoreList){
1102+
1103+
try {
1104+
long downloadUrlCurrentAgeInSecs = DateUtil.getTimeDifference(DateUtil.now(), volumeOnImageStore.getUpdated());
1105+
if(downloadUrlCurrentAgeInSecs < _downloadUrlExpirationInterval){ // URL hasnt expired yet
1106+
continue;
1107+
}
1108+
1109+
s_logger.debug("Removing download url " + volumeOnImageStore.getExtractUrl() + " for volume id " + volumeOnImageStore.getVolumeId());
1110+
1111+
// Remove it from image store
1112+
ImageStoreEntity secStore = (ImageStoreEntity) _dataStoreMgr.getDataStore(volumeOnImageStore.getDataStoreId(), DataStoreRole.Image);
1113+
secStore.deleteExtractUrl(volumeOnImageStore.getInstallPath(), volumeOnImageStore.getExtractUrl(), ImageFormat.VHD);
1114+
1115+
// Now remove it from DB.
1116+
volumeOnImageStore.setExtractUrl(null);
1117+
_volumeStoreDao.update(volumeOnImageStore.getId(), volumeOnImageStore);
1118+
}catch(Throwable th){
1119+
s_logger.warn("caught exception while deleting download url " +volumeOnImageStore.getExtractUrl(), th);
1120+
}
1121+
}
1122+
1123+
}
1124+
11001125
@Override
11011126
@DB
11021127
public void cleanupSecondaryStorage(boolean recurring) {
@@ -1262,6 +1287,25 @@ public void run() {
12621287
}
12631288
}
12641289

1290+
1291+
protected class DownloadURLGarbageCollector implements Runnable {
1292+
1293+
public DownloadURLGarbageCollector() {
1294+
}
1295+
1296+
@Override
1297+
public void run() {
1298+
try {
1299+
s_logger.trace("Download URL Garbage Collection Thread is running.");
1300+
1301+
cleanupDownloadUrls();
1302+
1303+
} catch (Exception e) {
1304+
s_logger.error("Caught the following Exception", e);
1305+
}
1306+
}
1307+
}
1308+
12651309
@Override
12661310
public void onManagementNodeJoined(List<ManagementServerHostVO> nodeList, long selfNodeId) {
12671311
// TODO Auto-generated method stub

‎utils/src/com/cloud/utils/DateUtil.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,17 @@ public static Date getNextRunTime(IntervalType type, String schedule, String tim
227227
}
228228

229229
return scheduleTime.getTime();
230+
}
231+
232+
public static long getTimeDifference(Date date1, Date date2){
233+
234+
Calendar dateCalendar1 = Calendar.getInstance();
235+
dateCalendar1.setTime(date1);
236+
Calendar dateCalendar2 = Calendar.getInstance();
237+
dateCalendar2.setTime(date2);
238+
239+
return (dateCalendar1.getTimeInMillis() - dateCalendar2.getTimeInMillis() )/1000;
240+
230241
}
231242

232243
// test only

0 commit comments

Comments
 (0)