Пример #1
0
        private async Task CreateGrainServices(GrainServiceConfigurations grainServiceConfigurations)
        {
            foreach (var serviceConfig in grainServiceConfigurations.GrainServices)
            {
                // Construct the Grain Service
                var serviceType = System.Type.GetType(serviceConfig.Value.ServiceType);
                if (serviceType == null)
                {
                    throw new Exception(String.Format("Cannot find Grain Service type {0} of Grain Service {1}", serviceConfig.Value.ServiceType, serviceConfig.Value.Name));
                }

                var grainServiceInterfaceType = serviceType.GetInterfaces().FirstOrDefault(x => x.GetInterfaces().Contains(typeof(IGrainService)));
                if (grainServiceInterfaceType == null)
                {
                    throw new Exception(String.Format("Cannot find an interface on {0} which implements IGrainService", serviceConfig.Value.ServiceType));
                }

                var typeCode     = GrainInterfaceUtils.GetGrainClassTypeCode(grainServiceInterfaceType);
                var grainId      = (IGrainIdentity)GrainId.GetGrainServiceGrainId(0, typeCode);
                var grainService = (GrainService)ActivatorUtilities.CreateInstance(this.Services, serviceType, grainId, serviceConfig.Value);
                RegisterSystemTarget(grainService);

                await this.scheduler.QueueTask(() => grainService.Init(Services), grainService.SchedulingContext).WithTimeout(this.initTimeout);

                await this.scheduler.QueueTask(grainService.Start, grainService.SchedulingContext).WithTimeout(this.initTimeout);

                if (this.logger.IsEnabled(LogLevel.Debug))
                {
                    logger.Debug(String.Format("{0} Grain Service started successfully.", serviceConfig.Value.Name));
                }
            }
        }
        public bool TryGetGrainType(Type grainClass, out GrainType grainType)
        {
            if (!LegacyGrainId.IsLegacyGrainType(grainClass))
            {
                grainType = default;
                return(false);
            }

            Type canonicalGrainClass;

            if (grainClass.IsConstructedGenericType)
            {
                canonicalGrainClass = grainClass.GetGenericTypeDefinition();
            }
            else
            {
                canonicalGrainClass = grainClass;
            }

            var isKeyExt = LegacyGrainId.IsLegacyKeyExtGrainType(canonicalGrainClass);
            var typeCode = GrainInterfaceUtils.GetGrainClassTypeCode(canonicalGrainClass);

            grainType = LegacyGrainId.GetGrainType(typeCode, isKeyExt);

            if (grainClass.IsGenericType)
            {
                grainType = GrainType.Create($"{grainType}`{canonicalGrainClass.GetGenericArguments().Length}");
            }

            return(true);
        }
Пример #3
0
        public void AddToUnorderedList(Type grainClass)
        {
            var grainClassTypeCode = GrainInterfaceUtils.GetGrainClassTypeCode(grainClass);

            if (!unordered.Contains(grainClassTypeCode))
            {
                unordered.Add(grainClassTypeCode);
            }
        }
Пример #4
0
        /// <summary>
        /// Currently we only support a single GrainService per Silo, when multiple are supported we will request the number of GrainServices to partition per silo here.
        /// </summary>
        protected GrainServiceClient(IServiceProvider serviceProvider)
        {
            grainFactory = serviceProvider.GetRequiredService <IInternalGrainFactory>();
            ringProvider = serviceProvider.GetRequiredService <IConsistentRingProvider>();

            // GrainInterfaceMap only holds IGrain types, not ISystemTarget types, so resolved via Orleans.CodeGeneration.
            // Resolve this before merge.
            grainTypeCode = GrainInterfaceUtils.GetGrainClassTypeCode(typeof(TGrainService));
        }
Пример #5
0
        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);
        }
        private static IGrainService GrainServiceFactory(Type serviceType, IServiceProvider services)
        {
            var grainServiceInterfaceType = serviceType.GetInterfaces().FirstOrDefault(x => x.GetInterfaces().Contains(typeof(IGrainService)));

            if (grainServiceInterfaceType == null)
            {
                throw new InvalidOperationException(String.Format($"Cannot find an interface on {serviceType.FullName} which implements IGrainService"));
            }
            var typeCode     = GrainInterfaceUtils.GetGrainClassTypeCode(grainServiceInterfaceType);
            var grainId      = (GrainId)LegacyGrainId.GetGrainServiceGrainId(0, typeCode);
            var grainService = (GrainService)ActivatorUtilities.CreateInstance(services, serviceType, grainId);

            return(grainService);
        }
