/// <summary> /// Initializes a new instance of the <see cref="PinContext" /> class. /// </summary> public PinContext(BackgroundTaskTracker taskTracker, Func <IEnumerable <KeyValuePair <ContentHash, int> >, Task> unpinAsync) { Contract.Requires(unpinAsync != null); Contract.Requires(taskTracker != null); _unpinAsync = unpinAsync; _taskTracker = taskTracker; }
/// <summary> /// Initializes a new instance of the <see cref="PinContext" /> class. /// </summary> public PinContext(BackgroundTaskTracker taskTracker, Action <IEnumerable <KeyValuePair <ContentHash, int> > > unpinAction) { Contract.Requires(unpinAction != null); Contract.Requires(taskTracker != null); _unpinAction = unpinAction; _taskTracker = taskTracker; }
/// <inheritdoc /> public Task <BoolResult> StartupAsync(Context context) { StartupStarted = true; return(StartupCall <MemoizationStoreTracer> .RunAsync(Tracer.MemoizationStoreTracer, context, async() => { BoolResult result; var backingContentSessionTask = Task.Run(async() => await BackingContentSession.StartupAsync(context).ConfigureAwait(false)); var writeThroughContentSessionResult = WriteThroughContentSession != null ? await WriteThroughContentSession.StartupAsync(context).ConfigureAwait(false) : BoolResult.Success; var backingContentSessionResult = await backingContentSessionTask.ConfigureAwait(false); if (backingContentSessionResult.Succeeded && writeThroughContentSessionResult.Succeeded) { _taskTracker = new BackgroundTaskTracker(Component, new Context(context)); result = BoolResult.Success; } else { var sb = new StringBuilder(); if (backingContentSessionResult.Succeeded) { var r = await BackingContentSession.ShutdownAsync(context).ConfigureAwait(false); if (!r.Succeeded) { sb.Append($"Backing content session shutdown failed, error=[{r}]"); } } else { sb.Append($"Backing content session startup failed, error=[{backingContentSessionResult}]"); } if (writeThroughContentSessionResult.Succeeded) { var r = WriteThroughContentSession != null ? await WriteThroughContentSession.ShutdownAsync(context).ConfigureAwait(false) : BoolResult.Success; if (!r.Succeeded) { sb.Append(sb.Length > 0 ? ", " : string.Empty); sb.Append($"Write-through content session shutdown failed, error=[{r}]"); } } else { sb.Append(sb.Length > 0 ? ", " : string.Empty); sb.Append($"Write-through content session startup failed, error=[{writeThroughContentSessionResult}]"); } result = new BoolResult(sb.ToString()); } StartupCompleted = true; return result; })); }
/// <inheritdoc /> protected override async Task <BoolResult> StartupCoreAsync(OperationContext context) { _taskTracker = new BackgroundTaskTracker(nameof(RedisMetadataCache), new Context(context)); var redisDatabaseAdapter = new RedisDatabaseAdapter(await RedisDatabaseFactory.CreateAsync(context, ConnectionStringProvider), Keyspace); _dbAdapter = redisDatabaseAdapter; _stringDatabaseAdapter = redisDatabaseAdapter; return(BoolResult.Success); }
/// <inheritdoc /> protected override async Task <BoolResult> StartupCoreAsync(OperationContext context) { _taskTracker = new BackgroundTaskTracker(nameof(RedisMetadataCache), context.CreateNested(nameof(RedisMetadataCache))); var redisDatabaseAdapter = new RedisDatabaseAdapter(await RedisDatabaseFactory.CreateAsync(context, ConnectionStringProvider, logSeverity: BuildXL.Cache.ContentStore.Interfaces.Logging.Severity.Unknown, usePreventThreadTheft: false), Keyspace); _dbAdapter = redisDatabaseAdapter; _stringDatabaseAdapter = redisDatabaseAdapter; return(BoolResult.Success); }
public OneLevelCacheTests() : base(() => new MemoryFileSystem(TestSystemClock.Instance), TestGlobal.Logger) { _taskTracker = new BackgroundTaskTracker(nameof(OneLevelCacheTests), new Context(Logger)); _backgroundContext = new Context(Logger); _mockContentSession = new TestContentSession(); _mockContentStore = new TestContentStore(_mockContentSession); _mockMemoizationSession = new TestMemoizationSession(); _mockMemoizationStore = new TestMemoizationStore(_mockMemoizationSession); }
public void TestStackedActions() { // Start tracked task A using (BackgroundTaskTracker.BeginTask("TestTaskA")) { // Start tracked task B using (BackgroundTaskTracker.BeginTask("TestTaskB")) { // Should reflect action pending Assert.IsTrue(BackgroundTaskTracker.Instance.ActionsPending); // Should reflect 0 completion Assert.AreEqual(BackgroundTaskTracker.Instance.CompletionProgress, 0); // Should reflect task name as decription Assert.AreEqual(BackgroundTaskTracker.Instance.CurrentActionDescription, "TestTaskB"); // Start tracked task C using (BackgroundTaskTracker.BeginTask("TestTaskC")) { // Should reflect action pending Assert.IsTrue(BackgroundTaskTracker.Instance.ActionsPending); // Should reflect 0 completion Assert.AreEqual(BackgroundTaskTracker.Instance.CompletionProgress, 0); // Should reflect task name as decription Assert.AreEqual(BackgroundTaskTracker.Instance.CurrentActionDescription, "TestTaskC"); } // Complete C // Should reflect 33% completion Assert.AreEqual(BackgroundTaskTracker.Instance.CompletionProgress, 33); // Should reflect task name B as decription Assert.AreEqual(BackgroundTaskTracker.Instance.CurrentActionDescription, "TestTaskB"); } // Complete B // Should reflect 66% completion Assert.AreEqual(BackgroundTaskTracker.Instance.CompletionProgress, 66); // Should reflect task name B as decription Assert.AreEqual(BackgroundTaskTracker.Instance.CurrentActionDescription, "TestTaskA"); } // Tasks now complete // Verify no action state VerifyNoActionState(); }
/// <inheritdoc /> protected override async Task <BoolResult> StartupCoreAsync(OperationContext context) { foreach (var quotaRule in _rules.Where(r => r.CanBeCalibrated)) { await CalibrateAsync(context, quotaRule); } _reserveTask = Task.Run(() => Reserve(new Context(context))); _taskTracker = new BackgroundTaskTracker(Component, new Context(context)); // Start purging immediately on startup to clear out residual content in the cache // over the cache quota StartPurging(); return(BoolResult.Success); }
public void TestSingleAction() { // Start a tracked task using (BackgroundTaskTracker.BeginTask("TestTaskA")) { // Should reflect action pending Assert.IsTrue(BackgroundTaskTracker.Instance.ActionsPending); // Should reflect 0 completion Assert.AreEqual(BackgroundTaskTracker.Instance.CompletionProgress, 0); // Should reflect task name as decription Assert.AreEqual(BackgroundTaskTracker.Instance.CurrentActionDescription, "TestTaskA"); } // Task now complete // Verify no action state VerifyNoActionState(); }
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); } } }
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); }
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 /> protected override async Task <BoolResult> StartupCoreAsync(OperationContext context) { foreach (var quotaRule in _rules.Where(r => r.CanBeCalibrated)) { await CalibrateAsync(context, quotaRule); } _reserveTask = Task.Run(() => Reserve(new Context(context))); _taskTracker = new BackgroundTaskTracker(Component, new Context(context)); if (PurgeAtStartup) { // Start purging immediately on startup to clear out residual content in the cache // over the cache quota if configured. StartPurging(); } else { _tracer.Debug(context, $"{_tracer.Name}: do not purge at startup based on configuration settings."); } return(BoolResult.Success); }