private bool CreateLocalMapFile(string localMapDestination) { try { Utility.PerformIOWithRetries( () => { using (FileStream fs = FabricFile.Create(localMapDestination)) { } }, AzureBlobEtwConstants.MethodExecutionInitialRetryIntervalMs, AzureBlobEtwConstants.MethodExecutionMaxRetryCount, AzureBlobEtwConstants.MethodExecutionMaxRetryIntervalMs); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to create local map file {0}.", localMapDestination); return(false); } return(true); }
private void TestIntersectingHardLinkAndReplace(bool isLongPath, FileShare fileShare) { var folderPath = this.testPath; if (true == isLongPath) { folderPath = this.ExtendPath(folderPath); } var filePath = Path.Combine(folderPath, this.testFileName); Assert.IsTrue(!isLongPath || filePath.Length > 260, "file path must be greater than max path size."); var hardLinkedFilePath = Path.Combine(folderPath, this.testHardLinkedFileName); Assert.IsTrue(!isLongPath || hardLinkedFilePath.Length > 260, "hard linked file path must be greater than max path size."); var replaceFilePath = Path.Combine(folderPath, this.testReplaceFileName); Assert.IsTrue(!isLongPath || replaceFilePath.Length > 260, "replace file path must be greater than max path size."); var backupFilePath = Path.Combine(folderPath, this.testBackupFileName); Assert.IsTrue(!isLongPath || backupFilePath.Length > 260, "backup file path must be greater than max path size."); LogHelper.Log("FabricDirectory.Create {0}", folderPath); FabricDirectory.CreateDirectory(folderPath); LogHelper.Log("FabricFile.Create {0}", filePath); using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(filePath))) { LogHelper.Log("Write {0}", this.testString); streamWriter.WriteLine(this.testString); streamWriter.Flush(); } using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(replaceFilePath))) { LogHelper.Log("Write {0}", this.testNewString); streamWriter.WriteLine(this.testNewString); streamWriter.Flush(); } FabricFile.CreateHardLink(hardLinkedFilePath, filePath); using (StreamReader hardLinkReader0 = new StreamReader(FabricFile.Open(hardLinkedFilePath, FileMode.Open, FileAccess.Read, fileShare))) { var hardLinkContent0 = hardLinkReader0.ReadLine(); Assert.AreEqual <string>(this.testString, hardLinkContent0, "after replace hard link file must have the old content."); try { FabricFile.Replace(replaceFilePath, filePath, backupFilePath, false); Assert.Fail(); } catch (FileLoadException) { } } }
private static ulong GetNewContainerInstanceIdAtomic( ContainerDescription containerDesc) { #if DotNetCoreClrLinux // There is a file called containerid in // /log/application/partitionid/servicepackageactivationid/codepackagename/ // This file contains the number corresponding to the latest folder for logs // /mnt/log/application/partitionid/servicepackageactivationid/codepackagename/number/application.log // // For the first instance of thecontainer, create a new file and start at 0, // For every container afterwards, check the file exists, read from the file // and add 1 to the value in the file in order to get the new current instance id // and save the value to the file. var applicationNamePart = containerDesc.ApplicationName; var applicationNameExtraPart = string.Format("{0}/", RootNamingUriString); if (applicationNamePart.StartsWith(applicationNameExtraPart)) { applicationNamePart = applicationNamePart.Substring(applicationNameExtraPart.Length); } applicationNamePart = applicationNamePart.Replace('/', '_'); var digestedApplicationName = applicationNamePart + DigestedApplicationNameDockerSuffix; var logRoots = "/mnt/logs"; var logPath = Path.Combine(logRoots, digestedApplicationName, containerDesc.PartitionId, containerDesc.ServicePackageActivationId, containerDesc.CodePackageName); if (!FabricDirectory.Exists(logPath)) { FabricDirectory.CreateDirectory(logPath); } ulong containerId = 0; var containerInstanceIdFilePath = Path.Combine(logPath, CodePackageInstanceIdFileName); if (!FabricFile.Exists(containerInstanceIdFilePath)) { using (var containerIdFile = FabricFile.Create(containerInstanceIdFilePath)) { containerId = 0; } } else { containerId = ReadContainerIdFromFile(containerInstanceIdFilePath) + 1; } WriteContainerIdToFile(containerInstanceIdFilePath, containerId); return(containerId); #else return(0); #endif }
private bool TryCreateLocalMapFile(int retryCount, string localMapDestination, DateTime sourceLastWriteTime) { // First create a temp file string tempFile = Utility.GetTempFileName(); try { Utility.PerformIOWithRetries( () => { using (FileStream fs = FabricFile.Create(tempFile)) { } }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to create temporary file to move to {0}.", localMapDestination); return(false); } // Move the temp file to the local map try { // Set the last write time for the temp file to the // last write time from the source file snapshot // Today, it appear that last write time isn't really being used (for LMap files.. so we ignore the rare case where it may fail) this.TryUpdateLastWriteTime(tempFile, sourceLastWriteTime); Utility.PerformIOWithRetries( () => { FabricFile.Move(tempFile, localMapDestination); }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to move file {0} to {1}.", tempFile, localMapDestination); return(false); } return(true); }
private bool TryCreateLocalMapFile(int retryCount, string localMapDestination, DateTime sourceLastWriteTime) { // First create a temp file string tempFile = Utility.GetTempFileName(); try { Utility.PerformIOWithRetries( () => { using (FileStream fs = FabricFile.Create(tempFile)) { } }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to create temporary file to move to {0}.", localMapDestination); return(false); } // Set the last write time for the temp file to the // last write time from the source file snapshot File.SetLastWriteTime(tempFile, sourceLastWriteTime); // Move the temp file to the local map try { Utility.PerformIOWithRetries( () => { FabricFile.Move(tempFile, localMapDestination); }, retryCount); } catch (Exception e) { this.TraceSource.WriteExceptionAsError( this.LogSourceId, e, "Failed to move file {0} to {1}.", tempFile, localMapDestination); return(false); } return(true); }
public void FabricFile_CreateNegative() { try { LogHelper.Log("FabricFile.Create {0}", this.badPath); FabricFile.Create(this.badPath); Assert.Fail("should never reach here"); } catch (Exception e) { LogHelper.Log("caught exception {0}", e); Assert.IsTrue(e is IOException); } }
public void FabricFile_GetSize_FileOpenInReadShare_Succeeds() { string filePath = Path.Combine(this.testPath, this.testFileName); LogHelper.Log("FabricDirectory.Create {0}", this.testPath); FabricDirectory.CreateDirectory(this.testPath); LogHelper.Log("FabricFile.Create {0}", filePath); using (var writer = new StreamWriter(FabricFile.Create(filePath))) { writer.WriteLine(this.testString); } LogHelper.Log("FabricFile.Open {0}", filePath); using (var filestream = FabricFile.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { long fileSize = FabricFile.GetSize(filePath); LogHelper.Log("FabricFile.GetSize {0}", fileSize); Assert.IsTrue(fileSize > 0, "FabricFile.GetSize should return non-zero file size."); } }
internal void ProcessInactiveEtlFile(FileInfo etlFile, DateTime lastEndTime, CancellationToken cancellationToken) { this.currentTimestamp = DateTime.MinValue; // Raise an event to indicate that we're starting on a new ETL file this.OnEtlFileReadStart(etlFile.Name, false, lastEndTime); // Read events from the ETL file DateTime startTime = this.GetStartTime(lastEndTime); DateTime endTime = this.processingWinFabEvents ? DateTime.MaxValue : Utility.ApplicationEtwTracesEndTime; ITraceFileEventReader eventReader; try { eventReader = this.traceFileEventReaderFactory.CreateTraceFileEventReader(etlFile.FullName); } catch (FileNotFoundException e) { // The file was not found, most likely because it was deleted by // the our old logs deletion routine. // // NOTE: The method that processes the active ETL file does not handle // this exception because we have logic to avoid deleting an active // ETL file. We don't want to delete the active ETL file because ETW is // still writing to it. this.traceSource.WriteWarning( this.logSourceId, "Exception occurred while trying to process ETL file {0}. The file may have been deleted. Exception information: {1}", etlFile.FullName, e); // Raise an event to indicate that we're done with this ETL file this.OnEtlFileReadStop(etlFile.Name, false, lastEndTime, this.lastCompletedTimestamp, true); return; } eventReader.EventRead += (sender, args) => this.OnEventFromEtl(sender, args, cancellationToken); this.eventsReadFromCurrentEtl = false; ReadEventsParameters readParam = new ReadEventsParameters() { EventReader = eventReader, StartTime = startTime, EndTime = endTime }; try { var traceSessionMetadata = eventReader.ReadTraceSessionMetadata(); FabricEvents.Events.TraceSessionStats( traceSessionMetadata.TraceSessionName, traceSessionMetadata.EventsLostCount, traceSessionMetadata.StartTime, traceSessionMetadata.EndTime); Utility.PerformWithRetries( ReadEventsWorker, readParam, new RetriableOperationExceptionHandler(this.ReadEventsExceptionHandler)); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to read some or all of the events from ETL file {0}.", etlFile.FullName); } finally { eventReader.Dispose(); } // Raise an event to indicate that we're done with this ETL file this.OnEtlFileReadStop(etlFile.Name, false, lastEndTime, this.lastCompletedTimestamp, cancellationToken.IsCancellationRequested); // We're done with this ETL file. Create a marker file for it. if (!cancellationToken.IsCancellationRequested) { string etlFileMarker = Path.Combine( this.markerFileDirectory, etlFile.Name); try { Utility.PerformIOWithRetries( () => { // Create file using (FileStream fs = FabricFile.Create(etlFileMarker)) { } }); this.traceSource.WriteInfo( this.logSourceId, "Marker file {0} created.", etlFileMarker); } catch (Exception e) { this.traceSource.WriteExceptionAsError( this.logSourceId, e, "Failed to create marker file {0}.", etlFileMarker); } } else { // The producer is being stopped. It is possible that we didn't fully // process this ETL file because we detected that the producer was // being stopped. Therefore do not create a marker file for it in the // archived traces directory yet. We should process it again when the // producer is restarted. this.traceSource.WriteInfo( this.logSourceId, "The producer is being stopped. Therefore, no marker file will be created for ETL file {0} in the archived traces directory.", etlFile.FullName); } }
private void TestIntersectingCopyAndReplace(bool isLongPath) { var folderPath = this.testPath; if (true == isLongPath) { folderPath = this.ExtendPath(folderPath); } var filePath = Path.Combine(folderPath, this.testFileName); Assert.IsTrue(!isLongPath || filePath.Length > 260, "file path must be greater than max path size."); var copyFilePath = Path.Combine(folderPath, this.testHardLinkedFileName); Assert.IsTrue(!isLongPath || copyFilePath.Length > 260, "hard linked file path must be greater than max path size."); var replaceFilePath = Path.Combine(folderPath, this.testReplaceFileName); Assert.IsTrue(!isLongPath || replaceFilePath.Length > 260, "replace file path must be greater than max path size."); var backupFilePath = Path.Combine(folderPath, this.testBackupFileName); Assert.IsTrue(!isLongPath || backupFilePath.Length > 260, "backup file path must be greater than max path size."); LogHelper.Log("FabricDirectory.Create {0}", folderPath); FabricDirectory.CreateDirectory(folderPath); LogHelper.Log("FabricFile.Create {0}", filePath); using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(filePath))) { LogHelper.Log("Write {0}", this.testString); streamWriter.WriteLine(this.testString); streamWriter.Flush(); } using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(replaceFilePath))) { LogHelper.Log("Write {0}", this.testNewString); streamWriter.WriteLine(this.testNewString); streamWriter.Flush(); } FabricFile.Copy(filePath, copyFilePath, false); using (StreamReader reader = new StreamReader(FabricFile.Open(filePath, FileMode.Open, FileAccess.Read))) { var content = reader.ReadLine(); Assert.AreEqual <string>(this.testString, content, "before replace current file must have the old content."); } using (StreamReader copyReader0 = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var hardLinkContent0 = copyReader0.ReadLine(); Assert.AreEqual <string>(this.testString, hardLinkContent0, "after replace hard link file must have the old content."); FabricFile.Replace(replaceFilePath, filePath, backupFilePath, false); using (StreamReader fileReader = new StreamReader(FabricFile.Open(filePath, FileMode.Open, FileAccess.Read))) { var content = fileReader.ReadLine(); Assert.AreEqual <string>(this.testNewString, content, "after replace current file must have the new content."); } using (StreamReader copyReader1 = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var content = copyReader1.ReadLine(); Assert.AreEqual <string>(this.testString, content, "after replace hard link file must have the old content."); } } }
private void CopyTest(bool copyFileExists, bool isLongPath, bool overWrite) { var folderPath = this.testPath; if (true == isLongPath) { folderPath = this.ExtendPath(folderPath); } var filePath = Path.Combine(folderPath, this.testFileName); Assert.IsTrue(!isLongPath || filePath.Length > 260, "file path must be greater than max path size."); var copyFilePath = Path.Combine(folderPath, this.testReplaceFileName); Assert.IsTrue(!isLongPath || copyFilePath.Length > 260, "replace file path must be greater than max path size."); LogHelper.Log("FabricDirectory.Create {0}", folderPath); FabricDirectory.CreateDirectory(folderPath); LogHelper.Log("FabricFile.Create {0}", filePath); using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(filePath))) { LogHelper.Log("Write {0}", this.testNewString); streamWriter.WriteLine(this.testNewString); streamWriter.Flush(); } if (copyFileExists) { using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(copyFilePath))) { LogHelper.Log("Write {0}", this.testString); streamWriter.WriteLine(this.testString); streamWriter.Flush(); } using (StreamReader copyReader = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var content = copyReader.ReadLine(); Assert.AreEqual <string>(this.testString, content, "before copy state."); } } else { Assert.IsFalse(FabricFile.Exists(copyFilePath)); } try { FabricFile.Copy(filePath, copyFilePath, overWrite); Assert.IsTrue(overWrite); } catch (FabricException e) { Assert.IsFalse(overWrite); LogHelper.Log("Exception thrown as expected {0}", e.GetType().ToString()); } if (true == overWrite || false == copyFileExists) { using (StreamReader copyReader = new StreamReader(FabricFile.Open(copyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { var content = copyReader.ReadLine(); Assert.AreEqual <string>(this.testNewString, content, "after copy file must have the new content."); LogHelper.Log("Read {0} as expected", this.testNewString); } } }
public void FabricFile_EndToEndPositive() { var folderPath = this.testPath; folderPath = this.ExtendPath(folderPath); var filePath = Path.Combine(folderPath, this.testFileName); Assert.IsTrue(filePath.Length > 260); LogHelper.Log("FabricDirectory.Create {0}", folderPath); FabricDirectory.CreateDirectory(folderPath); LogHelper.Log("FabricFile.Create {0}", filePath); using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(filePath))) { LogHelper.Log("Write {0}", this.testString); streamWriter.WriteLine(this.testString); } LogHelper.Log("FabricDirectory.GetDirectories {0}", this.testPath); var result = FabricDirectory.GetDirectories(this.testPath); Assert.AreEqual(1, result.Length); LogHelper.Log("FabricDirectory.GetFiles {0}", this.testPath); result = FabricDirectory.GetFiles(this.testPath); Assert.AreEqual(0, result.Length); LogHelper.Log("FabricDirectory.GetFiles {0}, AllDirectories", this.testPath); result = FabricDirectory.GetFiles(this.testPath, "*", SearchOption.AllDirectories); Assert.AreEqual(1, result.Length); LogHelper.Log("FabricDirectory.GetDirectories {0}", folderPath); result = FabricDirectory.GetDirectories(folderPath); Assert.AreEqual(0, result.Length); LogHelper.Log("FabricDirectory.GetFiles {0}", folderPath); result = FabricDirectory.GetFiles(folderPath); Assert.AreEqual(1, result.Length); LogHelper.Log("FabricFile.Open {0}", filePath); using (StreamReader streamReader = new StreamReader(FabricFile.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None))) { string actual = streamReader.ReadLine(); LogHelper.Log("Read {0}", actual); Assert.AreEqual(this.testString, actual); } LogHelper.Log("FabricFile.GetSize {0}", filePath); long size = FabricFile.GetSize(filePath); Assert.IsTrue(size > 0); LogHelper.Log("FabricPath.GetDirectoryName {0}", filePath); string directoryName = FabricPath.GetDirectoryName(filePath); Assert.AreEqual(folderPath, directoryName); LogHelper.Log("FabricFile.GetLastWriteTime {0}", filePath); DateTime oldTime = FabricFile.GetLastWriteTime(filePath); Thread.Sleep(TimeSpan.FromSeconds(1)); using (StreamWriter streamWriter = new StreamWriter(FabricFile.Open(filePath, FileMode.Open, FileAccess.Write))) { LogHelper.Log("Write {0}", this.testString); streamWriter.WriteLine(this.testString); } DateTime newTime = FabricFile.GetLastWriteTime(filePath); Assert.IsTrue(newTime > oldTime); }