summaryrefslogtreecommitdiff
path: root/src/mongo/db/curop.h
blob: 06539aeaacd7c1f14cdea9d9db4c8c6b5d4c1d63 (plain)
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
// @file curop.h

/*
 *    Copyright (C) 2010 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.
 */


#pragma once

#include "mongo/base/disallow_copying.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/server_options.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/util/concurrency/spin_lock.h"
#include "mongo/util/progress_meter.h"
#include "mongo/util/thread_safe_string.h"
#include "mongo/util/time_support.h"
#include "mongo/util/net/message.h"

namespace mongo {

class Client;
class Command;
class CurOp;
class OperationContext;
struct PlanSummaryStats;

/**
 * stores a copy of a bson obj in a fixed size buffer
 * if its too big for the buffer, says "too big"
 * useful for keeping a copy around indefinitely without wasting a lot of space or doing malloc
 */
class CachedBSONObjBase {
public:
    static BSONObj _tooBig;  // { $msg : "query not recording (too large)" }
};

template <size_t BUFFER_SIZE>
class CachedBSONObj : public CachedBSONObjBase {
public:
    enum { TOO_BIG_SENTINEL = 1 };

    CachedBSONObj() {
        reset();
    }

    void reset(int sz = 0) {
        _lock.lock();
        _reset(sz);
        _lock.unlock();
    }

    void set(const BSONObj& o) {
        scoped_spinlock lk(_lock);
        size_t sz = o.objsize();
        if (sz > sizeof(_buf)) {
            _reset(TOO_BIG_SENTINEL);
        } else {
            memcpy(_buf, o.objdata(), sz);
        }
    }

    int size() const {
        return ConstDataView(_buf).read<LittleEndian<int>>();
    }
    bool have() const {
        return size() > 0;
    }
    bool tooBig() const {
        return size() == TOO_BIG_SENTINEL;
    }

    BSONObj get() const {
        scoped_spinlock lk(_lock);
        return _get();
    }

    void append(BSONObjBuilder& b, StringData name) const {
        scoped_spinlock lk(_lock);
        BSONObj temp = _get();
        b.append(name, temp);
    }

private:
    /** you have to be locked when you call this */
    BSONObj _get() const {
        int sz = size();
        if (sz == 0)
            return BSONObj();
        if (sz == TOO_BIG_SENTINEL)
            return _tooBig;
        return BSONObj(_buf).copy();
    }

    /** you have to be locked when you call this */
    void _reset(int sz) {
        DataView(_buf).write<LittleEndian<int>>(sz);
    }

    mutable SpinLock _lock;
    char _buf[BUFFER_SIZE];
};

/* lifespan is different than CurOp because of recursives with DBDirectClient */
class OpDebug {
public:
    OpDebug() = default;

    std::string report(const CurOp& curop, const SingleThreadedLockStats& lockStats) const;

    /**
     * Appends information about the current operation to "builder"
     *
     * @param curop reference to the CurOp that owns this OpDebug
     * @param lockStats lockStats object containing locking information about the operation
     */
    void append(const CurOp& curop,
                const SingleThreadedLockStats& lockStats,
                BSONObjBuilder& builder) const;

    /**
     * Copies relevant plan summary metrics to this OpDebug instance.
     */
    void setPlanSummaryMetrics(const PlanSummaryStats& planSummaryStats);

    // -------------------

    // basic options
    // _networkOp represents the network-level op code: OP_QUERY, OP_GET_MORE, OP_COMMAND, etc.
    NetworkOp networkOp{opInvalid};  // only set this through setNetworkOp_inlock() to keep synced
    // _logicalOp is the logical operation type, ie 'dbQuery' regardless of whether this is an
    // OP_QUERY find, a find command using OP_QUERY, or a find command using OP_COMMAND.
    // Similarly, the return value will be dbGetMore for both OP_GET_MORE and getMore command.
    LogicalOp logicalOp{LogicalOp::opInvalid};  // only set this through setNetworkOp_inlock()
    bool iscommand{false};
    BSONObj query{};
    BSONObj updateobj{};

