get_connection method invoked with incorrect parameter in several methods of cqlengine\connection.py

Description

I defined several object mapper classes and attempt to perform queries via assigning obtained connection to the class global “__connection__“ attribute. But unfortunately exception occurred.

 

Below is my sample code to reproduce the issue.

 

from uuid import uuid4

from cassandra.cluster import Cluster

from cassandra.cqlengine import connection as cass_connection

from cassandra.cqlengine import CQLEngineException

from cassandra.cqlengine.models import Model

from cassandra.policies import RoundRobinPolicy

from cassandra.auth import PlainTextAuthProvider

from cassandra.cqlengine.columns import UUID, Text, Inet

 

class Host(Model):

__keyspace__ = "resource"

__table_name__ = "host"

hostid = UUID(primary_key=True, default=uuid4)

name = Text()

ip = Inet()

brand = Text()

 

def cassandra_setup(cass_user, cass_pwd, cass_port=9042, ssh_tunnel=None, host=None):

cass_conn = None

try:

# Cassandra initialization

auth_provider = PlainTextAuthProvider(username=cass_user, password=cass_pwd)

cluster = Cluster(contact_points=["127.0.0.1"] if ssh_tunnel else [host],

port=ssh_tunnel.local_bind_port if ssh_tunnel else cass_port,

load_balancing_policy=RoundRobinPolicy(),

auth_provider=auth_provider)

session = cluster.connect()

cass_conn_name = str(uuid4())

cass_conn = cass_connection.register_connection(name=cass_conn_name, session=session)

except CQLEngineException as cql_exp:

raise RuntimeError("Cassandra exception, info: {}.".format(cql_exp))

except Exception as ex:

print(ex)

finally:

return cass_conn

 

Host.__connection__ = cassandra_setup("", "", host="172.119.32.202")

for h in Host.objects.all():

print(h.name)

 

Below is the call trace:

Traceback (most recent call last):
File "D:/Project/ACB/get_all_classes.py", line 11, in <module>
for host in Host.objects.all():
File "D:\Python37\lib\site-packages\cassandra\cqlengine\query.py", line 515, in iter
self._execute_query()
File "D:\Python37\lib\site-packages\cassandra\cqlengine\query.py", line 472, in _execute_query
self._result_generator = (i for i in self._execute(self._select_query()))
File "D:\Python37\lib\site-packages\cassandra\cqlengine\query.py", line 404, in _execute
result = _execute_statement(self.model, statement, self._consistency, self._timeout, connection=connection)
File "D:\Python37\lib\site-packages\cassandra\cqlengine\query.py", line 1531, in _execute_statement
return conn.execute(s, params, timeout=timeout, connection=connection)
File "D:\Python37\lib\site-packages\cassandra\cqlengine\connection.py", line 331, in execute
conn = get_connection(connection)
File "D:\Python37\lib\site-packages\cassandra\cqlengine\connection.py", line 241, in get_connection
raise CQLEngineException("Connection name '{0}' doesn't exist in the registry.".format(name))
cassandra.cqlengine.CQLEngineException: Connection name '<cassandra.cqlengine.connection.Connection object at 0x000000000B956D88>' doesn't exist in the registry.

 

The root cause is:

in execute( )method, assign a connection instance to invoke get_connection( ) method, but the name of connection is expected as method argument.

Environment

None

Pull Requests

None

Activity

Show:
James Falcon
December 14, 2019, 3:08 AM

Sorry, I’m not sure I understand the problem. Is there a functional problem that causes an exception or unintended behavior? If so, can you specify what the expected behavior is vs the actual behavior happening with this code? Is the problem that the naming of the parameters are unclear? How is this code being used?

Cooper Yang
December 15, 2019, 8:00 PM
Edited

@James Falcon

Sorry for my unclear issue specification. Now I update it, you could reproduce the issue with the given sample codes.

James Falcon
December 16, 2019, 6:35 AM

Thanks, that helps me understand.

If you return cass_conn_name from your function, rather than cass_conn, then use it as follows:

Your code should work as you expect.

You can find more examples in our documentation here:

https://docs.datastax.com/en/developer/python-driver/3.20/object_mapper/ and
https://docs.datastax.com/en/developer/python-driver/3.20/cqlengine/connections/

Cooper Yang
December 16, 2019, 1:19 PM

@James Falcon

hmm, it works well.

Please close this ticket.

Not a Problem

Assignee

Unassigned

Reporter

Cooper Yang

Fix versions

None

Labels

Reproduced in

3.20.2

PM Priority

None

External issue ID

None

Doc Impact

None

Reviewer

None

Size

None

Pull Request

None

Affects versions

Priority

Major