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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
|
/* Copyright 2009 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding
#include "mongo/platform/basic.h"
#include "mongo/s/distlock.h"
#include <boost/scoped_ptr.hpp>
#include <boost/thread/thread.hpp>
#include "mongo/db/server_options.h"
#include "mongo/client/dbclientcursor.h"
#include "mongo/s/type_locks.h"
#include "mongo/s/type_lockpings.h"
#include "mongo/util/concurrency/thread_name.h"
#include "mongo/util/exit.h"
#include "mongo/util/log.h"
#include "mongo/util/timer.h"
namespace mongo {
using boost::scoped_ptr;
using std::auto_ptr;
using std::endl;
using std::list;
using std::set;
using std::string;
using std::stringstream;
using std::vector;
LabeledLevel DistributedLock::logLvl( 1 );
DistributedLock::LastPings DistributedLock::lastPings;
ThreadLocalValue<string> distLockIds("");
/* ==================
* Module initialization
*/
static SimpleMutex _cachedProcessMutex("distlock_initmodule");
static string* _cachedProcessString = NULL;
static void initModule() {
SimpleMutex::scoped_lock lk(_cachedProcessMutex);
if (_cachedProcessString) {
// someone got the lock before us
return;
}
// cache process string
stringstream ss;
ss << getHostName() << ":" << serverGlobalParams.port << ":" << time(0) << ":" << rand();
_cachedProcessString = new string( ss.str() );
}
/* =================== */
string getDistLockProcess() {
if (!_cachedProcessString)
initModule();
verify( _cachedProcessString );
return *_cachedProcessString;
}
string getDistLockId() {
string s = distLockIds.get();
if ( s.empty() ) {
stringstream ss;
ss << getDistLockProcess() << ":" << getThreadName() << ":" << rand();
s = ss.str();
distLockIds.set( s );
}
return s;
}
// Used for disabling the lock pinger during tests
// Should *always* be true otherwise.
static bool lockPingerEnabled = true;
bool isLockPingerEnabled() { return lockPingerEnabled; }
void setLockPingerEnabled(bool enabled) { lockPingerEnabled = enabled; }
class DistributedLockPinger {
public:
DistributedLockPinger()
: _mutex( "DistributedLockPinger" ) {
}
void _distLockPingThread( ConnectionString addr,
const std::string& process,
unsigned long long sleepTime ) {
setThreadName( "LockPinger" );
string pingId = pingThreadId( addr, process );
LOG( DistributedLock::logLvl - 1 ) << "creating distributed lock ping thread for " << addr
<< " and process " << process
<< " (sleeping for " << sleepTime << "ms)" << endl;
static int loops = 0;
Date_t lastPingTime = jsTime();
while( ! inShutdown() && ! shouldKill( addr, process ) ) {
LOG( DistributedLock::logLvl + 2 ) << "distributed lock pinger '" << pingId << "' about to ping." << endl;
Date_t pingTime;
try {
ScopedDbConnection conn(addr.toString(), 30.0);
pingTime = jsTime();
Date_t elapsed(pingTime.millis - lastPingTime.millis);
if (elapsed.millis > 10 * sleepTime) {
warning() << "Lock pinger for addr: " << addr
<< ", proc: " << process
<< " was inactive for " << elapsed.millis << " ms" << endl;
}
lastPingTime = pingTime;
// refresh the entry corresponding to this process in the lockpings collection
conn->update( LockpingsType::ConfigNS,
BSON( LockpingsType::process(process) ),
BSON( "$set" << BSON( LockpingsType::ping(pingTime) ) ),
true );
string err = conn->getLastError();
if ( ! err.empty() ) {
warning() << "pinging failed for distributed lock pinger '" << pingId << "'."
<< causedBy( err ) << endl;
conn.done();
// Sleep for normal ping time
sleepmillis(sleepTime);
continue;
}
// Remove really old entries from the lockpings collection if they're not
// holding a lock. This may happen if an instance of a process was taken down
// and no new instance came up to replace it for a quite a while.
// NOTE this is NOT the same as the standard take-over mechanism, which forces
// the lock entry.
BSONObj fieldsToReturn = BSON( LocksType::state() << 1 <<
LocksType::process() << 1 );
auto_ptr<DBClientCursor> activeLocks =
conn->query( LocksType::ConfigNS,
BSON( LocksType::state() << GT << 0 ) );
uassert( 16060,
str::stream() << "cannot query locks collection on config server "
<< conn.getHost(),
activeLocks.get() );
set<string> pids;
while ( activeLocks->more() ) {
BSONObj lock = activeLocks->nextSafe();
if ( !lock[LocksType::process()].eoo() ) {
pids.insert( lock[LocksType::process()].str() );
}
else {
warning() << "found incorrect lock document during lock ping cleanup: "
<< lock.toString() << endl;
}
}
Date_t fourDays = pingTime - ( 4 * 86400 * 1000 ); // 4 days
conn->remove( LockpingsType::ConfigNS,
BSON( LockpingsType::process() << NIN << pids <<
LockpingsType::ping() << LT << fourDays ) );
err = conn->getLastError();
if ( ! err.empty() ) {
warning() << "ping cleanup for distributed lock pinger '" << pingId << " failed."
<< causedBy( err ) << endl;
conn.done();
// Sleep for normal ping time
sleepmillis(sleepTime);
continue;
}
LOG( DistributedLock::logLvl - ( loops % 10 == 0 ? 1 : 0 ) ) << "cluster " << addr << " pinged successfully at " << pingTime
<< " by distributed lock pinger '" << pingId
<< "', sleeping for " << sleepTime << "ms" << endl;
// Remove old locks, if possible
// Make sure no one else is adding to this list at the same time
scoped_lock lk( _mutex );
int numOldLocks = _oldLockOIDs.size();
if( numOldLocks > 0 ) {
LOG( DistributedLock::logLvl - 1 ) << "trying to delete " << _oldLockOIDs.size() << " old lock entries for process " << process << endl;
}
bool removed = false;
for( list<OID>::iterator i = _oldLockOIDs.begin(); i != _oldLockOIDs.end();
i = ( removed ? _oldLockOIDs.erase( i ) : ++i ) ) {
removed = false;
try {
// Got OID from lock with id, so we don't need to specify id again
conn->update( LocksType::ConfigNS ,
BSON( LocksType::lockID(*i) ),
BSON( "$set" << BSON( LocksType::state(0) ) ) );
// Either the update went through or it didn't, either way we're done trying to
// unlock
LOG( DistributedLock::logLvl - 1 ) << "handled late remove of old distributed lock with ts " << *i << endl;
removed = true;
}
catch( UpdateNotTheSame& ) {
LOG( DistributedLock::logLvl - 1 ) << "partially removed old distributed lock with ts " << *i << endl;
removed = true;
}
catch ( std::exception& e) {
warning() << "could not remove old distributed lock with ts " << *i
<< causedBy( e ) << endl;
}
}
if( numOldLocks > 0 && _oldLockOIDs.size() > 0 ){
LOG( DistributedLock::logLvl - 1 ) << "not all old lock entries could be removed for process " << process << endl;
}
conn.done();
}
catch ( std::exception& e ) {
warning() << "distributed lock pinger '" << pingId << "' detected an exception while pinging."
<< causedBy( e ) << endl;
}
sleepmillis(sleepTime);
}
warning() << "removing distributed lock ping thread '" << pingId << "'" << endl;
if( shouldKill( addr, process ) )
finishKill( addr, process );
}
void distLockPingThread( ConnectionString addr,
long long clockSkew,
const std::string& processId,
unsigned long long sleepTime ) {
try {
jsTimeVirtualThreadSkew( clockSkew );
_distLockPingThread( addr, processId, sleepTime );
}
catch ( std::exception& e ) {
error() << "unexpected error while running distributed lock pinger for " << addr << ", process " << processId << causedBy( e ) << endl;
}
catch ( ... ) {
error() << "unknown error while running distributed lock pinger for " << addr << ", process " << processId << endl;
}
}
string pingThreadId( const ConnectionString& conn, const string& processId ) {
return conn.toString() + "/" + processId;
}
string got( DistributedLock& lock, unsigned long long sleepTime ) {
if (!lockPingerEnabled) return "";
// Make sure we don't start multiple threads for a process id
scoped_lock lk( _mutex );
const ConnectionString& conn = lock.getRemoteConnection();
const string& processId = lock.getProcessId();
string s = pingThreadId( conn, processId );
// Ignore if we already have a pinging thread for this process.
if ( _seen.count( s ) > 0 ) return s;
// Check our clock skew
if (lock.isRemoteTimeSkewed()) {
throw LockException(
str::stream() << "clock skew of the cluster " << conn.toString()
<< " is too far out of bounds to allow distributed locking.",
ErrorCodes::DistributedClockSkewed);
}
boost::thread t( stdx::bind( &DistributedLockPinger::distLockPingThread, this, conn, getJSTimeVirtualThreadSkew(), processId, sleepTime) );
_seen.insert( s );
return s;
}
void addUnlockOID( const OID& oid ) {
// Modifying the lock from some other thread
scoped_lock lk( _mutex );
_oldLockOIDs.push_back( oid );
}
bool willUnlockOID( const OID& oid ) {
scoped_lock lk( _mutex );
return find( _oldLockOIDs.begin(), _oldLockOIDs.end(), oid ) != _oldLockOIDs.end();
}
void kill( const ConnectionString& conn, const string& processId ) {
// Make sure we're in a consistent state before other threads can see us
scoped_lock lk( _mutex );
string pingId = pingThreadId( conn, processId );
verify( _seen.count( pingId ) > 0 );
_kill.insert( pingId );
}
bool shouldKill( const ConnectionString& conn, const string& processId ) {
scoped_lock lk( _mutex );
return _kill.count( pingThreadId( conn, processId ) ) > 0;
}
void finishKill( const ConnectionString& conn, const string& processId ) {
// Make sure we're in a consistent state before other threads can see us
scoped_lock lk( _mutex );
string pingId = pingThreadId( conn, processId );
_kill.erase( pingId );
_seen.erase( pingId );
}
private:
// Protects all of the members below.
mongo::mutex _mutex;
set<string> _kill;
set<string> _seen;
list<OID> _oldLockOIDs;
} distLockPinger;
/**
* Create a new distributed lock, potentially with a custom sleep and takeover time. If a custom sleep time is
* specified (time between pings)
*/
DistributedLock::DistributedLock( const ConnectionString& conn , const string& name , unsigned long long lockTimeout, bool asProcess )
: _conn(conn), _name(name),
_processId( asProcess ? getDistLockId() : getDistLockProcess() ),
_lockTimeout( lockTimeout == 0 ? LOCK_TIMEOUT : lockTimeout ),
_maxClockSkew( _lockTimeout / LOCK_SKEW_FACTOR ), _maxNetSkew( _maxClockSkew ),
_lockPing( _maxClockSkew ), _mutex( "DistributedLock" )
{
LOG( logLvl ) << "created new distributed lock for " << name << " on " << conn
<< " ( lock timeout : " << _lockTimeout
<< ", ping interval : " << _lockPing << ", process : " << asProcess << " )" << endl;
}
DistributedLock::PingData DistributedLock::LastPings::getLastPing( const ConnectionString& conn, const string& lockName ){
scoped_lock lock( _mutex );
return _lastPings[ std::pair< string, string >( conn.toString(), lockName ) ];
}
void DistributedLock::LastPings::setLastPing( const ConnectionString& conn, const string& lockName, const PingData& pd ){
scoped_lock lock( _mutex );
_lastPings[ std::pair< string, string >( conn.toString(), lockName ) ] = pd;
}
Date_t DistributedLock::getRemoteTime() {
return DistributedLock::remoteTime( _conn, _maxNetSkew );
}
bool DistributedLock::isRemoteTimeSkewed() {
return !DistributedLock::checkSkew( _conn, NUM_LOCK_SKEW_CHECKS, _maxClockSkew, _maxNetSkew );
}
const ConnectionString& DistributedLock::getRemoteConnection() {
return _conn;
}
const string& DistributedLock::getProcessId() {
return _processId;
}
/**
* Returns the remote time as reported by the cluster or server. The maximum difference between the reported time
* and the actual time on the remote server (at the completion of the function) is the maxNetSkew
*/
Date_t DistributedLock::remoteTime( const ConnectionString& cluster, unsigned long long maxNetSkew ) {
ConnectionString server( *cluster.getServers().begin() );
// Get result and delay if successful, errMsg if not
bool success = false;
BSONObj result;
string errMsg;
long long delay = 0;
scoped_ptr<ScopedDbConnection> connPtr;
try {
connPtr.reset( new ScopedDbConnection( server.toString() ) );
ScopedDbConnection& conn = *connPtr;
Date_t then = jsTime();
success = conn->runCommand( string( "admin" ), BSON( "serverStatus" << 1 ), result );
delay = jsTime() - then;
if ( !success ) errMsg = result.toString();
conn.done();
}
catch ( const DBException& ex ) {
if ( connPtr && connPtr->get()->isFailed() ) {
// Return to the pool so the pool knows about the failure
connPtr->done();
}
success = false;
errMsg = ex.toString();
}
if( !success ) {
throw TimeNotFoundException( str::stream() << "could not get status from server "
<< server.toString() << " in cluster "
<< cluster.toString() << " to check time"
<< causedBy( errMsg ),
13647 );
}
// Make sure that our delay is not more than 2x our maximum network skew, since this is the max our remote
// time value can be off by if we assume a response in the middle of the delay.
if ( delay > (long long) ( maxNetSkew * 2 ) ) {
throw TimeNotFoundException( str::stream()
<< "server " << server.toString() << " in cluster "
<< cluster.toString()
<< " did not respond within max network delay of "
<< maxNetSkew << "ms",
13648 );
}
return result["localTime"].Date() - (delay / 2);
}
bool DistributedLock::checkSkew( const ConnectionString& cluster, unsigned skewChecks, unsigned long long maxClockSkew, unsigned long long maxNetSkew ) {
vector<HostAndPort> servers = cluster.getServers();
if(servers.size() < 1) return true;
vector<long long> avgSkews;
for(unsigned i = 0; i < skewChecks; i++) {
// Find the average skew for each server
unsigned s = 0;
for(vector<HostAndPort>::iterator si = servers.begin(); si != servers.end(); ++si,s++) {
if(i == 0) avgSkews.push_back(0);
// Could check if this is self, but shouldn't matter since local network connection should be fast.
ConnectionString server( *si );
vector<long long> skew;
BSONObj result;
Date_t remote = remoteTime( server, maxNetSkew );
Date_t local = jsTime();
// Remote time can be delayed by at most MAX_NET_SKEW
// Skew is how much time we'd have to add to local to get to remote
avgSkews[s] += (long long) (remote - local);
LOG( logLvl + 1 ) << "skew from remote server " << server << " found: " << (long long) (remote - local) << endl;
}
}
// Analyze skews
long long serverMaxSkew = 0;
long long serverMinSkew = 0;
for(unsigned s = 0; s < avgSkews.size(); s++) {
long long avgSkew = (avgSkews[s] /= skewChecks);
// Keep track of max and min skews
if(s == 0) {
serverMaxSkew = avgSkew;
serverMinSkew = avgSkew;
}
else {
if(avgSkew > serverMaxSkew)
serverMaxSkew = avgSkew;
if(avgSkew < serverMinSkew)
serverMinSkew = avgSkew;
}
}
long long totalSkew = serverMaxSkew - serverMinSkew;
// Make sure our max skew is not more than our pre-set limit
if(totalSkew > (long long) maxClockSkew) {
LOG( logLvl + 1 ) << "total clock skew of " << totalSkew << "ms for servers " << cluster << " is out of " << maxClockSkew << "ms bounds." << endl;
return false;
}
LOG( logLvl + 1 ) << "total clock skew of " << totalSkew << "ms for servers " << cluster << " is in " << maxClockSkew << "ms bounds." << endl;
return true;
}
// For use in testing, ping thread should run indefinitely in practice.
bool DistributedLock::killPinger( DistributedLock& lock ) {
if( lock._threadId == "") return false;
distLockPinger.kill( lock._conn, lock._processId );
return true;
}
Status DistributedLock::checkStatus(double timeout) {
BSONObj lockObj;
try {
ScopedDbConnection conn(_conn.toString(), timeout );
lockObj = conn->findOne( LocksType::ConfigNS,
BSON( LocksType::name(_name) ) ).getOwned();
conn.done();
}
catch ( DBException& e ) {
return e.toStatus();
}
if ( lockObj.isEmpty() ) {
return Status(ErrorCodes::LockFailed,
str::stream() << "no lock for " << _name << " exists in the locks collection");
}
if ( lockObj[LocksType::state()].numberInt() < 2 ) {
return Status(ErrorCodes::LockFailed,
str::stream() << "lock " << _name << " current state is not held ("
<< lockObj[LocksType::state()].numberInt() << ")");
}
if ( lockObj[LocksType::process()].String() != _processId ) {
return Status(ErrorCodes::LockFailed,
str::stream() << "lock " << _name << " is currently being held by "
<< "another process ("
<< lockObj[LocksType::process()].String() << ")");
}
if ( distLockPinger.willUnlockOID( lockObj[LocksType::lockID()].OID() ) ) {
return Status(ErrorCodes::LockFailed,
str::stream() << "lock " << _name << " is not held and is currently being "
<< "scheduled for lazy unlock by "
<< lockObj[LocksType::lockID()].OID());
}
return Status::OK();
}
static void logErrMsgOrWarn(StringData messagePrefix,
StringData lockName,
StringData errMsg,
StringData altErrMsg) {
if (errMsg.empty()) {
LOG(DistributedLock::logLvl - 1) << messagePrefix << " '" << lockName << "' " <<
altErrMsg << std::endl;
}
else {
warning() << messagePrefix << " '" << lockName << "' " << causedBy(errMsg.toString());
}
}
// Semantics of this method are basically that if the lock cannot be acquired, returns false, can be retried.
// If the lock should not be tried again (some unexpected error) a LockException is thrown.
// If we are only trying to re-enter a currently held lock, reenter should be true.
// Note: reenter doesn't actually make this lock re-entrant in the normal sense, since it can still only
// be unlocked once, instead it is used to verify that the lock is already held.
bool DistributedLock::lock_try( const string& why , bool reenter, BSONObj * other, double timeout ) {
// TODO: Start pinging only when we actually get the lock?
// If we don't have a thread pinger, make sure we shouldn't have one
if( _threadId == "" ){
scoped_lock lk( _mutex );
_threadId = distLockPinger.got( *this, _lockPing );
}
// This should always be true, if not, we are using the lock incorrectly.
verify( _name != "" );
LOG( logLvl ) << "trying to acquire new distributed lock for " << _name << " on " << _conn
<< " ( lock timeout : " << _lockTimeout
<< ", ping interval : " << _lockPing << ", process : " << _processId << " )"
<< endl;
// write to dummy if 'other' is null
BSONObj dummyOther;
if ( other == NULL )
other = &dummyOther;
ScopedDbConnection conn(_conn.toString(), timeout );
BSONObjBuilder queryBuilder;
queryBuilder.append( LocksType::name() , _name );
queryBuilder.append( LocksType::state() , 0 );
{
// make sure its there so we can use simple update logic below
BSONObj o = conn->findOne( LocksType::ConfigNS , BSON( LocksType::name(_name) ) ).getOwned();
// Case 1: No locks
if ( o.isEmpty() ) {
try {
LOG( logLvl ) << "inserting initial doc in " << LocksType::ConfigNS << " for lock " << _name << endl;
conn->insert( LocksType::ConfigNS,
BSON( LocksType::name(_name)
<< LocksType::state(0)
<< LocksType::who("")
<< LocksType::lockID(OID()) ));
}
catch ( UserException& e ) {
warning() << "could not insert initial doc for distributed lock " << _name << causedBy( e ) << endl;
}
}
// Case 2: A set lock that we might be able to force
else if ( o[LocksType::state()].numberInt() > 0 ) {
string lockName = o[LocksType::name()].String() + string("/") + o[LocksType::process()].String();
bool canReenter = reenter &&
o[LocksType::process()].String() == _processId &&
!distLockPinger.willUnlockOID( o[LocksType::lockID()].OID() ) &&
o[LocksType::state()].numberInt() == 2;
if( reenter && ! canReenter ) {
LOG( logLvl - 1 ) << "not re-entering distributed lock " << lockName;
if ( o[LocksType::process()].String() != _processId ) {
LOG( logLvl - 1 ) << ", different process " << _processId << endl;
}
else if ( o[LocksType::state()].numberInt() == 2 ) {
LOG( logLvl - 1 ) << ", state not finalized" << endl;
}
else {
LOG( logLvl - 1 ) << ", ts " << o[LocksType::lockID()].OID() << " scheduled for late unlock" << endl;
}
// reset since we've been bounced by a previous lock not being where we thought it was,
// and should go through full forcing process if required.
// (in theory we should never see a ping here if used correctly)
*other = o; other->getOwned(); conn.done(); resetLastPing();
return false;
}
BSONObj lastPing = conn->findOne( LockpingsType::ConfigNS, o[LocksType::process()].wrap( LockpingsType::process() ) );
if ( lastPing.isEmpty() ) {
LOG( logLvl ) << "empty ping found for process in lock '" << lockName << "'" << endl;
// TODO: Using 0 as a "no time found" value Will fail if dates roll over, but then, so will a lot.
lastPing = BSON( LockpingsType::process(o[LocksType::process()].String()) <<
LockpingsType::ping((Date_t) 0) );
}
unsigned long long elapsed = 0;
unsigned long long takeover = _lockTimeout;
PingData _lastPingCheck = getLastPing();
LOG( logLvl ) << "checking last ping for lock '" << lockName << "'" << " against process " << _lastPingCheck.id << " and ping " << _lastPingCheck.lastPing << endl;
try {
Date_t remote = remoteTime( _conn );
// Timeout the elapsed time using comparisons of remote clock
// For non-finalized locks, timeout 15 minutes since last seen (ts)
// For finalized locks, timeout 15 minutes since last ping
bool recPingChange = o[LocksType::state()].numberInt() == 2 &&
( _lastPingCheck.id != lastPing[LockpingsType::process()].String() ||
_lastPingCheck.lastPing != lastPing[LockpingsType::ping()].Date() );
bool recTSChange = _lastPingCheck.ts != o[LocksType::lockID()].OID();
if( recPingChange || recTSChange ) {
// If the ping has changed since we last checked, mark the current date and time
setLastPing( PingData( lastPing[LockpingsType::process()].String().c_str(),
lastPing[LockpingsType::ping()].Date(),
remote, o[LocksType::lockID()].OID() ) );
}
else {
// GOTCHA! Due to network issues, it is possible that the current time
// is less than the remote time. We *have* to check this here, otherwise
// we overflow and our lock breaks.
if(_lastPingCheck.remote >= remote)
elapsed = 0;
else
elapsed = remote - _lastPingCheck.remote;
}
}
catch( LockException& e ) {
// Remote server cannot be found / is not responsive
warning() << "Could not get remote time from " << _conn << causedBy( e );
// If our config server is having issues, forget all the pings until we can see it again
resetLastPing();
}
if ( elapsed <= takeover && ! canReenter ) {
LOG( logLvl ) << "could not force lock '" << lockName << "' because elapsed time " << elapsed << " <= takeover time " << takeover << endl;
*other = o; other->getOwned(); conn.done();
return false;
}
else if( elapsed > takeover && canReenter ) {
LOG( logLvl - 1 ) << "not re-entering distributed lock " << lockName << "' because elapsed time " << elapsed << " > takeover time " << takeover << endl;
*other = o; other->getOwned(); conn.done();
return false;
}
LOG( logLvl - 1 ) << ( canReenter ? "re-entering" : "forcing" ) << " lock '" << lockName << "' because "
<< ( canReenter ? "re-entering is allowed, " : "" )
<< "elapsed time " << elapsed << " > takeover time " << takeover << endl;
if( elapsed > takeover ) {
// Lock may forced, reset our timer if succeeds or fails
// Ensures that another timeout must happen if something borks up here, and resets our pristine
// ping state if acquired.
resetLastPing();
try {
// Check the clock skew again. If we check this before we get a lock
// and after the lock times out, we can be pretty sure the time is
// increasing at the same rate on all servers and therefore our
// timeout is accurate
if (isRemoteTimeSkewed()) {
throw LockException(
str::stream() << "remote time in cluster " << _conn.toString()
<< " is now skewed, cannot force lock.",
ErrorCodes::DistributedClockSkewed);
}
// Make sure we break the lock with the correct "ts" (OID) value, otherwise
// we can overwrite a new lock inserted in the meantime.
conn->update( LocksType::ConfigNS,
BSON( LocksType::name(_name) <<
LocksType::state(o[LocksType::state()].numberInt()) <<
LocksType::lockID(o[LocksType::lockID()].OID()) ),
BSON( "$set" << BSON( LocksType::state(0) ) ) );
BSONObj err = conn->getLastErrorDetailed();
string errMsg = DBClientWithCommands::getLastErrorString(err);
// TODO: Clean up all the extra code to exit this method, probably with a refactor
if ( !errMsg.empty() || !err["n"].type() || err["n"].numberInt() < 1 ) {
logErrMsgOrWarn("Could not force lock", lockName, errMsg, "(another force won");
*other = o; other->getOwned(); conn.done();
return false;
}
}
catch( UpdateNotTheSame& ) {
// Ok to continue since we know we forced at least one lock document, and all lock docs
// are required for a lock to be held.
warning() << "lock forcing " << lockName << " inconsistent" << endl;
}
catch (const LockException& ) {
// Let the exception go up and don't repackage the exception.
throw;
}
catch( std::exception& e ) {
conn.done();
throw LockException( str::stream() << "exception forcing distributed lock "
<< lockName << causedBy( e ), 13660);
}
}
else {
verify( canReenter );
// Lock may be re-entered, reset our timer if succeeds or fails
// Not strictly necessary, but helpful for small timeouts where thread scheduling is significant.
// This ensures that two attempts are still required for a force if not acquired, and resets our
// state if we are acquired.
resetLastPing();
// Test that the lock is held by trying to update the finalized state of the lock to the same state
// if it does not update or does not update on all servers, we can't re-enter.
try {
// Test the lock with the correct "ts" (OID) value
conn->update( LocksType::ConfigNS,
BSON( LocksType::name(_name) <<
LocksType::state(2) <<
LocksType::lockID(o[LocksType::lockID()].OID()) ),
BSON( "$set" << BSON( LocksType::state(2) ) ) );
BSONObj err = conn->getLastErrorDetailed();
string errMsg = DBClientWithCommands::getLastErrorString(err);
// TODO: Clean up all the extra code to exit this method, probably with a refactor
if ( ! errMsg.empty() || ! err["n"].type() || err["n"].numberInt() < 1 ) {
logErrMsgOrWarn("Could not re-enter lock", lockName, errMsg, "(not sure lock is held");
*other = o; other->getOwned(); conn.done();
return false;
}
}
catch( UpdateNotTheSame& ) {
// NOT ok to continue since our lock isn't held by all servers, so isn't valid.
warning() << "inconsistent state re-entering lock, lock " << lockName << " not held" << endl;
*other = o; other->getOwned(); conn.done();
return false;
}
catch( std::exception& e ) {
conn.done();
throw LockException( str::stream() << "exception re-entering distributed lock "
<< lockName << causedBy( e ), 13660);
}
LOG( logLvl - 1 ) << "re-entered distributed lock '" << lockName << "'" << endl;
*other = o.getOwned();
conn.done();
return true;
}
LOG( logLvl - 1 ) << "lock '" << lockName << "' successfully forced" << endl;
// We don't need the ts value in the query, since we will only ever replace locks with state=0.
}
// Case 3: We have an expired lock
else if ( o[LocksType::lockID()].type() ) {
queryBuilder.append( o[LocksType::lockID()] );
}
}
// Always reset our ping if we're trying to get a lock, since getting a lock implies the lock state is open
// and no locks need to be forced. If anything goes wrong, we don't want to remember an old lock.
resetLastPing();
bool gotLock = false;
BSONObj currLock;
BSONObj lockDetails = BSON( LocksType::state(1)
<< LocksType::who(getDistLockId())
<< LocksType::process(_processId)
<< "when" << jsTime()
<< LocksType::why(why)
<< LocksType::lockID(OID::gen()) );
BSONObj whatIWant = BSON( "$set" << lockDetails );
BSONObj query = queryBuilder.obj();
string lockName = _name + string("/") + _processId;
try {
// Main codepath to acquire lock
LOG( logLvl ) << "about to acquire distributed lock '" << lockName << "'";
LOG( logLvl + 1 ) << "trying to acquire lock " << query.toString( false, true )
<< " with details " << lockDetails.toString( false, true ) << endl;
conn->update( LocksType::ConfigNS , query , whatIWant );
BSONObj err = conn->getLastErrorDetailed();
string errMsg = DBClientWithCommands::getLastErrorString(err);
currLock = conn->findOne( LocksType::ConfigNS , BSON( LocksType::name(_name) ) );
if ( !errMsg.empty() || !err["n"].type() || err["n"].numberInt() < 1 ) {
logErrMsgOrWarn("could not acquire lock", lockName, errMsg, "(another update won)");
*other = currLock;
other->getOwned();
gotLock = false;
}
else {
gotLock = true;
}
}
catch ( UpdateNotTheSame& up ) {
// this means our update got through on some, but not others
warning() << "distributed lock '" << lockName << " did not propagate properly." << causedBy( up ) << endl;
// Overall protection derives from:
// All unlocking updates use the ts value when setting state to 0
// This ensures that during locking, we can override all smaller ts locks with
// our own safe ts value and not be unlocked afterward.
for ( unsigned i = 0; i < up.size(); i++ ) {
ScopedDbConnection indDB(up[i].first);
BSONObj indUpdate;
try {
indUpdate = indDB->findOne( LocksType::ConfigNS , BSON( LocksType::name(_name) ) );
// If we override this lock in any way, grab and protect it.
// We assume/ensure that if a process does not have all lock documents, it is no longer
// holding the lock.
// Note - finalized locks may compete too, but we know they've won already if competing
// in this round. Cleanup of crashes during finalizing may take a few tries.
if( indUpdate[LocksType::lockID()] < lockDetails[LocksType::lockID()] || indUpdate[LocksType::state()].numberInt() == 0 ) {
BSONObj grabQuery = BSON( LocksType::name(_name)
<< LocksType::lockID(indUpdate[LocksType::lockID()].OID()) );
// Change ts so we won't be forced, state so we won't be relocked
BSONObj grabChanges = BSON( LocksType::lockID(lockDetails[LocksType::lockID()].OID())
<< LocksType::state(1) );
// Either our update will succeed, and we'll grab the lock, or it will fail b/c some other
// process grabbed the lock (which will change the ts), but the lock will be set until forcing
indDB->update( LocksType::ConfigNS, grabQuery, BSON( "$set" << grabChanges ) );
indUpdate = indDB->findOne( LocksType::ConfigNS, BSON( LocksType::name(_name) ) );
// The tournament was interfered and it is not safe to proceed further.
// One case this could happen is when the LockPinger processes old
// entries from addUnlockOID. See SERVER-10688 for more detailed
// description of race.
if ( indUpdate[LocksType::state()].numberInt() <= 0 ) {
LOG( logLvl - 1 ) << "lock tournament interrupted, "
<< "so no lock was taken; "
<< "new state of lock: " << indUpdate << endl;
// We now break and set our currLock lockID value to zero, so that
// we know that we did not acquire the lock below. Later code will
// cleanup failed entries.
currLock = BSON(LocksType::lockID(OID()));
indDB.done();
break;
}
}
// else our lock is the same, in which case we're safe, or it's a bigger lock,
// in which case we won't need to protect anything since we won't have the lock.
}
catch( std::exception& e ) {
conn.done();
throw LockException( str::stream() << "distributed lock " << lockName
<< " had errors communicating with individual server "
<< up[1].first << causedBy( e ), 13661 );
}
verify( !indUpdate.isEmpty() );
// Find max TS value
if ( currLock.isEmpty() || currLock[LocksType::lockID()] < indUpdate[LocksType::lockID()] ) {
currLock = indUpdate.getOwned();
}
indDB.done();
}
// Locks on all servers are now set and safe until forcing
if ( currLock[LocksType::lockID()] == lockDetails[LocksType::lockID()] ) {
LOG( logLvl - 1 ) << "lock update won, completing lock propagation for '" << lockName << "'" << endl;
gotLock = true;
}
else {
LOG( logLvl - 1 ) << "lock update lost, lock '" << lockName << "' not propagated." << endl;
gotLock = false;
}
}
catch( std::exception& e ) {
conn.done();
throw LockException( str::stream() << "exception creating distributed lock "
<< lockName << causedBy( e ), 13663 );
}
// Complete lock propagation
if( gotLock ) {
// This is now safe, since we know that no new locks will be placed on top of the ones we've checked for at
// least 15 minutes. Sets the state = 2, so that future clients can determine that the lock is truly set.
// The invariant for rollbacks is that we will never force locks with state = 2 and active pings, since that
// indicates the lock is active, but this means the process creating/destroying them must explicitly poll
// when something goes wrong.
try {
BSONObjBuilder finalLockDetails;
BSONObjIterator bi( lockDetails );
while( bi.more() ) {
BSONElement el = bi.next();
if( (string) ( el.fieldName() ) == LocksType::state() )
finalLockDetails.append( LocksType::state(), 2 );
else finalLockDetails.append( el );
}
conn->update( LocksType::ConfigNS , BSON( LocksType::name(_name) ) , BSON( "$set" << finalLockDetails.obj() ) );
BSONObj err = conn->getLastErrorDetailed();
string errMsg = DBClientWithCommands::getLastErrorString(err);
currLock = conn->findOne( LocksType::ConfigNS , BSON( LocksType::name(_name) ) );
if ( !errMsg.empty() || !err["n"].type() || err["n"].numberInt() < 1 ) {
warning() << "could not finalize winning lock " << lockName
<< ( !errMsg.empty() ? causedBy( errMsg ) : " (did not update lock) " ) << endl;
gotLock = false;
}
else {
// SUCCESS!
gotLock = true;
}
}
catch( std::exception& e ) {
conn.done();
// Register the bad final lock for deletion, in case it exists
distLockPinger.addUnlockOID( lockDetails[LocksType::lockID()].OID() );
throw LockException( str::stream() << "exception finalizing winning lock"
<< causedBy( e ), 13662 );
}
}
*other = currLock;
other->getOwned();
// Log our lock results
if(gotLock)
LOG( logLvl - 1 ) << "distributed lock '" << lockName <<
"' acquired, ts : " << currLock[LocksType::lockID()].OID() << endl;
else
LOG( logLvl - 1 ) << "distributed lock '" << lockName << "' was not acquired." << endl;
conn.done();
return gotLock;
}
// Unlock now takes an optional pointer to the lock, so you can be specific about which
// particular lock you want to unlock. This is required when the config server is down,
// and so cannot tell you what lock ts you should try later.
//
// This function *must not* throw exceptions, since it can be used in destructors - failure
// results in queuing and trying again later.
void DistributedLock::unlock( BSONObj* oldLockPtr ) {
verify( _name != "" );
string lockName = _name + string("/") + _processId;
const int maxAttempts = 3;
int attempted = 0;
BSONObj oldLock;
if( oldLockPtr ) oldLock = *oldLockPtr;
while ( ++attempted <= maxAttempts ) {
// Awkward, but necessary since the constructor itself throws exceptions
scoped_ptr<ScopedDbConnection> connPtr;
try {
connPtr.reset( new ScopedDbConnection( _conn.toString() ) );
ScopedDbConnection& conn = *connPtr;
if( oldLock.isEmpty() )
oldLock = conn->findOne( LocksType::ConfigNS, BSON( LocksType::name(_name) ) );
if( oldLock[LocksType::state()].eoo() ||
oldLock[LocksType::state()].numberInt() != 2 ||
oldLock[LocksType::lockID()].eoo() ) {
warning() << "cannot unlock invalid distributed lock " << oldLock << endl;
conn.done();
break;
}
// Use ts when updating lock, so that new locks can be sure they won't get trampled.
conn->update( LocksType::ConfigNS ,
BSON( LocksType::name(_name) <<
LocksType::lockID(oldLock[LocksType::lockID()].OID()) ),
BSON( "$set" << BSON( LocksType::state(0) ) ) );
// Check that the lock was actually unlocked... if not, try again
BSONObj err = conn->getLastErrorDetailed();
string errMsg = DBClientWithCommands::getLastErrorString(err);
if ( !errMsg.empty() || !err["n"].type() || err["n"].numberInt() < 1 ){
warning() << "distributed lock unlock update failed, retrying "
<< ( errMsg.empty() ? causedBy( "( update not registered )" ) : causedBy( errMsg ) ) << endl;
conn.done();
continue;
}
LOG( logLvl - 1 ) << "distributed lock '" << lockName << "' unlocked. " << endl;
conn.done();
return;
}
catch( UpdateNotTheSame& ) {
LOG( logLvl - 1 ) << "distributed lock '" << lockName << "' unlocked (messily). " << endl;
// This isn't a connection problem, so don't throw away the conn
connPtr->done();
break;
}
catch ( std::exception& e) {
warning() << "distributed lock '" << lockName << "' failed unlock attempt."
<< causedBy( e ) << endl;
// TODO: If our lock timeout is small, sleeping this long may be unsafe.
if( attempted != maxAttempts) sleepsecs(1 << attempted);
}
}
if( attempted > maxAttempts && ! oldLock.isEmpty() && ! oldLock[LocksType::lockID()].eoo() ) {
LOG( logLvl - 1 ) << "could not unlock distributed lock with ts " <<
oldLock[LocksType::lockID()].OID() << ", will attempt again later" << endl;
// We couldn't unlock the lock at all, so try again later in the pinging thread...
distLockPinger.addUnlockOID( oldLock[LocksType::lockID()].OID() );
}
else if( attempted > maxAttempts ) {
warning() << "could not unlock untracked distributed lock, a manual force may be required" << endl;
}
warning() << "distributed lock '" << lockName << "' couldn't consummate unlock request. "
<< "lock may be taken over after " << ( _lockTimeout / (60 * 1000) )
<< " minutes timeout." << endl;
}
const long long ScopedDistributedLock::kDefaultLockTryIntervalMillis = 1000;
const long long ScopedDistributedLock::kDefaultSocketTimeoutMillis = 30 * 1000;
ScopedDistributedLock::ScopedDistributedLock(const ConnectionString& conn, const string& name)
: _lock(conn, name),
_why(""),
_lockTryIntervalMillis(kDefaultLockTryIntervalMillis),
_socketTimeoutMillis(kDefaultSocketTimeoutMillis),
_acquired(false) {
}
ScopedDistributedLock::~ScopedDistributedLock() {
if (_acquired) {
unlock();
}
}
Status ScopedDistributedLock::tryAcquire() {
try {
_acquired = _lock.lock_try(_why,
false,
&_other,
static_cast<double>(_socketTimeoutMillis / 1000));
}
catch (const DBException& e) {
return e.toStatus();
}
if (_acquired) {
return Status::OK();
}
return Status(ErrorCodes::LockBusy, str::stream() << "Lock for " << _why << " is taken.");
}
void ScopedDistributedLock::unlock() {
_lock.unlock(&_other);
}
Status ScopedDistributedLock::acquire(long long waitForMillis) {
Timer timer;
Timer msgTimer;
Status lastStatus = Status::OK();
while (waitForMillis <= 0 || timer.millis() < waitForMillis) {
lastStatus = tryAcquire();
_acquired = lastStatus.isOK();
if (_acquired) {
verify(!_other.isEmpty());
return Status::OK();
}
if (waitForMillis == 0) break;
if (lastStatus.code() == ErrorCodes::DistributedClockSkewed) {
return lastStatus;
}
// Periodically message for debugging reasons
if (msgTimer.seconds() > 10) {
log() << "waited " << timer.seconds() << "s for distributed lock " << _lock._name
<< " for " << _why << ": " << lastStatus.toString() << endl;
msgTimer.reset();
}
long long timeRemainingMillis = std::max(0LL, waitForMillis - timer.millis());
sleepmillis(std::min(_lockTryIntervalMillis, timeRemainingMillis));
}
return lastStatus;
}
Status ScopedDistributedLock::checkStatus() {
if (!_acquired) {
return Status(ErrorCodes::LockFailed, "lock was never acquired");
}
return _lock.checkStatus(static_cast<double>(_socketTimeoutMillis / 1000));
}
}
|