Пример #7
0
        internal IReminderService CreateReminderService(
            Silo silo,
            TimeSpan iniTimeSpan,
            ISiloRuntimeClient runtimeClient)
        {
            logger.Info(
                "Creating reminder grain service for type={0}", this.reminderTable.GetType());

            // GrainInterfaceMap only holds IGrain types, not ISystemTarget types, so resolved via Orleans.CodeGeneration.
            // Resolve this before merge.
            var typeCode = GrainInterfaceUtils.GetGrainClassTypeCode(typeof(IReminderService));
            var grainId  = GrainId.GetGrainServiceGrainId(0, typeCode);

            return(new LocalReminderService(silo, grainId, this.reminderTable, iniTimeSpan, this.loggerFactory));
        }
Пример #8
0
        internal void AddEntry(Type iface, Type grain, PlacementStrategy placement, MultiClusterRegistrationStrategy registrationStrategy, bool primaryImplementation)
        {
            lock (this)
            {
                var grainTypeInfo       = grain.GetTypeInfo();
                var grainName           = TypeUtils.GetFullName(grainTypeInfo);
                var isGenericGrainClass = grainTypeInfo.ContainsGenericParameters;
                var grainTypeCode       = GrainInterfaceUtils.GetGrainClassTypeCode(grain);

                var grainInterfaceData = GetOrAddGrainInterfaceData(iface, isGenericGrainClass);

                var implementation = new GrainClassData(grainTypeCode, grainName, isGenericGrainClass);
                if (!implementationIndex.ContainsKey(grainTypeCode))
                {
                    implementationIndex.Add(grainTypeCode, implementation);
                }
                if (!placementStrategiesIndex.ContainsKey(grainTypeCode))
                {
                    placementStrategiesIndex.Add(grainTypeCode, placement);
                }
                if (!registrationStrategiesIndex.ContainsKey(grainTypeCode))
                {
                    registrationStrategiesIndex.Add(grainTypeCode, registrationStrategy);
                }

                grainInterfaceData.AddImplementation(implementation, primaryImplementation);
                if (primaryImplementation)
                {
                    primaryImplementations[grainInterfaceData.GrainInterface] = grainName;
                }
                else
                {
                    if (!primaryImplementations.ContainsKey(grainInterfaceData.GrainInterface))
                    {
                        primaryImplementations.Add(grainInterfaceData.GrainInterface, grainName);
                    }
                }

                if (localTestMode)
                {
                    var assembly = grainTypeInfo.Assembly.CodeBase;
                    if (!loadedGrainAsemblies.Contains(assembly))
                    {
                        loadedGrainAsemblies.Add(assembly);
                    }
                }
            }
        }
        internal IReminderService CreateReminderService(Silo silo, IGrainFactory grainFactory, TimeSpan iniTimeSpan, ISiloRuntimeClient runtimeClient)
        {
            var reminderServiceType = silo.GlobalConfig.ReminderServiceType;

            logger.Info("Creating reminder grain service for type={0}", Enum.GetName(typeof(GlobalConfiguration.ReminderServiceProviderType), reminderServiceType));

            // GrainInterfaceMap only holds IGrain types, not ISystemTarget types, so resolved via Orleans.CodeGeneration.
            // Resolve this before merge.
            var typeCode = GrainInterfaceUtils.GetGrainClassTypeCode(typeof(IReminderService));
            var grainId  = GrainId.GetGrainServiceGrainId(0, typeCode);

            ReminderTable.Initialize(silo, grainFactory, silo.GlobalConfig.ReminderTableAssembly);
            return(new LocalReminderService(
                       silo,
                       grainId,
                       ReminderTable.Singleton,
                       silo.GlobalConfig,
                       iniTimeSpan));
        }
