forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdc_aware_policy.cpp
More file actions
265 lines (218 loc) · 8.21 KB
/
dc_aware_policy.cpp
File metadata and controls
265 lines (218 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Copyright (c) DataStax, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "dc_aware_policy.hpp"
#include "logger.hpp"
#include "request_handler.hpp"
#include "scoped_lock.hpp"
#include <algorithm>
using namespace datastax;
using namespace datastax::internal;
using namespace datastax::internal::core;
DCAwarePolicy::DCAwarePolicy(const String& local_dc, size_t used_hosts_per_remote_dc,
bool skip_remote_dcs_for_local_cl)
: local_dc_(local_dc)
, used_hosts_per_remote_dc_(used_hosts_per_remote_dc)
, skip_remote_dcs_for_local_cl_(skip_remote_dcs_for_local_cl)
, local_dc_live_hosts_(new HostVec())
, index_(0) {
uv_rwlock_init(&available_rwlock_);
if (used_hosts_per_remote_dc_ > 0 || !skip_remote_dcs_for_local_cl) {
LOG_WARN("Remote multi-dc settings have been deprecated and will be removed"
" in the next major release");
}
}
DCAwarePolicy::~DCAwarePolicy() { uv_rwlock_destroy(&available_rwlock_); }
void DCAwarePolicy::init(const Host::Ptr& connected_host, const HostMap& hosts, Random* random,
const String& local_dc) {
if (local_dc_.empty()) { // Only override if no local DC was specified.
local_dc_ = local_dc;
}
if (local_dc_.empty() && connected_host && !connected_host->dc().empty()) {
LOG_INFO("Using '%s' for the local data center "
"(if this is incorrect, please provide the correct data center)",
connected_host->dc().c_str());
local_dc_ = connected_host->dc();
}
available_.resize(hosts.size());
std::transform(hosts.begin(), hosts.end(), std::inserter(available_, available_.begin()),
GetAddress());
for (HostMap::const_iterator i = hosts.begin(), end = hosts.end(); i != end; ++i) {
on_host_added(i->second);
}
if (random != NULL) {
index_ = random->next(std::max(static_cast<size_t>(1), hosts.size()));
}
}
CassHostDistance DCAwarePolicy::distance(const Host::Ptr& host) const {
if (local_dc_.empty() || host->dc() == local_dc_) {
return CASS_HOST_DISTANCE_LOCAL;
}
const CopyOnWriteHostVec& hosts = per_remote_dc_live_hosts_.get_hosts(host->dc());
size_t num_hosts = std::min(hosts->size(), used_hosts_per_remote_dc_);
for (size_t i = 0; i < num_hosts; ++i) {
if ((*hosts)[i]->address() == host->address()) {
return CASS_HOST_DISTANCE_REMOTE;
}
}
return CASS_HOST_DISTANCE_IGNORE;
}
QueryPlan* DCAwarePolicy::new_query_plan(const String& keyspace, RequestHandler* request_handler,
const TokenMap* token_map) {
CassConsistency cl =
request_handler != NULL ? request_handler->consistency() : CASS_DEFAULT_CONSISTENCY;
return new DCAwareQueryPlan(this, cl, index_++);
}
bool DCAwarePolicy::is_host_up(const Address& address) const {
ScopedReadLock rl(&available_rwlock_);
return available_.count(address) > 0;
}
void DCAwarePolicy::on_host_added(const Host::Ptr& host) {
const String& dc = host->dc();
if (local_dc_.empty() && !dc.empty()) {
LOG_INFO("Using '%s' for local data center "
"(if this is incorrect, please provide the correct data center)",
host->dc().c_str());
local_dc_ = dc;
}
if (dc == local_dc_) {
add_host(local_dc_live_hosts_, host);
} else {
per_remote_dc_live_hosts_.add_host_to_dc(dc, host);
}
}
void DCAwarePolicy::on_host_removed(const Host::Ptr& host) {
const String& dc = host->dc();
if (dc == local_dc_) {
remove_host(local_dc_live_hosts_, host);
} else {
per_remote_dc_live_hosts_.remove_host_from_dc(host->dc(), host);
}
ScopedWriteLock wl(&available_rwlock_);
available_.erase(host->address());
}
void DCAwarePolicy::on_host_up(const Host::Ptr& host) {
on_host_added(host);
ScopedWriteLock wl(&available_rwlock_);
available_.insert(host->address());
}
void DCAwarePolicy::on_host_down(const Address& address) {
if (!remove_host(local_dc_live_hosts_, address) &&
!per_remote_dc_live_hosts_.remove_host(address)) {
LOG_DEBUG("Attempted to mark host %s as DOWN, but it doesn't exist",
address.to_string().c_str());
}
ScopedWriteLock wl(&available_rwlock_);
available_.erase(address);
}
bool DCAwarePolicy::skip_remote_dcs_for_local_cl() const {
ScopedReadLock rl(&available_rwlock_);
return skip_remote_dcs_for_local_cl_;
}
size_t DCAwarePolicy::used_hosts_per_remote_dc() const {
ScopedReadLock rl(&available_rwlock_);
return used_hosts_per_remote_dc_;
}
const String& DCAwarePolicy::local_dc() const {
ScopedReadLock rl(&available_rwlock_);
return local_dc_;
}
void DCAwarePolicy::PerDCHostMap::add_host_to_dc(const String& dc, const Host::Ptr& host) {
ScopedWriteLock wl(&rwlock_);
Map::iterator i = map_.find(dc);
if (i == map_.end()) {
CopyOnWriteHostVec hosts(new HostVec());
hosts->push_back(host);
map_.insert(Map::value_type(dc, hosts));
} else {
add_host(i->second, host);
}
}
void DCAwarePolicy::PerDCHostMap::remove_host_from_dc(const String& dc, const Host::Ptr& host) {
ScopedWriteLock wl(&rwlock_);
Map::iterator i = map_.find(dc);
if (i != map_.end()) {
core::remove_host(i->second, host);
}
}
bool DCAwarePolicy::PerDCHostMap::remove_host(const Address& address) {
ScopedWriteLock wl(&rwlock_);
for (Map::iterator i = map_.begin(), end = map_.end(); i != end; ++i) {
if (core::remove_host(i->second, address)) {
return true;
}
}
return false;
}
const CopyOnWriteHostVec& DCAwarePolicy::PerDCHostMap::get_hosts(const String& dc) const {
ScopedReadLock rl(&rwlock_);
Map::const_iterator i = map_.find(dc);
if (i == map_.end()) return no_hosts_;
return i->second;
}
void DCAwarePolicy::PerDCHostMap::copy_dcs(KeySet* dcs) const {
ScopedReadLock rl(&rwlock_);
for (Map::const_iterator i = map_.begin(), end = map_.end(); i != end; ++i) {
dcs->insert(i->first);
}
}
// Helper functions to prevent copy (Notice: "const CopyOnWriteHostVec&")
static const Host::Ptr& get_next_host(const CopyOnWriteHostVec& hosts, size_t index) {
return (*hosts)[index % hosts->size()];
}
static const Host::Ptr& get_next_host_bounded(const CopyOnWriteHostVec& hosts, size_t index,
size_t bound) {
return (*hosts)[index % std::min(hosts->size(), bound)];
}
static size_t get_hosts_size(const CopyOnWriteHostVec& hosts) { return hosts->size(); }
DCAwarePolicy::DCAwareQueryPlan::DCAwareQueryPlan(const DCAwarePolicy* policy, CassConsistency cl,
size_t start_index)
: policy_(policy)
, cl_(cl)
, hosts_(policy_->local_dc_live_hosts_)
, local_remaining_(get_hosts_size(hosts_))
, remote_remaining_(0)
, index_(start_index) {}
Host::Ptr DCAwarePolicy::DCAwareQueryPlan::compute_next() {
while (local_remaining_ > 0) {
--local_remaining_;
const Host::Ptr& host(get_next_host(hosts_, index_++));
if (policy_->is_host_up(host->address())) {
return host;
}
}
if (policy_->skip_remote_dcs_for_local_cl_ && is_dc_local(cl_)) {
return Host::Ptr();
}
if (!remote_dcs_) {
remote_dcs_.reset(new PerDCHostMap::KeySet());
policy_->per_remote_dc_live_hosts_.copy_dcs(remote_dcs_.get());
}
while (true) {
while (remote_remaining_ > 0) {
--remote_remaining_;
const Host::Ptr& host(
get_next_host_bounded(hosts_, index_++, policy_->used_hosts_per_remote_dc_));
if (policy_->is_host_up(host->address())) {
return host;
}
}
if (remote_dcs_->empty()) {
break;
}
PerDCHostMap::KeySet::iterator i = remote_dcs_->begin();
hosts_ = policy_->per_remote_dc_live_hosts_.get_hosts(*i);
remote_remaining_ = std::min(get_hosts_size(hosts_), policy_->used_hosts_per_remote_dc_);
remote_dcs_->erase(i);
}
return Host::Ptr();
}