Beispiel #1
0
        /// <summary>
        /// Restores the checkpoint for a given checkpoint id.
        /// </summary>
        public Task <BoolResult> RestoreCheckpointAsync(OperationContext context, string checkpointId)
        {
            context = context.CreateNested();
            return(context.PerformOperationAsync(
                       _tracer,
                       async() =>
            {
                bool successfullyUpdatedIncrementalState = false;
                try
                {
                    bool isIncrementalCheckpoint = false;
                    var checkpointFileExtension = ".zip";
                    if (checkpointId.EndsWith(IncrementalCheckpointIdSuffix, StringComparison.OrdinalIgnoreCase))
                    {
                        isIncrementalCheckpoint = true;
                        checkpointFileExtension = ".txt";
                        // Remove the suffix to get the real checkpoint id used with central storage
                        checkpointId = checkpointId.Substring(0, checkpointId.Length - IncrementalCheckpointIdSuffix.Length);
                    }

                    var checkpointFile = _checkpointStagingDirectory / $"chkpt{checkpointFileExtension}";
                    var extractedCheckpointDirectory = _checkpointStagingDirectory / "chkpt";

                    FileUtilities.DeleteDirectoryContents(_checkpointStagingDirectory.ToString());
                    FileUtilities.DeleteDirectoryContents(extractedCheckpointDirectory.ToString());

                    // Creating a working temporary folder
                    using (new DisposableDirectory(_fileSystem, _checkpointStagingDirectory))
                    {
                        // Getting the checkpoint from the central store
                        await _storage.TryGetFileAsync(context, checkpointId, checkpointFile).ThrowIfFailure();

                        if (isIncrementalCheckpoint)
                        {
                            successfullyUpdatedIncrementalState = await RestoreCheckpointIncrementalAsync(context, checkpointFile, extractedCheckpointDirectory);
                        }
                        else
                        {
                            RestoreFullCheckpointAsync(checkpointFile, extractedCheckpointDirectory);
                        }

                        // Restoring the checkpoint
                        return _database.RestoreCheckpoint(context, extractedCheckpointDirectory);
                    }
                }
                finally
                {
                    ClearIncrementalCheckpointStateIfNeeded(successfullyUpdatedIncrementalState);
                }
            }));
        }
        private async Task <ContentHashWithSize> TryGetFromFallbackAndPutAsync(OperationContext context, AbsolutePath targetFilePath, string fallbackStorageId, bool isImmutable)
        {
            // In the success case the content will be put at targetFilePath
            await _fallbackStorage.TryGetFileAsync(context, fallbackStorageId, targetFilePath, isImmutable).ThrowIfFailure();

            var placementFileRealizationMode = isImmutable ? FileRealizationMode.Any : FileRealizationMode.Copy;
            var putResult = await PrivateCas.PutFileAsync(context, targetFilePath, placementFileRealizationMode, HashType, pinRequest : null).ThrowIfFailure();

            return(new ContentHashWithSize(putResult.ContentHash, putResult.ContentSize));
        }
Beispiel #3
0
        /// <summary>
        /// Restores the checkpoint for a given checkpoint id.
        /// </summary>
        public Task <BoolResult> RestoreCheckpointAsync(OperationContext context, CheckpointState checkpointState)
        {
            context = context.CreateNested();
            var checkpointId = checkpointState.CheckpointId;

            return(context.PerformOperationAsync(
                       _tracer,
                       async() =>
            {
                bool successfullyUpdatedIncrementalState = false;
                try
                {
                    bool isIncrementalCheckpoint = false;
                    var checkpointFileExtension = ".zip";
                    if (checkpointId.EndsWith(IncrementalCheckpointIdSuffix, StringComparison.OrdinalIgnoreCase))
                    {
                        isIncrementalCheckpoint = true;
                        checkpointFileExtension = ".txt";
                        // Remove the suffix to get the real checkpoint id used with central storage
                        checkpointId = checkpointId.Substring(0, checkpointId.Length - IncrementalCheckpointIdSuffix.Length);
                    }

                    var checkpointFile = _checkpointStagingDirectory / $"chkpt{checkpointFileExtension}";
                    var extractedCheckpointDirectory = _checkpointStagingDirectory / "chkpt";

                    FileUtilities.DeleteDirectoryContents(_checkpointStagingDirectory.ToString());
                    FileUtilities.DeleteDirectoryContents(extractedCheckpointDirectory.ToString());

                    // Creating a working temporary folder
                    using (new DisposableDirectory(_fileSystem, _checkpointStagingDirectory))
                    {
                        // Getting the checkpoint from the central store
                        await _storage.TryGetFileAsync(context, checkpointId, checkpointFile).ThrowIfFailure();

                        if (isIncrementalCheckpoint)
                        {
                            var incrementalRestoreResult = await RestoreCheckpointIncrementalAsync(context, checkpointFile, extractedCheckpointDirectory);
                            incrementalRestoreResult.ThrowIfFailure();
                        }
                        else
                        {
                            RestoreFullCheckpoint(checkpointFile, extractedCheckpointDirectory);
                        }

                        // Restoring the checkpoint
                        _database.RestoreCheckpoint(context, extractedCheckpointDirectory).ThrowIfFailure();

                        // Save latest checkpoint info to file in case we get restarded and want to know about the previous checkpoint.
                        WriteLatestCheckpoint(context, checkpointState);

                        successfullyUpdatedIncrementalState = true;
                        return BoolResult.Success;
                    }
                }
                finally
                {
                    ClearIncrementalCheckpointStateIfNeeded(context, successfullyUpdatedIncrementalState);
                }
            },
                       extraStartMessage: $"CheckpointId=[{checkpointId}]",
                       extraEndMessage: _ => $"CheckpointId=[{checkpointId}]"));
        }