Exemplo n.º 1
0
    virtual bool run(OperationContext* txn,
                     const string&,
                     BSONObj& cmdObj,
                     int,
                     string& errmsg,
                     BSONObjBuilder& result) {
        string fromhost = cmdObj.getStringField("fromhost");
        if (fromhost.empty()) {
            /* copy from self */
            stringstream ss;
            ss << "localhost:" << serverGlobalParams.port;
            fromhost = ss.str();
        }

        const ConnectionString cs(uassertStatusOK(ConnectionString::parse(fromhost)));

        auto& authConn = CopyDbAuthConnection::forClient(txn->getClient());
        authConn.reset(cs.connect(errmsg));
        if (!authConn) {
            return false;
        }

        BSONObj ret;

        if (!authConn->runCommand("admin", BSON("getnonce" << 1), ret)) {
            errmsg = "couldn't get nonce " + ret.toString();
            authConn.reset();
            return false;
        }

        result.appendElements(ret);
        return true;
    }
Exemplo n.º 2
0
    BSONObj Sync::getMissingDoc(const BSONObj& o) {
        OplogReader missingObjReader;
        const char *ns = o.getStringField("ns");

        // capped collections
        NamespaceDetails *nsd = nsdetails(ns);
        if ( nsd && nsd->isCapped() ) {
            log() << "replication missing doc, but this is okay for a capped collection (" << ns << ")" << endl;
            return BSONObj();
        }

        uassert(15916, str::stream() << "Can no longer connect to initial sync source: " << hn, missingObjReader.connect(hn));

        // might be more than just _id in the update criteria
        BSONObj query = BSONObjBuilder().append(o.getObjectField("o2")["_id"]).obj();
        BSONObj missingObj;
        try {
            missingObj = missingObjReader.findOne(ns, query);
        } catch(DBException& e) {
            log() << "replication assertion fetching missing object: " << e.what() << endl;
            throw;
        }

        return missingObj;
    }
Exemplo n.º 3
0
void CMISProductNotificationAPI::Convert2JSON(CNotificationModel* pData, BSONObj &boRecord)
{
	//{"data":[{"request_code":"RP130314/004","operation_department":"BO6"}],"source":"SDK"}
	BSONArrayBuilder babElement;
	BSONObjBuilder bobProductInfo;
	map<string, string>::iterator mit;
	map<string, string> mapAPIField;
	mapAPIField["department_alias"] = "operation_department";
	mapAPIField["request_code"] = "request_code";

	BSONObj boTemp = *pData;
	for (mit = mapAPIField.begin(); mit != mapAPIField.end(); mit++)
	{
		if (boTemp.hasField(mit->first)){
			bobProductInfo.append(mit->second, boTemp.getStringField(mit->first.c_str()));
		}
		else{
			bobProductInfo.append(mit->second, "");
		}
	}

	babElement << bobProductInfo.obj();
	boRecord = BSON(
		"data" << babElement.arr() <<
		"source" << "SDK"
		);
}
Exemplo n.º 4
0
    bool CmdAuthenticate::getUserObj(const string& dbname, const string& user, BSONObj& userObj, string& pwd) {
        if (user == internalSecurity.user) {
            pwd = internalSecurity.pwd;
        }
        else {
            string systemUsers = dbname + ".system.users";
            DBConfigPtr config = grid.getDBConfig( systemUsers );
            Shard s = config->getShard( systemUsers );

            static BSONObj userPattern = BSON("user" << 1);

            ShardConnection conn( s, systemUsers );
            OCCASIONALLY conn->ensureIndex(systemUsers, userPattern, false, "user_1");
            {
                BSONObjBuilder b;
                b << "user" << user;
                BSONObj query = b.done();
                userObj = conn->findOne(systemUsers, query);
                if( userObj.isEmpty() ) {
                    log() << "auth: couldn't find user " << user << ", " << systemUsers << endl;
                    return false;
                }
            }

            pwd = userObj.getStringField("pwd");
        }
        return true;
    }
