Search in sources :

Example 1 with CommandResult

use of com.mongodb.CommandResult in project morphia by mongodb.

the class DatastoreImpl method process.

void process(final MappedClass mc, final Validation validation) {
    if (validation != null) {
        String collectionName = mc.getCollectionName();
        CommandResult result = getDB().command(new BasicDBObject("collMod", collectionName).append("validator", parse(validation.value())).append("validationLevel", validation.level().getValue()).append("validationAction", validation.action().getValue()));
        if (!result.ok()) {
            if (result.getInt("code") == 26) {
                ValidationOptions options = new ValidationOptions().validator(parse(validation.value())).validationLevel(validation.level()).validationAction(validation.action());
                getDatabase().createCollection(collectionName, new CreateCollectionOptions().validationOptions(options));
            } else {
                result.throwOnError();
            }
        }
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) ValidationOptions(com.mongodb.client.model.ValidationOptions) CommandResult(com.mongodb.CommandResult)

Example 2 with CommandResult

use of com.mongodb.CommandResult in project graylog2-server by Graylog2.

the class MongoConnectionImpl method getMongoVersion.

@Nullable
private Version getMongoVersion(DB adminDb) {
    final CommandResult buildInfoResult = adminDb.command("buildInfo");
    if (buildInfoResult.ok()) {
        final BasicDBList versionArray = (BasicDBList) buildInfoResult.get("versionArray");
        if (versionArray == null || versionArray.size() < 3) {
            LOG.debug("Couldn't retrieve MongoDB version");
            return null;
        }
        final int majorVersion = (int) versionArray.get(0);
        final int minorVersion = (int) versionArray.get(1);
        final int patchVersion = (int) versionArray.get(2);
        return Version.forIntegers(majorVersion, minorVersion, patchVersion);
    } else {
        LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage());
        return null;
    }
}
Also used : BasicDBList(com.mongodb.BasicDBList) CommandResult(com.mongodb.CommandResult) Nullable(javax.annotation.Nullable)

Example 3 with CommandResult

use of com.mongodb.CommandResult in project felix by apache.

the class MongoDBStoreTest method canRunTest.

/**
 * Sets up MongoDB and tries to clear the useradmin collection. When this fails, it is assumed that no MongoDB service is available.
 */
private boolean canRunTest() throws BundleException {
    Bundle mongoBundle = getMongoDBBundle();
    mongoBundle.start();
    Bundle mongoStoreBundle = getMongoDBStoreBundle();
    mongoStoreBundle.start();
    // Provision an empty configuration...
    BundleContext context = mongoStoreBundle.getBundleContext();
    ServiceReference serviceRef = context.getServiceReference(ManagedService.class.getName());
    assertNotNull(serviceRef);
    ManagedService service = (ManagedService) context.getService(serviceRef);
    try {
        service.updated(null);
        Mongo mongo = new Mongo();
        DB db = mongo.getDB("ua_repo");
        DBCollection collection = db.getCollection("useradmin");
        // we always get a collection back, regardless if there is an actual MongoDB listening, hence we should do
        // some actual calls that cause a connection to MongoDB to be created...
        collection.remove(new BasicDBObject(), WriteConcern.SAFE);
        CommandResult lastError = db.getLastError();
        return (lastError.getException() == null && collection.getCount() == 0L);
    } catch (Exception e) {
    // Ignore; apparently, we failed to connect to MongoDB...
    }
    return false;
}
Also used : ManagedService(org.osgi.service.cm.ManagedService) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Mongo(com.mongodb.Mongo) Bundle(org.osgi.framework.Bundle) DB(com.mongodb.DB) BundleException(org.osgi.framework.BundleException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) CommandResult(com.mongodb.CommandResult)

Example 4 with CommandResult

use of com.mongodb.CommandResult in project mongo-hadoop by mongodb.

the class StandaloneMongoSplitter method calculateSplits.

