Beispiel #1
0
        private async ValueTask <PlacementResult> GetOrPlaceActivationAsync(
            ValueTask <PlacementResult> selectActivationTask,
            PlacementTarget target,
            PlacementStrategy strategy,
            IPlacementRuntime placementRuntime,
            IPlacementDirector director)
        {
            var placementResult = await selectActivationTask;

            if (placementResult is object)
            {
                return(placementResult);
            }

            var siloAddress = await director.OnAddActivation(strategy, target, placementRuntime);

            ActivationId activationId;

            if (strategy.IsDeterministicActivationId)
            {
                // Use the grain id as the activation id.
                activationId = ActivationId.GetDeterministic(target.GrainIdentity);
            }
            else
            {
                activationId = ActivationId.NewId();
            }

            return(PlacementResult.SpecifyCreation(
                       siloAddress,
                       activationId,
                       strategy));
        }
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The Dfa user object running the code example.
        /// </param>
        public override void Run(DfaUser user)
        {
            // Create PlacementStrategyRemoteService instance.
              PlacementStrategyRemoteService service = (PlacementStrategyRemoteService) user.GetService(
              DfaService.v1_20.PlacementStrategyRemoteService);

              String placementStrategyName = _T("INSERT_PLACEMENT_STRATEGY_NAME_HERE");

              // Create placement strategy structure.
              PlacementStrategy placementStrategy = new PlacementStrategy();
              placementStrategy.id = 0;
              placementStrategy.name = placementStrategyName;

              try {
            // Create placement strategy.
            PlacementStrategySaveResult placementStrategySaveResult =
            service.savePlacementStrategy(placementStrategy);

            if (placementStrategySaveResult != null) {
              // Display placement strategy id.
              Console.WriteLine("Placement Strategy with id \"{0}\" was created.",
              placementStrategySaveResult.id);
            }
              } catch (Exception e) {
            Console.WriteLine("Failed to create placement strategy. Exception says \"{0}\"",
            e.Message);
              }
        }
        public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var silos = context.GetCompatibleSilos(target).OrderBy(x => x).ToArray();

            if (Constants.DefaultNumGrainsInOneLayer == 0)
            {
                Constants.DefaultNumGrainsInOneLayer = 2 * (silos.Length - 1);
            }
            var targetSilo = RequestContext.Get("targetSilo");

            if (targetSilo != null)
            {
                foreach (SiloAddress silo in silos)
                {
                    if (silo.Endpoint.Address.ToString().Equals(targetSilo))
                    {
                        return(Task.FromResult(silo));
                    }
                }
            }
            var excludeSilo = RequestContext.Get("excludeSilo");

            if (excludeSilo != null)
            {
                silos = silos.Where(x => !x.Endpoint.Address.ToString().Equals(excludeSilo)).ToArray();
            }

            object index = RequestContext.Get("grainIndex");

            if (index == null)
            {
                index = new Random().Next(0, silos.Count());
            }
            return(Task.FromResult(silos[(int)index % silos.Count()]));
        }
        /// <summary>
        /// Selects the best match from list of silos, updates local statistics.
        /// </summary>
        /// <note>
        /// This is equivalent with SelectSiloPowerOfK() with chooseHowMany = #Silos
        /// </note>
        private Task <SiloAddress> SelectSiloGreedy(PlacementStrategy strategy, GrainId grain, IPlacementRuntime context)
        {
            int             minLoad       = int.MaxValue;
            CachedLocalStat minLoadedSilo = null;

            foreach (CachedLocalStat current in localCache.Values)
            {
                if (IsSiloOverloaded(current.SiloStats))
                {
                    continue;
                }

                int load = SiloLoad_ByRecentActivations(current);
                if (load >= minLoad)
                {
                    continue;
                }

                minLoadedSilo = current;
                minLoad       = load;
            }

            if (minLoadedSilo != null)
            {
                return(MakePlacement(minLoadedSilo));
            }

            var debugLog = string.Format("Unable to select a candidate from {0} silos: {1}", localCache.Count,
                                         Utils.EnumerableToString(
                                             localCache,
                                             kvp => String.Format("SiloAddress = {0} -> {1}", kvp.Key.ToString(), kvp.Value.ToString())));

            logger.Warn(ErrorCode.Placement_ActivationCountBasedDirector_NoSilos, debugLog);
            throw new OrleansException(debugLog);
        }
        public static List <PlacementStrategy> ConvertPlacementStrategy(string[] values)
        {
            if (values == null || values.Length == 0)
            {
                return(null);
            }

            var list = new List <PlacementStrategy>();

            foreach (var value in values)
            {
                var tokens     = value.Split('=');
                var constraint = new PlacementStrategy
                {
                    Type = tokens[0]
                };

                if (tokens.Length > 1)
                {
                    constraint.Field = tokens[1];
                }

                list.Add(constraint);
            }

            return(list);
        }
