Esempio n. 1
0
static void logCursorsWaiting(RangeDeleteEntry* entry) {
    // We always log the first cursors waiting message (so we have cursor ids in the logs).
    // After 15 minutes (the cursor timeout period), we start logging additional messages at
    // a 1 minute interval.
    static const auto kLogCursorsThreshold = stdx::chrono::minutes{15};
    static const auto kLogCursorsInterval = stdx::chrono::minutes{1};

    Date_t currentTime = jsTime();
    Milliseconds elapsedMillisSinceQueued{0};

    // We always log the first message when lastLoggedTime == 0
    if (entry->lastLoggedTS != Date_t()) {
        if (currentTime > entry->stats.queueStartTS)
            elapsedMillisSinceQueued = currentTime - entry->stats.queueStartTS;

        // Not logging, threshold not passed
        if (elapsedMillisSinceQueued < kLogCursorsThreshold)
            return;

        Milliseconds elapsedMillisSinceLog{0};
        if (currentTime > entry->lastLoggedTS)
            elapsedMillisSinceLog = currentTime - entry->lastLoggedTS;

        // Not logging, logged a short time ago
        if (elapsedMillisSinceLog < kLogCursorsInterval)
            return;
    }

    str::stream cursorList;
    for (std::set<CursorId>::const_iterator it = entry->cursorsToWait.begin();
         it != entry->cursorsToWait.end();
         ++it) {
        if (it != entry->cursorsToWait.begin())
            cursorList << ", ";
        cursorList << *it;
    }

    log() << "waiting for open cursors before removing range "
          << "[" << entry->options.range.minKey << ", " << entry->options.range.maxKey << ") "
          << "in " << entry->options.range.ns
          << (entry->lastLoggedTS == Date_t()
                  ? string("")
                  : string(str::stream() << ", elapsed secs: "
                                         << durationCount<Seconds>(elapsedMillisSinceQueued)))
          << ", cursor ids: [" << string(cursorList) << "]";

    entry->lastLoggedTS = currentTime;
}
Esempio n. 2
0
Status ModifierCurrentDate::apply() const {
    const bool destExists = (_preparedState->elemFound.ok() &&
                             _preparedState->idxFound == (_updatePath.numParts() - 1));

    mutablebson::Document& doc = _preparedState->doc;
    StringData lastPart = _updatePath.getPart(_updatePath.numParts() - 1);
    // If the element exists and is the same type, then that is what we want to work with
    mutablebson::Element elemToSet = destExists ? _preparedState->elemFound : doc.end();

    if (!destExists) {
        // Creates the final element that's going to be $set in 'doc'.
        // fills in the value with place-holder/empty

        elemToSet = _typeIsDate ? doc.makeElementDate(lastPart, Date_t())
                                : doc.makeElementTimestamp(lastPart, Timestamp());

        if (!elemToSet.ok()) {
            return Status(ErrorCodes::InternalError, "can't create new element");
        }

        // Now, we can be in two cases here, as far as attaching the element being set goes:
        // (a) none of the parts in the element's path exist, or (b) some parts of the path
        // exist but not all.
        if (!_preparedState->elemFound.ok()) {
            _preparedState->elemFound = doc.root();
            _preparedState->idxFound = 0;
        } else {
            _preparedState->idxFound++;
        }

        // createPathAt() will complete the path and attach 'elemToSet' at the end of it.
        Status s = pathsupport::createPathAt(
                       _updatePath, _preparedState->idxFound, _preparedState->elemFound, elemToSet)
                       .getStatus();
        if (!s.isOK())
            return s;
    }

    dassert(elemToSet.ok());

    // By the time we are here the element is in place and we just need to update the value
    if (_typeIsDate) {
        const mongo::Date_t now = mongo::jsTime();
        Status s = elemToSet.setValueDate(now);
        if (!s.isOK())
            return s;
    } else {
        ServiceContext* service = getGlobalServiceContext();
        auto ts = LogicalClock::get(service)->reserveTicks(1).asTimestamp();
        Status s = elemToSet.setValueTimestamp(ts);
        if (!s.isOK())
            return s;
    }

    // Set the elemFound, idxFound to the changed element for oplog logging.
    _preparedState->elemFound = elemToSet;
    _preparedState->idxFound = (_updatePath.numParts() - 1);

    return Status::OK();
}
void MemberHeartbeatData::setUpValues(Date_t now,
                                      const HostAndPort& host,
                                      ReplSetHeartbeatResponse hbResponse) {
    _health = 1;
    if (_upSince == Date_t()) {
        _upSince = now;
    }
    _authIssue = false;
    _lastHeartbeat = now;
    if (!hbResponse.hasState()) {
        hbResponse.setState(MemberState::RS_UNKNOWN);
    }
    if (!hbResponse.hasElectionTime()) {
        hbResponse.setElectionTime(_lastResponse.getElectionTime());
    }
    if (!hbResponse.hasOpTime()) {
        hbResponse.setOpTime(_lastResponse.getOpTime());
    }

    // Log if the state changes
    if (_lastResponse.getState() != hbResponse.getState()) {
        log() << "Member " << host.toString() << " is now in state "
              << hbResponse.getState().toString() << rsLog;
    }

    _lastResponse = hbResponse;
}
Esempio n. 4
0
    std::vector<ReplicationExecutor::RemoteCommandRequest>
    FreshnessChecker::Algorithm::getRequests() const {
        invariant(_targets.size());
        const MemberConfig& selfConfig = _rsConfig.getMemberAt(_selfIndex);

        // gather all not-down nodes, get their fullnames(or hostandport's)
        // schedule fresh command for each node
        BSONObjBuilder freshCmdBuilder;
        freshCmdBuilder.append("replSetFresh", 1);
        freshCmdBuilder.append("set", _rsConfig.getReplSetName());
        freshCmdBuilder.append("opTime", Date_t(_lastOpTimeApplied.asDate()));
        freshCmdBuilder.append("who", selfConfig.getHostAndPort().toString());
        freshCmdBuilder.appendIntOrLL("cfgver", _rsConfig.getConfigVersion());
        freshCmdBuilder.append("id", selfConfig.getId());
        const BSONObj replSetFreshCmd = freshCmdBuilder.obj();

        std::vector<ReplicationExecutor::RemoteCommandRequest> requests;
        for (std::vector<HostAndPort>::const_iterator it = _targets.begin();
             it != _targets.end();
             ++it) {
            invariant(*it != selfConfig.getHostAndPort());
            requests.push_back(ReplicationExecutor::RemoteCommandRequest(
                        *it,
                        "admin",
                        replSetFreshCmd,
                        Milliseconds(30*1000)));   // trying to match current Socket timeout
        }

        return requests;
    }
