示例#1
0
        public static void DeleteOrphanedFiles(object source, ElapsedEventArgs e)
        {
            try
            {
                try
                {
                    using (DevelopmentStorageDbDataContext dbContext = DevelopmentStorageDbDataContext.GetDbContext())
                    {
                        BlockBlobDataManager.CurrentBlobCount = dbContext.Blobs.OfType <BlockBlob>().Count <BlockBlob>();
                        if (dbContext.Blobs.Any <Blob>((Blob blob) => blob is BlockBlob || blob is PageBlob))
                        {
                            DateTime         utcNow   = DateTime.UtcNow;
                            DateTime         dateTime = utcNow.Subtract(TimeSpan.FromMinutes(20));
                            HashSet <string> strs1    = new HashSet <string>((
                                                                                 from topDir in BlockBlobDataManager.BlobDataTopLevelDirectories
                                                                                 where Directory.Exists(topDir)
                                                                                 select topDir).SelectMany <string, string>((string topDir) => {
                                IEnumerable <string> strs;
                                try
                                {
                                    strs =
                                        from dir in Directory.EnumerateDirectories(topDir)
                                        where Directory.GetCreationTimeUtc(dir) < dateTime
                                        select dir;
                                }
                                catch (Exception exception)
                                {
                                    Logger <INormalAndDebugLogger> .Instance.Error.Log("BlockBlob: Listing Directories under {0} failed. Exception: {1}", new object[] { topDir, exception.ToString() });
                                    strs = Enumerable.Empty <string>();
                                }
                                return(strs);
                            }), StringComparer.InvariantCultureIgnoreCase);
                            IQueryable <string> strs2 =
                                from blob in dbContext.Blobs.OfType <BlockBlob>()
                                select blob.DirectoryPath;
                            strs1.ExceptWith(strs2);
                            foreach (string str in strs1)
                            {
                                try
                                {
                                    Logger <INormalAndDebugLogger> .Instance.InfoDebug.Log("BlockBlob: Deleting Directory {0}", new object[] { str });

                                    Directory.Delete(str, true);
                                    BlockBlobDataManager.RemoveEntryIfExists(str);
                                }
                                catch (Exception exception2)
                                {
                                    Exception exception1 = exception2;
                                    Logger <INormalAndDebugLogger> .Instance.Error.Log("DeleteOrphanedFiles: Unable to delete directory {0} with Exception {1}", new object[] { str, exception1 });
                                }
                            }
                            HashSet <string> strs3 = new HashSet <string>(
                                from dir in Directory.EnumerateFiles(DevelopmentStorageDbDataContext.PageBlobRoot)
                                where Directory.GetCreationTimeUtc(dir) < dateTime
                                select dir, StringComparer.InvariantCultureIgnoreCase);
                            IQueryable <string> strs4 =
                                from blob in dbContext.Blobs.OfType <PageBlob>()
                                where blob.FileName != null
                                select blob.FileName into fileName
                                select DbPageBlobObject.GetFilePath(fileName);

                            strs3.ExceptWith(strs4);
                            foreach (string str1 in strs3)
                            {
                                try
                                {
                                    string filePath = DbPageBlobObject.GetFilePath(str1);
                                    Logger <INormalAndDebugLogger> .Instance.InfoDebug.Log("PageBlob: Deleting File {0}", new object[] { filePath });

                                    File.Delete(filePath);
                                }
                                catch (Exception exception4)
                                {
                                    Exception exception3 = exception4;
                                    Logger <INormalAndDebugLogger> .Instance.Error.Log("DeleteOrphanedFiles: Unable to delete page blob files with Exception {0}", new object[] { exception3 });
                                }
                            }
                        }
                    }
                }
                catch (Exception exception6)
                {
                    Exception exception5 = exception6;
                    Logger <INormalAndDebugLogger> .Instance.Error.Log("BlockBlob: DeleteOrphanedFiles: Exception {0}", new object[] { exception5 });
                }
            }
            finally
            {
                BlockBlobGarbageCollector.gcTimer.Interval = BlockBlobGarbageCollector.GetTimerIntervalOrDefault(true);
                BlockBlobGarbageCollector.gcTimer.Start();
            }
        }
示例#2
0
        public bool PutPage(long startIndex, byte[] data)
        {
            bool flag;
            bool flag1 = false;

            this._currentLength += data.LongLength;
            bool     flag2    = false;
            DateTime?nullable = this._lastModifiedTime;

            try
            {
                for (int i = 0; i < this._maxNumberOfRetries; i++)
                {
                    this._lastModifiedTime = nullable;
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            using (DevelopmentStorageDbDataContext dbContext = DevelopmentStorageDbDataContext.GetDbContext())
                            {
                                this.GetContainer(dbContext);
                                PageBlob    blob        = this.GetBlob(dbContext, true) as PageBlob;
                                CurrentPage currentPage = new CurrentPage()
                                {
                                    AccountName      = blob.AccountName,
                                    ContainerName    = blob.ContainerName,
                                    BlobName         = blob.BlobName,
                                    VersionTimestamp = blob.VersionTimestamp,
                                    StartOffset      = startIndex,
                                    EndOffset        = startIndex + data.LongLength,
                                    SnapshotCount    = blob.SnapshotCount
                                };
                                dbContext.CurrentPages.InsertOnSubmit(currentPage);
                                lock (DbPageBlobObject.m_writeFileLock)
                                {
                                    if (!flag2)
                                    {
                                        using (FileStream fileStream = new FileStream(DbPageBlobObject.GetFilePath(blob.FileName), FileMode.OpenOrCreate, FileAccess.Write))
                                        {
                                            fileStream.Seek(startIndex, SeekOrigin.Begin);
                                            fileStream.Write(data, 0, (int)data.Length);
                                        }
                                        flag2 = true;
                                    }
                                    dbContext.SubmitChanges();
                                    dbContext.Refresh(RefreshMode.OverwriteCurrentValues, blob);
                                    this.SetCopyStatus(blob);
                                    dbContext.SubmitChanges();
                                    dbContext.Refresh(RefreshMode.OverwriteCurrentValues, blob);
                                    transactionScope.Complete();
                                    this._lastModifiedTime = blob.LastModificationTime;
                                }
                                IStringDataEventStream info = Logger <IRestProtocolHeadLogger> .Instance.Info;
                                object[] objArray           = new object[] { startIndex, startIndex + data.LongLength - (long)1 };
                                info.Log("Uploaded Page: {0}-{1}", objArray);
                            }
                        }
                        break;
                    }
                    catch (SqlException sqlException)
                    {
                        if (this.ShouldRethrowException(i, sqlException, out flag1))
                        {
                            throw;
                        }
                    }
                }
                return(true);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                Logger <IRestProtocolHeadLogger> .Instance.Error.Log("CopyBlob: PutPage exception {0}", new object[] { exception });

                this.SetCopyFailed(exception, flag1, null);
                flag = false;
            }
            return(flag);
        }