Exemplo n.º 5
0
BSONObj SyncTail::getMissingDoc(OperationContext* txn, Database* db, const BSONObj& o) {
    OplogReader missingObjReader;  // why are we using OplogReader to run a non-oplog query?
    const char* ns = o.getStringField("ns");

    // capped collections
    Collection* collection = db->getCollection(ns);
    if (collection && collection->isCapped()) {
        log() << "missing doc, but this is okay for a capped collection (" << ns << ")";
        return BSONObj();
    }

    const int retryMax = 3;
    for (int retryCount = 1; retryCount <= retryMax; ++retryCount) {
        if (retryCount != 1) {
            // if we are retrying, sleep a bit to let the network possibly recover
            sleepsecs(retryCount * retryCount);
        }
        try {
            bool ok = missingObjReader.connect(HostAndPort(_hostname));
            if (!ok) {
                warning() << "network problem detected while connecting to the "
                          << "sync source, attempt " << retryCount << " of " << retryMax << endl;
                continue;  // try again
            }
        } catch (const SocketException&) {
            warning() << "network problem detected while connecting to the "
                      << "sync source, attempt " << retryCount << " of " << retryMax << endl;
            continue;  // try again
        }

        // get _id from oplog entry to create query to fetch document.
        const BSONElement opElem = o.getField("op");
        const bool isUpdate = !opElem.eoo() && opElem.str() == "u";
        const BSONElement idElem = o.getObjectField(isUpdate ? "o2" : "o")["_id"];

        if (idElem.eoo()) {
            severe() << "cannot fetch missing document without _id field: " << o.toString();
            fassertFailedNoTrace(28742);
        }

        BSONObj query = BSONObjBuilder().append(idElem).obj();
        BSONObj missingObj;
        try {
            missingObj = missingObjReader.findOne(ns, query);
        } catch (const SocketException&) {
            warning() << "network problem detected while fetching a missing document from the "
                      << "sync source, attempt " << retryCount << " of " << retryMax << endl;
            continue;  // try again
        } catch (DBException& e) {
            error() << "assertion fetching missing object: " << e.what() << endl;
            throw;
        }

        // success!
        return missingObj;
    }
    // retry count exceeded
    msgasserted(15916,
                str::stream() << "Can no longer connect to initial sync source: " << _hostname);
}
Exemplo n.º 6
0
    bool Sync::shouldRetry(const BSONObj& o) {
        // should already have write lock
        const char *ns = o.getStringField("ns");
        Client::Context ctx(ns);

        // we don't have the object yet, which is possible on initial sync.  get it.
        log() << "replication info adding missing object" << endl; // rare enough we can log

        BSONObj missingObj = getMissingDoc(o);

        if( missingObj.isEmpty() ) {
            log() << "replication missing object not found on source. presumably deleted later in oplog" << endl;
            log() << "replication o2: " << o.getObjectField("o2").toString() << endl;
            log() << "replication o firstfield: " << o.getObjectField("o").firstElementFieldName() << endl;

            return false;
        }
        else {
            Collection* collection = ctx.db()->getOrCreateCollection( ns );
            verify( collection ); // should never happen
            StatusWith<DiskLoc> result = collection->insertDocument( missingObj, true );
            uassert(15917,
                    str::stream() << "failed to insert missing doc: " << result.toString(),
                    result.isOK() );

            LOG(1) << "replication inserted missing doc: " << missingObj.toString() << endl;
            return true;
        }
    }
void CUpdateAlertSttProcessor::UpdateAlertStatus(AlertInfo sAlertInf)
{
	string strCurrStatus, strTicketId, strItsmId, strLog;
	int iItsmSttNoti;
	BSONObj boRecord;
	auto_ptr<DBClientCursor> ptrResultCursor = auto_ptr<DBClientCursor>();

	m_pAlertModel->Load(sAlertInf);
	if (m_pAlertController->Find(ptrResultCursor, m_pAlertModel->GetObjectIdQuery())){
		if (ptrResultCursor->more())
		{
			boRecord = ptrResultCursor->nextSafe();
			m_pAlertModel->Load(boRecord);
			// ============================Outage-End is empty==========================
			if (m_pAlertController->UpdateINCStatus(m_pAlertModel->GetRecordBson())) // API Update Status from Inc
			{
				m_pAlertController->Update(m_pAlertModel->GetAlertStatus(), m_pAlertModel->GetObjectIdQuery());
				strCurrStatus = boRecord.hasField("itsm_status") ? boRecord.getStringField("itsm_status") : "";
				strLog = CUtilities::FormatLog(INFO_MSG, "UpdateAlertStt", "ProcessCSUpdate_Call_API", sAlertInf.strSourceId + ": " +
					strCurrStatus + " -> " + sAlertInf.strStatus);
				CUtilities::WriteInfoLog(INFO_MSG, strLog);
			}
		}
	}
}
Exemplo n.º 8
0
void printIfAge(DBClientConnection& c, int age) {
    auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );
    while( cursor->more() ) {
        BSONObj p = cursor->next();
        cout << p.getStringField("name") << endl;
    }
}
Exemplo n.º 9
0
 void DBConfig::unserialize(const BSONObj& from){
     _name = from.getStringField("name");
     _partitioned = from.getBoolField("partitioned");
     _primary = from.getStringField("primary");
     
     _sharded.clear();
     BSONObj sharded = from.getObjectField( "sharded" );
     if ( ! sharded.isEmpty() ){
         BSONObjIterator i(sharded);
         while ( i.more() ){
             BSONElement e = i.next();
             uassert( "sharded things have to be objects" , e.type() == Object );
             _sharded[e.fieldName()] = e.embeddedObject();
         }
     }
 }
Exemplo n.º 10
0
    virtual bool run(OperationContext* txn,
                     const string&,
                     BSONObj& cmdObj,
                     int,
                     string& errmsg,
                     BSONObjBuilder& result,
                     bool fromRepl) {
        string fromhost = cmdObj.getStringField("fromhost");
        if (fromhost.empty()) {
            /* copy from self */
            stringstream ss;
            ss << "localhost:" << serverGlobalParams.port;
            fromhost = ss.str();
        }

        BSONObj ret;

        ConnectionString cs = ConnectionString::parse(fromhost, errmsg);
        if (!cs.isValid()) {
            return false;
        }

        authConn_.reset(cs.connect(errmsg));
        if (!authConn_.get()) {
            return false;
        }

        if (!authConn_->runCommand("admin", BSON("getnonce" << 1), ret)) {
            errmsg = "couldn't get nonce " + ret.toString();
            return false;
        }

        result.appendElements(ret);
        return true;
    }
