public static async Task <IEnumerable <Message> > ReadMessagesFromMessageBoxAsync(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
         {
             IMessageBoxService messageBoxService =
                 ServiceProxy.Create <IMessageBoxService>(
                     ConfigurationHelper.MessageBoxServiceUri,
                     new ServicePartitionKey(PartitionResolver.Resolve(uri.AbsoluteUri, ConfigurationHelper.MessageBoxServicePartitionCount)));
             return(await messageBoxService.ReadMessagesAsync(uri));
         }
         catch (FabricTransientException ex)
         {
             ActorEventSource.Current.Error(ex);
         }
         catch (AggregateException ex)
         {
             foreach (Exception innerException in ex.InnerExceptions)
             {
                 ActorEventSource.Current.Error(innerException);
             }
         }
         catch (Exception ex)
         {
             ActorEventSource.Current.Error(ex);
         }
         await Task.Delay(ConfigurationHelper.BackoffQueryDelay);
     }
     throw new TimeoutException(Constants.RetryTimeoutExhausted);
 }
示例#2
0
        /// <summary>
        ///     Registers an entity as observable for a given topic.
        ///     This method is called by a management service or actor.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <returns>The asynchronous result of the operation.</returns>
        public async Task RegisterObservableActorAsync(string topic)
        {
            EntityId id = await this.GetEntityIdAsync();

            if (id == null)
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(topic))
            {
                throw new ArgumentException($"The {nameof(topic)} parameter cannot be null.", nameof(topic));
            }
            ConditionalValue <Dictionary <Uri, ObserverInfo> > topicState = await this.StateManager.TryGetStateAsync <Dictionary <Uri, ObserverInfo> >(topic);

            if (topicState.HasValue)
            {
                throw new ArgumentException($"{id} is already an observable for Topic=[{topic}]");
            }
            for (int k = 1; k <= ConfigurationHelper.MaxQueryRetryCount; k++)
            {
                try
                {
                    IRegistryService registryService = ServiceProxy.Create <IRegistryService>(
                        ConfigurationHelper.RegistryServiceUri,
                        new ServicePartitionKey(PartitionResolver.Resolve(topic, ConfigurationHelper.RegistryServicePartitionCount)));
                    await registryService.RegisterObservableAsync(topic, id);

                    ActorEventSource.Current.Message($"Observable successfully registered.\r\n[Observable]: {id}\r\n[Publication]: Topic=[{topic}].");
                    await this.StateManager.SetStateAsync(topic, new Dictionary <Uri, ObserverInfo>());

                    return;
                }
                catch (FabricTransientException ex)
                {
                    ActorEventSource.Current.Error(ex);
                }
                catch (AggregateException ex)
                {
                    foreach (Exception e in ex.InnerExceptions)
                    {
                        ActorEventSource.Current.Error(e);
                    }
                    throw;
                }
                catch (Exception ex)
                {
                    ActorEventSource.Current.Error(ex);
                    throw;
                }
                await Task.Delay(ConfigurationHelper.BackoffQueryDelay);
            }
            throw new TimeoutException(Constants.RetryTimeoutExhausted);
        }
        public static async Task WriteMessageToMessageBoxAsync(Uri uri, Message message)
        {
            if (uri == null)
            {
                throw new ArgumentException($"The {nameof(uri)} parameter cannot be null.", nameof(uri));
            }
            for (int k = 1; k <= ConfigurationHelper.MaxQueryRetryCount; k++)
            {
                try
                {
                    IMessageBoxService messageBoxService =
                        ServiceProxy.Create <IMessageBoxService>(
                            ConfigurationHelper.MessageBoxServiceUri,
                            new ServicePartitionKey(PartitionResolver.Resolve(uri.AbsoluteUri, ConfigurationHelper.MessageBoxServicePartitionCount)));
                    await messageBoxService.WriteMessagesAsync(uri, new[] { message });

                    return;
                }
                catch (FabricTransientException ex)
                {
                    ActorEventSource.Current.Error(ex);
                    if (k == ConfigurationHelper.MaxQueryRetryCount)
                    {
                        throw;
                    }
                }
                catch (AggregateException ex)
                {
                    foreach (Exception innerException in ex.InnerExceptions)
                    {
                        ActorEventSource.Current.Error(innerException);
                    }
                    if (k == ConfigurationHelper.MaxQueryRetryCount)
                    {
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    ActorEventSource.Current.Error(ex);
                    if (k == ConfigurationHelper.MaxQueryRetryCount)
                    {
                        throw;
                    }
                }
                await Task.Delay(ConfigurationHelper.BackoffQueryDelay);
            }
        }
示例#4
0
        /// <summary>
        /// Unregisters an entity as observable for a given topic.
        /// This method is called by a management service or actor.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="useObserverAsProxy">Observable uses one observer for each cluster node as a proxy when true,
        /// it directly sends the message to all observers otherwise.</param>
        /// <returns>The asynchronous result of the operation.</returns>
        public async Task UnregisterObservableActorAsync(string topic, bool useObserverAsProxy)
        {
            EntityId id = await this.GetEntityIdAsync();

            if (id == null)
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(topic))
            {
                throw new ArgumentException($"The {nameof(topic)} parameter cannot be null.", nameof(topic));
            }
            ConditionalValue <Dictionary <Uri, ObserverInfo> > topicState = await this.StateManager.TryGetStateAsync <Dictionary <Uri, ObserverInfo> >(topic);

            if (!topicState.HasValue)
            {
                throw new ArgumentException($"{id} is not an observable for Topic=[{topic}]");
            }
            Dictionary <Uri, ObserverInfo> observerDictionary = topicState.Value;

            try
            {
                for (int k = 1; k <= ConfigurationHelper.MaxQueryRetryCount; k++)
                {
                    try
                    {
                        IRegistryService registryService = ServiceProxy.Create <IRegistryService>(ConfigurationHelper.RegistryServiceUri,
                                                                                                  new ServicePartitionKey(PartitionResolver.Resolve(topic, ConfigurationHelper.RegistryServicePartitionCount)));
                        await registryService.UnregisterObservableAsync(topic, id);

                        break;
                    }
                    catch (FabricTransientException ex)
                    {
                        ActorEventSource.Current.Error(ex);
                        if (k == ConfigurationHelper.MaxQueryRetryCount)
                        {
                            throw;
                        }
                    }
                    catch (AggregateException ex)
                    {
                        foreach (Exception innerException in ex.InnerExceptions)
                        {
                            ActorEventSource.Current.Error(innerException);
                        }
                        if (k == ConfigurationHelper.MaxQueryRetryCount)
                        {
                            throw;
                        }
                    }
                    catch (Exception ex)
                    {
                        ActorEventSource.Current.Error(ex);
                        if (k == ConfigurationHelper.MaxQueryRetryCount)
                        {
                            throw;
                        }
                    }
                    await Task.Delay(ConfigurationHelper.BackoffQueryDelay);
                }
                List <Task> taskList = new List <Task>();

                try
                {
                    if (useObserverAsProxy)
                    {
                        // observers are grouped by NodeName
                        taskList.AddRange(
                            observerDictionary.
                            Select(kvp => kvp.Value.EntityId).
                            GroupBy(e => e.NodeName).
                            Select(groupingByNodeName => ProcessingHelper.GetObserverProxyAndList(groupingByNodeName, true)).
                            Select(tuple => ProcessingHelper.UnregisterObservableAsync(topic, tuple.Item1, id, tuple.Item2)));
                    }
                    else
                    {
                        taskList.AddRange(
                            observerDictionary.Select(
                                observer => ProcessingHelper.UnregisterObservableAsync(topic, observer.Value.EntityId, id, null)));
                    }
                    await Task.WhenAll(taskList.ToArray());
                }
                catch (AggregateException ex)
                {
                    foreach (Exception e in ex.InnerExceptions)
                    {
                        ActorEventSource.Current.Error(e);
                    }
                }
                catch (Exception ex)
                {
                    ActorEventSource.Current.Error(ex);
                }
                await this.StateManager.TryRemoveStateAsync(topic);

                ActorEventSource.Current.Message($"Observable successfully unregistered.\r\n[Observable]: {id}\r\n[Publication]: Topic=[{topic}].");
            }
            catch (FabricTransientException ex)
            {
                ActorEventSource.Current.Error(ex);
            }
            catch (AggregateException ex)
            {
                foreach (Exception e in ex.InnerExceptions)
                {
                    ActorEventSource.Current.Error(e);
                }
                throw;
            }
            catch (Exception ex)
            {
                ActorEventSource.Current.Error(ex);
                throw;
            }
        }
