private CacheNode <TKey, TValue> .Rebalancer CreateRebalancer(RebalancingMode mode)
        {
            var count = _cacheNodeCount;

            CacheNode <TKey, TValue> .Rebalancer rebalancer;
            var targetMode = mode;

            if (targetMode == RebalancingMode.Heavy)
            {
                rebalancer = new CacheNode <TKey, TValue> .HeavyRebalancer(_root, count, _currentPath,
                                                                           (_currentPath + 1) % CacheNode <TKey, TValue> .PathCount);
            }
            else if (targetMode == RebalancingMode.Light)
            {
                rebalancer = new CacheNode <TKey, TValue> .LightRebalancer(_root, count, _currentPath,
                                                                           (_currentPath + 1) % CacheNode <TKey, TValue> .PathCount);
            }
            else if (targetMode == RebalancingMode.Hybrid)
            {
                rebalancer = new CacheNode <TKey, TValue> .HybridRebalancer(_root, count, _currentPath,
                                                                            (_currentPath + 1) % CacheNode <TKey, TValue> .PathCount, TimeSpan.FromSeconds(10));
            }
            else
            {
                Debugger.Break();
                throw new ArgumentException();
            }
            return(rebalancer);
        }
        public ZooKeeperProvider(string zookeeperHosts,
                                 string zooKeeperRootPath,
                                 TimeSpan sessionTimeout,
                                 TimeSpan connectTimeout,
                                 TimeSpan minimumRebalancingInterval,
                                 RebalancingMode rebalancingMode,
                                 ILogger logger,
                                 IZooKeeperService zooKeeperService = null)
        {
            this.zooKeeperRootPath          = zooKeeperRootPath;
            this.rebalancingMode            = rebalancingMode;
            this.logger                     = logger;
            this.sessionTimeout             = sessionTimeout;
            this.connectTimeout             = connectTimeout;
            this.minimumRebalancingInterval = minimumRebalancingInterval;
            this.rand = new Random(Guid.NewGuid().GetHashCode());

            if (zooKeeperService == null)
            {
                this.zooKeeperService = new ZooKeeperService(zookeeperHosts);
            }
            else
            {
                this.zooKeeperService = zooKeeperService;
            }
        }
        internal void QueueRebalance(RebalancingMode mode)
        {
            if (Interlocked.CompareExchange(ref _rebalancingQueued, Queued, NotQueued) == NotQueued)
            {
                Channel.SendMessageAboutOneResource(Guid.Empty, Actions.RebalanceQueued);

                _queueRebalance(() =>
                {
                    Channel.SendMessageAboutOneResource(Guid.Empty, Actions.RebalanceStarted);
                    var time = Stopwatch.StartNew();

                    bool lockTaken = false;
                    try
                    {
                        _writeLock.Enter(ref lockTaken);
                        var rebalancer = CreateRebalancer(mode);
                        _expectedGet   = rebalancer.Rebalance(_token);
                        _root          = rebalancer.ConstructNewTreeAfterCalculation();
                        _currentPath   = rebalancer.OutPath;
                        rebalancer.Dispose();
                    }
                    finally
                    {
                        time.Stop();
                        Channel.SendMessageAboutOneResource(Guid.Empty, Actions.RebalanceEnded,
                                                            time.Elapsed);
                        _rebalancingQueued = NotQueued;
                        if (lockTaken)
                        {
                            _writeLock.Exit();
                        }
                    }
                });
            }
        }
 public ResourceManager(IZooKeeperService zooKeeperService,
                        ILogger logger,
                        OnChangeActions onChangeActions,
                        RebalancingMode rebalancingMode)
 {
     this.zooKeeperService = zooKeeperService;
     this.logger           = logger;
     this.resources        = new List <string>();
     this.assignmentStatus = AssignmentStatus.NoAssignmentYet;
     this.onChangeActions  = onChangeActions;
     this.rebalancingMode  = rebalancingMode;
 }
