/// <inheritdoc />
        public Task <BoolResult> ShutdownAsync(Context context)
        {
            ShutdownStarted = true;
            return(ShutdownCall <BuildCacheCacheTracer> .RunAsync(_tracer, context, async() =>
            {
                var statsResult = await GetStatsInternalAsync(context).ConfigureAwait(false);
                if (statsResult.Succeeded)
                {
                    _tracer.TraceStatisticsAtShutdown(context, statsResult.CounterSet, prefix: "BuildCacheCacheStats");
                }
                else
                {
                    _tracer.Debug(context, $"Getting stats failed: [{statsResult}]");
                }

                var backingContentStoreTask = Task.Run(async() => await _backingContentStore.ShutdownAsync(context).ConfigureAwait(false));
                var writeThroughContentStoreResult = _writeThroughContentStore != null
                    ? await _writeThroughContentStore.ShutdownAsync(context)
                    : BoolResult.Success;
                var backingContentStoreResult = await backingContentStoreTask.ConfigureAwait(false);

                BoolResult result;
                if (backingContentStoreResult.Succeeded && writeThroughContentStoreResult.Succeeded)
                {
                    result = BoolResult.Success;
                }
                else
                {
                    var sb = new StringBuilder();
                    if (!backingContentStoreResult.Succeeded)
                    {
                        sb.Append($"Backing content store shutdown failed, error=[{backingContentStoreResult}]");
                    }

                    if (!writeThroughContentStoreResult.Succeeded)
                    {
                        sb.Append($"Write-through content store shutdown failed, error=[{writeThroughContentStoreResult}]");
                    }

                    result = new BoolResult(sb.ToString());
                }

                ShutdownCompleted = true;
                return result;
            }));
        }
        /// <inheritdoc />
        public Task <BoolResult> StartupAsync(Context context)
        {
            StartupStarted = true;
            return(StartupCall <BuildCacheCacheTracer> .RunAsync(_tracer, context, async() =>
            {
                BoolResult result;

                _tracer.Debug(context, $"Creating ContentHashListAdapterFactory with {nameof(_useBlobContentHashLists)}={_useBlobContentHashLists}");
                _contentHashListAdapterFactory = await ContentHashListAdapterFactory.CreateAsync(
                    context, _buildCacheHttpClientFactory, _useBlobContentHashLists);
                Id =
                    await _contentHashListAdapterFactory.BuildCacheHttpClient.GetBuildCacheServiceDeterminism(_cacheNamespace)
                    .ConfigureAwait(false);

                var backingContentStoreTask = Task.Run(async() => await _backingContentStore.StartupAsync(context).ConfigureAwait(false));
                var writeThroughContentStoreResult = _writeThroughContentStore != null
                    ? await _writeThroughContentStore.StartupAsync(context).ConfigureAwait(false)
                    : BoolResult.Success;
                var backingContentStoreResult = await backingContentStoreTask.ConfigureAwait(false);

                if (backingContentStoreResult.Succeeded && writeThroughContentStoreResult.Succeeded)
                {
                    result = BoolResult.Success;
                }
                else
                {
                    var sb = new StringBuilder();
                    if (backingContentStoreResult.Succeeded)
                    {
                        var r = await _backingContentStore.ShutdownAsync(context).ConfigureAwait(false);
                        if (!r.Succeeded)
                        {
                            sb.Append($"Backing content store shutdown failed, error=[{r}]");
                        }
                    }
                    else
                    {
                        sb.Append($"Backing content store startup failed, error=[{backingContentStoreResult}]");
                    }

                    if (writeThroughContentStoreResult.Succeeded)
                    {
                        var r = _writeThroughContentStore != null
                            ? await _writeThroughContentStore.ShutdownAsync(context).ConfigureAwait(false)
                            : BoolResult.Success;
                        if (!r.Succeeded)
                        {
                            sb.Append(sb.Length > 0 ? ", " : string.Empty);
                            sb.Append($"Write-through content store shutdown failed, error=[{r}]");
                        }
                    }
                    else
                    {
                        sb.Append(sb.Length > 0 ? ", " : string.Empty);
                        sb.Append($"Write-through content store startup failed, error=[{writeThroughContentStoreResult}]");
                    }

                    result = new BoolResult(sb.ToString());
                }

                StartupCompleted = true;
                return result;
            }));
        }
        /// <inheritdoc />
        public Task <BoolResult> ShutdownAsync(Context context)
        {
            ShutdownStarted = true;
            return(ShutdownCall <MemoizationStoreTracer> .RunAsync(Tracer.MemoizationStoreTracer, context, async() =>
            {
                Tracer.Debug(context, "IncorporateOnShutdown start");
                Tracer.Debug(context, $"Incorporate fingerprints feature enabled:[{_fingerprintIncorporationEnabled}]");
                Tracer.Debug(context, $"Total fingerprints to be incorporated:[{FingerprintTracker.Count}]");
                Tracer.Debug(context, $"Max fingerprints per incorporate request(=chunk size):[{_maxFingerprintsPerIncorporateRequest}]");
                Tracer.Debug(context, $"Max incorporate requests allowed in parallel:[{_maxDegreeOfParallelismForIncorporateRequests}]");
                if (_fingerprintIncorporationEnabled)
                {
                    // Incorporating all of the fingerprints for a build, in one request, to a single endpoint causes pain. Incorporation involves
                    // extending the lifetime of all fingerprints *and* content/s mapped to each fingerprint. Processing a large request payload
                    // results in, potentially, fanning out a massive number of "lifetime extend" requests to itemstore and blobstore, which can
                    // bring down the endpoint. Break this down into chunks so that multiple, load-balanced endpoints can share the burden.
                    List <StrongFingerprint> fingerprintsToBump = FingerprintTracker.StaleFingerprints.ToList();
                    Tracer.Debug(context, $"Total fingerprints to be sent in incorporation requeststo the service: {fingerprintsToBump.Count}");

                    List <List <StrongFingerprintAndExpiration> > chunks = fingerprintsToBump.Select(
                        strongFingerprint => new StrongFingerprintAndExpiration(strongFingerprint, FingerprintTracker.GenerateNewExpiration())
                        ).GetPages(_maxFingerprintsPerIncorporateRequest).ToList();
                    Tracer.Debug(context, $"Total fingerprint incorporation requests to be issued(=number of fingerprint chunks):[{chunks.Count}]");

                    var incorporateBlock = new ActionBlock <IEnumerable <StrongFingerprintAndExpiration> >(
                        async chunk =>
                    {
                        await ContentHashListAdapter.IncorporateStrongFingerprints(
                            context,
                            CacheNamespace,
                            new IncorporateStrongFingerprintsRequest(chunk.ToList().AsReadOnly())
                            ).ConfigureAwait(false);
                    },
                        new ExecutionDataflowBlockOptions {
                        MaxDegreeOfParallelism = _maxDegreeOfParallelismForIncorporateRequests
                    });

                    foreach (var chunk in chunks)
                    {
                        await incorporateBlock.SendAsync(chunk);
                    }

                    incorporateBlock.Complete();
                    await incorporateBlock.Completion.ConfigureAwait(false); // TODO: Gracefully handle exceptions so that the rest of shutdown can happen (bug 1365340)
                    Tracer.Debug(context, "IncorporateOnShutdown stop");
                }

                if (_taskTracker != null)
                {
                    await _taskTracker.Synchronize().ConfigureAwait(false);
                    await _taskTracker.ShutdownAsync(context).ConfigureAwait(false);
                }

                var backingContentSessionTask = Task.Run(async() => await BackingContentSession.ShutdownAsync(context).ConfigureAwait(false));
                var writeThroughContentSessionResult = WriteThroughContentSession != null
                    ? await WriteThroughContentSession.ShutdownAsync(context).ConfigureAwait(false)
                    : BoolResult.Success;
                var backingContentSessionResult = await backingContentSessionTask.ConfigureAwait(false);

                BoolResult result;
                if (backingContentSessionResult.Succeeded && writeThroughContentSessionResult.Succeeded)
                {
                    if (_sealingErrorsToPrintOnShutdown.Any())
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine("Error(s) during background sealing:");
                        foreach (var sealingError in _sealingErrorsToPrintOnShutdown)
                        {
                            sb.AppendLine($"[{sealingError}]");
                        }

                        if (_sealingErrorCount > MaxSealingErrorsToPrintOnShutdown)
                        {
                            sb.AppendLine($"See log for the other {MaxSealingErrorsToPrintOnShutdown - _sealingErrorCount} error(s).");
                        }

                        result = new BoolResult(sb.ToString());
                    }
                    else
                    {
                        result = BoolResult.Success;
                    }
                }
                else
                {
                    var sb = new StringBuilder();
                    if (!backingContentSessionResult.Succeeded)
                    {
                        sb.Append($"Backing content session shutdown failed, error=[{backingContentSessionResult}]");
                    }

                    if (!writeThroughContentSessionResult.Succeeded)
                    {
                        sb.Append(sb.Length > 0 ? ", " : string.Empty);
                        sb.Append($"Write-through content session shutdown failed, error=[{writeThroughContentSessionResult}]");
                    }

                    result = new BoolResult(sb.ToString());
                }

                ShutdownCompleted = true;
                return result;
            }));
        }