public async Task <ConditionalValue <TValue> > TryRemoveAsync(ITransaction tx, TKey key)
        {
            if (reliableDictionary == null)
            {
                await InitializeReliableDictionary();
            }

            return(await reliableDictionary.TryRemoveAsync(tx, key));
        }
Example #2
0
        /// <summary>
        /// Unregisters an entity as observable for a given topic.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="entityId">The entity id.</param>
        /// <returns>The asynchronous result of the operation.</returns>
        public async Task UnregisterObservableAsync(string topic, EntityId entityId)
        {
            if (string.IsNullOrWhiteSpace(topic))
            {
                throw new ArgumentException($"The {nameof(topic)} parameter cannot be null.", nameof(topic));
            }
            if (entityId == null)
            {
                throw new ArgumentException($"The {nameof(entityId)} parameter cannot be null.", nameof(entityId));
            }
            try
            {
                IReliableDictionary <string, EntityId> observablesDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <string, EntityId> >(topic);

                string entityUri = entityId.EntityUri.ToString();

                using (ITransaction transaction = this.StateManager.CreateTransaction())
                {
                    ConditionalValue <EntityId> result = await observablesDictionary.TryGetValueAsync(transaction, entityUri);

                    if (result.HasValue)
                    {
                        await observablesDictionary.TryRemoveAsync(transaction, entityUri);

                        ServiceEventSource.Current.Message($"Observable successfully unregistered.\r\n[Observable]: {entityId}\r\n[Publication]: Topic=[{topic}]");
                    }
                    await transaction.CommitAsync();
                }
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Error(ex);
                throw;
            }
        }
 public async Task Clear(string key, CancellationToken cancellationToken = default(CancellationToken))
 {
     if (await Contains(key, cancellationToken).ConfigureAwait(false))
     {
         await state.TryRemoveAsync(transactionProvider.Current, key, TimeSpan.FromSeconds(5), cancellationToken).ConfigureAwait(false);
     }
 }
Example #4
0
        private async Task PeriodicOldMessageTrimming(CancellationToken cancellationToken)
        {
            IReliableDictionary <CustomerOrderActorMessageId, DateTime> recentRequests =
                await this.stateManager.GetOrAddAsync <IReliableDictionary <CustomerOrderActorMessageId, DateTime> >(ActorMessageDictionaryName);

            IReliableDictionary <CustomerOrderActorMessageId, Tuple <InventoryItemId, int> > requestHistory =
                await
                this.stateManager.GetOrAddAsync <IReliableDictionary <CustomerOrderActorMessageId, Tuple <InventoryItemId, int> > >(RequestHistoryDictionaryName);

            while (!cancellationToken.IsCancellationRequested)
            {
                using (ITransaction tx = this.stateManager.CreateTransaction())
                {
                    foreach (KeyValuePair <CustomerOrderActorMessageId, DateTime> request in recentRequests)
                    {
                        //if we have a record of a message that is older than 2 hours from current time, then remove that record
                        //from both of the stale message tracking dictionaries.
                        if (request.Value < (DateTime.UtcNow.AddHours(-2)))
                        {
                            await recentRequests.TryRemoveAsync(tx, request.Key);

                            await requestHistory.TryRemoveAsync(tx, request.Key);
                        }
                    }

                    await tx.CommitAsync();
                }

                //sleep for 5 minutes then scan again
                await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken);
            }
        }
        public async Task <IActionResult> Delete(string name)
        {
            IReliableDictionary <string, string> dictionary =
                await this.stateManager.GetOrAddAsync <IReliableDictionary <string, string> >(ValuesDictionaryName);

            try
            {
                using (ITransaction tx = this.stateManager.CreateTransaction())
                {
                    ConditionalValue <string> result = await dictionary.TryRemoveAsync(tx, name);

                    await tx.CommitAsync();

                    if (result.HasValue)
                    {
                        return(this.Ok());
                    }

                    return(new ContentResult {
                        StatusCode = 400, Content = $"A value with name {name} doesn't exist."
                    });
                }
            }
            catch (FabricNotPrimaryException)
            {
                return(new ContentResult {
                    StatusCode = 503, Content = "The primary replica has moved. Please re-resolve the service."
                });
            }
        }