Esempio n. 5
0
/**
 * Test that client cursors time out and get deleted.
 */
TEST_F(CursorManagerTest, InactiveCursorShouldTimeout) {
    CursorManager* cursorManager = useCursorManager();
    auto clock = useClock();

    cursorManager->registerCursor(_opCtx.get(),
                                  {makeFakePlanExecutor(),
                                   NamespaceString{"test.collection"},
                                   {},
                                   repl::ReadConcernLevel::kLocalReadConcern,
                                   BSONObj()});

    ASSERT_EQ(0UL, cursorManager->timeoutCursors(_opCtx.get(), Date_t()));

    clock->advance(getDefaultCursorTimeoutMillis());
    ASSERT_EQ(1UL, cursorManager->timeoutCursors(_opCtx.get(), clock->now()));
    ASSERT_EQ(0UL, cursorManager->numCursors());

    cursorManager->registerCursor(_opCtx.get(),
                                  {makeFakePlanExecutor(),
                                   NamespaceString{"test.collection"},
                                   {},
                                   repl::ReadConcernLevel::kLocalReadConcern,
                                   BSONObj()});
    ASSERT_EQ(1UL, cursorManager->timeoutCursors(_opCtx.get(), Date_t::max()));
    ASSERT_EQ(0UL, cursorManager->numCursors());
}
Esempio n. 6
0
mxArray *GetTick(mxArray *inst, mxArray *start, mxArray *end)
{
    mxArray *result;
    const char *field_names[] = {"tradingday", "time", "instrument", "o", "h", "l", "c", "v", "i", "a1", "b1", "av1", "bv1"};
    
    string instrument = mxArrayToString(inst);
    int st = mxGetScalar(start);
    int et = mxGetScalar(end);
    auto_ptr<DBClientCursor> cursor;
    BSONObjBuilder b;
    BSONObjBuilder timePeriod;
    
    b.append("InstrumentID", instrument);
    timePeriod.appendDate("$gte",( (st - 719529) * 24LL)* 60LL * 60LL * 1000LL);
    timePeriod.appendDate("$lte", ( (et - 719529 + 1) * 24LL) * 60LL * 60LL * 1000LL);
    b.append("UpdateTime", timePeriod.obj());
    BSONObj qry = b.obj();
    cursor = mCon->query(string("MarketData.") + collection, qry);
    int size = cursor->itcount();
//     mexPrintf("数据长度%d, collection为%s\n", size, collection.c_str());
    mwSize dims[2] = {1, size};
    result = mxCreateStructArray(2, dims, sizeof(field_names)/sizeof(*field_names), field_names);
    cursor = mCon->query(string("MarketData.") + collection, qry);
    BSONObj p;
    int i = size - 1;
    while(cursor->more())
    {
        p = cursor->next();
        tm buf;
        //trun into peking time;
        Date_t pkTime = Date_t(p["UpdateTime"].Date().millis + 8 * 3600000LL);
        double time = pkTime.millis%1000 / 100 / 100000.0;
        pkTime.toTm(&buf);
        int day = (buf.tm_year + 1900) * 10000 + (buf.tm_mon + 1) * 100 + buf.tm_mday;
        time = time + buf.tm_hour + buf.tm_min / 100.0 + buf.tm_sec / 10000.0;
        
        mxSetField(result, i, "tradingday", mxCreateDoubleScalar(day));
        mxSetField(result, i, "time", mxCreateDoubleScalar(time));
        mxSetField(result, i, "instrument", mxCreateString(instrument.c_str()));
        mxSetField(result, i, "o", mxCreateDoubleScalar( p["OpenPrice"].Double() ));
        mxSetField(result, i, "h", mxCreateDoubleScalar(p["HighestPrice"].Double()));
        mxSetField(result, i, "l", mxCreateDoubleScalar(p["LowestPrice"].Double()));
        mxSetField(result, i, "c", mxCreateDoubleScalar(p["LastPrice"].Double()));
        mxSetField(result, i, "v", mxCreateDoubleScalar(p["Volume"].Int()));
        mxSetField(result, i, "i", mxCreateDoubleScalar(p["OpenInterest"].Double()));
        mxSetField(result, i, "a1", mxCreateDoubleScalar(p["AskPrice1"].Double()));
        mxSetField(result, i, "b1", mxCreateDoubleScalar(p["BidPrice1"].Double()));
        mxSetField(result, i, "av1", mxCreateDoubleScalar(p["AskVolume1"].Int()));
        mxSetField(result, i, "bv1", mxCreateDoubleScalar(p["BidVolume1"].Int()));
        
        --i;
        if(i < -1)
        {
            mexWarnMsgTxt("GetTick程序越界!");
            break;
        }
    }
    
    return result;
}
Esempio n. 7
0
TEST_F(ReporterTest, ShutdownImmediatelyAfterTriggerWhileKeepAliveTimeoutIsScheduledShouldSucceed) {
    processNetworkResponse(BSON("ok" << 1));

    auto keepAliveTimeoutWhen = getExecutor().now() + reporter->getKeepAliveInterval();
    ASSERT_EQUALS(keepAliveTimeoutWhen, reporter->getKeepAliveTimeoutWhen_forTest());
    ASSERT_TRUE(reporter->isActive());

    auto until = keepAliveTimeoutWhen - reporter->getKeepAliveInterval() / 2;
    runUntil(until);

    ASSERT_OK(reporter->trigger());

    // '_keepAliveTimeoutWhen' is reset by trigger() not by the canceled callback.
    ASSERT_EQUALS(Date_t(), reporter->getKeepAliveTimeoutWhen_forTest());
    ASSERT_TRUE(reporter->isActive());

    auto net = getNet();
    net->enterNetwork();
    ASSERT_TRUE(net->hasReadyRequests());
    net->exitNetwork();

    reporter->shutdown();

    net->enterNetwork();
    ASSERT_FALSE(net->hasReadyRequests());
    // Executor should invoke reporter callback with a ErrorCodes::CallbackCanceled status.
    net->runReadyNetworkOperations();
    net->exitNetwork();

    ASSERT_EQUALS(ErrorCodes::CallbackCanceled, reporter->join());
    assertReporterDone();
}
Esempio n. 8
0
TEST_F(ReporterTest,
       TriggerBeforeKeepAliveTimeoutShouldCancelExistingTimeoutAndSendUpdateImmediately) {
    processNetworkResponse(BSON("ok" << 1));

    auto keepAliveTimeoutWhen = getExecutor().now() + reporter->getKeepAliveInterval();

    ASSERT_EQUALS(keepAliveTimeoutWhen, reporter->getKeepAliveTimeoutWhen_forTest());
    ASSERT_TRUE(reporter->isActive());

    auto until = keepAliveTimeoutWhen - reporter->getKeepAliveInterval() / 2;
    runUntil(until);

    ASSERT_OK(reporter->trigger());

    // '_keepAliveTimeoutWhen' is reset by trigger() not by the canceled callback.
    ASSERT_EQUALS(Date_t(), reporter->getKeepAliveTimeoutWhen_forTest());
    ASSERT_TRUE(reporter->isActive());

    processNetworkResponse(BSON("ok" << 1));

    keepAliveTimeoutWhen = getExecutor().now() + reporter->getKeepAliveInterval();

    // A new keep alive timeout should be scheduled.
    ASSERT_EQUALS(keepAliveTimeoutWhen, reporter->getKeepAliveTimeoutWhen_forTest());
    ASSERT_TRUE(reporter->isActive());

    reporter->shutdown();

    ASSERT_EQUALS(ErrorCodes::CallbackCanceled, reporter->join());
    assertReporterDone();
}
 std::pair<ReplicationExecutor::WorkItem, ReplicationExecutor::CallbackHandle>
 ReplicationExecutor::getWork() {
     boost::unique_lock<boost::mutex> lk(_mutex);
     while (true) {
         const Date_t now = _networkInterface->now();
         Date_t nextWakeupDate = scheduleReadySleepers_inlock(now);
         if (!_readyQueue.empty()) {
             break;
         }
         else if (_inShutdown) {
             return std::make_pair(WorkItem(), CallbackHandle());
         }
         lk.unlock();
         if (nextWakeupDate == Date_t(~0ULL)) {
             _networkInterface->waitForWork();
         }
         else {
             _networkInterface->waitForWorkUntil(nextWakeupDate);
         }
         lk.lock();
     }
     const CallbackHandle cbHandle(_readyQueue.begin());
     const WorkItem work = *cbHandle._iter;
     _readyQueue.begin()->callback = CallbackFn();
     _freeQueue.splice(_freeQueue.begin(), _readyQueue, _readyQueue.begin());
     return std::make_pair(work, cbHandle);
 }
    void UserCacheInvalidator::run() {
        Client::initThread("UserCacheInvalidatorThread");
        lastInvalidationTime = Date_t(curTimeMillis64());

        while (true) {
            boost::unique_lock<boost::mutex> lock(invalidationIntervalMutex);
            Date_t sleepUntil = Date_t(
                    lastInvalidationTime.millis + userCacheInvalidationIntervalSecs * 1000);
            Date_t now(curTimeMillis64());
            while (now.millis < sleepUntil.millis) {
                invalidationIntervalChangedCondition.timed_wait(lock,
                                                                Milliseconds(sleepUntil - now));
                sleepUntil = Date_t(
                        lastInvalidationTime.millis + (userCacheInvalidationIntervalSecs * 1000));
                now = Date_t(curTimeMillis64());
            }
            lastInvalidationTime = now;
            lock.unlock();

            if (inShutdown()) {
                break;
            }

            StatusWith<OID> currentGeneration = getCurrentCacheGeneration();
            if (!currentGeneration.isOK()) {
                if (currentGeneration.getStatus().code() == ErrorCodes::CommandNotFound) {
                    warning() << "_getUserCacheGeneration command not found on config server(s), "
                            "this most likely means you are running an outdated version of mongod "
                            "on the config servers" << std::endl;
                } else {
                    warning() << "An error occurred while fetching current user cache generation "
                            "to check if user cache needs invalidation: " <<
                            currentGeneration.getStatus() << std::endl;
                }
                // When in doubt, invalidate the cache
                _authzManager->invalidateUserCache();
            }

            if (currentGeneration.getValue() != _previousCacheGeneration) {
                log() << "User cache generation changed from " << _previousCacheGeneration <<
                        " to " << currentGeneration.getValue() << "; invalidating user cache" <<
                        std::endl;
                _authzManager->invalidateUserCache();
                _previousCacheGeneration = currentGeneration.getValue();
            }
        }
    }
