Exemplo n.º 1
0
        /// <summary>
        /// Opens a <see cref="ValueCheckpointFile"/> from the given file.
        /// The file stream will be disposed when the checkpoint file is disposed.
        /// </summary>
        /// <param name="filename">The file to open that contains an existing checkpoint file.</param>
        /// <param name="traceType">Tracing information.</param>
        public static async Task <ValueCheckpointFile> OpenAsync(string filename, string traceType)
        {
            FabricEvents.Events.ValueCheckpointFileAsyncOpen(traceType, DiskConstants.state_opening, filename);
            Diagnostics.Assert(FabricFile.Exists(filename), traceType, "File name {0} does not exist", filename);

            ValueCheckpointFile checkpointFile = null;

            try
            {
                checkpointFile = new ValueCheckpointFile(filename);
                await checkpointFile.ReadMetadataAsync().ConfigureAwait(false);
            }
            catch (Exception)
            {
                // Ensure the checkpoint file get disposed quickly if we get an exception.
                if (checkpointFile != null)
                {
                    checkpointFile.Dispose();
                }

                throw;
            }

            FabricEvents.Events.ValueCheckpointFileAsyncOpen(traceType, DiskConstants.state_complete, filename);
            return(checkpointFile);
        }
Exemplo n.º 2
0
        public BlockAlignedWriter(
            FileStream valueFileStream, FileStream keyFileStream, ValueCheckpointFile valueCheckpointFile, KeyCheckpointFile keyCheckpointFile,
            InMemoryBinaryWriter valueBuffer, InMemoryBinaryWriter keyBuffer, IStateSerializer <TValue> valueSerializer, IStateSerializer <TKey> keySerializer,
            long timeStamp, string traceType)
        {
            this.valueCheckpointFile = valueCheckpointFile;
            this.keyCheckpointFile   = keyCheckpointFile;
            this.timeStamp           = timeStamp;
            this.valueFileStream     = valueFileStream;
            this.keyFileStream       = keyFileStream;
            this.valueBuffer         = valueBuffer;
            this.keyBuffer           = keyBuffer;
            this.valueSerializer     = valueSerializer;
            this.keySerializer       = keySerializer;

            this.traceType = traceType;

            this.keyBlockAlignedWriter = new KeyBlockAlignedWriter <TKey, TValue>(
                keyFileStream,
                keyCheckpointFile,
                keyBuffer,
                keySerializer,
                timeStamp,
                traceType);
            this.valueBlockAlignedWriter = new ValueBlockAlignedWriter <TKey, TValue>(
                valueFileStream,
                valueCheckpointFile,
                valueBuffer,
                valueSerializer,
                traceType);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Opens a TStore checkpoint from the given file name.
        /// </summary>
        /// <param name="filename">The file to open that contains an existing checkpoint.</param>
        /// <param name="traceType">Tracing information.</param>
        /// <param name="isValueReferenceType"></param>
        public static async Task <CheckpointFile> OpenAsync(string filename, string traceType, bool isValueReferenceType)
        {
            KeyCheckpointFile   keyFile   = null;
            ValueCheckpointFile valueFile = null;

            try
            {
                keyFile = await KeyCheckpointFile.OpenAsync(filename + KeyCheckpointFile.FileExtension, traceType, isValueReferenceType).ConfigureAwait(false);

                valueFile = await ValueCheckpointFile.OpenAsync(filename + ValueCheckpointFile.FileExtension, traceType).ConfigureAwait(false);

                return(new CheckpointFile(filename, keyFile, valueFile, traceType));
            }
            catch (Exception)
            {
                // Ensure the key and value files get disposed quickly if we get an exception.
                // Normally the CheckpointFile would dispose them, but it may not get constructed.
                if (keyFile != null)
                {
                    keyFile.Dispose();
                }
                if (valueFile != null)
                {
                    valueFile.Dispose();
                }

                throw;
            }
        }
Exemplo n.º 4
0
 public CheckpointFile(string filename, KeyCheckpointFile keyCheckpointFile, ValueCheckpointFile valueCheckpointName, string traceType)
 {
     // Reserved for private static CheckpointFile.OpenAsync() or CheckpointFile.CreateAsync().
     this.FileNameBase        = filename;
     this.KeyCheckpointFile   = keyCheckpointFile;
     this.ValueCheckpointFile = valueCheckpointName;
     this.TraceType           = traceType;
 }
        public ValueBlockAlignedWriter(
            FileStream valueFileStream, ValueCheckpointFile valueCheckpointFile, InMemoryBinaryWriter valueBuffer, IStateSerializer <TValue> valueSerializer,
            string traceType)
        {
            this.valueCheckpointFile = valueCheckpointFile;
            this.valueFileStream     = valueFileStream;
            this.valueBuffer         = valueBuffer;
            this.valueSerializer     = valueSerializer;

            this.traceType          = traceType;
            this.blockAlignmentSize = BlockAlignedWriter <TKey, TValue> .DefaultBlockAlignmentSize;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create a new <see cref="CheckpointFile"/> from the given file, serializing the given key-values into the file.
        /// </summary>
        /// <param name="fileId">File identifier.</param>
        /// <param name="filename">File to create and write to.</param>
        /// <param name="sortedItemData">Sorted key-value pairs to include in the table.</param>
        /// <param name="keySerializer"></param>
        /// <param name="valueSerializer"></param>
        /// <param name="timeStamp"></param>
        /// <param name="traceType">Tracing information.</param>
        /// <param name="perfCounters">Store performance counters instance.</param>
        /// <param name="isValueAReferenceType">Indicated if the value type is reference type.</param>
        /// <returns>The new <see cref="CheckpointFile"/>.</returns>
        public static async Task <CheckpointFile> CreateAsync <TKey, TValue>(
            uint fileId,
            string filename,
            IEnumerable <KeyValuePair <TKey, TVersionedItem <TValue> > > sortedItemData,
            IStateSerializer <TKey> keySerializer,
            IStateSerializer <TValue> valueSerializer,
            long timeStamp,
            string traceType,
            FabricPerformanceCounterSetInstance perfCounters,
            bool isValueAReferenceType)
        {
            var keyFileName   = filename + KeyCheckpointFile.FileExtension;
            var valueFileName = filename + ValueCheckpointFile.FileExtension;

            var keyFile   = new KeyCheckpointFile(keyFileName, fileId, isValueAReferenceType);
            var valueFile = new ValueCheckpointFile(valueFileName, fileId, traceType);

            var checkpointFile = new CheckpointFile(filename, keyFile, valueFile, traceType);

            var perfCounterWriter = new TStoreCheckpointRateCounterWriter(perfCounters);

            // Create the key checkpoint file and memory buffer.
            // MCoskun: Creation of checkpoint file's IO priority is not set.
            // Reason: Checkpointing is a life-cycle operation for State Provider that can cause throttling of backup, copy and (extreme cases) write operations.
            using (var keyFileStream = FabricFile.Open(keyFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.Asynchronous))
                using (var keyMemoryBuffer = new InMemoryBinaryWriter(new MemoryStream(capacity: 64 * 1024)))
                {
                    // Create the value checkpoint file and memory buffer.
                    // MCoskun: Creation of checkpoint file's IO priority is not set.
                    // Reason: Checkpointing is a life-cycle operation for State Provider that can cause throttling of backup, copy and (extreme cases) write operations.
                    using (var valueFileStream = FabricFile.Open(valueFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.Asynchronous))
                        using (var valueMemoryBuffer = new InMemoryBinaryWriter(new MemoryStream(capacity: 64 * 1024)))
                        {
                            var blockAlignedWriter = new BlockAlignedWriter <TKey, TValue>(
                                valueFileStream,
                                keyFileStream,
                                valueFile,
                                keyFile,
                                valueMemoryBuffer,
                                keyMemoryBuffer,
                                valueSerializer,
                                keySerializer,
                                timeStamp,
                                traceType);

                            perfCounterWriter.StartMeasurement();

                            foreach (var item in sortedItemData)
                            {
                                await blockAlignedWriter.BlockAlignedWriteItemAsync(item, null, true).ConfigureAwait(false);
                            }

                            // Flush both key and value checkpoints to disk.
                            await blockAlignedWriter.FlushAsync().ConfigureAwait(false);

                            perfCounterWriter.StopMeasurement();
                        }
                }

            long writeBytes       = checkpointFile.GetFileSize();
            long writeBytesPerSec = perfCounterWriter.UpdatePerformanceCounter(writeBytes);

#if !DotNetCoreClr
            FabricEvents.Events.CheckpointFileWriteBytesPerSec(traceType, writeBytesPerSec);
#endif

            return(checkpointFile);
        }