示例#1
0
        private static void TryAddShardMapManagement <TTenantIdentity>(IServiceCollection services,
                                                                       string connectionString)
        {
            services.TryAddSingleton(s =>
            {
                var shardingConfig = s.GetService <IOptions <ShardingConfiguration> >().Value;
                EnsureDbExists(connectionString, shardingConfig.ElasticPool);
                if (!ShardMapManagerFactory.TryGetSqlShardMapManager(
                        connectionString,
                        ShardMapManagerLoadPolicy.Lazy,
                        out ShardMapManager shardMapManager))
                {
                    ShardMapManagerFactory.CreateSqlShardMapManager(connectionString);

                    shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                        connectionString,
                        ShardMapManagerLoadPolicy.Lazy);
                }

                return(shardMapManager);
            });

            services.TryAddSingleton(s =>
            {
                var smm        = s.GetService <ShardMapManager>();
                var options    = s.GetService <IOptions <ShardingConfiguration> >().Value;
                string mapName = options.ShardMap;
                // check if shardmap exists and if not, create it
                if (!smm.TryGetListShardMap(mapName, out ListShardMap <TTenantIdentity> shardMap))
                {
                    return(smm.CreateListShardMap <TTenantIdentity>(mapName));
                }
                return(shardMap);
            });
        }
示例#2
0
        public SqlDataManager(
            Logger logger,
            GrainStateMap grainStateMap,
            string connectionString,
            string shardCredentials,
            string mapName,
            BatchingOptions batchingOptions = null)
        {
            Logger = logger;
            Guard.NotNullOrEmpty(connectionString, "conectionString");
            Guard.NotNullOrEmpty(shardCredentials, "shardCredentials");
            Guard.NotNullOrEmpty(mapName, "mapName");

            // Try to get a reference to the Shard Map Manager via the Shard Map Manager database.
            // If it doesn't already exist, then fail
            var shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(connectionString, ShardMapManagerLoadPolicy.Lazy);
            var shardMap        = (RangeShardMap <int>)shardMapManager.GetShardMap(mapName);
            var shardBatchers   = new ConcurrentDictionary <Range <int>, ShardBatcher>();

            foreach (var rangeMapping in shardMap.GetMappings())
            {
                Range <int> range = rangeMapping.Value;
                shardBatchers.TryAdd(range, new ShardBatcher(logger, grainStateMap, rangeMapping.Shard, shardCredentials, batchingOptions));
            }

            _shardBatchers = shardBatchers;
            _shardMap      = shardMap;
        }
        public void CreateShardDefault()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

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

            Assert.IsNotNull(sm);

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

            Shard sNew = sm.CreateShard(sl);

            Assert.IsNotNull(sNew);

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

            // Validate that we can connect to the shard
            using (SqlConnection conn = sNew.OpenConnection(
                       Globals.ShardUserConnectionString,
                       ConnectionOptions.Validate))
            {
            }
        }
        private static RangeShardMap <int> GetShardMap() //singleton
        {
            var shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(ShardMgrConnectionString, ShardMapManagerLoadPolicy.Lazy);
            var shardMap        = shardMapManager.GetRangeShardMap <int>(ShardMapName);

            return(shardMap);
        }
示例#5
0
        public void CreateRangeShardMapDuplicate()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.CreateRangeShardMap <int>(ShardMapManagerTests.s_shardMapName);

            Assert.IsNotNull(sm);

            Assert.AreEqual(ShardMapManagerTests.s_shardMapName, sm.Name);

            bool creationFailed = false;

            try
            {
                RangeShardMap <int> rsm = smm.CreateRangeShardMap <int>(ShardMapManagerTests.s_shardMapName);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCategory.ShardMapManager, sme.ErrorCategory);
                Assert.AreEqual(ShardManagementErrorCode.ShardMapAlreadyExists, sme.ErrorCode);
                creationFailed = true;
            }

            Assert.IsTrue(creationFailed);
        }
示例#6
0
        public void UpgradeGSM()
        {
            // Get shard map manager
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            try
            {
                ShardMap testsm = smm.CreateListShardMap <int>(ShardMapManagerUpgradeTests.s_shardMapNames[0]);
                Assert.IsNotNull(testsm);
            }
            catch (ShardManagementException sme)
            {
                Assert.AreEqual(ShardManagementErrorCode.GlobalStoreVersionMismatch, sme.ErrorCode);
            }

            // Upgrade to version 1.0: no-op
            smm.UpgradeGlobalStore(new Version(1, 0));

            // Upgrade to version 1.1
            smm.UpgradeGlobalStore(new Version(1, 1));

            // Below call should succeed as latest supported major version of library matches major version of deployed store.
            ShardMap sm = smm.CreateListShardMap <int>(ShardMapManagerUpgradeTests.s_shardMapNames[0]);

            Assert.IsNotNull(sm);

            // Upgrade to version 1.2
            smm.UpgradeGlobalStore(new Version(1, 2));

            // Upgrade to latest version (1.2): no-op
            smm.UpgradeGlobalStore();
        }
