forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgossiping_property_file_snitch.cc
342 lines (287 loc) · 10.9 KB
/
gossiping_property_file_snitch.cc
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
* Modified by ScyllaDB
* Copyright (C) 2015 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include "locator/gossiping_property_file_snitch.hh"
#include "gms/versioned_value.hh"
#include "message/msg_addr.hh"
#include "message/messaging_service.hh"
#include "gms/gossiper.hh"
namespace locator {
future<bool> gossiping_property_file_snitch::property_file_was_modified() {
return open_file_dma(_prop_file_name, open_flags::ro)
.then([this](file f) {
return do_with(std::move(f), [] (file& f) {
return f.stat();
});
}).then_wrapped([this] (auto&& f) {
try {
auto st = f.get0();
if (!_last_file_mod ||
_last_file_mod->tv_sec != st.st_mtim.tv_sec) {
_last_file_mod = st.st_mtim;
return true;
} else {
return false;
}
} catch (...) {
logger().error("Failed to open {} for read or to get stats", _prop_file_name);
throw;
}
});
}
gossiping_property_file_snitch::gossiping_property_file_snitch(
const sstring& fname, unsigned io_cpuid)
: production_snitch_base(fname), _file_reader_cpu_id(io_cpuid) {
if (this_shard_id() == _file_reader_cpu_id) {
io_cpu_id() = _file_reader_cpu_id;
}
}
future<> gossiping_property_file_snitch::start() {
using namespace std::chrono_literals;
_state = snitch_state::initializing;
reset_io_state();
// Run a timer only on specific CPU
if (this_shard_id() == _file_reader_cpu_id) {
//
// Here we will create a timer that will read the properties file every
// minute and load its contents into the gossiper.endpoint_state_map
//
_file_reader.set_callback([this] {
periodic_reader_callback();
});
return read_property_file().then([this] {
start_io();
set_snitch_ready();
return make_ready_future<>();
});
}
set_snitch_ready();
return make_ready_future<>();
}
void gossiping_property_file_snitch::periodic_reader_callback() {
_file_reader_runs = true;
//FIXME: discarded future.
(void)property_file_was_modified().then([this] (bool was_modified) {
if (was_modified) {
return read_property_file();
}
return make_ready_future<>();
}).then_wrapped([this] (auto&& f) {
try {
f.get();
} catch (...) {
logger().error("Exception has been thrown when parsing the property file.");
}
if (_state == snitch_state::stopping || _state == snitch_state::io_pausing) {
this->set_stopped();
} else if (_state != snitch_state::stopped) {
_file_reader.arm(reload_property_file_period());
}
_file_reader_runs = false;
});
}
future<> gossiping_property_file_snitch::gossiper_starting() {
using namespace gms;
using namespace service;
//
// Note: currently gossiper "main" instance always runs on CPU0 therefore
// this function will be executed on CPU0 only.
//
auto& g = get_local_gossiper();
auto local_internal_addr = g.get_local_messaging().listen_address();
std::ostringstream ostrm;
ostrm<<local_internal_addr<<std::flush;
return gossip_snitch_info({
{ application_state::INTERNAL_IP, versioned_value::internal_ip(ostrm.str()) },
}).then([this] {
_gossip_started = true;
return reload_gossiper_state();
});
}
future<> gossiping_property_file_snitch::read_property_file() {
using namespace exceptions;
return load_property_file().then([this] {
return reload_configuration();
}).then_wrapped([this] (auto&& f) {
try {
f.get();
return make_ready_future<>();
} catch (...) {
//
// In case of an error:
// - Halt if in the constructor.
// - Print an error when reloading.
//
if (_state == snitch_state::initializing) {
logger().error("Failed to parse a properties file ({}). Halting...", _prop_file_name);
throw;
} else {
logger().warn("Failed to reload a properties file ({}). Using previous values.", _prop_file_name);
return make_ready_future<>();
}
}
});
}
future<> gossiping_property_file_snitch::reload_configuration() {
// "prefer_local" is FALSE by default
bool new_prefer_local = false;
sstring new_dc;
sstring new_rack;
// Rack and Data Center have to be defined in the properties file!
if (!_prop_values.contains(dc_property_key) || !_prop_values.contains(rack_property_key)) {
throw_incomplete_file();
}
new_dc = _prop_values[dc_property_key];
new_rack = _prop_values[rack_property_key];
if (_prop_values.contains(prefer_local_property_key)) {
if (_prop_values[prefer_local_property_key] == "false") {
new_prefer_local = false;
} else if (_prop_values[prefer_local_property_key] == "true") {
new_prefer_local = true;
} else {
throw_bad_format("prefer_local configuration is malformed");
}
}
if (_state == snitch_state::initializing || _my_dc != new_dc ||
_my_rack != new_rack || _prefer_local != new_prefer_local) {
_my_dc = new_dc;
_my_rack = new_rack;
_prefer_local = new_prefer_local;
assert(_my_distributed);
return _my_distributed->invoke_on_all(
[this] (snitch_ptr& local_s) {
// Distribute the new values on all CPUs but the current one
if (this_shard_id() != _file_reader_cpu_id) {
local_s->set_my_dc(_my_dc);
local_s->set_my_rack(_my_rack);
local_s->set_prefer_local(_prefer_local);
}
}).then([this] {
return seastar::async([this] {
// reload Gossiper state (executed on CPU0 only)
smp::submit_to(0, [] {
auto& local_snitch_ptr = get_local_snitch_ptr();
return local_snitch_ptr->reload_gossiper_state();
}).get();
_reconfigured();
// spread the word...
smp::submit_to(0, [] {
auto& local_snitch_ptr = get_local_snitch_ptr();
if (local_snitch_ptr->local_gossiper_started()) {
return local_snitch_ptr->gossip_snitch_info({});
}
return make_ready_future<>();
}).get();
});
});
}
return make_ready_future<>();
}
void gossiping_property_file_snitch::set_stopped() {
if (_state == snitch_state::stopping) {
_state = snitch_state::stopped;
} else {
_state = snitch_state::io_paused;
}
_io_is_stopped.set_value();
}
future<> gossiping_property_file_snitch::stop_io() {
if (this_shard_id() == _file_reader_cpu_id) {
_file_reader.cancel();
// If timer is not running then set the STOPPED state right away.
if (!_file_reader_runs) {
set_stopped();
}
} else {
set_stopped();
}
return _io_is_stopped.get_future();
}
void gossiping_property_file_snitch::resume_io() {
reset_io_state();
start_io();
set_snitch_ready();
}
void gossiping_property_file_snitch::start_io() {
// Run a timer only on specific CPU
if (this_shard_id() == _file_reader_cpu_id) {
_file_reader.arm(reload_property_file_period());
}
}
future<> gossiping_property_file_snitch::stop() {
if (_state == snitch_state::stopped || _state == snitch_state::io_paused) {
return make_ready_future<>();
}
_state = snitch_state::stopping;
return stop_io();
}
future<> gossiping_property_file_snitch::pause_io() {
if (_state == snitch_state::stopped || _state == snitch_state::io_paused) {
return make_ready_future<>();
}
_state = snitch_state::io_pausing;
return stop_io();
}
// should be invoked of CPU0 only
future<> gossiping_property_file_snitch::reload_gossiper_state() {
if (!_gossip_started) {
return make_ready_future<>();
}
future<> ret = make_ready_future<>();
if (_reconnectable_helper) {
ret = gms::get_local_gossiper().unregister_(_reconnectable_helper);
}
if (!_prefer_local) {
return ret;
}
return ret.then([this] {
_reconnectable_helper = ::make_shared<reconnectable_snitch_helper>(_my_dc);
gms::get_local_gossiper().register_(_reconnectable_helper);
});
}
using registry_2_params = class_registrator<i_endpoint_snitch,
gossiping_property_file_snitch,
const sstring&, unsigned>;
static registry_2_params registrator2("org.apache.cassandra.locator.GossipingPropertyFileSnitch");
using registry_1_param = class_registrator<i_endpoint_snitch,
gossiping_property_file_snitch,
const sstring&>;
static registry_1_param registrator1("org.apache.cassandra.locator.GossipingPropertyFileSnitch");
using registry_default = class_registrator<i_endpoint_snitch,
gossiping_property_file_snitch>;
static registry_default registrator_default("org.apache.cassandra.locator.GossipingPropertyFileSnitch");
static registry_default registrator_default_short_name("GossipingPropertyFileSnitch");
} // namespace locator