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
|
/**
* Copyright (C) 2013 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/>.
*/
#include "mongo/db/range_deleter_db_env.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/authorization_session.h"
#include "mongo/db/clientcursor.h"
#include "mongo/db/cmdline.h"
#include "mongo/db/dbhelpers.h"
#include "mongo/db/repl/rs.h"
#include "mongo/db/repl/write_concern.h"
#include "mongo/s/d_logic.h"
namespace mongo {
/**
* Outline of the delete process:
* 1. Initialize the client for this thread if there is no client. This is for the worker
* threads that are attached to any of the threads servicing client requests.
* 2. Grant this thread authorization to perform deletes.
* 3. Temporarily enable mode to bypass shard version checks. TODO: Replace this hack.
* 4. Setup callback to save deletes to moveChunk directory (only if moveParanoia is true).
* 5. Delete range.
* 6. Wait until the majority of the secondaries catch up.
*/
bool RangeDeleterDBEnv::deleteRange(const StringData& ns,
const BSONObj& inclusiveLower,
const BSONObj& exclusiveUpper,
const BSONObj& keyPattern,
bool secondaryThrottle,
std::string* errMsg) {
const bool initiallyHaveClient = haveClient();
if (!initiallyHaveClient) {
Client::initThread("RangeDeleter");
}
if (AuthorizationManager::isAuthEnabled()) {
cc().getAuthorizationSession()->grantInternalAuthorization("_cleanupOldData");
}
ShardForceVersionOkModeBlock forceVersion;
{
RemoveSaver removeSaver("moveChunk", ns.toString(), "post-cleanup");
// log the opId so the user can use it to cancel the delete using killOp.
unsigned int opId = cc().curop()->opNum();
log() << "Deleter starting delete for: " << ns
<< " from " << inclusiveLower
<< " -> " << exclusiveUpper
<< ", with opId: " << opId
<< endl;
try {
long long numDeleted =
Helpers::removeRange(KeyRange(ns.toString(),
inclusiveLower,
exclusiveUpper,
keyPattern),
false, /*maxInclusive*/
replSet? secondaryThrottle : false,
cmdLine.moveParanoia ? &removeSaver : NULL,
true, /*fromMigrate*/
true); /*onlyRemoveOrphans*/
if (numDeleted < 0) {
warning() << "collection or index dropped "
<< "before data could be cleaned" << endl;
if (!initiallyHaveClient) {
cc().shutdown();
}
return false;
}
log() << "rangeDeleter deleted " << numDeleted
<< " documents for " << ns
<< " from " << inclusiveLower
<< " -> " << exclusiveUpper
<< endl;
}
catch (const DBException& ex) {
*errMsg = str::stream() << "Error encountered while deleting range: "
<< "ns" << ns
<< " from " << inclusiveLower
<< " -> " << exclusiveUpper
<< ", cause by:" << causedBy(ex);
if (!initiallyHaveClient) {
cc().shutdown();
}
return false;
}
}
if (replSet) {
Timer elapsedTime;
ReplTime lastOpApplied = cc().getLastOp().asDate();
while (!opReplicatedEnough(lastOpApplied,
BSON("w" << "majority").firstElement())) {
if (elapsedTime.seconds() >= 3600) {
*errMsg = str::stream() << "moveChunk repl sync timed out after "
<< elapsedTime.seconds() << " seconds";
if (!initiallyHaveClient) {
cc().shutdown();
}
return false;
}
sleepsecs(1);
}
LOG(elapsedTime.seconds() < 30 ? 1 : 0)
<< "moveChunk repl sync took "
<< elapsedTime.seconds() << " seconds" << endl;
}
if (!initiallyHaveClient) {
cc().shutdown();
}
return true;
}
void RangeDeleterDBEnv::getCursorIds(const StringData& ns,
std::set<CursorId>* openCursors) {
ClientCursor::find(ns.toString(), *openCursors);
}
}
|