ShardMapManager.TryGetRangeShardMap wont retrieve the RangeShardMap

974 views Asked by At

I am trying to play around with the sample app provided by Microsoft here. This works fine for creating a new database and its shards etc. But it fails when i try to connect it to an existing DB with a single shard.

It has a class named ShardManagementUtils which provides the following method to retrieve RangeShardMap<T> from a sharded azure database:

    /// <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;
    }

My database has one shard in it and sharding data is kept in Global Shard Map (GSM). When i call this method with by passing in correct ShardMapManager and shardMapName, following happens:

//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);

Exception being thrown is as follows:

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'.

So I checked the __ShardManagement.ShardMapsGlobal table and it contains shard map info in it. Then why would the first line not retrieve RangShardMap if it already exists?

What am I doing wrong. Your help is much appreciated.

1

There are 1 answers

2
Aamir On BEST ANSWER

This was a silly mistake from my side. I was about to close this question but thought to post my mistake here as it might be helpful to someone else.

The code to call this function was like this:

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

Notice the int which tells the type of sharding key. In my case, UserId is long so int would not work.

Just changing the above line to following resolved the issue :)

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