Ejemplo n.º 1
0
        public void DeleteShardDuplicate()
        {
            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);

            Assert.IsNotNull(sNew);

            bool removeFailed = false;

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

            Assert.IsTrue(removeFailed);
        }
Ejemplo n.º 2
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.º 3
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);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Helper function to clean default shard map.
        /// </summary>
        private static void CleanShardMapsHelper()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            // Remove all existing mappings from the list shard map.
            ShardMap sm = smm.GetShardMap(ShardMapTests.s_defaultShardMapName);

            Assert.IsNotNull(sm);

            // Remove all shards from list shard map
            IEnumerable <Shard> s = sm.GetShards();

            using (IEnumerator <Shard> sEnum = s.GetEnumerator())
            {
                while (sEnum.MoveNext())
                {
                    sm.DeleteShard(sEnum.Current);
                }
            }
        }
Ejemplo n.º 5
0
        public void DeleteShardVersionMismatch()
        {
            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);

            // Update shard to increment version

            ShardUpdate su = new ShardUpdate();

            su.Status = ShardStatus.Offline;

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

            bool removeFailed = false;

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

            Assert.IsTrue(removeFailed);
        }
Ejemplo n.º 6
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.º 7
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);
        }