コード例 #1
0
        internal static void CopyFile(string source, string destination, bool overwrite, bool compressDestination)
        {
            string tempArchivePath = string.Empty;

            try
            {
                if (compressDestination)
                {
                    string archiveEntryName = Path.GetFileNameWithoutExtension(destination);
                    FileCompressor.Compress(source, archiveEntryName, out tempArchivePath);

                    source = tempArchivePath;
                }

                CopyFileParameters copyParam = new CopyFileParameters()
                {
                    Source      = source,
                    Destination = destination,
                    Overwrite   = true
                };

                Utility.PerformIOWithRetries(
                    CopyFileWorker,
                    (object)copyParam);
            }
            finally
            {
                if (false == string.IsNullOrEmpty(tempArchivePath))
                {
                    Utility.PerformIOWithRetries(
                        () => { FabricFile.Delete(tempArchivePath); });
                }
            }
        }
コード例 #2
0
 internal void Close()
 {
     try
     {
         Utility.PerformIOWithRetries(
             () =>
         {
             try
             {
                 this.fileStream.Dispose();
             }
             catch (System.Text.EncoderFallbackException)
             {
                 // This can happen if the manifest file does not match the binary.
                 // Write an error message and move on.
                 this.traceSource.WriteError(
                     this.logSourceId,
                     "EncoderFallbackException occurred while closing file {0}",
                     this.TempFileName);
             }
         });
     }
     catch (Exception e)
     {
         this.traceSource.WriteExceptionAsError(
             this.logSourceId,
             e,
             "Failed to close handle to temporary file {0}.",
             this.TempFileName);
     }
 }
コード例 #3
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();
        }
コード例 #4
0
 internal void Delete()
 {
     try
     {
         Utility.PerformIOWithRetries(
             () =>
         {
             FabricFile.Delete(this.TempFileName);
         });
     }
     catch (Exception e)
     {
         this.traceSource.WriteExceptionAsError(
             this.logSourceId,
             e,
             "Failed to delete temporary file {0}.",
             this.TempFileName);
     }
 }
コード例 #5
0
        internal static void GetFilesMatchingPattern(string directory, string pattern, out string[] files)
        {
            PatternMatchingParameters patternMatchingParam = new PatternMatchingParameters
            {
                Directory = directory,
                Pattern   = pattern
            };

            Utility.PerformIOWithRetries(
                ctx =>
            {
                PatternMatchingParameters param = ctx;
                param.Files = FabricDirectory.GetFiles(
                    param.Directory,
                    param.Pattern);
            },
                patternMatchingParam);

            files = patternMatchingParam.Files;
        }
コード例 #6
0
        public bool GetLastEndTime(string fileName, out DateTime lastEndTime)
        {
            lastEndTime = DateTime.MinValue;

            var fileParam = new LastEndTimeFileParameters(this.traceSource, this.logSourceId)
            {
                Fs       = null,
                FileName = fileName,
                Mode     = FileMode.Open,
                Access   = FileAccess.Read
            };

            // Try to open the file that contains the last ETL read timestamp
            try
            {
                Utility.PerformIOWithRetries(
                    fileParam.OpenLastEndTimeFile,
                    EtlInMemoryProducerConstants.MethodExecutionInitialRetryIntervalMs,
                    EtlInMemoryProducerConstants.MethodExecutionMaxRetryCount,
                    EtlInMemoryProducerConstants.MethodExecutionMaxRetryIntervalMs);
            }
            catch (Exception e)
            {
                this.traceSource.WriteExceptionAsError(
                    this.logSourceId,
                    e,
                    "Failed to open file {0} for read.",
                    fileName);
                return(false);
            }

            if (null == fileParam.Fs)
            {
                // File wasn't found. So we'll assume that this is the first
                // time that we are attempting to read ETL files.
                return(true);
            }

            try
            {
                // Read the timestamp from the file
                fileParam.Reader = new StreamReader(fileParam.Fs);
                try
                {
                    try
                    {
                        Utility.PerformIOWithRetries(
                            fileParam.ReadLastEndTime,
                            EtlInMemoryProducerConstants.MethodExecutionInitialRetryIntervalMs,
                            EtlInMemoryProducerConstants.MethodExecutionMaxRetryCount,
                            EtlInMemoryProducerConstants.MethodExecutionMaxRetryIntervalMs);
                    }
                    catch (Exception e)
                    {
                        // Abort this ETL processing pass. We'll try again in the next pass.
                        this.traceSource.WriteExceptionAsError(
                            this.logSourceId,
                            e,
                            "Failed to read last ETL read end time from file {0}.",
                            fileName);
                        return(false);
                    }
                }
                finally
                {
                    fileParam.Reader.Dispose();
                }
            }
            finally
            {
                fileParam.Fs.Dispose();
            }

            // Convert the timestamp from binary to DateTime
            lastEndTime = DateTime.FromBinary(fileParam.LastEndTimeLong);
            return(true);
        }
