/// <summary>
        /// Helper function to add a new shard to given shard map.
        /// </summary>
        /// <param name="sm"></param>
        private void AddShardToShardMap(ShardMap sm)
        {
            IEnumerable <Shard> existingShards = sm.GetShards();

            // get list of shard locations that are not already added to this shard map.
            List <string> availableLocationList = (from dbName in ShardMapManagerLoadTests.s_shardedDBs
                                                   where !existingShards.Select(s => s.Location.Database).ToList().Contains(dbName)
                                                   select dbName).ToList();

            if (availableLocationList.Count > 0)
            {
                ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, availableLocationList[_r.Next(availableLocationList.Count)]);

                Debug.WriteLine("Trying to add shard at location {0} to shard map {1}", sl, sm);

                ShardCreationInfo si = new ShardCreationInfo(sl, ShardStatus.Online);

                Shard newShard = sm.CreateShard(si);
            }
        }
        /// <summary>
        /// Helper function to add a new shard to given shard map.
        /// </summary>
        /// <param name="sm"></param>
        private void AddShardToShardMap(ShardMap sm)
        {
            IEnumerable<Shard> existingShards = sm.GetShards();

            // get list of shard locations that are not already added to this shard map.
            List<string> availableLocationList = (from dbName in ShardMapManagerLoadTests.s_shardedDBs
                                                  where !existingShards.Select(s => s.Location.Database).ToList().Contains(dbName)
                                                  select dbName).ToList();

            if (availableLocationList.Count > 0)
            {
                ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, availableLocationList[_r.Next(availableLocationList.Count)]);

                Debug.WriteLine("Trying to add shard at location {0} to shard map {1}", sl, sm);

                ShardCreationInfo si = new ShardCreationInfo(sl, ShardStatus.Online);

                Shard newShard = sm.CreateShard(si);
            }
        }
        public static void ShardMapManagerLoadTestsInitialize(TestContext testContext)
        {
            // Clear all connection pools.
            SqlConnection.ClearAllPools();

            using (SqlConnection conn = new SqlConnection(Globals.ShardMapManagerTestConnectionString))
            {
                conn.Open();

                // Create ShardMapManager database
                using (SqlCommand cmd = new SqlCommand(
                           string.Format(Globals.CreateDatabaseQuery, Globals.ShardMapManagerDatabaseName),
                           conn))
                {
                    cmd.ExecuteNonQuery();
                }

                // Create shard databases
                for (int i = 0; i < ShardMapManagerLoadTests.s_shardedDBs.Length; i++)
                {
                    using (SqlCommand cmd = new SqlCommand(
                               string.Format(Globals.CreateDatabaseQuery, ShardMapManagerLoadTests.s_shardedDBs[i]),
                               conn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }

                // cleanup for deadlock monitoring
                foreach (string q in s_deadlockDetectionCleanupQueries)
                {
                    using (SqlCommand cmd = new SqlCommand(q, conn))
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException)
                        {
                        }
                    }
                }

                // setup for deadlock monitoring
                foreach (string q in s_deadlockDetectionSetupQueries)
                {
                    using (SqlCommand cmd = new SqlCommand(q, conn))
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException)
                        {
                        }
                    }
                }
            }

            // Create shard map manager.
            ShardMapManagerFactory.CreateSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerCreateMode.ReplaceExisting);

            // Create list shard map.
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ListShardMap <int> lsm = smm.CreateListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName);

            Assert.IsNotNull(lsm);

            // Create range shard map.
            RangeShardMap <int> rsm = smm.CreateRangeShardMap <int>(ShardMapManagerLoadTests.s_rangeShardMapName);

            Assert.IsNotNull(rsm);

            // Add 'InitialShardCount' shards to list and range shard map.

            for (int i = 0; i < ShardMapManagerLoadTests.InitialShardCount; i++)
            {
                ShardCreationInfo si = new ShardCreationInfo(
                    new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapManagerLoadTests.s_shardedDBs[i]),
                    ShardStatus.Online);

                Shard sList = lsm.CreateShard(si);
                Assert.IsNotNull(sList);

                Shard sRange = rsm.CreateShard(si);
                Assert.IsNotNull(sRange);
            }

            // Initialize retry policy
            s_retryPolicy = new RetryPolicy <SqlAzureTransientErrorDetectionStrategy>(
                new ExponentialBackoff(5, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(100)));
        }
        public static void ShardMapManagerLoadTestsInitialize(TestContext testContext)
        {
            // Clear all connection pools.
            SqlConnection.ClearAllPools();

            using (SqlConnection conn = new SqlConnection(Globals.ShardMapManagerTestConnectionString))
            {
                conn.Open();

                // Create ShardMapManager database
                using (SqlCommand cmd = new SqlCommand(
                    string.Format(Globals.CreateDatabaseQuery, Globals.ShardMapManagerDatabaseName),
                    conn))
                {
                    cmd.ExecuteNonQuery();
                }

                // Create shard databases
                for (int i = 0; i < ShardMapManagerLoadTests.s_shardedDBs.Length; i++)
                {
                    using (SqlCommand cmd = new SqlCommand(
                        string.Format(Globals.CreateDatabaseQuery, ShardMapManagerLoadTests.s_shardedDBs[i]),
                        conn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }

                // cleanup for deadlock monitoring
                foreach (string q in s_deadlockDetectionCleanupQueries)
                {
                    using (SqlCommand cmd = new SqlCommand(q, conn))
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException)
                        {
                        }
                    }
                }

                // setup for deadlock monitoring
                foreach (string q in s_deadlockDetectionSetupQueries)
                {
                    using (SqlCommand cmd = new SqlCommand(q, conn))
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException)
                        {
                        }
                    }
                }
            }

            // Create shard map manager.
            ShardMapManagerFactory.CreateSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerCreateMode.ReplaceExisting);

            // Create list shard map.
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                        Globals.ShardMapManagerConnectionString,
                        ShardMapManagerLoadPolicy.Lazy);

            ListShardMap<int> lsm = smm.CreateListShardMap<int>(ShardMapManagerLoadTests.s_listShardMapName);
            Assert.IsNotNull(lsm);

            // Create range shard map.
            RangeShardMap<int> rsm = smm.CreateRangeShardMap<int>(ShardMapManagerLoadTests.s_rangeShardMapName);
            Assert.IsNotNull(rsm);

            // Add 'InitialShardCount' shards to list and range shard map.

            for (int i = 0; i < ShardMapManagerLoadTests.InitialShardCount; i++)
            {
                ShardCreationInfo si = new ShardCreationInfo(
                    new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapManagerLoadTests.s_shardedDBs[i]),
                    ShardStatus.Online);

                Shard sList = lsm.CreateShard(si);
                Assert.IsNotNull(sList);

                Shard sRange = rsm.CreateShard(si);
                Assert.IsNotNull(sRange);
            }

            // Initialize retry policy
            s_retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(
                new ExponentialBackoff(5, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(100)));
        }