pythoncassandracassandra-driveramazon-keyspaces

Amazon Keyspaces sync_table() doesn't see a table immediately


I am new in cassandra-driver==3.25.0. I've created a model from Model and tried to sync it. The problem:

  1. I am calling sync_table
  2. I see that table starts creating in Keyspaces and the definition is correct
  3. BUT sync_table raises an exception KeyError {__table_name__} from this line of code table = cluster.metadata.keyspaces[ks_name].tables[raw_cf_name]
  4. I can call sync_table again and it is working fine after some time

So I am assuming it can some delay from Keyspaces, so do cassandra-driver has some sort of wait on sync table, or can you point to the documentation, please.

class Users(Model):
    __keyspace__ = 'aura'
    __table_name__ = 'users'
    __connection__ = 'aws_keyspace'

    user_id = columns.UUID(primary_key=True)
    tags = columns.Map(key_type=columns.Text(), value_type=columns.Text())

cluster = Cluster(...)
session = cluster.connect()
connection.register_connection('aws_keyspace', session=session, default=True)
sync_table(Users)

Full exception:

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "models.py", line 69, in <module>
    sync_table(AttachmentsByUsers)
  File "python3.6/site-packages/cassandra/cqlengine/management.py", line 190, in sync_table
    _sync_table(m, connection=connection)
  File "python3.6/site-packages/cassandra/cqlengine/management.py", line 274, in _sync_table
    table = cluster.metadata.keyspaces[ks_name].tables[raw_cf_name]
KeyError: 'users'

Solution

  • Amazon Keyspaces creates tables in a non-blocking way. Since its asynchronous, you may not see the table immediately. This link has guidance on how to monitor system tables to check when a table is available. https://docs.aws.amazon.com/keyspaces/latest/devguide/functional-differences.html#functional-differences.table-keyspace-management

    The following query will show the status of your table in CQL

    SELECT keyspace_name, table_name, status FROM system_schema_mcs.tables
    WHERE keyspace_name = 'mykeyspace' AND table_name = 'mytable';
    

    is the table is still being created the output of the query will look like this:

    keyspace_name table_name status
    mykeyspace mytable CREATING

    If table was successfully created and is active the output of the query looks like this:

    keyspace_name table_name status
    mykeyspace mytable ACTIVE