public void DateAddPointMappingDuplicate()
        {
            CountingCacheStore countingCache = new CountingCacheStore(new CacheStore());

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

            ListShardMap <DateTime> lsm = smm.GetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName);

            Assert.IsNotNull(lsm);

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

            Shard s = lsm.CreateShard(sl);

            Assert.IsNotNull(s);

            DateTime val = DateTime.Now;
            PointMapping <DateTime> p1 = lsm.CreatePointMapping(val, s);

            Assert.IsNotNull(p1);

            bool addFailed = false;

            try
            {
                // add same point mapping again.
                PointMapping <DateTime> pNew = lsm.CreatePointMapping(val, s);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.MappingPointAlreadyMapped, sme.ErrorCode);
                addFailed = true;
            }

            Assert.IsTrue(addFailed);

            PointMapping <DateTime> p2 = lsm.GetMappingForKey(val);

            Assert.IsNotNull(p2);
            Assert.AreEqual(0, countingCache.LookupMappingHitCount);
        }
Esempio n. 2
0
        /// <summary> This app will be for automating on-boarding of new tenant </summary>
        static void Main(string[] args)
        {
            Console.WriteLine($"Starting Shard Registration For Tenant:{args[0]}");
            var tenantKey           = args[0];
            var sqlShardDBServer    = args[1];
            var sqlShardDB          = args[2];
            var shardedDBConnection = args[3];

            ShardMapFactory     shardMapFactory = new ShardMapFactory(shardedDBConnection);
            ListShardMap <Guid> shardMap        = (ListShardMap <Guid>)shardMapFactory.CreateShardMap();


            string appserver   = sqlShardDBServer;
            string appdatabase = sqlShardDB;
            Guid   TenantId    = Guid.Parse(tenantKey);

            var shardLocation = new ShardLocation(appserver, appdatabase);

            if (!shardMap.TryGetShard(shardLocation, out var shard))
            {
                shard = shardMap.CreateShard(new ShardLocation(appserver, appdatabase));
            }
            PointMapping <Guid> mapping;

            if (!shardMap.TryGetMappingForKey(TenantId, out mapping))
            {
                shardMap.CreatePointMapping(TenantId, shard);
            }
        }
        public void DateDeletePointMappingDefault()
        {
            CountingCacheStore countingCache = new CountingCacheStore(new CacheStore());

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

            ListShardMap <DateTime> lsm = smm.GetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName);

            Assert.IsNotNull(lsm);

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

            Shard s = lsm.CreateShard(sl);

            Assert.IsNotNull(s);

            DateTime val = DateTime.Now;
            PointMapping <DateTime> p1 = lsm.CreatePointMapping(val, s);

            PointMapping <DateTime> p2 = lsm.GetMappingForKey(val);

            Assert.IsNotNull(p2);
            Assert.AreEqual(0, countingCache.LookupMappingHitCount);

            // The mapping must be made offline first before it can be deleted.
            PointMappingUpdate ru = new PointMappingUpdate();

            ru.Status = MappingStatus.Offline;

            PointMapping <DateTime> mappingToDelete = lsm.UpdateMapping(p1, ru);

            lsm.DeleteMapping(mappingToDelete);

            // Try to get from store. Because the mapping is missing from the store, we will try to
            // invalidate the cache, but since it is also missing from cache there will be an cache miss.
            bool lookupFailed = false;

            try
            {
                PointMapping <DateTime> pLookup = lsm.GetMappingForKey(val);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.MappingNotFoundForKey, sme.ErrorCode);
                lookupFailed = true;
            }

            Assert.IsTrue(lookupFailed);
            Assert.AreEqual(1, countingCache.LookupMappingMissCount);
        }
        public void CreateIfNotExists(ListShardMap <TKey> shardMap, Shard shard, TKey key)
        {
            var mappingExists = shardMap.TryGetMappingForKey(key, out var pointMapping);

            if (!mappingExists)
            {
                shardMap.CreatePointMapping(key, shard);
            }
        }
        public void CreateShard(ListShardMap <int> shardMap, int id)
        {
            // Create a new shard, or get an existing empty shard (if a previous create partially succeeded).
            Shard shard = CreateOrGetEmptyShard(shardMap);

            // Create a mapping to that shard.
            var r = shardMap.CreatePointMapping(id, shard);
            // ConsoleUtils.WriteInfo("Mapped range {0} to shard {1}", mappingForNewShard.Value, shard.Location.Database);
        }
        public void LoadTestAddPointMapping()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

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

                Assert.IsNotNull(lsm);
                do
                {
                    // Chose a random shard to add mapping.
                    Shard s = GetRandomOnlineShardFromShardMap((ShardMap)lsm);
                    if (s == null)
                    {
                        continue;
                    }

                    // Create a random integer key for a new mapping and verify that its not already present in this shard map.
                    int key = _r.Next(MinMappingPoint, MaxMappingPoint);
                    PointMapping <int> pExisting = null;

                    // choose different mapping if this one already exists.
                    if (lsm.TryGetMappingForKey(key, out pExisting))
                    {
                        continue;
                    }

                    Debug.WriteLine("Trying to add point mapping for key {0} to shard location {1}", key, s.Location);

                    PointMapping <int> p1 = lsm.CreatePointMapping(key, s);

                    Assert.IsNotNull(p1);

                    // Validate mapping by trying to connect
                    s_retryPolicy.ExecuteAction(
                        () => ValidateImpl(
                            (ShardMap)lsm,
                            key));
                }while (false);
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
Esempio n. 7
0
        public async Task <PointMapping <Guid> > CreateShard(ListShardMap <Guid> shardMap, Guid point, string dbName, Func <Stream> shardInitializerStript, Action <string> stepOverCallBack, string dbEdition = "Basic")
        {
            var callBack = stepOverCallBack ?? (x => { });

            if (string.IsNullOrWhiteSpace(dbName))
            {
                callBack("No se pudo determinar el nombre de la location del shard (dbname).");
            }
            await dataBase.Verify(conf.UniverseServerName, dbName, shardInitializerStript, callBack, dbEdition);

            callBack("Verificando inicialización del shard.");
            var shardLocation = new ShardLocation(conf.UniverseServerName, dbName);
            var shard         = CreateOrGetShard(shardMap, shardLocation);

            return(shardMap.CreatePointMapping(point, shard));
        }
        public void DateDeletePointMappingNonExisting()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ListShardMap <DateTime> lsm = smm.GetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName);

            Assert.IsNotNull(lsm);

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

            Shard s = lsm.CreateShard(sl);

            Assert.IsNotNull(s);

            DateTime val = DateTime.Now;
            PointMapping <DateTime> p1 = lsm.CreatePointMapping(val, s);

            Assert.IsNotNull(p1);

            PointMappingUpdate ru = new PointMappingUpdate();

            ru.Status = MappingStatus.Offline;

            // The mapping must be made offline before it can be deleted.
            p1 = lsm.UpdateMapping(p1, ru);

            lsm.DeleteMapping(p1);

            bool removeFailed = false;

            try
            {
                lsm.DeleteMapping(p1);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.MappingDoesNotExist, sme.ErrorCode);
                removeFailed = true;
            }

            Assert.IsTrue(removeFailed);
        }
