def test_profile_pool_management(self):
        """
        Tests that changes to execution profiles correctly impact our cluster's pooling

        @since 3.5
        @jira_ticket PYTHON-569
        @expected_result pools should be correctly updated as EP's are added and removed

        @test_category config_profiles
        """

        node1 = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
        node2 = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.2']))
        with Cluster(execution_profiles={
                EXEC_PROFILE_DEFAULT: node1,
                'node2': node2
        }) as cluster:
            session = cluster.connect(wait_for_all_pools=True)
            pools = session.get_pool_state()
            # there are more hosts, but we connected to the ones in the lbp aggregate
            self.assertGreater(len(cluster.metadata.all_hosts()), 2)
            self.assertEqual(set(h.address for h in pools),
                             set(('127.0.0.1', '127.0.0.2')))

            # dynamically update pools on add
            node3 = ExecutionProfile(
                load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.3']))
            cluster.add_execution_profile('node3', node3)
            pools = session.get_pool_state()
            self.assertEqual(set(h.address for h in pools),
                             set(('127.0.0.1', '127.0.0.2', '127.0.0.3')))
Пример #2
0
    def test_add_profile_timeout(self):
        """
        Tests that EP Timeouts are honored.

        @since 3.5
        @jira_ticket PYTHON-569
        @expected_result EP timeouts should override defaults

        @test_category config_profiles
        """

        node1 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
        with Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: node1}) as cluster:
            session = cluster.connect(wait_for_all_pools=True)
            pools = session.get_pool_state()
            self.assertGreater(len(cluster.metadata.all_hosts()), 2)
            self.assertEqual(set(h.address for h in pools), set(('127.0.0.1',)))

            node2 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.2']))

            max_retry_count = 10
            for i in range(max_retry_count):
                start = time.time()
                try:
                    self.assertRaises(cassandra.OperationTimedOut, cluster.add_execution_profile,
                                      'profile_{0}'.format(i),
                                      node2, pool_wait_timeout=sys.float_info.min)
                    break
                except AssertionError:
                    end = time.time()
                    self.assertAlmostEqual(start, end, 1)
            else:
                raise Exception("add_execution_profile didn't timeout after {0} retries".format(max_retry_count))
    def test_add_profile_timeout(self):
        """
        Tests that EP Timeouts are honored.

        @since 3.5
        @jira_ticket PYTHON-569
        @expected_result EP timeouts should override defaults

        @test_category config_profiles
        """

        node1 = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
        with Cluster(
                execution_profiles={EXEC_PROFILE_DEFAULT: node1}) as cluster:
            session = cluster.connect(wait_for_all_pools=True)
            pools = session.get_pool_state()
            self.assertGreater(len(cluster.metadata.all_hosts()), 2)
            self.assertEqual(set(h.address for h in pools), set(
                ('127.0.0.1', )))

            node2 = ExecutionProfile(
                load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.2']))
            self.assertRaises(cassandra.OperationTimedOut,
                              cluster.add_execution_profile,
                              'node2',
                              node2,
                              pool_wait_timeout=0.0000001)
Пример #4
0
    def test_graph_profile(self):
        """
            Test verifying various aspects of graph config properties.

            @since 1.0.0
            @jira_ticket PYTHON-570

            @test_category dse graph
            """
        generate_classic(self.session)
        # Create variou execution policies
        exec_dif_factory = GraphExecutionProfile(
            row_factory=single_object_row_factory)
        exec_dif_factory.graph_options.graph_name = self.graph_name
        exec_dif_lbp = GraphExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
        exec_dif_lbp.graph_options.graph_name = self.graph_name
        exec_bad_lbp = GraphExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.2']))
        exec_dif_lbp.graph_options.graph_name = self.graph_name
        exec_short_timeout = GraphExecutionProfile(
            request_timeout=1,
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
        exec_short_timeout.graph_options.graph_name = self.graph_name

        # Add a single exection policy on cluster creation
        local_cluster = Cluster(
            protocol_version=PROTOCOL_VERSION,
            execution_profiles={"exec_dif_factory": exec_dif_factory})
        local_session = local_cluster.connect()
        rs1 = self.session.execute_graph('g.V()')
        rs2 = local_session.execute_graph('g.V()',
                                          execution_profile='exec_dif_factory')

        # Verify default and non default policy works
        self.assertFalse(isinstance(rs2[0], Vertex))
        self.assertTrue(isinstance(rs1[0], Vertex))
        # Add other policies validate that lbp are honored
        local_cluster.add_execution_profile("exec_dif_ldp", exec_dif_lbp)
        local_session.execute_graph('g.V()', execution_profile="exec_dif_ldp")
        local_cluster.add_execution_profile("exec_bad_lbp", exec_bad_lbp)
        with self.assertRaises(NoHostAvailable):
            local_session.execute_graph('g.V()',
                                        execution_profile="exec_bad_lbp")

        # Try with missing EP
        with self.assertRaises(ValueError):
            local_session.execute_graph('g.V()',
                                        execution_profile='bad_exec_profile')

        # Validate that timeout is honored
        local_cluster.add_execution_profile("exec_short_timeout",
                                            exec_short_timeout)
        with self.assertRaises(OperationTimedOut):
            local_session.execute_graph(
                'java.util.concurrent.TimeUnit.MILLISECONDS.sleep(2000L);',
                execution_profile='exec_short_timeout')
Пример #5
0
    def test_hosts_with_hostname(self):
        hosts = ['localhost']
        policy = WhiteListRoundRobinPolicy(hosts)
        host = Host("127.0.0.1", SimpleConvictionPolicy)
        policy.populate(None, [host])

        qplan = list(policy.make_query_plan())
        self.assertEqual(sorted(qplan), [host])

        self.assertEqual(policy.distance(host), HostDistance.LOCAL)
Пример #6
0
    def get_connection(self):
        """ Returns Cassandra session """
        auth_provider = None

        if self.ssl:
            ssl_context = SSLContext(PROTOCOL_TLSv1)
            ssl_context.load_verify_locations(path_cert)
            ssl_context.verify_mode = CERT_REQUIRED
            #ssl_options = {
            #    'ca_certs' : self.ssl,
            #    'ssl_version' : PROTOCOL_TLSv1_2
            #}
        else:
            ssl_context = None

        if (self.username and self.password):
            auth_provider = PlainTextAuthProvider(self.username, self.password)

        node1_profile = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(
                [self.endpoint_name]))
        profiles = {'node1': node1_profile}
        cluster = Cluster([self.endpoint_name],
                          port=self.port,
                          auth_provider=auth_provider,
                          ssl_context=ssl_context,
                          control_connection_timeout=360,
                          execution_profiles=profiles)
        session = cluster.connect()
        return session
Пример #7
0
    def test_white_list(self):
        use_singledc()
        keyspace = 'test_white_list'

        cluster = Cluster(('127.0.0.2',), load_balancing_policy=WhiteListRoundRobinPolicy((IP_FORMAT % 2,)),
                          protocol_version=PROTOCOL_VERSION, topology_event_refresh_window=0,
                          status_event_refresh_window=0)
        session = cluster.connect()
        self._wait_for_nodes_up([1, 2, 3])

        create_schema(cluster, session, keyspace)
        self._insert(session, keyspace)
        self._query(session, keyspace)

        self.coordinator_stats.assert_query_count_equals(self, 1, 0)
        self.coordinator_stats.assert_query_count_equals(self, 2, 12)
        self.coordinator_stats.assert_query_count_equals(self, 3, 0)

        # white list policy should not allow reconnecting to ignored hosts
        force_stop(3)
        self._wait_for_nodes_down([3])
        self.assertFalse(cluster.metadata._hosts[IP_FORMAT % 3].is_currently_reconnecting())

        self.coordinator_stats.reset_counts()
        force_stop(2)
        self._wait_for_nodes_down([2])

        try:
            self._query(session, keyspace)
            self.fail()
        except NoHostAvailable:
            pass
        finally:
            cluster.shutdown()
Пример #8
0
    def test_white_list(self):
        use_singledc()
        keyspace = 'test_white_list'

        cluster = Cluster(('127.0.0.2', ),
                          load_balancing_policy=WhiteListRoundRobinPolicy(
                              (IP_FORMAT % 2, )))
        session = cluster.connect()
        wait_for_up(cluster, 1, wait=False)
        wait_for_up(cluster, 2, wait=False)
        wait_for_up(cluster, 3)

        create_schema(session, keyspace)
        self._insert(session, keyspace)
        self._query(session, keyspace)

        self.coordinator_stats.assert_query_count_equals(self, 1, 0)
        self.coordinator_stats.assert_query_count_equals(self, 2, 12)
        self.coordinator_stats.assert_query_count_equals(self, 3, 0)

        self.coordinator_stats.reset_counts()
        decommission(2)
        wait_for_down(cluster, 2, wait=True)

        try:
            self._query(session, keyspace)
            self.fail()
        except NoHostAvailable:
            pass
Пример #9
0
class DuplicateRpcTest(unittest.TestCase):

    load_balancing_policy = WhiteListRoundRobinPolicy(['127.0.0.1'])

    def setUp(self):
        self.cluster = Cluster(protocol_version=PROTOCOL_VERSION, load_balancing_policy=self.load_balancing_policy)
        self.session = self.cluster.connect()
        self.session.execute("UPDATE system.peers SET rpc_address = '127.0.0.1' WHERE peer='127.0.0.2'")

    def tearDown(self):
        self.session.execute("UPDATE system.peers SET rpc_address = '127.0.0.2' WHERE peer='127.0.0.2'")
        self.cluster.shutdown()

    def test_duplicate(self):
        """
        Test duplicate RPC addresses.

        Modifies the system.peers table to make hosts have the same rpc address. Ensures such hosts are filtered out and a message is logged

        @since 3.4
        @jira_ticket PYTHON-366
        @expected_result only one hosts' metadata will be populated

        @test_category metadata
        """
        mock_handler = MockLoggingHandler()
        logger = logging.getLogger(cassandra.cluster.__name__)
        logger.addHandler(mock_handler)
        test_cluster = self.cluster = Cluster(protocol_version=PROTOCOL_VERSION, load_balancing_policy=self.load_balancing_policy)
        test_cluster.connect()
        warnings = mock_handler.messages.get("warning")
        self.assertEqual(len(warnings), 1)
        self.assertTrue('multiple' in warnings[0])
        logger.removeHandler(mock_handler)
Пример #10
0
 def setUp(self):
     self.defaultInFlight = Connection.max_in_flight
     Connection.max_in_flight = 2
     self.cluster = Cluster(protocol_version=PROTOCOL_VERSION,
                            load_balancing_policy=WhiteListRoundRobinPolicy(
                                ['127.0.0.1']))
     self.session = self.cluster.connect()
    def test_node_busy(self):
        """ Verify that once TCP buffer is full, queries continue to get re-routed to other nodes """
        start_and_prime_singledc()
        profile = ExecutionProfile(load_balancing_policy=RoundRobinPolicy())
        cluster = Cluster(
            protocol_version=PROTOCOL_VERSION,
            compression=False,
            execution_profiles={EXEC_PROFILE_DEFAULT: profile},
        )
        session = cluster.connect(wait_for_all_pools=True)
        self.addCleanup(cluster.shutdown)

        query = session.prepare("INSERT INTO table1 (id) VALUES (?)")

        prime_request(PauseReads(dc_id=0, node_id=0))

        blocked_profile = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(["127.0.0.1"]))
        cluster.add_execution_profile('blocked_profile', blocked_profile)

        # Fill our blocked node's tcp buffer until we get a busy exception
        self._fill_buffers(session,
                           query,
                           expected_blocked=1,
                           execution_profile='blocked_profile')

        # Now that our send buffer is completely full on one node,
        # verify queries get re-routed to other nodes and queries complete successfully
        for i in range(1000):
            session.execute(query, [str(i)])
