private NativeCommon.IFabricAsyncOperationContext PartitionBackupOperationBeginWrapper( Uri serviceName, Guid partitionId, Guid operationId, BackupNowConfiguration configuration, TimeSpan timeout, NativeCommon.IFabricAsyncOperationCallback callback) { var timeoutMilliseconds = Utility.ToMilliseconds(timeout, "timeout"); var partitionInfo = new BackupPartitionInfo { PartitionId = partitionId, ServiceName = serviceName.ToString(), }; using (var pin = new PinCollection()) { return(this._nativeAgent.BeginPartitionBackupOperation( partitionInfo.ToNative(pin), operationId, configuration.ToNative(pin), timeoutMilliseconds, callback)); } }
NativeCommon.IFabricAsyncOperationContext NativeBackupRestoreService.IFabricBackupRestoreService.BeginGetBackupSchedulePolicy(IntPtr partitionInfo, uint timeoutMilliseconds, NativeCommon.IFabricAsyncOperationCallback callback) { var managedBackupPartitionInfo = BackupPartitionInfo.FromNative(partitionInfo); var managedTimout = TimeSpan.FromMilliseconds(timeoutMilliseconds); return(Utility.WrapNativeAsyncMethodImplementation( cancellationToken => this.Service.GetBackupSchedulingPolicyAsync( managedBackupPartitionInfo, managedTimout, cancellationToken), callback, "BackupRestoreServiceBroker.GetBackupSchedulingPolicyAsync", ThreadErrorMessageSetter)); }
private NativeCommon.IFabricAsyncOperationContext UpdateBackupSchedulePolicyBeginWrapper( Uri serviceName, Guid partitionId, BackupPolicy policy, TimeSpan timeout, NativeCommon.IFabricAsyncOperationCallback callback) { var timeoutMilliseconds = Utility.ToMilliseconds(timeout, "timeout"); var partitionInfo = new BackupPartitionInfo { PartitionId = partitionId, ServiceName = serviceName.ToString(), }; using (var pin = new PinCollection()) { return(this._nativeAgent.BeginUpdateBackupSchedulePolicy( partitionInfo.ToNative(pin), policy == null ? IntPtr.Zero : policy.ToNative(pin), timeoutMilliseconds, callback)); } }
public async Task <BackupPolicy> GetBackupSchedulingPolicyAsync(BackupPartitionInfo backupPartitionInfo, TimeSpan timeout, CancellationToken cancellationToken) { BackupPolicy backupPolicy; BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "{0} GetBackupPolicy invoked", backupPartitionInfo.PartitionId); var backupMappingStore = await BackupMappingStore.CreateOrGetBackupMappingStore(this.statefulService); var backupPolicyStore = await BackupPolicyStore.CreateOrGetBackupPolicyStore(this.statefulService); var suspendStore = await SuspendStore.CreateOrGetSuspendStatusStore(this.statefulService); var serviceNameUri = await UtilityHelper.GetCustomServiceUri(backupPartitionInfo.ServiceName, timeout, cancellationToken); var backupMappingKey = UtilityHelper.GetBackupMappingKey(serviceNameUri, backupPartitionInfo.PartitionId.ToString()); var backupMappingModel = (await backupMappingStore.GetValueAsync(backupMappingKey) ?? await backupMappingStore.GetValueAsync(serviceNameUri)) ?? await backupMappingStore.GetValueAsync(UtilityHelper.GetApplicationNameFromService(serviceNameUri)); bool isPartitionSuspended = await suspendStore.GetValueAsync(backupMappingKey) != null; if (backupMappingModel == null || isPartitionSuspended) { BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "{0} Backup protection not enabled", backupPartitionInfo.PartitionId); throw new FabricPeriodicBackupNotEnabledException(); } var backupPolicyName = backupMappingModel.BackupPolicyName; var backupPolicyModel = await backupPolicyStore.GetValueAsync(backupPolicyName); switch (backupPolicyModel.BackupSchedule.BackupScheduleType) { case BackupScheduleType.FrequencyBased: backupPolicy = new FrequencyBasedBackupPolicy(); var frequencyBasedSchedulePolicy = (System.Fabric.BackupRestore.Common.Model.FrequencyBasedBackupSchedule)backupPolicyModel.BackupSchedule; backupPolicy.PolicyType = BackupPolicyType.FrequencyBased; var freqBackupPolicy = (FrequencyBasedBackupPolicy)backupPolicy; freqBackupPolicy.RunFrequency = (ushort)frequencyBasedSchedulePolicy.Interval; if (frequencyBasedSchedulePolicy.IntervalType == BackupScheduleInterval.Hours) { freqBackupPolicy.RunFrequencyType = BackupPolicyRunFrequency.Hours; } if (frequencyBasedSchedulePolicy.IntervalType == BackupScheduleInterval.Minutes) { freqBackupPolicy.RunFrequencyType = BackupPolicyRunFrequency.Minutes; } break; case BackupScheduleType.TimeBased: backupPolicy = new ScheduleBasedBackupPolicy(); var scheduleBasedSchedulePolicy = (System.Fabric.BackupRestore.Common.Model.TimeBasedBackupSchedule)backupPolicyModel.BackupSchedule; backupPolicy.PolicyType = BackupPolicyType.ScheduleBased; var schdBackupPolicy = (ScheduleBasedBackupPolicy)backupPolicy; if (scheduleBasedSchedulePolicy.ScheduleFrequencyType == BackupScheduleFrequency.Daily) { schdBackupPolicy.RunSchedule = BackupPolicyRunSchedule.Daily; } if (scheduleBasedSchedulePolicy.ScheduleFrequencyType == BackupScheduleFrequency.Weekly) { schdBackupPolicy.RunSchedule = BackupPolicyRunSchedule.Weekly; } schdBackupPolicy.RunDays = new List <DayOfWeek>(scheduleBasedSchedulePolicy.RunDays); schdBackupPolicy.RunTimes = new List <TimeSpan>(scheduleBasedSchedulePolicy.RunTimes); break; default: throw new FabricPeriodicBackupNotEnabledException(); } backupPolicy.MaxIncrementalBackups = Convert.ToByte(backupPolicyModel.MaxIncrementalBackup); backupPolicy.Name = backupPolicyModel.Name; backupPolicy.PolicyId = backupPolicyModel.UniqueId; switch (backupPolicyModel.Storage.BackupStorageType) { case BackupStorageType.FileShare: var fileShareStorage = (System.Fabric.BackupRestore.Common.Model.FileShareBackupStorageInfo)backupPolicyModel.Storage; backupPolicy.StoreInformation = new FileShareBackupStore { AccessType = String.IsNullOrEmpty(fileShareStorage.PrimaryUserName) ? FileShareAccessType.None : FileShareAccessType.DomainUser, FileSharePath = Path.Combine(fileShareStorage.Path, UtilityHelper.GetBaseDirectoryPathForPartition(serviceNameUri, backupPartitionInfo.PartitionId.ToString())), PrimaryUserName = fileShareStorage.PrimaryUserName, PrimaryPassword = fileShareStorage.PrimaryPassword, SecondaryUserName = fileShareStorage.SecondaryUserName, SecondaryPassword = fileShareStorage.SecondaryPassword, IsPasswordEncrypted = fileShareStorage.IsPasswordEncrypted, }; break; case BackupStorageType.AzureBlobStore: var azureStorage = (System.Fabric.BackupRestore.Common.Model.AzureBlobBackupStorageInfo)backupPolicyModel.Storage; backupPolicy.StoreInformation = new AzureBlobBackupStore { ConnectionString = azureStorage.ConnectionString, ContainerName = azureStorage.ContainerName, FolderPath = UtilityHelper.GetBaseDirectoryPathForPartition(serviceNameUri, backupPartitionInfo.PartitionId.ToString()), IsAccountKeyEncrypted = azureStorage.IsConnectionStringEncrypted, }; break; case BackupStorageType.DsmsAzureBlobStore: var dsmsAzureStorage = (System.Fabric.BackupRestore.Common.Model.DsmsAzureBlobBackupStorageInfo)backupPolicyModel.Storage; backupPolicy.StoreInformation = new DsmsAzureBlobBackupStore { StorageCredentialsSourceLocation = dsmsAzureStorage.StorageCredentialsSourceLocation, ContainerName = dsmsAzureStorage.ContainerName, FolderPath = UtilityHelper.GetBaseDirectoryPathForPartition(serviceNameUri, backupPartitionInfo.PartitionId.ToString()), }; break; } return(backupPolicy); }
public async Task <RestorePointDetails> GetRestorePointDetailsAsync(BackupPartitionInfo backupPartitionInfo, TimeSpan timeout, CancellationToken cancellationToken) { BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "GetRestorePointDetailsAsync invoked for partition {0}", backupPartitionInfo.PartitionId); var restoreStore = await RestoreStore.CreateOrGetRestoreStatusStore(this.statefulService); var backupMappingStore = await BackupMappingStore.CreateOrGetBackupMappingStore(this.statefulService); var backupPolicyStore = await BackupPolicyStore.CreateOrGetBackupPolicyStore(this.statefulService); bool userInitiatedOperation; BackupStorage backupStorage; BackupStoreInformation backupStoreInformation = null; List <string> backupLocations; string brsServiceUri = await UtilityHelper.GetCustomServiceUri(backupPartitionInfo.ServiceName, timeout, cancellationToken); var fabricUri = UtilityHelper.GetBackupMappingKey(brsServiceUri, backupPartitionInfo.PartitionId.ToString()); var restoreStatus = await restoreStore.GetValueAsync(fabricUri); if (restoreStatus != null && ( restoreStatus.RestoreStatusState != RestoreState.Failure || restoreStatus.RestoreStatusState != RestoreState.Invalid || restoreStatus.RestoreStatusState != RestoreState.Success || restoreStatus.RestoreStatusState != RestoreState.Timeout)) { backupStorage = restoreStatus.BackupStorage; userInitiatedOperation = true; backupLocations = restoreStatus.BackupLocations.ToList(); } else { var backupMappingKey = UtilityHelper.GetBackupMappingKey(brsServiceUri, backupPartitionInfo.PartitionId.ToString()); var backupMappingModel = (await backupMappingStore.GetValueAsync(backupMappingKey) ?? await backupMappingStore.GetValueAsync(brsServiceUri)) ?? await backupMappingStore.GetValueAsync(UtilityHelper.GetApplicationNameFromService(brsServiceUri)); if (backupMappingModel == null) { throw new FabricPeriodicBackupNotEnabledException(); } var backupPolicy = await backupPolicyStore.GetValueAsync(backupMappingModel.BackupPolicyName); if (backupPolicy != null && backupPolicy.AutoRestore) { backupStorage = backupPolicy.Storage; userInitiatedOperation = false; string applicationName, serviceName; UtilityHelper.GetApplicationAndServiceNameFromServiceUri(brsServiceUri, out applicationName, out serviceName); // Get the latest backup now var backups = await backupStorage.GetBackupEnumerationsTask( applicationName, serviceName, backupPartitionInfo.PartitionId.ToString(), DateTime.MinValue, DateTime.MaxValue, true, cancellationToken); if (backups == null || backups.Count == 0) { throw new ArgumentException(); // TODO: Change this with appropriate exception } var backupDetail = backups.First(); var recoveryPointManager = RecoveryPointManagerFactory.GetRecoveryPointManager(backupStorage); backupLocations = await recoveryPointManager.GetBackupLocationsInBackupChainAsync(backupDetail.BackupLocation, cancellationToken); } else { throw new ArgumentException(); // TODO: Change with the OTher exception } } switch (backupStorage.BackupStorageType) { case BackupStorageType.FileShare: var fileShareStorage = (FileShareStorage)backupStorage; backupStoreInformation = new FileShareBackupStore { AccessType = String.IsNullOrEmpty(fileShareStorage.PrimaryUserName) ? FileShareAccessType.None : FileShareAccessType.DomainUser, FileSharePath = fileShareStorage.Path, PrimaryUserName = fileShareStorage.PrimaryUserName, PrimaryPassword = fileShareStorage.PrimaryPassword, SecondaryUserName = fileShareStorage.SecondaryUserName, SecondaryPassword = fileShareStorage.SecondaryPassword, IsPasswordEncrypted = fileShareStorage.IsPasswordEncrypted, }; break; case BackupStorageType.AzureBlobStore: var azureStorage = (AzureStorage)backupStorage; backupStoreInformation = new AzureBlobBackupStore { ConnectionString = azureStorage.ConnectionString, ContainerName = azureStorage.ContainerName, FolderPath = String.Empty, // TODO: This should be constructed IsAccountKeyEncrypted = azureStorage.IsConnectionStringEncrypted, }; break; case BackupStorageType.DsmsAzureBlobStore: var dsmsAzureStorage = (DsmsAzureStorage)backupStorage; backupStoreInformation = new DsmsAzureBlobBackupStore { StorageCredentialsSourceLocation = dsmsAzureStorage.StorageCredentialsSourceLocation, ContainerName = dsmsAzureStorage.ContainerName, FolderPath = String.Empty, // TODO: This should be constructed }; break; } Guid restoreOperationId = Guid.NewGuid(); if (userInitiatedOperation) { await UtilityHelper.InvokeWithRetry(async() => { using (ITransaction transaction = this.statefulService.StateManager.CreateTransaction()) { var currentRestoreStatus = await restoreStore.GetValueWithUpdateLockModeAsync(fabricUri, timeout, cancellationToken, transaction); restoreOperationId = currentRestoreStatus.RestoreRequestGuid; if (currentRestoreStatus.RestoreStatusState == RestoreState.Accepted) //TODO Check for Guid { var toUpdateRestoreStatus = currentRestoreStatus.ToBuilder().WithState(RestoreState.RestoreInProgress).Build(); await restoreStore.UpdateValueAsync(fabricUri, toUpdateRestoreStatus, timeout, cancellationToken, transaction); await transaction.CommitAsync(); } else { transaction.Abort(); } } }); } var restorePartitionDetails = new RestorePointDetails() { BackupLocations = backupLocations, UserInitiatedOperation = userInitiatedOperation, StoreInformation = backupStoreInformation, OperationId = restoreOperationId }; BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "GetRestorePointDetailsAsync invoked for partition {0} with Return Values {1}", backupPartitionInfo.PartitionId, restorePartitionDetails); return(restorePartitionDetails); }