Cannot connect to cassandra db from my application

Hi everyone,

I have a go backend application where I try to connect to cassandra db and run some tests. So I need an instance to cassandra db. Referring to this: https://github.com/marketplace/actions/setup-cassandra-action I tried connecting to cassandra using the ip provided or the localhost and none of them seem to work.

An issue https://github.com/fabasoad/setup-cassandra-action/issues/1 is already created but seems like there is no response.

Thanks in advance.

Hi @vivekab,

Glad to see you in Github Community Forum!

This is not an official github action, it’s recommended to comment on the issue and @author, wait for the update.

In the meanwhile, you can try to use the official image and set it as a service(doc here), code sample as below:

jobs:
  cassandra:
    services:
      cassandra:
        image: cassandra
        ports:
          - 9042:9042
    runs-on: [ubuntu-latest]
    steps:
      - name: check
        run: |
          docker ps

Please refer to my workflow for more details.

Thanks.

Hi @weide-zhou,

Thanks on the update I tried connecting to the container as per your mentioned guidelines.

But still I have problem connecting to the container though.

when I tried running:

build:
    name: Build and Test
    services:
      cassandra:
        image: cassandra
        ports:
          - 9042:9042
    runs-on: [self-hosted, linux]
    steps:
    - name: check cassandra installation
      run: |
        docker ps
    - name: create containerId
      run: |
        echo "::set-env name=ContianerID::$(docker ps --format {{.ID}})"
    - name: Create products_keyspace
      run: |
        docker exec $ContianerID cqlsh --debug localhost:9024 --execute="CREATE KEYSPACE products_core WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"

It gives me connectivity error

Traceback (most recent call last):
  File "/opt/cassandra/bin/cqlsh.py", line 2443, in <module>
    main(*read_options(sys.argv[1:], os.environ))
  File "/opt/cassandra/bin/cqlsh.py", line 2421, in main
    encoding=options.encoding)
  File "/opt/cassandra/bin/cqlsh.py", line 485, in __init__
    load_balancing_policy=WhiteListRoundRobinPolicy([self.hostname]),
  File "/opt/cassandra/bin/../lib/cassandra-driver-internal-only-3.11.0-bb96859b.zip/cassandra-driver-3.11.0-bb96859b/cassandra/policies.py", line 417, in __init__
socket.gaierror: [Errno -2] Name or service not known

I also tried connecting without giving the host and the port but then its still an error.

docker exec $ContianerID cqlsh -e "CREATE KEYSPACE products_core WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"
  shell: /bin/bash -e {0}
  env:
    CASS_DB_KEYSPACE: products_core
    CASS_DB_HOST: cassandra
    CASS_DB_PORT: 9042
    CASS_DB_USER: cassandra
    CASS_DB_PASS: cassandra
    ContianerID: 2af6369a2338
Connection error: ('Unable to connect to any servers', {'127.0.0.1': error(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
##[error]Process completed with exit code 1.

Am I doing something wrong here? :slightly_frowning_face:

This is a common problem thats caused by trying to access the service (cassandra) before it is fully online and ready to handle connections. This is more a matter of how docker works when starting containers in the background, its async. We can add a simple healthcheck so that the rest of the steps downstream dont run until the service is healthy (if the container doesnt have any good tools in it for performing a healthcheck, you could always just sleep 30 on the host as a step, for eg)

Heres a quick sample I wrote:

on:
  push:
    paths:
    - .github/workflows/cassandra.yaml

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      cassandra:
        image: cassandra
        ports:
        - 9042:9042
        options: --health-cmd "cqlsh --debug" --health-interval 5s --health-retries 10
    steps:
    - run: docker ps
    - run: docker exec ${{ job.services.cassandra.id }} cqlsh --debug

Basically docker will run cqlsh --debug in the container every 5s for up to 10 times to update its “health”, and the runner polls the containers health to block before starting the rest of the job

@dakale and @weide-zhou That really helps, will be following the same from now on. Thanks.

@vivekab What .yml file are you using ATM? I copied the answer from @dakale but I am unable to create a keyspace or to query anything. This simple .yml file fails to run on github:

name: CasDB

on: push

env:
  CARGO_TERM_COLOR: always


jobs:
  test:
    runs-on: ubuntu-latest
    services:
      cassandra:
        image: cassandra
        ports:
          - 9042:9042
        options: --health-cmd "cqlsh --debug" --health-interval 5s --health-retries 10
    steps:
      - run: docker ps
      - run: docker exec ${{ job.services.cassandra.id }} cqlsh --debug localhost:9042 --execute="use x;"

Note: I would expect it to fail with a message like: keyspace x does not exists, but I get this error message:

Using CQL driver: <module ‘cassandra’ from ‘/opt/cassandra/bin/…/lib/cassandra-driver-internal-only-3.11.0-bb96859b.zip/cassandra-driver-3.11.0-bb96859b/cassandra/init.py’>
Using connect timeout: 5 seconds
Using ‘utf-8’ encoding
Using ssl: False
Traceback (most recent call last):
File “/opt/cassandra/bin/cqlsh.py”, line 2459, in
main(*read_options(sys.argv[1:], os.environ))
File “/opt/cassandra/bin/cqlsh.py”, line 2437, in main
encoding=options.encoding)
File “/opt/cassandra/bin/cqlsh.py”, line 485, in init
load_balancing_policy=WhiteListRoundRobinPolicy([self.hostname]),
File “/opt/cassandra/bin/…/lib/cassandra-driver-internal-only-3.11.0-bb96859b.zip/cassandra-driver-3.11.0-bb96859b/cassandra/policies.py”, line 417, in init
socket.gaierror: [Errno -2] Name or service not known
##[error]Process completed with exit code 1.

@vivekab could you share your final yml file? I also asked my question on https://stackoverflow.com/questions/63410396/setup-cassandra-container-in-github-actions-and-query and https://www.reddit.com/r/cassandra/comments/id8lgo/use_cassandra_with_github_actions/ but I don’t get any useful responses

Hi @Jasperav,

Here is the yaml that I use.

jobs:

  build:
    name: Build and Test
    services:
      cassandra:
        image: cassandra
        ports:
          - 9042:9042
        options: --name cassandra_test --health-cmd "cqlsh --debug" --health-interval 5s --health-retries 10
    runs-on: [self-hosted, linux]
    steps:
    - name: Create products_core keyspace
      run: |
        docker exec cassandra_test cqlsh -u cassandra -p cassandra -e "CREATE KEYSPACE test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"