Esempio n. 11
0
    Status SyncSourceFeedback::updateUpstream(OperationContext* txn) {
        ReplicationCoordinator* replCoord = getGlobalReplicationCoordinator();
        if (replCoord->getMemberState().primary()) {
            // primary has no one to update to
            return Status::OK();
        }
        BSONObjBuilder cmd;
        {
            boost::unique_lock<boost::mutex> lock(_mtx);
            if (_handshakeNeeded) {
                // Don't send updates if there are nodes that haven't yet been handshaked
                return Status(ErrorCodes::NodeNotFound,
                              "Need to send handshake before updating position upstream");
            }
            // the command could not be created, likely because the node was removed from the set
            if (!replCoord->prepareReplSetUpdatePositionCommand(&cmd)) {
                return Status::OK();
            }
        }
        BSONObj res;

        LOG(2) << "Sending slave oplog progress to upstream updater: " << cmd.done();
        try {
            _connection->runCommand("admin", cmd.obj(), res);
        }
        catch (const DBException& e) {
            log() << "SyncSourceFeedback error sending update: " << e.what() << endl;
            // blacklist sync target for .5 seconds and find a new one
            replCoord->blacklistSyncSource(_syncTarget,
                                           Date_t(curTimeMillis64() + 500));
            BackgroundSync::get()->clearSyncTarget();
            _resetConnection();
            return e.toStatus();
        }

        Status status = Command::getStatusFromCommandResult(res);
        if (!status.isOK()) {
            log() << "SyncSourceFeedback error sending update, response: " << res.toString() <<endl;
            // blacklist sync target for .5 seconds and find a new one
            replCoord->blacklistSyncSource(_syncTarget,
                                           Date_t(curTimeMillis64() + 500));
            BackgroundSync::get()->clearSyncTarget();
            _resetConnection();
        }
        return status;
    }