Beispiel #6
0
        internal override Task <PlacementResult> OnAddActivation(PlacementStrategy strategy, GrainId grain, IPlacementContext context)
        {
            var grainType = context.GetGrainTypeName(grain);

            return(Task.FromResult(
                       PlacementResult.SpecifyCreation(context.LocalSilo, strategy, grainType)));
        }
        public virtual Task <SiloAddress> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var allSilos = context.GetCompatibleSilos(target);

            return(Task.FromResult(allSilos[random.Next(allSilos.Length)]));
        }
        internal override async Task <PlacementResult> OnSelectActivation(
            PlacementStrategy strategy, GrainId target, IPlacementContext context)
        {
            List <ActivationAddress> places = await context.Lookup(target);

            if (places.Count <= 0)
            {
                // we return null to indicate that we were unable to select a target from places activations.
                return(null);
            }

            if (places.Count == 1)
            {
                return(PlacementResult.IdentifySelection(places[0]));
            }

            // places.Count > 0
            // Choose randomly if there is one, else make a new activation of the target
            // pick local if available (consider making this a purely random assignment of grains).
            var here  = context.LocalSilo;
            var local = places.Where(a => a.Silo.Equals(here)).ToList();

            if (local.Count > 0)
            {
                return(PlacementResult.IdentifySelection(local[random.Next(local.Count)]));
            }
            if (places.Count > 0)
            {
                return(PlacementResult.IdentifySelection(places[random.Next(places.Count)]));
            }
            // we return null to indicate that we were unable to select a target from places activations.
            return(null);
        }
        public override async Task <PlacementResult> OnSelectActivation(PlacementStrategy strategy, GrainId target, IPlacementContext context)
        {
            // first, check if we can find an activation for this client in the cache or local directory partition
            AddressesAndTag addresses;

            if (context.FastLookup(target, out addresses))
            {
                return(ChooseRandomActivation(addresses.Addresses, context));
            }

            // we need to look up the directory entry for this grain on a remote silo
            switch (target.Category)
            {
            case UniqueKey.Category.Client:
            {
                addresses = await context.FullLookup(target);

                return(ChooseRandomActivation(addresses.Addresses, context));
            }

            case UniqueKey.Category.GeoClient:
            {
                // we need to look up the activations in the remote cluster
                addresses = await context.LookupInCluster(target, target.Key.ClusterId);

                return(ChooseRandomActivation(addresses.Addresses, context));
            }

            default:
                throw new InvalidOperationException("Unsupported client type. Grain " + target);
            }
        }
        public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            IReadOnlyList <SiloAddress> silos;

            if (target.InterfaceVersion == 0)
            {
                silos = (IReadOnlyList <SiloAddress>)context.GetCompatibleSilos(target);
            }
            else
            {
                var    silosByVersion = context.GetCompatibleSilosWithVersions(target);
                var    maxSiloCount   = 0;
                ushort version        = 0;
                foreach (var kvp in silosByVersion)
                {
                    if (kvp.Value.Count > maxSiloCount)
                    {
                        version      = kvp.Key;
                        maxSiloCount = kvp.Value.Count;
                    }
                }
                silos = silosByVersion[version];
            }

            return(Task.FromResult(silos[random.Next(silos.Count)]));
        }
Beispiel #11
0
        public async Task <PlacementResult> SelectOrAddActivation(
            ActivationAddress sendingAddress,
            PlacementTarget targetGrain,
            IPlacementRuntime context,
            PlacementStrategy strategy)
        {
            if (targetGrain.IsClient)
            {
                var res = await clientObserversPlacementDirector.OnSelectActivation(strategy, (GrainId)targetGrain.GrainIdentity, context);

                if (res == null)
                {
                    throw new ClientNotAvailableException(targetGrain.GrainIdentity);
                }
                return(res);
            }

            var actualStrategy = strategy ?? defaultPlacementStrategy;
            var director       = ResolveSelector(actualStrategy);
            var result         = await director.OnSelectActivation(strategy, (GrainId)targetGrain.GrainIdentity, context);

            if (result != null)
            {
                return(result);
            }

            return(await AddActivation(targetGrain, context, actualStrategy));
        }
