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

            return(grainRef);
        }
Example #2
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        = LegacyGrainId.GetGrainId(implementation.GetTypeCode(interfaceType), primaryKey, null);

            return(this.Cast <TGrainInterface>(this.MakeGrainReferenceFromType(interfaceType, grainId.ToGrainId())));
        }
Example #3
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));
        }
Example #4
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        = LegacyGrainId.GetGrainId(implementation.GetTypeCode(interfaceType), primaryKey, keyExtension);

            return(this.Cast <TGrainInterface>(this.MakeGrainReferenceFromType(interfaceType, grainId.ToGrainId())));
        }
        public bool TryGetGrainType(Type grainClass, out GrainType grainType)
        {
            if (LegacyGrainId.IsLegacyGrainType(grainClass))
            {
                grainType = LegacyGrainId.GetGrainId(GrainInterfaceUtils.GetGrainClassTypeCode(grainClass), Guid.Empty).ToGrainId().Type;
                return(true);
            }

            grainType = default;
            return(false);
        }
Example #6
0
        public void GrainId_ToFromPrintableString()
        {
            Guid    guid         = Guid.NewGuid();
            GrainId grainId      = LegacyGrainId.GetGrainIdForTesting(guid);
            GrainId roundTripped = RoundTripGrainIdToParsable(grainId);

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

            string extKey = "Guid-ExtKey-1";

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

            grainId      = LegacyGrainId.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      = LegacyGrainId.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      = LegacyGrainId.GetGrainId(0, guid, extKey);
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key + Extended Key

            guid         = UniqueKey.NewKey(key).PrimaryKeyToGuid();
            grainId      = LegacyGrainId.GetGrainId(0, guid, null);
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key + null Extended Key
        }
Example #7
0
        public void DefaultDhtDirectory()
        {
            var grainId = LegacyGrainId.GetGrainId(DefaultDirectoryGrain.TYPECODE, Guid.NewGuid());

            Assert.Null(this.target.Resolve(grainId.ToGrainId().Type));
        }
Example #8
0
        public void UserProvidedDirectory()
        {
            var grainId = LegacyGrainId.GetGrainId(AzureTableDirectoryGrain.TYPECODE, Guid.NewGuid());

            Assert.Same(this.azureDirectory, this.target.Resolve(grainId.ToGrainId().Type));
        }
Example #9
0
        internal async Task Run_ActivationSched_Test1(TaskScheduler scheduler, bool bounceToThreadPool)
        {
            var grainId = LegacyGrainId.GetGrainId(0, Guid.NewGuid());
            var silo    = new MockSiloDetails
            {
                SiloAddress = SiloAddressUtils.NewLocalSiloAddress(23)
            };
            var grain = NonReentrentStressGrainWithoutState.Create(grainId, new GrainRuntime(Options.Create(new ClusterOptions()), silo, null, null, null, null, null, NullLoggerFactory.Instance));

            await Task.Factory.StartNew(() => grain.OnActivateAsync(), CancellationToken.None, TaskCreationOptions.None, scheduler).Unwrap();

            Task        wrapped     = null;
            var         wrapperDone = new TaskCompletionSource <bool>();
            var         wrappedDone = new TaskCompletionSource <bool>();
            Task <Task> wrapper     = new Task <Task>(() =>
            {
                this.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");
        }
 /// <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((GrainReference)this.grainFactory.GetGrain(LegacyGrainId.FromParsableString(LegacyGrainId.GetGrainId(RandomUtilities.NormalGrainTypeCode, grainId).ToParsableString())), new GrainState <TestState1> {
         State = new TestState1(), ETag = version
     }));
 }
 /// <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((GrainReference)this.grainFactory.GetGrain(LegacyGrainId.GetGrainId(UniqueKey.NewKey(grainId, UniqueKey.Category.Grain))), new GrainState <TestState1> {
         State = new TestState1(), ETag = version
     }));
 }
Example #12
0
 private GrainId GetGrainId(Type interafaceType, string grainPrimaryKey, string grainClassNamePrefix)
 {
     return(LegacyGrainId.GetGrainId(GetTypeCode(interafaceType, grainClassNamePrefix), grainPrimaryKey));
 }
Example #13
0
 private GrainId GetGrainId(Type interfaceType, Guid grainPrimaryKey, string keyExtension, string grainClassNamePrefix)
 {
     return(LegacyGrainId.GetGrainId(GetTypeCode(interfaceType, grainClassNamePrefix), grainPrimaryKey, keyExtension));
 }
Example #14
0
        /// <inheritdoc />
        public IGrain GetGrain(Type grainInterfaceType, long grainPrimaryKey, string keyExtension)
        {
            var grainId = LegacyGrainId.GetGrainId(GetTypeCode(grainInterfaceType), grainPrimaryKey, keyExtension);

            return(MakeGrainReference(grainInterfaceType, grainId.ToGrainId()));
        }
Example #15
0
        /// <inheritdoc />
        public IGrain GetGrain(Type grainInterfaceType, Guid grainPrimaryKey)
        {
            var grainId = LegacyGrainId.GetGrainId(GetTypeCode(grainInterfaceType), grainPrimaryKey);

            return(MakeGrainReference(grainInterfaceType, grainId.ToGrainId()));
        }