Exemple #1
0
 /// <inheritdoc/>
 public void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     info.AddValue("tv", GrainType.UnsafeGetArray(Type));
     info.AddValue("th", Type.GetHashCode());
     info.AddValue("kv", IdSpan.UnsafeGetArray(Key));
     info.AddValue("kh", Key.GetHashCode());
 }
Exemple #2
0
        public bool TryGet(GrainType grainType, out IGrainContextActivator activator)
        {
            if (!_grainClassMap.TryGetGrainClass(grainType, out var grainClass) ||
                !typeof(Grain).IsAssignableFrom(grainClass))
            {
                activator = null;
                return(false);
            }

            var             sharedComponents  = _sharedComponentsResolver.GetComponents(grainType);
            IGrainActivator instanceActivator = sharedComponents.GetComponent <IGrainActivator>();

            if (instanceActivator is null)
            {
                throw new InvalidOperationException($"Could not find a suitable {nameof(IGrainActivator)} implementation for grain type {grainType}");
            }

            var(activationCollector, collectionAgeLimit) = GetCollectionAgeLimit(grainType, grainClass);

            activator = new ActivationDataActivator(
                instanceActivator,
                _placementStrategyResolver.GetPlacementStrategy(grainType),
                activationCollector,
                collectionAgeLimit,
                _messagingOptions,
                _maxWarningRequestProcessingTime,
                _maxRequestProcessingTime,
                _loggerFactory,
                _serviceProvider,
                _grainRuntime ??= _serviceProvider.GetRequiredService <IGrainRuntime>(),
                _grainReferenceActivator,
                sharedComponents,
                _activationMessageScheduler ??= _serviceProvider.GetRequiredService <ActivationMessageScheduler>());
            return(true);
        }
Exemple #3
0
 public readonly void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     info.AddValue("tv", GrainType.UnsafeGetArray(_type));
     info.AddValue("th", _type.GetHashCode());
     info.AddValue("kv", SpanId.UnsafeGetArray(_key));
     info.AddValue("kh", _key.GetHashCode());
 }
        /// <summary>
        /// Converts the provided <see cref="GrainReference"/> to a string which can be parsed by <see cref="GrainReferenceKeyStringConverter"/>.
        /// </summary>
        /// <param name="grainReference">
        /// The grain reference.
        /// </param>
        /// <returns>The key string.</returns>
        public static string ToKeyString(this GrainReference grainReference)
        {
            var id         = grainReference.GrainId;
            var typeString = Convert.ToBase64String(GrainType.UnsafeGetArray(id.Type));
            var keyString  = Convert.ToBase64String(IdSpan.UnsafeGetArray(id.Key));

            return($"{typeString}_{keyString}");
        }