Beispiel #12
0
        private void ResolveBuiltInStrategies()
        {
            var statelessWorker = new StatelessWorkerPlacement();

            var placementStrategies = new PlacementStrategy[]
            {
                RandomPlacement.Singleton,
                ActivationCountBasedPlacement.Singleton,
                statelessWorker,
                PreferLocalPlacement.Singleton
            };

            foreach (var strategy in placementStrategies)
            {
                this.ResolveDirector(strategy);
            }

            var selectorStrategies = new PlacementStrategy[]
            {
                RandomPlacement.Singleton,
                statelessWorker,
            };

            foreach (var strategy in selectorStrategies)
            {
                this.ResolveSelector(strategy, true);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        /// <param name="user">The Dfa user object running the code example.
        /// </param>
        public override void Run(DfaUser user)
        {
            // Create PlacementStrategyRemoteService instance.
            PlacementStrategyRemoteService service = (PlacementStrategyRemoteService)user.GetService(
                DfaService.v1_20.PlacementStrategyRemoteService);

            String placementStrategyName = _T("INSERT_PLACEMENT_STRATEGY_NAME_HERE");

            // Create placement strategy structure.
            PlacementStrategy placementStrategy = new PlacementStrategy();

            placementStrategy.id   = 0;
            placementStrategy.name = placementStrategyName;

            try {
                // Create placement strategy.
                PlacementStrategySaveResult placementStrategySaveResult =
                    service.savePlacementStrategy(placementStrategy);

                if (placementStrategySaveResult != null)
                {
                    // Display placement strategy id.
                    Console.WriteLine("Placement Strategy with id \"{0}\" was created.",
                                      placementStrategySaveResult.id);
                }
            } catch (Exception ex) {
                Console.WriteLine("Failed to create placement strategy. Exception says \"{0}\"",
                                  ex.Message);
            }
        }
Beispiel #14
0
        public virtual async Task <PlacementResult> OnSelectActivation(
            PlacementStrategy strategy, GrainId target, IPlacementContext context)
        {
            List <ActivationAddress> places = (await context.Lookup(target)).Addresses;

            return(ChooseRandomActivation(places, context));
        }
        internal override Task<PlacementResult> OnSelectActivation(
            PlacementStrategy strategy, GrainId target, IPlacementContext context)
        {
            if (target.IsClient)
                throw new InvalidOperationException("Cannot use StatelessWorkerStrategy to route messages to client grains.");

            // If there are available (not busy with a request) activations, it returns the first one.
            // If all are busy and the number of local activations reached or exceeded MaxLocal, it randomly returns one of them.
            // Otherwise, it requests creation of a new activation.
            List<ActivationData> local;

            if (!context.LocalLookup(target, out local) || local.Count == 0)
                return Task.FromResult((PlacementResult)null);

            var placement = (StatelessWorkerPlacement)strategy;

            foreach (var activation in local)
            {
                ActivationData info;
                if (!context.TryGetActivationData(activation.ActivationId, out info) ||
                    info.State != ActivationState.Valid || !info.IsInactive) continue;

                return Task.FromResult(PlacementResult.IdentifySelection(ActivationAddress.GetAddress(context.LocalSilo, target, activation.ActivationId)));
            }

            if (local.Count >= placement.MaxLocal)
            {
                var id = local[local.Count == 1 ? 0 : random.Next(local.Count)].ActivationId;
                return Task.FromResult(PlacementResult.IdentifySelection(ActivationAddress.GetAddress(context.LocalSilo, target, id)));
            }

            return Task.FromResult((PlacementResult)null);
        }
Beispiel #16
0
        public async Task <PlacementResult> SelectOrAddActivation(
            ActivationAddress sendingAddress,
            GrainId targetGrain,
            IPlacementContext context,
            PlacementStrategy strategy)
        {
            if (targetGrain.IsClient)
            {
                var res = await clientObserversPlacementDirector.OnSelectActivation(strategy, targetGrain, context);

                if (res == null)
                {
                    throw new KeyNotFoundException("No activation for client " + targetGrain);
                }
                return(res);
            }

            var actualStrategy = strategy ?? defaultPlacementStrategy;
            var result         = await SelectActivation(targetGrain, context, actualStrategy);

            if (result != null)
            {
                return(result);
            }

            return(await AddActivation(targetGrain, context, actualStrategy));
        }
Beispiel #17
0
        private async Task <PlacementResult> AddActivation(
            PlacementTarget target,
            IPlacementRuntime context,
            PlacementStrategy strategy)
        {
            if (target.IsClient)
            {
                throw new InvalidOperationException("Client grains are not activated using the placement subsystem.");
            }

            var director    = ResolveDirector(strategy);
            var siloAddress = await director.OnAddActivation(strategy, target, context);

            var grainTypeName = context.GetGrainTypeName(target.GrainIdentity.TypeCode);

            ActivationId activationId;

            if (strategy.IsDeterministicActivationId)
            {
                // Use the grain id as the activation id.
                activationId = ActivationId.GetActivationId(((GrainId)target.GrainIdentity).Key);
            }
            else
            {
                activationId = ActivationId.NewId();
            }

            return(PlacementResult.SpecifyCreation(
                       siloAddress,
                       activationId,
                       strategy,
                       grainTypeName));
        }
 public override Task <SiloAddress> OnAddActivation(
     PlacementStrategy strategy,
     PlacementTarget target,
     IPlacementContext context)
 {
     throw new InvalidOperationException("Client Observers are not activated using the placement subsystem. Grain " + target.GrainIdentity);
 }
        public override async Task<PlacementResult> OnSelectActivation(PlacementStrategy strategy, GrainId target, IPlacementContext context)
        {
            // first, check if we can find an activation for this client in the cache or local directory partition
            AddressesAndTag addresses;
            if (context.FastLookup(target, out addresses))
                return ChooseRandomActivation(addresses.Addresses, context);

            // we need to look up the directory entry for this grain on a remote silo
            switch (target.Category)
            {
                case UniqueKey.Category.Client:
                    {
                        addresses = await context.FullLookup(target);
                        return ChooseRandomActivation(addresses.Addresses, context);
                    }

                case UniqueKey.Category.GeoClient:
                    {
                        // we need to look up the activations in the remote cluster
                        addresses = await context.LookupInCluster(target, target.Key.ClusterId);
                        return ChooseRandomActivation(addresses.Addresses, context);
                    }

                default:
                    throw new InvalidOperationException("Unsupported client type. Grain " + target);
            }
        }
 internal override Task<PlacementResult> OnAddActivation(
     PlacementStrategy strategy, GrainId grain, IPlacementContext context)
 {
     var grainType = context.GetGrainTypeName(grain);
     var allSilos = context.AllActiveSilos;
     return Task.FromResult(
         PlacementResult.SpecifyCreation(allSilos[random.Next(allSilos.Count)], strategy, grainType));
 }
Beispiel #21
0
        internal static void InitDefaultGrainStrategies()
        {
            RandomPlacement = Orleans.Runtime.RandomPlacement.Singleton;

            PreferLocalPlacement = Orleans.Runtime.PreferLocalPlacement.Singleton;

            ActivationCountBasedPlacement = Orleans.Runtime.ActivationCountBasedPlacement.Singleton;
        }
Beispiel #22
0
        public virtual Task <SiloAddress> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var allSilos = context.GetCompatibleSilos(target).OrderBy(s => s).ToArray();  // need to sort the list, so that the outcome is deterministic
            int hash     = (int)(target.GrainIdentity.GetUniformHashCode() & 0x7fffffff); // reset highest order bit to avoid negative ints

            return(Task.FromResult(allSilos[hash % allSilos.Length]));
        }
Beispiel #23
0
        public virtual Task <SiloAddress> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var allSilos = context.GetCompatibleSilos(target);

            int hash = (int)(target.GrainIdentity.GetUniformHashCode() & 0x7fffffff);  // reset highest order bit to avoid negative ints

            return(Task.FromResult(allSilos[hash % allSilos.Count]));
        }
