internal async override Task InitializeAsync() { this.BackupMappingStore = await BackupMappingStore.CreateOrGetBackupMappingStore(this.StatefulService).ConfigureAwait(false); this.BackupPolicyStore = await BackupPolicyStore.CreateOrGetBackupPolicyStore(this.StatefulService).ConfigureAwait(false); this.WorkItemQueue = await WorkItemQueue.CreateOrGetWorkItemQueue(this.StatefulService).ConfigureAwait(false); }
private async Task UpdateSecretsAsync(string thumbprintFromManifest, string x509StoreNameFromManifest, CancellationToken cancellationToken) { BackupRestoreTrace.TraceSource.WriteInfo(TraceType, String.Format("Updating secrets using the new thumbprint {0}", thumbprintFromManifest)); using (var transaction = this.StateManager.CreateTransaction()) { try { var policyStore = await BackupPolicyStore.CreateOrGetBackupPolicyStore(this); var workItemQueue = await WorkItemQueue.CreateOrGetWorkItemQueue(this); var policies = await policyStore.GetValuesAsync(Common.Constants.StoreTimeOut, cancellationToken, transaction); foreach (var policy in policies) { cancellationToken.ThrowIfCancellationRequested(); var updatedStorage = this.UpdateSecretInStorage(policy.Storage, thumbprintFromManifest, x509StoreNameFromManifest, cancellationToken); var newPolicy = policy.ToBuilder() .WithBackupStorage(updatedStorage) .UpdateUniqueId() .Build(); if (!await policyStore.UpdateValueAsync(policy.Name, policy, newPolicy, Common.Constants.StoreTimeOut, cancellationToken, transaction)) { throw new InvalidOperationException(String.Format("Failed to update secret for policy with name {0}", newPolicy.Name)); } if (newPolicy.BackupEnabledSet.Count > 0) { await workItemQueue.AddWorkItem( new UpdateEnablementWorkItem(newPolicy.BackupEnabledSet.ToList(), new WorkItemInfo { WorkItemType = WorkItemPropogationType.UpdateBackupPolicy, BackupPolicyUpdateGuid = newPolicy.UniqueId }), Common.Constants.StoreTimeOut, cancellationToken, transaction); } } // Update the config store var configStore = await ConfigStore.CreateOrGetConfigStore(this); await configStore.UpdateValueAsync(Common.Constants.SecretUpdateInProgress, String.Empty, Common.Constants.StoreTimeOut, cancellationToken, transaction); await configStore.UpdateValueAsync(Common.Constants.EncryptionCertThumbprintKey, thumbprintFromManifest, Common.Constants.StoreTimeOut, cancellationToken, transaction); await transaction.CommitAsync(); BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "All secrets updated with manifest {0}", thumbprintFromManifest); FabricEvents.Events.BRSSecretsUpdated(TraceType, thumbprintFromManifest); } catch (Exception ex) { BackupRestoreTrace.TraceSource.WriteExceptionAsWarning(TraceType, ex, "Exception encountered while updating secrets"); throw; } } }
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)); }
public async Task RestoreOperationResultAsync(RestoreOperationResult operationResult, TimeSpan timeout, CancellationToken cancellationToken) { BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "RestoreOperationResultAsync invoked for partition {0}, ErrorCode {1}, message {2}", operationResult.PartitionId, operationResult.ErrorCode, operationResult.Message); var restoreStore = await RestoreStore.CreateOrGetRestoreStatusStore(this.statefulService); var workItemQueue = await WorkItemQueue.CreateOrGetWorkItemQueue(this.statefulService); string brsServiceUri = await UtilityHelper.GetCustomServiceUri(operationResult.ServiceName, timeout, cancellationToken); var fabricUri = UtilityHelper.GetBackupMappingKey(brsServiceUri, operationResult.PartitionId.ToString()); WorkItem pushPolicyAfterRestore = new SendToServiceNodeWorkItem(brsServiceUri, operationResult.PartitionId.ToString(), new WorkItemInfo() { WorkItemType = WorkItemPropogationType.UpdatePolicyAfterRestore, }); await UtilityHelper.InvokeWithRetry(async() => { BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "Trying to Update the Restore request for partition {0}", operationResult.PartitionId); using (ITransaction transaction = this.statefulService.StateManager.CreateTransaction()) { var restoreStatus = await restoreStore.GetValueWithUpdateLockModeAsync(fabricUri, timeout, cancellationToken, transaction); if ((restoreStatus != null) && (restoreStatus.RestoreStatusState == RestoreState.RestoreInProgress || restoreStatus.RestoreStatusState == RestoreState.Accepted) && (restoreStatus.RestoreRequestGuid.Equals(operationResult.OperationId))) //TODO: Add check for each request by Guid { var updatedRestoreStatus = restoreStatus.ToBuilder() .WithState(operationResult.ErrorCode != 0 ? RestoreState.Failure : RestoreState.Success) .WithErrorCode(operationResult.ErrorCode) .WithMessage(operationResult.Message) .WithTimeStampUtc(operationResult.TimeStampUtc) .Build(); await restoreStore.UpdateValueAsync(fabricUri, updatedRestoreStatus, timeout, cancellationToken, transaction); await workItemQueue.AddWorkItem(pushPolicyAfterRestore, timeout, cancellationToken, transaction); await transaction.CommitAsync(); BackupRestoreTrace.TraceSource.WriteInfo(TraceType, "Updated the Restore request for partition {0}", operationResult.PartitionId); } else { transaction.Abort(); } } }); }