Example #6
0
        internal async Task <bool> TryDequeueAndProcessAsync(CancellationToken cancellationToken)
        {
            IReliableQueue <Guid> queue =
                await this.StateManager.GetOrAddAsync <IReliableQueue <Guid> >(QueueName);

            IReliableDictionary <Guid, ApplicationDeployment> dictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <Guid, ApplicationDeployment> >(DictionaryName);

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                ConditionalValue <Guid> workItem = await queue.TryDequeueAsync(tx, this.transactionTimeout, cancellationToken);

                if (!workItem.HasValue)
                {
                    ServiceEventSource.Current.ServiceMessage(this, "No new application deployment requests.");
                    return(false);
                }

                Guid workItemId = workItem.Value;

                ConditionalValue <ApplicationDeployment> appDeployment =
                    await dictionary.TryGetValueAsync(tx, workItemId, LockMode.Update, this.transactionTimeout, cancellationToken);

                if (!appDeployment.HasValue)
                {
                    ServiceEventSource.Current.ServiceMessage(
                        this,
                        "Found queued application deployment request with no associated deployment information. Discarding.");

                    return(true);
                }

                ApplicationDeployment processedDeployment = await this.ProcessApplicationDeployment(appDeployment.Value, cancellationToken);

                if (processedDeployment.Status == ApplicationDeployStatus.Complete ||
                    processedDeployment.Status == ApplicationDeployStatus.Failed)
                {
                    // Remove deployments that completed or failed
                    await dictionary.TryRemoveAsync(tx, workItemId, this.transactionTimeout, cancellationToken);
                }
                else
                {
                    // The deployment hasn't completed or failed, so queue up the next stage of deployment
                    await queue.EnqueueAsync(tx, workItemId, this.transactionTimeout, cancellationToken);

                    // And update the deployment record with the new status
                    await dictionary.SetAsync(tx, workItemId, processedDeployment, this.transactionTimeout, cancellationToken);

                    ServiceEventSource.Current.ServiceMessage(
                        this,
                        "Application deployment request successfully processed. Cluster: {0}. Status: {1}",
                        processedDeployment.Cluster,
                        processedDeployment.Status);
                }

                await tx.CommitAsync();
            }

            return(true);
        }
Example #7
0
        public async Task <IActionResult> Delete(string name)
        {
            ServiceEventSource.Current.Message($"VotingData.Delete start. name='{name}'");
            ConditionalValue <int> result = new ConditionalValue <int>(false, -1);

            IReliableDictionary <string, int> votesDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, int> >("votes");

            IReliableDictionary <string, long> ballotDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, long> >("ballots");

            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                if (await votesDictionary.ContainsKeyAsync(tx, name))
                {
                    ConditionalValue <int> deleteVotes = await votesDictionary.TryGetValueAsync(tx, name);

                    result = await votesDictionary.TryRemoveAsync(tx, name);

                    long ballots = await GetTotalBallotsCast();
                    await AuditBallot(-1 *(ballots >= deleteVotes.Value ? deleteVotes.Value : ballots));

                    await tx.CommitAsync();

                    ServiceEventSource.Current.Message($"VotingData.Delete end. '{name}' deleted.");
                    return(new OkResult());
                }
                else
                {
                    ServiceEventSource.Current.Message($"VotingData.Delete end. '{name}' not found.");
                    return(new NotFoundResult());
                }
            }
        }
        private async Task PersistChanges(Order order, OrderSnapshot initial, IReliableDictionary <Guid, OrderSnapshot> dictionary, ITransaction transaction)
        {
            var updated = await dictionary.TryUpdateAsync(transaction, order.Id, order.GetSnapshot(), initial);

            if (updated && order.IsClosed())
            {
                await dictionary.TryRemoveAsync(transaction, order.Id);
            }
        }
Example #9
0
        /// <inheritdocs/>
        public async Task DeleteEntry(Guid replicaId)
        {
            await EnsureChunkTables();

            using (var tx = ServiceFabricUtils.CreateTransaction())
            {
                await ServiceFabricUtils.DoWithTimeoutRetry(
                    async() => await m_chunktable.TryRemoveAsync(tx, replicaId));
            }
        }