Beispiel #24
0
        public virtual Task <PlacementResult> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var grainType = context.GetGrainTypeName(target.GrainId);
            var allSilos  = context.GetCompatibleSiloList(target);

            return(Task.FromResult(
                       PlacementResult.SpecifyCreation(allSilos[random.Next(allSilos.Count)], strategy, grainType)));
        }
Beispiel #25
0
            protected PlacementAttribute(PlacementStrategy placement)
            {
                if (placement == null)
                {
                    throw new ArgumentNullException(nameof(placement));
                }

                PlacementStrategy = placement;
            }
Beispiel #26
0
        public virtual Task <PlacementResult> OnAddActivation(
            PlacementStrategy strategy, GrainId grain, IPlacementContext context)
        {
            var grainType = context.GetGrainTypeName(grain);
            var allSilos  = context.AllActiveSilos;

            return(Task.FromResult(
                       PlacementResult.SpecifyCreation(allSilos[random.Next(allSilos.Count)], strategy, grainType)));
        }
Beispiel #27
0
        private Task <PlacementResult> SelectActivation(
            GrainId targetGrain,
            IPlacementContext context,
            PlacementStrategy strategy)
        {
            var director = ResolveDirector(strategy);

            return(director.OnSelectActivation(strategy, targetGrain, context));
        }
