/// <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.DatabaseExists(Configuration.ShardMapManagerServerName, databaseName)) { SqlDatabaseUtils.CreateDatabase(Configuration.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( Configuration.ShardMapManagerServerName, databaseName, InitializeShardScriptFile); // Add it to the shard map ShardLocation shardLocation = new ShardLocation(Configuration.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> /// ExecuteMultiShardQuery method to get the database records using MultiShardConnection, /// MultiShardCommand,multiShardDataReader class /// </summary> /// <param name="shardMap"></param> /// <param name="credentialsConnectionString"></param> public static void ExecuteMultiShardQuery(RangeShardMap <int> shardMap) { // Get the Shards from Shard Map manager MultiShardConfiguration.Shards = shardMap.GetShards(); TestParameterValue testParameterValue = new TestParameterValue( "MultiShard", "ExecMultiShard", "SelectAll_DR", "SqlDbWithMultiShard" + "%" + "individual" + "%" + "-", new MyUserInfo("MultiShard", "MultiShard")); // 分離レベルの設定 DbEnum.IsolationLevelEnum iso = DbEnum.IsolationLevelEnum.NoTransaction; // B層を生成 LayerB myBusiness = new LayerB(); // 業務処理を実行 TestReturnValue testReturnValue = (TestReturnValue)myBusiness.DoBusinessLogic( (BaseParameterValue)testParameterValue, iso); string strErrorMsg = ""; if (testReturnValue.ErrorFlag == true) { // 結果(業務続行可能なエラー) strErrorMsg = "ErrorMessageID:" + testReturnValue.ErrorMessageID + "\r\n"; strErrorMsg += "ErrorMessage:" + testReturnValue.ErrorMessage + "\r\n"; strErrorMsg += "ErrorInfo:" + testReturnValue.ErrorInfo + "\r\n"; Console.WriteLine("Inserted failed for Error message : {0}", strErrorMsg); } else { //Converts Return value object to dataTable data to display the data in screen DataTable dtTable = (DataTable)testReturnValue.Obj; int rows = 0; // Get the column names TableFormatter formatter = new TableFormatter(ShardManagementUtils.GetColumnNames(dtTable).ToArray()); foreach (DataRow dr in dtTable.Rows) { // Extract just database name from the $ShardLocation pseudocolumn to make the output formater cleaner. // Note that the $ShardLocation pseudocolumn is always the last column int shardLocationOrdinal = dr.ItemArray.Length - 1; dr.ItemArray[shardLocationOrdinal] = ShardManagementUtils.ExtractDatabaseName(dr.ItemArray[shardLocationOrdinal].ToString()); // Add values to output formatter formatter.AddRow(dr.ItemArray); rows++; } Console.WriteLine(formatter.ToString()); Console.WriteLine("({0} rows returned)", rows); } }
/// <summary> /// Main program loop. /// </summary> private static void MenuLoop() { // Get the shard map manager, if it already exists. // It is recommended that you keep only one shard map manager instance in // memory per AppDomain so that the mapping cache is not duplicated. MultiShardConfiguration.objShardMapManager = ShardManagementUtils.TryGetShardMapManager(); // Loop until the user chose "Exit". bool continueLoop; do { PrintShardMapState(); Console.WriteLine(); PrintMenu(); Console.WriteLine(); continueLoop = GetMenuChoiceAndExecute(); Console.WriteLine(); }while (continueLoop); }