void ReplSetImpl::_getOplogDiagsAsHtml(unsigned server_id, stringstream& ss) const { const Member *m = findById(server_id); if( m == 0 ) { ss << "Error : can't find a member with id: " << server_id << '\n'; return; } ss << p("Server : " + m->fullName() + "<br>ns : " + rsoplog ); //const bo fields = BSON( "o" << false << "o2" << false ); const bo fields; /** todo fix we might want an so timeout here */ OplogReader reader; if (reader.connect(m->h()) == false) { ss << "couldn't connect to " << m->fullName(); return; } reader.query(rsoplog, Query().sort("$natural",1), 20, 0, &fields); if ( !reader.haveCursor() ) { ss << "couldn't query " << rsoplog; return; } static const char *h[] = {"ts","optime","h","op","ns","rest",0}; ss << "<style type=\"text/css\" media=\"screen\">" "table { font-size:75% }\n" // "th { background-color:#bbb; color:#000 }\n" // "td,th { padding:.25em }\n" "</style>\n"; ss << table(h, true); //ss << "<pre>\n"; int n = 0; OpTime otFirst; OpTime otLast; OpTime otEnd; while( reader.more() ) { bo o = reader.next(); otLast = o["ts"]._opTime(); if( otFirst.isNull() ) otFirst = otLast; say(ss, o); n++; } if( n == 0 ) { ss << rsoplog << " is empty\n"; } else { reader.query(rsoplog, Query().sort("$natural",-1), 20, 0, &fields); if( !reader.haveCursor() ) { ss << "couldn't query [2] " << rsoplog; return; } string x; bo o = reader.next(); otEnd = o["ts"]._opTime(); while( 1 ) { stringstream z; if( o["ts"]._opTime() == otLast ) break; say(z, o); x = z.str() + x; if( !reader.more() ) break; o = reader.next(); } if( !x.empty() ) { ss << "<tr><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>\n" << x; //ss << "\n...\n\n" << x; } } ss << _table(); ss << p(time_t_to_String_short(time(0)) + " current time"); if( !otEnd.isNull() ) { ss << "<p>Log length in time: "; unsigned d = otEnd.getSecs() - otFirst.getSecs(); double h = d / 3600.0; ss.precision(3); if( h < 72 ) ss << h << " hours"; else ss << h / 24.0 << " days"; ss << "</p>\n"; } }
/* initial oplog application, during initial sync, after cloning. @return false on failure. this method returns an error and doesn't throw exceptions (i think). */ bool ReplSetImpl::initialSyncOplogApplication( const Member *source, OpTime applyGTE, OpTime minValid) { if( source == 0 ) return false; const string hn = source->h().toString(); OpTime ts; try { OplogReader r; if( !r.connect(hn) ) { log() << "replSet initial sync error can't connect to " << hn << " to read " << rsoplog << rsLog; return false; } { BSONObjBuilder q; q.appendDate("$gte", applyGTE.asDate()); BSONObjBuilder query; query.append("ts", q.done()); BSONObj queryObj = query.done(); r.query(rsoplog, queryObj); } assert( r.haveCursor() ); /* we lock outside the loop to avoid the overhead of locking on every operation. server isn't usable yet anyway! */ writelock lk(""); { if( !r.more() ) { sethbmsg("replSet initial sync error reading remote oplog"); log() << "replSet initial sync error remote oplog (" << rsoplog << ") on host " << hn << " is empty?" << rsLog; return false; } bo op = r.next(); OpTime t = op["ts"]._opTime(); r.putBack(op); if( op.firstElement().fieldName() == string("$err") ) { log() << "replSet initial sync error querying " << rsoplog << " on " << hn << " : " << op.toString() << rsLog; return false; } uassert( 13508 , str::stream() << "no 'ts' in first op in oplog: " << op , !t.isNull() ); if( t > applyGTE ) { sethbmsg(str::stream() << "error " << hn << " oplog wrapped during initial sync"); log() << "replSet initial sync expected first optime of " << applyGTE << rsLog; log() << "replSet initial sync but received a first optime of " << t << " from " << hn << rsLog; return false; } } // todo : use exhaust unsigned long long n = 0; while( 1 ) { if( !r.more() ) break; BSONObj o = r.nextSafe(); /* note we might get "not master" at some point */ { ts = o["ts"]._opTime(); /* if we have become primary, we dont' want to apply things from elsewhere anymore. assumePrimary is in the db lock so we are safe as long as we check after we locked above. */ if( (source->state() != MemberState::RS_PRIMARY && source->state() != MemberState::RS_SECONDARY) || replSetForceInitialSyncFailure ) { int f = replSetForceInitialSyncFailure; if( f > 0 ) { replSetForceInitialSyncFailure = f-1; log() << "replSet test code invoked, replSetForceInitialSyncFailure" << rsLog; throw DBException("forced error",0); } log() << "replSet we are now primary" << rsLog; throw DBException("primary changed",0); } if( ts >= applyGTE ) { // optimes before we started copying need not be applied. syncApply(o); } _logOpObjRS(o); /* with repl sets we write the ops to our oplog too */ } if( ++n % 100000 == 0 ) { // simple progress metering log() << "replSet initialSyncOplogApplication " << n << rsLog; } } } catch(DBException& e) { if( ts <= minValid ) { // didn't make it far enough log() << "replSet initial sync failing, error applying oplog " << e.toString() << rsLog; return false; } } return true; }