Search in sources :

Example 1 with IntervalType

use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.

the class SnapshotManagerImpl method getSnapshotType.

private Type getSnapshotType(Long policyId) {
    if (policyId.equals(Snapshot.MANUAL_POLICY_ID)) {
        return Type.MANUAL;
    } else {
        SnapshotPolicyVO spstPolicyVO = _snapshotPolicyDao.findById(policyId);
        IntervalType intvType = DateUtil.getIntervalType(spstPolicyVO.getInterval());
        return getSnapshotType(intvType);
    }
}
Also used : IntervalType(com.cloud.utils.DateUtil.IntervalType) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Example 2 with IntervalType

use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.

the class SnapshotSchedulerImpl method getNextScheduledTime.

private Date getNextScheduledTime(final long policyId, final Date currentTimestamp) {
    final SnapshotPolicyVO policy = _snapshotPolicyDao.findById(policyId);
    Date nextTimestamp = null;
    if (policy != null) {
        final short intervalType = policy.getInterval();
        final IntervalType type = DateUtil.getIntervalType(intervalType);
        final String schedule = policy.getSchedule();
        final String timezone = policy.getTimezone();
        nextTimestamp = DateUtil.getNextRunTime(type, schedule, timezone, currentTimestamp);
        final String currentTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, currentTimestamp);
        final String nextScheduledTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, nextTimestamp);
        s_logger.debug("Current time is " + currentTime + ". NextScheduledTime of policyId " + policyId + " is " + nextScheduledTime);
    }
    return nextTimestamp;
}
Also used : IntervalType(com.cloud.utils.DateUtil.IntervalType) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO) Date(java.util.Date)

Example 3 with IntervalType

use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.

the class SnapshotManagerTest method validatePersistSnapshotPolicyLockAquiredUpdateSnapshotPolicy.

