mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
Revert "do preallocation after initial allocation too, make file allocator interface more flexible"
This reverts commit 5d9cb32605
.
This commit is contained in:
parent
9ccebf3f4c
commit
668cf57c52
@ -108,15 +108,9 @@ namespace mongo {
|
||||
int n = (int) files.size();
|
||||
MongoDataFile *ret = getFile( n, sizeNeeded );
|
||||
if ( preallocateNextFile )
|
||||
preallocateAFile();
|
||||
getFile( n + 1, 0, true );
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ok to call multiple times
|
||||
void preallocateAFile() {
|
||||
int n = (int) files.size();
|
||||
getFile( n, 0, true );
|
||||
}
|
||||
|
||||
MongoDataFile* suitableFile( int sizeNeeded ) {
|
||||
MongoDataFile* f = newestFile();
|
||||
|
@ -143,18 +143,13 @@ namespace mongo {
|
||||
for ( int i = 0; i < nExtents; ++i ) {
|
||||
database->suitableFile((int) size)->allocExtent( ns, (int) size, newCapped );
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
while ( size > 0 ) {
|
||||
int max = MongoDataFile::maxSize() - MDFHeader::headerSize();
|
||||
int desiredExtentSize = (int) (size > max ? max : size);
|
||||
Extent *e = database->suitableFile( desiredExtentSize )->allocExtent( ns, desiredExtentSize, newCapped );
|
||||
size -= e->length;
|
||||
}
|
||||
if ( !newCapped ) {
|
||||
// ok to call this multiple times
|
||||
database->preallocateAFile();
|
||||
}
|
||||
}
|
||||
|
||||
NamespaceDetails *d = nsdetails(ns);
|
||||
assert(d);
|
||||
@ -238,6 +233,8 @@ namespace mongo {
|
||||
|
||||
if ( preallocateOnly ) {
|
||||
#if !defined(_WIN32)
|
||||
// if file exists, update 'size' to match existing file size.
|
||||
MemoryMappedFile::updateLength( filename, size );
|
||||
theFileAllocator().requestAllocation( filename, size );
|
||||
#endif
|
||||
return;
|
||||
|
@ -25,54 +25,45 @@ namespace mongo {
|
||||
|
||||
// Handles allocation of contiguous files on disk.
|
||||
class FileAllocator {
|
||||
// The public functions may not be called concurrently. The allocation
|
||||
// functions may be called multiple times per file, but only the first
|
||||
// size specified per file will be used.
|
||||
// The public functions may not be called concurrently. If
|
||||
// allocateAsap() is called for a file after requestAllocation(), the
|
||||
// sizes in each call must be the same.
|
||||
public:
|
||||
void start() {
|
||||
Runner r( *this );
|
||||
boost::thread t( r );
|
||||
}
|
||||
// May be called if file exists. If file exists, or its allocation has
|
||||
// been requested, size is updated to match existing file size.
|
||||
void requestAllocation( const string &name, int &size ) {
|
||||
// May be called if file exists, but may not be called more than once
|
||||
// for a file.
|
||||
void requestAllocation( const string &name, int size ) {
|
||||
if ( boost::filesystem::exists( name ) )
|
||||
return;
|
||||
{
|
||||
boostlock lk( pendingMutex_ );
|
||||
int oldSize = prevSize( name );
|
||||
if ( oldSize != -1 ) {
|
||||
size = oldSize;
|
||||
return;
|
||||
}
|
||||
pending_.push_back( name );
|
||||
pendingSize_[ name ] = size;
|
||||
pending_.push_back( make_pair( name, size ) );
|
||||
}
|
||||
pendingUpdated_.notify_all();
|
||||
}
|
||||
// Returns when file has been allocated. If file exists, size is
|
||||
// updated to match existing file size.
|
||||
// Returns when file has been allocated.
|
||||
void allocateAsap( const string &name, int size ) {
|
||||
pair< string, int > spec( name, size );
|
||||
{
|
||||
boostlock lk( pendingMutex_ );
|
||||
int oldSize = prevSize( name );
|
||||
if ( oldSize != -1 ) {
|
||||
size = oldSize;
|
||||
if ( !inProgress( name ) )
|
||||
return;
|
||||
}
|
||||
pendingSize_[ name ] = size;
|
||||
if ( allocated( name ) )
|
||||
return;
|
||||
if ( pending_.size() == 0 )
|
||||
pending_.push_back( name );
|
||||
else if ( pending_.front() != name ) {
|
||||
pending_.remove( name );
|
||||
list< string >::iterator i = pending_.begin();
|
||||
pending_.push_back( spec );
|
||||
else if ( pending_.front() != spec ) {
|
||||
pending_.remove( spec );
|
||||
list< pair< string, int > >::iterator i = pending_.begin();
|
||||
++i;
|
||||
pending_.insert( i, name );
|
||||
pending_.insert( i, spec );
|
||||
}
|
||||
}
|
||||
pendingUpdated_.notify_all();
|
||||
boostlock lk( pendingMutex_ );
|
||||
while( 1 ) {
|
||||
if ( !inProgress( name ) ) {
|
||||
if ( allocated( name ) ) {
|
||||
return;
|
||||
}
|
||||
pendingUpdated_.wait( lk );
|
||||
@ -89,28 +80,19 @@ namespace mongo {
|
||||
}
|
||||
|
||||
private:
|
||||
// caller must hold pendingMutex_ lock. Returns size if allocated or
|
||||
// allocation requested, -1 otherwise.
|
||||
int prevSize( const string &name ) const {
|
||||
if ( pendingSize_.count( name ) > 0 )
|
||||
return pendingSize_[ name ];
|
||||
if ( boost::filesystem::exists( name ) )
|
||||
return boost::filesystem::file_size( name );
|
||||
return -1;
|
||||
}
|
||||
|
||||
// caller must hold pendingMutex_ lock.
|
||||
bool inProgress( const string &name ) const {
|
||||
for( list< string >::const_iterator i = pending_.begin(); i != pending_.end(); ++i )
|
||||
if ( *i == name )
|
||||
return true;
|
||||
return false;
|
||||
// caller must hold pendingMutex_ lock
|
||||
bool allocated( const string &name ) const {
|
||||
if ( !boost::filesystem::exists( name ) )
|
||||
return false;
|
||||
for( list< pair< string, int > >::const_iterator i = pending_.begin(); i != pending_.end(); ++i )
|
||||
if ( i->first == name )
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
mutable boost::mutex pendingMutex_;
|
||||
mutable boost::condition_variable pendingUpdated_;
|
||||
list< string > pending_;
|
||||
mutable map< string, int > pendingSize_;
|
||||
list< pair< string, int > > pending_;
|
||||
|
||||
struct Runner {
|
||||
Runner( FileAllocator &allocator ) : a_( allocator ) {}
|
||||
@ -129,8 +111,8 @@ namespace mongo {
|
||||
boostlock lk( a_.pendingMutex_ );
|
||||
if ( a_.pending_.size() == 0 )
|
||||
break;
|
||||
name = a_.pending_.front();
|
||||
size = a_.pendingSize_[ name ];
|
||||
name = a_.pending_.front().first;
|
||||
size = a_.pending_.front().second;
|
||||
}
|
||||
try {
|
||||
int fd = open(name.c_str(), O_CREAT | O_RDWR | O_NOATIME, S_IRUSR | S_IWUSR);
|
||||
@ -175,7 +157,6 @@ namespace mongo {
|
||||
|
||||
{
|
||||
boostlock lk( a_.pendingMutex_ );
|
||||
a_.pendingSize_.erase( name );
|
||||
a_.pending_.pop_front();
|
||||
}
|
||||
a_.pendingUpdated_.notify_all();
|
||||
|
@ -51,10 +51,11 @@ namespace mongo {
|
||||
#endif
|
||||
|
||||
void* MemoryMappedFile::map(const char *filename, int length) {
|
||||
// length may be updated by callee.
|
||||
theFileAllocator().allocateAsap( filename, length );
|
||||
updateLength( filename, length );
|
||||
len = length;
|
||||
|
||||
|
||||
theFileAllocator().allocateAsap( filename, length );
|
||||
|
||||
fd = open(filename, O_RDWR | O_NOATIME);
|
||||
if ( fd <= 0 ) {
|
||||
out() << "couldn't open " << filename << ' ' << errno << endl;
|
||||
|
Loading…
Reference in New Issue
Block a user