|
| 1 | +Frequently Asked Questions |
| 2 | +========================== |
| 3 | + |
| 4 | +See also :doc:`cqlengine FAQ <cqlengine/faq>` |
| 5 | + |
| 6 | +Why do connections or IO operations timeout in my WSGI application? |
| 7 | +------------------------------------------------------------------- |
| 8 | +Depending on your application process model, it may be forking after driver Session is created. Most IO reactors do not handle this, and problems will manifest as timeouts. |
| 9 | + |
| 10 | +To avoid this, make sure to create sessions per process, after the fork. Using uWSGI and Flask for example: |
| 11 | + |
| 12 | +.. code-block:: python |
| 13 | +
|
| 14 | + from flask import Flask |
| 15 | + from uwsgidecorators import postfork |
| 16 | + from cassandra.cluster import Cluster |
| 17 | +
|
| 18 | + session = None |
| 19 | + prepared = None |
| 20 | +
|
| 21 | + @postfork |
| 22 | + def connect(): |
| 23 | + global session, prepared |
| 24 | + session = Cluster().connect() |
| 25 | + prepared = session.prepare("SELECT release_version FROM system.local WHERE key=?") |
| 26 | +
|
| 27 | + app = Flask(__name__) |
| 28 | +
|
| 29 | + @app.route('/') |
| 30 | + def server_version(): |
| 31 | + row = session.execute(prepared, ('local',))[0] |
| 32 | + return row.release_version |
| 33 | +
|
| 34 | +uWSGI provides a ``postfork`` hook you can use to create sessions and prepared statements after the child process forks. |
| 35 | + |
| 36 | +How do I trace a request? |
| 37 | +------------------------- |
| 38 | +Request tracing can be turned on for any request by setting ``trace=True`` in :meth:`.Session.execute_async`. View the results by waiting on the future, then :meth:`.ResponseFuture.get_query_trace`. |
| 39 | +Since tracing is done asynchronously to the request, this method polls until the trace is complete before querying data. |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + >>> future = session.execute_async("SELECT * FROM system.local", trace=True) |
| 44 | + >>> result = future.result() |
| 45 | + >>> trace = future.get_query_trace() |
| 46 | + >>> for e in trace.events: |
| 47 | + >>> print e.source_elapsed, e.description |
| 48 | +
|
| 49 | + 0:00:00.000077 Parsing select * from system.local |
| 50 | + 0:00:00.000153 Preparing statement |
| 51 | + 0:00:00.000309 Computing ranges to query |
| 52 | + 0:00:00.000368 Submitting range requests on 1 ranges with a concurrency of 1 (279.77142 rows per range expected) |
| 53 | + 0:00:00.000422 Submitted 1 concurrent range requests covering 1 ranges |
| 54 | + 0:00:00.000480 Executing seq scan across 1 sstables for (min(-9223372036854775808), min(-9223372036854775808)) |
| 55 | + 0:00:00.000669 Read 1 live and 0 tombstone cells |
| 56 | + 0:00:00.000755 Scanned 1 rows and matched 1 |
| 57 | +
|
| 58 | +``trace`` is a :class:`QueryTrace` object. |
| 59 | +
|
| 60 | +How do I determine the replicas for a query? |
| 61 | +---------------------------------------------- |
| 62 | +With prepared statements, the replicas are obtained by ``routing_key``, based on current cluster token metadata: |
| 63 | +
|
| 64 | +.. code-block:: python |
| 65 | +
|
| 66 | + >>> prepared = session.prepare("SELECT * FROM example.t WHERE key=?") |
| 67 | + >>> bound = prepared.bind((1,)) |
| 68 | + >>> replicas = cluster.metadata.get_replicas(bound.keyspace, bound.routing_key) |
| 69 | + >>> for h in replicas: |
| 70 | + >>> print h.address |
| 71 | + 127.0.0.1 |
| 72 | + 127.0.0.2 |
| 73 | +
|
| 74 | +``replicas`` is a list of :class:`Host` objects. |
| 75 | +
|
| 76 | +How does the driver manage request retries? |
| 77 | +------------------------------------------- |
| 78 | +By default, retries are managed by the :attr:`.Cluster.default_retry_policy` set on the session Cluster. It can also |
| 79 | +be specialized per statement by setting :attr:`.Statement.retry_policy`. |
| 80 | +
|
| 81 | +Retries are presently attempted on the same coordinator, but this may change in the future. |
| 82 | +
|
| 83 | +Please see :class:`.policies.RetryPolicy` for further details. |
0 commit comments