Beispiel #28
0
        /// <summary>
        /// Generate one GrainReference class for each Grain Type in the inputLib file
        /// and output one GrainClient.dll under outputLib directory
        /// </summary>
        private bool CreateGrainClient(CodeGenOptions options)
        {
            PlacementStrategy.Initialize();

            // Load input assembly
            ConsoleText.WriteLine("");
            var assemblyName  = AssemblyName.GetAssemblyName(options.InputLib.FullName);
            var grainAssembly = (Path.GetFileName(options.InputLib.FullName) != "Orleans.dll")
                                    ? Assembly.LoadFrom(options.InputLib.FullName)
                                    : Assembly.Load(assemblyName);

            // special case Orleans.dll because there is a circular dependency.

            // Create sources directory
            if (!Directory.Exists(options.SourcesDir))
            {
                Directory.CreateDirectory(options.SourcesDir);
            }

            // Generate source
            var outputFileName = Path.Combine(
                options.SourcesDir,
                Path.GetFileNameWithoutExtension(options.InputLib.Name) + ".codegen.cs");

            ConsoleText.WriteStatus("Orleans-CodeGen - Generating file {0}", outputFileName);

            var codeGenerator = RoslynCodeGenerator.Instance;

            using (var sourceWriter = new StreamWriter(outputFileName))
            {
                if (options.TargetLanguage != Language.CSharp)
                {
                    var message = "Compile-time code generation is supported for C# only. "
                                  + "Remove code generation from your project in order to use run-time code generation.";
                    ConsoleText.WriteLine("ERROR: " + message);
                    throw new NotSupportedException(message);
                }

                sourceWriter.WriteLine("#if !EXCLUDE_CODEGEN");
                DisableWarnings(sourceWriter, suppressCompilerWarnings);
                sourceWriter.WriteLine(codeGenerator.GenerateSourceForAssembly(grainAssembly));
                RestoreWarnings(sourceWriter, suppressCompilerWarnings);
                sourceWriter.WriteLine("#endif");
            }

            ConsoleText.WriteStatus("Orleans-CodeGen - Generated file written {0}", outputFileName);

            // Copy intermediate file to permanent location, if newer.
            ConsoleText.WriteStatus(
                "Orleans-CodeGen - Updating IntelliSense file {0} -> {1}",
                outputFileName,
                options.CodeGenFile);
            UpdateIntellisenseFile(options.CodeGenFile, outputFileName);

            return(true);
        }
Beispiel #29
0
 public PlacementDirectorsManager(
     IServiceProvider services,
     DefaultPlacementStrategy defaultPlacementStrategy,
     ClientObserversPlacementDirector clientObserversPlacementDirector)
 {
     this.serviceProvider                  = services;
     this.defaultPlacementStrategy         = defaultPlacementStrategy.PlacementStrategy;
     this.clientObserversPlacementDirector = clientObserversPlacementDirector;
     this.ResolveBuiltInStrategies();
 }
 /// <summary>
 /// Create a <see cref="PlacementStrategyResolver"/> instance.
 /// </summary>
 public PlacementStrategyResolver(
     IServiceProvider services,
     IEnumerable <IPlacementStrategyResolver> resolvers,
     GrainPropertiesResolver grainPropertiesResolver)
 {
     _getStrategyInternal      = GetPlacementStrategyInternal;
     _resolvers                = resolvers.ToArray();
     _services                 = services;
     _grainPropertiesResolver  = grainPropertiesResolver;
     _defaultPlacementStrategy = services.GetService <PlacementStrategy>();
     _strategies               = GetAllStrategies(services);
Beispiel #31
0
        OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            // if local silo is not active or does not support this type of grain, revert to random placement
            if (context.LocalSiloStatus != SiloStatus.Active || !context.GetCompatibleSilos(target).Contains(context.LocalSilo))
            {
                return(base.OnAddActivation(strategy, target, context));
            }

            cachedLocalSilo = cachedLocalSilo ?? Task.FromResult(context.LocalSilo);
            return(cachedLocalSilo);
        }
Beispiel #32
0
        public async Task DefaultPlacementShouldBeRandom()
        {
            await this.HostedCluster.WaitForLivenessToStabilizeAsync();

            logger.Info("********************** Starting the test DefaultPlacementShouldBeRandom ******************************");
            TestSilosStarted(2);

            Assert.Equal(
                RandomPlacement.Singleton,
                PlacementStrategy.GetDefault());
        }
        public override Task <SiloAddress> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            // If the cache was not populated, just place locally
            if (this.localCache.IsEmpty)
            {
                return(Task.FromResult(this.localAddress));
            }

            return(SelectSilo(strategy, target, context));
        }
