2010-06-11 23:16:58 +02:00
|
|
|
// d_writeback.cpp
|
2010-06-11 21:12:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-06-11 23:37:24 +02:00
|
|
|
#include "pch.h"
|
2010-06-11 21:12:25 +02:00
|
|
|
|
|
|
|
#include "../db/commands.h"
|
|
|
|
#include "../util/queue.h"
|
2011-06-27 00:11:45 +02:00
|
|
|
#include "../util/net/listen.h"
|
2010-06-11 21:12:25 +02:00
|
|
|
|
2010-09-22 17:16:44 +02:00
|
|
|
#include "d_writeback.h"
|
2010-06-11 21:12:25 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
2010-09-22 17:16:44 +02:00
|
|
|
// ---------- WriteBackManager class ----------
|
|
|
|
|
|
|
|
// TODO init at mongod startup
|
|
|
|
WriteBackManager writeBackManager;
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
WriteBackManager::WriteBackManager() : _writebackQueueLock("sharding:writebackQueueLock") {
|
2010-09-22 17:16:44 +02:00
|
|
|
}
|
2010-06-11 21:12:25 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
WriteBackManager::~WriteBackManager() {
|
2010-09-22 17:16:44 +02:00
|
|
|
}
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
void WriteBackManager::queueWriteBack( const string& remote , const BSONObj& o ) {
|
2011-05-16 22:53:05 +02:00
|
|
|
getWritebackQueue( remote )->queue.push( o );
|
2010-09-22 17:16:44 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 22:53:05 +02:00
|
|
|
shared_ptr<WriteBackManager::QueueInfo> WriteBackManager::getWritebackQueue( const string& remote ) {
|
2010-09-22 17:16:44 +02:00
|
|
|
scoped_lock lk ( _writebackQueueLock );
|
2011-05-16 22:53:05 +02:00
|
|
|
shared_ptr<QueueInfo>& q = _writebackQueues[remote];
|
2010-06-11 21:12:25 +02:00
|
|
|
if ( ! q )
|
2011-05-16 22:53:05 +02:00
|
|
|
q.reset( new QueueInfo() );
|
|
|
|
q->lastCall = Listener::getElapsedTimeMillis();
|
2010-06-11 21:12:25 +02:00
|
|
|
return q;
|
2011-01-04 06:40:41 +01:00
|
|
|
}
|
2010-09-22 17:16:44 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
bool WriteBackManager::queuesEmpty() const {
|
2010-09-22 17:16:44 +02:00
|
|
|
scoped_lock lk( _writebackQueueLock );
|
2011-01-04 06:40:41 +01:00
|
|
|
for ( WriteBackQueuesMap::const_iterator it = _writebackQueues.begin(); it != _writebackQueues.end(); ++it ) {
|
2011-05-16 22:53:05 +02:00
|
|
|
const shared_ptr<QueueInfo> queue = it->second;
|
|
|
|
if (! queue->queue.empty() ) {
|
2010-09-22 17:16:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2010-06-11 21:12:25 +02:00
|
|
|
}
|
2010-09-22 17:16:44 +02:00
|
|
|
|
2011-05-16 22:53:05 +02:00
|
|
|
void WriteBackManager::appendStats( BSONObjBuilder& b ) const {
|
|
|
|
BSONObjBuilder sub;
|
|
|
|
long long totalQueued = 0;
|
|
|
|
long long now = Listener::getElapsedTimeMillis();
|
|
|
|
{
|
|
|
|
scoped_lock lk( _writebackQueueLock );
|
|
|
|
for ( WriteBackQueuesMap::const_iterator it = _writebackQueues.begin(); it != _writebackQueues.end(); ++it ) {
|
|
|
|
const shared_ptr<QueueInfo> queue = it->second;
|
|
|
|
|
|
|
|
BSONObjBuilder t( sub.subobjStart( it->first ) );
|
|
|
|
t.appendNumber( "n" , queue->queue.size() );
|
|
|
|
t.appendNumber( "minutesSinceLastCall" , ( now - queue->lastCall ) / ( 1000 * 60 ) );
|
|
|
|
t.done();
|
|
|
|
|
|
|
|
totalQueued += queue->queue.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.appendBool( "hasOpsQueued" , totalQueued > 0 );
|
|
|
|
b.appendNumber( "totalOpsQueued" , totalQueued );
|
|
|
|
b.append( "queues" , sub.obj() );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteBackManager::cleanupOldQueues() {
|
|
|
|
long long now = Listener::getElapsedTimeMillis();
|
|
|
|
|
|
|
|
scoped_lock lk( _writebackQueueLock );
|
|
|
|
for ( WriteBackQueuesMap::iterator it = _writebackQueues.begin(); it != _writebackQueues.end(); ++it ) {
|
|
|
|
const shared_ptr<QueueInfo> queue = it->second;
|
|
|
|
long long sinceMinutes = ( now - queue->lastCall ) / ( 1000 * 60 );
|
|
|
|
|
|
|
|
if ( sinceMinutes < 60 ) // minutes of inactivity.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
log() << "deleting queue from: " << it->first
|
|
|
|
<< " of size: " << queue->queue.size()
|
|
|
|
<< " after " << sinceMinutes << " inactivity"
|
|
|
|
<< " (normal if any mongos has restarted)"
|
|
|
|
<< endl;
|
|
|
|
|
|
|
|
_writebackQueues.erase( it );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-17 20:22:40 +02:00
|
|
|
void WriteBackManager::Cleaner::taskDoWork() {
|
2011-05-17 17:58:56 +02:00
|
|
|
for ( int i=0; i<1000; i++ ) {
|
|
|
|
if ( ! writeBackManager.cleanupOldQueues() )
|
|
|
|
break;
|
2011-05-16 22:53:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-22 17:16:44 +02:00
|
|
|
// ---------- admin commands ----------
|
2010-06-11 21:12:25 +02:00
|
|
|
|
2010-07-12 18:43:13 +02:00
|
|
|
// Note, this command will block until there is something to WriteBack
|
2010-06-11 21:12:25 +02:00
|
|
|
class WriteBackCommand : public Command {
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
virtual LockType locktype() const { return NONE; }
|
2010-08-16 21:47:40 +02:00
|
|
|
virtual bool slaveOk() const { return true; }
|
2010-06-11 21:12:25 +02:00
|
|
|
virtual bool adminOnly() const { return true; }
|
2011-01-04 06:40:41 +01:00
|
|
|
|
|
|
|
WriteBackCommand() : Command( "writebacklisten" ) {}
|
2010-06-11 21:12:25 +02:00
|
|
|
|
|
|
|
void help(stringstream& h) const { h<<"internal"; }
|
|
|
|
|
2011-07-18 21:23:37 +02:00
|
|
|
bool run(const string& , BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool) {
|
2010-06-11 21:12:25 +02:00
|
|
|
|
|
|
|
BSONElement e = cmdObj.firstElement();
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( e.type() != jstOID ) {
|
2010-06-11 21:12:25 +02:00
|
|
|
errmsg = "need oid as first value";
|
|
|
|
return 0;
|
|
|
|
}
|
2010-09-20 19:43:20 +02:00
|
|
|
|
|
|
|
// get the command issuer's (a mongos) serverID
|
2010-06-11 21:12:25 +02:00
|
|
|
const OID id = e.__oid();
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-09-20 19:20:25 +02:00
|
|
|
// the command issuer is blocked awaiting a response
|
|
|
|
// we want to do return at least at every 5 minutes so sockets don't timeout
|
2010-09-13 17:35:14 +02:00
|
|
|
BSONObj z;
|
2011-05-16 22:53:05 +02:00
|
|
|
if ( writeBackManager.getWritebackQueue(id.str())->queue.blockingPop( z, 5 * 60 /* 5 minutes */ ) ) {
|
2010-09-13 06:51:54 +02:00
|
|
|
log(1) << "WriteBackCommand got : " << z << endl;
|
|
|
|
result.append( "data" , z );
|
|
|
|
}
|
2010-09-13 17:35:14 +02:00
|
|
|
else {
|
2010-09-13 06:51:54 +02:00
|
|
|
result.appendBool( "noop" , true );
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-06-11 21:12:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} writeBackCommand;
|
|
|
|
|
2010-09-20 19:20:25 +02:00
|
|
|
class WriteBacksQueuedCommand : public Command {
|
|
|
|
public:
|
2011-01-04 06:40:41 +01:00
|
|
|
virtual LockType locktype() const { return NONE; }
|
2010-09-20 19:20:25 +02:00
|
|
|
virtual bool slaveOk() const { return true; }
|
|
|
|
virtual bool adminOnly() const { return true; }
|
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
WriteBacksQueuedCommand() : Command( "writeBacksQueued" ) {}
|
|
|
|
|
|
|
|
void help(stringstream& help) const {
|
2010-09-20 19:20:25 +02:00
|
|
|
help << "Returns whether there are operations in the writeback queue at the time the command was called. "
|
2011-05-26 11:59:13 +02:00
|
|
|
<< "This is an internal command";
|
2010-09-20 19:20:25 +02:00
|
|
|
}
|
|
|
|
|
2011-07-18 21:23:37 +02:00
|
|
|
bool run(const string& , BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool) {
|
2011-05-16 22:53:05 +02:00
|
|
|
writeBackManager.appendStats( result );
|
2010-09-20 19:20:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} writeBacksQueuedCommand;
|
2011-05-16 22:53:05 +02:00
|
|
|
|
2010-09-20 19:20:25 +02:00
|
|
|
|
|
|
|
} // namespace mongo
|