Esempio n. 1
0
        private static void CreateSchemaInfo(ShardMapManager smm, string shardMapName)
        {
            SchemaInfo schemaInfo = new SchemaInfo();

            schemaInfo.Add(new ShardedTableInfo("Companies", "TenantId"));
            schemaInfo.Add(new ShardedTableInfo("Professionals", "TenantId"));
            schemaInfo.Add(new ShardedTableInfo("Worksdone", "TenantId"));

            smm.GetSchemaInfoCollection().Add(shardMapName, schemaInfo);
        }
        /// <summary>
        /// Creates schema info for the schema defined in InitializeShard.sql.
        /// </summary>
        private static void CreateSchemaInfo(string shardMapName)
        {
            // Create schema info
            SchemaInfo schemaInfo = new SchemaInfo();

            schemaInfo.Add(new ReferenceTableInfo("Regions"));
            schemaInfo.Add(new ReferenceTableInfo("Products"));
            schemaInfo.Add(new ShardedTableInfo("Customers", "CustomerId"));
            schemaInfo.Add(new ShardedTableInfo("Orders", "CustomerId"));

            // Register it with the shard map manager for the given shard map name
            s_shardMapManager.GetSchemaInfoCollection().Add(shardMapName, schemaInfo);
        }
        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);
        }
        /// <summary>
        /// Creates a new Range Shard Map with the specified name, or gets the Range Shard Map if it already exists.
        /// </summary>
        private static RangeShardMap <int> CreateOrGetRangeShardMap(ShardMapManager shardMapManager, string shardMapName)
        {
            // Try to get a reference to the Shard Map.
            RangeShardMap <int> shardMap;
            bool shardMapExists = shardMapManager.TryGetRangeShardMap(shardMapName, out shardMap);

            if (!shardMapExists)
            {
                shardMap = shardMapManager.CreateRangeShardMap <int>(shardMapName);

                SchemaInfo schemaInfo = new SchemaInfo();
                schemaInfo.Add(new ShardedTableInfo("Votes", "Captain"));
                shardMapManager.GetSchemaInfoCollection().Add(shardMapName, schemaInfo);

                for (int i = 0; i < 4; i++)
                {
                    int low  = i * 25;
                    int high = low + 24;

                    CreateShard(shardMap, new Range <int>(low, high));
                }
            }

            return(shardMap);
        }
        public void TestUpdateSchemaInfo()
        {
            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("dbo", "ShardedTableName1", "ColumnName");

            si.Add(sti);

            ReferenceTableInfo rtmd = new ReferenceTableInfo("ReferenceTableName1");

            si.Add(rtmd);

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

            // Try updating schema info without adding it first.
            SchemaInfoException siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => siCollection.Replace(mdName, si));

            Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameDoesNotExist, siex.ErrorCode);

            siCollection.Add(mdName, si);

            SchemaInfo sdmdNew = new SchemaInfo();

            sdmdNew.Add(new ShardedTableInfo("dbo", "NewShardedTableName1", "NewColumnName"));
            sdmdNew.Add(new ReferenceTableInfo("NewReferenceTableName1"));

            siCollection.Replace(mdName, sdmdNew);

            SchemaInfo sdmdRead = siCollection.Get(mdName);

            Assert.IsTrue(AreEqual <ShardedTableInfo>(sdmdNew.ShardedTables, sdmdRead.ShardedTables));
            Assert.IsTrue(AreEqual <ReferenceTableInfo>(sdmdNew.ReferenceTables, sdmdRead.ReferenceTables));
        }
        public void SerializeCompatibility()
        {
            SchemaInfo schemaInfo = new SchemaInfo();

            schemaInfo.Add(new ReferenceTableInfo("r1", "r2"));
            schemaInfo.Add(new ShardedTableInfo("s1", "s2", "s3"));

            // Why is this slightly different from the XML in the DeserializeCompatibility test?
            // Because this is the exact formatting that we expect DataContractSerializer will create.
            string expectedSerializedSchemaInfo = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
  <ReferenceTableSet xmlns="""" i:type=""ArrayOfReferenceTableInfo"">
    <ReferenceTableInfo>
      <SchemaName>r1</SchemaName>
      <TableName>r2</TableName>
    </ReferenceTableInfo>
  </ReferenceTableSet>
  <ShardedTableSet xmlns="""" i:type=""ArrayOfShardedTableInfo"">
    <ShardedTableInfo>
      <SchemaName>s1</SchemaName>
      <TableName>s2</TableName>
      <KeyColumnName>s3</KeyColumnName>
    </ShardedTableInfo>
  </ShardedTableSet>
</Schema>";
            string actualSerializedSchemaInfo   = ToXml(schemaInfo);

            Assert.AreEqual(
                expectedSerializedSchemaInfo,
                actualSerializedSchemaInfo);

            // Deserialize it back as a sanity check
            SchemaInfo finalSchemaInfo = FromXml(actualSerializedSchemaInfo);

            AssertEqual(schemaInfo, finalSchemaInfo);
        }
        // Create schema info so that the split-merge service can be used to move data in sharded tables
        // and reference tables.
        public void CreateSchemaInfo(string shardMapName)
        {
            var schemaInfo = new SchemaInfo();

            schemaInfo.Add(new ReferenceTableInfo("SaleType"));
            schemaInfo.Add(new ReferenceTableInfo("LotStatus"));
            schemaInfo.Add(new ReferenceTableInfo("Location"));
            schemaInfo.Add(new ReferenceTableInfo("Country"));
            schemaInfo.Add(new ShardedTableInfo("Bid", "CountryId"));
            schemaInfo.Add(new ShardedTableInfo("Buyer", "CountryId"));
            schemaInfo.Add(new ShardedTableInfo("Lot", "CountryId"));
            schemaInfo.Add(new ShardedTableInfo("LotItem", "CountryId"));
            schemaInfo.Add(new ShardedTableInfo("Sale", "CountryId"));
            schemaInfo.Add(new ShardedTableInfo("Seller", "CountryId"));
            schemaInfo.Add(new ShardedTableInfo("Vehicle", "CountryId"));

            // Register it with the shard map manager for the given shard map name
            if (this.shardMapManager.GetSchemaInfoCollection().Any(s => s.Key == shardMapName))
            {
                return;
            }

            this.shardMapManager.GetSchemaInfoCollection().Add(shardMapName, schemaInfo);
        }
        public void TestAddAndLookupAndDeleteSchemaInfo()
        {
            ShardMapManagerFactory.CreateSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerCreateMode.ReplaceExisting);

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

            #region TestAddAndLookup

            SchemaInfoCollection siCollection = shardMapManager.GetSchemaInfoCollection();

            SchemaInfo si = new SchemaInfo();

            ShardedTableInfo stmd1 = new ShardedTableInfo("ShardedTableName1", "ColumnName");
            ShardedTableInfo stmd2 = new ShardedTableInfo("dbo", "ShardedTableName2", "ColumnName");

            si.Add(stmd1);
            si.Add(stmd2);

            Assert.AreEqual(2, si.ShardedTables.Count);

            ReferenceTableInfo rtmd1 = new ReferenceTableInfo("ReferenceTableName1");
            ReferenceTableInfo rtmd2 = new ReferenceTableInfo("dbo", "ReferenceTableName2");

            si.Add(rtmd1);
            si.Add(rtmd2);

            Assert.AreEqual(2, si.ReferenceTables.Count);
            // Add an existing sharded table again. Make sure it doesn't create duplicate entries.
            SchemaInfoException siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => si.Add(new ShardedTableInfo("ShardedTableName1", "ColumnName")));
            Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode);

            // Add an existing sharded table with a different key column name. This should fail too.
            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => si.Add(new ShardedTableInfo("ShardedTableName1", "ColumnName_Different")));
            Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode);

            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => si.Add(new ShardedTableInfo("dbo", "ShardedTableName2", "ColumnName_Different")));
            Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode);

            Assert.AreEqual(2, si.ShardedTables.Count);

            // Add an existing reference tables again. Make sure it doesn't create duplicate entries.
            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => si.Add(new ReferenceTableInfo("dbo", "ReferenceTableName2")));
            Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode);

            Assert.AreEqual(2, si.ReferenceTables.Count);

            // Now trying adding a reference table as a sharded table and vice versa. Both operations should fail.
            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => si.Add(new ShardedTableInfo("ReferenceTableName1", "ColumnName")));
            Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode);

            Assert.AreEqual(2, si.ShardedTables.Count);

            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => si.Add(new ReferenceTableInfo("dbo", "ShardedTableName2")));
            Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode);

            Assert.AreEqual(2, si.ReferenceTables.Count);

            // Try removing an existing table info and adding it back.
            si.Remove(stmd1);
            si.Add(stmd1);
            Assert.AreEqual(2, si.ShardedTables.Count);

            si.Remove(rtmd2);
            si.Add(rtmd2);
            Assert.AreEqual(2, si.ReferenceTables.Count);

            // Test with NULL inputs.
            ArgumentException arex = AssertExtensions.AssertThrows <ArgumentException>(
                () => si.Add((ShardedTableInfo)null));

            arex = AssertExtensions.AssertThrows <ArgumentException>(
                () => si.Add((ReferenceTableInfo)null));

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

            SchemaInfo sdmdRead = siCollection.Get(mdName);

            AssertEqual(si, sdmdRead);

            // Trying to add schema info with the same name again will result in a 'name conflict' exception.
            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => siCollection.Add(mdName, si));
            Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameConflict, siex.ErrorCode);

            #endregion

            #region TestLookup

            // Try looking up schema info with a non-existent name.
            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => siCollection.Get(mdName + "Fail"));
            Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameDoesNotExist, siex.ErrorCode);


            #endregion

            #region TestDelete

            // Try removing any of the recently created schema info.
            siCollection.Remove(mdName);

            // Lookup should fail on removed data.
            siex = AssertExtensions.AssertThrows <SchemaInfoException>(
                () => siCollection.Get(mdName));
            Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameDoesNotExist, siex.ErrorCode);

            #endregion
        }