Repository navigation
Expand file tree
/
Copy pathhost.cpp
More file actions
170 lines (138 loc) · 4.91 KB
/
Copy pathhost.cpp
File metadata and controls
170 lines (138 loc) · 4.91 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
/*
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 "host.hpp"
#include "row.hpp"
#include "value.hpp"
#include "collection_iterator.hpp"
namespace cass {
void add_host(CopyOnWriteHostVec& hosts, const Host::Ptr& host) {
HostVec::iterator i;
for (i = hosts->begin(); i != hosts->end(); ++i) {
if ((*i)->address() == host->address()) {
*i = host;
break;
}
}
if (i == hosts->end()) {
hosts->push_back(host);
}
}
void remove_host(CopyOnWriteHostVec& hosts, const Host::Ptr& host) {
remove_host(hosts, host->address());
}
bool remove_host(CopyOnWriteHostVec& hosts, const Address& address) {
HostVec::iterator i;
for (i = hosts->begin(); i != hosts->end(); ++i) {
if ((*i)->address() == address) {
hosts->erase(i);
return true;
}
}
return false;
}
void Host::LatencyTracker::update(uint64_t latency_ns) {
uint64_t now = uv_hrtime();
ScopedSpinlock l(SpinlockPool<LatencyTracker>::get_spinlock(this));
TimestampedAverage previous = current_;
if (previous.num_measured < threshold_to_account_) {
current_.average = -1;
} else if (previous.average < 0) {
current_.average = latency_ns;
} else {
int64_t delay = now - previous.timestamp;
if (delay <= 0) {
return;
}
double scaled_delay = static_cast<double>(delay) / scale_ns_;
double weight = log(scaled_delay + 1) / scaled_delay;
current_.average = static_cast<int64_t>((1.0 - weight) * latency_ns + weight * previous.average);
}
current_.num_measured = previous.num_measured + 1;
current_.timestamp = now;
}
bool VersionNumber::parse(const String& version) {
return sscanf(version.c_str(), "%d.%d.%d", &major_version_, &minor_version_, &patch_version_) >= 2;
}
void Host::set(const Row* row, bool use_tokens) {
const Value* v;
String rack;
row->get_string_by_name("rack", &rack);
String dc;
row->get_string_by_name("data_center", &dc);
String release_version;
row->get_string_by_name("release_version", &release_version);
rack_ = rack;
dc_ = dc;
VersionNumber server_version;
if (server_version.parse(release_version)) {
server_version_ = server_version;
} else {
LOG_WARN("Invalid release version string \"%s\" on host %s",
release_version.c_str(),
address().to_string().c_str());
}
// Possibly correct for invalid Cassandra version numbers for specific
// versions of DSE.
if (server_version_ >= VersionNumber(4, 0, 0) &&
row->get_by_name("dse_version") != NULL) { // DSE only
String dse_version_str;
row->get_string_by_name("dse_version", &dse_version_str);
VersionNumber dse_version;
if (dse_version.parse(dse_version_str)) {
// Versions before DSE 6.7 erroneously return they support Cassandra 4.0.0
// features even though they don't.
if (dse_version < VersionNumber(6, 7, 0)) {
server_version_ = VersionNumber(3, 11, 0);
}
} else {
LOG_WARN("Invalid DSE version string \"%s\" on host %s",
dse_version_str.c_str(),
address().to_string().c_str());
}
}
row->get_string_by_name("partitioner", &partitioner_);
if (use_tokens) {
v = row->get_by_name("tokens");
if (v != NULL && v->is_collection()) {
CollectionIterator iterator(v);
while (iterator.next()) {
tokens_.push_back(iterator.value()->to_string());
}
}
}
}
ExternalHostListener::ExternalHostListener(const CassHostListenerCallback callback,
void *data)
: callback_(callback)
, data_(data) { }
void ExternalHostListener::on_host_up(const Host::Ptr& host) {
CassInet address;
address.address_length = host->address().to_inet(address.address);
callback_(CASS_HOST_LISTENER_EVENT_UP, address, data_);
}
void ExternalHostListener::on_host_down(const Host::Ptr& host) {
CassInet address;
address.address_length = host->address().to_inet(address.address);
callback_(CASS_HOST_LISTENER_EVENT_DOWN, address, data_);
}
void ExternalHostListener::on_host_added(const Host::Ptr& host) {
CassInet address;
address.address_length = host->address().to_inet(address.address);
callback_(CASS_HOST_LISTENER_EVENT_ADD, address, data_);
}
void ExternalHostListener::on_host_removed(const Host::Ptr& host) {
CassInet address;
address.address_length = host->address().to_inet(address.address);
callback_(CASS_HOST_LISTENER_EVENT_REMOVE, address, data_);
}
} // namespace cass