Example #1
0
        private static IDictionary <string, long> GetCacheBulkStatistics(ICacheCoreSession session)
        {
            var cacheStatistics = new Dictionary <string, long>();

            foreach (var statistic in GetLoggedStatistics(session, out _))
            {
                cacheStatistics.Add(statistic.qualifiedId, statistic.value);
            }

            return(cacheStatistics);
        }
Example #2
0
 private CacheCoreCacheInitializer(
     LoggingContext loggingContext,
     ICacheCoreCache cache,
     ICacheCoreSession session,
     List <IDisposable> acquiredDisposables,
     bool enableFingerprintLookup,
     RootTranslator rootTranslator)
     : base(
         loggingContext,
         acquiredDisposables,
         enableFingerprintLookup)
 {
     Contract.Requires(cache != null);
     Contract.Requires(session != null);
     m_cache             = cache;
     m_session           = session;
     m_rootTranslator    = rootTranslator;
     m_initialStatistics = GetCacheBulkStatistics(session);
 }
Example #3
0
        private static IEnumerable <(string cacheId, string cacheType, string qualifiedId, string id, long value)> GetLoggedStatistics(ICacheCoreSession session, out Dictionary <string, string> cacheNameMap)
        {
            cacheNameMap = null;
            var statistics = new List <(string, string, string, string, long)>();
            var maybeStats = session.GetStatisticsAsync().GetAwaiter().GetResult();

            if (maybeStats.Succeeded)
            {
                var stats = maybeStats.Result;

                cacheNameMap = BuildCacheNameMap(stats);
                foreach (var singleCacheStats in stats)
                {
                    foreach (KeyValuePair <string, double> statValue in singleCacheStats.Statistics)
                    {
                        // The *Sum2 stats are sum of squares of values and while stored as doubles can esaily explode a ulong, so they'll drop here.
                        // If those stats are wanted, they should be collected via an ETW based telemtry system.
                        if (!statValue.Key.EndsWith("Sum2", StringComparison.OrdinalIgnoreCase))
                        {
                            var value = Convert.ToInt64(statValue.Value);
                            statistics.Add((
                                               singleCacheStats.CacheId,
                                               singleCacheStats.CacheType,
                                               $"{singleCacheStats.CacheId}.{statValue.Key}",
                                               statValue.Key,
                                               value));
                        }
                    }
                }
            }

            return(statistics);
        }
Example #4
0
        internal static async Task <Possible <CacheCoreCacheInitializer> > TryInitializeCacheInternalAsync(
            LoggingContext loggingContext,
            PathTable pathTable,
            string cacheDirectory,
            ICacheConfiguration config,
            bool enableFingerprintLookup,
            RootTranslator rootTranslator)
        {
            Contract.Requires(pathTable != null);
            Contract.Requires(pathTable.IsValid);
            Contract.Requires(config != null);
            Contract.Requires(config.CacheLogFilePath.IsValid);
            Contract.Requires(config.CacheConfigFile.IsValid);
            Contract.Requires(!string.IsNullOrWhiteSpace(cacheDirectory));

            bool              succeeded = false;
            ICacheCoreCache   cache     = null;
            ICacheCoreSession session   = null;

            try
            {
                Possible <ICacheConfigData> cacheConfigData = TryGetCacheConfigData(pathTable, cacheDirectory, config, rootTranslator);
                if (!cacheConfigData.Succeeded)
                {
                    return(cacheConfigData.Failure);
                }

                Possible <ICacheCoreCache> maybeCache = await CacheFactory.InitializeCacheAsync(cacheConfigData.Result, loggingContext.ActivityId, config);

                if (!maybeCache.Succeeded)
                {
                    return(maybeCache.Failure);
                }

                // We are now responsible for shutting this down (even if something later fails).
                cache = maybeCache.Result;

                cache.SuscribeForCacheStateDegredationFailures(
                    failure => { Tracing.Logger.Log.CacheReportedRecoverableError(loggingContext, failure.DescribeIncludingInnerFailures()); });

                // Log the cache ID we got.
                Tracing.Logger.Log.CacheInitialized(loggingContext, cache.CacheId);

                Possible <ICacheCoreSession> maybeSession =
                    string.IsNullOrWhiteSpace(config.CacheSessionName)
                        ? await cache.CreateSessionAsync()
                        : await cache.CreateSessionAsync(config.CacheSessionName);

                if (!maybeSession.Succeeded)
                {
                    return(maybeSession.Failure);
                }

                session = maybeSession.Result;

                succeeded = true;
                return(new CacheCoreCacheInitializer(
                           loggingContext,
                           cache,
                           session,
                           new List <IDisposable>(),
                           enableFingerprintLookup: enableFingerprintLookup,
                           rootTranslator: rootTranslator,
                           replaceExistingFileOnMaterialization: config.ReplaceExistingFileOnMaterialization));
            }
            finally
            {
                if (!succeeded)
                {
                    // Note that we clean up in reverse order that we initialized things.
                    if (session != null)
                    {
                        Analysis.IgnoreResult(await session.CloseAsync(), justification: "Okay to ignore close");
                        Analysis.IgnoreResult(await cache.ShutdownAsync(), justification:  "Okay to ignore shutdown");
                    }
                }
            }
        }