Search in sources :

Example 1 with SnapshotScheduleVO

use of com.cloud.storage.SnapshotScheduleVO in project cloudstack by apache.

the class SnapshotManagerImpl method postCreateSnapshot.

private void postCreateSnapshot(Long volumeId, Long snapshotId, Long policyId) {
    Long userId = getSnapshotUserId();
    SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
    if (policyId != Snapshot.MANUAL_POLICY_ID) {
        SnapshotScheduleVO snapshotSchedule = _snapshotScheduleDao.getCurrentSchedule(volumeId, policyId, true);
        if (snapshotSchedule != null) {
            snapshotSchedule.setSnapshotId(snapshotId);
            _snapshotScheduleDao.update(snapshotSchedule.getId(), snapshotSchedule);
        }
    }
    if (snapshot != null && snapshot.isRecursive()) {
        postCreateRecurringSnapshotForPolicy(userId, volumeId, snapshotId, policyId);
    }
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) SnapshotScheduleVO(com.cloud.storage.SnapshotScheduleVO)

Example 2 with SnapshotScheduleVO

use of com.cloud.storage.SnapshotScheduleVO in project cloudstack by apache.

the class SnapshotSchedulerImpl method checkStatusOfCurrentlyExecutingSnapshots.

private void checkStatusOfCurrentlyExecutingSnapshots() {
    final SearchCriteria<SnapshotScheduleVO> sc = _snapshotScheduleDao.createSearchCriteria();
    sc.addAnd("asyncJobId", SearchCriteria.Op.NNULL);
    final List<SnapshotScheduleVO> snapshotSchedules = _snapshotScheduleDao.search(sc, null);
    for (final SnapshotScheduleVO snapshotSchedule : snapshotSchedules) {
        final Long asyncJobId = snapshotSchedule.getAsyncJobId();
        final AsyncJobVO asyncJob = _asyncJobDao.findById(asyncJobId);
        switch(asyncJob.getStatus()) {
            case SUCCEEDED:
                // The snapshot has been successfully backed up.
                // The snapshot state has also been cleaned up.
                // We can schedule the next job for this snapshot.
                // Remove the existing entry in the snapshot_schedule table.
                scheduleNextSnapshotJob(snapshotSchedule);
                break;
            case FAILED:
                // Check the snapshot status.
                final Long snapshotId = snapshotSchedule.getSnapshotId();
                if (snapshotId == null) {
                    // createSnapshotAsync exited, successfully or unsuccessfully,
                    // even before creating a snapshot record
                    // No cleanup needs to be done.
                    // Schedule the next snapshot.
                    scheduleNextSnapshotJob(snapshotSchedule);
                } else {
                    final SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
                    if (snapshot == null || snapshot.getRemoved() != null) {
                        // This snapshot has been deleted successfully from the primary storage
                        // Again no cleanup needs to be done.
                        // Schedule the next snapshot.
                        // There's very little probability that the code reaches this point.
                        // The snapshotId is a foreign key for the snapshot_schedule table
                        // set to ON DELETE CASCADE. So if the snapshot entry is deleted, the snapshot_schedule entry will be too.
                        // But what if it has only been marked as removed?
                        scheduleNextSnapshotJob(snapshotSchedule);
                    } else {
                        // The management server executing this snapshot job appears to have crashed
                        // while creating the snapshot on primary storage/or backing it up.
                        // We have no idea whether the snapshot was successfully taken on the primary or not.
                        // Schedule the next snapshot job.
                        // The ValidatePreviousSnapshotCommand will take appropriate action on this snapshot
                        // If the snapshot was taken successfully on primary, it will retry backing it up.
                        // and cleanup the previous snapshot
                        // Set the userId to that of system.
                        //_snapshotManager.validateSnapshot(1L, snapshot);
                        // In all cases, schedule the next snapshot job
                        scheduleNextSnapshotJob(snapshotSchedule);
                    }
                }
                break;
            case IN_PROGRESS:
                // And it will remain in stasis.
                break;
        }
    }
}
Also used : SnapshotVO(com.cloud.storage.SnapshotVO) SnapshotScheduleVO(com.cloud.storage.SnapshotScheduleVO) AsyncJobVO(org.apache.cloudstack.framework.jobs.impl.AsyncJobVO)

