Пример #1
0
        private GrainReference MakeTestGrainReference()
        {
            GrainId        regularGrainId = GrainId.GetGrainId(12345, Guid.NewGuid(), "foo/bar\\#baz?");
            GrainReference grainRef       = this.ClusterFixture.InternalGrainFactory.GetGrain(regularGrainId);

            return(grainRef);
        }
Пример #2
0
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="primaryKey">The primary key of the grain.</param>
        /// <param name="grainClassName">The name of the grain class to instantiate.</param>
        /// <param name="grainIfaceName">The name of the an IGrain-derived interface that `className` implements (required by MakeGrainReferenceInternal)</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(Guid primaryKey, int implTypeCode)
        {
            GrainId      grainId     = GrainId.GetGrainId(implTypeCode, primaryKey);
            IAddressable addressable = GrainReference.FromGrainId(grainId);

            return(GrainFactory.Cast <IStreamConsumerExtension>(addressable));
        }
        /// <summary>
        /// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
        /// <see paramref="outputGrainInterfaceType"/>. It is the caller's responsibility to ensure <see paramref="outputGrainInterfaceType"/>
        /// extends IGrain, as there is no compile-time checking for this overload.
        ///
        /// The main use-case is when you want to get a grain whose type is unknown at compile time.
        /// </summary>
        /// <param name="grainPrimaryKey">the primary key of the grain</param>
        /// <param name="grainInterfaceType">the runtime type of the grain interface</param>
        /// <param name="outputGrainInterfaceType">the type of grain interface that should be returned</param>
        /// <returns></returns>
        public IGrain GetGrain(string grainPrimaryKey, Type grainInterfaceType, Type outputGrainInterfaceType)
        {
            this.runtimeClient.GrainTypeResolver.TryGetGrainClassData(grainInterfaceType, out GrainClassData implementation, string.Empty);
            var grainId = GrainId.GetGrainId(implementation.GetTypeCode(grainInterfaceType), grainPrimaryKey);

            return((IGrain)this.Cast(this.MakeGrainReferenceFromType(grainInterfaceType, grainId), outputGrainInterfaceType));
        }
        /// <summary>
        /// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
        /// outputGrainInterfaceType.
        ///
        /// The main use-case is when you want to get a grain whose type is unknown at compile time (e.g. generic type parameters).
        /// </summary>
        /// <typeparam name="OutputGrainInterfaceType">The output type of the grain</typeparam>
        /// <param name="grainPrimaryKey">the primary key of the grain</param>
        /// <param name="grainInterfaceType">the runtime type of the grain interface</param>
        /// <returns>the requested grain with the given grainID and grainInterfaceType</returns>
        public OutputGrainInterfaceType GetGrain <OutputGrainInterfaceType>(Guid grainPrimaryKey, Type grainInterfaceType)
            where OutputGrainInterfaceType : IGrain
        {
            this.runtimeClient.GrainTypeResolver.TryGetGrainClassData(grainInterfaceType, out GrainClassData implementation, string.Empty);
            var grainId = GrainId.GetGrainId(implementation.GetTypeCode(grainInterfaceType), grainPrimaryKey);

            return(this.Cast <OutputGrainInterfaceType>(this.MakeGrainReferenceFromType(grainInterfaceType, grainId)));
        }
Пример #5
0
        /// <inheritdoc />
        public TGrainInterface GetGrain <TGrainInterface>(long primaryKey, string grainClassNamePrefix = null) where TGrainInterface : IGrainWithIntegerKey
        {
            Type interfaceType  = typeof(TGrainInterface);
            var  implementation = this.GetGrainClassData(interfaceType, grainClassNamePrefix);
            var  grainId        = GrainId.GetGrainId(implementation.GetTypeCode(interfaceType), primaryKey, null);

            return(this.Cast <TGrainInterface>(this.MakeGrainReferenceFromType(interfaceType, grainId)));
        }
Пример #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 = GrainId.GetGrainId(implTypeCode, streamId.Guid, keyExtension);

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
Пример #7
0
        /// <inheritdoc />
        public TGrainInterface GetGrain <TGrainInterface>(Guid primaryKey, string keyExtension, string grainClassNamePrefix = null)
            where TGrainInterface : IGrainWithGuidCompoundKey
        {
            GrainFactoryBase.DisallowNullOrWhiteSpaceKeyExtensions(keyExtension);

            Type interfaceType  = typeof(TGrainInterface);
            var  implementation = this.GetGrainClassData(interfaceType, grainClassNamePrefix);
            var  grainId        = GrainId.GetGrainId(implementation.GetTypeCode(interfaceType), primaryKey, keyExtension);

            return(this.Cast <TGrainInterface>(this.MakeGrainReferenceFromType(interfaceType, grainId)));
        }
