你是對的,run_command
不支持直接做currentOp
。但是,如果我們看一下在mongo
殼db.currentOp
的實施,我們可以看到它是如何工作的引擎蓋下:
> db.currentOp
function (arg) {
var q = {};
if (arg) {
if (typeof arg == "object") {
Object.extend(q, arg);
} else if (arg) {
q.$all = true;
}
}
return this.$cmd.sys.inprog.findOne(q);
}
因此,我們可以查詢Perl的一側專門收集$cmd.sys.inprog
得到相同的inprog
陣列這將在shell中返回。
use strict;
use warnings;
use MongoDB;
my $db = MongoDB::MongoClient->new->get_database('test');
my $current_op = $db->get_collection('$cmd.sys.inprog')->find_one;
當服務器不鎖定,它會返回一個$current_op
結構看起來是這樣的:
{
'inprog' => [
{
'connectionId' => 53,
'insert' => {},
'active' => bless(do{\(my $o = 0)}, 'boolean'),
'lockStats' => {
'timeAcquiringMicros' => {
'w' => 1,
'r' => 0
},
'timeLockedMicros' => {
'w' => 9,
'r' => 0
}
},
'numYields' => 0,
'locks' => {
'^' => 'w',
'^test' => 'W'
},
'waitingForLock' => $VAR1->{'inprog'}[0]{'active'},
'ns' => 'test.fnoof',
'client' => '127.0.0.1:50186',
'threadId' => '0x105a81000',
'desc' => 'conn53',
'opid' => 7152352,
'op' => 'insert'
}
]
};
期間fsyncLock()
,你會得到一個空數組inprog
但你將有一個有用的info
字段和預期的fsyncLock
布爾值:
{
'info' => 'use db.fsyncUnlock() to terminate the fsync write/snapshot lock',
'fsyncLock' => bless(do{\(my $o = 1)}, 'boolean'), # <--- that's true
'inprog' => []
};
所以,把他們放在一起,我們得到:
use strict;
use warnings;
use MongoDB;
my $db = MongoDB::MongoClient->new->get_database('fnarf');
my $current_op = $db->get_collection('$cmd.sys.inprog')->find_one;
if ($current_op->{fsyncLock}) {
print "fsync lock is currently ON\n";
} else {
print "fsync lock is currently OFF\n";
}
嗯,這是一個令人驚訝的準確的答案,工程就像一個魅力。非常感謝! –