Clean up entries in template_store_ref, volume_store_ref and

snapshot_store_ref in case of operation failure.
This commit is contained in:
Min Chen 2013-05-21 12:00:04 -07:00
parent 2f6d94462e
commit 8f549db518
6 changed files with 200 additions and 164 deletions

View File

@ -28,5 +28,5 @@ public interface StorageCacheManager {
* @return
*/
DataObject getCacheObject(DataObject data, Scope scope);
DataObject deleteCacheObject(DataObject data);
boolean deleteCacheObject(DataObject data);
}

View File

@ -81,8 +81,6 @@ public class SnapshotDataStoreVO implements StateObject<ObjectInDataStoreStateMa
@Column (name="install_path")
private String installPath;
@Column(name = GenericDao.REMOVED_COLUMN)
Date removed;
@Column(name="update_count", updatable = true, nullable=false)
protected long updatedCount;

View File

@ -233,7 +233,7 @@ public class AncientDataMotionStrategy implements DataMotionStrategy {
CopyCommand cmd = new CopyCommand(srcData.getTO(), volObj.getTO(), _createVolumeFromSnapshotWait);
EndPoint ep = selector.select(snapObj, volObj);
Answer answer = ep.sendMessage(cmd);
return answer;
} catch (Exception e) {
s_logger.error(basicErrMsg, e);
@ -299,7 +299,7 @@ public class AncientDataMotionStrategy implements DataMotionStrategy {
} else if (srcData.getType() == DataObjectType.SNAPSHOT &&
destData.getType() == DataObjectType.SNAPSHOT) {
answer = copySnapshot(srcData, destData);
}
}
if (answer != null && !answer.getResult()) {
errMsg = answer.getDetails();
@ -368,21 +368,27 @@ public class AncientDataMotionStrategy implements DataMotionStrategy {
int _backupsnapshotwait = NumbersUtil.parseInt(value, Integer.parseInt(Config.BackupSnapshotWait.getDefaultValue()));
DataObject cacheData = null;
Answer answer = null;
try {
if (needCacheStorage(srcData, destData)) {
cacheData = cacheMgr.getCacheObject(srcData, destData.getDataStore().getScope());
if (needCacheStorage(srcData, destData)) {
cacheData = cacheMgr.getCacheObject(srcData, destData.getDataStore().getScope());
CopyCommand cmd = new CopyCommand(srcData.getTO(), destData.getTO(), _backupsnapshotwait);
cmd.setCacheTO(cacheData.getTO());
EndPoint ep = selector.select(srcData, destData);
Answer answer = ep.sendMessage(cmd);
CopyCommand cmd = new CopyCommand(srcData.getTO(), destData.getTO(), _backupsnapshotwait);
cmd.setCacheTO(cacheData.getTO());
EndPoint ep = selector.select(srcData, destData);
answer = ep.sendMessage(cmd);
} else {
CopyCommand cmd = new CopyCommand(srcData.getTO(), destData.getTO(), _backupsnapshotwait);
EndPoint ep = selector.select(srcData, destData);
answer = ep.sendMessage(cmd);
}
// clean up cache entry in case of failure
if (answer == null || !answer.getResult()) {
if (cacheData != null) {
cacheMgr.deleteCacheObject(cacheData);
}
}
return answer;
} else {
CopyCommand cmd = new CopyCommand(srcData.getTO(), destData.getTO(), _backupsnapshotwait);
EndPoint ep = selector.select(srcData, destData);
Answer answer = ep.sendMessage(cmd);
return answer;
}
} catch (Exception e) {
s_logger.debug("copy snasphot failed: " + e.toString());
if (cacheData != null) {

View File

@ -63,7 +63,7 @@ public class TemplateObject implements TemplateInfo {
@Inject
VMTemplateDao imageDao;
@Inject
ObjectInDataStoreManager ojbectInStoreMgr;
ObjectInDataStoreManager objectInStoreMgr;
@Inject VMTemplatePoolDao templatePoolDao;
@Inject TemplateDataStoreDao templateStoreDao;
@ -191,10 +191,15 @@ public class TemplateObject implements TemplateInfo {
}
}
ojbectInStoreMgr.update(this, event);
objectInStoreMgr.update(this, event);
} catch (NoTransitionException e) {
s_logger.debug("failed to update state", e);
throw new CloudRuntimeException("Failed to update state" + e.toString());
} finally{
// in case of OperationFailed, expunge the entry
if ( event == ObjectInDataStoreStateMachine.Event.OperationFailed){
objectInStoreMgr.delete(this);
}
}
}
@ -247,10 +252,15 @@ public class TemplateObject implements TemplateInfo {
this.stateTransit(templEvent);
}
}
ojbectInStoreMgr.update(this, event);
objectInStoreMgr.update(this, event);
} catch (NoTransitionException e) {
s_logger.debug("failed to update state", e);
throw new CloudRuntimeException("Failed to update state" + e.toString());
} finally{
// in case of OperationFailed, expunge the entry
if ( event == ObjectInDataStoreStateMachine.Event.OperationFailed){
objectInStoreMgr.delete(this);
}
}
}
@ -266,7 +276,7 @@ public class TemplateObject implements TemplateInfo {
@Override
public String getInstallPath() {
DataObjectInStore obj = ojbectInStoreMgr.findObject(this, this.dataStore);
DataObjectInStore obj = objectInStoreMgr.findObject(this, this.dataStore);
return obj.getInstallPath();
}

View File

@ -52,34 +52,37 @@ import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.fsm.NoTransitionException;
public class SnapshotObject implements SnapshotInfo {
private static final Logger s_logger = Logger.getLogger(SnapshotObject.class);
private static final Logger s_logger = Logger.getLogger(SnapshotObject.class);
private SnapshotVO snapshot;
private DataStore store;
@Inject
protected SnapshotDao snapshotDao;
@Inject
protected VolumeDao volumeDao;
@Inject protected VolumeDataFactory volFactory;
@Inject protected SnapshotStateMachineManager stateMachineMgr;
@Inject
protected VolumeDataFactory volFactory;
@Inject
protected SnapshotStateMachineManager stateMachineMgr;
@Inject
SnapshotDataFactory snapshotFactory;
@Inject
ObjectInDataStoreManager ojbectInStoreMgr;
ObjectInDataStoreManager objectInStoreMgr;
@Inject
SnapshotDataStoreDao snapshotStoreDao;
public SnapshotObject() {
}
protected void configure(SnapshotVO snapshot, DataStore store) {
this.snapshot = snapshot;
this.store = store;
this.snapshot = snapshot;
this.store = store;
}
public static SnapshotObject getSnapshotObject(SnapshotVO snapshot, DataStore store) {
SnapshotObject snapObj = ComponentContext.inject(SnapshotObject.class);
snapObj.configure(snapshot, store);
return snapObj;
SnapshotObject snapObj = ComponentContext.inject(SnapshotObject.class);
snapObj.configure(snapshot, store);
return snapObj;
}
public DataStore getStore() {
@ -88,17 +91,17 @@ public class SnapshotObject implements SnapshotInfo {
@Override
public SnapshotInfo getParent() {
SnapshotDataStoreVO snapStoreVO = this.snapshotStoreDao.findByStoreSnapshot(this.store.getRole(), this.store.getId(), this.snapshot.getId());
if (snapStoreVO == null) {
return null;
}
SnapshotDataStoreVO snapStoreVO = this.snapshotStoreDao.findByStoreSnapshot(this.store.getRole(), this.store.getId(), this.snapshot.getId());
if (snapStoreVO == null) {
return null;
}
long parentId = snapStoreVO.getParentSnapshotId();
if (parentId == 0) {
return null;
}
long parentId = snapStoreVO.getParentSnapshotId();
if (parentId == 0) {
return null;
}
return this.snapshotFactory.getSnapshot(parentId, store);
return this.snapshotFactory.getSnapshot(parentId, store);
}
@Override
@ -121,7 +124,7 @@ public class SnapshotObject implements SnapshotInfo {
@Override
public long getId() {
return this.snapshot.getId();
return this.snapshot.getId();
}
@Override
@ -136,100 +139,100 @@ public class SnapshotObject implements SnapshotInfo {
@Override
public Long getSize() {
return this.snapshot.getSize();
return this.snapshot.getSize();
}
@Override
public DataObjectType getType() {
return DataObjectType.SNAPSHOT;
return DataObjectType.SNAPSHOT;
}
@Override
public String getUuid() {
return this.snapshot.getUuid();
}
@Override
public void processEvent(
ObjectInDataStoreStateMachine.Event event) {
try {
ojbectInStoreMgr.update(this, event);
} catch (Exception e) {
s_logger.debug("Failed to update state:" + e.toString());
throw new CloudRuntimeException("Failed to update state: " + e.toString());
}
}
@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event) {
try {
objectInStoreMgr.update(this, event);
} catch (Exception e) {
s_logger.debug("Failed to update state:" + e.toString());
throw new CloudRuntimeException("Failed to update state: " + e.toString());
} finally {
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.delete(this);
}
}
}
@Override
public long getAccountId() {
return this.snapshot.getAccountId();
}
@Override
public long getAccountId() {
return this.snapshot.getAccountId();
}
@Override
public long getVolumeId() {
return this.snapshot.getVolumeId();
}
@Override
public long getVolumeId() {
return this.snapshot.getVolumeId();
}
@Override
public String getPath() {
return this.ojbectInStoreMgr.findObject(this, getDataStore()).getInstallPath();
}
@Override
public String getPath() {
return this.objectInStoreMgr.findObject(this, getDataStore()).getInstallPath();
}
@Override
public String getName() {
return this.snapshot.getName();
}
@Override
public String getName() {
return this.snapshot.getName();
}
@Override
public Date getCreated() {
return this.snapshot.getCreated();
}
@Override
public Date getCreated() {
return this.snapshot.getCreated();
}
@Override
public Type getRecurringType() {
return this.snapshot.getRecurringType();
}
@Override
public Type getRecurringType() {
return this.snapshot.getRecurringType();
}
@Override
public State getState() {
return this.snapshot.getState();
}
@Override
public State getState() {
return this.snapshot.getState();
}
@Override
public HypervisorType getHypervisorType() {
return this.snapshot.getHypervisorType();
}
@Override
public HypervisorType getHypervisorType() {
return this.snapshot.getHypervisorType();
}
@Override
public boolean isRecursive() {
return this.snapshot.isRecursive();
}
@Override
public boolean isRecursive() {
return this.snapshot.isRecursive();
}
@Override
public short getsnapshotType() {
return this.snapshot.getsnapshotType();
}
@Override
public short getsnapshotType() {
return this.snapshot.getsnapshotType();
}
@Override
public long getDomainId() {
return this.snapshot.getDomainId();
}
@Override
public long getDomainId() {
return this.snapshot.getDomainId();
}
@Override
public Long getDataCenterId() {
return this.snapshot.getDataCenterId();
}
@Override
public Long getDataCenterId() {
return this.snapshot.getDataCenterId();
}
public void processEvent(Snapshot.Event event) throws NoTransitionException {
stateMachineMgr.processEvent(this.snapshot, event);
}
public void processEvent(Snapshot.Event event)
throws NoTransitionException {
stateMachineMgr.processEvent(this.snapshot, event);
}
public SnapshotVO getSnapshotVO(){
return this.snapshot;
}
public SnapshotVO getSnapshotVO() {
return this.snapshot;
}
@Override
public DataTO getTO() {
@ -242,34 +245,41 @@ public class SnapshotObject implements SnapshotInfo {
@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
SnapshotDataStoreVO snapshotStore = this.snapshotStoreDao.findByStoreSnapshot(this.getDataStore().getRole(),
this.getDataStore().getId(), this.getId());
if (answer instanceof CreateObjectAnswer) {
SnapshotObjectTO snapshotTO = (SnapshotObjectTO)((CreateObjectAnswer) answer).getData();
snapshotStore.setInstallPath(snapshotTO.getPath());
this.snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
} else if (answer instanceof CopyCmdAnswer) {
SnapshotObjectTO snapshotTO = (SnapshotObjectTO)((CopyCmdAnswer) answer).getNewData();
snapshotStore.setInstallPath(snapshotTO.getPath());
if (snapshotTO.getParentSnapshotPath() == null) {
snapshotStore.setParentSnapshotId(0L);
}
this.snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
} else {
throw new CloudRuntimeException("Unknown answer: " + answer.getClass());
}
this.processEvent(event);
try {
SnapshotDataStoreVO snapshotStore = this.snapshotStoreDao.findByStoreSnapshot(this.getDataStore().getRole(), this.getDataStore().getId(),
this.getId());
if (answer instanceof CreateObjectAnswer) {
SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CreateObjectAnswer) answer).getData();
snapshotStore.setInstallPath(snapshotTO.getPath());
this.snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
} else if (answer instanceof CopyCmdAnswer) {
SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CopyCmdAnswer) answer).getNewData();
snapshotStore.setInstallPath(snapshotTO.getPath());
if (snapshotTO.getParentSnapshotPath() == null) {
snapshotStore.setParentSnapshotId(0L);
}
this.snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
} else {
throw new CloudRuntimeException("Unknown answer: " + answer.getClass());
}
} catch (RuntimeException ex) {
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.delete(this);
}
throw ex;
}
this.processEvent(event);
}
@Override
public ObjectInDataStoreStateMachine.State getStatus() {
return this.ojbectInStoreMgr.findObject(this, store).getObjectInStoreState();
return this.objectInStoreMgr.findObject(this, store).getObjectInStoreState();
}
@Override
public void addPayload(Object data) {
// TODO Auto-generated method stub
@Override
public void addPayload(Object data) {
// TODO Auto-generated method stub
}
}
}

