public Task <Possible <string, Failure>[]> PinToCasAsync(CasEntries hashes, UrgencyHint urgencyHint, Guid activityId)
        {
            var callback = PinToCasMultipleAsyncCallback;

            if (callback != null)
            {
                return(callback(hashes, urgencyHint, activityId, m_realSession));
            }
            else
            {
                return(m_realSession.PinToCasAsync(hashes, urgencyHint, activityId));
            }
        }
Example #2
0
        /// <summary>
        /// Check the fake build via the session given
        /// </summary>
        /// <param name="session">Session to use for the check</param>
        /// <param name="index">The "index" CasHash</param>
        /// <param name="entries">The CasHash entries that should match the index</param>
        /// <param name="accessMethod">Method (File or stream) for how files are materialized from the cache</param>
        /// <returns>An Async Task</returns>
        public static async Task CheckContentsAsync(ICacheReadOnlySession session, CasHash index, CasEntries entries, CasAccessMethod accessMethod = CasAccessMethod.Stream)
        {
            string cacheId = await session.PinToCasAsync(index, CancellationToken.None).SuccessAsync("Cannot pin entry {0} to cache {1}", index.ToString(), session.CacheId);

            string[] expected = (await GetStreamAsync(index, accessMethod, session)).Success().Stream.AsString().Split(s_splitLines, StringSplitOptions.RemoveEmptyEntries);

            XAssert.AreEqual(expected.Length, entries.Count, "Counts did not match from cache {0}: {1} != {2}", cacheId, expected.Length, entries.Count);

            for (int i = 0; i < expected.Length; i++)
            {
                string casCacheId = await session.PinToCasAsync(entries[i], CancellationToken.None).SuccessAsync();

                string entry = (await GetStreamAsync(entries[i], accessMethod, session)).Success().Stream.AsString();

                XAssert.AreEqual(expected[i], entry, "CasEntry {0} mismatch from cache {1}:  [{2}] != [{3}]", i, casCacheId, expected[i], entry);
            }
        }
        public async Task <Possible <string, Failure>[]> PinToCasAsync(CasEntries hashes, UrgencyHint urgencyHint, Guid activityId)
        {
            using (var eventing = new PinToCasMultipleActivity(CompositingCache.EventSource, activityId, this))
            {
                eventing.Start(hashes, urgencyHint);

                var results = await m_casSession.PinToCasAsync(hashes, urgencyHint, eventing.Id);

                for (int i = 0; i < results.Length; i++)
                {
                    if (results[i].Succeeded)
                    {
                        PinnedToCas.TryAdd(hashes[i], 0);
                    }
                }

                return(eventing.Returns(results));
            }
        }
        /// <summary>
        /// Attempts to pin the specified CasHash. Returns true if the
        /// pinning succeeds.
        /// </summary>
        /// <param name="casHash">CasHash value to attempt to pin</param>
        private async Task <bool> AttemptToPinAsync(CasHash casHash)
        {
            if (casHash.Equals(CasHash.NoItem))
            {
                return(true);
            }

            Possible <string, Failure> pinAttempt = await m_readOnlySession.PinToCasAsync(casHash);

            return(pinAttempt.Succeeded);
        }
Example #5
0
        private async Task <long> PinAndGetStreamSize(ICacheReadOnlySession session, CasHash hash)
        {
            long result;

            await session.PinToCasAsync(hash, CancellationToken.None).SuccessAsync();

            using (var stream = await session.GetStreamAsync(hash).SuccessAsync())
            {
                result = stream.Length;
            }

            return(result);
        }
Example #6
0
        internal static async Task <Tuple <CasHash, long> > GetContentSizeAsync(this ICacheReadOnlySession session, CasHash casHash)
        {
            var possibleString = await session.PinToCasAsync(casHash);

            if (!possibleString.Succeeded)
            {
                return(new Tuple <CasHash, long>(casHash, (long)ContentError.UnableToPin));
            }

            var possibleStream = await session.GetStreamAsync(casHash);

            if (!possibleStream.Succeeded)
            {
                return(new Tuple <CasHash, long>(casHash, (long)ContentError.UnableToStream));
            }

            long length = possibleStream.Result.Length;

            possibleStream.Result.Dispose();
            return(new Tuple <CasHash, long>(casHash, length));
        }
 public Task <Possible <string, Failure>[]> PinToCasAsync(CasEntries hashes, UrgencyHint urgencyHint, Guid activityId)
 {
     return(m_session.PinToCasAsync(hashes, urgencyHint, activityId));
 }
Example #8
0
        /// <summary>
        /// Takes a strong fingerprint and returns the deserialized contents of
        /// the input assertion list file that corresponds to it
        /// </summary>
        /// <param name="sfp">The strong fingerprint to get the input assertion
        /// list file contents for</param>
        /// <param name="cacheErrors">Any cache errors that are found will be
        /// added to this collection</param>
        /// <returns>Deserialized contents of the input assertion list file
        /// corresponding to the specified strong fingerprint</returns>
        private async Task <string> GetInputAssertionListFileContentsAsync(StrongFingerprint sfp, ConcurrentDictionary <CacheError, int> cacheErrors)
        {
            // Check for the NoItem
            if (sfp.CasElement.Equals(CasHash.NoItem))
            {
                return(string.Empty);
            }

            // Pin the input assertion list file
            Possible <string, Failure> possibleString = await m_readOnlySession.PinToCasAsync(sfp.CasElement).ConfigureAwait(false);

            if (!possibleString.Succeeded)
            {
                return(string.Empty);
            }

            // Get the stream for the input assertion list file
            Possible <Stream, Failure> possibleStream = await m_readOnlySession.GetStreamAsync(sfp.CasElement).ConfigureAwait(false);

            if (!possibleStream.Succeeded)
            {
                cacheErrors.TryAdd(
                    new CacheError(
                        CacheErrorType.CasHashError,
                        "The input assertion list for SFP " + sfp.ToString() + " was not found in CAS"), 0);
                return(string.Empty);
            }

            // Read the stream contents while hashing
            return(await Task.Run(() =>
            {
                using (var hasher = ContentHashingUtilities.HashInfo.CreateContentHasher())
                {
                    using (var hashingStream = hasher.CreateReadHashingStream(possibleStream.Result))
                    {
                        using (var reader = new BuildXLReader(false, hashingStream, false))
                        {
                            var maybePathSet = ObservedPathSet.TryDeserialize(s_pathTable, reader);

                            // Check if deserialization was successful
                            if (!maybePathSet.Succeeded)
                            {
                                // Deserialization failed
                                cacheErrors.TryAdd(
                                    new CacheError(
                                        CacheErrorType.CasHashError,
                                        "The input assertion list for SFP " + sfp.ToString() + " could not be deserialized"), 0);
                                return string.Empty;
                            }

                            CasHash newCasHash = new CasHash(hashingStream.GetContentHash());

                            // Check if the hashes match
                            if (!sfp.CasElement.Equals(newCasHash))
                            {
                                cacheErrors.TryAdd(
                                    new CacheError(
                                        CacheErrorType.CasHashError,
                                        "The input assertion list for SFP " + sfp.ToString() + " has been altered in the CAS"), 0);
                                return string.Empty;
                            }

                            // Deserialization was successful and file was unaltered
                            StringBuilder fileContents = new StringBuilder();
                            foreach (ObservedPathEntry entry in maybePathSet.Result.Paths)
                            {
                                fileContents.Append(entry.Path.ToString(s_pathTable)).Append(Environment.NewLine);
                            }

                            return fileContents.ToString();
                        }
                    }
                }
            }).ConfigureAwait(false));
        }