    // detailed options
    long long cursorid{-1};
    long long ntoreturn{-1};
    long long ntoskip{-1};
    bool exhaust{false};

    // debugging/profile info
    long long keysExamined{-1};
    long long docsExamined{-1};

    // indicates short circuited code path on an update to make the update faster
    bool idhack{false};

    bool hasSortStage{false};  // true if the query plan involves an in-memory sort

    // True if the plan came from the multi-planner (not from the plan cache and not a query with a
    // single solution).
    bool fromMultiPlanner{false};

    // True if a replan was triggered during the execution of this operation.
    bool replanned{false};

    long long nMatched{-1};   // number of records that match the query
    long long nModified{-1};  // number of records written (no no-ops)
    long long nmoved{-1};     // updates resulted in a move (moves are expensive)
    long long ninserted{-1};
    long long ndeleted{-1};
    bool fastmod{false};
    bool fastmodinsert{false};  // upsert of an $operation. builds a default object
    bool upsert{false};         // true if the update actually did an insert
    bool cursorExhausted{
        false};  // true if the cursor has been closed at end a find/getMore operation
    int keyUpdates{-1};
    long long writeConflicts{0};

    // New Query Framework debugging/profiling info
    // TODO: should this really be an opaque BSONObj?  Not sure.
    CachedBSONObj<4096> execStats;

    // error handling
    ExceptionInfo exceptionInfo;

    // response info
    int executionTime{0};
    long long nreturned{-1};
    int responseLength{-1};

private:
    /**
     * Returns true if this OpDebug instance was generated by a find command. Returns false for
     * OP_QUERY find and all other operations.
     */
    bool isFindCommand() const;

    /**
     * Returns true if this OpDebug instance was generated by a find command. Returns false for
     * OP_GET_MORE and all other operations.
     */
    bool isGetMoreCommand() const;
};

/**
 * Container for data used to report information about an OperationContext.
 *
 * Every OperationContext in a server with CurOp support has a stack of CurOp
 * objects. The entry at the top of the stack is used to record timing and
 * resource statistics for the executing operation or suboperation.
 *
 * All of the accessor methods on CurOp may be called by the thread executing
 * the associated OperationContext at any time, or by other threads that have
 * locked the context's owning Client object.
 *
 * The mutator methods on CurOp whose names end _inlock may only be called by the thread
 * executing the associated OperationContext and Client, and only when that thread has also
 * locked the Client object.  All other mutators may only be called by the thread executing
 * CurOp, but do not require holding the Client lock.  The exception to this is the kill()
 * method, which is self-synchronizing.
 *
 * The OpDebug member of a CurOp, accessed via the debug() accessor should *only* be accessed
 * from the thread executing an operation, and as a result its fields may be accessed without
 * any synchronization.
 */
class CurOp {
    MONGO_DISALLOW_COPYING(CurOp);

public:
    static CurOp* get(const OperationContext* opCtx);
    static CurOp* get(const OperationContext& opCtx);

    /**
     * Constructs a nested CurOp at the top of the given "opCtx"'s CurOp stack.
     */
    explicit CurOp(OperationContext* opCtx);
    ~CurOp();

    bool haveQuery() const {
        return _query.have();
    }
    BSONObj query() const {
        return _query.get();
    }
    void appendQuery(BSONObjBuilder& b, StringData name) const {
        _query.append(b, name);
    }

    void enter_inlock(const char* ns, int dbProfileLevel);

    /**
     * Sets the type of the current network operation.
     */
    void setNetworkOp_inlock(NetworkOp op) {
        _networkOp = op;
        _debug.networkOp = op;
    }

    /**
     * Sets the type of the current logical operation.
     */
    void setLogicalOp_inlock(LogicalOp op) {
        _logicalOp = op;
        _debug.logicalOp = op;
    }

    /**
     * Marks the current operation as being a command.
     */
    void markCommand_inlock() {
        _isCommand = true;
    }

    /**
     * Returns a structure containing data used for profiling, accessed only by a thread
     * currently executing the operation context associated with this CurOp.
     */
    OpDebug& debug() {
        return _debug;
    }

