| 1 |
# Copyright DataStax, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
def initialize_registry(insights_registry): |
| 17 |
# This will be called from the cluster module, so we put all this behavior |
| 18 |
# in a function to avoid circular imports |
| 19 |
|
| 20 |
if insights_registry.initialized: |
| 21 |
return False |
| 22 |
|
| 23 |
from cassandra import ConsistencyLevel |
| 24 |
from cassandra.cluster import ( |
| 25 |
ExecutionProfile, GraphExecutionProfile, |
| 26 |
ProfileManager, ContinuousPagingOptions, |
| 27 |
EXEC_PROFILE_DEFAULT, EXEC_PROFILE_GRAPH_DEFAULT, |
| 28 |
EXEC_PROFILE_GRAPH_ANALYTICS_DEFAULT, |
| 29 |
EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT, |
| 30 |
_NOT_SET |
| 31 |
) |
| 32 |
from cassandra.datastax.graph import GraphOptions |
| 33 |
from cassandra.datastax.insights.registry import insights_registry |
| 34 |
from cassandra.datastax.insights.util import namespace |
| 35 |
from cassandra.policies import ( |
| 36 |
RoundRobinPolicy, |
| 37 |
DCAwareRoundRobinPolicy, |
| 38 |
TokenAwarePolicy, |
| 39 |
WhiteListRoundRobinPolicy, |
| 40 |
HostFilterPolicy, |
| 41 |
ConstantReconnectionPolicy, |
| 42 |
ExponentialReconnectionPolicy, |
| 43 |
RetryPolicy, |
| 44 |
SpeculativeExecutionPolicy, |
| 45 |
ConstantSpeculativeExecutionPolicy, |
| 46 |
WrapperPolicy |
| 47 |
) |
| 48 |
|
| 49 |
import logging |
| 50 |
|
| 51 |
log = logging.getLogger(__name__) |
| 52 |
|
| 53 |
@insights_registry.register_serializer_for(RoundRobinPolicy) |
| 54 |
def round_robin_policy_insights_serializer(policy): |
| 55 |
return {'type': policy.__class__.__name__, |
| 56 |
'namespace': namespace(policy.__class__), |
| 57 |
'options': {}} |
| 58 |
|
| 59 |
@insights_registry.register_serializer_for(DCAwareRoundRobinPolicy) |
| 60 |
def dc_aware_round_robin_policy_insights_serializer(policy): |
| 61 |
return {'type': policy.__class__.__name__, |
| 62 |
'namespace': namespace(policy.__class__), |
| 63 |
'options': {'local_dc': policy.local_dc, |
| 64 |
'used_hosts_per_remote_dc': policy.used_hosts_per_remote_dc} |
| 65 |
} |
| 66 |
|
| 67 |
@insights_registry.register_serializer_for(TokenAwarePolicy) |
| 68 |
def token_aware_policy_insights_serializer(policy): |
| 69 |
return {'type': policy.__class__.__name__, |
| 70 |
'namespace': namespace(policy.__class__), |
| 71 |
'options': {'child_policy': insights_registry.serialize(policy._child_policy, |
| 72 |
policy=True), |
| 73 |
'shuffle_replicas': policy.shuffle_replicas} |
| 74 |
} |
| 75 |
|
| 76 |
@insights_registry.register_serializer_for(WhiteListRoundRobinPolicy) |
| 77 |
def whitelist_round_robin_policy_insights_serializer(policy): |
| 78 |
return {'type': policy.__class__.__name__, |
| 79 |
'namespace': namespace(policy.__class__), |
| 80 |
'options': {'allowed_hosts': policy._allowed_hosts} |
| 81 |
} |
| 82 |
|
| 83 |
@insights_registry.register_serializer_for(HostFilterPolicy) |
| 84 |
def host_filter_policy_insights_serializer(policy): |
| 85 |
return { |
| 86 |
'type': policy.__class__.__name__, |
| 87 |
'namespace': namespace(policy.__class__), |
| 88 |
'options': {'child_policy': insights_registry.serialize(policy._child_policy, |
| 89 |
policy=True), |
| 90 |
'predicate': policy.predicate.__name__} |
| 91 |
} |
| 92 |
|
| 93 |
@insights_registry.register_serializer_for(ConstantReconnectionPolicy) |
| 94 |
def constant_reconnection_policy_insights_serializer(policy): |
| 95 |
return {'type': policy.__class__.__name__, |
| 96 |
'namespace': namespace(policy.__class__), |
| 97 |
'options': {'delay': policy.delay, |
| 98 |
'max_attempts': policy.max_attempts} |
| 99 |
} |
| 100 |
|
| 101 |
@insights_registry.register_serializer_for(ExponentialReconnectionPolicy) |
| 102 |
def exponential_reconnection_policy_insights_serializer(policy): |
| 103 |
return {'type': policy.__class__.__name__, |
| 104 |
'namespace': namespace(policy.__class__), |
| 105 |
'options': {'base_delay': policy.base_delay, |
| 106 |
'max_delay': policy.max_delay, |
| 107 |
'max_attempts': policy.max_attempts} |
| 108 |
} |
| 109 |
|
| 110 |
@insights_registry.register_serializer_for(RetryPolicy) |
| 111 |
def retry_policy_insights_serializer(policy): |
| 112 |
return {'type': policy.__class__.__name__, |
| 113 |
'namespace': namespace(policy.__class__), |
| 114 |
'options': {}} |
| 115 |
|
| 116 |
@insights_registry.register_serializer_for(SpeculativeExecutionPolicy) |
| 117 |
def speculative_execution_policy_insights_serializer(policy): |
| 118 |
return {'type': policy.__class__.__name__, |
| 119 |
'namespace': namespace(policy.__class__), |
| 120 |
'options': {}} |
| 121 |
|
| 122 |
@insights_registry.register_serializer_for(ConstantSpeculativeExecutionPolicy) |
| 123 |
def constant_speculative_execution_policy_insights_serializer(policy): |
| 124 |
return {'type': policy.__class__.__name__, |
| 125 |
'namespace': namespace(policy.__class__), |
| 126 |
'options': {'delay': policy.delay, |
| 127 |
'max_attempts': policy.max_attempts} |
| 128 |
} |
| 129 |
|
| 130 |
@insights_registry.register_serializer_for(WrapperPolicy) |
| 131 |
def wrapper_policy_insights_serializer(policy): |
| 132 |
return {'type': policy.__class__.__name__, |
| 133 |
'namespace': namespace(policy.__class__), |
| 134 |
'options': { |
| 135 |
'child_policy': insights_registry.serialize(policy._child_policy, |
| 136 |
policy=True) |
| 137 |
}} |
| 138 |
|
| 139 |
@insights_registry.register_serializer_for(ExecutionProfile) |
| 140 |
def execution_profile_insights_serializer(profile): |
| 141 |
return { |
| 142 |
'loadBalancing': insights_registry.serialize(profile.load_balancing_policy, |
| 143 |
policy=True), |
| 144 |
'retry': insights_registry.serialize(profile.retry_policy, |
| 145 |
policy=True), |
| 146 |
'readTimeout': profile.request_timeout, |
| 147 |
'consistency': ConsistencyLevel.value_to_name.get(profile.consistency_level, None), |
| 148 |
'serialConsistency': ConsistencyLevel.value_to_name.get(profile.serial_consistency_level, None), |
| 149 |
'continuousPagingOptions': (insights_registry.serialize(profile.continuous_paging_options) |
| 150 |
if (profile.continuous_paging_options is not None and |
| 151 |
profile.continuous_paging_options is not _NOT_SET) else |
| 152 |
None), |
| 153 |
'speculativeExecution': insights_registry.serialize(profile.speculative_execution_policy), |
| 154 |
'graphOptions': None |
| 155 |
} |
| 156 |
|
| 157 |
@insights_registry.register_serializer_for(GraphExecutionProfile) |
| 158 |
def graph_execution_profile_insights_serializer(profile): |
| 159 |
rv = insights_registry.serialize(profile, cls=ExecutionProfile) |
| 160 |
rv['graphOptions'] = insights_registry.serialize(profile.graph_options) |
| 161 |
return rv |
| 162 |
|
| 163 |
_EXEC_PROFILE_DEFAULT_KEYS = (EXEC_PROFILE_DEFAULT, |
| 164 |
EXEC_PROFILE_GRAPH_DEFAULT, |
| 165 |
EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT, |
| 166 |
EXEC_PROFILE_GRAPH_ANALYTICS_DEFAULT) |
| 167 |
|
| 168 |
@insights_registry.register_serializer_for(ProfileManager) |
| 169 |
def profile_manager_insights_serializer(manager): |
| 170 |
defaults = { |
| 171 |
# Insights's expected default |
| 172 |
'default': insights_registry.serialize(manager.profiles[EXEC_PROFILE_DEFAULT]), |
| 173 |
# remaining named defaults for driver's defaults, including duplicated default |
| 174 |
'EXEC_PROFILE_DEFAULT': insights_registry.serialize(manager.profiles[EXEC_PROFILE_DEFAULT]), |
| 175 |
'EXEC_PROFILE_GRAPH_DEFAULT': insights_registry.serialize(manager.profiles[EXEC_PROFILE_GRAPH_DEFAULT]), |
| 176 |
'EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT': insights_registry.serialize( |
| 177 |
manager.profiles[EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT] |
| 178 |
), |
| 179 |
'EXEC_PROFILE_GRAPH_ANALYTICS_DEFAULT': insights_registry.serialize( |
| 180 |
manager.profiles[EXEC_PROFILE_GRAPH_ANALYTICS_DEFAULT] |
| 181 |
) |
| 182 |
} |
| 183 |
other = { |
| 184 |
key: insights_registry.serialize(value) |
| 185 |
for key, value in manager.profiles.items() |
| 186 |
if key not in _EXEC_PROFILE_DEFAULT_KEYS |
| 187 |
} |
| 188 |
overlapping_keys = set(defaults) & set(other) |
| 189 |
if overlapping_keys: |
| 190 |
log.debug('The following key names overlap default key sentinel keys ' |
| 191 |
'and these non-default EPs will not be displayed in Insights ' |
| 192 |
': {}'.format(list(overlapping_keys))) |
| 193 |
|
| 194 |
other.update(defaults) |
| 195 |
return other |
| 196 |
|
| 197 |
@insights_registry.register_serializer_for(GraphOptions) |
| 198 |
def graph_options_insights_serializer(options): |
| 199 |
rv = { |
| 200 |
'source': options.graph_source, |
| 201 |
'language': options.graph_language, |
| 202 |
'graphProtocol': options.graph_protocol |
| 203 |
} |
| 204 |
updates = {k: v.decode('utf-8') for k, v in rv.items() |
| 205 |
if isinstance(v, bytes)} |
| 206 |
rv.update(updates) |
| 207 |
return rv |
| 208 |
|
| 209 |
@insights_registry.register_serializer_for(ContinuousPagingOptions) |
| 210 |
def continuous_paging_options_insights_serializer(paging_options): |
| 211 |
return { |
| 212 |
'page_unit': paging_options.page_unit, |
| 213 |
'max_pages': paging_options.max_pages, |
| 214 |
'max_pages_per_second': paging_options.max_pages_per_second, |
| 215 |
'max_queue_size': paging_options.max_queue_size |
| 216 |
} |
| 217 |
|
| 218 |
insights_registry.initialized = True |
| 219 |
return True |