Bug #75287 Python does not work with HASH sahrding in MySQL Fabric
Submitted: 22 Dec 2014 13:44 Modified: 24 Feb 2015 16:07
Reporter: Ted Wennmark Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S1 (Critical)
Version:2.0 OS:Any
Assigned to: Peeyush Gupta CPU Architecture:Any

[22 Dec 2014 13:44] Ted Wennmark
Description:
When setting up MySQL Fabric to use HASH sharding Python program fails as seen below:

ted@ted-PORTEGE-Z30-A:/opt/MySQL-fabricII/src$ python fabric-demoII.py
Traceback (most recent call last):
  File "fabric-demoII.py", line 68, in <module>
    add_employee(conn, x, "John"+":"+str(x), "Doe")
  File "fabric-demoII.py", line 10, in add_employee
    cur = conn.cursor()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/fabric/connection.py", line 1222, in cursor
    self._connect()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/fabric/connection.py", line 1158, in _connect
    mode=props['mode'])
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/fabric/connection.py", line 744, in get_shard_server
    self.get_sharding_information((table,), database)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/fabric/connection.py", line 719, in get_sharding_information
    row.group_id, row.global_group)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/fabric/caching.py", line 194, in sharding_cache_table
    fabric_uuid=fabric_uuid)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/fabric/caching.py", line 88, in __init__
    self.add_partition(shard.key, shard.group)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/fabric/caching.py", line 112, in add_partition
    self.shard_type
ValueError: Unsupported sharding type HASH

I downloaded latest version 1.5.3 and modified (removed else clause as seen below) in /usr/local/lib/python2.7/distpackages/mysql/connector/fabric/caching.py:

>     def add_partition(self, key, group):
>         """Add sharding information for a group"""
>         if self.shard_type == 'RANGE':
>             key = int(key)
>         elif self.shard_type == 'RANGE_DATETIME':
>             try:
>                 if ':' in key:
>                     key = datetime.strptime(key, "%Y-%m-%d %H:%M:%S")
>                 else:
>                     key = datetime.strptime(key, "%Y-%m-%d").date()
>             except:
>                 raise ValueError(
>                     "RANGE_DATETIME key could not be parsed, was: {0}".format(
>                         key
>                     ))
>         elif self.shard_type == 'RANGE_STRING':
>             pass
> #        else:
> #            raise ValueError("Unsupported sharding type {0}".format(
> #                self.shard_type
> #            ))
>         self.partitioning[key] = {
>             'group': group,
>         }
>         self.reset_ttl()

And now Python + HASH sharding works with fabric ;)

Perhaps add clause:
>    elif self.shard_type == 'HASH:
>        pass
and then print Unsuported sharding type ....

This worked in older verions when there was no else branch failing if value was HASH (or any other value) in function add_partition:

This is from: mysql-utilities-1.4.4/mysql/connector/fabric/caching.py
>     def add_partition(self, key, group):
>         """Add sharding information for a group"""
>         if self.shard_type == 'RANGE':
>             key = int(key)
>         self.partitioning[key] = {
>             'group': group,
>         }
>         self.reset_ttl()

How to repeat:
Send me a email and I'll share test scrips for triggering this problem.

Suggested fix:
Perhaps add clause:

    elif self.shard_type == 'HASH:
        pass

in file caching.py when controling supported sharding methods and after this print Unsuported sharding type ....
[8 Jan 2015 11:16] Geert Vanderkelen
Thanks for this bug report. Caching should indeed not fail for HASH sharding.
[24 Feb 2015 16:07] Paul DuBois
Noted in 2.0.4 changelog.

HASH sharding for Fabric failed.
[30 Mar 2015 15:58] Paul DuBois
Noted in 2.1.2 changelog.