Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ExecutionProfiles in Cassandra Driver #450

Open
jpowie01 opened this issue Sep 8, 2018 · 0 comments
Open

Switch to ExecutionProfiles in Cassandra Driver #450

jpowie01 opened this issue Sep 8, 2018 · 0 comments
Labels
Backend 🏭 Stuff related to Backend part. Enhancement 🌪 Things that improve our project. P2 Things with #2 Priority

Comments

@jpowie01
Copy link
Member

jpowie01 commented Sep 8, 2018

Current Behavior

Currently, Backend uses legacy and deprecated methods to handle Load Balancing and Timeouts. New version introduces Execution Profiles which should now be used.

Expected Behavior

Use new API for Cassandra Cluster definition.

Additional comment

Sadly, the newest version of Cassandra Driver has a bug that it logs a lot of deprecation warnings but new API does not work properly:
https://datastax-oss.atlassian.net/browse/PYTHON-1009

Please monitor and resolve this Issue only if above bug will be fixed and new version of Cassandra Driver will be released!

New code that may handle new API:

"""Definition of storage for MedTagger."""
from cassandra.cluster import Cluster, Session, NoHostAvailable, ExecutionProfile, EXEC_PROFILE_DEFAULT
from cassandra.policies import RoundRobinPolicy
from cassandra.cqlengine import connection
from cassandra.io.asyncorereactor import AsyncoreConnection
from cassandra.io.geventreactor import GeventConnection

from medtagger.config import AppConfiguration


MEDTAGGER_KEYSPACE = 'medtagger'

configuration = AppConfiguration()
addresses = configuration.get('cassandra', 'addresses', 'localhost').split(',')
port = configuration.getint('cassandra', 'port', 9042)
default_timeout = configuration.getint('cassandra', 'default_timeout', 20)
connect_timeout = configuration.getint('cassandra', 'connect_timeout', 20)


def create_session(use_gevent: bool = False) -> Session:
    """Create a Session object for above Cluster."""
    connection_class = GeventConnection if use_gevent else AsyncoreConnection
    profile = ExecutionProfile(load_balancing_policy=RoundRobinPolicy(), request_timeout=default_timeout)
    cluster = Cluster(addresses, port=port, execution_profiles={EXEC_PROFILE_DEFAULT: profile},
                      connection_class=connection_class)
    session = cluster.connect()
    return session


def create_connection(use_gevent: bool = False) -> None:
    """Create a Session object for above Cluster."""
    connection_class = GeventConnection if use_gevent else AsyncoreConnection
    profile = ExecutionProfile(load_balancing_policy=RoundRobinPolicy(), request_timeout=default_timeout)
    connection.setup(addresses, MEDTAGGER_KEYSPACE, port=port, execution_profiles={EXEC_PROFILE_DEFAULT: profile},
                     connection_class=connection_class)
    connection.get_session()


def is_alive() -> bool:
    """Return boolean information if Cassandra is alive or not."""
    try:
        create_session()
        return True
    except NoHostAvailable:
        return False
@jpowie01 jpowie01 added Enhancement 🌪 Things that improve our project. Backend 🏭 Stuff related to Backend part. P2 Things with #2 Priority Backlog 🌍 labels Sep 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend 🏭 Stuff related to Backend part. Enhancement 🌪 Things that improve our project. P2 Things with #2 Priority
Projects
None yet
Development

No branches or pull requests

1 participant