/// <summary>
        /// Asynchronously starts the creation of a backup of the state of this replica and stores that into the central store.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="backupOption"></param>
        /// <returns></returns>
        public static async Task BeginCreateBackup(this IBackupRestoreServiceInternal service, BackupOption backupOption)
        {
            var backupDescription = new BackupDescription(backupOption, service.PostBackupCallbackAsync);
            await service.BackupAsync(backupDescription);

            service.LogCallback?.Invoke($"BackupRestoreService - BeginCreateBackup for partition {service.Context.PartitionId}.");
        }
        private async Task PeriodicTakeBackupAsync(CancellationToken cancellationToken)
        {
            long backupsTaken = 0;

            this.SetupBackupManager();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();
                if (this.backupStorageType == BackupManagerType.None)
                {
                    break;
                }
                else
                {
                    await Task.Delay(TimeSpan.FromSeconds(this.backupManager.backupFrequencyInSeconds));


                    BackupDescription backupDescription = new BackupDescription(BackupOption.Full, this.BackupCallbackAsync);

                    await this.BackupAsync(backupDescription, TimeSpan.FromHours(1), cancellationToken);

                    backupsTaken++;

                    ServiceEventSource.Current.ServiceMessage(this, "Backup {0} taken", backupsTaken);
                }
            }
        }
 /// <summary>
 /// Performs a backup of all reliable state managed by this <see cref="StatefulServiceBase"/>.
 /// </summary>
 /// <param name="backupDescription">
 /// A <see cref="BackupDescription"/> describing the backup request.
 /// </param>
 /// <returns>Task that represents the asynchronous backup operation.</returns>
 public Task BackupAsync(BackupDescription backupDescription)
 {
     return(this.StateProviderReplica.BackupAsync(
                backupDescription.Option,
                TimeSpan.FromHours(1),
                CancellationToken.None,
                backupDescription.BackupCallback));
 }
 /// <summary>
 /// Performs a backup of all reliable state managed by this <see cref="StatefulServiceBase"/>.
 /// </summary>
 /// <param name="backupDescription">A <see cref="BackupDescription"/> describing the backup request.</param>
 /// <param name="timeout">The timeout for this operation.</param>
 /// <param name="cancellationToken">The cancellation token is used to monitor for cancellation requests.</param>
 /// <returns>Task that represents the asynchronous backup operation.</returns>
 /// <remarks>
 /// Boolean returned by the backupCallback indicate whether the service was able to successfully move the backup folder to an external location.
 /// If false is returned, BackupAsync throws InvalidOperationException with the relevant message indicating backupCallback returned false.
 /// Also, backup will be marked as unsuccessful.
 /// </remarks>
 public Task BackupAsync(
     BackupDescription backupDescription,
     TimeSpan timeout,
     CancellationToken cancellationToken)
 {
     return(this.StateProviderReplica.BackupAsync(
                backupDescription.Option,
                timeout,
                cancellationToken,
                backupDescription.BackupCallback));
 }
        private async Task PeriodicTakeBackupAsync(CancellationToken cancellationToken)
        {
            this.SetupBackupManager();

            cancellationToken.ThrowIfCancellationRequested();

            await Task.Delay(TimeSpan.FromSeconds(this.backupManager.backupFrequencyInSeconds));

            BackupDescription backupDescription = new BackupDescription(BackupOption.Full, this.BackupCallbackAsync);

            await this.BackupAsync(backupDescription, TimeSpan.FromHours(1), cancellationToken);
        }
Example #6
0
        public async Task PeriodicTakeBackupAsync()
        {
            long backupsTaken = 0;

            this.SetupBackupManager();

            await Task.Delay(TimeSpan.FromSeconds(this.backupManager.backupFrequencyInSeconds));

            BackupDescription backupDescription = new BackupDescription(BackupOption.Full, this.BackupCallbackAsync);

            await this.BackupAsync(backupDescription);

            backupsTaken++;
        }