示例#5
0
        /// <summary>
        /// Registers an entity as observable for a given topic.
        /// This method is called by a management service or actor.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <returns>The asynchronous result of the operation.</returns>
        public async Task RegisterObservableServiceAsync(string topic)
        {
            if (string.IsNullOrWhiteSpace(topic))
            {
                throw new ArgumentException($"The {nameof(topic)} parameter cannot be null.", nameof(topic));
            }
            for (int k = 1; k <= ConfigurationHelper.MaxQueryRetryCount; k++)
            {
                try
                {
                    EntityId id = await this.GetEntityIdAsync();

                    IRegistryService registryService =
                        ServiceProxy.Create <IRegistryService>(ConfigurationHelper.RegistryServiceUri,
                                                               new ServicePartitionKey(PartitionResolver.Resolve(topic, ConfigurationHelper.RegistryServicePartitionCount)));
                    await registryService.RegisterObservableAsync(topic, id);

                    ServiceEventSource.Current.Message($"Observable successfully registered.\r\n[Observable]: {id}\r\n[Publication]: Topic=[{topic}].");
                    IReliableDictionary <string, Dictionary <Uri, ObserverInfo> > topicsDictionary =
                        await this.StateManager.GetOrAddAsync <IReliableDictionary <string, Dictionary <Uri, ObserverInfo> > >(Constants.TopicDictionary);

                    using (ITransaction transaction = this.StateManager.CreateTransaction())
                    {
                        await topicsDictionary.AddOrUpdateAsync(transaction, topic, s => new Dictionary <Uri, ObserverInfo>(), (s, b) => b);

                        await transaction.CommitAsync();
                    }
                    return;
                }
                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);
        }