Files
mongo/db/durop.cpp

162 lines
5.6 KiB
C++
Raw Normal View History

2010-11-26 18:18:24 -05:00
// @file durop.cpp
/**
* Copyright (C) 2010 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 "pch.h"
2010-12-12 19:47:10 -05:00
#include "concurrency.h"
2010-12-20 09:28:19 -05:00
#include "../util/alignedbuilder.h"
2010-11-26 18:18:24 -05:00
#include "../util/mongoutils/str.h"
#include "../util/file.h"
#include "mongommf.h"
2010-11-26 18:18:24 -05:00
#include "durop.h"
#include "../util/file_allocator.h"
2010-11-26 18:18:24 -05:00
using namespace mongoutils;
2011-01-04 00:40:41 -05:00
namespace mongo {
2010-11-26 18:18:24 -05:00
extern string dbpath; // --dbpath parm
2010-12-12 19:47:10 -05:00
void _deleteDataFiles(const char *);
2010-11-26 18:18:24 -05:00
namespace dur {
2011-01-04 00:40:41 -05:00
2010-12-06 16:00:15 -05:00
/** read a durop from journal file referenced by br.
2010-11-26 18:18:24 -05:00
@param opcode the opcode which has already been written from the bufreader
*/
2011-01-04 00:40:41 -05:00
shared_ptr<DurOp> DurOp::read(unsigned opcode, BufReader& br) {
2010-11-26 18:18:24 -05:00
shared_ptr<DurOp> op;
2011-01-04 00:40:41 -05:00
switch( opcode ) {
2010-11-26 18:18:24 -05:00
case JEntry::OpCode_FileCreated:
op = shared_ptr<DurOp>( new FileCreatedOp(br) );
break;
2010-12-12 19:47:10 -05:00
case JEntry::OpCode_DropDb:
op = shared_ptr<DurOp>( new DropDbOp(br) );
break;
2010-11-26 18:18:24 -05:00
default:
massert(13546, (str::stream() << "journal recover: unrecognized opcode in journal " << opcode), false);
2010-11-26 18:18:24 -05:00
}
return op;
}
2011-01-04 00:40:41 -05:00
void DurOp::serialize(AlignedBuilder& ab) {
2010-11-26 18:18:24 -05:00
ab.appendNum(_opcode);
_serialize(ab);
}
2010-12-12 19:47:10 -05:00
DropDbOp::DropDbOp(BufReader& log) : DurOp(JEntry::OpCode_DropDb) {
unsigned long long reserved;
log.read(reserved);
log.read(reserved);
log.readStr(_db);
string reservedStr;
log.readStr(reservedStr);
}
2011-01-04 00:40:41 -05:00
void DropDbOp::_serialize(AlignedBuilder& ab) {
2010-12-12 19:47:10 -05:00
ab.appendNum((unsigned long long) 0); // reserved for future use
ab.appendNum((unsigned long long) 0); // reserved for future use
ab.appendStr(_db);
ab.appendStr(""); // reserved
}
/** throws */
2011-01-04 00:40:41 -05:00
void DropDbOp::replay() {
2010-12-12 19:47:10 -05:00
log() << "recover replay drop db " << _db << endl;
_deleteDataFiles(_db.c_str());
}
2011-01-04 00:40:41 -05:00
FileCreatedOp::FileCreatedOp(string f, unsigned long long l) :
DurOp(JEntry::OpCode_FileCreated) {
_p = RelativePath::fromFullPath(f);
_len = l;
}
2011-01-04 00:40:41 -05:00
FileCreatedOp::FileCreatedOp(BufReader& log) : DurOp(JEntry::OpCode_FileCreated) {
2010-11-26 18:18:24 -05:00
unsigned long long reserved;
log.read(reserved);
log.read(reserved);
log.read(_len); // size of file, not length of name
string s;
log.readStr(s);
_p._p = s;
2010-11-26 18:18:24 -05:00
}
2010-12-20 09:28:19 -05:00
void FileCreatedOp::_serialize(AlignedBuilder& ab) {
2010-11-26 18:18:24 -05:00
ab.appendNum((unsigned long long) 0); // reserved for future use
ab.appendNum((unsigned long long) 0); // reserved for future use
ab.appendNum(_len);
ab.appendStr(_p.toString());
2010-11-26 18:18:24 -05:00
}
2011-01-04 00:40:41 -05:00
string FileCreatedOp::toString() {
2010-12-22 12:43:46 -05:00
return str::stream() << "FileCreatedOp " << _p.toString() << ' ' << _len/1024.0/1024.0 << "MB";
2010-11-26 18:18:24 -05:00
}
// if an operation deletes or creates a file (or moves etc.), it may need files closed.
2010-12-12 19:47:10 -05:00
bool FileCreatedOp::needFilesClosed() {
return exists( _p.asFullPath() );
2010-12-12 19:47:10 -05:00
}
2011-01-04 00:40:41 -05:00
void FileCreatedOp::replay() {
2010-11-26 18:18:24 -05:00
// i believe the code assumes new files are filled with zeros. thus we have to recreate the file,
2010-12-06 16:00:15 -05:00
// or rewrite at least, even if it were the right length. perhaps one day we should change that
// although easier to avoid defects if we assume it is zeros perhaps.
string full = _p.asFullPath();
if( exists(full) ) {
2011-01-04 00:40:41 -05:00
try {
remove(full);
2010-12-12 19:47:10 -05:00
}
2011-01-04 00:40:41 -05:00
catch(std::exception& e) {
2010-12-12 19:47:10 -05:00
log(1) << "recover info FileCreateOp::replay unlink " << e.what() << endl;
}
2010-11-26 18:18:24 -05:00
}
log() << "recover create file " << full << ' ' << _len/1024.0/1024.0 << "MB" << endl;
if( MemoryMappedFile::exists(full) ) {
// first delete if exists.
try {
remove(full);
}
2011-01-04 00:40:41 -05:00
catch(...) {
log() << "warning could not delete file " << full << endl;
}
}
ensureParentDirCreated(full);
2010-11-26 18:18:24 -05:00
File f;
f.open(full.c_str());
massert(13547, str::stream() << "recover couldn't create file " << full, f.is_open());
2010-11-26 18:18:24 -05:00
unsigned long long left = _len;
const unsigned blksz = 64 * 1024;
scoped_array<char> v( new char[blksz] );
2010-11-26 18:18:24 -05:00
memset( v.get(), 0, blksz );
fileofs ofs = 0;
2011-01-04 00:40:41 -05:00
while( left ) {
2010-11-26 18:18:24 -05:00
unsigned long long w = left < blksz ? left : blksz;
f.write(ofs, v.get(), (unsigned) w);
left -= w;
ofs += w;
}
f.fsync();
flushMyDirectory(full);
massert(13628, str::stream() << "recover failure writing file " << full, !f.bad() );
2010-11-26 18:18:24 -05:00
}
}
}