private Task AddClusters(ITransaction tx, IReliableDictionary<int, Cluster> dictionary, int count, ClusterStatus status) { return this.AddClusters(tx, dictionary, count, () => this.CreateCluster(status)); }
public static async Task<StateManagerLease> GetOrCreateAsync( IReliableStateManager StateManager, IReliableDictionary<string, string> StateDictionary, string EntryName, string partitionId) { using (ITransaction tx = StateManager.CreateTransaction()) { StateManagerLease lease; // if something has been saved before load it ConditionalResult<string> cResults = await StateDictionary.TryGetValueAsync(tx, EntryName); if (cResults.HasValue) { lease = FromJsonString(cResults.Value); lease.m_EntryName = EntryName; lease.m_StateDictionary = StateDictionary; lease.m_StateManager = StateManager; } else { // if not create new lease = new StateManagerLease(StateManager, StateDictionary, EntryName, partitionId); } await tx.CommitAsync(); return lease; } }
private async Task EnsureLeasesDictionaryExists() { if (_leases == null) { _leases = await _stateManager.GetOrAddAsync<IReliableDictionary<string, string>>("leases"); } }
public static Task<StateManagerLease> GetOrCreateAsync( IReliableStateManager StateManager, IReliableDictionary<string, string> StateDictionary, string ServiceBusNamespace, string ConsumerGroupName, string EventHubName, string PartitionId) { string defaultEntryName = GetDefaultLeaseEntryName(ServiceBusNamespace, ConsumerGroupName, EventHubName, PartitionId); return GetOrCreateAsync(StateManager, StateDictionary, defaultEntryName, PartitionId); }
private StateManagerLease( IReliableStateManager StateManager, IReliableDictionary<string, string> StateDictionary, string EntryName, string partitionId) { this.m_StateManager = StateManager; this.m_StateDictionary = StateDictionary; this.m_EntryName = EntryName; this.PartitionId = partitionId; }
private async Task UpdateBuildStatus(string name, string status) { IReliableDictionary <string, string> buildsDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, string> >("builds"); using (ITransaction tx = this.stateManager.CreateTransaction()) { string timestampStatus = $"{DateTime.UtcNow}: {status}"; await buildsDictionary.AddOrUpdateAsync(tx, name, timestampStatus, (key, oldvalue) => timestampStatus); await tx.CommitAsync(); } }
public async Task <IActionResult> GetGame(string roomid) { try { if (!RoomManager.IsActive) { return new ContentResult { StatusCode = 500, Content = "Service is still starting up. Please retry." } } ; IReliableDictionary <string, Room> roomdict = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, Room> >(RoomDictionaryName); List <KeyValuePair <string, Player> > result = new List <KeyValuePair <string, Player> >(); using (ITransaction tx = this.stateManager.CreateTransaction()) { //Make sure the requested room exists if (!await roomdict.ContainsKeyAsync(tx, roomid)) { await tx.CommitAsync(); return(new ContentResult { StatusCode = 400, Content = "This room does not exist" }); } IReliableDictionary <string, ActivePlayer> activeroom = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, ActivePlayer> >(roomid); //Return an eumerator of the rooms in this dictionary to be parsed together with others IAsyncEnumerable <KeyValuePair <string, ActivePlayer> > enumerable = await activeroom.CreateEnumerableAsync(tx); IAsyncEnumerator <KeyValuePair <string, ActivePlayer> > enumerator = enumerable.GetAsyncEnumerator(); while (await enumerator.MoveNextAsync(CancellationToken.None)) { result.Add(new KeyValuePair <string, Player>(enumerator.Current.Key, enumerator.Current.Value.Player)); } await tx.CommitAsync(); } return(new ContentResult { StatusCode = 200, Content = JsonConvert.SerializeObject(result) }); } catch (Exception e) { return(exceptionHandler(e)); } }
private async Task <IReliableDictionary <string, string> > GetTypeNameDictionaryAsync() { if (this.collectionTypeNamesInstance == null) { this.collectionTypeNamesInstance = await this.stateManagerReplica.GetOrAddAsync <IReliableDictionary <string, string> >( new Uri(MetricCollectionTypeDictionaryName)); } return(this.collectionTypeNamesInstance); }
private async Task AddToHistoryAsync(CheckoutSummary checkout) { IReliableDictionary <DateTime, CheckoutSummary> history = await StateManager.GetOrAddAsync <IReliableDictionary <DateTime, CheckoutSummary> >("history"); using (ITransaction tx = StateManager.CreateTransaction()) { await history.AddAsync(tx, checkout.Date, checkout); await tx.CommitAsync(); } }
private void StateManager_StateManagerChanged(object sender, NotifyStateManagerChangedEventArgs e) { if (e.Action == NotifyStateManagerChangedAction.Add) { NotifyStateManagerSingleEntityChangedEventArgs args = e as NotifyStateManagerSingleEntityChangedEventArgs; if (args.ReliableState.Name.ToString() == "urn:" + HealthStatusDictionary) { IReliableDictionary <int, NationalCountyStats> dictionary = (IReliableDictionary <int, NationalCountyStats>)args.ReliableState; dictionary.DictionaryChanged += this.Dictionary_DictionaryChanged; } } }
internal static async Task <BackupPartitionStore> CreateOrGetBackupPartitionStore(StatefulService statefulService) { if (Store == null) { BackupRestoreTrace.TraceSource.WriteInfo(TraceRestoreStoreType, "Creating a BackupPartition Store"); IReliableDictionary <string, BackupPartitionStatus> reliableDictionary = await statefulService.StateManager.GetOrAddAsync <IReliableDictionary <string, BackupPartitionStatus> >(BackupPartitionStoreName); Store = new BackupPartitionStore(reliableDictionary, statefulService); BackupRestoreTrace.TraceSource.WriteInfo(TraceRestoreStoreType, "Created a BackupPartition Store successfully"); } return(Store); }
/// <summary> /// Search data for dictinary with match /// </summary> /// <typeparam name="TEntityKey"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <param name="dict"></param> /// <param name="stateManager"></param> /// <param name="match"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task <List <TEntity> > FindAllAsync <TEntityKey, TEntity>( this IReliableDictionary <TEntityKey, TEntity> dict, IReliableStateManager stateManager, Predicate <TEntity> match, CancellationToken cancellationToken) where TEntityKey : IComparable <TEntityKey>, IEquatable <TEntityKey> { using (var tx = stateManager.CreateTransaction()) { return(await(await dict.CreateEnumerableAsync(tx)).FindAllAsync(match, cancellationToken)); } }
public async Task DeleteStoryPointEntry(Guid entryId) { IReliableDictionary <Guid, perStoryData> storypointdata = await _stateManager.GetOrAddAsync <IReliableDictionary <Guid, perStoryData> >("storypointdata"); using (ITransaction tx = _stateManager.CreateTransaction()) { await storypointdata.TryRemoveAsync(tx, entryId); await tx.CommitAsync(); } }
public async Task DeleteAllStoryPointEntries() { IReliableDictionary <Guid, perStoryData> storypointdata = await _stateManager.GetOrAddAsync <IReliableDictionary <Guid, perStoryData> >("storypointdata"); using (ITransaction tx = _stateManager.CreateTransaction()) { await storypointdata.ClearAsync(); await tx.CommitAsync(); } }
public async Task <IActionResult> Put(string id, [FromBody] JToken values) { try { var userId = values["player"].Value <string>(); var idx = values["idx"].Value <int>(); _logger.Info($"Got move from {userId} for {idx}"); IReliableDictionary <string, Game> gamesDictionary = await _stateManager.GetOrAddAsync <IReliableDictionary <string, Game> >("games"); using (ITransaction tx = _stateManager.CreateTransaction()) { var game = await gamesDictionary.TryGetValueAsync(tx, id); if (!game.HasValue) { return(NotFound()); } if (game.Value.State is TicTacToeState state) { _logger.Info("It was a TicTacToeGame"); var playerMarker = game.Value.UserRoles[userId]; if (state.Board[idx] == null && ((state.IsXTurn && playerMarker == "X") || (!state.IsXTurn && playerMarker == "O"))) { state.Board[idx] = playerMarker; state.IsXTurn = !state.IsXTurn; await gamesDictionary.SetAsync(tx, id, game.Value); await tx.CommitAsync(); } return(Json(state.Board)); } else { _logger.Info("It was not a TicTacToeGame"); return(BadRequest()); } } } catch (Exception e) { _logger.Info(e.ToString()); throw; } }
public async Task <Product> GetProduct(Guid productId) { IReliableDictionary <Guid, Product> products = await _stateManager .GetOrAddAsync <IReliableDictionary <Guid, Product> >("products"); using (ITransaction tx = _stateManager.CreateTransaction()) { var product = await products.TryGetValueAsync(tx, productId); return(product.HasValue ? product.Value : null); } }
public async Task GetOrAddAsyncResultType() { MetricReliableStateManager target = new MetricReliableStateManager( this.GetContext(), new JsonReliableStateSerializerResolver(), this.GetConfig(), new MockReliableStateManager()); IReliableDictionary <int, string> actual = await target.GetOrAddAsync <IReliableDictionary <int, string> >("test://dictionary"); Assert.IsTrue(actual is MetricReliableDictionary <int, string>); }
private async Task <long> ExecuteCountAsync(IReliableDictionary <string, Order> orders) { long count = 0; using (ITransaction tx = this.stateManager.CreateTransaction()) { count = await orders.GetCountAsync(tx); await tx.CommitAsync(); } return(count); }
private async Task <IReliableDictionary <int, BasePoint> > GetScadaModel(string name) { IReliableDictionary <int, BasePoint> result = null; using (var tx = _stateManager.CreateTransaction()) { result = await _stateManager.GetOrAddAsync <IReliableDictionary <int, BasePoint> >(name, TimeSpan.FromSeconds(60)); await tx.CommitAsync(); } return(result); }
public async Task <IActionResult> Delete(string staff) { IReliableDictionary <string, int> StaffDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, int> >("counts"); using (ITransaction tx = this.stateManager.CreateTransaction()) { await StaffDictionary.TryRemoveAsync(tx, staff); await tx.CommitAsync(); } return(new OkResult()); }
private async Task AddToHistoryAsync(CheckoutSummary summary) { IReliableDictionary <DateTime, CheckoutSummary> history = await StateManager.GetOrAddAsync <IReliableDictionary <DateTime, CheckoutSummary> >(HISTORY_COLLECTION); using (ITransaction tx = StateManager.CreateTransaction()) { await history.AddAsync(tx, summary.Date, summary); await tx.CommitAsync(); } }
internal static async Task RemoveSubscription( this IReliableDictionary <string, BrokerServiceState> brokerState, ITransaction tx, string queues, SubscriptionDetails subscriptionDetails) { var subscribers = await brokerState.TryGetValueAsync(tx, queues, LockMode.Update); if (subscribers.HasValue) { var newState = BrokerServiceState.RemoveSubscriber(subscribers.Value, subscriptionDetails); await brokerState.SetAsync(tx, queues, newState); } }
public async Task AddProduct(Product product) { IReliableDictionary <Guid, Product> products = await _stateManager .GetOrAddAsync <IReliableDictionary <Guid, Product> >("products"); using (ITransaction tx = _stateManager.CreateTransaction()) { await products.AddOrUpdateAsync(tx, product.Id, product, (id, value) => product); await tx.CommitAsync(); } }
/// <summary> /// Called to add a health check to the watchdog. /// </summary> /// <param name="hcm"></param> /// <returns>Task instance.</returns> /// <exception cref="ArgumentException">ServiceName parameter within the HealthCheck instance does not exist.</exception> public async Task <bool> AddHealthCheckAsync(HealthCheck hcm) { // Validate that the service name actually exists within the cluster. If it doesn't, throw an error. if (false == await this.ValidateServiceExistsAsync(hcm.ServiceName, hcm.Partition).ConfigureAwait(false)) { throw new ArgumentException($"Service '{hcm.ServiceName?.AbsoluteUri}' does not exist within the cluster.", nameof(hcm.ServiceName)); } // Get the required dictionaries. IReliableDictionary <string, HealthCheck> hcDict = await this.GetHealthCheckDictionaryAsync().ConfigureAwait(false); try { // Create a transaction. using (ITransaction tx = this._service.StateManager.CreateTransaction()) { // Add or update the HealthCheck item in the dictionary. await hcDict.AddOrUpdateAsync(tx, hcm.Key, hcm, (k, v) => { return(hcm); }, this._timeout, this._token).ConfigureAwait(false); Interlocked.Increment(ref this._healthCheckCount); // Create the HealthCheckScheduleItem instance and save it. if (await this.SaveAsync(tx, new WatchdogScheduledItem(DateTimeOffset.UtcNow, hcm.Key)).ConfigureAwait(false)) { await tx.CommitAsync().ConfigureAwait(false); return(true); } } } catch (FabricObjectClosedException ex) { ServiceEventSource.Current.Exception(ex.Message, ex.GetType().Name, nameof(this.AddHealthCheckAsync)); } catch (TimeoutException ex) { ServiceEventSource.Current.Exception(ex.Message, ex.GetType().Name, nameof(this.AddHealthCheckAsync)); } catch (FabricTransientException ex) { ServiceEventSource.Current.Exception(ex.Message, ex.GetType().Name, nameof(this.AddHealthCheckAsync)); } catch (FabricException ex) { ServiceEventSource.Current.Exception(ex.Message, ex.GetType().Name, nameof(this.AddHealthCheckAsync)); } catch (Exception ex) { ServiceEventSource.Current.Exception(ex.Message, ex.GetType().Name, nameof(this.AddHealthCheckAsync)); } return(false); }
protected override async Task RunAsync(CancellationToken cancellationToken) { cancellationToken.Register(() => this._webApiCancellationSource.Cancel()); IReliableDictionary <string, RateAggregation> citiesDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <string, RateAggregation> >(RateCitiesDictionaryName); IReliableQueue <RateRequest> queue = await this.StateManager.GetOrAddAsync <IReliableQueue <RateRequest> >(RateQueueName); while (true) { cancellationToken.ThrowIfCancellationRequested(); try { using (var tx = this.StateManager.CreateTransaction()) { var result = await queue.TryDequeueAsync(tx); if (result.HasValue) { RateRequest request = result.Value; // TODO: Process the request // TODO: Go against the reservation provider to pick up the rate // TODO: Determine the reservation provider per tenant from the configuration parameters string providerName = GetParameterValue(ParamatersSection, "ProviderName"); int nights = (request.CheckOutDate - request.CheckInDate).Days; int netAmount = _random.Next(500) * nights; var newAggregation = new RateAggregation(); newAggregation.Transactions = 1; newAggregation.Nights = nights; newAggregation.Amount = (double)netAmount; await citiesDictionary.AddOrUpdateAsync(tx, $"{request.City}/{request.Country}/{providerName}", newAggregation, (key, currentValue) => { currentValue.Transactions += newAggregation.Transactions; currentValue.Nights += newAggregation.Nights; currentValue.Amount += newAggregation.Amount; return(currentValue); }); // This commits the add to dictionary and the dequeue operation. await tx.CommitAsync(); } } } catch (Exception e) { } await Task.Delay(TimeSpan.FromMilliseconds(500), cancellationToken); } }
public async Task <IEnumerable <KeyValuePair <DateTime, Message> > > GetMessagesAsync() { IReliableDictionary <DateTime, Message> messagesDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <DateTime, Message> >("messages"); using (ITransaction tx = this.StateManager.CreateTransaction()) { var messagesEnumerable = await messagesDictionary.CreateEnumerableAsync(tx, EnumerationMode.Ordered); return(messagesEnumerable.ToList()); } }
public async Task DeleteClusterAsync(string name) { IReliableDictionary <string, ClusterOperationStatus> clusters = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, ClusterOperationStatus> >(new Uri("fakeclusterops:/clusters")); using (ITransaction tx = this.stateManager.CreateTransaction()) { await clusters.SetAsync(tx, name, ClusterOperationStatus.Deleting); await tx.CommitAsync(); } }
// An interface method which is called by the webservice to configure the applications for stanby in secondary cluster // Takes in list of applications to be configured and corresponding policies and maps the partitions public async Task Configure(List <string> applications, List <PolicyStorageEntity> policyDeatils, ClusterDetails primaryCluster, ClusterDetails secondaryCluster) { IReliableDictionary <Guid, PartitionWrapper> myDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <Guid, PartitionWrapper> >("partitionDictionary"); IPolicyStorageService policyStorageClient = ServiceProxy.Create <IPolicyStorageService>(new Uri("fabric:/SFAppDRTool/PolicyStorageService")); bool stored = await policyStorageClient.PostStorageDetails(policyDeatils, primaryCluster.address + ':' + primaryCluster.httpEndpoint); foreach (string application in applications) { await MapPartitionsOfApplication(new Uri("fabric:/" + application), primaryCluster, secondaryCluster, "partitionDictionary"); } }
internal static async Task <WorkItemStore> CreateOrGetWorkItemStore(StatefulService statefulService) { if (Store == null) { BackupRestoreTrace.TraceSource.WriteNoise(TraceWorkItemStoreType, "Creating a WorkItem Store"); IReliableDictionary <Guid, WorkItem> reliableDictionary = await statefulService.StateManager.GetOrAddAsync <IReliableDictionary <Guid, WorkItem> >(WorkItemStoreName); Store = new WorkItemStore(reliableDictionary, statefulService); BackupRestoreTrace.TraceSource.WriteNoise(TraceWorkItemStoreType, "Created a WorkItem Store successfully"); } return(Store); }
internal static async Task <CleanupStore> CreateOrGetCleanupStore(StatefulService statefulService) { if (Store == null) { BackupRestoreTrace.TraceSource.WriteInfo(TraceRestoreStoreType, "Creating a cleanup Store"); IReliableDictionary <string, List <string> > reliableDictionary = await statefulService.StateManager.GetOrAddAsync <IReliableDictionary <string, List <string> > >(CleanUpStoreName); Store = new CleanupStore(reliableDictionary, statefulService); BackupRestoreTrace.TraceSource.WriteInfo(TraceRestoreStoreType, "Created a Cleanup Store successfully"); } return(Store); }
/// <summary> /// Looks for errors in the update tables, and processes one record at a time to create or update corresponding issues. /// </summary> /// <param name="checkPoint">The last record processed datetime.</param> /// <param name="issueRepo">Repository where the gitHub issue has to be created.</param> /// <param name="checkpointEvaluator">Reliable dictionary that holds the time the last record was processed.</param> /// <returns></returns> private async Task CheckForErrorsInUpdateHistoryTablesAsync( DateTimeOffset checkPoint, string issueRepo, IReliableDictionary <string, DateTimeOffset> checkpointEvaluator) { // First get the un-processed entries from the RepositoryBranchHistory table List <UpdateHistoryEntry> unprocessedHistoryEntries = _context.RepositoryBranchUpdateHistory .Where(entry => entry.Success == false && entry.Timestamp > checkPoint.UtcDateTime) .ToList <UpdateHistoryEntry>(); // Add in the SubscriptionUpdate errors: unprocessedHistoryEntries.AddRange( _context.SubscriptionUpdateHistory .Where(entry => entry.Success == false && entry.Timestamp > checkPoint.UtcDateTime)); // Sort union of these sets by timestamp, so the oldest checkpoint is the oldest unprocessed from both update history tables unprocessedHistoryEntries = unprocessedHistoryEntries.OrderBy(entry => entry.Timestamp).ToList(); if (!unprocessedHistoryEntries.Any()) { _logger.LogInformation($"No errors found in the 'RepositoryBranchUpdates' or 'SubscriptionUpdates' tables. The last checkpoint time was : '{checkPoint}'"); return; } foreach (var error in unprocessedHistoryEntries) { try { await IssueDescriptionEvaluator(error, issueRepo); using (ITransaction tx = _stateManager.CreateTransaction()) { await checkpointEvaluator.SetAsync( tx, "checkpointEvaluator", error.Timestamp ); await tx.CommitAsync(); } } catch (TimeoutException exe) { _logger.LogError(exe, $"Unable to update the last processed error timestamp : '{error.Timestamp}"); } catch (Exception ex) { _logger.LogError(ex, $"Unable to create a github issue for error message : '{error.ErrorMessage}' for {GetPrintableDescription(error)}"); } } }
/// <summary> /// NOTE: This should not be used in published MVP code. /// This function allows us to remove inventory items from inventory. /// </summary> /// <param name="itemId"></param> /// <returns></returns> public async Task DeleteInventoryItemAsync(InventoryItemId itemId) { IReliableDictionary <InventoryItemId, InventoryItem> inventoryItems = await this.stateManager.GetOrAddAsync <IReliableDictionary <InventoryItemId, InventoryItem> >(InventoryItemDictionaryName); using (ITransaction tx = this.stateManager.CreateTransaction()) { await inventoryItems.TryRemoveAsync(tx, itemId); await tx.CommitAsync(); } }
private static async Task SaveToCache( string query, IReliableDictionary <string, CachedItem <SearchOutput> > cache, SearchOutput output, ITransaction tx) { await cache.AddAsync(tx, query, new CachedItem <SearchOutput> { CachedAt = DateTime.UtcNow, Data = output }); }
public static Task <StateManagerLease> GetOrCreateAsync( IReliableStateManager StateManager, IReliableDictionary <string, string> StateDictionary, string ServiceBusNamespace, string ConsumerGroupName, string EventHubName, string PartitionId) { string defaultEntryName = GetDefaultLeaseEntryName(ServiceBusNamespace, ConsumerGroupName, EventHubName, PartitionId); return(GetOrCreateAsync(StateManager, StateDictionary, defaultEntryName, PartitionId)); }
private async Task <IReliableDictionary <CimModelKey, Container> > GetCim() { IReliableDictionary <CimModelKey, Container> result = null; using (var tx = _stateManager.CreateTransaction()) { result = await _stateManager.GetOrAddAsync <IReliableDictionary <CimModelKey, Container> >(tx, cimName, TimeSpan.FromSeconds(60)); await tx.CommitAsync(); } return(result); }
async public Task<JoinRoomResponse> Process(JoinRoomS2S request, IOperationContext context) { if (_roomMembers.Count >= MaxRoomSize) return new JoinRoomResponse { RetCode = JoinRoomRetCode.RoomIsFull }; else { if (_roomMembers.Any(x => x.UserName == request.UserName)) return new JoinRoomResponse { RetCode = JoinRoomRetCode.NameIsTaken }; _state = await _stateManager.GetOrAddAsync<IReliableDictionary<string, DateTime>>(ActorRef.Key.Id); try { var callbackChannel = await _node.ConnectToCallbackChannel(request.CallbackChannelRef); callbackChannel.DisconnectedEvent .Subscribe(_ => Fiber.Process(() => { _roomMembers.RemoveAll(x => x.UserName == request.UserName); var leaveNotification = new RoomEvent { Text = "User left", UserName = request.UserName }; foreach (var roomMember in _roomMembers) { roomMember.CallbackChannel.Send(leaveNotification); } if (_roomMembers.Count == 0) Dispose(); })); var joinNotification = new RoomEvent { Text = "User joined", UserName = request.UserName }; foreach (var roomMember in _roomMembers) { roomMember.CallbackChannel.Send(joinNotification); } _roomMembers.Add(new RoomMember { UserName = request.UserName, CallbackChannel = callbackChannel }); return new JoinRoomResponse { RetCode = JoinRoomRetCode.Ok }; } catch (Exception) { return new JoinRoomResponse() { RetCode = JoinRoomRetCode.Retry }; } } }
public EventHubCommunicationListener( ITraceWriter TraceWriter, IReliableStateManager stateManager, IReliableDictionary<string, string> stateDictionary, ServiceInitializationParameters serviceParameters, string eventHubName, string eventHubConnectionString, string eventHubConsumerGroupName, IEventDataHandler handler) : this(TraceWriter, stateManager, stateDictionary, serviceParameters, eventHubName, eventHubConnectionString, eventHubConsumerGroupName, handler, EventHubCommunicationListenerMode.SafeDistribute, string.Empty) { }
public EventHubCommunicationListener( ITraceWriter TraceWriter, IReliableStateManager stateManager, IReliableDictionary<string, string> stateDictionary, ServiceInitializationParameters serviceParameters, string eventHubName, string eventHubConnectionString, string eventHubConsumerGroupName, IEventDataHandler handler, EventHubCommunicationListenerMode Mode, string eventHubPartitionId) { this.ListenerMode = Mode; if (this.ListenerMode == EventHubCommunicationListenerMode.Single && string.IsNullOrEmpty(eventHubPartitionId)) { throw new InvalidOperationException("Event hub communication listener in single mode requires a partition id"); } this.m_TraceWriter = TraceWriter; this.m_InitParams = serviceParameters; this.EventHubName = eventHubName; this.EventHubConnectionString = eventHubConnectionString; this.Handler = handler; this.EventHubConsumerGroupName = eventHubConsumerGroupName; this.StateManager = stateManager; this.StateDictionary = stateDictionary; this.ListenerMode = Mode; this.m_TraceWriter.TraceMessage( string.Format( "Event Hub Listener created for {0} on {1} group:{2} mode:{3}", this.EventHubName, this.Namespace, this.EventHubConsumerGroupName, this.ListenerMode.ToString())); }
public async Task<IEventHubPartitionState> GetOrCreateAsync(string PartitionId) { if (null == mStateManager) throw new InvalidOperationException("assigned state manager is null"); if (null == StateStore) StateStore = await mStateManager.GetOrAddAsync<IReliableDictionary<string, DefaultPartitionState>>(ReliableDictionaryName); if (null == StateStore) throw new InvalidOperationException("could not create a reliable dictionary to store partition state"); var entryname = string.Concat(EntriesPrefix, PartitionId); DefaultPartitionState partitionState = null; using (var tx = mStateManager.CreateTransaction()) { var result = await StateStore.TryGetValueAsync(tx, entryname); if (result.HasValue) { partitionState = result.Value; } else { partitionState = new DefaultPartitionState(); partitionState.PartitionId = PartitionId; } await tx.CommitAsync(); } partitionState.EntryName = entryname; partitionState.StateStore = StateStore; partitionState.StateManager = mStateManager; return partitionState; }
protected override async Task RunAsync(CancellationToken cancellationToken) { Logger.Debug(nameof(this.RunAsync)); try { if (this.Partition.PartitionInfo.Kind != ServicePartitionKind.Int64Range) { throw new ApplicationException("Partition kind is not Int64Range"); } Int64RangePartitionInformation partitionInfo = (Int64RangePartitionInformation) this.Partition.PartitionInfo; int lowId = (int) partitionInfo.LowKey; int highId = (int) partitionInfo.HighKey; //bulk-import test data from CSV files Logger.Debug("attempting to bulk-import data"); //get or create the "products" dictionary this.productsCollection = await this.StateManager.GetOrAddAsync<IReliableDictionary<int, Product>>(ProductsCollectionName); //get or create the purchaseLog collection this.purchaseLog = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, ProductPurchase>>(OrdersCollectionName); if (!TimeSpan.TryParse(ConfigurationHelper.ReadValue("AppSettings", "ProcessOrdersInterval"), out this.processOrdersInterval)) { this.processOrdersInterval = TimeSpan.FromSeconds(3); } //check if data has been imported using (ITransaction tx = this.StateManager.CreateTransaction()) { if (await this.productsCollection.GetCountAsync(tx) <= 0) { Logger.Debug("Importing products with productId from {0} to {1}...", lowId, highId); //bulk-import the data BulkDataSource dataSource = new BulkDataSource(); foreach (Product p in dataSource.ReadProducts(lowId, highId)) { await this.productsCollection.AddAsync(tx, p.ProductId, p); } await tx.CommitAsync(); } } // launch watcher thread await this.DispatchPurchaseLogAsync(cancellationToken); } catch (Exception ex) { Logger.Error(ex, nameof(this.RunAsync)); throw; } }
public ServiceFabricTimeoutPersister(ServiceFabricStorageContext context) { _context = context; storages = context.StateManager.GetOrAddAsync<IReliableDictionary<string, List<TimeoutData>>>("nservicebusTimeout").Result; }
private async Task EnsureLeases() { if (_leases == null) { _leases = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, Lease>>("leases"); } }
/// <summary> /// Creates an EventHubReceiver from the given connection sting and partition key. /// The Reliable Dictionaries are used to create a receiver from wherever the service last left off, /// or from the current date/time if it's the first time the service is coming up. /// </summary> /// <param name="connectionString"></param> /// <param name="servicePartitionKey"></param> /// <param name="epochDictionary"></param> /// <param name="offsetDictionary"></param> /// <returns></returns> private async Task<Tuple<EventHubReceiver, MessagingFactory>> ConnectToIoTHubAsync( string connectionString, long servicePartitionKey, IReliableDictionary<string, long> epochDictionary, IReliableDictionary<string, string> offsetDictionary) { // EventHubs doesn't support NetMessaging, so ensure the transport type is AMQP. ServiceBusConnectionStringBuilder connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString); connectionStringBuilder.TransportType = TransportType.Amqp; ServiceEventSource.Current.ServiceMessage( this.Context, "RouterService connecting to IoT Hub at {0}", String.Join(",", connectionStringBuilder.Endpoints.Select(x => x.ToString()))); // A new MessagingFactory is created here so that each partition of this service will have its own MessagingFactory. // This gives each partition its own dedicated TCP connection to IoT Hub. MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString()); EventHubClient eventHubClient = messagingFactory.CreateEventHubClient("messages/events"); EventHubRuntimeInformation eventHubRuntimeInfo = await eventHubClient.GetRuntimeInformationAsync(); EventHubReceiver eventHubReceiver; // Get an IoT Hub partition ID that corresponds to this partition's low key. // This assumes that this service has a partition count 'n' that is equal to the IoT Hub partition count and a partition range of 0..n-1. // For example, given an IoT Hub with 32 partitions, this service should be created with: // partition count = 32 // partition range = 0..31 string eventHubPartitionId = eventHubRuntimeInfo.PartitionIds[servicePartitionKey]; using (ITransaction tx = this.StateManager.CreateTransaction()) { ConditionalValue<string> offsetResult = await offsetDictionary.TryGetValueAsync(tx, "offset", LockMode.Default); ConditionalValue<long> epochResult = await epochDictionary.TryGetValueAsync(tx, "epoch", LockMode.Update); long newEpoch = epochResult.HasValue ? epochResult.Value + 1 : 0; if (offsetResult.HasValue) { // continue where the service left off before the last failover or restart. ServiceEventSource.Current.ServiceMessage( this.Context, "Creating EventHub listener on partition {0} with offset {1}", eventHubPartitionId, offsetResult.Value); eventHubReceiver = await eventHubClient.GetDefaultConsumerGroup().CreateReceiverAsync(eventHubPartitionId, offsetResult.Value, newEpoch); } else { // first time this service is running so there is no offset value yet. // start with the current time. ServiceEventSource.Current.ServiceMessage( this.Context, "Creating EventHub listener on partition {0} with offset {1}", eventHubPartitionId, DateTime.UtcNow); eventHubReceiver = await eventHubClient.GetDefaultConsumerGroup() .CreateReceiverAsync(eventHubPartitionId, DateTime.UtcNow, newEpoch); } // epoch is recorded each time the service fails over or restarts. await epochDictionary.SetAsync(tx, "epoch", newEpoch); await tx.CommitAsync(); } return new Tuple<EventHubReceiver, MessagingFactory>(eventHubReceiver, messagingFactory); }
private async Task PrintInventoryItemsAsync(IReliableDictionary<InventoryItemId, InventoryItem> inventoryItems, CancellationToken cancellationToken) { using (ITransaction tx = this.StateManager.CreateTransaction()) { ServiceEventSource.Current.Message("Printing Inventory for {0} items:", await inventoryItems.GetCountAsync(tx)); IAsyncEnumerator<KeyValuePair<InventoryItemId, InventoryItem>> enumerator = (await inventoryItems.CreateEnumerableAsync(tx)).GetAsyncEnumerator(); while (await enumerator.MoveNextAsync(cancellationToken)) { ServiceEventSource.Current.Message("ID:{0}|Item:{1}", enumerator.Current.Key, enumerator.Current.Value); } } }
private Task AddClusters(ITransaction tx, IReliableDictionary<int, Cluster> dictionary, int count, ClusterStatus status) { return this.AddClusters(tx, dictionary, count, () => new Cluster() { Status = status }); }
async protected override Task RunAsync(CancellationToken cancelServicePartitionReplica) { _roomsState = await StateManager.GetOrAddAsync<IReliableDictionary<string, RoomData>>("Rooms"); using (var tx = StateManager.CreateTransaction()) { var roomEnumerable = await _roomsState.CreateEnumerableAsync(tx, EnumerationMode.Unordered); using (var roomEnumerator = roomEnumerable.GetAsyncEnumerator()) { while (await roomEnumerator.MoveNextAsync(cancelServicePartitionReplica).ConfigureAwait(false)) { roomEnumerator.Current.Value.Members = new List<RoomMember>(); Rooms.Add(roomEnumerator.Current.Key, roomEnumerator.Current.Value); } } } ServiceEventSource.Current.ServiceMessage(this, "RunAsync called"); while (!cancelServicePartitionReplica.IsCancellationRequested) { await Task.Delay(10000, cancelServicePartitionReplica); } }
/// <summary> /// Gets a list of active clusters. Clusters that are new, being created, or ready and not expired are considered active. /// </summary> /// <param name="clusterDictionary"></param> /// <returns></returns> private async Task<IEnumerable<KeyValuePair<int, Cluster>>> GetActiveClusters( IReliableDictionary<int, Cluster> clusterDictionary, CancellationToken cancellationToken) { List<KeyValuePair<int, Cluster>> activeClusterList = new List<KeyValuePair<int, Cluster>>(); using (ITransaction tx = this.StateManager.CreateTransaction()) { IAsyncEnumerable<KeyValuePair<int, Cluster>> clusterAsyncEnumerable = await clusterDictionary.CreateEnumerableAsync(tx); await clusterAsyncEnumerable.ForeachAsync( cancellationToken, x => { if (x.Value.Status == ClusterStatus.New || x.Value.Status == ClusterStatus.Creating || x.Value.Status == ClusterStatus.Ready) { activeClusterList.Add(x); } }); } return activeClusterList; }
private async Task AddClusters(ITransaction tx, IReliableDictionary<int, Cluster> dictionary, int count, Func<Cluster> newCluster) { for (int i = 0; i < count; ++i) { await dictionary.AddAsync(tx, this.GetRandom(), newCluster()); } }
private IEnumerable<KeyValuePair<int, Cluster>> GetActiveClusters(IReliableDictionary<int, Cluster> clusterDictionary) { return clusterDictionary.Where( x => x.Value.Status == ClusterStatus.New || x.Value.Status == ClusterStatus.Creating || x.Value.Status == ClusterStatus.Ready); }
private static void PrintInventoryItems(IReliableDictionary<InventoryItemId, InventoryItem> inventoryItems) { ServiceEventSource.Current.Message("PRINTING INVENTORY"); Dictionary<KeyValuePair<InventoryItemId, InventoryItem>, KeyValuePair<InventoryItemId, InventoryItem>> items = inventoryItems.ToDictionary(v => v); foreach (KeyValuePair<KeyValuePair<InventoryItemId, InventoryItem>, KeyValuePair<InventoryItemId, InventoryItem>> tempitem in items) { ServiceEventSource.Current.Message("ID:{0}|Item:{1}", tempitem.Key, tempitem.Value); } }
async Task<OperationResult> Process(TestStateful msg, IOperationContext arg2) { using (var trans = _stateManager.CreateTransaction()) { _state = await _stateManager.GetOrAddAsync<IReliableDictionary<string, DateTime>>(ActorRef.Key.Id); await _state.AddOrUpdateAsync(trans, ActorRef.Key.Id, (x) => DateTime.UtcNow, (x, _) => DateTime.UtcNow); await trans.CommitAsync(); } return OperationResult.Success; }