/// <summary> This app will be for automating on-boarding of new tenant </summary> static void Main(string[] args) { Console.WriteLine($"Starting Shard Registration For Tenant:{args[0]}"); var tenantKey = args[0]; var sqlShardDBServer = args[1]; var sqlShardDB = args[2]; var shardedDBConnection = args[3]; ShardMapFactory shardMapFactory = new ShardMapFactory(shardedDBConnection); ListShardMap <Guid> shardMap = (ListShardMap <Guid>)shardMapFactory.CreateShardMap(); string appserver = sqlShardDBServer; string appdatabase = sqlShardDB; Guid TenantId = Guid.Parse(tenantKey); var shardLocation = new ShardLocation(appserver, appdatabase); if (!shardMap.TryGetShard(shardLocation, out var shard)) { shard = shardMap.CreateShard(new ShardLocation(appserver, appdatabase)); } PointMapping <Guid> mapping; if (!shardMap.TryGetMappingForKey(TenantId, out mapping)) { shardMap.CreatePointMapping(TenantId, shard); } }
public bool TryAddTenant(T tenantId, string shardName) { var database = $"{_shardingConfig.ShardPrefix}.{shardName}"; var location = new ShardLocation(_shardingConfig.ShardServer, database); if (!_shardMap.TryGetMappingForKey(tenantId, out _) && _shardMap.TryGetShard(location, out _)) { lock (_tenantLock) { if (!_shardMap.TryGetMappingForKey(tenantId, out _) && _shardMap.TryGetShard(location, out Shard shard)) { _shardMap.CreatePointMapping(new PointMappingCreationInfo <T>(tenantId, shard, MappingStatus.Online)); return(true); } } } return(false); }
private Shard CreateOrGetShard(ListShardMap <int> shardMap, string databaseShardName) { var shardLocation = new ShardLocation(this.configuration["ShardingServerName"], databaseShardName); var shardExists = shardMap.TryGetShard(shardLocation, out Shard shard); if (!shardExists) { shard = shardMap.CreateShard(shardLocation); } return(shard); }
private static Shard GetOrCreateOrderShard(ListShardMap <int> shardMap, string headConnectionString, int customerId) { string databaseName = $"CustomerOrders{customerId}"; var shardLocation = new ShardLocation(ShardServer, databaseName); // Does this shard already exist? Shard shard; if (shardMap.TryGetShard(shardLocation, out shard)) { Console.WriteLine($"Found order shard at {databaseName}"); return(shard); } // Create the database try { using (var headConn = new SqlConnection(headConnectionString)) { headConn.Open(); Console.WriteLine($"Creating order shard database {databaseName}"); headConn.Execute(sql: $@"create database {databaseName} ( SERVICE_OBJECTIVE = ELASTIC_POOL(name = {ShardDatabasePoolName}) )", commandTimeout: 0); } } catch (SqlException ex) { // Does the database already exist? if (ex.Message.Contains("already exists.")) { Console.WriteLine("Database already exists"); } else { throw; } } // Add to the shard map Console.WriteLine($"Adding {databaseName} to Shard Map"); return(shardMap.CreateShard(shardLocation)); }
private bool RefreshShardListMap(List <Tuple <long, string> > ListMappings = null) { try { string shardServer1 = WingtipTicketApp.Config.PrimaryDatabaseServer; string shardServer2 = WingtipTicketApp.Config.ShardDatabaseServer; string ticketsDbName = WingtipTicketApp.Config.TicketsDbName; Shard shard1 = null, shard2 = null; PointMapping <long> lmpg; // check if shard map manager exists and if not, create it (Idempotent / tolerant of re-execute) if (!ShardMapManager.TryGetListShardMap(mapManagerName, out lsm)) { lsm = ShardMapManager.CreateListShardMap <long>(mapManagerName); } try { // check if shard exists and if not, create it (Idempotent / tolerant of re-execute) if (!lsm.TryGetShard(new ShardLocation(shardServer1, ticketsDbName), out shard1)) { shard1 = lsm.CreateShard(new ShardLocation(shardServer1, ticketsDbName)); } } catch // sometimes, it may throw an error stating that a concurrent user recently changed some settings. //This is a retry logic to cover this scenario. { Thread.Sleep(500); // check if shard map manager exists and if not, create it (Idempotent / tolerant of re-execute) if (!ShardMapManager.TryGetListShardMap(mapManagerName, out lsm)) { lsm = ShardMapManager.CreateListShardMap <long>(mapManagerName); } // check if shard exists and if not, create it (Idempotent / tolerant of re-execute) if (!lsm.TryGetShard(new ShardLocation(shardServer1, ticketsDbName), out shard1)) { shard1 = lsm.CreateShard(new ShardLocation(shardServer1, ticketsDbName)); } } if (!lsm.TryGetShard(new ShardLocation(shardServer2, ticketsDbName), out shard2)) { shard2 = lsm.CreateShard(new ShardLocation(shardServer2, ticketsDbName)); } // Check if mapping exists and if not, create it (Idempotent / tolerant of re-execute) if (ListMappings != null) { foreach (Tuple <long, string> mapping in ListMappings) { if (!lsm.TryGetMappingForKey(mapping.Item1, out lmpg)) { if (mapping.Item2 == shardServer1) { lsm.CreatePointMapping(new PointMappingCreationInfo <long>(mapping.Item1, shard1, MappingStatus.Online)); } else if (mapping.Item2 == shardServer2) { lsm.CreatePointMapping(new PointMappingCreationInfo <long>(mapping.Item1, shard2, MappingStatus.Online)); } } } } return(true); } catch { return(false); } }