private async Task <SubscriptionWorker <User> > CreateAndInitiateSubscription(RavenServer server, IDocumentStore store, string database, List <User> usersCount, AsyncManualResetEvent reachedMaxDocCountMre, int batchSize, string mentor) { var proggress = new SubscriptionProggress() { MaxId = 0 }; var subscriptionName = await store.Subscriptions.CreateAsync <User>(options : new SubscriptionCreationOptions { MentorNode = mentor }, database : database).ConfigureAwait(false); var subscription = store.Subscriptions.GetSubscriptionWorker <User>(new SubscriptionWorkerOptions(subscriptionName) { TimeToWaitBeforeConnectionRetry = TimeSpan.FromMilliseconds(500), MaxDocsPerBatch = batchSize }, database: database); var subscripitonState = await store.Subscriptions.GetSubscriptionStateAsync(subscriptionName, database); var getDatabaseTopologyCommand = new GetDatabaseRecordOperation(database); var record = await store.Maintenance.Server.SendAsync(getDatabaseTopologyCommand).ConfigureAwait(false); await server.ServerStore.Cluster.WaitForIndexNotification(subscripitonState.SubscriptionId).ConfigureAwait(false); Assert.Equal(mentor, record.Topology.WhoseTaskIsIt(RachisState.Follower, subscripitonState, null)); var task = subscription.Run(a => { foreach (var item in a.Items) { var x = item.Result; int curId = 0; var afterSlash = x.Id.Substring(x.Id.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1); curId = int.Parse(afterSlash.Substring(0, afterSlash.Length - 2)); Assert.True(curId >= proggress.MaxId); usersCount.Add(x); proggress.MaxId = curId; } }); subscription.AfterAcknowledgment += b => { try { if (usersCount.Count == 10) { reachedMaxDocCountMre.Set(); } } catch (Exception) { } return(Task.CompletedTask); }; await Task.WhenAny(task, Task.Delay(_reasonableWaitTime)).ConfigureAwait(false); return(subscription); }
private async Task <(SubscriptionWorker <User> worker, Task subsTask)> CreateAndInitiateSubscription(IDocumentStore store, string defaultDatabase, List <User> usersCountInAck, AsyncManualResetEvent reachedMaxDocCountInAckMre, List <User> usersCountInBatch, AsyncManualResetEvent reachedMaxDocCountInBatchMre, int batchSize, int numberOfConnections = 1, string mentor = null) { var proggress = new SubscriptionProggress() { MaxId = 0 }; var subscriptionName = await store.Subscriptions.CreateAsync <User>(options : new SubscriptionCreationOptions { MentorNode = mentor }).ConfigureAwait(false); SubscriptionWorker <User> subscription; List <Task> subTasks = new(); int connections = numberOfConnections; do { subscription = store.Subscriptions.GetSubscriptionWorker <User>(new SubscriptionWorkerOptions(subscriptionName) { TimeToWaitBeforeConnectionRetry = TimeSpan.FromMilliseconds(500), MaxDocsPerBatch = batchSize, Strategy = numberOfConnections > 1 ? SubscriptionOpeningStrategy.Concurrent : SubscriptionOpeningStrategy.OpenIfFree }); subscription.AfterAcknowledgment += b => { try { foreach (var item in b.Items) { var x = item.Result; int curId = 0; var afterSlash = x.Id.Substring(x.Id.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1); curId = int.Parse(afterSlash.Substring(0, afterSlash.Length - 2)); Assert.True(curId >= proggress.MaxId); usersCountInAck.Add(x); proggress.MaxId = curId; } if (usersCountInAck.Count == 10) { reachedMaxDocCountInAckMre.Set(); } } catch (Exception) { } return(Task.CompletedTask); }; subTasks.Add(subscription.Run(batch => { foreach (var item in batch.Items) { usersCountInBatch.Add(item.Result); if (usersCountInBatch.Count == 10) { reachedMaxDocCountInBatchMre.Set(); } } })); connections--; } while (connections > 0); var subscripitonState = await store.Subscriptions.GetSubscriptionStateAsync(subscriptionName, store.Database); var getDatabaseTopologyCommand = new GetDatabaseRecordOperation(defaultDatabase); var record = await store.Maintenance.Server.SendAsync(getDatabaseTopologyCommand).ConfigureAwait(false); foreach (var server in Servers.Where(s => record.Topology.RelevantFor(s.ServerStore.NodeTag))) { await server.ServerStore.Cluster.WaitForIndexNotification(subscripitonState.SubscriptionId).ConfigureAwait(false); } if (mentor != null) { Assert.Equal(mentor, record.Topology.WhoseTaskIsIt(RachisState.Follower, subscripitonState, null)); } //await Task.WhenAny(task, Task.Delay(_reasonableWaitTime)).ConfigureAwait(false); return(subscription, Task.WhenAll(subTasks)); }
private async Task <Subscription <User> > CreateAndInitiateSubscription(IDocumentStore store, string defaultDatabase, List <User> usersCount, AsyncManualResetEvent reachedMaxDocCountMre, int batchSize) { var proggress = new SubscriptionProggress() { MaxId = 0 }; var subscriptionName = await store.Subscriptions.CreateAsync <User>().ConfigureAwait(false); var subscription = store.Subscriptions.Open <User>(new SubscriptionConnectionOptions(subscriptionName) { TimeToWaitBeforeConnectionRetry = TimeSpan.FromMilliseconds(500), MaxDocsPerBatch = batchSize }); var subscripitonState = await store.Subscriptions.GetSubscriptionStateAsync(store.Database, subscriptionName); var getDatabaseTopologyCommand = new GetDatabaseRecordOperation(defaultDatabase); var record = await store.Admin.Server.SendAsync(getDatabaseTopologyCommand).ConfigureAwait(false); foreach (var server in Servers.Where(s => record.Topology.RelevantFor(s.ServerStore.NodeTag))) { await server.ServerStore.Cluster.WaitForIndexNotification(subscripitonState.SubscriptionId).ConfigureAwait(false); } var task = subscription.Run(a => { foreach (var item in a.Items) { var x = item.Result; try { int curId = 0; var afterSlash = x.Id.Substring(x.Id.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1); curId = int.Parse(afterSlash.Substring(0, afterSlash.Length - 2)); Assert.True(curId >= proggress.MaxId);// todo: change back to '>' usersCount.Add(x); proggress.MaxId = curId; } catch (Exception) { } } }); subscription.AfterAcknowledgment += b => { try { if (usersCount.Count == 10) { reachedMaxDocCountMre.Set(); } } catch (Exception) { } return(Task.CompletedTask); }; await Task.WhenAny(task, Task.Delay(_reasonableWaitTime)).ConfigureAwait(false); return(subscription); }