summaryrefslogtreecommitdiff
path: root/src/mongo/db/lasterror.h
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@mongodb.com>2015-04-23 17:50:55 -0400
committerAndy Schwerin <schwerin@mongodb.com>2015-04-23 18:02:53 -0400
commitab8295bf7cc572a391e8c364b5291f1d9b3b0720 (patch)
tree440f00fab6ef56747c46bdc995fc41a07aa45845 /src/mongo/db/lasterror.h
parent1f24d43e419b674bfe866bdebf8224d44fa163b0 (diff)
downloadmongo-ab8295bf7cc572a391e8c364b5291f1d9b3b0720.tar.gz
Revert "SERVER-18131 Clean up LastError."
This reverts commit 54c25da33ec7270295c8948f6a51376ec4fd278c and commit c952a9396a1843aa45d6afa3c6785dec607de112.
Diffstat (limited to 'src/mongo/db/lasterror.h')
-rw-r--r--src/mongo/db/lasterror.h150
1 files changed, 90 insertions, 60 deletions
diff --git a/src/mongo/db/lasterror.h b/src/mongo/db/lasterror.h
index 55a04c8334d..5b04b69a57a 100644
--- a/src/mongo/db/lasterror.h
+++ b/src/mongo/db/lasterror.h
@@ -29,13 +29,16 @@
#pragma once
+#include <boost/noncopyable.hpp>
+#include <boost/thread/tss.hpp>
#include <string>
-#include "mongo/db/client.h"
#include "mongo/db/jsobj.h"
+#include "mongo/bson/oid.h"
namespace mongo {
class BSONObjBuilder;
+ class Message;
static const char kUpsertedFieldName[] = "upserted";
static const char kGLEStatsFieldName[] = "$gleStats";
@@ -43,80 +46,107 @@ namespace mongo {
static const char kGLEStatsLastOpTimeTermFieldName[] = "lastOpTimeTerm";
static const char kGLEStatsElectionIdFieldName[] = "electionId";
- class LastError {
- public:
- static const Client::Decoration<LastError> get;
+ struct LastError {
+ int code;
+ std::string msg;
+ enum UpdatedExistingType { NotUpdate, True, False } updatedExisting;
+ // _id field value from inserted doc, returned as kUpsertedFieldName (above)
+ BSONObj upsertedId;
+ long long nObjects;
+ int nPrev;
+ bool valid;
+ bool disabled;
+ void raiseError(int _code , const char *_msg) {
+ reset( true );
+ code = _code;
+ msg = _msg;
+ }
+ void recordUpdate( bool _updateObjects , long long _nObjects , BSONObj _upsertedId ) {
+ reset( true );
+ nObjects = _nObjects;
+ updatedExisting = _updateObjects ? True : False;
+ if ( _upsertedId.valid() && _upsertedId.hasField(kUpsertedFieldName) )
+ upsertedId = _upsertedId;
+
+ }
+ void recordDelete( long long nDeleted ) {
+ reset( true );
+ nObjects = nDeleted;
+ }
+ LastError() {
+ reset();
+ }
+ void reset( bool _valid = false ) {
+ code = 0;
+ msg.clear();
+ updatedExisting = NotUpdate;
+ nObjects = 0;
+ nPrev = 1;
+ valid = _valid;
+ disabled = false;
+ upsertedId = BSONObj();
+ }
/**
- * Resets the object to a newly constructed state. If "valid" is true, marks the last-error
- * object as "valid".
+ * @return if there is an err
*/
- void reset(bool valid = false);
+ bool appendSelf( BSONObjBuilder &b , bool blankErr = true );
+
+ struct Disabled : boost::noncopyable {
+ Disabled( LastError * le ) {
+ _le = le;
+ if ( _le ) {
+ _prev = _le->disabled;
+ _le->disabled = true;
+ }
+ else {
+ _prev = false;
+ }
+ }
- /**
- * when db receives a message/request, call this
- */
- void startRequest();
+ ~Disabled() {
+ if ( _le )
+ _le->disabled = _prev;
+ }
- /**
- * Disables error recording for the current operation.
- */
- void disable();
+ LastError * _le;
+ bool _prev;
+ };
- /**
- * Sets the error information for the current operation, if error recording was not
- * explicitly disabled via a call to disable() since the call to startRequest.
- */
- void setLastError(int code, std::string msg);
+ static LastError noError;
+ };
- void recordInsert(long long nObjects);
+ extern class LastErrorHolder {
+ public:
+ LastErrorHolder(){}
+ ~LastErrorHolder();
- void recordUpdate(bool updateObjects, long long nObjects, BSONObj upsertedId);
+ LastError * get( bool create = false );
+ LastError * getSafe();
+ LastError * _get( bool create = false ); // may return a disabled LastError
- void recordDelete(long long nDeleted);
+ void reset( LastError * le );
- /**
- * Writes the last-error state described by this object to "b".
- *
- * If "blankErr" is true, the "err" field will be explicitly set to null in the result
- * instead of being omitted when the error string is empty.
- *
- * Returns true if there is a non-empty error message.
- */
- bool appendSelf(BSONObjBuilder &b, bool blankErr) const;
+ /** ok to call more than once. */
+ void initThread();
- bool isValid() const { return _valid; }
- int const getNPrev() const { return _nPrev; }
+ void release();
- class Disabled {
- public:
- explicit Disabled(LastError* le) : _le(le), _prev(le->_disabled) {
- _le->_disabled = true;
- }
+ /** when db receives a message/request, call this */
+ LastError * startRequest( Message& m , LastError * connectionOwned );
- ~Disabled() {
- _le->_disabled = _prev;
- }
+ // used to disable lastError reporting while processing a killCursors message
+ // disable causes get() to return 0.
+ LastError *disableForCommand(); // only call once per command invocation!
+ private:
+ boost::thread_specific_ptr<LastError> _tl;
- private:
- LastError * const _le;
- const bool _prev;
+ struct Status {
+ time_t time;
+ LastError *lerr;
};
+ } lastError;
- static LastError noError;
-
- private:
- enum UpdatedExistingType { NotUpdate, True, False };
-
- int _code = 0;
- std::string _msg = {};
- UpdatedExistingType _updatedExisting = NotUpdate;
- // _id field value from inserted doc, returned as kUpsertedFieldName (above)
- BSONObj _upsertedId = {};
- long long _nObjects = 0;
- int _nPrev = 1;
- bool _valid = false;
- bool _disabled = false;
- };
+ void setLastError(int code , const char *msg);
} // namespace mongo