Search code examples
pythongoogle-cloud-platformgoogle-cloud-spanner

spanner read with timestamp fails


I am trying to read from a google Spanner table with a timestamp, using the code below.

import datetime as dt
from google.cloud.spanner.client import Client

proj = 'my_project'
inst = 'my_instance'
db = 'my_database'

datetime_now = dt.datetime.now()
with Client(proj).instance(inst).database(db).snapshot(read_timestamp=datetime_now) as snapshot: # this fails
#with Client(proj).instance(inst).database(db).snapshot() as snapshot: # this works
    result = snapshot.execute_sql('some SQL query')
    for row in result:
        print row

If I set a timestamp in the "snapshot" call, this returns the error message below:

grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.DEADLINE_EXCEEDED, Deadline Exceeded)>

while, if I just call "[...].snapshot()", the table is correctly read.

Is this an error in my script, or an installation problem?

Edit: just noticed I was not on the latest version of the python spanner API. After updating from 0.26 to 0.28, same thing happens except the script goes on forever without returning anything, rather than raising an error.


Solution

  • After some research, the solution (in case someone else runs into a similar issue):

    datetime_now = dt.datetime.now() (when run locally) returns the current system time. If this is in the future compared to the database time (UTC), the read fails or hangs. Using datetime_now = dt.datetime.utcnow() fixes this problem, though this is probably not required if the script is executed on the cloud.