コード例 #7
0
        public bool SetLastEndTime(string fileName, DateTime stopTime)
        {
            var fileParam = new LastEndTimeFileParameters(this.traceSource, this.logSourceId)
            {
                Fs       = null,
                FileName = fileName,
                Mode     = FileMode.OpenOrCreate,
                Access   = FileAccess.Write,

                // Convert the timestamp from DateTime to binary
                LastEndTimeLong = stopTime.ToBinary(),
                LastEndTime     = stopTime
            };

            // Open the file that contains the last ETL read timestamp or create
            // the file if it doesn't already exist.
            try
            {
                Utility.PerformIOWithRetries(
                    fileParam.OpenLastEndTimeFile,
                    EtlInMemoryProducerConstants.MethodExecutionInitialRetryIntervalMs,
                    EtlInMemoryProducerConstants.MethodExecutionMaxRetryCount,
                    EtlInMemoryProducerConstants.MethodExecutionMaxRetryIntervalMs);
            }
            catch (Exception e)
            {
                this.traceSource.WriteExceptionAsError(
                    this.logSourceId,
                    e,
                    "Failed to open file {0} for write.",
                    fileName);
                return(false);
            }

            try
            {
                // Write the time stamp to the file
                fileParam.Writer = new StreamWriter(fileParam.Fs);
                try
                {
                    try
                    {
                        Utility.PerformIOWithRetries(
                            fileParam.WriteLastEndTime,
                            EtlInMemoryProducerConstants.MethodExecutionInitialRetryIntervalMs,
                            EtlInMemoryProducerConstants.MethodExecutionMaxRetryCount,
                            EtlInMemoryProducerConstants.MethodExecutionMaxRetryIntervalMs);
                    }
                    catch (Exception e)
                    {
                        this.traceSource.WriteExceptionAsError(
                            this.logSourceId,
                            e,
                            "Failed to write last ETL read end time to file {0}.",
                            fileName);
                        return(false);
                    }
                }
                finally
                {
                    fileParam.Writer.Dispose();
                }
            }
            finally
            {
                fileParam.Fs.Dispose();
            }

            return(true);
        }
