public async Task <IEnumerable <IGrain> > GetActiveGrains(Type grainType)
        {
            string grainTypeName = TypeCodeMapper.GetImplementation(grainType).GrainClass;

            IEnumerable <Tuple <GrainId, string, int> > activeGrainList = await GetGrainActivations();

            IEnumerable <IGrain> filteredList = activeGrainList.Where(s => s.Item2.Equals(grainTypeName)).Select(s => GrainFactory.GetGrain <IIndexableGrain>(s.Item1.GetPrimaryKey(), grainType));

            return(filteredList.ToList());
        }
Esempio n. 2
0
        public static async Task <IEnumerable <T> > GetActiveGrains <T>(IGrainFactory gf, params SiloAddress[] hostsIds) where T : IGrain
        {
            string grainTypeName = TypeCodeMapper.GetImplementation(typeof(T)).GrainClass;

            IEnumerable <Tuple <GrainId, string, int> > activeGrainList = await GetGrainActivations(hostsIds);

            IEnumerable <T> filteredList = activeGrainList.Where(s => s.Item2.Equals(grainTypeName)).Select(s => gf.GetGrain <T>(s.Item1.GetPrimaryKey(), typeof(T)));

            return(filteredList.ToList());
        }
        /// <summary>
        /// This method extends GrainFactory by adding a new GetGrain method
        /// to it that can get the runtime type of the grain interface along
        /// with its primary key, and return the grain casted to an interface
        /// that the given grainInterfaceType extends it.
        ///
        /// The main use-case is when you want to get a grain that its type
        /// is unknown at compile time, and also SuperOutputGrainInterfaceType
        /// is non-generic, while outputGrainInterfaceType is a generic type.
        /// </summary>
        /// <typeparam name="SuperOutputGrainInterfaceType">The output type of
        /// the grain</typeparam>
        /// <param name="gf">the grainFactory instance that this method
        /// is extending its functionality</param>
        /// <param name="grainID">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 static IGrain GetGrain(this IGrainFactory gf, string grainID, Type grainInterfaceType, Type outputGrainInterfaceType)
        {
            Type           interfaceType = grainInterfaceType;
            GrainClassData implementation;
            var            grainTypeResolver = RuntimeClient.Current.GrainTypeResolver;

            grainTypeResolver.TryGetGrainClassData(interfaceType, out implementation, "");
            var grainId = TypeCodeMapper.ComposeGrainId(implementation, grainID, interfaceType);

            return((IGrain)((GrainFactory)gf).Cast(((GrainFactory)gf).MakeGrainReferenceFromType(interfaceType, grainId), outputGrainInterfaceType));
        }
        /// <summary>
        /// This method extends GrainFactory by adding a new GetGrain method
        /// to it that can get the runtime type of the grain interface along
        /// with its primary key, and return the grain casted to an interface
        /// that the given grainInterfaceType extends it.
        ///
        /// The main use-case is when you want to get a grain that its type
        /// is unknown at compile time.
        /// </summary>
        /// <typeparam name="OutputGrainInterfaceType">The output type of
        /// the grain</typeparam>
        /// <param name="gf">the grainFactory instance that this method
        /// is extending its functionality</param>
        /// <param name="grainID">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 static OutputGrainInterfaceType GetGrain <OutputGrainInterfaceType>(this IGrainFactory gf, Guid grainID, Type grainInterfaceType) where OutputGrainInterfaceType : IGrain
        {
            Type           interfaceType = grainInterfaceType;
            GrainClassData implementation;
            var            grainTypeResolver = RuntimeClient.Current.GrainTypeResolver;

            grainTypeResolver.TryGetGrainClassData(interfaceType, out implementation, "");
            var grainId = TypeCodeMapper.ComposeGrainId(implementation, grainID, interfaceType);

            return(((GrainFactory)gf).Cast <OutputGrainInterfaceType>(((GrainFactory)gf).MakeGrainReferenceFromType(interfaceType, grainId)));
        }