Пример #12
0
    def get_lb_policy(policy_name: str, policy_args: Dict[str, Any]) -> Policy:
        """
        Creates load balancing policy.

        :param policy_name: Name of the policy to use.
        :param policy_args: Parameters for the policy.
        """
        if policy_name == 'DCAwareRoundRobinPolicy':
            local_dc = policy_args.get('local_dc', '')
            used_hosts_per_remote_dc = int(policy_args.get('used_hosts_per_remote_dc', 0))
            return DCAwareRoundRobinPolicy(local_dc, used_hosts_per_remote_dc)

        if policy_name == 'WhiteListRoundRobinPolicy':
            hosts = policy_args.get('hosts')
            if not hosts:
                raise Exception('Hosts must be specified for WhiteListRoundRobinPolicy')
            return WhiteListRoundRobinPolicy(hosts)

        if policy_name == 'TokenAwarePolicy':
            allowed_child_policies = (
                'RoundRobinPolicy',
                'DCAwareRoundRobinPolicy',
                'WhiteListRoundRobinPolicy',
            )
            child_policy_name = policy_args.get('child_load_balancing_policy', 'RoundRobinPolicy')
            child_policy_args = policy_args.get('child_load_balancing_policy_args', {})
            if child_policy_name not in allowed_child_policies:
                return TokenAwarePolicy(RoundRobinPolicy())
            else:
                child_policy = CassandraHook.get_lb_policy(child_policy_name, child_policy_args)
                return TokenAwarePolicy(child_policy)

        # Fallback to default RoundRobinPolicy
        return RoundRobinPolicy()
