public static Task <ProducerProxy> NewProducerGrainsAsync(Guid streamId, string streamProvider, string streamNamespace, ILogger logger, IInternalGrainFactory grainFactory, Guid[] grainIds = null, int grainCount = 1)
        {
            grainCount = grainIds != null ? grainIds.Length : grainCount;
            if (grainCount < 1)
            {
                throw new ArgumentOutOfRangeException("grainCount", "The grain count must be at least one");
            }
            logger.LogInformation("ProducerProxy.NewProducerGrainsAsync: multiplexing {GrainCount} producer grains for stream {StreamId}.", grainCount, streamId);
            var grains = new IStreaming_ProducerGrain[grainCount];
            var dedup  = new Dictionary <Guid, IStreaming_ProducerGrain>();
            var producerGrainFullName = typeof(Streaming_ProducerGrain).FullName;

            for (var i = 0; i < grainCount; ++i)
            {
                if (grainIds != null)
                {
                    // we deduplicate the grain references to ensure that IEnumerable.Distinct() works as intended.
                    if (dedup.ContainsKey(grainIds[i]))
                    {
                        grains[i] = dedup[grainIds[i]];
                    }
                    else
                    {
                        var gref = grainFactory.GetGrain <IStreaming_ProducerGrain>(grainIds[i], producerGrainFullName);
                        grains[i]          = gref;
                        dedup[grainIds[i]] = gref;
                    }
                }
                else
                {
                    grains[i] = grainFactory.GetGrain <IStreaming_ProducerGrain>(Guid.NewGuid(), producerGrainFullName);
                }
            }
            return(NewProducerProxy(grains, streamId, streamProvider, streamNamespace, logger));
        }
        public static Task <ProducerProxy> NewProducerConsumerGrainsAsync(Guid streamId, string streamProvider, ILogger logger, int[] grainIds, bool useReentrantGrain, IInternalGrainFactory grainFactory)
        {
            int grainCount = grainIds.Length;

            if (grainCount < 1)
            {
                throw new ArgumentOutOfRangeException("grainIds", "The grain count must be at least one");
            }
            logger.LogInformation("ConsumerProxy.NewProducerConsumerGrainsAsync: multiplexing {GrainCount} producer grains for stream {StreamId}.", grainCount, streamId);
            var grains = new IStreaming_ProducerGrain[grainCount];
            var dedup  = new Dictionary <int, IStreaming_ProducerGrain>();

            for (var i = 0; i < grainCount; ++i)
            {
                // we deduplicate the grain references to ensure that IEnumerable.Distinct() works as intended.
                if (dedup.ContainsKey(grainIds[i]))
                {
                    grains[i] = dedup[grainIds[i]];
                }
                else
                {
                    if (useReentrantGrain)
                    {
                        grains[i] = grainFactory.GetGrain <IStreaming_Reentrant_ProducerConsumerGrain>(grainIds[i]);
                    }
                    else
                    {
                        var grainFullName = typeof(Streaming_ProducerConsumerGrain).FullName;
                        grains[i] = grainFactory.GetGrain <IStreaming_ProducerConsumerGrain>(grainIds[i], grainFullName);
                    }
                    dedup[grainIds[i]] = grains[i];
                }
            }
            return(NewProducerProxy(grains, streamId, streamProvider, null, logger));
        }
Example #3
0
        internal (GrainReference GrainReference, GrainState <TestState1> GrainState)  GetTestReferenceAndState(long grainId, string version)
        {
            var grainReference = (GrainReference)_grainFactory.GetGrain(GrainId.Create("my-grain-type", grainId.ToString("X")));
            var grainState     = new GrainState <TestState1> {
                State = new TestState1(), ETag = version
            };

            return(grainReference, grainState);
        }
        public static async Task <int> GetNumActivations(IEnumerable <IGrain> targets, IInternalGrainFactory grainFactory)
        {
            var grainIds = targets.Distinct().Where(t => t is GrainReference).Select(t => ((GrainReference)t).GrainId).ToArray();
            IManagementGrain systemManagement = grainFactory.GetGrain <IManagementGrain>(0);
            var tasks = grainIds.Select(g => systemManagement.GetGrainActivationCount((GrainReference)grainFactory.GetGrain(g))).ToArray();
            await Task.WhenAll(tasks);

            return(tasks.Sum(t => t.Result));
        }
Example #5
0
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="streamId">The stream ID to use for the grain ID construction.</param>
        /// <param name="streamSubscriber">The GrainBindings for the grain to create</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(
            IInternalGrainFactory grainFactory,
            InternalStreamId streamId,
            StreamSubscriber streamSubscriber)
        {
            var grainId = streamSubscriber.GetGrainId(streamId);

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
Example #6
0
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="streamId">The stream ID to use for the grain ID construction.</param>
        /// <param name="implTypeCode">The type code of the grain interface.</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(IInternalGrainFactory grainFactory, StreamId streamId,
                                                               int implTypeCode)
        {
            var keyExtension = grainsWithKeyExtensions.Contains(implTypeCode)
                ? streamId.Namespace
                : null;
            GrainId grainId = LegacyGrainId.GetGrainId(implTypeCode, streamId.Guid, keyExtension);

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="streamId">The stream ID to use for the grain ID construction.</param>
        /// <param name="subscriber">The subscriber prototype.</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(
            IInternalGrainFactory grainFactory,
            StreamId streamId,
            StreamSubscriber subscriber)
        {
            var keyExtension = subscriber.IncludeNamespaceInGrainId ? streamId.Namespace : null;
            var grainId      = GrainId.Create(subscriber.GrainType, GrainIdKeyExtensions.CreateGuidKey(streamId.Guid, keyExtension));

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
Example #8
0
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="streamId">The stream ID to use for the grain ID construction.</param>
        /// <param name="subscriber">The subscriber prototype.</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(
            IInternalGrainFactory grainFactory,
            InternalStreamId streamId,
            StreamSubscriber subscriber)
        {
            var keyExtension = subscriber.IncludeNamespaceInGrainId ? streamId.GetNamespace() : null;
            // TODO BPETIT: CHANGE THIS TO STRING
            var grainId = GrainId.Create(subscriber.GrainType, GrainIdKeyExtensions.CreateGuidKey(Guid.Parse(streamId.GetKeyAsString()), keyExtension));

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
        public static ConsumerProxy NewConsumerGrainAsync_WithoutBecomeConsumer(Guid consumerGrainId, ILogger logger, IInternalGrainFactory grainFactory, string grainClassName = "")
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            if (string.IsNullOrEmpty(grainClassName))
            {
                grainClassName = typeof(Streaming_ConsumerGrain).FullName;
            }

            var grains = new IStreaming_ConsumerGrain[1];

            grains[0] = grainFactory.GetGrain <IStreaming_ConsumerGrain>(consumerGrainId, grainClassName);
            ConsumerProxy newObj = new ConsumerProxy(grains, logger, grainFactory);

            return(newObj);
        }
Example #10
0
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="primaryKey">The primary key of the grain.</param>
        /// <param name="implTypeCode">The type code of the grain interface.</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(IInternalGrainFactory grainFactory, Guid primaryKey, int implTypeCode)
        {
            GrainId grainId = GrainId.GetGrainId(implTypeCode, primaryKey);

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
Example #11
0
        internal static IClientGatewayObserver GetObserver(IInternalGrainFactory grainFactory, ClientGrainId clientId)
        {
            var observerId = ObserverGrainId.Create(clientId, ScopedId);

            return(grainFactory.GetGrain <IClientGatewayObserver>(observerId.GrainId));
        }