forked from datastax/cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatency_aware_policy.cpp
142 lines (113 loc) · 4.59 KB
/
latency_aware_policy.cpp
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
/*
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 "latency_aware_policy.hpp"
#include "get_time.hpp"
#include "logger.hpp"
#include <algorithm>
#include <iterator>
namespace cass {
void LatencyAwarePolicy::init(const Host::Ptr& connected_host,
const HostMap& hosts,
Random* random) {
hosts_->reserve(hosts.size());
std::transform(hosts.begin(), hosts.end(), std::back_inserter(*hosts_), GetHost());
for (HostMap::const_iterator i = hosts.begin(),
end = hosts.end(); i != end; i) {
i->second->enable_latency_tracking(settings_.scale_ns, settings_.min_measured);
}
ChainedLoadBalancingPolicy::init(connected_host, hosts, random);
}
void LatencyAwarePolicy::register_handles(uv_loop_t* loop) {
calculate_min_average_task_ = PeriodicTask::start(loop,
settings_.update_rate_ms,
this,
LatencyAwarePolicy::on_work,
LatencyAwarePolicy::on_after_work);
}
void LatencyAwarePolicy::close_handles() {
if (calculate_min_average_task_) {
PeriodicTask::stop(calculate_min_average_task_);
}
}
QueryPlan* LatencyAwarePolicy::new_query_plan(const std::string& keyspace,
RequestHandler* request_handler,
const TokenMap* token_map) {
return new LatencyAwareQueryPlan(this,
child_policy_->new_query_plan(keyspace,
request_handler,
token_map));
}
void LatencyAwarePolicy::on_add(const Host::Ptr& host) {
host->enable_latency_tracking(settings_.scale_ns, settings_.min_measured);
add_host(hosts_, host);
ChainedLoadBalancingPolicy::on_add(host);
}
void LatencyAwarePolicy::on_remove(const Host::Ptr& host) {
remove_host(hosts_, host);
ChainedLoadBalancingPolicy::on_remove(host);
}
void LatencyAwarePolicy::on_up(const Host::Ptr& host) {
add_host(hosts_, host);
ChainedLoadBalancingPolicy::on_up(host);
}
void LatencyAwarePolicy::on_down(const Host::Ptr& host) {
remove_host(hosts_, host);
ChainedLoadBalancingPolicy::on_down(host);
}
Host::Ptr LatencyAwarePolicy::LatencyAwareQueryPlan::compute_next() {
int64_t min = policy_->min_average_.load();
const Settings& settings = policy_->settings_;
uint64_t now = uv_hrtime();
Host::Ptr host;
while ((host = child_plan_->compute_next())) {
TimestampedAverage latency = host->get_current_average();
if (min < 0 ||
latency.average < 0 ||
latency.num_measured < settings.min_measured ||
(now - latency.timestamp) > settings.retry_period_ns) {
return host;
}
if (latency.average <= static_cast<int64_t>(settings.exclusion_threshold * min)) {
return host;
}
skipped_.push_back(host);
}
if (skipped_index_ < skipped_.size()) {
return skipped_[skipped_index_ ];
}
return Host::Ptr();
}
void LatencyAwarePolicy::on_work(PeriodicTask* task) {
LatencyAwarePolicy* policy = static_cast<LatencyAwarePolicy*>(task->data());
const Settings& settings = policy->settings_;
const CopyOnWriteHostVec& hosts = policy->hosts_;
int64_t new_min_average = CASS_INT64_MAX;
int64_t now = uv_hrtime();
for (HostVec::const_iterator i = hosts->begin(),
end = hosts->end(); i != end; i) {
TimestampedAverage latency = (*i)->get_current_average();
if (latency.average >= 0
&& latency.num_measured >= settings.min_measured
&& (now - latency.timestamp) <= settings.retry_period_ns) {
new_min_average = std::min(new_min_average, latency.average);
}
}
if (new_min_average != CASS_INT64_MAX) {
LOG_TRACE("Calculated new minimum: %f", static_cast<double>(new_min_average) / 1e6);
policy->min_average_.store(new_min_average);
}
}
void LatencyAwarePolicy::on_after_work(PeriodicTask* task) {
// no-op
}
} // namespace cass