Esempio n. 12
0
    /* Do we have the newest data of them all?
       @param allUp - set to true if all members are up.  Only set if true returned.
       @return true if we are freshest.  Note we may tie.
    */
    bool Consensus::_weAreFreshest(bool& allUp, int& nTies) {
        const OpTime ord = theReplSet->lastOpTimeWritten;
        nTies = 0;
        verify( !ord.isNull() );
        BSONObj cmd = BSON(
                          "replSetFresh" << 1 <<
                          "set" << rs.name() <<
                          "opTime" << Date_t(ord.asDate()) <<
                          "who" << rs._self->fullName() <<
                          "cfgver" << rs._cfg->version <<
                          "id" << rs._self->id());
        list<Target> L;
        int ver;
        /* the following queries arbiters, even though they are never fresh.  wonder if that makes sense.
           it doesn't, but it could, if they "know" what freshness it one day.  so consider removing
           arbiters from getTargets() here.  although getTargets is used elsewhere for elections; there
           arbiters are certainly targets - so a "includeArbs" bool would be necessary if we want to make
           not fetching them herein happen.
           */
        rs.getTargets(L, ver);
        _multiCommand(cmd, L);
        int nok = 0;
        allUp = true;
        for( list<Target>::iterator i = L.begin(); i != L.end(); i++ ) {
            if( i->ok ) {
                nok++;
                if( i->result["fresher"].trueValue() ) {
                    log() << "not electing self, we are not freshest" << rsLog;
                    return false;
                }
                OpTime remoteOrd( i->result["opTime"].Date() );
                if( remoteOrd == ord )
                    nTies++;
                verify( remoteOrd <= ord );

                if( i->result["veto"].trueValue() ) {
                    BSONElement msg = i->result["errmsg"];
                    if (!msg.eoo()) {
                        log() << "not electing self, " << i->toHost << " would veto with '" <<
                            msg.String() << "'" << rsLog;
                    }
                    else {
                        log() << "not electing self, " << i->toHost << " would veto" << rsLog;
                    }
                    return false;
                }
            }
            else {
                DEV log() << "replSet freshest returns " << i->result.toString() << rsLog;
                allUp = false;
            }
        }
        LOG(1) << "replSet dev we are freshest of up nodes, nok:" << nok << " nTies:" << nTies << rsLog;
        verify( ord <= theReplSet->lastOpTimeWritten ); // <= as this may change while we are working...
        return true;
    }
