Last active
April 23, 2021 03:54
-
-
Save konstruktoid/bcb9daefab6beca67de833b5f547be91 to your computer and use it in GitHub Desktop.
replacing eval getReplicationInfo with python functions -- https://jira.mongodb.org/browse/PYTHON-1717
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def oplogstats(connection): | |
c = connection | |
localdb = c["local"] | |
if "oplog.rs" in localdb.collection_names(): | |
collstats = dict() | |
collstats.clear() | |
collstats = localdb.command("collstats", "oplog.rs") | |
return collstats | |
else: | |
return None | |
def logsizemb(connection): | |
c = connection | |
localdb = c["local"] | |
if "oplog.rs" in localdb.collection_names(): | |
collstats = dict() | |
collstats.clear() | |
collstats = localdb.command("collstats", "oplog.rs") | |
logsizemb = round((collstats["maxSize"] / (1024 * 1024)), 2) | |
return logsizemb | |
else: | |
return None | |
def usedmb(connection): | |
c = connection | |
localdb = c["local"] | |
if "oplog.rs" in localdb.collection_names(): | |
collstats = dict() | |
collstats.clear() | |
collstats = localdb.command("collstats", "oplog.rs") | |
usedmb = round(((collstats["size"] / (1024 * 1024)) * 100) / 100, 2) | |
return usedmb | |
else: | |
return None | |
def tfirst(connection): | |
c = connection | |
localdb = c["local"] | |
if "oplog.rs" in localdb.collection_names(): | |
oplogcol = localdb["oplog.rs"] | |
firstdoc = oplogcol.find_one(sort=[("$natural", 1)]) | |
return firstdoc["ts"].as_datetime() | |
else: | |
return None | |
def tlast(connection): | |
c = connection | |
localdb = c["local"] | |
if "oplog.rs" in localdb.collection_names(): | |
oplogcol = localdb["oplog.rs"] | |
lastdoc = oplogcol.find_one(sort=[("$natural", -1)]) | |
return lastdoc["ts"].as_datetime() | |
else: | |
return None | |
def timediff(connection): | |
oplogcol = connection.local.oplog.rs | |
tsfirst = oplogcol.find_one(sort=[("$natural", 1)])["ts"] | |
tslast = oplogcol.find_one(sort=[("$natural", -1)])["ts"] | |
timediff = tslast.time - tsfirst.time | |
if isinstance(timediff, int): | |
return timediff | |
else: | |
return None | |
def timediffhours(connection): | |
timediffhours = round(((timediff(connection) / 36) / 100), 2) | |
if isinstance(timediffhours, float): | |
return timediffhours | |
else: | |
return None |
Thanks for the code improvements and answering the question about performance @ShaneHarvey!
I've updated the gist.
Test code:
#!/usr/bin/python3
from pymongo import MongoClient
import getReplicationInfo
connection = MongoClient("mongodb://localhost:27017/")
print("oplogstats: " + str(getReplicationInfo.oplogstats(connection)))
print("logsizemb: " + str(getReplicationInfo.logsizemb(connection)))
print("usedmb: " + str(getReplicationInfo.usedmb(connection)))
print("tfirst: " + str(getReplicationInfo.tfirst(connection)))
print("tlast: " + str(getReplicationInfo.tlast(connection)))
print("timediff: " + str(getReplicationInfo.timediff(connection)))
print("timediffhours: " + str(getReplicationInfo.timediffhours(connection)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat! A couple suggestions:
find_one
instead of adding alimit(1)
and$natural
in the sort instead of"ts"
:With this:
time
attribute which represents the time in seconds since epoch UTC. You can calculate the difference in seconds between two Timestamps without converting them to datetime at all (see https://pymongo.readthedocs.io/en/stable/api/bson/timestamp.html#bson.timestamp.Timestamp):MongoDB will automatically optimize queries on the oplog that sort by
$natural
(both ascending and descending). Thefind_one
(orlimit(1)
) means that the server will only scan a single oplog entry which is fast.