summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/rollback_impl.cpp
blob: 84d03035654c826748bcc6a9a714dcb003785d47 (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
/**
 *    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.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kReplication

#include "mongo/platform/basic.h"

#include "mongo/db/repl/rollback_impl.h"

#include <exception>

#include "mongo/db/client.h"
#include "mongo/db/concurrency/d_concurrency.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/repl/rollback_impl_listener.h"
#include "mongo/db/s/shard_identity_rollback_notifier.h"
#include "mongo/stdx/memory.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {
namespace repl {

namespace {

/**
 * Creates an operation context using the current Client.
 */
ServiceContext::UniqueOperationContext makeOpCtx() {
    return cc().makeOperationContext();
}

}  // namespace

RollbackImpl::RollbackImpl(executor::TaskExecutor* executor,
                           OplogInterface* localOplog,
                           const HostAndPort& syncSource,
                           const NamespaceString& remoteOplogNss,
                           std::size_t maxFetcherRestarts,
                           int requiredRollbackId,
                           ReplicationCoordinator* replicationCoordinator,
                           StorageInterface* storageInterface,
                           const OnCompletionFn& onCompletion)
    : AbstractAsyncComponent(executor, "rollback"),
      _localOplog(localOplog),
      _syncSource(syncSource),
      _remoteOplogNss(remoteOplogNss),
      _maxFetcherRestarts(maxFetcherRestarts),
      _requiredRollbackId(requiredRollbackId),
      _replicationCoordinator(replicationCoordinator),
      _storageInterface(storageInterface),
      _listener(stdx::make_unique<Listener>()),
      _commonPointResolver(stdx::make_unique<RollbackCommonPointResolver>(
          executor,
          syncSource,
          remoteOplogNss,
          maxFetcherRestarts,
          localOplog,
          _listener.get(),
          stdx::bind(&RollbackImpl::_commonPointResolverCallback, this, stdx::placeholders::_1))),
      _onCompletion(onCompletion) {
    // Task executor will be validated by AbstractAsyncComponent's constructor.
    invariant(localOplog);
    uassert(ErrorCodes::BadValue, "sync source must be valid", !syncSource.empty());
    invariant(replicationCoordinator);
    invariant(storageInterface);
    invariant(onCompletion);
}

RollbackImpl::~RollbackImpl() {
    shutdown();
    join();
}

// static
StatusWith<OpTime> RollbackImpl::readLocalRollbackInfoAndApplyUntilConsistentWithSyncSource(
    ReplicationCoordinator* replicationCoordinator, StorageInterface* storageInterface) {
    return {ErrorCodes::InternalError, "Method not implemented"};
}

Status RollbackImpl::_doStartup_inlock() noexcept {
    return _scheduleWorkAndSaveHandle_inlock(
        stdx::bind(&RollbackImpl::_transitionToRollbackCallback, this, stdx::placeholders::_1),
        &_transitionToRollbackHandle,
        str::stream() << "_transitionToRollbackCallback");
}

void RollbackImpl::_doShutdown_inlock() noexcept {
    _cancelHandle_inlock(_transitionToRollbackHandle);
    _shutdownComponent_inlock(_commonPointResolver);
}

stdx::mutex* RollbackImpl::_getMutex() noexcept {
    return &_mutex;
}

void RollbackImpl::_transitionToRollbackCallback(
    const executor::TaskExecutor::CallbackArgs& callbackArgs) {
    auto status = _checkForShutdownAndConvertStatus(
        callbackArgs, str::stream() << "error before transition to ROLLBACK");
    if (!status.isOK()) {
        _finishCallback(nullptr, status);
        return;
    }

    log() << "Rollback - transition to ROLLBACK";
    auto opCtx = makeOpCtx();
    {
        Lock::GlobalWrite globalWrite(opCtx.get());

        status = _replicationCoordinator->setFollowerMode(MemberState::RS_ROLLBACK);
        if (!status.isOK()) {
            std::string msg = str::stream()
                << "Cannot transition from " << _replicationCoordinator->getMemberState().toString()
                << " to " << MemberState(MemberState::RS_ROLLBACK).toString()
                << causedBy(status.reason());
            log() << msg;
            status = Status(status.code(), msg);
        }
    }
    if (!status.isOK()) {
        _finishCallback(opCtx.get(), status);
        return;
    }

    // Schedule RollbackCommonPointResolver
    status = _startupComponent(_commonPointResolver);
    if (!status.isOK()) {
        _finishCallback(opCtx.get(), status);
        return;
    }
}

