public void LoadTestKillLSMConnections()
        {
            String databaseName = null;

            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);
                do
                {
                    List <ShardLocation> sl = smm.GetDistinctShardLocations().ToList();
                    if (sl.Count == 0)
                    {
                        continue;
                    }

                    // Select a random database(shard) to kill connections
                    databaseName = sl[_r.Next(sl.Count)].Database;
                }while (false);
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }

            if (databaseName != null)
            {
                using (SqlConnection conn = new SqlConnection(Globals.ShardMapManagerTestConnectionString))
                {
                    conn.Open();

                    // kill all connections for given shard location
                    try
                    {
                        using (SqlCommand cmd = new SqlCommand(
                                   string.Format(KillConnectionsForDatabaseQuery, databaseName),
                                   conn))
                        {
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (SqlException e)
                    {
                        //  233: A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)
                        // 6106: Process ID %d is not an active process ID.
                        // 6107: Only user processes can be killed
                        if ((e.Number != 233) && (e.Number != 6106) && (e.Number != 6107))
                        {
                            Assert.Fail("error number {0} with message {1}", e.Number, e.Message);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void GetDistinctLocations()
        {
            // Get shard map manager and 2 shard maps.
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            // Upgrade GSM to latest version
            smm.UpgradeGlobalStore();

            // create shard maps
            foreach (string name in ShardMapManagerUpgradeTests.s_shardMapNames)
            {
                ShardMap sm = smm.CreateListShardMap <int>(name);
                Assert.IsNotNull(sm);
            }

            ShardMap sm1 = smm.GetShardMap(ShardMapManagerUpgradeTests.s_shardMapNames[0]);

            Assert.IsNotNull(sm1);

            ShardMap sm2 = smm.GetShardMap(ShardMapManagerUpgradeTests.s_shardMapNames[1]);

            Assert.IsNotNull(sm2);

            // Add shards to the shard maps.

            ShardLocation sl1 = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapManagerUpgradeTests.s_shardedDBs[0]);
            ShardLocation sl2 = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapManagerUpgradeTests.s_shardedDBs[1]);
            ShardLocation sl3 = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapManagerUpgradeTests.s_shardedDBs[2]);

            Shard s1 = sm1.CreateShard(sl1);
            Shard s2 = sm1.CreateShard(sl2);
            Shard s3 = sm1.CreateShard(sl3);
            Shard s4 = sm2.CreateShard(sl2);
            Shard s5 = sm2.CreateShard(sl3);

            int count = 0;

            foreach (ShardLocation sl in smm.GetDistinctShardLocations())
            {
                count++;
            }

            Assert.AreEqual(3, count);
        }
        public static void ShardMapManagerLoadTestsCleanup()
        {
            // Clear all connection pools.
            SqlConnection.ClearAllPools();

            // Detect inconsistencies for all shard locations in a shard map.
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            RecoveryManager rm = new RecoveryManager(smm);

            bool inconsistencyDetected = false;

            foreach (ShardLocation sl in smm.GetDistinctShardLocations())
            {
                IEnumerable <RecoveryToken> gs = rm.DetectMappingDifferences(sl);

                foreach (RecoveryToken g in gs)
                {
                    var kvps = rm.GetMappingDifferences(g);
                    if (kvps.Keys.Count > 0)
                    {
                        inconsistencyDetected = true;
                        Debug.WriteLine("LSM at location {0} is not consistent with GSM", sl);
                    }
                }
            }

            bool deadlocksDetected = false;

            // Check for deadlocks during the run and cleanup database and deadlock objects on successful run
            using (SqlConnection conn = new SqlConnection(Globals.ShardMapManagerTestConnectionString))
            {
                conn.Open();

                // check for any deadlocks occured during the run and cleanup deadlock monitoring objects
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = s_deadlockDetectionQuery;
                    cmd.CommandType = System.Data.CommandType.Text;

                    try
                    {
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                // some deadlocks occured during the test, collect xml plan for these deadlocks
                                deadlocksDetected = true;

                                while (reader.Read())
                                {
                                    Debug.WriteLine("Deadlock information");
                                    Debug.WriteLine(reader.GetSqlXml(0).Value);
                                }
                            }
                        }
                    }
                    catch (SqlException)
                    {
                    }
                }

                // cleanup only if there are no inconsistencies and deadlocks during the run.
                if (!deadlocksDetected && !inconsistencyDetected)
                {
                    foreach (string q in s_deadlockDetectionCleanupQueries)
                    {
                        using (SqlCommand cmd = new SqlCommand(q, conn))
                        {
                            try
                            {
                                cmd.ExecuteNonQuery();
                            }
                            catch (SqlException)
                            {
                            }
                        }
                    }

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

                    // Drop shard map manager database
                    using (SqlCommand cmd = new SqlCommand(
                               string.Format(Globals.DropDatabaseQuery, Globals.ShardMapManagerDatabaseName),
                               conn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }