Exemple #1
0
 protected override void Dispose(bool disposing)
 {
     _taskTracker.Synchronize().GetAwaiter().GetResult();
     _taskTracker.ShutdownAsync(_backgroundContext).Wait();
     _taskTracker.Dispose();
     base.Dispose(disposing);
 }
Exemple #2
0
        /// <inheritdoc />
        protected override async Task <BoolResult> ShutdownCoreAsync(OperationContext context)
        {
            if (_taskTracker != null)
            {
                await _taskTracker.Synchronize();

                await _taskTracker.ShutdownAsync(context);
            }

            return(BoolResult.Success);
        }
Exemple #3
0
        public async Task AddPinThrowsOnDisposedPinContext()
        {
            var context = new Context(Logger);

            using (var taskTracker = new BackgroundTaskTracker(nameof(PinContextTests), context))
            {
                try
                {
                    var pinContext = new PinContext(taskTracker, contentHashes => { });
                    pinContext.AddPin(ContentHash.Random());
                    await pinContext.DisposeAsync();

                    Action addPinAction = () => pinContext.AddPin(ContentHash.Random());
                    addPinAction.Should().Throw <ObjectDisposedException>();
                }
                finally
                {
                    await taskTracker.ShutdownAsync(context);
                }
            }
        }
Exemple #4
0
        public async Task DisposeCallsUnpinHashesAction()
        {
            var calledOnDispose = false;
            var context         = new Context(Logger);

            using (var taskTracker = new BackgroundTaskTracker(nameof(PinContextTests), context))
            {
                try
                {
                    using (new PinContext(taskTracker, contentHashes => { calledOnDispose = true; }))
                    {
                    }
                }
                finally
                {
                    await taskTracker.ShutdownAsync(context);
                }
            }

            Assert.True(calledOnDispose);
        }
Exemple #5
0
        public async Task AddPinAccumulatesDuplicates()
        {
            var context = new Context(Logger);

            using (var taskTracker = new BackgroundTaskTracker(nameof(PinContextTests), context))
            {
                try
                {
                    ContentHash hash1      = ContentHash.Random();
                    ContentHash hash2      = ContentHash.Random();
                    var         hashCounts = new ConcurrentDictionary <ContentHash, int>
                    {
                        [hash1] = 0,
                        [hash2] = 0
                    };

                    using (var pinContext = new PinContext(taskTracker, pinCounts =>
                    {
                        foreach (var pinCount in pinCounts)
                        {
                            hashCounts[pinCount.Key] = pinCount.Value;
                        }
                    }))
                    {
                        pinContext.AddPin(hash2);
                        pinContext.AddPin(hash1);
                        pinContext.AddPin(hash2);
                    }

                    hashCounts[hash1].Should().Be(1);
                    hashCounts[hash2].Should().Be(2);
                }
                finally
                {
                    await taskTracker.ShutdownAsync(context);
                }
            }
        }
        /// <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;
            }));
        }