public void DCAwareRoundRobinPolicyTestInParallel() { byte hostLength = 6; var hostList = new List<Host>(); hostList.AddRange(GetHostList(1, 1, "remote1")); hostList.AddRange(GetHostList(1, 1, "remote2")); hostList.AddRange(GetHostList((byte)(hostLength - 3))); hostList.AddRange(GetHostList(1, 2, "remote1")); var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy("local", 1); policy.Initialize(clusterMock.Object); Action action = () => { var resultingHosts = new List<Host>(); var hostEnumerator = policy.NewQueryPlan(new SimpleStatement()); foreach (var h in hostEnumerator) { //Slow down to try to execute it at the same time Thread.Sleep(50); resultingHosts.Add(h); } //The first hosts should be local Assert.True(resultingHosts.Take(hostLength-3).All(h => h.Datacenter == "local")); //It should return the local hosts first and then 1 per each dc Assert.AreEqual(hostLength - 1, resultingHosts.Count); Assert.AreEqual(hostLength - 1, resultingHosts.Distinct().Count()); }; var actions = new List<Action>(); for (var i = 0; i < 100; i++) { actions.Add(action); } var parallelOptions = new ParallelOptions(); parallelOptions.TaskScheduler = new ThreadPerTaskScheduler(); parallelOptions.MaxDegreeOfParallelism = 1000; Parallel.Invoke(parallelOptions, actions.ToArray()); clusterMock.Verify(); }
public void DCAwareRoundRobinPolicyNeverHistsOtherDcWhenNodeUp() { byte hostLength = 5; var hostList = new List<Host>(); //Add a remote host at the beginning of the list hostList.AddRange(GetHostList(2, 1, "remote")); //add local hosts hostList.AddRange(GetHostList((byte)(hostLength - 2))); //Add another remote host at the end hostList.AddRange(GetHostList(2, 2, "remote")); var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy("local"); policy.Initialize(clusterMock.Object); var balancedHosts = policy.NewQueryPlan(new SimpleStatement()); var firstRound = balancedHosts.Take(hostLength - 2).ToList(); //Returns only local hosts Assert.AreEqual(hostLength - 2, firstRound.Where(h => h.Datacenter == "local").Count()); Assert.AreEqual(0, firstRound.Where(h => h.Datacenter != "local").Count()); //following rounds: test it multiple times var followingRounds = new List<Host>(); for (var i = 0; i < 10; i++) { followingRounds.AddRange(policy.NewQueryPlan(new SimpleStatement()).Take(hostLength - 2)); } Assert.AreEqual(10 * (hostLength - 2), followingRounds.Count); //Check that there aren't remote nodes. Assert.AreEqual(0, followingRounds.Where(h => h.Datacenter != "local").Count()); }
public void DCAwareRoundRobinPolicyWithNodesChanging() { var hostList = new ConcurrentBag <Host> { TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2"), TestHelper.CreateHost("0.0.0.3", "dc1"), TestHelper.CreateHost("0.0.0.4", "dc2"), TestHelper.CreateHost("0.0.0.5", "dc1"), TestHelper.CreateHost("0.0.0.6", "dc2"), TestHelper.CreateHost("0.0.0.7", "dc1"), TestHelper.CreateHost("0.0.0.8", "dc2"), TestHelper.CreateHost("0.0.0.9", "dc1"), TestHelper.CreateHost("0.0.0.10", "dc2") }; const string localDc = "dc1"; //to remove the host 3 var hostToRemove = hostList.First(h => TestHelper.GetLastAddressByte(h) == 3); var clusterMock = new Mock <ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(() => { return(hostList.ToList()); }); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy(localDc, 1); policy.Initialize(clusterMock.Object); var hostYielded = new ConcurrentBag <IEnumerable <Host> >(); Action action = () => hostYielded.Add(policy.NewQueryPlan(null, null).ToList()); //Invoke without nodes changing TestHelper.ParallelInvoke(action, 100); Assert.True(hostYielded.Any(hl => hl.Any(h => h == hostToRemove))); var actionList = new List <Action>(Enumerable.Repeat <Action>(action, 1000)); actionList.Insert(200, () => { var host = TestHelper.CreateHost("0.0.0.11", "dc1"); //raise event and then add clusterMock.Raise(c => c.HostAdded += null, host); hostList.Add(host); }); actionList.Insert(400, () => { var host = TestHelper.CreateHost("0.0.0.12", "dc1"); //first add and then raise event hostList.Add(host); clusterMock.Raise(c => c.HostAdded += null, host); }); actionList.Insert(400, () => { var host = hostToRemove; hostList = new ConcurrentBag <Host>(hostList.Where(h => h != hostToRemove)); clusterMock.Raise(c => c.HostRemoved += null, host); }); //Invoke it with nodes being modified TestHelper.ParallelInvoke(actionList); //Clear the host yielded so far hostYielded = new ConcurrentBag <IEnumerable <Host> >(); //Invoke it a some of times more in parallel TestHelper.ParallelInvoke(action, 100); //The removed node should not be returned Assert.False(hostList.Any(h => h == hostToRemove)); Assert.False(hostYielded.Any(hl => hl.Any(h => h == hostToRemove))); }
public void DCAwareRoundRobinPolicyTestInParallel() { var hostList = new List <Host> { TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2"), TestHelper.CreateHost("0.0.0.3", "dc1"), TestHelper.CreateHost("0.0.0.4", "dc2"), TestHelper.CreateHost("0.0.0.5", "dc1"), TestHelper.CreateHost("0.0.0.6", "dc2"), TestHelper.CreateHost("0.0.0.7", "dc1"), TestHelper.CreateHost("0.0.0.8", "dc2"), TestHelper.CreateHost("0.0.0.9", "dc1"), TestHelper.CreateHost("0.0.0.10", "dc2") }; var localHostsLength = hostList.Count(h => h.Datacenter == "dc1"); const string localDc = "dc1"; var clusterMock = new Mock <ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy(localDc, 1); policy.Initialize(clusterMock.Object); var allHosts = new ConcurrentBag <Host>(); var firstHosts = new ConcurrentBag <Host>(); Action action = () => { var hosts = policy.NewQueryPlan(null, null).ToList(); //Check that the value is not repeated Assert.AreEqual(0, hosts.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(y => y.Key) .Count()); firstHosts.Add(hosts[0]); //Add to the general list foreach (var h in hosts) { allHosts.Add(h); } }; var actions = new List <Action>(); const int times = 100; for (var i = 0; i < times; i++) { actions.Add(action); } TestHelper.ParallelInvoke(actions); //Check that the first nodes where different foreach (var h in hostList) { if (h.Datacenter == localDc) { Assert.AreEqual(times / localHostsLength, firstHosts.Count(hc => hc == h)); } else { Assert.AreEqual(0, firstHosts.Count(hc => hc == h)); } } clusterMock.Verify(); }
public void DCAwareRoundRobinPolicyWithNodesChanging() { var hostList = new ConcurrentBag<Host> { TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2"), TestHelper.CreateHost("0.0.0.3", "dc1"), TestHelper.CreateHost("0.0.0.4", "dc2"), TestHelper.CreateHost("0.0.0.5", "dc1"), TestHelper.CreateHost("0.0.0.6", "dc2"), TestHelper.CreateHost("0.0.0.7", "dc1"), TestHelper.CreateHost("0.0.0.8", "dc2"), TestHelper.CreateHost("0.0.0.9", "dc1"), TestHelper.CreateHost("0.0.0.10", "dc2") }; const string localDc = "dc1"; //to remove the host 3 var hostToRemove = hostList.First(h => TestHelper.GetLastAddressByte(h) == 3); var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(() => { return hostList.ToList(); }); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy(localDc, 1); policy.Initialize(clusterMock.Object); var hostYielded = new ConcurrentBag<IEnumerable<Host>>(); Action action = () => hostYielded.Add(policy.NewQueryPlan(null, null).ToList()); //Invoke without nodes changing TestHelper.ParallelInvoke(action, 100); Assert.True(hostYielded.Any(hl => hl.Any(h => h == hostToRemove))); var actionList = new List<Action>(Enumerable.Repeat<Action>(action, 1000)); actionList.Insert(200, () => { var host = TestHelper.CreateHost("0.0.0.11", "dc1"); //raise event and then add clusterMock.Raise(c => c.HostAdded += null, host); hostList.Add(host); }); actionList.Insert(400, () => { var host = TestHelper.CreateHost("0.0.0.12", "dc1"); //first add and then raise event hostList.Add(host); clusterMock.Raise(c => c.HostAdded += null, host); }); actionList.Insert(400, () => { var host = hostToRemove; hostList = new ConcurrentBag<Host>(hostList.Where(h => h != hostToRemove)); clusterMock.Raise(c => c.HostRemoved += null, host); }); //Invoke it with nodes being modified TestHelper.ParallelInvoke(actionList); //Clear the host yielded so far hostYielded = new ConcurrentBag<IEnumerable<Host>>(); //Invoke it a some of times more in parallel TestHelper.ParallelInvoke(action, 100); //The removed node should not be returned Assert.False(hostList.Any(h => h == hostToRemove)); Assert.False(hostYielded.Any(hl => hl.Any(h => h == hostToRemove))); }
public void DCAwareRoundRobinPolicyCachesLocalNodes() { var hostList = new List<Host> { TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2"), TestHelper.CreateHost("0.0.0.3", "dc1"), TestHelper.CreateHost("0.0.0.4", "dc2"), TestHelper.CreateHost("0.0.0.5", "dc1"), TestHelper.CreateHost("0.0.0.6", "dc2"), TestHelper.CreateHost("0.0.0.7", "dc1"), TestHelper.CreateHost("0.0.0.8", "dc2"), TestHelper.CreateHost("0.0.0.9", "dc1"), TestHelper.CreateHost("0.0.0.10", "dc2") }; const string localDc = "dc1"; var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy(localDc, 1); policy.Initialize(clusterMock.Object); var instances = new ConcurrentBag<object>(); Action action = () => instances.Add(policy.GetHosts()); TestHelper.ParallelInvoke(action, 100); Assert.AreEqual(1, instances.GroupBy(i => i.GetHashCode()).Count()); }
public void DCAwareRoundRobinPolicyTestInParallel() { var hostList = new List<Host> { TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2"), TestHelper.CreateHost("0.0.0.3", "dc1"), TestHelper.CreateHost("0.0.0.4", "dc2"), TestHelper.CreateHost("0.0.0.5", "dc1"), TestHelper.CreateHost("0.0.0.6", "dc2"), TestHelper.CreateHost("0.0.0.7", "dc1"), TestHelper.CreateHost("0.0.0.8", "dc2"), TestHelper.CreateHost("0.0.0.9", "dc1"), TestHelper.CreateHost("0.0.0.10", "dc2") }; var localHostsLength = hostList.Count(h => h.Datacenter == "dc1"); const string localDc = "dc1"; var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy(localDc, 1); policy.Initialize(clusterMock.Object); var allHosts = new ConcurrentBag<Host>(); var firstHosts = new ConcurrentBag<Host>(); Action action = () => { var hosts = policy.NewQueryPlan(null, null).ToList(); //Check that the value is not repeated Assert.AreEqual(0, hosts.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(y => y.Key) .Count()); firstHosts.Add(hosts[0]); //Add to the general list foreach (var h in hosts) { allHosts.Add(h); } }; var actions = new List<Action>(); const int times = 100; for (var i = 0; i < times; i++) { actions.Add(action); } TestHelper.ParallelInvoke(actions); //Check that the first nodes where different foreach (var h in hostList) { if (h.Datacenter == localDc) { Assert.AreEqual(times/localHostsLength, firstHosts.Count(hc => hc == h)); } else { Assert.AreEqual(0, firstHosts.Count(hc => hc == h)); } } clusterMock.Verify(); }
public void DCAwareRoundRobinInitializeNotMatchingDcThrows() { var hostList = new List<Host> { TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2") }; var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); var policy = new DCAwareRoundRobinPolicy("not_valid_dc"); Assert.Throws<ArgumentException>(() => policy.Initialize(clusterMock.Object)); }
public void DCAwareRoundRobinInitializeRetrievesLocalDc() { var hostList = new List<Host> { TestHelper.CreateHost("0.0.0.1", null), TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2") }; var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); var policy = new DCAwareRoundRobinPolicy(); policy.Initialize(clusterMock.Object); Assert.AreEqual(HostDistance.Local, policy.Distance(hostList[1])); Assert.AreNotEqual(HostDistance.Local, policy.Distance(hostList[2])); }
public void DCAwareRoundRobinYieldsRemoteNodesAtTheEnd() { var hostList = new List<Host> { //5 local nodes and 4 remote TestHelper.CreateHost("0.0.0.1", "dc1"), TestHelper.CreateHost("0.0.0.2", "dc2"), TestHelper.CreateHost("0.0.0.3", "dc1"), TestHelper.CreateHost("0.0.0.4", "dc2"), TestHelper.CreateHost("0.0.0.5", "dc1"), TestHelper.CreateHost("0.0.0.6", "dc2"), TestHelper.CreateHost("0.0.0.7", "dc1"), TestHelper.CreateHost("0.0.0.8", "dc2"), TestHelper.CreateHost("0.0.0.9", "dc1"), TestHelper.CreateHost("0.0.0.10", "dc2") }; var localHostsLength = hostList.Count(h => h.Datacenter == "dc1"); const string localDc = "dc1"; var clusterMock = new Mock<ICluster>(); clusterMock .Setup(c => c.AllHosts()) .Returns(hostList); //Initialize the balancing policy var policy = new DCAwareRoundRobinPolicy(localDc, 1); policy.Initialize(clusterMock.Object); Action action = () => { var hosts = policy.NewQueryPlan(null, null).ToList(); for (var i = 0; i < hosts.Count; i++) { var h = hosts[i]; if (i < localHostsLength) { Assert.AreEqual(localDc, h.Datacenter); } else { Assert.AreNotEqual(localDc, h.Datacenter); } } }; TestHelper.ParallelInvoke(action, 100); }