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
|
// fsync.cpp
/**
* Copyright (C) 2012 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/pch.h"
#include "mongo/db/commands/fsync.h"
#include <string>
#include <vector>
#include "mongo/db/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/d_concurrency.h"
#include "mongo/db/commands.h"
#include "mongo/db/dur.h"
#include "mongo/db/client.h"
#include "mongo/db/jsobj.h"
#include "mongo/util/background.h"
namespace mongo {
class FSyncLockThread : public BackgroundJob {
void doRealWork();
public:
FSyncLockThread() : BackgroundJob( true ) {}
virtual ~FSyncLockThread(){}
virtual string name() const { return "FSyncLockThread"; }
virtual void run() {
Client::initThread( "fsyncLockWorker" );
try {
doRealWork();
}
catch ( std::exception& e ) {
error() << "FSyncLockThread exception: " << e.what() << endl;
}
cc().shutdown();
}
};
/* see unlockFsync() for unlocking:
db.$cmd.sys.unlock.findOne()
*/
class FSyncCommand : public Command {
public:
static const char* url() { return "http://dochub.mongodb.org/core/fsynccommand"; }
bool locked;
bool pendingUnlock;
SimpleMutex m; // protects locked var above
string err;
boost::condition _threadSync;
boost::condition _unlockSync;
FSyncCommand() : Command( "fsync" ), m("lockfsync") { locked=false; pendingUnlock=false; }
virtual LockType locktype() const { return NONE; }
virtual bool slaveOk() const { return true; }
virtual bool adminOnly() const { return true; }
virtual void help(stringstream& h) const { h << url(); }
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) {
ActionSet actions;
actions.addAction(ActionType::fsync);
out->push_back(Privilege(AuthorizationManager::SERVER_RESOURCE_NAME, actions));
}
virtual bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
if (Lock::isLocked()) {
errmsg = "fsync: Cannot execute fsync command from contexts that hold a data lock";
return false;
}
bool sync = !cmdObj["async"].trueValue(); // async means do an fsync, but return immediately
bool lock = cmdObj["lock"].trueValue();
log() << "CMD fsync: sync:" << sync << " lock:" << lock << endl;
if( lock ) {
if ( ! sync ) {
errmsg = "fsync: sync option must be true when using lock";
return false;
}
SimpleMutex::scoped_lock lk(m);
err = "";
(new FSyncLockThread())->go();
while ( ! locked && err.size() == 0 ) {
_threadSync.wait( m );
}
if ( err.size() ){
errmsg = err;
return false;
}
log() << "db is now locked for snapshotting, no writes allowed. db.fsyncUnlock() to unlock" << endl;
log() << " For more info see " << FSyncCommand::url() << endl;
result.append("info", "now locked against writes, use db.fsyncUnlock() to unlock");
result.append("seeAlso", FSyncCommand::url());
}
else {
// the simple fsync command case
if (sync) {
Lock::GlobalWrite w; // can this be GlobalRead? and if it can, it should be nongreedy.
getDur().commitNow();
}
// question : is it ok this is not in the dblock? i think so but this is a change from past behavior,
// please advise.
result.append( "numFiles" , MemoryMappedFile::flushAll( sync ) );
}
return 1;
}
} fsyncCmd;
SimpleMutex filesLockedFsync("filesLockedFsync");
void FSyncLockThread::doRealWork() {
SimpleMutex::scoped_lock lkf(filesLockedFsync);
Lock::GlobalWrite global(true/*stopGreed*/);
SimpleMutex::scoped_lock lk(fsyncCmd.m);
verify( ! fsyncCmd.locked ); // impossible to get here if locked is true
try {
getDur().syncDataAndTruncateJournal();
}
catch( std::exception& e ) {
error() << "error doing syncDataAndTruncateJournal: " << e.what() << endl;
fsyncCmd.err = e.what();
fsyncCmd._threadSync.notify_one();
fsyncCmd.locked = false;
return;
}
global.downgrade();
try {
MemoryMappedFile::flushAll(true);
}
catch( std::exception& e ) {
error() << "error doing flushAll: " << e.what() << endl;
fsyncCmd.err = e.what();
fsyncCmd._threadSync.notify_one();
fsyncCmd.locked = false;
return;
}
verify( ! fsyncCmd.locked );
fsyncCmd.locked = true;
fsyncCmd._threadSync.notify_one();
while ( ! fsyncCmd.pendingUnlock ) {
fsyncCmd._unlockSync.wait(fsyncCmd.m);
}
fsyncCmd.pendingUnlock = false;
fsyncCmd.locked = false;
fsyncCmd.err = "unlocked";
fsyncCmd._unlockSync.notify_one();
}
bool lockedForWriting() {
return fsyncCmd.locked;
}
// @return true if unlocked
bool _unlockFsync() {
verify(!Lock::isLocked());
SimpleMutex::scoped_lock lk( fsyncCmd.m );
if( !fsyncCmd.locked ) {
return false;
}
fsyncCmd.pendingUnlock = true;
fsyncCmd._unlockSync.notify_one();
fsyncCmd._threadSync.notify_one();
while ( fsyncCmd.locked ) {
fsyncCmd._unlockSync.wait( fsyncCmd.m );
}
return true;
}
}
|