Example #10
0
        private static async Task <bool> ExecuteRemoveAsync(ITransaction tx, Order order, IReliableDictionary <string, Order> orders)
        {
            var result = await orders.TryRemoveAsync(tx, order.Id);

            if (result.HasValue)
            {
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// This method saves the description (eventString) and timestamp of the recentmost induced fault as
        /// a ChaosEntry in a Reliable Dictionary
        /// </summary>
        /// <param name="eventString"></param>
        /// <returns>A task to await on</returns>
        private async Task StoreEventAsync(string eventString)
        {
            ServiceEventSource.Current.ServiceMessage(this, "ChaosTest: {0}", eventString);

            IReliableDictionary <string, long> eventCount =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >(StringResource.EventCountKey);

            IReliableDictionary <string, DateTime> startTime =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, DateTime> >(StringResource.StartTimeKey);

            IReliableDictionary <long, ChaosEntry> savedEvents =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <long, ChaosEntry> >(StringResource.SavedEventsKey);


            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                if (!await startTime.ContainsKeyAsync(tx, StringResource.StartTimeKey))
                {
                    await startTime.AddAsync(tx, StringResource.StartTimeKey, DateTime.UtcNow);
                }

                if (!await eventCount.ContainsKeyAsync(tx, StringResource.EventCountKey))
                {
                    await eventCount.AddAsync(tx, StringResource.EventCountKey, 0);
                }

                ConditionalValue <long> result =
                    await eventCount.TryGetValueAsync(tx, StringResource.EventCountKey, LockMode.Update);

                if (result.HasValue)
                {
                    long currentCount = result.Value;

                    // If we have HistoryLength number of events, we make room for new events by removing oldest ones,
                    // always keeping HistoryLength number of recentmost events on the show on the webpage.
                    if (currentCount > Constants.HistoryLength - 1)
                    {
                        await savedEvents.TryRemoveAsync(tx, currentCount - Constants.HistoryLength + 1);
                    }

                    ChaosEntry chaosEntry = new ChaosEntry
                    {
                        Record    = eventString,
                        TimeStamp = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)
                    };

                    await savedEvents.AddAsync(tx, ++currentCount, chaosEntry);

                    await eventCount.SetAsync(tx, StringResource.EventCountKey, currentCount);

                    await tx.CommitAsync();
                }
            }
        }
Example #12
0
        /// <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();
            }
        }
Example #13
0
        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());
        }
Example #14
0
        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();
            }
        }
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            TimeSpan timeSpan = new TimeSpan(0, 0, 30);

            ServiceEventSource.Current.ServiceMessage(
                this,
                "Partition {0} started processing messages.",
                this.Context.PartitionId);

            IReliableDictionary <DateTime, Message> messagesDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <DateTime, Message> >("messages");

            //Use this method to periodically clean up messages in the messagesDictionary
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    IEnumerable <KeyValuePair <DateTime, Message> > messagesEnumerable = await GetMessagesAsync();

                    // Remove all the messages that are older than 30 seconds keeping the last 50 messages
                    IEnumerable <KeyValuePair <DateTime, Message> > oldMessages = from t in messagesEnumerable
                                                                                  where t.Key < (DateTime.Now - timeSpan)
                                                                                  orderby t.Key ascending
                                                                                  select t;

                    using (ITransaction tx = this.StateManager.CreateTransaction())
                    {
                        int messagesCount = (int)await messagesDictionary.GetCountAsync(tx);

                        foreach (KeyValuePair <DateTime, Message> item in oldMessages.Take(messagesCount - MessagesToKeep))
                        {
                            await messagesDictionary.TryRemoveAsync(tx, item.Key);
                        }
                        await tx.CommitAsync();
                    }
                }
                catch (Exception e)
                {
                    if (!this.HandleException(e))
                    {
                        ServiceEventSource.Current.ServiceMessage(
                            this,
                            "Partition {0} stopped processing because of error {1}",
                            this.Context.PartitionId,
                            e);
                        break;
                    }
                }

                await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
            }
        }
Example #16
0
        public async Task DeleteKeyAsync(string key)
        {
            if (_dict == null)
            {
                throw new NullReferenceException(nameof(_dict));
            }
            using (var tx = this.StateManager.CreateTransaction())
            {
                await _dict.TryRemoveAsync(tx, key);

                await tx.CommitAsync();
            }
        }
        public async Task <IActionResult> Delete(string id)
        {
            IReliableDictionary <string, ConnectedUserData> usersDict = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, ConnectedUserData> >(CONNECTED_USERS);

            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                await usersDict.TryRemoveAsync(tx, id);

                await tx.CommitAsync();
            }

            return(new OkResult());
        }