Exemple #5
0
        public ExperimentConfiguration(ExperimentConfiguration baseConfiguration)
        {
            this.GarbageCollectingRate = baseConfiguration.GarbageCollectingRate;
            this.KeyCount = baseConfiguration.KeyCount;
            this.MaxFixedCache2BranchLength = baseConfiguration.MaxFixedCache2BranchLength;
            this.PenaltyForExternalGet      = baseConfiguration.PenaltyForExternalGet;
            this.RebalanceActive            = baseConfiguration.RebalanceActive;
            this.ResultShowRate             = baseConfiguration.ResultShowRate;
            this.Seed = baseConfiguration.Seed;
            this.Frequences.AddRange(baseConfiguration.Frequences);
            this.TryCount        = baseConfiguration.TryCount;
            this.WriteTimeout    = baseConfiguration.WriteTimeout;
            this.ParallelWork    = baseConfiguration.ParallelWork;
            this.RebalancingMode = baseConfiguration.RebalancingMode;

            this.GoneIntensityForBranchDecrease = baseConfiguration.GoneIntensityForBranchDecrease;
            this.GoneIntensityForBranchIncrease = baseConfiguration.GoneIntensityForBranchIncrease;
            this.CheckThreshold             = baseConfiguration.CheckThreshold;
            this.MinBranchLengthToRebalance = baseConfiguration.MinBranchLengthToRebalance;
        }
