2

我正在尝试使用 Microsoft在此处提供的示例应用程序。这适用于创建新数据库及其分片等。但是当我尝试使用单个分片将其连接到现有数据库时它会失败。

它有一个名为的类ShardManagementUtils,它提供了以下RangeShardMap<T>从分片 azure 数据库中检索的方法:

    /// <summary>
    /// Creates a new Range Shard Map with the specified name, or gets the Range Shard Map if it already exists.
    /// </summary>
    public static RangeShardMap<T> CreateOrGetRangeShardMap<T>(ShardMapManager shardMapManager, string shardMapName)
    {
        // Try to get a reference to the Shard Map.
        RangeShardMap<T> shardMap;
        bool shardMapExists = shardMapManager.TryGetRangeShardMap(shardMapName, out shardMap);

        if (shardMapExists)
        {
            ConsoleUtils.WriteInfo("Shard Map {0} already exists", shardMap.Name);
        }
        else
        {
            // The Shard Map does not exist, so create it
            shardMap = shardMapManager.CreateRangeShardMap<T>(shardMapName);
            ConsoleUtils.WriteInfo("Created Shard Map {0}", shardMap.Name);
        }

        return shardMap;
    }

我的数据库中有一个分片,分片数据保存在Global Shard Map (GSM). 当我通过传入正确的ShardMapManagerand调用此方法时shardMapName,会发生以下情况:

//this return FALSE, it should not
bool shardMapExists = shardMapManager.TryGetRangeShardMap(shardMapName, out shardMap);

//Then this line throw exception that map with this name already exists
shardMap = shardMapManager.CreateRangeShardMap<T>(shardMapName);

抛出的异常如下:

An unhandled exception of type 'Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement.ShardManagementException' occurred in Microsoft.Azure.SqlDatabase.ElasticScale.Client.dll

Additional information: Shard map with name 'UserId' already exists in the store. Error occurred while executing stored procedure '__ShardManagement.spAddShardMapGlobal' for operation 'CreateRangeShardMap'.

所以我检查了__ShardManagement.ShardMapsGlobal表格,其中包含分片映射信息。那么如果第一行RangShardMap已经存在,为什么它不会检索呢?

我究竟做错了什么。非常感谢您的帮助。

4

1 回答 1

4

从我这边看,这是一个愚蠢的错误。我正要close回答这个问题,但想在这里发布我的错误,因为它可能对其他人有帮助。

调用这个函数的代码是这样的:

RangeShardMap<int> shardMap = ShardManagementUtils.CreateOrGetRangeShardMap<int>(shardMapManager, Configuration.ShardMapName);

注意int它告诉分片键的类型。long就我而言, UserId 是int行不通的。

只需将上述行更改为以下行即可解决问题:)

RangeShardMap<long> shardMap = ShardManagementUtils.CreateOrGetRangeShardMap<long>(shardMapManager, Configuration.ShardMapName);
于 2015-06-11T12:27:52.857 回答