@Override
public List<InputSplit> calculateSplits() throws SplitFailedException {
    final DBObject splitKey = MongoConfigUtil.getInputSplitKey(getConfiguration());
    final DBObject splitKeyMax = MongoConfigUtil.getMaxSplitKey(getConfiguration());
    final DBObject splitKeyMin = MongoConfigUtil.getMinSplitKey(getConfiguration());
    final int splitSize = MongoConfigUtil.getSplitSize(getConfiguration());
    final MongoClientURI inputURI;
    DBCollection inputCollection = null;
    final ArrayList<InputSplit> returnVal;
    try {
        inputURI = MongoConfigUtil.getInputURI(getConfiguration());
        MongoClientURI authURI = MongoConfigUtil.getAuthURI(getConfiguration());
        if (authURI != null) {
            inputCollection = MongoConfigUtil.getCollectionWithAuth(inputURI, authURI);
        } else {
            inputCollection = MongoConfigUtil.getCollection(inputURI);
        }
        returnVal = new ArrayList<InputSplit>();
        final String ns = inputCollection.getFullName();
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Running splitVector on namespace: %s.%s; hosts: %s", inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts()));
        }
        final DBObject cmd = BasicDBObjectBuilder.start("splitVector", ns).add("keyPattern", splitKey).add("min", splitKeyMin).add("max", splitKeyMax).add("force", false).add("maxChunkSize", splitSize).get();
        CommandResult data;
        boolean ok = true;
        try {
            data = inputCollection.getDB().getSisterDB(inputURI.getDatabase()).command(cmd, ReadPreference.primary());
        } catch (final MongoException e) {
            // 2.0 servers throw exceptions rather than info in a CommandResult
            data = null;
            LOG.info(e.getMessage(), e);
            if (e.getMessage().contains("unrecognized command: splitVector")) {
                ok = false;
            } else {
                throw e;
            }
        }
        if (data != null) {
            if (data.containsField("$err")) {
                throw new SplitFailedException("Error calculating splits: " + data);
            } else if (!data.get("ok").equals(1.0)) {
                ok = false;
            }
        }
        if (!ok) {
            final CommandResult stats = inputCollection.getStats();
            if (stats.containsField("primary")) {
                final DBCursor shards = inputCollection.getDB().getSisterDB("config").getCollection("shards").find(new BasicDBObject("_id", stats.getString("primary")));
                try {
                    if (shards.hasNext()) {
                        final DBObject shard = shards.next();
                        final String host = ((String) shard.get("host")).replace(shard.get("_id") + "/", "");
                        final MongoClientURI shardHost;
                        if (authURI != null) {
                            shardHost = new MongoClientURIBuilder(authURI).host(host).build();
                        } else {
                            shardHost = new MongoClientURIBuilder(inputURI).host(host).build();
                        }
                        MongoClient shardClient = null;
                        try {
                            shardClient = new MongoClient(shardHost);
                            data = shardClient.getDB(shardHost.getDatabase()).command(cmd, ReadPreference.primary());
                        } catch (final Exception e) {
                            LOG.error(e.getMessage(), e);
                        } finally {
                            if (shardClient != null) {
                                shardClient.close();
                            }
                        }
                    }
                } finally {
                    shards.close();
                }
            }
            if (data != null && !data.get("ok").equals(1.0)) {
                throw new SplitFailedException("Unable to calculate input splits: " + data.get("errmsg"));
            }
        }
        // Comes in a format where "min" and "max" are implicit
        // and each entry is just a boundary key; not ranged
        final BasicDBList splitData = (BasicDBList) data.get("splitKeys");
        if (splitData.size() == 0) {
            LOG.warn("WARNING: No Input Splits were calculated by the split code. Proceeding with a *single* split. Data may be too" + " small, try lowering 'mongo.input.split_size' if this is undesirable.");
        }
        // Lower boundary of the first min split
        BasicDBObject lastKey = null;
        // If splitKeyMin was given, use it as first boundary.
        if (!splitKeyMin.toMap().isEmpty()) {
            lastKey = new BasicDBObject(splitKeyMin.toMap());
        }
        for (final Object aSplitData : splitData) {
            final BasicDBObject currentKey = (BasicDBObject) aSplitData;
            returnVal.add(createSplitFromBounds(lastKey, currentKey));
            lastKey = currentKey;
        }
        BasicDBObject maxKey = null;
        // If splitKeyMax was given, use it as last boundary.
        if (!splitKeyMax.toMap().isEmpty()) {
            maxKey = new BasicDBObject(splitKeyMax.toMap());
        }
        // Last max split
        final MongoInputSplit lastSplit = createSplitFromBounds(lastKey, maxKey);
        returnVal.add(lastSplit);
    } finally {
        if (inputCollection != null) {
            MongoConfigUtil.close(inputCollection.getDB().getMongo());
        }
    }
    if (MongoConfigUtil.isFilterEmptySplitsEnabled(getConfiguration())) {
        return filterEmptySplits(returnVal);
    }
    return returnVal;
}
Also used : MongoException(com.mongodb.MongoException) MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit) MongoClientURI(com.mongodb.MongoClientURI) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) MongoException(com.mongodb.MongoException) CommandResult(com.mongodb.CommandResult) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoClient(com.mongodb.MongoClient) BasicDBList(com.mongodb.BasicDBList) DBCursor(com.mongodb.DBCursor) MongoClientURIBuilder(com.mongodb.hadoop.util.MongoClientURIBuilder) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) InputSplit(org.apache.hadoop.mapreduce.InputSplit) MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit)