Esempio n. 9
0
        public bool TryAddTenant(T tenantId, string shardName)
        {
            var database = $"{_shardingConfig.ShardPrefix}.{shardName}";
            var location = new ShardLocation(_shardingConfig.ShardServer, database);

            if (!_shardMap.TryGetMappingForKey(tenantId, out _) && _shardMap.TryGetShard(location, out _))
            {
                lock (_tenantLock)
                {
                    if (!_shardMap.TryGetMappingForKey(tenantId, out _) && _shardMap.TryGetShard(location, out Shard shard))
                    {
                        _shardMap.CreatePointMapping(new PointMappingCreationInfo <T>(tenantId, shard, MappingStatus.Online));
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 10
0
        public void DateAddPointMappingDefault()
        {
            CountingCacheStore countingCache = new CountingCacheStore(new CacheStore());

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

            ListShardMap <DateTime> lsm = smm.GetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName);

            Assert.IsNotNull(lsm);

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

            Shard s = lsm.CreateShard(sl);

            Assert.IsNotNull(s);

            DateTime val = DateTime.Now;
            PointMapping <DateTime> p1 = lsm.CreatePointMapping(val, s);

            Assert.IsNotNull(p1);

            PointMapping <DateTime> p2 = lsm.GetMappingForKey(val);

            Assert.IsNotNull(p2);
            Assert.AreEqual(0, countingCache.LookupMappingCount);
            Assert.AreEqual(0, countingCache.LookupMappingHitCount);

            // Validate mapping by trying to connect
            using (SqlConnection conn = lsm.OpenConnection(
                       p1,
                       Globals.ShardUserConnectionString,
                       ConnectionOptions.Validate))
            {
            }
        }
        public void DateGetPointMappingsForRange()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ListShardMap <DateTime> lsm = smm.GetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName);

            Assert.IsNotNull(lsm);

            Shard s1 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, DateTimeShardMapperTests.s_shardedDBs[0]));

            Assert.IsNotNull(s1);

            Shard s2 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, DateTimeShardMapperTests.s_shardedDBs[1]));

            Assert.IsNotNull(s2);

            DateTime val1 = DateTime.Now.Subtract(TimeSpan.FromMinutes(10));
            PointMapping <DateTime> p1 = lsm.CreatePointMapping(val1, s1);

            Assert.IsNotNull(p1);

            DateTime val2 = DateTime.Now.Subtract(TimeSpan.FromMinutes(20));
            PointMapping <DateTime> p2 = lsm.CreatePointMapping(val2, s1);

            Assert.IsNotNull(p2);

            DateTime val3 = DateTime.Now.Subtract(TimeSpan.FromMinutes(30));
            PointMapping <DateTime> p3 = lsm.CreatePointMapping(val3, s2);

            Assert.IsNotNull(p2);

            // Get all mappings in shard map.
            int count = 0;
            IEnumerable <PointMapping <DateTime> > allMappings = lsm.GetMappings();

            using (IEnumerator <PointMapping <DateTime> > mEnum = allMappings.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(3, count);

            // Get all mappings in specified range.
            Range <DateTime> wantedRange = new Range <DateTime>(val3.AddMinutes(-5), val3.AddMinutes(15));

            count = 0;
            IEnumerable <PointMapping <DateTime> > mappingsInRange = lsm.GetMappings(wantedRange);

            using (IEnumerator <PointMapping <DateTime> > mEnum = mappingsInRange.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(2, count);

            // Get all mappings for a shard.
            count = 0;
            IEnumerable <PointMapping <DateTime> > mappingsForShard = lsm.GetMappings(s1);

            using (IEnumerator <PointMapping <DateTime> > mEnum = mappingsForShard.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(2, count);

            // Get all mappings in specified range for a particular shard.
            count = 0;
            IEnumerable <PointMapping <DateTime> > mappingsInRangeForShard = lsm.GetMappings(wantedRange, s1);

            using (IEnumerator <PointMapping <DateTime> > mEnum = mappingsInRangeForShard.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(1, count);
        }
        public void CreateShard(ListShardMap <int> shardMap, string databaseShardName, int shardingKey)
        {
            var shard = CreateOrGetShard(shardMap, databaseShardName);

            shardMap.CreatePointMapping(shardingKey, shard);
        }
Esempio n. 13
0
        public void BasicScenarioListShardMaps()
        {
            bool   success      = true;
            string shardMapName = "PerTenantShardMap";

            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 CreateListShardMap

                // Create a single user per-tenant shard map.
                ListShardMap <int> perTenantShardMap = shardMapManager.CreateListShardMap <int>(shardMapName);

                #endregion CreateListShardMap

                #region CreateShardAndPointMapping

                for (int i = 0; i < ScenarioTests.s_perTenantDBs.Length; i++)
                {
                    // Create the shard.
                    Shard s = perTenantShardMap.CreateShard(
                        new ShardLocation(
                            Globals.ShardMapManagerTestsDatasourceName,
                            ScenarioTests.s_perTenantDBs[i]));

                    // Create the mapping.
                    PointMapping <int> p = perTenantShardMap.CreatePointMapping(
                        i + 1,
                        s);
                }

                #endregion CreateShardAndPointMapping

                #region UpdatePointMapping

                // Let's add another point 5 and map it to same shard as 1.

                PointMapping <int> mappingForOne = perTenantShardMap.GetMappingForKey(1);

                PointMapping <int> mappingForFive = perTenantShardMap.CreatePointMapping(5, mappingForOne.Shard);

                Assert.IsTrue(mappingForOne.Shard.Location.Equals(mappingForFive.Shard.Location));

                // Move 3 from PerTenantDB3 to PerTenantDB for 5.
                PointMapping <int> mappingToUpdate = perTenantShardMap.GetMappingForKey(3);
                bool updateFailed = false;

                // Try updating that shard in the mapping without taking it offline first.
                try
                {
                    perTenantShardMap.UpdateMapping(
                        mappingToUpdate,
                        new PointMappingUpdate
                    {
                        Shard = mappingForFive.Shard
                    });
                }
                catch (ShardManagementException smme)
                {
                    Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.MappingIsNotOffline);
                    updateFailed = true;
                }

                Assert.IsTrue(updateFailed);

                // Perform the actual update.
                PointMapping <int> newMappingFor3 = MarkMappingOfflineAndUpdateShard <int>(
                    perTenantShardMap, mappingToUpdate, mappingForFive.Shard);

                // Verify that update succeeded.
                Assert.IsTrue(newMappingFor3.Shard.Location.Equals(mappingForFive.Shard.Location));
                Assert.IsTrue(newMappingFor3.Status == MappingStatus.Offline);

                // Update custom field for the updated mapping.
                //PointMapping<int> veryNewMappingFor3 = perTenantShardMap.UpdatePointMapping(
                //    newMappingFor3,
                //    new PointMappingUpdate
                //    {
                //        Custom = new byte[] { 0x12, 0x34 }
                //    });

                #endregion UpdatePointMapping

                #region DeleteMapping

                // Find the shard by location.
                PointMapping <int> mappingToDelete = perTenantShardMap.GetMappingForKey(5);
                bool operationFailed = false;

                // Try to delete mapping while it is online, the delete should fail.
                try
                {
                    perTenantShardMap.DeleteMapping(mappingToDelete);
                }
                catch (ShardManagementException smme)
                {
                    operationFailed = true;
                    Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.MappingIsNotOffline);
                }

                Trace.Assert(operationFailed);

                // The mapping must be taken offline first before it can be deleted.
                mappingToDelete = perTenantShardMap.UpdateMapping(
                    mappingToDelete,
                    new PointMappingUpdate
                {
                    Status = MappingStatus.Offline,
                });

                perTenantShardMap.DeleteMapping(mappingToDelete);

                // Verify that delete succeeded.
                try
                {
                    PointMapping <int> deletedMapping = perTenantShardMap.GetMappingForKey(5);
                }
                catch (ShardManagementException smme)
                {
                    Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.MappingNotFoundForKey);
                }

                #endregion DeleteMapping

                #region OpenConnection without Validation

                using (SqlConnection conn = perTenantShardMap.OpenConnectionForKey(
                           2,
                           Globals.ShardUserConnectionString,
                           ConnectionOptions.None))
                {
                }

                #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 = perTenantShardMap.OpenConnection(
                               mappingToDelete,
                               Globals.ShardUserConnectionString,
                               ConnectionOptions.Validate))
                    {
                    }
                }
                catch (ShardManagementException smme)
                {
                    validationFailed = true;
                    Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.MappingDoesNotExist);
                }

                Assert.AreEqual(validationFailed, true);

                #endregion OpenConnection with Validation

                #region OpenConnection without Validation and Empty Cache

                // Obtain a new ShardMapManager instance
                ShardMapManager newShardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                // Get the ShardMap
                ListShardMap <int> newPerTenantShardMap = newShardMapManager.GetListShardMap <int>(shardMapName);

                using (SqlConnection conn = newPerTenantShardMap.OpenConnectionForKey(
                           2,
                           Globals.ShardUserConnectionString,
                           ConnectionOptions.None))
                {
                }

                #endregion

                #region OpenConnection with Validation and Empty Cache

                // Use the stale state of "shardToUpdate" shard & see if validation works.
                validationFailed = false;

                // Obtain a new ShardMapManager instance
                newShardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                // Get the ShardMap
                newPerTenantShardMap = newShardMapManager.GetListShardMap <int>(shardMapName);

                // Create a new mapping
                PointMapping <int> newMappingToDelete = newPerTenantShardMap.CreatePointMapping(6,
                                                                                                newPerTenantShardMap.GetMappingForKey(1).Shard);

                // Delete the mapping
                newMappingToDelete = newPerTenantShardMap.UpdateMapping(
                    newMappingToDelete,
                    new PointMappingUpdate
                {
                    Status = MappingStatus.Offline,
                });

                newPerTenantShardMap.DeleteMapping(newMappingToDelete);

                try
                {
                    using (SqlConnection conn = newPerTenantShardMap.OpenConnection(
                               newMappingToDelete,
                               Globals.ShardUserConnectionString,
                               ConnectionOptions.Validate))
                    {
                    }
                }
                catch (ShardManagementException smme)
                {
                    validationFailed = true;
                    Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.MappingDoesNotExist);
                }

                Assert.AreEqual(validationFailed, true);

                #endregion

                #region OpenConnectionAsync without Validation

                using (SqlConnection conn = perTenantShardMap.OpenConnectionForKeyAsync(
                           2,
                           Globals.ShardUserConnectionString,
                           ConnectionOptions.None).Result)
                {
                }

                #endregion

                #region OpenConnectionAsync with Validation

                // Use the stale state of "shardToUpdate" shard & see if validation works.
                validationFailed = false;
                try
                {
                    using (SqlConnection conn = perTenantShardMap.OpenConnectionAsync(
                               mappingToDelete,
                               Globals.ShardUserConnectionString,
                               ConnectionOptions.Validate).Result)
                    {
                    }
                }
                catch (AggregateException ex)
                {
                    ShardManagementException smme = ex.InnerException as ShardManagementException;
                    if (smme != null)
                    {
                        validationFailed = true;
                        Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.MappingDoesNotExist);
                    }
                }

                Assert.AreEqual(validationFailed, true);

                #endregion

                #region OpenConnectionAsync without Validation and Empty Cache

                // Obtain a new ShardMapManager instance
                newShardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                // Get the ShardMap
                newPerTenantShardMap = newShardMapManager.GetListShardMap <int>(shardMapName);
                using (SqlConnection conn = newPerTenantShardMap.OpenConnectionForKeyAsync(
                           2,
                           Globals.ShardUserConnectionString,
                           ConnectionOptions.None).Result)
                {
                }

                #endregion

                #region OpenConnectionAsync with Validation and Empty Cache

                // Use the stale state of "shardToUpdate" shard & see if validation works.
                validationFailed = false;

                // Obtain a new ShardMapManager instance
                newShardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                // Get the ShardMap
                newPerTenantShardMap = newShardMapManager.GetListShardMap <int>(shardMapName);

                // Create a new mapping
                newMappingToDelete = newPerTenantShardMap.CreatePointMapping(6,
                                                                             newPerTenantShardMap.GetMappingForKey(1).Shard);

                // Delete the mapping
                newMappingToDelete = newPerTenantShardMap.UpdateMapping(
                    newMappingToDelete,
                    new PointMappingUpdate
                {
                    Status = MappingStatus.Offline,
                });

                newPerTenantShardMap.DeleteMapping(newMappingToDelete);

                try
                {
                    using (SqlConnection conn = newPerTenantShardMap.OpenConnectionAsync(
                               newMappingToDelete,
                               Globals.ShardUserConnectionString,
                               ConnectionOptions.Validate).Result)
                    {
                    }
                }
                catch (AggregateException ex)
                {
                    ShardManagementException smme = ex.InnerException as ShardManagementException;
                    if (smme != null)
                    {
                        validationFailed = true;
                        Assert.AreEqual(smme.ErrorCode, ShardManagementErrorCode.MappingDoesNotExist);
                    }
                }

                Assert.AreEqual(validationFailed, true);

                #endregion

                #region LookupPointMapping

                // Perform tenant lookup. This will populate the cache.
                for (int i = 0; i < ScenarioTests.s_perTenantDBs.Length; i++)
                {
                    PointMapping <int> result = shardMapManager
                                                .GetListShardMap <int>("PerTenantShardMap")
                                                .GetMappingForKey(i + 1);

                    Trace.WriteLine(result.Shard.Location);

                    // Since we moved 3 to database 1 earlier.
                    Assert.IsTrue(result.Shard.Location.Database == ScenarioTests.s_perTenantDBs[i != 2 ? i : 0]);
                }

                // Perform tenant lookup. This will read from the cache.
                for (int i = 0; i < ScenarioTests.s_perTenantDBs.Length; i++)
                {
                    PointMapping <int> result = shardMapManager
                                                .GetListShardMap <int>("PerTenantShardMap")
                                                .GetMappingForKey(i + 1);

                    Trace.WriteLine(result.Shard.Location);

                    // Since we moved 3 to database 1 earlier.
                    Assert.IsTrue(result.Shard.Location.Database == ScenarioTests.s_perTenantDBs[i != 2 ? i : 0]);
                }

                #endregion LookupPointMapping
            }
            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);
        }
        private bool RefreshShardListMap(List <Tuple <long, string> > ListMappings = null)
        {
            try
            {
                string shardServer1 = WingtipTicketApp.Config.PrimaryDatabaseServer;
                string shardServer2 = WingtipTicketApp.Config.ShardDatabaseServer;
                string ticketsDbName = WingtipTicketApp.Config.TicketsDbName;
                Shard  shard1 = null, shard2 = null;
                PointMapping <long> lmpg;

                // check if shard map manager exists and if not, create it (Idempotent / tolerant of re-execute)
                if (!ShardMapManager.TryGetListShardMap(mapManagerName, out lsm))
                {
                    lsm = ShardMapManager.CreateListShardMap <long>(mapManagerName);
                }

                try
                {
                    // check if shard exists and if not, create it (Idempotent / tolerant of re-execute)
                    if (!lsm.TryGetShard(new ShardLocation(shardServer1, ticketsDbName), out shard1))
                    {
                        shard1 = lsm.CreateShard(new ShardLocation(shardServer1, ticketsDbName));
                    }
                }
                catch // sometimes, it may throw an error stating that a concurrent user recently changed some settings.
                      //This is a retry logic to cover this scenario.
                {
                    Thread.Sleep(500);
                    // check if shard map manager exists and if not, create it (Idempotent / tolerant of re-execute)
                    if (!ShardMapManager.TryGetListShardMap(mapManagerName, out lsm))
                    {
                        lsm = ShardMapManager.CreateListShardMap <long>(mapManagerName);
                    }
                    // check if shard exists and if not, create it (Idempotent / tolerant of re-execute)
                    if (!lsm.TryGetShard(new ShardLocation(shardServer1, ticketsDbName), out shard1))
                    {
                        shard1 = lsm.CreateShard(new ShardLocation(shardServer1, ticketsDbName));
                    }
                }
                if (!lsm.TryGetShard(new ShardLocation(shardServer2, ticketsDbName), out shard2))
                {
                    shard2 = lsm.CreateShard(new ShardLocation(shardServer2, ticketsDbName));
                }

                // Check if mapping exists and if not, create it (Idempotent / tolerant of re-execute)

                if (ListMappings != null)
                {
                    foreach (Tuple <long, string> mapping in ListMappings)
                    {
                        if (!lsm.TryGetMappingForKey(mapping.Item1, out lmpg))
                        {
                            if (mapping.Item2 == shardServer1)
                            {
                                lsm.CreatePointMapping(new PointMappingCreationInfo <long>(mapping.Item1, shard1, MappingStatus.Online));
                            }
                            else if (mapping.Item2 == shardServer2)
                            {
                                lsm.CreatePointMapping(new PointMappingCreationInfo <long>(mapping.Item1, shard2, MappingStatus.Online));
                            }
                        }
                    }
                }
                return(true);
            }
            catch { return(false); }
        }