question

JS avatar image
JS asked bettina.swynnerton commented

Python workshop code unable to connect to Astra instance from Windows machine

When I run db_connection.py, I got error message as below: I followed the instruction in previous post about this problem, and confirmed my file path is correct in window explore. and also I tried to use double slash // or \\ , single quote, double quote, etc. in the path string. None of them work. I still cannot moving forward for later excercise. Please help. Thank you!

SECURE_CONNECT_BUNDLE = "C:\Jie Work\cassandra\secure-connect-jstest.zip"

Output:

runfile('C:/Jie Work/cassandra/Ex02_Connect_to_Cassandra.py', wdir='C:/Jie Work/cassandra')
========================================
Start exercise
Unable to connect to the metadata service at https://26502f4d-475b-401a-9bbc-63ab53023eaf-us-east1.db.astra.datastax.com:32279/metadata. Check the cluster status in the cloud console.
Failure
Closing connection (up to 10s)
Traceback (most recent call last):

File "<ipython-input-32-41f8bedfed6e>", line 1, in <module>
runfile('C:/Jie Work/cassandra/Ex02_Connect_to_Cassandra.py', wdir='C:/Jie Work/cassandra')

File "C:\Users\jie\AppData\Local\Continuum\anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)

File "C:\Users\jie\AppData\Local\Continuum\anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 93, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Jie Work/cassandra/Ex02_Connect_to_Cassandra.py", line 46, in <module>
connection.close()

NameError: name 'connection' is not defined
workshop
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

bettina.swynnerton avatar image
bettina.swynnerton answered bettina.swynnerton commented

Hi @JS,

the error says:

Unable to connect to the metadata service at https://26502f4d-475b-401a-9bbc-63ab53023eaf-us-east1.db.astra.datastax.com:32279/metadata. Check the cluster status in the cloud console.

That means that the secure bundle was definitely found, and the connection fails on the connection to the database itself.

I assume you have already verified the status of the Astra database and that it is running, and you have verified all the login details, and everything is correct.

The most likely cause of this error now is that the connection from your local machine to the Astra instance is a bit too slow for the connection timeouts that are set by default. We have seen this in a couple of instances when connecting from local machines, and sometimes from Gitpod.

Try to add more generous timeouts in the db_connection.py file where we define the connection:

class Connection:
    def __init__(self):
        self.secure_connect_bundle=SECURE_CONNECT_BUNDLE
        self.path_to_creds=''
        self.cluster = Cluster(
            cloud={
                'secure_connect_bundle': self.secure_connect_bundle,
                'init-query-timeout': 10,
                'connect_timeout': 10,
                'set-keyspace-timeout': 10
            },
            auth_provider=PlainTextAuthProvider(USERNAME, PASSWORD)
        )
        self.session = self.cluster.connect(KEYSPACE)
    def close(self):
        self.cluster.shutdown()
        self.session.shutdown()

We found that especially the 'init-query-timeout' default of 500ms is too tight in some cases. In the code above we have increased it to 10s.

Let us know if this gets you over this hurdle, your feedback is appreciated.

2 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

JS avatar image JS commented ·

thank you very much, Bettina. It works now.

1 Like 1 ·
bettina.swynnerton avatar image bettina.swynnerton ♦♦ JS commented ·

fabulous, thanks for the feedback!

0 Likes 0 ·