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);
 }
Exemplo n.º 2
0
 public override Task <SiloAddress> OnAddActivation(
     PlacementStrategy strategy,
     PlacementTarget target,
     IPlacementContext context)
 {
     throw new ClientNotAvailableException(target.GrainIdentity);
 }
        public async Task <PlacementResult> SelectOrAddActivation(
            ActivationAddress sendingAddress,
            PlacementTarget targetGrain,
            IPlacementContext context,
            PlacementStrategy strategy)
        {
            if (targetGrain.IsClient)
            {
                var res = await clientObserversPlacementDirector.OnSelectActivation(strategy, targetGrain.GrainId, context);

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

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

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

            return(await AddActivation(targetGrain, context, actualStrategy));
        }
Exemplo n.º 4
0
        public async Task <PlacementResult> SelectOrAddActivation(
            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, targetGrain.GrainIdentity, context);

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

            return(await AddActivation(targetGrain, context, actualStrategy));
        }
Exemplo n.º 5
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(((LegacyGrainId)target.GrainIdentity).TypeCode);

            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,
                       grainTypeName));
        }
Exemplo n.º 6
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));
        }
        public virtual Task <SiloAddress> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var allSilos = context.GetCompatibleSilos(target);

            return(Task.FromResult(allSilos[random.Next(allSilos.Count)]));
        }
Exemplo n.º 8
0
        public Task <PlacementResult> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var grainType = context.GetGrainTypeName(target.GrainId);

            return(Task.FromResult(
                       PlacementResult.SpecifyCreation(context.LocalSilo, strategy, grainType)));
        }
Exemplo n.º 9
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]));
        }
Exemplo n.º 10
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)));
        }
Exemplo n.º 11
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]));
        }
Exemplo n.º 12
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);
        }
        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));
        }
        public Task <SiloAddress> SelectSiloPowerOfK(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var compatibleSilos = context.GetCompatibleSilos(target);
            // Exclude overloaded and non-compatible silos
            var relevantSilos = new List <CachedLocalStat>();

            foreach (CachedLocalStat current in localCache.Values)
            {
                if (IsSiloOverloaded(current.SiloStats))
                {
                    continue;
                }
                if (!compatibleSilos.Contains(current.Address))
                {
                    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      = ThreadSafeRandom.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(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);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets or places an activation.
        /// </summary>
        public ValueTask <PlacementResult> GetOrPlaceActivation(PlacementTarget target, IPlacementRuntime placementRuntime)
        {
            var strategy = _strategyResolver.GetPlacementStrategy(target.GrainIdentity.Type);
            var director = _directorResolver.GetPlacementDirector(strategy);
            var selector = director as IActivationSelector ?? _defaultActivationSelector;

            if (selector.TrySelectActivationSynchronously(strategy, target.GrainIdentity, placementRuntime, out var placementResult))
            {
                return(new ValueTask <PlacementResult>(placementResult));
            }

            return(GetOrPlaceActivationAsync(target, strategy, placementRuntime, selector, director));
        }
Exemplo n.º 16
0
        public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            // If the current silo is not shutting down, place locally
            if (!context.LocalSiloStatus.IsTerminating())
            {
                return(Task.FromResult(context.LocalSilo));
            }

            // otherwise, place somewhere else
            var compatibleSilos = context.GetCompatibleSilos(target);

            return(Task.FromResult(compatibleSilos[random.Next(compatibleSilos.Count)]));
        }
Exemplo n.º 17
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.GetCompatibleSiloList(target).Contains(context.LocalSilo))
            {
                return(base.OnAddActivation(strategy, target, context));
            }

            var grainType = context.GetGrainTypeName(target.GrainId);

            return(Task.FromResult(
                       PlacementResult.SpecifyCreation(context.LocalSilo, strategy, grainType)));
        }
Exemplo n.º 18
0
        private Task <PlacementResult> AddActivation(
            PlacementTarget target,
            IPlacementContext context,
            PlacementStrategy strategy)
        {
            if (target.IsClient)
            {
                throw new InvalidOperationException("Client grains are not activated using the placement subsystem.");
            }

            var director = ResolveDirector(strategy);

            return(director.OnAddActivation(strategy, target, context));
        }
