forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.cpp
More file actions
779 lines (648 loc) · 24.4 KB
/
cluster.cpp
File metadata and controls
779 lines (648 loc) · 24.4 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
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 "cluster.hpp"
#include "constants.hpp"
#include "dc_aware_policy.hpp"
#include "external.hpp"
#include "logger.hpp"
#include "resolver.hpp"
#include "round_robin_policy.hpp"
#include "speculative_execution.hpp"
#include "utils.hpp"
using namespace datastax;
using namespace datastax::internal::core;
namespace datastax { namespace internal { namespace core {
/**
* A task for initiating the cluster close process.
*/
class ClusterRunClose : public Task {
public:
ClusterRunClose(const Cluster::Ptr& cluster)
: cluster_(cluster) {}
void run(EventLoop* event_loop) { cluster_->internal_close(); }
private:
Cluster::Ptr cluster_;
};
/**
* A task for marking a node as UP.
*/
class ClusterNotifyUp : public Task {
public:
ClusterNotifyUp(const Cluster::Ptr& cluster, const Address& address)
: cluster_(cluster)
, address_(address) {}
void run(EventLoop* event_loop) { cluster_->internal_notify_host_up(address_); }
private:
Cluster::Ptr cluster_;
Address address_;
};
/**
* A task for marking a node as DOWN.
*/
class ClusterNotifyDown : public Task {
public:
ClusterNotifyDown(const Cluster::Ptr& cluster, const Address& address)
: cluster_(cluster)
, address_(address) {}
void run(EventLoop* event_loop) { cluster_->internal_notify_host_down(address_); }
private:
Cluster::Ptr cluster_;
Address address_;
};
class ClusterStartEvents : public Task {
public:
ClusterStartEvents(const Cluster::Ptr& cluster)
: cluster_(cluster) {}
void run(EventLoop* event_loop) { cluster_->internal_start_events(); }
private:
Cluster::Ptr cluster_;
};
class ClusterStartClientMonitor : public Task {
public:
ClusterStartClientMonitor(const Cluster::Ptr& cluster, const String& client_id,
const String& session_id, const Config& config)
: cluster_(cluster)
, client_id_(client_id)
, session_id_(session_id)
, config_(config) {}
void run(EventLoop* event_loop) {
cluster_->internal_start_monitor_reporting(client_id_, session_id_, config_);
}
private:
Cluster::Ptr cluster_;
String client_id_;
String session_id_;
Config config_;
};
/**
* A no operation cluster listener. This is used when a listener is not set.
*/
class NopClusterListener : public ClusterListener {
public:
virtual void on_connect(Cluster* cluster) {}
virtual void on_host_up(const Host::Ptr& host) {}
virtual void on_host_down(const Host::Ptr& host) {}
virtual void on_host_added(const Host::Ptr& host) {}
virtual void on_host_removed(const Host::Ptr& host) {}
virtual void on_token_map_updated(const TokenMap::Ptr& token_map) {}
virtual void on_close(Cluster* cluster) {}
};
}}} // namespace datastax::internal::core
void ClusterEvent::process_event(const ClusterEvent& event, ClusterListener* listener) {
switch (event.type) {
case HOST_UP:
listener->on_host_up(event.host);
break;
case HOST_DOWN:
listener->on_host_down(event.host);
break;
case HOST_ADD:
listener->on_host_added(event.host);
break;
case HOST_REMOVE:
listener->on_host_removed(event.host);
break;
case HOST_MAYBE_UP:
listener->on_host_maybe_up(event.host);
break;
case HOST_READY:
listener->on_host_ready(event.host);
break;
case TOKEN_MAP_UPDATE:
listener->on_token_map_updated(event.token_map);
break;
}
}
void ClusterEvent::process_events(const ClusterEvent::Vec& events, ClusterListener* listener) {
for (ClusterEvent::Vec::const_iterator it = events.begin(), end = events.end(); it != end; ++it) {
process_event(*it, listener);
}
}
static NopClusterListener nop_cluster_listener__;
LockedHostMap::LockedHostMap(const HostMap& hosts)
: hosts_(hosts) {
uv_mutex_init(&mutex_);
}
LockedHostMap::~LockedHostMap() { uv_mutex_destroy(&mutex_); }
LockedHostMap::const_iterator LockedHostMap::find(const Address& address) const {
HostMap::const_iterator it = hosts_.find(address);
if (it == hosts_.end()) {
// If this is from an event (not SNI) and we're using SNI addresses then fallback to using the
// "rpc_address" to compare.
for (HostMap::const_iterator i = hosts_.begin(), end = hosts_.end(); i != end; ++i) {
if (i->second->rpc_address() == address) {
return i;
}
}
}
return it;
}
Host::Ptr LockedHostMap::get(const Address& address) const {
ScopedMutex l(&mutex_);
const_iterator it = find(address);
if (it == end()) return Host::Ptr();
return it->second;
}
void LockedHostMap::erase(const Address& address) {
ScopedMutex l(&mutex_);
hosts_.erase(address);
}
Host::Ptr& LockedHostMap::operator[](const Address& address) {
ScopedMutex l(&mutex_);
return hosts_[address];
}
LockedHostMap& LockedHostMap::operator=(const HostMap& hosts) {
ScopedMutex l(&mutex_);
hosts_ = hosts;
return *this;
}
ClusterSettings::ClusterSettings()
: load_balancing_policy(new RoundRobinPolicy())
, port(CASS_DEFAULT_PORT)
, reconnection_policy(new ExponentialReconnectionPolicy())
, prepare_on_up_or_add_host(CASS_DEFAULT_PREPARE_ON_UP_OR_ADD_HOST)
, max_prepares_per_flush(CASS_DEFAULT_MAX_PREPARES_PER_FLUSH)
, disable_events_on_startup(false)
, cluster_metadata_resolver_factory(new DefaultClusterMetadataResolverFactory()) {
load_balancing_policies.push_back(load_balancing_policy);
}
ClusterSettings::ClusterSettings(const Config& config)
: control_connection_settings(config)
, load_balancing_policy(config.load_balancing_policy())
, load_balancing_policies(config.load_balancing_policies())
, port(config.port())
, reconnection_policy(config.reconnection_policy())
, prepare_on_up_or_add_host(config.prepare_on_up_or_add_host())
, max_prepares_per_flush(CASS_DEFAULT_MAX_PREPARES_PER_FLUSH)
, disable_events_on_startup(false)
, cluster_metadata_resolver_factory(config.cluster_metadata_resolver_factory()) {}
Cluster::Cluster(const ControlConnection::Ptr& connection, ClusterListener* listener,
EventLoop* event_loop, const Host::Ptr& connected_host, const HostMap& hosts,
const ControlConnectionSchema& schema,
const LoadBalancingPolicy::Ptr& load_balancing_policy,
const LoadBalancingPolicy::Vec& load_balancing_policies, const String& local_dc,
const StringMultimap& supported_options, const ClusterSettings& settings)
: connection_(connection)
, listener_(listener ? listener : &nop_cluster_listener__)
, event_loop_(event_loop)
, load_balancing_policy_(load_balancing_policy)
, load_balancing_policies_(load_balancing_policies)
, settings_(settings)
, is_closing_(false)
, connected_host_(connected_host)
, hosts_(hosts)
, local_dc_(local_dc)
, supported_options_(supported_options)
, is_recording_events_(settings.disable_events_on_startup) {
inc_ref();
connection_->set_listener(this);
query_plan_.reset(load_balancing_policy_->new_query_plan("", NULL, NULL));
update_schema(schema);
update_token_map(hosts, connected_host_->partitioner(), schema);
listener_->on_reconnect(this);
}
void Cluster::close() { event_loop_->add(new ClusterRunClose(Ptr(this))); }
void Cluster::notify_host_up(const Address& address) {
event_loop_->add(new ClusterNotifyUp(Ptr(this), address));
}
void Cluster::notify_host_down(const Address& address) {
event_loop_->add(new ClusterNotifyDown(Ptr(this), address));
}
void Cluster::start_events() { event_loop_->add(new ClusterStartEvents(Ptr(this))); }
void Cluster::start_monitor_reporting(const String& client_id, const String& session_id,
const Config& config) {
event_loop_->add(new ClusterStartClientMonitor(Ptr(this), client_id, session_id, config));
}
Metadata::SchemaSnapshot Cluster::schema_snapshot() { return metadata_.schema_snapshot(); }
Host::Ptr Cluster::find_host(const Address& address) const { return hosts_.get(address); }
PreparedMetadata::Entry::Ptr Cluster::prepared(const String& id) const {
return prepared_metadata_.get(id);
}
void Cluster::prepared(const String& id, const PreparedMetadata::Entry::Ptr& entry) {
prepared_metadata_.set(id, entry);
}
HostMap Cluster::available_hosts() const {
HostMap available;
for (HostMap::const_iterator it = hosts_.begin(), end = hosts_.end(); it != end; ++it) {
if (!is_host_ignored(it->second)) {
available[it->first] = it->second;
}
}
return available;
}
void Cluster::set_listener(ClusterListener* listener) {
listener_ = listener ? listener : &nop_cluster_listener__;
}
void Cluster::update_hosts(const HostMap& hosts) {
// Update the hosts and properly notify the listener
HostMap existing(hosts_);
for (HostMap::const_iterator it = hosts.begin(), end = hosts.end(); it != end; ++it) {
HostMap::iterator find_it = existing.find(it->first);
if (find_it != existing.end()) {
existing.erase(find_it); // Already exists mark as visited
} else {
notify_host_add(it->second); // A new host has been added
}
}
// Any hosts that existed before, but aren't in the new hosts
// need to be marked as removed.
for (HostMap::const_iterator it = existing.begin(), end = existing.end(); it != end; ++it) {
notify_host_remove(it->first);
}
}
void Cluster::update_schema(const ControlConnectionSchema& schema) {
metadata_.clear_and_update_back(connection_->server_version());
if (schema.keyspaces) {
metadata_.update_keyspaces(schema.keyspaces.get(), false);
}
if (schema.tables) {
metadata_.update_tables(schema.tables.get());
}
if (schema.views) {
metadata_.update_views(schema.views.get());
}
if (schema.columns) {
metadata_.update_columns(schema.columns.get());
}
if (schema.indexes) {
metadata_.update_indexes(schema.indexes.get());
}
if (schema.user_types) {
metadata_.update_user_types(schema.user_types.get());
}
if (schema.functions) {
metadata_.update_functions(schema.functions.get());
}
if (schema.aggregates) {
metadata_.update_aggregates(schema.aggregates.get());
}
if (schema.virtual_keyspaces) {
metadata_.update_keyspaces(schema.virtual_keyspaces.get(), true);
}
if (schema.virtual_tables) {
metadata_.update_tables(schema.virtual_tables.get());
}
if (schema.virtual_columns) {
metadata_.update_columns(schema.virtual_columns.get());
}
metadata_.swap_to_back_and_update_front();
}
void Cluster::update_token_map(const HostMap& hosts, const String& partitioner,
const ControlConnectionSchema& schema) {
if (settings_.control_connection_settings.use_token_aware_routing && schema.keyspaces) {
// Create a new token map and populate it
token_map_ = TokenMap::from_partitioner(partitioner);
if (!token_map_) {
return; // Partition is not supported
}
token_map_->add_keyspaces(connection_->server_version(), schema.keyspaces.get());
for (HostMap::const_iterator it = hosts.begin(), end = hosts.end(); it != end; ++it) {
token_map_->add_host(it->second);
}
token_map_->build();
}
}
// All hosts from the cluster are included in the host map and in the load
// balancing policies (LBP) so that LBPs return the correct host distance (esp.
// important for DC-aware). This method prevents connection pools from being
// created to ignored hosts.
bool Cluster::is_host_ignored(const Host::Ptr& host) const {
return core::is_host_ignored(load_balancing_policies_, host);
}
void Cluster::schedule_reconnect() {
if (!reconnection_schedule_) {
reconnection_schedule_.reset(settings_.reconnection_policy->new_reconnection_schedule());
}
uint64_t delay_ms = reconnection_schedule_->next_delay_ms();
if (delay_ms > 0) {
timer_.start(connection_->loop(), delay_ms,
bind_callback(&Cluster::on_schedule_reconnect, this));
} else {
handle_schedule_reconnect();
}
}
void Cluster::on_schedule_reconnect(Timer* timer) { handle_schedule_reconnect(); }
void Cluster::handle_schedule_reconnect() {
const Host::Ptr& host = query_plan_->compute_next();
if (host) {
reconnector_.reset(new ControlConnector(host, connection_->protocol_version(),
bind_callback(&Cluster::on_reconnect, this)));
reconnector_->with_settings(settings_.control_connection_settings)
->connect(connection_->loop());
} else {
// No more hosts, refresh the query plan and schedule a re-connection
LOG_TRACE("Control connection query plan has no more hosts. "
"Reset query plan and schedule reconnect");
query_plan_.reset(load_balancing_policy_->new_query_plan("", NULL, NULL));
schedule_reconnect();
}
}
void Cluster::on_reconnect(ControlConnector* connector) {
reconnector_.reset();
if (is_closing_) {
handle_close();
return;
}
if (connector->is_ok()) {
connection_ = connector->release_connection();
connection_->set_listener(this);
// Incrementally update the hosts (notifying the listener)
update_hosts(connector->hosts());
// Get the newly connected host
connected_host_ = hosts_[connection_->address()];
assert(connected_host_ && "Connected host not found in hosts map");
update_schema(connector->schema());
update_token_map(connector->hosts(), connected_host_->partitioner(), connector->schema());
// Notify the listener that we've built a new token map
if (token_map_) {
notify_or_record(ClusterEvent(token_map_));
}
LOG_INFO("Control connection connected to %s", connected_host_->address_string().c_str());
listener_->on_reconnect(this);
reconnection_schedule_.reset();
} else if (!connector->is_canceled()) {
LOG_ERROR(
"Unable to reestablish a control connection to host %s because of the following error: %s",
connector->address().to_string().c_str(), connector->error_message().c_str());
schedule_reconnect();
}
}
void Cluster::internal_close() {
is_closing_ = true;
bool was_timer_running = timer_.is_running();
timer_.stop();
monitor_reporting_timer_.stop();
if (was_timer_running) {
handle_close();
} else if (reconnector_) {
reconnector_->cancel();
} else if (connection_) {
connection_->close();
}
}
void Cluster::handle_close() {
for (LoadBalancingPolicy::Vec::const_iterator it = load_balancing_policies_.begin(),
end = load_balancing_policies_.end();
it != end; ++it) {
(*it)->close_handles();
}
connection_.reset();
listener_->on_close(this);
dec_ref();
}
void Cluster::internal_notify_host_up(const Address& address) {
LockedHostMap::const_iterator it = hosts_.find(address);
if (it == hosts_.end()) {
LOG_WARN("Attempting to mark host %s that we don't have as UP", address.to_string().c_str());
return;
}
Host::Ptr host(it->second);
if (load_balancing_policy_->is_host_up(address)) {
// Already marked up so don't repeat duplicate notifications.
if (!is_host_ignored(host)) {
notify_or_record(ClusterEvent(ClusterEvent::HOST_READY, host));
}
return;
}
for (LoadBalancingPolicy::Vec::const_iterator it = load_balancing_policies_.begin(),
end = load_balancing_policies_.end();
it != end; ++it) {
(*it)->on_host_up(host);
}
if (is_host_ignored(host)) {
return; // Ignore host
}
if (!prepare_host(host, bind_callback(&Cluster::on_prepare_host_up, Cluster::Ptr(this)))) {
notify_host_up_after_prepare(host);
}
}
void Cluster::notify_host_up_after_prepare(const Host::Ptr& host) {
notify_or_record(ClusterEvent(ClusterEvent::HOST_READY, host));
notify_or_record(ClusterEvent(ClusterEvent::HOST_UP, host));
}
void Cluster::internal_notify_host_down(const Address& address) {
LockedHostMap::const_iterator it = hosts_.find(address);
if (it == hosts_.end()) {
// Using DEBUG level here because this can happen normally as the result of
// a remove event.
LOG_DEBUG("Attempting to mark host %s that we don't have as DOWN", address.to_string().c_str());
return;
}
Host::Ptr host(it->second);
if (!load_balancing_policy_->is_host_up(address)) {
// Already marked down so don't repeat duplicate notifications.
return;
}
for (LoadBalancingPolicy::Vec::const_iterator it = load_balancing_policies_.begin(),
end = load_balancing_policies_.end();
it != end; ++it) {
(*it)->on_host_down(address);
}
notify_or_record(ClusterEvent(ClusterEvent::HOST_DOWN, host));
}
void Cluster::internal_start_events() {
// Ignore if closing or already processed events
if (!is_closing_ && is_recording_events_) {
is_recording_events_ = false;
ClusterEvent::process_events(recorded_events_, listener_);
recorded_events_.clear();
}
}
void Cluster::internal_start_monitor_reporting(const String& client_id, const String& session_id,
const Config& config) {
monitor_reporting_.reset(create_monitor_reporting(client_id, session_id, config));
if (!is_closing_ && monitor_reporting_->interval_ms(connection_->dse_server_version()) > 0) {
monitor_reporting_->send_startup_message(connection_->connection(), config, available_hosts(),
load_balancing_policies_);
monitor_reporting_timer_.start(
event_loop_->loop(), monitor_reporting_->interval_ms(connection_->dse_server_version()),
bind_callback(&Cluster::on_monitor_reporting, this));
}
}
void Cluster::on_monitor_reporting(Timer* timer) {
if (!is_closing_) {
monitor_reporting_->send_status_message(connection_->connection(), available_hosts());
monitor_reporting_timer_.start(
event_loop_->loop(), monitor_reporting_->interval_ms(connection_->dse_server_version()),
bind_callback(&Cluster::on_monitor_reporting, this));
}
}
void Cluster::notify_host_add(const Host::Ptr& host) {
LockedHostMap::const_iterator host_it = hosts_.find(host->address());
if (host_it != hosts_.end()) {
LOG_WARN("Attempting to add host %s that we already have", host->address_string().c_str());
// If an entry already exists then notify that the node has been removed
// then re-add it.
for (LoadBalancingPolicy::Vec::const_iterator it = load_balancing_policies_.begin(),
end = load_balancing_policies_.end();
it != end; ++it) {
(*it)->on_host_removed(host_it->second);
}
notify_or_record(ClusterEvent(ClusterEvent::HOST_REMOVE, host));
}
hosts_[host->address()] = host;
for (LoadBalancingPolicy::Vec::const_iterator it = load_balancing_policies_.begin(),
end = load_balancing_policies_.end();
it != end; ++it) {
(*it)->on_host_added(host);
}
if (is_host_ignored(host)) {
return; // Ignore host
}
if (!prepare_host(host, bind_callback(&Cluster::on_prepare_host_add, Cluster::Ptr(this)))) {
notify_host_add_after_prepare(host);
}
}
void Cluster::notify_host_add_after_prepare(const Host::Ptr& host) {
if (token_map_) {
token_map_ = token_map_->copy();
token_map_->update_host_and_build(host);
notify_or_record(ClusterEvent(token_map_));
}
notify_or_record(ClusterEvent(ClusterEvent::HOST_ADD, host));
}
void Cluster::notify_host_remove(const Address& address) {
LockedHostMap::const_iterator it = hosts_.find(address);
if (it == hosts_.end()) {
LOG_WARN("Attempting removing host %s that we don't have", address.to_string().c_str());
return;
}
Host::Ptr host(it->second);
if (token_map_) {
token_map_ = token_map_->copy();
token_map_->remove_host_and_build(host);
notify_or_record(ClusterEvent(token_map_));
}
// If not marked down yet then explicitly trigger the event.
if (load_balancing_policy_->is_host_up(address)) {
notify_or_record(ClusterEvent(ClusterEvent::HOST_DOWN, host));
}
hosts_.erase(it->first);
for (LoadBalancingPolicy::Vec::const_iterator it = load_balancing_policies_.begin(),
end = load_balancing_policies_.end();
it != end; ++it) {
(*it)->on_host_removed(host);
}
notify_or_record(ClusterEvent(ClusterEvent::HOST_REMOVE, host));
}
void Cluster::notify_or_record(const ClusterEvent& event) {
if (is_recording_events_) {
recorded_events_.push_back(event);
} else {
ClusterEvent::process_event(event, listener_);
}
}
bool Cluster::prepare_host(const Host::Ptr& host, const PrepareHostHandler::Callback& callback) {
if (connection_ && settings_.prepare_on_up_or_add_host) {
PrepareHostHandler::Ptr prepare_host_handler(
new PrepareHostHandler(host, prepared_metadata_.copy(), callback,
connection_->protocol_version(), settings_.max_prepares_per_flush));
prepare_host_handler->prepare(connection_->loop(),
settings_.control_connection_settings.connection_settings);
return true;
}
return false;
}
void Cluster::on_prepare_host_add(const PrepareHostHandler* handler) {
notify_host_add_after_prepare(handler->host());
}
void Cluster::on_prepare_host_up(const PrepareHostHandler* handler) {
notify_host_up_after_prepare(handler->host());
}
void Cluster::on_update_schema(SchemaType type, const ResultResponse::Ptr& result,
const String& keyspace_name, const String& target_name) {
switch (type) {
case KEYSPACE:
// Virtual keyspaces are not updated (always false)
metadata_.update_keyspaces(result.get(), false);
if (token_map_) {
token_map_ = token_map_->copy();
token_map_->update_keyspaces_and_build(connection_->server_version(), result.get());
notify_or_record(ClusterEvent(token_map_));
}
break;
case TABLE:
metadata_.update_tables(result.get());
break;
case VIEW:
metadata_.update_views(result.get());
break;
case COLUMN:
metadata_.update_columns(result.get());
break;
case INDEX:
metadata_.update_indexes(result.get());
break;
case USER_TYPE:
metadata_.update_user_types(result.get());
break;
case FUNCTION:
metadata_.update_functions(result.get());
break;
case AGGREGATE:
metadata_.update_aggregates(result.get());
break;
}
}
void Cluster::on_drop_schema(SchemaType type, const String& keyspace_name,
const String& target_name) {
switch (type) {
case KEYSPACE:
metadata_.drop_keyspace(keyspace_name);
if (token_map_) {
token_map_ = token_map_->copy();
token_map_->drop_keyspace(keyspace_name);
notify_or_record(ClusterEvent(token_map_));
}
break;
case TABLE:
metadata_.drop_table_or_view(keyspace_name, target_name);
break;
case VIEW:
metadata_.drop_table_or_view(keyspace_name, target_name);
break;
case USER_TYPE:
metadata_.drop_user_type(keyspace_name, target_name);
break;
case FUNCTION:
metadata_.drop_function(keyspace_name, target_name);
break;
case AGGREGATE:
metadata_.drop_aggregate(keyspace_name, target_name);
break;
default:
break;
}
}
void Cluster::on_up(const Address& address) {
LockedHostMap::const_iterator it = hosts_.find(address);
if (it == hosts_.end()) {
LOG_WARN("Received UP event for an unknown host %s", address.to_string().c_str());
return;
}
notify_or_record(ClusterEvent(ClusterEvent::HOST_MAYBE_UP, it->second));
}
void Cluster::on_down(const Address& address) {
// Ignore down events from the control connection. Use the method
// `notify_host_down()` to trigger the DOWN status.
}
void Cluster::on_add(const Host::Ptr& host) { notify_host_add(host); }
void Cluster::on_remove(const Address& address) { notify_host_remove(address); }
void Cluster::on_close(ControlConnection* connection) {
if (!is_closing_) {
LOG_WARN("Lost control connection to host %s", connection_->address_string().c_str());
schedule_reconnect();
} else {
handle_close();
}
}