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

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

    /// <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)中。当我通过传入正确的ShardMapManagershardMapName调用此方法时,将发生以下情况:

//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(如果已经存在)?

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

最佳答案

我这是一个愚蠢的错误。我正要对此问题进行解答,但想在这里张贴我的错误,因为这可能对其他人有帮助。

调用此函数的代码如下:

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


注意close,它告诉了分片密钥的类型。在我的情况下,UserId是int,因此long无法正常工作。

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

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

关于c# - ShardMapManager.TryGetRangeShardMap将不会检索RangeShardMap,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30779727/

10-09 06:00