Esempio n. 13
0
void DatabaseCloner::Stats::append(BSONObjBuilder* builder) const {
    builder->appendNumber("collections", collections);
    builder->appendNumber("clonedCollections", clonedCollections);
    if (start != Date_t()) {
        builder->appendDate("start", start);
        if (end != Date_t()) {
            builder->appendDate("end", end);
            auto elapsed = end - start;
            long long elapsedMillis = duration_cast<Milliseconds>(elapsed).count();
            builder->appendNumber("elapsedMillis", elapsedMillis);
        }
    }

    for (auto&& collection : collectionStats) {
        BSONObjBuilder collectionBuilder(builder->subobjStart(collection.ns));
        collection.append(&collectionBuilder);
        collectionBuilder.doneFast();
    }
}
Esempio n. 14
0
// File Sanity check
TEST(FTDCFileTest, TestFileBasicCompress) {
    unittest::TempDir tempdir("metrics_testpath");
    boost::filesystem::path p(tempdir.path());
    p /= kTestFile;

    deleteFileIfNeeded(p);

    BSONObj doc1 = BSON("name"
                        << "joe"
                        << "key1" << 34 << "key2" << 45);
    BSONObj doc2 = BSON("name"
                        << "joe"
                        << "key3" << 34 << "key5" << 45);

    FTDCConfig config;
    FTDCFileWriter writer(&config);

    ASSERT_OK(writer.open(p));

    ASSERT_OK(writer.writeSample(doc1, Date_t()));
    ASSERT_OK(writer.writeSample(doc2, Date_t()));

    writer.close();

    FTDCFileReader reader;
    ASSERT_OK(reader.open(p));

    ASSERT_OK(reader.hasNext());

    BSONObj doc1a = std::get<1>(reader.next());

    ASSERT_TRUE(doc1 == doc1a);

    ASSERT_OK(reader.hasNext());

    BSONObj doc2a = std::get<1>(reader.next());

    ASSERT_TRUE(doc2 == doc2a);

    auto sw = reader.hasNext();
    ASSERT_OK(sw);
    ASSERT_EQUALS(sw.getValue(), false);
}
Esempio n. 15
0
Status FTDCFileWriter::close() {
    if (_archiveStream.is_open()) {
        Status s = flush(boost::none, Date_t());

        _archiveStream.close();

        return s;
    }

    return Status::OK();
}
Esempio n. 16
0
 Date_t ReplicationExecutor::scheduleReadySleepers_inlock(const Date_t now) {
     WorkQueue::iterator iter = _sleepersQueue.begin();
     while ((iter != _sleepersQueue.end()) && (iter->readyDate <= now)) {
         ++iter;
     }
     _readyQueue.splice(_readyQueue.end(), _sleepersQueue, _sleepersQueue.begin(), iter);
     if (iter == _sleepersQueue.end()) {
         // indicate no sleeper to wait for
         return Date_t(~0ULL);
     }
     return iter->readyDate;
 }
