예제 #1
0
        private void MoveToPermanentStorage(string sessionId, string sourceFile, string fileName, string blobSasUri)
        {
            if (string.IsNullOrWhiteSpace(blobSasUri))
            {
                var sessionDirectory = GetLogsFolderForSession(sessionId);
                var collectFileName  = Path.Combine(sessionDirectory, fileName);
                AppendToMonitoringLog($"Copying file from temp folders to [{collectFileName}]", true);
                try
                {
                    FileSystemHelpers.CopyFile(sourceFile, collectFileName);
                    AppendToMonitoringLog($"Copied file from temp folders to [{collectFileName}]", true);
                }
                catch (Exception ex)
                {
                    Logger.LogCpuMonitoringErrorEvent("Failed while moving file to Permanent FileSystem storage", ex, sessionId);
                }
            }
            else
            {
                try
                {
                    string relativeFilePath = Path.Combine("Monitoring", "Logs", sessionId, fileName);
                    Lease  lease            = Infrastructure.LeaseManager.TryGetLease(relativeFilePath, blobSasUri);
                    if (lease == null)
                    {
                        // This instance is already running this collector
                        Logger.LogCpuMonitoringVerboseEvent($"Could not get lease to upload the memory dump - {relativeFilePath}", sessionId);
                    }

                    AppendToMonitoringLog($"Copying {fileName} from temp folders to Blob Storage", true);
                    var accessCondition = AccessCondition.GenerateLeaseCondition(lease.Id);
                    var taskToUpload    = Task.Run(() =>
                    {
                        FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        var blockBlob         = BlobController.GetBlobForFile(relativeFilePath, blobSasUri);
                        blockBlob.UploadFromStream(fileStream, accessCondition);
                    });

                    while (!taskToUpload.IsCompleted)
                    {
                        lease.Renew();
                        Logger.LogCpuMonitoringVerboseEvent($"Renewing lease to the blob file", sessionId);
                        Thread.Sleep(Infrastructure.Settings.LeaseRenewalTime);
                    }
                    lease.Release();
                    AppendToMonitoringLog($"Copied {fileName} from temp folders to Blob Storage", true);
                }
                catch (Exception ex)
                {
                    Logger.LogCpuMonitoringErrorEvent("Failed copying file to blob storage", ex, sessionId);
                }
            }
        }
예제 #2
0
        private static string CacheFileInTempDirectory(AnalysisRequest request)
        {
            string outputPath = MonitoringSessionController.TempFilePath;

            FileSystemHelpers.CreateDirectoryIfNotExists(outputPath);

            string dumpFileInTempDirectory = Path.Combine(outputPath, Path.GetFileName(request.LogFileName));

            Logger.LogCpuMonitoringVerboseEvent($"Caching file {request.LogFileName} to {dumpFileInTempDirectory}", request.SessionId);

            if (!FileSystemHelpers.FileExists(dumpFileInTempDirectory))
            {
                Logger.LogCpuMonitoringVerboseEvent($"File {dumpFileInTempDirectory} does not exist. Copying it locally", request.SessionId);
                if (!string.IsNullOrWhiteSpace(request.BlobSasUri))
                {
                    try
                    {
                        string filePath = Path.Combine("Monitoring", "Logs", request.SessionId, Path.GetFileName(request.LogFileName));
                        var    blob     = BlobController.GetBlobForFile(filePath, request.BlobSasUri);
                        var    ms       = new MemoryStream();
                        blob.DownloadToStream(ms);
                        ms.Position = 0;
                        using (FileStream file = new FileStream(dumpFileInTempDirectory, FileMode.Create, FileAccess.Write))
                        {
                            byte[] bytes = new byte[ms.Length];
                            ms.Read(bytes, 0, (int)ms.Length);
                            file.Write(bytes, 0, bytes.Length);
                            ms.Close();
                        }
                        Logger.LogCpuMonitoringVerboseEvent($"Copied file from {request.LogFileName} to {dumpFileInTempDirectory} ", request.SessionId);
                    }
                    catch (Exception ex)
                    {
                        Logger.LogCpuMonitoringErrorEvent("Failed while copying file from Blob", ex, request.SessionId);
                    }
                }
                else
                {
                    FileSystemHelpers.CopyFile(request.LogFileName, dumpFileInTempDirectory);
                    Logger.LogCpuMonitoringVerboseEvent($"Copied file from {request.LogFileName} to {dumpFileInTempDirectory} ", request.SessionId);
                }
            }
            return(dumpFileInTempDirectory);
        }
예제 #3
0
        public static BlobLease TryGetLease(string pathToFile, string blobSasUri, string leaseId = null)
        {
            BlobLease lease = null;

            try
            {
                Logger.LogInfo($"Inside TryGetLease method with {pathToFile} and lease {leaseId == null}");
                var blob = BlobController.GetBlobForFile(pathToFile, blobSasUri);
                if (!blob.Exists())
                {
                    // This was a path blob. We want to create a new blob to hold the lease
                    // Luckily for us, just uploading some text to it does the trick
                    blob.UploadText("LeaseHolder");
                }

                leaseId = string.IsNullOrEmpty(leaseId) ? Guid.NewGuid().ToString() : leaseId;
                var leaseDuration = Infrastructure.Settings.LeaseDuration;
                lease = new BlobLease()
                {
                    PathBeingLeased = pathToFile,
                    ExpirationDate  = DateTime.UtcNow + leaseDuration,
                    _fileBlob       = blob
                };
                try
                {
                    Logger.LogInfo($"Acquiring Lease");
                    lease.Id = blob.AcquireLease(leaseDuration, leaseId);
                }
                catch (StorageException ex)
                {
                    Logger.LogErrorEvent("Got StorageException while acquiring lease", ex);
                    // Someone else is holding the lease
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Logger.LogErrorEvent("Failed in TryGetLease method", ex);
            }
            return(lease);
        }