summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/sync_tail.h
blob: 2e4424b71cfcfd8fbf9d58d3623e187c221f786f (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
/**
 *    Copyright (C) 2008 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 <deque>

#include "mongo/base/status.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/db/storage/mmap_v1/dur.h"
#include "mongo/stdx/functional.h"
#include "mongo/util/concurrency/old_thread_pool.h"

namespace mongo {

class Database;
class OperationContext;

namespace repl {
class BackgroundSyncInterface;
class ReplicationCoordinator;
class OpTime;

/**
 * "Normal" replica set syncing
 */
class SyncTail {
public:
    using MultiSyncApplyFunc = stdx::function<void(const std::vector<BSONObj>& ops, SyncTail* st)>;

    /**
     * Type of function that takes a non-command op and applies it locally.
     * Used for applying from an oplog.
     * Last boolean argument 'convertUpdateToUpsert' converts some updates to upserts for
     * idempotency reasons.
     * Returns failure status if the op was an update that could not be applied.
     */
    using ApplyOperationInLockFn =
        stdx::function<Status(OperationContext*, Database*, const BSONObj&, bool)>;

    /**
     * Type of function that takes a command op and applies it locally.
     * Used for applying from an oplog.
     * Returns failure status if the op that could not be applied.
     */
    using ApplyCommandInLockFn = stdx::function<Status(OperationContext*, const BSONObj&)>;

    /**
     * Type of function to increment "repl.apply.ops" server status metric.
     */
    using IncrementOpsAppliedStatsFn = stdx::function<void()>;

    SyncTail(BackgroundSyncInterface* q, MultiSyncApplyFunc func);
    virtual ~SyncTail();

    /**
     * Applies the operation that is in param o.
     * Functions for applying operations/commands and increment server status counters may
     * be overridden for testing.
     */
    static Status syncApply(OperationContext* txn,
                            const BSONObj& o,
                            bool convertUpdateToUpsert,
                            ApplyOperationInLockFn applyOperationInLock,
                            ApplyCommandInLockFn applyCommandInLock,
                            IncrementOpsAppliedStatsFn incrementOpsAppliedStats);

    static Status syncApply(OperationContext* txn, const BSONObj& o, bool convertUpdateToUpsert);

    /**
     * Runs _applyOplogUntil(stopOpTime)
     */
    virtual void oplogApplication(OperationContext* txn, const OpTime& stopOpTime);

    void oplogApplication();
    bool peek(BSONObj* obj);

    class OpQueue {
    public:
        OpQueue() : _size(0) {}
        size_t getSize() const {
            return _size;
        }
        const std::deque<BSONObj>& getDeque() const {
            return _deque;
        }
        void push_back(BSONObj& op) {
            _deque.push_back(op);
            _size += op.objsize();
        }
        bool empty() const {
            return _deque.empty();
        }

        BSONObj back() const {
            invariant(!_deque.empty());
            return _deque.back();
        }

    private:
        std::deque<BSONObj> _deque;
        size_t _size;
    };

    // returns true if we should continue waiting for BSONObjs, false if we should
    // stop waiting and apply the queue we have.  Only returns false if !ops.empty().
    bool tryPopAndWaitForMore(OperationContext* txn,
                              OpQueue* ops,
                              ReplicationCoordinator* replCoord);

    /**
     * Fetch a single document referenced in the operation from the sync source.
     */
    virtual BSONObj getMissingDoc(OperationContext* txn, Database* db, const BSONObj& o);

    /**
     * If applyOperation_inlock should be called again after an update fails.
     */
    virtual bool shouldRetry(OperationContext* txn, const BSONObj& o);
    void setHostname(const std::string& hostname);

protected:
    // Cap the batches using the limit on journal commits.
    // This works out to be 100 MB (64 bit) or 50 MB (32 bit)
    static const unsigned int replBatchLimitBytes = dur::UncommittedBytesLimit;
    static const int replBatchLimitSeconds = 1;
    static const unsigned int replBatchLimitOperations = 5000;

    // SyncTail base class always supports awaiting commit if any op has j:true flag
    // that indicates awaiting commit before updating last OpTime.
    virtual bool supportsWaitingUntilDurable() {
        return true;
    }

    // Prefetch and write a deque of operations, using the supplied function.
    // Initial Sync and Sync Tail each use a different function.
    // Returns the last OpTime applied.
    static OpTime multiApply(OperationContext* txn,
                             const OpQueue& ops,
                             OldThreadPool* prefetcherPool,
                             OldThreadPool* writerPool,
                             MultiSyncApplyFunc func,
                             SyncTail* sync,
                             bool supportsAwaitingCommit);

    /**
     * Applies oplog entries until reaching "endOpTime".
     *
     * NOTE:Will not transition or check states
     */
    void _applyOplogUntil(OperationContext* txn, const OpTime& endOpTime);

private:
    std::string _hostname;

    BackgroundSyncInterface* _networkQueue;

    // Function to use during applyOps
    MultiSyncApplyFunc _applyFunc;

    void handleSlaveDelay(const BSONObj& op);

    // persistent pool of worker threads for writing ops to the databases
    OldThreadPool _writerPool;
    // persistent pool of worker threads for prefetching
    OldThreadPool _prefetcherPool;
};

// These free functions are used by the thread pool workers to write ops to the db.
void multiSyncApply(const std::vector<BSONObj>& ops, SyncTail* st);
void multiInitialSyncApply(const std::vector<BSONObj>& ops, SyncTail* st);

}  // namespace repl
}  // namespace mongo