Exemple #1
0
 internal ResourceHealthMonitorWrapper(CacheableResourceHealthMonitor monitor)
 {
     if (monitor == null)
     {
         throw new ArgumentNullException("monitor");
     }
     this.wrappedMonitor = monitor;
 }
        // Token: 0x0600768D RID: 30349 RVA: 0x0018686C File Offset: 0x00184A6C
        private void HandleMonitorRemove(ResourceKey key, CacheableResourceHealthMonitor value, RemoveReason reason)
        {
            ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceKey, RemoveReason>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.HandleMonitorRemove] Removing monitor {0} due to reason {1}", key, reason);
            if (!value.Expired)
            {
                value.Expire();
            }
            IDisposable disposable = value as IDisposable;

            if (disposable != null)
            {
                disposable.Dispose();
            }
        }
        // Token: 0x0600768A RID: 30346 RVA: 0x001865DC File Offset: 0x001847DC
        private bool HandleResourceCacheShouldRemove(ResourceKey key, CacheableResourceHealthMonitor monitor)
        {
            bool flag = false;
            bool result;

            try
            {
                if (monitor is IResourceHealthPoller)
                {
                    if (!Monitor.TryEnter(monitor, 0))
                    {
                        ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceKey>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.HandleResourceCacheShouldRemove] Could not grab the monitor - extending life in cache for key: {0}", key);
                        return(false);
                    }
                    flag = true;
                }
                bool flag2 = true;
                if (monitor != null)
                {
                    flag2 = (DateTime.UtcNow - monitor.LastAccessUtc > ResourceHealthMonitorManager.MonitorSlidingExpiration && monitor.ShouldRemoveResourceFromCache());
                }
                if (flag2)
                {
                    this.pollingMonitors.Remove(key);
                    monitor.Expire();
                }
                result = flag2;
            }
            finally
            {
                if (flag)
                {
                    Monitor.Exit(monitor);
                }
            }
            return(result);
        }
        // Token: 0x0600768C RID: 30348 RVA: 0x001866AC File Offset: 0x001848AC
        private bool HandlePollingItemShouldRemove(ResourceKey key, IResourceHealthPoller value)
        {
            ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceKey>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.HandlePollingItemShouldRemove] Firing poll for monitor {0}", key);
            if (!VariantConfiguration.GetSnapshot(MachineSettingsContext.Local, null, null).WorkloadManagement.GetObject <IResourceSettings>(key.MetricType, new object[0]).Enabled)
            {
                return(false);
            }
            CacheableResourceHealthMonitor cacheableResourceHealthMonitor = value as CacheableResourceHealthMonitor;

            if (cacheableResourceHealthMonitor.Expired)
            {
                ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceKey>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.HandlePollingItemShouldRemove] Monitor has already expired - will not poll for key: {0}", key);
                return(false);
            }
            bool flag = false;

            try
            {
                if (!Monitor.TryEnter(value, 0))
                {
                    ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceKey>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.HandlePollingItemShouldRemove] Could not obtain lock.  Will not poll for key: {0}", key);
                    return(false);
                }
                flag = true;
                if (cacheableResourceHealthMonitor.Expired)
                {
                    return(false);
                }
                if (!value.IsActive)
                {
                    return(false);
                }
                bool flag2 = false;
                try
                {
                    lock (this.instanceLock)
                    {
                        if (!this.outstandingPollCalls.ContainsKey(key))
                        {
                            this.outstandingPollCalls[key] = true;
                            flag2 = true;
                        }
                    }
                    if (flag2)
                    {
                        try
                        {
                            value.Execute();
                        }
                        catch (DataSourceTransientException ex)
                        {
                            ExTraceGlobals.ResourceHealthManagerTracer.TraceError <ResourceKey, string>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.HandlePollingItemShouldRemove] Failed to set metrics for monitor {0}. Exception: {1}", key, ex.Message);
                            return(false);
                        }
                    }
                }
                finally
                {
                    if (flag2)
                    {
                        lock (this.instanceLock)
                        {
                            this.outstandingPollCalls.Remove(key);
                        }
                    }
                }
            }
            finally
            {
                if (flag)
                {
                    Monitor.Exit(value);
                }
            }
            return(false);
        }
        // Token: 0x06007686 RID: 30342 RVA: 0x00186284 File Offset: 0x00184484
        public IResourceLoadMonitor Get(ResourceKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            CacheableResourceHealthMonitor cacheableResourceHealthMonitor = null;

            if (!this.monitors.TryGetValue(key, out cacheableResourceHealthMonitor))
            {
                bool flag  = false;
                bool flag2 = false;
                bool flag3 = true;
                CacheableResourceHealthMonitor cacheableResourceHealthMonitor2 = null;
                try
                {
                    if (ResourceHealthMonitorManager.Active && ExTraceGlobals.ResourceHealthManagerTracer.IsTraceEnabled(TraceType.DebugTrace))
                    {
                        StackTrace arg = new StackTrace();
                        ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceHealthComponent, ResourceKey, StackTrace>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.Get] Creating new monitor for component '{0}'.  Resource Identity: {1}, Stack Trace: {2}", ResourceHealthMonitorManager.resourceHealthComponent, key, arg);
                    }
                    cacheableResourceHealthMonitor2 = ((ResourceHealthMonitorManager.Active && key.MetricType != ResourceMetricType.None) ? key.CreateMonitor() : new DummyResourceHealthMonitor(key));
                    lock (this.instanceLock)
                    {
                        if (!this.monitors.TryGetValue(key, out cacheableResourceHealthMonitor))
                        {
                            flag3 = false;
                            flag2 = true;
                            if (cacheableResourceHealthMonitor2 is DummyResourceHealthMonitor)
                            {
                                ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceKey>((long)this.GetHashCode(), "[ResourceHealthMonitorManager.Get] Dummy monitor created for resource key {0}", key);
                                this.monitors.TryAddAbsolute(key, cacheableResourceHealthMonitor2, ResourceHealthMonitorManager.DummyMonitorAbsoluteExpiration);
                            }
                            else
                            {
                                this.monitors.TryAddAbsolute(key, cacheableResourceHealthMonitor2, ResourceHealthMonitorManager.MonitorSlidingExpiration);
                            }
                            cacheableResourceHealthMonitor = cacheableResourceHealthMonitor2;
                            flag = true;
                        }
                    }
                }
                finally
                {
                    if (flag3 && cacheableResourceHealthMonitor2 != null)
                    {
                        cacheableResourceHealthMonitor2.Expire();
                        IDisposable disposable = cacheableResourceHealthMonitor2 as IDisposable;
                        if (disposable != null)
                        {
                            disposable.Dispose();
                        }
                        cacheableResourceHealthMonitor2 = null;
                    }
                }
                if (flag2)
                {
                    ExTraceGlobals.ResourceHealthManagerTracer.TraceDebug <ResourceKey>((long)this.GetHashCode(), "[ResourceHealthMonitorManager::Get] Cache miss for key '{0}'", key);
                }
                if (flag)
                {
                    IResourceHealthPoller pollHealth = cacheableResourceHealthMonitor as IResourceHealthPoller;
                    if (pollHealth != null)
                    {
                        using (ActivityContext.SuppressThreadScope())
                        {
                            ThreadPool.QueueUserWorkItem(delegate(object state)
                            {
                                this.HandlePollingItemShouldRemove(key, pollHealth);
                            });
                        }
                        ExTraceGlobals.ClientThrottlingTracer.TraceDebug <ResourceKey>((long)this.GetHashCode(), "[ResourceHealthMonitorManager::Get] Adding monitor as polling monitor: {0}", key);
                        this.pollingMonitors.TryAddAbsolute(key, pollHealth, pollHealth.Interval);
                    }
                }
            }
            return(cacheableResourceHealthMonitor.CreateWrapper());
        }