/// <summary> /// Initializes a new instance of the <see cref="Sharding" /> class. /// <para>Bootstrap Elastic Scale by creating a new shard map manager and a shard map on the shard map manager database if necessary.</para> /// </summary> /// <param name="catalogConfig">The catalog configuration.</param> /// <param name="databaseConfig">The database configuration.</param> /// <param name="tenantsRepository">The tenants repository.</param> /// <param name="helper">The helper.</param> public Sharding(CatalogConfig catalogConfig, DatabaseConfig databaseConfig, ITenantsRepository tenantsRepository, IHelper helper) { try { _tenantsRepository = tenantsRepository; _helper = helper; var smmconnstr = _helper.GetSqlConnectionString(databaseConfig, catalogConfig); // Deploy shard map manager // if shard map manager exists, refresh content, else create it, then add content ShardMapManager smm; ShardMapManager = !ShardMapManagerFactory.TryGetSqlShardMapManager(smmconnstr, ShardMapManagerLoadPolicy.Lazy, out smm) ? ShardMapManagerFactory.CreateSqlShardMapManager(smmconnstr) : smm; // check if shard map exists and if not, create it ListShardMap <int> sm; ShardMap = !ShardMapManager.TryGetListShardMap(catalogConfig.CatalogDatabase, out sm) ? ShardMapManager.CreateListShardMap <int>(catalogConfig.CatalogDatabase) : sm; } catch (Exception ex) { // _logger.LogError(0, ex, "Error in sharding initialisation."); throw new ApplicationException("Error in sharding initialisation."); } }
// Bootstrap Elastic Scale by creating a new shard map manager and a shard map on // the shard map manager database if necessary. public Sharding(string smmserver, string smmdatabase, string smmconnstr) { // Connection string with administrative credentials for the root database SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder(smmconnstr); connStrBldr.DataSource = smmserver; connStrBldr.InitialCatalog = smmdatabase; // Deploy shard map manager. ShardMapManager smm; if (!ShardMapManagerFactory.TryGetSqlShardMapManager(connStrBldr.ConnectionString, ShardMapManagerLoadPolicy.Lazy, out smm)) { this.ShardMapManager = ShardMapManagerFactory.CreateSqlShardMapManager(connStrBldr.ConnectionString); } else { this.ShardMapManager = smm; } ListShardMap <int> sm; if (!ShardMapManager.TryGetListShardMap <int>("ElasticScaleWithEF", out sm)) { this.ShardMap = ShardMapManager.CreateListShardMap <int>("ElasticScaleWithEF"); } else { this.ShardMap = sm; } }
// Bootstrap Elastic Scale by creating a new shard map manager and a shard map on // the shard map manager database if necessary. public ShardingClient(string shardMapManagerServer, string shardMapManagerDb, string shardMapManagerConnString, string shardMapName = "ONETUG_DEMO") { // Connection string with administrative credentials for the root database var connStrBldr = new SqlConnectionStringBuilder(shardMapManagerConnString); connStrBldr.DataSource = shardMapManagerServer; connStrBldr.InitialCatalog = shardMapManagerDb; // Deploy shard map manager. if (!ShardMapManagerFactory.TryGetSqlShardMapManager(connStrBldr.ConnectionString, ShardMapManagerLoadPolicy.Lazy, out var existingShardMapManager)) { ShardMapManager = ShardMapManagerFactory.CreateSqlShardMapManager(connStrBldr.ConnectionString); } else { ShardMapManager = existingShardMapManager; } if (!ShardMapManager.TryGetListShardMap <int>(shardMapName, out var existingListShardMap)) { ShardMap = ShardMapManager.CreateListShardMap <int>(shardMapName); } else { ShardMap = existingListShardMap; } }
/// <summary> /// Initializes a new instance of the <see cref="Sharding" /> class. /// <para>Bootstrap Elastic Scale by creating a new shard map manager and a shard map on the shard map manager database if necessary.</para> /// </summary> /// <param name="catalogDatabase">The catalog database.</param> /// <param name="connectionString">The connection string.</param> /// <param name="catalogRepository">The catalog repository.</param> /// <param name="tenantRepository">The tenant repository.</param> /// <param name="utilities">The utilities class.</param> /// <exception cref="System.ApplicationException">Error in sharding initialisation.</exception> public Sharding(string catalogDatabase, string connectionString, ICatalogRepository catalogRepository, ITenantRepository tenantRepository, IUtilities utilities) { try { _catalogRepository = catalogRepository; _tenantRepository = tenantRepository; _utilities = utilities; // Deploy shard map manager // if shard map manager exists, refresh content, else create it, then add content ShardMapManager smm; ShardMapManager = !ShardMapManagerFactory.TryGetSqlShardMapManager(connectionString, ShardMapManagerLoadPolicy.Lazy, out smm) ? ShardMapManagerFactory.CreateSqlShardMapManager(connectionString) : smm; // check if shard map exists and if not, create it ListShardMap <int> sm; ShardMap = !ShardMapManager.TryGetListShardMap(catalogDatabase, out sm) ? ShardMapManager.CreateListShardMap <int>(catalogDatabase) : sm; } catch (Exception exception) { Trace.TraceError(exception.Message, "Error in sharding initialisation."); } }
/// <summary> /// Gets the shard map, if it exists. If it doesn't exist, writes out the reason and returns null. /// </summary> private static ListShardMap <int> TryGetShardMap() { if (s_shardMapManager == null) { ConsoleUtils.WriteWarning("Shard Map Manager has not yet been created"); // Create shard map manager //string shardMapManagerConnectionString = // Configuration.GetConnectionString( // Configuration.ShardMapManagerServerName, // Configuration.ShardMapManagerDatabaseName); //s_shardMapManager = ShardManagementUtils.CreateOrGetShardMapManager(shardMapManagerConnectionString); return(null); } ListShardMap <int> shardMap; // bool mapExists = s_shardMapManager.TryGetRangeShardMap(Configuration.ShardMapName, out shardMap); bool mapExists = s_shardMapManager.TryGetListShardMap(Configuration.ShardMapName, out shardMap); if (!mapExists) { ConsoleUtils.WriteWarning("Shard Map Manager has been created, but the Shard Map has not been created"); return(null); } return(shardMap); }
/// <summary> /// Creates a new List Shard Map with the specified name, or gets the List Shard Map if it already exists. /// </summary> public ListShardMap <T> CreateOrGetListShardMap <T>(ShardMapManager shardMapManager, string shardMapName) { ListShardMap <T> shardMap; var shardMapExists = shardMapManager.TryGetListShardMap(shardMapName, out shardMap); if (!shardMapExists) { shardMap = shardMapManager.CreateListShardMap <T>(shardMapName); } return(shardMap); }
private static ListShardMap <int> TryGetShardMap() { ListShardMap <int> shardMap; bool mapExists = s_shardMapManager.TryGetListShardMap("ElasticScaleWithEF", out shardMap); if (!mapExists) { // ConsoleUtils.WriteWarning("Shard Map Manager has been created, but the Shard Map has not been created"); return(null); } return(shardMap); }
/// <summary> /// Helper function to clean list and range shard maps. /// </summary> private static void CleanShardMapsHelper() { ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); // Remove all existing mappings from the list shard map. ListShardMap <DateTime> lsm; if (smm.TryGetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName, out lsm)) { Assert.IsNotNull(lsm); foreach (PointMapping <DateTime> pm in lsm.GetMappings()) { PointMapping <DateTime> pmOffline = lsm.MarkMappingOffline(pm); Assert.IsNotNull(pmOffline); lsm.DeleteMapping(pmOffline); } // Remove all shards from list shard map foreach (Shard s in lsm.GetShards()) { lsm.DeleteShard(s); } } // Remove all existing mappings from the range shard map. RangeShardMap <DateTime> rsm; if (smm.TryGetRangeShardMap <DateTime>(DateTimeShardMapperTests.s_rangeShardMapName, out rsm)) { Assert.IsNotNull(rsm); foreach (RangeMapping <DateTime> rm in rsm.GetMappings()) { MappingLockToken mappingLockToken = rsm.GetMappingLockOwner(rm); rsm.UnlockMapping(rm, mappingLockToken); RangeMapping <DateTime> rmOffline = rsm.MarkMappingOffline(rm); Assert.IsNotNull(rmOffline); rsm.DeleteMapping(rmOffline); } // Remove all shards from range shard map foreach (Shard s in rsm.GetShards()) { rsm.DeleteShard(s); } } }
private static ListShardMap <T> GetOrCreateListShardMap <T>(ShardMapManager shardMapManager, string shardMapName) { ListShardMap <T> map; if (shardMapManager.TryGetListShardMap(shardMapName, out map)) { // Shard map already exists Console.WriteLine("Shard map {0} already exists", shardMapName); return(map); } // Initialize shard map Console.WriteLine("Creating shard map {0}", shardMapName); return(shardMapManager.CreateListShardMap <T>(shardMapName)); }
private ListShardMap <Guid> GetTenantMap(Guid tenantId) { ListShardMap <Guid> tenantMap; PointMapping <Guid> mapping; if (!smm.TryGetListShardMap(conf.TenantsShardMapName, out tenantMap) || !tenantMap.TryGetMappingForKey(tenantId, out mapping)) { return(null); } if (mapping.Status == MappingStatus.Offline) { return(null); } return(tenantMap); }
// Bootstrap Elastic Scale by creating a new shard map manager and a shard map on // the shard map manager database if necessary. public Sharding()//string smmserver, string smmdatabase, string smmconnstr) { string smmserver = Sharding.server; string smmconnstr = Sharding.connectionString; string smmdatabase = Sharding.shardmapmgrdb; // Connection string with administrative credentials for the root database SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder(smmconnstr); connStrBldr.DataSource = smmserver; connStrBldr.InitialCatalog = smmdatabase; connstring = smmconnstr; // Deploy shard map manager. ShardMapManager smm; if (!ShardMapManagerFactory.TryGetSqlShardMapManager(connStrBldr.ConnectionString, ShardMapManagerLoadPolicy.Lazy, out smm)) { this.ShardMapManager = ShardMapManagerFactory.CreateSqlShardMapManager(connStrBldr.ConnectionString); } else { this.ShardMapManager = smm; } ListShardMap <Guid> sm; if (!ShardMapManager.TryGetListShardMap <Guid>(sharmapName, out sm)) { this.ShardMap = ShardMapManager.CreateListShardMap <Guid>(sharmapName); } else { this.ShardMap = sm; } initDd("shard0"); initDd("shard1"); //initDd("shard2"); //initDd("shard3"); //initDd("shard4"); }
public async Task CreateShard(Tenant tenant) { ListShardMap <Guid> tenantMap; if (!smm.TryGetListShardMap(conf.TenantsShardMapName, out tenantMap)) { // No está registrado el shard map return; } var tenantId = tenant.Id; var dbName = GetTenantDbName(tenant.Level, tenantId); var dbEdition = GetTenantDbEdition(tenant.Level); PointMapping <Guid> mapping; if (!tenantMap.TryGetMappingForKey(tenantId, out mapping)) { await shards.CreateShard(tenantMap, tenantId, dbName, () => typeof(TenantPersister).Assembly.GetManifestResourceStream(conf.InitializeShardDbScriptPath), x => { }, dbEdition); } }
/// <summary> /// Creates a new Range Shard Map with the specified name, or gets the Range Shard Map if it already exists. /// </summary> public static ListShardMap <T> CreateOrGetRangeShardMap <T>(ShardMapManager shardMapManager, string shardMapName) { // Try to get a reference to the Shard Map. //RangeShardMap<T> shardMap; ListShardMap <T> shardMap; bool shardMapExists = shardMapManager.TryGetListShardMap(shardMapName, out shardMap); if (shardMapExists) { ConsoleUtils.WriteInfo("Shard Map {0} already exists", shardMap.Name); } else { shardMap = shardMapManager.CreateListShardMap <T>(shardMapName); // The Shard Map does not exist, so create it // shardMap = shardMapManager.CreateRangeShardMap<T>(shardMapName); ConsoleUtils.WriteInfo("Created Shard Map {0}", shardMap.Name); } return(shardMap); }
public Sharding(string shardManagerDatabase, string connectionString) { ShardMapManager shardMapManager; if (!ShardMapManagerFactory.TryGetSqlShardMapManager(connectionString, ShardMapManagerLoadPolicy.Lazy, out shardMapManager)) { this.ShardMapManager = ShardMapManagerFactory.CreateSqlShardMapManager(connectionString); } else { this.ShardMapManager = shardMapManager; } ListShardMap <byte[]> shardMap; if (!ShardMapManager.TryGetListShardMap(shardManagerDatabase, out shardMap)) { this.ShardMap = ShardMapManager.CreateListShardMap <byte[]>(shardManagerDatabase); } else { this.ShardMap = shardMap; } }
public ListShardMap <int> TryGetShardMap() { if (s_shardMapManager == null) { string shardMapManagerConnectionString = Configuration.GetConnectionString( Configuration.ShardMapManagerServerName, Configuration.ShardMapManagerDatabaseName); s_shardMapManager = this.CreateOrGetShardMapManager(shardMapManagerConnectionString); } ListShardMap <int> shardMap; // bool mapExists = s_shardMapManager.TryGetRangeShardMap(Configuration.ShardMapName, out shardMap); bool mapExists = s_shardMapManager.TryGetListShardMap(Configuration.ShardMapName, out shardMap); if (!mapExists) { // ConsoleUtils.WriteWarning("Shard Map Manager has been created, but the Shard Map has not been created"); return(null); } return(shardMap); }
private ListShardMap <int> GetShardMap() { return(!ShardMapManager.TryGetListShardMap(CatalogConfig.CatalogDatabase, out ListShardMap <int> shardMap) ? ShardMapManager.CreateListShardMap <int>(CatalogConfig.CatalogDatabase) : shardMap); }
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); } }