public void FailsValidationWhenAnyShardLocationIsMissingServerName()
        {
            var sut = new AddInt32RangeMapShardsArgs
            {
                ConnectionString = @"Server = (localdb)\mssqllocaldb; Initial Catalog = SomeTestDb; Integrated Security = true; Application Name = Galen.CI.Azure.Sql.Sharding.App.Tests;",
                MapName          = "MyTestIntRangeMapName",
                ShardLocations   = new ShardLocations()
            };

            var shardLocations = new[]
            {
                new ShardLocation
                {
                    ServerName   = @"(localdb)\.\SharedLocalDb",
                    DatabaseName = "MyTestShardDb001"
                },
                new ShardLocation
                {
                    DatabaseName = "MyTestShardDb002"
                }
            };

            sut.ShardLocations.Locations = shardLocations;

            sut.Validate();
        }
        public void CorrectlyAcceptsValidArguments()
        {
            var sut = new AddInt32RangeMapShardsArgs
            {
                ConnectionString = @"Server = (localdb)\mssqllocaldb; Initial Catalog = SomeTestDb; Integrated Security = true; Application Name = Galen.CI.Azure.Sql.Sharding.App.Tests;",
                MapName          = "MyTestIntRangeMapName",
                ShardLocations   = new ShardLocations
                {
                    Locations = new []
                    {
                        new ShardLocation
                        {
                            ServerName   = @"(localdb)\.\SharedLocalDb",
                            DatabaseName = "MyTestShardDb001"
                        }
                    }
                }
            };

            sut.Validate();     // should not throw exception
        }
Ejemplo n.º 3
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);
        }