Пример #13
0
    def test_max_in_flight(self):
        """ Verify we don't exceed max_in_flight when borrowing connections or sending heartbeats """
        Connection.max_in_flight = 50
        start_and_prime_singledc()
        profile = ExecutionProfile(request_timeout=1, load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
        cluster = Cluster(
            protocol_version=PROTOCOL_VERSION,
            compression=False,
            execution_profiles={EXEC_PROFILE_DEFAULT: profile},
            idle_heartbeat_interval=.1,
            idle_heartbeat_timeout=.1,
        )
        session = cluster.connect(wait_for_all_pools=True)
        self.addCleanup(cluster.shutdown)

        query = session.prepare("INSERT INTO table1 (id) VALUES (?)")

        prime_request(PauseReads())

        futures = []
        # + 50 because simulacron doesn't immediately block all queries
        for i in range(Connection.max_in_flight + 50):
            futures.append(session.execute_async(query, ['a']))

        prime_request(ResumeReads())

        for future in futures:
            # We're veryfing we don't get an assertion error from Connection.get_request_id,
            # so skip any valid errors
            try:
                future.result()
            except OperationTimedOut:
                pass
            except NoHostAvailable:
                pass
Пример #14
0
    def test_white_list(self):
        use_singledc()
        keyspace = 'test_white_list'

        cluster = Cluster(('127.0.0.2',),
            load_balancing_policy=WhiteListRoundRobinPolicy((IP_FORMAT % 2,)),
            protocol_version=PROTOCOL_VERSION)
        session = cluster.connect()
        wait_for_up(cluster, 1, wait=False)
        wait_for_up(cluster, 2, wait=False)
        wait_for_up(cluster, 3)

        create_schema(session, keyspace)
        self._insert(session, keyspace)
        self._query(session, keyspace)

        self.coordinator_stats.assert_query_count_equals(self, 1, 0)
        self.coordinator_stats.assert_query_count_equals(self, 2, 12)
        self.coordinator_stats.assert_query_count_equals(self, 3, 0)

        # white list policy should not allow reconnecting to ignored hosts
        force_stop(3)
        wait_for_down(cluster, 3)
        self.assertFalse(cluster.metadata._hosts[IP_FORMAT % 3].is_currently_reconnecting())

        self.coordinator_stats.reset_counts()
        force_stop(2)
        time.sleep(10)

        try:
            self._query(session, keyspace)
            self.fail()
        except NoHostAvailable:
            pass
Пример #15
0
    def get_session(self, hosts):
        """
        We select a host to connect to. If we have no connections to one of the hosts
        yet then we select this host, else we pick the one with the smallest number
        of jobs.

        :return: An ExportSession connected to the chosen host.
        """
        new_hosts = [h for h in hosts if h not in self.hosts_to_sessions]
        if new_hosts:
            host = new_hosts[0]
            new_cluster = Cluster(
                contact_points=(host, ),
                port=self.port,
                cql_version=self.cql_version,
                protocol_version=self.protocol_version,
                auth_provider=self.auth_provider,
                ssl_options=sslhandling.ssl_settings(host, self.config_file)
                if self.ssl else None,
                load_balancing_policy=TokenAwarePolicy(
                    WhiteListRoundRobinPolicy(hosts)),
                default_retry_policy=ExpBackoffRetryPolicy(self),
                compression=None,
                executor_threads=max(2, self.csv_options['jobs'] / 2))

            session = ExportSession(new_cluster, self)
            self.hosts_to_sessions[host] = session
            return session
        else:
            host = min(hosts, key=lambda h: self.hosts_to_sessions[h].jobs)
            session = self.hosts_to_sessions[host]
            session.add_job()
            return session
Пример #16
0
 def test_whitelist_round_robin_policy(self):
     self.assertEqual(
         insights_registry.serialize(WhiteListRoundRobinPolicy(['127.0.0.3'])),
         {'namespace': 'cassandra.policies',
          'options': {'allowed_hosts': ('127.0.0.3',)},
          'type': 'WhiteListRoundRobinPolicy'}
     )
Пример #17
0
    def __init__(self, ip_addresses, cassandra_config):
        self._ip_addresses = ip_addresses
        self._auth_provider = None
        self._ssl_context = None
        self._cassandra_config = cassandra_config

        if cassandra_config.cql_username is not None and cassandra_config.cql_password is not None:
            auth_provider = PlainTextAuthProvider(
                username=cassandra_config.cql_username,
                password=cassandra_config.cql_password)
            self._auth_provider = auth_provider

        if cassandra_config.certfile is not None and cassandra_config.usercert is not None and \
           cassandra_config.userkey is not None:
            ssl_context = SSLContext(PROTOCOL_TLSv1)
            ssl_context.load_verify_locations(cassandra_config.certfile)
            ssl_context.verify_mode = CERT_REQUIRED
            ssl_context.load_cert_chain(certfile=cassandra_config.usercert,
                                        keyfile=cassandra_config.userkey)
            self._ssl_context = ssl_context

        load_balancing_policy = WhiteListRoundRobinPolicy(ip_addresses)
        self._execution_profiles = {
            'local':
            ExecutionProfile(load_balancing_policy=load_balancing_policy)
        }
    def __init__(self, keyspace, table):
        self.keyspace = keyspace
        self.table_name = table
        self.profile = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']),
            retry_policy=DowngradingConsistencyRetryPolicy(),
            consistency_level=ConsistencyLevel.QUORUM,
            request_timeout=20,
            row_factory=dict_factory)
        self.cluster = Cluster(
            execution_profiles={EXEC_PROFILE_DEFAULT: self.profile})

        # Checking if keyspace exists or not
        # noinspection PyBroadException
        try:
            self.session = self.cluster.connect('d3')
        except Exception as e:
            self.session = self.cluster.connect()
            self.session.execute(
                "CREATE KEYSPACE d3 WITH REPLICATION = {'class':'SimpleStrategy','replication_factor':3} AND durable_writes = 'true';"
            )
            self.session.set_keyspace('d3')

        # checking if the table exists or not
        if self.table_name not in self.cluster.metadata.keyspaces['d3'].tables:
            self.session.execute(
                f"CREATE TABLE {self.table_name} (uuid text , key text , data text , PRIMARY KEY ((uuid,key),data));"
            )
