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

137 lines
4.2 KiB
C
Raw Normal View History

// curop.h
#pragma once
#include "namespace.h"
#include "security.h"
#include "client.h"
namespace mongo {
2009-12-21 19:19:20 +01:00
/* Current operation (for the current Client).
an embedded member of Client class, and typically used from within the mutex there. */
class CurOp {
2009-12-22 21:22:37 +01:00
static WrappingInt _nextOpNum;
2009-12-21 19:19:20 +01:00
static BSONObj _tooBig; // { $msg : "query not recording (too large)" }
bool _active;
time_t startTime;
int _op;
2009-12-22 21:22:37 +01:00
WrappingInt _opNum;
2009-12-21 19:19:20 +01:00
char _ns[Namespace::MaxNsLen+2];
struct sockaddr_in client;
char _queryBuf[256];
bool haveQuery() const { return *((int *) _queryBuf) != 0; }
void resetQuery(int x=0) { *((int *)_queryBuf) = x; }
BSONObj query() {
if( *((int *) _queryBuf) == 1 ) {
return _tooBig;
}
BSONObj o(_queryBuf);
return o;
}
public:
void reset(time_t now, const sockaddr_in &_client) {
2009-12-21 19:19:20 +01:00
_active = true;
2009-12-22 21:22:37 +01:00
_opNum = _nextOpNum.atomicIncrement();
startTime = now;
2009-12-21 19:19:20 +01:00
_ns[0] = '?'; // just in case not set later
resetQuery();
client = _client;
}
2009-12-22 21:22:37 +01:00
WrappingInt opNum() const { return _opNum; }
2009-12-21 19:19:20 +01:00
bool active() const { return _active; }
void setActive(bool active) { _active = active; }
void setNS(const char *ns) {
strncpy(_ns, ns, Namespace::MaxNsLen);
}
void setOp(int op) { _op = op; }
void setQuery(const BSONObj& query) {
if( query.objsize() > (int) sizeof(_queryBuf) ) {
resetQuery(1); // flag as too big and return
return;
}
memcpy(_queryBuf, query.objdata(), query.objsize());
}
CurOp() {
2009-12-21 19:19:20 +01:00
_active = false;
// opNum = 0;
2009-12-07 21:42:26 +01:00
startTime = 0;
2009-12-21 19:19:20 +01:00
_op = 0;
// These addresses should never be written to again. The zeroes are
// placed here as a precaution because currentOp may be accessed
// without the db mutex.
2009-12-21 19:19:20 +01:00
memset(_ns, 0, sizeof(_ns));
memset(_queryBuf, 0, sizeof(_queryBuf));
}
BSONObj info() {
AuthenticationInfo *ai = currentClient.get()->ai;
if( !ai->isAuthorized("admin") ) {
BSONObjBuilder b;
b.append("err", "unauthorized");
return b.obj();
}
return infoNoauth();
}
BSONObj infoNoauth() {
BSONObjBuilder b;
2009-12-22 21:22:37 +01:00
b.append("opid", _opNum);
2009-12-21 19:19:20 +01:00
b.append("active", _active);
if( _active )
b.append("secs_running", (int) (time(0)-startTime));
2009-12-21 19:19:20 +01:00
if( _op == 2004 )
b.append("op", "query");
2009-12-21 19:19:20 +01:00
else if( _op == 2005 )
b.append("op", "getMore");
2009-12-21 19:19:20 +01:00
else if( _op == 2001 )
b.append("op", "update");
2009-12-21 19:19:20 +01:00
else if( _op == 2002 )
b.append("op", "insert");
2009-12-21 19:19:20 +01:00
else if( _op == 2006 )
b.append("op", "delete");
else
2009-12-21 19:19:20 +01:00
b.append("op", _op);
b.append("ns", _ns);
if( haveQuery() ) {
b.append("query", query());
}
2009-12-07 21:42:26 +01:00
// b.append("inLock", ??
stringstream clientStr;
clientStr << inet_ntoa( client.sin_addr ) << ":" << ntohs( client.sin_port );
b.append("client", clientStr.str());
return b.obj();
}
};
2009-12-22 21:22:37 +01:00
/* 0 = ok
1 = kill current operation and reset this to 0
future: maybe use this as a "going away" thing on process termination with a higher flag value
*/
extern class KillCurrentOp {
enum { Off, On, All } state;
WrappingInt toKill;
public:
void killAll() { state = All; }
void kill(WrappingInt i) { toKill = i; state = On; }
void checkForInterrupt() {
if( state != Off ) {
if( state == All )
uasserted("interrupted at shutdown");
if( cc().curop()->opNum() == toKill ) {
state = Off;
uasserted("interrupted");
}
}
}
} killCurrentOp;
}