    /**
     * Gets the name of the namespace on which the current operation operates.
     */
    std::string getNS() const {
        return _ns;
    }

    bool shouldDBProfile(int ms) const {
        if (_dbprofile <= 0)
            return false;

        return _dbprofile >= 2 || ms >= serverGlobalParams.slowMS;
    }

    /**
     * Raises the profiling level for this operation to "dbProfileLevel" if it was previously
     * less than "dbProfileLevel".
     *
     * This belongs on OpDebug, and so does not have the _inlock suffix.
     */
    void raiseDbProfileLevel(int dbProfileLevel);

    /**
     * Gets the network operation type. No lock is required if called by the thread executing
     * the operation, but the lock must be held if called from another thread.
     */
    NetworkOp getNetworkOp() const {
        return _networkOp;
    }

    /**
     * Gets the logical operation type. No lock is required if called by the thread executing
     * the operation, but the lock must be held if called from another thread.
     */
    LogicalOp getLogicalOp() const {
        return _logicalOp;
    }

    /**
     * Returns true if the current operation is known to be a command.
     */
    bool isCommand() const {
        return _isCommand;
    }

    //
    // Methods for controlling CurOp "max time".
    //

    /**
     * Sets the amount of time operation this should be allowed to run, units of microseconds.
     * The special value 0 is "allow to run indefinitely".
     */
    void setMaxTimeMicros(uint64_t maxTimeMicros);

    /**
     * Returns true if a time limit has been set on this operation, and false otherwise.
     */
    bool isMaxTimeSet() const;

    /**
     * Checks whether this operation has been running longer than its time limit.  Returns
     * false if not, or if the operation has no time limit.
     */
    bool maxTimeHasExpired();

    /**
     * Returns the number of microseconds remaining for this operation's time limit, or the
     * special value 0 if the operation has no time limit.
     *
     * Calling this method is more expensive than calling its sibling "maxTimeHasExpired()",
     * since an accurate measure of remaining time needs to be calculated.
     */
    uint64_t getRemainingMaxTimeMicros() const;

    //
    // Methods for getting/setting elapsed time. Note that the observed elapsed time may be
    // negative, if the system time has been reset during the course of this operation.
    //

    void ensureStarted();
    bool isStarted() const {
        return _start > 0;
    }
    long long startTime() {  // micros
        ensureStarted();
        return _start;
    }
    void done() {
        _end = curTimeMicros64();
    }

    long long totalTimeMicros() {
        massert(12601, "CurOp not marked done yet", _end);
        return _end - startTime();
    }
    int totalTimeMillis() {
        return (int)(totalTimeMicros() / 1000);
    }
    long long elapsedMicros() {
        return curTimeMicros64() - startTime();
    }
    int elapsedMillis() {
        return (int)(elapsedMicros() / 1000);
    }
    int elapsedSeconds() {
        return elapsedMillis() / 1000;
    }

    void setQuery_inlock(const BSONObj& query) {
        _query.set(query);
    }

    Command* getCommand() const {
        return _command;
    }
    void setCommand_inlock(Command* command) {
        _command = command;
    }

    /**
     * Appends information about this CurOp to "builder".
     *
     * If called from a thread other than the one executing the operation associated with this
     * CurOp, it is necessary to lock the associated Client object before executing this method.
     */
    void reportState(BSONObjBuilder* builder);

    /**
     * Sets the message and the progress meter for this CurOp.
     *
     * While it is necessary to hold the lock while this method executes, the
     * "hit" and "finished" methods of ProgressMeter may be called safely from
     * the thread executing the operation without locking the Client.
     */
    ProgressMeter& setMessage_inlock(const char* msg,
                                     std::string name = "Progress",
                                     unsigned long long progressMeterTotal = 0,
                                     int secondsBetween = 3);

    /**
     * Gets the message for this CurOp.
     */
    const std::string& getMessage() const {
        return _message;
    }
    const ProgressMeter& getProgressMeter() {
        return _progressMeter;
    }
    CurOp* parent() const {
        return _parent;
    }
    void yielded() {
        _numYields++;
    }  // Should be _inlock()?

