public void LeaderAbortingCreatesNewLeader() { const int ConcurrentMutexes = 5; var settings = new BlobSettings(CloudStorageAccount.DevelopmentStorageAccount, "leases", "LeaderAbortingCreatesNewLeader"); var firstCts = new CancellationTokenSource(); var firstMutexAcquired = new TaskCompletionSource <bool>(); var firstLeader = new BlobDistributedMutex(settings, SignalAndWait(firstMutexAcquired)); firstLeader.RunTaskWhenMutexAcquired(firstCts.Token); Assert.IsTrue(firstMutexAcquired.Task.Wait(TimeSpan.FromSeconds(5))); var mutexAcquired = Enumerable.Range(0, ConcurrentMutexes).Select(_ => new TaskCompletionSource <bool>()).ToArray(); var mutexes = mutexAcquired.Select(completed => new BlobDistributedMutex(settings, SignalAndWait(completed))).ToArray(); var cts = new CancellationTokenSource(); foreach (var mutex in mutexes) { mutex.RunTaskWhenMutexAcquired(cts.Token); } firstCts.Cancel(); Task.WaitAny(mutexAcquired.Select(x => (Task)x.Task).ToArray(), TimeSpan.FromSeconds(80)); cts.Cancel(); Assert.AreEqual(1, mutexAcquired.Count(x => x.Task.IsCompleted)); }
public FileManager(IOptions <BlobSettings> blobSettings) { _blobSettings = blobSettings.Value; storageCredentials = new StorageCredentials(_blobSettings.accountName, _blobSettings.keyValue); storageAccount = new CloudStorageAccount(storageCredentials, true); blobClient = storageAccount.CreateCloudBlobClient(); container = blobClient.GetContainerReference(_blobSettings.containerName); }
public override void Run() { var settings = new BlobSettings(CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Storage")), "leases", "MyLeaderCoordinatorTask"); var mutex = new BlobDistributedMutex(settings, MyLeaderCoordinatorTask); mutex.RunTaskWhenMutexAcquired(this.cts.Token).Wait(); Trace.TraceInformation("Returning from WorkerRole Run and signaling runCompletedEvent"); this.runCompletedEvent.Set(); }
public FileStorageService(IUnityContainer container, ISaphirCloudBoxDataContextManager dataContextManager, BlobSettings blobSettings, AzureBlobClient azureBlobClient, IPermissionHelper permissionHelper, IUserService userService) : base(container, dataContextManager) { _userService = userService ?? throw new ArgumentNullException(nameof(userService)); _blobSettings = blobSettings ?? throw new ArgumentNullException(nameof(blobSettings)); _azureBlobClient = azureBlobClient ?? throw new ArgumentNullException(nameof(azureBlobClient)); _permissionHelper = permissionHelper ?? throw new ArgumentNullException(nameof(permissionHelper)); }
public async Task when_the_leader_aborts_another_node_can_become_leader() { var settings = new BlobSettings(CloudStorageAccount.DevelopmentStorageAccount, "integration", "LeaderAbortingCreatesNewLeader"); var tokenSource = new CancellationTokenSource(); var firstLeader = new AzureBlobLeaseManager(settings, TimeSpan.FromSeconds(15)); var secondLeader = new AzureBlobLeaseManager(settings, TimeSpan.FromSeconds(15)); Assert.True(await firstLeader.AcquireLease(new LeaseOptions(nameof(firstLeader)), tokenSource.Token)); Assert.False(await secondLeader.AcquireLease(new LeaseOptions(nameof(secondLeader)), tokenSource.Token)); await firstLeader.ReleaseLease(new LeaseReleaseOptions(nameof(firstLeader))); Assert.True(await secondLeader.AcquireLease(new LeaseOptions(nameof(secondLeader)), tokenSource.Token)); }
protected override async Task DoRunAsync(CancellationToken stoppingToken) { var connectionString = configuration["Data:Azure:Storage:ConnectionString"]; if (string.IsNullOrWhiteSpace(connectionString)) { return; } var settings = new BlobSettings(connectionString, "mutex", configuration["General:ServiceName"]); var mutex = new BlobDistributedMutex(settings, dispatcher.RunDispatcherAsync, logger); await mutex.RunTaskWhenMutexAcquired(stoppingToken); }
static void Main(string[] args) { var rc = HostFactory.Run(x => { x.Service <Service>(sc => { sc.WhenStartedAsLeader(b => { b.Lease(lcb => { lcb.RenewLeaseEvery(TimeSpan.FromSeconds(30)); lcb.AquireLeaseEvery(TimeSpan.FromMinutes(1)); lcb.LeaseLength(TimeSpan.FromSeconds(15)); var cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["CloudStorageAccount"].ConnectionString); var blobSettings = new BlobSettings(cloudStorageAccount); lcb.WithAzureBlobStorageLeaseManager(blobSettings); }); b.WhenStarted(async(service, token) => await service.Start(token)); b.WhenLeaderIsElected(iamLeader => { if (iamLeader) { System.Console.ForegroundColor = ConsoleColor.Black; System.Console.ForegroundColor = ConsoleColor.Red; System.Console.BackgroundColor = ConsoleColor.White; } System.Console.WriteLine($"Leader election took place: {iamLeader}"); System.Console.ForegroundColor = ConsoleColor.Gray; System.Console.BackgroundColor = ConsoleColor.Black; }); }); sc.WhenStopped(service => service.Stop()); sc.ConstructUsing(name => new Service()); }); x.OnException(exception => { System.Console.WriteLine(exception); System.Console.ReadLine(); }); }); }
public async Task when_the_leader_doesnt_renew_the_lease_another_node_can_become_leader() { var settings = new BlobSettings(CloudStorageAccount.DevelopmentStorageAccount, "integration", "LeaderRenewing"); var leaseLength = TimeSpan.FromSeconds(15); var leaseExpiryWaitTime = TimeSpan.FromSeconds(5); var firstLeader = new AzureBlobLeaseManager(settings, leaseLength); await firstLeader.AcquireLease(new LeaseOptions(nameof(firstLeader)), CancellationToken.None); await Task.Delay(leaseLength + leaseExpiryWaitTime); var secondLeader = new AzureBlobLeaseManager(settings, leaseLength); Assert.True(await secondLeader.AcquireLease(new LeaseOptions(nameof(secondLeader)), CancellationToken.None)); await secondLeader.ReleaseLease(new LeaseReleaseOptions(nameof(secondLeader))); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var connectionString = configuration["Data:Azure:Storage:ConnectionString"]; if (string.IsNullOrWhiteSpace(connectionString)) { return; } var storageAccount = CloudStorageAccount.Parse(connectionString); var settings = new BlobSettings(storageAccount, "mutex", configuration["General:ServiceName"]); var mutex = new BlobDistributedMutex(settings, dispatcher.RunDispatcherAsync, logger); while (!stoppingToken.IsCancellationRequested) { await mutex.RunTaskWhenMutexAcquired(stoppingToken); } }
public void allow_only_one_lease_manager_to_own_the_mutex_at_any_given_time() { const int concurrentMutexes = 5; var leaseLength = TimeSpan.FromSeconds(15); using (var cts = new CancellationTokenSource()) { var settings = new BlobSettings(CloudStorageAccount.DevelopmentStorageAccount, "integration", "onlyonegetsalease"); var counter = 0; var managers = Enumerable.Range(0, concurrentMutexes) .Select(completed => new AzureBlobLeaseManager(settings, leaseLength)).Select(manager => manager.AcquireLease(new LeaseOptions(counter++.ToString()), cts.Token) .ConfigureAwait(false)); var count = managers.Select(task => task.GetAwaiter().GetResult()).Count(b => b); Assert.Equal(1, count); } }
public void LeaderRenewsLease() { const int ConcurrentMutexes = 5; var settings = new BlobSettings(CloudStorageAccount.DevelopmentStorageAccount, "leases", "LeaderRenewsLease"); var mutexAcquired = Enumerable.Range(0, ConcurrentMutexes).Select(_ => new TaskCompletionSource <bool>()).ToArray(); var mutexes = mutexAcquired.Select(completed => new BlobDistributedMutex(settings, SignalAndWait(completed))).ToArray(); var cts = new CancellationTokenSource(); foreach (var mutex in mutexes) { mutex.RunTaskWhenMutexAcquired(cts.Token); } bool allFinished = Task.WaitAll(mutexAcquired.Select(x => (Task)x.Task).ToArray(), TimeSpan.FromMinutes(3)); cts.Cancel(); Assert.IsFalse(allFinished); Assert.AreEqual(1, mutexAcquired.Count(x => x.Task.IsCompleted)); }
public void OnlyOneMutexStartsTask() { const int ConcurrentMutexes = 5; var settings = new BlobSettings(CONN_STR, "leases", "OnlyOneMutexStartsTask"); var mutexAcquired = Enumerable.Range(0, ConcurrentMutexes).Select(_ => new TaskCompletionSource <bool>()).ToArray(); var mutexes = mutexAcquired.Select(completed => new BlobDistributedMutex(settings, SignalAndWait(completed))).ToArray(); var cts = new CancellationTokenSource(); foreach (var mutex in mutexes) { mutex.RunTaskWhenMutexAcquired(cts.Token); } bool allFinished = Task.WaitAll(mutexAcquired.Select(x => (Task)x.Task).ToArray(), TimeSpan.FromSeconds(10)); cts.Cancel(); Assert.IsFalse(allFinished); Assert.AreEqual(1, mutexAcquired.Count(x => x.Task.IsCompleted)); }
public static void WithAzureBlobStorageLeaseManager(this LeaseConfigurationBuilder builder, BlobSettings settings) { builder.WithLeaseManager(lc => { var validator = new AzureBlobLeaseLengthValidator(); var leaseLength = lc.LeaseLengthCalculator.Calculate(); validator.Validate(leaseLength); return(new AzureBlobLeaseManager(settings, leaseLength)); }); }
public GetBlobsAsync(BlobSettings storageSettings, IConfiguration configuration) { _storageSettings = storageSettings; _configuration = configuration; }