6

I setup 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.

Mark Jones
  • 147
  • 2
  • 13

3 Answers3

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

Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
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

Simon Fontana Oscarsson
  • 2,114
  • 1
  • 17
  • 20
3

Based on the documentation, request_timeout is an attribute of ExecutionProfile class, and you can give an execution profile to the cluster constructor (this 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 èxecute_async, you have to specify the execution_profile name.

Martin
  • 31
  • 3