void MemberHeartbeatData::setAuthIssue(Date_t now) {
    _health = 0;  // set health to 0 so that this doesn't count towards majority.
    _upSince = Date_t();
    _lastHeartbeat = now;
    _authIssue = true;

    _lastResponse = ReplSetHeartbeatResponse();
    _lastResponse.setState(MemberState::RS_UNKNOWN);
    _lastResponse.setElectionTime(Timestamp());
    _lastResponse.setOpTime(OpTime());
    _lastResponse.setHbMsg("");
    _lastResponse.setSyncingTo(HostAndPort());
}
void MemberHeartbeatData::setDownValues(Date_t now, const std::string& heartbeatMessage) {
    _health = 0;
    _upSince = Date_t();
    _lastHeartbeat = now;
    _authIssue = false;

    _lastResponse = ReplSetHeartbeatResponse();
    _lastResponse.setState(MemberState::RS_DOWN);
    _lastResponse.setElectionTime(Timestamp());
    _lastResponse.setOpTime(OpTime());
    _lastResponse.setHbMsg(heartbeatMessage);
    _lastResponse.setSyncingTo(HostAndPort());
}
    BSONObj generateSection(OperationContext* txn, const BSONElement& configElement) const {
        RangeDeleter* deleter = getDeleter();
        if (!deleter) {
            return BSONObj();
        }

        BSONObjBuilder result;

        OwnedPointerVector<DeleteJobStats> statsList;
        deleter->getStatsHistory(&statsList.mutableVector());
        BSONArrayBuilder oldStatsBuilder;
        for (OwnedPointerVector<DeleteJobStats>::const_iterator it = statsList.begin();
             it != statsList.end();
             ++it) {
            BSONObjBuilder entryBuilder;
            entryBuilder.append("deletedDocs", (*it)->deletedDocCount);

            if ((*it)->queueEndTS > Date_t()) {
                entryBuilder.append("queueStart", (*it)->queueStartTS);
                entryBuilder.append("queueEnd", (*it)->queueEndTS);
            }

            if ((*it)->deleteEndTS > Date_t()) {
                entryBuilder.append("deleteStart", (*it)->deleteStartTS);
                entryBuilder.append("deleteEnd", (*it)->deleteEndTS);

                if ((*it)->waitForReplEndTS > Date_t()) {
                    entryBuilder.append("waitForReplStart", (*it)->waitForReplStartTS);
                    entryBuilder.append("waitForReplEnd", (*it)->waitForReplEndTS);
                }
            }

            oldStatsBuilder.append(entryBuilder.obj());
        }
        result.append("lastDeleteStats", oldStatsBuilder.arr());

        return result.obj();
    }
Esempio n. 20
0
    bool SyncSourceFeedback::replHandshake(OperationContext* txn) {
        ReplicationCoordinator* replCoord = getGlobalReplicationCoordinator();
        if (replCoord->getCurrentMemberState().primary()) {
            // primary has no one to handshake to
            return true;
        }
        // construct a vector of handshake obj for us as well as all chained members
        std::vector<BSONObj> handshakeObjs;
        replCoord->prepareReplSetUpdatePositionCommandHandshakes(txn, &handshakeObjs);
        LOG(1) << "handshaking upstream updater";
        for (std::vector<BSONObj>::iterator it = handshakeObjs.begin();
                it != handshakeObjs.end();
                ++it) {
            BSONObj res;
            try {
                LOG(2) << "Sending to " << _connection.get()->toString() << " the replication "
                        "handshake: " << *it;
                if (!_connection->runCommand("admin", *it, res)) {
                    std::string errMsg = res["errmsg"].valuestrsafe();
                    massert(17447, "upstream updater is not supported by the member from which we"
                            " are syncing, please update all nodes to 2.6 or later.",
                            errMsg.find("no such cmd") == std::string::npos);

                    log() << "replSet error while handshaking the upstream updater: "
                        << errMsg;

                    // sleep half a second if we are not in our sync source's config
                    // TODO(dannenberg) after 2.8, remove the string comparison 
                    if (res["code"].numberInt() == ErrorCodes::NodeNotFound ||
                            errMsg.find("could not be found in replica set config while attempting "
                                        "to associate it with") != std::string::npos) {

                        // black list sync target for 10 seconds and find a new one
                        replCoord->blacklistSyncSource(_syncTarget,
                                                       Date_t(curTimeMillis64() + 10*1000));
                        BackgroundSync::get()->clearSyncTarget();
                    }

                    _resetConnection();
                    return false;
                }
            }
            catch (const DBException& e) {
                log() << "SyncSourceFeedback error sending handshake: " << e.what() << endl;
                _resetConnection();
                return false;
            }
        }
        return true;
    }
