diff --git a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/StorageCacheManager.java b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/StorageCacheManager.java index 0fdec110661..70332c70db8 100644 --- a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/StorageCacheManager.java +++ b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/StorageCacheManager.java @@ -21,4 +21,5 @@ package org.apache.cloudstack.engine.subsystem.api.storage; public interface StorageCacheManager { public DataStore getCacheStorage(Scope scope); + public DataObject createCacheObject(DataObject data, Scope scope); } diff --git a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java index c85565d8266..f943ed49f82 100644 --- a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java +++ b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java @@ -24,6 +24,8 @@ import java.util.Map; import javax.inject.Inject; import javax.naming.ConfigurationException; +import org.apache.cloudstack.engine.subsystem.api.storage.DataMotionService; +import org.apache.cloudstack.engine.subsystem.api.storage.DataObject; import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; import org.apache.cloudstack.engine.subsystem.api.storage.Scope; import org.apache.cloudstack.engine.subsystem.api.storage.StorageCacheManager; @@ -34,6 +36,8 @@ import com.cloud.utils.component.Manager; public class StorageCacheManagerImpl implements StorageCacheManager, Manager { @Inject List storageCacheAllocator; + @Inject + DataMotionService dataMotionSvr; @Override public DataStore getCacheStorage(Scope scope) { for (StorageCacheAllocator allocator : storageCacheAllocator) { @@ -97,4 +101,14 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { // TODO Auto-generated method stub return true; } + + @Override + public DataObject createCacheObject(DataObject data, Scope scope) { + DataStore cacheStore = this.getCacheStorage(scope); + DataObject objOnCacheStore = cacheStore.create(data); + //AsyncCallFuture<> + //dataMotionSvr.copyAsync(data, objOnCacheStore, callback); + // TODO Auto-generated method stub + return null; + } } \ No newline at end of file diff --git a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotDataFactoryImpl.java b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotDataFactoryImpl.java index 0bd40d35122..6a7edc2302c 100644 --- a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotDataFactoryImpl.java +++ b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotDataFactoryImpl.java @@ -63,7 +63,7 @@ public class SnapshotDataFactoryImpl implements SnapshotDataFactory { SnapshotVO snapshot = snapshotDao.findByIdIncludingRemoved(snapshotId); SnapshotObject so = null; if (snapshot.getState() == Snapshot.State.BackedUp) { - DataStore store = objMap.findStore(snapshot.getUuid(), DataObjectType.SNAPSHOT, DataStoreRole.Image); + DataStore store = objMap.findStore(snapshot.getId(), DataObjectType.SNAPSHOT, DataStoreRole.Image); so = SnapshotObject.getSnapshotObject(snapshot, store); } else { VolumeInfo volume = this.volumeFactory.getVolume(snapshot.getVolumeId()); diff --git a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/strategy/AncientSnapshotStrategy.java b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/strategy/AncientSnapshotStrategy.java index 7eddb34943d..ce2786b80b9 100644 --- a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/strategy/AncientSnapshotStrategy.java +++ b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/strategy/AncientSnapshotStrategy.java @@ -470,7 +470,7 @@ public class AncientSnapshotStrategy implements SnapshotStrategy { @DB protected boolean destroySnapshotBackUp(SnapshotVO snapshot) { - DataStore store = objInStoreMgr.findStore(snapshot.getUuid(), DataObjectType.SNAPSHOT, DataStoreRole.Image); + DataStore store = objInStoreMgr.findStore(snapshot.getId(), DataObjectType.SNAPSHOT, DataStoreRole.Image); if (store == null) { s_logger.debug("Can't find snapshot" + snapshot.getId() + " backed up into image store"); return false; diff --git a/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManager.java b/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManager.java index 17b782ab6a0..cb27c0271c9 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManager.java +++ b/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManager.java @@ -32,5 +32,5 @@ public interface ObjectInDataStoreManager { DataObjectInStore findObject(long objId, DataObjectType type, long dataStoreId, DataStoreRole role); DataObjectInStore findObject(DataObject obj, DataStore store); - DataStore findStore(String objUuid, DataObjectType type, DataStoreRole role); + DataStore findStore(long objId, DataObjectType type, DataStoreRole role); } diff --git a/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManagerImpl.java b/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManagerImpl.java index 9f98d5d518d..ac96c5f9f69 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManagerImpl.java +++ b/engine/storage/src/org/apache/cloudstack/storage/datastore/ObjectInDataStoreManagerImpl.java @@ -35,6 +35,7 @@ import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreDao; import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO; +import org.apache.cloudstack.storage.db.ObjectInDataStoreDao; import org.apache.cloudstack.storage.db.ObjectInDataStoreVO; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -69,6 +70,8 @@ public class ObjectInDataStoreManagerImpl implements ObjectInDataStoreManager { VMTemplatePoolDao templatePoolDao; @Inject SnapshotDataFactory snapshotFactory; + @Inject + ObjectInDataStoreDao objInStoreDao; protected StateMachine2 stateMachines; public ObjectInDataStoreManagerImpl() { @@ -110,6 +113,13 @@ public class ObjectInDataStoreManagerImpl implements ObjectInDataStoreManager { VMTemplateStoragePoolVO vo = new VMTemplateStoragePoolVO(dataStore.getId(), obj.getId()); vo = templatePoolDao.persist(vo); } + } else if (dataStore.getRole() == DataStoreRole.ImageCache) { + ObjectInDataStoreVO vo = new ObjectInDataStoreVO(); + vo.setDataStoreRole(dataStore.getRole()); + vo.setDataStoreId(dataStore.getId()); + vo.setObjectType(obj.getType()); + vo.setObjectId(obj.getId()); + vo = objInStoreDao.persist(vo); } else { // Image store switch ( obj.getType()){ @@ -212,6 +222,13 @@ public class ObjectInDataStoreManagerImpl implements ObjectInDataStoreManager { } } else if (type == DataObjectType.TEMPLATE && role == DataStoreRole.Primary) { vo = templatePoolDao.findByPoolTemplate(dataStoreId, objId); + } else if (role == DataStoreRole.ImageCache) { + SearchCriteriaService sc = SearchCriteria2.create(ObjectInDataStoreVO.class); + sc.addAnd(sc.getEntity().getObjectId(), Op.EQ, objId); + sc.addAnd(sc.getEntity().getObjectType(), Op.EQ, type); + sc.addAnd(sc.getEntity().getDataStoreId(), Op.EQ, dataStoreId); + sc.addAnd(sc.getEntity().getDataStoreRole(), Op.EQ, role); + vo = sc.find(); } else { s_logger.debug("Invalid data or store type: " + type + " " + role); throw new CloudRuntimeException("Invalid data or store type: " + type + " " + role); @@ -222,16 +239,16 @@ public class ObjectInDataStoreManagerImpl implements ObjectInDataStoreManager { } @Override - public DataStore findStore(String objUuid, DataObjectType type, DataStoreRole role) { + public DataStore findStore(long objId, DataObjectType type, DataStoreRole role) { DataStore store = null; if (role == DataStoreRole.Image) { SearchCriteriaService sc = SearchCriteria2.create(ObjectInDataStoreVO.class); sc.addAnd(sc.getEntity().getDataStoreRole(), Op.EQ, role); - sc.addAnd(sc.getEntity().getObjectUuid(), Op.EQ, objUuid); + sc.addAnd(sc.getEntity().getObjectId(), Op.EQ, objId); sc.addAnd(sc.getEntity().getObjectType(), Op.EQ, type); ObjectInDataStoreVO vo = sc.find(); if (vo != null) { - store = this.storeMgr.getDataStore(vo.getDataStoreUuid(), vo.getDataStoreRole()); + store = this.storeMgr.getDataStore(vo.getDataStoreId(), vo.getDataStoreRole()); } } return store; diff --git a/engine/storage/src/org/apache/cloudstack/storage/db/ObjectInDataStoreVO.java b/engine/storage/src/org/apache/cloudstack/storage/db/ObjectInDataStoreVO.java index 0fbcbb1f956..7b44de0ce91 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/db/ObjectInDataStoreVO.java +++ b/engine/storage/src/org/apache/cloudstack/storage/db/ObjectInDataStoreVO.java @@ -46,15 +46,15 @@ public class ObjectInDataStoreVO implements StateObject 0) { + TemplateInfo tmpl = store.getTemplate(template.getId()); + if (tmpl != null) { + return tmpl; + } + try { + Thread.sleep(sleepTime * 1000); + } catch (InterruptedException e) { + s_logger.debug("waiting for template download been interrupted: " + e.toString()); + } + tries--; + } + return null; + } @DB protected void createBaseImageAsync(VolumeInfo volume, PrimaryDataStore dataStore, TemplateInfo template, AsyncCallFuture future) { @@ -293,8 +316,21 @@ public class VolumeServiceImpl implements VolumeService { caller.setCallback(caller.getTarget().copyBaseImageCallback(null, null)) .setContext(context); - templateOnPrimaryStoreObj.processEvent(Event.CreateOnlyRequested); - + try { + templateOnPrimaryStoreObj.processEvent(Event.CreateOnlyRequested); + } catch (Exception e) { + try { + templateOnPrimaryStoreObj = waitForTemplateDownloaded(dataStore, template); + } finally { + if (templateOnPrimaryStoreObj == null) { + VolumeApiResult result = new VolumeApiResult(volume); + result.setResult(e.toString()); + caller.complete(result); + return; + } + } + } + try { motionSrv.copyAsync(template, templateOnPrimaryStoreObj, caller); } catch (Exception e) {