Exemplo n.º 11
0
    bool Sync::shouldRetry(const BSONObj& o) {
        // should already have write lock
        const char *ns = o.getStringField("ns");
        Client::Context ctx(ns);

        // we don't have the object yet, which is possible on initial sync.  get it.
        log() << "replication info adding missing object" << endl; // rare enough we can log

        BSONObj missingObj = getMissingDoc(o);

        if( missingObj.isEmpty() ) {
            log() << "replication missing object not found on source. presumably deleted later in oplog" << endl;
            log() << "replication o2: " << o.getObjectField("o2").toString() << endl;
            log() << "replication o firstfield: " << o.getObjectField("o").firstElementFieldName() << endl;

            return false;
        }
        else {
            DiskLoc d = theDataFileMgr.insert(ns, (void*) missingObj.objdata(), missingObj.objsize());
            uassert(15917, "Got bad disk location when attempting to insert", !d.isNull());

            LOG(1) << "replication inserted missing doc: " << missingObj.toString() << endl;
            return true;
        }
    }
Exemplo n.º 12
0
    virtual bool run(OperationContext* txn,
                     const string&,
                     BSONObj& cmdObj,
                     int,
                     string& errmsg,
                     BSONObjBuilder& result) {
        const string fromDb = cmdObj.getStringField("fromdb");

        string fromHost = cmdObj.getStringField("fromhost");
        if (fromHost.empty()) {
            /* copy from self */
            stringstream ss;
            ss << "localhost:" << serverGlobalParams.port;
            fromHost = ss.str();
        }

        const ConnectionString cs(uassertStatusOK(ConnectionString::parse(fromHost)));

        BSONElement mechanismElement;
        Status status = bsonExtractField(cmdObj, saslCommandMechanismFieldName, &mechanismElement);
        if (!status.isOK()) {
            return appendCommandStatus(result, status);
        }

        BSONElement payloadElement;
        status = bsonExtractField(cmdObj, saslCommandPayloadFieldName, &payloadElement);
        if (!status.isOK()) {
            log() << "Failed to extract payload: " << status;
            return false;
        }

        auto& authConn = CopyDbAuthConnection::forClient(txn->getClient());
        authConn.reset(cs.connect(StringData(), errmsg));
        if (!authConn.get()) {
            return false;
        }

        BSONObj ret;
        if (!authConn->runCommand(
                fromDb, BSON("saslStart" << 1 << mechanismElement << payloadElement), ret)) {
            authConn.reset();
            return appendCommandStatus(result, getStatusFromCommandResult(ret));
        }

        result.appendElements(ret);
        return true;
    }
Exemplo n.º 13
0
    bool run(OperationContext* opCtx,
             const std::string& ns,
             const BSONObj& cmdObj,
             BSONObjBuilder& result) {
        log() << "test only command sleep invoked";
        long long millis = 0;

        if (cmdObj["secs"] || cmdObj["seconds"] || cmdObj["millis"]) {
            uassert(51153,
                    "Only one of 'secs' and 'seconds' may be specified",
                    !(cmdObj["secs"] && cmdObj["seconds"]));

            if (auto secsElem = cmdObj["secs"]) {
                uassert(34344, "'secs' must be a number.", secsElem.isNumber());
                millis += secsElem.numberLong() * 1000;
            } else if (auto secondsElem = cmdObj["seconds"]) {
                uassert(51154, "'seconds' must be a number.", secondsElem.isNumber());
                millis += secondsElem.numberLong() * 1000;
            }

            if (auto millisElem = cmdObj["millis"]) {
                uassert(34345, "'millis' must be a number.", millisElem.isNumber());
                millis += millisElem.numberLong();
            }
        } else {
            millis = 10 * 1000;
        }

        StringData lockTarget;
        if (cmdObj["lockTarget"]) {
            lockTarget = cmdObj["lockTarget"].checkAndGetStringData();
        }

        if (!cmdObj["lock"]) {
            // Legacy implementation
            if (cmdObj.getBoolField("w")) {
                _sleepInLock(opCtx, millis, MODE_X, lockTarget);
            } else {
                _sleepInLock(opCtx, millis, MODE_S, lockTarget);
            }
        } else {
            uassert(34346, "Only one of 'w' and 'lock' may be set.", !cmdObj["w"]);

            std::string lock(cmdObj.getStringField("lock"));
            if (lock == "none") {
                opCtx->sleepFor(Milliseconds(millis));
            } else if (lock == "w") {
                _sleepInLock(opCtx, millis, MODE_X, lockTarget);
            } else {
                uassert(34347, "'lock' must be one of 'r', 'w', 'none'.", lock == "r");
                _sleepInLock(opCtx, millis, MODE_S, lockTarget);
            }
        }

        // Interrupt point for testing (e.g. maxTimeMS).
        opCtx->checkForInterrupt();

        return true;
    }
Exemplo n.º 14
0
// static
std::string WorkingSetCommon::toStatusString(const BSONObj& obj) {
    if (!isValidStatusMemberObject(obj)) {
        Status unknownStatus(ErrorCodes::UnknownError, "no details available");
        return unknownStatus.toString();
    }
    Status status(ErrorCodes::fromInt(obj.getIntField("code")), obj.getStringField("errmsg"));
    return status.toString();
}
Exemplo n.º 15
0
        bool run(const string& dbname, BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl ) {
            string from = jsobj.getStringField( "cloneCollectionAsCapped" );
            string to = jsobj.getStringField( "toCollection" );
            double size = jsobj.getField( "size" ).number();
            bool temp = jsobj.getField( "temp" ).trueValue();

            if ( from.empty() || to.empty() || size == 0 ) {
                errmsg = "invalid command spec";
                return false;
            }

            Lock::DBWrite dbXLock(dbname);
            Client::Context ctx(dbname);

            Status status = cloneCollectionAsCapped( ctx.db(), from, to, size, temp, true );
            return appendCommandStatus( result, status );
        }
