summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/tenant_migration_recipient_service.cpp
blob: 02b0f7be1ab62f8db76c7dcd3974e50c880206ae (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
/**
 *    Copyright (C) 2020-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    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 Server Side 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_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kReplication

#include "mongo/platform/basic.h"

#include "mongo/db/client.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/repl/tenant_migration_recipient_entry_helpers.h"
#include "mongo/db/repl/tenant_migration_recipient_service.h"
#include "mongo/db/repl/tenant_migration_state_machine_gen.h"
#include "mongo/db/repl/wait_for_majority_service.h"
#include "mongo/db/write_concern_options.h"
#include "mongo/logv2/log.h"
#include "mongo/rpc/get_status_from_command_result.h"

namespace mongo {
namespace repl {

// Fails before waiting for the state doc to be majority replicated.
MONGO_FAIL_POINT_DEFINE(failWhilePersistingTenantMigrationRecipientInstanceStateDoc);

TenantMigrationRecipientService::TenantMigrationRecipientService(ServiceContext* serviceContext)
    : PrimaryOnlyService(serviceContext) {}

StringData TenantMigrationRecipientService::getServiceName() const {
    return kTenantMigrationRecipientServiceName;
}

NamespaceString TenantMigrationRecipientService::getStateDocumentsNS() const {
    return NamespaceString::kTenantMigrationRecipientsNamespace;
}

ThreadPool::Limits TenantMigrationRecipientService::getThreadPoolLimits() const {
    // TODO SERVER-50669: This will be replaced by a tunable server parameter.
    return ThreadPool::Limits();
}

std::shared_ptr<PrimaryOnlyService::Instance> TenantMigrationRecipientService::constructInstance(
    BSONObj initialStateDoc) const {
    return std::make_shared<TenantMigrationRecipientService::Instance>(initialStateDoc);
}

TenantMigrationRecipientService::Instance::Instance(BSONObj stateDoc)
    : PrimaryOnlyService::TypedInstance<Instance>() {
    _stateDoc = TenantMigrationRecipientDocument::parse(IDLParserErrorContext("recipientStateDoc"),
                                                        stateDoc);
}

SemiFuture<void> TenantMigrationRecipientService::Instance::run(
    std::shared_ptr<executor::ScopedTaskExecutor> executor) noexcept {
    return ExecutorFuture(**executor)
        .then([this]() -> SharedSemiFuture<void> {
            auto uniqueOpCtx = cc().makeOperationContext();
            auto opCtx = uniqueOpCtx.get();

            // The instance is marked as garbage collect if the migration is either
            // committed or aborted on donor side. So, don't start the recipient task if the
            // instance state doc is marked for garbage collect.
            uassert(ErrorCodes::IllegalOperation,
                    str::stream() << "Can't start the data sync as the state doc is already marked "
                                     "for garbage collect for migration uuid: "
                                  << getMigrationUUID(),
                    !isMarkedForGarbageCollect());

            auto lastOpBeforeRun = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();

            // Persist the state doc before starting the data sync.
            auto status = tenantMigrationRecipientEntryHelpers::insertStateDoc(opCtx, _stateDoc);

            // TODO SERVER-50742: Ignoring duplicate check step should be removed.
            // We can hit duplicate key error when the instances are rebuilt on a new primary after
            // step up. So, it's ok to ignore duplicate key errors.
            if (status != ErrorCodes::DuplicateKey) {
                uassertStatusOK(status);
            }

            auto lastOpAfterRun = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
            // No writes happened implies that the state doc is already on disk. This can happen
            // only when the instances are rebuilt on node step up. And,
            // PrimaryOnlyService::onStepUp() waits for majority commit of the primary no-op oplog
            // entry written by the node in the newer term before scheduling the Instance::run().
            // So, it's safe to assume that instance's state document written in an older term on
            // disk won't get rolled back for step up case.
            if (lastOpBeforeRun == lastOpAfterRun) {
                // TODO SERVER-50742: Add an invariant check to make sure this case can happen only
                // for step up.
                return {Future<void>::makeReady()};
            }

            if (MONGO_unlikely(
                    failWhilePersistingTenantMigrationRecipientInstanceStateDoc.shouldFail())) {
                LOGV2(4878500, "Persisting state doc failed due to fail point enabled.");
                uassert(ErrorCodes::NotWritablePrimary, "not writable primary ", false);
            }

            // Wait for the state doc to be majority replicated.
            return WaitForMajorityService::get(opCtx->getServiceContext())
                .waitUntilMajority(lastOpAfterRun);
        })
        .then([this] {
            // TODO SERVER-48808: Run cloners in MigrationServiceInstance
            // TODO SERVER-48811: Oplog fetching in MigrationServiceInstance
        })
        .onCompletion([this](Status status) {
            LOGV2(4878501,
                  "Tenant Recipient data sync completed.",
                  "migrationId"_attr = getMigrationUUID(),
                  "dbPrefix"_attr = _stateDoc.getDatabasePrefix(),
                  "status"_attr = status);
            return status;
        })
        .semi();
}

const UUID& TenantMigrationRecipientService::Instance::getMigrationUUID() const {
    stdx::lock_guard lk(_mutex);
    return _stateDoc.getId();
}

bool TenantMigrationRecipientService::Instance::isMarkedForGarbageCollect() const {
    stdx::lock_guard lk(_mutex);
    return _stateDoc.getGarbageCollect();
}

}  // namespace repl
}  // namespace mongo