Example #18
0
        public async Task CheckpointAsync(CheckpointInfo checkpointInfo)
        {
            if (checkpointInfo.RecordCount <= 0)
            {
                return;
            }

            var index    = checkpointInfo.FirstRecordInfo;
            var counting = 0;

            using (var tx = this.stateManager.CreateTransaction())
            {
                // Two break conditions:
                // 1. counting == checkpointInfo.RecordCount
                // 2. index == checkpointInfo.LastRecordInfo
                // After breaking, double check if both condition is true.
                // Otherwise there's a mismatch within checkpointInfo
                while (counting < checkpointInfo.RecordCount)
                {
                    var removed = await recordDictionary.TryRemoveAsync(tx, index.Index);

                    if (removed.HasValue && removed.Value != null)
                    {
                        counting++;
                    }

                    if (index.Equals(checkpointInfo.LastRecordInfo))
                    {
                        break;
                    }

                    index = index.Next();
                }

                if (!index.Equals(checkpointInfo.LastRecordInfo) || counting != checkpointInfo.RecordCount)
                {
                    MessageDispatcherEventSource.Current.Warning(MessageDispatcherEventSource.EmptyTrackingId, this, nameof(this.CheckpointAsync), OperationStates.NoMatch, $"Mismatch in checkpoint info. LastRecordInfo={checkpointInfo.LastRecordInfo.Index}, Index={index.Index}, RecordCount={checkpointInfo.RecordCount}, Counting={counting}");
                }

                await this.metaDictonary.AddOrUpdateAsync(tx, this.checkpointKeyName, checkpointInfo.LastRecordInfo, (k, v) => checkpointInfo.LastRecordInfo);

                await tx.CommitAsync();
            }

            lock (this.Lock)
            {
                this.currentCheckpointIndex = checkpointInfo.LastRecordInfo;
            }

            MessageDispatcherEventSource.Current.Info(MessageDispatcherEventSource.EmptyTrackingId, this, nameof(this.CheckpointAsync), OperationStates.Succeeded, $"Checkpoint CurrentCheckpoint={checkpointInfo.LastRecordInfo}. NumberOfRecords={checkpointInfo.RecordCount}, Actual NumberOfRecords={counting}");
        }
Example #19
0
        public async Task <bool> DeleteAsync(string key)
        {
            using (var tx = _stateManager.CreateTransaction())
            {
                var result = await _dictionary.TryRemoveAsync(tx, key);

                if (result.HasValue)
                {
                    await tx.CommitAsync();
                }

                return(result.HasValue);
            }
        }