示例#7
0
        public void GetShardMapsDefault()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ShardMap sm = smm.CreateListShardMap <int>(ShardMapManagerTests.s_shardMapName);

            Assert.IsNotNull(sm);

            Assert.AreEqual(ShardMapManagerTests.s_shardMapName, sm.Name);

            IEnumerable <ShardMap> shardmaps = smm.GetShardMaps();

            int count = 0;

            using (IEnumerator <ShardMap> mEnum = shardmaps.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(1, count);
        }
        public void LoadTestDeleteRangeMapping()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                RangeShardMap <int> rsm = smm.GetRangeShardMap <int>(ShardMapManagerLoadTests.s_rangeShardMapName);
                Assert.IsNotNull(rsm);

                RangeMapping <int> r1 = this.GetRandomRangeMapping(rsm);

                if (r1 != null)
                {
                    Debug.WriteLine("Trying to delete mapping for range with low value = {0}", r1.Range.Low);

                    RangeMappingUpdate ru = new RangeMappingUpdate();
                    ru.Status = MappingStatus.Offline;

                    RangeMapping <int> mappingToDelete = rsm.UpdateMapping(r1, ru);

                    rsm.DeleteMapping(mappingToDelete);
                }
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
        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);
        }
        public void LoadTestRangeMappingDDR()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                RangeShardMap <int> rsm = smm.GetRangeShardMap <int>(ShardMapManagerLoadTests.s_rangeShardMapName);
                Assert.IsNotNull(rsm);

                RangeMapping <int> r1 = this.GetRandomRangeMapping(rsm);

                if (r1 != null)
                {
                    int keyToValidate = _r.Next((int)(r1.Range.Low.Value), (int)(r1.Range.High.Value));

                    Debug.WriteLine("Trying to validate mapping for key {0}", keyToValidate);

                    // Validate mapping by trying to connect
                    s_retryPolicy.ExecuteAction(
                        () => ValidateImpl(
                            (ShardMap)rsm,
                            keyToValidate));
                }
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
示例#11
0
        public void UpgradeLsm()
        {
            // Get shard map manager
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            // upgrade GSM to latest version.
            smm.UpgradeGlobalStore();
            VerifyGlobalStore(smm, GlobalConstants.GsmVersionClient);

            // deploy LSM initial version.
            ShardLocation sl = new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, s_shardedDBs[0]);

            smm.UpgradeLocalStore(sl, s_initialLsmVersion);

            // upgrade to version 1.1
            smm.UpgradeLocalStore(sl, new Version(1, 1));

            // Library is still at LSM major version 1, so adding shard with LSM 1.0 should succeed.
            // Library will see that LSM schema already exists at 'sl' and hence will not deploy LSM again, will just try to add the shard.
            // CreateShard will not work with LSM initial version (1.0) as it only has 'StoreVersion' column in ShardMApManagerLocal table, from 1.1 onwards it follows latest schema for version table.
            ListShardMap <int> listsm = smm.CreateListShardMap <int>(ShardMapManagerUpgradeTests.s_shardMapNames[0]);

            listsm.CreateShard(sl);


            // upgrade to version 1.2
            smm.UpgradeLocalStore(sl, new Version(1, 2));

            // upgrade to latest version (1.2): no-op
            smm.UpgradeLocalStore(sl);
        }
        public void TestSetSchemaInfoWithSpecialChars()
        {
            ShardMapManagerFactory.CreateSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerCreateMode.ReplaceExisting);

            ShardMapManager shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            SchemaInfoCollection siCollection = shardMapManager.GetSchemaInfoCollection();

            SchemaInfo si = new SchemaInfo();

            ShardedTableInfo sti = new ShardedTableInfo(NewNameWithSpecialChars(), NewNameWithSpecialChars());

            si.Add(sti);

            string mdName = String.Format("TestSI_{0}", Guid.NewGuid());

            siCollection.Add(mdName, si);

            SchemaInfo sdmdRead = siCollection.Get(mdName);

            AssertEqual(si, sdmdRead);
        }
        public void LoadTestSplitRangeNoLock()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                RangeShardMap <int> rsm = smm.GetRangeShardMap <int>(ShardMapManagerLoadTests.s_rangeShardMapName);
                Assert.IsNotNull(rsm);

                RangeMapping <int> r1 = this.GetRandomRangeMapping(rsm, 2);

                if (r1 != null)
                {
                    int splitPoint = _r.Next((int)(r1.Range.Low.Value) + 1, (int)(r1.Range.High.Value) - 1);

                    Debug.WriteLine("Trying to split range mapping for key range ({0} - {1}) at {2}", r1.Range.Low.Value, r1.Range.High.Value, splitPoint);

                    IReadOnlyList <RangeMapping <int> > rList = rsm.SplitMapping(r1, splitPoint);
                    Assert.AreEqual(2, rList.Count);
                }
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
示例#14
0
        public void UpgradeGsm()
        {
            // Get shard map manager
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            // Sanity check setup: version should be 1.0
            VerifyGlobalStore(smm, new Version(1, 0));

            // Upgrade to version 1.0: no-op
            smm.UpgradeGlobalStore(new Version(1, 0));
            VerifyGlobalStore(smm, new Version(1, 0));

            // Upgrade to version 1.1
            smm.UpgradeGlobalStore(new Version(1, 1));
            VerifyGlobalStore(smm, new Version(1, 1));

            // Upgrade to version 1.2
            smm.UpgradeGlobalStore(new Version(1, 2));
            VerifyGlobalStore(smm, new Version(1, 2));

            // Upgrade to latest version
            smm.UpgradeGlobalStore();
            VerifyGlobalStore(smm, GlobalConstants.GsmVersionClient);
        }
        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 static void ShardMapperTestsInitialize(TestContext testContext)
        {
            // Clear all connection pools.
            SqlConnection.ClearAllPools();

            using (SqlConnection conn = new SqlConnection(Globals.ShardMapManagerTestConnectionString))
            {
                conn.Open();

                // Create ShardMapManager database
                using (SqlCommand cmd = new SqlCommand(
                           string.Format(Globals.CreateDatabaseQuery, Globals.ShardMapManagerDatabaseName),
                           conn))
                {
                    cmd.ExecuteNonQuery();
                }

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

                    using (SqlCommand cmd = new SqlCommand(
                               string.Format(Globals.CreateDatabaseQuery, DateTimeShardMapperTests.s_shardedDBs[i]),
                               conn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
            }

            // Create shard map manager.
            ShardMapManagerFactory.CreateSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerCreateMode.ReplaceExisting);

            // Create list shard map.
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

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

            Assert.IsNotNull(lsm);

            Assert.AreEqual(DateTimeShardMapperTests.s_listShardMapName, lsm.Name);

            // Create range shard map.
            RangeShardMap <DateTime> rsm = smm.CreateRangeShardMap <DateTime>(DateTimeShardMapperTests.s_rangeShardMapName);

            Assert.IsNotNull(rsm);

            Assert.AreEqual(DateTimeShardMapperTests.s_rangeShardMapName, rsm.Name);
        }
        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);
            }
        }