Пример #19
0
    def get_lb_policy(policy_name, policy_args):
        policies = {
            'RoundRobinPolicy': RoundRobinPolicy,
            'DCAwareRoundRobinPolicy': DCAwareRoundRobinPolicy,
            'WhiteListRoundRobinPolicy': WhiteListRoundRobinPolicy,
            'TokenAwarePolicy': TokenAwarePolicy,
        }

        if not policies.get(policy_name) or policy_name == 'RoundRobinPolicy':
            return RoundRobinPolicy()

        if policy_name == 'DCAwareRoundRobinPolicy':
            local_dc = policy_args.get('local_dc', '')
            used_hosts_per_remote_dc = int(policy_args.get('used_hosts_per_remote_dc', 0))
            return DCAwareRoundRobinPolicy(local_dc, used_hosts_per_remote_dc)

        if policy_name == 'WhiteListRoundRobinPolicy':
            hosts = policy_args.get('hosts')
            if not hosts:
                raise Exception('Hosts must be specified for WhiteListRoundRobinPolicy')
            return WhiteListRoundRobinPolicy(hosts)

        if policy_name == 'TokenAwarePolicy':
            allowed_child_policies = ('RoundRobinPolicy',
                                      'DCAwareRoundRobinPolicy',
                                      'WhiteListRoundRobinPolicy',)
            child_policy_name = policy_args.get('child_load_balancing_policy',
                                                'RoundRobinPolicy')
            child_policy_args = policy_args.get('child_load_balancing_policy_args', {})
            if child_policy_name not in allowed_child_policies:
                return TokenAwarePolicy(RoundRobinPolicy())
            else:
                child_policy = CassandraHook.get_lb_policy(child_policy_name,
                                                           child_policy_args)
                return TokenAwarePolicy(child_policy)
