summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/replication_process.h
blob: 5b6229ac6d0042cd39a80fad07d89f4eeb993ae6 (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
/**
 *    Copyright (C) 2017 MongoDB 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 <memory>

#include "mongo/base/disallow_copying.h"
#include "mongo/base/status.h"
#include "mongo/base/status_with.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/repl/optime.h"
#include "mongo/db/repl/replication_consistency_markers.h"
#include "mongo/db/repl/replication_recovery.h"
#include "mongo/stdx/mutex.h"

namespace mongo {

class OperationContext;
class ServiceContext;

namespace repl {

class StorageInterface;

/**
 * This class represents the current replication process state that is used during the replication
 * of operations from the sync source to the current node.
 *
 * For example, the rollback ID, which is persisted to storage, is cached here for the purposes of
 * filling in the metadata for the find/getMore queries used to tail the oplog on the sync source.
 *
 * This class DOES NOT hold any information related to the consensus protocol.
 */
class ReplicationProcess {
    MONGO_DISALLOW_COPYING(ReplicationProcess);

public:
    /**
     * Contains at most one document representing the progress of the current rollback process.
     *
     * Schema:
     *     {_id: "rollbackProgress", applyUntil: <optime>}
     *
     * '_id' is always "rollbackProgress".
     *
     * 'applyUntil' contains the optime of the last oplog entry from the sync source that we need to
     * apply in order to complete rollback successfully.
     */
    static const NamespaceString kRollbackProgressNamespace;

    // Operation Context binding.
    static ReplicationProcess* get(ServiceContext* service);
    static ReplicationProcess* get(ServiceContext& service);
    static ReplicationProcess* get(OperationContext* opCtx);
    static void set(ServiceContext* service, std::unique_ptr<ReplicationProcess> process);

    ReplicationProcess(StorageInterface* storageInterface,
                       std::unique_ptr<ReplicationConsistencyMarkers> consistencyMarkers,
                       std::unique_ptr<ReplicationRecovery> recovery);
    virtual ~ReplicationProcess() = default;

    /**
     * Rollback ID is an increasing counter of how many rollbacks have occurred on this server.
     */
    StatusWith<int> getRollbackID(OperationContext* opCtx);
    Status initializeRollbackID(OperationContext* opCtx);
    Status incrementRollbackID(OperationContext* opCtx);

    /**
     * Rollback progress is set after we have retrieved all the information from the sync source
     * that we need to complete rollback without further communication with the sync source.
     * Rollback progress is cleared when rollback has completed successfully. This information is
     * stored in the 'kRollbackProgressNamespace' collection.
     * If the collection is not empty, it will hold the optime of the oplog entry we pulled down
     * from the sync source into the local.system.rollback.oplog collection that we need to apply
     * through from that collection. It is safe to exit rollback once we have applied this optime.
     *
     * If the collection is not present, we return NamespaceNotFound.
     * If the document is not present, we return NoSuchKey.
     *
     * This function is used at replication startup to check if a previously interrupted rollback
     * process has occurred and that the rollback process can be resumed without contacting any
     * sync source.
     * An error status returned by this function indicates that we did not detect any interrupted
     * rollback and that we can continue with normal replication startup.
     */
    StatusWith<OpTime> getRollbackProgress(OperationContext* opCtx);

    /**
     * Upon success, a document representing the current rollback progress will be present in the
     * 'kRollbackProgressNamespace' collection. This document will contain the optime that this
     * node will have to reach in order to consider rollback complete.
     *
     * If the 'kRollbackProgressNamespace' collection is not present when this function is called,
     * this function will create it before inserting the document.
     */
    Status setRollbackProgress(OperationContext* opCtx, const OpTime& applyUntil);

    /**
     * Removes the rollback progress document from the 'kRollbackProgressNamespace' collection.
     *
     * If the collection is not found, this function will return a successful Status because there's
     * nothing further to do.
     */
    Status clearRollbackProgress(OperationContext* opCtx);

    /**
     * Returns an object used for operating on the documents that maintain replication consistency.
     */
    ReplicationConsistencyMarkers* getConsistencyMarkers();

    /**
     * Returns an object used to recover from the oplog on startup or rollback.
     */
    ReplicationRecovery* getReplicationRecovery();

private:
    // All member variables are labeled with one of the following codes indicating the
    // synchronization rules for accessing them.
    //
    // (R)  Read-only in concurrent operation; no synchronization required.
    // (S)  Self-synchronizing; access in any way from any context.
    // (M)  Reads and writes guarded by _mutex.

    // Guards access to member variables.
    stdx::mutex _mutex;

    // Used to access the storage layer.
    StorageInterface* const _storageInterface;  // (R)

    // Used for operations on documents that maintain replication consistency.
    std::unique_ptr<ReplicationConsistencyMarkers> _consistencyMarkers;  // (S)

    std::unique_ptr<ReplicationRecovery> _recovery;  // (S)

    // Rollback ID. This is a cached copy of the persisted value in the local.system.rollback.id
    // collection.
    int _rbid;  // (M)
};

}  // namespace repl
}  // namespace mongo