Ejemplo n.º 1
0
        public void RoundRobinRuleWithUnavailableServerTest()
        {
            int    serverCount    = 10;
            int    repeatTimes    = 5;
            int    threadCount    = 1;
            int    availableCount = 0;
            string loadBalancerId = MethodInfo.GetCurrentMethod().Name;

            IConfiguration configuration        = ObjectFactory.CreateAppSettingConfiguration();
            var            configurationSource  = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration);
            var            configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource);

            var           urlCountMap = new ConcurrentDictionary <Server, AtomicInteger>();
            List <Server> servers     = new List <Server>();

            for (int i = 0; i < serverCount; i++)
            {
                var metadata = new Dictionary <string, string>();
                metadata["Index"] = i.ToString();
                var server = new Server("Server_" + i, i % 2 == 0, metadata);
                servers.Add(server);
                urlCountMap[server] = 0;
                if (server.IsAlive)
                {
                    availableCount++;
                }
            }

            var routeConfig  = new LoadBalancerRouteConfig("default", 1, false);
            var routeConfigs = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>()
            {
                routeConfig
            });

            var ping               = new PredicatePing(server => int.Parse(server.Metadata["Index"]) % 2 == 0);
            var serverSource       = new DefaultDynamicServerSource(servers);
            var factory            = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager));
            var loadBalancerConfig = new LoadBalancerConfig(ping, serverSource);
            var loadBalancer       = factory.GetLoadBalancer(loadBalancerId, loadBalancerConfig);

            Action action = () =>
            {
                for (int i = 0; i < availableCount * repeatTimes; i++)
                {
                    urlCountMap[loadBalancer.GetRequestContext(routeConfigs).Server].IncrementAndGet();
                }
            };

            List <Task> tasks = new List <Task>();

            for (int i = 0; i < threadCount; i++)
            {
                tasks.Add(Task.Factory.StartNew(action));
            }
            Task.WaitAll(tasks.ToArray());


            foreach (var item in urlCountMap)
            {
                int index = int.Parse(item.Key.Metadata["Index"]);
                if (ping.IsAlive(item.Key))
                {
                    Assert.AreEqual(repeatTimes * threadCount, (int)item.Value);
                }
                else
                {
                    Assert.AreEqual(0, (int)item.Value);
                }
            }
        }
        public void CanReplaceRouteTest()
        {
            int repeat      = 2;
            int serverCount = 10;

            IConfiguration configuration        = ObjectFactory.CreateAppSettingConfiguration();
            var            configurationSource  = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration);
            var            configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource);

            List <Server> servers = new List <Server>();

            for (int i = 0; i < serverCount; i++)
            {
                var server = new Server("Server_" + i, null);
                servers.Add(server);
            }

            LoadBalancerRouteConfig   routeConfig   = new LoadBalancerRouteConfig("default", 1, false);
            LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>()
            {
                routeConfig
            });
            var groups = new List <ServerGroup>()
            {
                new ServerGroup("default", 1, servers)
            };
            var routes = new List <LoadBalancerRoute>()
            {
                new LoadBalancerRoute("default", groups)
            };

            var newRoute = new LoadBalancerRoute("newRoutes", new List <ServerGroup>()
            {
                new ServerGroup("newGroup", 1, servers.Take(1))
            });

            EventHandler <SeekRouteEventArgs> seekRouteEventHandler = (o, e) => e.Route = newRoute;

            var serverSource       = new DefaultDynamicServerSource(routes);
            var factory            = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager));
            var loadBalancerConfig = new LoadBalancerConfig(new TruePing(), serverSource)
            {
                SeekRouteEventHandler = seekRouteEventHandler
            };
            var loadBalancer        = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig);
            var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext;

            Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.LoadBalancerRoutes[0].Servers.Length);
            Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.LoadBalancerRoutes[0].AvailableServers.Length);

            Dictionary <Server, int> serverCountMap = new Dictionary <Server, int>();

            for (int i = 0; i < serverCount * repeat; i++)
            {
                ILoadBalancerRequestContext requestContext = loadBalancer.GetRequestContext(requestConfig);
                if (serverCountMap.ContainsKey(requestContext.Server))
                {
                    serverCountMap[requestContext.Server] += 1;
                }
                else
                {
                    serverCountMap[requestContext.Server] = 1;
                }
            }

            Assert.AreEqual(1, serverCountMap.Count);
            foreach (int count in serverCountMap.Values)
            {
                Assert.AreEqual(serverCount * repeat, count);
            }
        }
        public void UpdateServerList_PullOutServer_Test()
        {
            int repeat      = 2;
            int serverCount = 10;

            IConfiguration configuration        = ObjectFactory.CreateAppSettingConfiguration();
            var            configurationSource  = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration);
            var            configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource);

            List <Server> servers = new List <Server>();

            for (int i = 0; i < serverCount; i++)
            {
                var server = new Server("Server_" + i);
                servers.Add(server);
            }

            LoadBalancerRouteConfig   routeConfig   = new LoadBalancerRouteConfig("default", 1, false);
            LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>()
            {
                routeConfig
            });

            var serverSource        = new DefaultDynamicServerSource(servers);
            var factory             = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager));
            var loadBalancerConfig  = new LoadBalancerConfig(new TruePing(), serverSource);
            var loadBalancer        = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig);
            var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext;

            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length);
            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length);

            Dictionary <string, int> serverCountMap = new Dictionary <string, int>();

            for (int i = 0; i < servers.Count * repeat; i++)
            {
                var requestContext = loadBalancer.GetRequestContext(requestConfig);
                if (!serverCountMap.ContainsKey(requestContext.Server.ServerId))
                {
                    serverCountMap[requestContext.Server.ServerId] = 1;
                }
                else
                {
                    serverCountMap[requestContext.Server.ServerId] += 1;
                }
            }
            foreach (var count in serverCountMap.Values)
            {
                Assert.AreEqual(2, count);
            }

            servers.RemoveAt(0);
            serverSource.LoadBalancerRoutes = new DefaultDynamicServerSource(servers).LoadBalancerRoutes;
            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length);
            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length);

            serverCountMap.Clear();
            for (int i = 0; i < servers.Count * repeat; i++)
            {
                var requestContext = loadBalancer.GetRequestContext(requestConfig);
                if (!serverCountMap.ContainsKey(requestContext.Server.ServerId))
                {
                    serverCountMap[requestContext.Server.ServerId] = 1;
                }
                else
                {
                    serverCountMap[requestContext.Server.ServerId] += 1;
                }
            }
            foreach (var count in serverCountMap.Values)
            {
                Assert.AreEqual(2, count);
            }
        }
        public void UpdateServerList_AddNewRoute_Test()
        {
            int    repeat      = 2;
            int    serverCount = 10;
            string routeId     = "default";

            IConfiguration configuration        = ObjectFactory.CreateAppSettingConfiguration();
            var            configurationSource  = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration);
            var            configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource);

            List <Server> servers = new List <Server>();

            for (int i = 0; i < serverCount; i++)
            {
                var server = new Server("Server_" + i);
                servers.Add(server);
            }

            List <LoadBalancerRoute> routes = new List <LoadBalancerRoute>();

            routes.Add(new LoadBalancerRoute(routeId, new List <ServerGroup>()
            {
                new ServerGroup("default", 1, servers, null)
            }));

            LoadBalancerRouteConfig   routeConfig   = new LoadBalancerRouteConfig(routeId, 0, true);
            LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>()
            {
                routeConfig
            });

            var serverSource        = new DefaultDynamicServerSource(servers);
            var factory             = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager));
            var loadBalancerConfig  = new LoadBalancerConfig(new TruePing(), serverSource);
            var loadBalancer        = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig);
            var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext;

            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length);
            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length);

            Dictionary <string, int> serverCountMap = new Dictionary <string, int>();

            for (int i = 0; i < servers.Count * repeat; i++)
            {
                var requestContext = loadBalancer.GetRequestContext(requestConfig);
                if (!serverCountMap.ContainsKey(requestContext.Server.ServerId))
                {
                    serverCountMap[requestContext.Server.ServerId] = 1;
                }
                else
                {
                    serverCountMap[requestContext.Server.ServerId] += 1;
                }
            }
            foreach (var count in serverCountMap.Values)
            {
                Assert.AreEqual(2, count);
            }


            List <LoadBalancerRoute> newRoutes  = new List <LoadBalancerRoute>();
            List <Server>            newServers = new List <Server>()
            {
                servers[0]
            };

            newRoutes.Add(new LoadBalancerRoute(routeId, new List <ServerGroup>()
            {
                new ServerGroup("default", 1, servers, null)
            }));
            newRoutes.Add(new LoadBalancerRoute("newRoute", new List <ServerGroup>()
            {
                new ServerGroup("default", 1, newServers, null)
            }));
            Console.WriteLine(string.Join(Environment.NewLine, newRoutes));
            serverSource.LoadBalancerRoutes = newRoutes;

            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length);
            Assert.AreEqual(servers.Count, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length);

            serverCountMap.Clear();
            for (int i = 0; i < servers.Count * repeat; i++)
            {
                var requestContext = loadBalancer.GetRequestContext(requestConfig);
                if (!serverCountMap.ContainsKey(requestContext.Server.ServerId))
                {
                    serverCountMap[requestContext.Server.ServerId] = 1;
                }
                else
                {
                    serverCountMap[requestContext.Server.ServerId] += 1;
                }
            }
            foreach (var count in serverCountMap.Values)
            {
                Assert.AreEqual(2, count);
            }
        }
        public void UpdateServerList_RetainStatus_Test()
        {
            IConfiguration configuration        = ObjectFactory.CreateAppSettingConfiguration();
            var            configurationSource  = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration);
            var            configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource);

            int    serverCount    = 10;
            string managerId      = "soa";
            string loadBalancerId = MethodInfo.GetCurrentMethod().Name;

            List <Server> servers = new List <Server>();

            for (int i = 0; i < serverCount; i++)
            {
                servers.Add(new Server("Server_" + i, i % 2 == 0, new Dictionary <string, string>()
                {
                    { "Index", i.ToString() }
                }));
            }

            LoadBalancerRouteConfig   routeConfig   = new LoadBalancerRouteConfig("default", 1, false);
            LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>()
            {
                routeConfig
            });

            var ping = new PredicatePing(server =>
            {
                int index = int.Parse(server.Metadata["Index"]);
                return(index >= serverCount || index % 2 == 0);
            });
            var serverSource        = new DefaultDynamicServerSource(servers);
            var factory             = LoadBalancerManager.GetManager(managerId, new LoadBalancerManagerConfig(configurationManager));
            var loadBalancerConfig  = new LoadBalancerConfig(ping, serverSource);
            var loadBalancer        = factory.GetLoadBalancer(loadBalancerId, loadBalancerConfig);
            var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext;

            List <Server> newServers = new List <Server>();

            for (int i = 0; i < serverCount * 2; i++)
            {
                var metadata = new Dictionary <string, string>()
                {
                    { "Index", i.ToString() },
                    { "Hello", "World" }
                };
                newServers.Add(new Server("Server_" + i, metadata));
            }
            serverSource.LoadBalancerRoutes = new DefaultDynamicServerSource(newServers).LoadBalancerRoutes;
            Assert.AreEqual(serverCount * 2, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length);
            foreach (var server in loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers)
            {
                int index = int.Parse(server.Metadata["Index"]);
                Assert.AreEqual(2, server.Metadata.Count);
                if (index < serverCount)
                {
                    Assert.AreEqual(index % 2 == 0, server.IsAlive, index.ToString());
                }
                else
                {
                    Assert.IsTrue(server.IsAlive);
                }
            }
        }