Пример #8
0
        private static GrainId ConstructGrainId(string[] args, string operation)
        {
            if (args == null || args.Length < 2)
            {
                PrintUsage();
                return(null);
            }
            string interfaceTypeCodeOrImplClassName = args[0];
            int    interfaceTypeCodeDataLong;
            long   implementationTypeCode;

            var grainTypeResolver = (IGrainTypeResolver)client.ServiceProvider.GetService(typeof(IGrainTypeResolver));

            if (int.TryParse(interfaceTypeCodeOrImplClassName, out interfaceTypeCodeDataLong))
            {
                // parsed it as int, so it is an interface type code.
                implementationTypeCode =
                    GetImplementation(grainTypeResolver, interfaceTypeCodeDataLong).GrainTypeCode;
            }
            else
            {
                // interfaceTypeCodeOrImplClassName is the implementation class name
                implementationTypeCode = GetImplementation(grainTypeResolver, interfaceTypeCodeOrImplClassName).GrainTypeCode;
            }

            var     grainIdStr = args[1];
            GrainId grainId    = null;
            long    grainIdLong;
            Guid    grainIdGuid;

            if (long.TryParse(grainIdStr, out grainIdLong))
            {
                grainId = GrainId.GetGrainId(implementationTypeCode, grainIdLong);
            }
            else if (Guid.TryParse(grainIdStr, out grainIdGuid))
            {
                grainId = GrainId.GetGrainId(implementationTypeCode, grainIdGuid);
            }
            else
            {
                grainId = GrainId.GetGrainId(implementationTypeCode, grainIdStr);
            }

            WriteStatus(string.Format("**Full Grain Id to {0} is: GrainId = {1}", operation, grainId.ToFullString()));
            return(grainId);
        }
Пример #9
0
        public void GrainId_ToFromPrintableString()
        {
            Guid    guid         = Guid.NewGuid();
            GrainId grainId      = GrainId.GetGrainIdForTesting(guid);
            GrainId roundTripped = RoundTripGrainIdToParsable(grainId);

            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Guid key

            string extKey = "Guid-ExtKey-1";

            guid         = Guid.NewGuid();
            grainId      = GrainId.GetGrainId(0, guid, extKey);
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Guid key + Extended Key

            grainId      = GrainId.GetGrainId(0, guid, null);
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Guid key + null Extended Key

            long key = random.Next();

            guid         = UniqueKey.NewKey(key).PrimaryKeyToGuid();
            grainId      = GrainId.GetGrainIdForTesting(guid);
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key

            extKey       = "Long-ExtKey-2";
            key          = random.Next();
            guid         = UniqueKey.NewKey(key).PrimaryKeyToGuid();
            grainId      = GrainId.GetGrainId(0, guid, extKey);
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key + Extended Key

            guid         = UniqueKey.NewKey(key).PrimaryKeyToGuid();
            grainId      = GrainId.GetGrainId(0, guid, null);
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key + null Extended Key
        }
Пример #10
0
        /// <inheritdoc />
        public IGrain GetGrain(Type grainInterfaceType, long grainPrimaryKey, string keyExtension)
        {
            var grainId = GrainId.GetGrainId(GetTypeCode(grainInterfaceType), grainPrimaryKey, keyExtension);

            return(MakeGrainReference(grainInterfaceType, grainId));
        }
Пример #11
0
 internal static GrainId ComposeGrainId(GrainClassData implementation, string primaryKey, Type interfaceType)
 {
     return(GrainId.GetGrainId(implementation.GetTypeCode(interfaceType), primaryKey));
 }
Пример #12
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));
        }
Пример #13
0
        /// <inheritdoc />
        public IGrain GetGrain(Type grainInterfaceType, long grainPrimaryKey)
        {
            var grainId = GrainId.GetGrainId(GetTypeCode(grainInterfaceType), grainPrimaryKey);

            return(MakeGrainReference(grainInterfaceType, grainId));
        }
Пример #14
0
        /// <summary> Read an <c>GrainId</c> value from the stream. </summary>
        /// <returns>Data from current position in stream, converted to the appropriate output type.</returns>
        internal static GrainId ReadGrainId(this IBinaryTokenStreamReader @this)
        {
            UniqueKey key = @this.ReadUniqueKey();

            return(GrainId.GetGrainId(key));
        }
