Exemple #1
0
            public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
            {
                if (unusedSilos.Count == 0 && usedSilos.Count == 0)
                {
                    var silos = context.GetCompatibleSilos(target).OrderBy(x => x).ToArray();
                    foreach (var siloAddress in silos)
                    {
                        unusedSilos.Enqueue(siloAddress);
                    }
                }
                if (unusedSilos.Count == 0 && usedSilos.Count > 0)
                {
                    //check if all usedSilos are compatible
                    //var additionalSilos = context.GetCompatibleSilos(target).OrderBy(x => x).ToArray();

                    unusedSilos = new ConcurrentQueue <SiloAddress>(usedSilos);
                    usedSilos   = new ConcurrentQueue <SiloAddress>();
                }

                //TODO: check if siloAddres is still compatible
                SiloAddress nextSiloAddress = null;

                unusedSilos.TryDequeue(out nextSiloAddress);
                if (nextSiloAddress == null)
                {
                    throw new ArgumentNullException("Couldn't find a compatible silo for grain");
                }
                usedSilos.Enqueue(nextSiloAddress);

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

            return(Task.FromResult(allSilos[random.Next(allSilos.Count)]));
        }
        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()]));
        }
Exemple #4
0
        public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            SiloAddress[] silos;
            if (target.InterfaceVersion == 0)
            {
                silos = context.GetCompatibleSilos(target);
            }
            else
            {
                var    silosByVersion = context.GetCompatibleSilosWithVersions(target);
                var    maxSiloCount   = 0;
                ushort version        = 0;
                foreach (var kvp in silosByVersion)
                {
                    if (kvp.Value.Length > maxSiloCount)
                    {
                        version      = kvp.Key;
                        maxSiloCount = kvp.Value.Length;
                    }
                }
                silos = silosByVersion[version];
            }

            return(Task.FromResult(silos[random.Next(silos.Length)]));
        }
Exemple #5
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]));
        }
Exemple #6
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]));
        }
Exemple #7
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 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);
        }
        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)]));
        }
        public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var silos = context.GetCompatibleSilos(target).OrderBy(s => s).ToArray();
            //尽量把每一个隔开
            long grainid = target.GrainIdentity.PrimaryKeyLong;

            if (silos.Length > grainid)
            {
                return(Task.FromResult(silos[grainid]));
            }
            else
            {
                return(Task.FromResult(silos[random.Next(silos.Length)]));
            }
        }
Exemple #11
0
        public Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var silos   = context.GetCompatibleSilos(target).OrderBy(s => s).ToArray();
            var oddTick = DateTime.UtcNow.Ticks % 2 == 1;

            switch (((TestCustomPlacementStrategy)strategy).Scenario)
            {
            case CustomPlacementScenario.FixedSilo:
                return(Task.FromResult(silos[silos.Length - 2]));    // second from last silos.

            case CustomPlacementScenario.ExcludeOne:
                return(Task.FromResult(oddTick ? silos[0] : silos[silos.Length - 1]));    // randomly return first or last silos

            default:
                throw new InvalidOperationException();     // should never get here, only to make compiler happy
            }
        }
Exemple #12
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)]));
        }
Exemple #13
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)]));
        }
Exemple #14
0
        public async Task <SiloAddress> OnAddActivation(PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            Task <SiloAddress> fromHolder = grainFactory.GetGrain <IPlacementHolder>(target.GrainIdentity.PrimaryKey).GetSiloAddress(
                target.GrainIdentity.TypeCode);

            var         compatibleSilos = context.GetCompatibleSilos(target);
            Random      random          = new Random();
            SiloAddress randomSilo      = compatibleSilos[random.Next(compatibleSilos.Count)];

            SiloAddress holderSilo = await fromHolder;

            if (holderSilo == null)
            {
                return(randomSilo);
            }
            else
            {
                return(holderSilo);
            }
        }
        //TODO: get rid of dead activations. Somehow.
        public virtual Task <SiloAddress> OnAddActivation(
            PlacementStrategy strategy, PlacementTarget target, IPlacementContext context)
        {
            var allSilos          = context.GetCompatibleSilos(target);
            var incompatibleSilos = new List <SiloAddress>();

            SiloAddress leastLoadedSilo = GetLeastLoadedCompatibleSilo(allSilos, incompatibleSilos);

            //Remove incompatible silos
            int value = 0;

            foreach (var silo in incompatibleSilos)
            {
                siloCache.TryRemove(silo, out value);
            }

            UpdateCache(leastLoadedSilo);

            return(Task.FromResult(leastLoadedSilo));
        }