本文整理汇总了C++中OperationContextImpl类的典型用法代码示例。如果您正苦于以下问题:C++ OperationContextImpl类的具体用法?C++ OperationContextImpl怎么用?C++ OperationContextImpl使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OperationContextImpl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: lkf
void FSyncLockThread::doRealWork() {
SimpleMutex::scoped_lock lkf(filesLockedFsync);
OperationContextImpl txn;
ScopedTransaction transaction(&txn, MODE_X);
Lock::GlobalWrite global(txn.lockState()); // No WriteUnitOfWork needed
SimpleMutex::scoped_lock lk(fsyncCmd.m);
invariant(!fsyncCmd.locked); // impossible to get here if locked is true
try {
getDur().syncDataAndTruncateJournal(&txn);
}
catch( std::exception& e ) {
error() << "error doing syncDataAndTruncateJournal: " << e.what() << endl;
fsyncCmd.err = e.what();
fsyncCmd._threadSync.notify_one();
fsyncCmd.locked = false;
return;
}
txn.lockState()->downgradeGlobalXtoSForMMAPV1();
try {
StorageEngine* storageEngine = getGlobalEnvironment()->getGlobalStorageEngine();
storageEngine->flushAllFiles(true);
}
catch( std::exception& e ) {
error() << "error doing flushAll: " << e.what() << endl;
fsyncCmd.err = e.what();
fsyncCmd._threadSync.notify_one();
fsyncCmd.locked = false;
return;
}
invariant(!fsyncCmd.locked);
fsyncCmd.locked = true;
fsyncCmd._threadSync.notify_one();
while ( ! fsyncCmd.pendingUnlock ) {
fsyncCmd._unlockSync.wait(fsyncCmd.m);
}
fsyncCmd.pendingUnlock = false;
fsyncCmd.locked = false;
fsyncCmd.err = "unlocked";
fsyncCmd._unlockSync.notify_one();
}
示例2: getInitialSyncFlag
bool getInitialSyncFlag() {
OperationContextImpl txn;
MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN {
ScopedTransaction transaction(&txn, MODE_IX);
Lock::DBLock lk(txn.lockState(), "local", MODE_X);
BSONObj mv;
bool found = Helpers::getSingleton(&txn, minvalidNS, mv);
if (found) {
return mv[initialSyncFlagString].trueValue();
}
return false;
}
MONGO_WRITE_CONFLICT_RETRY_LOOP_END(&txn, "getInitialSyncFlags", minvalidNS);
}
示例3: TEST
TEST(DBHelperTests, FindDiskLocs) {
OperationContextImpl txn;
DBDirectClient client(&txn);
// Some unique tag we can use to make sure we're pulling back the right data
OID tag = OID::gen();
client.remove( ns, BSONObj() );
int numDocsInserted = 10;
for ( int i = 0; i < numDocsInserted; ++i ) {
client.insert( ns, BSON( "_id" << i << "tag" << tag ) );
}
long long maxSizeBytes = 1024 * 1024 * 1024;
set<DiskLoc> locs;
long long numDocsFound;
long long estSizeBytes;
{
// search _id range (0, 10)
Lock::DBRead lk(txn.lockState(), ns);
KeyRange range( ns,
BSON( "_id" << 0 ),
BSON( "_id" << numDocsInserted ),
BSON( "_id" << 1 ) );
Status result = Helpers::getLocsInRange( &txn,
range,
maxSizeBytes,
&locs,
&numDocsFound,
&estSizeBytes );
ASSERT_EQUALS( result, Status::OK() );
ASSERT_EQUALS( numDocsFound, numDocsInserted );
ASSERT_NOT_EQUALS( estSizeBytes, 0 );
ASSERT_LESS_THAN( estSizeBytes, maxSizeBytes );
Database* db = dbHolder().get( &txn, nsToDatabase(range.ns) );
const Collection* collection = db->getCollection(&txn, ns);
// Make sure all the disklocs actually correspond to the right info
for ( set<DiskLoc>::const_iterator it = locs.begin(); it != locs.end(); ++it ) {
const BSONObj obj = collection->docFor(&txn, *it);
ASSERT_EQUALS(obj["tag"].OID(), tag);
}
}
}
示例4: lkf
void FSyncLockThread::doRealWork() {
SimpleMutex::scoped_lock lkf(filesLockedFsync);
OperationContextImpl txn; // XXX?
Lock::GlobalWrite global(txn.lockState());
SimpleMutex::scoped_lock lk(fsyncCmd.m);
verify( ! fsyncCmd.locked ); // impossible to get here if locked is true
try {
getDur().syncDataAndTruncateJournal();
}
catch( std::exception& e ) {
error() << "error doing syncDataAndTruncateJournal: " << e.what() << endl;
fsyncCmd.err = e.what();
fsyncCmd._threadSync.notify_one();
fsyncCmd.locked = false;
return;
}
global.downgrade();
try {
MemoryMappedFile::flushAll(true);
}
catch( std::exception& e ) {
error() << "error doing flushAll: " << e.what() << endl;
fsyncCmd.err = e.what();
fsyncCmd._threadSync.notify_one();
fsyncCmd.locked = false;
return;
}
verify( ! fsyncCmd.locked );
fsyncCmd.locked = true;
fsyncCmd._threadSync.notify_one();
while ( ! fsyncCmd.pendingUnlock ) {
fsyncCmd._unlockSync.wait(fsyncCmd.m);
}
fsyncCmd.pendingUnlock = false;
fsyncCmd.locked = false;
fsyncCmd.err = "unlocked";
fsyncCmd._unlockSync.notify_one();
}
示例5: _dropAllDBs
void ServiceContextMongoDTest::_dropAllDBs() {
OperationContextImpl txn;
dropAllDatabasesExceptLocal(&txn);
ScopedTransaction transaction(&txn, MODE_X);
Lock::GlobalWrite lk(txn.lockState());
AutoGetDb autoDBLocal(&txn, "local", MODE_X);
const auto localDB = autoDBLocal.getDb();
if (localDB) {
MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN {
// Do not wrap in a WriteUnitOfWork until SERVER-17103 is addressed.
autoDBLocal.getDb()->dropDatabase(&txn, localDB);
}
MONGO_WRITE_CONFLICT_RETRY_LOOP_END(&txn, "_dropAllDBs", "local");
}
}
示例6: _logOpRS
/** write an op to the oplog that is already built.
todo : make _logOpRS() call this so we don't repeat ourself?
*/
void _logOpObjRS(const BSONObj& op) {
OperationContextImpl txn;
Lock::DBWrite lk(txn.lockState(), "local");
const OpTime ts = op["ts"]._opTime();
long long h = op["h"].numberLong();
{
if ( localOplogRSCollection == 0 ) {
Client::Context ctx(rsoplog, storageGlobalParams.dbpath);
localDB = ctx.db();
verify( localDB );
localOplogRSCollection = localDB->getCollection( &txn, rsoplog );
massert(13389,
"local.oplog.rs missing. did you drop it? if so restart server",
localOplogRSCollection);
}
Client::Context ctx(rsoplog, localDB);
checkOplogInsert( localOplogRSCollection->insertDocument( &txn, op, false ) );
/* todo: now() has code to handle clock skew. but if the skew server to server is large it will get unhappy.
this code (or code in now() maybe) should be improved.
*/
if( theReplSet ) {
if( !(theReplSet->lastOpTimeWritten<ts) ) {
log() << "replication oplog stream went back in time. previous timestamp: "
<< theReplSet->lastOpTimeWritten << " newest timestamp: " << ts
<< ". attempting to sync directly from primary." << endl;
std::string errmsg;
BSONObjBuilder result;
if (!theReplSet->forceSyncFrom(theReplSet->box.getPrimary()->fullName(),
errmsg, result)) {
log() << "Can't sync from primary: " << errmsg << endl;
}
}
theReplSet->lastOpTimeWritten = ts;
theReplSet->lastH = h;
ctx.getClient()->setLastOp( ts );
BackgroundSync::notify();
}
}
setNewOptime(ts);
}
示例7: run
void run() {
const string dbName = "rollback_drop_collection";
const string droppedName = dbName + ".dropped";
const string rolledBackName = dbName + ".rolled_back";
OperationContextImpl txn;
ScopedTransaction transaction(&txn, MODE_IX);
Lock::DBLock lk(txn.lockState(), dbName, MODE_X);
bool justCreated;
Database* db = dbHolder().openDb(&txn, dbName, &justCreated);
ASSERT(justCreated);
{
WriteUnitOfWork wunit(&txn);
ASSERT_FALSE(db->getCollection(droppedName));
Collection* droppedColl;
droppedColl = db->createCollection(&txn, droppedName);
ASSERT_EQUALS(db->getCollection(droppedName), droppedColl);
db->dropCollection(&txn, droppedName);
wunit.commit();
}
// Should have been really dropped
ASSERT_FALSE(db->getCollection(droppedName));
{
WriteUnitOfWork wunit(&txn);
ASSERT_FALSE(db->getCollection(rolledBackName));
Collection* rolledBackColl = db->createCollection(&txn, rolledBackName);
wunit.commit();
ASSERT_EQUALS(db->getCollection(rolledBackName), rolledBackColl);
db->dropCollection(&txn, rolledBackName);
// not committing so dropping should be rolled back
}
// The rolledBackCollection dropping should have been rolled back.
// Original Collection pointers are no longer valid.
ASSERT(db->getCollection(rolledBackName));
// The droppedCollection should not have been restored by the rollback.
ASSERT_FALSE(db->getCollection(droppedName));
}
示例8: logStartup
static void logStartup() {
BSONObjBuilder toLog;
stringstream id;
id << getHostNameCached() << "-" << jsTime().asInt64();
toLog.append("_id", id.str());
toLog.append("hostname", getHostNameCached());
toLog.appendTimeT("startTime", time(0));
toLog.append("startTimeLocal", dateToCtimeString(Date_t::now()));
toLog.append("cmdLine", serverGlobalParams.parsedOpts);
toLog.append("pid", ProcessId::getCurrent().asLongLong());
BSONObjBuilder buildinfo(toLog.subobjStart("buildinfo"));
appendBuildInfo(buildinfo);
appendStorageEngineList(&buildinfo);
buildinfo.doneFast();
BSONObj o = toLog.obj();
OperationContextImpl txn;
ScopedTransaction transaction(&txn, MODE_X);
Lock::GlobalWrite lk(txn.lockState());
AutoGetOrCreateDb autoDb(&txn, "local", mongo::MODE_X);
Database* db = autoDb.getDb();
const std::string ns = "local.startup_log";
Collection* collection = db->getCollection(ns);
WriteUnitOfWork wunit(&txn);
if (!collection) {
BSONObj options = BSON("capped" << true << "size" << 10 * 1024 * 1024);
bool shouldReplicateWrites = txn.writesAreReplicated();
txn.setReplicatedWrites(false);
ON_BLOCK_EXIT(&OperationContext::setReplicatedWrites, &txn, shouldReplicateWrites);
uassertStatusOK(userCreateNS(&txn, db, ns, options));
collection = db->getCollection(ns);
}
invariant(collection);
uassertStatusOK(collection->insertDocument(&txn, o, false).getStatus());
wunit.commit();
}
示例9: applyOpsToOplog
OpTime SyncTail::applyOpsToOplog(std::deque<BSONObj>* ops) {
OpTime lastOpTime;
{
OperationContextImpl txn; // XXX?
Lock::DBLock lk(txn.lockState(), "local", MODE_X);
WriteUnitOfWork wunit(&txn);
while (!ops->empty()) {
const BSONObj& op = ops->front();
// this updates lastOpTimeApplied
lastOpTime = _logOpObjRS(&txn, op);
ops->pop_front();
}
wunit.commit();
}
// Update write concern on primary
BackgroundSync::get()->notify();
return lastOpTime;
}
示例10: applyOpsToOplog
void SyncTail::applyOpsToOplog(std::deque<BSONObj>* ops) {
{
OperationContextImpl txn; // XXX?
Lock::DBWrite lk(txn.lockState(), "local");
while (!ops->empty()) {
const BSONObj& op = ops->front();
// this updates theReplSet->lastOpTimeWritten
_logOpObjRS(op);
ops->pop_front();
}
}
if (BackgroundSync::get()->isAssumingPrimary()) {
LOG(1) << "notifying BackgroundSync";
}
// Update write concern on primary
BackgroundSync::notify();
}
示例11: pretouchN
void pretouchN(vector<BSONObj>& v, unsigned a, unsigned b) {
Client* c = currentClient.get();
if (c == 0) {
Client::initThread("pretouchN");
c = &cc();
}
OperationContextImpl txn; // XXX
ScopedTransaction transaction(&txn, MODE_S);
Lock::GlobalRead lk(txn.lockState());
for (unsigned i = a; i <= b; i++) {
const BSONObj& op = v[i];
const char* which = "o";
const char* opType = op.getStringField("op");
if (*opType == 'i')
;
else if (*opType == 'u')
which = "o2";
else
continue;
/* todo : other operations */
try {
BSONObj o = op.getObjectField(which);
BSONElement _id;
if (o.getObjectID(_id)) {
const char* ns = op.getStringField("ns");
BSONObjBuilder b;
b.append(_id);
BSONObj result;
Client::Context ctx(&txn, ns);
if (Helpers::findById(&txn, ctx.db(), ns, b.done(), result))
_dummy_z += result.objsize(); // touch
}
} catch (DBException& e) {
log() << "ignoring assertion in pretouchN() " << a << ' ' << b << ' ' << i << ' '
<< e.toString() << endl;
}
}
}
示例12: run
void run() {
for ( int i = 0; i < 10; ++i ) {
client.insert( ns, BSON( "_id" << i ) );
}
{
// Remove _id range [_min, _max).
OperationContextImpl txn;
Lock::DBWrite lk(txn.lockState(), ns);
Client::Context ctx( ns );
KeyRange range( ns,
BSON( "_id" << _min ),
BSON( "_id" << _max ),
BSON( "_id" << 1 ) );
Helpers::removeRange( &txn, range );
}
// Check that the expected documents remain.
ASSERT_EQUALS( expected(), docs() );
}
示例13: LOG
void IndexBuilder::run() {
Client::initThread(name().c_str());
LOG(2) << "IndexBuilder building index " << _index;
OperationContextImpl txn;
txn.lockState()->setIsBatchWriter(true);
txn.getClient()->getAuthorizationSession()->grantInternalAuthorization();
txn.getCurOp()->reset(HostAndPort(), dbInsert);
NamespaceString ns(_index["ns"].String());
ScopedTransaction transaction(&txn, MODE_IX);
Lock::DBLock dlk(txn.lockState(), ns.db(), MODE_X);
Client::Context ctx(&txn, ns.getSystemIndexesCollection());
Database* db = dbHolder().get(&txn, ns.db().toString());
Status status = _build(&txn, db, true, &dlk);
if ( !status.isOK() ) {
error() << "IndexBuilder could not build index: " << status.toString();
fassert(28555, ErrorCodes::isInterruption(status.code()));
}
txn.getClient()->shutdown();
}
示例14: multiInitialSyncApply
// This free function is used by the initial sync writer threads to apply each op
void multiInitialSyncApply(const std::vector<BSONObj>& ops, SyncTail* st) {
initializeWriterThread();
OperationContextImpl txn;
txn.setReplicatedWrites(false);
DisableDocumentValidation validationDisabler(&txn);
// allow us to get through the magic barrier
txn.lockState()->setIsBatchWriter(true);
bool convertUpdatesToUpserts = false;
for (std::vector<BSONObj>::const_iterator it = ops.begin(); it != ops.end(); ++it) {
try {
const Status s = SyncTail::syncApply(&txn, *it, convertUpdatesToUpserts);
if (!s.isOK()) {
if (st->shouldRetry(&txn, *it)) {
const Status s2 = SyncTail::syncApply(&txn, *it, convertUpdatesToUpserts);
if (!s2.isOK()) {
severe() << "Error applying operation (" << it->toString() << "): " << s2;
fassertFailedNoTrace(15915);
}
}
// If shouldRetry() returns false, fall through.
// This can happen if the document that was moved and missed by Cloner
// subsequently got deleted and no longer exists on the Sync Target at all
}
} catch (const DBException& e) {
severe() << "writer worker caught exception: " << causedBy(e)
<< " on: " << it->toString();
if (inShutdown()) {
return;
}
fassertFailedNoTrace(16361);
}
}
}
示例15: TEST
TEST(DBHelperTests, FindDiskLocsTooBig) {
DBDirectClient client;
OperationContextImpl txn;
client.remove( ns, BSONObj() );
int numDocsInserted = 10;
for ( int i = 0; i < numDocsInserted; ++i ) {
client.insert( ns, BSON( "_id" << i ) );
}
// Very small max size
long long maxSizeBytes = 10;
set<DiskLoc> locs;
long long numDocsFound;
long long estSizeBytes;
{
Lock::DBRead lk(txn.lockState(), ns);
Client::Context ctx( ns );
KeyRange range( ns,
BSON( "_id" << 0 ),
BSON( "_id" << numDocsInserted ),
BSON( "_id" << 1 ) );
Status result = Helpers::getLocsInRange( &txn,
range,
maxSizeBytes,
&locs,
&numDocsFound,
&estSizeBytes );
// Make sure we get the right error code and our count and size estimates are valid
ASSERT_EQUALS( result.code(), ErrorCodes::InvalidLength );
ASSERT_EQUALS( numDocsFound, numDocsInserted );
ASSERT_GREATER_THAN( estSizeBytes, maxSizeBytes );
}
}