Пример #20
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     addresses = [a.strip() for a in self.host.split(',')]
     self.client = Cluster(
         addresses,
         protocol_version=4,
         load_balancing_policy=WhiteListRoundRobinPolicy(addresses))
Пример #21
0
    def _initialize_engine(self,
                           db_engine=None,
                           db_host=None,
                           db_name=None,
                           db_username=None,
                           db_password=None):
        """Initialize DB engine."""
        if not _extra_supports['cassandra']:
            raise ImportError('Cassandra-related modules could not be loaded.',
                              'Make sure you installed extra modules')

        # Load settings from environment variables
        engine = db_engine if db_engine is not None else os.environ.get('PYDTK_TIME_SERIES_DB_ENGINE', None)
        username = db_username if db_username is not None else os.environ.get('PYDTK_TIME_SERIES_DB_USERNAME', None)
        password = db_password if db_password is not None else os.environ.get('PYDTK_TIME_SERIES_DB_PASSWORD', None)
        host = db_host if db_host is not None else os.environ.get('PYDTK_TIME_SERIES_DB_HOST', None)
        database = db_name if db_name is not None else os.environ.get('PYDTK_TIME_SERIES_DB_DATABASE', None)

        # Load default settings from config file
        engine = engine if engine is not None else self.db_defaults.engine
        username = username if username is not None else self.db_defaults.username
        password = password if password is not None else self.db_defaults.password
        host = host if host is not None else self.db_defaults.host
        database = database if database is not None else self.db_defaults.database

        assert engine == 'cassandra'

        # Substitute
        self._config.current_db = {
            'engine': engine,
            'host': host,
            'username': username,
            'password': password,
            'database': database,
        }

        # Connect
        hostname = host
        hostport = 9042
        if ':' in host:
            hostname, hostport = host.split(':')

        auth_provider = PlainTextAuthProvider(username=username, password=password)
        cluster = Cluster(contact_points=[hostname],
                          port=int(hostport),
                          connect_timeout=int(os.environ.get('PYDTK_CASSANDRA_CONNECT_TIMEOUT', 60)),
                          auth_provider=auth_provider,
                          load_balancing_policy=WhiteListRoundRobinPolicy([hostname]))

        # Define pandas factory function
        def pandas_factory(colnames, rows):
            return pd.DataFrame(rows, columns=colnames)

        session = cluster.connect(database)
        session.row_factory = pandas_factory
        session.default_fetch_size = None

        self._engine = cluster
        self._session = session
