Ejemplo n.º 1
0
        public void TokenMap_Build_SimpleStrategy_Adjacent_Ranges_Test()
        {
            const string strategy = ReplicationStrategies.SimpleStrategy;
            var          hosts    = new[]
            {
                //0 and 100 are adjacent
                TestHelper.CreateHost("192.168.0.1", "dc1", "rack1", new HashSet <string> {
                    "0", "100", "1000"
                }),
                TestHelper.CreateHost("192.168.0.2", "dc1", "rack1", new HashSet <string> {
                    "200", "2000", "20000"
                }),
                TestHelper.CreateHost("192.168.0.3", "dc1", "rack1", new HashSet <string> {
                    "300", "3000", "30000"
                })
            };
            var ks = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int> {
                { "replication_factor", 2 }
            });
            var map      = TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks });
            var replicas = map.GetReplicas("ks1", new M3PToken(0));

            Assert.AreEqual(2, replicas.Count);
            //It should contain the first host and the second, even though the first host contains adjacent
            CollectionAssert.AreEqual(new byte[] { 1, 2 }, replicas.Select(TestHelper.GetLastAddressByte));
        }
Ejemplo n.º 2
0
        public void TokenMap_SimpleStrategy_With_Hosts_Without_Tokens()
        {
            var hosts = new List <Host>
            {
                { TestHelper.CreateHost("192.168.0.0", "dc1", "rack", new HashSet <string> {
                        "0"
                    }) },
                { TestHelper.CreateHost("192.168.0.1", "dc1", "rack", new string[0]) },
                { TestHelper.CreateHost("192.168.0.2", "dc1", "rack", new HashSet <string> {
                        "20"
                    }) }
            };
            var keyspaces = new List <KeyspaceMetadata>
            {
                TokenTests.CreateSimpleKeyspace("ks1", 10),
                TokenTests.CreateSimpleKeyspace("ks2", 2)
            };
            var tokenMap = TokenMap.Build("Murmur3Partitioner", hosts, keyspaces);

            //the primary replica and the next
            var replicas = tokenMap.GetReplicas("ks1", new M3PToken(0));

            //The node without tokens should not be considered
            CollectionAssert.AreEqual(new byte[] { 0, 2 }, replicas.Select(TestHelper.GetLastAddressByte));
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(-100));
            CollectionAssert.AreEqual(new byte[] { 0, 2 }, replicas.Select(TestHelper.GetLastAddressByte));
            //Greater than the greatest token
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(500000));
            CollectionAssert.AreEqual(new byte[] { 0, 2 }, replicas.Select(TestHelper.GetLastAddressByte));

            //The next replica should be the first
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(20));
            CollectionAssert.AreEqual(new byte[] { 2, 0 }, replicas.Select(TestHelper.GetLastAddressByte));
        }