    /**
     * Returns the number of times yielded() was called.  Callers on threads other
     * than the one executing the operation must lock the client.
     */
    int numYields() const {
        return _numYields;
    }

    /**
     * Access to _expectedLatencyMs is not synchronized, so it is illegal for threads other than the
     * one executing the operation to call getExpectedLatencyMs() and setExpectedLatencyMs().
     */
    long long getExpectedLatencyMs() const {
        return _expectedLatencyMs;
    }
    void setExpectedLatencyMs(long long latency) {
        _expectedLatencyMs = latency;
    }

    /**
     * this should be used very sparingly
     * generally the Context should set this up
     * but sometimes you want to do it ahead of time
     */
    void setNS_inlock(StringData ns);

    StringData getPlanSummary() const {
        return _planSummary;
    }

    void setPlanSummary_inlock(StringData summary) {
        _planSummary = summary.toString();
    }

    void setPlanSummary_inlock(std::string summary) {
        _planSummary = std::move(summary);
    }

private:
    class CurOpStack;

    static const OperationContext::Decoration<CurOpStack> _curopStack;

    CurOp(OperationContext*, CurOpStack*);

    CurOpStack* _stack;
    CurOp* _parent{nullptr};
    Command* _command{nullptr};
    long long _start{0};
    long long _end{0};

    // _networkOp represents the network-level op code: OP_QUERY, OP_GET_MORE, OP_COMMAND, etc.
    NetworkOp _networkOp{opInvalid};  // only set this through setNetworkOp_inlock() to keep synced
    // _logicalOp is the logical operation type, ie 'dbQuery' regardless of whether this is an
    // OP_QUERY find, a find command using OP_QUERY, or a find command using OP_COMMAND.
    // Similarly, the return value will be dbGetMore for both OP_GET_MORE and getMore command.
    LogicalOp _logicalOp{LogicalOp::opInvalid};  // only set this through setNetworkOp_inlock()

    bool _isCommand{false};
    int _dbprofile{0};  // 0=off, 1=slow, 2=all
    std::string _ns;
    CachedBSONObj<512> _query;  // CachedBSONObj is thread safe
    OpDebug _debug;
    std::string _message;
    ProgressMeter _progressMeter;
    int _numYields{0};

    // this is how much "extra" time a query might take
    // a writebacklisten for example will block for 30s
    // so this should be 30000 in that case
    long long _expectedLatencyMs{0};

    // Time limit for this operation.  0 if the operation has no time limit.
    uint64_t _maxTimeMicros{0u};

    std::string _planSummary;

    /** Nested class that implements tracking of a time limit for a CurOp object. */
    class MaxTimeTracker {
        MONGO_DISALLOW_COPYING(MaxTimeTracker);

    public:
        /** Newly-constructed MaxTimeTracker objects have the time limit disabled. */
        MaxTimeTracker() = default;

        /** Returns whether or not time tracking is enabled. */
        bool isEnabled() const {
            return _enabled;
        }

        /**
         * Enables time tracking.  The time limit is set to be "durationMicros" microseconds
         * from "startEpochMicros" (units of microseconds since the epoch).
         *
         * "durationMicros" must be nonzero.
         */
        void setTimeLimit(uint64_t startEpochMicros, uint64_t durationMicros);

        /**
         * Checks whether the time limit has been hit.  Returns false if not, or if time
         * tracking is disabled.
         */
        bool checkTimeLimit();

        /**
         * Returns the number of microseconds remaining for the time limit, or the special
         * value 0 if time tracking is disabled.
         *
         * Calling this method is more expensive than calling its sibling "checkInterval()",
         * since an accurate measure of remaining time needs to be calculated.
         */
        uint64_t getRemainingMicros() const;

    private:
        // Whether or not time tracking is enabled for this operation.
        bool _enabled{false};

        // Point in time at which the time limit is hit.  Units of microseconds since the
        // epoch.
        uint64_t _targetEpochMicros{0};

        // Approximate point in time at which the time limit is hit.   Units of milliseconds
        // since the server process was started.
        int64_t _approxTargetServerMillis{0};
    } _maxTimeTracker;
};
}