BSONObj CCentralizeSO6AlertProcessor::GetAdditionInfo(BSONObj boRecord)
{
	auto_ptr<DBClientCursor> ptrMapProductCursor;
	BSONObj boAddtionInfo, boMapProductRecord;
	string strProduct, strContent, strDescription, strTmp, strMsgAlert;
	vector<string> vInformation;

	/*-- Get Map Product Info --*/
	strProduct = GetSO6Product(boRecord.getStringField("servername"));
	Query queryCondition = QUERY("source" << "SO6" << "src_product" << strProduct);
	ptrMapProductCursor = m_pMapProductController->Find(queryCondition);
	if ((ptrMapProductCursor.get() != NULL) && (ptrMapProductCursor->more()))
	{
		boMapProductRecord = ptrMapProductCursor->nextSafe();
		if (boMapProductRecord.hasField("itsm_product"))
		{
			strProduct = boMapProductRecord.getStringField("itsm_product");
			//====================================
			strContent = boRecord.getStringField("content");
			strContent = CUtilities::StripTags(strContent);
			strContent = CUtilities::ReplaceString(strContent, "\r\n", "\n");
			strContent = CUtilities::ReplaceString(strContent, "\n\r", "\n");
			strDescription = strContent;
			vInformation = CUtilities::SplitString(strContent, "\n");
			if (vInformation.size() > 1)
			{
				strTmp = vInformation[1];
				std::remove(strTmp.begin(), strTmp.end(), ' ');
				if (!strTmp.empty())
					strMsgAlert = CUtilities::ReplaceString(vInformation[1], ",", "\n");
				else
					strMsgAlert = CUtilities::ReplaceString(vInformation[0], ",", "\n");
			}
			else
				strMsgAlert = CUtilities::ReplaceString(vInformation[0], ",", "\n");
			strMsgAlert = "[" + strProduct + "] " + strMsgAlert;
		}
	}
	boAddtionInfo = BSON(
		"product" << strProduct <<
		"description" << strDescription <<
		"alert_message" << strMsgAlert
		);

	return boAddtionInfo.copy();
}
    Status checkAuthForRenameCollectionCommand(ClientBasic* client,
                                               const std::string& dbname,
                                               const BSONObj& cmdObj) {
        NamespaceString sourceNS = NamespaceString(cmdObj.getStringField("renameCollection"));
        NamespaceString targetNS = NamespaceString(cmdObj.getStringField("to"));
        bool dropTarget = cmdObj["dropTarget"].trueValue();

        if (sourceNS.db() == targetNS.db() && !sourceNS.isSystem() && !targetNS.isSystem()) {
            bool authed1 = client->getAuthorizationSession()->isAuthorizedForActionsOnResource(
                    ResourcePattern::forDatabaseName(sourceNS.db()),
                    ActionType::renameCollectionSameDB);

            bool authed2 = true;
            if (dropTarget) {
                authed2 = client->getAuthorizationSession()->isAuthorizedForActionsOnResource(
                        ResourcePattern::forExactNamespace(targetNS), ActionType::dropCollection);
            }

            if (authed1 && authed2) {
                return Status::OK();
            }
        }

        // Check privileges on source collection
        ActionSet actions;
        actions.addAction(ActionType::find);
        actions.addAction(ActionType::dropCollection);
        if (!client->getAuthorizationSession()->isAuthorizedForActionsOnResource(
                ResourcePattern::forExactNamespace(sourceNS), actions)) {
            return Status(ErrorCodes::Unauthorized, "Unauthorized");
        }

        // Check privileges on dest collection
        actions.removeAllActions();
        actions.addAction(ActionType::insert);
        actions.addAction(ActionType::createIndex);
        if (dropTarget) {
            actions.addAction(ActionType::dropCollection);
        }
        if (!client->getAuthorizationSession()->isAuthorizedForActionsOnResource(
                ResourcePattern::forExactNamespace(targetNS), actions)) {
            return Status(ErrorCodes::Unauthorized, "Unauthorized");
        }

        return Status::OK();
    }
Exemplo n.º 18
0
        /**
         * Parses a count command object, 'cmdObj'.
         *
         * On success, fills in the out-parameter 'request' and returns an OK status.
         *
         * Returns a failure status if 'cmdObj' is not well formed.
         */
        Status parseRequest(const std::string& dbname,
                            const BSONObj& cmdObj,
                            CountRequest* request) const {

            long long skip = 0;
            if (cmdObj["skip"].isNumber()) {
                skip = cmdObj["skip"].numberLong();
                if (skip < 0) {
                    return Status(ErrorCodes::BadValue, "skip value is negative in count query");
                }
            }
            else if (cmdObj["skip"].ok()) {
                return Status(ErrorCodes::BadValue, "skip value is not a valid number");
            }

            long long limit = 0;
            if (cmdObj["limit"].isNumber()) {
                limit = cmdObj["limit"].numberLong();
            }
            else if (cmdObj["limit"].ok()) {
                return Status(ErrorCodes::BadValue, "limit value is not a valid number");
            }

            // For counts, limit and -limit mean the same thing.
            if (limit < 0) {
                limit = -limit;
            }

            // We don't validate that "query" is a nested object due to SERVER-15456.
            BSONObj query = cmdObj.getObjectField("query");

            BSONObj hintObj;
            if (Object == cmdObj["hint"].type()) {
                hintObj = cmdObj["hint"].Obj();
            }
            else if (String == cmdObj["hint"].type()) {
                const std::string hint = cmdObj.getStringField("hint");
                hintObj = BSON("$hint" << hint);
            }

            std::string ns = parseNs(dbname, cmdObj);

            if (!nsIsFull(ns)) {
                return Status(ErrorCodes::BadValue, "collection name missing");
            }

            // Parsed correctly. Fill out 'request' with the results.
            request->ns = ns;
            request->query = query;
            request->hint = hintObj;
            request->limit = limit;
            request->skip = skip;

            // By default, count requests are regular count not explain of count.
            request->explain = false;

            return Status::OK();
        }