Example #7
0
 /// <summary>
 /// Asynchronously starts the creation of a backup of the state of this replica and stores that into the central store.
 /// This method completes and returns before the backup process is completely done.
 /// </summary>
 /// <param name="service"></param>
 /// <param name="backupOption"></param>
 /// <returns></returns>
 public static async Task BeginCreateBackup(this IBackupRestoreServiceOperations service, BackupOption backupOption)
 {
     try
     {
         var backupDescription = new BackupDescription(backupOption, service.PostBackupCallbackAsync);
         await service.BackupAsync(backupDescription);
     }
     catch (Exception ex)
     {
         string message = $"Failed to create backup information for partition {service.Context.PartitionId}";
         service.LogCallback?.Invoke($"{nameof(BackupRestoreServiceOperations)} - {nameof(BeginCreateBackup)} failed for partition: {service.Context.PartitionId}. Message:{message} - Error: {ex.Message}");
         throw new Exception(message, ex);
     }
     service.LogCallback?.Invoke($"{nameof(BackupRestoreServiceOperations)} - {nameof(BeginCreateBackup)} succeeded for partition {service.Context.PartitionId}.");
 }
        public async Task Backup(IPersistentUploader uploader, EventArgs _)
        {
            await m_init;

            Log.WriteLine($"{nameof(ServiceFabricBackupManager)}: Creating ServiceFabric backup data.");
            var dsc = new BackupDescription(async(bi, ct) =>
            {
                var fname = Path.Combine(TrinityConfig.StorageRoot, Path.GetRandomFileName());
                Log.WriteLine($"{nameof(ServiceFabricBackupManager)}: Compressing ServiceFabric backup data.");
                ZipFile.CreateFromDirectory(bi.Directory, fname);
                using (var f = File.OpenRead(fname))
                {
                    Log.WriteLine($"{nameof(ServiceFabricBackupManager)}: Uploading ServiceFabric backup data.");
                    await uploader.UploadMetadataAsync(MetadataKey, f);
                }
                Log.WriteLine($"{nameof(ServiceFabricBackupManager)}: Backed up ServiceFabric backup data.");
                return(true);
            });
            await m_svc.Backup(dsc);
        }
Example #9
0
        public async Task DoBackup()
        {
            long backupsTaken = 0;

            this.SetupBackupManager();
            if (this.backupStorageType == BackupManagerType.None)
            {
                return;
            }
            else
            {
                BackupDescription backupDescription = new BackupDescription(BackupOption.Full, this.BackupCallbackAsync);

                await this.BackupAsync(backupDescription);

                backupsTaken++;

                ServiceEventSource.Current.ServiceMessage(this, "Backup {0} taken", backupsTaken);
            }
        }
Example #10
0
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            IReliableDictionary <string, long> myDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >
                    (CountDictionaryName);

            bool takeBackup     = false;
            bool takeFullBackup = false;


            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (var tx = this.StateManager.CreateTransaction())
                {
                    var result = await myDictionary.TryGetValueAsync(tx, "Counter");

                    if (result.HasValue)
                    {
                        ServiceEventSource.Current.ServiceMessage(this, "Current Counter Value: {0}", result.Value.ToString());
                    }
                    else
                    {
                        ServiceEventSource.Current.ServiceMessage(this, "Value does not exist and will be added to the reliable dictionary...");
                    }

                    // Setting a flag that will be true when the counter in the reliable dictionary
                    // hits a multiple of 100
                    long newCount = await myDictionary.AddOrUpdateAsync(tx, "Counter", 0,
                                                                        (key, value) => ++ value);

                    takeBackup = newCount > 0 && newCount % backupCount == 0;

                    // If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are
                    // discarded, and nothing is saved to the secondary replicas.
                    await tx.CommitAsync();
                }


                // If the backup flag was set, then take a backup of this service's state
                if (takeBackup)
                {
                    ServiceEventSource.Current.ServiceMessage(this, "Backup initiated...");

                    // NOTE
                    // Here you could have logic to change the type of backup, full or incremental
                    // Indicate that we want a full backup, and to call BackupCallbackAsync when the backup is complete

                    if (!takeFullBackup)
                    {
                        BackupDescription backupDescription = new BackupDescription(BackupOption.Full,
                                                                                    this.BackupCallbackAsync);
                        incrementalCount = 0;
                        await base.BackupAsync(backupDescription);

                        takeFullBackup = true;
                    }
                    else
                    {
                        try
                        {
                            BackupDescription backupDescription = new BackupDescription(BackupOption.Incremental,
                                                                                        this.BackupCallbackAsync);

                            // Call BackupAsync, which is implemented in StatefulServiceBase (not in this code).
                            // Calling it prompts Service Fabric to do the backup you requested.
                            // All reliable objects are collected
                            // The BackupDescription object created above tells it what kind of backup and where to call
                            // this code back with status
                            incrementalCount++;
                            await base.BackupAsync(backupDescription);
                        }
                        catch (System.Exception ee)
                        {
                        }
                    }
                }
                else
                {
                    await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
                }
            }
        }
        private async Task PeriodicTakeBackupAsync(CancellationToken cancellationToken)
        {
            long backupsTaken = 0;
            this.SetupBackupManager();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();
                if (this.backupStorageType == BackupManagerType.None)
                {
                    break;
                }
                else
                {
                    await Task.Delay(TimeSpan.FromSeconds(this.backupManager.backupFrequencyInSeconds));


                    BackupDescription backupDescription = new BackupDescription(BackupOption.Full, this.BackupCallbackAsync);

                    await this.BackupAsync(backupDescription, TimeSpan.FromHours(1), cancellationToken);

                    backupsTaken++;

                    ServiceEventSource.Current.ServiceMessage(this, "Backup {0} taken", backupsTaken);
                }
            }
        }