/// <summary> /// Creates a new shard, or gets an existing empty shard (i.e. a shard that has no mappings). /// The reason why an empty shard might exist is that it was created and initialized but we /// failed to create a mapping to it. /// </summary> private static Shard CreateOrGetEmptyShard(RangeShardMap <int> shardMap) { // Get an empty shard if one already exists, otherwise create a new one Shard shard = FindEmptyShard(shardMap); if (shard == null) { // No empty shard exists, so create one // Choose the shard name string databaseName = string.Format(ShardNameFormat, shardMap.GetShards().Count()); // Only create the database if it doesn't already exist. It might already exist if // we tried to create it previously but hit a transient fault. if (!SqlDatabaseUtils.ExistsDatabase(MultiShardConfiguration.ShardMapManagerServerName, databaseName)) { SqlDatabaseUtils.CreateDatabase(MultiShardConfiguration.ShardMapManagerServerName, databaseName); } // Create schema and populate reference data on that database // The initialize script must be idempotent, in case it was already run on this database // and we failed to add it to the shard map previously SqlDatabaseUtils.ExecuteSqlScript( MultiShardConfiguration.ShardMapManagerServerName, databaseName, InitializeShardScriptFile); // Add it to the shard map ShardLocation shardLocation = new ShardLocation(MultiShardConfiguration.ShardMapManagerServerName, databaseName); shard = ShardManagementUtils.CreateOrGetShard(shardMap, shardLocation); } return(shard); }
/// <summary> /// Creates a shard map manager, creates a shard map, and creates a shard /// with a mapping for the full range of 32-bit integers. /// </summary> private static void CreateShardMapManagerAndShard() { if (MultiShardConfiguration.objShardMapManager != null) { ConsoleUtils.WriteWarning("Shard Map Manager already exists"); return; } // Create shard map manager database if (!SqlDatabaseUtils.ExistsDatabase(MultiShardConfiguration.ShardMapManagerServerName, MultiShardConfiguration.ShardMapManagerDatabaseName)) { SqlDatabaseUtils.CreateDatabase(MultiShardConfiguration.ShardMapManagerServerName, MultiShardConfiguration.ShardMapManagerDatabaseName); } // Create shard map manager string shardMapManagerConnectionString = MultiShardConfiguration.GetConnectionString(); MultiShardConfiguration.objShardMapManager = ShardManagementUtils.CreateOrGetShardMapManager(shardMapManagerConnectionString); // Create shard map RangeShardMap <int> shardMap = ShardManagementUtils.CreateOrGetRangeShardMap <int>( MultiShardConfiguration.objShardMapManager, MultiShardConfiguration.ShardMapName); // Create schema info so that the split-merge service can be used to move data in sharded tables // and reference tables. CreateSchemaInfo(shardMap.Name); // If there are no shards, add two shards: one for [0,100) and one for [100,+inf) if (!shardMap.GetShards().Any()) { CreateShardSample.CreateShard(shardMap, new Range <int>(0, 100)); CreateShardSample.CreateShard(shardMap, new Range <int>(100, 200)); } }
/// <summary> /// Tries to get the ShardMapManager that is stored in the specified database. /// </summary> public static ShardMapManager TryGetShardMapManager() { if (!SqlDatabaseUtils.ExistsDatabase(MultiShardConfiguration.ShardMapManagerServerName, MultiShardConfiguration.ShardMapManagerDatabaseName)) { // Shard Map Manager database has not yet been created return(null); } ShardMapManager shardMapManager; bool smmExists = ShardMapManagerFactory.TryGetSqlShardMapManager(MultiShardConfiguration.GetConnectionString(), ShardMapManagerLoadPolicy.Lazy, out shardMapManager); if (!smmExists) { // Shard Map Manager database exists, but Shard Map Manager has not been created return(null); } return(shardMapManager); }
/// <summary> /// Drops all shards and the shard map manager database (if it exists). /// </summary> private static void DropAll() { RangeShardMap <int> shardMap = MultiShardConfiguration.TryGetShardMap(); if (shardMap != null) { // Drop shards foreach (Shard shard in shardMap.GetShards()) { SqlDatabaseUtils.DropDatabase(shard.Location.DataSource, shard.Location.Database); } } if (SqlDatabaseUtils.ExistsDatabase(MultiShardConfiguration.ShardMapManagerServerName, MultiShardConfiguration.ShardMapManagerDatabaseName)) { // Drop shard map manager database SqlDatabaseUtils.DropDatabase(MultiShardConfiguration.ShardMapManagerServerName, MultiShardConfiguration.ShardMapManagerDatabaseName); } // Since we just dropped the shard map manager database, this shardMapManager reference is now non-functional. // So set it to null so that the program knows that the shard map manager is gone. MultiShardConfiguration.objShardMapManager = null; }