Пример #22
0
    def exclusive_cql_connection(self, node, keyspace=None, user=None,
                                 password=None, compression=True, protocol_version=None, port=None, ssl_opts=None, **kwargs):

        node_ip = get_ip_from_node(node)
        wlrr = WhiteListRoundRobinPolicy([node_ip])

        return self._create_session(node, keyspace, user, password, compression,
                                    protocol_version, port=port, ssl_opts=ssl_opts, load_balancing_policy=wlrr, **kwargs)
Пример #23
0
    def cql_connection(self, node, keyspace=None, user=None,
                       password=None, compression=True, protocol_version=None,
                       port=None, ssl_opts=None):

        wlrr = WhiteListRoundRobinPolicy(self.db_cluster.get_node_public_ips())
        return self._create_session(node, keyspace, user, password,
                                    compression, protocol_version, wlrr,
                                    port=port, ssl_opts=ssl_opts)
Пример #24
0
 def setUp(self):
     contact_point = ['127.0.0.2']
     self.cluster = Cluster(
         contact_points=contact_point,
         metrics_enabled=True,
         protocol_version=PROTOCOL_VERSION,
         load_balancing_policy=WhiteListRoundRobinPolicy(contact_point),
         default_retry_policy=FallthroughRetryPolicy())
     self.session = self.cluster.connect("test3rf", wait_for_all_pools=True)
