use of com.mongodb.CommandResult in project mongo-hadoop by mongodb.
the class BaseHadoopTest method isSharded.
protected boolean isSharded(final MongoClientURI uri) {
final CommandResult isMasterResult = runIsMaster(uri);
final Object msg = isMasterResult.get("msg");
return msg != null && msg.equals("isdbgrid");
}
use of com.mongodb.CommandResult in project mongo-hadoop by mongodb.
the class SampleSplitter method calculateSplits.
@Override
public List<InputSplit> calculateSplits() throws SplitFailedException {
Configuration conf = getConfiguration();
long splitSizeMB = MongoConfigUtil.getSplitSize(conf);
long samplesPerSplit = MongoConfigUtil.getSamplesPerSplit(conf);
DBObject splitKey = MongoConfigUtil.getInputSplitKey(conf);
DBCollection inputCollection = MongoConfigUtil.getInputCollection(conf);
CommandResult result = inputCollection.getDB().command(new BasicDBObject("collstats", inputCollection.getName()));
if (!result.ok()) {
throw new SplitFailedException("Could not execute command 'collstats': " + result.getErrorMessage());
}
int count = result.getInt("count");
int avgObjSize = result.getInt("avgObjSize");
int numDocsPerSplit = (int) Math.floor(splitSizeMB * 1024 * 1024 / avgObjSize);
int numSplits = (int) Math.ceil((double) count / numDocsPerSplit);
int totalSamples = (int) Math.floor(samplesPerSplit * numSplits);
if (count < numDocsPerSplit) {
LOG.warn("Not enough documents for more than one split! Consider " + "setting " + MongoConfigUtil.INPUT_SPLIT_SIZE + " to a " + "lower value.");
InputSplit split = createSplitFromBounds(null, null);
return Collections.singletonList(split);
}
DBObject[] pipeline = { new BasicDBObjectBuilder().push("$sample").add("size", totalSamples).get(), new BasicDBObject("$project", splitKey), new BasicDBObject("$sort", splitKey) };
AggregationOutput aggregationOutput;
try {
aggregationOutput = inputCollection.aggregate(Arrays.asList(pipeline));
} catch (MongoException e) {
throw new SplitFailedException("Failed to aggregate sample documents. Note that this Splitter " + "implementation is incompatible with MongoDB versions " + "prior to 3.2.", e);
}
BasicDBObject previousKey = null;
List<InputSplit> splits = new ArrayList<InputSplit>(numSplits);
int i = 0;
for (DBObject sample : aggregationOutput.results()) {
if (i++ % samplesPerSplit == 0) {
BasicDBObject bdbo = (BasicDBObject) sample;
splits.add(createSplitFromBounds(previousKey, bdbo));
previousKey = bdbo;
}
}
splits.add(createSplitFromBounds(previousKey, null));
if (MongoConfigUtil.isFilterEmptySplitsEnabled(conf)) {
return filterEmptySplits(splits);
}
return splits;
}
use of com.mongodb.CommandResult in project graylog2-server by Graylog2.
the class MongoProbe method createHostInfo.
private static HostInfo createHostInfo(DB adminDb) {
final HostInfo hostInfo;
final CommandResult hostInfoResult = adminDb.command("hostInfo");
if (hostInfoResult.ok()) {
final BasicDBObject systemMap = (BasicDBObject) hostInfoResult.get("system");
final HostInfo.System system = HostInfo.System.create(new DateTime(systemMap.getDate("currentTime")), systemMap.getString("hostname"), systemMap.getInt("cpuAddrSize"), systemMap.getLong("memSizeMB"), systemMap.getInt("numCores"), systemMap.getString("cpuArch"), systemMap.getBoolean("numaEnabled"));
final BasicDBObject osMap = (BasicDBObject) hostInfoResult.get("os");
final HostInfo.Os os = HostInfo.Os.create(osMap.getString("type"), osMap.getString("name"), osMap.getString("version"));
final BasicDBObject extraMap = (BasicDBObject) hostInfoResult.get("extra");
final HostInfo.Extra extra = HostInfo.Extra.create(extraMap.getString("versionString"), extraMap.getString("libcVersion"), extraMap.getString("kernelVersion"), extraMap.getString("cpuFrequencyMHz"), extraMap.getString("cpuFeatures"), extraMap.getString("scheduler"), extraMap.getLong("pageSize", -1L), extraMap.getLong("numPages", -1L), extraMap.getLong("maxOpenFiles", -1L));
hostInfo = HostInfo.create(system, os, extra);
} else {
LOG.debug("Couldn't retrieve MongoDB hostInfo: {}", hostInfoResult.getErrorMessage());
hostInfo = null;
}
return hostInfo;
}
use of com.mongodb.CommandResult in project graylog2-server by Graylog2.
the class MongoProbe method mongoStats.
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()));
}
final DatabaseStats dbStats;
final CommandResult dbStatsResult = db.command("dbStats");
if (dbStatsResult.ok()) {
final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList");
final DatabaseStats.ExtentFreeList extentFreeList;
if (extentFreeListMap == null) {
extentFreeList = null;
} else {
extentFreeList = DatabaseStats.ExtentFreeList.create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize"));
}
final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion");
final DatabaseStats.DataFileVersion dataFileVersion;
if (dataFileVersionMap == null) {
dataFileVersion = null;
} else {
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.containsField("numExtents") ? dbStatsResult.getLong("numExtents") : null, dbStatsResult.getLong("indexes"), dbStatsResult.getLong("indexSize"), dbStatsResult.containsField("fileSize") ? dbStatsResult.getLong("fileSize") : null, dbStatsResult.containsField("nsSizeMB") ? dbStatsResult.getLong("nsSizeMB") : null, extentFreeList, dataFileVersion);
} else {
LOG.debug("Couldn't retrieve MongoDB dbStats: {}", dbStatsResult.getErrorMessage());
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.containsField("totalCreated") ? connectionsMap.getLong("totalCreated") : null);
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", -1), memoryMap.getInt("mappedWithJournal", -1));
final BasicDBObject storageEngineMap = (BasicDBObject) serverStatusResult.get("storageEngine");
final ServerStatus.StorageEngine storageEngine;
if (storageEngineMap == null) {
storageEngine = ServerStatus.StorageEngine.DEFAULT;
} else {
storageEngine = ServerStatus.StorageEngine.create(storageEngineMap.getString("name"));
}
final int uptime = serverStatusResult.getInt("uptime", 0);
serverStatus = ServerStatus.create(serverStatusResult.getString("host"), serverStatusResult.getString("version"), serverStatusResult.getString("process"), serverStatusResult.getLong("pid", 0), uptime, serverStatusResult.getLong("uptimeMillis", uptime * 1000L), serverStatusResult.getInt("uptimeEstimate"), new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory, storageEngine);
} else {
LOG.debug("Couldn't retrieve MongoDB serverStatus: {}", serverStatusResult.getErrorMessage());
serverStatus = null;
}
// TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/
return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats);
}
use of com.mongodb.CommandResult in project commons by craftercms.
the class MongoScriptRunner method runScript.
private void runScript(DB db, Resource scriptPath) throws MongoDataException {
String script;
try {
if (scriptPath.getFile().isDirectory()) {
final File[] files = scriptPath.getFile().listFiles(new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return name.toLowerCase().endsWith(".js");
}
});
List<File> orderFiles = Arrays.asList(files);
Collections.sort(orderFiles, new Comparator<File>() {
@Override
public int compare(final File o1, final File o2) {
return o1.getName().compareTo(o2.getName());
}
});
logger.debug("Directory {} files to exec {}", scriptPath.getFile(), orderFiles);
for (File file : orderFiles) {
runScript(db, new FileSystemResource(file.getPath()));
}
} else {
logger.debug("Running Script {}", scriptPath.getURI());
try {
script = IOUtils.toString(scriptPath.getInputStream(), "UTF-8");
} catch (IOException e) {
throw new MongoDataException("Unable to read script at " + scriptPath.getURI().toString());
}
CommandResult result = db.doEval(script);
if (!result.ok()) {
Exception ex = result.getException();
throw new MongoDataException("An error occurred while running script at " + scriptPath.getURI().toString(), ex);
}
logger.info("Mongo script at {} executed successfully", scriptPath.getDescription());
}
} catch (IOException ex) {
logger.error("Unable to read files from {}", ex);
}
}
Aggregations