Exemplo n.º 19
0
        /// <summary>
        /// Gets or places an activation.
        /// </summary>
        public ValueTask <PlacementResult> GetOrPlaceActivation(PlacementTarget target, IPlacementRuntime placementRuntime)
        {
            var strategy = _strategyResolver.GetPlacementStrategy(target.GrainIdentity.Type);
            var director = _directorResolver.GetPlacementDirector(strategy);
            var selector = director as IActivationSelector ?? _defaultActivationSelector;

            var selectActivationTask = selector.OnSelectActivation(strategy, target.GrainIdentity, placementRuntime);

            if (selectActivationTask.IsCompletedSuccessfully && selectActivationTask.Result is object)
            {
                return(selectActivationTask);
            }

            return(GetOrPlaceActivationAsync(selectActivationTask, target, strategy, placementRuntime, director));
        }
Exemplo n.º 20
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);

            return(PlacementResult.SpecifyCreation(
                       await director.OnAddActivation(strategy, target, context),
                       strategy,
                       context.GetGrainTypeName(target.GrainIdentity.TypeCode)));
        }
Exemplo n.º 21
0
        private async ValueTask <PlacementResult> GetOrPlaceActivationAsync(
            PlacementTarget target,
            PlacementStrategy strategy,
            IPlacementRuntime placementRuntime,
            IActivationSelector selector,
            IPlacementDirector director)
        {
            var placementResult = await selector.OnSelectActivation(strategy, target.GrainIdentity, placementRuntime);

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

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

            string grainTypeName;

            if (LegacyGrainId.TryConvertFromGrainId(target.GrainIdentity, out var legacyId))
            {
                grainTypeName = placementRuntime.GetGrainTypeName(legacyId.TypeCode);
            }
            else
            {
                grainTypeName = null;
            }

            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,
                       grainTypeName));
        }
Exemplo n.º 22
0
        public virtual Task <SiloAddress> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            string siloRole = target.GrainIdentity.Key.ToString();

            List <SiloAddress> siloAddressesSameRole = membershipTableManager.MembershipTableSnapshot.Entries
                                                       .Where(s => s.Value.Status == SiloStatus.Active && s.Value.RoleName == siloRole)
                                                       .Select(s => s.Key)
                                                       .Intersect(context.GetCompatibleSilos(target))
                                                       .ToList();

            if (siloAddressesSameRole == null || siloAddressesSameRole.Count == 0)
            {
                throw new OrleansException($"Cannot place grain with RoleName {siloRole}. Either Role name is invalid or there are no active silos with type {siloRole} in MembershipTableSnapshot registered yet.");
            }

            return(Task.FromResult(siloAddressesSameRole[ThreadSafeRandom.Next(siloAddressesSameRole.Count)]));
        }
Exemplo n.º 23
0
        public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var compatibleSilos = context.GetCompatibleSilos(target);

            // If the current silo is not shutting down, place locally if we are compatible
            if (!context.LocalSiloStatus.IsTerminating())
            {
                foreach (var silo in compatibleSilos)
                {
                    if (silo.Equals(context.LocalSilo))
                    {
                        return(Task.FromResult(context.LocalSilo));
                    }
                }
            }

            // otherwise, place somewhere else
            return(Task.FromResult(compatibleSilos[ThreadSafeRandom.Next(compatibleSilos.Length)]));
        }
Exemplo n.º 24
0
        public bool TrySelectActivationSynchronously(
            ActivationAddress sendingAddress,
            PlacementTarget targetGrain,
            IPlacementRuntime context,
            PlacementStrategy strategy,
            out PlacementResult placementResult)
        {
            if (targetGrain.IsClient)
            {
                return(clientObserversPlacementDirector.TrySelectActivationSynchronously(
                           strategy,
                           (GrainId)targetGrain.GrainIdentity,
                           context,
                           out placementResult));
            }

            var actualStrategy = strategy ?? defaultPlacementStrategy;
            var director       = ResolveSelector(actualStrategy);

            return(director.TrySelectActivationSynchronously(strategy, (GrainId)targetGrain.GrainIdentity, context, out placementResult));
        }
 OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
 {
     throw new InvalidOperationException("Client Observers are not activated using the placement subsystem. Grain " + target.GrainId);
 }
 public override Task <SiloAddress> OnAddActivation(
     PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
 {
     return(SelectSilo(strategy, target, context));
 }
Exemplo n.º 27
0
 public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
 {
     return(Task.FromResult(context.LocalSilo));
 }