internal override async Task <HttpResponseMessage> RunAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            this.ThrowInvalidArgumentIfNull(this.backupPolicy);
            RetentionManager retentionManager = await RetentionManager.CreateOrGetRetentionManager(this.StatefulService);

            try
            {
                using (var transaction = this.StatefulService.StateManager.CreateTransaction())
                {
                    await
                    this.BackupPolicyStore.AddAsync(this.backupPolicy.Name,
                                                    BackupPolicyModel.FromBackupPolicyView(this.backupPolicy)
                                                    , timeout, cancellationToken, transaction);

                    await retentionManager.CreatePolicyAsync(BackupPolicyModel.FromBackupPolicyView(this.backupPolicy), timeout, cancellationToken, transaction);

                    await transaction.CommitAsync();
                }
            }
            catch (FabricBackupRestoreKeyAlreadyExisingException)
            {
                throw new FabricException(StringResources.BackupPolicyAlreayExists, FabricErrorCode.BackupPolicyAlreayExists);
            }
            FabricEvents.Events.BackupPolicyDetails(this.backupPolicy.Name, this.backupPolicy.MaxIncrementalBackups,
                                                    this.backupPolicy.AutoRestoreOnDataLoss == true ? 1 : 0,
                                                    this.backupPolicy.Schedule.ScheduleKind == Enums.BackupScheduleType.TimeBased ? 1 : 0,
                                                    this.backupPolicy.Schedule.ScheduleKind == Enums.BackupScheduleType.FrequencyBased ? 1 : 0,
                                                    this.backupPolicy.Storage.StorageKind == Enums.BackupStorageType.AzureBlobStore ? 1 : 0,
                                                    this.backupPolicy.Storage.StorageKind == Enums.BackupStorageType.FileShare ? 1 : 0);

            return(new HttpResponseMessage(HttpStatusCode.Created));
        }
