A :class:`.LoadBalancingPolicy` wrapper that adds the ability to target a specific host first. If no host is set on the query, the child policy's query plan will be used as is.
| 1131 | |
| 1132 | |
| 1133 | class DefaultLoadBalancingPolicy(WrapperPolicy): |
| 1134 | """ |
| 1135 | A :class:`.LoadBalancingPolicy` wrapper that adds the ability to target a specific host first. |
| 1136 | |
| 1137 | If no host is set on the query, the child policy's query plan will be used as is. |
| 1138 | """ |
| 1139 | |
| 1140 | _cluster_metadata = None |
| 1141 | |
| 1142 | def populate(self, cluster, hosts): |
| 1143 | self._cluster_metadata = cluster.metadata |
| 1144 | self._child_policy.populate(cluster, hosts) |
| 1145 | |
| 1146 | def make_query_plan(self, working_keyspace=None, query=None): |
| 1147 | if query and query.keyspace: |
| 1148 | keyspace = query.keyspace |
| 1149 | else: |
| 1150 | keyspace = working_keyspace |
| 1151 | |
| 1152 | # TODO remove next major since execute(..., host=XXX) is now available |
| 1153 | addr = getattr(query, 'target_host', None) if query else None |
| 1154 | target_host = self._cluster_metadata.get_host(addr) |
| 1155 | |
| 1156 | child = self._child_policy |
| 1157 | if target_host and target_host.is_up: |
| 1158 | yield target_host |
| 1159 | for h in child.make_query_plan(keyspace, query): |
| 1160 | if h != target_host: |
| 1161 | yield h |
| 1162 | else: |
| 1163 | for h in child.make_query_plan(keyspace, query): |
| 1164 | yield h |
| 1165 | |
| 1166 | |
| 1167 | # TODO for backward compatibility, remove in next major |