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
|
// fsync.cpp
#include "pch.h"
#include "mongo/db/d_concurrency.h"
#include "mongo/db/commands.h"
#include "mongo/db/dur.h"
#include "mongo/db/client.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://www.mongodb.org/display/DOCS/fsync+Command"; }
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 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;
}
}
|