List of usage examples for com.mongodb CommandResult getDate
public Date getDate(final String field)
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java
License:Apache License
@Override public long determineServerTimeDifferenceMillis() { // the assumption is that the network delay from this instance // to the server, and from the server back to this instance // are (more or less) equal. // taking this assumption into account allows to remove // the network delays from the picture: the difference // between end and start time is exactly this network // delay (plus some server time, but that's neglected). // so if the clocks are in perfect sync and the above // mentioned assumption holds, then the server time should // be exactly at the midPoint between start and end. // this should allow a more accurate picture of the diff. final long start = System.currentTimeMillis(); // assumption here: server returns UTC - ie the returned // date object is correctly taking care of time zones. final CommandResult serverStatus = db.command("serverStatus"); if (serverStatus == null) { // OAK-4107 / OAK-4515 : extra safety LOG.warn(//from w w w. j av a 2s . c om "determineServerTimeDifferenceMillis: db.serverStatus returned null - cannot determine time difference - assuming 0ms."); return 0; } final Date serverLocalTime = serverStatus.getDate("localTime"); if (serverLocalTime == null) { // OAK-4107 / OAK-4515 : looks like this can happen - at least // has been seen once on mongo 3.0.9 // let's handle this gently and issue a log.warn // instead of throwing a NPE LOG.warn( "determineServerTimeDifferenceMillis: db.serverStatus.localTime returned null - cannot determine time difference - assuming 0ms. " + "(Result details: server exception=" + serverStatus.getException() + ", server error message=" + serverStatus.getErrorMessage() + ")", serverStatus.getException()); return 0; } final long end = System.currentTimeMillis(); final long midPoint = (start + end) / 2; final long serverLocalTimeMillis = serverLocalTime.getTime(); // the difference should be // * positive when local instance is ahead // * and negative when the local instance is behind final long diff = midPoint - serverLocalTimeMillis; return diff; }
From source file:org.graylog2.system.stats.mongo.MongoProbe.java
License:Open Source License
public MongoStats mongoStats() { final List<ServerAddress> serverAddresses = mongoClient.getServerAddressList(); final List<HostAndPort> servers = Lists.newArrayListWithCapacity(serverAddresses.size()); for (ServerAddress serverAddress : serverAddresses) { servers.add(HostAndPort.fromParts(serverAddress.getHost(), serverAddress.getPort())); }// w w w. j a va 2s . com final DatabaseStats dbStats; final CommandResult dbStatsResult = db.command("dbStats"); if (dbStatsResult.ok()) { final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList"); final DatabaseStats.ExtentFreeList extentFreeList = DatabaseStats.ExtentFreeList .create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize")); final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion"); final DatabaseStats.DataFileVersion dataFileVersion = DatabaseStats.DataFileVersion .create(dataFileVersionMap.getInt("major"), dataFileVersionMap.getInt("minor")); dbStats = DatabaseStats.create(dbStatsResult.getString("db"), dbStatsResult.getLong("collections"), dbStatsResult.getLong("objects"), dbStatsResult.getDouble("avgObjSize"), dbStatsResult.getLong("dataSize"), dbStatsResult.getLong("storageSize"), dbStatsResult.getLong("numExtents"), dbStatsResult.getLong("indexes"), dbStatsResult.getLong("indexSize"), dbStatsResult.getLong("fileSize"), dbStatsResult.getLong("nsSizeMB"), extentFreeList, dataFileVersion); } else { dbStats = null; } final ServerStatus serverStatus; final CommandResult serverStatusResult = adminDb.command("serverStatus"); if (serverStatusResult.ok()) { final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections"); final ServerStatus.Connections connections = ServerStatus.Connections.create( connectionsMap.getInt("current"), connectionsMap.getInt("available"), connectionsMap.getLong("totalCreated")); final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network"); final ServerStatus.Network network = ServerStatus.Network.create(networkMap.getInt("bytesIn"), networkMap.getInt("bytesOut"), networkMap.getInt("numRequests")); final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem"); final ServerStatus.Memory memory = ServerStatus.Memory.create(memoryMap.getInt("bits"), memoryMap.getInt("resident"), memoryMap.getInt("virtual"), memoryMap.getBoolean("supported"), memoryMap.getInt("mapped"), memoryMap.getInt("mappedWithJournal")); serverStatus = ServerStatus.create(serverStatusResult.getString("host"), serverStatusResult.getString("version"), serverStatusResult.getString("process"), serverStatusResult.getLong("pid"), serverStatusResult.getInt("uptime"), serverStatusResult.getLong("uptimeMillis"), serverStatusResult.getInt("uptimeEstimate"), new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory); } else { serverStatus = null; } // TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/ return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats); }