本文整理汇总了C++中BatchedCommandRequest类的典型用法代码示例。如果您正苦于以下问题:C++ BatchedCommandRequest类的具体用法?C++ BatchedCommandRequest怎么用?C++ BatchedCommandRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BatchedCommandRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: runBatchWriteCommandOnConfig
BatchedCommandResponse Shard::runBatchWriteCommandOnConfig(
OperationContext* txn, const BatchedCommandRequest& batchRequest, RetryPolicy retryPolicy) {
invariant(isConfig());
const std::string dbname = batchRequest.getNS().db().toString();
invariant(batchRequest.sizeWriteOps() == 1);
const BSONObj cmdObj = batchRequest.toBSON();
for (int retry = 1; retry <= kOnErrorNumRetries; ++retry) {
auto response = _runCommand(txn,
ReadPreferenceSetting{ReadPreference::PrimaryOnly},
dbname,
kDefaultConfigCommandTimeout,
cmdObj);
BatchedCommandResponse batchResponse;
Status writeStatus =
CommandResponse::processBatchWriteResponse(response.commandResponse, &batchResponse);
if (!writeStatus.isOK() && response.host) {
updateReplSetMonitor(response.host.get(), writeStatus);
}
if (retry < kOnErrorNumRetries && isRetriableError(writeStatus.code(), retryPolicy)) {
LOG(2) << "Batch write command failed with retriable error and will be retried"
<< causedBy(redact(writeStatus));
continue;
}
return batchResponse;
}
MONGO_UNREACHABLE;
}
示例2: checkShardVersion
static bool checkShardVersion(OperationContext* txn,
ShardingState* shardingState,
const BatchedCommandRequest& request,
WriteOpResult* result) {
const NamespaceString nss( request.getTargetingNS() );
txn->lockState()->assertWriteLocked( nss.ns() );
ChunkVersion requestShardVersion =
request.isMetadataSet() && request.getMetadata()->isShardVersionSet() ?
request.getMetadata()->getShardVersion() : ChunkVersion::IGNORED();
if ( shardingState->enabled() ) {
CollectionMetadataPtr metadata = shardingState->getCollectionMetadata( nss.ns() );
if ( !ChunkVersion::isIgnoredVersion( requestShardVersion ) ) {
ChunkVersion shardVersion =
metadata ? metadata->getShardVersion() : ChunkVersion::UNSHARDED();
if ( !requestShardVersion.isWriteCompatibleWith( shardVersion ) ) {
result->setError(new WriteErrorDetail);
buildStaleError(requestShardVersion, shardVersion, result->getError());
return false;
}
}
}
return true;
}
示例3: msgToBatchUpdate
BatchedCommandRequest* msgToBatchUpdate( const Message& updateMsg ) {
// Parsing DbMessage throws
DbMessage dbMsg( updateMsg );
NamespaceString nss( dbMsg.getns() );
int flags = dbMsg.pullInt();
bool upsert = flags & UpdateOption_Upsert;
bool multi = flags & UpdateOption_Multi;
const BSONObj query = dbMsg.nextJsObj();
const BSONObj updateExpr = dbMsg.nextJsObj();
// No exceptions from here on
BatchedUpdateDocument* updateDoc = new BatchedUpdateDocument;
updateDoc->setQuery( query );
updateDoc->setUpdateExpr( updateExpr );
updateDoc->setUpsert( upsert );
updateDoc->setMulti( multi );
BatchedCommandRequest* request =
new BatchedCommandRequest( BatchedCommandRequest::BatchType_Update );
request->setNS( nss.ns() );
request->getUpdateRequest()->addToUpdates( updateDoc );
return request;
}
示例4: checkIndexConstraints
static bool checkIndexConstraints(OperationContext* txn,
ShardingState* shardingState,
const BatchedCommandRequest& request,
WriteOpResult* result) {
const NamespaceString nss( request.getTargetingNS() );
txn->lockState()->assertWriteLocked( nss.ns() );
if ( !request.isUniqueIndexRequest() )
return true;
if ( shardingState->enabled() ) {
CollectionMetadataPtr metadata = shardingState->getCollectionMetadata( nss.ns() );
if ( metadata ) {
if ( !isUniqueIndexCompatible( metadata->getKeyPattern(),
request.getIndexKeyPattern() ) ) {
result->setError(new WriteErrorDetail);
buildUniqueIndexError(metadata->getKeyPattern(),
request.getIndexKeyPattern(),
result->getError());
return false;
}
}
}
return true;
}
示例5: write
void ClusterWriter::write(OperationContext* opCtx,
const BatchedCommandRequest& request,
BatchWriteExecStats* stats,
BatchedCommandResponse* response) {
const NamespaceString& nss = request.getNS();
LastError::Disabled disableLastError(&LastError::get(opCtx->getClient()));
// Config writes and shard writes are done differently
if (nss.db() == NamespaceString::kConfigDb || nss.db() == NamespaceString::kAdminDb) {
Grid::get(opCtx)->catalogClient()->writeConfigServerDirect(opCtx, request, response);
} else {
TargeterStats targeterStats;
{
ChunkManagerTargeter targeter(request.getTargetingNS(), &targeterStats);
Status targetInitStatus = targeter.init(opCtx);
if (!targetInitStatus.isOK()) {
toBatchError({targetInitStatus.code(),
str::stream() << "unable to target"
<< (request.isInsertIndexRequest() ? " index" : "")
<< " write op for collection "
<< request.getTargetingNS().ns()
<< causedBy(targetInitStatus)},
response);
return;
}
BatchWriteExec::executeBatch(opCtx, targeter, request, response, stats);
}
splitIfNeeded(opCtx, request.getNS(), targeterStats);
}
}
示例6: shardWrite
void ClusterWriter::shardWrite( const BatchedCommandRequest& request,
BatchedCommandResponse* response ) {
ChunkManagerTargeter targeter;
Status targetInitStatus = targeter.init( NamespaceString( request.getTargetingNS() ) );
if ( !targetInitStatus.isOK() ) {
warning() << "could not initialize targeter for"
<< ( request.isInsertIndexRequest() ? " index" : "" )
<< " write op in collection " << request.getTargetingNS() << endl;
// Errors will be reported in response if we are unable to target
}
DBClientShardResolver resolver;
DBClientMultiCommand dispatcher;
BatchWriteExec exec( &targeter, &resolver, &dispatcher );
exec.executeBatch( request, response );
if ( _autoSplit )
splitIfNeeded( request.getNS(), *targeter.getStats() );
_stats->setShardStats( exec.releaseStats() );
}
示例7: runBatchWriteCommand
BatchedCommandResponse Shard::runBatchWriteCommand(OperationContext* opCtx,
const Milliseconds maxTimeMS,
const BatchedCommandRequest& batchRequest,
RetryPolicy retryPolicy) {
const std::string dbname = batchRequest.getNS().db().toString();
const BSONObj cmdObj = batchRequest.toBSON();
for (int retry = 1; retry <= kOnErrorNumRetries; ++retry) {
// Note: write commands can only be issued against a primary.
auto swResponse = _runCommand(
opCtx, ReadPreferenceSetting{ReadPreference::PrimaryOnly}, dbname, maxTimeMS, cmdObj);
BatchedCommandResponse batchResponse;
auto writeStatus = CommandResponse::processBatchWriteResponse(swResponse, &batchResponse);
if (retry < kOnErrorNumRetries && isRetriableError(writeStatus.code(), retryPolicy)) {
LOG(2) << "Batch write command to " << getId()
<< " failed with retriable error and will be retried"
<< causedBy(redact(writeStatus));
continue;
}
return batchResponse;
}
MONGO_UNREACHABLE;
}
示例8: msgToBatchInsert
BatchedCommandRequest* msgToBatchInsert( const Message& insertMsg ) {
// Parsing DbMessage throws
DbMessage dbMsg( insertMsg );
NamespaceString nss( dbMsg.getns() );
bool coe = dbMsg.reservedField() & Reserved_InsertOption_ContinueOnError;
vector<BSONObj> docs;
do {
docs.push_back( dbMsg.nextJsObj() );
}
while ( dbMsg.moreJSObjs() );
// Continue-on-error == unordered
bool ordered = !coe;
// No exceptions from here on
BatchedCommandRequest* request =
new BatchedCommandRequest( BatchedCommandRequest::BatchType_Insert );
request->setNS( nss.ns() );
for ( vector<BSONObj>::const_iterator it = docs.begin(); it != docs.end(); ++it ) {
request->getInsertRequest()->addToDocuments( *it );
}
request->setOrdered( ordered );
return request;
}
示例9: safeWriteBatch
void BatchSafeWriter::safeWriteBatch( DBClientBase* conn,
const BatchedCommandRequest& request,
BatchedCommandResponse* response ) {
// N starts at zero, and we add to it for each item
response->setN( 0 );
for ( size_t i = 0; i < request.sizeWriteOps(); ++i ) {
BatchItemRef itemRef( &request, static_cast<int>( i ) );
LastError lastError;
_safeWriter->safeWrite( conn, itemRef, &lastError );
// Register the error if we need to
BatchedErrorDetail* batchError = lastErrorToBatchError( lastError );
if ( batchError ) {
batchError->setIndex( i );
response->addToErrDetails( batchError );
}
response->setN( response->getN() + lastError.nObjects );
if ( !lastError.upsertedId.isEmpty() ) {
BatchedUpsertDetail* upsertedId = new BatchedUpsertDetail;
upsertedId->setIndex( i );
upsertedId->setUpsertedID( lastError.upsertedId );
response->addToUpsertDetails( upsertedId );
}
// Break on first error if we're ordered
if ( request.getOrdered() && BatchSafeWriter::isFailedOp( lastError ) ) break;
}
if ( request.sizeWriteOps() == 1 && response->isErrDetailsSet()
&& !response->isErrCodeSet() ) {
// Promote single error to batch error
const BatchedErrorDetail* error = response->getErrDetailsAt( 0 );
response->setErrCode( error->getErrCode() );
if ( error->isErrInfoSet() ) response->setErrInfo( error->getErrInfo() );
response->setErrMessage( error->getErrMessage() );
response->unsetErrDetails();
}
if ( request.sizeWriteOps() == 1 && response->isUpsertDetailsSet() ) {
// Promote single upsert to batch upsert
const BatchedUpsertDetail* upsertedId = response->getUpsertDetailsAt( 0 );
response->setSingleUpserted( upsertedId->getUpsertedID() );
response->unsetUpsertDetails();
}
response->setOk( !response->isErrCodeSet() );
dassert( response->isValid( NULL ) );
}
示例10: normalizeInserts
// Goes over the request and preprocesses normalized versions of all the inserts in the request
static void normalizeInserts( const BatchedCommandRequest& request,
vector<StatusWith<BSONObj> >* normalInserts ) {
for ( size_t i = 0; i < request.sizeWriteOps(); ++i ) {
StatusWith<BSONObj> normalInsert = normalizeInsert( BatchItemRef( &request, i ) );
normalInserts->push_back( normalInsert );
if ( request.getOrdered() && !normalInsert.isOK() )
break;
}
}
示例11: execInserts
void WriteBatchExecutor::bulkExecute( const BatchedCommandRequest& request,
std::vector<BatchedUpsertDetail*>* upsertedIds,
std::vector<WriteErrorDetail*>* errors ) {
if ( request.getBatchType() == BatchedCommandRequest::BatchType_Insert ) {
execInserts( request, errors );
}
else if ( request.getBatchType() == BatchedCommandRequest::BatchType_Update ) {
for ( size_t i = 0; i < request.sizeWriteOps(); i++ ) {
WriteErrorDetail* error = NULL;
BSONObj upsertedId;
execUpdate( BatchItemRef( &request, i ), &upsertedId, &error );
if ( !upsertedId.isEmpty() ) {
BatchedUpsertDetail* batchUpsertedId = new BatchedUpsertDetail;
batchUpsertedId->setIndex( i );
batchUpsertedId->setUpsertedID( upsertedId );
upsertedIds->push_back( batchUpsertedId );
}
if ( error ) {
errors->push_back( error );
if ( request.getOrdered() )
break;
}
}
}
else {
dassert( request.getBatchType() == BatchedCommandRequest::BatchType_Delete );
for ( size_t i = 0; i < request.sizeWriteOps(); i++ ) {
WriteErrorDetail* error = NULL;
execRemove( BatchItemRef( &request, i ), &error );
if ( error ) {
errors->push_back( error );
if ( request.getOrdered() )
break;
}
}
}
// Fill in stale version errors for unordered batches (update/delete can't do this on own)
if ( !errors->empty() && !request.getOrdered() ) {
const WriteErrorDetail* finalError = errors->back();
if ( finalError->getErrCode() == ErrorCodes::StaleShardVersion ) {
for ( size_t i = finalError->getIndex() + 1; i < request.sizeWriteOps(); i++ ) {
WriteErrorDetail* dupStaleError = new WriteErrorDetail;
finalError->cloneTo( dupStaleError );
errors->push_back( dupStaleError );
}
}
}
}
示例12: containsNoIDUpsert
bool BatchedCommandRequest::containsNoIDUpsert(const BatchedCommandRequest& request) {
if (request.getBatchType() != BatchedCommandRequest::BatchType_Update)
return false;
const vector<BatchedUpdateDocument*>& updates = request.getUpdateRequest()->getUpdates();
for (vector<BatchedUpdateDocument*>::const_iterator it = updates.begin(); it != updates.end();
++it) {
const BatchedUpdateDocument* updateDoc = *it;
if (updateDoc->getUpsert() && updateDoc->getQuery()["_id"].eoo())
return true;
}
return false;
}
示例13: dassert
void Strategy::writeOp(OperationContext* txn, int op, Request& request) {
// make sure we have a last error
dassert(&LastError::get(cc()));
OwnedPointerVector<BatchedCommandRequest> commandRequestsOwned;
vector<BatchedCommandRequest*>& commandRequests = commandRequestsOwned.mutableVector();
msgToBatchRequests(request.m(), &commandRequests);
for (vector<BatchedCommandRequest*>::iterator it = commandRequests.begin();
it != commandRequests.end();
++it) {
// Multiple commands registered to last error as multiple requests
if (it != commandRequests.begin())
LastError::get(cc()).startRequest();
BatchedCommandRequest* commandRequest = *it;
// Adjust namespaces for command
NamespaceString fullNS(commandRequest->getNS());
string cmdNS = fullNS.getCommandNS();
// We only pass in collection name to command
commandRequest->setNS(fullNS);
BSONObjBuilder builder;
BSONObj requestBSON = commandRequest->toBSON();
{
// Disable the last error object for the duration of the write cmd
LastError::Disabled disableLastError(&LastError::get(cc()));
Command::runAgainstRegistered(txn, cmdNS.c_str(), requestBSON, builder, 0);
}
BatchedCommandResponse commandResponse;
bool parsed = commandResponse.parseBSON(builder.done(), NULL);
(void)parsed; // for compile
dassert(parsed && commandResponse.isValid(NULL));
// Populate the lastError object based on the write response
LastError::get(cc()).reset();
bool hadError =
batchErrorToLastError(*commandRequest, commandResponse, &LastError::get(cc()));
// Check if this is an ordered batch and we had an error which should stop processing
if (commandRequest->getOrdered() && hadError)
break;
}
}
示例14: msgToBatchInserts
void msgToBatchInserts( const Message& insertMsg,
vector<BatchedCommandRequest*>* insertRequests ) {
// Parsing DbMessage throws
DbMessage dbMsg( insertMsg );
NamespaceString nss( dbMsg.getns() );
// Continue-on-error == unordered
bool coe = dbMsg.reservedField() & Reserved_InsertOption_ContinueOnError;
bool ordered = !coe;
while ( insertRequests->empty() || dbMsg.moreJSObjs() ) {
// Collect docs for next batch, but don't exceed maximum size
int totalInsertSize = 0;
vector<BSONObj> docs;
do {
const char* prevObjMark = dbMsg.markGet();
BSONObj nextObj = dbMsg.nextJsObj();
if ( totalInsertSize + nextObj.objsize() <= BSONObjMaxUserSize ) {
docs.push_back( nextObj );
totalInsertSize += docs.back().objsize();
}
else {
// Size limit exceeded, rollback to previous insert position
dbMsg.markReset( prevObjMark );
break;
}
}
while ( docs.size() < BatchedCommandRequest::kMaxWriteBatchSize
&& dbMsg.moreJSObjs() );
dassert( !docs.empty() );
// No exceptions from here on
BatchedCommandRequest* request =
new BatchedCommandRequest( BatchedCommandRequest::BatchType_Insert );
request->setNSS( nss );
for ( vector<BSONObj>::const_iterator it = docs.begin(); it != docs.end(); ++it ) {
request->getInsertRequest()->addToDocuments( *it );
}
request->setOrdered( ordered );
request->setWriteConcern( WriteConcernOptions::Acknowledged );
insertRequests->push_back( request );
}
}
示例15: execInserts
void WriteBatchExecutor::execInserts( const BatchedCommandRequest& request,
std::vector<WriteErrorDetail*>* errors ) {
// Theory of operation:
//
// Instantiates an ExecInsertsState, which represents all of the state involved in the batch
// insert execution algorithm. Most importantly, encapsulates the lock state.
//
// Every iteration of the loop in execInserts() processes one document insertion, by calling
// insertOne() exactly once for a given value of state.currIndex.
//
// If the ExecInsertsState indicates that the requisite write locks are not held, insertOne
// acquires them and performs lock-acquisition-time checks. However, on non-error
// execution, it does not release the locks. Therefore, the yielding logic in the while
// loop in execInserts() is solely responsible for lock release in the non-error case.
//
// Internally, insertOne loops performing the single insert until it completes without a
// PageFaultException, or until it fails with some kind of error. Errors are mostly
// propagated via the request->error field, but DBExceptions or std::exceptions may escape,
// particularly on operation interruption. These kinds of errors necessarily prevent
// further insertOne calls, and stop the batch. As a result, the only expected source of
// such exceptions are interruptions.
ExecInsertsState state(&request);
normalizeInserts(request, &state.normalizedInserts, &state.pregeneratedKeys);
ElapsedTracker elapsedTracker(128, 10); // 128 hits or 10 ms, matching RunnerYieldPolicy's
for (state.currIndex = 0;
state.currIndex < state.request->sizeWriteOps();
++state.currIndex) {
if (elapsedTracker.intervalHasElapsed()) {
// Consider yielding between inserts.
if (state.hasLock()) {
int micros = ClientCursor::suggestYieldMicros();
if (micros > 0) {
state.unlock();
killCurrentOp.checkForInterrupt();
sleepmicros(micros);
}
}
killCurrentOp.checkForInterrupt();
elapsedTracker.resetLastTime();
}
WriteErrorDetail* error = NULL;
execOneInsert(&state, &error);
if (error) {
errors->push_back(error);
error->setIndex(state.currIndex);
if (request.getOrdered())
return;
}
}
}