-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver_state.cpp
1611 lines (1507 loc) · 55.4 KB
/
server_state.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
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018-2019 Codership Oy <[email protected]>
*
* This file is part of wsrep-lib.
*
* Wsrep-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Wsrep-lib 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 wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#include "wsrep/server_state.hpp"
#include "wsrep/client_state.hpp"
#include "wsrep/server_service.hpp"
#include "wsrep/high_priority_service.hpp"
#include "wsrep/transaction.hpp"
#include "wsrep/view.hpp"
#include "wsrep/logger.hpp"
#include "wsrep/compiler.hpp"
#include "wsrep/id.hpp"
#include <cassert>
#include <sstream>
#include <algorithm>
//////////////////////////////////////////////////////////////////////////////
// Helpers //
//////////////////////////////////////////////////////////////////////////////
//
// This method is used to deal with historical burden of several
// ways to bootstrap the cluster. Bootstrap happens if
//
// * bootstrap option is given
// * cluster_address is "gcomm://" (Galera provider)
//
static bool is_bootstrap(const std::string& cluster_address, bool bootstrap)
{
return (bootstrap || cluster_address == "gcomm://");
}
// Helper method to provide detailed error message if transaction
// adopt for fragment removal fails.
static void log_adopt_error(const wsrep::transaction& transaction)
{
wsrep::log_warning() << "Adopting a transaction ("
<< transaction.server_id() << "," << transaction.id()
<< ") for rollback failed, "
<< "this may leave stale entries to streaming log "
<< "which may need to be removed manually.";
}
// resolve which of the two errors return to caller
static inline int resolve_return_error(bool const vote,
int const vote_err,
int const apply_err)
{
if (vote) return vote_err;
return vote_err != 0 ? vote_err : apply_err;
}
static void
discard_streaming_applier(wsrep::server_state& server_state,
wsrep::high_priority_service& high_priority_service,
wsrep::high_priority_service* streaming_applier,
const wsrep::ws_meta& ws_meta)
{
server_state.stop_streaming_applier(
ws_meta.server_id(), ws_meta.transaction_id());
server_state.server_service().release_high_priority_service(
streaming_applier);
high_priority_service.store_globals();
}
static int apply_fragment(wsrep::server_state& server_state,
wsrep::high_priority_service& high_priority_service,
wsrep::high_priority_service* streaming_applier,
const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta,
const wsrep::const_buffer& data)
{
int ret(0);
int apply_err;
wsrep::mutable_buffer err;
{
wsrep::high_priority_switch sw(high_priority_service,
*streaming_applier);
apply_err = streaming_applier->apply_write_set(ws_meta, data, err);
if (!apply_err)
{
assert(err.size() == 0);
streaming_applier->after_apply();
}
else
{
bool const remove_fragments(streaming_applier->transaction(
).streaming_context().fragments().size() > 0);
ret = streaming_applier->rollback(ws_handle, ws_meta);
ret = ret || (streaming_applier->after_apply(), 0);
if (remove_fragments)
{
ret = ret || streaming_applier->start_transaction(ws_handle,
ws_meta);
ret = ret || (streaming_applier->adopt_apply_error(err), 0);
ret = ret || streaming_applier->remove_fragments(ws_meta);
ret = ret || streaming_applier->commit(ws_handle, ws_meta);
ret = ret || (streaming_applier->after_apply(), 0);
}
else
{
ret = streaming_applier->log_dummy_write_set(ws_handle,
ws_meta, err);
}
}
}
if (!ret)
{
if (!apply_err)
{
high_priority_service.debug_crash("crash_apply_cb_before_append_frag");
const wsrep::xid xid(streaming_applier->transaction().xid());
ret = high_priority_service.append_fragment_and_commit(
*streaming_applier, ws_handle, ws_meta, data, xid);
high_priority_service.debug_crash("crash_apply_cb_after_append_frag");
ret = ret || (high_priority_service.after_apply(), 0);
}
else
{
discard_streaming_applier(server_state,
high_priority_service,
streaming_applier,
ws_meta);
ret = resolve_return_error(err.size() > 0, ret, apply_err);
}
}
return ret;
}
static int commit_fragment(wsrep::server_state& server_state,
wsrep::high_priority_service& high_priority_service,
wsrep::high_priority_service* streaming_applier,
const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta,
const wsrep::const_buffer& data)
{
int ret(0);
{
wsrep::high_priority_switch sw(
high_priority_service, *streaming_applier);
wsrep::mutable_buffer err;
int const apply_err(
streaming_applier->apply_write_set(ws_meta, data, err));
if (apply_err)
{
assert(streaming_applier->transaction(
).streaming_context().fragments().size() > 0);
ret = streaming_applier->rollback(ws_handle, ws_meta);
ret = ret || (streaming_applier->after_apply(), 0);
ret = ret || streaming_applier->start_transaction(
ws_handle, ws_meta);
ret = ret || (streaming_applier->adopt_apply_error(err),0);
}
else
{
assert(err.size() == 0);
}
const wsrep::transaction& trx(streaming_applier->transaction());
// Fragment removal for XA is going to happen in after_commit
if (trx.state() != wsrep::transaction::s_prepared)
{
streaming_applier->debug_crash(
"crash_apply_cb_before_fragment_removal");
ret = ret || streaming_applier->remove_fragments(ws_meta);
streaming_applier->debug_crash(
"crash_apply_cb_after_fragment_removal");
}
streaming_applier->debug_crash(
"crash_commit_cb_before_last_fragment_commit");
ret = ret || streaming_applier->commit(ws_handle, ws_meta);
streaming_applier->debug_crash(
"crash_commit_cb_last_fragment_commit_success");
ret = ret || (streaming_applier->after_apply(), 0);
ret = resolve_return_error(err.size() > 0, ret, apply_err);
}
if (!ret)
{
discard_streaming_applier(server_state, high_priority_service,
streaming_applier, ws_meta);
}
return ret;
}
static int rollback_fragment(wsrep::server_state& server_state,
wsrep::high_priority_service& high_priority_service,
wsrep::high_priority_service* streaming_applier,
const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta)
{
int ret(0);
int adopt_error(0);
bool const remove_fragments(streaming_applier->transaction().
streaming_context().fragments().size() > 0);
// If fragment removal is needed, adopt transaction state
// and start a transaction for it.
if (remove_fragments &&
(adopt_error = high_priority_service.adopt_transaction(
streaming_applier->transaction())))
{
log_adopt_error(streaming_applier->transaction());
}
// Even if the adopt above fails we roll back the streaming transaction.
// Adopt failure will leave stale entries in streaming log which can
// be removed manually.
wsrep::const_buffer no_error;
{
wsrep::high_priority_switch ws(
high_priority_service, *streaming_applier);
// Streaming applier rolls back out of order. Fragment
// removal grabs commit order below.
ret = streaming_applier->rollback(wsrep::ws_handle(), wsrep::ws_meta());
ret = ret || (streaming_applier->after_apply(), 0);
}
if (!ret)
{
discard_streaming_applier(server_state, high_priority_service,
streaming_applier, ws_meta);
if (adopt_error == 0)
{
if (remove_fragments)
{
ret = high_priority_service.remove_fragments(ws_meta);
ret = ret || high_priority_service.commit(ws_handle, ws_meta);
if (ret)
{
high_priority_service.rollback(ws_handle, ws_meta);
}
high_priority_service.after_apply();
}
else
{
if (ws_meta.ordered())
{
wsrep::mutable_buffer no_error;
ret = high_priority_service.log_dummy_write_set(
ws_handle, ws_meta, no_error);
}
}
}
}
return ret;
}
static int apply_write_set(wsrep::server_state& server_state,
wsrep::high_priority_service& high_priority_service,
const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta,
const wsrep::const_buffer& data)
{
int ret(0);
if (wsrep::rolls_back_transaction(ws_meta.flags()))
{
wsrep::mutable_buffer no_error;
if (wsrep::starts_transaction(ws_meta.flags()))
{
// No transaction existed before, log a dummy write set
ret = high_priority_service.log_dummy_write_set(
ws_handle, ws_meta, no_error);
}
else
{
wsrep::high_priority_service* sa(
server_state.find_streaming_applier(
ws_meta.server_id(), ws_meta.transaction_id()));
if (sa == 0)
{
// It is a known limitation that galera provider
// cannot always determine if certification test
// for interrupted transaction will pass or fail
// (see comments in transaction::certify_fragment()).
// As a consequence, unnecessary rollback fragments
// may be delivered here. The message below has
// been intentionally turned into a debug message,
// rather than warning.
WSREP_LOG_DEBUG(wsrep::log::debug_log_level(),
wsrep::log::debug_level_server_state,
"Could not find applier context for "
<< ws_meta.server_id()
<< ": " << ws_meta.transaction_id());
ret = high_priority_service.log_dummy_write_set(
ws_handle, ws_meta, no_error);
}
else
{
// rollback_fragment() consumes sa
ret = rollback_fragment(server_state,
high_priority_service,
sa,
ws_handle,
ws_meta);
}
}
}
else if (wsrep::starts_transaction(ws_meta.flags()) &&
wsrep::commits_transaction(ws_meta.flags()))
{
ret = high_priority_service.start_transaction(ws_handle, ws_meta);
if (!ret)
{
wsrep::mutable_buffer err;
int const apply_err(high_priority_service.apply_write_set(
ws_meta, data, err));
if (!apply_err)
{
assert(err.size() == 0);
ret = high_priority_service.commit(ws_handle, ws_meta);
ret = ret || (high_priority_service.after_apply(), 0);
}
else
{
ret = high_priority_service.rollback(ws_handle, ws_meta);
ret = ret || (high_priority_service.after_apply(), 0);
ret = ret || high_priority_service.log_dummy_write_set(
ws_handle, ws_meta, err);
ret = resolve_return_error(err.size() > 0, ret, apply_err);
}
}
}
else if (wsrep::starts_transaction(ws_meta.flags()))
{
assert(server_state.find_streaming_applier(
ws_meta.server_id(), ws_meta.transaction_id()) == 0);
wsrep::high_priority_service* sa(
server_state.server_service().streaming_applier_service(
high_priority_service));
server_state.start_streaming_applier(
ws_meta.server_id(), ws_meta.transaction_id(), sa);
sa->start_transaction(ws_handle, ws_meta);
ret = apply_fragment(server_state,
high_priority_service,
sa,
ws_handle,
ws_meta,
data);
}
else if (ws_meta.flags() == 0 || ws_meta.flags() == wsrep::provider::flag::pa_unsafe ||
wsrep::prepares_transaction(ws_meta.flags()))
{
wsrep::high_priority_service* sa(
server_state.find_streaming_applier(
ws_meta.server_id(), ws_meta.transaction_id()));
if (sa == 0)
{
// It is possible that rapid group membership changes
// may cause streaming transaction be rolled back before
// commit fragment comes in. Although this is a valid
// situation, log a warning if a sac cannot be found as
// it may be an indication of a bug too.
wsrep::log_warning() << "Could not find applier context for "
<< ws_meta.server_id()
<< ": " << ws_meta.transaction_id();
wsrep::mutable_buffer no_error;
ret = high_priority_service.log_dummy_write_set(
ws_handle, ws_meta, no_error);
}
else
{
sa->next_fragment(ws_meta);
ret = apply_fragment(server_state,
high_priority_service,
sa,
ws_handle,
ws_meta,
data);
}
}
else if (wsrep::commits_transaction(ws_meta.flags()))
{
if (high_priority_service.is_replaying())
{
wsrep::mutable_buffer unused;
ret = high_priority_service.start_transaction(
ws_handle, ws_meta) ||
high_priority_service.apply_write_set(ws_meta, data, unused) ||
high_priority_service.commit(ws_handle, ws_meta);
}
else
{
wsrep::high_priority_service* sa(
server_state.find_streaming_applier(
ws_meta.server_id(), ws_meta.transaction_id()));
if (sa == 0)
{
// It is possible that rapid group membership changes
// may cause streaming transaction be rolled back before
// commit fragment comes in. Although this is a valid
// situation, log a warning if a sac cannot be found as
// it may be an indication of a bug too.
wsrep::log_warning()
<< "Could not find applier context for "
<< ws_meta.server_id()
<< ": " << ws_meta.transaction_id();
wsrep::mutable_buffer no_error;
ret = high_priority_service.log_dummy_write_set(
ws_handle, ws_meta, no_error);
}
else
{
// Commit fragment consumes sa
sa->next_fragment(ws_meta);
ret = commit_fragment(server_state,
high_priority_service,
sa,
ws_handle,
ws_meta,
data);
}
}
}
else
{
assert(0);
}
if (ret)
{
wsrep::log_error() << "Failed to apply write set: " << ws_meta;
}
return ret;
}
static int apply_toi(wsrep::provider& provider,
wsrep::high_priority_service& high_priority_service,
const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta,
const wsrep::const_buffer& data)
{
if (wsrep::starts_transaction(ws_meta.flags()) &&
wsrep::commits_transaction(ws_meta.flags()))
{
//
// Regular TOI.
//
provider.commit_order_enter(ws_handle, ws_meta);
wsrep::mutable_buffer err;
int const apply_err(high_priority_service.apply_toi(ws_meta,data,err));
int const vote_err(provider.commit_order_leave(ws_handle, ws_meta,err));
return resolve_return_error(err.size() > 0, vote_err, apply_err);
}
else if (wsrep::starts_transaction(ws_meta.flags()))
{
provider.commit_order_enter(ws_handle, ws_meta);
wsrep::mutable_buffer err;
int const apply_err(high_priority_service.apply_nbo_begin(ws_meta, data, err));
int const vote_err(provider.commit_order_leave(ws_handle, ws_meta, err));
return resolve_return_error(err.size() > 0, vote_err, apply_err);
}
else if (wsrep::commits_transaction(ws_meta.flags()))
{
// NBO end event is ignored here, both local and applied
// have NBO end handled via local TOI calls.
provider.commit_order_enter(ws_handle, ws_meta);
wsrep::mutable_buffer err;
provider.commit_order_leave(ws_handle, ws_meta, err);
return 0;
}
else
{
assert(0);
return 0;
}
}
//////////////////////////////////////////////////////////////////////////////
// Server State //
//////////////////////////////////////////////////////////////////////////////
int wsrep::server_state::load_provider(
const std::string& provider_spec, const std::string& provider_options,
const wsrep::provider::services& services)
{
wsrep::log_info() << "Loading provider " << provider_spec
<< " initial position: " << initial_position_;
provider_ = wsrep::provider::make_provider(*this,
provider_spec,
provider_options,
services);
return (provider_ ? 0 : 1);
}
void wsrep::server_state::unload_provider()
{
delete provider_;
provider_ = 0;
}
int wsrep::server_state::connect(const std::string& cluster_name,
const std::string& cluster_address,
const std::string& state_donor,
bool bootstrap)
{
bootstrap_ = is_bootstrap(cluster_address, bootstrap);
wsrep::log_info() << "Connecting with bootstrap option: " << bootstrap_;
return provider().connect(cluster_name, cluster_address, state_donor,
bootstrap_);
}
int wsrep::server_state::disconnect()
{
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
// In case of failure situations which are caused by provider
// being shut down some failing operation may also try to shut
// down the replication. Check the state here and
// return success if the provider disconnect is already in progress
// or has completed.
if (state(lock) == s_disconnecting || state(lock) == s_disconnected)
{
return 0;
}
state(lock, s_disconnecting);
interrupt_state_waiters(lock);
}
return provider().disconnect();
}
wsrep::server_state::~server_state()
{
delete provider_;
}
std::vector<wsrep::provider::status_variable>
wsrep::server_state::status() const
{
return provider().status();
}
wsrep::seqno wsrep::server_state::pause()
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
// Disallow concurrent calls to pause to in order to have non-concurrent
// access to desynced_on_pause_ which is checked in resume() call.
wsrep::log_info() << "pause";
while (pause_count_ > 0)
{
cond_.wait(lock);
}
++pause_count_;
assert(pause_seqno_.is_undefined());
lock.unlock();
pause_seqno_ = provider().pause();
lock.lock();
if (pause_seqno_.is_undefined())
{
--pause_count_;
}
return pause_seqno_;
}
void wsrep::server_state::resume()
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
wsrep::log_info() << "resume";
assert(pause_seqno_.is_undefined() == false);
assert(pause_count_ == 1);
if (provider().resume())
{
throw wsrep::runtime_error("Failed to resume provider");
}
pause_seqno_ = wsrep::seqno::undefined();
--pause_count_;
cond_.notify_all();
}
wsrep::seqno wsrep::server_state::desync_and_pause()
{
wsrep::log_info() << "Desyncing and pausing the provider";
// Temporary variable to store desync() return status. This will be
// assigned to desynced_on_pause_ after pause() call to prevent
// concurrent access to member variable desynced_on_pause_.
bool desync_successful;
if (desync())
{
// Desync may give transient error if the provider cannot
// communicate with the rest of the cluster. However, this
// error can be tolerated because if the provider can be
// paused successfully below.
WSREP_LOG_DEBUG(wsrep::log::debug_log_level(),
wsrep::log::debug_level_server_state,
"Failed to desync server before pause");
desync_successful = false;
}
else
{
desync_successful = true;
}
wsrep::seqno ret(pause());
if (ret.is_undefined())
{
wsrep::log_warning() << "Failed to pause provider";
resync();
return wsrep::seqno::undefined();
}
else
{
desynced_on_pause_ = desync_successful;
}
wsrep::log_info() << "Provider paused at: " << ret;
return ret;
}
void wsrep::server_state::resume_and_resync()
{
wsrep::log_info() << "Resuming and resyncing the provider";
try
{
// Assign desynced_on_pause_ to local variable before resuming
// in order to avoid concurrent access to desynced_on_pause_ member
// variable.
bool do_resync = desynced_on_pause_;
desynced_on_pause_ = false;
resume();
if (do_resync)
{
resync();
}
}
catch (const wsrep::runtime_error& e)
{
wsrep::log_warning()
<< "Resume and resync failed, server may have to be restarted";
}
}
std::string wsrep::server_state::prepare_for_sst()
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
state(lock, s_joiner);
lock.unlock();
return server_service_.sst_request();
}
int wsrep::server_state::start_sst(const std::string& sst_request,
const wsrep::gtid& gtid,
bool bypass)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
state(lock, s_donor);
int ret(0);
lock.unlock();
if (server_service_.start_sst(sst_request, gtid, bypass))
{
lock.lock();
wsrep::log_warning() << "SST preparation failed";
// v26 API does not have JOINED event, so in anticipation of SYNCED
// we must do it here.
state(lock, s_joined);
ret = 1;
}
return ret;
}
void wsrep::server_state::sst_sent(const wsrep::gtid& gtid, int error)
{
if (0 == error)
wsrep::log_info() << "SST sent: " << gtid;
else
wsrep::log_info() << "SST sending failed: " << error;
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
// v26 API does not have JOINED event, so in anticipation of SYNCED
// we must do it here.
state(lock, s_joined);
lock.unlock();
enum provider::status const retval(provider().sst_sent(gtid, error));
if (retval != provider::success)
{
std::string msg("wsrep::sst_sent() returned an error: ");
msg += wsrep::provider::to_string(retval);
server_service_.log_message(wsrep::log::warning, msg.c_str());
}
}
void wsrep::server_state::sst_received(wsrep::client_service& cs,
int const error)
{
wsrep::log_info() << "SST received";
wsrep::gtid gtid(wsrep::gtid::undefined());
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(state_ == s_joiner || state_ == s_initialized);
// Run initialization only if the SST was successful.
// In case of SST failure the system is in undefined state
// may not be recoverable.
if (error == 0)
{
if (server_service_.sst_before_init())
{
if (init_initialized_ == false)
{
state(lock, s_initializing);
lock.unlock();
server_service_.debug_sync("on_view_wait_initialized");
lock.lock();
wait_until_state(lock, s_initialized);
assert(init_initialized_);
}
}
lock.unlock();
if (id_.is_undefined())
{
assert(0);
throw wsrep::runtime_error(
"wsrep::sst_received() called before connection to cluster");
}
gtid = server_service_.get_position(cs);
wsrep::log_info() << "Recovered position from storage: " << gtid;
lock.lock();
if (gtid.seqno() >= connected_gtid().seqno())
{
/* Now the node has all the data the cluster has: part in
* storage, part in replication event queue. */
state(lock, s_joined);
}
lock.unlock();
wsrep::view const v(server_service_.get_view(cs, id_));
wsrep::log_info() << "Recovered view from SST:\n" << v;
/*
* If the state id from recovered view has undefined ID, we may
* be upgrading from earlier version which does not provide
* view stored in stable storage. In this case we skip
* sanity checks and assigning the current view and wait
* until the first view delivery.
*/
if (v.state_id().id().is_undefined() == false)
{
if (v.state_id().id() != gtid.id() ||
v.state_id().seqno() > gtid.seqno())
{
/* Since IN GENERAL we may not be able to recover SST GTID from
* the state data, we have to rely on SST script passing the
* GTID value explicitly.
* Here we check if the passed GTID makes any sense: it should
* have the same UUID and greater or equal seqno than the last
* logged view. */
std::ostringstream msg;
msg << "SST script passed bogus GTID: " << gtid
<< ". Preceding view GTID: " << v.state_id();
throw wsrep::runtime_error(msg.str());
}
if (current_view_.status() == wsrep::view::primary)
{
previous_primary_view_ = current_view_;
}
current_view_ = v;
server_service_.log_view(NULL /* this view is stored already */, v);
}
else
{
wsrep::log_warning()
<< "View recovered from stable storage was empty. If the "
<< "server is doing rolling upgrade from previous version "
<< "which does not support storing view info into stable "
<< "storage, this is ok. Otherwise this may be a sign of "
<< "malfunction.";
}
lock.lock();
recover_streaming_appliers_if_not_recovered(lock, cs);
lock.unlock();
}
enum provider::status const retval(provider().sst_received(gtid, error));
if (retval != provider::success)
{
std::string msg("wsrep::sst_received() failed: ");
msg += wsrep::provider::to_string(retval);
throw wsrep::runtime_error(msg);
}
}
void wsrep::server_state::initialized()
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
wsrep::log_info() << "Server initialized";
init_initialized_ = true;
if (server_service_.sst_before_init())
{
state(lock, s_initialized);
}
else
{
state(lock, s_initializing);
state(lock, s_initialized);
}
}
enum wsrep::provider::status
wsrep::server_state::wait_for_gtid(const wsrep::gtid& gtid, int timeout)
const
{
return provider().wait_for_gtid(gtid, timeout);
}
int
wsrep::server_state::set_encryption_key(std::vector<unsigned char>& key)
{
encryption_key_ = key;
if (provider_)
{
wsrep::const_buffer const key(encryption_key_.data(),
encryption_key_.size());
enum provider::status const retval(provider_->enc_set_key(key));
if (retval != provider::success)
{
wsrep::log_error() << "Failed to set encryption key: "
<< provider::to_string(retval);
return 1;
}
}
return 0;
}
std::pair<wsrep::gtid, enum wsrep::provider::status>
wsrep::server_state::causal_read(int timeout) const
{
return provider().causal_read(timeout);
}
void wsrep::server_state::on_connect(const wsrep::view& view)
{
// Sanity checks
if (view.own_index() < 0 ||
size_t(view.own_index()) >= view.members().size())
{
std::ostringstream os;
os << "Invalid view on connect: own index out of range: " << view;
#ifndef NDEBUG
wsrep::log_error() << os.str();
assert(0);
#endif
throw wsrep::runtime_error(os.str());
}
const size_t own_index(static_cast<size_t>(view.own_index()));
if (id_.is_undefined() == false && id_ != view.members()[own_index].id())
{
std::ostringstream os;
os << "Connection in connected state.\n"
<< "Connected view:\n" << view
<< "Previous view:\n" << current_view_
<< "Current own ID: " << id_;
#ifndef NDEBUG
wsrep::log_error() << os.str();
assert(0);
#endif
throw wsrep::runtime_error(os.str());
}
else
{
id_ = view.members()[own_index].id();
}
wsrep::log_info() << "Server "
<< name_
<< " connected to cluster at position "
<< view.state_id()
<< " with ID "
<< id_;
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
connected_gtid_ = view.state_id();
state(lock, s_connected);
}
void wsrep::server_state::on_primary_view(
const wsrep::view& view,
wsrep::high_priority_service* high_priority_service)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
assert(view.final() == false);
//
// Reached primary from connected state. This may mean the following
//
// 1) Server was joined to the cluster and got SST transfer
// 2) Server was partitioned from the cluster and got back
// 3) A new cluster was bootstrapped from non-prim cluster
//
// There is no enough information here what was the cause
// of the primary component, so we need to walk through
// all states leading to joined to notify possible state
// waiters in other threads.
//
if (server_service_.sst_before_init())
{
if (state_ == s_connected)
{
state(lock, s_joiner);
// We need to assign init_initialized_ here to local
// variable. If the value here was false, we need to skip
// the initializing -> initialized -> joined state cycle
// below. However, if we don't assign the value to
// local, it is possible that the main thread gets control
// between changing the state to initializing and checking
// initialized flag, which may cause the initialzing -> initialized
// state change to be executed even if it should not be.
const bool was_initialized(init_initialized_);
state(lock, s_initializing);
if (was_initialized)
{
// If server side has already been initialized,
// skip directly to s_joined.
state(lock, s_initialized);
}
}
}
else
{
if (state_ == s_connected)
{
state(lock, s_joiner);
}
}
if (init_initialized_ == false)
{
lock.unlock();
server_service_.debug_sync("on_view_wait_initialized");
lock.lock();
wait_until_state(lock, s_initialized);
}
assert(init_initialized_);
if (bootstrap_)
{
server_service_.bootstrap();
bootstrap_ = false;
}
assert(high_priority_service);
if (high_priority_service)
{
recover_streaming_appliers_if_not_recovered(lock,
*high_priority_service);
close_orphaned_sr_transactions(lock, *high_priority_service);
}
if (state(lock) < s_joined &&
view.state_id().seqno() >= connected_gtid().seqno())
{
// If we progressed beyond connected seqno, it means we have full state
state(lock, s_joined);
}
}
void wsrep::server_state::on_non_primary_view(
const wsrep::view& view,
wsrep::high_priority_service* high_priority_service)
{
wsrep::unique_lock<wsrep::mutex> lock(mutex_);
wsrep::log_info() << "Non-primary view";
if (view.final())
{
go_final(lock, view, high_priority_service);
}
else if (state_ != s_disconnecting)
{
state(lock, s_connected);
}
}
void wsrep::server_state::go_final(wsrep::unique_lock<wsrep::mutex>& lock,
const wsrep::view& view,
wsrep::high_priority_service* hps)