void RollbackImpl::_commonPointResolverCallback(const Status& commonPointResolverStatus) {
    auto status = _checkForShutdownAndConvertStatus(
        commonPointResolverStatus,
        str::stream() << "failed to find common point between local and remote oplogs");
    if (!status.isOK()) {
        _finishCallback(nullptr, status);
        return;
    }

    // Success! For now....
    _finishCallback(nullptr, OpTime());
}

void RollbackImpl::_checkShardIdentityRollback(OperationContext* opCtx) {
    invariant(opCtx);

    if (ShardIdentityRollbackNotifier::get(opCtx)->didRollbackHappen()) {
        severe() << "shardIdentity document rollback detected.  Shutting down to clear "
                    "in-memory sharding state.  Restarting this process should safely return it "
                    "to a healthy state";
        fassertFailedNoTrace(40407);
    }
}

void RollbackImpl::_transitionFromRollbackToSecondary(OperationContext* opCtx) {
    invariant(opCtx);

    Lock::GlobalWrite globalWrite(opCtx);

    // If the current member state is not ROLLBACK, this means that the
    // ReplicationCoordinator::setFollowerMode(ROLLBACK) call in _transitionToRollbackCallback()
    // failed. In that case, there's nothing to do in this function.
    if (MemberState(MemberState::RS_ROLLBACK) != _replicationCoordinator->getMemberState()) {
        return;
    }

    auto status = _replicationCoordinator->setFollowerMode(MemberState::RS_SECONDARY);
    if (!status.isOK()) {
        severe() << "Failed to transition into " << MemberState(MemberState::RS_SECONDARY)
                 << "; expected to be in state " << MemberState(MemberState::RS_ROLLBACK)
                 << "; found self in " << _replicationCoordinator->getMemberState()
                 << causedBy(status);
        fassertFailedNoTrace(40408);
    }
}

void RollbackImpl::_tearDown(OperationContext* opCtx) {
    invariant(opCtx);

    _checkShardIdentityRollback(opCtx);
    _transitionFromRollbackToSecondary(opCtx);
}

void RollbackImpl::_finishCallback(OperationContext* opCtx, StatusWith<OpTime> lastApplied) {
    // Abort only when we are in a unrecoverable state.
    // WARNING: these statuses sometimes have location codes which are lost with uassertStatusOK
    // so we need to check here first.
    if (ErrorCodes::UnrecoverableRollbackError == lastApplied.getStatus().code()) {
        severe() << "Unable to complete rollback. A full resync may be needed: "
                 << redact(lastApplied.getStatus());
        fassertFailedNoTrace(40435);
    }

    // After running callback function '_onCompletion', clear '_onCompletion' to release any
    // resources that might be held by this function object.
    // '_onCompletion' must be moved to a temporary copy and destroyed outside the lock in case
    // there is any logic that's invoked at the function object's destruction that might call into
    // this RollbackImpl. 'onCompletion' must be destroyed outside the lock and this should happen
    // before we transition the state to Complete.
    decltype(_onCompletion) onCompletion;
    {
        stdx::lock_guard<stdx::mutex> lock(_mutex);
        invariant(_onCompletion);
        std::swap(_onCompletion, onCompletion);
    }

    // If 'opCtx' is null, lazily create OperationContext using makeOpCtx() that will last for the
    // duration of this function call.
    _tearDown(opCtx ? opCtx : makeOpCtx().get());

    // Completion callback must be invoked outside mutex.
    try {
        onCompletion(lastApplied);
    } catch (...) {
        severe() << "rollback finish callback threw exception: " << redact(exceptionToStatus());
        // This exception handling block should be unreachable because OnCompletionFn is declared
        // noexcept. This is purely a defensive mechanism to guard against C++ runtime
        // implementations that have less than ideal support for noexcept.
        MONGO_UNREACHABLE;
    }

    // Destroy the remaining reference to the completion callback before we transition the state to
    // Complete so that callers can expect any resources bound to '_onCompletion' to be released
    // before RollbackImpl::join() returns.
    onCompletion = {};

    _transitionToComplete();
}

}  // namespace repl
}  // namespace mongo