示例#20
0
        public static void CreateRangeShardMap <TKey>(string connectionString, string mapName)
        {
            var shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                connectionString,
                ShardMapManagerLoadPolicy.Lazy);

            shardMapManager.CreateRangeShardMap <TKey>(mapName);
        }
示例#21
0
        public void ValidateShard()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

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

            Assert.IsNotNull(sm);

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

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

            ShardUpdate su = new ShardUpdate();

            su.Status = ShardStatus.Offline;

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

            Assert.IsNotNull(sUpdated);

            bool validationFailed = false;

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

            Assert.IsTrue(validationFailed);

            validationFailed = false;

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

            Assert.IsFalse(validationFailed);
        }
示例#22
0
        public static bool RangeShardMapExists <TKey>(string connectionString, string mapName)
        {
            var shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                connectionString,
                ShardMapManagerLoadPolicy.Lazy);

            RangeShardMap <TKey> _ = null;

            return(shardMapManager.TryGetRangeShardMap(mapName, out _));
        }
        public void LoadTestKillLSMConnections()
        {
            String databaseName = null;

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

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

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

                    // kill all connections for given shard location
                    try
                    {
                        using (SqlCommand cmd = new SqlCommand(
                                   string.Format(KillConnectionsForDatabaseQuery, databaseName),
                                   conn))
                        {
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (SqlException e)
                    {
                        //  233: A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)
                        // 6106: Process ID %d is not an active process ID.
                        // 6107: Only user processes can be killed
                        if ((e.Number != 233) && (e.Number != 6106) && (e.Number != 6107))
                        {
                            Assert.Fail("error number {0} with message {1}", e.Number, e.Message);
                        }
                    }
                }
            }
        }