Beispiel #34
0
 public GrainInterfaceMap(bool localTestMode, PlacementStrategy defaultPlacementStrategy)
 {
     table = new Dictionary<int, GrainInterfaceData>();
     typeToInterfaceData = new Dictionary<string, GrainInterfaceData>();
     primaryImplementations = new Dictionary<string, string>();
     implementationIndex = new Dictionary<int, GrainClassData>();
     unordered = new HashSet<int>();
     this.localTestMode = localTestMode;
     this.defaultPlacementStrategy = defaultPlacementStrategy;
     if(localTestMode) // if we are running in test mode, we'll build a list of loaded grain assemblies to help with troubleshooting deployment issue
         loadedGrainAsemblies = new HashSet<string>();
 }
        private Task<PlacementResult> MakePlacement(PlacementStrategy strategy, GrainId grain, IPlacementContext context, CachedLocalStat minLoadedSilo)
        {
            // Increment placement by number of silos instead of by one.
            // This is our trick to get more balanced placement, accounting to the probable
            // case when multiple silos place on the same silo at the same time, before stats are refreshed.
            minLoadedSilo.IncrementActivationCount(localCache.Count);

            return Task.FromResult(PlacementResult.SpecifyCreation(
                minLoadedSilo.Address,
                strategy,
                context.GetGrainTypeName(grain)));
        }
        private Task <PlacementResult> MakePlacement(PlacementStrategy strategy, GrainId grain, IPlacementContext context, CachedLocalStat minLoadedSilo)
        {
            // Increment placement by number of silos instead of by one.
            // This is our trick to get more balanced placement, accounting to the probable
            // case when multiple silos place on the same silo at the same time, before stats are refreshed.
            minLoadedSilo.IncrementActivationCount(localCache.Count);

            return(Task.FromResult(PlacementResult.SpecifyCreation(
                                       minLoadedSilo.Address,
                                       strategy,
                                       context.GetGrainTypeName(grain))));
        }
Beispiel #37
0
 public GrainTypeManager(bool localTestMode, SiloAssemblyLoader loader, DefaultPlacementStrategy defaultPlacementStrategy)
 {
     this.defaultPlacementStrategy = defaultPlacementStrategy.PlacementStrategy;
     this.loader = loader;
     grainInterfaceMap = new GrainInterfaceMap(localTestMode, this.defaultPlacementStrategy);
     lock (lockable)
     {
         if (Instance != null)
             throw new InvalidOperationException("An attempt to create a second insance of GrainTypeManager.");
         Instance = this;
     }
 }
 public PlacementDirectorsManager(
     IServiceProvider services,
     PlacementStrategy defaultPlacementStrategy,
     ClientObserversPlacementDirector clientObserversPlacementDirector)
 {
     this.serviceProvider                  = services;
     this.defaultPlacementStrategy         = defaultPlacementStrategy;
     this.clientObserversPlacementDirector = clientObserversPlacementDirector;
     this.ResolveBuiltInStrategies();
     // TODO: Make default selector configurable
     this.defaultActivationSelector = ResolveSelector(RandomPlacement.Singleton, true);
 }
    /// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="service">An initialized Dfa Reporting service object
    /// </param>
    public override void Run(DfareportingService service) {
      long profileId = long.Parse(_T("INSERT_USER_PROFILE_ID_HERE"));

      string placementStrategyName = _T("INSERT_PLACEMENT_STRATEGY_NAME_HERE");

      // Create the placement strategy.
      PlacementStrategy placementStrategy = new PlacementStrategy();
      placementStrategy.Name = placementStrategyName;

      // Insert the placement strategy.
      PlacementStrategy result =
          service.PlacementStrategies.Insert(placementStrategy, profileId).Execute();

      // Display the new placement strategy ID.
      Console.WriteLine("Placement strategy with ID {0} was created.", result.Id);
    }
Beispiel #40
0
        internal void AddEntry(int interfaceId, Type iface, int grainTypeCode, string grainInterface, string grainClass, string assembly, 
                                bool isGenericGrainClass, PlacementStrategy placement, bool primaryImplementation = false)
        {
            lock (this)
            {
                GrainInterfaceData grainInterfaceData;

                if (table.ContainsKey(interfaceId))
                {
                    grainInterfaceData = table[interfaceId];
                }
                else
                {
                    grainInterfaceData = new GrainInterfaceData(interfaceId, iface, grainInterface);

                    table[interfaceId] = grainInterfaceData;
                    var interfaceTypeKey = GetTypeKey(iface, isGenericGrainClass);
                    typeToInterfaceData[interfaceTypeKey] = grainInterfaceData;
                }

                var implementation = new GrainClassData(grainTypeCode, grainClass, isGenericGrainClass, grainInterfaceData, placement);
                if (!implementationIndex.ContainsKey(grainTypeCode))
                    implementationIndex.Add(grainTypeCode, implementation);

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

                if (localTestMode)
                {
                    if (!loadedGrainAsemblies.Contains(assembly))
                        loadedGrainAsemblies.Add(assembly);
                }
            }
        }
