public Shard CreateOrGet(ShardMap shardMap, string server, string database) { var location = new ShardLocation(server, database); var shardExists = shardMap.TryGetShard(location, out var shard); return(shardExists ? shard : shardMap.CreateShard(location)); }
/// <summary> /// Adds Shards to the Shard Map, or returns them if they have already been added. /// </summary> public Shard CreateOrGetShard(ShardMap shardMap, ShardLocation shardLocation) { Shard shard; var shardExists = shardMap.TryGetShard(shardLocation, out shard); if (!shardExists) { shard = shardMap.CreateShard(shardLocation); } return(shard); }
/// <summary> /// Adds Shards to the Shard Map, or returns them if they have already been added. /// </summary> public static Shard CreateOrGetShard(ShardMap shardMap, ShardLocation shardLocation) { // Try to get a reference to the Shard Shard shard; bool shardExists = shardMap.TryGetShard(shardLocation, out shard); if (!shardExists) { shard = shardMap.CreateShard(shardLocation); } return(shard); }
private static Shard CreateOrGetShard(ShardMap shardMap, ShardLocation shardLocation) { // Try to get a reference to the Shard Shard shard; bool shardExists = shardMap.TryGetShard(shardLocation, out shard); if (!shardExists) { // The Shard Map does not exist, so create it shard = shardMap.CreateShard(shardLocation); //force EF to push the schema for the new shard var voteContext = new VoteContext(shard.Location.DataSource, shard.Location.Database); voteContext.Database.Initialize(true); } return(shard); }
/// <summary> /// Adds Shards to the Shard Map, or returns them if they have already been added. /// </summary> public static Shard CreateOrGetShard(ShardMap shardMap, ShardLocation shardLocation) { // Try to get a reference to the Shard Shard shard; bool shardExists = shardMap.TryGetShard(shardLocation, out shard); if (shardExists) { ConsoleUtils.WriteInfo("Shard {0} has already been added to the Shard Map", shardLocation.Database); } else { // The Shard Map does not exist, so create it shard = shardMap.CreateShard(shardLocation); ConsoleUtils.WriteInfo("Added shard {0} to the Shard Map", shardLocation.Database); } return(shard); }
public void BasicScenarioDefaultShardMaps() { bool success = true; try { #region DeployShardMapManager // Deploy shard map manager. ShardMapManagerFactory.CreateSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerCreateMode.ReplaceExisting); #endregion DeployShardMapManager #region GetShardMapManager // Obtain shard map manager. ShardMapManager shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); #endregion GetShardMapManager #region CreateDefaultShardMap // Create a single user per-tenant shard map. ShardMap defaultShardMap = shardMapManager.CreateListShardMap <int>("DefaultShardMap"); #endregion CreateDefaultShardMap #region CreateShard for (int i = 0; i < ScenarioTests.s_perTenantDBs.Length; i++) { // Create the shard. defaultShardMap.CreateShard( new ShardLocation( Globals.ShardMapManagerTestsDatasourceName, ScenarioTests.s_perTenantDBs[i])); } #endregion CreateShard #region UpdateShard // Find the shard by location. Shard shardToUpdate = defaultShardMap.GetShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, "PerTenantDB1")); // Perform the actual update. Mark offline. Shard updatedShard = defaultShardMap.UpdateShard( shardToUpdate, new ShardUpdate { Status = ShardStatus.Offline }); // Verify that update succeeded. Assert.AreEqual(ShardStatus.Offline, updatedShard.Status); #endregion UpdateShard #region DeleteShard // Find the shard by location. Shard shardToDelete = defaultShardMap.GetShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, "PerTenantDB4")); defaultShardMap.DeleteShard(shardToDelete); // Verify that delete succeeded. Shard deletedShard; defaultShardMap.TryGetShard(shardToDelete.Location, out deletedShard); Assert.IsNull(deletedShard); // Now add the shard back for further tests. // Create the shard. defaultShardMap.CreateShard(shardToDelete.Location); #endregion DeleteShard #region OpenConnection without Validation // Find the shard by location. Shard shardForConnection = defaultShardMap.GetShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, "PerTenantDB1")); using (SqlConnection conn = shardForConnection.OpenConnection( Globals.ShardUserConnectionString, ConnectionOptions.None)) // validate = false { } #endregion OpenConnection without Validation #region OpenConnection with Validation // Use the stale state of "shardToUpdate" shard & see if validation works. bool validationFailed = false; try { using (SqlConnection conn = shardToDelete.OpenConnection( Globals.ShardUserConnectionString, ConnectionOptions.Validate)) // validate = true { } } catch (ShardManagementException smme) { validationFailed = true; Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.ShardDoesNotExist); } Assert.AreEqual(validationFailed, true); #endregion OpenConnection with Validation #region OpenConnectionAsync without Validation // Find the shard by location. shardForConnection = defaultShardMap.GetShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, "PerTenantDB1")); using (SqlConnection conn = shardForConnection.OpenConnectionAsync( Globals.ShardUserConnectionString, ConnectionOptions.None).Result) // validate = false { } #endregion OpenConnectionAsync without Validation #region OpenConnectionAsync with Validation // Use the stale state of "shardToUpdate" shard & see if validation works. validationFailed = false; try { using (SqlConnection conn = shardToDelete.OpenConnectionAsync( Globals.ShardUserConnectionString, ConnectionOptions.Validate).Result) // validate = true { } } catch (AggregateException ex) { ShardManagementException smme = ex.InnerException as ShardManagementException; if (smme != null) { validationFailed = true; Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.ShardDoesNotExist); } } Assert.AreEqual(validationFailed, true); #endregion OpenConnectionAsync with Validation #if FUTUREWORK #region GetAllOnlineShards // Get all online shards. foreach (Shard s in defaultShardMap.GetShards(Int32.MaxValue, 1)) { Trace.WriteLine(s.Location); } #endregion GetAllOnlineShards #endif } catch (ShardManagementException smme) { success = false; Trace.WriteLine(String.Format("Error Category: {0}", smme.ErrorCategory)); Trace.WriteLine(String.Format("Error Code : {0}", smme.ErrorCode)); Trace.WriteLine(String.Format("Error Message : {0}", smme.Message)); if (smme.InnerException != null) { Trace.WriteLine(String.Format("Storage Error Message : {0}", smme.InnerException.Message)); if (smme.InnerException.InnerException != null) { Trace.WriteLine(String.Format("SqlClient Error Message : {0}", smme.InnerException.InnerException.Message)); } } } Assert.IsTrue(success); }