Esempio n. 21
0
 Status ReplicationCoordinatorImpl::processReplSetFreeze(int secs, BSONObjBuilder* resultObj) {
     Status result(ErrorCodes::InternalError, "didn't set status in prepareFreezeResponse");
     CBHStatus cbh = _replExecutor->scheduleWork(
         stdx::bind(&TopologyCoordinator::prepareFreezeResponse,
                    _topCoord.get(),
                    stdx::placeholders::_1,
                    Date_t(curTimeMillis64()),
                    secs,
                    resultObj,
                    &result));
     if (cbh.getStatus() == ErrorCodes::ShutdownInProgress) {
         return Status(ErrorCodes::ShutdownInProgress, "replication shutdown in progress");
     }
     fassert(18641, cbh.getStatus());
     _replExecutor->wait(cbh.getValue());
     return result;
 }
Esempio n. 22
0
 Status ReplicationCoordinatorImpl::processHeartbeat(const BSONObj& cmdObj, 
                                                     BSONObjBuilder* resultObj) {
     Status result(ErrorCodes::InternalError, "didn't set status in prepareHeartbeatResponse");
     StatusWith<ReplicationExecutor::CallbackHandle> cbh = _replExecutor->scheduleWork(
         stdx::bind(&TopologyCoordinator::prepareHeartbeatResponse,
                    _topCoord.get(),
                    stdx::placeholders::_1,
                    Date_t(curTimeMillis64()),
                    cmdObj,
                    resultObj,
                    &result));
     if (cbh.getStatus() == ErrorCodes::ShutdownInProgress) {
         return Status(ErrorCodes::ShutdownInProgress, "replication shutdown in progress");
     }
     fassert(18508, cbh.getStatus());
     _replExecutor->wait(cbh.getValue());
     return result;
 }
Esempio n. 23
0
 Status ReplicationCoordinatorImpl::processReplSetGetStatus(BSONObjBuilder* response) {
     Status result(ErrorCodes::InternalError, "didn't set status in prepareStatusResponse");
     CBHStatus cbh = _replExecutor->scheduleWork(
         stdx::bind(&TopologyCoordinator::prepareStatusResponse,
                    _topCoord.get(),
                    stdx::placeholders::_1,
                    Date_t(curTimeMillis64()),
                    time(0) - serverGlobalParams.started,
                    _getLastOpApplied(),
                    response,
                    &result));
     if (cbh.getStatus() == ErrorCodes::ShutdownInProgress) {
         return Status(ErrorCodes::ShutdownInProgress, "replication shutdown in progress");
     }
     fassert(18640, cbh.getStatus());
     _replExecutor->wait(cbh.getValue());
     return result;
 }
Esempio n. 24
0
 void Scope::append( BSONObjBuilder & builder , const char * fieldName , const char * scopeName ){
     int t = type( scopeName );
     
     switch ( t ){
     case Object:
         builder.append( fieldName , getObject( scopeName ) );
         break;
     case Array:
         builder.appendArray( fieldName , getObject( scopeName ) );
         break;
     case NumberDouble:
         builder.append( fieldName , getNumber( scopeName ) );
         break;
     case NumberInt:
         builder.append( fieldName , getNumberInt( scopeName ) );
         break;
     case NumberLong:
         builder.append( fieldName , getNumberLongLong( scopeName ) );
         break;
     case String:
         builder.append( fieldName , getString( scopeName ).c_str() );
         break;
     case Bool:
         builder.appendBool( fieldName , getBoolean( scopeName ) );
         break;
     case jstNULL:
     case Undefined:
         builder.appendNull( fieldName );
         break;
     case Date:
         // TODO: make signed
         builder.appendDate( fieldName , Date_t((unsigned long long)getNumber( scopeName )) );
         break;
     case Code:
         builder.appendCode( fieldName , getString( scopeName ) );
         break;
     default:
         stringstream temp;
         temp << "can't append type from:";
         temp << t;
         uassert( 10206 ,  temp.str() , 0 );
     }
     
 }
Esempio n. 25
0
void MemberData::setAuthIssue(Date_t now) {
    _health = 0;  // set health to 0 so that this doesn't count towards majority.
    _upSince = Date_t();
    _lastHeartbeat = now;
    _authIssue = true;
    _updatedSinceRestart = true;
    _lastHeartbeatMessage.clear();

    if (_lastResponse.getState() != MemberState::RS_UNKNOWN) {
        log() << "Member " << _hostAndPort.toString()
              << " is now in state RS_UNKNOWN due to authentication issue." << rsLog;
    }

    _lastResponse = ReplSetHeartbeatResponse();
    _lastResponse.setState(MemberState::RS_UNKNOWN);
    _lastResponse.setElectionTime(Timestamp());
    _lastResponse.setAppliedOpTimeAndWallTime(OpTimeAndWallTime());
    _lastResponse.setSyncingTo(HostAndPort());
}
Esempio n. 26
0
    StatusWith<ReplicationExecutor::CallbackHandle> ReplicationExecutor::enqueueWork_inlock(
            WorkQueue* queue, const CallbackFn& callback) {

        invariant(callback);
        StatusWith<EventHandle> event = makeEvent_inlock();
        if (!event.isOK())
            return StatusWith<CallbackHandle>(event.getStatus());

        if (_freeQueue.empty())
            _freeQueue.push_front(WorkItem());
        const WorkQueue::iterator iter = _freeQueue.begin();
        iter->generation++;
        iter->callback = callback;
        iter->finishedEvent = event.getValue();
        iter->readyDate = Date_t();
        iter->isCanceled = false;
        queue->splice(queue->end(), _freeQueue, iter);
        return StatusWith<CallbackHandle>(CallbackHandle(iter));
    }