Exemplo n.º 19
0
void MDistor::checkSize() {
    int count = 0;
    while((count) >  -1) {
        map<string, ChunkInfo>::iterator chunks_it ; 

        {
            boost::mutex::scoped_lock sl(distor_mutex);
            chunks_it = chunkInfos.begin();
        

        while(chunks_it != chunkInfos.end()){
            cout<<"Something in chunkInfos"<<endl;
            string mdistor_ns = MDIS::CHUNKS + chunks_it->first;
            //query for all the chunks
            auto_ptr<DBClientCursor> cursor = dbConn->query(mdistor_ns, BSONObj());
            while(cursor->more()){
                BSONObj b = cursor->next();
                string host = b.getStringField("host");
                string db = b.getStringField("db");
                string coll = b.getStringField("collection");
                mongo::BSONElement range = b.getField("key");
                BSONObj result;
                //cout<<"Start to runCommand"<<endl;
                workers[host]->runCommand(db, BSON("collStats" << coll), result);
                //cout<<"result is  "<<result<<endl;
                int dbSize = result.getField("size").Int();
                cout<<"db size is-----"<<dbSize<<endl;
                //cout<<"range.Obj() : "<<range.Obj()<<endl;
                dbConn->update(mdistor_ns, BSON("key" << range.Obj()), BSON("$set" << BSON("size"<< dbSize)));
                if(dbSize > MDIS::MAX_CHUNK_SIZE){
                    BSONObj newHost = getAvailableWorker();
                    //TODO: is it possible to get rid of this query? 
                    //since we have the cursor here.
                    string newHostStr = newHost.getStringField("host");
                    BSONObj _range = range.Obj();
                    dbConn->update(mdistor_ns, _range, BSON("$set" << BSON("host" << newHostStr)));
                }
            }

            chunks_it++;
        }
        }
        boost::this_thread::sleep(boost::posix_time::seconds(1));
    }
}
Exemplo n.º 20
0
 virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
     string from = cmdObj.getStringField("clone");
     if ( from.empty() )
         return false;
     /* replication note: we must logOp() not the command, but the cloned data -- if the slave
        were to clone it would get a different point-in-time and not match.
        */
     return cloneFrom(from.c_str(), errmsg, database->name, /*logForReplication=*/!fromRepl, /*slaveok*/false, /*usereplauth*/false);
 }
Exemplo n.º 21
0
        bool run(OperationContext* txn,
                 const string& dbname,
                 BSONObj& jsobj,
                 int,
                 string& errmsg,
                 BSONObjBuilder& result,
                 bool fromRepl ) {
            // calls renamecollection which does a global lock, so we must too:
            //
            Lock::GlobalWrite globalWriteLock(txn->lockState());
            WriteUnitOfWork wunit(txn->recoveryUnit());
            Client::Context ctx(txn, dbname);

            Database* db = ctx.db();

            stopIndexBuilds(txn, db, jsobj);
            BackgroundOperation::assertNoBgOpInProgForDb(dbname.c_str());

            string shortSource = jsobj.getStringField( "convertToCapped" );
            string longSource = dbname + "." + shortSource;
            double size = jsobj.getField( "size" ).number();

            if ( shortSource.empty() || size == 0 ) {
                errmsg = "invalid command spec";
                return false;
            }

            string shortTmpName = str::stream() << "tmp.convertToCapped." << shortSource;
            string longTmpName = str::stream() << dbname << "." << shortTmpName;

            if ( db->getCollection( txn, longTmpName ) ) {
                Status status = db->dropCollection( txn, longTmpName );
                if ( !status.isOK() )
                    return appendCommandStatus( result, status );
            }

            Status status = cloneCollectionAsCapped( txn, db, shortSource, shortTmpName, size, true, false );

            if ( !status.isOK() )
                return appendCommandStatus( result, status );

            verify( db->getCollection( txn, longTmpName ) );

            status = db->dropCollection( txn, longSource );
            if ( !status.isOK() )
                return appendCommandStatus( result, status );

            status = db->renameCollection( txn, longTmpName, longSource, false );
            if ( !status.isOK() )
                return appendCommandStatus( result, status );

            if (!fromRepl)
                repl::logOp(txn, "c",(dbname + ".$cmd").c_str(), jsobj);

            wunit.commit();
            return true;
        }
