0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/dur.cpp

381 lines
14 KiB
C++
Raw Normal View History

2010-10-05 02:09:41 +02:00
// @file dur.cpp durability in the storage engine (crash-safeness / journaling)
/**
* Copyright (C) 2009 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/>.
*/
2010-10-05 02:09:41 +02:00
/*
phases
PREPLOGBUFFER
we will build an output buffer ourself and then use O_DIRECT
we could be in read lock for this
2010-11-13 19:04:48 +01:00
for very large objects write directly to redo log in situ?
2010-11-15 04:28:04 +01:00
WRITETOJOURNAL
2010-10-05 02:09:41 +02:00
we could be unlocked (the main db lock that is...) for this, with sufficient care, but there is some complexity
have to handle falling behind which would use too much ram (going back into a read lock would suffice to stop that).
downgrading to (a perhaps upgradable) read lock would be a good start
2010-11-15 04:28:04 +01:00
WRITETODATAFILES
2010-10-05 02:09:41 +02:00
apply the writes back to the non-private MMF after they are for certain in redo log
REMAPPRIVATEVIEW
we could in a write lock quickly flip readers back to the main view, then stay in read lock and do our real
remapping. with many files (e.g., 1000), remapping could be time consuming (several ms), so we don't want
to be too frequent. tracking time for this step would be wise.
there could be a slow down immediately after remapping as fresh copy-on-writes for commonly written pages will
be required. so doing these remaps more incrementally in the future might make sense - but have to be careful
not to introduce bugs.
*/
#include "pch.h"
2010-11-14 00:32:41 +01:00
#if defined(_DURABLE)
2010-10-05 02:09:41 +02:00
2010-11-15 04:28:04 +01:00
#include "client.h"
2010-10-05 02:09:41 +02:00
#include "dur.h"
2010-11-13 23:42:41 +01:00
#include "dur_journal.h"
2010-11-20 21:29:49 +01:00
#include "dur_commitjob.h"
2010-10-05 02:09:41 +02:00
#include "../util/mongoutils/hash.h"
2010-11-27 00:18:24 +01:00
#include "../util/mongoutils/str.h"
2010-11-15 04:28:04 +01:00
#include "../util/timer.h"
2010-10-05 02:09:41 +02:00
2010-11-27 00:18:24 +01:00
using namespace mongoutils;
2010-10-05 02:09:41 +02:00
namespace mongo {
namespace dur {
2010-11-20 21:29:49 +01:00
static CommitJob cj;
2010-10-05 02:09:41 +02:00
2010-11-27 00:18:24 +01:00
/** Declare that a file has been created
Normally writes are applied only after journalling, for safety. But here the file
is created first, and the journal will just replay the creation if the create didn't
happen because of crashing.
*/
void createdFile(string filename, unsigned long long len) {
shared_ptr<DurOp> op( new FileCreatedOp(filename, len) );
cj.noteOp(op);
}
/** declare write intent. when already in the write view if testIntent is true. */
void declareWriteIntent(void *p, unsigned len) {
2010-11-27 00:18:24 +01:00
// log() << "TEMP dur writing " << p << ' ' << len << endl;
2010-11-15 04:28:04 +01:00
WriteIntent w(p, len);
2010-11-20 21:29:49 +01:00
cj.note(w);
}
void* writingPtr(void *x, unsigned len) {
void *p = x;
if( testIntent )
p = MongoMMF::switchToPrivateView(x);
declareWriteIntent(p, len);
return p;
}
/** declare intent to write
@param ofs offset within buf at which we will write
@param len the length at ofs we will write
@return new buffer pointer. this is modified when testIntent is true.
*/
void* writingAtOffset(void *buf, unsigned ofs, unsigned len) {
char *p = (char *) buf;
if( testIntent )
p = (char *) MongoMMF::switchToPrivateView(buf);
declareWriteIntent(p+ofs, len);
2010-11-15 04:28:04 +01:00
return p;
2010-10-05 02:09:41 +02:00
}
2010-11-27 23:30:51 +01:00
/** Used in _DEBUG builds to check that we didn't overwrite the last intent
that was declared. called just before writelock release. we check a few
bytes after the declared region to see if they changed.
As implemented so far, this doesn't really work as we don't
there may have been other validly declared
s to that area, but helpful for debugging.
*/
void debugCheckLastDeclaredWrite() {
#if 0
assert(debug && durable);
vector<WriteIntent>& w = cj.writes();
if( w.size() == 0 )
return;
const WriteIntent &i = w[w.size()-1];
size_t ofs;
MongoMMF *mmf = privateViews.find(i.p, ofs);
if( mmf == 0 )
return;
size_t past = ofs + i.len;
if( mmf->length() < past + 8 )
return; // too close to end of view
char *priv = (char *) mmf->getView();
char *writ = (char *) mmf->view_write();
unsigned long long *a = (unsigned long long *) (priv+past);
unsigned long long *b = (unsigned long long *) (writ+past);
if( *a != *b ) {
stringstream ss;
ss << "dur data after write area (@" << ((void*)a) << ") does not agree\n"
<< "p: " << i.p << '\n'
<< "now : " << setw(16) << hex << *a << '\n'
<< "was : " << setw(16) << hex << *b;
log() << ss.str() << endl;
for( unsigned z = 0; z < w.size() - 1; z++ ) {
const WriteIntent& wi = w[z];
char *r1 = (char*) wi.p;
char *r2 = r1 + wi.len;
if( r1 <= (char*)a && r2 > (char*)a ) {
log() << "it's ok " << wi.p << ' ' << wi.len << endl;
}
}
log() << "temp" << endl;
}
#endif
}
2010-11-20 21:29:49 +01:00
/** we will build an output buffer ourself and then use O_DIRECT
we could be in read lock for this
caller handles locking
*/
2010-11-27 00:18:24 +01:00
static void PREPLOGBUFFER() {
2010-11-20 21:29:49 +01:00
AlignedBuilder& bb = cj._ab;
2010-11-13 23:42:41 +01:00
bb.reset();
2010-11-15 04:28:04 +01:00
unsigned *lenInBlockHeader;
2010-11-27 00:18:24 +01:00
// JSectHeader
2010-11-15 04:28:04 +01:00
{
bb.appendStr("\nHH\n", false);
lenInBlockHeader = (unsigned *) bb.skip(4);
}
2010-11-13 23:42:41 +01:00
2010-11-27 00:18:24 +01:00
// ops other than basic writes
{
for( vector< shared_ptr<DurOp> >::iterator i = cj.ops().begin(); i != cj.ops().end(); ++i ) {
(*i)->serialize(bb);
}
}
2010-11-15 04:28:04 +01:00
2010-11-27 00:18:24 +01:00
// write intents
2010-11-15 04:28:04 +01:00
{
scoped_lock lk(privateViews._mutex());
2010-11-27 00:18:24 +01:00
string lastFilePath;
2010-11-20 21:29:49 +01:00
for( vector<WriteIntent>::iterator i = cj.writes().begin(); i != cj.writes().end(); i++ ) {
2010-11-15 04:28:04 +01:00
size_t ofs;
MongoMMF *mmf = privateViews._find(i->p, ofs);
if( mmf == 0 ) {
2010-11-27 00:18:24 +01:00
string s = str::stream() << "view pointer cannot be resolved " << i->p;
journalingFailure(s.c_str()); // asserts
return;
}
2010-11-27 21:25:08 +01:00
if( !mmf->willNeedRemap() ) {
mmf->willNeedRemap() = true; // usually it will already be dirty so don't bother writing then
2010-11-15 04:28:04 +01:00
}
2010-11-27 00:18:24 +01:00
//size_t ofs = ((char *)i->p) - ((char*)mmf->getView().p);
i->w_ptr = ((char*)mmf->view_write()) + ofs;
if( mmf->filePath() != lastFilePath ) {
lastFilePath = mmf->filePath();
JDbContext c;
bb.appendStruct(c);
bb.appendStr(lastFilePath);
2010-11-15 04:28:04 +01:00
}
2010-11-27 00:18:24 +01:00
JEntry e;
e.len = i->len;
assert( ofs <= 0x80000000 );
e.ofs = (unsigned) ofs;
e.fileNo = mmf->fileSuffixNo();
bb.appendStruct(e);
bb.appendBuf(i->p, i->len);
2010-11-14 00:32:41 +01:00
}
2010-11-13 23:42:41 +01:00
}
2010-11-15 04:28:04 +01:00
{
JSectFooter f;
f.hash = 0;
bb.appendStruct(f);
}
{
2010-11-27 00:18:24 +01:00
assert( 0xffffe000 == (~(Alignment-1)) );
unsigned L = (bb.len() + Alignment-1) & (~(Alignment-1)); // fill to alignment
2010-11-15 04:28:04 +01:00
dassert( L >= (unsigned) bb.len() );
*lenInBlockHeader = L;
unsigned padding = L - bb.len();
bb.skip(padding);
2010-11-27 00:18:24 +01:00
dassert( bb.len() % Alignment == 0 );
2010-11-15 04:28:04 +01:00
}
2010-11-27 00:18:24 +01:00
return;
2010-11-15 04:28:04 +01:00
}
2010-11-20 21:29:49 +01:00
/** write the buffer we have built to the journal and fsync it.
outside of lock as that could be slow.
*/
static void WRITETOJOURNAL(AlignedBuilder& ab) {
journal(ab);
2010-11-15 04:28:04 +01:00
}
2010-11-17 07:53:52 +01:00
/** apply the writes back to the non-private MMF after they are for certain in redo log
(1) todo we don't need to write back everything every group commit. we MUST write back
that which is going to be a remapped on its private view - but that might not be all
views.
(2) todo should we do this using N threads? would be quite easy
2010-11-18 04:31:38 +01:00
see Hackenberg paper table 5 and 6. 2 threads might be a good balance.
2010-11-17 19:59:29 +01:00
locking: in read lock when called
2010-11-17 07:53:52 +01:00
*/
static void WRITETODATAFILES() {
2010-11-17 19:59:29 +01:00
/* we go backwards as what is at the end is most likely in the cpu cache. it won't be much, but we'll take it. */
2010-11-20 21:29:49 +01:00
for( int i = cj.writes().size() - 1; i >= 0; i-- ) {
const WriteIntent& intent = cj.writes()[i];
2010-11-17 19:59:29 +01:00
char *dst = (char *) (intent.w_ptr);
memcpy(dst, intent.p, intent.len);
}
}
2010-11-27 21:25:08 +01:00
/** We need to remap the private views periodically. otherwise they would become very large.
Call within write lock.
2010-11-17 19:59:29 +01:00
*/
2010-11-27 21:25:08 +01:00
void REMAPPRIVATEVIEW() {
static unsigned startAt;
static unsigned long long lastRemap;
dbMutex.assertWriteLocked();
dbMutex._remapPrivateViewRequested = false;
assert( !cj.hasWritten() );
// we want to remap all private views about every 2 seconds. there could be ~1000 views so
// we do a little each pass; beyond the remap time, more significantly, there will be copy on write
// faults after remapping, so doing a little bit at a time will avoid big load spikes on
// remapping.
unsigned long long now = curTimeMicros64();
double fraction = (now-lastRemap)/20000000.0;
set<MongoFile*>& files = MongoFile::getAllFiles();
unsigned sz = files.size();
if( sz == 0 )
return;
unsigned ntodo = (unsigned) (sz * fraction);
if( ntodo < 1 ) ntodo = 1;
if( ntodo > sz ) ntodo = sz;
const set<MongoFile*>::iterator b = files.begin();
const set<MongoFile*>::iterator e = files.end();
set<MongoFile*>::iterator i = b;
for( unsigned x = 0; x < startAt; x++ ) {
i++;
if( i == e ) i = b;
}
startAt = (startAt + ntodo) % sz;
for( unsigned x = 0; x < ntodo; x++ ) {
dassert( i != e );
MongoMMF *mmf = dynamic_cast<MongoMMF*>(*i);
if( mmf && mmf->willNeedRemap() ) {
mmf->willNeedRemap() = false;
mmf->remapThePrivateView();
}
i++;
if( i == e ) i = b;
2010-11-17 07:53:52 +01:00
}
2010-11-17 19:59:29 +01:00
}
2010-11-17 07:53:52 +01:00
2010-11-27 00:18:24 +01:00
/** locking in read lock when called
@see MongoMMF::close()
*/
void _go() {
dbMutex.assertAtLeastReadLocked();
2010-11-20 21:29:49 +01:00
if( !cj.hasWritten() )
2010-11-17 19:59:29 +01:00
return;
2010-11-20 21:29:49 +01:00
PREPLOGBUFFER();
2010-11-15 04:28:04 +01:00
2010-11-20 21:29:49 +01:00
WRITETOJOURNAL(cj._ab);
2010-11-17 07:53:52 +01:00
2010-11-20 21:29:49 +01:00
// write the noted write intent entries to the data files.
// this has to come after writing to the journal, obviously...
2010-11-17 07:53:52 +01:00
WRITETODATAFILES();
2010-11-27 21:25:08 +01:00
cj.reset();
2010-11-20 21:29:49 +01:00
// remapping private views must occur after WRITETODATAFILES otherwise
// we wouldn't see newly written data on reads.
2010-11-27 21:25:08 +01:00
//
// this needs done in a write lock thus we do it on the next acquisition of that
// instead of here.
// REMAPPRIVATEVIEW();
2010-11-13 23:42:41 +01:00
}
2010-11-20 21:29:49 +01:00
static void go() {
if( !cj.hasWritten() )
return;
2010-11-13 23:42:41 +01:00
{
readlocktry lk("", 1000);
if( lk.got() ) {
2010-11-20 21:29:49 +01:00
_go();
2010-11-13 23:42:41 +01:00
return;
}
}
// starvation on read locks could occur. so if read lock acquisition is slow, try to get a
// write lock instead. otherwise writes could use too much RAM.
writelock lk;
2010-11-20 21:29:49 +01:00
_go();
2010-11-13 23:42:41 +01:00
}
2010-11-15 04:28:04 +01:00
static void durThread() {
Client::initThread("dur");
const int HowOftenToGroupCommitMs = 100;
2010-11-13 23:42:41 +01:00
while( 1 ) {
try {
2010-11-15 04:28:04 +01:00
int millis = HowOftenToGroupCommitMs;
{
Timer t;
journalRotate(); // note we do this part outside of mongomutex
millis -= t.millis();
if( millis < 5 || millis > HowOftenToGroupCommitMs )
millis = 5;
}
sleepmillis(millis);
2010-11-20 21:29:49 +01:00
go();
2010-11-13 23:42:41 +01:00
}
2010-11-15 04:28:04 +01:00
catch(std::exception& e) {
log() << "exception in durThread " << e.what() << endl;
2010-11-13 23:42:41 +01:00
}
}
2010-11-13 19:04:48 +01:00
}
2010-11-15 22:03:56 +01:00
void unlinkThread();
2010-11-22 16:35:30 +01:00
void recover();
2010-11-15 22:03:56 +01:00
2010-11-13 23:42:41 +01:00
void startup() {
2010-11-23 16:12:31 +01:00
if( !durable )
return;
2010-11-20 21:29:49 +01:00
if( testIntent )
return;
2010-11-27 21:40:57 +01:00
recover();
2010-11-15 04:28:04 +01:00
journalMakeDir();
2010-11-13 23:42:41 +01:00
boost::thread t(durThread);
2010-11-15 22:03:56 +01:00
boost::thread t2(unlinkThread);
2010-11-13 23:42:41 +01:00
}
2010-11-13 19:04:48 +01:00
2010-10-05 02:09:41 +02:00
} // namespace dur
} // namespace mongo
#endif