|
| 1 | +Execution Profiles (experimental) |
| 2 | +================================= |
| 3 | + |
| 4 | +Execution profiles are an experimental API aimed at making it easier to execute requests in different ways within |
| 5 | +a single connected ``Session``. Execution profiles are being introduced the exploding number of configuration options, |
| 6 | +especially as the database platform evolves more complex workloads. |
| 7 | + |
| 8 | +The Execution Profile API is being introduced now, in an experimental capacity in order to take advantage of it in |
| 9 | +existing projects, and to guage interest and feedback in the community. For now, the legacy configuration remains |
| 10 | +intact, but legacy and Execution Profile APIs cannot be used simultaneously on the same client ``Cluster``. |
| 11 | + |
| 12 | +This document explains how Execution Profiles relate to existing settings, and shows how to use the new profiles for |
| 13 | +request execution. |
| 14 | + |
| 15 | +Mapping Legacy Parameters to Profiles |
| 16 | +------------------------------------- |
| 17 | + |
| 18 | +Execution profiles can inherit from :class:`.cluster.ExecutionProfile`, and currently provide the following options, |
| 19 | +previously input from the noted attributes: |
| 20 | + |
| 21 | +- load_balancing_policy - :attr:`.Cluster.load_balancing_policy` |
| 22 | +- request_timeout - :attr:`.Session.default_timeout`, optional :meth:`.Session.execute` parameter |
| 23 | +- retry_policy - :attr:`.Cluster.default_retry_policy`, optional :attr:`.Statement.retry_policy` attribute |
| 24 | +- consistency_level - :attr:`.Session.default_consistency_level`, optional :attr:`.Statement.consistency_level` attribute |
| 25 | +- serial_consistency_level - :attr:`.Session.default_serial_consistency_level`, optional :attr:`.Statement.serial_consistency_level` attribute |
| 26 | +- row_factory - :attr:`.Session.row_factory` attribute |
| 27 | + |
| 28 | +When using the new API, these parameters can be defined by instances of :class:`.cluster.ExecutionProfile`. |
| 29 | + |
| 30 | +Using Execution Profiles |
| 31 | +------------------------ |
| 32 | +Default |
| 33 | +~~~~~~~ |
| 34 | + |
| 35 | +.. code:: python |
| 36 | +
|
| 37 | + from cassandra.cluster import Cluster |
| 38 | + cluster = Cluster() |
| 39 | + session = cluster.connect() |
| 40 | + local_query = 'SELECT rpc_address FROM system.local' |
| 41 | + for _ in cluster.metadata.all_hosts(): |
| 42 | + print session.execute(local_query)[0] |
| 43 | +
|
| 44 | +
|
| 45 | +.. parsed-literal:: |
| 46 | +
|
| 47 | + Row(rpc_address='127.0.0.2') |
| 48 | + Row(rpc_address='127.0.0.1') |
| 49 | +
|
| 50 | +
|
| 51 | +The default execution profile is built from Cluster parameters and default Session attributes. This profile matches existing default |
| 52 | +parameters. |
| 53 | + |
| 54 | +Initializing cluster with profiles |
| 55 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 56 | + |
| 57 | +.. code:: python |
| 58 | +
|
| 59 | + from cassandra.cluster import ExecutionProfile |
| 60 | + from cassandra.policies import WhiteListRoundRobinPolicy |
| 61 | +
|
| 62 | + node1_profile = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1'])) |
| 63 | + node2_profile = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.2'])) |
| 64 | +
|
| 65 | + profiles = {'node1': node1_profile, 'node2': node2_profile} |
| 66 | + session = Cluster(execution_profiles=profiles).connect() |
| 67 | + for _ in cluster.metadata.all_hosts(): |
| 68 | + print session.execute(local_query, execution_profile='node1')[0] |
| 69 | +
|
| 70 | +
|
| 71 | +.. parsed-literal:: |
| 72 | +
|
| 73 | + Row(rpc_address='127.0.0.1') |
| 74 | + Row(rpc_address='127.0.0.1') |
| 75 | +
|
| 76 | +
|
| 77 | +.. code:: python |
| 78 | +
|
| 79 | + for _ in cluster.metadata.all_hosts(): |
| 80 | + print session.execute(local_query, execution_profile='node2')[0] |
| 81 | +
|
| 82 | +
|
| 83 | +.. parsed-literal:: |
| 84 | +
|
| 85 | + Row(rpc_address='127.0.0.2') |
| 86 | + Row(rpc_address='127.0.0.2') |
| 87 | +
|
| 88 | +
|
| 89 | +.. code:: python |
| 90 | +
|
| 91 | + for _ in cluster.metadata.all_hosts(): |
| 92 | + print session.execute(local_query)[0] |
| 93 | +
|
| 94 | +
|
| 95 | +.. parsed-literal:: |
| 96 | +
|
| 97 | + Row(rpc_address='127.0.0.2') |
| 98 | + Row(rpc_address='127.0.0.1') |
| 99 | +
|
| 100 | +Note that, even when custom profiles are injected, the default ``TokenAwarePolicy(DCAwareRoundRobinPolicy())`` is still |
| 101 | +present. To override the default, specify a policy with the :data:`~.cluster.EXEC_PROFILE_DEFAULT` key. |
| 102 | + |
| 103 | +.. code:: python |
| 104 | +
|
| 105 | + from cassandra.cluster import EXEC_PROFILE_DEFAULT |
| 106 | + profile = ExecutionProfile(request_timeout=30) |
| 107 | + cluster = Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: profile}) |
| 108 | +
|
| 109 | +
|
| 110 | +Adding named profiles |
| 111 | +~~~~~~~~~~~~~~~~~~~~~ |
| 112 | + |
| 113 | +New profiles can be added constructing from scratch, or deriving from default: |
| 114 | + |
| 115 | +.. code:: python |
| 116 | +
|
| 117 | + from cassandra.cluster import ExecutionProfile |
| 118 | + from cassandra.policies import WhiteListRoundRobinPolicy |
| 119 | + locked_execution = ExecutionProfile() |
| 120 | + locked_execution.load_balancing_policy = WhiteListRoundRobinPolicy(['127.0.0.1']) |
| 121 | + node1_profile = 'node1_whitelist' |
| 122 | + cluster.add_execution_profile(node1_profile, locked_execution) |
| 123 | + |
| 124 | + for _ in cluster.metadata.all_hosts(): |
| 125 | + print session.execute(local_query, execution_profile=node1_profile)[0] |
| 126 | +
|
| 127 | +
|
| 128 | +.. parsed-literal:: |
| 129 | +
|
| 130 | + Row(rpc_address='127.0.0.1') |
| 131 | + Row(rpc_address='127.0.0.1') |
| 132 | +
|
| 133 | +See :meth:`.Cluster.add_execution_profile` for details and optional parameters. |
| 134 | + |
| 135 | +Passing a profile instance without mapping |
| 136 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 137 | + |
| 138 | +We also have the ability to pass profile instances to be used for execution, but not added to the mapping: |
| 139 | + |
| 140 | +.. code:: python |
| 141 | +
|
| 142 | + from copy import copy |
| 143 | + from cassandra.query import tuple_factory |
| 144 | + |
| 145 | + tmp = copy(node1_profile) |
| 146 | + tmp.request_timeout = 100 |
| 147 | + tmp.row_factory = tuple_factory |
| 148 | + |
| 149 | + print session.execute(local_query, execution_profile=tmp)[0] |
| 150 | + print session.execute(local_query, execution_profile='node1')[0] |
| 151 | +
|
| 152 | +.. parsed-literal:: |
| 153 | +
|
| 154 | + ('127.0.0.1',) |
| 155 | + Row(rpc_address='127.0.0.1') |
| 156 | +
|
| 157 | +As shown above, the ``tmp`` profile shares a load balancing policy with one managed by the cluster. If this technique |
| 158 | +is not used, the application would need to initialize and maintain the policy state manually. |
| 159 | + |
0 commit comments