public void CorrectlyGeneratesRanges()
        {
            foreach (var numShards in Enumerable.Range(2, 10000))
            {
                var ranges = Int32RangeGenerator.GetRanges(numShards);
                Assert.IsNotNull(ranges);
                Assert.AreEqual(numShards, ranges.Count);

                var actualFirstRange = ranges.First();
                Assert.AreEqual(1, actualFirstRange.Key);
                Assert.AreEqual(int.MinValue, actualFirstRange.Value.LowValue);

                for (int shardNum = 2; shardNum < ranges.Count; shardNum++)
                {
                    var previousRange = ranges[shardNum - 1];
                    var actualRange   = ranges[shardNum];
                    Assert.AreEqual(previousRange.HighValue, actualRange.LowValue);
                }

                var actualLastRange = ranges.Last();
                Assert.AreEqual(ranges.Count, actualLastRange.Key);
                Assert.AreEqual(int.MaxValue, actualLastRange.Value.HighValue);

                var actualNextToLastRange = ranges[ranges.Count - 1];
                Assert.AreEqual(actualNextToLastRange.HighValue, actualLastRange.Value.LowValue);
            }
        }
        public void CorrectlyGeneratesRangeForOneShard()
        {
            var ranges = Int32RangeGenerator.GetRanges(1);

            Assert.IsNotNull(ranges);
            Assert.AreEqual(1, ranges.Count);

            var actualFullRange = ranges.Single();

            Assert.AreEqual(1, actualFullRange.Key);
            Assert.AreEqual(int.MinValue, actualFullRange.Value.LowValue);
            Assert.AreEqual(int.MaxValue, actualFullRange.Value.HighValue);
        }
 public void ThrowsExceptionForNumShardsLessThan1()
 {
     Int32RangeGenerator.GetRanges(0);
 }
Exemplo n.º 4
0
        public void AddInt32RangeMapShards(AddInt32RangeMapShardsArgs args)
        {
            args.Validate();

            var shardRanges = Int32RangeGenerator.GetRanges(args.ShardLocations.Locations.Length);

            // sanity checks
            if (shardRanges.Count != args.ShardLocations.Locations.Length)
            {
                throw new Exception($"The number of locations ({args.ShardLocations.Locations.Length}) does not match the number of generated shard ranges ({shardRanges.Count}).");
            }

            if (shardRanges[1].LowValue != int.MinValue)
            {
                throw new Exception($"Expected the first shard range low value to be int.MinValue, but it is {shardRanges[1].LowValue}.");
            }

            if (shardRanges[shardRanges.Count].HighValue != int.MaxValue)
            {
                throw new Exception($"Expected the last shard range high value to be int.MaxValue, but it is {shardRanges[shardRanges.Count].HighValue}.");
            }

            // now that we're sure everything is as expected, let's proceed...
            var shardManagementService = new ShardMapManagementService(args.ConnectionString);

            var keyType          = typeof(int).Name;
            var connectionString = new SqlConnectionStringBuilder(args.ConnectionString);

            for (int i = 0; i < args.ShardLocations.Locations.Length - 1; i++)
            {
                var shardNumber = (i + 1);
                var location    = args.ShardLocations.Locations[i];
                var range       = shardRanges[shardNumber];

                Log.Information(
                    LoggingMessageTemplates.AddRangeMapShard,
                    shardNumber,
                    location.DatabaseName,
                    location.ServerName,
                    keyType,
                    range.LowValue,
                    range.HighValue,
                    args.MapName,
                    connectionString.InitialCatalog,
                    connectionString.DataSource);

                shardManagementService.AddRangeMapShard(
                    args.MapName,
                    range.LowValue,
                    range.HighValue,
                    location.ServerName,
                    location.DatabaseName);
            }

            var lastLocation = args.ShardLocations.Locations.Last();
            var lastRange    = shardRanges.Last();

            Log.Information(
                LoggingMessageTemplates.AddRangeMapShard,
                lastRange.Key,
                lastLocation.DatabaseName,
                lastLocation.ServerName,
                keyType,
                lastRange.Value.LowValue,
                "+infinity",
                args.MapName,
                connectionString.InitialCatalog,
                connectionString.DataSource);

            shardManagementService.AddRangeMapShard(
                args.MapName,
                lastRange.Value.LowValue,
                lastLocation.ServerName,
                lastLocation.DatabaseName);
        }