本文整理汇总了C++中Status类的典型用法代码示例。如果您正苦于以下问题:C++ Status类的具体用法?C++ Status怎么用?C++ Status使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Status类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Status
Status CmdAuthenticate::_authenticateCR(OperationContext* opCtx,
const UserName& user,
const BSONObj& cmdObj) {
if (user == internalSecurity.user->getName() &&
serverGlobalParams.clusterAuthMode.load() == ServerGlobalParams::ClusterAuthMode_x509) {
return Status(ErrorCodes::AuthenticationFailed,
"Mechanism x509 is required for internal cluster authentication");
}
if (_isCRAuthDisabled) {
// SERVER-8461, MONGODB-CR must be enabled for authenticating the internal user, so that
// cluster members may communicate with each other.
if (user != internalSecurity.user->getName()) {
return Status(ErrorCodes::BadValue, _nonceAuthenticationDisabledMessage);
}
}
string key = cmdObj.getStringField("key");
string received_nonce = cmdObj.getStringField("nonce");
if (user.getUser().empty() || key.empty() || received_nonce.empty()) {
sleepmillis(10);
return Status(ErrorCodes::ProtocolError,
"field missing/wrong type in received authenticate command");
}
stringstream digestBuilder;
{
Client* client = Client::getCurrent();
std::unique_ptr<AuthenticationSession> session;
AuthenticationSession::swap(client, session);
if (!session || session->getType() != AuthenticationSession::SESSION_TYPE_MONGO) {
sleepmillis(30);
return Status(ErrorCodes::ProtocolError, "No pending nonce");
} else {
nonce64 nonce = static_cast<MongoAuthenticationSession*>(session.get())->getNonce();
digestBuilder << hex << nonce;
if (digestBuilder.str() != received_nonce) {
sleepmillis(30);
return Status(ErrorCodes::AuthenticationFailed, "Received wrong nonce.");
}
}
}
User* userObj;
Status status =
getGlobalAuthorizationManager()->acquireUserForInitialAuth(opCtx, user, &userObj);
if (!status.isOK()) {
// Failure to find the privilege document indicates no-such-user, a fact that we do not
// wish to reveal to the client. So, we return AuthenticationFailed rather than passing
// through the returned status.
return Status(ErrorCodes::AuthenticationFailed, status.toString());
}
string pwd = userObj->getCredentials().password;
getGlobalAuthorizationManager()->releaseUser(userObj);
if (pwd.empty()) {
return Status(ErrorCodes::AuthenticationFailed,
"MONGODB-CR credentials missing in the user document");
}
md5digest d;
{
digestBuilder << user.getUser() << pwd;
string done = digestBuilder.str();
md5_state_t st;
md5_init(&st);
md5_append(&st, (const md5_byte_t*)done.c_str(), done.size());
md5_finish(&st, d);
}
string computed = digestToString(d);
if (key != computed) {
return Status(ErrorCodes::AuthenticationFailed, "key mismatch");
}
AuthorizationSession* authorizationSession = AuthorizationSession::get(Client::getCurrent());
status = authorizationSession->addAndAuthorizeUser(opCtx, user);
if (!status.isOK()) {
return status;
}
return Status::OK();
}
示例2: invariant
Status ShardingCatalogClientImpl::applyChunkOpsDeprecated(OperationContext* opCtx,
const BSONArray& updateOps,
const BSONArray& preCondition,
const NamespaceString& nss,
const ChunkVersion& lastChunkVersion,
const WriteConcernOptions& writeConcern,
repl::ReadConcernLevel readConcern) {
invariant(serverGlobalParams.clusterRole == ClusterRole::ConfigServer ||
(readConcern == repl::ReadConcernLevel::kMajorityReadConcern &&
writeConcern.wMode == WriteConcernOptions::kMajority));
BSONObj cmd = BSON("applyOps" << updateOps << "preCondition" << preCondition
<< WriteConcernOptions::kWriteConcernField
<< writeConcern.toBSON());
auto response =
Grid::get(opCtx)->shardRegistry()->getConfigShard()->runCommandWithFixedRetryAttempts(
opCtx,
ReadPreferenceSetting{ReadPreference::PrimaryOnly},
"config",
cmd,
Shard::RetryPolicy::kIdempotent);
if (!response.isOK()) {
return response.getStatus();
}
Status status = response.getValue().commandStatus.isOK()
? std::move(response.getValue().writeConcernStatus)
: std::move(response.getValue().commandStatus);
// TODO (Dianna) This fail point needs to be reexamined when CommitChunkMigration is in:
// migrations will no longer be able to exercise it, so split or merge will need to do so.
// SERVER-22659.
if (MONGO_FAIL_POINT(failApplyChunkOps)) {
status = Status(ErrorCodes::InternalError, "Failpoint 'failApplyChunkOps' generated error");
}
if (!status.isOK()) {
string errMsg;
// This could be a blip in the network connectivity. Check if the commit request made it.
//
// If all the updates were successfully written to the chunks collection, the last
// document in the list of updates should be returned from a query to the chunks
// collection. The last chunk can be identified by namespace and version number.
warning() << "chunk operation commit failed and metadata will be revalidated"
<< causedBy(redact(status));
// Look for the chunk in this shard whose version got bumped. We assume that if that
// mod made it to the config server, then transaction was successful.
BSONObjBuilder query;
lastChunkVersion.appendLegacyWithField(&query, ChunkType::lastmod());
query.append(ChunkType::ns(), nss.ns());
auto chunkWithStatus = getChunks(opCtx, query.obj(), BSONObj(), 1, nullptr, readConcern);
if (!chunkWithStatus.isOK()) {
errMsg = str::stream()
<< "getChunks function failed, unable to validate chunk "
<< "operation metadata: " << chunkWithStatus.getStatus().toString()
<< ". applyChunkOpsDeprecated failed to get confirmation "
<< "of commit. Unable to save chunk ops. Command: " << cmd
<< ". Result: " << response.getValue().response;
return status.withContext(errMsg);
};
const auto& newestChunk = chunkWithStatus.getValue();
if (newestChunk.empty()) {
errMsg = str::stream() << "chunk operation commit failed: version "
<< lastChunkVersion.toString()
<< " doesn't exist in namespace: " << nss.ns()
<< ". Unable to save chunk ops. Command: " << cmd
<< ". Result: " << response.getValue().response;
return status.withContext(errMsg);
};
invariant(newestChunk.size() == 1);
return Status::OK();
}
return Status::OK();
}
示例3: repairDatabasesAndCheckVersion
// ran at startup.
static void repairDatabasesAndCheckVersion(bool shouldClearNonLocalTmpCollections) {
LOG(1) << "enter repairDatabases (to check pdfile version #)" << endl;
OperationContextImpl txn;
Lock::GlobalWrite lk(txn.lockState());
WriteUnitOfWork wunit(txn.recoveryUnit());
vector< string > dbNames;
globalStorageEngine->listDatabases( &dbNames );
for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) {
string dbName = *i;
LOG(1) << "\t" << dbName << endl;
Client::Context ctx(&txn, dbName );
if (repl::replSettings.usingReplSets()) {
// we only care about the _id index if we are in a replset
checkForIdIndexes(&txn, ctx.db());
}
if (shouldClearNonLocalTmpCollections || dbName == "local")
ctx.db()->clearTmpCollections(&txn);
if ( mongodGlobalParams.repair ) {
fassert(18506, globalStorageEngine->repairDatabase(&txn, dbName));
}
else if (!ctx.db()->getDatabaseCatalogEntry()->currentFilesCompatible(&txn)) {
log() << "****";
log() << "cannot do this upgrade without an upgrade in the middle";
log() << "please do a --repair with 2.6 and then start this version";
dbexit( EXIT_NEED_UPGRADE );
invariant( false );
return;
}
else {
// major versions match, check indexes
const string systemIndexes = ctx.db()->name() + ".system.indexes";
Collection* coll = ctx.db()->getCollection( &txn, systemIndexes );
auto_ptr<Runner> runner(InternalPlanner::collectionScan(&txn, systemIndexes,coll));
BSONObj index;
Runner::RunnerState state;
while (Runner::RUNNER_ADVANCED == (state = runner->getNext(&index, NULL))) {
const BSONObj key = index.getObjectField("key");
const string plugin = IndexNames::findPluginName(key);
if (ctx.db()->getDatabaseCatalogEntry()->isOlderThan24(&txn)) {
if (IndexNames::existedBefore24(plugin))
continue;
log() << "Index " << index << " claims to be of type '" << plugin << "', "
<< "which is either invalid or did not exist before v2.4. "
<< "See the upgrade section: "
<< "http://dochub.mongodb.org/core/upgrade-2.4"
<< startupWarningsLog;
}
const Status keyStatus = validateKeyPattern(key);
if (!keyStatus.isOK()) {
log() << "Problem with index " << index << ": " << keyStatus.reason()
<< " This index can still be used however it cannot be rebuilt."
<< " For more info see"
<< " http://dochub.mongodb.org/core/index-validation"
<< startupWarningsLog;
}
}
if (Runner::RUNNER_EOF != state) {
warning() << "Internal error while reading collection " << systemIndexes;
}
Database::closeDatabase(&txn, dbName.c_str());
}
}
wunit.commit();
LOG(1) << "done repairDatabases" << endl;
}
示例4: whyMessage
StatusWith<ForwardingCatalogManager::ScopedDistLock*>
ChunkMoveOperationState::acquireMoveMetadata() {
// Get the distributed lock
const string whyMessage(stream() << "migrating chunk [" << _minKey << ", " << _maxKey << ") in "
<< _nss.ns());
_distLockStatus = grid.forwardingCatalogManager()->distLock(_txn, _nss.ns(), whyMessage);
if (!_distLockStatus->isOK()) {
const string msg = stream() << "could not acquire collection lock for " << _nss.ns()
<< " to migrate chunk [" << _minKey << "," << _maxKey << ")"
<< causedBy(_distLockStatus->getStatus());
warning() << msg;
return Status(_distLockStatus->getStatus().code(), msg);
}
ShardingState* const shardingState = ShardingState::get(_txn);
// Snapshot the metadata
Status refreshStatus = shardingState->refreshMetadataNow(_txn, _nss.ns(), &_shardVersion);
if (!refreshStatus.isOK()) {
const string msg = stream() << "moveChunk cannot start migrate of chunk "
<< "[" << _minKey << "," << _maxKey << ")"
<< causedBy(refreshStatus.reason());
warning() << msg;
return Status(refreshStatus.code(), msg);
}
if (_shardVersion.majorVersion() == 0) {
// It makes no sense to migrate if our version is zero and we have no chunks
const string msg = stream() << "moveChunk cannot start migrate of chunk "
<< "[" << _minKey << "," << _maxKey << ")"
<< " with zero shard version";
warning() << msg;
return Status(ErrorCodes::IncompatibleShardingMetadata, msg);
}
{
// Mongos >= v3.2 sends the full version, v3.0 only sends the epoch.
// TODO(SERVER-20742): Stop parsing epoch separately after 3.2.
auto& operationVersion = OperationShardVersion::get(_txn);
if (operationVersion.hasShardVersion()) {
_collectionVersion = operationVersion.getShardVersion(_nss);
_collectionEpoch = _collectionVersion.epoch();
} // else the epoch will already be set from the parsing of the ChunkMoveOperationState
if (_collectionEpoch != _shardVersion.epoch()) {
const string msg = stream() << "moveChunk cannot move chunk "
<< "[" << _minKey << "," << _maxKey << "), "
<< "collection may have been dropped. "
<< "current epoch: " << _shardVersion.epoch()
<< ", cmd epoch: " << _collectionEpoch;
warning() << msg;
throw SendStaleConfigException(_nss.toString(), msg, _collectionVersion, _shardVersion);
}
}
_collMetadata = shardingState->getCollectionMetadata(_nss.ns());
// With nonzero shard version, we must have a coll version >= our shard version
invariant(_collMetadata->getCollVersion() >= _shardVersion);
// With nonzero shard version, we must have a shard key
invariant(!_collMetadata->getKeyPattern().isEmpty());
ChunkType origChunk;
if (!_collMetadata->getNextChunk(_minKey, &origChunk) ||
origChunk.getMin().woCompare(_minKey) || origChunk.getMax().woCompare(_maxKey)) {
// Our boundaries are different from those passed in
const string msg = stream() << "moveChunk cannot find chunk "
<< "[" << _minKey << "," << _maxKey << ")"
<< " to migrate, the chunk boundaries may be stale";
warning() << msg;
throw SendStaleConfigException(_nss.toString(), msg, _collectionVersion, _shardVersion);
}
return &_distLockStatus->getValue();
}
示例5: catch
StatusWith<bool> SaslPLAINServerConversation::step(const StringData& inputData,
std::string* outputData) {
// Expecting user input on the form: user\0user\0pwd
std::string input = inputData.toString();
std::string pwd = "";
try {
_user = input.substr(0, inputData.find('\0'));
pwd = input.substr(inputData.find('\0', _user.size()+1)+1);
}
catch (std::out_of_range& exception) {
return StatusWith<bool>(ErrorCodes::AuthenticationFailed,
mongoutils::str::stream() << "Incorrectly formatted PLAIN client message");
}
User* userObj;
// The authentication database is also the source database for the user.
Status status = _saslAuthSession->getAuthorizationSession()->getAuthorizationManager().
acquireUser(_saslAuthSession->getOpCtxt(),
UserName(_user, _saslAuthSession->getAuthenticationDatabase()),
&userObj);
if (!status.isOK()) {
return StatusWith<bool>(status);
}
const User::CredentialData creds = userObj->getCredentials();
_saslAuthSession->getAuthorizationSession()->getAuthorizationManager().
releaseUser(userObj);
std::string authDigest = createPasswordDigest(_user, pwd);
if (!creds.password.empty()) {
// Handle schemaVersion26Final (MONGODB-CR/SCRAM mixed mode)
if (authDigest != creds.password) {
return StatusWith<bool>(ErrorCodes::AuthenticationFailed,
mongoutils::str::stream() << "Incorrect user name or password");
}
}
else {
// Handle schemaVersion28SCRAM (SCRAM only mode)
unsigned char storedKey[scram::hashSize];
unsigned char serverKey[scram::hashSize];
scram::generateSecrets(authDigest,
reinterpret_cast<const unsigned char*>(base64::decode(creds.scram.salt).c_str()),
16,
creds.scram.iterationCount,
storedKey,
serverKey);
if (creds.scram.storedKey != base64::encode(reinterpret_cast<const char*>(storedKey),
scram::hashSize)) {
return StatusWith<bool>(ErrorCodes::AuthenticationFailed,
mongoutils::str::stream() << "Incorrect user name or password");
}
}
*outputData = "";
return StatusWith<bool>(true);
}
示例6: bsonExtractStringField
StatusWith<LocksType> LocksType::fromBSON(const BSONObj& source) {
LocksType lock;
{
std::string lockName;
Status status = bsonExtractStringField(source, name.name(), &lockName);
if (!status.isOK())
return status;
lock._name = lockName;
}
{
long long lockStateInt;
Status status = bsonExtractIntegerField(source, state.name(), &lockStateInt);
if (!status.isOK())
return status;
lock._state = static_cast<State>(lockStateInt);
}
if (source.hasField(process.name())) {
std::string lockProcess;
Status status = bsonExtractStringField(source, process.name(), &lockProcess);
if (!status.isOK())
return status;
lock._process = lockProcess;
}
if (source.hasField(lockID.name())) {
BSONElement lockIDElem;
Status status = bsonExtractTypedField(source, lockID.name(), BSONType::jstOID, &lockIDElem);
if (!status.isOK())
return status;
lock._lockID = lockIDElem.OID();
}
if (source.hasField(who.name())) {
std::string lockWho;
Status status = bsonExtractStringField(source, who.name(), &lockWho);
if (!status.isOK())
return status;
lock._who = lockWho;
}
if (source.hasField(why.name())) {
std::string lockWhy;
Status status = bsonExtractStringField(source, why.name(), &lockWhy);
if (!status.isOK())
return status;
lock._why = lockWhy;
}
return lock;
}
示例7: parseCreateOrUpdateUserCommands
Status parseCreateOrUpdateUserCommands(const BSONObj& cmdObj,
const StringData& cmdName,
const std::string& dbname,
CreateOrUpdateUserArgs* parsedArgs) {
unordered_set<std::string> validFieldNames;
validFieldNames.insert(cmdName.toString());
validFieldNames.insert("customData");
validFieldNames.insert("pwd");
validFieldNames.insert("roles");
validFieldNames.insert("writeConcern");
Status status = _checkNoExtraFields(cmdObj, cmdName, validFieldNames);
if (!status.isOK()) {
return status;
}
status = _extractWriteConcern(cmdObj, &parsedArgs->writeConcern);
if (!status.isOK()) {
return status;
}
BSONObjBuilder userObjBuilder;
// Parse user name
std::string userName;
status = bsonExtractStringField(cmdObj, cmdName, &userName);
if (!status.isOK()) {
return status;
}
parsedArgs->userName = UserName(userName, dbname);
// Parse password
if (cmdObj.hasField("pwd")) {
std::string clearTextPassword;
status = bsonExtractStringField(cmdObj, "pwd", &clearTextPassword);
if (!status.isOK()) {
return status;
}
parsedArgs->hashedPassword = auth::createPasswordDigest(userName, clearTextPassword);
parsedArgs->hasHashedPassword = true;
}
// Parse custom data
if (cmdObj.hasField("customData")) {
BSONElement element;
status = bsonExtractTypedField(cmdObj, "customData", Object, &element);
if (!status.isOK()) {
return status;
}
parsedArgs->customData = element.Obj();
parsedArgs->hasCustomData = true;
}
// Parse roles
if (cmdObj.hasField("roles")) {
BSONElement rolesElement;
status = bsonExtractTypedField(cmdObj, "roles", Array, &rolesElement);
if (!status.isOK()) {
return status;
}
status = _extractRoleDataFromBSONArray(rolesElement, dbname, &parsedArgs->roles);
if (!status.isOK()) {
return status;
}
parsedArgs->hasRoles = true;
}
return Status::OK();
}
示例8: wrappedRun
bool wrappedRun(OperationContext* txn,
const string& dbname,
BSONObj& jsobj,
string& errmsg,
BSONObjBuilder& anObjBuilder) {
BSONElement e = jsobj.firstElement();
const string toDeleteNs = dbname + '.' + e.valuestr();
if (!serverGlobalParams.quiet) {
LOG(0) << "CMD: dropIndexes " << toDeleteNs << endl;
}
Client::Context ctx(toDeleteNs);
Database* db = ctx.db();
Collection* collection = db->getCollection( txn, toDeleteNs );
if ( ! collection ) {
errmsg = "ns not found";
return false;
}
stopIndexBuilds(txn, db, jsobj);
IndexCatalog* indexCatalog = collection->getIndexCatalog();
anObjBuilder.appendNumber("nIndexesWas", indexCatalog->numIndexesTotal() );
BSONElement f = jsobj.getField("index");
if ( f.type() == String ) {
string indexToDelete = f.valuestr();
if ( indexToDelete == "*" ) {
Status s = indexCatalog->dropAllIndexes(txn, false);
if ( !s.isOK() ) {
appendCommandStatus( anObjBuilder, s );
return false;
}
anObjBuilder.append("msg", "non-_id indexes dropped for collection");
return true;
}
IndexDescriptor* desc = collection->getIndexCatalog()->findIndexByName( indexToDelete );
if ( desc == NULL ) {
errmsg = str::stream() << "index not found with name [" << indexToDelete << "]";
return false;
}
if ( desc->isIdIndex() ) {
errmsg = "cannot drop _id index";
return false;
}
Status s = indexCatalog->dropIndex(txn, desc);
if ( !s.isOK() ) {
appendCommandStatus( anObjBuilder, s );
return false;
}
return true;
}
if ( f.type() == Object ) {
IndexDescriptor* desc = collection->getIndexCatalog()->findIndexByKeyPattern( f.embeddedObject() );
if ( desc == NULL ) {
errmsg = "can't find index with key:";
errmsg += f.embeddedObject().toString();
return false;
}
if ( desc->isIdIndex() ) {
errmsg = "cannot drop _id index";
return false;
}
Status s = indexCatalog->dropIndex(txn, desc);
if ( !s.isOK() ) {
appendCommandStatus( anObjBuilder, s );
return false;
}
return true;
}
errmsg = "invalid index name spec";
return false;
}
示例9: run
bool run(OperationContext* txn, const string& dbname , BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) {
DBDirectClient db;
BSONElement e = jsobj.firstElement();
string toDeleteNs = dbname + '.' + e.valuestr();
LOG(0) << "CMD: reIndex " << toDeleteNs << endl;
Lock::DBWrite dbXLock(txn->lockState(), dbname);
Client::Context ctx(toDeleteNs);
Collection* collection = ctx.db()->getCollection( txn, toDeleteNs );
if ( !collection ) {
errmsg = "ns not found";
return false;
}
BackgroundOperation::assertNoBgOpInProgForNs( toDeleteNs );
std::vector<BSONObj> indexesInProg = stopIndexBuilds(txn, ctx.db(), jsobj);
list<BSONObj> all;
auto_ptr<DBClientCursor> i = db.query( dbname + ".system.indexes" , BSON( "ns" << toDeleteNs ) , 0 , 0 , 0 , QueryOption_SlaveOk );
BSONObjBuilder b;
while ( i->more() ) {
const BSONObj spec = i->next().removeField("v").getOwned();
const BSONObj key = spec.getObjectField("key");
const Status keyStatus = validateKeyPattern(key);
if (!keyStatus.isOK()) {
errmsg = str::stream()
<< "Cannot rebuild index " << spec << ": " << keyStatus.reason()
<< " For more info see http://dochub.mongodb.org/core/index-validation";
return false;
}
b.append( BSONObjBuilder::numStr( all.size() ) , spec );
all.push_back( spec );
}
result.appendNumber( "nIndexesWas", collection->getIndexCatalog()->numIndexesTotal() );
Status s = collection->getIndexCatalog()->dropAllIndexes(txn, true);
if ( !s.isOK() ) {
errmsg = "dropIndexes failed";
return appendCommandStatus( result, s );
}
for ( list<BSONObj>::iterator i=all.begin(); i!=all.end(); i++ ) {
BSONObj o = *i;
LOG(1) << "reIndex ns: " << toDeleteNs << " index: " << o << endl;
Status s = collection->getIndexCatalog()->createIndex(txn, o, false);
if ( !s.isOK() )
return appendCommandStatus( result, s );
}
result.append( "nIndexes" , (int)all.size() );
result.appendArray( "indexes" , b.obj() );
IndexBuilder::restoreIndexes(indexesInProg);
return true;
}
示例10: invariant
Status MetadataLoader::initChunks(CatalogManager* catalogManager,
const string& ns,
const string& shard,
const CollectionMetadata* oldMetadata,
CollectionMetadata* metadata) const
{
map<string, ChunkVersion> versionMap;
// Preserve the epoch
versionMap[shard] = metadata->_shardVersion;
OID epoch = metadata->getCollVersion().epoch();
bool fullReload = true;
// Check to see if we should use the old version or not.
if ( oldMetadata ) {
// If our epochs are compatible, it's useful to use the old metadata for diffs
if ( oldMetadata->getCollVersion().hasEqualEpoch( epoch ) ) {
fullReload = false;
invariant( oldMetadata->isValid() );
versionMap[shard] = oldMetadata->_shardVersion;
metadata->_collVersion = oldMetadata->_collVersion;
// TODO: This could be made more efficient if copying not required, but
// not as frequently reloaded as in mongos.
metadata->_chunksMap = oldMetadata->_chunksMap;
LOG( 2 ) << "loading new chunks for collection " << ns
<< " using old metadata w/ version " << oldMetadata->getShardVersion()
<< " and " << metadata->_chunksMap.size() << " chunks" << endl;
}
else {
warning() << "reloading collection metadata for " << ns << " with new epoch "
<< epoch.toString() << ", the current epoch is "
<< oldMetadata->getCollVersion().epoch().toString() << endl;
}
}
// Exposes the new metadata's range map and version to the "differ," who
// would ultimately be responsible of filling them up.
SCMConfigDiffTracker differ( shard );
differ.attach( ns, metadata->_chunksMap, metadata->_collVersion, versionMap );
try {
std::vector<ChunkType> chunks;
Status status = catalogManager->getChunks(differ.configDiffQuery(),
&chunks);
if (!status.isOK()) {
if (status == ErrorCodes::HostUnreachable) {
// Make our metadata invalid
metadata->_collVersion = ChunkVersion( 0, 0, OID() );
metadata->_chunksMap.clear();
}
return status;
}
//
// The diff tracker should always find at least one chunk (the highest chunk we saw
// last time). If not, something has changed on the config server (potentially between
// when we read the collection data and when we read the chunks data).
//
int diffsApplied = differ.calculateConfigDiff(chunks);
if ( diffsApplied > 0 ) {
// Chunks found, return ok
LOG(2) << "loaded " << diffsApplied << " chunks into new metadata for " << ns
<< " with version " << metadata->_collVersion << endl;
metadata->_shardVersion = versionMap[shard];
metadata->fillRanges();
invariant( metadata->isValid() );
return Status::OK();
}
else if ( diffsApplied == 0 ) {
// No chunks found, the collection is dropping or we're confused
// If this is a full reload, assume it is a drop for backwards compatibility
// TODO: drop the config.collections entry *before* the chunks and eliminate this
// ambiguity
string errMsg =
str::stream() << "no chunks found when reloading " << ns
<< ", previous version was "
<< metadata->_collVersion.toString()
<< ( fullReload ? ", this is a drop" : "" );
warning() << errMsg << endl;
metadata->_collVersion = ChunkVersion( 0, 0, OID() );
metadata->_chunksMap.clear();
return fullReload ? Status( ErrorCodes::NamespaceNotFound, errMsg ) :
Status( ErrorCodes::RemoteChangeDetected, errMsg );
}
//.........这里部分代码省略.........
示例11: acquireSubscription
// Construct a subscription if needed
// =============================================================================================
Status SubscriptionFactory::acquireSubscription(
const SubscriptionSettings& subscriptionSettings,
Subscription*& subscription)
{
logger_->debug("Acquiring subscription with the following settings:");
logger_->debug(subscriptionSettings.toString());
Status ret;
subscription = 0;
// lock the mutex to make sure the subscriptionMap_ is not being manipulated
UaMutexLocker locker(&subscriptionMapMutex_);
// we'll try to find a similar subscription that can be re-used,
// unless we're creating an "unique" subscription
// (one that is only created for -and used by- the current request)
if (subscriptionSettings.unique)
{
logger_->debug("The requested subscription must be unique");
}
else
{
// loop trough the subscriptions ...
for (SubscriptionMap::const_iterator it = subscriptionMap_.begin();
it != subscriptionMap_.end();
++it)
{
// ... until a suitable one is found
if (it->second->subscriptionSettings() == subscriptionSettings)
{
subscription = it->second;
logger_->debug("A suitable subscription (ClientSubscriptionHandle=%d) already exists",
subscription->clientSubscriptionHandle());
// get the ClientSubscriptionHandle of the subscription
ClientSubscriptionHandle handle = subscription->clientSubscriptionHandle();
// now increment the activity count of the subscription
activityMapMutex_.lock();
activityMap_[handle] = activityMap_[handle] + 1;
activityMapMutex_.unlock();
ret.setGood();
break;
}
}
}
// if no subscription exists yet, we create one
if (subscription == 0)
{
ClientSubscriptionHandle clientSubscriptionHandle;
clientSubscriptionHandle = database_->createUniqueClientSubscriptionHandle();
logger_->debug("We create a new subscription with clientSubscriptionHandle %d",
clientSubscriptionHandle);
// create a new subscription instance
subscription = new Subscription(
logger_->loggerFactory(),
subscriptionSettings,
clientSubscriptionHandle,
clientConnectionId_,
uaSession_,
this,
clientInterface_,
database_);
// store the new subscription instance in the subscriptionMap
subscriptionMap_[clientSubscriptionHandle] = subscription;
logger_->debug("The new subscription has been created");
// create an activity count for the subscription
activityMapMutex_.lock();
activityMap_[clientSubscriptionHandle] = 1;
activityMapMutex_.unlock();
// create the subscription on the server
ret = subscription->createSubscription();
}
// 'subscription' now points to an existing Subscription instance
// (i.e. a valid memory location)
ret.setGood();
// add some diagnostics
if (ret.isGood())
{
activityMapMutex_.lock();
logger_->debug("The requested subscription is acquired (#activities: %d)",
activityMap_[subscription->clientSubscriptionHandle()]);
activityMapMutex_.unlock();
}
//.........这里部分代码省略.........
示例12: repairDatabasesAndCheckVersion
// ran at startup.
static void repairDatabasesAndCheckVersion(bool shouldClearNonLocalTmpCollections) {
// LastError * le = lastError.get( true );
LOG(1) << "enter repairDatabases (to check pdfile version #)" << endl;
Lock::GlobalWrite lk;
DurTransaction txn;
vector< string > dbNames;
getDatabaseNames( dbNames );
for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) {
string dbName = *i;
LOG(1) << "\t" << dbName << endl;
Client::Context ctx( dbName );
DataFile *p = ctx.db()->getExtentManager().getFile( 0 );
DataFileHeader *h = p->getHeader();
if ( replSettings.usingReplSets() ) {
// we only care about the _id index if we are in a replset
checkForIdIndexes(ctx.db());
}
if (shouldClearNonLocalTmpCollections || dbName == "local")
ctx.db()->clearTmpCollections(&txn);
if (!h->isCurrentVersion() || mongodGlobalParams.repair) {
if( h->version <= 0 ) {
uasserted(14026,
str::stream() << "db " << dbName << " appears corrupt pdfile version: " << h->version
<< " info: " << h->versionMinor << ' ' << h->fileLength);
}
if ( !h->isCurrentVersion() ) {
log() << "****" << endl;
log() << "****" << endl;
log() << "need to upgrade database " << dbName << " "
<< "with pdfile version " << h->version << "." << h->versionMinor << ", "
<< "new version: "
<< PDFILE_VERSION << "." << PDFILE_VERSION_MINOR_22_AND_OLDER
<< endl;
}
if (mongodGlobalParams.upgrade) {
// QUESTION: Repair even if file format is higher version than code?
doDBUpgrade( dbName, h );
}
else {
log() << "\t Not upgrading, exiting" << endl;
log() << "\t run --upgrade to upgrade dbs, then start again" << endl;
log() << "****" << endl;
dbexit( EXIT_NEED_UPGRADE );
mongodGlobalParams.upgrade = 1;
return;
}
}
else {
const string systemIndexes = ctx.db()->name() + ".system.indexes";
Collection* coll = ctx.db()->getCollection( systemIndexes );
auto_ptr<Runner> runner(InternalPlanner::collectionScan(systemIndexes,coll));
BSONObj index;
Runner::RunnerState state;
while (Runner::RUNNER_ADVANCED == (state = runner->getNext(&index, NULL))) {
const BSONObj key = index.getObjectField("key");
const string plugin = IndexNames::findPluginName(key);
if (h->versionMinor == PDFILE_VERSION_MINOR_22_AND_OLDER) {
if (IndexNames::existedBefore24(plugin))
continue;
log() << "Index " << index << " claims to be of type '" << plugin << "', "
<< "which is either invalid or did not exist before v2.4. "
<< "See the upgrade section: "
<< "http://dochub.mongodb.org/core/upgrade-2.4"
<< startupWarningsLog;
}
const Status keyStatus = validateKeyPattern(key);
if (!keyStatus.isOK()) {
log() << "Problem with index " << index << ": " << keyStatus.reason()
<< " This index can still be used however it cannot be rebuilt."
<< " For more info see"
<< " http://dochub.mongodb.org/core/index-validation"
<< startupWarningsLog;
}
}
if (Runner::RUNNER_EOF != state) {
warning() << "Internal error while reading collection " << systemIndexes;
}
Database::closeDatabase(dbName.c_str(), storageGlobalParams.dbpath);
}
}
LOG(1) << "done repairDatabases" << endl;
if (mongodGlobalParams.upgrade) {
log() << "finished checking dbs" << endl;
cc().shutdown();
//.........这里部分代码省略.........
示例13: fixDocumentForInsert
StatusWith<BSONObj> fixDocumentForInsert( const BSONObj& doc ) {
if ( doc.objsize() > BSONObjMaxUserSize )
return StatusWith<BSONObj>( ErrorCodes::BadValue,
str::stream()
<< "object to insert too large"
<< doc.objsize() );
bool firstElementIsId = doc.firstElement().fieldNameStringData() == "_id";
bool hasTimestampToFix = false;
{
BSONObjIterator i( doc );
while ( i.more() ) {
BSONElement e = i.next();
if ( e.type() == Timestamp && e.timestampValue() == 0 ) {
// we replace Timestamp(0,0) at the top level with a correct value
// in the fast pass, we just mark that we want to swap
hasTimestampToFix = true;
break;
}
const char* fieldName = e.fieldName();
if ( fieldName[0] == '$' ) {
return StatusWith<BSONObj>( ErrorCodes::BadValue,
str::stream()
<< "Document can't have $ prefixed field names: "
<< e.fieldName() );
}
// check no regexp for _id (SERVER-9502)
// also, disallow undefined and arrays
if ( str::equals( fieldName, "_id") ) {
if ( e.type() == RegEx ) {
return StatusWith<BSONObj>( ErrorCodes::BadValue,
"can't use a regex for _id" );
}
if ( e.type() == Undefined ) {
return StatusWith<BSONObj>( ErrorCodes::BadValue,
"can't use a undefined for _id" );
}
if ( e.type() == Array ) {
return StatusWith<BSONObj>( ErrorCodes::BadValue,
"can't use an array for _id" );
}
if ( e.type() == Object ) {
BSONObj o = e.Obj();
Status s = o.storageValidEmbedded();
if ( !s.isOK() )
return StatusWith<BSONObj>( s );
}
}
}
}
if ( firstElementIsId && !hasTimestampToFix )
return StatusWith<BSONObj>( BSONObj() );
bool hadId = firstElementIsId;
BSONObjIterator i( doc );
BSONObjBuilder b( doc.objsize() + 16 );
if ( firstElementIsId ) {
b.append( doc.firstElement() );
i.next();
}
else {
BSONElement e = doc["_id"];
if ( e.type() ) {
b.append( e );
hadId = true;
}
else {
b.appendOID( "_id", NULL, true );
}
}
while ( i.more() ) {
BSONElement e = i.next();
if ( hadId && e.fieldNameStringData() == "_id" ) {
// no-op
}
else if ( e.type() == Timestamp && e.timestampValue() == 0 ) {
mutex::scoped_lock lk(OpTime::m);
b.append( e.fieldName(), OpTime::now(lk) );
}
else {
b.append( e );
}
}
return StatusWith<BSONObj>( b.obj() );
}
示例14: compactCollection
StatusWith<CompactStats> compactCollection(OperationContext* opCtx,
Collection* collection,
const CompactOptions* compactOptions) {
dassert(opCtx->lockState()->isCollectionLockedForMode(collection->ns(), MODE_X));
DisableDocumentValidation validationDisabler(opCtx);
auto recordStore = collection->getRecordStore();
auto indexCatalog = collection->getIndexCatalog();
if (!recordStore->compactSupported())
return StatusWith<CompactStats>(ErrorCodes::CommandNotSupported,
str::stream()
<< "cannot compact collection with record store: "
<< recordStore->name());
if (recordStore->compactsInPlace()) {
CompactStats stats;
Status status = recordStore->compact(opCtx);
if (!status.isOK())
return StatusWith<CompactStats>(status);
// Compact all indexes (not including unfinished indexes)
status = indexCatalog->compactIndexes(opCtx);
if (!status.isOK())
return StatusWith<CompactStats>(status);
return StatusWith<CompactStats>(stats);
}
if (indexCatalog->numIndexesInProgress(opCtx))
return StatusWith<CompactStats>(ErrorCodes::BadValue,
"cannot compact when indexes in progress");
std::vector<BSONObj> indexSpecs;
{
std::unique_ptr<IndexCatalog::IndexIterator> ii(
indexCatalog->getIndexIterator(opCtx, false));
while (ii->more()) {
const IndexDescriptor* descriptor = ii->next()->descriptor();
// Compact always creates the new index in the foreground.
const BSONObj spec =
descriptor->infoObj().removeField(IndexDescriptor::kBackgroundFieldName);
const BSONObj key = spec.getObjectField("key");
const Status keyStatus =
index_key_validate::validateKeyPattern(key, descriptor->version());
if (!keyStatus.isOK()) {
return StatusWith<CompactStats>(
ErrorCodes::CannotCreateIndex,
str::stream() << "Cannot compact collection due to invalid index " << spec
<< ": "
<< keyStatus.reason()
<< " For more info see"
<< " http://dochub.mongodb.org/core/index-validation");
}
indexSpecs.push_back(spec);
}
}
// Give a chance to be interrupted *before* we drop all indexes.
opCtx->checkForInterrupt();
{
// note that the drop indexes call also invalidates all clientcursors for the namespace,
// which is important and wanted here
WriteUnitOfWork wunit(opCtx);
log() << "compact dropping indexes";
indexCatalog->dropAllIndexes(opCtx, true);
wunit.commit();
}
CompactStats stats;
MultiIndexBlock indexer;
indexer.ignoreUniqueConstraint(); // in compact we should be doing no checking
// The 'indexer' could throw, so ensure build cleanup occurs.
ON_BLOCK_EXIT([&] { indexer.cleanUpAfterBuild(opCtx, collection); });
Status status =
indexer.init(opCtx, collection, indexSpecs, MultiIndexBlock::kNoopOnInitFn).getStatus();
if (!status.isOK())
return StatusWith<CompactStats>(status);
status = recordStore->compact(opCtx);
if (!status.isOK())
return StatusWith<CompactStats>(status);
log() << "starting index commits";
status = indexer.dumpInsertsFromBulk(opCtx);
if (!status.isOK())
return StatusWith<CompactStats>(status);
{
WriteUnitOfWork wunit(opCtx);
status = indexer.commit(opCtx,
collection,
MultiIndexBlock::kNoopOnCreateEachFn,
MultiIndexBlock::kNoopOnCommitFn);
//.........这里部分代码省略.........
示例15: getGlobalReplicationCoordinator
void SyncSourceFeedback::run() {
Client::initThread("SyncSourceFeedbackThread");
OperationContextImpl txn;
bool positionChanged = false;
bool handshakeNeeded = false;
ReplicationCoordinator* replCoord = getGlobalReplicationCoordinator();
while (!inShutdown()) { // TODO(spencer): Remove once legacy repl coordinator is gone.
{
boost::unique_lock<boost::mutex> lock(_mtx);
while (!_positionChanged && !_handshakeNeeded && !_shutdownSignaled) {
_cond.wait(lock);
}
if (_shutdownSignaled) {
break;
}
positionChanged = _positionChanged;
handshakeNeeded = _handshakeNeeded;
_positionChanged = false;
_handshakeNeeded = false;
}
MemberState state = replCoord->getCurrentMemberState();
if (state.primary() || state.startup()) {
_resetConnection();
continue;
}
const HostAndPort target = BackgroundSync::get()->getSyncTarget();
if (_syncTarget != target) {
_resetConnection();
_syncTarget = target;
}
if (!hasConnection()) {
// fix connection if need be
if (target.empty()) {
sleepmillis(500);
continue;
}
if (!_connect(&txn, target)) {
sleepmillis(500);
continue;
}
handshakeNeeded = true;
}
if (handshakeNeeded) {
positionChanged = true;
if (!replHandshake(&txn)) {
boost::unique_lock<boost::mutex> lock(_mtx);
_handshakeNeeded = true;
continue;
}
}
if (positionChanged) {
Status status = updateUpstream(&txn);
if (!status.isOK()) {
boost::unique_lock<boost::mutex> lock(_mtx);
_positionChanged = true;
if (status == ErrorCodes::NodeNotFound) {
_handshakeNeeded = true;
}
}
}
}
cc().shutdown();
}