示例#24
0
        public static void Main(string[] args)
        {
            try
            {
                // Parse command line arguments
                s_commandLine = new CommandLine(args);
                if (!s_commandLine.IsValid)
                {
                    s_commandLine.WriteUsage();
                    return;
                }

                // Get Shard Map Manager
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    GetConnectionString(), ShardMapManagerLoadPolicy.Eager);
                Console.WriteLine("Connected to Shard Map Manager");

                // Get Shard Map
                ShardMap map = smm.GetShardMap(s_commandLine.ShardMap);
                Console.WriteLine("Found {0} shards", map.GetShards().Count());

                // Create connection string for MultiShardConnection
                string connectionString = GetCredentialsConnectionString();

                // REPL
                Console.WriteLine();
                while (true)
                {
                    // Read command from console
                    string commandText = GetCommand();
                    if (commandText == null)
                    {
                        // Exit requested
                        break;
                    }

                    // Evaluate command
                    string output;
                    using (MultiShardConnection conn = new MultiShardConnection(map.GetShards(), connectionString))
                    {
                        output = ExecuteCommand(conn, commandText);
                    }

                    // Print output
                    Console.WriteLine(output);
                }
            }
            catch (Exception e)
            {
                // Print exception and exit
                Console.WriteLine(e);
                return;
            }
        }