Exemplo n.º 22
0
Arquivo: sync.cpp Projeto: wjin/mongo
    BSONObj Sync::getMissingDoc(OperationContext* txn, Database* db, const BSONObj& o) {
        OplogReader missingObjReader; // why are we using OplogReader to run a non-oplog query?
        const char *ns = o.getStringField("ns");

        // capped collections
        Collection* collection = db->getCollection(ns);
        if ( collection && collection->isCapped() ) {
            log() << "replication missing doc, but this is okay for a capped collection (" << ns << ")" << endl;
            return BSONObj();
        }

        const int retryMax = 3;
        for (int retryCount = 1; retryCount <= retryMax; ++retryCount) {
            if (retryCount != 1) {
                // if we are retrying, sleep a bit to let the network possibly recover
                sleepsecs(retryCount * retryCount);
            }
            try {
                bool ok = missingObjReader.connect(HostAndPort(hn));
                if (!ok) {
                    warning() << "network problem detected while connecting to the "
                              << "sync source, attempt " << retryCount << " of "
                              << retryMax << endl;
                        continue;  // try again
                }
            } 
            catch (const SocketException&) {
                warning() << "network problem detected while connecting to the "
                          << "sync source, attempt " << retryCount << " of "
                          << retryMax << endl;
                continue; // try again
            }

            // might be more than just _id in the update criteria
            BSONObj query = BSONObjBuilder().append(o.getObjectField("o2")["_id"]).obj();
            BSONObj missingObj;
            try {
                missingObj = missingObjReader.findOne(ns, query);
            } 
            catch (const SocketException&) {
                warning() << "network problem detected while fetching a missing document from the "
                          << "sync source, attempt " << retryCount << " of "
                          << retryMax << endl;
                continue; // try again
            } 
            catch (DBException& e) {
                log() << "replication assertion fetching missing object: " << e.what() << endl;
                throw;
            }

            // success!
            return missingObj;
        }
        // retry count exceeded
        msgasserted(15916, 
                    str::stream() << "Can no longer connect to initial sync source: " << hn);
    }
Exemplo n.º 23
0
bool CCSAlertController::UpdateINCStatus(BSONObj bsonRecord)
{
	int iResult;
	string strResponse, strItsmStatus, strLog, strRejectMess;
	char *cINCCode, *cITSMCode, *cITSMCloseDate, *cCreatedBy, *cComment;
	short iINCStatusID = 99;
	short *p_iINCStatusID;
	if (bsonRecord.isEmpty())
	{
		return false;
	}
	strItsmStatus = bsonRecord.hasField("itsm_status") ? bsonRecord.getStringField("itsm_status") : "";
	strRejectMess = bsonRecord.hasField("msg") ? bsonRecord.getStringField("msg") : "";
	cINCCode = new char[100];
	cITSMCode = new char[100];
	cITSMCloseDate = new char[100];
	cCreatedBy = new char[100];
	cComment = new char[strRejectMess.size() + 1];
	// ========= Get Inc Status ===========
	if (strItsmStatus.compare("open") == 0)
		iINCStatusID = 24;
	else if (strItsmStatus.compare("closed") == 0)
		iINCStatusID = 26;
	else if (strItsmStatus.compare("reopen") == 0)
		iINCStatusID = 28;
	else if (strItsmStatus.compare("rejected") == 0)
		iINCStatusID = 27;
	else if (strItsmStatus.compare("resolved") == 0)
		iINCStatusID = 25;
	strcpy(cINCCode, bsonRecord.getStringField("ticket_id"));
	strcpy(cITSMCode, bsonRecord.getStringField("itsm_id"));
	strcpy(cCreatedBy, "sdk");
	memcpy(cComment, strRejectMess.c_str(), strRejectMess.size() + 1);
	strcpy(cITSMCloseDate, bsonRecord.getStringField("outage_end"));
	p_iINCStatusID = &iINCStatusID;
	iResult = CAPIUtilities::CallUpdateINCStatusAPI(cINCCode, p_iINCStatusID, cITSMCode, cCreatedBy, cComment, cITSMCloseDate, strResponse);
	//============================Write Log==========================
	strLog = CUtilities::FormatLog(INFO_MSG, "UpdateAlertStt", "CSAlertController", "UpdateCSStatusINC:" + strResponse);
	CUtilities::WriteInfoLog(INFO_MSG, strLog);
	//============================Destroy==========================
	delete[] cINCCode;
	delete[] cITSMCode;
	delete[] cITSMCloseDate;
	delete[] cCreatedBy;
	delete[] cComment;
	if (API_ACTION_ERROR == iResult || CODE_ERROR_INIT == strResponse || CODE_ERROR_UPDATE_STATUS_INC == strResponse
		|| CODE_ERROR_SSL_CLIENT_CONTEXT == strResponse
		|| strResponse.find(RESPONSE_UNSUCCESS) != std::string::npos || strResponse.find("fail") != std::string::npos)
	{
		string strSourceId = bsonRecord.hasField("_id") ? CUtilities::GetMongoObjId(bsonRecord["_id"]) : "";
		string strStatus = bsonRecord.hasField("itsm_status") ? bsonRecord.getStringField("itsm_status") : "";
		string strCurrStatus = bsonRecord.hasField("prev_itsm_status") ? bsonRecord.getStringField("prev_itsm_status") : "";
		string strLog = CUtilities::FormatLog(INFO_MSG, "CCSAlertController", "UpdateINCStatus", "FAIL:" + strSourceId + ": " +
			strCurrStatus + " -> " + strStatus);
		CUtilities::WriteInfoLog(INFO_MSG, strLog);
		return false;
	}
	return true;
}
Exemplo n.º 24
0
void MDistor::addChunk(string ns, int range) {
    BSONObjBuilder chunk;
    //cout<<"Starting to set keys"<<endl;
    size_t p_db = ns.find('.');
    string db = ns.substr(0,p_db);
    size_t p_coll = ns.find('.', p_db + 1);
    string coll = ns.substr(p_db + 1, (p_coll - p_db - 1));
    cout<<"addChunk: adding chunk for "<<ns<<endl;
    string chunkColl = MDIS::CHUNKS + ns;
    cout<<"db is "<<db<<"; coll is "<<coll<<endl; 
    
    map<string, ChunkInfo>::iterator info_iter;
    {
        boost::mutex::scoped_lock ls(distor_mutex);
        info_iter = chunkInfos.find(ns);
    }

    if(info_iter != chunkInfos.end()) {
        ChunkInfo &info = (*info_iter).second;
        chunk.append("type", "chunk");
        chunk.append("ns", ns);
        chunk.append("collection", coll);
        chunk.append("db", db);
        chunk.append("range", range);

        int start = info.getCurrentMax() + 1;
        int end = info.getCurrentMax() + range;

        info.setCurrentMax(end);

        chunk.append("key", BSON("min" << start << "max" << end));

        BSONObj worker = getAvailableWorker();

        chunk.append("worker", worker.getStringField("worker"));
        chunk.append("host" , worker.getStringField("host"));
        chunk.append("size", 0);

        dbConn->ensureIndex(chunkColl, BSON("ns" << 1 << "key" <<  1), true);
        dbConn->insert(chunkColl, chunk.obj());
    } else  {
        cout<<"addChunk: did not add chunk"<<endl;
    }
}
Exemplo n.º 25
0
 virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
     string fromhost = cmdObj.getStringField("fromhost");
     if ( fromhost.empty() ) {
         /* copy from self */
         stringstream ss;
         ss << "localhost:" << cmdLine.port;
         fromhost = ss.str();
     }
     string fromdb = cmdObj.getStringField("fromdb");
     string todb = cmdObj.getStringField("todb");
     if ( fromhost.empty() || todb.empty() || fromdb.empty() ) {
         errmsg = "parms missing - {copydb: 1, fromhost: <hostname>, fromdb: <db>, todb: <db>}";
         return false;
     }
     setClient(todb.c_str());
     bool res = cloneFrom(fromhost.c_str(), errmsg, fromdb, /*logForReplication=*/!fromRepl, /*slaveok*/false, /*replauth*/false, /*snapshot*/true);
     cc().clearns();
     return res;
 }
