public void UpdateServerList_FirstTimeNotPing_Test() { 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 }); int pingCount = 0; var ping = new PredicatePing(server => { ++pingCount; return(true); }); var serverSource = new DefaultDynamicServerSource(servers); var factory = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager)); var loadBalancerConfig = new LoadBalancerConfig(ping, serverSource); var loadBalancer = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig); var defaultLoadBalancer = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; Assert.AreEqual(0, pingCount); }
public void FilterInvalidEntitiesTest4() { IConfiguration configuration = ObjectFactory.CreateAppSettingConfiguration(); var configurationSource = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration); var configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource); string managerId = "soa"; string loadBalancerId = MethodInfo.GetCurrentMethod().Name; string fileName = configuration.GetPropertyValue("soa.ribbon.local-cache.data-folder") + "\\" + managerId + "." + loadBalancerId; var loadBalancerConfig = new LoadBalancerConfig(new TruePing(), new DefaultServerSource(new Server[0])); var loadBalancerManager = LoadBalancerManager.GetManager(managerId, new LoadBalancerManagerConfig(configurationManager)); var lbContext = new DefaultLoadBalancerContext(loadBalancerManager, loadBalancerId, loadBalancerConfig); try { string data = "[{\"RouteId\":\"default-route-rule\",\"ServerGroups\":[{\"GroupId\":\"default-group-key\",\"Metadata\":[],\"Servers\":[{\"IsAlive\":true,\"Metadata\":[{\"Key\":\"subenv\",\"Value\":\"uat\"},{\"Key\":\"url\",\"Value\":\"http://10.2.44.138/test-service/\"},{\"Key\":\"healthCheckUrl\",\"Value\":\"http://10.2.44.138/test-service/checkhealth.json\"}],\"ServerId\":\"\"},{\"IsAlive\":true,\"Metadata\":[{\"Key\":\"subenv\",\"Value\":\"uat\"},{\"Key\":\"url\",\"Value\":\"http://10.2.42.106/test-service/\"},{\"Key\":\"healthCheckUrl\",\"Value\":\"http://10.2.42.106/test-service/checkhealth.json\"}],\"ServerId\":\"10.2.42.106\"}],\"Weight\":5}]}]"; File.WriteAllText(fileName, data); var result = lbContext.ServerSourceManager.Restore(); result = result.FilterInvalidEntities(_logger, new Dictionary <string, string>()); Assert.AreEqual(1, result.Count); Assert.AreEqual(1, result[0].ServerGroups.Length); Assert.AreEqual(1, result[0].ServerGroups[0].Servers.Length); } finally { File.Delete(fileName); } }
private ILoadBalancer NewLoadBalancer(string loadBalancerId, LoadBalancerConfig loadBalancerConfig) { ParameterChecker.NotNullOrWhiteSpace(loadBalancerId, "loadBalancerId"); ParameterChecker.NotNull(loadBalancerConfig, "loadBalancerConfig"); var lbContext = new DefaultLoadBalancerContext(this, loadBalancerId, loadBalancerConfig); return(lbContext.LoadBalancer); }
public void UpdateServerList_AlwaysChange_Test() { int serverCount = 10; bool onChangeEventRaised = false; 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 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(serverCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); List <Server> newServers = new List <Server>(); for (int i = 0; i < serverCount; i++) { newServers.Add(servers[serverCount - i - 1]); } loadBalancerContext.ServerSourceFilter.OnChange += (o, e) => onChangeEventRaised = true; var serverGroups = new List <ServerGroup>() { new ServerGroup("default", 1, servers) }; var newLoadBalancerRoutes = new List <LoadBalancerRoute>() { new LoadBalancerRoute("default", serverGroups) }; serverSource.LoadBalancerRoutes = newLoadBalancerRoutes; Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); var targetRoute = loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig); var originRoute = loadBalancerContext.ServerSourceFilter.LoadBalancerRoutes[0]; Assert.AreSame(originRoute, targetRoute); Assert.IsTrue(onChangeEventRaised); }
public void UpdateServerList_UnavailableServer_Test() { 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, new Dictionary <string, string>() { { "Index", i.ToString() } }); 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(null, serverSource); var loadBalancer = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig); var defaultLoadBalancer = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; Assert.AreEqual(serverCount, defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); for (int i = serverCount; i < serverCount * 2; i++) { var server = new Server("Server_" + i, i % 2 == 0, new Dictionary <string, string>() { { "Index", i.ToString() } }); servers.Add(server); } serverSource.LoadBalancerRoutes = new DefaultDynamicServerSource(servers).LoadBalancerRoutes; Assert.AreEqual(serverCount * 2, defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); Assert.AreEqual(serverCount + (serverCount / 2), defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length); foreach (var server in defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers) { int index = int.Parse(server.Metadata["Index"]); if (index < serverCount) { Assert.IsTrue(server.IsAlive); } else { Assert.AreEqual(index % 2 == 0, server.IsAlive, server.ToString()); } } }
private void RetainLastServerTest(string loadBalancerId, int serverCount) { int repeatTimes = 10; int repeatInterval = 100; 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 metadata = new Dictionary <string, string>(); metadata["Index"] = i.ToString(); var server = new Server("Server_" + i, metadata); servers.Add(server); } var routeConfig = new LoadBalancerRouteConfig("default", 1, false); var 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 FalsePing(), serverSource); var loadBalancer = factory.GetLoadBalancer(loadBalancerId, loadBalancerConfig); var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; Task.Factory.StartNew(() => { while (true) { for (int i = 0; i < repeatTimes; i++) { var requestContext = loadBalancer.GetRequestContext(requestConfig); requestContext.MarkServerUnavailable(); } Thread.Sleep(repeatInterval); } }); Thread.Sleep(serverCount * repeatTimes * repeatInterval); Console.WriteLine("MinAvailableServerCount: " + loadBalancerContext.MinAvailableServerCount); Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); Assert.AreEqual(loadBalancerContext.MinAvailableServerCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length); }
public void UpdateServerList_NotPingMinAvailableServerCountServer_Test() { 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 FalsePing(), serverSource); var loadBalancer = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig); var loadBalancerContext = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length); servers = new List <Server>(); int minAvailableServerCount = int.Parse(ConfigurationManager.AppSettings["soa." + MethodInfo.GetCurrentMethod().Name + ".ribbon.min-available-server-count"]); for (int i = 0; i < minAvailableServerCount; i++) { servers.Add(new Server("newServer_" + i)); } Console.WriteLine("MinAvailableServerCount: " + minAvailableServerCount); serverSource.LoadBalancerRoutes = new DefaultDynamicServerSource(servers).LoadBalancerRoutes; Assert.AreEqual(minAvailableServerCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); Assert.AreEqual(minAvailableServerCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).AvailableServers.Length); }
public void BackupRestoreTest() { IConfiguration configuration = ObjectFactory.CreateAppSettingConfiguration(); var configurationSource = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration); var configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource); string managerId = "soa"; string loadBalancerId = MethodInfo.GetCurrentMethod().Name; string fileName = configuration.GetPropertyValue("soa.ribbon.local-cache.data-folder") + "\\" + managerId + "." + loadBalancerId; try { var loadBalancerConfig = new LoadBalancerConfig(new TruePing(), new DefaultServerSource(new Server[0])); var loadBalancerManager = LoadBalancerManager.GetManager(managerId, new LoadBalancerManagerConfig(configurationManager)); var lbContext = new DefaultLoadBalancerContext(loadBalancerManager, loadBalancerId, loadBalancerConfig); var manager = new DefaultServerSourceManager(lbContext); List <ServerGroup> serverGroups = new List <ServerGroup>(); for (int i = 0; i < 10; i++) { var server = new Server("Server_" + i, i % 2 == 0); serverGroups.Add(new ServerGroup(server.ServerId, i, new List <Server>() { server })); } var expected = new List <LoadBalancerRoute>() { new LoadBalancerRoute("default", serverGroups) }; manager.Backup(expected); var actual = manager.Restore(); CollectionAssert.AreEqual(expected, actual); } finally { File.Delete(fileName); } }
public void UpdateServerList_NotUpdateEmptyList_Test() { 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 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(serverCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); servers.Clear(); serverSource.LoadBalancerRoutes = new List <LoadBalancerRoute>() { new LoadBalancerRoute("default") }; // clear and set empty list Assert.AreNotEqual(0, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); Assert.AreEqual(serverCount, loadBalancerContext.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers.Length); }
private void PingFailureServerToSuccessTest(string loadBalancerId, int serverCount) { 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); } var routeConfig = new LoadBalancerRouteConfig("default", 1, false); var routeConfigs = 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(loadBalancerId, loadBalancerConfig); var defaultLoadBalancer = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; foreach (var serverContext in defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(routeConfigs).Servers) { serverContext.IsAlive = false; } defaultLoadBalancer.ServerSourceFilter.Refresh(); Thread.Sleep(3000); foreach (var serverContext in defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(routeConfigs).Servers) { Assert.IsTrue(serverContext.IsAlive); } }
public void RemoteServerListFirstTest() { int serverCount = 10; IConfiguration configuration = ObjectFactory.CreateAppSettingConfiguration(); var configurationSource = ObjectFactory.CreateDefaultConfigurationSource(0, "App.config", configuration); var configurationManager = ObjectFactory.CreateDefaultConfigurationManager(configurationSource); string managerId = "soa"; string loadBalancerId = MethodInfo.GetCurrentMethod().Name; string fileName = configuration.GetPropertyValue("soa.ribbon.local-cache.data-folder") + "\\" + managerId + "." + loadBalancerId; try { List <ServerGroup> serverGroups = new List <ServerGroup>(); for (int i = 0; i < serverCount; i++) { var server = new Server("Server_" + i, i % 2 == 0, new Dictionary <string, string>() { { "Index", i.ToString() } }); serverGroups.Add(new ServerGroup(server.ServerId, i, new List <Server>() { server })); } using (Stream stream = File.OpenWrite(fileName)) { _serializer.WriteObject(stream, new List <LoadBalancerRoute>() { new LoadBalancerRoute("default", serverGroups) }); stream.Flush(); } List <Server> servers = new List <Server>(); for (int i = serverCount; i < serverCount * 2; i++) { var server = new Server("Server_" + i, new Dictionary <string, string>() { { "Index", i.ToString() } }); servers.Add(server); } var serverSource = new DefaultDynamicServerSource(servers.ToArray()); var factory = LoadBalancerManager.GetManager("soa", new LoadBalancerManagerConfig(configurationManager)); var loadBalancerConfig = new LoadBalancerConfig(new TruePing(), serverSource); var loadBalancer = factory.GetLoadBalancer(MethodInfo.GetCurrentMethod().Name, loadBalancerConfig); var defaultLoadBalancer = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; LoadBalancerRouteConfig routeConfig = new LoadBalancerRouteConfig("default", 1, false); LoadBalancerRequestConfig requestConfig = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>() { routeConfig }); foreach (var server in defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(requestConfig).Servers) { int index = int.Parse(server.Metadata["Index"]); Assert.IsTrue(index >= serverCount); } } finally { File.Delete(fileName); } }
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 ILoadBalancer GetLoadBalancer(string loadBalancerId, LoadBalancerConfig loadBalancerConfig) { return(_loadBalancerCache.GetOrAdd(loadBalancerId, key => NewLoadBalancer(key, loadBalancerConfig))); }
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_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); } } }
private void HighFailurePercentageAndPingFailure(string loadBalancerId, IPing ping = null) { int serverCount = 10; int repeatTimes = 10; int repeatInterval = 100; 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 metadata = new Dictionary <string, string>(); metadata["Index"] = i.ToString(); var server = new Server("Server_" + i, metadata); servers.Add(server); } var routeConfig = new LoadBalancerRouteConfig("default", 1, false); var routeConfigs = new LoadBalancerRequestConfig(new List <LoadBalancerRouteConfig>() { routeConfig }); 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); var defaultLoadBalancer = ((DefaultLoadBalancer)loadBalancer).LoadBalancerContext; Task.Factory.StartNew(() => { while (true) { for (int i = 0; i < repeatTimes; i++) { var requestContext = loadBalancer.GetRequestContext(routeConfigs); int index = int.Parse(requestContext.Server.Metadata["Index"]); if (index % 2 == 0) { requestContext.MarkServerAvailable(); } else { requestContext.MarkServerUnavailable(); } } Thread.Sleep(repeatInterval); } }); Thread.Sleep(serverCount * repeatTimes * repeatInterval); foreach (var serverContext in defaultLoadBalancer.ServerSourceFilter.GetLoadBalancerRoute(routeConfigs).Servers) { int index = int.Parse(serverContext.Metadata["Index"]); if (index % 2 == 0) { Assert.IsTrue(serverContext.IsAlive, serverContext.ToString()); } else { Assert.IsFalse(serverContext.IsAlive, serverContext.ToString()); } } }
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); } }