summaryrefslogtreecommitdiff
path: root/src/mongo/shell/utils.js
diff options
context:
space:
mode:
authorVesselina Ratcheva <vesselina.ratcheva@10gen.com>2018-03-09 15:10:04 -0500
committerVesselina Ratcheva <vesselina.ratcheva@10gen.com>2018-03-09 15:10:52 -0500
commitc5a4250a649ab0afb4ecdf227d4a0400f9e68786 (patch)
tree51499f49e3edf03891f335164937333d2b6a7297 /src/mongo/shell/utils.js
parenta4d29b292f1bc42ae8133b0a0984c2b012c43528 (diff)
downloadmongo-c5a4250a649ab0afb4ecdf227d4a0400f9e68786.tar.gz
Revert "SERVER-32144 Remove test coverage for replication protocol version 0"
This reverts commit d05e04551b7e399a5554858de48541ae11988b10.
Diffstat (limited to 'src/mongo/shell/utils.js')
-rw-r--r--src/mongo/shell/utils.js48
1 files changed, 34 insertions, 14 deletions
diff --git a/src/mongo/shell/utils.js b/src/mongo/shell/utils.js
index 6d130491b29..c07aebac8d7 100644
--- a/src/mongo/shell/utils.js
+++ b/src/mongo/shell/utils.js
@@ -1405,29 +1405,49 @@ rs.debug.getLastOpWritten = function(server) {
};
/**
- * Compares OpTimes in the format {ts:Timestamp, t:NumberLong}.
- * Returns -1 if ot1 is 'earlier' than ot2, 1 if 'later' and 0 if equal.
+ * Compares OpTimes. Returns -1 if ot1 is 'earlier' than ot2, 1 if 'later' and 0 if equal.
+ *
+ * Note: Since Protocol Version 1 was introduced for replication, 'OpTimes'
+ * can come in two different formats. This function will throw an error when the OpTime
+ * passed do not have the same protocol version.
+ *
+ * OpTime Formats:
+ * PV0: Timestamp
+ * PV1: {ts:Timestamp, t:NumberLong}
*/
rs.compareOpTimes = function(ot1, ot2) {
+ function _isOpTimeV1(opTime) {
+ return (opTime.hasOwnProperty("ts") && opTime.hasOwnProperty("t"));
+ }
+ function _isEmptyOpTime(opTime) {
+ return (opTime.ts.getTime() == 0 && opTime.ts.getInc() == 0 && opTime.t == -1);
+ }
- function _isValidOptime(opTime) {
- let timestampIsValid = (opTime.hasOwnProperty("ts") && (opTime.ts !== Timestamp(0, 0)));
- let termIsValid = (opTime.hasOwnProperty("t") && (opTime.t != -1));
+ // Make sure both OpTimes have a timestamp and a term.
+ var ot1 = _isOpTimeV1(ot1) ? ot1 : {ts: ot1, t: NumberLong(-1)};
+ var ot2 = _isOpTimeV1(ot2) ? ot2 : {ts: ot2, t: NumberLong(-1)};
- return timestampIsValid && termIsValid;
+ if (_isEmptyOpTime(ot1) || _isEmptyOpTime(ot2)) {
+ throw Error("cannot do comparison with empty OpTime, received: " + tojson(ot1) + " and " +
+ tojson(ot2));
}
- if (!_isValidOptime(ot1) || !_isValidOptime(ot2)) {
- throw Error("invalid optimes, received: " + tojson(ot1) + " and " + tojson(ot2));
+ if ((ot1.t == -1 && ot2.t != -1) || (ot1.t != -1 && ot2.t == -1)) {
+ throw Error("cannot compare OpTimes between different protocol versions, received: " +
+ tojson(ot1) + " and " + tojson(ot2));
}
- if (ot1.t > ot2.t) {
- return 1;
- } else if (ot1.t < ot2.t) {
- return -1;
- } else {
- return timestampCmp(ot1.ts, ot2.ts);
+ if (!friendlyEqual(ot1.t, ot2.t)) {
+ if (ot1.t < ot2.t) {
+ return -1;
+ } else {
+ return 1;
+ }
}
+ // else equal terms, so proceed to compare timestamp component.
+
+ // Otherwise, choose the OpTime with the lower timestamp.
+ return timestampCmp(ot1.ts, ot2.ts);
};
help = shellHelper.help = function(x) {