public void LoadTestPointMappingDDR() { try { ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); ListShardMap <int> lsm = smm.GetListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName); Assert.IsNotNull(lsm); PointMapping <int> p1 = this.GetRandomPointMapping(lsm); if (p1 != null) { Debug.WriteLine("Trying to validate point mapping for key {0}", p1.Key); // Validate mapping by trying to connect s_retryPolicy.ExecuteAction( () => ValidateImpl( (ShardMap)lsm, (int)(p1.Key.Value))); } } catch (ShardManagementException sme) { Debug.WriteLine("Exception caught: {0}", sme.Message); } }
public void LoadTestMarkAllShardsAsOnlineInListShardMap() { try { ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); ListShardMap <int> lsm = smm.GetListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName); Assert.IsNotNull(lsm); foreach (Shard s in lsm.GetShards()) { if (s.Status == ShardStatus.Offline) { lsm.UpdateShard(s, new ShardUpdate { Status = ShardStatus.Online }); } } } catch (ShardManagementException sme) { Debug.WriteLine("Exception caught: {0}", sme.Message); } }
public void LoadTestDeletePointMapping() { try { ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); ListShardMap <int> lsm = smm.GetListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName); Assert.IsNotNull(lsm); PointMapping <int> p1 = this.GetRandomPointMapping(lsm); if (p1 != null) { Debug.WriteLine("Trying to delete point mapping for key {0}", p1.Key); PointMappingUpdate pu = new PointMappingUpdate(); pu.Status = MappingStatus.Offline; PointMapping <int> mappingToDelete = lsm.UpdateMapping(p1, pu); lsm.DeleteMapping(mappingToDelete); } } catch (ShardManagementException sme) { Debug.WriteLine("Exception caught: {0}", sme.Message); } }
public void LoadTestRemoveShardFromListShardMap() { try { ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); ListShardMap <int> lsm = smm.GetListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName); Assert.IsNotNull(lsm); List <Shard> existingShards = lsm.GetShards().ToList(); if (existingShards.Count == 0) { return; } // If there is already a shard marked as offline, chose that one to delete. // This can happend if earlier remove operation was terminated for some reason - ex. killing connections. Shard offlineShard = existingShards.Find(e => e.Status == ShardStatus.Offline); if (offlineShard == null) { offlineShard = existingShards[_r.Next(existingShards.Count)]; // First mark shard as offline so that other test threads will not add new mappings to it. offlineShard = lsm.UpdateShard(offlineShard, new ShardUpdate { Status = ShardStatus.Offline }); } Debug.WriteLine("Trying to remove shard at location {0}", offlineShard.Location); PointMappingUpdate pu = new PointMappingUpdate(); pu.Status = MappingStatus.Offline; // Remove all mappings from this shard for given shard map. foreach (PointMapping <int> p in lsm.GetMappings(offlineShard)) { PointMapping <int> mappingToDelete = lsm.UpdateMapping(p, pu); lsm.DeleteMapping(mappingToDelete); } // Shard object is changed as mappings are removed, get it again. Shard deleteShard = lsm.GetShard(offlineShard.Location); // now remove shard. lsm.DeleteShard(deleteShard); Debug.WriteLine("Removed shard at location {0} from shard map {1}", deleteShard.Location, lsm); } catch (ShardManagementException sme) { Debug.WriteLine("Exception caught: {0}", sme.Message); } }
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 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); } }
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); }
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); }
public void LoadTestAddShardToListShardMap() { try { ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); ListShardMap <int> lsm = smm.GetListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName); Assert.IsNotNull(lsm); AddShardToShardMap((ShardMap)lsm); } catch (ShardManagementException sme) { Debug.WriteLine("Exception caught: {0}", sme.Message); } }
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 AddPointMappingDefault() { 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<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(2, s); Assert.IsNotNull(p1); PointMapping<int> p2 = lsm.GetMappingForKey(2); 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 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); }
public void DeletePointMappingAbortLSM() { IStoreOperationFactory sof = new StubStoreOperationFactory() { CallBase = true, CreateRemoveMappingOperationShardMapManagerStoreOperationCodeIStoreShardMapIStoreMappingGuid = (_smm, _opcode, _ssm, _sm, _loid) => { StubRemoveMappingOperation op = new StubRemoveMappingOperation(_smm, _opcode, _ssm, _sm, _loid); op.CallBase = true; op.DoLocalSourceExecuteIStoreTransactionScope = (ts) => { throw new StoreException("", ShardMapFaultHandlingTests.TransientSqlException); }; 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); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(1, s); PointMappingUpdate ru = new PointMappingUpdate(); ru.Status = MappingStatus.Offline; // The mapping must be made offline before it can be deleted. p1 = lsm.UpdateMapping(p1, ru); Assert.AreEqual(MappingStatus.Offline, p1.Status); bool storeOperationFailed = false; try { lsm.DeleteMapping(p1); } catch (ShardManagementException sme) { Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory); Assert.AreEqual(ShardManagementErrorCode.StorageOperationFailure, sme.ErrorCode); storeOperationFailed = true; } Assert.IsTrue(storeOperationFailed); // validation: Lookup point will succeed. PointMapping<int> pNew = lsm.GetMappingForKey(1); Assert.IsNotNull(pNew); }
public void AddPointMappingNoCacheUpdate() { // Create a cache store that never inserts. CountingCacheStore cacheStore = new CountingCacheStore( new StubCacheStore() { CallBase = true, AddOrUpdateMappingIStoreMappingCacheStoreMappingUpdatePolicy = (ssm, p) => { } }); ShardMapManager smm = new ShardMapManager( new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString), new SqlStoreConnectionFactory(), new StoreOperationFactory(), cacheStore, ShardMapManagerLoadPolicy.Lazy, RetryPolicy.DefaultRetryPolicy, RetryBehavior.DefaultRetryBehavior); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(2, s); Assert.IsNotNull(p1); PointMapping<int> p2 = lsm.GetMappingForKey(2); Assert.IsNotNull(p2); Assert.AreEqual(0, cacheStore.LookupMappingMissCount); }
public void UpdatePointMappingDefault() { 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<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(1, s); PointMappingUpdate pu = new PointMappingUpdate(); pu.Status = MappingStatus.Offline; PointMapping<int> pNew = lsm.UpdateMapping(p1, pu); Assert.IsNotNull(pNew); PointMapping<int> p2 = lsm.GetMappingForKey(1); Assert.IsNotNull(p2); Assert.AreEqual(0, countingCache.LookupMappingHitCount); // Mark the mapping online again so that it will be cleaned up pu.Status = MappingStatus.Online; PointMapping<int> pUpdated = lsm.UpdateMapping(pNew, pu); Assert.IsNotNull(pUpdated); }
public void DeletePointMappingDefault() { 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<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(1, s); PointMapping<int> p2 = lsm.GetMappingForKey(1); 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<int> mappingToDelete = lsm.UpdateMapping(p1, ru); lsm.DeleteMapping(mappingToDelete); // Verify that the mapping is removed from cache. bool lookupFailed = false; try { PointMapping<int> pLookup = lsm.GetMappingForKey(1); } catch (ShardManagementException sme) { Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory); Assert.AreEqual(ShardManagementErrorCode.MappingNotFoundForKey, sme.ErrorCode); lookupFailed = true; } Assert.IsTrue(lookupFailed); Assert.AreEqual(0, countingCache.LookupMappingMissCount); }
public void AddPointMappingAbortGSMDoAndGSMUndo() { bool shouldThrow = true; IStoreOperationFactory sof = new StubStoreOperationFactory() { CallBase = true, CreateAddMappingOperationShardMapManagerStoreOperationCodeIStoreShardMapIStoreMapping = (_smm, _opcode, _ssm, _sm) => { StubAddMappingOperation op = new StubAddMappingOperation(_smm, _opcode, _ssm, _sm); 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.UndoGlobalPostLocalExecuteIStoreTransactionScope = (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.UndoGlobalPostLocalExecuteIStoreTransactionScope; op.UndoGlobalPostLocalExecuteIStoreTransactionScope = null; try { return op.UndoGlobalPostLocalExecute(ts); } finally { op.UndoGlobalPostLocalExecuteIStoreTransactionScope = 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); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); ShardLocation sl2 = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[1]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); Shard s2 = lsm.CreateShard(sl2); Assert.IsNotNull(s2); bool storeOperationFailed = false; try { PointMapping<int> p1 = lsm.CreatePointMapping(2, s); Assert.IsNotNull(p1); } catch (ShardManagementException sme) { Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory); Assert.AreEqual(ShardManagementErrorCode.StorageOperationFailure, sme.ErrorCode); storeOperationFailed = true; } Assert.IsTrue(storeOperationFailed); // Obtain the pending operations. var pendingOperations = ShardMapperTests.GetPendingStoreOperations(); Assert.AreEqual(pendingOperations.Count(), 1); shouldThrow = false; // Validation: Adding same mapping again even at different location should succeed. PointMapping<int> p2 = lsm.CreatePointMapping(2, s2); Assert.IsNotNull(p2); pendingOperations = ShardMapperTests.GetPendingStoreOperations(); Assert.AreEqual(pendingOperations.Count(), 0); }
public void AddPointMappingDuplicate() { 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<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(1, s); Assert.IsNotNull(p1); bool addFailed = false; try { // add same point mapping again. PointMapping<int> pNew = lsm.CreatePointMapping(1, s); } catch (ShardManagementException sme) { Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory); Assert.AreEqual(ShardManagementErrorCode.MappingPointAlreadyMapped, sme.ErrorCode); addFailed = true; } Assert.IsTrue(addFailed); PointMapping<int> p2 = lsm.GetMappingForKey(1); Assert.IsNotNull(p2); Assert.AreEqual(0, countingCache.LookupMappingHitCount); }
public void UpdatePointMappingLocationAbortLSM() { bool shouldThrow = false; IStoreOperationFactory sof = new StubStoreOperationFactory() { CallBase = true, CreateUpdateMappingOperationShardMapManagerStoreOperationCodeIStoreShardMapIStoreMappingIStoreMappingStringGuid = (_smm, _opcode, _ssm, _sms, _smt, _p, _loid) => { StubUpdateMappingOperation op = new StubUpdateMappingOperation(_smm, _opcode, _ssm, _sms, _smt, _p, _loid); op.CallBase = true; if (shouldThrow) { // Abort on target. op.DoLocalTargetExecuteIStoreTransactionScope = (ts) => { throw new StoreException("", ShardMapFaultHandlingTests.TransientSqlException); }; } 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); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); Shard s1 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0])); Assert.IsNotNull(s1); Shard s2 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[1])); Assert.IsNotNull(s2); PointMapping<int> p1 = lsm.CreatePointMapping(1, s1); PointMappingUpdate pu1 = new PointMappingUpdate(); // Take the mapping offline first before the shard location can be updated. pu1.Status = MappingStatus.Offline; PointMapping<int> pNew = lsm.UpdateMapping(p1, pu1); PointMappingUpdate pu2 = new PointMappingUpdate(); pu2.Shard = s2; shouldThrow = true; bool storeOperationFailed = false; try { pNew = lsm.UpdateMapping(pNew, pu2); Assert.IsNotNull(pNew); } catch (ShardManagementException sme) { Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory); Assert.AreEqual(ShardManagementErrorCode.StorageOperationFailure, sme.ErrorCode); storeOperationFailed = true; } Assert.IsTrue(storeOperationFailed); // validation: validate location of the mapping. PointMapping<int> pValidate = lsm.GetMappingForKey(1); Assert.AreEqual(p1.Shard.Id, pValidate.Shard.Id); }
public void KillConnectionOnOfflinePointMapping() { ShardMapManager smm = new ShardMapManager( new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString), new SqlStoreConnectionFactory(), new StoreOperationFactory(), new CacheStore(), ShardMapManagerLoadPolicy.Lazy, new RetryPolicy(1, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero), RetryBehavior.DefaultRetryBehavior); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(1, s); using (SqlConnection conn = lsm.OpenConnectionForKeyAsync(1, Globals.ShardUserConnectionString).Result) { Assert.AreEqual(ConnectionState.Open, conn.State); PointMappingUpdate pu = new PointMappingUpdate(); pu.Status = MappingStatus.Offline; PointMapping<int> pNew = lsm.UpdateMapping(p1, pu); Assert.IsNotNull(pNew); bool failed = false; try { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select 1"; cmd.CommandType = CommandType.Text; using (SqlDataReader rdr = cmd.ExecuteReader()) { } } } catch (SqlException) { failed = true; } Assert.AreEqual(true, failed); Assert.AreEqual(ConnectionState.Closed, conn.State); failed = false; // Open 2nd connection. try { using (SqlConnection conn2 = lsm.OpenConnectionForKeyAsync(1, Globals.ShardUserConnectionString).Result) { } } catch (AggregateException ex) { var sme = ex.InnerException as ShardManagementException; if (sme != null) { failed = true; Assert.AreEqual(ShardManagementErrorCode.MappingIsOffline, sme.ErrorCode); } } Assert.AreEqual(true, failed); // Mark the mapping online again so that it will be cleaned up pu.Status = MappingStatus.Online; PointMapping<int> pUpdated = lsm.UpdateMapping(pNew, pu); Assert.IsNotNull(pUpdated); failed = false; // Open 3rd connection. This should succeed. try { using (SqlConnection conn3 = lsm.OpenConnectionForKey(1, Globals.ShardUserConnectionString)) { } } catch (ShardManagementException) { failed = true; } Assert.AreEqual(false, failed); } }
/// <summary> /// OpenConnectionForKey for unavailable server using ListShardMap. /// </summary> /// <param name="openConnectionAsync">Whether the connection should be opened asynchronously</param> private void UnavailableServerOpenConnectionForKeyListShardMapInternal(bool openConnectionAsync = false) { StubSqlStoreConnectionFactory scf = null; bool shouldThrow = false; scf = new StubSqlStoreConnectionFactory() { CallBase = true, GetUserConnectionString = (cstr) => { if (shouldThrow) { throw ShardMapFaultHandlingTests.TransientSqlException; } else { var original = scf.GetUserConnectionString; scf.GetUserConnectionString = null; try { return scf.GetUserConnection(cstr); } finally { scf.GetUserConnectionString = original; } } } }; int callCount = 0; // Counting implementation of FindMappingByKey IStoreOperationFactory sof = new StubStoreOperationFactory() { CallBase = true, CreateFindMappingByKeyGlobalOperationShardMapManagerStringIStoreShardMapShardKeyCacheStoreMappingUpdatePolicyShardManagementErrorCategoryBooleanBoolean = (_smm, _opname, _ssm, _sk, _pol, _ec, _cr, _if) => { StubFindMappingByKeyGlobalOperation op = new StubFindMappingByKeyGlobalOperation(_smm, _opname, _ssm, _sk, _pol, _ec, _cr, _if); op.CallBase = true; op.DoGlobalExecuteIStoreTransactionScope = (ts) => { callCount++; // Call the base function, hack for this behavior is to save current operation, set current to null, restore current operation. var original = op.DoGlobalExecuteIStoreTransactionScope; op.DoGlobalExecuteIStoreTransactionScope = null; try { return op.DoGlobalExecute(ts); } finally { op.DoGlobalExecuteIStoreTransactionScope = original; } }; op.DoGlobalExecuteAsyncIStoreTransactionScope = (ts) => Task.FromResult<IStoreResults>(op.DoGlobalExecuteIStoreTransactionScope(ts)); return op; } }; StubCacheStore scs = null; ICacheStoreMapping currentMapping = null; StubICacheStoreMapping sics = new StubICacheStoreMapping { MappingGet = () => currentMapping.Mapping, CreationTimeGet = () => currentMapping.CreationTime, TimeToLiveMillisecondsGet = () => currentMapping.TimeToLiveMilliseconds, ResetTimeToLive = () => currentMapping.ResetTimeToLive(), HasTimeToLiveExpired = () => currentMapping.HasTimeToLiveExpired() }; scs = new StubCacheStore() { CallBase = true, LookupMappingByKeyIStoreShardMapShardKey = (_ssm, _sk) => { var original = scs.LookupMappingByKeyIStoreShardMapShardKey; scs.LookupMappingByKeyIStoreShardMapShardKey = null; try { currentMapping = scs.LookupMappingByKey(_ssm, _sk); return sics; } finally { scs.LookupMappingByKeyIStoreShardMapShardKey = original; } } }; ShardMapManager smm = new ShardMapManager( new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString), scf, sof, scs, ShardMapManagerLoadPolicy.Lazy, RetryPolicy.DefaultRetryPolicy, RetryBehavior.DefaultRetryBehavior); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0]); Shard s = lsm.CreateShard(sl); Assert.IsNotNull(s); PointMapping<int> p1 = lsm.CreatePointMapping(2, s); Assert.IsNotNull(p1); // Mapping is there, now let's try to abort the OpenConnectionForKey shouldThrow = true; bool failed = false; for (int i = 1; i <= 10; i++) { failed = false; try { if (openConnectionAsync) { lsm.OpenConnectionForKeyAsync(2, Globals.ShardUserConnectionString).Wait(); } else { lsm.OpenConnectionForKey(2, Globals.ShardUserConnectionString); } } catch (Exception ex) { if (ex is AggregateException) { ex = ex.InnerException as SqlException; } if (ex is SqlException) { failed = true; } } Assert.IsTrue(failed); } Assert.AreEqual(1, callCount); long currentTtl = ((ICacheStoreMapping)sics).TimeToLiveMilliseconds; Assert.IsTrue(currentTtl > 0); // Let's fake the TTL to be 0, to force another call to store. sics.TimeToLiveMillisecondsGet = () => 0; sics.HasTimeToLiveExpired = () => true; failed = false; try { if (openConnectionAsync) { lsm.OpenConnectionForKeyAsync(2, Globals.ShardUserConnectionString).Wait(); } else { lsm.OpenConnectionForKey(2, Globals.ShardUserConnectionString); } } catch (Exception ex) { if (ex is AggregateException) { ex = ex.InnerException as SqlException; } if (ex is SqlException) { failed = true; } } Assert.IsTrue(failed); Assert.AreEqual(2, callCount); sics.TimeToLiveMillisecondsGet = () => currentMapping.TimeToLiveMilliseconds; sics.HasTimeToLiveExpired = () => currentMapping.HasTimeToLiveExpired(); failed = false; try { if (openConnectionAsync) { lsm.OpenConnectionForKeyAsync(2, Globals.ShardUserConnectionString).Wait(); } else { lsm.OpenConnectionForKey(2, Globals.ShardUserConnectionString); } } catch (Exception ex) { if (ex is AggregateException) { ex = ex.InnerException as SqlException; } if (ex is SqlException) { failed = true; } } Assert.IsTrue(failed); Assert.IsTrue(((ICacheStoreMapping)sics).TimeToLiveMilliseconds > currentTtl); shouldThrow = false; failed = false; try { if (openConnectionAsync) { lsm.OpenConnectionForKeyAsync(2, Globals.ShardUserConnectionString).Wait(); } else { lsm.OpenConnectionForKey(2, Globals.ShardUserConnectionString); } } catch (Exception ex) { if (ex is AggregateException) { ex = ex.InnerException as SqlException; } if (ex is SqlException) { failed = true; } } Assert.IsFalse(failed); Assert.AreEqual(0, ((ICacheStoreMapping)sics).TimeToLiveMilliseconds); }
public void UpdatePointMappingLocation() { 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<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); Shard s1 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0])); Assert.IsNotNull(s1); Shard s2 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[1])); Assert.IsNotNull(s2); PointMapping<int> p1 = lsm.CreatePointMapping(1, s1); PointMappingUpdate pu = new PointMappingUpdate(); pu.Status = MappingStatus.Offline; // Shard location in a mapping cannot be changed unless it is offline. PointMapping<int> pOffline = lsm.UpdateMapping(p1, pu); Assert.IsNotNull(pOffline); Assert.AreEqual(pu.Status, pOffline.Status); pu.Shard = s2; PointMapping<int> pNew = lsm.UpdateMapping(pOffline, pu); Assert.IsNotNull(pNew); PointMapping<int> p2 = lsm.GetMappingForKey(1); Assert.IsNotNull(p2); Assert.AreEqual(0, countingCache.LookupMappingHitCount); Assert.AreEqual(s2.Id, p2.Shard.Id); }
public void UpdatePointMappingIdempotency() { ShardMapManager smm = new ShardMapManager( new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString), new SqlStoreConnectionFactory(), new StoreOperationFactory(), new CacheStore(), ShardMapManagerLoadPolicy.Lazy, new RetryPolicy(1, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero), RetryBehavior.DefaultRetryBehavior); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapperTests.s_listShardMapName); Assert.IsNotNull(lsm); Shard s1 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[0])); Assert.IsNotNull(s1); Shard s2 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapperTests.s_shardedDBs[1])); Assert.IsNotNull(s2); PointMapping<int> p1 = lsm.CreatePointMapping(1, s1); // Online -> Offline - No Location Change PointMappingUpdate pu = new PointMappingUpdate { Status = MappingStatus.Offline }; PointMapping<int> presult = lsm.UpdateMapping(p1, pu); Assert.IsNotNull(presult); Assert.IsTrue(presult.Status == MappingStatus.Offline); // Offline -> Offline - No Location Change pu = new PointMappingUpdate { Status = MappingStatus.Offline }; presult = lsm.UpdateMapping(presult, pu); Assert.IsNotNull(presult); Assert.IsTrue(presult.Status == MappingStatus.Offline); // Offline -> Offline - Location Change pu = new PointMappingUpdate { Shard = s2 }; presult = lsm.UpdateMapping(presult, pu); Assert.IsNotNull(presult); Assert.IsTrue(presult.Status == MappingStatus.Offline); Assert.AreEqual(s2.Location, presult.Shard.Location); // Offline -> Online - No Location Change pu = new PointMappingUpdate { Status = MappingStatus.Online }; presult = lsm.UpdateMapping(presult, pu); Assert.IsNotNull(presult); Assert.IsTrue(presult.Status == MappingStatus.Online); // Online -> Offline - Location Change pu = new PointMappingUpdate { Status = MappingStatus.Offline, Shard = s1 }; presult = lsm.UpdateMapping(presult, pu); Assert.IsNotNull(presult); Assert.IsTrue(presult.Status == MappingStatus.Offline); Assert.AreEqual(s1.Location, presult.Shard.Location); // Offline -> Online - Location Change pu = new PointMappingUpdate { Status = MappingStatus.Online, Shard = s2 }; presult = lsm.UpdateMapping(presult, pu); Assert.IsNotNull(presult); Assert.IsTrue(presult.Status == MappingStatus.Online); Assert.AreEqual(s2.Location, presult.Shard.Location); // Online -> Online - No Location Change pu = new PointMappingUpdate { Status = MappingStatus.Online }; presult = lsm.UpdateMapping(presult, pu); Assert.IsNotNull(presult); Assert.IsTrue(presult.Status == MappingStatus.Online); // Online -> Online - Location Change pu = new PointMappingUpdate { Shard = s1 }; bool failed = false; try { presult = lsm.UpdateMapping(presult, pu); } catch (ShardManagementException sme) { failed = true; Assert.AreEqual(ShardManagementErrorCategory.ListShardMap, sme.ErrorCategory); Assert.AreEqual(ShardManagementErrorCode.MappingIsNotOffline, sme.ErrorCode); } Assert.IsTrue(failed); }
public void AddPointMappingFailGSMAfterSuccessLSMSingleRetry() { ShardMapManager smm = new ShardMapManager( new SqlShardMapManagerCredentials(Globals.ShardMapManagerConnectionString), new SqlStoreConnectionFactory(), new StubStoreOperationFactory() { CallBase = true, CreateAddMappingOperationShardMapManagerStoreOperationCodeIStoreShardMapIStoreMapping = (_smm, _opcode, _ssm, _sm) => new NTimeFailingAddMappingOperation(1, _smm, _opcode, _ssm, _sm) }, new CacheStore(), ShardMapManagerLoadPolicy.Lazy, new RetryPolicy(1, TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero), RetryBehavior.DefaultRetryBehavior); ListShardMap<int> lsm = smm.GetListShardMap<int>(ShardMapFaultHandlingTests.s_listShardMapName); Assert.IsNotNull(lsm); Shard s = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, ShardMapFaultHandlingTests.s_shardedDBs[0])); Assert.IsNotNull(s); bool failed = false; try { PointMapping<int> p1 = lsm.CreatePointMapping(2, s); } catch (ShardManagementException) { failed = true; } Assert.IsFalse(failed); }
public void ConcurrencyScenarioListShardMap() { bool operationFailed; // variable to track status of negative test scenarios // Create 2 SMM objects representing management and client ShardMapManager smmMgmt = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); ShardMapManager smmClient = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); #region CreateShardMap // Add a shard map from management SMM. ShardMap smMgmt = smmMgmt.CreateListShardMap <int>(ShardMapManagerConcurrencyTests.s_shardMapName); Assert.AreEqual(ShardMapManagerConcurrencyTests.s_shardMapName, smMgmt.Name); // Lookup shard map from client SMM. ShardMap smClient = smmClient.GetShardMap(ShardMapManagerConcurrencyTests.s_shardMapName); Assert.IsNotNull(smClient); #endregion CreateShardMap #region ConvertToListShardMap ListShardMap <int> lsmMgmt = smmMgmt.GetListShardMap <int>(ShardMapManagerConcurrencyTests.s_shardMapName); Assert.IsNotNull(lsmMgmt); // look up shard map again, it will ListShardMap <int> lsmClient = smmClient.GetListShardMap <int>(ShardMapManagerConcurrencyTests.s_shardMapName); Assert.IsNotNull(lsmClient); #endregion ConvertToListShardMap #region DeleteShardMap // verify that smClient is accessible IEnumerable <Shard> shardClient = lsmClient.GetShards(); smmMgmt.DeleteShardMap(lsmMgmt); operationFailed = false; try { // smClient does not exist, below call will fail. IEnumerable <Shard> sCNew = lsmClient.GetShards(); } catch (ShardManagementException sme) { Assert.AreEqual(ShardManagementErrorCategory.ShardMap, sme.ErrorCategory); Assert.AreEqual(ShardManagementErrorCode.ShardMapDoesNotExist, sme.ErrorCode); operationFailed = true; } Assert.IsTrue(operationFailed); #endregion DeleteShardMap }