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

306 lines
9.9 KiB
C
Raw Normal View History

2009-03-24 19:19:03 +01:00
/* clientcursor.h */
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
* 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.
2008-12-29 02:28:49 +01:00
*
* 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.
2008-12-29 02:28:49 +01:00
*
* 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/>.
*/
2009-03-24 19:19:03 +01:00
/* Cursor -- and its derived classes -- are our internal cursors.
2008-06-06 20:59:58 +02:00
2008-12-29 02:28:49 +01:00
ClientCursor is a wrapper that represents a cursorid from our database
2008-06-06 20:59:58 +02:00
application's perspective.
*/
#pragma once
2010-04-27 21:27:52 +02:00
#include "../pch.h"
2009-05-11 16:44:27 +02:00
#include "cursor.h"
#include "jsobj.h"
#include "../util/message.h"
#include "../util/background.h"
2010-02-05 00:06:04 +01:00
#include "diskloc.h"
2009-05-11 16:44:27 +02:00
#include "dbhelpers.h"
2009-05-11 21:09:30 +02:00
#include "matcher.h"
#include "../client/dbclient.h"
2008-06-06 20:59:58 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
2009-03-24 19:19:03 +01:00
typedef long long CursorId; /* passed to the client so it can send back on getMore */
class Cursor; /* internal server cursor base class */
class ClientCursor;
class ParsedQuery;
2009-10-09 19:37:30 +02:00
/* todo: make this map be per connection. this will prevent cursor hijacking security attacks perhaps.
*/
typedef map<CursorId, ClientCursor*> CCById;
2009-10-09 19:37:30 +02:00
typedef multimap<DiskLoc, ClientCursor*> CCByLoc;
2008-06-06 20:59:58 +02:00
2009-05-11 16:44:27 +02:00
extern BSONObj id_obj;
class ClientCursor {
2009-10-07 21:34:14 +02:00
friend class CmdCursorInfo;
2009-10-09 17:24:37 +02:00
DiskLoc _lastLoc; // use getter and setter not this (important)
unsigned _idleAgeMillis; // how long has the cursor been around, relative to server idle time
/* 0 = normal
1 = no timeout allowed
100 = in use (pinned) -- see Pointer class
*/
unsigned _pinValue;
bool _doingDeletes;
2010-06-20 02:20:19 +02:00
int _yieldSometimesCalls;
2009-10-07 21:34:14 +02:00
static CCById clientCursorsById;
static CCByLoc byLoc;
2009-10-09 17:24:37 +02:00
static boost::recursive_mutex ccmutex; // must use this for all statics above!
2009-10-09 17:24:37 +02:00
static CursorId allocCursorId_inlock();
2010-06-20 02:20:19 +02:00
public:
/* use this to assure we don't in the background time out cursor while it is under use.
if you are using noTimeout() already, there is no risk anyway.
Further, this mechanism guards against two getMore requests on the same cursor executing
at the same time - which might be bad. That should never happen, but if a client driver
had a bug, it could (or perhaps some sort of attack situation).
*/
class Pointer : boost::noncopyable {
public:
ClientCursor *_c;
void release() {
if( _c ) {
assert( _c->_pinValue >= 100 );
_c->_pinValue -= 100;
}
_c = 0;
}
Pointer(long long cursorid) {
recursive_scoped_lock lock(ccmutex);
_c = ClientCursor::find_inlock(cursorid, true);
if( _c ) {
if( _c->_pinValue >= 100 ) {
_c = 0;
uassert(12051, "clientcursor already in use? driver problem?", false);
}
_c->_pinValue += 100;
}
}
~Pointer() {
release();
}
};
2009-10-09 17:24:37 +02:00
/*const*/ CursorId cursorid;
string ns;
shared_ptr<Cursor> c;
2010-06-06 21:46:55 +02:00
int pos; // # objects into the cursor so far
BSONObj query;
2010-06-06 21:46:55 +02:00
int _queryOptions; // see enum QueryOptions dbclient.h
OpTime _slaveReadTill;
2009-10-09 17:24:37 +02:00
ClientCursor(int queryOptions, shared_ptr<Cursor>& _c, const char *_ns) :
2010-01-20 23:11:25 +01:00
_idleAgeMillis(0), _pinValue(0),
2010-06-20 02:20:19 +02:00
_doingDeletes(false), _yieldSometimesCalls(0),
2010-01-20 23:11:25 +01:00
ns(_ns), c(_c),
pos(0), _queryOptions(queryOptions)
2010-01-20 23:11:25 +01:00
{
if( queryOptions & QueryOption_NoCursorTimeout )
noTimeout();
recursive_scoped_lock lock(ccmutex);
2009-10-09 17:24:37 +02:00
cursorid = allocCursorId_inlock();
clientCursorsById.insert( make_pair(cursorid, this) );
}
~ClientCursor();
DiskLoc lastLoc() const {
return _lastLoc;
}
2009-10-09 17:24:37 +02:00
shared_ptr< ParsedQuery > pq;
shared_ptr< FieldMatcher > fields; // which fields query wants returned
2009-03-27 21:27:35 +01:00
Message originalMessage; // this is effectively an auto ptr for data the matcher points to
2008-06-06 20:59:58 +02:00
/* Get rid of cursors for namespaces that begin with nsprefix.
2010-01-22 21:58:49 +01:00
Used by drop, dropIndexes, dropDatabase.
*/
static void invalidate(const char *nsPrefix);
2008-07-24 22:07:18 +02:00
/**
* do a dbtemprelease
* note: caller should check matcher.docMatcher().atomic() first and not yield if atomic -
* we don't do herein as this->matcher (above) is only initialized for true queries/getmore.
* (ie not set for remote/update)
* @return if the cursor is still valid.
* if false is returned, then this ClientCursor should be considered deleted -
* in fact, the whole database could be gone.
*/
bool yield();
2010-06-20 02:20:19 +02:00
/**
* @return same as yield()
*/
bool yieldSometimes();
2010-05-19 23:52:27 +02:00
struct YieldLock : boost::noncopyable {
2010-05-20 01:27:04 +02:00
explicit YieldLock( ptr<ClientCursor> cc )
: _cc( cc ) , _id( cc->cursorid ) , _doingDeletes( cc->_doingDeletes ) {
cc->updateLocation();
2010-05-19 23:52:27 +02:00
_unlock.reset(new dbtempreleasecond());
}
~YieldLock(){
assert( ! _unlock );
}
bool stillOk(){
relock();
if ( ClientCursor::find( _id , false ) == 0 ){
// i was deleted
return false;
}
_cc->_doingDeletes = _doingDeletes;
return true;
}
void relock(){
2010-05-19 23:52:27 +02:00
_unlock.reset();
}
2010-05-19 23:52:27 +02:00
private:
ClientCursor * _cc;
CursorId _id;
bool _doingDeletes;
2010-06-20 02:20:19 +02:00
2010-05-19 23:52:27 +02:00
scoped_ptr<dbtempreleasecond> _unlock;
};
// --- some pass through helpers for Cursor ---
BSONObj indexKeyPattern() {
return c->indexKeyPattern();
}
bool ok(){
return c->ok();
}
bool advance(){
return c->advance();
}
bool currentMatches(){
if ( ! c->matcher() )
return true;
return c->matcher()->matchesCurrent( c.get() );
}
BSONObj current(){
return c->current();
}
2009-10-09 17:24:37 +02:00
private:
2009-11-27 22:40:58 +01:00
void setLastLoc_inlock(DiskLoc);
2009-10-09 17:24:37 +02:00
static ClientCursor* find_inlock(CursorId id, bool warn = true) {
CCById::iterator it = clientCursorsById.find(id);
if ( it == clientCursorsById.end() ) {
if ( warn )
OCCASIONALLY out() << "ClientCursor::find(): cursor not found in map " << id << " (ok after a drop)\n";
return 0;
}
return it->second;
2008-12-29 02:28:49 +01:00
}
2009-10-09 17:24:37 +02:00
public:
static ClientCursor* find(CursorId id, bool warn = true) {
recursive_scoped_lock lock(ccmutex);
ClientCursor *c = find_inlock(id, warn);
// if this asserts, your code was not thread safe - you either need to set no timeout
// for the cursor or keep a ClientCursor::Pointer in scope for it.
massert( 12521, "internal error: use of an unlocked ClientCursor", c == 0 || c->_pinValue );
return c;
2009-10-09 17:24:37 +02:00
}
static bool erase(CursorId id) {
recursive_scoped_lock lock(ccmutex);
2009-10-09 17:24:37 +02:00
ClientCursor *cc = find_inlock(id);
if ( cc ) {
assert( cc->_pinValue < 100 ); // you can't still have an active ClientCursor::Pointer
2009-10-09 17:24:37 +02:00
delete cc;
return true;
}
return false;
}
2008-06-06 20:59:58 +02:00
/* call when cursor's location changes so that we can update the
cursorsbylocation map. if you are locked and internally iterating, only
need to call when you are ready to "unlock".
*/
void updateLocation();
2008-06-06 20:59:58 +02:00
2009-05-11 16:44:27 +02:00
void mayUpgradeStorage() {
2009-10-09 17:24:37 +02:00
/* if ( !ids_.get() )
return;
2009-05-11 16:44:27 +02:00
stringstream ss;
ss << ns << "." << cursorid;
2009-07-03 00:20:32 +02:00
ids_->mayUpgradeStorage( ss.str() );*/
2009-05-11 16:44:27 +02:00
}
2009-01-14 23:09:51 +01:00
/**
* @param millis amount of idle passed time since last call
*/
bool shouldTimeout( unsigned millis ){
_idleAgeMillis += millis;
return _idleAgeMillis > 600000 && _pinValue == 0;
}
void storeOpForSlave( DiskLoc last );
void updateSlaveLocation( CurOp& curop );
unsigned idleTime(){
return _idleAgeMillis;
}
static void idleTimeReport(unsigned millis);
private:
2010-01-03 22:37:38 +01:00
// cursors normally timeout after an inactivy period to prevent excess memory use
// setting this prevents timeout of the cursor in question.
void noTimeout() {
_pinValue++;
}
public:
void setDoingDeletes( bool doingDeletes ){
_doingDeletes = doingDeletes;
}
static unsigned byLocSize(); // just for diagnostics
2009-10-09 17:24:37 +02:00
static void informAboutToDeleteBucket(const DiskLoc& b);
static void aboutToDelete(const DiskLoc& dl);
};
class ClientCursorMonitor : public BackgroundJob {
public:
void run();
2010-05-18 18:17:43 +02:00
string name() { return "ClientCursorMonitor"; }
};
extern ClientCursorMonitor clientCursorMonitor;
2009-01-14 23:09:51 +01:00
} // namespace mongo