Esempio n. 1
0
 internal static IEnumerable <Task <Possible <CasEntries, Failure> > > GetCacheEntries(this ICacheReadOnlySession session, IEnumerable <StrongFingerprint> strongFingerprints)
 {
     foreach (var strongFingerprint in strongFingerprints)
     {
         yield return(session.GetCacheEntryAsync(strongFingerprint));
     }
 }
Esempio n. 2
0
        private async Task ValidateSessionAsync(HashSet <FullCacheRecord> expectedRecords, ICache cache, string sessionId, FakeBuild.CasAccessMethod accessMethod)
        {
            if (!ImplementsTrackedSessions || DummySessionName != null)
            {
                return;
            }

            ICacheReadOnlySession readOnlySession = (await cache.CreateReadOnlySessionAsync()).Success();

            // Check that the content is fine
            HashSet <FullCacheRecord> foundRecords = new HashSet <FullCacheRecord>();

            foreach (var strongFingerprintTask in cache.EnumerateSessionStrongFingerprints(DummySessionName ?? sessionId).Success().OutOfOrderTasks())
            {
                StrongFingerprint strongFingerprint = await strongFingerprintTask;
                CasEntries        casEntries        = (await readOnlySession.GetCacheEntryAsync(strongFingerprint)).Success();
                FullCacheRecord   record            = new FullCacheRecord(strongFingerprint, casEntries);
                XAssert.IsTrue(expectedRecords.Contains(record), "Found record that was not expected!");
                foundRecords.Add(record);
            }

            (await readOnlySession.CloseAsync()).Success();

            await FakeBuild.CheckContentsAsync(cache, foundRecords, accessMethod);

            XAssert.AreEqual(expectedRecords.Count, foundRecords.Count);
        }
        public async Task <Possible <CasEntries, Failure> > GetCacheEntryAsync(StrongFingerprint strong, UrgencyHint urgencyHint, Guid activityId)
        {
            using (var eventing = new GetCacheEntryActivity(CompositingCache.EventSource, activityId, this))
            {
                eventing.Start(strong, urgencyHint);

                return(eventing.Returns(await m_metadataSession.GetCacheEntryAsync(strong, urgencyHint, eventing.Id)));
            }
        }
        public Task <Possible <CasEntries, Failure> > GetCacheEntryAsync(StrongFingerprint strong, UrgencyHint urgencyHint, Guid activityId)
        {
            var callback = GetCacheEntryAsyncCallback;

            if (callback != null)
            {
                return(callback(strong, urgencyHint, activityId, m_realSession));
            }
            else
            {
                return(m_realSession.GetCacheEntryAsync(strong, urgencyHint, activityId));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Asynchronously loads all of the cache entries associated with the
        /// given strong fingerprints
        /// </summary>
        /// <param name="strongFingerprintTasks">
        /// Strong fingerprints of the cache entries to load
        /// </param>
        /// <param name="errors">
        /// Where any cache errors found get stored
        /// </param>
        /// <param name="weakFingerprintsFound">
        /// If not null, all weak fingerpints found will be added
        /// </param>
        private IEnumerable <Task> LoadCacheEntriesAsync(
            IEnumerable <Task <StrongFingerprint> > strongFingerprintTasks,
            ConcurrentDictionary <CacheError, int> errors,
            ConcurrentDictionary <WeakFingerprintHash, byte> weakFingerprintsFound)
        {
            foreach (Task <StrongFingerprint> strongFingerprintTask in strongFingerprintTasks)
            {
                yield return(Task.Run(async() =>
                {
                    StrongFingerprint strongFingerprint = await strongFingerprintTask.ConfigureAwait(false);
                    if (strongFingerprint == null)
                    {
                        errors.TryAdd(new CacheError(CacheErrorType.SessionError, "Null StrongFingerprint"), 0);
                        return;
                    }

                    Func <StrongFingerprint, Task <Possible <CasEntries, Failure> > > delegateToUse;
                    if (weakFingerprintsFound == null)
                    {
                        delegateToUse = (sfp) => m_readOnlySession.GetCacheEntryAsync(sfp);
                    }
                    else
                    {
                        delegateToUse = (sfp) =>
                        {
                            weakFingerprintsFound.TryAdd(sfp.WeakFingerprint, 0);
                            return m_readOnlySession.GetCacheEntryAsync(sfp);
                        };
                    }

                    // Only check StrongFingerprint if never seen before
                    Analysis.IgnoreResult(
                        await AllFullCacheRecords.GetOrAdd(strongFingerprint, delegateToUse).ConfigureAwait(false)
                        );
                }));
            }
        }
Esempio n. 6
0
        public async Task SimpleDummySession()
        {
            const string TestName    = "SimpleSession";
            string       testCacheId = MakeCacheId(TestName);
            ICache       cache       = await CreateCacheAsync(testCacheId);

            // Now for the session (which we base on the cache ID)
            string testSessionId = "Session1-" + testCacheId;

            ICacheSession session = await CreateSessionAsync(cache, testSessionId);

            // Do the default fake build for this test (first time, no cache hit)
            FullCacheRecord built = await FakeBuild.DoPipAsync(session, TestName);

            XAssert.AreEqual(FakeBuild.NewRecordCacheId, built.CacheId, "Should have been a new cache entry!");

            // Now we see if we can get back the items we think we should
            await CloseSessionAsync(session, testSessionId);

            // We need a read only session to get the CasEntries
            ICacheReadOnlySession readOnlySession = (await cache.CreateReadOnlySessionAsync()).Success();

            // Validate that the cache contains a dummy session and it has the one cache record it needs.
            HashSet <FullCacheRecord> found = new HashSet <FullCacheRecord>();

            foreach (var strongFingerprintTask in cache.EnumerateSessionStrongFingerprints(MemoizationStoreAdapterCache.DummySessionName).Success().OutOfOrderTasks())
            {
                StrongFingerprint strongFingerprint = await strongFingerprintTask;
                CasEntries        casEntries        = (await readOnlySession.GetCacheEntryAsync(strongFingerprint)).Success();
                FullCacheRecord   record            = new FullCacheRecord(strongFingerprint, casEntries);

                // If it is not the record we already found...
                if (!found.Contains(record))
                {
                    found.Add(record);
                }

                XAssert.AreEqual(1, found.Count, "There should be only 1 unique record in the session");

                XAssert.AreEqual(built.StrongFingerprint.WeakFingerprint, record.StrongFingerprint.WeakFingerprint);
                XAssert.AreEqual(built.StrongFingerprint.CasElement, record.StrongFingerprint.CasElement);
                XAssert.AreEqual(built.StrongFingerprint.HashElement, record.StrongFingerprint.HashElement);
                XAssert.AreEqual(built.CasEntries.Count, record.CasEntries.Count, "Did not return the same number of items");
                XAssert.IsTrue(record.CasEntries.Equals(built.CasEntries), "Items returned are not the same hash and/or order order");

                XAssert.AreEqual(built, record);

                // We can not check record.CasEntries.IsDeterministic
                // as the cache may have determined that they are deterministic
                // via cache determinism recovery.
            }

            XAssert.AreEqual(1, found.Count, "There should be 1 and only 1 record in the session!");

            await readOnlySession.CloseAsync().SuccessAsync();

            // Check that the cache has the items in it
            await FakeBuild.CheckContentsAsync(cache, built);

            // Now redo the "build" with a cache hit
            testSessionId = "Session2-" + testCacheId;
            session       = await CreateSessionAsync(cache, testSessionId);

            FullCacheRecord rebuilt = await FakeBuild.DoPipAsync(session, TestName);

            XAssert.AreEqual(built, rebuilt, "Should have been the same build!");

            // We make sure we did get it from a cache rather than a manual rebuild.
            XAssert.AreNotEqual(built.CacheId, rebuilt.CacheId, "Should not be the same cache ID");

            await CloseSessionAsync(session, testSessionId);

            readOnlySession = await cache.CreateReadOnlySessionAsync().SuccessAsync();

            // Now that we have done the second build via a cache hit, it should produce the
            // same cache record as before
            foreach (var strongFingerprintTask in cache.EnumerateSessionStrongFingerprints(MemoizationStoreAdapterCache.DummySessionName).Success().OutOfOrderTasks())
            {
                StrongFingerprint strongFingerprint = await strongFingerprintTask;
                CasEntries        casEntries        = (await readOnlySession.GetCacheEntryAsync(strongFingerprint)).Success();
                FullCacheRecord   record            = new FullCacheRecord(strongFingerprint, casEntries);

                XAssert.IsTrue(found.Contains(record), "Second session should produce the same cache record but did not!");
            }

            (await readOnlySession.CloseAsync()).Success();

            await ShutdownCacheAsync(cache, testCacheId);
        }
 public Task <Possible <CasEntries, Failure> > GetCacheEntryAsync(StrongFingerprint strong, UrgencyHint urgencyHint, Guid activityId)
 {
     return(m_session.GetCacheEntryAsync(strong, urgencyHint, activityId));
 }