示例#25
0
        public static ShardLocation GetShardLocationFromListMap <TKey>(string connectionString, string mapName, TKey key)
        {
            var shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(
                connectionString,
                ShardMapManagerLoadPolicy.Lazy);

            var listMap = shardMapManager.GetListShardMap <TKey>(mapName);
            var mapping = listMap.GetMappingForKey(key);

            return(mapping.Shard.Location);
        }
        public void GetShardMapManager_Success()
        {
            ShardMapManagerFactory.CreateSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerCreateMode.ReplaceExisting);

            foreach (ShardMapManagerLoadPolicy loadPolicy in Enum.GetValues(typeof(ShardMapManagerLoadPolicy)))
            {
                ShardMapManager smm1 = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    loadPolicy);
                Assert.IsNotNull(smm1);

                ShardMapManager smm2 = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    loadPolicy,
                    RetryBehavior.DefaultRetryBehavior);
                Assert.IsNotNull(smm2);

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

                if (sqlAuthLogin.Create())
                {
                    // Cover all the GetSqlShardMapManager overloads
                    ShardMapManager smm3 = ShardMapManagerFactory.GetSqlShardMapManager(
                        Globals.ShardMapManagerConnectionStringForSqlAuth,
                        Globals.ShardUserCredentialForSqlAuth(sqlAuthLogin.UniquifiedUserName),
                        loadPolicy,
                        RetryBehavior.DefaultRetryBehavior);
                    Assert.IsNotNull(smm3);

                    ShardMapManager smm4 = ShardMapManagerFactory.GetSqlShardMapManager(
                        Globals.ShardMapManagerConnectionStringForSqlAuth,
                        Globals.ShardUserCredentialForSqlAuth(sqlAuthLogin.UniquifiedUserName),
                        loadPolicy,
                        RetryBehavior.DefaultRetryBehavior,
                        null);
                    Assert.IsNotNull(smm4);

                    ShardMapManager smm5 = ShardMapManagerFactory.GetSqlShardMapManager(
                        Globals.ShardMapManagerConnectionStringForSqlAuth,
                        Globals.ShardUserCredentialForSqlAuth(sqlAuthLogin.UniquifiedUserName),
                        loadPolicy);
                    Assert.IsNotNull(smm5);

                    // Drop test login
                    sqlAuthLogin.Drop();
                }
                else
                {
                    Assert.Inconclusive("Failed to create sql login, test skipped");
                }
            }
        }
        private static ListShardMap <int> GetOrderShardMap()
        {
            var headConnectionString = Config.GetHeadDatabaseConnectionString();

            // The shard map is hosted on the "Head" database
            var manager = ShardMapManagerFactory.GetSqlShardMapManager(headConnectionString, ShardMapManagerLoadPolicy.Eager);

            // There shard map is identified by name
            var shardMap = manager.GetListShardMap <int>("OrderShardMap");

            return(shardMap);
        }
        public void LoadTestMergeRangesWithLock()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                RangeShardMap <int> rsm = smm.GetRangeShardMap <int>(ShardMapManagerLoadTests.s_rangeShardMapName);
                Assert.IsNotNull(rsm);

                IEnumerable <RangeMapping <int> > existingMappings = rsm.GetMappings(new Range <int>(MinMappingPoint, MaxMappingPoint));

                IQueryable <RangeMapping <int> > qr = Queryable.AsQueryable(existingMappings);

                // Find pair of adjacent mappings.
                var test = from a in qr
                           join b in qr on
                           new { a.Range.High.Value, a.StoreMapping.StoreShard.Id, a.StoreMapping.Status } equals
                new { b.Range.Low.Value, b.StoreMapping.StoreShard.Id, b.StoreMapping.Status }
                select new { a, b };

                if (test.Count() > 0)
                {
                    var t = test.First();

                    Debug.WriteLine("Trying to merge range mapping for key range ({0} - {1}) and ({2} - {3})", t.a.Range.Low.Value, t.a.Range.High.Value, t.b.Range.Low.Value, t.b.Range.High.Value);


                    MappingLockToken mappingLockTokenLeft = MappingLockToken.Create();
                    rsm.LockMapping(t.a, mappingLockTokenLeft);

                    MappingLockToken mappingLockTokenRight = MappingLockToken.Create();
                    rsm.LockMapping(t.b, mappingLockTokenLeft);

                    RangeMapping <int> rMerged = rsm.MergeMappings(t.a, t.b, mappingLockTokenLeft, mappingLockTokenRight);

                    Assert.IsNotNull(rMerged);

                    MappingLockToken storeMappingLockToken = rsm.GetMappingLockOwner(rMerged);
                    Assert.AreEqual(storeMappingLockToken, mappingLockTokenLeft, "Expected merged mapping lock id to equal left mapping id!");
                    rsm.UnlockMapping(rMerged, storeMappingLockToken);

                    storeMappingLockToken = rsm.GetMappingLockOwner(rMerged);
                    Assert.AreEqual(storeMappingLockToken, MappingLockToken.NoLock, "Expected merged mapping lock id to equal default mapping id after unlock!");
                }
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
        internal static ShardMap CreateAndGetTestShardMap()
        {
            ShardMap sm;

            ShardMapManagerFactory.CreateSqlShardMapManager(MultiShardTestUtils.ShardMapManagerConnectionString, ShardMapManagerCreateMode.ReplaceExisting);
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(MultiShardTestUtils.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy);

            sm = smm.CreateListShardMap <int>(s_testShardMapName);
            for (int i = 0; i < s_testDatabaseNames.Count; i++)
            {
                sm.CreateShard(GetTestShard(s_testDatabaseNames[i]));
            }
            return(sm);
        }
        /// <summary>
        /// Helper function to clean list and range shard maps.
        /// </summary>
        private static void CleanShardMapsHelper()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            // Remove all existing mappings from the list shard map.
            ListShardMap <DateTime> lsm;

            if (smm.TryGetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName, out lsm))
            {
                Assert.IsNotNull(lsm);

                foreach (PointMapping <DateTime> pm in lsm.GetMappings())
                {
                    PointMapping <DateTime> pmOffline = lsm.MarkMappingOffline(pm);
                    Assert.IsNotNull(pmOffline);
                    lsm.DeleteMapping(pmOffline);
                }

                // Remove all shards from list shard map
                foreach (Shard s in lsm.GetShards())
                {
                    lsm.DeleteShard(s);
                }
            }

            // Remove all existing mappings from the range shard map.
            RangeShardMap <DateTime> rsm;

            if (smm.TryGetRangeShardMap <DateTime>(DateTimeShardMapperTests.s_rangeShardMapName, out rsm))
            {
                Assert.IsNotNull(rsm);

                foreach (RangeMapping <DateTime> rm in rsm.GetMappings())
                {
                    MappingLockToken mappingLockToken = rsm.GetMappingLockOwner(rm);
                    rsm.UnlockMapping(rm, mappingLockToken);
                    RangeMapping <DateTime> rmOffline = rsm.MarkMappingOffline(rm);
                    Assert.IsNotNull(rmOffline);
                    rsm.DeleteMapping(rmOffline);
                }

                // Remove all shards from range shard map
                foreach (Shard s in rsm.GetShards())
                {
                    rsm.DeleteShard(s);
                }
            }
        }