0

I have 2 cassandra clusters, my simple script is trying to read from one and write to another. As per the documentation here, I have a registered_connection (named) to the 2nd cluster and a default connection to the 1st cluster. I am able to query the different clusters using:

item = MyModel.filter(u_id=u_id).using(keyspace=KEYSPACE,connection=connection).first()

However when I try to write/save the item to another cluster using:

item.using(connection=connection).save()

it throws no error but also does not write to the Cluster. I verified this by querying the sink cluster. Am I missing something obvious from the documentation ?

PS: In the first scenario its the ModelQuerySet object that applies custom connection and in the save scenario its the Model object.

Segmented
  • 2,024
  • 2
  • 23
  • 44

1 Answers1

0

For someone who stumbles upon the same problem and needs a solution. Please keep in mind this is a workaround so if a proper solution pops up here please follow that.

This Solution is based on the assumption:

  1. You are using cassandra-driver==3.11.0 or lower, where connection registry is supported (Take a look here)
  2. You have 2 or more clusters to handle and you have registered all named connections though the above API (register_connection). Make one of the connections default connection.

A little background about your problem:

When you use a ModelSetQuery object with different connections, it exhibits expected behaviour. So if you have a query like:

MyModel.filter(u_id=u_id).first()

You could easily specify the keyspace and the connection for such a Query with the using method:

MyModel.filter(u_id=u_id).using(keyspace=KEYSPACE, connection=connection).first()

You will observe that however MyModel.using(connection=connection).save() does not work.

For the critically minded:

You will observe that underneath _execute_statement method in cassandra.cqlengine.query has the connection variable as None and even though you set with Model.__connection__ the model._get_connection() does not grab your connection name string. Just a Hint to get you started to look at the actual problem.

WORKAROUND SOLUTION: group your operations on each cluster together and before each cluster operation switch default connection to the appropriate cluster with connection.set_default_connection("cluster_connection_name")

So basically:

connection.set_default_connection("cluster1")
# operations and queries on cluster 1
....
connection.set_default_connection("cluster2")
# operations and queries on cluster 2
....

Hope this helps!

Segmented
  • 2,024
  • 2
  • 23
  • 44