Exemplo n.º 26
0
 void DBConfig::unserialize(const BSONObj& from){
     _name = from.getStringField("name");
     _shardingEnabled = from.getBoolField("partitioned");
     _primary = from.getStringField("primary");
     
     _sharded.clear();
     BSONObj sharded = from.getObjectField( "sharded" );
     if ( ! sharded.isEmpty() ){
         BSONObjIterator i(sharded);
         while ( i.more() ){
             BSONElement e = i.next();
             uassert( 10182 ,  "sharded things have to be objects" , e.type() == Object );
             BSONObj c = e.embeddedObject();
             uassert( 10183 ,  "key has to be an object" , c["key"].type() == Object );
             _sharded[e.fieldName()] = CollectionInfo( c["key"].embeddedObject() , 
                                                       c["unique"].trueValue() );
         }
     }
 }
Exemplo n.º 27
0
    virtual bool run(OperationContext* txn,
                     const string&,
                     BSONObj& cmdObj,
                     int,
                     string& errmsg,
                     BSONObjBuilder& result) {
        const auto fromdbElt = cmdObj["fromdb"];
        uassert(ErrorCodes::TypeMismatch,
                "'renameCollection' must be of type String",
                fromdbElt.type() == BSONType::String);
        const string fromDb = fromdbElt.str();
        uassert(
            ErrorCodes::InvalidNamespace,
            str::stream() << "Invalid 'fromdb' name: " << fromDb,
            NamespaceString::validDBName(fromDb, NamespaceString::DollarInDbNameBehavior::Allow));

        string fromHost = cmdObj.getStringField("fromhost");
        if (fromHost.empty()) {
            /* copy from self */
            stringstream ss;
            ss << "localhost:" << serverGlobalParams.port;
            fromHost = ss.str();
        }

        const ConnectionString cs(uassertStatusOK(ConnectionString::parse(fromHost)));

        BSONElement mechanismElement;
        Status status = bsonExtractField(cmdObj, saslCommandMechanismFieldName, &mechanismElement);
        if (!status.isOK()) {
            return appendCommandStatus(result, status);
        }

        BSONElement payloadElement;
        status = bsonExtractField(cmdObj, saslCommandPayloadFieldName, &payloadElement);
        if (!status.isOK()) {
            log() << "Failed to extract payload: " << status;
            return false;
        }

        auto& authConn = CopyDbAuthConnection::forClient(txn->getClient());
        authConn.reset(cs.connect(StringData(), errmsg));
        if (!authConn.get()) {
            return false;
        }

        BSONObj ret;
        if (!authConn->runCommand(
                    fromDb, BSON("saslStart" << 1 << mechanismElement << payloadElement), ret)) {
            authConn.reset();
            return appendCommandStatus(result, getStatusFromCommandResult(ret));
        }

        result.appendElements(ret);
        return true;
    }