Пример #25
0
    def cql_connection_exclusive(self, node, keyspace=None, user=None,
                                 password=None, compression=True,
                                 protocol_version=None, port=None,
                                 ssl_opts=None):

        wlrr = WhiteListRoundRobinPolicy([node.public_ip_address])
        return self._create_session(node, keyspace, user, password,
                                    compression, protocol_version, wlrr,
                                    port=port, ssl_opts=ssl_opts)
Пример #26
0
    def __init__(self, ip_addresses, *, username=None, password=None):
        self._ip_addresses = ip_addresses

        auth_provider = PlainTextAuthProvider(username=username, password=password) if username and password else None
        self._auth_provider = auth_provider

        load_balancing_policy = WhiteListRoundRobinPolicy(ip_addresses)
        self._execution_profiles = {
            'local': ExecutionProfile(load_balancing_policy=load_balancing_policy)
        }
    def test_profile_load_balancing(self):
        """
        Tests that profile load balancing policies are honored.

        @since 3.5
        @jira_ticket PYTHON-569
        @expected_result Execution Policy should be used when applicable.

        @test_category config_profiles
        """
        query = "select release_version from system.local"
        node1 = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
        with Cluster(execution_profiles={'node1': node1}) as cluster:
            session = cluster.connect(wait_for_all_pools=True)

            # default is DCA RR for all hosts
            expected_hosts = set(cluster.metadata.all_hosts())
            queried_hosts = set()
            for _ in expected_hosts:
                rs = session.execute(query)
                queried_hosts.add(rs.response_future._current_host)
            self.assertEqual(queried_hosts, expected_hosts)

            # by name we should only hit the one
            expected_hosts = set(h for h in cluster.metadata.all_hosts()
                                 if h.address == '127.0.0.1')
            queried_hosts = set()
            for _ in cluster.metadata.all_hosts():
                rs = session.execute(query, execution_profile='node1')
                queried_hosts.add(rs.response_future._current_host)
            self.assertEqual(queried_hosts, expected_hosts)

            # use a copied instance and override the row factory
            # assert last returned value can be accessed as a namedtuple so we can prove something different
            named_tuple_row = rs[0]
            self.assertIsInstance(named_tuple_row, tuple)
            self.assertTrue(named_tuple_row.release_version)

            tmp_profile = copy(node1)
            tmp_profile.row_factory = tuple_factory
            queried_hosts = set()
            for _ in cluster.metadata.all_hosts():
                rs = session.execute(query, execution_profile=tmp_profile)
                queried_hosts.add(rs.response_future._current_host)
            self.assertEqual(queried_hosts, expected_hosts)
            tuple_row = rs[0]
            self.assertIsInstance(tuple_row, tuple)
            with self.assertRaises(AttributeError):
                tuple_row.release_version

            # make sure original profile is not impacted
            self.assertTrue(
                session.execute(query,
                                execution_profile='node1')[0].release_version)
    def init(self, config):
        log = logging.getLogger(__name__)
        log.info("*** STARTING DOMS INITIALIZATION ***")

        domsconfig = configparser.SafeConfigParser()
        domsconfig.read(DomsInitializer._get_config_files('domsconfig.ini'))
        domsconfig = self.override_config(domsconfig, config)

        cassHost = domsconfig.get("cassandra", "host")
        cassPort = domsconfig.get("cassandra", "port")
        cassUsername = domsconfig.get("cassandra", "username")
        cassPassword = domsconfig.get("cassandra", "password")
        cassKeyspace = domsconfig.get("cassandra", "keyspace")
        cassDatacenter = domsconfig.get("cassandra", "local_datacenter")
        cassVersion = int(domsconfig.get("cassandra", "protocol_version"))
        cassPolicy = domsconfig.get("cassandra", "dc_policy")
        try:
            cassCreateKeyspaceGranted = domsconfig.get(
                "cassandra", "create_keyspace_granted")
        except configparser.NoOptionError:
            cassCreateKeyspaceGranted = "True"

        log.info("Cassandra Host(s): %s" % (cassHost))
        log.info("Cassandra Keyspace: %s" % (cassKeyspace))
        log.info("Cassandra Datacenter: %s" % (cassDatacenter))
        log.info("Cassandra Protocol Version: %s" % (cassVersion))
        log.info("Cassandra DC Policy: %s" % (cassPolicy))

        if cassPolicy == 'DCAwareRoundRobinPolicy':
            dc_policy = DCAwareRoundRobinPolicy(cassDatacenter)
            token_policy = TokenAwarePolicy(dc_policy)
        elif cassPolicy == 'WhiteListRoundRobinPolicy':
            token_policy = WhiteListRoundRobinPolicy([cassHost])

        if cassUsername and cassPassword:
            auth_provider = PlainTextAuthProvider(username=cassUsername,
                                                  password=cassPassword)
        else:
            auth_provider = None

        with Cluster([host for host in cassHost.split(',')],
                     port=int(cassPort),
                     load_balancing_policy=token_policy,
                     protocol_version=cassVersion,
                     auth_provider=auth_provider) as cluster:
            session = cluster.connect()

            if cassCreateKeyspaceGranted in ["True", "true"]:
                self.createKeyspace(session, cassKeyspace)
            else:
                session.set_keyspace(cassKeyspace)

            self.createTables(session)