Esempio n. 5
0
 private void EnsureStorageProvider()
 {
     if (_storageProvider == null)
     {
         var  implementation = TypeCodeMapper.GetImplementation(typeof(V));
         Type implType;
         if (implementation == null || (grainImplClass = implementation.GrainClass) == null || !TypeUtils.TryResolveType(grainImplClass, out implType))
         {
             throw new Exception("The grain implementation class " + implementation.GrainClass + " for grain interface " + TypeUtils.GetFullName(typeof(V)) + " was not resolved.");
         }
         _storageProvider = InsideRuntimeClient.Current.Catalog.SetupStorageProvider(implType);
     }
 }
 private void EnsureGrainStorage()
 {
     if (_grainStorage == null)
     {
         var implementation = TypeCodeMapper.GetImplementation(this.SiloIndexManager.GrainTypeResolver, typeof(V));
         if (implementation == null || (_grainClassName = implementation.GrainClass) == null ||
             !this.SiloIndexManager.CachedTypeResolver.TryResolveType(_grainClassName, out Type grainClassType))
         {
             throw new IndexException($"The grain implementation class {implementation.GrainClass} for grain" +
                                      " interface {IndexUtils.GetFullTypeName(typeof(V))} was not resolved.");
         }
         _grainStorage = grainClassType.GetGrainStorage(this.SiloIndexManager.ServiceProvider);
     }
 }
Esempio n. 7
0
 private void EnsureGrainStorage()
 {
     if (_grainStorage == null)
     {
         var implementation = TypeCodeMapper.GetImplementation(this.SiloIndexManager.GrainTypeResolver, typeof(V));
         if (implementation == null || (grainImplClass = implementation.GrainClass) == null ||
             !this.SiloIndexManager.CachedTypeResolver.TryResolveType(grainImplClass, out Type implType))
         {
             throw new IndexException($"The grain implementation class {implementation.GrainClass} for grain" +
                                      " interface {IndexUtils.GetFullTypeName(typeof(V))} was not resolved.");
         }
         _grainStorage = implType.GetGrainStorage(this.SiloIndexManager.ServiceProvider);
         bool isFaultTolerant = ApplicationPartsIndexableGrainLoader.IsSubclassOfRawGenericType(typeof(IndexableGrain <,>), implType);
         _indexedFieldPrefix = isFaultTolerant ? "UserState." : string.Empty;
     }
 }