Exemplo n.º 28
0
    Status CmdCount::parseRequest(const std::string& dbname,
                                  const BSONObj& cmdObj,
                                  CountRequest* request) const {
        const string ns = parseNs(dbname, cmdObj);

        long long skip = 0;
        if (cmdObj["skip"].isNumber()) {
            skip = cmdObj["skip"].numberLong();
            if (skip < 0) {
                return Status(ErrorCodes::BadValue, "skip value is negative in count query");
            }
        }
        else if (cmdObj["skip"].ok()) {
            return Status(ErrorCodes::BadValue, "skip value is not a valid number");
        }

        long long limit = 0;
        if (cmdObj["limit"].isNumber()) {
            limit = cmdObj["limit"].numberLong();
        }
        else if (cmdObj["limit"].ok()) {
            return Status(ErrorCodes::BadValue, "limit value is not a valid number");
        }

        // For counts, limit and -limit mean the same thing.
        if (limit < 0) {
            limit = -limit;
        }

        BSONObj query;
        if (!cmdObj["query"].eoo()) {
            if (Object != cmdObj["query"].type()) {
                return Status(ErrorCodes::BadValue, "query field for count must be an object");
            }
            query = cmdObj.getObjectField("query");
        }

        BSONObj hintObj;
        if (Object == cmdObj["hint"].type()) {
            hintObj = cmdObj["hint"].Obj();
        }
        else if (String == cmdObj["hint"].type()) {
            const std::string hint = cmdObj.getStringField("hint");
            hintObj = BSON("$hint" << hint);
        }

        // Parsed correctly. Fill out 'request' with the results.
        request->ns = ns;
        request->query = query;
        request->hint = hintObj;
        request->limit = limit;
        request->skip = skip;

        return Status::OK();
    }
Exemplo n.º 29
0
        void doWork() {
            if ( !theReplSet ) {
                LOG(2) << "replSet not initialized yet, skipping health poll this round" << rsLog;
                return;
            }

            HeartbeatInfo mem = m;
            HeartbeatInfo old = mem;
            bool needsNewStateChecked = false;
            try {
                BSONObj info;
                int theirConfigVersion = -10000;

                bool ok = _requestHeartbeat(mem, info, theirConfigVersion);

                // weight new ping with old pings
                // on the first ping, just use the ping value
                if (old.ping != 0) {
                    mem.ping = (unsigned int)((old.ping * .8) + (mem.ping * .2));
                }

                if( ok ) {
                    up(info, mem, &needsNewStateChecked);
                }
                else if (!info["errmsg"].eoo() && info["errmsg"].str() == "unauthorized") {
                    authIssue(mem);
                }
                else {
                    down(mem, info.getStringField("errmsg"));
                }
            }
            catch (const DBException& e) {
                log() << "replSet health poll task caught a DBException: " << e.what();
                down(mem, e.what());
            }
            catch (const std::exception& e) {
                log() << "replSet health poll task caught an exception: " << e.what();
                down(mem, e.what());
            }
            m = mem;

            theReplSet->mgr->send( boost::bind(&ReplSet::msgUpdateHBInfo, theReplSet, mem) );

            static time_t last = 0;
            time_t now = time(0);
            bool changed = mem.changed(old);
            if( changed ) {
                if( old.hbstate != mem.hbstate )
                    log() << "replSet member " << h.toString() << " is now in state " << mem.hbstate.toString() << rsLog;
            }
            if( needsNewStateChecked || changed || now-last>4 ) {
                last = now;
                theReplSet->mgr->send( boost::bind(&Manager::msgCheckNewState, theReplSet->mgr) );
            }
        }
Exemplo n.º 30
0
    /* Generally replAuthenticate will only be called within system threads to fully authenticate
     * connections to other nodes in the cluster that will be used as part of internal operations.
     * If a user-initiated action results in needing to call replAuthenticate, you can call it
     * with skipAuthCheck set to false. Only do this if you are certain that the proper auth
     * checks have already run to ensure that the user is authorized to do everything that this
     * connection will be used for!
     */
    bool replAuthenticate(DBClientBase *conn, bool skipAuthCheck) {
        if( noauth ) {
            return true;
        }
        if (!skipAuthCheck && !cc().getAuthorizationManager()->hasInternalAuthorization()) {
            log() << "replauthenticate: requires internal authorization, failing" << endl;
            return false;
        }

        string u;
        string p;
        if (internalSecurity.pwd.length() > 0) {
            u = internalSecurity.user;
            p = internalSecurity.pwd;
        }
        else {
            BSONObj user;
            {
                StringData ns("local.system.users");
                LOCK_REASON(lockReason, "repl: authenticating with local db");
                Client::ReadContext ctx(ns, lockReason);
                if (!Collection::findOne(ns, userReplQuery, user) ||
                        // try the first user in local
                        !Collection::findOne(ns, BSONObj(), user)) {
                    log() << "replauthenticate: no user in local.system.users to use for authentication\n";
                    return false;
                }
            }
            u = user.getStringField("user");
            p = user.getStringField("pwd");
            massert( 10392 , "bad user object? [1]", !u.empty());
            massert( 10393 , "bad user object? [2]", !p.empty());
        }

        string err;
        if( !conn->auth("local", u.c_str(), p.c_str(), err, false) ) {
            log() << "replauthenticate: can't authenticate to master server, user:" << u << endl;
            return false;
        }

        return true;
    }