Skip to content

Commit 9ad381f

Browse files
committed
SERVER-13084: clean up ExtentManager layering
1 parent 186d154 commit 9ad381f

File tree

7 files changed

+84
-87
lines changed

7 files changed

+84
-87
lines changed

src/mongo/db/catalog/collection.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,8 @@ namespace mongo {
430430
return &_database->getExtentManager();
431431
}
432432

433-
Extent* Collection::increaseStorageSize( int size, bool enforceQuota ) {
434-
return getExtentManager()->increaseStorageSize( _ns,
435-
_details,
436-
size,
437-
enforceQuota ? largestFileNumberInQuota() : 0 );
433+
void Collection::increaseStorageSize( int size, bool enforceQuota ) {
434+
_recordStore->increaseStorageSize( size, enforceQuota ? largestFileNumberInQuota() : 0 );
438435
}
439436

440437
int Collection::largestFileNumberInQuota() const {

src/mongo/db/catalog/collection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace mongo {
187187
// this will add a new extent the collection
188188
// the new extent will be returned
189189
// it will have been added to the linked list already
190-
Extent* increaseStorageSize( int size, bool enforceQuota );
190+
void increaseStorageSize( int size, bool enforceQuota );
191191

192192
//
193193
// Stats

src/mongo/db/catalog/database.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,10 @@ namespace mongo {
722722
}
723723
else if ( options.capped ) {
724724
// normal
725-
long long size = options.cappedSize;
726-
while ( size > 0 ) {
727-
int mySize = _massageExtentSize( size );
728-
mySize &= 0xffffff00;
729-
Extent* e = collection->increaseStorageSize( mySize, true );
730-
size -= e->length;
725+
while ( collection->storageSize() < options.cappedSize ) {
726+
int sz = _massageExtentSize( options.cappedSize - collection->storageSize() );
727+
sz &= 0xffffff00;
728+
collection->increaseStorageSize( sz, true );
731729
}
732730
}
733731
else {

src/mongo/db/storage/extent_manager.cpp

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
#include "mongo/db/storage/extent.h"
4141
#include "mongo/db/storage/extent_manager.h"
4242
#include "mongo/db/storage/record.h"
43-
#include "mongo/db/structure/catalog/namespace_details.h"
44-
45-
#include "mongo/db/pdfile.h"
4643

4744
namespace mongo {
4845

@@ -168,7 +165,7 @@ namespace mongo {
168165
return preallocateOnly ? 0 : p;
169166
}
170167

171-
DataFile* ExtentManager::addAFile( int sizeNeeded, bool preallocateNextFile ) {
168+
DataFile* ExtentManager::_addAFile( int sizeNeeded, bool preallocateNextFile ) {
172169
DEV Lock::assertWriteLocked( _dbname );
173170
int n = (int) _files.size();
174171
DataFile *ret = getFile( n, sizeNeeded );
@@ -353,7 +350,7 @@ namespace mongo {
353350
}
354351

355352

356-
DiskLoc ExtentManager::createExtent( int size, int maxFileNoForQuota ) {
353+
DiskLoc ExtentManager::_createExtent( int size, int maxFileNoForQuota ) {
357354
size = quantizeExtentSize( size );
358355

359356
if ( size > Extent::maxSize() )
@@ -378,7 +375,7 @@ namespace mongo {
378375
// no space in an existing file
379376
// allocate files until we either get one big enough or hit maxSize
380377
for ( int i = 0; i < 8; i++ ) {
381-
DataFile* f = addAFile( size, false );
378+
DataFile* f = _addAFile( size, false );
382379

383380
if ( f->getHeader()->unusedLength >= size ) {
384381
return _createExtentInFile( numFiles() - 1, f, size, maxFileNoForQuota );
@@ -390,7 +387,7 @@ namespace mongo {
390387
msgasserted(14810, "couldn't allocate space for a new extent" );
391388
}
392389

393-
DiskLoc ExtentManager::allocFromFreeList( int approxSize, bool capped ) {
390+
DiskLoc ExtentManager::_allocFromFreeList( int approxSize, bool capped ) {
394391
// setup extent constraints
395392

396393
int low, high;
@@ -473,54 +470,28 @@ namespace mongo {
473470
return best->myLoc;
474471
}
475472

476-
477-
Extent* ExtentManager::increaseStorageSize( const string& ns,
478-
NamespaceDetails* details,
479-
int size,
480-
int quotaMax ) {
473+
DiskLoc ExtentManager::allocateExtent( const string& ns,
474+
bool capped,
475+
int size,
476+
int quotaMax ) {
481477

482478
bool fromFreeList = true;
483-
DiskLoc eloc = allocFromFreeList( size, details->isCapped() );
479+
DiskLoc eloc = _allocFromFreeList( size, capped );
484480
if ( eloc.isNull() ) {
485481
fromFreeList = false;
486-
eloc = createExtent( size, quotaMax );
482+
eloc = _createExtent( size, quotaMax );
487483
}
488484

489-
verify( !eloc.isNull() );
490-
verify( eloc.isValid() );
485+
invariant( !eloc.isNull() );
486+
invariant( eloc.isValid() );
491487

492-
LOG(1) << "ExtentManager::increaseStorageSize"
488+
LOG(1) << "ExtentManager::allocateExtent"
493489
<< " ns:" << ns
494490
<< " desiredSize:" << size
495491
<< " fromFreeList: " << fromFreeList
496492
<< " eloc: " << eloc;
497493

498-
Extent *e = getExtent( eloc, false );
499-
verify( e );
500-
501-
DiskLoc emptyLoc = getDur().writing(e)->reuse( ns,
502-
details->isCapped() );
503-
504-
if ( details->lastExtent().isNull() ) {
505-
verify( details->firstExtent().isNull() );
506-
details->setFirstExtent( eloc );
507-
details->setLastExtent( eloc );
508-
getDur().writingDiskLoc( details->capExtent() ) = eloc;
509-
verify( e->xprev.isNull() );
510-
verify( e->xnext.isNull() );
511-
}
512-
else {
513-
verify( !details->firstExtent().isNull() );
514-
getDur().writingDiskLoc(e->xprev) = details->lastExtent();
515-
getDur().writingDiskLoc(getExtent(details->lastExtent())->xnext) = eloc;
516-
details->setLastExtent( eloc );
517-
}
518-
519-
details->setLastExtentSize( e->length );
520-
521-
details->addDeletedRec(emptyLoc.drec(), emptyLoc);
522-
523-
return e;
494+
return eloc;
524495
}
525496

526497
void ExtentManager::freeExtents(DiskLoc firstExt, DiskLoc lastExt) {

src/mongo/db/storage/extent_manager.h

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
namespace mongo {
4343

4444
class DataFile;
45-
class NamespaceDetails;
4645

4746
/**
4847
* ExtentManager basics
@@ -85,34 +84,19 @@ namespace mongo {
8584
size_t numFiles() const;
8685
long long fileSize() const;
8786

87+
// TODO: make private
8888
DataFile* getFile( int n, int sizeNeeded = 0, bool preallocateOnly = false );
8989

90-
DataFile* addAFile( int sizeNeeded, bool preallocateNextFile );
91-
92-
void preallocateAFile() { getFile( numFiles() , 0, true ); }// XXX-ERH
90+
// TODO: remove?
91+
void preallocateAFile() { getFile( numFiles() , 0, true ); }
9392

9493
void flushFiles( bool sync );
9594

96-
/* allocate a new Extent, does not check free list
97-
* @param maxFileNoForQuota - 0 for unlimited
98-
*/
99-
DiskLoc createExtent( int approxSize, int maxFileNoForQuota );
100-
101-
/**
102-
* will return NULL if nothing suitable in free list
103-
*/
104-
DiskLoc allocFromFreeList( int approxSize, bool capped );
105-
106-
/**
107-
* @param details - this is for the collection we're adding space to
108-
* @param quotaMax 0 == no limit
109-
* TODO: this isn't quite in the right spot
110-
* really need the concept of a NamespaceStructure in the current paradigm
111-
*/
112-
Extent* increaseStorageSize( const string& ns,
113-
NamespaceDetails* details,
114-
int size,
115-
int quotaMax );
95+
// must call Extent::reuse on the returned extent
96+
DiskLoc allocateExtent( const string& ns,
97+
bool capped,
98+
int size,
99+
int quotaMax );
116100

117101
/**
118102
* firstExt has to be == lastExt or a chain
@@ -167,6 +151,18 @@ namespace mongo {
167151

168152
private:
169153

154+
/**
155+
* will return NULL if nothing suitable in free list
156+
*/
157+
DiskLoc _allocFromFreeList( int approxSize, bool capped );
158+
159+
/* allocate a new Extent, does not check free list
160+
* @param maxFileNoForQuota - 0 for unlimited
161+
*/
162+
DiskLoc _createExtent( int approxSize, int maxFileNoForQuota );
163+
164+
DataFile* _addAFile( int sizeNeeded, bool preallocateNextFile );
165+
170166
DiskLoc _getFreeListStart() const;
171167
DiskLoc _getFreeListEnd() const;
172168
void _setFreeListStart( DiskLoc loc );

src/mongo/db/structure/record_store.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,38 @@ namespace mongo {
188188
}
189189
}
190190

191+
void RecordStoreV1Base::increaseStorageSize( int size, int quotaMax ) {
192+
DiskLoc eloc = _extentManager->allocateExtent( _ns,
193+
_details->isCapped(),
194+
size,
195+
quotaMax );
196+
197+
Extent *e = _extentManager->getExtent( eloc, false );
198+
199+
invariant( e );
200+
201+
DiskLoc emptyLoc = getDur().writing(e)->reuse( _ns, _details->isCapped() );
202+
203+
if ( _details->lastExtent().isNull() ) {
204+
verify( _details->firstExtent().isNull() );
205+
_details->setFirstExtent( eloc );
206+
_details->setLastExtent( eloc );
207+
getDur().writingDiskLoc( _details->capExtent() ) = eloc;
208+
verify( e->xprev.isNull() );
209+
verify( e->xnext.isNull() );
210+
}
211+
else {
212+
verify( !_details->firstExtent().isNull() );
213+
getDur().writingDiskLoc(e->xprev) = _details->lastExtent();
214+
getDur().writingDiskLoc(_extentManager->getExtent(_details->lastExtent())->xnext) = eloc;
215+
_details->setLastExtent( eloc );
216+
}
217+
218+
_details->setLastExtentSize( e->length );
219+
220+
_details->addDeletedRec(emptyLoc.drec(), emptyLoc);
221+
}
222+
191223
// -------------------------------
192224

193225
SimpleRecordStoreV1::SimpleRecordStoreV1( const StringData& ns,
@@ -207,10 +239,9 @@ namespace mongo {
207239

208240
LOG(1) << "allocating new extent";
209241

210-
_extentManager->increaseStorageSize( _ns, _details,
211-
Extent::followupSize( lengthWithHeaders,
212-
_details->lastExtentSize()),
213-
quotaMax );
242+
increaseStorageSize( Extent::followupSize( lengthWithHeaders,
243+
_details->lastExtentSize()),
244+
quotaMax );
214245

215246
loc = _details->alloc( NULL, _ns, lengthWithHeaders );
216247
if ( !loc.isNull() ) {
@@ -225,10 +256,9 @@ namespace mongo {
225256
for ( int z = 0; z < 10 && lengthWithHeaders > _details->lastExtentSize(); z++ ) {
226257
log() << "try #" << z << endl;
227258

228-
_extentManager->increaseStorageSize( _ns, _details,
229-
Extent::followupSize( lengthWithHeaders,
230-
_details->lastExtentSize()),
231-
quotaMax );
259+
increaseStorageSize( Extent::followupSize( lengthWithHeaders,
260+
_details->lastExtentSize()),
261+
quotaMax );
232262

233263
loc = _details->alloc( NULL, _ns, lengthWithHeaders);
234264
if ( ! loc.isNull() )

src/mongo/db/structure/record_store.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ namespace mongo {
5656

5757
virtual StatusWith<DiskLoc> insertRecord( const DocWriter* doc, int quotaMax ) = 0;
5858

59+
// TODO: this makes me sad, it shouldn't be in the interface
60+
// do not use this anymore
61+
virtual void increaseStorageSize( int size, int quotaMax ) = 0;
5962
protected:
6063
std::string _ns;
6164
};
@@ -77,6 +80,8 @@ namespace mongo {
7780

7881
StatusWith<DiskLoc> insertRecord( const DocWriter* doc, int quotaMax );
7982

83+
void increaseStorageSize( int size, int quotaMax );
84+
8085
protected:
8186
virtual StatusWith<DiskLoc> allocRecord( int lengthWithHeaders, int quotaMax ) = 0;
8287

0 commit comments

Comments
 (0)