prestodb / presto-python-client

Python DB-API client for Presto
Apache License 2.0
239 stars 87 forks source link

Support cookies in transactions #60

Closed ggreg closed 6 years ago

ggreg commented 6 years ago

Use same PrestoRequest instance to execute queries within the same transaction.

When an HTTP gateway is used to balance queries across multiple Presto coordinators, queries within the same transaction must go to the same coordinator. An HTTP cookie can support this behavior such as Set-Cookie: Presto-Gateway-Sticky={host}:{port};Version=1. The cookie is persisted by the requests.Session object referenced by PrestoRequest._http_session. Hence All queries should be sent with the same session data.

The behavior introduced by 30c1735 was not working because the Presto-Gateway-Sticky cookie was not in the Response object. It was already persisted in the requests.Session object.

I manually tested the execution of queries within the same transaction and will add an integration tests that will require a Presto HTTP gateway:

 with prestodb.dbapi.Connection(host=gateway_host, port=7778, user='ggreg', source='tests', catalog=catalog, schema=schema, http_scheme='https', isolation_level=prestodb.transaction.READ_COMMITTED, auth=auth) as conn:
    cur = conn.cursor()
    list(cur.execute('SELECT 1'))
    print(cur._query._stats['queryId'])
    list(cur.execute('SELECT 2'))
    print(cur._query._stats['queryId'])
    sleep(10)
    list(cur.execute('SELECT 3'))
    print(cur._query._stats['queryId'])
    sleep(60)
    list(cur.execute('SELECT 4'))
    print(cur._query._stats['queryId'])
    sleep(300)
    list(cur.execute('SELECT 5'))
    print(cur._query._stats['queryId'])