private ActivationAddress GenerateActivationAddress()
        {
            var grainId  = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateGuidKey(Guid.NewGuid()));
            var siloAddr = SiloAddress.New(new IPEndPoint(IPAddress.Loopback, 5000), ++generation);

            return(ActivationAddress.NewActivationAddress(siloAddr, grainId));
        }
Exemple #2
0
 private GrainAddress GenerateGrainAddress(SiloAddress siloAddress = null)
 {
     return(new GrainAddress
     {
         GrainId = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateGuidKey(Guid.NewGuid())),
         ActivationId = ActivationId.NewId(),
         SiloAddress = siloAddress ?? GenerateSiloAddress(),
         MembershipVersion = this.mockMembershipService.CurrentVersion,
     });
 }
        /// <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));
        }
Exemple #4
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));
        }
Exemple #5
0
        public void GrainIdShouldEncodeAndDecodePrimaryKeyGuidCorrectly()
        {
            const int repeat = 100;

            for (int i = 0; i < repeat; ++i)
            {
                Guid    expected = Guid.NewGuid();
                GrainId grainId  = GrainId.Create(GrainType.Create("foo"), GrainIdKeyExtensions.CreateGuidKey(expected));
                Guid    actual   = grainId.GetGuidKey();
                Assert.Equal(expected, actual); // Failed to encode and decode grain id
            }
        }
Exemple #6
0
        /// <summary>
        /// Returns the long representation of a grain primary key.
        /// </summary>
        /// <param name="grain">The grain to find the primary key for.</param>
        /// <returns>A long representing the primary key for this grain.</returns>
        public static long GetPrimaryKeyLong(this IAddressable grain)
        {
            var grainId = GetGrainId(grain);

            if (GrainIdKeyExtensions.TryGetIntegerKey(grainId, out var primaryKey, out _))
            {
                return(primaryKey);
            }

            if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId))
            {
                return(legacyId.GetPrimaryKeyLong());
            }

            throw new InvalidOperationException($"Unable to extract integer key from grain id {grainId}");
        }
Exemple #7
0
        public void GrainReference_Interning()
        {
            var grainId = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateGuidKey(Guid.NewGuid()));
            var g1      = GrainReference.FromGrainId(grainId, null);
            var g2      = GrainReference.FromGrainId(grainId, null);

            Assert.Equal(g1, g2); // Should be equal GrainReferences
            Assert.Same(g1, g2);  // Should be same / interned GrainReference object

            // Round-trip through Serializer
            var g3 = this.HostedCluster.SerializationManager.RoundTripSerializationForTesting(g1);

            Assert.Equal(g3, g1);
            Assert.Equal(g3, g2);
            Assert.Same(g3, g1);
            Assert.Same(g3, g2);
        }
Exemple #8
0
        /// <summary>
        /// Returns the Guid representation of a grain primary key.
        /// </summary>
        /// <param name="grain">The grain to find the primary key for.</param>
        /// <returns>A Guid representing the primary key for this grain.</returns>
        public static Guid GetPrimaryKey(this IAddressable grain)
        {
            var grainId = GetGrainId(grain);

            if (GrainIdKeyExtensions.TryGetGuidKey(grainId, out var guid))
            {
                return(guid);
            }

            if (LegacyGrainId.TryConvertFromGrainId(grainId, out var legacyId))
            {
                return(legacyId.GetPrimaryKey());
            }

            if (GrainIdKeyExtensions.TryGetIntegerKey(grainId, out var integerKey))
            {
                var N1 = integerKey;
                return(new Guid(0, 0, 0, (byte)N1, (byte)(N1 >> 8), (byte)(N1 >> 16), (byte)(N1 >> 24), (byte)(N1 >> 32), (byte)(N1 >> 40), (byte)(N1 >> 48), (byte)(N1 >> 56)));
            }

            throw new InvalidOperationException($"Unable to extract GUID key from grain id {grainId}");
        }
Exemple #9
0
        public void GrainId_ToFromPrintableString()
        {
            Guid    guid         = Guid.NewGuid();
            GrainId grainId      = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateGuidKey(guid));
            GrainId roundTripped = RoundTripGrainIdToParsable(grainId);

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

            string extKey = "Guid-ExtKey-1";

            guid         = Guid.NewGuid();
            grainId      = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateGuidKey(guid, extKey));
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Guid key + Extended Key

            grainId      = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateGuidKey(guid, null));
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Guid key + null Extended Key

            long key = random.Next();

            grainId      = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateIntegerKey(key));
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key

            extKey       = "Long-ExtKey-2";
            key          = random.Next();
            grainId      = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateIntegerKey(key, extKey));
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key + Extended Key

            key          = UniqueKey.NewKey(key).PrimaryKeyToLong();
            grainId      = GrainId.Create(GrainType.Create("test"), GrainIdKeyExtensions.CreateIntegerKey(key, extKey));
            roundTripped = RoundTripGrainIdToParsable(grainId);
            Assert.Equal(grainId, roundTripped); // GrainId.ToPrintableString -- Int64 key + null Extended Key
        }