コード例 #8
0
        public bool SetLastEndTime(string lastReadFileName, DateTime stopTime)
        {
            var fileFullPath = Path.Combine(this.lastEtlFileReadFileDirectoryPath, lastReadFileName);
            var fileParam    = new LastEndTimeFileParameters(this.traceSource, this.logSourceId)
            {
                Fs       = null,
                FileName = fileFullPath,
                Mode     = FileMode.OpenOrCreate,
                Access   = FileAccess.Write,

                // Convert the timestamp from DateTime to binary
                LastEndTimeLong = stopTime.ToBinary(),
                LastEndTime     = stopTime
            };

            // Open the file that contains the last ETL read timestamp or create
            // the file if it doesn't already exist.
            try
            {
                Utility.PerformIOWithRetries(
                    fileParam.OpenLastEndTimeFile);
            }
            catch (Exception e)
            {
                this.traceSource.WriteExceptionAsError(
                    this.logSourceId,
                    e,
                    "Failed to open file {0} for write.",
                    fileFullPath);
                return(false);
            }

            try
            {
                // Write the time stamp to the file
                fileParam.Writer = new StreamWriter(fileParam.Fs);
                try
                {
                    try
                    {
                        Utility.PerformIOWithRetries(
                            fileParam.WriteLastEndTime);
                    }
                    catch (Exception e)
                    {
                        this.traceSource.WriteExceptionAsError(
                            this.logSourceId,
                            e,
                            "Failed to write last ETL read end time to file {0}.",
                            fileFullPath);
                        return(false);
                    }
                }
                finally
                {
                    fileParam.Writer.Dispose();
                }
            }
            finally
            {
                fileParam.Fs.Dispose();
            }

            return(true);
        }
コード例 #9
0
        private bool ReadFromBackupFile(string backupFilePath, EtwEventTimestamp dataTimestamp)
        {
            // Open the backup file
            StreamReader reader = null;

            try
            {
                Utility.PerformIOWithRetries(
                    () =>
                {
                    FileStream file = FabricFile.Open(backupFilePath, FileMode.Open, FileAccess.Read);
#if !DotNetCoreClrLinux
                    Helpers.SetIoPriorityHint(file.SafeFileHandle, Kernel32Types.PRIORITY_HINT.IoPriorityHintVeryLow);
#endif
                    reader = new StreamReader(file);
                });
            }
            catch (Exception e)
            {
                Utility.TraceSource.WriteExceptionAsError(
                    TraceType,
                    e,
                    "Unable to open backup file {0}",
                    backupFilePath);
                return(false);
            }

            try
            {
                // Get the version line
                string versionLine = string.Empty;
                try
                {
                    Utility.PerformIOWithRetries(
                        () =>
                    {
                        versionLine = reader.ReadLine();
                    });
                }
                catch (Exception e)
                {
                    Utility.TraceSource.WriteExceptionAsError(
                        TraceType,
                        e,
                        "Unable to read version line from backup file {0}",
                        backupFilePath);
                    return(false);
                }

                if (null == versionLine)
                {
                    Utility.TraceSource.WriteError(
                        TraceType,
                        "Backup file {0} does not contain a version line.",
                        backupFilePath);
                    return(false);
                }

                // Ensure that the version line is compatible
                if (false == versionLine.Equals(
                        BackupFileVersionString,
                        StringComparison.Ordinal))
                {
                    Utility.TraceSource.WriteError(
                        TraceType,
                        "Backup file {0} has incompatible file version '{1}'.",
                        backupFilePath,
                        versionLine);
                    return(false);
                }

                // Read the service package table rows from the file
                string tableRecord = null;
                for (;;)
                {
                    tableRecord = string.Empty;
                    try
                    {
                        Utility.PerformIOWithRetries(
                            () =>
                        {
                            tableRecord = reader.ReadLine();
                        });
                    }
                    catch (Exception e)
                    {
                        Utility.TraceSource.WriteExceptionAsError(
                            TraceType,
                            e,
                            "Unable to retrieve table record from backup file {0}",
                            backupFilePath);
                        return(false);
                    }

                    if (null == tableRecord)
                    {
                        // Reached end of file
                        Utility.TraceSource.WriteInfo(
                            TraceType,
                            "Reached end of backup file {0}",
                            backupFilePath);
                        break;
                    }

                    this.ProcessTableRecord(tableRecord, backupFilePath, dataTimestamp);
                }
            }
            finally
            {
                reader.Dispose();
            }

            return(true);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
 internal void WriteEvent(string eventString)
 {
     Utility.PerformIOWithRetries(
         this.WriteEventWorker,
         eventString);
 }