How to set/override the client request timeout using the Cassandra Python driver?
Asked Answered
O

3

6

I have a simple script to insert a new record into a Cassandra database. It works fine on my local machine, but I am getting timeout errors from the client when I moved the database to a remote machine. How do I properly set the timeout for this driver? I have tried many things. I hacked the timeout in my IDE and got it to work without timing out, so I know for sure its just a timeout problem.

How I setup my Cluster:

profile = ExecutionProfile(request_timeout=100000)
self.cluster = Cluster([os.getenv('CASSANDRA_NODES', None)], auth_provider=auth_provider,
                       execution_profiles={EXEC_PROFILE_DEFAULT: profile})
connection.setup(hosts=[os.getenv('CASSANDRA_SEED', None)],
                 default_keyspace=os.getenv('KEYSPACE', None),
                 consistency=int(os.getenv('CASSANDRA_SESSION_CONSISTENCY', 1)), auth_provider=auth_provider,
                 connect_timeout=200)

session = self.cluster.connect()

The query I am trying to perform:

model = Model.create(buffer=_buffer, lock=False, version=self.version)

13..': 'Client request timeout. See Session.execute_async'}, last_host=54.213..

The record I'm inserting is 11mb, so I can understand there is a delay, just increasing the timeout should do it, but I can't seem to figure it out.

Oceanic answered 6/2, 2018 at 23:53 Comment(0)
K
11

The default request timeout is an attribute of the Session object (version 2.0.0 of the driver and later).

session = cluster.connect(keyspace)
session.default_timeout = 60

This is the simplest answer (no need to mess about with an execution profile), and I have confirmed that it works.

https://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Session

Krantz answered 28/3, 2019 at 11:23 Comment(1)
For those that need to use an execution profile, doing this on cassandra-driver 3.27 results in "Cannot set Session.default_timeout while using Configuration Profiles. Set this in a profile instead."Insectivore
A
5

Based on the documentation as of version 3.x of the driver, the request_timeout is an attribute of ExecutionProfile class, and you can give an execution profile to the cluster constructor (here is an example).

So, you can do:

from cassandra.cluster import Cluster
from cassandra.cluster import ExecutionProfile

execution_profil = ExecutionProfile(request_timeout=600)
profiles = {'node1': execution_profil}
cluster = Cluster([os.getenv('CASSANDRA_NODES', None)], execution_profiles=profiles)
session = cluster.connect()

session.execute('SELECT * FROM test', execution_profile='node1')

Important:
When you use execute or execute_async, you have to specify the execution_profile name.

Aculeate answered 25/2, 2019 at 15:30 Comment(0)
H
4

You can set request_timeout in the Cluster constructor:

self.cluster = Cluster([os.getenv('CASSANDRA_NODES', None)], 
                       auth_provider=auth_provider,
                       execution_profiles={EXEC_PROFILE_DEFAULT: profile},
                       request_timeout=10)

Reference: https://datastax.github.io/python-driver/api/cassandra/cluster.html

Hageman answered 7/2, 2018 at 7:1 Comment(3)
You cannot, that is an unexpected argument and it errors out.Oceanic
@MarkJones probably you're trying it on an older version? It works fine for me, and I even see the argument in the source.Glyco
As of cassandra-driver 3.27, it's been moved to ExecutionProfile: docs.datastax.com/en/developer/python-driver/3.28/api/cassandra/…Insectivore

© 2022 - 2024 — McMap. All rights reserved.