Exemple #5
0
 private (IActivationCollector, TimeSpan) GetCollectionAgeLimit(GrainType grainType, Type grainClass)
 {
     if (_siloManifest.Grains.TryGetValue(grainType, out var properties) &&
         properties.Properties.TryGetValue(WellKnownGrainTypeProperties.IdleDeactivationPeriod, out var idleTimeoutString))
     {
         if (string.Equals(idleTimeoutString, WellKnownGrainTypeProperties.IndefiniteIdleDeactivationPeriodValue))
         {
             return(null, default);
Exemple #6
0
        public static string SystemTargetName(GrainType id)
        {
            if (singletonSystemTargetNames.TryGetValue(id, out var name))
            {
                return(name);
            }

            return(string.Empty);
        }
        /// <summary>
        /// Creates a new <see cref="SystemTargetGrainId"/> instance.
        /// </summary>
        public static SystemTargetGrainId Create(GrainType kind, SiloAddress address, string extraIdentifier)
        {
            if (extraIdentifier is string)
            {
                return(new SystemTargetGrainId(GrainId.Create(kind, address.ToParsableString() + SegmentSeparator + extraIdentifier)));
            }

            return(Create(kind, address));
        }
Exemple #8
0
        public static string SystemTargetName(GrainType id)
        {
            if (singletonSystemTargetNames.TryGetValue(id, out var name))
            {
                return(name);
            }

            return(id.ToStringUtf8());
        }
Exemple #9
0
        /// <summary>
        /// Converts the provided value into a <see cref="GrainReference"/>.
        /// </summary>
        public GrainReference FromKeyString(string referenceString)
        {
            var splits = referenceString.Split('_');
            var type   = new GrainType(Convert.FromBase64String(splits[0]));
            var key    = new IdSpan(Convert.FromBase64String(splits[1]));
            var id     = new GrainId(type, key);

            return(_activator.CreateReference(id, default));
        }
        /// <summary>
        /// Returns the generic grain type corresponding to the provided value.
        /// </summary>
        public static bool TryParse(GrainType grainType, out GenericGrainType result)
        {
            if (TypeConverterExtensions.IsGenericType(grainType.Value))
            {
                result = new GenericGrainType(grainType);
                return(true);
            }

            result = default;
            return(false);
        }
Exemple #11
0
 public GrainReferenceShared(
     GrainType graintype,
     GrainInterfaceType grainInterfaceType,
     IGrainReferenceRuntime runtime,
     InvokeMethodOptions invokeMethodOptions)
 {
     this.GrainType           = graintype;
     this.InterfaceType       = grainInterfaceType;
     this.Runtime             = runtime;
     this.InvokeMethodOptions = invokeMethodOptions;
 }
        private GrainTypeComponents Create(GrainType grainType)
        {
            var result     = new GrainTypeComponents();
            var properties = _resolver.GetGrainProperties(grainType);

            foreach (var configurator in _configurators)
            {
                configurator.Configure(grainType, properties, result);
            }

            return(result);
        }
        /// <summary>
        /// Converts the provided grain reference key <see cref="string"/> into a <see cref="GrainReference"/>.
        /// </summary>
        /// <param name="referenceString">
        /// The string representation of a grain reference.
        /// </param>
        /// <returns>The grain reference.</returns>
        public GrainReference FromKeyString(string referenceString)
        {
            var i = referenceString.IndexOf('_');

            if (i < 0)
            {
                throw new ArgumentException(nameof(referenceString));
            }
            var type = new GrainType(Convert.FromBase64String(referenceString.Substring(0, i)));
            var key  = new IdSpan(Convert.FromBase64String(referenceString.Substring(i + 1)));
            var id   = new GrainId(type, key);

            return(_activator.CreateReference(id, default));
        }
Exemple #14
0
 public GrainReferenceShared(
     GrainType grainType,
     GrainInterfaceType grainInterfaceType,
     ushort interfaceVersion,
     IGrainReferenceRuntime runtime,
     InvokeMethodOptions invokeMethodOptions,
     IServiceProvider serviceProvider)
 {
     this.GrainType           = grainType;
     this.InterfaceType       = grainInterfaceType;
     this.Runtime             = runtime;
     this.InvokeMethodOptions = invokeMethodOptions;
     this.ServiceProvider     = serviceProvider;
     this.InterfaceVersion    = interfaceVersion;
 }
Exemple #15
0
        /// <summary>
        /// Creates a new <see cref="SystemTargetGrainId"/> instance.
        /// </summary>
        public static SystemTargetGrainId Create(GrainType kind, SiloAddress address, string extraIdentifier)
        {
            var addr = address.ToUtf8String();

            if (extraIdentifier is string)
            {
                var extraLen = Encoding.UTF8.GetByteCount(extraIdentifier);
                var buf      = new byte[addr.Length + 1 + extraLen];
                addr.CopyTo(buf.AsSpan());
                buf[addr.Length] = (byte)SegmentSeparator;
                Encoding.UTF8.GetBytes(extraIdentifier, 0, extraIdentifier.Length, buf, addr.Length + 1);
                addr = buf;
            }

            return(new SystemTargetGrainId(new GrainId(kind, new IdSpan(addr))));
        }
Exemple #16
0
        private GrainType GetGrainType()
        {
            // TODO: intern
            var key = this.Key;

            if (this.IsSystemTarget)
            {
                return(GrainType.Create($"{GrainTypePrefix.SystemTargetPrefix}{key.TypeCodeData:X16}"));
            }
            else if (this.IsClient)
            {
                return(GrainType.Create($"{GrainTypePrefix.ClientPrefix}.{key.TypeCodeData:X16}"));
            }
            else
            {
                return(GrainType.Create($"{GrainTypePrefix.LegacyGrainPrefix}{key.TypeCodeData:X16}"));
            }
        }
        public bool TryGet(GrainType grainType, out IGrainContextActivator activator)
        {
            if (!_grainClassMap.TryGetGrainClass(grainType, out var grainClass) ||
                !typeof(Grain).IsAssignableFrom(grainClass))
            {
                activator = null;
                return(false);
            }

            var sharedContext     = _sharedComponentsResolver.GetComponents(grainType);
            var instanceActivator = sharedContext.GetComponent <IGrainActivator>();

            if (instanceActivator is null)
            {
                throw new InvalidOperationException($"Could not find a suitable {nameof(IGrainActivator)} implementation for grain type {grainType}");
            }

            var innerActivator = new ActivationDataActivator(
                instanceActivator,
                _serviceProvider,
                sharedContext,
                _workItemGroupLogger,
                _activationTaskSchedulerLogger,
                _schedulerStatisticsGroup,
                _schedulingOptions,
                _statisticsOptions);

            if (sharedContext.PlacementStrategy is StatelessWorkerPlacement)
            {
                activator = new StatelessWorkerActivator(sharedContext, innerActivator);
            }
            else
            {
                activator = innerActivator;
            }

            return(true);
        }
        private ActivatorEntry CreateActivator(GrainType grainType)
        {
            lock (_lockObj)
            {
                if (!_activators.TryGetValue(grainType, out var configuredActivator))
                {
                    IGrainContextActivator unconfiguredActivator = null;
                    foreach (var provider in this._activatorProviders)
                    {
                        if (provider.TryGet(grainType, out unconfiguredActivator))
                        {
                            break;
                        }
                    }

                    if (unconfiguredActivator is null)
                    {
                        throw new InvalidOperationException($"Unable to find an {nameof(IGrainContextActivatorProvider)} for grain type {grainType}");
                    }

                    var properties = _resolver.GetGrainProperties(grainType);
                    List <IConfigureGrainContext> configureActions = new List <IConfigureGrainContext>();
                    foreach (var provider in _configuratorProviders)
                    {
                        if (provider.TryGetConfigurator(grainType, properties, out var configurator))
                        {
                            configureActions.Add(configurator);
                        }
                    }

                    var applicableConfigureActions = configureActions.Count > 0 ? configureActions.ToArray() : Array.Empty <IConfigureGrainContext>();
                    configuredActivator = new ActivatorEntry(unconfiguredActivator, applicableConfigureActions);
                    _activators         = _activators.SetItem(grainType, configuredActivator);
                }

                return(configuredActivator);
            }
        }
Exemple #19
0
 /// <summary>
 /// Creates a new <see cref="GrainType"/> instance.
 /// </summary>
 private GrainId(SerializationInfo info, StreamingContext context)
 {
     Type = new GrainType(IdSpan.UnsafeCreate((byte[])info.GetValue("tv", typeof(byte[])), info.GetInt32("th")));
     Key  = IdSpan.UnsafeCreate((byte[])info.GetValue("kv", typeof(byte[])), info.GetInt32("kh"));
 }
Exemple #20
0
 public static GrainType CreateGrainTypeForSystemTarget(int typeCode)
 {
     return(GrainType.Create($"{GrainTypePrefix.SystemTargetPrefix}{typeCode:X16}"));
 }
Exemple #21
0
 public static GrainType CreateGrainTypeForGrain(int typeCode)
 {
     return(GrainType.Create($"{GrainTypePrefix.LegacyGrainPrefix}{typeCode:X16}"));
 }
 /// <inheritdoc />
 TGrainInterface IInternalGrainFactory.GetSystemTarget <TGrainInterface>(GrainType grainType, SiloAddress destination)
 {
     return(this.grainFactory.GetSystemTarget <TGrainInterface>(grainType, destination));
 }
 /// <summary>
 /// Populates grain properties to specify the preferred placement strategy.
 /// </summary>
 /// <param name="services">The service provider.</param>
 /// <param name="grainClass">The grain class.</param>
 /// <param name="grainType">The grain type.</param>
 /// <param name="properties">The grain properties which will be populated by this method call.</param>
 public virtual void PopulateGrainProperties(IServiceProvider services, Type grainClass, GrainType grainType, Dictionary <string, string> properties)
 {
     properties[WellKnownGrainTypeProperties.PlacementStrategy] = this.GetType().Name;
 }
 public static GrainId GetSystemTargetGrainId(GrainType kind, SiloAddress address)
 {
     return(GrainId.Create(kind, address.ToParsableString()));
 }
Exemple #25
0
 /// <summary>
 /// Creates a new <see cref="SystemTargetGrainId"/> instance.
 /// </summary>
 public static SystemTargetGrainId Create(GrainType kind, SiloAddress address) => new SystemTargetGrainId(new GrainId(kind, new IdSpan(address.ToUtf8String())));
Exemple #26
0
 /// <summary>
 /// Creates a system target <see cref="GrainType"/> with the provided name.
 /// </summary>
 public static GrainType CreateGrainType(string name) => GrainType.Create($"{GrainTypePrefix.SystemTargetPrefix}{name}");
Exemple #27
0
 internal static GrainId CreateGrainServiceGrainId(GrainType grainType, SiloAddress address)
 => new GrainId(grainType, new IdSpan(address.ToUtf8String()));
Exemple #28
0
 /// <summary>
 /// Creates a new <see cref="GrainType"/> instance.
 /// </summary>
 public static GrainId Create(GrainType type, IdSpan key) => new GrainId(type, key);
Exemple #29
0
 /// <summary>
 /// Creates a new <see cref="GrainType"/> instance.
 /// </summary>
 public static GrainId Create(GrainType type, string key) => new GrainId(type, IdSpan.Create(key));
Exemple #30
0
 /// <summary>
 /// Creates a new <see cref="GrainType"/> instance.
 /// </summary>
 public static GrainId Create(string type, string key) => Create(GrainType.Create(type), key);