View File

@ -59,7 +59,7 @@ public class VolumeObject implements VolumeInfo {
@Inject
VolumeDataStoreDao volumeStoreDao;
@Inject
ObjectInDataStoreManager ojbectInStoreMgr;
ObjectInDataStoreManager objectInStoreMgr;
@Inject
VMInstanceDao vmInstanceDao;
private Object payload;
@ -159,7 +159,7 @@ public class VolumeObject implements VolumeInfo {
if (this.dataStore == null) {
throw new CloudRuntimeException("datastore must be set before using this object");
}
DataObjectInStore obj = ojbectInStoreMgr.findObject(this.volumeVO.getId(), DataObjectType.VOLUME, this.dataStore.getId(), this.dataStore.getRole());
DataObjectInStore obj = objectInStoreMgr.findObject(this.volumeVO.getId(), DataObjectType.VOLUME, this.dataStore.getId(), this.dataStore.getRole());
if (obj.getState() != ObjectInDataStoreStateMachine.State.Ready) {
return this.dataStore.getUri() +
"&" + EncodingType.OBJTYPE + "=" + DataObjectType.VOLUME +
@ -187,11 +187,11 @@ public class VolumeObject implements VolumeInfo {
try {
Volume.Event volEvent = null;
if ( this.dataStore.getRole() == DataStoreRole.ImageCache){
ojbectInStoreMgr.update(this, event);
objectInStoreMgr.update(this, event);
return;
}
if (this.dataStore.getRole() == DataStoreRole.Image) {
ojbectInStoreMgr.update(this, event);
objectInStoreMgr.update(this, event);
if (event == ObjectInDataStoreStateMachine.Event.CreateRequested) {
volEvent = Volume.Event.UploadRequested;
} else if (event == ObjectInDataStoreStateMachine.Event.OperationSuccessed) {
@ -225,6 +225,11 @@ public class VolumeObject implements VolumeInfo {
} catch (Exception e) {
s_logger.debug("Failed to update state", e);
throw new CloudRuntimeException("Failed to update state:" + e.toString());
} finally{
// in case of OperationFailed, expunge the entry
if ( event == ObjectInDataStoreStateMachine.Event.OperationFailed){
objectInStoreMgr.delete(this);
}
}
}
@ -249,7 +254,7 @@ public class VolumeObject implements VolumeInfo {
if (this.dataStore.getRole() == DataStoreRole.Primary) {
return this.volumeVO.getPath();
} else {
DataObjectInStore objInStore = this.ojbectInStoreMgr.findObject(this, dataStore);
DataObjectInStore objInStore = this.objectInStoreMgr.findObject(this, dataStore);
return objInStore.getInstallPath();
}
}
@ -379,34 +384,41 @@ public class VolumeObject implements VolumeInfo {
@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
if (this.dataStore.getRole() == DataStoreRole.Primary) {
if (answer instanceof CopyCmdAnswer) {
CopyCmdAnswer cpyAnswer = (CopyCmdAnswer)answer;
VolumeVO vol = this.volumeDao.findById(this.getId());
VolumeObjectTO newVol = (VolumeObjectTO)cpyAnswer.getNewData();
vol.setPath(newVol.getPath());
vol.setSize(newVol.getSize());
vol.setPoolId(this.getDataStore().getId());
volumeDao.update(vol.getId(), vol);
} else if (answer instanceof CreateObjectAnswer) {
CreateObjectAnswer createAnswer =(CreateObjectAnswer)answer;
VolumeObjectTO newVol = (VolumeObjectTO)createAnswer.getData();
VolumeVO vol = this.volumeDao.findById(this.getId());
vol.setPath(newVol.getPath());
vol.setSize(newVol.getSize());
vol.setPoolId(this.getDataStore().getId());
volumeDao.update(vol.getId(), vol);
}
} else if (this.dataStore.getRole() == DataStoreRole.Image) {
if (answer instanceof DownloadAnswer) {
DownloadAnswer dwdAnswer = (DownloadAnswer)answer;
VolumeDataStoreVO volStore = this.volumeStoreDao.findByStoreVolume(this.dataStore.getId(), this.getId());
volStore.setInstallPath(dwdAnswer.getInstallPath());
volStore.setChecksum(dwdAnswer.getCheckSum());
this.volumeStoreDao.update(volStore.getId(), volStore);
}
}
try {
if (this.dataStore.getRole() == DataStoreRole.Primary) {
if (answer instanceof CopyCmdAnswer) {
CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
VolumeVO vol = this.volumeDao.findById(this.getId());
VolumeObjectTO newVol = (VolumeObjectTO) cpyAnswer.getNewData();
vol.setPath(newVol.getPath());
vol.setSize(newVol.getSize());
vol.setPoolId(this.getDataStore().getId());
volumeDao.update(vol.getId(), vol);
} else if (answer instanceof CreateObjectAnswer) {
CreateObjectAnswer createAnswer = (CreateObjectAnswer) answer;
VolumeObjectTO newVol = (VolumeObjectTO) createAnswer.getData();
VolumeVO vol = this.volumeDao.findById(this.getId());
vol.setPath(newVol.getPath());
vol.setSize(newVol.getSize());
vol.setPoolId(this.getDataStore().getId());
volumeDao.update(vol.getId(), vol);
}
} else if (this.dataStore.getRole() == DataStoreRole.Image) {
if (answer instanceof DownloadAnswer) {
DownloadAnswer dwdAnswer = (DownloadAnswer) answer;
VolumeDataStoreVO volStore = this.volumeStoreDao.findByStoreVolume(this.dataStore.getId(), this.getId());
volStore.setInstallPath(dwdAnswer.getInstallPath());
volStore.setChecksum(dwdAnswer.getCheckSum());
this.volumeStoreDao.update(volStore.getId(), volStore);
}
}
} catch (RuntimeException ex) {
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.delete(this);
}
throw ex;
}
this.processEvent(event);
this.processEvent(event);
}
}