Ejemplo n.º 1
0
        public void CreateShardDuplicate()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(sl);

            Assert.IsNotNull(sNew);

            bool addFailed = false;

            try
            {
                Shard sDuplicate = sm.CreateShard(sl);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.ShardLocationAlreadyExists, sme.ErrorCode);
                addFailed = true;
            }

            Assert.IsTrue(addFailed);
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        public void CreateShardDefault()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(
                Globals.ShardMapManagerTestsDatasourceName,
                ShardMapTests.s_shardedDBs[0],
                SqlProtocol.Tcp,
                1433);

            Shard sNew = sm.CreateShard(sl);

            Assert.IsNotNull(sNew);

            // Validate that the shard location is round-tripped correctly
            Assert.AreEqual(sl, sNew.Location);
            Assert.AreEqual(sl, sm.GetShard(sl).Location);

            // Validate that we can connect to the shard
            using (SqlConnection conn = sNew.OpenConnection(
                       Globals.ShardUserConnectionString,
                       ConnectionOptions.Validate))
            {
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public void ValidateShard()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(new ShardCreationInfo(sl, ShardStatus.Online));

            ShardUpdate su = new ShardUpdate();

            su.Status = ShardStatus.Offline;

            Shard sUpdated = sm.UpdateShard(sNew, su);

            Assert.IsNotNull(sUpdated);

            bool validationFailed = false;

            try
            {
                using (SqlConnection conn = sNew.OpenConnection(
                           Globals.ShardUserConnectionString,
                           ConnectionOptions.Validate))
                {
                }
            }
            catch (ShardManagementException sme)
            {
                validationFailed = true;
                Assert.AreEqual(ShardManagementErrorCategory.Validation, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.ShardVersionMismatch, sme.ErrorCode);
            }

            Assert.IsTrue(validationFailed);

            validationFailed = false;

            try
            {
                using (SqlConnection conn = sUpdated.OpenConnection(
                           Globals.ShardUserConnectionString,
                           ConnectionOptions.Validate))
                {
                }
            }
            catch (ShardManagementException)
            {
                validationFailed = true;
            }

            Assert.IsFalse(validationFailed);
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
        /// <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);
        }
Ejemplo n.º 8
0
        public void UpdateShardAbortGSM()
        {
            ShardMapManager smm = new ShardMapManager(
                new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString),
                new SqlStoreConnectionFactory(),
                new StubStoreOperationFactory()
            {
                CallBase = true,
                CreateUpdateShardOperationShardMapManagerIStoreShardMapIStoreShardIStoreShard =
                    (_smm, _sm, _so, _sn) => new NTimeFailingUpdateShardOperation(10, _smm, _sm, _so, _sn)
            },
                new CacheStore(),
                ShardMapManagerLoadPolicy.Lazy,
                new RetryPolicy(1, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero),
                RetryBehavior.DefaultRetryBehavior);
            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(new ShardCreationInfo(sl, ShardStatus.Online));

            ShardUpdate su = new ShardUpdate();

            su.Status = ShardStatus.Offline;

            bool storeOperationFailed = false;

            try
            {
                Shard sUpdated = sm.UpdateShard(sNew, su);
                Assert.IsNotNull(sNew);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.StorageOperationFailure, sme.ErrorCode);
                storeOperationFailed = true;
            }

            Assert.IsTrue(storeOperationFailed);

            // verify that shard status is not changed.
            Shard sValidate = sm.GetShard(sl);

            Assert.AreEqual(sNew.Status, sValidate.Status);
        }
Ejemplo n.º 9
0
        public void DeleteShardAbortGSM()
        {
            ShardMapManager smm = new ShardMapManager(
                new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString),
                new SqlStoreConnectionFactory(),
                new StubStoreOperationFactory()
            {
                CallBase = true,
                CreateRemoveShardOperationShardMapManagerIStoreShardMapIStoreShard =
                    (_smm, _sm, _s) => new NTimeFailingRemoveShardOperation(10, _smm, _sm, _s)
            },
                new CacheStore(),
                ShardMapManagerLoadPolicy.Lazy,
                new RetryPolicy(1, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero), RetryBehavior.DefaultRetryBehavior);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(sl);

            Assert.IsNotNull(sNew);

            bool storeOperationFailed = false;

            try
            {
                sm.DeleteShard(sNew);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.StorageOperationFailure, sme.ErrorCode);
                storeOperationFailed = true;
            }

            Assert.IsTrue(storeOperationFailed);

            // verify that the shard exists in store.
            Shard sValidate = sm.GetShard(sl);

            Assert.IsNotNull(sValidate);
        }
