public static void DefragmentBlobFiles(object source, ElapsedEventArgs e) { string str; string str1; try { using (DevelopmentStorageDbDataContext dbContext = DevelopmentStorageDbDataContext.GetDbContext()) { IQueryable <BlockBlobMetaInfo> blocksData = from b in dbContext.BlocksData group b by new { AccountName = b.AccountName, BlobName = b.BlobName, ContainerName = b.ContainerName, DirectoryPath = b.BlockBlob.DirectoryPath } into blobBlocks select new BlockBlobMetaInfo(blobBlocks.Key.AccountName, blobBlocks.Key.ContainerName, blobBlocks.Key.BlobName, blobBlocks.Key.DirectoryPath) { BlobSize = blobBlocks.Sum <BlockData>((BlockData x) => x.Length.GetValueOrDefault((long)0)) }; foreach (BlockBlobMetaInfo blocksDatum in blocksData) { BlockBlobDataManager.GetDataFiles(blocksDatum, out str, out str1); IStringDataEventStream infoDebug = Logger <INormalAndDebugLogger> .Instance.InfoDebug; object[] blobName = new object[] { blocksDatum.BlobName, blocksDatum.ContainerName, blocksDatum.BlobDirectory, str, str1, blocksDatum.BlobSize }; infoDebug.Log("BlockBlob: DefragmentBlobFiles BlobInfo Name {0}, ContainerName {1}, Directory {2}, ROFile {3}, RWFile {4}, Size{5}", blobName); long length = (long)0; FileInfo fileInfo = null; if (!string.IsNullOrEmpty(str)) { fileInfo = new FileInfo(str); } FileInfo fileInfo1 = null; if (!string.IsNullOrEmpty(str1)) { fileInfo1 = new FileInfo(str1); } if (fileInfo1 != null && fileInfo1.Exists) { IStringDataEventStream stringDataEventStream = Logger <INormalAndDebugLogger> .Instance.InfoDebug; object[] objArray = new object[] { fileInfo1.Length }; stringDataEventStream.Log("BlockBlob: DefragmentBlobFiles RWFileLength {0}", objArray); length += fileInfo1.Length; } if (fileInfo != null && fileInfo.Exists) { IStringDataEventStream infoDebug1 = Logger <INormalAndDebugLogger> .Instance.InfoDebug; object[] length1 = new object[] { fileInfo.Length }; infoDebug1.Log("BlockBlob: DefragmentBlobFiles ROFileLength {0}", length1); length += fileInfo.Length; } if ((double)length <= (double)blocksDatum.BlobSize * 2) { continue; } Logger <INormalAndDebugLogger> .Instance.InfoDebug.Log("BlockBlob: DefragmentBlobFiles FileSize {0}", new object[] { length }); string key = blocksDatum.GetKey(); lock (BlockBlobGarbageCollector.defragLock) { if (!BlockBlobGarbageCollector.toDefragmentDict.ContainsKey(key)) { BlockBlobGarbageCollector.toDefragmentDict.Add(key, blocksDatum); BlockBlobGarbageCollector.defragWorkItems.Enqueue(blocksDatum); Logger <INormalAndDebugLogger> .Instance.InfoDebug.Log("BlockBlob: DefragmentBlobFiles QueuedWork"); Monitor.Pulse(BlockBlobGarbageCollector.defragLock); } } } } if (BlockBlobGarbageCollector.shouldInitializeWorkers) { for (int i = 0; i < 2; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(BlockBlobGarbageCollector.DoDefragmentBlobWork)); } BlockBlobGarbageCollector.shouldInitializeWorkers = false; } } catch (Exception exception1) { Exception exception = exception1; Logger <INormalAndDebugLogger> .Instance.Error.Log("BlockBlob: DefragmentBlobFiles Exception {0}", new object[] { exception.StackTrace }); } BlockBlobGarbageCollector.defragmentTimer.Interval = BlockBlobGarbageCollector.GetTimerIntervalOrDefault(false); BlockBlobGarbageCollector.defragmentTimer.Start(); }
private static void DoDefragmentBlobWork(object info) { string str; bool flag; long num; string str1; while (true) { BlockBlobMetaInfo blockBlobMetaInfo = null; lock (BlockBlobGarbageCollector.defragLock) { if (BlockBlobGarbageCollector.defragWorkItems.Count == 0) { Monitor.Wait(BlockBlobGarbageCollector.defragLock); } blockBlobMetaInfo = BlockBlobGarbageCollector.defragWorkItems.Dequeue(); } IStringDataEventStream infoDebug = Logger <INormalAndDebugLogger> .Instance.InfoDebug; object[] objArray = new object[] { blockBlobMetaInfo.ToString() }; infoDebug.Log("BlockBlob: DoDefragmentBlobWork WorkItem! {0}", objArray); try { BlockBlobDataManager.GetDataFiles(blockBlobMetaInfo, out str1, out str); Logger <INormalAndDebugLogger> .Instance.InfoDebug.Log("BlockBlob: DoDefragmentBlobWork readOnlyFile {0}, readWriteFile {1}", new object[] { str1, str }); if (string.IsNullOrEmpty(str1)) { BlockBlobDataManager.SealCurrentWriteFile(blockBlobMetaInfo, -1); } BlockBlobDataManager.GetDataFiles(blockBlobMetaInfo, out str1, out str); Logger <INormalAndDebugLogger> .Instance.InfoDebug.Log("BlockBlob: DoDefragmentBlobWork NEW readOnlyFile {0}, readWriteFile {1}", new object[] { str1, str }); } catch (Exception exception1) { Exception exception = exception1; Logger <INormalAndDebugLogger> .Instance.Error.Log("BlockBlob: DoDefragmentBlobWork Exception {0}", new object[] { exception.StackTrace }); BlockBlobGarbageCollector.RemoveWorkFromProcessingDictionary(blockBlobMetaInfo); continue; } int num1 = 0; do { Label0: if (num1 >= 5) { break; } flag = true; int maxItemsToSync = BlockBlobGarbageCollector.GetMaxItemsToSync(); try { using (DevelopmentStorageDbDataContext dbContext = DevelopmentStorageDbDataContext.GetDbContext()) { IQueryable <BlockData> blockDatas = ( from b in dbContext.BlocksData where (b.ContainerName == blockBlobMetaInfo.ContainerName) && (b.BlobName == blockBlobMetaInfo.BlobName) && (b.FilePath == str1) select b).Take <BlockData>(maxItemsToSync); foreach (BlockData nullable in blockDatas) { flag = false; byte[] numArray = null; using (FileStream fileStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fileStream.Seek(nullable.StartOffset.Value, SeekOrigin.Begin); numArray = new byte[checked ((IntPtr)nullable.Length.Value)]; fileStream.Read(numArray, 0, (int)numArray.Length); } string file = BlockBlobDataManager.WriteBytesToFile(blockBlobMetaInfo, numArray, out num); nullable.FilePath = file; nullable.StartOffset = new long?(num); } dbContext.SubmitChanges(ConflictMode.ContinueOnConflict); } } catch (Exception exception3) { Exception exception2 = exception3; num1++; IStringDataEventStream error = Logger <INormalAndDebugLogger> .Instance.Error; object[] stackTrace = new object[] { exception2.StackTrace, num1 }; error.Log("BlockBlob: DoDefragmentBlobWork Exception {0}, Attempt {1}", stackTrace); goto Label0; } }while (!flag); if (num1 < 5) { try { BlockBlobDataManager.ResetReadOnlyFileToNull(blockBlobMetaInfo); } catch (Exception exception5) { Exception exception4 = exception5; Logger <INormalAndDebugLogger> .Instance.Error.Log("BlockBlob: DoDefragmentBlobWork Exception while setting up rofile to null {0} ", new object[] { exception4.StackTrace }); } } BlockBlobGarbageCollector.RemoveWorkFromProcessingDictionary(blockBlobMetaInfo); } }