summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/migration_source_manager.h
blob: 5c9cf07751ec0f05d1da2f25f4421eb8202e978f (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
/**
 *    Copyright (C) 2015 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 <list>
#include <set>
#include <string>

#include "mongo/base/disallow_copying.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/s/migration_session_id.h"
#include "mongo/stdx/condition_variable.h"

namespace mongo {

class Database;
class OperationContext;
class PlanExecutor;
class RecordId;

class MigrationSourceManager {
    MONGO_DISALLOW_COPYING(MigrationSourceManager);

public:
    class CriticalSectionState;

    MigrationSourceManager();
    ~MigrationSourceManager();

    /**
     * Returns false if cannot start. One of the reason for not being able to start is there is
     * already an existing migration in progress.
     */
    bool start(OperationContext* txn,
               const MigrationSessionId& sessionId,
               const std::string& ns,
               const BSONObj& min,
               const BSONObj& max,
               const BSONObj& shardKeyPattern);

    void done(OperationContext* txn);

    /**
     * If a migration for the chunk in 'ns' containing 'obj' is in progress, saves this insert
     * to the transfer mods log. The entries saved here are later transferred to the receiving
     * side of the migration.
     */
    void logInsertOp(OperationContext* txn, const char* ns, const BSONObj& obj);

    /**
     * If a migration for the chunk in 'ns' containing 'updatedDoc' is in progress, saves this
     * update to the transfer mods log. The entries saved here are later transferred to the
     * receiving side of the migration.
     */
    void logUpdateOp(OperationContext* txn, const char* ns, const BSONObj& updatedDoc);

    /**
     * If a migration for the chunk in 'ns' containing 'obj' is in progress, saves this delete
     * to the transfer mods log. The entries saved here are later transferred to the receiving
     * side of the migration.
     */
    void logDeleteOp(OperationContext* txn, const char* ns, const BSONObj& obj);

    /**
     * Determines whether the given document 'doc' in namespace 'ns' is within the range
     * of a currently migrating chunk.
     */
    bool isInMigratingChunk(const NamespaceString& ns, const BSONObj& doc);

    /**
     * Called from the source of a migration process, this method transfers the accummulated local
     * mods from source to destination.
     */
    bool transferMods(OperationContext* txn,
                      const MigrationSessionId& sessionId,
                      std::string& errmsg,
                      BSONObjBuilder& b);

    /**
     * Get the disklocs that belong to the chunk migrated and sort them in _cloneLocs (to avoid
     * seeking disk later).
     *
     * @param maxChunkSize number of bytes beyond which a chunk's base data (no indices) is
     *      considered too large to move
     * @param errmsg filled with textual description of error if this call return false
     *
     * Returns false if approximate chunk size is too big to move or true otherwise.
     */
    bool storeCurrentLocs(OperationContext* txn,
                          long long maxChunkSize,
                          std::string& errmsg,
                          BSONObjBuilder& result);

    bool clone(OperationContext* txn,
               const MigrationSessionId& sessionId,
               std::string& errmsg,
               BSONObjBuilder& result);

    void aboutToDelete(const RecordId& dl);

    std::size_t cloneLocsRemaining() const;

    long long mbUsed() const;

    void setInCriticalSection(bool inCritSec);

    bool isActive() const;

    std::shared_ptr<CriticalSectionState> getMigrationCriticalSection();

private:
    friend class LogOpForShardingHandler;

    /**
     * Insert items from docIdList to a new array with the given fieldName in the given builder. If
     * explode is true, the inserted object will be the full version of the document. Note that
     * whenever an item from the docList is inserted to the array, it will also be removed from
     * docList.
     *
     * Should be holding the collection lock for ns if explode is true.
     */
    void _xfer(OperationContext* txn,
               const std::string& ns,
               Database* db,
               std::list<BSONObj>* docIdList,
               BSONObjBuilder& builder,
               const char* fieldName,
               long long& size,
               bool explode);

    NamespaceString _getNS() const;

    // All member variables are labeled with one of the following codes indicating the
    // synchronization rules for accessing them.
    //
    // (M)  Must hold _mutex for access.
    // (MG) For reads, _mutex *OR* Global IX Lock must be held.
    //      For writes, the _mutex *AND* (Global Shared or Exclusive Lock) must be held.
    // (C)  Must hold _cloneLocsMutex for access.
    //
    // Locking order:
    //
    // Global Lock -> _mutex -> _cloneLocsMutex

    mutable stdx::mutex _mutex;

    std::unique_ptr<PlanExecutor> _deleteNotifyExec;  // (M)

    // List of _id of documents that were modified that must be re-cloned.
    std::list<BSONObj> _reload;  // (M)

    // List of _id of documents that were deleted during clone that should be deleted later.
    std::list<BSONObj> _deleted;  // (M)

    // Bytes in _reload + _deleted
    long long _memoryUsed{0};  // (M)

    // Uniquely identifies a migration and indicates a migration is active when set.
    boost::optional<MigrationSessionId> _sessionId{boost::none};  // (MG)

    NamespaceString _nss;      // (MG)
    BSONObj _min;              // (MG)
    BSONObj _max;              // (MG)
    BSONObj _shardKeyPattern;  // (MG)

    mutable stdx::mutex _cloneLocsMutex;

    // List of record id that needs to be transferred from here to the other side.
    std::set<RecordId> _cloneLocs;  // (C)

    // This value is set when setInCriticalSection is called with true argument and is signalled and
    // cleared when it is called with false argument.
    std::shared_ptr<CriticalSectionState> _critSec;
};

/**
 * This object is instantiated once the migration logic enters critical section. It contains all
 * the state which is associated with being in a critical section, such as the bumped metadata
 * version (which has not yet been reflected on the config server).
 */
class MigrationSourceManager::CriticalSectionState {
    MONGO_DISALLOW_COPYING(CriticalSectionState);

public:
    CriticalSectionState();

    /**
     * Blocks until the critical section completes. Returns true if the wait succeeded and the
     * critical section is no longer active, or false if the waitTimeout was exceeded.
     */
    bool waitUntilOutOfCriticalSection(Microseconds waitTimeout);

    /**
     * To be called when the critical section has completed. Signals any threads sitting blocked in
     * waitUntilOutOfCriticalSection. Must only be used once for the lifetime of this object.
     */
    void exitCriticalSection();

private:
    // Only moves from true to false once. Happens under the critical section mutex and the critical
    // section will be signalled.
    bool _inCriticalSection{true};

    stdx::mutex _criticalSectionMutex;
    stdx::condition_variable _criticalSectionCV;
};

}  // namespace mongo