forked from Expensify/Bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBedrockServer.cpp
More file actions
1867 lines (1657 loc) · 92.1 KB
/
BedrockServer.cpp
File metadata and controls
1867 lines (1657 loc) · 92.1 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
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
// Manages connections to a single instance of the bedrock server.
#include <libstuff/libstuff.h>
#include "BedrockServer.h"
#include "BedrockPlugin.h"
#include "BedrockConflictMetrics.h"
#include "BedrockCore.h"
#include <iomanip>
set<string>BedrockServer::_blacklistedParallelCommands;
recursive_mutex BedrockServer::_blacklistedParallelCommandMutex;
void BedrockServer::acceptCommand(SQLiteCommand&& command, bool isNew) {
// If the sync node tells us that a command causes a crash, we immediately save that.
if (SIEquals(command.request.methodLine, "CRASH_COMMAND")) {
SData request;
request.deserialize(command.request.content);
// Take a unique lock so nobody else can read from this table while we update it.
unique_lock<decltype(_crashCommandMutex)> lock(_crashCommandMutex);
// Add the blacklisted command to the map.
_crashCommands[request.methodLine].insert(request.nameValueMap);
size_t totalCount = 0;
for (const auto& s : _crashCommands) {
totalCount += s.second.size();
}
SALERT("Blacklisting command (now have " << totalCount << " blacklisted commands): " << request.serialize());
} else {
if (SIEquals(command.request.methodLine, "BROADCAST_COMMAND")) {
SData newRequest;
newRequest.deserialize(command.request.content);
command.request = newRequest;
command.initiatingClientID = -1;
command.initiatingPeerID = 0;
}
SAUTOPREFIX(command.request["requestID"]);
if (command.writeConsistency != SQLiteNode::QUORUM
&& _syncCommands.find(command.request.methodLine) != _syncCommands.end()) {
command.writeConsistency = SQLiteNode::QUORUM;
SINFO("Forcing QUORUM consistency for command " << command.request.methodLine);
}
SINFO("Queued new '" << command.request.methodLine << "' command from bedrock node, with " << _commandQueue.size()
<< " commands already queued.");
_commandQueue.push(BedrockCommand(move(command)));
if (!isNew) {
// If the command isn't new, then we already think it's in progress, but it's been returned to us, so reset
// that.
_commandsInProgress--;
}
}
}
void BedrockServer::cancelCommand(const string& commandID) {
_commandQueue.removeByID(commandID);
}
bool BedrockServer::canStandDown() {
int count = _commandsInProgress.load();
int size = _commandQueue.size();
if (count || size) {
SINFO("Can't stand down with " << count << " commands in progress and " << size << " commands queued.");
return false;
} else {
return true;
}
}
void BedrockServer::syncWrapper(SData& args,
atomic<SQLiteNode::State>& replicationState,
atomic<bool>& upgradeInProgress,
atomic<string>& masterVersion,
CommandQueue& syncNodeQueuedCommands,
BedrockServer& server)
{
while(true) {
// If the server's set to be detached, we wait until that flag is unset, and then start the sync thread.
if (server._detach) {
// If we're set detached, we assume we'll be re-attached eventually, and then be `RUNNING`.
SINFO("Bedrock server entering detached state.");
server._shutdownState.store(RUNNING);
while (server._detach) {
// Just wait until we're attached.
SINFO("Bedrock server sleeping in detached state.");
sleep(1);
}
SINFO("Bedrock server entering attached state.");
}
sync(args, replicationState, upgradeInProgress, masterVersion, syncNodeQueuedCommands, server);
// Now that we've run the sync thread, we can exit if it hasn't set _detach again.
if (!server._detach) {
break;
}
}
}
void BedrockServer::sync(SData& args,
atomic<SQLiteNode::State>& replicationState,
atomic<bool>& upgradeInProgress,
atomic<string>& masterVersion,
CommandQueue& syncNodeQueuedCommands,
BedrockServer& server)
{
// Initialize the thread.
SInitialize(_syncThreadName);
// We currently have no commands in progress.
server._commandsInProgress.store(0);
// Parse out the number of worker threads we'll use. The DB needs to know this because it will expect a
// corresponding number of journal tables. "-readThreads" exists only for backwards compatibility.
int workerThreads = args.calc("-workerThreads");
// TODO: remove when nothing uses readThreads.
workerThreads = workerThreads ? workerThreads : args.calc("-readThreads");
// If still no value, use the number of cores on the machine, if available.
workerThreads = workerThreads ? workerThreads : max(1u, thread::hardware_concurrency());
// Initialize the DB.
SQLite db(args["-db"], args.calc("-cacheSize"), true, args.calc("-maxJournalSize"), -1, workerThreads - 1, args["-synchronous"]);
// And the command processor.
BedrockCore core(db, server);
// And the sync node.
uint64_t firstTimeout = STIME_US_PER_M * 2 + SRandom::rand64() % STIME_US_PER_S * 30;
// Initialize the shared pointer to our sync node object.
server._syncNode = make_shared<SQLiteNode>(server, db, args["-nodeName"], args["-nodeHost"],
args["-peerList"], args.calc("-priority"), firstTimeout,
server._version, args.calc("-quorumCheckpoint"));
// We keep a queue of completed commands that workers will insert into when they've successfully finished a command
// that just needs to be returned to a peer.
CommandQueue completedCommands;
// The node is now coming up, and should eventually end up in a `MASTERING` or `SLAVING` state. We can start adding
// our worker threads now. We don't wait until the node is `MASTERING` or `SLAVING`, as it's state can change while
// it's running, and our workers will have to maintain awareness of that state anyway.
SINFO("Starting " << workerThreads << " worker threads.");
list<thread> workerThreadList;
for (int threadId = 0; threadId < workerThreads; threadId++) {
workerThreadList.emplace_back(worker,
ref(args),
ref(replicationState),
ref(upgradeInProgress),
ref(masterVersion),
ref(syncNodeQueuedCommands),
ref(completedCommands),
ref(server),
threadId,
workerThreads);
}
// Now we jump into our main command processing loop.
uint64_t nextActivity = STimeNow();
BedrockCommand command;
bool committingCommand = false;
// We hold a lock here around all operations on `syncNode`, because `SQLiteNode` isn't thread-safe, but we need
// `BedrockServer` to be able to introspect it in `Status` requests. We hold this lock at all times until exiting
// our main loop, aside from when we're waiting on `poll`. Strictly, we could hold this lock less often, but there
// are not that many status commands coming in, and they can wait for a fraction of a second, which lets us keep
// the logic of this loop simpler.
server._syncMutex.lock();
do {
// Make sure the existing command prefix is still valid since they're reset when SAUTOPREFIX goes out of scope.
SAUTOPREFIX(command.request["requestID"]);
// If there were commands waiting on our commit count to come up-to-date, we'll move them back to the main
// command queue here. There's no place in particular that's best to do this, so we do it at the top of this
// main loop, as that prevents it from ever getting skipped in the event that we `continue` early from a loop
// iteration.
// We also move all commands back to the main queue here if we're shutting down, just to make sure they don't
// end up lost in the ether.
{
SAUTOLOCK(server._futureCommitCommandMutex);
if (!server._futureCommitCommands.empty()) {
uint64_t commitCount = db.getCommitCount();
auto it = server._futureCommitCommands.begin();
auto& eraseTo = it;
while (it != server._futureCommitCommands.end() && (it->first <= commitCount || server._shutdownState.load() != RUNNING)) {
SINFO("Returning command (" << it->second.request.methodLine << ") waiting on commit " << it->first
<< " to queue, now have commit " << commitCount);
server._commandQueue.push(move(it->second));
server._commandsInProgress--;
eraseTo = it;
it++;
}
if (eraseTo != server._futureCommitCommands.begin()) {
server._futureCommitCommands.erase(server._futureCommitCommands.begin(), eraseTo);
}
}
}
// If we're in a state where we can initialize shutdown, then go ahead and do so.
// Having responded to all clients means there are no *local* clients, but it doesn't mean there are no
// escalated commands. This is fine though - if we're slaving, there can't be any escalated commands, and if
// we're mastering, then the next update() loop will set us to standing down, and then we won't accept any new
// commands, and we'll shortly run through the existing queue.
if (server._shutdownState.load() == CLIENTS_RESPONDED) {
// The total time we'll wait for the sync node is whatever we haven't already waited from the original
// timeout, minus 5 seconds to allow to clean up afterward.
int64_t timeAllowed = server._gracefulShutdownTimeout.alarmDuration.load() - server._gracefulShutdownTimeout.elapsed();
timeAllowed -= 5'000'000;
server._syncNode->beginShutdown(max(timeAllowed, 1l));
}
// The fd_map contains a list of all file descriptors (eg, sockets, Unix pipes) that poll will wait on for
// activity. Once any of them has activity (or the timeout ends), poll will return.
fd_map fdm;
// Prepare our plugins for `poll` (for instance, in case they're making HTTP requests).
server._prePollPlugins(fdm);
// Pre-process any sockets the sync node is managing (i.e., communication with peer nodes).
server._syncNode->prePoll(fdm);
// Add our command queues to our fd_map.
syncNodeQueuedCommands.prePoll(fdm);
completedCommands.prePoll(fdm);
// Wait for activity on any of those FDs, up to a timeout.
const uint64_t now = STimeNow();
// Unlock our mutex, poll, and re-lock when finished.
server._syncMutex.unlock();
S_poll(fdm, max(nextActivity, now) - now);
server._syncMutex.lock();
// And set our next timeout for 1 second from now.
nextActivity = STimeNow() + STIME_US_PER_S;
// Process any network traffic that happened. Scope this so that we can change the log prefix and have it
// auto-revert when we're finished.
{
SAUTOPREFIX("xxxxxx");
// Process any activity in our plugins.
server._postPollPlugins(fdm, nextActivity);
server._syncNode->postPoll(fdm, nextActivity);
syncNodeQueuedCommands.postPoll(fdm);
completedCommands.postPoll(fdm);
}
// Ok, let the sync node to it's updating for as many iterations as it requires. We'll update the replication
// state when it's finished.
SQLiteNode::State preUpdateState = server._syncNode->getState();
while (server._syncNode->update()) {}
SQLiteNode::State nodeState = server._syncNode->getState();
replicationState.store(nodeState);
masterVersion.store(server._syncNode->getMasterVersion());
// If anything was in the stand down queue, move it back to the main queue.
if (nodeState != SQLiteNode::STANDINGDOWN) {
while (server._standDownQueue.size()) {
server._commandQueue.push(server._standDownQueue.pop());
}
} else if (preUpdateState != SQLiteNode::STANDINGDOWN) {
// Otherwise,if we just started standing down, discard any commands that had been scheduled in the future.
// In theory, it should be fine to keep these, as they shouldn't have sockets associated with them, and
// they could be re-escalated to master in the future, but there's not currently a way to decide if we've
// run through all of the commands that might need peer responses before standing down aside from seeing if
// the entire queue is empty.
server._commandQueue.abandonFutureCommands(5000);
}
// If we're not mastering, we turn off multi-write until we've finished upgrading the DB. This persists until
// after we're mastering again.
if (nodeState != SQLiteNode::MASTERING) {
server._suppressMultiWrite.store(true);
}
// If the node's not in a ready state at this point, we'll probably need to read from the network, so start the
// main loop over. This can let us wait for logins from peers (for example).
if (nodeState != SQLiteNode::MASTERING &&
nodeState != SQLiteNode::SLAVING &&
nodeState != SQLiteNode::STANDINGDOWN) {
continue;
}
// If we've just switched to the mastering state, we want to upgrade the DB. We'll set a global flag to let
// worker threads know that a DB upgrade is in progress, and start the upgrade process, which works basically
// like a regular distributed commit.
if (preUpdateState != SQLiteNode::MASTERING && nodeState == SQLiteNode::MASTERING) {
if (server._upgradeDB(db)) {
upgradeInProgress.store(true);
server._syncThreadCommitMutex.lock();
committingCommand = true;
server._commandsInProgress++;
server._syncNode->startCommit(SQLiteNode::QUORUM);
// As it's a quorum commit, we'll need to read from peers. Let's start the next loop iteration.
continue;
} else {
// If we're not doing an upgrade, we don't need to keep suppressing multi-write.
server._suppressMultiWrite.store(false);
}
} else if ((preUpdateState == SQLiteNode::MASTERING || preUpdateState == SQLiteNode::STANDINGDOWN)
&& nodeState == SQLiteNode::SEARCHING) {
// If we were MASTERING, but now we're searching, then something's gone wrong (perhaps we got disconnected
// from the cluster). We should give up an any commands, and let them be re-escalated. If commands were
// initiated locally, we can just re-queue them, they will get re-checked once things clear up, and then
// they'll get processed here, or escalated to the new master.
// Commands initiated on slaves just get dropped, they will need to be re-escalated, potentially to a
// different master.
int requeued = 0;
int dropped = 0;
try {
while (true) {
command = syncNodeQueuedCommands.pop();
if (command.initiatingClientID) {
// This one came from a local client, so we can save it for later.
server._commandQueue.push(move(command));
server._commandsInProgress--;
}
}
} catch (const out_of_range& e) {
SWARN("Abruptly stopped MASTERING. Re-queued " << requeued << " commands, Dropped " << dropped << " commands.");
}
}
// If we started a commit, and one's not in progress, then we've finished it and we'll take that command and
// stick it back in the appropriate queue.
if (committingCommand && !server._syncNode->commitInProgress()) {
// It should be impossible to get here if we're not mastering or standing down.
if (nodeState != SQLiteNode::MASTERING && nodeState != SQLiteNode::STANDINGDOWN) {
SERROR("Committing command but node state is " << SQLiteNode::stateNames[nodeState]);
}
// Record the time spent.
command.stopTiming(BedrockCommand::COMMIT_SYNC);
// We're done with the commit, we unlock our mutex and decrement our counter.
server._syncThreadCommitMutex.unlock();
committingCommand = false;
if (server._syncNode->commitSucceeded()) {
// If we were upgrading, there's no response to send, we're just done.
if (upgradeInProgress.load()) {
upgradeInProgress.store(false);
server._suppressMultiWrite.store(false);
server._commandsInProgress--;
continue;
}
BedrockConflictMetrics::recordSuccess(command.request.methodLine);
SINFO("[performance] Sync thread finished committing command " << command.request.methodLine);
// Otherwise, save the commit count, mark this command as complete, and reply.
command.response["commitCount"] = to_string(db.getCommitCount());
command.complete = true;
if (command.initiatingPeerID) {
// This is a command that came from a peer. Have the sync node send the response back to the peer.
server._finishPeerCommand(command);
} else {
// The only other option is this came from a client, so respond via the server.
server._reply(command);
}
} else {
// TODO: This `else` block should be unreachable since the sync thread now blocks workers for entire
// transactions. It should probably be removed, but we'll leave it in for the time being until the
// final implementation of multi-write is stabilized.
BedrockConflictMetrics::recordConflict(command.request.methodLine);
// If the commit failed, then it must have conflicted, so we'll re-queue it to try again.
SINFO("[performance] Conflict committing in sync thread, requeueing command "
<< command.request.methodLine << ". Sync thread has "
<< syncNodeQueuedCommands.size() << " queued commands.");
syncNodeQueuedCommands.push(move(command));
}
// Prevent the requestID from a finished command from being used.
command.request.clear();
}
// We're either mastering, standing down, or slaving. There could be a commit in progress on `command`, but
// there could also be other finished work to handle while we wait for that to complete. Let's see if we can
// handle any of that work.
try {
// If there are any completed commands to respond to, we'll do that first.
try {
while (true) {
BedrockCommand completedCommand = completedCommands.pop();
SAUTOPREFIX(completedCommand.request["requestID"]);
SASSERT(completedCommand.complete);
SASSERT(completedCommand.initiatingPeerID);
SASSERT(!completedCommand.initiatingClientID);
server._finishPeerCommand(completedCommand);
}
} catch (const out_of_range& e) {
// when completedCommands.pop() throws for running out of commands, we fall out of the loop.
}
// We don't start processing a new command until we've completed any existing ones.
if (committingCommand) {
continue;
}
// Get the next sync node command to work on.
command = syncNodeQueuedCommands.pop();
// We got a command to work on! Set our log prefix to the request ID.
SAUTOPREFIX(command.request["requestID"]);
SINFO("[performance] Sync thread dequeued command " << command.request.methodLine << ". Sync thread has "
<< syncNodeQueuedCommands.size() << " queued commands.");
// Set the function that will be called if this thread's signal handler catches an unrecoverable error,
// like a segfault. Note that it's possible we're in the middle of sending a message to peers when we call
// this, which would probably make this message malformed. This is the best we can do.
SSetSignalHandlerDieFunc([&](){
server._syncNode->broadcast(_generateCrashMessage(&command));
});
// And now we'll decide how to handle it.
if (nodeState == SQLiteNode::MASTERING || nodeState == SQLiteNode::STANDINGDOWN) {
// We need to grab this before peekCommand (or wherever our transaction is started), to verify that
// no worker thread can commit in the middle of our transaction. We need our entire transaction to
// happen with no other commits to ensure that we can't get a conflict.
uint64_t beforeLock = STimeNow();
// This needs to be done before we acquire _syncThreadCommitMutex or we can deadlock.
db.waitForCheckpoint();
server._syncThreadCommitMutex.lock();
// It appears that this might be taking significantly longer with multi-write enabled, so we're adding
// explicit logging for it to check.
SINFO("[performance] Waited " << (STimeNow() - beforeLock) << "us for _syncThreadCommitMutex.");
// We peek commands here in the sync thread to be able to run peek and process as part of the same
// transaction. This guarantees that any checks made in peek are still valid in process, as the DB can't
// have changed in the meantime.
// IMPORTANT: This check is omitted for commands with an HTTPS request object, because we don't want to
// risk duplicating that request. If your command creates an HTTPS request, it needs to explicitly
// re-verify that any checks made in peek are still valid in process.
if (!command.httpsRequest) {
if (core.peekCommand(command)) {
// Finished with this.
server._syncThreadCommitMutex.unlock();
// This command completed in peek, respond to it appropriately, either directly or by sending it
// back to the sync thread.
SASSERT(command.complete);
if (command.initiatingPeerID) {
server._finishPeerCommand(command);
} else {
server._reply(command);
}
continue;
}
// If we just started a new HTTPS request, save it for later.
if (command.httpsRequest) {
// Save this in our https commads queue.
lock_guard<mutex> lock(server._httpsCommandMutex);
server._outstandingHTTPSRequests.emplace(make_pair(command.httpsRequest, move(command)));
// Move on to the next command until this one finishes.
core.rollback();
server._syncThreadCommitMutex.unlock();
continue;
}
}
if (core.processCommand(command)) {
// The processor says we need to commit this, so let's start that process.
committingCommand = true;
SINFO("[performance] Sync thread beginning committing command " << command.request.methodLine);
// START TIMING.
command.startTiming(BedrockCommand::COMMIT_SYNC);
server._syncNode->startCommit(command.writeConsistency);
// And we'll start the next main loop.
// NOTE: This will cause us to read from the network again. This, in theory, is fine, but we saw
// performance problems in the past trying to do something similar on every commit. This may be
// alleviated now that we're only doing this on *sync* commits instead of all commits, which should
// be a much smaller fraction of all our traffic. We set nextActivity here so that there's no
// timeout before we'll give up on poll() if there's nothing to read.
nextActivity = STimeNow();
// Don't unlock _syncThreadCommitMutex here, we'll hold the lock till the commit completes.
continue;
} else {
// Otherwise, the command doesn't need a commit (maybe it was an error, or it didn't have any work
// to do). We'll just respond.
server._syncThreadCommitMutex.unlock();
if (command.initiatingPeerID) {
server._finishPeerCommand(command);
} else {
server._reply(command);
}
}
} else if (nodeState == SQLiteNode::SLAVING) {
// If we're slaving, we just escalate directly to master without peeking. We can only get an incomplete
// command on the slave sync thread if a slave worker thread peeked it unsuccessfully, so we don't
// bother peeking it again.
auto it = command.request.nameValueMap.find("Connection");
bool forget = it != command.request.nameValueMap.end() && SIEquals(it->second, "forget");
server._syncNode->escalateCommand(move(command), forget);
if (forget) {
// Command is no longer in progress.
server._commandsInProgress--;
}
}
} catch (const out_of_range& e) {
// Prevent the requestID from a finished command from being used.
command.request.clear();
// syncNodeQueuedCommands had no commands to work on, we'll need to re-poll for some.
continue;
}
} while (!server._syncNode->shutdownComplete() && !server._gracefulShutdownTimeout.ringing());
SSetSignalHandlerDieFunc([](){SWARN("Dying in shutdown");});
// If we forced a shutdown mid-transaction (this can happen, if, for instance, we hit our graceful timeout between
// getting a `BEGIN_TRANSACTION` and `COMMIT_TRANSACTION`) then we need to roll back the existing transaction and
// release the lock.
if (server._syncNode->commitInProgress()) {
SWARN("Shutting down mid-commit. Rolling back.");
db.rollback();
server._syncThreadCommitMutex.unlock();
}
// Done with the global lock.
server._syncMutex.unlock();
// We've finished shutting down the sync node, tell the workers that it's finished.
server._shutdownState.store(DONE);
SINFO("Sync thread finished with commands.");
// We just fell out of the loop where we were waiting for shutdown to complete. Update the state one last time when
// the writing replication thread exits.
replicationState.store(server._syncNode->getState());
if (replicationState.load() > SQLiteNode::WAITING) {
// This is because the graceful shutdown timer fired and syncNode.shutdownComplete() returned `true` above, but
// the server still thinks it's in some other state. We can only exit if we're in state <= SQLC_SEARCHING,
// (per BedrockServer::shutdownComplete()), so we force that state here to allow the shutdown to proceed.
SWARN("Sync thread exiting in state " << replicationState.load() << ". Setting to SEARCHING.");
replicationState.store(SQLiteNode::SEARCHING);
} else {
SINFO("Sync thread exiting, setting state to: " << replicationState.load());
}
// Wait for the worker threads to finish.
int threadId = 0;
for (auto& workerThread : workerThreadList) {
SINFO("Joining worker thread '" << "worker" << threadId << "'");
threadId++;
workerThread.join();
}
// If there's anything left in the command queue here, we'll discard it, because we have no way of processing it.
if (server._commandQueue.size()) {
SWARN("Sync thread shut down with " << server._commandQueue.size() << " queued commands. Commands were: "
<< SComposeList(server._commandQueue.getRequestMethodLines()) << ". Clearing.");
server._commandQueue.clear();
}
// Release our handle to this pointer. Any other functions that are still using it will keep the object alive
// until they return.
server._syncNode = nullptr;
// We're really done, store our flag so main() can be aware.
server._syncThreadComplete.store(true);
}
void BedrockServer::worker(SData& args,
atomic<SQLiteNode::State>& replicationState,
atomic<bool>& upgradeInProgress,
atomic<string>& masterVersion,
CommandQueue& syncNodeQueuedCommands,
CommandQueue& syncNodeCompletedCommands,
BedrockServer& server,
int threadId,
int threadCount)
{
SInitialize("worker" + to_string(threadId));
SQLite db(args["-db"], args.calc("-cacheSize"), false, args.calc("-maxJournalSize"), threadId, threadCount - 1, args["-synchronous"]);
BedrockCore core(db, server);
// Command to work on. This default command is replaced when we find work to do.
BedrockCommand command;
// We just run this loop looking for commands to process forever. There's a check for appropriate exit conditions
// at the bottom, which will cause our loop and thus this thread to exit when that becomes true.
while (true) {
try {
// Set a signal handler function that we can call even if we die early with no command.
SSetSignalHandlerDieFunc([&](){
SWARN("Die function called early with no command, probably died in `getSynchronized`.");
});
// If we can't find any work to do, this will throw. If we can, this will increment _commandsInProgress for
// us before returning the command that it is dequeuing. We don't update _commandsInProgress before calling
// this, as it can spend up to a second finding out that there is no command to dequeue, which makes our
// count wrong while we wait.
command = server._commandQueue.getSynchronized(1000000, server._commandsInProgress);
SAUTOPREFIX(command.request["requestID"]);
SINFO("[performance] Dequeued command " << command.request.methodLine << " in worker, "
<< server._commandQueue.size() << " commands in queue.");
// Set the function that lets the signal handler know which command caused a problem, in case that happens.
// If a signal is caught on this thread, which should only happen for unrecoverable, yet synchronous
// signals, like SIGSEGV, this function will be called.
SSetSignalHandlerDieFunc([&](){
server._syncNode->broadcast(_generateCrashMessage(&command));
});
// If we dequeue a status or control command, handle it immediately.
if (server._handleIfStatusOrControlCommand(command)) {
server._commandsInProgress--;
continue;
}
// Check if this command would be likely to cause a crash
if (server._wouldCrash(command)) {
// If so, make a lot of noise, and respond 500 without processing it.
SALERT("CRASH-INDUCING COMMAND FOUND: " << command.request.methodLine);
command.response.methodLine = "500 Refused";
command.complete = true;
if (command.initiatingPeerID) {
// Escalated command. Give it back to the sync thread to respond.
syncNodeCompletedCommands.push(move(command));
} else {
server._reply(command);
}
continue;
}
// If this was a command initiated by a peer as part of a cluster operation, then we process it separately
// and respond immediately. This allows SQLiteNode to offload read-only operations to worker threads.
if (SQLiteNode::peekPeerCommand(server._syncNode.get(), db, command)) {
server._commandsInProgress--;
// Move on to the next command.
continue;
}
// We just spin until the node looks ready to go. Typically, this doesn't happen expect briefly at startup.
while (upgradeInProgress.load() ||
(replicationState.load() != SQLiteNode::MASTERING &&
replicationState.load() != SQLiteNode::SLAVING &&
replicationState.load() != SQLiteNode::STANDINGDOWN)
) {
// Make sure that the node isn't shutting down, leaving us in an endless loop.
if (server._shutdownState.load() != RUNNING) {
SWARN("Sync thread shut down while were waiting for it to come up. Discarding command '"
<< command.request.methodLine << "'.");
return;
}
// This sleep call is pretty ugly, but it should almost never happen. We're accepting the potential
// looping sleep call for the general case where we just check some bools and continue, instead of
// avoiding the sleep call but having every thread lock a mutex here on every loop.
usleep(10000);
}
// If this command is dependent on a commitCount newer than what we have (maybe it's a follow-up to a
// command that was escalated to master), we'll set it aside for later processing. When the sync node
// finishes its update loop, it will re-queue any of these commands that are no longer blocked on our
// updated commit count.
uint64_t commitCount = db.getCommitCount();
uint64_t commandCommitCount = command.request.calcU64("commitCount");
if (commandCommitCount > commitCount) {
SAUTOLOCK(server._futureCommitCommandMutex);
auto newQueueSize = server._futureCommitCommands.size() + 1;
SINFO("Command (" << command.request.methodLine << ") depends on future commit(" << commandCommitCount
<< "), Currently at: " << commitCount << ", storing for later. Queue size: " << newQueueSize);
server._futureCommitCommands.insert(make_pair(commandCommitCount, move(command)));
if (newQueueSize > 100) {
SHMMM("server._futureCommitCommands.size() == " << newQueueSize);
}
continue;
}
// OK, so this is the state right now, which isn't necessarily anything in particular, because the sync
// node can change it at any time, and we're not synchronizing on it. We're going to go ahead and assume
// it's something reasonable, because in most cases, that's pretty safe. If we think we're anything but
// MASTERING, we'll just peek this command and return it's result, which should be harmless. If we think
// we're mastering, we'll go ahead and start a `process` for the command, but we'll synchronously verify
// our state right before we commit.
SQLiteNode::State state = replicationState.load();
// If we find that we've gotten a command with an initiatingPeerID, but we're not in a mastering or
// standing down state, we'll have no way of returning this command to the caller, so we discard it. The
// original caller will need to re-send the request. This can happen if we're mastering, and receive a
// request from a peer, but then we stand down from mastering. The SQLiteNode should have already told its
// peers that their outstanding requests were being canceled at this point.
if (command.initiatingPeerID && !(state == SQLiteNode::MASTERING || SQLiteNode::STANDINGDOWN)) {
SWARN("Found " << (command.complete ? "" : "in") << "complete " << "command "
<< command.request.methodLine << " from peer, but not mastering. Too late for it, discarding.");
server._commandsInProgress--;
continue;
}
// If this command is already complete, then we should be a slave, and the sync node got a response back
// from a command that had been escalated to master, and queued it for a worker to respond to. We'll send
// that response now.
if (command.complete) {
// If this command is already complete, we can return it to the caller.
// If it has an initiator, it should have been returned to a peer by a sync node instead, but if we've
// just switched states out of mastering, we might have an old command in the queue. All we can do here
// is note that and discard it, as we have nobody to deliver it to.
if (command.initiatingPeerID) {
// Let's note how old this command is.
uint64_t ageSeconds = (STimeNow() - command.creationTime) / STIME_US_PER_S;
SWARN("Found unexpected complete command " << command.request.methodLine
<< " from peer in worker thread. Discarding (command was " << ageSeconds << "s old).");
server._commandsInProgress--;
continue;
}
// Make sure we have an initiatingClientID at this point. If we do, but it's negative, it's for a
// client that we can't respond to, so we don't bother sending the response.
SASSERT(command.initiatingClientID);
if (command.initiatingClientID > 0) {
server._reply(command);
}
// This command is done, move on to the next one.
continue;
}
if (command.request.isSet("mockRequest")) {
SINFO("mockRequest set for command '" << command.request.methodLine << "'.");
}
// We'll retry on conflict up to this many times.
int retry = 3;
// We check first, and allow this command to retry all three times, even if it becomes disallowed during
// iteration.
bool multiWriteOK = BedrockConflictMetrics::multiWriteOK(command.request.methodLine);
while (retry) {
// Block if a checkpoint is happening so we don't interrupt it.
db.waitForCheckpoint();
// If the command doesn't already have an httpsRequest from a previous peek attempt, try peeking it
// now. We don't duplicate peeks for commands that make https requests.
// If peek succeeds, then it's finished, and all we need to do is respond to the command at the bottom.
bool calledPeek = false;
bool peekResult = false;
if (!command.httpsRequest) {
peekResult = core.peekCommand(command);
calledPeek = true;
}
if (!calledPeek || !peekResult) {
// We've just unsuccessfully peeked a command, which means we're in a state where we might want to
// write it. We'll flag that here, to keep the node from falling out of MASTERING/STANDINGDOWN
// until we're finished with this command.
if (command.httpsRequest) {
// This *should* be impossible, but previous bugs have existed where it's feasible that we call
// `peekCommand` while mastering, and by the time we're done, we're SLAVING, so we check just
// in case we ever introduce another similar bug.
if (state != SQLiteNode::MASTERING && state != SQLiteNode::STANDINGDOWN) {
SALERT("Not mastering or standing down (" << SQLiteNode::stateNames[state]
<< ") but have outstanding HTTPS command: " << command.request.methodLine
<< ", returning 500.");
command.response.methodLine = "500 STANDDOWN TIMEOUT";
server._reply(command);
core.rollback();
break;
}
// If the command isn't complete, we'll move it into our map of outstanding HTTPS requests.
if (!command.httpsRequest->response) {
// Roll back the existing transaction, but only if we are inside an transaction
if (calledPeek) {
core.rollback();
}
// We're not handling a writable command anymore (at the moment). We need to make sure we
// don't shut down without checking for outstanding HTTPS commands.
lock_guard<mutex> lock(server._httpsCommandMutex);
SINFO("[performance] Waiting on HTTPS response " << command.request.methodLine
<< ", " << server._outstandingHTTPSRequests.size() << " queued HTTPS commands.");
// Save this in our https commads queue.
server._outstandingHTTPSRequests.emplace(make_pair(command.httpsRequest, move(command)));
// Move on to the next command until this one finishes.
break;
}
}
// Peek wasn't enough to handle this command. Now we need to decide if we should try and process
// it, or if we should send it off to the sync node.
bool canWriteParallel = server._multiWriteEnabled.load();
if (canWriteParallel) {
// If multi-write is enabled, then we need to make sure the command isn't blacklisted.
SAUTOLOCK(_blacklistedParallelCommandMutex);
canWriteParallel =
(_blacklistedParallelCommands.find(command.request.methodLine) == _blacklistedParallelCommands.end());
}
// We need to have multi-write enabled, the command needs to not be explicitly blacklisted, and it
// needs to not be automatically blacklisted.
canWriteParallel = canWriteParallel && multiWriteOK;
if (!canWriteParallel ||
server._suppressMultiWrite.load() ||
state != SQLiteNode::MASTERING ||
command.onlyProcessOnSyncThread ||
command.writeConsistency != SQLiteNode::ASYNC)
{
// Roll back the transaction, it'll get re-run in the sync thread.
core.rollback();
// We're not handling a writable command anymore.
SINFO("[performance] Sending non-parallel command " << command.request.methodLine
<< " to sync thread. Sync thread has " << syncNodeQueuedCommands.size()
<< " queued commands.");
syncNodeQueuedCommands.push(move(command));
// We'll break out of our retry loop here, as we don't need to do anything else, we can just
// look for another command to work on.
break;
} else {
// In this case, there's nothing blocking us from processing this in a worker, so let's try it.
if (core.processCommand(command)) {
// If processCommand returned true, then we need to do a commit. Otherwise, the command is
// done, and we just need to respond. Before we commit, we need to grab the sync thread
// lock. Because the sync thread grabs an exclusive lock on this wrapping any transactions
// that it performs, we'll get this lock while the sync thread isn't in the process of
// handling a transaction, thus guaranteeing that we can't commit and cause a conflict on
// the sync thread. We can still get conflicts here, as the sync thread might have
// performed a transaction after we called `processCommand` and before we call `commit`,
// or we could conflict with another worker thread, but the sync thread will never see a
// conflict as long as we don't commit while it's performing a transaction. This is scoped
// to the minimum time required.
bool commitSuccess = false;
{
uint64_t preLockTime = STimeNow();
shared_lock<decltype(server._syncThreadCommitMutex)> lock1(server._syncThreadCommitMutex);
SINFO("_syncThreadCommitMutex acquired in worker in " << fixed << setprecision(2)
<< ((STimeNow() - preLockTime)/1000.0) << "ms.");
// This is the first place we get really particular with the state of the node from a
// worker thread. We only want to do this commit if we're *SURE* we're mastering, and
// not allow the state of the node to change while we're committing. If it turns out
// we've changed states, we'll roll this command back, so we lock the node's state
// until we complete.
//
// IMPORTANT: If we acquire both _syncThreadCommitMutex and stateMutex, they always
// need to be locked in that order. The reason for this is that it's possible for the
// sync thread to to change states mid-commit, meaning that it needs to acquire these
// locks in the same order. Always acquiring the locks in the same order prevents the
// deadlocks.
shared_lock<decltype(server._syncNode->stateMutex)> lock2(server._syncNode->stateMutex);
if (replicationState.load() != SQLiteNode::MASTERING &&
replicationState.load() != SQLiteNode::STANDINGDOWN) {
SALERT("Node State changed from MASTERING to "
<< SQLiteNode::stateNames[replicationState.load()]
<< " during worker commit. Rolling back transaction!");
core.rollback();
} else {
BedrockCore::AutoTimer(command, BedrockCommand::COMMIT_WORKER);
commitSuccess = core.commit();
}
}
if (commitSuccess) {
BedrockConflictMetrics::recordSuccess(command.request.methodLine);
SINFO("Successfully committed " << command.request.methodLine << " on worker thread.");
// So we must still be mastering, and at this point our commit has succeeded, let's
// mark it as complete. We add the currentCommit count here as well.
command.response["commitCount"] = to_string(db.getCommitCount());
command.complete = true;
} else {
SINFO("Conflict or state change committing " << command.request.methodLine
<< " on worker thread with " << retry << " retries remaining.");
}
}
}
}
// If the command was completed above, then we'll go ahead and respond. Otherwise there must have been
// a conflict, and we'll retry.
if (command.complete) {
if (command.initiatingPeerID) {
// Escalated command. Give it back to the sync thread to respond.
syncNodeCompletedCommands.push(move(command));
} else {
server._reply(command);
}
// Don't need to retry.
break;
}
// We're about to retry, decrement the retry count.
--retry;
}
// We ran out of retries without finishing! We give it to the sync thread.
if (!retry) {
BedrockConflictMetrics::recordConflict(command.request.methodLine);
SINFO("[performance] Max retries hit in worker, forwarding command " << command.request.methodLine
<< " to sync thread. Sync thread has " << syncNodeQueuedCommands.size() << " queued commands.");
syncNodeQueuedCommands.push(move(command));
}
} catch (const BedrockCommandQueue::timeout_error& e) {
// No commands to process after 1 second.
// If the sync node has shut down, we can return now, there will be no more work to do.
if (server._shutdownState.load() == DONE) {
SINFO("No commands found in queue and DONE.");
return;
}
}
// If we hit the timeout, doesn't matter if we've got work to do. Exit.
if (server._gracefulShutdownTimeout.ringing()) {
SINFO("_shutdownState is DONE and we've timed out, exiting worker.");
return;
}
}
}
bool BedrockServer::_handleIfStatusOrControlCommand(BedrockCommand& command) {
if (_isStatusCommand(command)) {
_commandsInProgress++;
_status(command);
_reply(command);
return true;
} else if (_isControlCommand(command)) {
_commandsInProgress++;
// Control commands can only come from localhost (and thus have an empty `_source`).
if (command.request["_source"].empty()) {
_control(command);
} else {
SWARN("Got control command " << command.request.methodLine << " on non-localhost socket ("
<< command.request["_source"] << "). Ignoring.");
command.response.methodLine = "401 Unauthorized";
}
_reply(command);
return true;
}
return false;
}
bool BedrockServer::_wouldCrash(const BedrockCommand& command) {
// Get a shared lock so that all the workers can look at this map simultaneously.
shared_lock<decltype(_crashCommandMutex)> lock(_crashCommandMutex);
// Typically, this map is empty and this returns no results.
auto commandIt = _crashCommands.find(command.request.methodLine);
if (commandIt == _crashCommands.end()) {
return false;
}
// Look at each crash-inducing command that has the same methodLine.
for (const STable& values : commandIt->second) {
// These are all of the keys that need to match to kill this command.
bool isMatch = true;
for (auto& pair : values) {
// We skip Content-Length, as it's added automatically when serializing commands.
if (SIEquals(pair.first, "Content-Length")) {
continue;
}
// See if our current command even has the blacklisted key.
auto it = command.request.nameValueMap.find(pair.first);
if (it == command.request.nameValueMap.end()) {
// If we didn't find it, the command's not sufficiently similar, and is not blacklisted.
isMatch = false;
break;
}
// At this point, we must have the same key, but if it doesn't have the same value, then it doesn't match.
if (it->second != pair.second) {
isMatch = false;
break;
}
}
// If we got through the whole list and everything was a match, then this is a match, we think it'll crash.
if (isMatch) {
return true;
}
}
// If nothing in our range returned true, then this command looks fine.
return false;
}
BedrockServer::BedrockServer(const SData& args)
: SQLiteServer(""), _args(args), _requestCount(0), _replicationState(SQLiteNode::SEARCHING),
_upgradeInProgress(false), _suppressCommandPort(false), _suppressCommandPortManualOverride(false),
_syncThreadComplete(false), _syncNode(nullptr), _suppressMultiWrite(true), _shutdownState(RUNNING),
_multiWriteEnabled(args.test("-enableMultiWrite")), _backupOnShutdown(false), _detach(false),
_controlPort(nullptr), _commandPort(nullptr)
{
_version = SVERSION;
// Output the list of plugins.