Example #20
0
        private static async Task RemoveKeyFromReliableDictionaryPrivateAsync(
            this IReliableStateManager stateManager,
            string dictionaryName,
            string keyToRemove,
            CancellationToken ctok)
        {
            IReliableDictionary <string, byte[]> dictionary = await stateManager.GetOrAddAsync <IReliableDictionary <string, byte[]> >(dictionaryName).ConfigureAwait(false);

            using (var tx = stateManager.CreateTransaction())
            {
                await dictionary.TryRemoveAsync(tx, keyToRemove).ConfigureAwait(false);

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
Example #21
0
        private async Task <bool> ExecuteRemoveAsync(Order order, IReliableDictionary <string, Order> orders)
        {
            using (var tx = this.stateManager.CreateTransaction())
            {
                var result = await orders.TryRemoveAsync(tx, order.Id);

                await tx.CommitAsync();

                if (result.HasValue)
                {
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Read messages stored for an observer.
        /// </summary>
        /// <param name="uri">Observer uri.</param>
        /// <returns>An enumerable containing messages for the observer.</returns>
        public async Task <IEnumerable <Message> > ReadMessagesAsync(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentException($"The {nameof(uri)} parameter cannot be null.", nameof(uri));
            }
            for (int k = 1; k <= ConfigurationHelper.MaxQueryRetryCount; k++)
            {
                try
                {
                    List <Message> messageList = new List <Message>();
                    IReliableDictionary <string, List <Message> > topicsDictionary =
                        await this.StateManager.GetOrAddAsync <IReliableDictionary <string, List <Message> > >(Constants.ObserverDictionary);

                    using (ITransaction transaction = this.StateManager.CreateTransaction())
                    {
                        ConditionalValue <List <Message> > result = await topicsDictionary.TryGetValueAsync(transaction, uri.AbsoluteUri);

                        if (result.HasValue)
                        {
                            messageList = result.Value;
                            await topicsDictionary.TryRemoveAsync(transaction, uri.AbsoluteUri);
                        }
                        await transaction.CommitAsync();
                    }
                    return(messageList);
                }
                catch (FabricTransientException ex)
                {
                    ServiceEventSource.Current.Error(ex);
                }
                catch (AggregateException ex)
                {
                    foreach (Exception e in ex.InnerExceptions)
                    {
                        ServiceEventSource.Current.Error(e);
                    }
                    throw;
                }
                catch (Exception ex)
                {
                    ServiceEventSource.Current.Error(ex);
                    throw;
                }
                await Task.Delay(ConfigurationHelper.BackoffQueryDelay);
            }
            throw new TimeoutException(Constants.RetryTimeoutExhausted);
        }
        public async Task DeleteAsync(IEnumerable <string> fullPaths, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPaths(fullPaths);

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync().ConfigureAwait(false);

                foreach (string fullPath in fullPaths)
                {
                    await coll.TryRemoveAsync(tx.Tx, ToFullPath(fullPath)).ConfigureAwait(false);
                }

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
        public async Task DeleteAsync(IEnumerable <string> ids, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(ids);

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync();

                foreach (string id in ids)
                {
                    await coll.TryRemoveAsync(tx.Tx, ToId(id));
                }

                await tx.CommitAsync();
            }
        }
Example #25
0
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            int hourlyLimit = (int)Math.Round(3600D / HourlyInterval.TotalSeconds);

            IReliableDictionary <DateTimeOffset, Dictionary <string, long> > dictionary = await
                                                                                          this.StateManager.GetOrAddAsync <IReliableDictionary <DateTimeOffset, Dictionary <string, long> > >($"history:/hourly");

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    ClusterLoadInformation capacities = await this.query.GetClusterLoadAsync();

                    DateTimeOffset            utcnow    = DateTimeOffset.UtcNow;
                    DateTimeOffset            timestamp = new DateTimeOffset(utcnow.Year, utcnow.Month, utcnow.Day, utcnow.Hour, utcnow.Minute, utcnow.Second, utcnow.Offset);
                    Dictionary <string, long> values    = new Dictionary <string, long>();

                    foreach (var capacity in capacities.LoadMetricInformationList)
                    {
                        values[capacity.Name] = capacity.ClusterLoad;
                    }

                    using (ITransaction tx = this.StateManager.CreateTransaction())
                    {
                        long count = await dictionary.GetCountAsync(tx);

                        if (count >= hourlyLimit)
                        {
                            var min = await(await dictionary.CreateLinqAsyncEnumerable(tx)).Min(x => x.Key);
                            await dictionary.TryRemoveAsync(tx, min);
                        }

                        await dictionary.SetAsync(tx, timestamp, values);

                        await tx.CommitAsync();
                    }
                }
                catch (FabricTransientException)
                {
                    // retry
                }

                await Task.Delay(HourlyInterval, cancellationToken);
            }
        }
Example #26
0
        /// <summary>
        /// Delete the given key from the category dictionary.
        /// </summary>
        /// <param name="stateName">State name</param>
        /// <param name="key">Key</param>
        /// <returns>Task</returns>
        internal async Task <bool> DeleteFromState(string stateName, string key)
        {
            IReliableDictionary <string, string> votesDictionary = await this.stateManager.GetOrAddAsync <IReliableDictionary <string, string> >(stateName);

            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                if (await votesDictionary.ContainsKeyAsync(tx, key))
                {
                    await votesDictionary.TryRemoveAsync(tx, key);

                    await tx.CommitAsync();

                    return(true);
                }
                return(false);
            }
        }