Esempio n. 27
0
 Status ReplicationCoordinatorImpl::processHeartbeat(const ReplSetHeartbeatArgs& args,
                                                     ReplSetHeartbeatResponse* response) {
     Status result(ErrorCodes::InternalError, "didn't set status in prepareHeartbeatResponse");
     CBHStatus cbh = _replExecutor->scheduleWork(
         stdx::bind(&TopologyCoordinator::prepareHeartbeatResponse,
                    _topCoord.get(),
                    stdx::placeholders::_1,
                    Date_t(curTimeMillis64()),
                    args,
                    _settings.ourSetName(),
                    _getLastOpApplied(),
                    response,
                    &result));
     if (cbh.getStatus() == ErrorCodes::ShutdownInProgress) {
         return Status(ErrorCodes::ShutdownInProgress, "replication shutdown in progress");
     }
     fassert(18508, cbh.getStatus());
     _replExecutor->wait(cbh.getValue());
     return result;
 }
Esempio n. 28
0
void MemberData::setDownValues(Date_t now, const std::string& heartbeatMessage) {
    _health = 0;
    _upSince = Date_t();
    _lastHeartbeat = now;
    _authIssue = false;
    _updatedSinceRestart = true;
    _lastHeartbeatMessage = heartbeatMessage;

    if (_lastResponse.getState() != MemberState::RS_DOWN) {
        log() << "Member " << _hostAndPort.toString() << " is now in state RS_DOWN" << rsLog;
    }

    _lastResponse = ReplSetHeartbeatResponse();
    _lastResponse.setState(MemberState::RS_DOWN);
    _lastResponse.setElectionTime(Timestamp());
    _lastResponse.setAppliedOpTimeAndWallTime(OpTimeAndWallTime());
    _lastResponse.setSyncingTo(HostAndPort());

    // The _lastAppliedOpTime/_lastDurableOpTime fields don't get cleared merely by missing a
    // heartbeat.
}
Esempio n. 29
0
    StatusWith<boost::optional<std::tuple<ConstDataRange, FTDCCompressor::CompressorState, Date_t>>>
    addSample(const BSONObj& sample) {
        auto st = _compressor.addSample(sample, Date_t());

        if (!st.getValue().is_initialized()) {
            _docs.emplace_back(sample);
        } else if (std::get<1>(st.getValue().get()) ==
                   FTDCCompressor::CompressorState::kSchemaChanged) {
            validate(std::get<0>(st.getValue().get()));
            _docs.clear();
            _docs.emplace_back(sample);
        } else if (std::get<1>(st.getValue().get()) ==
                   FTDCCompressor::CompressorState::kCompressorFull) {
            _docs.emplace_back(sample);
            validate(std::get<0>(st.getValue().get()));
            _docs.clear();
        } else {
            MONGO_UNREACHABLE;
        }

        return st;
    }
Esempio n. 30
0
bool MemberData::setUpValues(Date_t now, ReplSetHeartbeatResponse&& hbResponse) {
    _health = 1;
    if (_upSince == Date_t()) {
        _upSince = now;
    }
    _authIssue = false;
    _lastHeartbeat = now;
    _lastUpdate = now;
    _lastUpdateStale = false;
    _updatedSinceRestart = true;
    _lastHeartbeatMessage.clear();

    if (!hbResponse.hasState()) {
        hbResponse.setState(MemberState::RS_UNKNOWN);
    }
    if (!hbResponse.hasElectionTime()) {
        hbResponse.setElectionTime(_lastResponse.getElectionTime());
    }
    if (!hbResponse.hasAppliedOpTime()) {
        hbResponse.setAppliedOpTimeAndWallTime(_lastResponse.getAppliedOpTimeAndWallTime());
    }
    // Log if the state changes
    if (_lastResponse.getState() != hbResponse.getState()) {
        log() << "Member " << _hostAndPort.toString() << " is now in state "
              << hbResponse.getState().toString() << rsLog;
    }

    bool opTimeAdvanced =
        advanceLastAppliedOpTimeAndWallTime(hbResponse.getAppliedOpTimeAndWallTime(), now);
    auto durableOpTimeAndWallTime = hbResponse.hasDurableOpTime()
        ? hbResponse.getDurableOpTimeAndWallTime()
        : OpTimeAndWallTime();
    opTimeAdvanced =
        advanceLastDurableOpTimeAndWallTime(durableOpTimeAndWallTime, now) || opTimeAdvanced;
    _lastResponse = std::move(hbResponse);
    return opTimeAdvanced;
}