Esempio n. 8
0
        /// <summary>
        /// This is a helper method for creating an index on a field of an actor.
        /// </summary>
        /// <param name="gf">The current instance of IGrainFactory</param>
        /// <param name="idxType">The type of index to be created</param>
        /// <param name="indexName">The index name to be created</param>
        /// <param name="isUniqueIndex">Determines whether this is a unique index that needs to be created</param>
        /// <param name="isEager">Determines whether updates to this index should be applied eagerly or not</param>
        /// <param name="maxEntriesPerBucket">Determines the maximum number of entries in
        /// each bucket of a distributed index, if this index type is a distributed one.</param>
        /// <param name="indexedProperty">the PropertyInfo object for the indexed field.
        /// This object helps in creating a default instance of IndexUpdateGenerator.</param>
        /// <returns>A triple that consists of:
        /// 1) the index object (that implements IndexInterface
        /// 2) the IndexMetaData object for this index, and
        /// 3) the IndexUpdateGenerator instance for this index.
        /// This triple is untyped, because IndexInterface, IndexMetaData
        /// and IndexUpdateGenerator types are not visible in the core project.</returns>
        internal static Tuple <object, object, object> CreateIndex(this IGrainFactory gf, Type idxType, string indexName, bool isUniqueIndex, bool isEager, int maxEntriesPerBucket, PropertyInfo indexedProperty)
        {
            Type iIndexType = idxType.GetGenericType(typeof(IndexInterface <,>));

            if (iIndexType != null)
            {
                Type[] indexTypeArgs = iIndexType.GetGenericArguments();
                Type   keyType       = indexTypeArgs[0];
                Type   grainType     = indexTypeArgs[1];

                IndexInterface index;
                if (typeof(IGrain).IsAssignableFrom(idxType))
                {
                    index = (IndexInterface)gf.GetGrain(IndexUtils.GetIndexGrainID(grainType, indexName), idxType, idxType);

                    Type idxImplType = TypeUtils.ResolveType(TypeCodeMapper.GetImplementation(idxType).GrainClass);

                    if (idxImplType.IsGenericTypeDefinition)
                    {
                        idxImplType = idxImplType.MakeGenericType(iIndexType.GetGenericArguments());
                    }

                    MethodInfo initPerSilo;
                    if ((initPerSilo = idxImplType.GetMethod("InitPerSilo", BindingFlags.Static | BindingFlags.Public)) != null)
                    {
                        var initPerSiloMethod = (Action <Silo, string, bool>)Delegate.CreateDelegate(
                            typeof(Action <Silo, string, bool>),
                            initPerSilo);
                        initPerSiloMethod(Silo.CurrentSilo, indexName, isUniqueIndex);
                    }
                }
                else if (idxType.IsClass)
                {
                    index = (IndexInterface)Activator.CreateInstance(idxType, indexName, isUniqueIndex);
                }
                else
                {
                    throw new Exception(string.Format("{0} is neither a grain nor a class. Index \"{1}\" cannot be created.", idxType, indexName));
                }

                return(Tuple.Create((object)index, (object)new IndexMetaData(idxType, isUniqueIndex, isEager, maxEntriesPerBucket), (object)CreateIndexUpdateGenFromProperty(indexedProperty)));
            }
            else
            {
                throw new NotSupportedException(string.Format("Adding an index that does not implement IndexInterface<K,V> is not supported yet. Your requested index ({0}) is invalid.", idxType.ToString()));
            }
        }
        /// <summary>
        /// This is a helper method for creating an index on a field of an actor.
        /// </summary>
        /// <param name="idxType">The type of index to be created</param>
        /// <param name="indexName">The index name to be created</param>
        /// <param name="isUniqueIndex">Determines whether this is a unique index that needs to be created</param>
        /// <param name="isEager">Determines whether updates to this index should be applied eagerly or not</param>
        /// <param name="maxEntriesPerBucket">Determines the maximum number of entries in
        /// each bucket of a distributed index, if this index type is a distributed one.</param>
        /// <param name="indexedProperty">the PropertyInfo object for the indexed field.
        /// This object helps in creating a default instance of IndexUpdateGenerator.</param>
        /// <returns>An <see cref="IndexInfo"/> for the specified idxType and indexName.</returns>
        internal async Task <IndexInfo> CreateIndex(Type idxType, string indexName, bool isUniqueIndex, bool isEager, int maxEntriesPerBucket, PropertyInfo indexedProperty)
        {
            Type iIndexType = idxType.GetGenericType(typeof(IIndexInterface <,>));

            if (iIndexType == null)
            {
                throw new NotSupportedException(string.Format("Adding an index that does not implement IndexInterface<K,V> is not supported yet. Your requested index ({0}) is invalid.", idxType.ToString()));
            }

            Type[] indexTypeArgs = iIndexType.GetGenericArguments();
            Type   keyType       = indexTypeArgs[0];
            Type   grainType     = indexTypeArgs[1];

            IIndexInterface index;

            if (typeof(IGrain).IsAssignableFrom(idxType))
            {
                // This must call the static Silo methods because we may not be InSilo.
                index = (IIndexInterface)Silo.GetGrain(this.grainFactory, IndexUtils.GetIndexGrainPrimaryKey(grainType, indexName), idxType, idxType);

                if (this.IsInSilo)
                {
                    var idxImplType = this.indexManager.CachedTypeResolver.ResolveType(
                        TypeCodeMapper.GetImplementation(this.siloIndexManager.GrainTypeResolver, idxType).GrainClass);
                    if (idxImplType.IsGenericTypeDefinition)
                    {
                        idxImplType = idxImplType.MakeGenericType(iIndexType.GetGenericArguments());
                    }

                    var initPerSiloMethodInfo = idxImplType.GetMethod("InitPerSilo", BindingFlags.Static | BindingFlags.NonPublic);
                    if (initPerSiloMethodInfo != null)  // Static method so cannot use an interface
                    {
                        var initPerSiloMethod = (Func <SiloIndexManager, string, bool, Task>)Delegate.CreateDelegate(typeof(Func <SiloIndexManager, string, bool, Task>), initPerSiloMethodInfo);
                        await initPerSiloMethod(this.siloIndexManager, indexName, isUniqueIndex);
                    }
                }
            }
            else
            {
                index = idxType.IsClass
                    ? (IIndexInterface)Activator.CreateInstance(idxType, this.indexManager.ServiceProvider, indexName, isUniqueIndex)
                    : throw new IndexException(string.Format("{0} is neither a grain nor a class. Index \"{1}\" cannot be created.", idxType, indexName));
            }

            return(new IndexInfo(index, new IndexMetaData(idxType, isUniqueIndex, isEager, maxEntriesPerBucket), CreateIndexUpdateGenFromProperty(indexedProperty)));
        }
Esempio n. 10
0
 /// <summary>
 /// Determines whether the index grain is a stateless worker
 /// or not. This piece of information can impact the relationship
 /// between index handlers and the index.
 /// </summary>
 /// <returns>the result of whether the current index is
 /// a stateless worker or not</returns>
 public bool IsIndexStatelessWorker()
 {
     return(IsStatelessWorker(Type.GetType(TypeCodeMapper.GetImplementation(_indexType).GrainClass)));
 }