Example 3 with SnapshotScheduleVO

use of com.cloud.storage.SnapshotScheduleVO in project cloudstack by apache.

the class SnapshotSchedulerImpl method scheduleNextSnapshotJob.

@Override
@DB
public Date scheduleNextSnapshotJob(final SnapshotPolicyVO policy) {
    if (policy == null) {
        return null;
    }
    // If display attribute is false then remove schedules if any and return.
    if (!policy.isDisplay()) {
        removeSchedule(policy.getVolumeId(), policy.getId());
        return null;
    }
    final long policyId = policy.getId();
    if (policyId == Snapshot.MANUAL_POLICY_ID) {
        return null;
    }
    final Date nextSnapshotTimestamp = getNextScheduledTime(policyId, _currentTimestamp);
    SnapshotScheduleVO spstSchedVO = _snapshotScheduleDao.findOneByVolumePolicy(policy.getVolumeId(), policy.getId());
    if (spstSchedVO == null) {
        spstSchedVO = new SnapshotScheduleVO(policy.getVolumeId(), policyId, nextSnapshotTimestamp);
        _snapshotScheduleDao.persist(spstSchedVO);
    } else {
        TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        try {
            spstSchedVO = _snapshotScheduleDao.acquireInLockTable(spstSchedVO.getId());
            spstSchedVO.setPolicyId(policyId);
            spstSchedVO.setScheduledTimestamp(nextSnapshotTimestamp);
            spstSchedVO.setAsyncJobId(null);
            spstSchedVO.setSnapshotId(null);
            _snapshotScheduleDao.update(spstSchedVO.getId(), spstSchedVO);
            txn.commit();
        } finally {
            if (spstSchedVO != null) {
                _snapshotScheduleDao.releaseFromLockTable(spstSchedVO.getId());
            }
            txn.close();
        }
    }
    return nextSnapshotTimestamp;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SnapshotScheduleVO(com.cloud.storage.SnapshotScheduleVO) Date(java.util.Date) DB(com.cloud.utils.db.DB)

Example 4 with SnapshotScheduleVO

use of com.cloud.storage.SnapshotScheduleVO in project cloudstack by apache.

the class SnapshotManagerImpl method deletePoliciesForVolume.

@Override
public void deletePoliciesForVolume(Long volumeId) {
    List<SnapshotPolicyVO> policyInstances = listPoliciesforVolume(volumeId);
    for (SnapshotPolicyVO policyInstance : policyInstances) {
        Long policyId = policyInstance.getId();
        deletePolicy(1L, policyId);
    }
    // We also want to delete the manual snapshots scheduled for this volume
    // We can only delete the schedules in the future, not the ones which are already executing.
    SnapshotScheduleVO snapshotSchedule = _snapshotScheduleDao.getCurrentSchedule(volumeId, Snapshot.MANUAL_POLICY_ID, false);
    if (snapshotSchedule != null) {
        _snapshotScheduleDao.expunge(snapshotSchedule.getId());
    }
}
Also used : SnapshotScheduleVO(com.cloud.storage.SnapshotScheduleVO) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Example 5 with SnapshotScheduleVO

use of com.cloud.storage.SnapshotScheduleVO in project cloudstack by apache.

the class SnapshotSchedulerImpl method scheduleSnapshots.

@DB
protected void scheduleSnapshots() {
    String displayTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, _currentTimestamp);
    s_logger.debug("Snapshot scheduler.poll is being called at " + displayTime);
    final List<SnapshotScheduleVO> snapshotsToBeExecuted = _snapshotScheduleDao.getSchedulesToExecute(_currentTimestamp);
    s_logger.debug("Got " + snapshotsToBeExecuted.size() + " snapshots to be executed at " + displayTime);
    for (final SnapshotScheduleVO snapshotToBeExecuted : snapshotsToBeExecuted) {
        SnapshotScheduleVO tmpSnapshotScheduleVO = null;
        final long snapshotScheId = snapshotToBeExecuted.getId();
        final long policyId = snapshotToBeExecuted.getPolicyId();
        final long volumeId = snapshotToBeExecuted.getVolumeId();
        try {
            final VolumeVO volume = _volsDao.findById(volumeId);
            if (volume.getPoolId() == null) {
                // this volume is not attached
                continue;
            }
            Account volAcct = _acctDao.findById(volume.getAccountId());
            if (volAcct == null || volAcct.getState() == Account.State.disabled) {
                // this account has been removed, so don't trigger recurring snapshot
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Skip snapshot for volume " + volume.getUuid() + " since its account has been removed or disabled");
                }
                continue;
            }
            if (_snapshotPolicyDao.findById(policyId) == null) {
                _snapshotScheduleDao.remove(snapshotToBeExecuted.getId());
            }
            if (s_logger.isDebugEnabled()) {
                final Date scheduledTimestamp = snapshotToBeExecuted.getScheduledTimestamp();
                displayTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, scheduledTimestamp);
                s_logger.debug("Scheduling 1 snapshot for volume id " + volumeId + " (volume name:" + volume.getName() + ") for schedule id: " + snapshotToBeExecuted.getId() + " at " + displayTime);
            }
            tmpSnapshotScheduleVO = _snapshotScheduleDao.acquireInLockTable(snapshotScheId);
            final Long eventId = ActionEventUtils.onScheduledActionEvent(User.UID_SYSTEM, volume.getAccountId(), EventTypes.EVENT_SNAPSHOT_CREATE, "creating snapshot for volume Id:" + volumeId, true, 0);
            final Map<String, String> params = new HashMap<String, String>();
            params.put(ApiConstants.VOLUME_ID, "" + volumeId);
            params.put(ApiConstants.POLICY_ID, "" + policyId);
            params.put("ctxUserId", "1");
            params.put("ctxAccountId", "" + volume.getAccountId());
            params.put("ctxStartEventId", String.valueOf(eventId));
            final CreateSnapshotCmd cmd = new CreateSnapshotCmd();
            ComponentContext.inject(cmd);
            _dispatcher.dispatchCreateCmd(cmd, params);
            params.put("id", "" + cmd.getEntityId());
            params.put("ctxStartEventId", "1");
            AsyncJobVO job = new AsyncJobVO("", User.UID_SYSTEM, volume.getAccountId(), CreateSnapshotCmd.class.getName(), ApiGsonHelper.getBuilder().create().toJson(params), cmd.getEntityId(), cmd.getInstanceType() != null ? cmd.getInstanceType().toString() : null, null);
            job.setDispatcher(_asyncDispatcher.getName());
            final long jobId = _asyncMgr.submitAsyncJob(job);
            tmpSnapshotScheduleVO.setAsyncJobId(jobId);
            _snapshotScheduleDao.update(snapshotScheId, tmpSnapshotScheduleVO);
        } catch (final Exception e) {
            // TODO Logging this exception is enough?
            s_logger.warn("Scheduling snapshot failed due to " + e.toString());
        } finally {
            if (tmpSnapshotScheduleVO != null) {
                _snapshotScheduleDao.releaseFromLockTable(snapshotScheId);
            }
        }
    }
}
Also used : Account(com.cloud.user.Account) HashMap(java.util.HashMap) CreateSnapshotCmd(org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd) Date(java.util.Date) ConfigurationException(javax.naming.ConfigurationException) AsyncJobVO(org.apache.cloudstack.framework.jobs.impl.AsyncJobVO) VolumeVO(com.cloud.storage.VolumeVO) SnapshotScheduleVO(com.cloud.storage.SnapshotScheduleVO) DB(com.cloud.utils.db.DB)

Aggregations

SnapshotScheduleVO (com.cloud.storage.SnapshotScheduleVO)7 DB (com.cloud.utils.db.DB)3 SnapshotPolicyVO (com.cloud.storage.SnapshotPolicyVO)2 SnapshotVO (com.cloud.storage.SnapshotVO)2 VolumeVO (com.cloud.storage.VolumeVO)2 Account (com.cloud.user.Account)2 Date (java.util.Date)2 AsyncJobVO (org.apache.cloudstack.framework.jobs.impl.AsyncJobVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConfigurationException (javax.naming.ConfigurationException)1 CreateSnapshotCmd (org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotCmd)1