Beispiel #41
0
            SpecifyCreation(
                SiloAddress silo,
                PlacementStrategy placement,
                string grainType)
        {
            if (silo == null)
                throw new ArgumentNullException("silo");
            if (placement == null)
                throw new ArgumentNullException("placement");
            if (string.IsNullOrWhiteSpace(grainType))
                throw new ArgumentException("'grainType' must contain a valid typename.");

            return
                new PlacementResult
                    {
                        Activation = ActivationId.NewId(),
                        Silo = silo,
                        PlacementStrategy = placement,
                        GrainType = grainType
                    };
        }
        internal override async Task<PlacementResult> OnSelectActivation(
            PlacementStrategy strategy, GrainId target, IPlacementContext context)
        {
            List<ActivationAddress> places = await context.Lookup(target);
            if (places.Count <= 0)
            {
                // we return null to indicate that we were unable to select a target from places activations.
                return null;
            }

            if (places.Count == 1) return PlacementResult.IdentifySelection(places[0]);

            // places.Count > 0
            // Choose randomly if there is one, else make a new activation of the target
            // pick local if available (consider making this a purely random assignment of grains).
            var here = context.LocalSilo;
            var local = places.Where(a => a.Silo.Equals(here)).ToList();
            if (local.Count > 0) 
                return PlacementResult.IdentifySelection(local[random.Next(local.Count)]);
            if (places.Count > 0)
                return PlacementResult.IdentifySelection(places[random.Next(places.Count)]);
            // we return null to indicate that we were unable to select a target from places activations.
            return null;
        }
Beispiel #43
0
            internal PlacementAttribute(PlacementStrategy placement)
            {
                if (placement == null) throw new ArgumentNullException(nameof(placement));

                PlacementStrategy = placement;
            }
 internal override Task<PlacementResult> OnAddActivation(
     PlacementStrategy strategy, GrainId grain, IPlacementContext context)
 {
     return SelectSilo(strategy, grain, context);
 }
        /// <summary>
        /// Selects the best match from list of silos, updates local statistics.
        /// </summary>
        /// <note>
        /// This is equivalent with SelectSiloPowerOfK() with chooseHowMany = #Silos
        /// </note>
        private Task<PlacementResult> SelectSiloGreedy(PlacementStrategy strategy, GrainId grain, IPlacementContext context)
        {
            int minLoad = int.MaxValue;
            CachedLocalStat minLoadedSilo = null;
            foreach (CachedLocalStat current in localCache.Values)
            {
                if (IsSiloOverloaded(current.SiloStats)) continue;

                int load = SiloLoad_ByRecentActivations(current);
                if (load >= minLoad) continue;

                minLoadedSilo = current;
                minLoad = load;
            }

            if (minLoadedSilo != null) 
                return MakePlacement(strategy, grain, context, minLoadedSilo);
            
            var debugLog = string.Format("Unable to select a candidate from {0} silos: {1}", localCache.Count, 
                Utils.EnumerableToString(
                    localCache, 
                    kvp => String.Format("SiloAddress = {0} -> {1}", kvp.Key.ToString(), kvp.Value.ToString())));
            logger.Warn(ErrorCode.Placement_ActivationCountBasedDirector_NoSilos, debugLog);
            throw new OrleansException(debugLog);
        }
Beispiel #46
0
 public static void GetGrainTypeInfo(this IPlacementContext @this, GrainId grainId, out string grainClass, out PlacementStrategy placement, string genericArguments = null)
 {
     @this.GetGrainTypeInfo(grainId.GetTypeCode(), out grainClass, out placement, genericArguments);
 }
        public Task<PlacementResult> SelectSiloPowerOfK(PlacementStrategy strategy, GrainId grain, IPlacementContext context)
        {
            // Exclude overloaded silos
            var relevantSilos = new List<CachedLocalStat>();
            foreach (CachedLocalStat current in localCache.Values)
            {
                if (IsSiloOverloaded(current.SiloStats)) continue;

                relevantSilos.Add(current);
            }

            if (relevantSilos.Count > 0)
            {
                int chooseFrom = Math.Min(relevantSilos.Count, chooseHowMany);
                var chooseFromThoseSilos = new List<CachedLocalStat>();
                while (chooseFromThoseSilos.Count < chooseFrom)
                {
                    int index = random.Next(relevantSilos.Count);
                    var pickedSilo = relevantSilos[index];
                    relevantSilos.RemoveAt(index);
                    chooseFromThoseSilos.Add(pickedSilo);
                }

                CachedLocalStat minLoadedSilo = chooseFromThoseSilos.First();
                foreach (CachedLocalStat s in chooseFromThoseSilos)
                {
                    if (SiloLoad_ByRecentActivations(s) < SiloLoad_ByRecentActivations(minLoadedSilo))
                        minLoadedSilo = s;
                }

                return MakePlacement(strategy, grain, context, minLoadedSilo);
            }
            
            var debugLog = string.Format("Unable to select a candidate from {0} silos: {1}", localCache.Count,
                Utils.EnumerableToString(
                    localCache,
                    kvp => String.Format("SiloAddress = {0} -> {1}", kvp.Key.ToString(), kvp.Value.ToString())));
            logger.Warn(ErrorCode.Placement_ActivationCountBasedDirector_NoSilos, debugLog);
            throw new OrleansException(debugLog);
        }
     OnAddActivation(PlacementStrategy strategy, GrainId grain, IPlacementContext context)
 {
     var grainType = context.GetGrainTypeName(grain);
     return Task.FromResult( 
         PlacementResult.SpecifyCreation(context.LocalSilo, strategy, grainType));
 }
