2009-03-26 18:45:29 +01:00
|
|
|
// lasterror.cpp
|
|
|
|
|
2009-10-27 20:58:27 +01:00
|
|
|
/* Copyright 2009 10gen Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2009-03-26 18:45:29 +01:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2009-09-11 17:56:57 +02:00
|
|
|
#include "../util/unittest.h"
|
2009-09-11 18:52:17 +02:00
|
|
|
#include "../util/message.h"
|
|
|
|
|
2009-03-26 18:45:29 +01:00
|
|
|
|
2009-09-11 17:56:57 +02:00
|
|
|
#include "lasterror.h"
|
2009-03-26 18:45:29 +01:00
|
|
|
#include "jsobj.h"
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
|
|
LastError LastError::noError;
|
2009-09-11 17:56:57 +02:00
|
|
|
LastErrorHolder lastError;
|
2010-03-15 17:42:01 +01:00
|
|
|
mongo::mutex LastErrorHolder::_idsmutex;
|
2009-10-12 21:12:16 +02:00
|
|
|
|
2009-03-26 18:45:29 +01:00
|
|
|
void LastError::appendSelf( BSONObjBuilder &b ) {
|
|
|
|
if ( !valid ) {
|
|
|
|
b.appendNull( "err" );
|
|
|
|
b.append( "n", 0 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( msg.empty() )
|
|
|
|
b.appendNull( "err" );
|
|
|
|
else
|
|
|
|
b.append( "err", msg );
|
2009-12-28 23:12:49 +01:00
|
|
|
if ( code )
|
|
|
|
b.append( "code" , code );
|
2009-03-26 18:45:29 +01:00
|
|
|
if ( updatedExisting != NotUpdate )
|
|
|
|
b.appendBool( "updatedExisting", updatedExisting == True );
|
|
|
|
b.append( "n", nObjects );
|
|
|
|
}
|
2009-09-11 17:56:57 +02:00
|
|
|
|
|
|
|
void LastErrorHolder::setID( int id ){
|
2009-10-13 18:55:23 +02:00
|
|
|
_id.set( id );
|
2009-09-11 17:56:57 +02:00
|
|
|
}
|
|
|
|
|
2009-09-11 18:52:17 +02:00
|
|
|
int LastErrorHolder::getID(){
|
|
|
|
return _id.get();
|
|
|
|
}
|
|
|
|
|
2010-01-13 00:33:29 +01:00
|
|
|
LastError * LastErrorHolder::disableForCommand() {
|
2010-01-13 01:56:29 +01:00
|
|
|
LastError *le = _get();
|
2010-01-13 00:33:29 +01:00
|
|
|
assert( le );
|
2010-01-13 01:56:29 +01:00
|
|
|
le->disabled = true;
|
2010-01-13 00:33:29 +01:00
|
|
|
le->nPrev--; // caller is a command that shouldn't count as an operation
|
|
|
|
return le;
|
|
|
|
}
|
|
|
|
|
2010-01-13 01:56:29 +01:00
|
|
|
LastError * LastErrorHolder::get( bool create ) {
|
|
|
|
LastError *ret = _get( create );
|
|
|
|
if ( ret && !ret->disabled )
|
|
|
|
return ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LastError * LastErrorHolder::_get( bool create ){
|
2009-09-11 17:56:57 +02:00
|
|
|
int id = _id.get();
|
|
|
|
if ( id == 0 )
|
|
|
|
return _tl.get();
|
2010-01-15 21:57:57 +01:00
|
|
|
|
2010-03-15 17:42:01 +01:00
|
|
|
scoped_lock lock(_idsmutex);
|
2009-10-12 21:12:16 +02:00
|
|
|
map<int,Status>::iterator i = _ids.find( id );
|
2009-09-11 18:52:17 +02:00
|
|
|
if ( i == _ids.end() ){
|
|
|
|
if ( ! create )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LastError * le = new LastError();
|
2009-10-12 21:12:16 +02:00
|
|
|
Status s;
|
|
|
|
s.time = time(0);
|
|
|
|
s.lerr = le;
|
|
|
|
_ids[id] = s;
|
2009-09-11 18:52:17 +02:00
|
|
|
return le;
|
|
|
|
}
|
2009-09-11 17:56:57 +02:00
|
|
|
|
2009-10-12 21:12:16 +02:00
|
|
|
Status &status = i->second;
|
|
|
|
status.time = time(0);
|
|
|
|
return status.lerr;
|
2009-09-11 17:56:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LastErrorHolder::remove( int id ){
|
2010-03-15 17:42:01 +01:00
|
|
|
scoped_lock lock(_idsmutex);
|
2009-10-12 21:12:16 +02:00
|
|
|
map<int,Status>::iterator i = _ids.find( id );
|
2009-09-11 17:56:57 +02:00
|
|
|
if ( i == _ids.end() )
|
|
|
|
return;
|
|
|
|
|
2009-10-12 21:12:16 +02:00
|
|
|
delete i->second.lerr;
|
2009-09-11 17:56:57 +02:00
|
|
|
_ids.erase( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
void LastErrorHolder::release(){
|
|
|
|
int id = _id.get();
|
|
|
|
if ( id == 0 ){
|
|
|
|
_tl.release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remove( id );
|
|
|
|
}
|
2009-03-26 18:45:29 +01:00
|
|
|
|
2009-09-11 17:56:57 +02:00
|
|
|
void LastErrorHolder::reset( LastError * le ){
|
|
|
|
int id = _id.get();
|
|
|
|
if ( id == 0 ){
|
|
|
|
_tl.reset( le );
|
|
|
|
return;
|
|
|
|
}
|
2010-01-15 21:57:57 +01:00
|
|
|
|
2010-03-15 17:42:01 +01:00
|
|
|
scoped_lock lock(_idsmutex);
|
2009-10-12 21:12:16 +02:00
|
|
|
Status & status = _ids[id];
|
|
|
|
status.time = time(0);
|
|
|
|
status.lerr = le;
|
2009-09-11 17:56:57 +02:00
|
|
|
}
|
2010-01-13 01:56:29 +01:00
|
|
|
|
|
|
|
void prepareErrForNewRequest( Message &m, LastError * err ) {
|
|
|
|
// a killCursors message shouldn't affect last error
|
|
|
|
if ( m.data->operation() == dbKillCursors ) {
|
|
|
|
err->disabled = true;
|
|
|
|
} else {
|
|
|
|
err->disabled = false;
|
|
|
|
err->nPrev++;
|
|
|
|
}
|
|
|
|
}
|
2009-09-11 18:52:17 +02:00
|
|
|
|
2009-10-12 21:12:16 +02:00
|
|
|
void LastErrorHolder::startRequest( Message& m ) {
|
2009-09-11 18:52:17 +02:00
|
|
|
int id = m.data->id & 0xFFFF0000;
|
|
|
|
setID( id );
|
2010-01-13 01:56:29 +01:00
|
|
|
LastError * le = _get( true );
|
|
|
|
prepareErrForNewRequest( m, le );
|
2009-09-11 18:52:17 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 21:12:16 +02:00
|
|
|
void LastErrorHolder::startRequest( Message& m , LastError * connectionOwned ) {
|
|
|
|
if ( !connectionOwned->overridenById ) {
|
2010-01-13 01:56:29 +01:00
|
|
|
prepareErrForNewRequest( m, connectionOwned );
|
2009-10-12 21:12:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
startRequest(m);
|
|
|
|
}
|
2009-09-11 17:56:57 +02:00
|
|
|
|
|
|
|
struct LastErrorHolderTest : public UnitTest {
|
|
|
|
public:
|
|
|
|
|
|
|
|
void test( int i ){
|
2009-10-13 18:55:23 +02:00
|
|
|
_tl.set( i );
|
2009-09-11 17:56:57 +02:00
|
|
|
assert( _tl.get() == i );
|
|
|
|
}
|
|
|
|
|
|
|
|
void tlmaptest(){
|
|
|
|
test( 1 );
|
|
|
|
test( 12123123 );
|
|
|
|
test( -123123 );
|
|
|
|
test( numeric_limits<int>::min() );
|
|
|
|
test( numeric_limits<int>::max() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(){
|
|
|
|
tlmaptest();
|
|
|
|
|
|
|
|
LastError * a = new LastError();
|
|
|
|
LastError * b = new LastError();
|
|
|
|
|
|
|
|
LastErrorHolder holder;
|
|
|
|
holder.reset( a );
|
|
|
|
assert( a == holder.get() );
|
|
|
|
holder.setID( 1 );
|
|
|
|
assert( 0 == holder.get() );
|
|
|
|
holder.reset( b );
|
|
|
|
assert( b == holder.get() );
|
|
|
|
holder.setID( 0 );
|
|
|
|
assert( a == holder.get() );
|
|
|
|
|
|
|
|
holder.remove( 1 );
|
|
|
|
}
|
|
|
|
|
2009-10-13 18:55:23 +02:00
|
|
|
ThreadLocalValue<int> _tl;
|
2009-09-11 17:56:57 +02:00
|
|
|
} lastErrorHolderTest;
|
|
|
|
|
2009-03-26 18:45:29 +01:00
|
|
|
} // namespace mongo
|