Пример #15
0
        internal async Task Run_ActivationSched_Test1(TaskScheduler scheduler, bool bounceToThreadPool)
        {
            var grainId = GrainId.GetGrainId(0, Guid.NewGuid());
            var grain   = new NonReentrentStressGrainWithoutState(grainId, new GrainRuntime(Guid.NewGuid(), null, null, null, null, null, null));
            await grain.OnActivateAsync();

            Task        wrapped     = null;
            var         wrapperDone = new TaskCompletionSource <bool>();
            var         wrappedDone = new TaskCompletionSource <bool>();
            Task <Task> wrapper     = new Task <Task>(() =>
            {
                output.WriteLine("#0 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
                                 SynchronizationContext.Current, TaskScheduler.Current);

                Task t1 = grain.Test1();

                Action wrappedDoneAction = () => { wrappedDone.SetResult(true); };

                if (bounceToThreadPool)
                {
                    wrapped = t1.ContinueWith(_ => wrappedDoneAction(),
                                              CancellationToken.None,
                                              TaskContinuationOptions.ExecuteSynchronously,
                                              TaskScheduler.Default);
                }
                else
                {
                    wrapped = t1.ContinueWith(_ => wrappedDoneAction());
                }
                wrapperDone.SetResult(true);
                return(wrapped);
            });

            wrapper.Start(scheduler);
            await wrapper;

            var timeoutLimit = TimeSpan.FromSeconds(1);

            try
            {
                await wrapperDone.Task.WithTimeout(timeoutLimit);
            }
            catch (TimeoutException)
            {
                Assert.True(false, "Result did not arrive before timeout " + timeoutLimit);
            }
            bool done = wrapperDone.Task.Result;

            Assert.True(done, "Wrapper Task finished");
            Assert.True(wrapper.IsCompleted, "Wrapper Task completed");

            //done = wrapped.Wait(TimeSpan.FromSeconds(12));
            //Assert.True(done, "Wrapped Task not timeout");
            await wrapped;

            try
            {
                await wrappedDone.Task.WithTimeout(timeoutLimit);
            }
            catch (TimeoutException)
            {
                Assert.True(false, "Result did not arrive before timeout " + timeoutLimit);
            }
            done = wrappedDone.Task.Result;
            Assert.True(done, "Wrapped Task should be finished");
            Assert.True(wrapped.IsCompleted, "Wrapped Task completed");
        }
Пример #16
0
 private static GrainId ComposeGrainId(int baseTypeCode, string primaryKey, Type interfaceType)
 {
     return(GrainId.GetGrainId(ComposeGenericTypeCode(interfaceType, baseTypeCode), primaryKey));
 }
Пример #17
0
        /// <summary> Read an <c>GrainId</c> value from the stream. </summary>
        /// <returns>Data from current position in stream, converted to the appropriate output type.</returns>
        internal static GrainId ReadGrainId <TReader>(this TReader @this) where TReader : IBinaryTokenStreamReader
        {
            UniqueKey key = @this.ReadUniqueKey();

            return(GrainId.GetGrainId(key));
        }
Пример #18
0
 /// <summary>
 /// Creates a new grain and a grain reference pair.
 /// </summary>
 /// <param name="grainId">The grain ID.</param>
 /// <param name="version">The initial version of the state.</param>
 /// <returns>A grain reference and a state pair.</returns>
 internal Tuple <GrainReference, GrainState <TestState1> > GetTestReferenceAndState(long grainId, string version)
 {
     return(Tuple.Create(this.grainFactory.GetGrain(GrainId.GetGrainId(UniqueKey.NewKey(grainId, UniqueKey.Category.Grain))), new GrainState <TestState1> {
         State = new TestState1(), ETag = version
     }));
 }
Пример #19
0
 /// <summary>
 /// Creates a new grain and a grain reference pair.
 /// </summary>
 /// <param name="grainId">The grain ID.</param>
 /// <param name="version">The initial version of the state.</param>
 /// <returns>A grain reference and a state pair.</returns>
 internal Tuple <GrainReference, GrainState <TestState1> > GetTestReferenceAndState(string grainId, string version)
 {
     return(Tuple.Create(this.grainFactory.GetGrain(GrainId.FromParsableString(GrainId.GetGrainId(RandomUtilities.NormalGrainTypeCode, grainId).ToParsableString())), new GrainState <TestState1> {
         State = new TestState1(), ETag = version
     }));
 }
Пример #20
0
 internal static GrainId ComposeGrainId(GrainClassData implementation, Guid primaryKey, Type interfaceType, string keyExt = null)
 {
     return(GrainId.GetGrainId(implementation.GetTypeCode(interfaceType), primaryKey, keyExt));
 }
Пример #21
0
        /// <summary> Read an <c>GrainId</c> value from the stream. </summary>
        /// <returns>Data from current position in stream, converted to the appropriate output type.</returns>
        internal GrainId ReadGrainId()
        {
            UniqueKey key = ReadUniqueKey();

            return(GrainId.GetGrainId(key));
        }
Пример #22
0
 internal static Tuple <GrainReference, GrainState <TestState1> > GetTestReferenceAndState(long grainId, string version)
 {
     return(new Tuple <GrainReference, GrainState <TestState1> >(GrainReference.FromGrainId(GrainId.GetGrainId(UniqueKey.NewKey(grainId, UniqueKey.Category.Grain))), new GrainState <TestState1> {
         State = new TestState1(), ETag = version
     }));
 }