@Test
public void validatePersistSnapshotPolicyLockAquiredUpdateSnapshotPolicy() {
    PowerMockito.mockStatic(GlobalLock.class);
    BDDMockito.given(GlobalLock.getInternLock(Mockito.anyString())).willReturn(globalLockMock);
    Mockito.doReturn(true).when(globalLockMock).lock(Mockito.anyInt());
    for (IntervalType intervalType : listIntervalTypes) {
        Mockito.doReturn(snapshotPolicyVoInstance).when(snapshotPolicyDaoMock).findOneByVolumeInterval(Mockito.anyLong(), Mockito.eq(intervalType));
        Mockito.doNothing().when(_snapshotMgr).updateSnapshotPolicy(Mockito.any(SnapshotPolicyVO.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean());
        Mockito.doNothing().when(_snapshotMgr).createTagsForSnapshotPolicy(mapStringStringMock, snapshotPolicyVoMock);
        SnapshotPolicyVO result = _snapshotMgr.persistSnapshotPolicy(volumeMock, TEST_SNAPSHOT_POLICY_SCHEDULE, TEST_SNAPSHOT_POLICY_TIMEZONE, intervalType, TEST_SNAPSHOT_POLICY_MAX_SNAPS, TEST_SNAPSHOT_POLICY_DISPLAY, TEST_SNAPSHOT_POLICY_ACTIVE, null);
        assertSnapshotPolicyResultAgainstPreBuiltInstance(result);
    }
    VerificationMode timesVerification = Mockito.times(listIntervalTypes.size());
    Mockito.verify(_snapshotMgr, Mockito.never()).createSnapshotPolicy(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean());
    Mockito.verify(_snapshotMgr, timesVerification).updateSnapshotPolicy(Mockito.any(SnapshotPolicyVO.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean());
    Mockito.verify(_snapshotMgr, timesVerification).createTagsForSnapshotPolicy(Mockito.any(), Mockito.any());
}
Also used : IntervalType(com.cloud.utils.DateUtil.IntervalType) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO) VerificationMode(org.mockito.verification.VerificationMode) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with IntervalType

use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.

the class SnapshotManagerTest method validatePersistSnapshotPolicyLockAquiredCreateSnapshotPolicy.

@Test
public void validatePersistSnapshotPolicyLockAquiredCreateSnapshotPolicy() {
    PowerMockito.mockStatic(GlobalLock.class);
    BDDMockito.given(GlobalLock.getInternLock(Mockito.anyString())).willReturn(globalLockMock);
    Mockito.doReturn(true).when(globalLockMock).lock(Mockito.anyInt());
    for (IntervalType intervalType : listIntervalTypes) {
        Mockito.doReturn(null).when(snapshotPolicyDaoMock).findOneByVolumeInterval(Mockito.anyLong(), Mockito.eq(intervalType));
        Mockito.doReturn(snapshotPolicyVoInstance).when(_snapshotMgr).createSnapshotPolicy(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.eq(intervalType), Mockito.anyInt(), Mockito.anyBoolean());
        Mockito.doNothing().when(_snapshotMgr).createTagsForSnapshotPolicy(mapStringStringMock, snapshotPolicyVoMock);
        SnapshotPolicyVO result = _snapshotMgr.persistSnapshotPolicy(volumeMock, TEST_SNAPSHOT_POLICY_SCHEDULE, TEST_SNAPSHOT_POLICY_TIMEZONE, intervalType, TEST_SNAPSHOT_POLICY_MAX_SNAPS, TEST_SNAPSHOT_POLICY_DISPLAY, TEST_SNAPSHOT_POLICY_ACTIVE, null);
        assertSnapshotPolicyResultAgainstPreBuiltInstance(result);
    }
    VerificationMode timesVerification = Mockito.times(listIntervalTypes.size());
    Mockito.verify(_snapshotMgr, timesVerification).createSnapshotPolicy(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean());
    Mockito.verify(_snapshotMgr, Mockito.never()).updateSnapshotPolicy(Mockito.any(SnapshotPolicyVO.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean());
    Mockito.verify(_snapshotMgr, timesVerification).createTagsForSnapshotPolicy(Mockito.any(), Mockito.any());
}
Also used : IntervalType(com.cloud.utils.DateUtil.IntervalType) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO) VerificationMode(org.mockito.verification.VerificationMode) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with IntervalType

use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.

the class SnapshotManagerImpl method createPolicy.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_POLICY_CREATE, eventDescription = "creating snapshot policy")
public SnapshotPolicyVO createPolicy(CreateSnapshotPolicyCmd cmd, Account policyOwner) {
    Long volumeId = cmd.getVolumeId();
    boolean display = cmd.isDisplay();
    VolumeVO volume = _volsDao.findById(cmd.getVolumeId());
    if (volume == null) {
        throw new InvalidParameterValueException("Failed to create snapshot policy, unable to find a volume with id " + volumeId);
    }
    String volumeDescription = volume.getVolumeDescription();
    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
    // If display is false we don't actually schedule snapshots.
    if (volume.getState() != Volume.State.Ready && display) {
        throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
    }
    if (volume.getTemplateId() != null) {
        VMTemplateVO template = _templateDao.findById(volume.getTemplateId());
        Long instanceId = volume.getInstanceId();
        UserVmVO userVmVO = null;
        if (instanceId != null) {
            userVmVO = _vmDao.findById(instanceId);
        }
        if (template != null && template.getTemplateType() == Storage.TemplateType.SYSTEM && (userVmVO == null || !UserVmManager.CKS_NODE.equals(userVmVO.getUserVmType()))) {
            throw new InvalidParameterValueException("VolumeId: " + volumeId + " is for System VM , Creating snapshot against System VM volumes is not supported");
        }
    }
    AccountVO owner = _accountDao.findById(volume.getAccountId());
    Long instanceId = volume.getInstanceId();
    String intervalType = cmd.getIntervalType();
    if (instanceId != null) {
        // It is not detached, but attached to a VM
        if (_vmDao.findById(instanceId) == null) {
            // It is not a UserVM but a SystemVM or DomR
            throw new InvalidParameterValueException(String.format("Failed to create snapshot policy [%s] for volume %s; Snapshots of volumes attached to System or router VM are not allowed.", intervalType, volumeDescription));
        }
    }
    IntervalType intvType = DateUtil.IntervalType.getIntervalType(intervalType);
    if (intvType == null) {
        throw new InvalidParameterValueException("Unsupported interval type " + intervalType);
    }
    Type type = getSnapshotType(intvType);
    String cmdTimezone = cmd.getTimezone();
    TimeZone timeZone = TimeZone.getTimeZone(cmdTimezone);
    String timezoneId = timeZone.getID();
    if (!timezoneId.equals(cmdTimezone)) {
        s_logger.warn(String.format("Using timezone [%s] for running the snapshot policy [%s] for volume %s, as an equivalent of [%s].", timezoneId, intervalType, volumeDescription, cmdTimezone));
    }
    String schedule = cmd.getSchedule();
    try {
        DateUtil.getNextRunTime(intvType, schedule, timezoneId, null);
    } catch (Exception e) {
        throw new InvalidParameterValueException(String.format("%s has an invalid schedule [%s] for interval type [%s].", volumeDescription, schedule, intervalType));
    }
    int maxSnaps = cmd.getMaxSnaps();
    if (maxSnaps <= 0) {
        throw new InvalidParameterValueException(String.format("maxSnaps [%s] for volume %s should be greater than 0.", maxSnaps, volumeDescription));
    }
    int intervalMaxSnaps = type.getMax();
    if (maxSnaps > intervalMaxSnaps) {
        throw new InvalidParameterValueException(String.format("maxSnaps [%s] for volume %s exceeds limit [%s] for interval type [%s].", maxSnaps, volumeDescription, intervalMaxSnaps, intervalType));
    }
    // Verify that max doesn't exceed domain and account snapshot limits in case display is on
    if (display) {
        long accountLimit = _resourceLimitMgr.findCorrectResourceLimitForAccount(owner, ResourceType.snapshot);
        long domainLimit = _resourceLimitMgr.findCorrectResourceLimitForDomain(_domainMgr.getDomain(owner.getDomainId()), ResourceType.snapshot);
        if (!_accountMgr.isRootAdmin(owner.getId()) && ((accountLimit != -1 && maxSnaps > accountLimit) || (domainLimit != -1 && maxSnaps > domainLimit))) {
            String message = "domain/account";
            if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT) {
                message = "domain/project";
            }
            throw new InvalidParameterValueException("Max number of snapshots shouldn't exceed the " + message + " level snapshot limit");
        }
    }
    Map<String, String> tags = cmd.getTags();
    boolean active = true;
    return persistSnapshotPolicy(volume, schedule, timezoneId, intvType, maxSnaps, display, active, tags);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) IntervalType(com.cloud.utils.DateUtil.IntervalType) VMTemplateVO(com.cloud.storage.VMTemplateVO) AccountVO(com.cloud.user.AccountVO) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) ScopeType(com.cloud.storage.ScopeType) Type(com.cloud.storage.Snapshot.Type) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ResourceType(com.cloud.configuration.Resource.ResourceType) IntervalType(com.cloud.utils.DateUtil.IntervalType) ResourceObjectType(com.cloud.server.ResourceTag.ResourceObjectType) StoragePoolType(com.cloud.storage.Storage.StoragePoolType) TimeZone(java.util.TimeZone) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

IntervalType (com.cloud.utils.DateUtil.IntervalType)5 SnapshotPolicyVO (com.cloud.storage.SnapshotPolicyVO)4 Test (org.junit.Test)2 VerificationMode (org.mockito.verification.VerificationMode)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ResourceType (com.cloud.configuration.Resource.ResourceType)1 ActionEvent (com.cloud.event.ActionEvent)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)1 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)1 ResourceObjectType (com.cloud.server.ResourceTag.ResourceObjectType)1 ScopeType (com.cloud.storage.ScopeType)1 Type (com.cloud.storage.Snapshot.Type)1 StoragePoolType (com.cloud.storage.Storage.StoragePoolType)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1 VolumeVO (com.cloud.storage.VolumeVO)1 AccountVO (com.cloud.user.AccountVO)1 DB (com.cloud.utils.db.DB)1