Exemple #1
0
        internal void Initialize()
        {
            this.tempFileName = Utility.GetTempFileName();
            FileStream tmpFileStream = FabricFile.Open(this.tempFileName, FileMode.Create, FileAccess.Write);

#if !DotNetCoreClrLinux
            Helpers.SetIoPriorityHint(tmpFileStream.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow);
#endif
            this.fileStream      = new StreamWriter(tmpFileStream);
            this.writeStatistics = new Statistics();
        }
        internal bool Update(Dictionary <string, ServicePackageTableRecord> servicePackageTable, EtwEventTimestamp latestDataTimestamp)
        {
            // Create a new temp file
            string tempFilePath = Utility.GetTempFileName();

            string backupFilePath = string.Empty;

            try
            {
                // Open the temp file
                StreamWriter writer = null;
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FileStream file = FabricFile.Open(tempFilePath, FileMode.Create, FileAccess.Write);
#if !DotNetCoreClrLinux
                        Helpers.SetIoPriorityHint(file.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow);
#endif
                        writer = new StreamWriter(file);
                    });
                }
                catch (Exception e)
                {
                    Utility.TraceSource.WriteExceptionAsError(
                        TraceType,
                        e,
                        "Failed to open temp file {0}.",
                        tempFilePath);
                    return(false);
                }

                try
                {
                    // Write the version information to the backup file
                    try
                    {
                        Utility.PerformIOWithRetries(
                            () =>
                        {
                            writer.WriteLine(BackupFileVersionString);
                        });
                    }
                    catch (Exception e)
                    {
                        Utility.TraceSource.WriteExceptionAsError(
                            TraceType,
                            e,
                            "Failed to write version information to temp file {0}.",
                            tempFilePath);
                        return(false);
                    }

                    // Write the table records to the backup file
                    foreach (string tableKey in servicePackageTable.Keys)
                    {
                        string tableRecord = string.Concat(
                            servicePackageTable[tableKey].NodeName,
                            ", ",
                            servicePackageTable[tableKey].ApplicationInstanceId,
                            ", ",
                            servicePackageTable[tableKey].ApplicationRolloutVersion,
                            ", ",
                            servicePackageTable[tableKey].ServicePackageName,
                            ", ",
                            servicePackageTable[tableKey].ServiceRolloutVersion,
                            ", ",
                            servicePackageTable[tableKey].RunLayoutRoot);

                        try
                        {
                            Utility.PerformIOWithRetries(
                                () =>
                            {
                                writer.WriteLine(tableRecord);
                            });
                        }
                        catch (Exception e)
                        {
                            Utility.TraceSource.WriteExceptionAsError(
                                TraceType,
                                e,
                                "Failed to write record {0} to temp file {1}",
                                tableRecord,
                                tempFilePath);
                            return(false);
                        }
                    }
                }
                finally
                {
                    writer.Dispose();
                }

                // Compute the name of the backup file
                long   timstampBinary = latestDataTimestamp.Timestamp.ToBinary();
                string fileName       = string.Concat(
                    BackupFilePrefix,
                    timstampBinary.ToString("D20", CultureInfo.InvariantCulture),
                    "_",
                    latestDataTimestamp.Differentiator.ToString("D10", CultureInfo.InvariantCulture),
                    ".",
                    BackupFileExt);
                backupFilePath = Path.Combine(this.backupDirectory, fileName);

                // Copy the temp file as the new backup file
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FabricFile.Copy(tempFilePath, backupFilePath, true);
                    });
                }
                catch (Exception e)
                {
                    Utility.TraceSource.WriteExceptionAsError(
                        TraceType,
                        e,
                        "Failed to copy file {0} to {1}",
                        tempFilePath,
                        backupFilePath);
                    return(false);
                }

                Utility.TraceSource.WriteInfo(
                    TraceType,
                    "Backup file {0} created. The backup file is valid up to timestamp {1} ({2}, {3}).",
                    backupFilePath,
                    latestDataTimestamp.Timestamp,
                    latestDataTimestamp.Timestamp.Ticks,
                    latestDataTimestamp.Differentiator);
            }
            finally
            {
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FabricFile.Delete(tempFilePath);
                    });
                }
                catch (Exception e)
                {
                    Utility.TraceSource.WriteExceptionAsError(
                        TraceType,
                        e,
                        "Failed to delete temp file {0}",
                        tempFilePath);
                }
            }

            // Update the latest backup time
            this.LatestBackupTime = latestDataTimestamp;

            // Delete older backup files
            string backupFilePattern = string.Concat(
                BackupFilePrefix,
                "*.",
                BackupFileExt);
            string[] backupFiles = FabricDirectory.GetFiles(
                this.backupDirectory,
                backupFilePattern);
            foreach (string fileToDelete in backupFiles)
            {
                if (fileToDelete.Equals(
                        backupFilePath,
                        StringComparison.OrdinalIgnoreCase))
                {
                    // Don't delete the current backup file
                    continue;
                }

                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        FabricFile.Delete(fileToDelete);
                    });
                }
                catch (Exception e)
                {
                    // Deletion is on a best-effort basis. Log an error and
                    // continue.
                    Utility.TraceSource.WriteExceptionAsError(
                        TraceType,
                        e,
                        "Failed to delete old backup file {0}",
                        fileToDelete);
                }
            }

            return(true);
        }