Ejemplo n.º 3
0
        public void TokenMapSimpleStrategyWithKeyspaceTest()
        {
            var rp    = new ConstantReconnectionPolicy(1);
            var hosts = new List <Host>
            {
                { TestHelper.CreateHost("192.168.0.0", "dc1", "rack", new HashSet <string> {
                        "0"
                    }) },
                { TestHelper.CreateHost("192.168.0.1", "dc1", "rack", new HashSet <string> {
                        "10"
                    }) },
                { TestHelper.CreateHost("192.168.0.2", "dc1", "rack", new HashSet <string> {
                        "20"
                    }) }
            };
            const string strategy  = ReplicationStrategies.SimpleStrategy;
            var          keyspaces = new List <KeyspaceMetadata>
            {
                new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int> {
                    { "replication_factor", 2 }
                }),
                new KeyspaceMetadata(null, "ks2", true, strategy, new Dictionary <string, int> {
                    { "replication_factor", 10 }
                })
            };
            var tokenMap = TokenMap.Build("Murmur3Partitioner", hosts, keyspaces);

            //the primary replica and the next
            var replicas = tokenMap.GetReplicas("ks1", new M3PToken(0));

            Assert.AreEqual("0,1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(-100));
            Assert.AreEqual("0,1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            //Greater than the greatest token
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(500000));
            Assert.AreEqual("0,1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //The next replica should be the first
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(20));
            Assert.AreEqual("2,0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //The closest replica and the next
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(19));
            Assert.AreEqual("2,0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //Even if the replication factor is greater than the ring, it should return only ring size
            replicas = tokenMap.GetReplicas("ks2", new M3PToken(5));
            Assert.AreEqual("1,2,0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //The primary replica only as the keyspace was not found
            replicas = tokenMap.GetReplicas(null, new M3PToken(0));
            Assert.AreEqual("0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas(null, new M3PToken(10));
            Assert.AreEqual("1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas("ks_does_not_exist", new M3PToken(20));
            Assert.AreEqual("2", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas(null, new M3PToken(19));
            Assert.AreEqual("2", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
        }
Ejemplo n.º 4
0
        public void TokenMap_SimpleStrategy_With_Keyspace_Test()
        {
            var hosts = new List <Host>
            {
                { TestHelper.CreateHost("192.168.0.0", "dc1", "rack", new HashSet <string> {
                        "0"
                    }) },
                { TestHelper.CreateHost("192.168.0.1", "dc1", "rack", new HashSet <string> {
                        "10"
                    }) },
                { TestHelper.CreateHost("192.168.0.2", "dc1", "rack", new HashSet <string> {
                        "20"
                    }) }
            };
            var keyspaces = new List <KeyspaceMetadata>
            {
                TokenTests.CreateSimpleKeyspace("ks1", 2),
                TokenTests.CreateSimpleKeyspace("ks2", 10)
            };
            var tokenMap = TokenMap.Build("Murmur3Partitioner", hosts, keyspaces);

            //the primary replica and the next
            var replicas = tokenMap.GetReplicas("ks1", new M3PToken(0));

            Assert.AreEqual("0,1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(-100));
            Assert.AreEqual("0,1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            //Greater than the greatest token
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(500000));
            Assert.AreEqual("0,1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //The next replica should be the first
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(20));
            Assert.AreEqual("2,0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //The closest replica and the next
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(19));
            Assert.AreEqual("2,0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //Even if the replication factor is greater than the ring, it should return only ring size
            replicas = tokenMap.GetReplicas("ks2", new M3PToken(5));
            Assert.AreEqual("1,2,0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //The primary replica only as the keyspace was not found
            replicas = tokenMap.GetReplicas(null, new M3PToken(0));
            Assert.AreEqual("0", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas(null, new M3PToken(10));
            Assert.AreEqual("1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas("ks_does_not_exist", new M3PToken(20));
            Assert.AreEqual("2", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas(null, new M3PToken(19));
            Assert.AreEqual("2", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
        }
Ejemplo n.º 5
0
        public void TokenMap_Build_NetworkTopology_Multiple_Racks_Skipping_Hosts_Test()
        {
            const string strategy = ReplicationStrategies.NetworkTopologyStrategy;
            var          hosts    = new[]
            {
                // DC1 racks has contiguous tokens
                // DC2 racks are properly organized
                TestHelper.CreateHost("192.168.0.0", "dc1", "dc1_rack1", new HashSet <string> {
                    "0"
                }),
                TestHelper.CreateHost("192.168.0.1", "dc2", "dc2_rack1", new HashSet <string> {
                    "1"
                }),
                TestHelper.CreateHost("192.168.0.2", "dc1", "dc1_rack1", new HashSet <string> {
                    "2"
                }),
                TestHelper.CreateHost("192.168.0.3", "dc2", "dc2_rack2", new HashSet <string> {
                    "3"
                }),
                TestHelper.CreateHost("192.168.0.4", "dc1", "dc1_rack2", new HashSet <string> {
                    "4"
                }),
                TestHelper.CreateHost("192.168.0.5", "dc2", "dc2_rack1", new HashSet <string> {
                    "5"
                }),
                TestHelper.CreateHost("192.168.0.6", "dc1", "dc1_rack2", new HashSet <string> {
                    "6"
                }),
                TestHelper.CreateHost("192.168.0.7", "dc2", "dc2_rack2", new HashSet <string> {
                    "7"
                })
            };
            var ks = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int>
            {
                { "dc1", 3 },
                { "dc2", 2 }
            });
            var map    = TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks });
            var values = new[]
            {
                Tuple.Create(0, new byte[] { 0, 1, 3, 4, 2 }),
                Tuple.Create(1, new byte[] { 1, 2, 3, 4, 6 }),
                Tuple.Create(4, new byte[] { 4, 5, 7, 0, 6 })
            };

            foreach (var v in values)
            {
                var replicas = map.GetReplicas("ks1", new M3PToken(v.Item1));
                CollectionAssert.AreEqual(v.Item2, replicas.Select(TestHelper.GetLastAddressByte));
            }
        }
Ejemplo n.º 6
0
        public void TokenMap_Build_Should_Memorize_Tokens_Per_Replication_Test()
        {
            const string strategy = ReplicationStrategies.NetworkTopologyStrategy;
            var          hosts    = new[]
            {
                //0 and 100 are adjacent
                TestHelper.CreateHost("192.168.0.1", "dc1", "dc1_rack1", new HashSet <string> {
                    "0", "100", "1000"
                }),
                TestHelper.CreateHost("192.168.0.2", "dc1", "dc1_rack2", new HashSet <string> {
                    "200", "2000", "20000"
                }),
                TestHelper.CreateHost("192.168.0.3", "dc1", "dc1_rack1", new HashSet <string> {
                    "300", "3000", "30000"
                }),
                TestHelper.CreateHost("192.168.0.4", "dc2", "dc2_rack1", new HashSet <string> {
                    "400", "4000", "40000"
                }),
                TestHelper.CreateHost("192.168.0.5", "dc2", "dc2_rack2", new HashSet <string> {
                    "500", "5000", "50000"
                })
            };
            var ks1 = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int> {
                { "dc1", 2 }, { "dc2", 1 }
            });
            var ks2 = new KeyspaceMetadata(null, "ks2", true, strategy, new Dictionary <string, int> {
                { "dc1", 2 }, { "dc2", 1 }
            });
            var ks3 = new KeyspaceMetadata(null, "ks3", true, strategy, new Dictionary <string, int> {
                { "dc1", 2 }
            });
            var map     = TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks1, ks2, ks3 });
            var tokens1 = map.GetByKeyspace("ks1");
            var tokens2 = map.GetByKeyspace("ks2");
            var tokens3 = map.GetByKeyspace("ks3");

            Assert.AreSame(tokens1, tokens2);
            Assert.AreNotSame(tokens1, tokens3);
        }
Ejemplo n.º 7
0
        public void TokenMap_Build_NetworkTopology_Quickly_Leave_When_Dc_Not_Found()
        {
            const string strategy = ReplicationStrategies.NetworkTopologyStrategy;
            var          hosts    = new Host[100];

            for (var i = 0; i < hosts.Length; i++)
            {
                hosts[i] = TestHelper.CreateHost("192.168.0." + i, "dc" + (i % 2), "rack1", new HashSet <string>());
            }
            for (var i = 0; i < 256 * hosts.Length; i++)
            {
                var tokens = (HashSet <string>)hosts[i % hosts.Length].Tokens;
                tokens.Add(i.ToString());
            }
            var ks = new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int>
            {
                { "dc1", 3 },
                { "dc2", 2 },
                { "dc3", 1 }
            });

            TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks });
        }
Ejemplo n.º 8
0
        public void TokenMap_Build_SimpleStrategy_Adjacent_Ranges_Test()
        {
            var hosts = new[]
            {
                //0 and 100 are adjacent
                TestHelper.CreateHost("192.168.0.1", "dc1", "rack1", new HashSet <string> {
                    "0", "100", "1000"
                }),
                TestHelper.CreateHost("192.168.0.2", "dc1", "rack1", new HashSet <string> {
                    "200", "2000", "20000"
                }),
                TestHelper.CreateHost("192.168.0.3", "dc1", "rack1", new HashSet <string> {
                    "300", "3000", "30000"
                })
            };
            var ks       = FakeSchemaParserFactory.CreateSimpleKeyspace("ks1", 2);
            var map      = TokenMap.Build("Murmur3Partitioner", hosts, new[] { ks });
            var replicas = map.GetReplicas("ks1", new M3PToken(0));

            Assert.AreEqual(2, replicas.Count);
            //It should contain the first host and the second, even though the first host contains adjacent
            CollectionAssert.AreEqual(new byte[] { 1, 2 }, replicas.Select(TestHelper.GetLastAddressByte));
        }
Ejemplo n.º 9
0
        public void TokenMap_NetworkTopologyStrategy_With_Keyspace_Test()
        {
            var hosts = new List <Host>
            {
                { TestHelper.CreateHost("192.168.0.0", "dc1", "rack1", new HashSet <string> {
                        "0"
                    }) },
                { TestHelper.CreateHost("192.168.0.1", "dc1", "rack1", new HashSet <string> {
                        "100"
                    }) },
                { TestHelper.CreateHost("192.168.0.2", "dc1", "rack1", new HashSet <string> {
                        "200"
                    }) },
                { TestHelper.CreateHost("192.168.0.100", "dc2", "rack1", new HashSet <string> {
                        "1"
                    }) },
                { TestHelper.CreateHost("192.168.0.101", "dc2", "rack1", new HashSet <string> {
                        "101"
                    }) },
                { TestHelper.CreateHost("192.168.0.102", "dc2", "rack1", new HashSet <string> {
                        "201"
                    }) }
            };
            const string strategy  = ReplicationStrategies.NetworkTopologyStrategy;
            var          keyspaces = new List <KeyspaceMetadata>
            {
                //network strategy with rf 2 per dc
                new KeyspaceMetadata(null, "ks1", true, strategy, new Dictionary <string, int> {
                    { "dc1", 2 }, { "dc2", 2 }
                }),
                //Testing simple (even it is not supposed to be)
                new KeyspaceMetadata(null, "ks2", true, ReplicationStrategies.SimpleStrategy, new Dictionary <string, int> {
                    { "replication_factor", 3 }
                }),
                //network strategy with rf 3 dc1 and 1 dc2
                new KeyspaceMetadata(null, "ks3", true, strategy, new Dictionary <string, int> {
                    { "dc1", 3 }, { "dc2", 1 }, { "dc3", 5 }
                }),
                //network strategy with rf 4 dc1
                new KeyspaceMetadata(null, "ks4", true, strategy, new Dictionary <string, int> {
                    { "dc1", 5 }
                })
            };
            var tokenMap = TokenMap.Build("Murmur3Partitioner", hosts, keyspaces);

            //KS1
            //the primary replica and the next
            var replicas = tokenMap.GetReplicas("ks1", new M3PToken(0));

            Assert.AreEqual("0,100,1,101", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            //The next replica should be the first
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(200));
            Assert.AreEqual("2,102,0,100", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            //The closest replica and the next
            replicas = tokenMap.GetReplicas("ks1", new M3PToken(190));
            Assert.AreEqual("2,102,0,100", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //KS2
            //Simple strategy: 3 tokens no matter which dc
            replicas = tokenMap.GetReplicas("ks2", new M3PToken(5000));
            Assert.AreEqual("0,100,1", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //KS3
            replicas = tokenMap.GetReplicas("ks3", new M3PToken(0));
            Assert.AreEqual("0,100,1,2", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
            replicas = tokenMap.GetReplicas("ks3", new M3PToken(201));
            Assert.AreEqual("102,0,1,2", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));

            //KS4
            replicas = tokenMap.GetReplicas("ks4", new M3PToken(0));
            Assert.AreEqual("0,1,2", String.Join(",", replicas.Select(TestHelper.GetLastAddressByte)));
        }
Ejemplo n.º 10
0
        public void Build_Should_OnlyCallOncePerReplicationConfiguration_When_MultipleKeyspacesWithSameReplicationOptions()
        {
            var hosts = new List <Host>
            {
                { TestHelper.CreateHost("192.168.0.0", "dc1", "rack", new HashSet <string> {
                        "0"
                    }) },
                { TestHelper.CreateHost("192.168.0.1", "dc1", "rack", new HashSet <string> {
                        "10"
                    }) },
                { TestHelper.CreateHost("192.168.0.2", "dc1", "rack", new HashSet <string> {
                        "20"
                    }) },
                { TestHelper.CreateHost("192.168.0.3", "dc2", "rack", new HashSet <string> {
                        "30"
                    }) },
                { TestHelper.CreateHost("192.168.0.4", "dc2", "rack", new HashSet <string> {
                        "40"
                    }) }
            };

            var factory   = new ProxyReplicationStrategyFactory();
            var keyspaces = new List <KeyspaceMetadata>
            {
                // unique configurations
                TokenTests.CreateSimpleKeyspace("ks1", 2, factory),
                TokenTests.CreateSimpleKeyspace("ks2", 10, factory),
                TokenTests.CreateSimpleKeyspace("ks3", 5, factory),
                TokenTests.CreateNetworkTopologyKeyspace("ks4", new Dictionary <string, int> {
                    { "dc1", 2 }, { "dc2", 2 }
                }, factory),
                TokenTests.CreateNetworkTopologyKeyspace("ks5", new Dictionary <string, int> {
                    { "dc1", 1 }, { "dc2", 2 }
                }, factory),
                TokenTests.CreateNetworkTopologyKeyspace("ks6", new Dictionary <string, int> {
                    { "dc1", 1 }
                }, factory),

                // duplicate configurations
                TokenTests.CreateNetworkTopologyKeyspace("ks7", new Dictionary <string, int> {
                    { "dc1", 2 }, { "dc2", 2 }
                }, factory),
                TokenTests.CreateNetworkTopologyKeyspace("ks8", new Dictionary <string, int> {
                    { "dc1", 1 }
                }, factory),
                TokenTests.CreateNetworkTopologyKeyspace("ks9", new Dictionary <string, int> {
                    { "dc1", 1 }, { "dc2", 2 }
                }, factory),
                TokenTests.CreateSimpleKeyspace("ks10", 10, factory),
                TokenTests.CreateSimpleKeyspace("ks11", 2, factory)
            };
            var tokenMap = TokenMap.Build("Murmur3Partitioner", hosts, keyspaces);

            var proxyStrategies = keyspaces.Select(k => (ProxyReplicationStrategy)k.Strategy).ToList();

            Assert.AreEqual(6, proxyStrategies.Count(strategy => strategy.Calls > 0));

            AssertOnlyOneStrategyIsCalled(proxyStrategies, 0, 10);
            AssertOnlyOneStrategyIsCalled(proxyStrategies, 1, 9);
            AssertOnlyOneStrategyIsCalled(proxyStrategies, 2);
            AssertOnlyOneStrategyIsCalled(proxyStrategies, 3, 6);
            AssertOnlyOneStrategyIsCalled(proxyStrategies, 4, 8);
            AssertOnlyOneStrategyIsCalled(proxyStrategies, 5, 7);
        }