Пример #10
0
 internal LocalReminderService(
     Silo silo,
     IReminderTable reminderTable,
     TimeSpan initTimeout,
     ILoggerFactory loggerFactory,
     IAsyncTimerFactory asyncTimerFactory)
     : base(SystemTargetGrainId.CreateGrainServiceGrainId(GrainInterfaceUtils.GetGrainClassTypeCode(typeof(IReminderService)), null, silo.SiloAddress), silo, loggerFactory)
 {
     localReminders         = new Dictionary <ReminderIdentity, LocalReminderData>();
     this.reminderTable     = reminderTable;
     this.initTimeout       = initTimeout;
     this.asyncTimerFactory = asyncTimerFactory;
     localTableSequence     = 0;
     tardinessStat          = AverageTimeSpanStatistic.FindOrCreate(StatisticNames.REMINDERS_AVERAGE_TARDINESS_SECONDS);
     IntValueStatistic.FindOrCreate(StatisticNames.REMINDERS_NUMBER_ACTIVE_REMINDERS, () => localReminders.Count);
     ticksDeliveredStat    = CounterStatistic.FindOrCreate(StatisticNames.REMINDERS_COUNTERS_TICKS_DELIVERED);
     startedTask           = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
     this.logger           = loggerFactory.CreateLogger <LocalReminderService>();
     this.listRefreshTimer = asyncTimerFactory.Create(Constants.RefreshReminderList, "ReminderService.ReminderListRefresher");
 }
Пример #11
0
        internal void AddEntry(Type iface, Type grain, PlacementStrategy placement, string directory, bool primaryImplementation)
        {
            lock (this)
            {
                var grainName           = TypeUtils.GetFullName(grain);
                var isGenericGrainClass = grain.ContainsGenericParameters;
                var grainTypeCode       = GrainInterfaceUtils.GetGrainClassTypeCode(grain);

                var grainInterfaceData = GetOrAddGrainInterfaceData(iface, isGenericGrainClass);

                var implementation = new GrainClassData(grainTypeCode, grainName, isGenericGrainClass);
                if (!implementationIndex.ContainsKey(grainTypeCode))
                {
                    implementationIndex.Add(grainTypeCode, implementation);
                }
                if (!placementStrategiesIndex.ContainsKey(grainTypeCode))
                {
                    placementStrategiesIndex.Add(grainTypeCode, placement);
                }
                if (!directoriesIndex.ContainsKey(grainTypeCode))
                {
                    directoriesIndex.Add(grainTypeCode, directory);
                }

                grainInterfaceData.AddImplementation(implementation, primaryImplementation);
                if (primaryImplementation)
                {
                    primaryImplementations[grainInterfaceData.GrainInterface] = grainName;
                }
                else
                {
                    if (!primaryImplementations.ContainsKey(grainInterfaceData.GrainInterface))
                    {
                        primaryImplementations.Add(grainInterfaceData.GrainInterface, grainName);
                    }
                }
            }
        }
Пример #12
0
        private void AddToGrainInterfaceToClassMap(Type grainClass, IEnumerable <Type> grainInterfaces, bool isUnordered)
        {
            var grainTypeInfo          = grainClass.GetTypeInfo();
            var grainClassCompleteName = TypeUtils.GetFullName(grainTypeInfo);
            var isGenericGrainClass    = grainTypeInfo.ContainsGenericParameters;
            var grainClassTypeCode     = GrainInterfaceUtils.GetGrainClassTypeCode(grainClass);
            var placement            = GrainTypeData.GetPlacementStrategy(grainClass, this.defaultPlacementStrategy);
            var registrationStrategy = GrainTypeData.GetMultiClusterRegistrationStrategy(grainClass);

            foreach (var iface in grainInterfaces)
            {
                var ifaceCompleteName    = TypeUtils.GetFullName(iface);
                var ifaceName            = TypeUtils.GetRawClassName(ifaceCompleteName);
                var isPrimaryImplementor = IsPrimaryImplementor(grainClass, iface);
                var ifaceId = GrainInterfaceUtils.GetGrainInterfaceId(iface);
                grainInterfaceMap.AddEntry(ifaceId, iface, grainClassTypeCode, ifaceName, grainClassCompleteName,
                                           grainTypeInfo.Assembly.CodeBase, isGenericGrainClass, placement, registrationStrategy, isPrimaryImplementor);
            }

            if (isUnordered)
            {
                grainInterfaceMap.AddToUnorderedList(grainClassTypeCode);
            }
        }
Пример #13
0
        private static GrainId GetGrainId()
        {
            var typeCode = GrainInterfaceUtils.GetGrainClassTypeCode(typeof(IReminderService));

            return(GrainId.GetGrainServiceGrainId(0, typeCode));
        }
Пример #14
0
 public static int TypeCode(this Type type) => GrainInterfaceUtils.GetGrainClassTypeCode(type);