Example #27
0
        private async Task FinishRemovalOfSubscriptions(IReliableDictionary <string, string> subscriptions)
        {
            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                var unregisteredServices = new List <string>();

                IAsyncEnumerable <KeyValuePair <string, string> > asyncEnumerable = await subscriptions.CreateEnumerableAsync(tx).ConfigureAwait(false);

                using (IAsyncEnumerator <KeyValuePair <string, string> > asyncEnumerator = asyncEnumerable.GetAsyncEnumerator())
                {
                    while (await asyncEnumerator.MoveNextAsync(CancellationToken.None).ConfigureAwait(false))
                    {
                        string subscriber = asyncEnumerator.Current.Key;
                        string topic      = asyncEnumerator.Current.Value;

                        try
                        {
                            // https://social.msdn.microsoft.com/Forums/en-US/ce8aff1d-6246-4b53-9075-13b738a24b13/best-way-to-determine-if-a-service-already-exists?forum=AzureServiceFabric
                            // treat as "Desired state management"
                            // "Instead treat this more like desired state management - create the service until you are told it already exists."
                            Uri serviceUri  = this.Context.CreateSubscriptionUri(topic, subscriber);
                            var description = new DeleteServiceDescription(serviceUri);

                            using (var fabric = new FabricClient())
                            {
                                await fabric.ServiceManager.DeleteServiceAsync(description);
                            }
                        }
                        catch (FabricElementNotFoundException)
                        {
                            await UnregisterSubscriberFromTopic(subscriber, topic);

                            unregisteredServices.Add(subscriber);
                        }
                    }
                }

                // do after enumerator is complete.
                foreach (var sub in unregisteredServices)
                {
                    await subscriptions.TryRemoveAsync(tx, sub);
                }

                await tx.CommitAsync();
            }
        }
Example #28
0
        public async Task DeleteBookAsync(Book book)
        {
            IReliableDictionary <Guid, Book> books =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <Guid, Book> >(BooksDictionaryName);

            ServiceEventSource.Current.ServiceMessage(this, message: "Received delete book request. ID: {0}. Name: {1}.", args: new object[] { book.ID, book.BookName });

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                await books.TryRemoveAsync(tx, book.ID);

                await tx.CommitAsync();

                ServiceEventSource.Current.ServiceMessage(
                    this, message: "Succesfully deleted book. ID: {0}. Name: {1}.", args: new object[] { book.ID, book.BookName });
            }
        }
        public async Task <string> DisconfigureService(string serviceName, string primaryCluster, string secondaryCluster)
        {
            List <String> keysToRemove = new List <String>();
            IReliableDictionary <String, PartitionWrapper> myDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <String, PartitionWrapper> >("partitionDictionary");

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                IAsyncEnumerable <KeyValuePair <String, PartitionWrapper> > enumerable = await myDictionary.CreateEnumerableAsync(tx);

                IAsyncEnumerator <KeyValuePair <String, PartitionWrapper> > asyncEnumerator = enumerable.GetAsyncEnumerator();
                while (await asyncEnumerator.MoveNextAsync(CancellationToken.None))
                {
                    PartitionWrapper secondaryPartition = asyncEnumerator.Current.Value;
                    String           partitionAccessKey = asyncEnumerator.Current.Key;

                    if (Utility.isPartitionFromPrimarySecondaryCombination(partitionAccessKey, primaryCluster, secondaryCluster))
                    {
                        if (secondaryPartition.serviceName.ToString().Equals(serviceName))
                        {
                            keysToRemove.Add(asyncEnumerator.Current.Key);
                        }
                    }
                }
                await tx.CommitAsync();
            }
            bool allPartitionsRemoved = true;

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                foreach (String key in keysToRemove)
                {
                    ConditionalValue <PartitionWrapper> value = myDictionary.TryRemoveAsync(tx, key).Result;
                    if (!value.HasValue)
                    {
                        allPartitionsRemoved = false;
                    }
                }
                await tx.CommitAsync();
            }
            if (allPartitionsRemoved)
            {
                return(serviceName);
            }
            return(null);
        }
Example #30
0
        private static async Task RemoveKeysWithPrefix(
            ITransaction tx,
            IReliableDictionary <string, byte[]> relDict,
            string keyPrefix,
            CancellationToken cancellationToken)
        {
            var enumerable = await relDict.CreateEnumerableAsync(tx);

            var enumerator = enumerable.GetAsyncEnumerator();

            while (await enumerator.MoveNextAsync(cancellationToken))
            {
                if (enumerator.Current.Key.StartsWith(keyPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    await relDict.TryRemoveAsync(tx, enumerator.Current.Key);
                }
            }
        }