summaryrefslogtreecommitdiff
path: root/util/optime.h
diff options
context:
space:
mode:
Diffstat (limited to 'util/optime.h')
-rw-r--r--util/optime.h36
1 files changed, 19 insertions, 17 deletions
diff --git a/util/optime.h b/util/optime.h
index 7e6be4d..9f78fda 100644
--- a/util/optime.h
+++ b/util/optime.h
@@ -26,7 +26,7 @@ namespace mongo {
ClockSkewException() : DBException( "clock skew exception" , 20001 ) {}
};
- /* replsets use RSOpTime.
+ /* replsets used to use RSOpTime.
M/S uses OpTime.
But this is useable from both.
*/
@@ -36,9 +36,10 @@ namespace mongo {
*/
#pragma pack(4)
class OpTime {
- unsigned i;
+ unsigned i; // ordinal comes first so we can do a single 64 bit compare on little endian
unsigned secs;
static OpTime last;
+ static OpTime skewed();
public:
static void setLast(const Date_t &date) {
last = OpTime(date);
@@ -46,47 +47,48 @@ namespace mongo {
unsigned getSecs() const {
return secs;
}
+ unsigned getInc() const {
+ return i;
+ }
OpTime(Date_t date) {
reinterpret_cast<unsigned long long&>(*this) = date.millis;
+ dassert( (int)secs >= 0 );
}
OpTime(ReplTime x) {
reinterpret_cast<unsigned long long&>(*this) = x;
+ dassert( (int)secs >= 0 );
}
OpTime(unsigned a, unsigned b) {
secs = a;
i = b;
+ dassert( (int)secs >= 0 );
}
OpTime( const OpTime& other ) {
secs = other.secs;
i = other.i;
+ dassert( (int)secs >= 0 );
}
OpTime() {
secs = 0;
i = 0;
}
- static OpTime now() {
+ // it isn't generally safe to not be locked for this. so use now(). some tests use this.
+ static OpTime now_inlock() {
unsigned t = (unsigned) time(0);
- if ( t < last.secs ) {
- bool toLog = false;
- ONCE toLog = true;
- RARELY toLog = true;
- if ( last.i & 0x80000000 )
- toLog = true;
- if ( toLog )
- log() << "clock skew detected prev: " << last.secs << " now: " << t << " trying to handle..." << endl;
- if ( last.i & 0x80000000 ) {
- log() << "ERROR Large clock skew detected, shutting down" << endl;
- throw ClockSkewException();
- }
- t = last.secs;
- }
if ( last.secs == t ) {
last.i++;
return last;
}
+ if ( t < last.secs ) {
+ return skewed(); // separate function to keep out of the hot code path
+ }
last = OpTime(t, 1);
return last;
}
+ static OpTime now() {
+ DEV dbMutex.assertWriteLocked();
+ return now_inlock();
+ }
/* We store OpTime's in the database as BSON Date datatype -- we needed some sort of
64 bit "container" for these values. While these are not really "Dates", that seems a