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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/*
* 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/>.
*/
// storage.cpp
#include "stdafx.h"
#include "pdfile.h"
#include "reccache.h"
#include "rec.h"
#include "db.h"
namespace mongo {
RecCache theRecCache(BucketSize);
// 100k * 8KB = 800MB
unsigned RecCache::MAXNODES = 50000;
void setRecCacheSize(unsigned mb) {
unsigned long long MB = mb;
log(2) << "reccache size: " << MB << "MB\n";
uassert( 10114 , "bad cache size", MB > 0 && MB < 1000000 );
RecCache::MAXNODES = (unsigned) MB * 1024 * 1024 / 8192;
log(3) << "RecCache::MAXNODES=" << RecCache::MAXNODES << '\n';
}
void writerThread() {
sleepsecs(10);
while( 1 ) {
try {
theRecCache.writeLazily();
}
catch(...) {
log() << "exception in writerThread()" << endl;
sleepsecs(3);
}
}
}
// called on program exit.
void recCacheCloseAll() {
#if defined(_RECSTORE)
theRecCache.closing();
#endif
}
int ndirtywritten;
inline static string escape(const char *ns) {
char buf[256];
char *p = buf;
while( 1 ) {
if( *ns == '$' ) *p = '~';
else
*p = *ns;
if( *ns == 0 )
break;
p++; ns++;
}
assert( p - buf < (int) sizeof(buf) );
return buf;
}
inline static string unescape(const char *ns) {
char buf[256];
char *p = buf;
while( 1 ) {
if( *ns == '~' ) *p = '$';
else
*p = *ns;
if( *ns == 0 )
break;
p++; ns++;
}
assert( p - buf < (int) sizeof(buf) );
return buf;
}
string RecCache::directory() {
return cc().database()->path;
}
/* filename format is
<n>-<ns>.idx
*/
BasicRecStore* RecCache::_initStore(string fname) {
assert( strchr(fname.c_str(), '/') == 0 );
assert( strchr(fname.c_str(), '\\') == 0 );
stringstream ss(fname);
int n;
ss >> n;
assert( n >= 0 );
char ch;
ss >> ch;
assert( ch == '-' );
string rest;
ss >> rest;
const char *p = rest.c_str();
const char *q = strstr(p, ".idx");
assert( q );
string escaped_ns(p, q-p);
// arbitrary limit. if you are hitting, we should use fewer files and put multiple
// indexes in a single file (which is easy to do)
massert( 10374 , "too many index files", n < 10000 );
if( stores.size() < (unsigned)n+1 )
stores.resize(n+1);
assert( stores[n] == 0 );
BasicRecStore *rs = new BasicRecStore(n);
path pf(directory());
pf /= fname;
string full = pf.string();
rs->init(full.c_str(), recsize);
stores[n] = rs;
string ns = unescape(escaped_ns.c_str());
storesByNsKey[mknskey(ns.c_str())] = rs;
return rs;
}
BasicRecStore* RecCache::initStore(int n) {
string ns;
{
stringstream ss;
ss << '/' << n << '-';
ns = ss.str();
}
/* this will be slow if there are thousands of files */
path dir(directory());
directory_iterator end;
try {
directory_iterator i(dir);
while ( i != end ) {
string s = i->string();
const char *p = strstr(s.c_str(), ns.c_str());
if( p && strstr(p, ".idx") ) {
// found it
path P = *i;
return _initStore(P.leaf());
}
i++;
}
}
catch( DBException & ) {
throw;
}
catch (...) {
string s = string("i/o error looking for .idx file in ") + directory();
massert( 10375 , s, false);
}
stringstream ss;
ss << "index datafile missing? n=" << n;
uasserted(12500,ss.str());
return 0;
}
/* find the filename for a given ns.
format is
<n>-<escaped_ns>.idx
returns filename. found is true if found. If false, a proposed name is returned for (optional) creation
of the file.
*/
string RecCache::findStoreFilename(const char *_ns, bool& found) {
string namefrag;
{
stringstream ss;
ss << '-';
ss << escape(_ns);
ss << ".idx";
namefrag = ss.str();
}
path dir(directory());
directory_iterator end;
int nmax = -1;
try {
directory_iterator i(dir);
while ( i != end ) {
string s = path(*i).leaf();
const char *p = strstr(s.c_str(), namefrag.c_str());
if( p ) {
found = true;
return s;
}
if( strstr(s.c_str(), ".idx") ) {
stringstream ss(s);
int n = -1;
ss >> n;
if( n > nmax )
nmax = n;
}
i++;
}
}
catch (...) {
string s = string("i/o error looking for .idx file in ") + directory();
massert( 10376 , s, false);
}
// DNE. return a name that would work.
stringstream ss;
ss << nmax+1 << namefrag;
found = false;
return ss.str();
}
void RecCache::initStoreByNs(const char *_ns, const string& nskey) {
bool found;
string fn = findStoreFilename(_ns, found);
_initStore(fn);
}
inline void RecCache::writeIfDirty(Node *n) {
if( n->dirty ) {
ndirtywritten++;
n->dirty = false;
store(n->loc).update(fileOfs(n->loc), n->data, recsize);
}
}
void RecCache::closeFiles(string dbname, string path) {
assertInWriteLock();
scoped_lock lk(rcmutex);
// first we write all dirty pages. it is not easy to check which Nodes are for a particular
// db, so we just write them all.
writeDirty( dirtyl.begin(), true );
string key = path + dbname + '.';
unsigned sz = key.size();
for( map<string, BasicRecStore*>::iterator i = storesByNsKey.begin(); i != storesByNsKey.end(); i++ ) {
map<string, BasicRecStore*>::iterator j = i;
i++;
if( strncmp(j->first.c_str(), key.c_str(), sz) == 0 ) {
assert( stores[j->second->fileNumber] != 0 );
stores[j->second->fileNumber] = 0;
delete j->second;
storesByNsKey.erase(j);
}
}
}
void RecCache::closing() {
scoped_lock lk(rcmutex);
(cout << "TEMP: recCacheCloseAll() writing dirty pages...\n").flush();
writeDirty( dirtyl.begin(), true );
for( unsigned i = 0; i < stores.size(); i++ ) {
if( stores[i] ) {
delete stores[i];
}
}
(cout << "TEMP: write dirty done\n").flush();
}
/* note that this is written in order, as much as possible, given that dirtyl is of type set. */
void RecCache::writeDirty( set<DiskLoc>::iterator startAt, bool rawLog ) {
try {
ndirtywritten=0;
for( set<DiskLoc>::iterator i = startAt; i != dirtyl.end(); i++ ) {
map<DiskLoc, Node*>::iterator j = m.find(*i);
if( j != m.end() )
writeIfDirty(j->second);
}
OCCASIONALLY out() << "TEMP: ndirtywritten: " << ndirtywritten << endl;
}
catch(...) {
const char *message = "Problem: bad() in RecCache::writeDirty, file io error\n";
if ( rawLog )
rawOut( message );
else
( log() << message ).flush();
}
dirtyl.clear();
}
void RecCache::writeLazily() {
int sleep = 0;
int k;
{
scoped_lock lk(rcmutex);
Timer t;
set<DiskLoc>::iterator i = dirtyl.end();
for( k = 0; k < 100; k++ ) {
if( i == dirtyl.begin() ) {
// we're not very far behind
sleep = k < 20 ? 2000 : 1000;
break;
}
i--;
}
writeDirty(i);
if( sleep == 0 ) {
sleep = t.millis() * 4 + 10;
}
}
OCCASIONALLY cout << "writeLazily " << k << " sleep:" << sleep << '\n';
sleepmillis(sleep);
}
void RecCache::_ejectOld() {
scoped_lock lk(rcmutex);
if( nnodes <= MAXNODES )
return;
Node *n = oldest;
while( 1 ) {
if( nnodes <= MAXNODES - 4 ) {
n->older = 0;
oldest = n;
assert( oldest ) ;
break;
}
nnodes--;
assert(n);
Node *nxt = n->newer;
writeIfDirty(n);
m.erase(n->loc);
delete n;
n = nxt;
}
}
void RecCache::dump() {
Node *n = oldest;
Node *last = 0;
while( n ) {
assert( n->older == last );
last = n;
// cout << n << ' ' << n->older << ' ' << n->newer << '\n';
n=n->newer;
}
assert( newest == last );
// cout << endl;
}
/* cleans up everything EXCEPT storesByNsKey.
note this function is slow should not be invoked often
*/
void RecCache::closeStore(BasicRecStore *rs) {
int n = rs->fileNumber + Base;
for( set<DiskLoc>::iterator i = dirtyl.begin(); i != dirtyl.end(); ) {
DiskLoc k = *i++;
if( k.a() == n )
dirtyl.erase(k);
}
for( map<DiskLoc,Node*>::iterator i = m.begin(); i != m.end(); ) {
DiskLoc k = i->first;
i++;
if( k.a() == n )
m.erase(k);
}
assert( stores[rs->fileNumber] != 0 );
stores[rs->fileNumber] = 0;
/*
for( unsigned i = 0; i < stores.size(); i++ ) {
if( stores[i] == rs ) {
stores[i] = 0;
break;
}
}*/
delete rs; // closes file
}
void RecCache::drop(const char *_ns) {
// todo: test with a non clean shutdown file
scoped_lock lk(rcmutex);
map<string, BasicRecStore*>::iterator it = storesByNsKey.find(mknskey(_ns));
string fname;
if( it != storesByNsKey.end() ) {
fname = it->second->filename;
closeStore(it->second); // cleans up stores[] etc.
storesByNsKey.erase(it);
}
else {
bool found;
fname = findStoreFilename(_ns, found);
if( !found ) {
log() << "RecCache::drop: no idx file found for " << _ns << endl;
return;
}
path pf(directory());
pf /= fname;
fname = pf.string();
}
try {
if( !boost::filesystem::exists(fname) )
log() << "RecCache::drop: can't find file to remove " << fname << endl;
boost::filesystem::remove(fname);
}
catch(...) {
log() << "RecCache::drop: exception removing file " << fname << endl;
}
}
}
|