Example 5 with CommandResult

use of com.mongodb.CommandResult in project mongo-hadoop by mongodb.

the class MongoSplitterFactory method getSplitterByStats.

public static MongoCollectionSplitter getSplitterByStats(final MongoClientURI uri, final Configuration config) {
    /* Looks at the collection in mongo.input.uri
         * and choose an implementation based on what's in there.  */
    MongoCollectionSplitter returnVal;
    // big split for the whole collection.
    if (!MongoConfigUtil.createInputSplits(config)) {
        returnVal = new SingleMongoSplitter(config);
    } else {
        MongoClientURI authURI = MongoConfigUtil.getAuthURI(config);
        CommandResult stats;
        DBCollection coll = null;
        CommandResult buildInfo;
        try {
            if (authURI != null) {
                coll = MongoConfigUtil.getCollectionWithAuth(uri, authURI);
                stats = coll.getStats();
                LOG.info("Retrieved Collection stats:" + stats);
            } else {
                coll = MongoConfigUtil.getCollection(uri);
                stats = coll.getStats();
            }
            buildInfo = coll.getDB().command("buildinfo");
        } finally {
            if (coll != null) {
                MongoConfigUtil.close(coll.getDB().getMongo());
            }
        }
        if (!stats.getBoolean("ok", false)) {
            throw new RuntimeException("Unable to calculate input splits from collection stats: " + stats.getString("errmsg"));
        }
        if (!stats.getBoolean("sharded", false)) {
            // Prefer SampleSplitter.
            List versionArray = (List) buildInfo.get("versionArray");
            boolean sampleOperatorSupported = ((Integer) versionArray.get(0) > 3 || ((Integer) versionArray.get(0) == 3 && (Integer) versionArray.get(1) >= 2));
            if (sampleOperatorSupported) {
                returnVal = new SampleSplitter(config);
            } else {
                returnVal = new StandaloneMongoSplitter(config);
            }
        } else {
            // Collection is sharded
            if (MongoConfigUtil.isShardChunkedSplittingEnabled(config)) {
                // Creates one split per chunk.
                returnVal = new ShardChunkMongoSplitter(config);
            } else if (MongoConfigUtil.canReadSplitsFromShards(config)) {
                // Creates one split per shard, but ignores chunk bounds.
                // Reads from shards directly (bypassing mongos).
                // Not usually recommended.
                returnVal = new ShardMongoSplitter(config);
            } else {
                // Not configured to use chunks or shards -
                // so treat this the same as if it was an unsharded collection
                returnVal = new StandaloneMongoSplitter(config);
            }
        }
    }
    return returnVal;
}
Also used : DBCollection(com.mongodb.DBCollection) MongoClientURI(com.mongodb.MongoClientURI) List(java.util.List) CommandResult(com.mongodb.CommandResult)

Aggregations

CommandResult (com.mongodb.CommandResult)16 BasicDBObject (com.mongodb.BasicDBObject)7 DB (com.mongodb.DB)5 DBCollection (com.mongodb.DBCollection)4 DBObject (com.mongodb.DBObject)3 MongoException (com.mongodb.MongoException)3 AggregationOutput (com.mongodb.AggregationOutput)2 BasicDBList (com.mongodb.BasicDBList)2 MongoClientURI (com.mongodb.MongoClientURI)2 File (java.io.File)2 List (java.util.List)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 DateTime (org.joda.time.DateTime)2 HostAndPort (com.google.common.net.HostAndPort)1 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 DBCursor (com.mongodb.DBCursor)1 EmbedMongoDB (com.mongodb.EmbedMongoDB)1 Mongo (com.mongodb.Mongo)1 MongoClient (com.mongodb.MongoClient)1 ServerAddress (com.mongodb.ServerAddress)1