Exemple #6
0
        static async Task MainAsync(string[] args)
        {
            // support graceful shutdown in Docker
            var ended    = new ManualResetEventSlim();
            var starting = new ManualResetEventSlim();

            AssemblyLoadContext.Default.Unloading += ctx =>
            {
                System.Console.WriteLine("Unloading fired");
                starting.Set();
                System.Console.WriteLine("Waiting for completion");
                ended.Wait();
            };

            try
            {
                var cts     = new CancellationTokenSource();
                var builder = new ConfigurationBuilder()
                              .AddEnvironmentVariables("Tester.RabbitMQ.")
                              .AddCommandLine(args);
                IConfigurationRoot configuration = builder.Build();
                Task task     = null;
                bool shutdown = false;

                string mode         = GetMandatoryArg(configuration, "Mode");
                string rabbitMQHost = GetMandatoryArg(configuration, "RabbitMQHost");

                if (mode == "publish")
                {
                    LogInfo("Publish mode");
                    Console.Title = "Rebalanser Tester - RabbitMQ Publisher";
                    var exchange     = GetMandatoryArg(configuration, "Exchange");
                    var stateCount   = int.Parse(GetMandatoryArg(configuration, "Keys"));
                    var sendInterval = TimeSpan.FromMilliseconds(int.Parse(GetMandatoryArg(configuration, "SendIntervalMs")));

                    task = Task.Run(async() => await PublishSequenceAsync(rabbitMQHost, exchange, stateCount, sendInterval, cts.Token));
                }
                else if (mode == "consume")
                {
                    LogInfo("Consume mode");
                    Console.Title = "Rebalanser Tester - RabbitMQ Consumer";
                    string consumerGroup = GetMandatoryArg(configuration, "Group");
                    string outputQueue   = GetMandatoryArg(configuration, "OutQueue");
                    ZooKeeperHosts       = GetMandatoryArg(configuration, "ZooKeeperHosts");
                    RebalanceMode        = GetMandatoryArg(configuration, "RebalancingMode") == "resource-barrier" ? RebalancingMode.ResourceBarrier : RebalancingMode.GlobalBarrier;
                    MinRebalanceInterval = TimeSpan.FromSeconds(int.Parse(GetMandatoryArg(configuration, "MinRebalanceIntervalSeconds")));
                    SessionTimeout       = TimeSpan.FromSeconds(int.Parse(GetMandatoryArg(configuration, "RebalanserSessionTimeoutSeconds")));

                    task = Task.Run(async() => await RunRebalanserAsync(rabbitMQHost, consumerGroup, outputQueue, cts.Token));
                }
                else if (mode == "verify-output-seq")
                {
                    LogInfo("Output mode");
                    Console.Title = "Rebalanser Tester - Sequence Validator";
                    var queue = GetMandatoryArg(configuration, "Queue");
                    task = Task.Run(() => StartConsumingAndPrinting(rabbitMQHost, queue, cts.Token));
                }
                else if (mode == "setup")
                {
                    string outputQueue    = GetMandatoryArg(configuration, "OutQueue");
                    string consumerGroup  = GetMandatoryArg(configuration, "Group");
                    string queuePrefix    = GetMandatoryArg(configuration, "QueuePrefix");
                    int    queueCount     = int.Parse(GetMandatoryArg(configuration, "QueueCount"));
                    string exchange       = GetMandatoryArg(configuration, "Exchange");
                    string zooKeeperHosts = GetMandatoryArg(configuration, "ZooKeeperHosts");
                    string rmqNodes       = GetMandatoryArg(configuration, "RabbitMQNodes");

                    var rabbitConn = new RabbitConnection()
                    {
                        Host           = rabbitMQHost,
                        Password       = "******",
                        ManagementPort = 15672,
                        Port           = 5672,
                        Username       = "******",
                        VirtualHost    = "/"
                    };
                    var queueManager = new QueueManager();
                    await queueManager.InitializeAsync(rabbitConn, zooKeeperHosts, rmqNodes);

                    await queueManager.MeetDesiredStateAsync(consumerGroup, queuePrefix, queueCount, exchange, outputQueue);

                    shutdown = true;
                }
                else
                {
                    Console.WriteLine("Unknown command");
                    shutdown = true;
                }


                // wait for shutdown signal
#if DEBUG
                Console.WriteLine("Press any key to shutdown");

                while (!shutdown)
                {
                    if (Console.KeyAvailable)
                    {
                        Console.WriteLine("Key pressed");
                        shutdown = true;
                    }
                    else if (task.IsCompleted)
                    {
                        Console.WriteLine("Task completed");
                        shutdown = true;
                    }

                    Thread.Sleep(500);
                }
#else
                while (!shutdown)
                {
                    if (starting.IsSet)
                    {
                        shutdown = true;
                    }
                    else if (task.IsCompleted)
                    {
                        shutdown = true;
                    }

                    Thread.Sleep(500);
                }
#endif

                Console.WriteLine("Received signal gracefully shutting down");
                cts.Cancel();
                if (task != null)
                {
                    task.GetAwaiter().GetResult();
                }
                ended.Set();
            }
            catch (CmdArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                ended.Set();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                ended.Set();
            }
        }
        public ExperimentConfiguration(ExperimentConfiguration baseConfiguration)
        {        
            this.GarbageCollectingRate  = baseConfiguration.GarbageCollectingRate;
            this.KeyCount  = baseConfiguration.KeyCount;
            this.MaxFixedCache2BranchLength = baseConfiguration.MaxFixedCache2BranchLength;
            this.PenaltyForExternalGet = baseConfiguration.PenaltyForExternalGet;
            this.RebalanceActive = baseConfiguration.RebalanceActive;
            this.ResultShowRate = baseConfiguration.ResultShowRate;
            this.Seed = baseConfiguration.Seed;
            this.Frequences.AddRange(baseConfiguration.Frequences);
            this.TryCount = baseConfiguration.TryCount;
            this.WriteTimeout = baseConfiguration.WriteTimeout;
            this.ParallelWork = baseConfiguration.ParallelWork;
            this.RebalancingMode = baseConfiguration.RebalancingMode;

            this.GoneIntensityForBranchDecrease = baseConfiguration.GoneIntensityForBranchDecrease;
            this.GoneIntensityForBranchIncrease = baseConfiguration.GoneIntensityForBranchIncrease;
            this.CheckThreshold = baseConfiguration.CheckThreshold;
            this.MinBranchLengthToRebalance = baseConfiguration.MinBranchLengthToRebalance;
        }