Пример #29
0
 def test_only_connects_to_subset(self):
     only_connect_hosts = {"127.0.0.1", "127.0.0.2"}
     white_list = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(only_connect_hosts))
     cluster = Cluster(execution_profiles={"white_list": white_list})
     #cluster = Cluster(load_balancing_policy=WhiteListRoundRobinPolicy(only_connect_hosts))
     session = cluster.connect(wait_for_all_pools=True)
     queried_hosts = set()
     for _ in range(10):
         response = session.execute('SELECT * from system.local', execution_profile="white_list")
         queried_hosts.update(response.response_future.attempted_hosts)
     queried_hosts = set(host.address for host in queried_hosts)
     self.assertEqual(queried_hosts, only_connect_hosts)
    def __init__(self):
        self.profile = ExecutionProfile(
            load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']),
            retry_policy=NeverRetryPolicy(),
            consistency_level=ConsistencyLevel.ONE,
            serial_consistency_level=ConsistencyLevel.LOCAL_SERIAL,
            request_timeout=15,
            row_factory=tuple_factory)

        self.cluster = Cluster(
            port=9042, execution_profiles={EXEC_PROFILE_DEFAULT: self.profile})
        self.session = self.cluster.connect('records', wait_for_all_pools=True)
        logging.info('WRITE Cluster connection is created')
Пример #31
0
def test_cassandra_row_estimator_init():
    endpoint_name='127.0.0.1'
    port=9042
    auth_provider = None
    ssl_context=None
    auth_provider = PlainTextAuthProvider('cassandra', 'cassandra')
    node1_profile = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy([endpoint_name]))
    profiles = {'node1': node1_profile}
    cluster = Cluster([endpoint_name], port=port ,auth_provider=auth_provider, ssl_context=ssl_context, control_connection_timeout=360, execution_profiles=profiles)
    session = cluster.connect()
    session.execute("CREATE KEYSPACE IF NOT EXISTS cassandra_row_estimator WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 }")
    session.execute("CREATE TABLE IF NOT EXISTS cassandra_row_estimator.test (a int PRIMARY KEY, b text)")
    params = [1, 'This is a simple test']
    session.execute("INSERT INTO cassandra_row_estimator.test (a, b) VALUES (%s, %s)", params)