Beispiel #49
0
        public ActivationData(ActivationAddress addr, string genericArguments, PlacementStrategy placedUsing, MultiClusterRegistrationStrategy registrationStrategy, IActivationCollector collector, TimeSpan ageLimit)
        {
            if (null == addr) throw new ArgumentNullException("addr");
            if (null == placedUsing) throw new ArgumentNullException("placedUsing");
            if (null == collector) throw new ArgumentNullException("collector");

            logger = LogManager.GetLogger("ActivationData", LoggerType.Runtime);
            ResetKeepAliveRequest();
            Address = addr;
            State = ActivationState.Create;
            PlacedUsing = placedUsing;
            RegistrationStrategy = registrationStrategy;
            if (!Grain.IsSystemTarget && !Constants.IsSystemGrain(Grain))
            {
                this.collector = collector;
            }
            CollectionAgeLimit = ageLimit;

            GrainReference = GrainReference.FromGrainId(addr.Grain, genericArguments,
                Grain.IsSystemTarget ? addr.Silo : null);
        }
Beispiel #50
0
 internal abstract Task<PlacementResult> OnSelectActivation(
     PlacementStrategy strategy, GrainId target, IPlacementContext context);
Beispiel #51
0
 internal abstract Task<PlacementResult> OnAddActivation(
     PlacementStrategy strategy, GrainId grain, IPlacementContext context);
Beispiel #52
0
 public void GetGrainTypeInfo(int typeCode, out string grainClass, out PlacementStrategy placement, string genericArguments = null)
 {
     GrainTypeManager.GetTypeInfo(typeCode, out grainClass, out placement, genericArguments);
 }
 public PlacementStrategySaveResult savePlacementStrategy(PlacementStrategy placementStrategy) {
   object[] results = this.Invoke("savePlacementStrategy", new object[] {placementStrategy});
   return ((PlacementStrategySaveResult) (results[0]));
 }
Beispiel #54
0
 public void GetGrainTypeInfo(int typeCode, out string grainClass, out PlacementStrategy placement, out MultiClusterRegistrationStrategy activationStrategy, string genericArguments = null)
 {
     GrainTypeManager.GetTypeInfo(typeCode, out grainClass, out placement, out activationStrategy, genericArguments);
 }
Beispiel #55
0
        internal bool TryGetTypeInfo(int typeCode, out string grainClass, out PlacementStrategy placement, string genericArguments = null)
        {
            lock (this)
            {
                grainClass = null;
                placement = null;

                if (!implementationIndex.ContainsKey(typeCode))
                    return false;

                var implementation = implementationIndex[typeCode];
                grainClass = implementation.GetClassName(genericArguments);
                placement = implementation.PlacementStrategy;
                return true;
            }
        }
Beispiel #56
0
 internal GrainClassData(int grainTypeCode, string grainClass, bool isGeneric, GrainInterfaceData interfaceData, PlacementStrategy placement)
 {
     GrainTypeCode = grainTypeCode;
     GrainClass = grainClass;
     this.isGeneric = isGeneric;
     this.interfaceData = interfaceData;
     genericClassNames = new Dictionary<string, string>(); // TODO: initialize only for generic classes
     placementStrategy = placement ?? PlacementStrategy.GetDefault();
 }
     OnAddActivation(PlacementStrategy strategy, GrainId grain, IPlacementContext context)
 {
     throw new InvalidOperationException("Client Observers are not activated using the placement subsystem. Grain " + grain);
 }
 public static void GetGrainTypeInfo(this IPlacementContext @this, GrainId grainId, out string grainClass, out PlacementStrategy placement, out MultiClusterRegistrationStrategy activationStrategy, string genericArguments = null)
 {
     @this.GetGrainTypeInfo(grainId.GetTypeCode(), out grainClass, out placement, out activationStrategy, genericArguments);
 }
Beispiel #59
0
 internal void GetTypeInfo(int typeCode, out string grainClass, out PlacementStrategy placement, string genericArguments = null)
 {
     if (!grainInterfaceMap.TryGetTypeInfo(typeCode, out grainClass, out placement, genericArguments))
         throw new OrleansException(String.Format("Unexpected: Cannot find an implementation class for grain interface {0}", typeCode));
 }
 internal override async Task<PlacementResult> OnSelectActivation(
     PlacementStrategy strategy, GrainId target, IPlacementContext context)
 {
     List<ActivationAddress> places = (await context.Lookup(target)).Addresses;
     return ChooseRandomActivation(places, context);
 }