Пример #2
0
        internal override async Task <HttpResponseMessage> RunAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            this.ThrowInvalidArgumentIfNull(this.backupPolicy);
            if (string.IsNullOrEmpty(this.backupPolicyName))
            {
                throw new ArgumentException();
            }

            var workItemQueue = await WorkItemQueue.CreateOrGetWorkItemQueue(this.StatefulService);

            var modelBackupPolicy = BackupPolicyModel.FromBackupPolicyView(this.backupPolicy);
            var policyBuilder     = modelBackupPolicy.ToBuilder()
                                    .WithName(this.backupPolicyName);
            RetentionManager retentionManager = await RetentionManager.CreateOrGetRetentionManager(this.StatefulService);

            using (var transaction = this.StatefulService.StateManager.CreateTransaction())
            {
                var existingBackupPolicy = await this.BackupPolicyStore.GetValueWithUpdateLockModeAsync(this.backupPolicyName, timeout, cancellationToken, transaction);

                if (existingBackupPolicy == null)
                {
                    throw new FabricException(StringResources.BackupPolicyDoesNotExist, FabricErrorCode.BackupPolicyDoesNotExist);
                }

                var existingEnablementSet = existingBackupPolicy.BackupEnabledSet;
                policyBuilder.WithBackupEnabledSet(existingEnablementSet);

                var updatedBackupPolicy = policyBuilder.Build();

                await this.BackupPolicyStore.UpdateValueAsync(this.backupPolicyName, existingBackupPolicy, updatedBackupPolicy, timeout, cancellationToken, transaction);

                if (existingEnablementSet.Count > 0)
                {
                    await
                    workItemQueue.AddWorkItem(
                        new UpdateEnablementWorkItem(existingEnablementSet.ToList(),
                                                     new WorkItemInfo {
                        WorkItemType = WorkItemPropogationType.UpdateBackupPolicy,
                        BackupPolicyUpdateGuid = updatedBackupPolicy.UniqueId
                    } ), timeout, cancellationToken, transaction);
                }

                await retentionManager.UpdatePolicyAsync(this.backupPolicyName, timeout, cancellationToken, transaction);

                await transaction.CommitAsync();
            }


            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        internal override async Task <HttpResponseMessage> RunAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(this.backupPolicyName))
            {
                throw new ArgumentException(StringResources.InvalidArguments);
            }
            var backupPolicy = await this.BackupPolicyStore.GetValueAsync(this.backupPolicyName);

            BackupMappingStore backupMappingStore = await
                                                    BackupMappingStore.CreateOrGetBackupMappingStore(this.StatefulService);

            SuspendStore suspendStore = await SuspendStore.CreateOrGetSuspendStatusStore(this.StatefulService);

            List <string> allSuspensions = await suspendStore.GetAllSuspensions(timeout, cancellationToken);

            if (backupPolicy != null && backupPolicy.BackupEnabledSet.Count > 0)
            {
                HashSet <string> backupEnables = new HashSet <string>(backupPolicy.BackupEnabledSet);
                foreach (var backupEnable in backupEnables)
                {
                    if (!await this.IsFabricEntityExisting(backupEnable, timeout, cancellationToken))
                    {
                        using (ITransaction transaction = this.StatefulService.StateManager.CreateTransaction())
                        {
                            var currentBackupPolicy =
                                await
                                this.BackupPolicyStore.GetValueWithUpdateLockModeAsync(this.backupPolicyName, timeout,
                                                                                       cancellationToken,
                                                                                       transaction);

                            var updatedBackupPolicy = currentBackupPolicy.ToBuilder()
                                                      .RemoveFromBackupEnabledSet(backupEnable)
                                                      .Build();

                            await
                            this.BackupPolicyStore.RemoveProtectionEntity(updatedBackupPolicy, backupEnable,
                                                                          timeout,
                                                                          cancellationToken, transaction);

                            await backupMappingStore.DeleteValueAsync(backupEnable, timeout, cancellationToken);

                            await transaction.CommitAsync();
                        }
                    }
                }
            }

            RetentionManager retentionManager = await RetentionManager.CreateOrGetRetentionManager(this.StatefulService);

            using (var transaction = this.StatefulService.StateManager.CreateTransaction())
            {
                foreach (string suspension in allSuspensions)
                {
                    string applicationNameUri = null;
                    string serviceNameUri     = null;
                    string partitionId        = null;
                    FabricBackupResourceType fabricBackupResourceType =
                        UtilityHelper.GetApplicationAndServicePartitionUri(suspension, out applicationNameUri, out serviceNameUri, out partitionId);

                    BackupMapping backupMapping = null;
                    switch (fabricBackupResourceType)
                    {
                    case FabricBackupResourceType.PartitionUri:
                        backupMapping =
                            await backupMappingStore.GetValueAsync(UtilityHelper.GetBackupMappingKey(serviceNameUri, partitionId),
                                                                   timeout, cancellationToken, transaction) ??
                            await backupMappingStore.GetValueAsync(serviceNameUri, timeout, cancellationToken, transaction) ??
                            await backupMappingStore.GetValueAsync(applicationNameUri, timeout, cancellationToken, transaction);

                        break;

                    case FabricBackupResourceType.ServiceUri:
                        backupMapping =
                            await backupMappingStore.GetValueAsync(serviceNameUri, timeout, cancellationToken, transaction) ??
                            await backupMappingStore.GetValueAsync(applicationNameUri, timeout, cancellationToken, transaction);

                        break;

                    case FabricBackupResourceType.ApplicationUri:
                        backupMapping =
                            await backupMappingStore.GetValueAsync(applicationNameUri, timeout, cancellationToken, transaction);

                        break;
                    }

                    if (backupMapping != null && this.backupPolicyName.Equals(backupMapping.BackupPolicyName))
                    {
                        await suspendStore.DeleteValueAsync(suspension, timeout, cancellationToken, transaction);
                    }
                }

                await this.BackupPolicyStore.DeleteValueAsync(this.backupPolicyName, timeout, cancellationToken, transaction);

                await retentionManager.DeletePolicyAsync(this.backupPolicyName, timeout, cancellationToken, transaction);

                await transaction.CommitAsync();
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
Пример #4
0
        internal override async Task <HttpResponseMessage> RunAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            string applicationNameUri;
            string serviceNameUri;
            string partitionId;
            var    fabricUri = await UtilityHelper.GetFabricUriFromRequstHeader(this.fabricRequestHeader, timeout, cancellationToken);

            WorkItem workItem = null;
            FabricBackupResourceType fabricBackupResourceType =
                UtilityHelper.GetApplicationAndServicePartitionUri(fabricUri, out applicationNameUri, out serviceNameUri,
                                                                   out partitionId);

            bool isApplicationOrServiceExisting;

            switch (fabricBackupResourceType)
            {
            case FabricBackupResourceType.ApplicationUri:
                isApplicationOrServiceExisting =
                    await this.IsApplicationOrServiceExisting(fabricBackupResourceType, applicationNameUri, null, timeout, cancellationToken);

                if (isApplicationOrServiceExisting)
                {
                    workItem = new ResolveToPartitionWorkItem(FabricBackupResourceType.ApplicationUri,
                                                              applicationNameUri,
                                                              new WorkItemInfo
                    {
                        WorkItemType = WorkItemPropogationType.Disable,
                    });
                }

                break;

            case FabricBackupResourceType.ServiceUri:
                isApplicationOrServiceExisting =
                    await this.IsApplicationOrServiceExisting(fabricBackupResourceType, serviceNameUri, null, timeout, cancellationToken);

                if (isApplicationOrServiceExisting)
                {
                    workItem = new ResolveToPartitionWorkItem(FabricBackupResourceType.ServiceUri, serviceNameUri,
                                                              new WorkItemInfo
                    {
                        WorkItemType = WorkItemPropogationType.Disable,
                    });
                }

                break;

            case FabricBackupResourceType.PartitionUri:
                isApplicationOrServiceExisting =
                    await this.IsApplicationOrServiceExisting(fabricBackupResourceType, serviceNameUri, partitionId, timeout, cancellationToken);

                if (isApplicationOrServiceExisting)
                {
                    serviceNameUri =
                        await
                        FabricClientHelper.GetFabricServiceUriFromPartitionId(partitionId, timeout,
                                                                              cancellationToken);

                    workItem = new SendToServiceNodeWorkItem(serviceNameUri, partitionId,
                                                             new WorkItemInfo
                    {
                        WorkItemType = WorkItemPropogationType.Disable,
                    });
                }

                break;

            case FabricBackupResourceType.Error:
                throw new ArgumentException(StringResources.InvalidArguments);
            }

            if (workItem == null)
            {
                using (var transaction = this.StatefulService.StateManager.CreateTransaction())
                {
                    var backupMapping = await this.BackupMappingStore.GetValueWithUpdateLockModeAsync(fabricUri, timeout, cancellationToken, transaction);

                    if (backupMapping == null)
                    {
                        throw new FabricPeriodicBackupNotEnabledException();
                    }

                    var assignedBackupPolicy =
                        await this.BackupPolicyStore.GetValueWithUpdateLockModeAsync(backupMapping.BackupPolicyName, timeout,
                                                                                     cancellationToken, transaction);

                    await this.BackupMappingStore.DeleteValueAsync(fabricUri, timeout, cancellationToken, transaction);

                    await this.BackupPolicyStore.RemoveProtectionEntity(assignedBackupPolicy, fabricUri, timeout, cancellationToken, transaction);

                    await transaction.CommitAsync();
                }
            }
            else
            {
                RetentionManager retentionManager = await RetentionManager.CreateOrGetRetentionManager(this.StatefulService);

                using (var transaction = this.StatefulService.StateManager.CreateTransaction())
                {
                    var backupMapping = await this.BackupMappingStore.GetValueWithUpdateLockModeAsync(fabricUri, timeout, cancellationToken, transaction);

                    if (backupMapping == null)
                    {
                        throw new FabricPeriodicBackupNotEnabledException();
                    }

                    var assignedBackupPolicy = await this.BackupPolicyStore.GetValueWithUpdateLockModeAsync(backupMapping.BackupPolicyName, timeout,
                                                                                                            cancellationToken, transaction);

                    await this.BackupMappingStore.DeleteValueAsync(fabricUri, timeout, cancellationToken, transaction);

                    await this.BackupPolicyStore.RemoveProtectionEntity(assignedBackupPolicy, fabricUri, timeout, cancellationToken, transaction);

                    await this.WorkItemQueue.AddWorkItem(workItem, timeout, cancellationToken, transaction);

                    if (cleanBackups)
                    {
                        await retentionManager.DisablePolicyAsync(fabricUri, backupMapping, timeout, cancellationToken, transaction);
                    }
                    await transaction.CommitAsync();
                }
            }
            return(new HttpResponseMessage(HttpStatusCode.Accepted));
        }