Example #1
0
        /// <summary>
        /// Create a new <see cref="RecoveryPointMetadataFile"/> and write it to the given file.
        /// </summary>
        /// <returns>Task that represents the asynchronous operation.</returns>
        public static async Task <RecoveryPointMetadataFile> CreateAsync(
            string fileName,
            DateTime backupTime,
            Guid backupId,
            Guid parentBackupId,
            Guid backupChainId,
            Epoch epochOfLastBackupRecord,
            long lsnOfLastBackupRecord,
            string backupLocation,
            string parentBackupLocation,
            ServicePartitionInformation partitionInformation,
            string serviceManifestVersion,
            CancellationToken cancellationToken)
        {
            // Create the file with asynchronous flag and 4096 cache size (C# default).
            using (var filestream = FabricFile.Open(
                       fileName,
                       FileMode.CreateNew,
                       FileAccess.Write,
                       FileShare.Read,
                       4096,
                       FileOptions.Asynchronous))
            {
                BackupRestoreUtility.SetIoPriorityHint(filestream.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintLow);

                var recoveryPointMetadataFile = new RecoveryPointMetadataFile(fileName);
                recoveryPointMetadataFile.properties = new RecoveryPointMetadataFileProperties
                {
                    BackupTime              = backupTime,
                    BackupLocation          = backupLocation,
                    ParentBackupLocation    = parentBackupLocation,
                    BackupId                = backupId,
                    ParentBackupId          = parentBackupId,
                    BackupChainId           = backupChainId,
                    EpochOfLastBackupRecord = epochOfLastBackupRecord,
                    LsnOfLastBackupRecord   = lsnOfLastBackupRecord,
                    PartitionInformation    = partitionInformation,
                    ServiceManifestVersion  = serviceManifestVersion,
                };

                // Write the properties.
                var propertiesHandle =
                    await FileBlock.WriteBlockAsync(filestream, writer => recoveryPointMetadataFile.properties.Write(writer)).ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                // Write the footer.
                recoveryPointMetadataFile.footer = new FileFooter(propertiesHandle, Version);
                await FileBlock.WriteBlockAsync(filestream, writer => recoveryPointMetadataFile.footer.Write(writer)).ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                // Flush to underlying stream.
                await filestream.FlushAsync(cancellationToken).ConfigureAwait(false);

                return(recoveryPointMetadataFile);
            }
        }
Example #2
0
        public static async Task WriteAsync(MetadataTable metadataTable, Stream stream)
        {
            // Write all metadata to disk.
            var properties = await WriteDiskMetadataAsync(metadataTable.Table, stream).ConfigureAwait(false);

            properties.CheckpointLSN = metadataTable.CheckpointLSN;

            // Write the Properties.
            var propertiesHandle = await FileBlock.WriteBlockAsync(stream, (sectionWriter) => properties.Write(sectionWriter)).ConfigureAwait(false);

            // Write the Footer.
            var footer = new FileFooter(propertiesHandle, FileVersion);
            await FileBlock.WriteBlockAsync(stream, (sectionWriter) => footer.Write(sectionWriter)).ConfigureAwait(false);

            // Finally, flush to disk.
            await stream.FlushAsync().ConfigureAwait(false);
        }
        /// <summary>
        /// A Flush indicates that all keys have been written to the checkpoint file (via AddItemAsync), so
        /// the checkpoint file can finish flushing any remaining in-memory buffered data, write any extra
        /// metadata (e.g. properties and footer), and flush to disk.
        /// </summary>
        /// <param name="fileStream"></param>
        /// <param name="memoryBuffer"></param>
        /// <returns></returns>
        public async Task FlushAsync(Stream fileStream, InMemoryBinaryWriter memoryBuffer)
        {
            // If there's any buffered keys in memory, flush them to disk first.
            if (memoryBuffer.BaseStream.Position > 0)
            {
                await this.FlushMemoryBufferAsync(fileStream, memoryBuffer).ConfigureAwait(false);
            }

            // Update the Properties.
            this.Properties.KeysHandle = new BlockHandle(0, fileStream.Position);

            // Write the Properties.
            var propertiesHandle = await FileBlock.WriteBlockAsync(fileStream, (sectionWriter) => this.Properties.Write(sectionWriter)).ConfigureAwait(false);

            // Write the Footer.
            this.Footer = new FileFooter(propertiesHandle, FileVersion);
            await FileBlock.WriteBlockAsync(fileStream, (sectionWriter) => this.Footer.Write(sectionWriter)).ConfigureAwait(false);

            // Finally, flush to disk.
            await fileStream.FlushAsync().ConfigureAwait(false);
        }
Example #4
0
        /// <summary>
        /// A Flush indicates that all values have been written to the checkpoint file (via AddItemAsync), so
        /// the checkpoint file can finish flushing any remaining in-memory buffered data, write any extra
        /// metadata (e.g. properties and footer), and flush to disk.
        /// </summary>
        /// <param name="fileStream"></param>
        /// <param name="memoryBuffer"></param>
        /// <returns></returns>
        public async Task FlushAsync(Stream fileStream, InMemoryBinaryWriter memoryBuffer)
        {
            // If there's any buffered values in memory, flush them to disk first.
            if (memoryBuffer.BaseStream.Position > 0)
            {
                await this.WriteToFileBufferAsync(fileStream, memoryBuffer).ConfigureAwait(false);
            }

            Diagnostics.Assert(fileStream.Position % DefaultBlockAlignmentSize == 0, this.traceType, "Value checkpoint file is incorrectly block aligned at the end.");

            // Update the Properties.
            this.Properties.ValuesHandle = new BlockHandle(0, fileStream.Position);

            // Write the Properties.
            var propertiesHandle = await FileBlock.WriteBlockAsync(fileStream, (sectionWriter) => this.Properties.Write(sectionWriter)).ConfigureAwait(false);

            // Write the Footer.
            this.Footer = new FileFooter(propertiesHandle, FileVersion);
            await FileBlock.WriteBlockAsync(fileStream, (sectionWriter) => this.Footer.Write(sectionWriter)).ConfigureAwait(false);

            // Finally, flush to disk.
            await fileStream.FlushAsync().ConfigureAwait(false);
        }