1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include "execution_profile.hpp"
18 
19 using namespace datastax;
20 using namespace datastax::internal::core;
21 
22 extern "C" {
23 
cass_execution_profile_new()24 CassExecProfile* cass_execution_profile_new() {
25   ExecutionProfile* profile = new ExecutionProfile();
26   return CassExecProfile::to(profile);
27 }
28 
cass_execution_profile_free(CassExecProfile * profile)29 void cass_execution_profile_free(CassExecProfile* profile) { delete profile->from(); }
30 
cass_execution_profile_set_request_timeout(CassExecProfile * profile,cass_uint64_t timeout_ms)31 CassError cass_execution_profile_set_request_timeout(CassExecProfile* profile,
32                                                      cass_uint64_t timeout_ms) {
33   profile->set_request_timeout(timeout_ms);
34   return CASS_OK;
35 }
36 
cass_execution_profile_set_consistency(CassExecProfile * profile,CassConsistency consistency)37 CassError cass_execution_profile_set_consistency(CassExecProfile* profile,
38                                                  CassConsistency consistency) {
39   profile->set_consistency(consistency);
40   return CASS_OK;
41 }
42 
cass_execution_profile_set_serial_consistency(CassExecProfile * profile,CassConsistency serial_consistency)43 CassError cass_execution_profile_set_serial_consistency(CassExecProfile* profile,
44                                                         CassConsistency serial_consistency) {
45   profile->set_serial_consistency(serial_consistency);
46   return CASS_OK;
47 }
48 
cass_execution_profile_set_load_balance_round_robin(CassExecProfile * profile)49 CassError cass_execution_profile_set_load_balance_round_robin(CassExecProfile* profile) {
50   profile->set_load_balancing_policy(new RoundRobinPolicy());
51   return CASS_OK;
52 }
53 
54 CassError
cass_execution_profile_set_load_balance_dc_aware(CassExecProfile * profile,const char * local_dc,unsigned used_hosts_per_remote_dc,cass_bool_t allow_remote_dcs_for_local_cl)55 cass_execution_profile_set_load_balance_dc_aware(CassExecProfile* profile, const char* local_dc,
56                                                  unsigned used_hosts_per_remote_dc,
57                                                  cass_bool_t allow_remote_dcs_for_local_cl) {
58   if (local_dc == NULL) {
59     return CASS_ERROR_LIB_BAD_PARAMS;
60   }
61   return cass_execution_profile_set_load_balance_dc_aware_n(
62       profile, local_dc, SAFE_STRLEN(local_dc), used_hosts_per_remote_dc,
63       allow_remote_dcs_for_local_cl);
64 }
65 
cass_execution_profile_set_load_balance_dc_aware_n(CassExecProfile * profile,const char * local_dc,size_t local_dc_length,unsigned used_hosts_per_remote_dc,cass_bool_t allow_remote_dcs_for_local_cl)66 CassError cass_execution_profile_set_load_balance_dc_aware_n(
67     CassExecProfile* profile, const char* local_dc, size_t local_dc_length,
68     unsigned used_hosts_per_remote_dc, cass_bool_t allow_remote_dcs_for_local_cl) {
69   if (local_dc == NULL || local_dc_length == 0) {
70     return CASS_ERROR_LIB_BAD_PARAMS;
71   }
72   profile->set_load_balancing_policy(new DCAwarePolicy(
73       String(local_dc, local_dc_length), used_hosts_per_remote_dc, !allow_remote_dcs_for_local_cl));
74   return CASS_OK;
75 }
76 
cass_execution_profile_set_token_aware_routing(CassExecProfile * profile,cass_bool_t enabled)77 CassError cass_execution_profile_set_token_aware_routing(CassExecProfile* profile,
78                                                          cass_bool_t enabled) {
79   profile->set_token_aware_routing(enabled == cass_true);
80   return CASS_OK;
81 }
82 
cass_execution_profile_set_token_aware_routing_shuffle_replicas(CassExecProfile * profile,cass_bool_t enabled)83 CassError cass_execution_profile_set_token_aware_routing_shuffle_replicas(CassExecProfile* profile,
84                                                                           cass_bool_t enabled) {
85   profile->set_token_aware_routing_shuffle_replicas(enabled == cass_true);
86   return CASS_OK;
87 }
88 
cass_execution_profile_set_latency_aware_routing(CassExecProfile * profile,cass_bool_t enabled)89 CassError cass_execution_profile_set_latency_aware_routing(CassExecProfile* profile,
90                                                            cass_bool_t enabled) {
91   profile->set_latency_aware_routing(enabled == cass_true);
92   return CASS_OK;
93 }
94 
cass_execution_profile_set_latency_aware_routing_settings(CassExecProfile * profile,cass_double_t exclusion_threshold,cass_uint64_t scale_ms,cass_uint64_t retry_period_ms,cass_uint64_t update_rate_ms,cass_uint64_t min_measured)95 CassError cass_execution_profile_set_latency_aware_routing_settings(
96     CassExecProfile* profile, cass_double_t exclusion_threshold, cass_uint64_t scale_ms,
97     cass_uint64_t retry_period_ms, cass_uint64_t update_rate_ms, cass_uint64_t min_measured) {
98   LatencyAwarePolicy::Settings settings;
99   settings.exclusion_threshold = exclusion_threshold;
100   settings.scale_ns = scale_ms * 1000 * 1000;
101   settings.retry_period_ns = retry_period_ms * 1000 * 1000;
102   settings.update_rate_ms = update_rate_ms;
103   settings.min_measured = min_measured;
104   profile->set_latency_aware_routing_settings(settings);
105   return CASS_OK;
106 }
107 
cass_execution_profile_set_whitelist_filtering(CassExecProfile * profile,const char * hosts)108 CassError cass_execution_profile_set_whitelist_filtering(CassExecProfile* profile,
109                                                          const char* hosts) {
110   return cass_execution_profile_set_whitelist_filtering_n(profile, hosts, SAFE_STRLEN(hosts));
111   return CASS_OK;
112 }
113 
cass_execution_profile_set_whitelist_filtering_n(CassExecProfile * profile,const char * hosts,size_t hosts_length)114 CassError cass_execution_profile_set_whitelist_filtering_n(CassExecProfile* profile,
115                                                            const char* hosts, size_t hosts_length) {
116   if (hosts_length == 0) {
117     profile->whitelist().clear();
118   } else {
119     explode(String(hosts, hosts_length), profile->whitelist());
120   }
121   return CASS_OK;
122 }
123 
cass_execution_profile_set_blacklist_filtering(CassExecProfile * profile,const char * hosts)124 CassError cass_execution_profile_set_blacklist_filtering(CassExecProfile* profile,
125                                                          const char* hosts) {
126   return cass_execution_profile_set_blacklist_filtering_n(profile, hosts, SAFE_STRLEN(hosts));
127 }
128 
cass_execution_profile_set_blacklist_filtering_n(CassExecProfile * profile,const char * hosts,size_t hosts_length)129 CassError cass_execution_profile_set_blacklist_filtering_n(CassExecProfile* profile,
130                                                            const char* hosts, size_t hosts_length) {
131   if (hosts_length == 0) {
132     profile->blacklist().clear();
133   } else {
134     explode(String(hosts, hosts_length), profile->blacklist());
135   }
136   return CASS_OK;
137 }
138 
cass_execution_profile_set_whitelist_dc_filtering(CassExecProfile * profile,const char * dcs)139 CassError cass_execution_profile_set_whitelist_dc_filtering(CassExecProfile* profile,
140                                                             const char* dcs) {
141   return cass_execution_profile_set_whitelist_dc_filtering_n(profile, dcs, SAFE_STRLEN(dcs));
142 }
143 
cass_execution_profile_set_whitelist_dc_filtering_n(CassExecProfile * profile,const char * dcs,size_t dcs_length)144 CassError cass_execution_profile_set_whitelist_dc_filtering_n(CassExecProfile* profile,
145                                                               const char* dcs, size_t dcs_length) {
146   if (dcs_length == 0) {
147     profile->whitelist_dc().clear();
148   } else {
149     explode(String(dcs, dcs_length), profile->whitelist_dc());
150   }
151   return CASS_OK;
152 }
153 
cass_execution_profile_set_blacklist_dc_filtering(CassExecProfile * profile,const char * dcs)154 CassError cass_execution_profile_set_blacklist_dc_filtering(CassExecProfile* profile,
155                                                             const char* dcs) {
156   return cass_execution_profile_set_blacklist_dc_filtering_n(profile, dcs, SAFE_STRLEN(dcs));
157 }
158 
cass_execution_profile_set_blacklist_dc_filtering_n(CassExecProfile * profile,const char * dcs,size_t dcs_length)159 CassError cass_execution_profile_set_blacklist_dc_filtering_n(CassExecProfile* profile,
160                                                               const char* dcs, size_t dcs_length) {
161   if (dcs_length == 0) {
162     profile->blacklist_dc().clear();
163   } else {
164     explode(String(dcs, dcs_length), profile->blacklist_dc());
165   }
166   return CASS_OK;
167 }
168 
cass_execution_profile_set_retry_policy(CassExecProfile * profile,CassRetryPolicy * retry_policy)169 CassError cass_execution_profile_set_retry_policy(CassExecProfile* profile,
170                                                   CassRetryPolicy* retry_policy) {
171   profile->set_retry_policy(retry_policy);
172   return CASS_OK;
173 }
174 
cass_execution_profile_set_constant_speculative_execution_policy(CassExecProfile * profile,cass_int64_t constant_delay_ms,int max_speculative_executions)175 CassError cass_execution_profile_set_constant_speculative_execution_policy(
176     CassExecProfile* profile, cass_int64_t constant_delay_ms, int max_speculative_executions) {
177   if (constant_delay_ms < 0 || max_speculative_executions < 0) {
178     return CASS_ERROR_LIB_BAD_PARAMS;
179   }
180   profile->set_speculative_execution_policy(
181       new ConstantSpeculativeExecutionPolicy(constant_delay_ms, max_speculative_executions));
182   return CASS_OK;
183 }
184 
cass_execution_profile_set_no_speculative_execution_policy(CassExecProfile * profile)185 CassError cass_execution_profile_set_no_speculative_execution_policy(CassExecProfile* profile) {
186   profile->set_speculative_execution_policy(new NoSpeculativeExecutionPolicy());
187   return CASS_OK;
188 }
189 
190 } // extern "C"
191