Ejemplo n.º 10
0
        public void DeleteShardDefault()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(sl);

            Assert.IsNotNull(sNew);

            sm.DeleteShard(sNew);
        }
        /// <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);
        }
        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>
        /// 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);
            }
        }
Ejemplo n.º 14
0
        public void UpdateShardVersionMismatch()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(new ShardCreationInfo(sl, ShardStatus.Online));

            // update shard once to increment version

            ShardUpdate su = new ShardUpdate();

            su.Status = ShardStatus.Offline;

            Shard sUpdated = sm.UpdateShard(sNew, su);

            Assert.IsNotNull(sNew);

            // now try updating sNew shard again.

            bool updateFailed = false;

            try
            {
                Shard sUpdatedFail = sm.UpdateShard(sNew, su);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.ShardVersionMismatch, sme.ErrorCode);
                updateFailed = true;
            }

            Assert.IsTrue(updateFailed);
        }
Ejemplo n.º 15
0
        public void UpdateShard()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(new ShardCreationInfo(sl, ShardStatus.Online));

            ShardUpdate su = new ShardUpdate();

            su.Status = ShardStatus.Offline;

            Shard sUpdated = sm.UpdateShard(sNew, su);

            Assert.IsNotNull(sNew);
        }
Ejemplo n.º 16
0
        public void CreateShardNullLocation()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            bool addFailed = false;

            try
            {
                ShardLocation sl   = new ShardLocation("", "");
                Shard         sNew = sm.CreateShard(sl);
            }
            catch (ArgumentException)
            {
                addFailed = true;
            }

            Assert.IsTrue(addFailed);
        }
Ejemplo n.º 17
0
        public void DeleteShardAbortGSMDoAndLSMUndo()
        {
            bool shouldThrow = true;

            IStoreOperationFactory sof = new StubStoreOperationFactory()
            {
                CallBase = true,
                CreateRemoveShardOperationShardMapManagerIStoreShardMapIStoreShard =
                    (_smm, _sm, _s) =>
                {
                    StubRemoveShardOperation op = new StubRemoveShardOperation(_smm, _sm, _s);
                    op.CallBase = true;
                    op.DoGlobalPostLocalExecuteIStoreTransactionScope = (ts) =>
                    {
                        if (shouldThrow)
                        {
                            throw new StoreException("", ShardMapFaultHandlingTests.TransientSqlException);
                        }
                        else
                        {
                            // Call the base function, hack for this behavior is to save current operation, set current to null, restore current operation.
                            var original = op.DoGlobalPostLocalExecuteIStoreTransactionScope;

                            op.DoGlobalPostLocalExecuteIStoreTransactionScope = null;
                            try
                            {
                                return(op.DoGlobalPostLocalExecute(ts));
                            }
                            finally
                            {
                                op.DoGlobalPostLocalExecuteIStoreTransactionScope = original;
                            }
                        }
                    };

                    op.UndoLocalSourceExecuteIStoreTransactionScope = (ts) =>
                    {
                        if (shouldThrow)
                        {
                            throw new StoreException("", ShardMapFaultHandlingTests.TransientSqlException);
                        }
                        else
                        {
                            // Call the base function, hack for this behavior is to save current operation, set current to null, restore current operation.
                            var original = op.UndoLocalSourceExecuteIStoreTransactionScope;

                            op.UndoLocalSourceExecuteIStoreTransactionScope = null;
                            try
                            {
                                return(op.UndoLocalSourceExecute(ts));
                            }
                            finally
                            {
                                op.UndoLocalSourceExecuteIStoreTransactionScope = original;
                            }
                        }
                    };

                    return(op);
                }
            };

            ShardMapManager smm = new ShardMapManager(
                new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString),
                new SqlStoreConnectionFactory(),
                sof,
                new CacheStore(),
                ShardMapManagerLoadPolicy.Lazy,
                new RetryPolicy(1, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero),
                RetryBehavior.DefaultRetryBehavior);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            Shard sNew = sm.CreateShard(sl);

            Assert.IsNotNull(sNew);

            bool storeOperationFailed = false;

            try
            {
                sm.DeleteShard(sNew);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.StorageOperationFailure, sme.ErrorCode);
                storeOperationFailed = true;
            }

            Assert.IsTrue(storeOperationFailed);

            // verify that the shard exists in store.
            Shard sValidate = sm.GetShard(sl);

            Assert.IsNotNull(sValidate);

            // Obtain the pending operations.
            var pendingOperations = ShardMapperTests.GetPendingStoreOperations();

            Assert.AreEqual(pendingOperations.Count(), 1);

            shouldThrow          = false;
            storeOperationFailed = false;
            try
            {
                sm.DeleteShard(sNew);
            }
            catch (ShardManagementException)
            {
                storeOperationFailed = true;
            }

            Assert.IsFalse(storeOperationFailed);
            Assert.AreEqual(0, sm.GetShards().Count());
        }
