summaryrefslogtreecommitdiff
path: root/src/mongo/db/op_observer.h
diff options
context:
space:
mode:
authorADAM David Alan Martin <adam.martin@10gen.com>2018-03-01 16:40:51 -0500
committerADAM David Alan Martin <adam.martin@10gen.com>2018-03-01 16:40:51 -0500
commit296fde1259d29e081069fde1c69bb9ae083932b1 (patch)
tree0e0973b0e6e642e09822f59be220be3a8c036466 /src/mongo/db/op_observer.h
parent22b2b828a922a7459b4e1c75860a11c7eb3db630 (diff)
downloadmongo-296fde1259d29e081069fde1c69bb9ae083932b1.tar.gz
SERVER-32843 Allow multiple times in OpObservers
The OpObserverRegistry was introduced as an abstraction to allow decoupling making data modifications from the side effects, which need to happen as a result of these modifications, such as op log writes, retryable writes, etc. Some of the OpObserver's methods currently return the OpTime which resulted from logging the operation to the replication oplog. In addition, in certain cases, the OpTime resulting from an earlier OpObserver might be needed by a later one, which is the case with retryable writes. In order to support these requirements, the OpObserver(s) chain should have access to some common per-operation structure, where runtime information could be persisted.
Diffstat (limited to 'src/mongo/db/op_observer.h')
-rw-r--r--src/mongo/db/op_observer.h66
1 files changed, 65 insertions, 1 deletions
diff --git a/src/mongo/db/op_observer.h b/src/mongo/db/op_observer.h
index 14d200fd458..2d9da73a1a4 100644
--- a/src/mongo/db/op_observer.h
+++ b/src/mongo/db/op_observer.h
@@ -45,7 +45,7 @@ class OperationContext;
namespace repl {
class OpTime;
-} // repl
+} // namespace repl
/**
* Holds document update information used in logging.
@@ -274,6 +274,70 @@ public:
*/
virtual void onReplicationRollback(OperationContext* opCtx,
const RollbackObserverInfo& rbInfo) = 0;
+
+ struct Times;
+
+protected:
+ class ReservedTimes;
};
+/**
+ * This struct is a decoration for `OperationContext` which contains collected `repl::OpTime`
+ * and `Date_t` timestamps of various critical stages of an operation performed by an OpObserver
+ * chain.
+ */
+struct OpObserver::Times {
+ static Times& get(OperationContext*);
+
+ std::vector<repl::OpTime> reservedOpTimes;
+
+private:
+ friend OpObserver::ReservedTimes;
+
+ // Because `OpObserver`s are re-entrant, it is necessary to track the recursion depth to know
+ // when to actually clear the `reservedOpTimes` vector, using the `ReservedTimes` scope object.
+ int _recursionDepth = 0;
+};
+
+/**
+ * This class is an RAII object to manage the state of the `OpObserver::Times` decoration on an
+ * operation context. Upon destruction the list of times in the decoration on the operation
+ * context is cleared. It is intended for use as a scope object in `OpObserverRegistry` to manage
+ * re-entrancy.
+ */
+class OpObserver::ReservedTimes {
+ ReservedTimes(const ReservedTimes&) = delete;
+ ReservedTimes& operator=(const ReservedTimes&) = delete;
+
+public:
+ ~ReservedTimes() {
+ // Every time the `ReservedTimes` guard goes out of scope, this indicates one fewer level of
+ // recursion in the `OpObserver` registered chain.
+ if (!--_times._recursionDepth) {
+ // When the depth hits 0, the `OpObserver` is considered to have finished, and therefore
+ // the `reservedOpTimes` state needs to be reset.
+ _times.reservedOpTimes.clear();
+ }
+ invariant(_times._recursionDepth >= 0);
+ }
+
+ explicit ReservedTimes(OperationContext* const opCtx) : _times(Times::get(opCtx)) {
+ // Every time that a `ReservedTimes` scope object is instantiated, we have to track if there
+ // was a potentially recursive call. When there was no `OpObserver` chain being executed
+ // before this instantiation, we should have an empty `reservedOpTimes` vector.
+ if (!_times._recursionDepth++) {
+ invariant(_times.reservedOpTimes.empty());
+ }
+
+ invariant(_times._recursionDepth > 0);
+ invariant(_times._recursionDepth == 1 || !opCtx->writesAreReplicated());
+ }
+
+ const Times& get() const {
+ return _times;
+ }
+
+private:
+ Times& _times;
+};
} // namespace mongo