Ejemplo n.º 18
0
        public void CreateShardAbortGSM()
        {
            int retryCount = 0;

            EventHandler <RetryingEventArgs> eventHandler = (sender, arg) =>
            {
                retryCount++;
            };

            ShardMapManager smm = new ShardMapManager(
                new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString),
                new SqlStoreConnectionFactory(),
                new StubStoreOperationFactory()
            {
                CallBase = true,
                CreateAddShardOperationShardMapManagerIStoreShardMapIStoreShard =
                    (_smm, _sm, _s) => new NTimeFailingAddShardOperation(10, _smm, _sm, _s)
            },
                new CacheStore(),
                ShardMapManagerLoadPolicy.Lazy,
                new RetryPolicy(5, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero),
                RetryBehavior.DefaultRetryBehavior);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapTests.s_shardedDBs[0]);

            bool storeOperationFailed = false;

            smm.ShardMapManagerRetrying += eventHandler;

            try
            {
                Shard sNew = sm.CreateShard(sl);
                Assert.IsNotNull(sNew);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.StorageOperationFailure, sme.ErrorCode);
                storeOperationFailed = true;
            }

            smm.ShardMapManagerRetrying -= eventHandler;

            Assert.AreEqual(5, retryCount);

            Assert.IsTrue(storeOperationFailed);

            // verify that shard map does not have any shards.
            int count = 0;
            IEnumerable <Shard> sList = sm.GetShards();

            using (IEnumerator <Shard> sEnum = sList.GetEnumerator())
            {
                while (sEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(0, count);
        }
        /// <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);
            }
        }
Ejemplo n.º 20
0
        public void CreateShardDefault()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            ShardLocation sl = new ShardLocation(
                Globals.ShardMapManagerTestsDatasourceName,
                ShardMapTests.s_shardedDBs[0],
                SqlProtocol.Tcp,
                1433);

            Shard sNew = sm.CreateShard(sl);

            Assert.IsNotNull(sNew);

            // Validate that the shard location is round-tripped correctly
            Assert.AreEqual(sl, sNew.Location);
            Assert.AreEqual(sl, sm.GetShard(sl).Location);

            // Validate that we can connect to the shard (using all the overloads OpenConnection available)
            using (sNew.OpenConnection(
                       Globals.ShardUserConnectionString))
            {
            }

            // Validate that we can connect to the shard
            using (sNew.OpenConnection(
                       Globals.ShardUserConnectionString,
                       ConnectionOptions.Validate))
            {
            }

            var sqlAuthLogin = new SqlAuthenticationLogin(Globals.ShardMapManagerConnectionString, Globals.SqlLoginTestUser, Globals.SqlLoginTestPassword);

            if (sqlAuthLogin.Create())
            {
                // Validate that we can connect to the shard using Sql Auth
                using (sNew.OpenConnection(Globals.ShardUserConnectionStringForSqlAuth(sqlAuthLogin.UniquifiedUserName), ConnectionOptions.Validate))
                {
                }

                // Validate that we can connect to the shard using a secure Sql Auth Credential
                using (sNew.OpenConnection(string.Empty, Globals.ShardUserCredentialForSqlAuth(sqlAuthLogin.UniquifiedUserName)))
                {
                }

                using (sNew.OpenConnection(
                           string.Empty,
                           Globals.ShardUserCredentialForSqlAuth(sqlAuthLogin.UniquifiedUserName),
                           ConnectionOptions.Validate))
                {
                }

                // Validate that we can connect to the shard using Sql Auth and a secure credential
                // This should fail with an ArgumentException (because you can't pass in both a SqlAuth connection string
                // and a secure credential.
                try
                {
                    using (sNew.OpenConnection(
                               Globals.ShardUserConnectionStringForSqlAuth(sqlAuthLogin.UniquifiedUserName),
                               Globals.ShardUserCredentialForSqlAuth(sqlAuthLogin.UniquifiedUserName),
                               ConnectionOptions.Validate))
                    {
                    }

                    Assert.Fail("This should have thrown, ");
                }
                catch (ArgumentException)
                {
                    // Expected failure. you can't pass in both a SqlAuth connection string and a secure credential
                }

                // Drop test login
                sqlAuthLogin.Drop();
            }
            else
            {
                Assert.Inconclusive("Failed to create sql login, test skipped");
            }
        }
Ejemplo n.º 21
0
        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);
        }